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, &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, &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.