2 * Copyright © 2008, 2017 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 /* Table of common monitor aspect ratios. Add to this as necessary. */
17 static const struct aspect {
28 * Get the xcb screen structure for the specified screen, or NULL if it doesn't
31 static xcb_screen_t *getscreen(xcb_connection_t *c, int screen)
33 xcb_screen_iterator_t iter;
35 iter = xcb_setup_roots_iterator(xcb_get_setup(c));
40 xcb_screen_next(&iter);
46 void find_nearest_aspect(xcb_screen_t *screen, struct aspect *out)
48 unsigned int w = screen->width_in_millimeters;
49 unsigned int h = screen->height_in_millimeters;
51 double r = (double)w/h;
55 for (i = 0; i < (sizeof aspects / sizeof aspects[0]); i++) {
56 double t = (double) aspects[i].num / aspects[i].denom;
67 out->num = aspects[i].denom;
68 out->denom = aspects[i].num;
77 } *parseoptions(int argc, char **argv)
79 static struct options opts = { 0 };
82 for (i = 1; i < argc; i++) {
83 if (strcmp(argv[i], "-display") == 0) {
85 opts.displayname = argv[i];
88 fprintf(stderr, "-display requires an argument\n");
89 } else if (strcmp(argv[i], "-dimensions") == 0) {
90 opts.showdimensions = 1;
97 int main(int argc, char **argv)
99 struct aspect screenaspect;
100 xcb_connection_t *display;
101 xcb_screen_t *screen;
104 struct options *opts = parseoptions(argc, argv);
106 display = xcb_connect(opts->displayname, &screen_num);
107 if (xcb_connection_has_error(display)) {
108 fprintf(stderr, "Failed to open display.\n");
109 xcb_disconnect(display);
113 screen = getscreen(display, screen_num);
115 fprintf(stderr, "Invalid screen number.\n");
116 xcb_disconnect(display);
120 if (opts->showdimensions) {
121 printf("%" PRIu16 "x%" PRIu16 "-",
122 screen->width_in_pixels, screen->height_in_pixels);
125 find_nearest_aspect(screen, &screenaspect);
126 printf("%u:%u\n", screenaspect.num, screenaspect.denom);
128 xcb_disconnect(display);