如何使用Xlib在Linux上监听屏幕分辨率的变化

6
我正在编写一个小型本地程序,用于通知另一个进程用户已更改了屏幕分辨率。我尝试使用gtk,但它在非组合窗口管理器上不稳定,并经常崩溃。我正在研究xlib,并已经有一个样例能够在生成的X Window大小更改时通知我,但是我无法弄清楚如何得到通知屏幕分辨率已更改。如果有任何帮助,将不胜感激。我包含了我的xlib测试代码和gtk+测试代码,其中提到当使用非组合窗口管理器时会经常崩溃。
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);
    }
}

使用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();
1个回答

4
解决方案是映射到默认根窗口。
    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);

来自未来的感谢,我需要找出如何跟踪窗口分辨率的变化(以便知道何时重新计算一些屏幕宽x高大小的缓冲图像)。现在要弄清楚XCB是如何做到这一点的... :) - i336_

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接