8 #define MKASPECT(w, h) { (double)w/h, #w ":" #h }
10 /* Table of common monitor aspect ratios. Add to this as necessary. */
11 static struct aspect {
23 * Get the xcb screen structure for the specified screen, or NULL if it doesn't
26 static xcb_screen_t *getscreen(xcb_connection_t *c, int screen)
28 xcb_screen_iterator_t iter;
30 iter = xcb_setup_roots_iterator(xcb_get_setup(c));
35 xcb_screen_next(&iter);
41 char *testaspect(xcb_screen_t *screen)
43 unsigned int w = screen->width_in_millimeters;
44 unsigned int h = screen->height_in_millimeters;
46 double ratio = (double)w/h;
51 for (i = 0; i < (sizeof aspects / sizeof aspects[0]); i++) {
52 if (!best || fabs(aspects[i].ratio - ratio) < diff) {
53 best = aspects[i].name;
54 diff = fabs(aspects[i].ratio - ratio);
64 } *parseoptions(int argc, char **argv)
66 static struct options opts = { 0 };
69 for (i = 1; i < argc; i++) {
70 if (strcmp(argv[i], "-display") == 0) {
72 opts.displayname = argv[i];
75 fprintf(stderr, "-display requires an argument\n");
76 } else if (strcmp(argv[i], "-dimensions") == 0) {
77 opts.showdimensions = 1;
84 int main(int argc, char **argv)
86 xcb_connection_t *display;
90 struct options *opts = parseoptions(argc, argv);
92 display = xcb_connect(opts->displayname, &screen_num);
93 if (xcb_connection_has_error(display)) {
94 fprintf(stderr, "Failed to open display.\n");
95 xcb_disconnect(display);
99 screen = getscreen(display, screen_num);
101 fprintf(stderr, "Invalid screen number.\n");
102 xcb_disconnect(display);
106 if (opts->showdimensions) {
107 printf("%" PRIu16 "x%" PRIu16 "-",
108 screen->width_in_pixels, screen->height_in_pixels);
110 printf("%s\n", testaspect(screen));
112 xcb_disconnect(display);