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