XLib X_ChangeWindowAttributes BadAccess

时间:2018-12-20 11:49:58

标签: xlib

我正在尝试通过Xlib从C应用程序预订窗口事件。 我通过它的ID访问窗口(我从其他应用程序知道它,并且我确定它是有效的)。但是当我调用xselectinput函数时,出现错误- **

  

X错误的失败请求:BadAccess(尝试访问私有   资源被拒绝)失败请求的主要操作码:2   (X_ChangeWindowAttributes)失败请求的序列号:10
  输出流中的当前序列号:11

我的系统是Ubuntu 16.04。 源代码

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cassert>


int main(int argc, char *argv[])
{
Display * display;
GC gc;
XEvent report;
Window window;

if( ( display = XOpenDisplay ( NULL ) ) == NULL )
{
    puts ("Can not connect to the X server!\n");
    return -1;
}

window = 102760455;

XWindowAttributes attr;
XSelectInput(display, window, 0);
XGetWindowAttributes(display, window, &attr);

XSelectInput(display, window, attr.all_event_masks);

XSync(display, 0);
while(1)
{
    XNextEvent ( display, &report );
    std::cout << "got it";
    switch ( report.type )
    {
        case ButtonPress:
            std::cout << "pressed" << std::endl;
            break;
        case FocusIn:
            std::cout << "FOCUS IN" << std::endl;
            break;

        case FocusOut:
            std::cout << "FOCUS OUT" << std::endl;
            break;

        case Expose :

            if(report.xexpose.count != 0)
                break;

            gc = XCreateGC(display, window, 0 , NULL);

            XSetForeground ( display, gc, BlackPixel ( display, 0) );
            XFreeGC ( display, gc );
            XFlush(display);
            break;

        case MotionNotify:

            std::cout << "MOUSE MOVE" << std::endl;
            std::cout << "x " << report.xmotion.x << std::endl;
            std::cout << "y " << report.xmotion.y << std::endl;

            break;

        case KeyPress :

            std::cout << "KEY PRESSED" << std::endl;

            break;

        case KeyRelease :
            std::cout << "KEY RELEASE" << std::endl;

            break;
    }
}

return 0;
}

0 个答案:

没有答案