src/video/x11/SDL_x11mouse.c
changeset 2940 b93965a16fe0
parent 2859 99210400e8b9
child 2942 1e431c2631ee
     1.1 --- a/src/video/x11/SDL_x11mouse.c	Thu Jan 01 07:58:20 2009 +0000
     1.2 +++ b/src/video/x11/SDL_x11mouse.c	Thu Jan 01 07:59:08 2009 +0000
     1.3 @@ -21,32 +21,50 @@
     1.4  */
     1.5  #include "SDL_config.h"
     1.6  #include "SDL_x11video.h"
     1.7 +#include "SDL_x11mouse.h"
     1.8  #include "../../events/SDL_mouse_c.h"
     1.9  
    1.10 +#if SDL_VIDEO_DRIVER_X11_XINPUT
    1.11 +static void
    1.12 +X11_FreeMouse(SDL_Mouse *mouse)
    1.13 +{
    1.14 +    X11_MouseData *data = (X11_MouseData *)mouse->driverdata;
    1.15 +
    1.16 +    if (data) {
    1.17 +        XCloseDevice(data->display, mouse->id);
    1.18 +        SDL_free(data);
    1.19 +    }
    1.20 +}
    1.21 +#endif
    1.22 +
    1.23  void
    1.24  X11_InitMouse(_THIS)
    1.25  {
    1.26 +    SDL_Mouse mouse;
    1.27  #if SDL_VIDEO_DRIVER_X11_XINPUT
    1.28 -    XDevice **newDevices;
    1.29 -    int i, j, index = 0, numOfDevices;
    1.30 +    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
    1.31 +    X11_MouseData *data;
    1.32 +    int i, j, n;
    1.33      XDeviceInfo *DevList;
    1.34      XAnyClassPtr deviceClass;
    1.35 -    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    1.36 +    int event_code;
    1.37 +    XEventClass xEvent;
    1.38 +#endif
    1.39  
    1.40 -    SDL_XDevices = NULL;
    1.41 -    SDL_NumOfXDevices = 0;
    1.42 +    SDL_zero(mouse);
    1.43 +    SDL_AddMouse(&mouse, "CorePointer", 0, 0, 1);
    1.44  
    1.45 +#if SDL_VIDEO_DRIVER_X11_XINPUT
    1.46      if (!SDL_X11_HAVE_XINPUT) {
    1.47          /* should have dynamically loaded, but wasn't available. */
    1.48          return;
    1.49      }
    1.50  
    1.51      /* we're getting the list of input devices */
    1.52 -    DevList = XListInputDevices(data->display, &numOfDevices);
    1.53 -    SDL_XDevices = (XDevice **) SDL_malloc(sizeof(XDevice));
    1.54 +    DevList = XListInputDevices(display, &n);
    1.55  
    1.56 -    /* we're aquiring valuators:mices, tablets, etc. */
    1.57 -    for (i = 0; i < numOfDevices; ++i) {
    1.58 +    /* we're aquiring valuators: mice, tablets, etc. */
    1.59 +    for (i = 0; i < n; ++i) {
    1.60          /* if it's the core pointer or core keyborard we don't want it */
    1.61          if ((DevList[i].use != IsXPointer && DevList[i].use != IsXKeyboard)) {
    1.62              /* we have to check all of the device classes */
    1.63 @@ -54,36 +72,59 @@
    1.64              for (j = 0; j < DevList[i].num_classes; ++j) {
    1.65                  if (deviceClass->class == ValuatorClass) {      /* bingo ;) */
    1.66                      XValuatorInfo *valInfo;
    1.67 -                    SDL_Mouse mouse;
    1.68  
    1.69 -                    newDevices =
    1.70 -                        (XDevice **) SDL_realloc(SDL_XDevices,
    1.71 -                                                 (index +
    1.72 -                                                  1) * sizeof(*newDevices));
    1.73 -                    if (!newDevices) {
    1.74 -                        SDL_OutOfMemory();
    1.75 -                        return;
    1.76 +                    data = (X11_MouseData *)SDL_calloc(1, sizeof(*data));
    1.77 +                    if (!data) {
    1.78 +                        continue;
    1.79                      }
    1.80 -                    SDL_XDevices = newDevices;
    1.81 -                    SDL_XDevices[index] =
    1.82 -                        XOpenDevice(data->display, DevList[i].id);
    1.83 +                    data->display = display;
    1.84 +                    data->device = XOpenDevice(display, DevList[i].id);
    1.85 +
    1.86 +                    /* motion events */
    1.87 +                    DeviceMotionNotify(data->device, event_code, xEvent);
    1.88 +                    if (xEvent) {
    1.89 +                        data->xevents[data->num_xevents++] = xEvent;
    1.90 +                        data->motion = event_code;
    1.91 +                    }
    1.92 +
    1.93 +                    /* button events */
    1.94 +                    DeviceButtonPress(data->device, event_code, xEvent);
    1.95 +                    if (xEvent) {
    1.96 +                        data->xevents[data->num_xevents++] = xEvent;
    1.97 +                        data->button_pressed = event_code;
    1.98 +                    }
    1.99 +                    DeviceButtonRelease(data->device, event_code, xEvent);
   1.100 +                    if (xEvent) {
   1.101 +                        data->xevents[data->num_xevents++] = xEvent;
   1.102 +                        data->button_released = event_code;
   1.103 +                    }
   1.104 +
   1.105 +                    /* proximity events */
   1.106 +                    ProximityIn(data->device, event_code, xEvent);
   1.107 +                    if (xEvent) {
   1.108 +                        data->xevents[data->num_xevents++] = xEvent;
   1.109 +                        data->proximity_in = event_code;
   1.110 +                    }
   1.111 +                    ProximityOut(data->device, event_code, xEvent);
   1.112 +                    if (xEvent) {
   1.113 +                        data->xevents[data->num_xevents++] = xEvent;
   1.114 +                        data->proximity_out = event_code;
   1.115 +                    }
   1.116 +
   1.117                      SDL_zero(mouse);
   1.118 +                    mouse.id = DevList[i].id;
   1.119 +                    mouse.FreeMouse = X11_FreeMouse;
   1.120 +                    mouse.driverdata = data;
   1.121  
   1.122 -                    /* the id of the device differs from its index
   1.123 -                     * so we're assigning the index of a device to it's id */
   1.124 -                    SDL_SetMouseIndexId(DevList[i].id, index);
   1.125                      /* lets get the device parameters */
   1.126                      valInfo = (XValuatorInfo *) deviceClass;
   1.127                      /* if the device reports pressure, lets check it parameteres */
   1.128                      if (valInfo->num_axes > 2) {
   1.129 -                        data->mouse =
   1.130 -                            SDL_AddMouse(&mouse, index++, DevList[i].name,
   1.131 +                            SDL_AddMouse(&mouse, DevList[i].name,
   1.132                                           valInfo->axes[2].max_value,
   1.133                                           valInfo->axes[2].min_value, 1);
   1.134                      } else {
   1.135 -                        data->mouse =
   1.136 -                            SDL_AddMouse(&mouse, index++, DevList[i].name, 0,
   1.137 -                                         0, 1);
   1.138 +                            SDL_AddMouse(&mouse, DevList[i].name, 0, 0, 1);
   1.139                      }
   1.140                      break;
   1.141                  }
   1.142 @@ -95,8 +136,6 @@
   1.143          }
   1.144      }
   1.145      XFreeDeviceList(DevList);
   1.146 -
   1.147 -    SDL_NumOfXDevices = index;
   1.148  #endif
   1.149  }
   1.150