我有一个应用程序,我试图修改它,以便工作线程可以告诉窗口使用新数据更新自身。
变量定义如下:
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 不是线程安全的,使用风险自负。