Skip to content

Latest commit

 

History

History
714 lines (583 loc) · 20.4 KB

windows_wminput.c

File metadata and controls

714 lines (583 loc) · 20.4 KB
 
Jun 29, 2005
Jun 29, 2005
1
2
3
/*
* Support for Windows via the WM_INPUT message.
*
Jul 28, 2008
Jul 28, 2008
4
* Please see the file LICENSE.txt in the source's root directory.
Jun 29, 2005
Jun 29, 2005
5
6
7
8
*
* This file written by Ryan C. Gordon.
*/
Mar 24, 2006
Mar 24, 2006
9
10
#include "manymouse.h"
Jan 8, 2010
Jan 8, 2010
11
#if (defined(_WIN32) || defined(_WINDOWS) || defined(__CYGWIN__))
Jun 29, 2005
Jun 29, 2005
12
13
14
15
16
17
18
/* WinUser.h won't include rawinput stuff without this... */
#if (_WIN32_WINNT < 0x0501)
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif
Jan 8, 2010
Jan 8, 2010
19
#define WIN32_LEAN_AND_MEAN 1
Jun 29, 2005
Jun 29, 2005
20
#include <windows.h>
Jan 9, 2010
Jan 9, 2010
21
#include <setupapi.h>
Jun 29, 2005
Jun 29, 2005
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <malloc.h> /* needed for alloca(). */
/* Cygwin's headers don't have WM_INPUT right now... */
#ifndef WM_INPUT
#define WM_INPUT 0x00FF
#endif
/* that should be enough, knock on wood. */
#define MAX_MICE 32
/*
* Just trying to avoid malloc() here...we statically allocate a buffer
* for events and treat it as a ring buffer.
*/
/* !!! FIXME: tweak this? */
#define MAX_EVENTS 1024
static ManyMouseEvent input_events[MAX_EVENTS];
static volatile int input_events_read = 0;
static volatile int input_events_write = 0;
static int available_mice = 0;
static int did_api_lookup = 0;
static HWND raw_hwnd = NULL;
static const char *class_name = "ManyMouseRawInputCatcher";
static const char *win_name = "ManyMouseRawInputMsgWindow";
static ATOM class_atom = 0;
Jun 29, 2005
Jun 29, 2005
47
static CRITICAL_SECTION mutex;
Jun 29, 2005
Jun 29, 2005
48
49
50
51
52
53
54
55
56
57
58
59
60
typedef struct
{
HANDLE handle;
char name[256];
} MouseStruct;
static MouseStruct mice[MAX_MICE];
/*
* The RawInput APIs only exist in Windows XP and later, so you want this
* to fail gracefully on earlier systems instead of refusing to start the
* process due to missing symbols. To this end, we do a symbol lookup on
Jun 29, 2005
Jun 29, 2005
61
62
63
64
65
66
* User32.dll, etc to get the entry points.
*
* A lot of these are available all the way back to the start of win32 in
* Windows 95 and WinNT 3.1, but just so you don't have to track down any
* import libraries, I've added those here, too. That fits well with the
* idea of just adding the sources to your build and going forward.
Jun 29, 2005
Jun 29, 2005
67
*/
Aug 4, 2008
Aug 4, 2008
68
static UINT (WINAPI *pGetRawInputDeviceList)(PRAWINPUTDEVICELIST,PUINT,UINT);
Jun 29, 2005
Jun 29, 2005
69
/* !!! FIXME: use unicode version */
Aug 4, 2008
Aug 4, 2008
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
static UINT (WINAPI *pGetRawInputDeviceInfoA)(HANDLE,UINT,LPVOID,PUINT);
static BOOL (WINAPI *pRegisterRawInputDevices)(PCRAWINPUTDEVICE,UINT,UINT);
static LRESULT (WINAPI *pDefRawInputProc)(PRAWINPUT *,INT,UINT);
static UINT (WINAPI *pGetRawInputBuffer)(PRAWINPUT,PUINT,UINT);
static UINT (WINAPI *pGetRawInputData)(HRAWINPUT,UINT,LPVOID,PUINT,UINT);
static HWND (WINAPI *pCreateWindowExA)(DWORD,LPCTSTR,LPCTSTR,DWORD,int,int,int,int,HWND,HMENU,HINSTANCE,LPVOID);
static ATOM (WINAPI *pRegisterClassExA)(CONST WNDCLASSEX *);
static LRESULT (WINAPI *pDefWindowProcA)(HWND,UINT,WPARAM,LPARAM);
static BOOL (WINAPI *pUnregisterClassA)(LPCTSTR,HINSTANCE);
static HMODULE (WINAPI *pGetModuleHandleA)(LPCTSTR);
static BOOL (WINAPI *pPeekMessageA)(LPMSG,HWND,UINT,UINT,UINT);
static BOOL (WINAPI *pTranslateMessage)(const MSG *);
static LRESULT (WINAPI *pDispatchMessageA)(const MSG *);
static BOOL (WINAPI *pDestroyWindow)(HWND);
static void (WINAPI *pInitializeCriticalSection)(LPCRITICAL_SECTION);
static void (WINAPI *pEnterCriticalSection)(LPCRITICAL_SECTION);
static void (WINAPI *pLeaveCriticalSection)(LPCRITICAL_SECTION);
static void (WINAPI *pDeleteCriticalSection)(LPCRITICAL_SECTION);
Jan 9, 2010
Jan 9, 2010
88
static DWORD (WINAPI *pGetLastError)(void);
Jan 22, 2010
Jan 22, 2010
89
static HDEVINFO (WINAPI *pSetupDiGetClassDevsA)(LPGUID, LPCTSTR, HWND, DWORD);
Jan 9, 2010
Jan 9, 2010
90
static BOOL (WINAPI *pSetupDiEnumDeviceInfo)(HDEVINFO, DWORD, PSP_DEVINFO_DATA);
Jan 9, 2010
Jan 9, 2010
91
92
static BOOL (WINAPI *pSetupDiGetDeviceInstanceIdA)(HDEVINFO, PSP_DEVINFO_DATA, PTSTR, DWORD, PDWORD);
static BOOL (WINAPI *pSetupDiGetDeviceRegistryPropertyA)(HDEVINFO, PSP_DEVINFO_DATA, DWORD, PDWORD, PBYTE, DWORD, PDWORD);
Jan 9, 2010
Jan 9, 2010
93
static BOOL (WINAPI *pSetupDiDestroyDeviceInfoList)(HDEVINFO);
Jun 29, 2005
Jun 29, 2005
94
Jun 29, 2005
Jun 29, 2005
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
static int symlookup(HMODULE dll, void **addr, const char *sym)
{
*addr = GetProcAddress(dll, sym);
if (*addr == NULL)
{
FreeLibrary(dll);
return(0);
} /* if */
return(1);
} /* symlookup */
static int find_api_symbols(void)
{
HMODULE dll;
if (did_api_lookup)
return(1);
Jun 29, 2005
Jun 29, 2005
114
#define LOOKUP(x) { if (!symlookup(dll, (void **) &p##x, #x)) return(0); }
Jun 29, 2005
Jun 29, 2005
115
116
117
118
119
120
121
122
123
124
dll = LoadLibrary("user32.dll");
if (dll == NULL)
return(0);
LOOKUP(GetRawInputDeviceInfoA);
LOOKUP(RegisterRawInputDevices);
LOOKUP(GetRawInputDeviceList);
LOOKUP(DefRawInputProc);
LOOKUP(GetRawInputBuffer);
LOOKUP(GetRawInputData);
Jun 29, 2005
Jun 29, 2005
125
126
127
128
129
130
131
132
133
134
135
136
137
138
LOOKUP(CreateWindowExA);
LOOKUP(RegisterClassExA);
LOOKUP(UnregisterClassA);
LOOKUP(DefWindowProcA);
LOOKUP(PeekMessageA);
LOOKUP(TranslateMessage);
LOOKUP(DispatchMessageA);
LOOKUP(DestroyWindow);
dll = LoadLibrary("kernel32.dll");
if (dll == NULL)
return(0);
LOOKUP(GetModuleHandleA);
Jan 9, 2010
Jan 9, 2010
139
LOOKUP(GetLastError);
Jun 29, 2005
Jun 29, 2005
140
141
142
143
LOOKUP(InitializeCriticalSection);
LOOKUP(EnterCriticalSection);
LOOKUP(LeaveCriticalSection);
LOOKUP(DeleteCriticalSection);
Jun 29, 2005
Jun 29, 2005
144
Jan 9, 2010
Jan 9, 2010
145
146
147
148
dll = LoadLibrary("setupapi.dll");
if (dll == NULL)
return(0);
Jan 9, 2010
Jan 9, 2010
149
LOOKUP(SetupDiGetClassDevsA);
Jan 9, 2010
Jan 9, 2010
150
LOOKUP(SetupDiEnumDeviceInfo);
Jan 9, 2010
Jan 9, 2010
151
152
LOOKUP(SetupDiGetDeviceInstanceIdA);
LOOKUP(SetupDiGetDeviceRegistryPropertyA);
Jan 9, 2010
Jan 9, 2010
153
154
LOOKUP(SetupDiDestroyDeviceInfoList);
Jun 29, 2005
Jun 29, 2005
155
#undef LOOKUP
Jun 29, 2005
Jun 29, 2005
156
157
158
159
160
did_api_lookup = 1;
return(1);
} /* find_api_symbols */
Jan 9, 2010
Jan 9, 2010
161
162
163
/* Some simple functions to avoid C runtime dependency... */
Jan 9, 2010
Jan 9, 2010
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
static char make_upper(const char a)
{
return ((a >= 'a') && (a <= 'z')) ? (a - ('a' - 'A')) : a;
} /* make_upper */
static void make_string_upper(char *str)
{
char *ptr;
for (ptr = str; *ptr; ptr++)
*ptr = make_upper(*ptr);
} /* make_string_upper */
static int string_compare(const char *a, const char *b)
{
while (1)
{
const char cha = *(a++);
const char chb = *(b++);
if (cha < chb)
return -1;
else if (cha > chb)
return 1;
else if (cha == '\0')
return 0;
} /* while */
return 0;
} /* string_compare */
static size_t string_length(const char *a)
{
size_t retval;
for (retval = 0; *(a++); retval++) { /* spin. */ }
return retval;
} /* string_length */
Jun 29, 2005
Jun 29, 2005
200
201
202
static void queue_event(const ManyMouseEvent *event)
{
Sep 18, 2005
Sep 18, 2005
203
204
205
/* copy the event info. We'll process it in ManyMouse_PollEvent(). */
CopyMemory(&input_events[input_events_write], event, sizeof (ManyMouseEvent));
Jun 29, 2005
Jun 29, 2005
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
input_events_write = ((input_events_write + 1) % MAX_EVENTS);
/* Ring buffer full? Lose oldest event. */
if (input_events_write == input_events_read)
{
/* !!! FIXME: we need to not lose mouse buttons here. */
input_events_read = ((input_events_read + 1) % MAX_EVENTS);
} /* if */
} /* queue_event */
static void queue_from_rawinput(const RAWINPUT *raw)
{
int i;
const RAWINPUTHEADER *header = &raw->header;
const RAWMOUSE *mouse = &raw->data.mouse;
ManyMouseEvent event;
if (raw->header.dwType != RIM_TYPEMOUSE)
return;
for (i = 0; i < available_mice; i++) /* find the device for event. */
{
if (mice[i].handle == header->hDevice)
break;
} /* for */
if (i == available_mice)
return; /* not found?! */
/*
* RAWINPUT packs a bunch of events into one, so we split it up into
* a bunch of ManyMouseEvents here and store them in an internal queue.
* Then ManyMouse_PollEvent() just shuffles items off that queue
* without any complicated processing.
*/
event.device = i;
Jun 29, 2005
Jun 29, 2005
245
246
pEnterCriticalSection(&mutex);
Jun 29, 2005
Jun 29, 2005
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
if (mouse->usFlags & MOUSE_MOVE_ABSOLUTE)
{
/* !!! FIXME: How do we get the min and max values for absmotion? */
event.type = MANYMOUSE_EVENT_ABSMOTION;
event.item = 0;
event.value = mouse->lLastX;
queue_event(&event);
event.item = 1;
event.value = mouse->lLastY;
queue_event(&event);
} /* if */
else /*if (mouse->usFlags & MOUSE_MOVE_RELATIVE)*/
{
event.type = MANYMOUSE_EVENT_RELMOTION;
if (mouse->lLastX != 0)
{
event.item = 0;
event.value = mouse->lLastX;
queue_event(&event);
} /* if */
if (mouse->lLastY != 0)
{
event.item = 1;
event.value = mouse->lLastY;
queue_event(&event);
} /* if */
} /* else if */
event.type = MANYMOUSE_EVENT_BUTTON;
#define QUEUE_BUTTON(x) { \
if (mouse->usButtonFlags & RI_MOUSE_BUTTON_##x##_DOWN) { \
event.item = x-1; \
event.value = 1; \
queue_event(&event); \
} \
if (mouse->usButtonFlags & RI_MOUSE_BUTTON_##x##_UP) { \
event.item = x-1; \
event.value = 0; \
queue_event(&event); \
} \
}
QUEUE_BUTTON(1);
QUEUE_BUTTON(2);
QUEUE_BUTTON(3);
QUEUE_BUTTON(4);
QUEUE_BUTTON(5);
#undef QUEUE_BUTTON
if (mouse->usButtonFlags & RI_MOUSE_WHEEL)
{
if (mouse->usButtonData != 0) /* !!! FIXME: can this ever be zero? */
{
event.type = MANYMOUSE_EVENT_SCROLL;
Jun 29, 2005
Jun 29, 2005
305
event.item = 0; /* !!! FIXME: horizontal wheel? */
Apr 22, 2006
Apr 22, 2006
306
event.value = ( ((SHORT) mouse->usButtonData) > 0) ? 1 : -1;
Jun 29, 2005
Jun 29, 2005
307
308
309
queue_event(&event);
} /* if */
} /* if */
Jun 29, 2005
Jun 29, 2005
310
311
pLeaveCriticalSection(&mutex);
Jun 29, 2005
Jun 29, 2005
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
} /* queue_from_rawinput */
static void wminput_handler(WPARAM wParam, LPARAM lParam)
{
UINT dwSize = 0;
LPBYTE lpb;
pGetRawInputData((HRAWINPUT) lParam, RID_INPUT, NULL, &dwSize,
sizeof (RAWINPUTHEADER));
if (dwSize < sizeof (RAWINPUT))
return; /* unexpected packet? */
lpb = (LPBYTE) alloca(dwSize);
if (lpb == NULL)
return;
if (pGetRawInputData((HRAWINPUT) lParam, RID_INPUT, lpb, &dwSize,
sizeof (RAWINPUTHEADER)) != dwSize)
return;
queue_from_rawinput((RAWINPUT *) lpb);
} /* wminput_handler */
static LRESULT CALLBACK RawWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
if (Msg == WM_INPUT)
wminput_handler(wParam, lParam);
else if (Msg == WM_DESTROY)
return(0);
Jun 29, 2005
Jun 29, 2005
345
return pDefWindowProcA(hWnd, Msg, wParam, lParam);
Jun 29, 2005
Jun 29, 2005
346
} /* RawWndProc */
Jun 29, 2005
Jun 29, 2005
347
348
349
350
static int init_event_queue(void)
{
Jun 29, 2005
Jun 29, 2005
351
HINSTANCE hInstance = pGetModuleHandleA(NULL);
Jun 29, 2005
Jun 29, 2005
352
353
354
355
356
357
358
359
360
361
362
WNDCLASSEX wce;
RAWINPUTDEVICE rid;
ZeroMemory(input_events, sizeof (input_events));
input_events_read = input_events_write = 0;
ZeroMemory(&wce, sizeof (wce));
wce.cbSize = sizeof(WNDCLASSEX);
wce.lpfnWndProc = RawWndProc;
wce.lpszClassName = class_name;
wce.hInstance = hInstance;
Jun 29, 2005
Jun 29, 2005
363
class_atom = pRegisterClassExA(&wce);
Jun 29, 2005
Jun 29, 2005
364
365
366
if (class_atom == 0)
return(0);
Jun 29, 2005
Jun 29, 2005
367
raw_hwnd = pCreateWindowExA(0, class_name, win_name, WS_OVERLAPPEDWINDOW,
Jun 29, 2005
Jun 29, 2005
368
369
370
371
372
373
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, HWND_MESSAGE, NULL, hInstance, NULL);
if (raw_hwnd == NULL)
return(0);
Jun 29, 2005
Jun 29, 2005
374
375
pInitializeCriticalSection(&mutex);
Jun 29, 2005
Jun 29, 2005
376
377
378
379
380
381
ZeroMemory(&rid, sizeof (rid));
rid.usUsagePage = 1; /* GenericDesktop page */
rid.usUsage = 2; /* GeneralDestop Mouse usage. */
rid.dwFlags = RIDEV_INPUTSINK;
rid.hwndTarget = raw_hwnd;
if (!pRegisterRawInputDevices(&rid, 1, sizeof (rid)))
Jun 29, 2005
Jun 29, 2005
382
383
{
pDeleteCriticalSection(&mutex);
Jun 29, 2005
Jun 29, 2005
384
return(0);
Jun 29, 2005
Jun 29, 2005
385
} /* if */
Jun 29, 2005
Jun 29, 2005
386
387
388
389
390
391
392
393
394
395
return(1);
} /* init_event_queue */
static void cleanup_window(void)
{
if (raw_hwnd)
{
MSG Msg;
Jun 29, 2005
Jun 29, 2005
396
397
pDestroyWindow(raw_hwnd);
while (pPeekMessageA(&Msg, raw_hwnd, 0, 0, PM_REMOVE))
Jun 29, 2005
Jun 29, 2005
398
{
Jun 29, 2005
Jun 29, 2005
399
400
pTranslateMessage(&Msg);
pDispatchMessageA(&Msg);
Jun 29, 2005
Jun 29, 2005
401
402
403
404
405
406
} /* while */
raw_hwnd = 0;
} /* if */
if (class_atom)
{
Jun 29, 2005
Jun 29, 2005
407
pUnregisterClassA(class_name, pGetModuleHandleA(NULL));
Jun 29, 2005
Jun 29, 2005
408
409
410
411
412
class_atom = 0;
} /* if */
} /* cleanup_window */
Jan 9, 2010
Jan 9, 2010
413
414
415
416
static int get_devinfo_data(HDEVINFO devinfo, const char *devinstance,
SP_DEVINFO_DATA *data)
{
DWORD i = 0;
Jan 9, 2010
Jan 9, 2010
417
418
419
420
const DWORD bufsize = string_length(devinstance) + 1;
char *buf = (char *) alloca(bufsize);
if (buf == NULL)
return 0;
Jan 9, 2010
Jan 9, 2010
421
422
423
424
425
426
427
428
429
while (1)
{
ZeroMemory(data, sizeof (SP_DEVINFO_DATA));
data->cbSize = sizeof (SP_DEVINFO_DATA);
if (!pSetupDiEnumDeviceInfo(devinfo, i++, data))
{
if (pGetLastError() == ERROR_NO_MORE_ITEMS)
break;
Jan 9, 2010
Jan 9, 2010
430
431
432
433
434
435
else
continue;
} /* if */
if (!pSetupDiGetDeviceInstanceIdA(devinfo, data, buf, bufsize, NULL))
continue;
Jan 9, 2010
Jan 9, 2010
436
437
438
439
440
441
442
443
444
445
446
447
448
make_string_upper(buf);
if (string_compare(devinstance, buf) == 0)
return 1; /* found it! */
} /* while */
return 0; /* not found. */
} /* get_devinfo_data */
static void get_dev_name_by_instance(const char *devinstance, char *name,
size_t namesize)
{
Jan 9, 2010
Jan 9, 2010
449
SP_DEVINFO_DATA devdata;
Jan 9, 2010
Jan 9, 2010
450
const DWORD flags = DIGCF_ALLCLASSES | DIGCF_PRESENT;
Feb 13, 2010
Feb 13, 2010
451
HDEVINFO devinfo = pSetupDiGetClassDevsA(NULL, NULL, NULL, flags);
Jan 9, 2010
Jan 9, 2010
452
453
454
455
if (devinfo == INVALID_HANDLE_VALUE)
return;
if (get_devinfo_data(devinfo, devinstance, &devdata))
Jan 9, 2010
Jan 9, 2010
456
457
{
pSetupDiGetDeviceRegistryPropertyA(devinfo, &devdata, SPDRP_DEVICEDESC,
Jan 9, 2010
Jan 9, 2010
458
459
460
461
462
463
464
NULL, (PBYTE) name, namesize, NULL);
} /* if */
pSetupDiDestroyDeviceInfoList(devinfo);
} /* get_dev_name_by_instance */
Jan 9, 2010
Jan 9, 2010
465
static void get_device_product_name(char *name, size_t namesize, char *devname)
Jun 29, 2005
Jun 29, 2005
466
467
468
469
470
471
472
473
474
475
{
const char default_device_name[] = "Unidentified input device";
*name = '\0'; /* really insane default. */
if (sizeof (default_device_name) >= namesize)
return;
/* in case we can't stumble upon something better... */
CopyMemory(name, default_device_name, sizeof (default_device_name));
Jan 9, 2010
Jan 9, 2010
476
477
478
479
480
481
482
/* okay, we're got the device instance. Now find the data for it. */
get_dev_name_by_instance(devname, name, namesize);
} /* get_device_product_name */
static void init_mouse(const RAWINPUTDEVICELIST *dev)
{
Jan 9, 2010
Jan 9, 2010
483
const char rdp_ident[] = "ROOT\\RDP_MOU\\";
Jan 9, 2010
Jan 9, 2010
484
485
486
487
488
489
490
491
MouseStruct *mouse = &mice[available_mice];
char *buf = NULL;
char *ptr = NULL;
UINT ct = 0;
if (dev->dwType != RIM_TYPEMOUSE)
return; /* keyboard or some other fruity thing. */
Jun 29, 2005
Jun 29, 2005
492
493
494
495
496
if (pGetRawInputDeviceInfoA(dev->hDevice, RIDI_DEVICENAME, NULL, &ct) < 0)
return;
/* ct == is chars, not bytes, but we used the ASCII version. */
buf = (char *) alloca(ct+1);
Jan 9, 2010
Jan 9, 2010
497
if (buf == NULL)
Jun 29, 2005
Jun 29, 2005
498
499
500
501
502
return;
if (pGetRawInputDeviceInfoA(dev->hDevice, RIDI_DEVICENAME, buf, &ct) < 0)
return;
Jan 9, 2010
Jan 9, 2010
503
504
buf[ct] = '\0'; /* make sure it's null-terminated. */
Jan 9, 2010
Jan 9, 2010
505
506
507
508
509
510
511
512
/* XP starts these strings with "\\??\\" ... Vista does "\\\\?\\". :/ */
while ((*buf == '?') || (*buf == '\\'))
{
buf++;
ct--;
} /* while */
/* This string tap dancing gets us the device instance id. */
Jan 9, 2010
Jan 9, 2010
513
for (ptr = buf; *ptr; ptr++) /* convert '#' to '\\' ... */
Jun 29, 2005
Jun 29, 2005
514
{
Jan 9, 2010
Jan 9, 2010
515
516
char ch = *ptr;
if (ch == '#')
Jun 29, 2005
Jun 29, 2005
517
*ptr = '\\';
Jan 9, 2010
Jan 9, 2010
518
519
520
521
else if (ch == '{') /* hit the GUID part of the string. */
{
if (*(ptr-1) == '\\')
ptr--;
Jun 29, 2005
Jun 29, 2005
522
break;
Jan 9, 2010
Jan 9, 2010
523
} /* else if */
Jun 29, 2005
Jun 29, 2005
524
525
526
527
} /* for */
*ptr = '\0';
Jan 9, 2010
Jan 9, 2010
528
make_string_upper(buf);
Jun 29, 2005
Jun 29, 2005
529
Jan 9, 2010
Jan 9, 2010
530
531
532
533
534
535
536
537
538
/*
* Apparently there's a fake "RDP" device...I guess this is
* "Remote Desktop Protocol" for controlling the system pointer
* remotely via Windows Remote Desktop, but that's just a guess.
* At any rate, we don't want that device, so skip it if detected.
*
* Idea for this found here:
* http://link.mywwwserver.com/~jstookey/arcade/rawmouse/raw_mouse.c
*/
Jun 29, 2005
Jun 29, 2005
539
Jan 9, 2010
Jan 9, 2010
540
541
/* avoiding memcmp here so we don't get a C runtime dependency... */
if (ct >= sizeof (rdp_ident) - 1)
Jun 29, 2005
Jun 29, 2005
542
{
Jan 9, 2010
Jan 9, 2010
543
544
545
546
547
548
549
550
551
int i;
for (i = 0; i < sizeof (rdp_ident) - 1; i++)
{
if (buf[i] != rdp_ident[i])
break;
} /* for */
if (i == sizeof (rdp_ident) - 1)
return; /* this is an RDP thing. Skip this device. */
Jun 29, 2005
Jun 29, 2005
552
} /* if */
Jan 9, 2010
Jan 9, 2010
553
554
555
556
557
558
/* accept this mouse! */
ZeroMemory(mouse, sizeof (MouseStruct));
get_device_product_name(mouse->name, sizeof (mouse->name), buf);
mouse->handle = dev->hDevice;
available_mice++;
Jun 29, 2005
Jun 29, 2005
559
560
561
} /* init_mouse */
Jun 29, 2005
Jun 29, 2005
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
static int windows_wminput_init(void)
{
RAWINPUTDEVICELIST *devlist = NULL;
UINT ct = 0;
UINT i;
available_mice = 0;
if (!find_api_symbols()) /* only supported on WinXP and later. */
return(0);
pGetRawInputDeviceList(NULL, &ct, sizeof (RAWINPUTDEVICELIST));
if (ct == 0) /* no devices. */
return(0);
devlist = (PRAWINPUTDEVICELIST) alloca(sizeof (RAWINPUTDEVICELIST) * ct);
pGetRawInputDeviceList(devlist, &ct, sizeof (RAWINPUTDEVICELIST));
for (i = 0; i < ct; i++)
init_mouse(&devlist[i]);
if (!init_event_queue())
{
cleanup_window();
available_mice = 0;
} /* if */
return(available_mice);
} /* windows_wminput_init */
static void windows_wminput_quit(void)
{
/* unregister WM_INPUT devices... */
RAWINPUTDEVICE rid;
ZeroMemory(&rid, sizeof (rid));
rid.usUsagePage = 1; /* GenericDesktop page */
rid.usUsage = 2; /* GeneralDestop Mouse usage. */
rid.dwFlags |= RIDEV_REMOVE;
pRegisterRawInputDevices(&rid, 1, sizeof (rid));
cleanup_window();
available_mice = 0;
Jun 29, 2005
Jun 29, 2005
603
pDeleteCriticalSection(&mutex);
Jun 29, 2005
Jun 29, 2005
604
605
606
607
608
609
} /* windows_wminput_quit */
static const char *windows_wminput_name(unsigned int index)
{
if (index < available_mice)
Jul 13, 2005
Jul 13, 2005
610
return(mice[index].name);
Jun 29, 2005
Jun 29, 2005
611
612
613
614
return(NULL);
} /* windows_wminput_name */
Jul 9, 2005
Jul 9, 2005
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
* Windows doesn't send a WM_INPUT event when you unplug a mouse,
* so we try to do a basic query by device handle here; if the
* query fails, we assume the device has vanished and generate a
* disconnect.
*/
static int check_for_disconnects(ManyMouseEvent *ev)
{
/*
* (i) is static so we iterate through all mice round-robin and check
* one mouse per call to ManyMouse_PollEvent(). This makes this test O(1).
*/
static unsigned int i = 0;
MouseStruct *mouse = NULL;
if (++i >= available_mice) /* check first in case of redetect */
i = 0;
mouse = &mice[i];
if (mouse->handle != NULL) /* not NULL == still plugged in. */
{
UINT size = 0;
UINT rc = pGetRawInputDeviceInfoA(mouse->handle, RIDI_DEVICEINFO,
NULL, &size);
if (rc == (UINT) -1) /* failed...probably unplugged... */
{
mouse->handle = NULL;
ev->type = MANYMOUSE_EVENT_DISCONNECT;
ev->device = i;
return(1);
} /* if */
} /* if */
return(0); /* no disconnect event this time. */
} /* check_for_disconnects */
Jun 29, 2005
Jun 29, 2005
652
static int windows_wminput_poll(ManyMouseEvent *ev)
Jun 29, 2005
Jun 29, 2005
653
654
{
MSG Msg; /* run the queue for WM_INPUT messages, etc ... */
Jun 29, 2005
Jun 29, 2005
655
int found = 0;
Jun 29, 2005
Jun 29, 2005
656
657
/* ...favor existing events in the queue... */
Jun 29, 2005
Jun 29, 2005
658
pEnterCriticalSection(&mutex);
Jun 29, 2005
Jun 29, 2005
659
660
if (input_events_read != input_events_write) /* no events if equal. */
{
Jun 29, 2005
Jun 29, 2005
661
CopyMemory(ev, &input_events[input_events_read], sizeof (*ev));
Jun 29, 2005
Jun 29, 2005
662
input_events_read = ((input_events_read + 1) % MAX_EVENTS);
Jun 29, 2005
Jun 29, 2005
663
found = 1;
Jun 29, 2005
Jun 29, 2005
664
} /* if */
Jun 29, 2005
Jun 29, 2005
665
pLeaveCriticalSection(&mutex);
Jun 29, 2005
Jun 29, 2005
666
Jun 29, 2005
Jun 29, 2005
667
if (!found)
Jun 29, 2005
Jun 29, 2005
668
{
Jun 29, 2005
Jun 29, 2005
669
670
671
672
673
674
/* pump Windows for new hardware events... */
while (pPeekMessageA(&Msg, raw_hwnd, 0, 0, PM_REMOVE))
{
pTranslateMessage(&Msg);
pDispatchMessageA(&Msg);
} /* while */
Jun 29, 2005
Jun 29, 2005
675
Jun 29, 2005
Jun 29, 2005
676
677
678
679
680
681
682
683
684
/* In case something new came in, give it to the app... */
pEnterCriticalSection(&mutex);
if (input_events_read != input_events_write) /* no events if equal. */
{
CopyMemory(ev, &input_events[input_events_read], sizeof (*ev));
input_events_read = ((input_events_read + 1) % MAX_EVENTS);
found = 1;
} /* if */
pLeaveCriticalSection(&mutex);
Jun 29, 2005
Jun 29, 2005
685
686
} /* if */
Sep 18, 2005
Sep 18, 2005
687
688
689
690
691
692
693
694
695
/*
* Check for disconnects if queue is totally empty and Windows didn't
* report anything new at this time. This ensures that we don't send a
* disconnect event through ManyMouse and then later give a valid
* event to the app for a device that is now missing.
*/
if (!found)
found = check_for_disconnects(ev);
Jun 29, 2005
Jun 29, 2005
696
return(found);
Jun 29, 2005
Jun 29, 2005
697
698
} /* windows_wminput_poll */
Jul 27, 2007
Jul 27, 2007
699
static const ManyMouseDriver ManyMouseDriver_interface =
Jun 29, 2005
Jun 29, 2005
700
701
702
703
704
705
706
{
windows_wminput_init,
windows_wminput_quit,
windows_wminput_name,
windows_wminput_poll
};
Jul 27, 2007
Jul 27, 2007
707
708
709
710
711
712
const ManyMouseDriver *ManyMouseDriver_windows = &ManyMouseDriver_interface;
#else
const ManyMouseDriver *ManyMouseDriver_windows = 0;
#endif /* ifdef Windows blocker */
Jun 29, 2005
Jun 29, 2005
713
/* end of windows_wminput.c ... */