set_capture.c

simple XGrabPointer() test application. Compile with gcc -lX11 set_capture.c - Spex, 09/06/2009 01:35 am

Download (2 kB)

 
1
#include <stdio.h>
2
#include <X11/Xlib.h>
3
#include <X11/keysym.h>
4
#include <X11/Xatom.h>
5

    
6
#define error printf
7

    
8
int main(int argc, char **argv) {
9
  Display *dpy = XOpenDisplay(NULL);
10
  if (NULL == dpy) {
11
    error("XOpenDisplay(NULL) failed.\n");
12
    goto err_no_display;
13
  }
14
  printf("got display\n");
15
  int width = 640;
16
  int height = 480;
17
  // create window with attributes inherited from root window
18
  Window window = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy),
19
                               0, 0, width, height, 0, 0, 0);
20
  if (0 == window) {
21
    error("XCreateSimpleWindow() failed.\n");
22
    goto err_no_window;
23
  }
24
  printf("created window\n");
25
  // display window on screen
26
  // result checking of XMapWindow()?
27
  XMapWindow(dpy, window);
28
  printf("mapped window\n");
29

    
30
  XSelectInput(dpy, window, ExposureMask | KeyPressMask | StructureNotifyMask |
31
               PointerMotionMask);
32
  Atom wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
33
  XSetWMProtocols(dpy, window, &wm_delete_window, 1);
34
  printf("selected inputs\n");
35

    
36
  int grab = 0;
37

    
38
  while (1) {
39
    while (XPending(dpy)) {
40
      XEvent ev;
41
      XNextEvent(dpy, &ev);
42
      if (ev.xany.window == window) {
43
        XKeyEvent *keyEvent;
44
        XMotionEvent *pointerEvent;
45
        KeySym keySym;
46
        switch (ev.type) {
47
        case KeyPress:
48
          keyEvent = (XKeyEvent*)(&ev);
49
          keySym = XLookupKeysym(keyEvent, 0);
50
          switch (keySym) {
51
          case XK_Escape:
52
            goto err_no_egldisplay;
53
            break;
54
          case XK_Return:
55
            grab ^= 1;
56
            if (grab)
57
              XGrabPointer(dpy, window, True, 0, GrabModeAsync, GrabModeAsync,
58
                           window, None, CurrentTime);
59
            else
60
              XUngrabPointer(dpy, CurrentTime);
61
            break;
62
          }
63
          break;
64
        case MotionNotify:
65
          pointerEvent = (XMotionEvent*)(&ev);
66
          printf("pointer at (%d, %d) rel (%d, %d)\n",
67
                 pointerEvent->x, pointerEvent->y, pointerEvent->x_root,
68
                 pointerEvent->y_root);
69
          break;
70
        case ClientMessage:
71
          goto err_no_egldisplay;
72
          break;
73
        }
74
      }
75
    }
76
  }
77

    
78
 err_no_egldisplay:
79
  XUnmapWindow(dpy, window);
80
  XDestroyWindow(dpy, window);
81
 err_no_window:
82
  XCloseDisplay(dpy);
83
 err_no_display:
84
  return 0;
85
}