How to query X11 display resolution?

前端 未结 7 869
一整个雨季
一整个雨季 2021-02-05 09:24

It seems like an simple problem, but I can\'t find the answer: How do you query (via X11) what monitors exist and their resolutions?

7条回答
  •  忘了有多久
    2021-02-05 10:00

    The library X11 working only with unix-like OS, so it is a not cross-platform solution.

    A full code

    #include 
    
    #include 
    
    int
    main(const int argc, const char *argv[])
    {
    
        Display *display;
        Screen *screen;
    
        // open a display
        display = XOpenDisplay(NULL);
    
        // return the number of available screens
        int count_screens = ScreenCount(display);
    
        printf("Total count screens: %d\n", count_screens);
    
    
        for (int i = 0; i < count_screens; ++i) {
            screen = ScreenOfDisplay(display, i);
            printf("\tScreen %d: %dX%d\n", i + 1, screen->width, screen->height);
        }
    
        // close the display
        XCloseDisplay(display);
    
       return 0;
    }
    

    A compilation

    gcc -o setup setup.c -std=c11 `pkg-config --cflags --libs x11`
    

    A result (actual for my computer)

    Total count screens: 1
        Screen 1: 1366X768
    

    Based on:

    1. https://tronche.com/gui/x/xlib/display/opening.html
    2. https://tronche.com/gui/x/xlib/display/display-macros.html
    3. https://tronche.com/gui/x/xlib/display/screen-information.html
    4. https://stackoverflow.com/a/1829747/6003870

提交回复
热议问题