src/video/x11/SDL_x11mouse.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 08 Dec 2008 00:27:32 +0000
changeset 2859 99210400e8b9
parent 2765 f55c87ae336b
child 2940 b93965a16fe0
permissions -rw-r--r--
Updated copyright date
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 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         /* should have dynamically loaded, but wasn't available. */
    41         return;
    42     }
    43 
    44     /* we're getting the list of input devices */
    45     DevList = XListInputDevices(data->display, &numOfDevices);
    46     SDL_XDevices = (XDevice **) SDL_malloc(sizeof(XDevice));
    47 
    48     /* we're aquiring valuators:mices, tablets, etc. */
    49     for (i = 0; i < numOfDevices; ++i) {
    50         /* if it's the core pointer or core keyborard we don't want it */
    51         if ((DevList[i].use != IsXPointer && DevList[i].use != IsXKeyboard)) {
    52             /* we have to check all of the device classes */
    53             deviceClass = DevList[i].inputclassinfo;
    54             for (j = 0; j < DevList[i].num_classes; ++j) {
    55                 if (deviceClass->class == ValuatorClass) {      /* bingo ;) */
    56                     XValuatorInfo *valInfo;
    57                     SDL_Mouse mouse;
    58 
    59                     newDevices =
    60                         (XDevice **) SDL_realloc(SDL_XDevices,
    61                                                  (index +
    62                                                   1) * sizeof(*newDevices));
    63                     if (!newDevices) {
    64                         SDL_OutOfMemory();
    65                         return;
    66                     }
    67                     SDL_XDevices = newDevices;
    68                     SDL_XDevices[index] =
    69                         XOpenDevice(data->display, DevList[i].id);
    70                     SDL_zero(mouse);
    71 
    72                     /* the id of the device differs from its index
    73                      * so we're assigning the index of a device to it's id */
    74                     SDL_SetMouseIndexId(DevList[i].id, index);
    75                     /* lets get the device parameters */
    76                     valInfo = (XValuatorInfo *) deviceClass;
    77                     /* if the device reports pressure, lets check it parameteres */
    78                     if (valInfo->num_axes > 2) {
    79                         data->mouse =
    80                             SDL_AddMouse(&mouse, index++, DevList[i].name,
    81                                          valInfo->axes[2].max_value,
    82                                          valInfo->axes[2].min_value, 1);
    83                     } else {
    84                         data->mouse =
    85                             SDL_AddMouse(&mouse, index++, DevList[i].name, 0,
    86                                          0, 1);
    87                     }
    88                     break;
    89                 }
    90                 /* if it's not class we're interested in, lets go further */
    91                 deviceClass =
    92                     (XAnyClassPtr) ((char *) deviceClass +
    93                                     deviceClass->length);
    94             }
    95         }
    96     }
    97     XFreeDeviceList(DevList);
    98 
    99     SDL_NumOfXDevices = index;
   100 #endif
   101 }
   102 
   103 void
   104 X11_QuitMouse(_THIS)
   105 {
   106     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   107 
   108     /* !!! FIXME: use XCloseDevice()? Or maybe handle under SDL_MouseQuit()? */
   109 
   110     /* let's delete all of the mice */
   111     SDL_MouseQuit();
   112 }
   113 
   114 /* vi: set ts=4 sw=4 expandtab: */