src/video/win32/SDL_win32mouse.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 25 Aug 2008 06:33:00 +0000
changeset 2710 44e49d3fa6cf
parent 1895 c121d94672cb
child 2724 0e2b65f32298
permissions -rw-r--r--
Final merge of Google Summer of Code 2008 work...

Many-mouse and tablet support
by Szymon Wilczek, mentored by Ryan C. Gordon

Everything concerning the project is noted on the wiki:
http://wilku.ravenlord.ws/doku.php?id=start
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@1895
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1895
     4
slouken@1895
     5
    This library is free software; you can redistribute it and/or
slouken@1895
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1895
     7
    License as published by the Free Software Foundation; either
slouken@1895
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1895
     9
slouken@1895
    10
    This library is distributed in the hope that it will be useful,
slouken@1895
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1895
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1895
    13
    Lesser General Public License for more details.
slouken@1895
    14
slouken@1895
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1895
    16
    License along with this library; if not, write to the Free Software
slouken@1895
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1895
    18
slouken@1895
    19
    Sam Lantinga
slouken@1895
    20
    slouken@libsdl.org
slouken@1895
    21
*/
slouken@2710
    22
slouken@2710
    23
/* we need to define it, so that raw input is included*/
slouken@2710
    24
slouken@2710
    25
#if (_WIN32_WINNT < 0x0501)
slouken@2710
    26
#undef _WIN32_WINNT
slouken@2710
    27
#define _WIN32_WINNT 0x0501
slouken@2710
    28
#endif
slouken@2710
    29
slouken@1895
    30
#include "SDL_config.h"
slouken@1895
    31
slouken@1895
    32
#include "SDL_win32video.h"
slouken@1895
    33
slouken@1895
    34
#include "../../events/SDL_mouse_c.h"
slouken@1895
    35
slouken@2710
    36
#include <wintab.h>
slouken@2710
    37
slouken@2710
    38
#define PACKETDATA ( PK_X | PK_Y | PK_BUTTONS | PK_NORMAL_PRESSURE | PK_CURSOR)
slouken@2710
    39
#define PACKETMODE 0
slouken@2710
    40
#include <pktdef.h>
slouken@2710
    41
extern HANDLE *mice;
slouken@2710
    42
extern int total_mice;
slouken@2710
    43
extern int tablet;
slouken@2710
    44
slouken@1895
    45
void
slouken@1895
    46
WIN_InitMouse(_THIS)
slouken@1895
    47
{
slouken@2710
    48
    int index = 0;
slouken@2710
    49
    RAWINPUTDEVICELIST *deviceList = NULL;
slouken@2710
    50
    int devCount = 0;
slouken@2710
    51
    int i;
slouken@2710
    52
    int tmp = 0;
slouken@2710
    53
    char *buffer = NULL;
slouken@2710
    54
    char *tab = "wacom";        /* since windows does't give us handles to tablets, we have to detect a tablet by it's name */
slouken@2710
    55
    const char *rdp = "rdp_mou";
slouken@1895
    56
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1895
    57
slouken@2710
    58
    /* we're checking for the number of rawinput devices */
slouken@2710
    59
    if (GetRawInputDeviceList(NULL, &devCount, sizeof(RAWINPUTDEVICELIST))) {
slouken@2710
    60
        return;
slouken@2710
    61
    }
slouken@2710
    62
slouken@2710
    63
    deviceList = SDL_malloc(sizeof(RAWINPUTDEVICELIST) * devCount);
slouken@2710
    64
slouken@2710
    65
    /* we're getting the raw input device list */
slouken@2710
    66
    GetRawInputDeviceList(deviceList, &devCount, sizeof(RAWINPUTDEVICELIST));
slouken@2710
    67
    mice = SDL_malloc(devCount * sizeof(HANDLE));
slouken@2710
    68
slouken@2710
    69
    /* we're getting the details of the devices */
slouken@2710
    70
    for (i = 0; i < devCount; ++i) {
slouken@2710
    71
        int is_rdp = 0;
slouken@2710
    72
        int j;
slouken@2710
    73
        int k;
slouken@2710
    74
        char *default_device_name = "Pointing device xx";
slouken@2710
    75
        const char *reg_key_root = "System\\CurrentControlSet\\Enum\\";
slouken@2710
    76
        char *device_name = SDL_malloc(256 * sizeof(char));
slouken@2710
    77
        char *key_name = NULL;
slouken@2710
    78
        char *tmp_name = NULL;
slouken@2710
    79
        LONG rc = 0;
slouken@2710
    80
        HKEY hkey;
slouken@2710
    81
        DWORD regtype = REG_SZ;
slouken@2710
    82
        DWORD out = 256 * sizeof(char);
slouken@2710
    83
        SDL_Mouse mouse;
slouken@2710
    84
        int l;
slouken@2710
    85
        if (deviceList[i].dwType != RIM_TYPEMOUSE) {    /* if a device isn't a mouse type we don't want it */
slouken@2710
    86
            continue;
slouken@2710
    87
        }
slouken@2710
    88
        if (GetRawInputDeviceInfoA
slouken@2710
    89
            (deviceList[i].hDevice, RIDI_DEVICENAME, NULL, &tmp) < 0) {
slouken@2710
    90
            continue;
slouken@2710
    91
        }
slouken@2710
    92
        buffer = SDL_malloc((tmp + 1) * sizeof(char));
slouken@2710
    93
        key_name = SDL_malloc(tmp + sizeof(reg_key_root) * sizeof(char));
slouken@2710
    94
slouken@2710
    95
        /* we're getting the device registry path and polishing it to get it's name,
slouken@2710
    96
           surely there must be an easier way, but we haven't found it yet */
slouken@2710
    97
        if (GetRawInputDeviceInfoA
slouken@2710
    98
            (deviceList[i].hDevice, RIDI_DEVICENAME, buffer, &tmp) < 0) {
slouken@2710
    99
            continue;
slouken@2710
   100
        }
slouken@2710
   101
        buffer += 4;
slouken@2710
   102
        tmp -= 4;
slouken@2710
   103
        tmp_name = buffer;
slouken@2710
   104
        for (j = 0; j < tmp; ++j) {
slouken@2710
   105
            if (*tmp_name == '#') {
slouken@2710
   106
                *tmp_name = '\\';
slouken@2710
   107
            }
slouken@2710
   108
slouken@2710
   109
            else if (*tmp_name == '{') {
slouken@2710
   110
                break;
slouken@2710
   111
            }
slouken@2710
   112
            ++tmp_name;
slouken@2710
   113
        }
slouken@2710
   114
        *tmp_name = '\0';
slouken@2710
   115
        SDL_memcpy(key_name, reg_key_root, SDL_strlen(reg_key_root));
slouken@2710
   116
        SDL_memcpy(key_name + (SDL_strlen(reg_key_root)), buffer, j + 1);
slouken@2710
   117
        l = SDL_strlen(key_name);
slouken@2710
   118
        is_rdp = 0;
slouken@2710
   119
        if (l >= 7) {
slouken@2710
   120
            for (j = 0; j < l - 7; ++j) {
slouken@2710
   121
                for (k = 0; k < 7; ++k) {
slouken@2710
   122
                    if (rdp[k] !=
slouken@2710
   123
                        SDL_tolower((unsigned char) key_name[j + k])) {
slouken@2710
   124
                        break;
slouken@2710
   125
                    }
slouken@2710
   126
                }
slouken@2710
   127
                if (k == 7) {
slouken@2710
   128
                    is_rdp = 1;
slouken@2710
   129
                    break;
slouken@2710
   130
                }
slouken@2710
   131
            }
slouken@2710
   132
        }
slouken@2710
   133
        if (is_rdp == 1) {
slouken@2710
   134
            SDL_free(buffer);
slouken@2710
   135
            SDL_free(key_name);
slouken@2710
   136
            SDL_free(device_name);
slouken@2710
   137
            is_rdp = 0;
slouken@2710
   138
            continue;
slouken@2710
   139
        }
slouken@2710
   140
slouken@2710
   141
        /* we're opening the registry key to get the mouse name */
slouken@2710
   142
        rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, key_name, 0, KEY_READ, &hkey);
slouken@2710
   143
        if (rc != ERROR_SUCCESS) {
slouken@2710
   144
            SDL_memcpy(device_name, default_device_name,
slouken@2710
   145
                       SDL_strlen(default_device_name));
slouken@2710
   146
        }
slouken@2710
   147
        rc = RegQueryValueExA(hkey, "DeviceDesc", NULL, &regtype, device_name,
slouken@2710
   148
                              &out);
slouken@2710
   149
        RegCloseKey(hkey);
slouken@2710
   150
        if (rc != ERROR_SUCCESS) {
slouken@2710
   151
            SDL_memcpy(device_name, default_device_name,
slouken@2710
   152
                       SDL_strlen(default_device_name));
slouken@2710
   153
        }
slouken@2710
   154
slouken@2710
   155
        /* we're saving the handle to the device */
slouken@2710
   156
        mice[index] = deviceList[i].hDevice;
slouken@2710
   157
        SDL_zero(mouse);
slouken@2710
   158
        SDL_SetMouseIndexId(index, index);
slouken@2710
   159
        l = SDL_strlen(device_name);
slouken@2710
   160
slouken@2710
   161
        /* we're checking if the device isn't by any chance a tablet */
slouken@2710
   162
        if (tablet == -1) {
slouken@2710
   163
            for (j = 0; j < l - 5; ++j) {
slouken@2710
   164
                for (k = 0; k < 5; ++k) {
slouken@2710
   165
                    if (tab[k] !=
slouken@2710
   166
                        SDL_tolower((unsigned char) device_name[j + k])) {
slouken@2710
   167
                        break;
slouken@2710
   168
                    }
slouken@2710
   169
                }
slouken@2710
   170
                if (k == 5) {
slouken@2710
   171
                    tablet = index;
slouken@2710
   172
                    break;
slouken@2710
   173
                }
slouken@2710
   174
            }
slouken@2710
   175
        }
slouken@2710
   176
slouken@2710
   177
        /* if it's a tablet, let's read it's maximum and minimum pressure */
slouken@2710
   178
        if (tablet == index) {
slouken@2710
   179
            AXIS pressure;
slouken@2710
   180
            int cursors;
slouken@2710
   181
            WTInfo(WTI_DEVICES, DVC_NPRESSURE, &pressure);
slouken@2710
   182
            WTInfo(WTI_DEVICES, DVC_NCSRTYPES, &cursors);
slouken@2710
   183
            data->mouse =
slouken@2710
   184
                SDL_AddMouse(&mouse, index, device_name, pressure.axMax,
slouken@2710
   185
                             pressure.axMin, cursors);
slouken@2710
   186
        } else {
slouken@2710
   187
            data->mouse = SDL_AddMouse(&mouse, index, device_name, 0, 0, 1);
slouken@2710
   188
        }
slouken@2710
   189
        ++index;
slouken@2710
   190
        SDL_free(buffer);
slouken@2710
   191
        SDL_free(key_name);
slouken@2710
   192
    }
slouken@2710
   193
    total_mice = index;
slouken@2710
   194
    SDL_free(deviceList);
slouken@1895
   195
}
slouken@1895
   196
slouken@1895
   197
void
slouken@1895
   198
WIN_QuitMouse(_THIS)
slouken@1895
   199
{
slouken@1895
   200
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1895
   201
slouken@2710
   202
    /* let's delete all of the mice */
slouken@2710
   203
    SDL_MouseQuit();
slouken@1895
   204
}
slouken@1895
   205
slouken@1895
   206
/* vi: set ts=4 sw=4 expandtab: */