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