如何允许工作线程更新 X11 窗口
How to allow a worker thread to update an X11 Window?
我有一个应用程序,我试图修改它,以便工作线程可以告诉窗口使用新数据更新自身。
变量定义如下:
Display *d;
Window w;
XEvent exppp;
该窗口使用以下代码启动:
XEvent e;
d = XOpenDisplay(NULL);
if (d == NULL)
return 0;
s = DefaultScreen(d);
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 200, 800, 1,
BlackPixel(d, s), WhitePixel(d, s));
XSelectInput(d, w, ExposureMask | KeyPressMask);
XMapWindow(d, w);
while (1) {
XNextEvent(d, &e);
if (e.type == Expose || e.type == KeyPress) {
// redraw routine goes here
}
}
我试图用来让窗口重绘的是一个可以被另一个线程调用的函数:
void graphical_out::redraw()
{
exppp.type = Expose;
XSendEvent(d, w, false, Expose, &exppp);
}
窗口仅在调整大小或收到按键时自行更新。这似乎有点像一个新手问题,但谷歌在这个问题上让我失望了。对我做错了什么有什么建议吗?
- 你对
XSendEvent
的论点是错误的。您需要传递掩码(ExposureMask
),而不是事件类型。 -
exppp.xexpose.window = w;
是必需的(XSendEvent
的窗口参数不是XEvent
结构的窗口)。 - 在发送之前清理事件:
memset(&exppp, 0, sizeof(exppp));
,以防万一。 - Xlib 不是线程安全的,从多个线程调用其函数可能是危险的。
更新 在多线程程序中,需要在这里和那里调用XFlush
(尽管多线程永远不能保证与Xlib一起使用)。
这段代码对我有用:
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <memory.h>
Display *d;
Window w;
int s;
void* tp (void* q)
{
XEvent exppp;
while (1)
{
sleep (3);
printf ("Sending event of type Exposen");
memset(&exppp, 0, sizeof(exppp));
exppp.type = Expose;
exppp.xexpose.window = w;
XSendEvent(d,w,False,ExposureMask,&exppp);
XFlush(d);
}
return NULL;
}
int main ()
{
XEvent e;
pthread_t thread;
d = XOpenDisplay(NULL);
if (d == NULL)
return 0;
s = DefaultScreen(d);
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 200, 800, 1,
BlackPixel(d, s), WhitePixel(d, s));
XSelectInput(d, w, ExposureMask | KeyPressMask);
XMapWindow(d, w);
pthread_create(&thread, NULL, tp, NULL);
while (1) {
XNextEvent(d, &e);
if (e.type == Expose) {
printf ("Got Expose event%sn", e.xexpose.send_event ? " (SendEvent)" : "");
}
else if (e.type == KeyPress) {
printf ("Got KeyPress event%sn", e.xkey.send_event ? " (SendEvent)" : "");
}
}
}
它可能对你有用,也可能爆炸。Xlib 不是线程安全的,使用风险自负。
相关文章:
- X11:移动窗口时获得通知
- 如何使用X11中的XFT字体在窗口中央对齐文本
- X11,更改分辨率并使窗口全屏显示
- C++(LINUX)使用DevIL设置X11窗口背景
- 当在拖动/调整窗口大小的同时对窗口调用isActive()时,返回false.(X11,问题5.4.0)
- 在特定显示器上打开 GL 的 X11 窗口
- X11 窗口在获取事件之前不会刷新
- C++ GNU-Plot 在 x11 窗口中是非交互式的
- 如何允许工作线程更新 X11 窗口
- 将ncurses密钥事件转发到X11窗口
- 如果我想关闭以前打开的窗口,X11窗口ID无效.(C/C++)
- X11窗口不放入QWidget以嵌入到Qt应用程序中
- X11 XMoveWindow与SDL 1.2不移动窗口
- 移动X11窗口上的窗口事件
- 如何使用x11窗口背景加载bmp文件
- 对于X11中的每个窗口,Display都是唯一的
- 获取鼠标在窗口中的相对位置(x11)
- 从GtkWidget获取X11窗口句柄
- X11:获得虚假窗口大小和位置
- X11应用程序的最佳实践—每个应用程序或每个窗口一个XOpenDisplay