X11 事件循环:纯 C/Xlib 如何每 3 秒移动文本?
数据小陈
明确结论
不要在 XNextEvent 循环里用 sleep(3) 做定时。纯 Xlib 下,事件循环应同时等待 X 连接 fd 和超时:用 select() 或 poll() 监听 ConnectionNumber(dsp),超时时间设为距离下一次移动的毫秒数;超时后更新 x、y 并重绘。每次重绘用 XClearWindow() + XDrawString() + XFlush()。收到 Expose 时也按当前坐标重绘。
为什么现在的代码不按 3 秒移动
XNextEvent() 会阻塞等待事件。代码在每次处理事件后 sleep(3),这 3 秒内事件循环完全停住:Xlib 不会处理新到的 Expose、ConfigureNotify 等事件,也不会自动替你刷新窗口。文本坐标只在事件处理路径里增加一次,所以看起来要等下一个事件才动。终端里的 22、21、19、12 是 X11 事件类型,分别对应 ConfigureNotify、ReparentNotify、MapNotify、Expose 等。窗口映射初期会产生一串事件;sleep(3) 让这些事件排队,之后连续处理时坐标连续增加,于是出现“突然跳一大段”的现象。
修正思路
- 把绘制逻辑抽成函数:清窗口、画字符串、
XFlush。 - 事件循环先用
XPending()取完所有已排队事件;收到Expose时重绘当前文本。 - 用
select()/poll()等待 X 连接 fd 可读,并设置下一次移动剩余时间。 - 超时后执行
x += 10; y += 5;,重绘,再安排下一次 3 秒。 - 不要用
sleep()/usleep()阻塞事件循环;也不要在信号处理函数里直接调 Xlib。 - 文本超出 300x300 的窗口后会不可见,可以扩大窗口或重置坐标。
可运行的纯 C/Xlib 示例
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/select.h>
#include <time.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
static void draw_text(Display *dsp, Window win, GC gc, int x, int y, const char *text) {
XClearWindow(dsp, win);
XDrawString(dsp, win, gc, x, y, text, (int)strlen(text));
XFlush(dsp);
}
static long long ms_until(const struct timespec *deadline) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
long long ms = (deadline->tv_sec - now.tv_sec) * 1000LL
+ (deadline->tv_nsec - now.tv_nsec) / 1000000LL;
return ms < 0 ? 0 : ms;
}
int main(void) {
Display *dsp = XOpenDisplay(NULL);
if (!dsp) { fprintf(stderr, "cannot open display\n"); return 1; }
int scr = XDefaultScreen(dsp);
Window win1;
XSetWindowAttributes att;
att.background_pixel = XWhitePixel(dsp, scr);
att.event_mask = ExposureMask | StructureNotifyMask;
unsigned long valuemask = CWBackPixel | CWEventMask | CWBorderPixel;
win1 = XCreateWindow(dsp, XDefaultRootWindow(dsp),
0, 0, 300, 300, 2,
XDefaultDepth(dsp, scr), InputOutput,
XDefaultVisual(dsp, scr), valuemask, &att);
GC gc = XDefaultGC(dsp, scr);
XMapWindow(dsp, win1);
XFlush(dsp);
const char *text = "sup dawgs";
int x = 100, y = 100;
int cnt = 0;
int fd = ConnectionNumber(dsp);
struct timespec next;
clock_gettime(CLOCK_MONOTONIC, &next);
next.tv_sec += 3;
while (1) {
while (XPending(dsp)) {
XEvent evt;
XNextEvent(dsp, &evt);
cnt++;
printf("%d - %d\n", evt.type, cnt);
if (evt.type == Expose) {
draw_text(dsp, win1, gc, x, y, text);
} else if (evt.type == ConfigureNotify) {
draw_text(dsp, win1, gc, x, y, text);
}
}
long long ms = ms_until(&next);
struct timeval tv;
tv.tv_sec = ms / 1000;
tv.tv_usec = (ms % 1000) * 1000;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
int ret = select(fd + 1, &rfds, NULL, NULL, &tv);
if (ret < 0) {
if (errno == EINTR) continue;
perror("select");
break;
}
if (ret == 0) {
x += 10;
y += 5;
draw_text(dsp, win1, gc, x, y, text);
clock_gettime(CLOCK_MONOTONIC, &next);
next.tv_sec += 3;
}
}
XCloseDisplay(dsp);
return 0;
}
收到 Expose 时重绘,只有 select 超时才移动,绘制后用 XFlush 立即提交。
注意事项
select()被信号中断会返回-1且errno == EINTR,此时继续循环。poll()也能替代select(),逻辑一样。XNextEvent()在没有事件时会阻塞,所以不能把定时放在它后面。- 事件类型打印适合调试,但不要让动画依赖事件到达来触发每 3 秒移动。
XDefaultGC()是共享 GC,简单示例可以用;复杂场景建议用XCreateGC()并记得释放。- 下一次时间用
CLOCK_MONOTONIC计算,避免系统时间调整造成漂移。
备注:内容仅供参考。
