src/video/x11/SDL_x11mouse.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 01 Jan 2009 08:21:19 +0000
changeset 2942 1e431c2631ee
parent 2940 b93965a16fe0
child 2952 79dee7da6ba5
permissions -rw-r--r--
indent
     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 "SDL_x11mouse.h"
    25 #include "../../events/SDL_mouse_c.h"
    26 
    27 #if SDL_VIDEO_DRIVER_X11_XINPUT
    28 static void
    29 X11_FreeMouse(SDL_Mouse * mouse)
    30 {
    31     X11_MouseData *data = (X11_MouseData *) mouse->driverdata;
    32 
    33     if (data) {
    34         XCloseDevice(data->display, mouse->id);
    35         SDL_free(data);
    36     }
    37 }
    38 #endif
    39 
    40 void
    41 X11_InitMouse(_THIS)
    42 {
    43     SDL_Mouse mouse;
    44 #if SDL_VIDEO_DRIVER_X11_XINPUT
    45     Display *display = ((SDL_VideoData *) _this->driverdata)->display;
    46     X11_MouseData *data;
    47     int i, j, n;
    48     XDeviceInfo *DevList;
    49     XAnyClassPtr deviceClass;
    50     int event_code;
    51     XEventClass xEvent;
    52 #endif
    53 
    54     SDL_zero(mouse);
    55     SDL_AddMouse(&mouse, "CorePointer", 0, 0, 1);
    56 
    57 #if SDL_VIDEO_DRIVER_X11_XINPUT
    58     if (!SDL_X11_HAVE_XINPUT) {
    59         /* should have dynamically loaded, but wasn't available. */
    60         return;
    61     }
    62 
    63     /* we're getting the list of input devices */
    64     DevList = XListInputDevices(display, &n);
    65 
    66     /* we're aquiring valuators: mice, tablets, etc. */
    67     for (i = 0; i < n; ++i) {
    68         /* if it's the core pointer or core keyborard we don't want it */
    69         if ((DevList[i].use != IsXPointer && DevList[i].use != IsXKeyboard)) {
    70             /* we have to check all of the device classes */
    71             deviceClass = DevList[i].inputclassinfo;
    72             for (j = 0; j < DevList[i].num_classes; ++j) {
    73                 if (deviceClass->class == ValuatorClass) {      /* bingo ;) */
    74                     XValuatorInfo *valInfo;
    75 
    76                     data = (X11_MouseData *) SDL_calloc(1, sizeof(*data));
    77                     if (!data) {
    78                         continue;
    79                     }
    80                     data->display = display;
    81                     data->device = XOpenDevice(display, DevList[i].id);
    82 
    83                     /* motion events */
    84                     DeviceMotionNotify(data->device, event_code, xEvent);
    85                     if (xEvent) {
    86                         data->xevents[data->num_xevents++] = xEvent;
    87                         data->motion = event_code;
    88                     }
    89 
    90                     /* button events */
    91                     DeviceButtonPress(data->device, event_code, xEvent);
    92                     if (xEvent) {
    93                         data->xevents[data->num_xevents++] = xEvent;
    94                         data->button_pressed = event_code;
    95                     }
    96                     DeviceButtonRelease(data->device, event_code, xEvent);
    97                     if (xEvent) {
    98                         data->xevents[data->num_xevents++] = xEvent;
    99                         data->button_released = event_code;
   100                     }
   101 
   102                     /* proximity events */
   103                     ProximityIn(data->device, event_code, xEvent);
   104                     if (xEvent) {
   105                         data->xevents[data->num_xevents++] = xEvent;
   106                         data->proximity_in = event_code;
   107                     }
   108                     ProximityOut(data->device, event_code, xEvent);
   109                     if (xEvent) {
   110                         data->xevents[data->num_xevents++] = xEvent;
   111                         data->proximity_out = event_code;
   112                     }
   113 
   114                     SDL_zero(mouse);
   115                     mouse.id = DevList[i].id;
   116                     mouse.FreeMouse = X11_FreeMouse;
   117                     mouse.driverdata = data;
   118 
   119                     /* lets get the device parameters */
   120                     valInfo = (XValuatorInfo *) deviceClass;
   121                     /* if the device reports pressure, lets check it parameteres */
   122                     if (valInfo->num_axes > 2) {
   123                         SDL_AddMouse(&mouse, DevList[i].name,
   124                                      valInfo->axes[2].max_value,
   125                                      valInfo->axes[2].min_value, 1);
   126                     } else {
   127                         SDL_AddMouse(&mouse, DevList[i].name, 0, 0, 1);
   128                     }
   129                     break;
   130                 }
   131                 /* if it's not class we're interested in, lets go further */
   132                 deviceClass =
   133                     (XAnyClassPtr) ((char *) deviceClass +
   134                                     deviceClass->length);
   135             }
   136         }
   137     }
   138     XFreeDeviceList(DevList);
   139 #endif
   140 }
   141 
   142 void
   143 X11_QuitMouse(_THIS)
   144 {
   145     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   146 
   147     /* !!! FIXME: use XCloseDevice()? Or maybe handle under SDL_MouseQuit()? */
   148 
   149     /* let's delete all of the mice */
   150     SDL_MouseQuit();
   151 }
   152 
   153 /* vi: set ts=4 sw=4 expandtab: */