/* * Copyright © 2007 Nick Bowler * * License WTFPL2: Do What The Fuck You Want To Public License, version 2. * This is free software: you are free to do what the fuck you want to. * There is NO WARRANTY, to the extent permitted by law. */ #include #include #include #include #include #include #define MKASPECT(w, h) { (double)w/h, #w ":" #h } /* Table of common monitor aspect ratios. Add to this as necessary. */ static struct aspect { double ratio; char *name; } aspects[] = { MKASPECT(16, 10), MKASPECT(16, 9), MKASPECT(8, 3), MKASPECT(5, 4), MKASPECT(4, 3) }; /* * Get the xcb screen structure for the specified screen, or NULL if it doesn't * exist. */ static xcb_screen_t *getscreen(xcb_connection_t *c, int screen) { xcb_screen_iterator_t iter; iter = xcb_setup_roots_iterator(xcb_get_setup(c)); while (iter.rem) { if (screen == 0) return iter.data; screen--; xcb_screen_next(&iter); } return NULL; } char *testaspect(xcb_screen_t *screen) { unsigned int w = screen->width_in_millimeters; unsigned int h = screen->height_in_millimeters; double ratio = (double)w/h; double diff = 0; char *best = NULL; int i; for (i = 0; i < (sizeof aspects / sizeof aspects[0]); i++) { if (!best || fabs(aspects[i].ratio - ratio) < diff) { best = aspects[i].name; diff = fabs(aspects[i].ratio - ratio); } } return best; } struct options { char *displayname; int showdimensions; } *parseoptions(int argc, char **argv) { static struct options opts = { 0 }; int i; for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-display") == 0) { if (++i < argc) { opts.displayname = argv[i]; continue; } fprintf(stderr, "-display requires an argument\n"); } else if (strcmp(argv[i], "-dimensions") == 0) { opts.showdimensions = 1; } } return &opts; } int main(int argc, char **argv) { xcb_connection_t *display; xcb_screen_t *screen; int screen_num; struct options *opts = parseoptions(argc, argv); display = xcb_connect(opts->displayname, &screen_num); if (xcb_connection_has_error(display)) { fprintf(stderr, "Failed to open display.\n"); xcb_disconnect(display); return EXIT_FAILURE; } screen = getscreen(display, screen_num); if (!screen) { fprintf(stderr, "Invalid screen number.\n"); xcb_disconnect(display); return EXIT_FAILURE; } if (opts->showdimensions) { printf("%" PRIu16 "x%" PRIu16 "-", screen->width_in_pixels, screen->height_in_pixels); } printf("%s\n", testaspect(screen)); xcb_disconnect(display); return EXIT_SUCCESS; }