帮助:达到最大客户端数 - 分段错误

发布于 2024-08-04 07:11:21 字数 1675 浏览 8 评论 0原文

我想模拟很多按键事件。我通过使用 XTestFakeKeyEvent 找到了解决方案,但是当我模拟超过 210 次时,我的程序会引发“已达到最大客户端数”分段错误。我不知道如何解决这个问题。

我的代码在这里:

#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
#include <stdio.h>

#define PRESS_UP    0
#define PRESS_DOWN  1
#define PRESS_LEFT  2
#define PRESS_RIGHT 3
#define PRESS_ENTER 4
#define PRESS_ESC   5
#define PRESS_HOME  6

 Display *display;
 unsigned int keycode;

 int press(int key){
    display = XOpenDisplay(NULL);

    if(key == PRESS_UP)
        keycode = XKeysymToKeycode(display, XK_Up);
    else if(key == PRESS_DOWN)
        keycode = XKeysymToKeycode(display, XK_Down);
    else if(key == PRESS_LEFT)
        keycode = XKeysymToKeycode(display, XK_Left);
    else if(key == PRESS_RIGHT)
        keycode = XKeysymToKeycode(display, XK_Right);
    else if(key == PRESS_ENTER)
        keycode = XKeysymToKeycode(display, XK_Return);
    else if(key == PRESS_ESC)
        keycode = XKeysymToKeycode(display, XK_Escape);
    else if(key == PRESS_HOME)
        keycode = XKeysymToKeycode(display, XK_Home);

    XTestFakeKeyEvent(display, keycode, True, 0);
    XTestFakeKeyEvent(display, keycode, False, 0);
    XFlush(display);
    return 0;
}

int main(int argc, char *argv[]){
    int count = 0;
    while (count < 500){
        press(PRESS_UP);
        printf("count =%d\n" ,count);
        count ++;
    }
}

输出:

$ gcc -lXtst -lX11 Util_KeyPressSimulator.c 
$ ./a.out 
count =0
^[[Acount =1
^[[Acount =2
...
^[[Acount =208
^[[Acount =209
^[[Acount =210
^[[AMaximum number of clients reachedSegmentation fault

请帮忙,谢谢。

I want to simulate many key press events. I found a solution by using XTestFakeKeyEvent, but when I simulate more than 210 times my program raises a "Maximum number of clients reached" segmentation fault. I don't known how to solve this problem.

My code here:

#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
#include <stdio.h>

#define PRESS_UP    0
#define PRESS_DOWN  1
#define PRESS_LEFT  2
#define PRESS_RIGHT 3
#define PRESS_ENTER 4
#define PRESS_ESC   5
#define PRESS_HOME  6

 Display *display;
 unsigned int keycode;

 int press(int key){
    display = XOpenDisplay(NULL);

    if(key == PRESS_UP)
        keycode = XKeysymToKeycode(display, XK_Up);
    else if(key == PRESS_DOWN)
        keycode = XKeysymToKeycode(display, XK_Down);
    else if(key == PRESS_LEFT)
        keycode = XKeysymToKeycode(display, XK_Left);
    else if(key == PRESS_RIGHT)
        keycode = XKeysymToKeycode(display, XK_Right);
    else if(key == PRESS_ENTER)
        keycode = XKeysymToKeycode(display, XK_Return);
    else if(key == PRESS_ESC)
        keycode = XKeysymToKeycode(display, XK_Escape);
    else if(key == PRESS_HOME)
        keycode = XKeysymToKeycode(display, XK_Home);

    XTestFakeKeyEvent(display, keycode, True, 0);
    XTestFakeKeyEvent(display, keycode, False, 0);
    XFlush(display);
    return 0;
}

int main(int argc, char *argv[]){
    int count = 0;
    while (count < 500){
        press(PRESS_UP);
        printf("count =%d\n" ,count);
        count ++;
    }
}

Output :

$ gcc -lXtst -lX11 Util_KeyPressSimulator.c 
$ ./a.out 
count =0
^[[Acount =1
^[[Acount =2
...
^[[Acount =208
^[[Acount =209
^[[Acount =210
^[[AMaximum number of clients reachedSegmentation fault

Does one please help, Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨与橙与城 2024-08-11 07:11:21

您每次迭代都会执行一个 XOpenDisplay,而没有匹配的 XCloseDisplay。考虑在循环外部的 main() 中打开一次显示,然后在退出之前将其关闭。

You're doing an XOpenDisplay per iteration without a matching XCloseDisplay. Consider opening the display once, in main(), outside the loop, and closing it before you exit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文