[英]Lag using Xlib/X11 event handling in C
Beginner user of C here. C的初学者。
I'm trying to build a library in C using X11/Xlib so I can use it just for little projects and I'm running into a problem when trying to handle events to get input(button presses and key presses) from the user. 我正在尝试使用X11 / Xlib在C中构建一个库,因此我只能将其用于小项目,并且在尝试处理事件以从用户获取输入(按钮和按键)时遇到问题。 It works fine for a while and then it starts to build up a significant lag over time.
它可以正常工作一会儿,然后随着时间的流逝逐渐积累起来。
Right now what I have is my program checking if there is an event waiting and if there is, retrieving it. 现在,我的程序是检查是否有事件在等待,是否有事件正在检索。
I think that my problem right now is that the events are getting stored in memory and its bogging down the program. 我认为我现在的问题是事件已存储在内存中,并且程序陷入瘫痪。 But that's just a total guess.
但这只是一个总的猜测。
Any help will be appreciated. 任何帮助将不胜感激。 Thank you.
谢谢。
EDIT: Forgot code (I knew I forgot something) 编辑:忘记代码(我知道我忘记了什么)
The two functions in question are: 有问题的两个功能是:
int event_waiting()
{
XEvent event;
if(XCheckMaskEvent(dspy,-1,&event)) {
if(event.type==KeyPress) {
XPutBackEvent(dspy,&event);
return 1;
} else if (event.type==ButtonPress) {
XPutBackEvent(dspy,&event);
return 1;
}
} /* <<=== added missing close-curly here */
return 0;
}
char wait()
{
XEvent event;
XNextEvent(dspy,&event);
if(event.type==KeyPress) {
saved_x = event.xkey.x;
saved_y = event.xkey.y;
return XLookupKeysym(&event.xkey,0);
} else if(event.type==ButtonPress) {
saved_x = event.xkey.x;
saved_y = event.xkey.y;
return event.xbutton.button;
}
}
And then they are called in the main like so, 然后像这样主叫它们,
if (event_waiting()){
char c = wait();
//Switch case goes here
}
EDIT 2: UPDATED CODE 编辑2:更新的代码
XEvent event;
if(XCheckMaskEvent(display,-1,&event))
{
if(event.type==KeyPress) {
XPutBackEvent(display,&event);
return 1;
} else if (event.type==ButtonPress) {
XPutBackEvent(display,&event);
return 1;
}
}
XFlush(display);
return 0;
` `
The lag, which gets worse over time, means that you have many untouched events in your event queue, which slows down XCheckMaskEvent()
. 滞后会随着时间的推移而加剧,这意味着事件队列中有许多未触动的事件,这会减慢
XCheckMaskEvent()
速度。
Try specifying events using XSelectInput(... ButtonPressMask | KeyPressMask)
, and try flushing the event queue using XFlush()
if there is no event in which you are interested: 尝试使用
XSelectInput(... ButtonPressMask | KeyPressMask)
指定事件,如果没有您感兴趣的事件,请尝试使用XFlush()
刷新事件队列:
if(event.type==KeyPress) {
XPutBackEvent(dspy,&event);
return 1;
} else if (event.type==ButtonPress) {
XPutBackEvent(dspy,&event);
return 1;
} else {
XFlush(dspy); // this
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.