Using X11 to get the mouse position

Source: Internet
Author: User

The code takes dry goods:

#include <X11/Xlib.h>//Every Xlib program must include this #include <assert.h>//I include the To test Return values the lazy way #include <unistd.h>//So we got the profiles for seconds #include <stdio.h> #d
    Efine NIL (0)//A name for the void pointer void AddListener (display* Display, Window R) {//get children list
    unsigned int listnum;
    Window parent;
    Window Root;

    Window *wa;
    Xquerytree (Display, R, &root, &parent, &wa, &listnum);
    if (wa==null) {return;
    int i=0;
    printf ("listnum:%d\n", LISTNUM);
        For (i=0 i<listnum; i++) {Window w=wa[i];
        printf ("Alive here?\n");
        Xwindowattributes tattributes;
        Xgetwindowattributes (Display, w, &tattributes); 
        printf ("map_state:%d,width:%d,height:%d\n", Tattributes.map_state, Tattributes.width, tattributes.height); if (tattributes.map_state==2) {Xselectinput(Display, W, keypressmask);
            } if (tattributes.map_state==2) {//Xselectinput (Display, w, buttonpressmask);
        Xgrabpointer (Display,w,false,buttonpressmask,grabmodeasync,grabmodeasync,none,none,currenttime);
    } addListener (Display, w);
    } int My_handler (Display *d, xerrorevent *e) {printf ("error!\n");
return 1;
    int main () {//Open the display display *display = Xopendisplay (NULL);


    ASSERT (display);

    Xseterrorhandler (My_handler);
    Screen *screen=xdefaultscreenofdisplay (display);
    int Width=xwidthofscreen (screen);
    int Height=xheightofscreen (screen);

    printf ("width:%d,height:%d\n", width, height);

    Window window=xrootwindowofscreen (screen);

    AddListener (display, window);
    Xwindowattributes attributes;

    Xgetwindowattributes (Display, window, &attributes); printf ("map_state:%d,width:%d,height:%d\n", Attributes.map_state, Attributes.width, attributes.height);

    Window FW;
    int state;

    Xgetinputfocus (Display, &AMP;FW, &state);

    Xgetwindowattributes (Display, FW, &attributes); printf ("Focus window---->map_state:%d,width:%d,height:%d\n", attributes.map_state, Attributes.width, attrib

    Utes.height);
    Xsetinputfocus (Display,window,reverttopointerroot,currenttime);
    printf ("Set focus window ok\n");
    Window fw2;
    Xgetinputfocus (Display, &AMP;FW2, &state);
    Xgetwindowattributes (Display, FW, &attributes); printf ("Focus window---->map_state:%d,width:%d,height:%d\n", attributes.map_state, Attributes.width, attrib
    Utes.height);

    printf ("Focus is the same?%d\n", &fw2==&window);

    Xselectinput (Display,fw2,keypressmask);

    Xselectinput (Display,pointerroot,keypressmask);
    int f=1;
            while (f) {if (xpending (display)) {int eq=xeventsqueued (display, queuedalready);
  printf ("Events num in queue:%d\n", eq);          Xevent e;
            Xnextevent (display, &e);
            printf ("Event type:%d\n", E.type);
                if (E.type = = KeyPress) {xkeyevent y=e.xkey;
                unsigned int keycode=y.keycode;
                unsigned int state=y.state;
                printf ("keycode:%d,state:%d\n", KeyCode, state);
            Break
                }else if (e.type==buttonpress) {xbuttonevent Xbutton=e.xbutton;
                printf ("x:%d,y:%d\n", XBUTTON.X,XBUTTON.Y);
            Xsendevent (display,xbutton.window,0,keypressmask,&e);
}} return 0;
 }

Use gcc test.c-lx11 to compile it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.