src/video/x11/SDL_x11mouse.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Dec 2009 08:11:06 +0000
changeset 3565 f43c8f688f77
parent 3195 08747e24a50f
child 3697 f7b03b6838cb
permissions -rw-r--r--
Fixed bug #906

Added better error reporting for OpenGL context creation failing.
     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, data->device);
    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     int num_mice = 0;
    54 
    55     SDL_zero(mouse);
    56 
    57 #if SDL_VIDEO_DRIVER_X11_XINPUT
    58     /* we're getting the list of input devices */
    59     n = 0;
    60     if (SDL_X11_HAVE_XINPUT) {
    61         DevList = XListInputDevices(display, &n);
    62     }
    63 
    64     /* we're aquiring valuators: mice, tablets, etc. */
    65     for (i = 0; i < n; ++i) {
    66         /* if it's the core pointer or core keyborard we don't want it */
    67         if ((DevList[i].use != IsXPointer && DevList[i].use != IsXKeyboard)) {
    68             /* we have to check all of the device classes */
    69             deviceClass = DevList[i].inputclassinfo;
    70             for (j = 0; j < DevList[i].num_classes; ++j) {
    71                 if (deviceClass->class == ValuatorClass) {      /* bingo ;) */
    72                     XValuatorInfo *valInfo;
    73 
    74                     data = (X11_MouseData *) SDL_calloc(1, sizeof(*data));
    75                     if (!data) {
    76                         continue;
    77                     }
    78                     data->display = display;
    79                     data->device = XOpenDevice(display, DevList[i].id);
    80 
    81                     /* motion events */
    82                     DeviceMotionNotify(data->device, event_code, xEvent);
    83                     if (xEvent) {
    84                         data->xevents[data->num_xevents++] = xEvent;
    85                         data->motion = event_code;
    86                     }
    87 
    88                     /* button events */
    89                     DeviceButtonPress(data->device, event_code, xEvent);
    90                     if (xEvent) {
    91                         data->xevents[data->num_xevents++] = xEvent;
    92                         data->button_pressed = event_code;
    93                     }
    94                     DeviceButtonRelease(data->device, event_code, xEvent);
    95                     if (xEvent) {
    96                         data->xevents[data->num_xevents++] = xEvent;
    97                         data->button_released = event_code;
    98                     }
    99 
   100                     /* proximity events */
   101                     ProximityIn(data->device, event_code, xEvent);
   102                     if (xEvent) {
   103                         data->xevents[data->num_xevents++] = xEvent;
   104                         data->proximity_in = event_code;
   105                     }
   106                     ProximityOut(data->device, event_code, xEvent);
   107                     if (xEvent) {
   108                         data->xevents[data->num_xevents++] = xEvent;
   109                         data->proximity_out = event_code;
   110                     }
   111 
   112                     SDL_zero(mouse);
   113                     mouse.id = DevList[i].id;
   114                     mouse.FreeMouse = X11_FreeMouse;
   115                     mouse.driverdata = data;
   116 
   117                     /* lets get the device parameters */
   118                     valInfo = (XValuatorInfo *) deviceClass;
   119                     /* if the device reports pressure, lets check it parameteres */
   120                     if (valInfo->num_axes > 2) {
   121                         SDL_AddMouse(&mouse, DevList[i].name,
   122                                      valInfo->axes[2].max_value,
   123                                      valInfo->axes[2].min_value, 1);
   124                     } else {
   125                         SDL_AddMouse(&mouse, DevList[i].name, 0, 0, 1);
   126                     }
   127 #ifndef IsXExtensionPointer
   128 #define IsXExtensionPointer 4
   129 #endif
   130                     if (DevList[i].use == IsXExtensionPointer) {
   131                         ++num_mice;
   132                     }
   133                     break;
   134                 }
   135                 /* if it's not class we're interested in, lets go further */
   136                 deviceClass =
   137                     (XAnyClassPtr) ((char *) deviceClass +
   138                                     deviceClass->length);
   139             }
   140         }
   141     }
   142     XFreeDeviceList(DevList);
   143 #endif
   144 
   145     if (num_mice == 0) {
   146         SDL_zero(mouse);
   147         SDL_AddMouse(&mouse, "CorePointer", 0, 0, 1);
   148     }
   149 }
   150 
   151 void
   152 X11_QuitMouse(_THIS)
   153 {
   154     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   155 
   156     /* !!! FIXME: use XCloseDevice()? Or maybe handle under SDL_MouseQuit()? */
   157 
   158     /* let's delete all of the mice */
   159     SDL_MouseQuit();
   160 }
   161 
   162 /* vi: set ts=4 sw=4 expandtab: */