gpt4 book ai didi

linux - 如何在 Linux 上使用 xlib 监听屏幕分辨率变化

转载 作者:IT王子 更新时间:2023-10-29 00:36:04 26 4
gpt4 key购买 nike

我正在编写一个小 native 例程来通知另一个进程用户已更改屏幕分辨率。我尝试使用 gtk 但它在非复合窗口管理器上不稳定并且经常崩溃。我正在研究 xlib 并有一个示例可以在生成的 X 窗口的大小发生更改时通知我,但我无法弄清楚如何获得屏幕分辨率已更改的通知。任何帮助,将不胜感激。我包括了我的 xlib 测试代码和 gtk+ 测试代码,如前所述,它们在使用非复合窗口管理器时经常崩溃。

这是我使用 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);
}
}

使用 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();

最佳答案

解决方案是映射到默认根窗口。

    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);

关于linux - 如何在 Linux 上使用 xlib 监听屏幕分辨率变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16907491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com