Advertisement
Guest User

Untitled

a guest
Sep 6th, 2010
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <X11/Xlib.h>
  5. #include <X11/extensions/XInput2.h>
  6.  
  7. static Window create_win(Display *dpy)
  8. {
  9.     XIEventMask mask;
  10.  
  11.     Window win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 200,
  12.             200, 0, 0, WhitePixel(dpy, 0));
  13.  
  14.     XSelectInput(dpy, win, ExposureMask);
  15.     mask.mask_len = XIMaskLen(XI_RawMotion);
  16.     mask.mask = calloc(mask.mask_len, sizeof(char));
  17.     mask.deviceid = XIAllMasterDevices;
  18.     memset(mask.mask, 0, mask.mask_len);
  19.     XISetMask(mask.mask, XI_RawMotion);
  20.  
  21.     XISelectEvents(dpy, DefaultRootWindow(dpy), &mask, 1);
  22.  
  23.     free(mask.mask);
  24.     XMapWindow(dpy, win);
  25.     XSync(dpy, True);
  26.     return win;
  27. }
  28.  
  29. static void print_rawmotion(XIRawEvent *event)
  30. {
  31.     int i;
  32.     double *raw_valuator = event->raw_values,
  33.            *valuator = event->valuators.values;
  34.  
  35.     printf("    device: %d (%d)", event->deviceid, event->sourceid);
  36.  
  37.     for (i = 0; i < event->valuators.mask_len * 8; i++)
  38.     {
  39.         if (XIMaskIsSet(event->valuators.mask, i))
  40.         {
  41.             printf("  raw_valuator %d: %f", i, *raw_valuator);
  42.             raw_valuator++;
  43.         }
  44.     }
  45.  
  46.     printf("\n");
  47. }
  48.  
  49. int main (int argc, char **argv)
  50. {
  51.     Display *dpy;
  52.     int xi_opcode, event, error;
  53.     Window win;
  54.     XEvent ev;
  55.  
  56.     dpy = XOpenDisplay(NULL);
  57.  
  58.     if (!dpy) {
  59.         fprintf(stderr, "Failed to open display.\n");
  60.         return -1;
  61.     }
  62.  
  63.     if (!XQueryExtension(dpy, "XInputExtension", &xi_opcode, &event, &error)) {
  64.            printf("X Input extension not available.\n");
  65.               return -1;
  66.     }
  67.  
  68.     win = create_win(dpy);
  69.  
  70.     while(1)
  71.     {
  72.         XGenericEventCookie *cookie = &ev.xcookie;
  73.  
  74.         XNextEvent(dpy, &ev);
  75.         if (cookie->type != GenericEvent ||
  76.             cookie->extension != xi_opcode ||
  77.             !XGetEventData(dpy, cookie))
  78.             continue;
  79.  
  80.         switch(cookie->evtype)
  81.         {
  82.             case XI_RawMotion:
  83.                 print_rawmotion(cookie->data);
  84.                 break;
  85.         }
  86.  
  87.         XFreeEventData(dpy, cookie);
  88.  
  89.         static int i = 0;
  90.         i++;
  91.         if(i > 10)
  92.         {
  93.             XWarpPointer(dpy, None, win, 0, 0, 0, 0, 100, 100);
  94.             i = 0;
  95.         }
  96.     }
  97.  
  98.     XCloseDisplay(dpy);
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement