How to use xlib to listen for screen resolution changes on Linux
Asked Answered
E

1

7

I'm writing a little native routine to notify another process that the user has changed the screen resolution. I tried using gtk but it's unstable on non composite window managers and crashes often. I'm looking into xlib and have a sample working that notifies me when a generated X Window's size is changed but I can't figure out how to get notified that the screen resolution has changed. Any help would be appreciated. I'm including my xlib test code and gtk+ test code which as indicated crashes a lot when using non composite window managers.

Here is my test code using xlib

Display * display;
int screen;
Window root, window;

display = XOpenDisplay (NULL);
if (!display){ syslog(LOG_INFO, "Could not open display.\n"); }
screen = DefaultScreen(display); root = RootWindow(display, screen);

window = XCreateSimpleWindow (display, root,
                              0, 0, 300, 300, // xpos, ypos, width, height
                              0, 0, // border width, border pixel
                              0  /* background */);
// Add StructureNotifyMask to send us events involving resizing of the window, etc.
XSelectInput (display, window, ExposureMask | StructureNotifyMask);
XMapWindow (display, window);
while (1){
    XEvent e;
    XNextEvent (display, &e);

    // Respond to ConfigureNotify, the type of event sent by the
    // server if the window is resized.
    if (e.type == ConfigureNotify){
        XConfigureEvent xce = e.xconfigure;

        /* This event type is generated for a variety of
           happenings, so check whether the window has been
           resized. */
        syslog(LOG_INFO, "New Width,Height %d,%d", xce.width, xce.height);
    }
}

Test code using GTK+

GtkWidget *window;
GdkScreen *screen;

screen = gdk_screen_get_default();
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
syslog(LOG_INFO, "NATIVESUPPORT: startDisplayChangedEventThread(): screen and window objects created.\n");

if (!screen){ syslog(LOG_INFO, "NATIVESUPPORT: screen is null."); }
if (!window){ syslog(LOG_INFO, "NATIVESUPPORT: window is null."); }

g_signal_connect(screen, "size-changed", G_CALLBACK(resize_cb), (gpointer)window);
g_signal_connect(screen, "monitors-changed", G_CALLBACK(resize_cb), (gpointer)window);

syslog(LOG_INFO, "NATIVESUPPORT: startDisplayChangedEventThread(): call back created.\n");

syslog(LOG_INFO, "NATIVESUPPORT: startDisplayChangedEventThread(): starting main.\n");
gtk_main();
Evanescent answered 3/6, 2013 at 23:32 Comment(0)
E
4

The solution was to map to the default root window.

    display = XOpenDisplay (NULL);
    if (!display)
    {
        syslog(LOG_INFO, "Could not open display.\n");
    }
    screen = DefaultScreen (display);
    root = RootWindow (display, screen);

    window = DefaultRootWindow( display );
    if ( 0 > window )
    {
         syslog(LOG_INFO, "Failed to obtain the root windows Id of the default screen of given display.\n");
    }

    Status ret = XGetWindowAttributes( display, window, &xwAttr );
    width = xwAttr.width;
    height = xwAttr.height;

    XSelectInput (display, window, ExposureMask |
                      /* Add StructureNotifyMask to send us events
                         involving resizing of the window, etc. */
                      StructureNotifyMask);

    XMapWindow (display, window);
Evanescent answered 4/6, 2013 at 0:16 Comment(1)
Thanks from the future for this, I needed to figure out how to track window resolution changes as well (to know when to recalculate some screen-WxH-sized buffer images). Now to figure out how XCB does this... :)Keenan

© 2022 - 2024 — McMap. All rights reserved.