我试图计算出延时以显示稳定的闪烁。我没有让它与sleep()
或time.h
一起使用,这似乎是合乎逻辑的。
我怎样才能达到类似的效果,显示白色矩形10秒,然后在循环中清除此区域(也是10秒)。
在this question中,提到socket of the X11 connection
来执行此类操作。但是在这里可以挖掘什么?
由于
答案 0 :(得分:0)
文件描述符在Display结构中,可以使用宏ConnectionNumber(dis)
获得。
然后,您可以使用poll
超时等待事件到达或发生超时。
正如其他问题所述,XPending
会让你看看是否有任何事件,所以你实际上不需要检查文件描述符,你可以这样做:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <time.h>
#include <unistd.h>
int main()
{
XEvent ev;
Display *dis = XOpenDisplay(NULL);
Window win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 1, 1, 500, 500, 0, BlackPixel (dis, 0), WhitePixel(dis, 0));
XMapWindow(dis, win);
GC gc = XCreateGC(dis, win, 0, 0);
int draw = 1;
time_t t = time(NULL) + 10;
XMapWindow(dis, win);
XSelectInput(dis, win, ExposureMask | KeyPressMask);
while (1)
{
if (XPending(dis) > 0)
{
XNextEvent(dis, &ev);
switch (ev.type)
{
case Expose:
printf("Exposed.\n");
if (draw > 0)
{
XFillRectangle(dis, win, gc, 100, 100, 300, 300);
}
break;
case KeyPress:
printf("KeyPress\n");
/* Close the program if q is pressed.*/
if (XLookupKeysym(&ev.xkey, 0) == XK_q)
{
exit(0);
}
break;
}
}
else
{
printf("Pending\n");
sleep(1);
if (time(NULL) >= t)
{
/* Force an exposure */
XClearArea(dis, win, 100, 100, 300, 300, True);
t += 10;
draw = 1 - draw;
}
}
}
return 0;
}
注意:您可能希望使用类似usleep
的内容来获得更精细的计时器粒度。