home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / extensions / test / xinput / XSelInput.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-19  |  33.3 KB  |  1,125 lines

  1. /* $XConsortium: XSelInput.c,v 1.2 91/02/20 09:17:58 rws Exp $ */
  2. /************************************************************************
  3.  *
  4.  * XSelInput.c   - Test case for XSelectInput function.
  5.  * Purpose:     - Turns on all available input devices and selects input from
  6.  *            them.
  7.  */
  8.  
  9. #include <X11/Xlib.h>
  10. #include <X11/extensions/XI.h>
  11. #include <X11/extensions/XInput.h>
  12. #include <X11/Xutil.h>
  13. #include "stdio.h"
  14. #include <X11/keysym.h>
  15.  
  16.  
  17. #define NOPRINT                    0
  18. #define PRINTTITLE                1
  19. #define PRINT                    2
  20.  
  21. #define XGetExtensionVersion_code        1
  22. #define XListInputDevices_code            2
  23. #define XOpenDevice_code            3
  24. #define XCloseDevice_code            4
  25. #define XSetDeviceMode_code            5
  26. #define XSelectExtensionEvent_code          6
  27. #define XGetSelectedExtensionEvents_code    7
  28. #define XChangeDeviceDontPropagateList_code     8
  29. #define XGetDeviceDontPropagateList_code     9
  30. #define XGetDeviceMotionEvents_code         10 
  31. #define XChangeKeyboardDevice_code        11
  32. #define XChangePointerDevice_code        12
  33. #define XGrabDevice_code             13
  34. #define XUngrabDevice_code              14
  35. #define XGrabDeviceKey_code            15
  36. #define XUngrabDeviceKey_code            16
  37. #define XGrabDeviceButton_code            17
  38. #define XUngrabDeviceButton_code        18
  39. #define XAllowDeviceEvents_code            19
  40. #define XGetDeviceFocus_code            20
  41. #define XSetDeviceFocus_code            21
  42. #define XGetFeedbackControl_code        22
  43. #define XChangeFeedbackControl_code        23
  44. #define XGetDeviceKeyMapping_code        24
  45. #define XChangeDeviceKeyMapping_code        25
  46. #define XGetDeviceModifierMapping_code        26
  47. #define XSetDeviceModifierMapping_code        27
  48. #define XGetDeviceButtonMapping_code        28
  49. #define XSetDeviceButtonMapping_code        29
  50. #define XQueryDeviceState_code             30
  51.  
  52. char *ext_errors[] = {"BadDevice","BadEvent","BadMode","DeviceBusy","BadClass"};
  53.  
  54. char *std_errors[] = {"Success","BadRequest","BadValue","BadWindow","BadPixmap",
  55.         "BadAtom","BadCursor","BadFont","BadMatch","BadDrawable",
  56.         "BadAccess","BadAlloc","BadColor","BadGC","BadIDChoice",
  57.         "BadName","BadLength","BadImplementation"};
  58.  
  59. char *extfuncs[] = {"undefined", "GetExtensionVersion","ListInputDevices",
  60. "OpenDevice", "CloseDevice","SetDeviceMode","SelectExtensionEvent",
  61. "GetSelectedExtensionEvents", "ChangeDeviceDontPropagateList",
  62. "GetDeviceDontPropagateList","GetDeviceMotionEvents", "ChangeKeyboardDevice",
  63. "ChangePointerDevice","GrabDevice","UngrabDevice", "GrabDeviceKey",
  64. "UngrabDeviceKey","GrabDeviceButton","UngrabDeviceButton", "AllowDeviceEvents",
  65. "GetDeviceFocus","SetDeviceFocus","GetFeedbackControl",
  66. "ChangeFeedbackControl","GetDeviceKeyMapping","ChangeDeviceKeyMapping",
  67. "GetDeviceModifierMapping","SetDeviceModifierMapping","GetDeviceButtonMapping",
  68. "SetDeviceButtonMapping","QueryDeviceState"};
  69.  
  70. int    Dflag = 0;
  71.  
  72. int    gstatus;
  73. int    expect = Success;
  74. int    error_code;
  75. int    major_code;
  76. int    first_err;
  77. int     ndevices;
  78.  
  79. int    devicekeypressflag;
  80. int    devicekeyreleaseflag;
  81. int    devicebuttonpressflag;
  82. int    devicebuttonreleaseflag;
  83. int    devicebuttonpressgrabflag;
  84. int    devicebutton1motionflag;
  85. int    devicebutton2motionflag;
  86. int    devicebutton3motionflag;
  87. int    devicebutton4motionflag;
  88. int    devicebutton5motionflag;
  89. int    devicebuttonmotionflag;
  90. int    devicemotionnotifyflag;
  91. int    devicemotionhintflag;
  92. int    devicefocuschangeflag;
  93. int    proximityinflag;
  94. int    proximityoutflag;
  95. int    devicemappingnotifyflag;
  96. int    devicestatenotifyflag;
  97. int    changedevicenotifyflag;
  98. int    deviceownergrabbuttonflag;
  99. int    devicebuttonpressgrabflag;
  100.  
  101. int    devicekeypress;
  102. int    devicekeyrelease;
  103. int    devicebuttonpress;
  104. int    devicebuttonrelease;
  105. int    devicebuttonpressgrab;
  106. int    devicebutton1motion;
  107. int    devicemotionnotify;
  108. int    devicepointermotionhint;
  109. int    devicefocusin;
  110. int    devicefocusout;
  111. int    proximityin;
  112. int    proximityout;
  113. int    devicemappingnotify;
  114. int    devicestatenotify;
  115. int    changedevicenotify;
  116.  
  117. struct    classes
  118.     {
  119.     int valid;
  120.     XEventClass class[15];
  121.     } class[256];
  122.  
  123. XDeviceInfoPtr    savlist[256];
  124. XDevice        *sdev[256];
  125. XDevice        *newpointer = NULL;
  126. XDevice        *newkeyboard = NULL;
  127.  
  128. Window        root;
  129. int        printmode = 1;
  130.  
  131. main(argc,argv)
  132.     int argc;
  133.     char *argv[];
  134.     {
  135.     int         i,j,count;
  136.     char        *name;
  137.     Display        *display, *display2, *display3;
  138.     Window        my;
  139.     XDeviceInfoPtr     list_input_devices ();
  140.     XDeviceInfoPtr    list, slist;
  141.     XInputClassInfo    *ip;
  142.     XEvent        event;
  143.  
  144.     name = argv[0];
  145.     process_args (argc, argv);
  146.     display = XOpenDisplay ("");
  147.     if (display == NULL)
  148.     {
  149.     printf ("No connection to server - aborting test.\n");
  150.     exit(1);
  151.     }
  152.     display2 = XOpenDisplay ("");
  153.     if (display2 == NULL)
  154.     {
  155.     printf ("No second connection to server - aborting test.\n");
  156.     exit(1);
  157.     }
  158.     display3 = XOpenDisplay ("");
  159.     if (display3 == NULL)
  160.     {
  161.     printf ("No second connection to server - aborting test.\n");
  162.     exit(1);
  163.     }
  164.     root = RootWindow (display,0);
  165.  
  166.     init_error_handler (display);
  167.     create_window (display, root, &my);
  168.     XSelectInput (display, my, KeyReleaseMask | KeyReleaseMask);
  169.     slist = list_input_devices (display, &ndevices);
  170.     list = slist;
  171.     open_all_devices (display, list, ndevices);
  172.  
  173.     for (i=0; i<ndevices; i++, list++)
  174.     if (list->use != IsXKeyboard && list->use != IsXPointer)
  175.         {
  176.         select_all_input (display, my, list->name, sdev[i]);
  177.         select_all_input (display2, my, list->name, sdev[i]);
  178.         select_all_input (display3, my, list->name, sdev[i]);
  179.         }
  180.  
  181.     for (count=0;;count++)
  182.     {
  183.     while (XPending (display) > 0)
  184.         {
  185.         XNextEvent (display,&event);
  186.         if (process_device_events (display, 1, &event, printmode) == -1)
  187.         {
  188.         XDestroyWindow (display, my);
  189.         XSync (display,0);
  190.             close_input_devices (display, slist, sdev, ndevices);
  191.         exit(1);
  192.         }
  193.         }
  194.     while (XPending (display2) > 0)
  195.         {
  196.         XNextEvent (display2,&event);
  197.         process_device_events (display, 2, &event, printmode);
  198.         }
  199.     while (XPending (display3) > 0)
  200.         {
  201.         XNextEvent (display3,&event);
  202.         process_device_events (display, 3, &event, printmode);
  203.         }
  204.     }
  205.     }
  206.  
  207. /******************************************************************
  208.  *
  209.  * This function closes all open input devices.
  210.  *
  211.  */
  212.  
  213. close_input_devices (display, list, devices, count)
  214.     Display        *display;
  215.     XDeviceInfoPtr    list;
  216.     XDevice        *devices[];
  217.     int            count;
  218.     {
  219.     int        i;
  220.  
  221.     for (i=0; i<count; i++, list++)
  222.     if (list->use != IsXKeyboard && list->use != IsXPointer)
  223.             XCloseDevice (display, devices[i]);
  224.     }
  225.  
  226. /******************************************************************
  227.  *
  228.  * This function creates a test windows.
  229.  *
  230.  */
  231.  
  232. create_window (display, root, my)
  233.     Display *display;
  234.     Window root, *my;
  235.     {   
  236.     XWindowAttributes attr;
  237.     XSetWindowAttributes attributes;
  238.     unsigned long     attribute_mask;
  239.     int         status;
  240.     XSizeHints         hints;
  241.     Screen        *screen = XDefaultScreenOfDisplay (display);
  242.  
  243.     attribute_mask = CWBackPixmap; 
  244.     attribute_mask = CWBackPixel; 
  245.     attribute_mask |= CWEventMask; 
  246.     attribute_mask |= CWDontPropagate; 
  247.     attributes.do_not_propagate_mask = 0;
  248.     attributes.background_pixmap = None;
  249.     attributes.background_pixel = WhitePixel(display, 0);
  250.     attributes.event_mask = ExposureMask;
  251.     
  252.     *my = XCreateWindow (display, root, 100,100, 400,200,1,
  253.     DefaultDepthOfScreen (screen),
  254.     InputOutput, CopyFromParent, attribute_mask, &attributes);
  255.  
  256.     if (*my == 0) {
  257.     fprintf (stderr, "can't create window!\n");
  258.     exit (1);
  259.     }
  260.     status = XGetNormalHints (display, *my, &hints);
  261.     hints.x = 100;
  262.     hints.y = 100;
  263.     hints.width = 400;
  264.     hints.height = 200;
  265.     hints.min_width = 400;
  266.     hints.min_height = 200;
  267.     hints.flags |= (PPosition | PSize | PMinSize);
  268.     XSetNormalHints (display, *my, &hints);
  269.     XMapWindow (display, *my);
  270.     XFlush(display);
  271.     for (;;)
  272.     {
  273.     XGetWindowAttributes (display, *my, &attr);
  274.     if (attr.map_state == IsViewable)
  275.         break;
  276.     }
  277.     }
  278.  
  279. /******************************************************************
  280.  *
  281.  * This function lists all available input devices.
  282.  *
  283.  */
  284.  
  285. XDeviceInfoPtr
  286. list_input_devices (display, ndevices)
  287.     Display *display;
  288.     int        *ndevices;
  289.     {
  290.     int            i,j,k;
  291.     XDeviceInfoPtr    list, slist;
  292.     XAnyClassPtr    any;
  293.     XKeyInfoPtr        K;
  294.     XButtonInfoPtr    b;
  295.     XValuatorInfoPtr    v;
  296.     XAxisInfoPtr    a;
  297.  
  298.     list = (XDeviceInfoPtr) XListInputDevices (display, ndevices);
  299.     slist = list;
  300.     if (Dflag)
  301.     printf ("The number of available input devices is %d\n",*ndevices);
  302.     for (i=0; i<*ndevices; i++, list++)
  303.     {
  304.     savlist[i] = list;
  305.     if (Dflag)
  306.         {
  307.         printf ("\nid is %d\n",list->id);
  308.         printf ("type is %d\n",list->type);
  309.         if (list->use == IsXKeyboard)
  310.             printf ("Device %s is the X keyboard.\n",list->name);
  311.         else if (list->use == IsXPointer)
  312.             printf ("Device %s is the X pointer.\n",list->name);
  313.         else if (list->use == IsXExtensionDevice)
  314.             printf ("Device %s is an extension device.\n",list->name);
  315.         printf ("num_classes is %d\n\n",list->num_classes);
  316.         }
  317.     if (list->num_classes > 0)
  318.         {
  319.         any = (XAnyClassPtr) (list->inputclassinfo);
  320.         for (j=0; j<list->num_classes; j++)
  321.         {
  322.         if (Dflag)
  323.             {
  324.             printf ("input class is %d\n", any->class);
  325.             printf ("length is %d\n", any->length);
  326.             }
  327.         switch (any->class)
  328.             {
  329.             case KeyClass:
  330.             K = (XKeyInfoPtr) any;
  331.             if (Dflag)
  332.                 {
  333.                 printf ("num_keys is %d\n",K->num_keys);
  334.                 printf ("min_keycode is %d\n",K->min_keycode);
  335.                 printf ("max_keycode is %d\n\n",K->max_keycode);
  336.                 }
  337.             break;
  338.             case ButtonClass:
  339.             b = (XButtonInfoPtr) any;
  340.             if (Dflag)
  341.                 printf ("num_buttons is %d\n\n",b->num_buttons);
  342.             break;
  343.             case ValuatorClass:
  344.             v = (XValuatorInfoPtr) any;
  345.             a = (XAxisInfoPtr) ((char *) v + 
  346.                 sizeof (XValuatorInfo));
  347.             if (Dflag)
  348.                 printf ("num_axes is %d\n\n",v->num_axes);
  349.             for (k=0; k<v->num_axes; k++,a++)
  350.                 {
  351.                 if (Dflag)
  352.                 {
  353.                 printf ("min_value is %d\n",a->min_value);
  354.                 printf ("max_value is %d\n",a->max_value);
  355.                 printf ("resolution is %d\n\n",a->resolution);
  356.                 }
  357.                 }
  358.             break;
  359.             default:
  360.             printf ("unknown class\n");
  361.             }
  362.         any = (XAnyClassPtr) ((char *) any + any->length);
  363.         }
  364.         }
  365.     }
  366.     return (slist);
  367.     }
  368.  
  369.  
  370. /******************************************************************
  371.  *
  372.  * This function opens all extension input devices.
  373.  *
  374.  */
  375.  
  376. open_all_devices (display, list, ndevices)
  377.     Display        *display;
  378.     XDeviceInfoPtr     list;
  379.     int            ndevices;
  380.     {
  381.     struct        classes    *cp;
  382.     int            i,j;
  383.     XDevice        *dev;
  384.     XDevice        *XOpenDevice();
  385.     XInputClassInfo    *ip;
  386.  
  387.     for (i=0; i<ndevices; i++, list++)
  388.     if (list->use != IsXKeyboard &&
  389.         list->use != IsXPointer)
  390.         {
  391.         dev = XOpenDevice (display, list->id);
  392.         sdev[i] = dev;
  393.         if (Dflag)
  394.         printf ("\nOpened device %s id is %d\n",
  395.             list->name, dev->device_id);
  396.         cp = &class[dev->device_id];
  397.         for (ip= dev->classes, j=0; j<dev->num_classes; j++, ip++)
  398.         {
  399.         if (Dflag)
  400.             {
  401.             printf ("class is %d\n",ip->input_class);
  402.             printf ("event type base is %x\n\n",ip->event_type_base);
  403.             }
  404.         if (ip->input_class == KeyClass)
  405.             {
  406.             if (newkeyboard == NULL)
  407.                 newkeyboard=dev;
  408.                 DeviceKeyPress (dev, devicekeypress, cp->class[cp->valid]);
  409.             if (devicekeypressflag)
  410.                 cp->valid++;
  411.                 DeviceKeyRelease (dev, devicekeyrelease,  
  412.             cp->class[cp->valid]);
  413.             if (devicekeyreleaseflag)
  414.             cp->valid++;
  415.             if (Dflag)
  416.             {
  417.             printf ("DeviceKeyPress reports: type=%x class=%x\n",
  418.                 devicekeypress, cp->class[cp->valid-1]);
  419.             printf ("DeviceKeyRelease reports: type=%x class=%x\n",
  420.                 devicekeyrelease, cp->class[cp->valid]);
  421.                     printf("\n");
  422.             }
  423.             }
  424.         else if (ip->input_class == ButtonClass)
  425.             {
  426.             if (newpointer == NULL)
  427.                 newpointer=dev;
  428.                 DeviceButtonPress (dev, devicebuttonpress, 
  429.             cp->class[cp->valid]);
  430.             if (devicebuttonpressflag)
  431.             cp->valid++;
  432.                 DeviceButtonRelease (dev, devicebuttonrelease, 
  433.             cp->class[cp->valid]);
  434.             if (devicebuttonreleaseflag)
  435.             cp->valid++;
  436.                 DeviceButtonPressGrab (dev, devicebuttonpressgrab, 
  437.             cp->class[cp->valid]);
  438.             if (devicebuttonpressgrabflag)
  439.             cp->valid++;
  440.                 DeviceButton1Motion (dev, devicebutton1motion, 
  441.             cp->class[cp->valid]);
  442.             if (devicebutton1motionflag)
  443.             cp->valid++;
  444.             if (Dflag)
  445.             {
  446.             printf ("DeviceButtonPress reports: type=%x class=%x\n",
  447.                 devicebuttonpress, cp->class[cp->valid-2]);
  448.             printf ("DeviceButtonRelease: type=%x class=%x\n",
  449.                 devicebuttonrelease, cp->class[cp->valid-1]);
  450.             printf ("DeviceButtonPressGrab: type=%x class=%x\n",
  451.                 devicebuttonpressgrab, cp->class[cp->valid]);
  452.             printf("\n");
  453.             }
  454.             }
  455.         else if (ip->input_class == ValuatorClass)
  456.             {
  457.                 DeviceMotionNotify (dev, devicemotionnotify, 
  458.             cp->class[cp->valid]);
  459.             if (devicemotionnotifyflag)
  460.             cp->valid++;
  461.                 DevicePointerMotionHint (dev, devicepointermotionhint, 
  462.             cp->class[cp->valid]);
  463.             if (devicemotionhintflag)
  464.             cp->valid++;
  465.             if (Dflag)
  466.             {
  467.             printf ("DeviceMotionNotify: type=%x class=%x\n",
  468.                 devicemotionnotify, cp->class[cp->valid-1]);
  469.             printf ("DevicePointerMotionHint: type=%x class=%x\n",
  470.                 devicepointermotionhint, cp->class[cp->valid]);
  471.             }
  472.             }
  473.         else if (ip->input_class == FocusClass)
  474.             {
  475.                 DeviceFocusIn (dev, devicefocusin, cp->class[cp->valid]);
  476.             if (devicefocuschangeflag)
  477.             cp->valid++;
  478.                 DeviceFocusOut (dev, devicefocusout,  cp->class[cp->valid]);
  479.             if (devicefocuschangeflag)
  480.             cp->valid++;
  481.             if (Dflag)
  482.             {
  483.             printf ("DeviceFocusIn: type=%x class=%x\n",
  484.                 devicefocusin, cp->class[cp->valid-1]);
  485.             printf ("DeviceFocusOut: type=%x class=%x\n",
  486.                 devicefocusout, cp->class[cp->valid-1]);
  487.             }
  488.             }
  489.         else if (ip->input_class == ProximityClass)
  490.             {
  491.                 ProximityIn (dev, proximityin, cp->class[cp->valid]);
  492.             if (proximityinflag)
  493.             cp->valid++;
  494.                 ProximityOut (dev, proximityout, cp->class[cp->valid]);
  495.             if (proximityoutflag)
  496.             cp->valid++;
  497.             if (Dflag)
  498.             {
  499.             printf ("ProximityIn: type=%x class=%x\n",
  500.                 proximityin, cp->class[cp->valid-1]);
  501.             printf ("ProximityOut: type=%x class=%x\n",
  502.                 proximityout, cp->class[cp->valid]);
  503.             }
  504.             }
  505.         else if (ip->input_class == OtherClass)
  506.             {
  507.                 DeviceMappingNotify (dev, devicemappingnotify, 
  508.             cp->class[cp->valid]);
  509.             if (devicemappingnotifyflag)
  510.             cp->valid++;
  511.                 DeviceStateNotify (dev, devicestatenotify, 
  512.             cp->class[cp->valid]);
  513.             if (devicestatenotifyflag)
  514.             cp->valid++;
  515.                 ChangeDeviceNotify (dev, changedevicenotify, 
  516.             cp->class[cp->valid]);
  517.             if (changedevicenotifyflag)
  518.             cp->valid++;
  519.             if (Dflag)
  520.             {
  521.             printf ("DeviceMappingNotify: type=%x class=%x\n",
  522.                 devicemappingnotify, cp->class[cp->valid-2]);
  523.             printf ("DeviceStateNotify: type=%x class=%x\n",
  524.                 devicestatenotify, cp->class[cp->valid-1]);
  525.             printf ("ChangeDeviceNotify: type=%x class=%x\n",
  526.                 changedevicenotify, cp->class[cp->valid]);
  527.                 }
  528.             }
  529.         }
  530.         }
  531.     }
  532.  
  533. /******************************************************************
  534.  *
  535.  * This function selects all available input from an extension 
  536.  * device.
  537.  *
  538.  */
  539.  
  540. select_all_input (display, win, name, dev)
  541.     Display    *display;
  542.     Window    win;
  543.     char    *name;
  544.     XDevice    *dev;
  545.     {
  546.     int            i, j;
  547.     int            status = 0;
  548.     int            this_client_count;
  549.     int            all_clients_count;
  550.     XEventClass        *this_client, *s_this;
  551.     XEventClass        *all_clients, *s_all;
  552.  
  553.     if (Dflag)
  554.     printf ("Selecting input from %s.\n", name);
  555.     XSelectExtensionEvent (display, win, 
  556.     &(class[dev->device_id].class[0]), 
  557.     class[dev->device_id].valid);
  558.     XGetSelectedExtensionEvents (display, win, &this_client_count,
  559.     &this_client, &all_clients_count, &all_clients);
  560.    
  561.     s_this = this_client;
  562.     s_all = all_clients;
  563.     for (i=0; i<class[dev->device_id].valid; i++)
  564.     {
  565.     this_client = s_this;
  566.         for (j=0; j<this_client_count; j++)
  567.         if (*this_client++ == class[dev->device_id].class[i])
  568.         break;
  569.     if (j==this_client_count)
  570.         status = -1;
  571.     }
  572.  
  573.     if (Dflag)
  574.     {
  575.     printf ("This_client_count is %d, all_clients_count is %d\n",
  576.         this_client_count, all_clients_count);
  577.     this_client = s_this;
  578.     all_clients = s_all;
  579.     for (i=0; i<this_client_count; i++)
  580.         printf ("This_client class[i] is %x\n", *this_client++);
  581.     for (i=0; i<all_clients_count; i++)
  582.         printf ("All_clients class[i] is %x\n", *all_clients++);
  583.     printf ("\n");
  584.     }
  585.  
  586.     if (status == 0)
  587.     printf ("Test of XSelect/ XGetSelectedExtensionEvents passed.\n");
  588.     else
  589.     printf ("Test of XSelect/ XGetSelectedExtensionEvents failed.\n");
  590.     }
  591.  
  592. /******************************************************************
  593.  *
  594.  * This function displays the contents of extension events.
  595.  *
  596.  */
  597.  
  598. process_device_events (display, clientno, event, mode)
  599.     Display    *display;
  600.     int        clientno;
  601.     XEvent    *event;
  602.     int        mode;
  603.     {
  604.     int                i, j;
  605.     char            *buf;
  606.     XKeyStatus            *kdata;
  607.     XButtonStatus        *bdata;
  608.     XValuatorStatus        *vdata;
  609.     XKeyEvent            *K;
  610.     XDeviceKeyEvent        *k;
  611.     XDeviceButtonEvent        *b;
  612.     XDeviceMappingEvent        *m;
  613.     XDeviceMotionEvent        *M;
  614.     XDeviceFocusChangeEvent    *f;
  615.     XProximityNotifyEvent    *p;
  616.     XChangeDeviceNotifyEvent    *c;
  617.     XDeviceStateNotifyEvent    *s;
  618.     XInputClass         *anyclass;
  619.     KeySym            sym;
  620.  
  621.     if (event->type == KeyRelease)
  622.     {
  623.     K = (XKeyReleasedEvent *) event;
  624.     sym = XKeycodeToKeysym (display, K->keycode, 0);
  625.     if (sym == XK_space)
  626.         return (-1);
  627.     }
  628.     else if (event->type == MappingNotify)
  629.     {
  630.     if (mode > NOPRINT)
  631.         printf ("MappingNotify event.\n");
  632.     }
  633.     else if (event->type == devicekeypress)
  634.     {
  635.     k = (XDeviceKeyEvent * ) event;
  636.     if (mode > NOPRINT)
  637.         printf ("Client: %d Device key press event device=%d\n", 
  638.         clientno, k->deviceid);
  639.     if (mode == PRINT)
  640.         {
  641.         printf ("     type =        %d\n", k->type);
  642.         printf ("     serial =      %ld\n", k->serial);
  643.         printf ("     send_event =  %ld\n", k->send_event);
  644.         printf ("     display =     %x\n", k->display);
  645.         printf ("     window =      %x\n", k->window);
  646.         printf ("     root =        %x\n", k->root);
  647.         printf ("     subwindow =   %x\n", k->subwindow);
  648.         printf ("     time =        %x\n", k->time);
  649.         printf ("     x =           %d\n", k->x);
  650.             printf ("     y =           %d\n", k->y);
  651.         printf ("     x_root =      %d\n", k->x_root);
  652.         printf ("     y_root =      %d\n", k->y_root);
  653.         printf ("     state =       %d\n", k->state);
  654.         printf ("     keycode =     %x\n", k->keycode);
  655.         printf ("     same_screen = %d\n", k->same_screen);
  656.         printf ("     device_state =%d\n", k->device_state);
  657.         printf ("     axes_count  = %d\n", k->axes_count);
  658.         printf ("     first_axis  = %d\n", k->first_axis);
  659.         }
  660.     }
  661.     else if (event->type == devicekeyrelease)
  662.     {
  663.     k = (XDeviceKeyEvent * ) event;
  664.     if (mode > NOPRINT)
  665.         printf ("Client: %d Device key release event device=%d\n", 
  666.         clientno, k->deviceid);
  667.     if (mode == PRINT)
  668.         {
  669.         printf ("     type =        %d\n", k->type);
  670.         printf ("     serial =      %ld\n", k->serial);
  671.         printf ("     send_event =  %ld\n", k->send_event);
  672.         printf ("     display =     %x\n", k->display);
  673.         printf ("     window =      %x\n", k->window);
  674.         printf ("     root =        %x\n", k->root);
  675.         printf ("     subwindow =   %x\n", k->subwindow);
  676.         printf ("     time =        %x\n", k->time);
  677.         printf ("     x =           %d\n", k->x);
  678.         printf ("     y =           %d\n", k->y);
  679.         printf ("     x_root =      %d\n", k->x_root);
  680.         printf ("     y_root =      %d\n", k->y_root);
  681.         printf ("     state =       %d\n", k->state);
  682.         printf ("     keycode =     %x\n", k->keycode);
  683.         printf ("     same_screen = %d\n", k->same_screen);
  684.         }
  685.         if (k->keycode == 0xd)
  686.         return (-1);
  687.     }
  688.     else if (event->type == devicebuttonpress)
  689.     {
  690.     b = (XDeviceButtonEvent * ) event;
  691.     if (mode > NOPRINT)
  692.         printf ("Client: %d Device button press event device=%d\n", 
  693.         clientno, b->deviceid);
  694.     if (mode == PRINT)
  695.         {
  696.         printf ("     type =        %d\n", b->type);
  697.         printf ("     serial =      %ld\n", b->serial);
  698.         printf ("     send_event =  %ld\n", b->send_event);
  699.         printf ("     display =     %x\n", b->display);
  700.         printf ("     window =      %x\n", b->window);
  701.         printf ("     root =        %x\n", b->root);
  702.         printf ("     subwindow =   %x\n", b->subwindow);
  703.         printf ("     time =        %x\n", b->time);
  704.         printf ("     x =           %d\n", b->x);
  705.         printf ("     y =           %d\n", b->y);
  706.         printf ("     x_root =      %d\n", b->x_root);
  707.         printf ("     y_root =      %d\n", b->y_root);
  708.         printf ("     state =       %d\n", b->state);
  709.         printf ("     button =      %x\n", b->button);
  710.         printf ("     same_screen = %d\n", b->same_screen);
  711.         printf ("     device_state =%d\n", b->device_state);
  712.         printf ("     axes_count  = %d\n", b->axes_count);
  713.         printf ("     first_axis  = %d\n", b->first_axis);
  714.         }
  715.     }
  716.     else if (event->type == devicebuttonrelease)
  717.      {
  718.     b = (XDeviceButtonEvent * ) event;
  719.     if (mode > NOPRINT)
  720.         printf ("Client: %d Device button release event device=%d\n", 
  721.         clientno, b->deviceid);
  722.     if (mode == PRINT)
  723.         {
  724.         printf ("     type =        %d\n", b->type);
  725.         printf ("     serial =      %ld\n", b->serial);
  726.         printf ("     send_event =  %ld\n", b->send_event);
  727.         printf ("     display =     %x\n", b->display);
  728.         printf ("     window =      %x\n", b->window);
  729.         printf ("     root =        %x\n", b->root);
  730.         printf ("     subwindow =   %x\n", b->subwindow);
  731.         printf ("     time =        %x\n", b->time);
  732.         printf ("     x =           %d\n", b->x);
  733.         printf ("     y =           %d\n", b->y);
  734.         printf ("     x_root =      %d\n", b->x_root);
  735.         printf ("     y_root =      %d\n", b->y_root);
  736.         printf ("     state =       %d\n", b->state);
  737.         printf ("     button =      %x\n", b->button);
  738.         printf ("     same_screen = %d\n", b->same_screen);
  739.             }
  740.         }
  741.     else if (event->type == devicemotionnotify)
  742.     {
  743.     M = (XDeviceMotionEvent * ) event;
  744.     if (mode > NOPRINT)
  745.         {
  746.         printf ("Client: %d Device motion event device=%d\n", 
  747.         clientno, M->deviceid);
  748.         printf ("     is_hint =     %x\n", M->is_hint);
  749.         }
  750.     if (mode == PRINT)
  751.         {
  752.         printf ("     type =          %d\n", M->type);
  753.         printf ("     serial =        %ld\n", M->serial);
  754.         printf ("     send_event =    %ld\n", M->send_event);
  755.         printf ("     display =       %x\n", M->display);
  756.         printf ("     window =        %x\n", M->window);
  757.         printf ("     root =        %x\n", M->root);
  758.         printf ("     subwindow =   %x\n", M->subwindow);
  759.         printf ("     time =        %x\n", M->time);
  760.         printf ("     x =           %d\n", M->x);
  761.         printf ("     y =           %d\n", M->y);
  762.         printf ("     x_root =      %d\n", M->x_root);
  763.         printf ("     y_root =      %d\n", M->y_root);
  764.         printf ("     state =       %d\n", M->state);
  765.         printf ("     same_screen = %d\n", M->same_screen);
  766.         printf ("     device_state =%d\n", M->device_state);
  767.         printf ("     axes_count  = %d\n", M->axes_count);
  768.         printf ("     first_axis  = %d\n", M->first_axis);
  769.         }
  770.     }
  771.     else if (event->type == devicefocusin || event->type == devicefocusout)
  772.     {
  773.     f = (XDeviceFocusChangeEvent * ) event;
  774.     if (mode > NOPRINT)
  775.         printf ("Client: %d Device focus event device=%d\n", 
  776.         clientno, f->deviceid);
  777.     if (mode == PRINT)
  778.         {
  779.         printf ("     type =          %d\n", f->type);
  780.         printf ("     serial =        %ld\n", f->serial);
  781.         printf ("     send_event =    %ld\n", f->send_event);
  782.         printf ("     display =       %x\n", f->display);
  783.         printf ("     window =        %x\n", f->window);
  784.         printf ("     time =          %x\n", f->time);
  785.         printf ("     mode =          %x\n", f->mode);
  786.         printf ("     detail =        %x\n", f->detail);
  787.         }
  788.     }
  789.     else if (event->type == proximityin || event->type == proximityout)
  790.     {
  791.     p = (XProximityNotifyEvent * ) event;
  792.     if (mode > NOPRINT)
  793.         printf ("Client: %d Device proximity event device=%d\n", 
  794.         clientno, p->deviceid);
  795.     if (mode == PRINT)
  796.         {
  797.         printf ("     type =          %d\n", p->type);
  798.         printf ("     serial =        %ld\n", p->serial);
  799.         printf ("     send_event =    %ld\n", p->send_event);
  800.         printf ("     display =       %x\n", p->display);
  801.         printf ("     window =        %x\n", p->window);
  802.         printf ("     root =        %x\n", p->root);
  803.         printf ("     subwindow =   %x\n", p->subwindow);
  804.         printf ("     time =        %x\n", p->time);
  805.         printf ("     x =           %d\n", p->x);
  806.         printf ("     y =           %d\n", p->y);
  807.         printf ("     x_root =      %d\n", p->x_root);
  808.         printf ("     y_root =      %d\n", p->y_root);
  809.         printf ("     state =       %d\n", p->state);
  810.         printf ("     same_screen = %d\n", p->same_screen);
  811.         printf ("     axes_count  = %d\n", p->axes_count);
  812.         printf ("     first_axis  = %d\n", p->first_axis);
  813.         }
  814.     }
  815.     else if (event->type == devicemappingnotify)
  816.     {
  817.     m = (XDeviceMappingEvent * ) event;
  818.     if (mode > NOPRINT)
  819.         printf ("Client: %d Device mapping event device=%d\n", 
  820.         clientno, m->deviceid);
  821.     if (mode == PRINT)
  822.         {
  823.         printf ("     type =          %d\n", m->type);
  824.         printf ("     serial =        %ld\n", m->serial);
  825.         printf ("     send_event =    %ld\n", m->send_event);
  826.         printf ("     display =       %x\n", m->display);
  827.         printf ("     window =        %x\n", m->window);
  828.         printf ("     time =          %x\n", m->time);
  829.         printf ("     request =       %x\n", m->request);
  830.         printf ("     first_keycode = %x\n", m->first_keycode);
  831.         printf ("     count =         %x\n", m->count);
  832.         }
  833.     }
  834.     else if (event->type == devicestatenotify)
  835.     {
  836.     s = (XDeviceStateNotifyEvent * ) event;
  837.     if (mode > NOPRINT)
  838.         printf ("Client: %d Device state notify event device=%d\n", 
  839.         clientno, s->deviceid);
  840.     if (mode == PRINT)
  841.     {
  842.     printf ("     type =          %d\n", s->type);
  843.     printf ("     serial =        %ld\n", s->serial);
  844.     printf ("     send_event =    %ld\n", s->send_event);
  845.     printf ("     display =       %x\n", s->display);
  846.     printf ("     window =        %x\n", s->window);
  847.     printf ("     time =          %x\n", s->time);
  848.     printf ("     num_classes =   %x\n", s->num_classes);
  849.     printf ("     data =          %x\n", s->data);
  850.     anyclass = (XInputClass *) &s->data[0];
  851.     for (i=0; i<s->num_classes; i++)
  852.         {
  853.         switch (anyclass->class)
  854.         {
  855.         case KeyClass:
  856.             kdata = (XKeyStatus *) anyclass;
  857.             printf ("num_keys is %d\n",kdata->num_keys);
  858.             for (j=0; j<kdata->num_keys/8; j++)
  859.             printf ("%x ", *(kdata->keys+j));
  860.             printf ("\n");
  861.             anyclass = (XInputClass *) ++kdata;
  862.             break;
  863.         case ButtonClass:
  864.             bdata = (XButtonStatus *) anyclass;
  865.             printf ("num_buttons is %d\n",bdata->num_buttons);
  866.             for (j=0; j<bdata->num_buttons/8; j++)
  867.             printf ("%x ", *(bdata->buttons+j));
  868.             printf ("\n");
  869.             anyclass = (XInputClass *) ++bdata;
  870.             break;
  871.         case ValuatorClass:
  872.             vdata = (XValuatorStatus *) anyclass;
  873.             printf ("num_valuators is %d\n",vdata->num_valuators);
  874.             for (j=0; j<vdata->num_valuators; j++)
  875.                 printf ("valuator %d has value %x\n",
  876.                 j, *(vdata->valuators+j));
  877.             anyclass = (XInputClass *) ++vdata;
  878.             break;
  879.             }
  880.         }
  881.     }
  882.     }
  883.     else if (event->type == changedevicenotify)
  884.     {
  885.     c = (XChangeDeviceNotifyEvent * ) event;
  886.     if (mode > NOPRINT)
  887.         printf ("Client: %d Device change event device=%d\n", 
  888.         clientno, c->deviceid);
  889.     if (mode == PRINT)
  890.         {
  891.         printf ("     type =          %d\n", c->type);
  892.         printf ("     serial =        %ld\n", c->serial);
  893.         printf ("     send_event =    %ld\n", c->send_event);
  894.         printf ("     display =       %x\n", c->display);
  895.         printf ("     window =        %x\n", c->window);
  896.         printf ("     time =          %x\n", c->time);
  897.         printf ("     request =       %x\n", c->request);
  898.         }
  899.     }
  900.     else if (event->type == Expose)
  901.     {
  902.     if (mode > NOPRINT)
  903.         printf ("Expose event\n");
  904.     return (0);
  905.     }
  906.     else 
  907.     {
  908.     buf = (char *) event;
  909.     printf ("unknown event!\n");
  910.     for (i=0; i<32; i++)
  911.         printf ("%x ",buf[i]);
  912.         return (1);
  913.     }
  914.     return (0);
  915.     }
  916.  
  917. /***********************************************************************
  918.  *
  919.  * This function initializes an X error handler.
  920.  *
  921.  */
  922.  
  923. init_error_handler (disp)
  924.     Display    *disp;
  925.     {
  926.     int         event;
  927.     int         handle_x_errors();
  928.  
  929.     XQueryExtension (disp, "XInputExtension", &major_code, &event, &first_err);
  930.     XSetErrorHandler (handle_x_errors);
  931.     }
  932.  
  933. /***********************************************************************
  934.  *
  935.  * This function handles X errors.
  936.  *
  937.  */
  938.  
  939. handle_x_errors (disp, error)
  940.     Display    *disp;
  941.     XErrorEvent *error;
  942.     {
  943.     char buf[256];
  944.  
  945.     if (error->request_code == major_code &&
  946.         error->minor_code == error_code &&
  947.         error->error_code == expect)
  948.         if (error->error_code >= first_err &&
  949.             error->error_code <= first_err+4)
  950.         {
  951.         if (Dflag)
  952.                 printf ("%s returned %s correctly.\n",
  953.             extfuncs[error->minor_code],
  954.             ext_errors[error->error_code-first_err]);
  955.         return;
  956.         }
  957.     else
  958.         {
  959.         if (Dflag)
  960.                 printf ("%s returned %s correctly.\n",
  961.             extfuncs[error->minor_code],
  962.                 std_errors[error->error_code]);
  963.         return;
  964.         }
  965.     if (error->request_code == major_code)
  966.         printf ("    Minor code=%s.\n",extfuncs[error->minor_code]);
  967.     else
  968.         printf ("    Minor code=%d.\n",error->minor_code);
  969.     
  970.     if (error->error_code >= Success &&
  971.         error->error_code <= BadImplementation)
  972.         printf ("    Error code=%s.\n",std_errors[error->error_code]);
  973.     else if (error->error_code >= first_err &&
  974.         error->error_code <= first_err+4)
  975.         printf ("    Error code=%s.\n",ext_errors[error->error_code-first_err]);
  976.     else
  977.         printf ("    Error code=%d.\n",error->error_code);
  978.     printf ("    Resource id=%d.\n",error->resourceid);
  979.     return (0);
  980.     }
  981.  
  982. process_events (display, mode)
  983.     Display *display;
  984.     int    mode;
  985.     {
  986.     int    ret;
  987.     int    count = 0;
  988.     XEvent event;
  989.  
  990.     XSync (display,0);
  991.     while (XPending (display) > 0)
  992.     {
  993.     XNextEvent (display,&event);
  994.     process_device_events (display, &event, mode);
  995.     count++;
  996.     }
  997.     if (Dflag)
  998.     printf ("processed %d events\n",count);
  999.     }
  1000.  
  1001. process_args (argc, argv)
  1002.     int argc;
  1003.     char *argv[];
  1004.     {
  1005.     int i;
  1006.     char *c;
  1007.  
  1008.     if (argc == 1)
  1009.     {
  1010.     printf ("Usage: -s [desired events]\n");
  1011.     printf ("           K  = DeviceKeyPress\n");
  1012.     printf ("           k  = DeviceKeyRelease\n");
  1013.     printf ("           B  = DeviceButtonPress\n");
  1014.     printf ("           b  = DeviceButtonRelease\n");
  1015.     printf ("           M  = DeviceMotionNotify\n");
  1016.     printf ("           P  = ProximityIn\n");
  1017.     printf ("           p  = ProximityOut\n");
  1018.     printf ("           F  = DeviceFocusChange\n");
  1019.     printf ("           S  = DeviceStateNotify\n");
  1020.     printf ("           m  = DeviceMappingNotify\n");
  1021.     printf ("           C  = ChangeDeviceNotify\n");
  1022.     printf ("           H  = DeviceMotionHint\n");
  1023.     printf ("           G  = auto-grab on DeviceButtonPress\n");
  1024.     printf ("           O  = DeviceOwnerGrabButton\n");
  1025.     printf ("           1  = DeviceButton1Motion\n");
  1026.     printf ("           2  = DeviceButton2Motion\n");
  1027.     printf ("           3  = DeviceButton3Motion\n");
  1028.     printf ("           4  = DeviceButton4Motion\n");
  1029.     printf ("           5  = DeviceButton5Motion\n");
  1030.     printf ("           6  = DeviceButtonMotion\n\n");
  1031.     printf ("Usage: -p [event fields print level]\n");
  1032.     printf ("           0  = nothing\n");
  1033.     printf ("           1  = 1 line per event (default)\n");
  1034.     printf ("           2  = all event fields\n\n");
  1035.     printf ("Usage: -D [turn debug info on.]\n\n");
  1036.     printf ("Press space bar on the X keyboard to terminate.\n");
  1037.     }
  1038.     while (--argc > 0)                /* while parameters are left  */
  1039.     {
  1040.     if ((++argv)[0][0] == '-')
  1041.     switch ((argv)[0][1])
  1042.         {
  1043.         case 's':
  1044.         printf ("setting Event selections.\n");
  1045.         for (i=0,c=(++argv)[0]; i<strlen(argv[0]); i++,c++)
  1046.             switch (*c)
  1047.             {
  1048.             case 'K':        /* DeviceKeyPress     */
  1049.                 devicekeypressflag++;
  1050.                 break;
  1051.             case 'k':        /* DeviceKeyRelase    */
  1052.                 devicekeyreleaseflag++;
  1053.                 break;
  1054.             case 'B':        /* DeviceButtonPress     */
  1055.                 devicebuttonpressflag++;
  1056.                 break;
  1057.             case 'b':        /* DeviceButtonRelease     */
  1058.                 devicebuttonreleaseflag++;
  1059.                 break;
  1060.             case 'M':        /* DeviceMotionNotify    */
  1061.                 devicemotionnotifyflag++;
  1062.                 break;
  1063.             case 'P':        /* Proximity        */
  1064.                 proximityinflag++;
  1065.                 break;
  1066.             case 'p':        /* Proximity        */
  1067.                 proximityoutflag++;
  1068.                 break;
  1069.             case 'F':        /* DeviceFocusChange    */
  1070.                 devicefocuschangeflag++;
  1071.                 break;
  1072.             case 'S':        /* DeviceStateNotify    */
  1073.                 devicestatenotifyflag++;
  1074.                 break;
  1075.             case 'm':        /* DeviceMappingNotify    */
  1076.                 devicemappingnotifyflag++;
  1077.                 break;
  1078.             case 'C':        /* ChangeDeviceNotify     */
  1079.                 changedevicenotifyflag++;
  1080.                 break;
  1081.             case 'H':        /* DeviceMotionHint    */
  1082.                 devicemotionhintflag++;
  1083.                 break;
  1084.             case 'G':        /* DeviceButtonPressGrab*/
  1085.                 devicebuttonpressgrabflag++;
  1086.                 break;
  1087.             case 'O':        /* DeviceOwnerGrabButton*/
  1088.                 deviceownergrabbuttonflag++;
  1089.                 break;
  1090.             case '1':        /* DeviceButton1Motion  */
  1091.                 devicebutton1motionflag++;
  1092.                 break;
  1093.             case '2':        /* DeviceButton2Motion  */
  1094.                 devicebutton2motionflag++;
  1095.                 break;
  1096.             case '3':        /* DeviceButton3Motion  */
  1097.                 devicebutton3motionflag++;
  1098.                 break;
  1099.             case '4':        /* DeviceButton4Motion  */
  1100.                 devicebutton4motionflag++;
  1101.                 break;
  1102.             case '5':        /* DeviceButton5Motion  */
  1103.                 devicebutton5motionflag++;
  1104.                 break;
  1105.             case '6':        /* DeviceButtonMotion    */
  1106.                 devicebuttonmotionflag++;
  1107.                 break;
  1108.             default:
  1109.                 break;
  1110.             }
  1111.         break;
  1112.         case 'p':
  1113.         printf ("setting print level.\n");
  1114.         printmode = atoi((++argv)[0]);
  1115.         break;
  1116.         case 'D':
  1117.         printf ("setting debug flag.\n");
  1118.         Dflag = 1;
  1119.         break;
  1120.         default:
  1121.         printf ("Unknown argument.\n");
  1122.         }
  1123.     }
  1124.     }
  1125.