src/video/win32/SDL_win32mouse.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2009 17:14:27 +0000
changeset 2990 502adab079a4
parent 2980 8789e4b0e0e7
child 3097 0d12e8f1de3c
permissions -rw-r--r--
indent
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 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
extern HANDLE *mice;
slouken@2710
    37
extern int total_mice;
slouken@2710
    38
extern int tablet;
slouken@2710
    39
slouken@1895
    40
void
slouken@1895
    41
WIN_InitMouse(_THIS)
slouken@1895
    42
{
slouken@2710
    43
    int index = 0;
slouken@2710
    44
    RAWINPUTDEVICELIST *deviceList = NULL;
slouken@2710
    45
    int devCount = 0;
slouken@2710
    46
    int i;
slouken@2710
    47
    int tmp = 0;
slouken@2710
    48
    char *buffer = NULL;
slouken@2710
    49
    char *tab = "wacom";        /* since windows does't give us handles to tablets, we have to detect a tablet by it's name */
slouken@2710
    50
    const char *rdp = "rdp_mou";
slouken@1895
    51
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1895
    52
slouken@2710
    53
    /* we're checking for the number of rawinput devices */
slouken@2710
    54
    if (GetRawInputDeviceList(NULL, &devCount, sizeof(RAWINPUTDEVICELIST))) {
slouken@2710
    55
        return;
slouken@2710
    56
    }
slouken@2710
    57
slouken@2710
    58
    deviceList = SDL_malloc(sizeof(RAWINPUTDEVICELIST) * devCount);
slouken@2710
    59
slouken@2710
    60
    /* we're getting the raw input device list */
slouken@2710
    61
    GetRawInputDeviceList(deviceList, &devCount, sizeof(RAWINPUTDEVICELIST));
slouken@2710
    62
    mice = SDL_malloc(devCount * sizeof(HANDLE));
slouken@2710
    63
slouken@2710
    64
    /* we're getting the details of the devices */
slouken@2710
    65
    for (i = 0; i < devCount; ++i) {
slouken@2710
    66
        int is_rdp = 0;
slouken@2710
    67
        int j;
slouken@2710
    68
        int k;
slouken@2710
    69
        char *default_device_name = "Pointing device xx";
slouken@2710
    70
        const char *reg_key_root = "System\\CurrentControlSet\\Enum\\";
slouken@2710
    71
        char *device_name = SDL_malloc(256 * sizeof(char));
slouken@2710
    72
        char *key_name = NULL;
slouken@2710
    73
        char *tmp_name = NULL;
slouken@2710
    74
        LONG rc = 0;
slouken@2710
    75
        HKEY hkey;
slouken@2710
    76
        DWORD regtype = REG_SZ;
slouken@2710
    77
        DWORD out = 256 * sizeof(char);
slouken@2710
    78
        SDL_Mouse mouse;
slouken@2710
    79
        int l;
slouken@2710
    80
        if (deviceList[i].dwType != RIM_TYPEMOUSE) {    /* if a device isn't a mouse type we don't want it */
slouken@2710
    81
            continue;
slouken@2710
    82
        }
slouken@2710
    83
        if (GetRawInputDeviceInfoA
slouken@2710
    84
            (deviceList[i].hDevice, RIDI_DEVICENAME, NULL, &tmp) < 0) {
slouken@2710
    85
            continue;
slouken@2710
    86
        }
slouken@2710
    87
        buffer = SDL_malloc((tmp + 1) * sizeof(char));
slouken@2990
    88
        key_name =
slouken@2990
    89
            SDL_malloc((tmp + SDL_strlen(reg_key_root) + 1) * sizeof(char));
slouken@2710
    90
slouken@2710
    91
        /* we're getting the device registry path and polishing it to get it's name,
slouken@2710
    92
           surely there must be an easier way, but we haven't found it yet */
slouken@2710
    93
        if (GetRawInputDeviceInfoA
slouken@2710
    94
            (deviceList[i].hDevice, RIDI_DEVICENAME, buffer, &tmp) < 0) {
slouken@2710
    95
            continue;
slouken@2710
    96
        }
slouken@2710
    97
        buffer += 4;
slouken@2710
    98
        tmp -= 4;
slouken@2710
    99
        tmp_name = buffer;
slouken@2710
   100
        for (j = 0; j < tmp; ++j) {
slouken@2710
   101
            if (*tmp_name == '#') {
slouken@2710
   102
                *tmp_name = '\\';
slouken@2710
   103
            }
slouken@2710
   104
slouken@2710
   105
            else if (*tmp_name == '{') {
slouken@2710
   106
                break;
slouken@2710
   107
            }
slouken@2710
   108
            ++tmp_name;
slouken@2710
   109
        }
slouken@2710
   110
        *tmp_name = '\0';
slouken@2710
   111
        SDL_memcpy(key_name, reg_key_root, SDL_strlen(reg_key_root));
slouken@2710
   112
        SDL_memcpy(key_name + (SDL_strlen(reg_key_root)), buffer, j + 1);
slouken@2710
   113
        l = SDL_strlen(key_name);
slouken@2710
   114
        is_rdp = 0;
slouken@2710
   115
        if (l >= 7) {
slouken@2710
   116
            for (j = 0; j < l - 7; ++j) {
slouken@2710
   117
                for (k = 0; k < 7; ++k) {
slouken@2710
   118
                    if (rdp[k] !=
slouken@2710
   119
                        SDL_tolower((unsigned char) key_name[j + k])) {
slouken@2710
   120
                        break;
slouken@2710
   121
                    }
slouken@2710
   122
                }
slouken@2710
   123
                if (k == 7) {
slouken@2710
   124
                    is_rdp = 1;
slouken@2710
   125
                    break;
slouken@2710
   126
                }
slouken@2710
   127
            }
slouken@2710
   128
        }
kazeuser@2980
   129
slouken@2990
   130
        buffer -= 4;
kazeuser@2980
   131
slouken@2710
   132
        if (is_rdp == 1) {
slouken@2710
   133
            SDL_free(buffer);
slouken@2710
   134
            SDL_free(key_name);
slouken@2710
   135
            SDL_free(device_name);
slouken@2710
   136
            is_rdp = 0;
slouken@2710
   137
            continue;
slouken@2710
   138
        }
slouken@2710
   139
slouken@2710
   140
        /* we're opening the registry key to get the mouse name */
slouken@2710
   141
        rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, key_name, 0, KEY_READ, &hkey);
slouken@2710
   142
        if (rc != ERROR_SUCCESS) {
slouken@2710
   143
            SDL_memcpy(device_name, default_device_name,
slouken@2710
   144
                       SDL_strlen(default_device_name));
slouken@2710
   145
        }
slouken@2710
   146
        rc = RegQueryValueExA(hkey, "DeviceDesc", NULL, &regtype, device_name,
slouken@2710
   147
                              &out);
slouken@2710
   148
        RegCloseKey(hkey);
slouken@2710
   149
        if (rc != ERROR_SUCCESS) {
slouken@2710
   150
            SDL_memcpy(device_name, default_device_name,
slouken@2710
   151
                       SDL_strlen(default_device_name));
slouken@2710
   152
        }
slouken@2710
   153
slouken@2710
   154
        /* we're saving the handle to the device */
slouken@2710
   155
        mice[index] = deviceList[i].hDevice;
slouken@2710
   156
        SDL_zero(mouse);
slouken@2940
   157
        mouse.id = index;
slouken@2710
   158
        l = SDL_strlen(device_name);
slouken@2710
   159
slouken@2710
   160
        /* we're checking if the device isn't by any chance a tablet */
slouken@2726
   161
        if (data->wintabDLL && tablet == -1) {
slouken@2710
   162
            for (j = 0; j < l - 5; ++j) {
slouken@2710
   163
                for (k = 0; k < 5; ++k) {
slouken@2710
   164
                    if (tab[k] !=
slouken@2710
   165
                        SDL_tolower((unsigned char) device_name[j + k])) {
slouken@2710
   166
                        break;
slouken@2710
   167
                    }
slouken@2710
   168
                }
slouken@2710
   169
                if (k == 5) {
slouken@2710
   170
                    tablet = index;
slouken@2710
   171
                    break;
slouken@2710
   172
                }
slouken@2710
   173
            }
slouken@2710
   174
        }
slouken@2710
   175
slouken@2710
   176
        /* if it's a tablet, let's read it's maximum and minimum pressure */
slouken@2710
   177
        if (tablet == index) {
slouken@2710
   178
            AXIS pressure;
slouken@2710
   179
            int cursors;
slouken@2728
   180
            data->WTInfoA(WTI_DEVICES, DVC_NPRESSURE, &pressure);
slouken@2728
   181
            data->WTInfoA(WTI_DEVICES, DVC_NCSRTYPES, &cursors);
slouken@2990
   182
            SDL_AddMouse(&mouse, device_name, pressure.axMax, pressure.axMin,
slouken@2990
   183
                         cursors);
slouken@2710
   184
        } else {
slouken@2974
   185
            SDL_AddMouse(&mouse, device_name, 0, 0, 1);
slouken@2710
   186
        }
slouken@2710
   187
        ++index;
slouken@2710
   188
        SDL_free(buffer);
slouken@2710
   189
        SDL_free(key_name);
slouken@2710
   190
    }
slouken@2710
   191
    total_mice = index;
slouken@2710
   192
    SDL_free(deviceList);
slouken@1895
   193
}
slouken@1895
   194
slouken@1895
   195
void
slouken@1895
   196
WIN_QuitMouse(_THIS)
slouken@1895
   197
{
slouken@1895
   198
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1895
   199
slouken@2710
   200
    /* let's delete all of the mice */
slouken@2710
   201
    SDL_MouseQuit();
slouken@1895
   202
}
slouken@1895
   203
slouken@1895
   204
/* vi: set ts=4 sw=4 expandtab: */