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

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