Fixed X11 mouse motion/button events - it's not actually safe to cast mouse events to device events.
authorSam Lantinga
Thu, 01 Jan 2009 07:59:08 +0000
changeset 2940b93965a16fe0
parent 2939 084e5b4fc5be
child 2941 1d2bc7259f30
Fixed X11 mouse motion/button events - it's not actually safe to cast mouse events to device events.
Fixed building SDL without XInput support
Simplified the process of registering a mouse device
src/events/SDL_mouse.c
src/events/SDL_mouse_c.h
src/video/cocoa/SDL_cocoamouse.m
src/video/directfb/SDL_DirectFB_mouse.c
src/video/uikit/SDL_uikitview.m
src/video/win32/SDL_win32mouse.c
src/video/x11/SDL_x11events.c
src/video/x11/SDL_x11mouse.c
src/video/x11/SDL_x11mouse.h
src/video/x11/SDL_x11video.c
src/video/x11/SDL_x11video.h
src/video/x11/SDL_x11window.c
     1.1 --- a/src/events/SDL_mouse.c	Thu Jan 01 07:58:20 2009 +0000
     1.2 +++ b/src/events/SDL_mouse.c	Thu Jan 01 07:59:08 2009 +0000
     1.3 @@ -31,8 +31,6 @@
     1.4  static int SDL_num_mice = 0;
     1.5  static int SDL_current_mouse = -1;
     1.6  static SDL_Mouse **SDL_mice = NULL;
     1.7 -static int *SDL_IdIndex = NULL;
     1.8 -static int SDL_highestId = -1;
     1.9  
    1.10  
    1.11  /* Public functions */
    1.12 @@ -51,62 +49,44 @@
    1.13      return SDL_mice[index];
    1.14  }
    1.15  
    1.16 -int
    1.17 -SDL_SetMouseIndexId(int id, int index)
    1.18 +static int
    1.19 +SDL_GetMouseIndexId(int id)
    1.20  {
    1.21 -    if (id < 0) {
    1.22 -        SDL_SetError("Invalid Mouse ID");
    1.23 -        return -1;
    1.24 +    int index;
    1.25 +    SDL_Mouse *mouse;
    1.26 +
    1.27 +    for (index = 0; index < SDL_num_mice; ++index) {
    1.28 +        mouse = SDL_GetMouse(index);
    1.29 +        if (mouse->id == id) {
    1.30 +            return index;
    1.31 +        }
    1.32      }
    1.33 -    if (id > SDL_highestId) {
    1.34 -        int *indexes;
    1.35 -        int i;
    1.36 -        indexes = (int *) SDL_realloc(SDL_IdIndex, (id + 1) * sizeof(int));
    1.37 -        if (!indexes) {
    1.38 -            SDL_OutOfMemory();
    1.39 -            return -1;
    1.40 -        }
    1.41 -        SDL_IdIndex = indexes;
    1.42 -        for (i = SDL_highestId + 1; i <= id; i++)
    1.43 -            SDL_IdIndex[i] = -1;
    1.44 -        SDL_IdIndex[id] = index;
    1.45 -        SDL_highestId = id;
    1.46 -    } else {
    1.47 -        SDL_IdIndex[id] = index;
    1.48 -    }
    1.49 -    return 1;
    1.50 +    return -1;
    1.51  }
    1.52  
    1.53  int
    1.54 -SDL_GetMouseIndexId(int id)
    1.55 -{
    1.56 -    if (id < 0 || id > SDL_highestId) {
    1.57 -        return -1;
    1.58 -    }
    1.59 -    return SDL_IdIndex[id];
    1.60 -}
    1.61 -
    1.62 -int
    1.63 -SDL_AddMouse(const SDL_Mouse * mouse, int index, char *name, int pressure_max,
    1.64 +SDL_AddMouse(const SDL_Mouse * mouse, char *name, int pressure_max,
    1.65               int pressure_min, int ends)
    1.66  {
    1.67      SDL_Mouse **mice;
    1.68      int selected_mouse;
    1.69 -    int length;
    1.70 +    int index, length;
    1.71 +
    1.72 +    if (SDL_GetMouseIndexId(mouse->id) != -1) {
    1.73 +        SDL_SetError("Mouse ID already in use");
    1.74 +    }
    1.75  
    1.76      /* Add the mouse to the list of mice */
    1.77 -    if (index < 0 || index >= SDL_num_mice || SDL_mice[index]) {
    1.78 -        mice =
    1.79 -            (SDL_Mouse **) SDL_realloc(SDL_mice,
    1.80 -                                       (SDL_num_mice + 1) * sizeof(*mice));
    1.81 -        if (!mice) {
    1.82 -            SDL_OutOfMemory();
    1.83 -            return -1;
    1.84 -        }
    1.85 +    mice = (SDL_Mouse **) SDL_realloc(SDL_mice,
    1.86 +                                      (SDL_num_mice + 1) * sizeof(*mice));
    1.87 +    if (!mice) {
    1.88 +        SDL_OutOfMemory();
    1.89 +        return -1;
    1.90 +    }
    1.91  
    1.92 -        SDL_mice = mice;
    1.93 -        index = SDL_num_mice++;
    1.94 -    }
    1.95 +    SDL_mice = mice;
    1.96 +    index = SDL_num_mice++;
    1.97 +
    1.98      SDL_mice[index] = (SDL_Mouse *) SDL_malloc(sizeof(*SDL_mice[index]));
    1.99      if (!SDL_mice[index]) {
   1.100          SDL_OutOfMemory();
     2.1 --- a/src/events/SDL_mouse_c.h	Thu Jan 01 07:58:20 2009 +0000
     2.2 +++ b/src/events/SDL_mouse_c.h	Thu Jan 01 07:59:08 2009 +0000
     2.3 @@ -64,6 +64,7 @@
     2.4      int current_end;
     2.5  
     2.6      /* Data common to all mice */
     2.7 +    int id;
     2.8      SDL_WindowID focus;
     2.9      int which;
    2.10      int x;
    2.11 @@ -89,19 +90,13 @@
    2.12  /* Initialize the mouse subsystem */
    2.13  extern int SDL_MouseInit(void);
    2.14  
    2.15 -/* Assign an id to a mouse at an index */
    2.16 -extern int SDL_SetMouseIndexId(int id, int index);
    2.17 -
    2.18 -/* Get the index of a mouse specified by id */
    2.19 -extern int SDL_GetMouseIndexId(int id);
    2.20 -
    2.21  /* Get the mouse at an index */
    2.22  extern SDL_Mouse *SDL_GetMouse(int index);
    2.23  
    2.24  /* Add a mouse, possibly reattaching at a particular index (or -1),
    2.25     returning the index of the mouse, or -1 if there was an error.
    2.26   */
    2.27 -extern int SDL_AddMouse(const SDL_Mouse * mouse, int index, char *name,
    2.28 +extern int SDL_AddMouse(const SDL_Mouse * mouse, char *name,
    2.29                          int pressure_max, int pressure_min, int ends);
    2.30  
    2.31  /* Remove a mouse at an index, clearing the slot for later */
     3.1 --- a/src/video/cocoa/SDL_cocoamouse.m	Thu Jan 01 07:58:20 2009 +0000
     3.2 +++ b/src/video/cocoa/SDL_cocoamouse.m	Thu Jan 01 07:59:08 2009 +0000
     3.3 @@ -32,8 +32,7 @@
     3.4      SDL_Mouse mouse;
     3.5  
     3.6      SDL_zero(mouse);
     3.7 -    data->mouse = SDL_AddMouse(&mouse, -1, "Mouse", 0, 0, 1);
     3.8 -    SDL_SetMouseIndexId(data->mouse, data->mouse);
     3.9 +    data->mouse = SDL_AddMouse(&mouse, "Mouse", 0, 0, 1);
    3.10  }
    3.11  
    3.12  void
     4.1 --- a/src/video/directfb/SDL_DirectFB_mouse.c	Thu Jan 01 07:58:20 2009 +0000
     4.2 +++ b/src/video/directfb/SDL_DirectFB_mouse.c	Thu Jan 01 07:59:08 2009 +0000
     4.3 @@ -47,6 +47,7 @@
     4.4          SDL_Mouse mouse;
     4.5  
     4.6          SDL_zero(mouse);
     4.7 +        mouse.id = device_id;
     4.8          mouse.CreateCursor = DirectFB_CreateCursor;
     4.9          mouse.ShowCursor = DirectFB_ShowCursor;
    4.10          mouse.MoveCursor = DirectFB_MoveCursor;
    4.11 @@ -55,10 +56,8 @@
    4.12          mouse.FreeMouse = DirectFB_FreeMouse;
    4.13          mouse.cursor_shown = 1;
    4.14  
    4.15 -        SDL_SetMouseIndexId(device_id, devdata->num_mice);
    4.16 -        SDL_AddMouse(&mouse, devdata->num_mice, desc.name, 0, 0, 1);
    4.17 -        devdata->mouse_id[devdata->num_mice] = device_id;
    4.18 -        devdata->num_mice++;
    4.19 +        SDL_AddMouse(&mouse, desc.name, 0, 0, 1);
    4.20 +        devdata->mouse_id[devdata->num_mice++] = device_id;
    4.21      }
    4.22      return DFENUM_OK;
    4.23  }
    4.24 @@ -91,9 +90,7 @@
    4.25          mouse.FreeMouse = DirectFB_FreeMouse;
    4.26          mouse.cursor_shown = 1;
    4.27  
    4.28 -        SDL_SetMouseIndexId(0, 0);      /* ID == Index ! */
    4.29 -        devdata->mouse_id[0] = 0;
    4.30 -        SDL_AddMouse(&mouse, 0, "Mouse", 0, 0, 1);
    4.31 +        SDL_AddMouse(&mouse, "Mouse", 0, 0, 1);
    4.32          devdata->num_mice = 1;
    4.33      }
    4.34  }
     5.1 --- a/src/video/uikit/SDL_uikitview.m	Thu Jan 01 07:58:20 2009 +0000
     5.2 +++ b/src/video/uikit/SDL_uikitview.m	Thu Jan 01 07:59:08 2009 +0000
     5.3 @@ -49,8 +49,9 @@
     5.4  
     5.5  	int i;
     5.6  	for (i=0; i<MAX_SIMULTANEOUS_TOUCHES; i++) {
     5.7 +        mice[i].id = i;
     5.8  		mice[i].driverdata = NULL;
     5.9 -		SDL_AddMouse(&mice[i], i, "Mouse", 0, 0, 1);
    5.10 +		SDL_AddMouse(&mice[i], "Mouse", 0, 0, 1);
    5.11  	}
    5.12  	self.multipleTouchEnabled = YES;
    5.13  			
     6.1 --- a/src/video/win32/SDL_win32mouse.c	Thu Jan 01 07:58:20 2009 +0000
     6.2 +++ b/src/video/win32/SDL_win32mouse.c	Thu Jan 01 07:59:08 2009 +0000
     6.3 @@ -150,7 +150,7 @@
     6.4          /* we're saving the handle to the device */
     6.5          mice[index] = deviceList[i].hDevice;
     6.6          SDL_zero(mouse);
     6.7 -        SDL_SetMouseIndexId(index, index);
     6.8 +        mouse.id = index;
     6.9          l = SDL_strlen(device_name);
    6.10  
    6.11          /* we're checking if the device isn't by any chance a tablet */
    6.12 @@ -176,10 +176,10 @@
    6.13              data->WTInfoA(WTI_DEVICES, DVC_NPRESSURE, &pressure);
    6.14              data->WTInfoA(WTI_DEVICES, DVC_NCSRTYPES, &cursors);
    6.15              data->mouse =
    6.16 -                SDL_AddMouse(&mouse, index, device_name, pressure.axMax,
    6.17 +                SDL_AddMouse(&mouse, device_name, pressure.axMax,
    6.18                               pressure.axMin, cursors);
    6.19          } else {
    6.20 -            data->mouse = SDL_AddMouse(&mouse, index, device_name, 0, 0, 1);
    6.21 +            data->mouse = SDL_AddMouse(&mouse, device_name, 0, 0, 1);
    6.22          }
    6.23          ++index;
    6.24          SDL_free(buffer);
     7.1 --- a/src/video/x11/SDL_x11events.c	Thu Jan 01 07:58:20 2009 +0000
     7.2 +++ b/src/video/x11/SDL_x11events.c	Thu Jan 01 07:59:08 2009 +0000
     7.3 @@ -28,6 +28,7 @@
     7.4  #include "SDL_syswm.h"
     7.5  #include "SDL_x11video.h"
     7.6  #include "../../events/SDL_events_c.h"
     7.7 +#include "../../events/SDL_mouse_c.h"
     7.8  
     7.9  static void
    7.10  X11_DispatchEvent(_THIS)
    7.11 @@ -91,10 +92,11 @@
    7.12  #endif
    7.13              if ((xevent.xcrossing.mode != NotifyGrab) &&
    7.14                  (xevent.xcrossing.mode != NotifyUngrab)) {
    7.15 -                XDeviceMotionEvent *move = (XDeviceMotionEvent *) & xevent;
    7.16 -                SDL_SetMouseFocus(move->deviceid, data->windowID);
    7.17 -                SDL_SendMouseMotion(move->deviceid, 0, move->x,
    7.18 -                                    move->y, move->axis_data[2]);
    7.19 +                /* FIXME: Should we reset data for all mice? */
    7.20 +#if 0
    7.21 +                SDL_SetMouseFocus(0, data->windowID);
    7.22 +                SDL_SendMouseMotion(0, 0, move->x, move->y, 0);
    7.23 +#endif
    7.24              }
    7.25          }
    7.26          break;
    7.27 @@ -112,8 +114,10 @@
    7.28              if ((xevent.xcrossing.mode != NotifyGrab) &&
    7.29                  (xevent.xcrossing.mode != NotifyUngrab) &&
    7.30                  (xevent.xcrossing.detail != NotifyInferior)) {
    7.31 -                XDeviceMotionEvent *move = (XDeviceMotionEvent *) & xevent;
    7.32 -                SDL_SetMouseFocus(move->deviceid, 0);
    7.33 +                /* FIXME: Should we reset data for all mice? */
    7.34 +#if 0
    7.35 +                SDL_SetMouseFocus(0, 0);
    7.36 +#endif
    7.37              }
    7.38          }
    7.39          break;
    7.40 @@ -276,39 +280,69 @@
    7.41          }
    7.42          break;
    7.43  
    7.44 +    case MotionNotify:
    7.45 +#ifdef DEBUG_MOTION
    7.46 +        printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
    7.47 +#endif
    7.48 +        SDL_SendMouseMotion(0, 0, xevent.xmotion.x, xevent.xmotion.y, 0);
    7.49 +        break;
    7.50 +
    7.51 +    case ButtonPress:
    7.52 +        SDL_SendMouseButton(0, SDL_PRESSED, xevent.xbutton.button);
    7.53 +        break;
    7.54 +
    7.55 +    case ButtonRelease:
    7.56 +        SDL_SendMouseButton(0, SDL_RELEASED, xevent.xbutton.button);
    7.57 +        break;
    7.58 +
    7.59      default:{
    7.60 -            if (xevent.type == motion) {        /* MotionNotify */
    7.61 +#if SDL_VIDEO_DRIVER_X11_XINPUT
    7.62 +        for (i = 0; i < SDL_GetNumMice(); ++i) {
    7.63 +            SDL_Mouse *mouse;
    7.64 +            X11_MouseData *data;
    7.65 +
    7.66 +            mouse = SDL_GetMouse(i);
    7.67 +            data = (X11_MouseData *)mouse->driverdata;
    7.68 +            if (!data) {
    7.69 +                continue;
    7.70 +            }
    7.71 +                    
    7.72 +            if (xevent.type == data->motion) {          /* MotionNotify */
    7.73                  XDeviceMotionEvent *move = (XDeviceMotionEvent *) & xevent;
    7.74  #ifdef DEBUG_MOTION
    7.75                  printf("X11 motion: %d,%d\n", move->x, move->y);
    7.76  #endif
    7.77 -                SDL_SendMouseMotion(move->deviceid, 0, move->x,
    7.78 -                                    move->y, move->axis_data[2]);
    7.79 -            } else if (xevent.type == button_pressed) { /* ButtonPress */
    7.80 +                SDL_SendMouseMotion(move->deviceid, 0, move->x, move->y, move->axis_data[2]);
    7.81 +                return;
    7.82 +            }
    7.83 +            if (xevent.type == data->button_pressed) {  /* ButtonPress */
    7.84                  XDeviceButtonPressedEvent *pressed =
    7.85                      (XDeviceButtonPressedEvent *) & xevent;
    7.86 -                SDL_SendMouseButton(pressed->deviceid, SDL_PRESSED,
    7.87 -                                    pressed->button);
    7.88 -            } else if (xevent.type == button_released) {        /* ButtonRelease */
    7.89 +                SDL_SendMouseButton(pressed->deviceid, SDL_PRESSED, pressed->button);
    7.90 +                return;
    7.91 +            }
    7.92 +            if (xevent.type == data->button_released) { /* ButtonRelease */
    7.93                  XDeviceButtonReleasedEvent *released =
    7.94                      (XDeviceButtonReleasedEvent *) & xevent;
    7.95 -                SDL_SendMouseButton(released->deviceid, SDL_RELEASED,
    7.96 -                                    released->button);
    7.97 -            } else if (xevent.type == proximity_in) {
    7.98 +                SDL_SendMouseButton(released->deviceid, SDL_RELEASED, released->button);
    7.99 +                return;
   7.100 +            }
   7.101 +            if (xevent.type == data->proximity_in) {
   7.102                  XProximityNotifyEvent *proximity =
   7.103                      (XProximityNotifyEvent *) & xevent;
   7.104 -                SDL_SendProximity(proximity->deviceid, proximity->x,
   7.105 -                                  proximity->y, SDL_PROXIMITYIN);
   7.106 -            } else if (xevent.type == proximity_out) {
   7.107 +                SDL_SendProximity(proximity->deviceid, proximity->x, proximity->y, SDL_PROXIMITYIN);
   7.108 +                return;
   7.109 +            }
   7.110 +            if (xevent.type == data->proximity_out) {
   7.111                  XProximityNotifyEvent *proximity =
   7.112                      (XProximityNotifyEvent *) & xevent;
   7.113 -                SDL_SendProximity(proximity->deviceid, proximity->x,
   7.114 -                                  proximity->y, SDL_PROXIMITYOUT);
   7.115 +                SDL_SendProximity(proximity->deviceid, proximity->x, proximity->y, SDL_PROXIMITYOUT);
   7.116 +                return;
   7.117              }
   7.118 +        }
   7.119 +#endif
   7.120  #ifdef DEBUG_XEVENTS
   7.121 -            else {
   7.122 -                printf("Unhandled event %d\n", xevent.type);
   7.123 -            }
   7.124 +            printf("Unhandled event %d\n", xevent.type);
   7.125  #endif
   7.126          }
   7.127          break;
     8.1 --- a/src/video/x11/SDL_x11mouse.c	Thu Jan 01 07:58:20 2009 +0000
     8.2 +++ b/src/video/x11/SDL_x11mouse.c	Thu Jan 01 07:59:08 2009 +0000
     8.3 @@ -21,32 +21,50 @@
     8.4  */
     8.5  #include "SDL_config.h"
     8.6  #include "SDL_x11video.h"
     8.7 +#include "SDL_x11mouse.h"
     8.8  #include "../../events/SDL_mouse_c.h"
     8.9  
    8.10 +#if SDL_VIDEO_DRIVER_X11_XINPUT
    8.11 +static void
    8.12 +X11_FreeMouse(SDL_Mouse *mouse)
    8.13 +{
    8.14 +    X11_MouseData *data = (X11_MouseData *)mouse->driverdata;
    8.15 +
    8.16 +    if (data) {
    8.17 +        XCloseDevice(data->display, mouse->id);
    8.18 +        SDL_free(data);
    8.19 +    }
    8.20 +}
    8.21 +#endif
    8.22 +
    8.23  void
    8.24  X11_InitMouse(_THIS)
    8.25  {
    8.26 +    SDL_Mouse mouse;
    8.27  #if SDL_VIDEO_DRIVER_X11_XINPUT
    8.28 -    XDevice **newDevices;
    8.29 -    int i, j, index = 0, numOfDevices;
    8.30 +    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
    8.31 +    X11_MouseData *data;
    8.32 +    int i, j, n;
    8.33      XDeviceInfo *DevList;
    8.34      XAnyClassPtr deviceClass;
    8.35 -    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    8.36 +    int event_code;
    8.37 +    XEventClass xEvent;
    8.38 +#endif
    8.39  
    8.40 -    SDL_XDevices = NULL;
    8.41 -    SDL_NumOfXDevices = 0;
    8.42 +    SDL_zero(mouse);
    8.43 +    SDL_AddMouse(&mouse, "CorePointer", 0, 0, 1);
    8.44  
    8.45 +#if SDL_VIDEO_DRIVER_X11_XINPUT
    8.46      if (!SDL_X11_HAVE_XINPUT) {
    8.47          /* should have dynamically loaded, but wasn't available. */
    8.48          return;
    8.49      }
    8.50  
    8.51      /* we're getting the list of input devices */
    8.52 -    DevList = XListInputDevices(data->display, &numOfDevices);
    8.53 -    SDL_XDevices = (XDevice **) SDL_malloc(sizeof(XDevice));
    8.54 +    DevList = XListInputDevices(display, &n);
    8.55  
    8.56 -    /* we're aquiring valuators:mices, tablets, etc. */
    8.57 -    for (i = 0; i < numOfDevices; ++i) {
    8.58 +    /* we're aquiring valuators: mice, tablets, etc. */
    8.59 +    for (i = 0; i < n; ++i) {
    8.60          /* if it's the core pointer or core keyborard we don't want it */
    8.61          if ((DevList[i].use != IsXPointer && DevList[i].use != IsXKeyboard)) {
    8.62              /* we have to check all of the device classes */
    8.63 @@ -54,36 +72,59 @@
    8.64              for (j = 0; j < DevList[i].num_classes; ++j) {
    8.65                  if (deviceClass->class == ValuatorClass) {      /* bingo ;) */
    8.66                      XValuatorInfo *valInfo;
    8.67 -                    SDL_Mouse mouse;
    8.68 +
    8.69 +                    data = (X11_MouseData *)SDL_calloc(1, sizeof(*data));
    8.70 +                    if (!data) {
    8.71 +                        continue;
    8.72 +                    }
    8.73 +                    data->display = display;
    8.74 +                    data->device = XOpenDevice(display, DevList[i].id);
    8.75 +
    8.76 +                    /* motion events */
    8.77 +                    DeviceMotionNotify(data->device, event_code, xEvent);
    8.78 +                    if (xEvent) {
    8.79 +                        data->xevents[data->num_xevents++] = xEvent;
    8.80 +                        data->motion = event_code;
    8.81 +                    }
    8.82  
    8.83 -                    newDevices =
    8.84 -                        (XDevice **) SDL_realloc(SDL_XDevices,
    8.85 -                                                 (index +
    8.86 -                                                  1) * sizeof(*newDevices));
    8.87 -                    if (!newDevices) {
    8.88 -                        SDL_OutOfMemory();
    8.89 -                        return;
    8.90 +                    /* button events */
    8.91 +                    DeviceButtonPress(data->device, event_code, xEvent);
    8.92 +                    if (xEvent) {
    8.93 +                        data->xevents[data->num_xevents++] = xEvent;
    8.94 +                        data->button_pressed = event_code;
    8.95 +                    }
    8.96 +                    DeviceButtonRelease(data->device, event_code, xEvent);
    8.97 +                    if (xEvent) {
    8.98 +                        data->xevents[data->num_xevents++] = xEvent;
    8.99 +                        data->button_released = event_code;
   8.100                      }
   8.101 -                    SDL_XDevices = newDevices;
   8.102 -                    SDL_XDevices[index] =
   8.103 -                        XOpenDevice(data->display, DevList[i].id);
   8.104 +
   8.105 +                    /* proximity events */
   8.106 +                    ProximityIn(data->device, event_code, xEvent);
   8.107 +                    if (xEvent) {
   8.108 +                        data->xevents[data->num_xevents++] = xEvent;
   8.109 +                        data->proximity_in = event_code;
   8.110 +                    }
   8.111 +                    ProximityOut(data->device, event_code, xEvent);
   8.112 +                    if (xEvent) {
   8.113 +                        data->xevents[data->num_xevents++] = xEvent;
   8.114 +                        data->proximity_out = event_code;
   8.115 +                    }
   8.116 +
   8.117                      SDL_zero(mouse);
   8.118 +                    mouse.id = DevList[i].id;
   8.119 +                    mouse.FreeMouse = X11_FreeMouse;
   8.120 +                    mouse.driverdata = data;
   8.121  
   8.122 -                    /* the id of the device differs from its index
   8.123 -                     * so we're assigning the index of a device to it's id */
   8.124 -                    SDL_SetMouseIndexId(DevList[i].id, index);
   8.125                      /* lets get the device parameters */
   8.126                      valInfo = (XValuatorInfo *) deviceClass;
   8.127                      /* if the device reports pressure, lets check it parameteres */
   8.128                      if (valInfo->num_axes > 2) {
   8.129 -                        data->mouse =
   8.130 -                            SDL_AddMouse(&mouse, index++, DevList[i].name,
   8.131 +                            SDL_AddMouse(&mouse, DevList[i].name,
   8.132                                           valInfo->axes[2].max_value,
   8.133                                           valInfo->axes[2].min_value, 1);
   8.134                      } else {
   8.135 -                        data->mouse =
   8.136 -                            SDL_AddMouse(&mouse, index++, DevList[i].name, 0,
   8.137 -                                         0, 1);
   8.138 +                            SDL_AddMouse(&mouse, DevList[i].name, 0, 0, 1);
   8.139                      }
   8.140                      break;
   8.141                  }
   8.142 @@ -95,8 +136,6 @@
   8.143          }
   8.144      }
   8.145      XFreeDeviceList(DevList);
   8.146 -
   8.147 -    SDL_NumOfXDevices = index;
   8.148  #endif
   8.149  }
   8.150  
     9.1 --- a/src/video/x11/SDL_x11mouse.h	Thu Jan 01 07:58:20 2009 +0000
     9.2 +++ b/src/video/x11/SDL_x11mouse.h	Thu Jan 01 07:59:08 2009 +0000
     9.3 @@ -24,6 +24,21 @@
     9.4  #ifndef _SDL_x11mouse_h
     9.5  #define _SDL_x11mouse_h
     9.6  
     9.7 +#if SDL_VIDEO_DRIVER_X11_XINPUT
     9.8 +typedef struct X11_MouseData
     9.9 +{
    9.10 +    Display *display;
    9.11 +    XDevice *device;
    9.12 +    int motion;
    9.13 +    int button_pressed;
    9.14 +    int button_released;
    9.15 +    int proximity_in;
    9.16 +    int proximity_out;
    9.17 +    int num_xevents;
    9.18 +    XEventClass xevents[5];
    9.19 +} X11_MouseData;
    9.20 +#endif
    9.21 +
    9.22  extern void X11_InitMouse(_THIS);
    9.23  extern void X11_QuitMouse(_THIS);
    9.24  
    10.1 --- a/src/video/x11/SDL_x11video.c	Thu Jan 01 07:58:20 2009 +0000
    10.2 +++ b/src/video/x11/SDL_x11video.c	Thu Jan 01 07:59:08 2009 +0000
    10.3 @@ -28,14 +28,6 @@
    10.4  
    10.5  #include "SDL_x11video.h"
    10.6  
    10.7 -#if SDL_VIDEO_DRIVER_X11_XINPUT
    10.8 -XDevice **SDL_XDevices;
    10.9 -int SDL_NumOfXDevices;
   10.10 -XEventClass SDL_XEvents[256];
   10.11 -int SDL_NumOfXEvents;
   10.12 -int motion, button_pressed, button_released;    /* the definitions of the mice events */
   10.13 -int proximity_in, proximity_out;
   10.14 -#endif
   10.15  
   10.16  /* Initialization/Query functions */
   10.17  static int X11_VideoInit(_THIS);
   10.18 @@ -218,8 +210,6 @@
   10.19  int
   10.20  X11_VideoInit(_THIS)
   10.21  {
   10.22 -    int i, index = 0, event_code;
   10.23 -    XEventClass xEvent;
   10.24      SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   10.25  
   10.26      /* Get the window class name, usually the name of the application */
   10.27 @@ -253,49 +243,6 @@
   10.28      }
   10.29      X11_InitMouse(_this);
   10.30  
   10.31 -    /* Set reasonable defaults, in case !SDL_VIDEO_DRIVER_X11_XINPUT */
   10.32 -    motion = MotionNotify;
   10.33 -    button_pressed = ButtonPress;
   10.34 -    button_released = ButtonRelease;
   10.35 -
   10.36 -#if SDL_VIDEO_DRIVER_X11_XINPUT
   10.37 -    /* we're generating the table of events that should be recognized */
   10.38 -    for (i = 0; i < SDL_NumOfXDevices; ++i) {
   10.39 -        /* button events */
   10.40 -        DeviceButtonPress(SDL_XDevices[i], event_code, xEvent);
   10.41 -        if (xEvent) {
   10.42 -            SDL_XEvents[index++] = xEvent;
   10.43 -            button_pressed = event_code;
   10.44 -        }
   10.45 -        DeviceButtonRelease(SDL_XDevices[i], event_code, xEvent);
   10.46 -        if (xEvent) {
   10.47 -            SDL_XEvents[index++] = xEvent;
   10.48 -            button_released = event_code;
   10.49 -        }
   10.50 -
   10.51 -        /* proximity events */
   10.52 -        ProximityIn(SDL_XDevices[i], event_code, xEvent);
   10.53 -        if (xEvent) {
   10.54 -            SDL_XEvents[index++] = xEvent;
   10.55 -            proximity_in = event_code;
   10.56 -        }
   10.57 -        ProximityOut(SDL_XDevices[i], event_code, xEvent);
   10.58 -        if (xEvent) {
   10.59 -            SDL_XEvents[index++] = xEvent;
   10.60 -            proximity_out = event_code;
   10.61 -        }
   10.62 -
   10.63 -        /* motion events */
   10.64 -        DeviceMotionNotify(SDL_XDevices[i], event_code, xEvent);
   10.65 -        if (xEvent) {
   10.66 -            SDL_XEvents[index++] = xEvent;
   10.67 -            motion = event_code;
   10.68 -        }
   10.69 -
   10.70 -    }
   10.71 -    SDL_NumOfXEvents = index;
   10.72 -#endif
   10.73 -
   10.74      return 0;
   10.75  }
   10.76  
   10.77 @@ -318,10 +265,6 @@
   10.78      X11_QuitModes(_this);
   10.79      X11_QuitKeyboard(_this);
   10.80      X11_QuitMouse(_this);
   10.81 -
   10.82 -#if SDL_VIDEO_DRIVER_X11_XINPUT
   10.83 -    free(SDL_XDevices);
   10.84 -#endif
   10.85  }
   10.86  
   10.87  /* vim: set ts=4 sw=4 expandtab: */
    11.1 --- a/src/video/x11/SDL_x11video.h	Thu Jan 01 07:58:20 2009 +0000
    11.2 +++ b/src/video/x11/SDL_x11video.h	Thu Jan 01 07:59:08 2009 +0000
    11.3 @@ -61,22 +61,6 @@
    11.4  
    11.5  /* Private display data */
    11.6  
    11.7 -#if SDL_VIDEO_DRIVER_X11_XINPUT
    11.8 -/* !!! FIXME: should be in SDL_VideoData, not globals. */
    11.9 -extern XDevice **SDL_XDevices;
   11.10 -extern int SDL_NumOfXDevices;
   11.11 -extern XEventClass SDL_XEvents[256];
   11.12 -extern int SDL_NumOfXEvents;
   11.13 -#endif
   11.14 -
   11.15 -/* !!! FIXME: should be in SDL_VideoData, not globals. */
   11.16 -/* !!! FIXME: change these names, too. */
   11.17 -extern int motion;              /* the motion event id defined by an XInput function */
   11.18 -extern int button_pressed;      /* the button_pressed event id defined by an XInput function */
   11.19 -extern int button_released;     /* the button_released event id defined by an XInput function */
   11.20 -extern int proximity_in;        /* the proximity in event defined by an XInput function */
   11.21 -extern int proximity_out;       /* the proximity out event defined by an XInput function */
   11.22 -
   11.23  typedef struct SDL_VideoData
   11.24  {
   11.25      Display *display;
   11.26 @@ -87,7 +71,6 @@
   11.27      int numwindows;
   11.28      SDL_WindowData **windowlist;
   11.29      int windowlistlength;
   11.30 -    int mouse;
   11.31      int keyboard;
   11.32      Atom WM_DELETE_WINDOW;
   11.33      SDL_scancode key_layout[256];
    12.1 --- a/src/video/x11/SDL_x11window.c	Thu Jan 01 07:58:20 2009 +0000
    12.2 +++ b/src/video/x11/SDL_x11window.c	Thu Jan 01 07:59:08 2009 +0000
    12.3 @@ -24,8 +24,10 @@
    12.4  #include "SDL_syswm.h"
    12.5  #include "../SDL_sysvideo.h"
    12.6  #include "../../events/SDL_keyboard_c.h"
    12.7 +#include "../../events/SDL_mouse_c.h"
    12.8  
    12.9  #include "SDL_x11video.h"
   12.10 +#include "SDL_x11mouse.h"
   12.11  #include "../Xext/extensions/StdCmap.h"
   12.12  
   12.13  static void
   12.14 @@ -172,8 +174,6 @@
   12.15      XSizeHints *sizehints;
   12.16      XWMHints *wmhints;
   12.17      XClassHint *classhints;
   12.18 -    extern XEventClass SDL_XEvents[];
   12.19 -    extern int SDL_NumOfXEvents;
   12.20  
   12.21  #if SDL_VIDEO_DRIVER_X11_XINERAMA
   12.22  /* FIXME
   12.23 @@ -523,8 +523,31 @@
   12.24      }
   12.25  #endif
   12.26  
   12.27 +#if SDL_VIDEO_DRIVER_X11_XINPUT
   12.28      /* we're informing the display what extension events we want to receive from it */
   12.29 -    XSelectExtensionEvent(data->display, w, SDL_XEvents, SDL_NumOfXEvents);
   12.30 +    {
   12.31 +        int i, j, n = 0;
   12.32 +        XEventClass xevents[256];
   12.33 +
   12.34 +        for (i = 0; i < SDL_GetNumMice(); ++i) {
   12.35 +            SDL_Mouse *mouse;
   12.36 +            X11_MouseData *data;
   12.37 +
   12.38 +            mouse = SDL_GetMouse(i);
   12.39 +            data = (X11_MouseData *)mouse->driverdata;
   12.40 +            if (!data) {
   12.41 +                continue;
   12.42 +            }
   12.43 +
   12.44 +            for (j = 0; j < data->num_xevents; ++j) {
   12.45 +                xevents[n++] = data->xevents[j];
   12.46 +            }
   12.47 +        }
   12.48 +        if (n > 0) {
   12.49 +            XSelectExtensionEvent(data->display, w, xevents, n);
   12.50 +        }
   12.51 +    }
   12.52 +#endif
   12.53  
   12.54      return 0;
   12.55  }