2 * Copyright © 2007 Nick Bowler
4 * License WTFPL2: Do What The Fuck You Want To Public License, version 2.
5 * This is free software: you are free to do what the fuck you want to.
6 * There is NO WARRANTY, to the extent permitted by law.
16 #define MKASPECT(w, h) { (double)w/h, #w ":" #h }
18 /* Table of common monitor aspect ratios. Add to this as necessary. */
19 static struct aspect {
31 * Get the xcb screen structure for the specified screen, or NULL if it doesn't
34 static xcb_screen_t *getscreen(xcb_connection_t *c, int screen)
36 xcb_screen_iterator_t iter;
38 iter = xcb_setup_roots_iterator(xcb_get_setup(c));
43 xcb_screen_next(&iter);
49 char *testaspect(xcb_screen_t *screen)
51 unsigned int w = screen->width_in_millimeters;
52 unsigned int h = screen->height_in_millimeters;
54 double ratio = (double)w/h;
59 for (i = 0; i < (sizeof aspects / sizeof aspects[0]); i++) {
60 if (!best || fabs(aspects[i].ratio - ratio) < diff) {
61 best = aspects[i].name;
62 diff = fabs(aspects[i].ratio - ratio);
72 } *parseoptions(int argc, char **argv)
74 static struct options opts = { 0 };
77 for (i = 1; i < argc; i++) {
78 if (strcmp(argv[i], "-display") == 0) {
80 opts.displayname = argv[i];
83 fprintf(stderr, "-display requires an argument\n");
84 } else if (strcmp(argv[i], "-dimensions") == 0) {
85 opts.showdimensions = 1;
92 int main(int argc, char **argv)
94 xcb_connection_t *display;
98 struct options *opts = parseoptions(argc, argv);
100 display = xcb_connect(opts->displayname, &screen_num);
101 if (xcb_connection_has_error(display)) {
102 fprintf(stderr, "Failed to open display.\n");
103 xcb_disconnect(display);
107 screen = getscreen(display, screen_num);
109 fprintf(stderr, "Invalid screen number.\n");
110 xcb_disconnect(display);
114 if (opts->showdimensions) {
115 printf("%" PRIu16 "x%" PRIu16 "-",
116 screen->width_in_pixels, screen->height_in_pixels);
118 printf("%s\n", testaspect(screen));
120 xcb_disconnect(display);