how to quit the blocking of xlib's XNextEvent

前端 未结 3 990
粉色の甜心
粉色の甜心 2020-11-30 01:43

Under windows, the GUI thread usually call GetMessage to waiting for message, when another thread use PoseMessage put a message into the queue, then the GUI thread will retu

3条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 02:37

    You can quit the blocking XNextEvent, by sending yourself a dummy event.

    Window interClientCommunicationWindow;
    Bool x11EventLoopActive = True;
    
    // create a dummy window, that we can use to end the blocking XNextEvent call
    interClientCommunicationWindow = XCreateSimpleWindow(dpy, root, 10, 10, 10, 10, 0, 0, 0);
    XSelectInput(dpy, interClientCommunicationWindow, StructureNotifyMask);
    
    XEvent event;
    while(x11EventLoopActive) {
      XNextEvent(dpy, &event);
      ...
    }
    

    In another thread you can do this to end the loop:

    x11EventLoopActive = False;
    // push a dummy event into the queue so that the event loop has a chance to stop
    XClientMessageEvent dummyEvent;
    memset(&dummyEvent, 0, sizeof(XClientMessageEvent));
    dummyEvent.type = ClientMessage;
    dummyEvent.window = interClientCommunicationWindow;
    dummyEvent.format = 32;
    XSendEvent(dpy, interClientCommunicationWindow, 0, 0, (XEvent*)&dummyEvent);
    XFlush(dpy);
    

提交回复
热议问题