Skip to content

Latest commit

 

History

History
436 lines (360 loc) · 13.6 KB

macosx_hidmanager.c

File metadata and controls

436 lines (360 loc) · 13.6 KB
 
1
2
3
4
5
6
7
/*
* Support for Mac OS X via the HID Manager APIs that are new to OS X 10.5
* ("Leopard"). The technotes suggest that after 10.5, the code in
* macosx_hidutilities.c may stop working. We dynamically look up the 10.5
* symbols, and if they are there, we use them. If they aren't, we fail so
* the legacy code can do its magic.
*
Jul 28, 2008
Jul 28, 2008
8
* Please see the file LICENSE.txt in the source's root directory.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
*
* This file written by Ryan C. Gordon.
*/
#include "manymouse.h"
#if ( (defined(__MACH__)) && (defined(__APPLE__)) )
# include <AvailabilityMacros.h> // we need the 10.5 SDK headers here...
# if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
# define MANYMOUSE_DO_MAC_10_POINT_5_API 1
# endif
#endif
#if MANYMOUSE_DO_MAC_10_POINT_5_API
#include <IOKit/hid/IOHIDLib.h>
#define ALLOCATOR kCFAllocatorDefault
#define RUNLOOPMODE (CFSTR("ManyMouse"))
#define HIDOPS kIOHIDOptionsTypeNone
Apr 26, 2011
Apr 26, 2011
29
30
31
32
typedef struct
{
IOHIDDeviceRef device;
Apr 26, 2011
Apr 26, 2011
33
char *name;
Apr 26, 2011
Apr 26, 2011
34
35
36
37
38
int logical; /* maps to what ManyMouse reports for an index. */
} MouseStruct;
static unsigned int logical_mice = 0;
static unsigned int physical_mice = 0;
39
static IOHIDManagerRef hidman = NULL;
Apr 26, 2011
Apr 26, 2011
40
41
static MouseStruct *mice = NULL;
Apr 26, 2011
Apr 26, 2011
42
static char *get_device_name(IOHIDDeviceRef device)
Apr 26, 2011
Apr 26, 2011
43
{
Apr 26, 2011
Apr 26, 2011
44
45
46
char *buf = NULL;
void *ptr = NULL;
CFIndex len = 0;
Apr 26, 2011
Apr 26, 2011
47
48
49
CFStringRef cfstr = (CFStringRef) IOHIDDeviceGetProperty(device,
CFSTR(kIOHIDProductKey));
if (!cfstr)
Oct 15, 2011
Oct 15, 2011
50
51
52
53
54
55
56
57
{
/* Maybe we can't get "AwesomeMouse2000", but we can get "Logitech"? */
cfstr = (CFStringRef) IOHIDDeviceGetProperty(device,
CFSTR(kIOHIDManufacturerKey));
} /* if */
if (!cfstr)
return strdup("Unidentified mouse device"); /* oh well. */
Apr 26, 2011
Apr 26, 2011
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
CFRetain(cfstr);
len = (CFStringGetLength(cfstr)+1) * 12; /* 12 is overkill, but oh well. */
buf = (char *) malloc(len);
if (!buf)
{
CFRelease(cfstr);
return NULL;
} /* if */
if (!CFStringGetCString(cfstr, buf, len, kCFStringEncodingUTF8))
{
free(buf);
CFRelease(cfstr);
return NULL;
} /* if */
Aug 9, 2012
Aug 9, 2012
76
77
CFRelease(cfstr);
Apr 26, 2011
Apr 26, 2011
78
79
80
81
82
ptr = realloc(buf, strlen(buf) + 1); /* shrink down our allocation. */
if (ptr != NULL)
buf = (char *) ptr;
return buf;
} /* get_device_name */
Apr 26, 2011
Apr 26, 2011
83
84
Apr 26, 2011
Apr 26, 2011
85
86
static inline int is_trackpad(const MouseStruct *mouse)
{
Apr 26, 2011
Apr 26, 2011
87
88
89
90
/*
* This stupid thing shows up as two logical devices. One does
* most of the mouse events, the other does the mouse wheel.
*/
Apr 26, 2011
Apr 26, 2011
91
return (strcmp(mouse->name, "Apple Internal Keyboard / Trackpad") == 0);
Apr 26, 2011
Apr 26, 2011
92
93
} /* is_trackpad */
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* 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 void queue_event(const ManyMouseEvent *event)
{
/* copy the event info. We'll process it in ManyMouse_PollEvent(). */
memcpy(&input_events[input_events_write], event, sizeof (ManyMouseEvent));
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 int dequeue_event(ManyMouseEvent *event)
{
if (input_events_read != input_events_write) /* no events if equal. */
{
memcpy(event, &input_events[input_events_read], sizeof (*event));
input_events_read = ((input_events_read + 1) % MAX_EVENTS);
return 1;
} /* if */
return 0; /* no event. */
} /* dequeue_event */
/* returns non-zero if (a <= b). */
typedef unsigned long long ui64;
static inline int oldEvent(const AbsoluteTime *a, const AbsoluteTime *b)
{
#if 0 // !!! FIXME: doesn't work, timestamps aren't reliable.
const ui64 a64 = (((unsigned long long) a->hi) << 32) | a->lo;
const ui64 b64 = (((unsigned long long) b->hi) << 32) | b->lo;
#endif
return 0;
} /* oldEvent */
/* Callback fires whenever a device is unplugged/lost/whatever. */
static void unplugged_callback(void *ctx, IOReturn res, void *sender)
{
Apr 26, 2011
Apr 26, 2011
148
149
const unsigned int idx = (unsigned int) ((size_t) ctx);
if ((idx < physical_mice) && (mice[idx].device) && (mice[idx].logical >= 0))
Apr 26, 2011
Apr 26, 2011
151
152
unsigned int i;
const int logical = mice[idx].logical;
153
154
155
ManyMouseEvent ev;
memset(&ev, '\0', sizeof (ev));
ev.type = MANYMOUSE_EVENT_DISCONNECT;
Apr 26, 2011
Apr 26, 2011
156
ev.device = logical;
Apr 26, 2011
Apr 26, 2011
158
159
160
161
162
163
164
165
166
167
/* disable any physical devices that back the same logical mouse. */
for (i = 0; i < physical_mice; i++)
{
if (mice[i].logical == logical)
{
mice[i].device = NULL;
mice[i].logical = -1;
} /* if */
} /* for */
168
169
170
171
172
173
174
175
} /* if */
} /* unplugged_callback */
/* Callback fires for new mouse input events. */
static void input_callback(void *ctx, IOReturn res,
void *sender, IOHIDValueRef val)
{
Apr 26, 2011
Apr 26, 2011
176
177
178
179
const unsigned int idx = (unsigned int) ((size_t) ctx);
const MouseStruct *mouse = NULL;
if ((res == kIOReturnSuccess) && (idx < physical_mice))
mouse = &mice[idx];
Apr 26, 2011
Apr 26, 2011
181
if ((mouse != NULL) && (mouse->device != NULL) && (mouse->logical >= 0))
182
183
184
185
186
187
188
189
190
{
ManyMouseEvent ev;
IOHIDElementRef elem = IOHIDValueGetElement(val);
const CFIndex value = IOHIDValueGetIntegerValue(val);
const uint32_t page = IOHIDElementGetUsagePage(elem);
const uint32_t usage = IOHIDElementGetUsage(elem);
memset(&ev, '\0', sizeof (ev));
ev.value = (int) value;
Apr 26, 2011
Apr 26, 2011
191
ev.device = mouse->logical;
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
if (page == kHIDPage_GenericDesktop)
{
/*
* some devices (two-finger-scroll trackpads?) seem to give
* a flood of events with values of zero for every legitimate
* event. Throw these zero events out.
*/
if (value != 0)
{
switch (usage)
{
case kHIDUsage_GD_X:
case kHIDUsage_GD_Y:
/*if (!oldEvent(&event.timestamp, &mouse->lastScrollTime))*/
{
ev.type = MANYMOUSE_EVENT_RELMOTION;
ev.item = (usage == kHIDUsage_GD_X) ? 0 : 1;
queue_event(&ev);
} /* if */
break;
case kHIDUsage_GD_Wheel:
/*memcpy(&mouse->lastScrollTime, &event.timestamp, sizeof (AbsoluteTime)); */
ev.type = MANYMOUSE_EVENT_SCROLL;
ev.item = 0; /* !!! FIXME: horiz scroll? */
queue_event(&ev);
break;
/*default: !!! FIXME: absolute motion? */
} /* switch */
} /* if */
} /* if */
else if (page == kHIDPage_Button)
{
ev.type = MANYMOUSE_EVENT_BUTTON;
ev.item = ((int) usage) - 1;
queue_event(&ev);
} /* else if */
} /* if */
} /* input_callback */
/* We ignore hotplugs...this callback is only for initial device discovery. */
static void enum_callback(void *ctx, IOReturn res,
void *sender, IOHIDDeviceRef device)
{
if (res == kIOReturnSuccess)
{
Apr 26, 2011
Apr 26, 2011
242
243
const size_t len = sizeof (MouseStruct) * (physical_mice + 1);
void *ptr = realloc(mice, len);
244
245
if (ptr != NULL) /* if realloc fails, we just drop the device. */
{
Apr 26, 2011
Apr 26, 2011
246
247
248
mice = (MouseStruct *) ptr;
mice[physical_mice].device = device;
mice[physical_mice].logical = -1; /* filled in later. */
Apr 26, 2011
Apr 26, 2011
249
250
251
252
mice[physical_mice].name = get_device_name(device);
if (mice[physical_mice].name == NULL)
return; /* This is bad! Don't add this mouse, I guess. */
Apr 26, 2011
Apr 26, 2011
253
physical_mice++;
254
255
256
257
258
259
260
261
} /* if */
} /* if */
} /* enum_callback */
static int config_hidmanager(CFMutableDictionaryRef dict)
{
CFRunLoopRef runloop = CFRunLoopGetCurrent();
Apr 26, 2011
Apr 26, 2011
262
263
int trackpad = -1;
unsigned int i;
264
265
266
267
268
269
270
271
272
IOHIDManagerRegisterDeviceMatchingCallback(hidman, enum_callback, NULL);
IOHIDManagerScheduleWithRunLoop(hidman,CFRunLoopGetCurrent(),RUNLOOPMODE);
IOHIDManagerSetDeviceMatching(hidman, dict);
IOHIDManagerOpen(hidman, HIDOPS);
while (CFRunLoopRunInMode(RUNLOOPMODE,0,TRUE)==kCFRunLoopRunHandledSource)
/* no-op. Callback fires once per existing device. */ ;
Apr 26, 2011
Apr 26, 2011
273
/* globals (physical_mice) and (mice) are now configured. */
274
275
276
277
278
/* don't care about any hotplugged devices after the initial list. */
IOHIDManagerRegisterDeviceMatchingCallback(hidman, NULL, NULL);
IOHIDManagerUnscheduleFromRunLoop(hidman, runloop, RUNLOOPMODE);
/* now put all those discovered devices into the runloop instead... */
Apr 26, 2011
Apr 26, 2011
279
for (i = 0; i < physical_mice; i++)
Apr 26, 2011
Apr 26, 2011
281
282
MouseStruct *mouse = &mice[i];
IOHIDDeviceRef dev = mouse->device;
283
if (IOHIDDeviceOpen(dev, HIDOPS) != kIOReturnSuccess)
Apr 26, 2011
Apr 26, 2011
284
285
286
287
{
mouse->device = NULL; /* oh well. */
mouse->logical = -1;
} /* if */
288
289
290
else
{
void *ctx = (void *) ((size_t) i);
Apr 26, 2011
Apr 26, 2011
291
Apr 26, 2011
Apr 26, 2011
292
if (!is_trackpad(mouse))
Apr 26, 2011
Apr 26, 2011
293
294
295
296
297
298
299
300
mouse->logical = logical_mice++;
else
{
if (trackpad < 0)
trackpad = logical_mice++;
mouse->logical = trackpad;
} /* else */
301
302
303
304
305
306
307
308
309
310
311
312
IOHIDDeviceRegisterRemovalCallback(dev, unplugged_callback, ctx);
IOHIDDeviceRegisterInputValueCallback(dev, input_callback, ctx);
IOHIDDeviceScheduleWithRunLoop(dev, runloop, RUNLOOPMODE);
} /* else */
} /* for */
return 1; /* good to go. */
} /* config_hidmanager */
static int create_hidmanager(const UInt32 page, const UInt32 usage)
{
Apr 26, 2011
Apr 26, 2011
313
int retval = -1;
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
345
CFNumberRef num = NULL;
CFMutableDictionaryRef dict = CFDictionaryCreateMutable(ALLOCATOR, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
if (dict != NULL)
{
num = CFNumberCreate(ALLOCATOR, kCFNumberIntType, &page);
if (num != NULL)
{
CFDictionarySetValue(dict, CFSTR(kIOHIDDeviceUsagePageKey), num);
CFRelease(num);
num = CFNumberCreate(ALLOCATOR, kCFNumberIntType, &usage);
if (num != NULL)
{
CFDictionarySetValue(dict, CFSTR(kIOHIDDeviceUsageKey), num);
CFRelease(num);
hidman = IOHIDManagerCreate(ALLOCATOR, HIDOPS);
if (hidman != NULL)
retval = config_hidmanager(dict);
} /* if */
} /* if */
CFRelease(dict);
} /* if */
return retval;
} /* create_hidmanager */
/* ManyMouseDriver interface... */
static void macosx_hidmanager_quit(void)
{
Apr 26, 2011
Apr 26, 2011
346
347
348
349
unsigned int i;
for (i = 0; i < physical_mice; i++)
free(mice[i].name);
350
351
352
353
354
355
356
357
if (hidman != NULL)
{
/* closing (hidman) should close all open devices, too. */
IOHIDManagerClose(hidman, HIDOPS);
CFRelease(hidman);
hidman = NULL;
} /* if */
Apr 26, 2011
Apr 26, 2011
358
359
360
361
logical_mice = 0;
physical_mice = 0;
free(mice);
mice = NULL;
362
363
364
365
366
367
368
369
370
371
372
373
374
memset(input_events, '\0', sizeof (input_events));
input_events_read = input_events_write = 0;
} /* macosx_hidmanager_quit */
static int macosx_hidmanager_init(void)
{
if (IOHIDManagerCreate == NULL)
return -1; /* weak symbol is NULL...we don't have OS X >= 10.5.0 */
macosx_hidmanager_quit(); /* just in case... */
Apr 26, 2011
Apr 26, 2011
375
/* Prepare global (hidman), (mice), (physical_mice), etc. */
376
377
378
if (!create_hidmanager(kHIDPage_GenericDesktop, kHIDUsage_GD_Mouse))
return -1;
Apr 26, 2011
Apr 26, 2011
379
return (int) logical_mice;
380
381
382
} /* macosx_hidmanager_init */
Apr 26, 2011
Apr 26, 2011
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* returns the first physical device that backs a logical device. */
static MouseStruct *map_logical_device(const unsigned int index)
{
if (index < logical_mice)
{
unsigned int i;
for (i = 0; i < physical_mice; i++)
{
if (mice[i].logical == ((int) index))
return &mice[i];
} /* for */
} /* if */
return NULL; /* not found (maybe unplugged?) */
} /* map_logical_device */
399
400
static const char *macosx_hidmanager_name(unsigned int index)
{
Apr 26, 2011
Apr 26, 2011
401
402
const MouseStruct *mouse = map_logical_device(index);
return mouse ? mouse->name : NULL;
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
} /* macosx_hidmanager_name */
static int macosx_hidmanager_poll(ManyMouseEvent *event)
{
/* ...favor existing events in the queue... */
if (dequeue_event(event))
return 1;
/* pump runloop for new hardware events... */
while (CFRunLoopRunInMode(RUNLOOPMODE,0,TRUE)==kCFRunLoopRunHandledSource)
/* no-op. We're filling our queue... !!! FIXME: stop if queue fills. */ ;
return dequeue_event(event); /* see if anything had shown up... */
} /* macosx_hidmanager_poll */
Apr 26, 2011
Apr 26, 2011
419
420
421
static const ManyMouseDriver ManyMouseDriver_interface =
{
Apr 26, 2011
Apr 26, 2011
422
"Mac OS X 10.5+ HID Manager",
423
424
425
426
427
428
429
430
431
432
433
434
435
macosx_hidmanager_init,
macosx_hidmanager_quit,
macosx_hidmanager_name,
macosx_hidmanager_poll
};
const ManyMouseDriver *ManyMouseDriver_hidmanager = &ManyMouseDriver_interface;
#else
const ManyMouseDriver *ManyMouseDriver_hidmanager = 0;
#endif /* ifdef Mac OS X blocker */
/* end of macosx_hidmanager.c ... */