src/video/x11/SDL_x11mouse.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 17 Sep 2008 08:20:57 +0000
changeset 2763 6fc50bdd88c0
parent 2710 44e49d3fa6cf
child 2765 f55c87ae336b
permissions -rw-r--r--
Some cleanups on the new XInput code.

One or two things got moved around, but largely this is hooked up correctly
in the Unix configure system now: it can be dynamically loaded and fallback
gracefully if not available, or libXi can be directly linked to libSDL.

XInput support can be --disable'd from the configure script, too (defaults to
enabled).

Please note that while the framework is in place to gracefully fallback, the
current state of the source requires XInput. We'll need to adjust a few
things still to correct this.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 #include "SDL_x11video.h"
    24 #include "../../events/SDL_mouse_c.h"
    25 
    26 void
    27 X11_InitMouse(_THIS)
    28 {
    29 #if SDL_VIDEO_DRIVER_X11_XINPUT
    30     XDevice **newDevices;
    31     int i, j, index = 0, numOfDevices;
    32     XDeviceInfo *DevList;
    33     XAnyClassPtr deviceClass;
    34     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    35 
    36     SDL_XDevices = NULL;
    37     SDL_NumOfXDevices = 0;
    38 
    39     if (!SDL_X11_HAVE_XINPUT)
    40         return;  /* should have dynamically loaded, but wasn't available. */
    41 
    42     /* we're getting the list of input devices */
    43     DevList = XListInputDevices(data->display, &numOfDevices);
    44     SDL_XDevices = (XDevice **) SDL_malloc(sizeof(XDevice));
    45 
    46     /* we're aquiring valuators:mices, tablets, etc. */
    47     for (i = 0; i < numOfDevices; ++i) {
    48         /* if it's the core pointer or core keyborard we don't want it */
    49         if ((DevList[i].use != IsXPointer && DevList[i].use != IsXKeyboard)) {
    50             /* we have to check all of the device classes */
    51             deviceClass = DevList[i].inputclassinfo;
    52             for (j = 0; j < DevList[i].num_classes; ++j) {
    53                 if (deviceClass->class == ValuatorClass) {      /* bingo ;) */
    54                     XValuatorInfo *valInfo;
    55                     SDL_Mouse mouse;
    56 
    57                     newDevices =
    58                         (XDevice **) SDL_realloc(SDL_XDevices,
    59                                                  (index +
    60                                                   1) * sizeof(*newDevices));
    61                     if (!newDevices) {
    62                         SDL_OutOfMemory();
    63                         return;
    64                     }
    65                     SDL_XDevices = newDevices;
    66                     SDL_XDevices[index] =
    67                         XOpenDevice(data->display, DevList[i].id);
    68                     SDL_zero(mouse);
    69 
    70                     /* the id of the device differs from its index
    71                      * so we're assigning the index of a device to it's id */
    72                     SDL_SetMouseIndexId(DevList[i].id, index);
    73                     /* lets get the device parameters */
    74                     valInfo = (XValuatorInfo *) deviceClass;
    75                     /* if the device reports pressure, lets check it parameteres */
    76                     if (valInfo->num_axes > 2) {
    77                         data->mouse =
    78                             SDL_AddMouse(&mouse, index++, DevList[i].name,
    79                                          valInfo->axes[2].max_value,
    80                                          valInfo->axes[2].min_value, 1);
    81                     } else {
    82                         data->mouse =
    83                             SDL_AddMouse(&mouse, index++, DevList[i].name, 0,
    84                                          0, 1);
    85                     }
    86                     break;
    87                 }
    88                 /* if it's not class we're interested in, lets go further */
    89                 deviceClass =
    90                     (XAnyClassPtr) ((char *) deviceClass +
    91                                     deviceClass->length);
    92             }
    93         }
    94     }
    95     XFreeDeviceList(DevList);
    96 
    97     SDL_NumOfXDevices = index;
    98 #endif
    99 }
   100 
   101 void
   102 X11_QuitMouse(_THIS)
   103 {
   104     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   105 
   106     /* !!! FIXME: use XCloseDevice()? Or maybe handle under SDL_MouseQuit()? */
   107 
   108     /* let's delete all of the mice */
   109     SDL_MouseQuit();
   110 }
   111 
   112 /* vi: set ts=4 sw=4 expandtab: */