|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include <X11/Xlib.h> |
| 5 | +#include <X11/extensions/Xinerama.h> |
| 6 | + |
| 7 | +void |
| 8 | +print_text(XineramaScreenInfo *si) { |
| 9 | + if (! si) { |
| 10 | + printf("\n"); |
| 11 | + return; |
| 12 | + } |
| 13 | + |
| 14 | + printf("screen_number: %d\n", si->screen_number); |
| 15 | + printf("\tx_org:\t%d\n", si->x_org); |
| 16 | + printf("\ty_org:\t%d\n", si->y_org); |
| 17 | + printf("\twidth:\t%d\n", si->width); |
| 18 | + printf("\theight:\t%d\n", si->height); |
| 19 | +} |
| 20 | + |
| 21 | +void |
| 22 | +print_csv(XineramaScreenInfo *si) { |
| 23 | + if (! si) |
| 24 | + return; |
| 25 | + |
| 26 | + printf("%d,%d,%d,%d,%d\n", si->screen_number, si->x_org, si->y_org, si->width, si->height); |
| 27 | +} |
| 28 | + |
| 29 | +void |
| 30 | +print_json(XineramaScreenInfo *si) { |
| 31 | + if (! si) { |
| 32 | + printf(",\n"); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + printf(" {\n"); |
| 37 | + printf(" \"screen\": %d,\n", si->screen_number); |
| 38 | + printf(" \"x\": %d,\n", si->x_org); |
| 39 | + printf(" \"y\": %d,\n", si->y_org); |
| 40 | + printf(" \"width\": %d,\n", si->width); |
| 41 | + printf(" \"height\": %d\n", si->height); |
| 42 | + printf(" }"); |
| 43 | +} |
| 44 | + |
| 45 | +enum Format { TEXT, CSV, JSON }; |
| 46 | + |
| 47 | +int |
| 48 | +main(int argc, char **argv) |
| 49 | +{ |
| 50 | + Display *display = XOpenDisplay(NULL); |
| 51 | + |
| 52 | + enum Format format = TEXT; |
| 53 | + void (*print)(XineramaScreenInfo*) = print_text; |
| 54 | + |
| 55 | + if ((argc > 1) && !strcmp(argv[1], "--csv")) { |
| 56 | + print = print_csv; |
| 57 | + format = CSV; |
| 58 | + } |
| 59 | + if ((argc > 1) && !strcmp(argv[1], "--json")) { |
| 60 | + print = print_json; |
| 61 | + format = JSON; |
| 62 | + } |
| 63 | + |
| 64 | + if (format == CSV) |
| 65 | + printf("screen_number,x_org,y_org,width,height\n"); |
| 66 | + if (format == JSON) |
| 67 | + printf("[\n"); |
| 68 | + |
| 69 | + int nscreens = 0; |
| 70 | + XineramaScreenInfo *xisi = XineramaQueryScreens(display, &nscreens); |
| 71 | + for (int i = 0; i < nscreens; i++) { |
| 72 | + if (i) |
| 73 | + print(NULL); |
| 74 | + print(xisi + i); |
| 75 | + } |
| 76 | + XFree(xisi); |
| 77 | + |
| 78 | + if (format == JSON) |
| 79 | + printf("\n]\n"); |
| 80 | + |
| 81 | + XCloseDisplay(display); |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
0 commit comments