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