gpt4 book ai didi

c++ - 在 X11 窗口上移动窗口事件

转载 作者:行者123 更新时间:2023-11-28 06:07:50 28 4
gpt4 key购买 nike

我目前有 2 个要保持同步的 X11 窗口。一个覆盖透明图形,而另一个显示视频。我目前有一个覆盖图形的窗口始终位于视频之一的顶部,但是我无法确保两个窗口在移动时都位于同一位置。我正在 X11 文档中寻找窗口移动事件,但似乎找不到。

此外,在我的事件句柄循环中,我试图获取其中一个窗口的位置并将另一个窗口移动到该位置。每当调用 XGetWindowAttributes 时都会失败,即使窗口已移动,它也总是返回 x=0 和 y=0。

Window   win;
int nxvisuals = 0;
XVisualInfo visual_template;
XVisualInfo *visual_list;
XVisualInfo vinfo;
Visual *visual;
int depth;
Atom wm_state;
(void)wm_state;

x_display = XOpenDisplay ( NULL ); // open the standard display (the primary screen)

visual_template.screen = DefaultScreen(x_display);
visual_list = XGetVisualInfo(x_display, VisualScreenMask, &visual_template, &nxvisuals);

XMatchVisualInfo(x_display, XDefaultScreen(x_display), 32, TrueColor, &vinfo)

Window parent = XDefaultRootWindow(x_display);
XSync(x_display, True);
visual = vinfo.visual;
depth = vinfo.depth;

XSetWindowAttributes swa;
swa.event_mask = ExposureMask | PointerMotionMask | KeyPressMask;
swa.colormap = XCreateColormap(x_display, XDefaultRootWindow(x_display), visual, AllocNone);
swa.background_pixel = 0;
swa.border_pixel = 0;

win = XCreateWindow ( // create a window with the provided parameters
x_display, parent,
0, 0, 1024, 576, 0,
depth, InputOutput,
visual, CWEventMask | CWBackPixel | CWColormap | CWBorderPixel,
&swa );

XSync(x_display, True);

XSetWindowAttributes xattr;

xattr.override_redirect = False;
XChangeWindowAttributes ( x_display, win, CWOverrideRedirect, &xattr );

XWMHints hints;
hints.input = True;
hints.flags = InputHint;
XSetWMHints(x_display, win, &hints);

XSizeHints *size_hints = XAllocSizeHints();
size_hints->flags = PMinSize | PMaxSize | PSize;
size_hints->min_width = 1024;
size_hints->max_width = 1024;
size_hints->min_height = 576;
size_hints->max_height = 576;
XSetNormalHints(x_display, win, size_hints);
XSetWMSizeHints(x_display,win , size_hints, PSize | PMinSize | PMaxSize);

XMapWindow ( x_display , win ); // make the window visible on the screen
XStoreName ( x_display , win , "OpenGL" ); // give the window a name

/* Second window starts here */
int cnxvisuals = 0;
XVisualInfo cvisual_template;
XVisualInfo *cvisual_list;
XVisualInfo cvinfo;
Visual *cvisual;
int cdepth;

cvisual_template.screen = DefaultScreen(x_display);
cvisual_list = XGetVisualInfo(x_display, VisualScreenMask, &cvisual_template, &cnxvisuals);

XMatchVisualInfo(x_display, XDefaultScreen(x_display), 24, TrueColor, &cvinfo)

Window child = XDefaultRootWindow(x_display);
XSync(x_display, True);
cvisual = cvinfo.visual;
cdepth = cvinfo.depth;

XSetWindowAttributes cswa;
cswa.event_mask = PointerMotionMask | KeyPressMask;
cswa.colormap = XCreateColormap(x_display, XDefaultRootWindow(x_display), cvisual, AllocNone);
cswa.background_pixel = 0;
cswa.border_pixel = 0;

child = XCreateWindow ( // create a window with the provided parameters
x_display, parent,
0, 0, 1024, 576, 0,
cdepth, InputOutput,
cvisual, CWEventMask | CWBackPixel | CWColormap | CWBorderPixel,
&cswa );

XSync(x_display, True);

XSetWindowAttributes xcattr;

xcattr.override_redirect = False;
XChangeWindowAttributes ( x_display, child, CWOverrideRedirect, &xcattr );

XWMHints chints;
chints.input = True;
chints.flags = InputHint;
XSetWMHints(x_display, child, &chints);

XSetNormalHints(x_display, child, size_hints);
XSetWMSizeHints(x_display,child , size_hints, PSize | PMinSize | PMaxSize);

XMapWindow ( x_display , child ); // make the window visible on the screen
XStoreName ( x_display , child , "video" ); // give the window a name

XSelectInput(x_display, child, ExposureMask | FocusChangeMask);

int id = pthread_create(&x11loop, NULL,x11_handle_events,this);

这是我的处理事件调用

void* x11_handle_events(void *void_ptr)
{
Renderer* renderer = static_cast<Renderer*>(void_ptr);
renderer->stop = false;
XEvent event;
XWindowAttributes opengl_attrs;
while(!renderer->stop)
{
XNextEvent(renderer->x_display, &event);
switch(event.type)
{
case Expose:
if (event.xexpose.window == renderer->child)
{
XRaiseWindow(renderer->x_display, renderer->win);
}
break;

case FocusIn:
if (event.xfocus.window == renderer->child)
{
XRaiseWindow(renderer->x_display, renderer->win);
}
break;

}

// Make sure both windows are in the same location
XGetWindowAttributes(renderer->x_display, renderer->child, &opengl_attrs);
XMoveWindow(renderer->x_display, renderer->win, opengl_attrs.x, opengl_attrs.y);
}


pthread_exit(0);
return NULL;
}

最佳答案

您要查找的事件是 ConfigureNotify

http://tronche.com/gui/x/xlib/events/window-state-change/configure.html

The X server can report ConfigureNotify events to clients wanting information about actual changes to a window's state, such as size, position, border, and stacking order. The X server generates this event type whenever one of the following configure window requests made by a client application actually completes:

snip

A window is moved by calling XMoveWindow().

The x and y members are set to the coordinates relative to the parent window's origin and indicate the position of the upper-left outside corner of the window. The width and height members are set to the inside size of the window, not including the border. The border_width member is set to the width of the window's border, in pixels.

事件掩码是 iirc StructureNotifyMask

尽管窗口管理器可能不同意您四处移动...但如果它仍然不起作用,请发表评论,我们会进行更深入的研究。

关于c++ - 在 X11 窗口上移动窗口事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32011083/

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