Created
January 24, 2023 23:03
-
-
Save twaik/a82c9ad2fca7f258aec2d130bebdad15 to your computer and use it in GitHub Desktop.
The tool to dump current displays layout the same way it is done by arandr...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdint.h> | |
#include <stdbool.h> | |
#include <assert.h> | |
#define startsWith(a, b) (strstr(a, b) == a) | |
int main() { | |
char chunk[512]; | |
struct output { | |
char name[64]; | |
bool connected; | |
bool active; | |
char mode[32]; | |
char pos[32]; | |
char rotate[9]; | |
} outputs[16] = {0}; | |
FILE * fp = popen("xrandr --verbose", "r"); | |
if (!fp) { | |
perror("popen"); | |
return -1; | |
} | |
size_t i = 0; | |
bool firstmode = true; | |
printf("xrandr"); | |
while (fgets(chunk, sizeof(chunk)-1, fp) != NULL) { | |
if (startsWith(chunk, "Screen ") || startsWith(chunk, "\t")) //Noise | |
continue; | |
else if (startsWith(chunk, " ")) { // Mode, width or height | |
if (!strstr(chunk, "current")) | |
continue; | |
uint16_t width, height; | |
strstr(chunk+2, " ")[0] = '\0'; | |
strcpy(outputs[i].mode, chunk+2); | |
fgets(chunk, sizeof(chunk)-1, fp); | |
sscanf(chunk, " %*c: %*s %hd start", &width); | |
fgets(chunk, sizeof(chunk)-1, fp); | |
sscanf(chunk, " %*c: %*s %hd start", &height); | |
printf(" --output %s --mode %s --pos %s --rotate %s", | |
outputs[i].name, outputs[i].mode, outputs[i].pos, outputs[i].rotate); | |
} else { // Output | |
if (firstmode) firstmode = false; else i++; | |
char * pch = strtok(chunk, " "); | |
strcpy(outputs[i].name, pch); | |
pch = strtok(NULL, " "); | |
if (!strcmp(pch, "connected") || | |
(!strcmp(pch, "unknown") && !strcmp(strtok(NULL, " "), "connection"))) | |
outputs[i].connected = true; | |
pch = strtok(NULL, " "); | |
if (!strcmp(pch, "primary")) | |
pch = strtok(NULL, " "); | |
if (!startsWith(pch, "(")) { | |
uint32_t left, top; | |
outputs[i].active = true; | |
sscanf(pch, "%*ux%*u+%u+%u", &left, &top); | |
sprintf(outputs[i].pos, "%hdx%hd", left, top); | |
strtok(NULL, " "); // Skip modeid | |
strcpy(outputs[i].rotate, strtok(NULL, " ")); | |
} else | |
printf(" --output %s --off", outputs[i].name); | |
} | |
} | |
printf("\n"); | |
pclose(fp); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment