Help: Maximum number of clients reached - Segmentation fault

吃可爱长大的小学妹 提交于 2020-01-01 10:59:24

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/1409886/help-maximum-number-of-clients-reached-segmentation-fault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!