-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIOHIDPointingEventDevice.cpp
319 lines (264 loc) · 11.8 KB
/
IOHIDPointingEventDevice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
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
242
243
244
245
246
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
*
* @APPLE_LICENSE_HEADER_START@
*
* Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include <IOKit/IOLib.h>
#include "IOHIDFamilyPrivate.h"
#include "IOHIDPointingEventDevice.h"
#include "IOHIDPointing.h"
#include "IOHIDPrivateKeys.h"
#include "IOHIDDebug.h"
static UInt8 DefaultMouseDesc[] = {
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x02, // Usage (Mouse)
0xA1, 0x01, // Collection (Application)
0x05, 0x09, // Usage Page (Button)
0x19, 0x01, // Usage Minimum........... (1)
0x29, 0x08, // Usage Maximum........... (8)
0x15, 0x00, // Logical Minimum......... (0)
0x25, 0x01, // Logical Maximum......... (1)
0x95, 0x08, // Report Count............ (8)
0x75, 0x01, // Report Size............. (1)
0x81, 0x02, // Input...................(Data, Variable, Absolute)
0x95, 0x00, // Report Count............ (0)
0x75, 0x01, // Report Size............. (1)
0x81, 0x00, // Input...................(Data, Array, Absolute)
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x01, // Usage (Pointer)
0xA1, 0x00, // Collection (Physical)
0x09, 0x30, // Usage (X)
0x09, 0x31, // Usage (Y)
0x16, 0x01, 0x80, // Logical Minimum......... (-32767)
0x26, 0xFF, 0x7F, // Logical Maximum......... (32767)
0x36, 0x00, 0x00, // Physical Minimum........ (0)
0x46, 0x00, 0x00, // Physical Maximum........ (0)
0x65, 0x00, // Unit.................... (0)
0x55, 0x00, // Unit Exponent........... (0)
0x09, 0x32, // Usage (Z)
0x75, 0x10, // Report Size............. (16)
0x95, 0x03, // Report Count............ (3)
0x81, 0x06, // Input...................(Data, Variable, Relative)
0x95, 0x01, // Report Count............ (1)
0x75, 0x10, // Report Size............. (16)
0x16, 0x01, 0x80, // Logical Minimum......... (-32767)
0x26, 0xFF, 0x7F, // Logical Maximum......... (32767)
0x09, 0x38, // Usage (Wheel)
0x81, 0x06, // Input...................(Data, Variable, Relative)
0xC0, // End Collection
0xC0, // End Collection
};
#define super IOHIDDeviceShim
OSDefineMetaClassAndStructors( IOHIDPointingEventDevice, IOHIDDeviceShim )
IOHIDPointingEventDevice *
IOHIDPointingEventDevice::newPointingDeviceAndStart(IOService *owner)
{
IOHIDPointingEventDevice * device = new IOHIDPointingEventDevice;
if (device)
{
if (!device->initWithParameters(0, true)){
device->release();
return 0;
}
device->setProperty("HIDDefaultBehavior", kOSBooleanTrue);
device->setProperty(kIOHIDCompatibilityInterface, kOSBooleanTrue);
if ( device->attach(owner) )
{
if (!device->start(owner))
{
device->detach(owner);
device->release();
device = 0;
}
}
else
{
device->release();
device = 0;
}
}
return device;
}
bool IOHIDPointingEventDevice::initWithLocation( UInt32 location )
{
if (!super::initWithLocation(location))
return false;
_report = 0;
return true;
}
void IOHIDPointingEventDevice::free()
{
if (_report) _report->release();
super::free();
}
bool IOHIDPointingEventDevice::handleStart( IOService * provider )
{
if (!super::handleStart(provider)) {
return false;
}
_report = IOBufferMemoryDescriptor::withCapacity(sizeof(GenericReport), kIODirectionNone, true);
if (_report) {
bzero(_report->getBytesNoCopy(), sizeof(GenericReport));
}
return (_report) ? true : false;
}
bool IOHIDPointingEventDevice::start( IOService * provider ) {
bool success = false;
if ( !super::start(provider) ) {
HIDLogError ("failed");
return false;
}
success = ((IOHIPointing*)provider)->open(
this,
kIOServiceSeize,
0,
(RelativePointerEventCallback) _relativePointerEvent,
(AbsolutePointerEventCallback) _absolutePointerEvent,
(ScrollWheelEventCallback) _scrollWheelEvent
);
provider->setProperty(kIOHIDResetPointerKey, kOSBooleanTrue);
return success;
}
IOReturn IOHIDPointingEventDevice::newReportDescriptor(
IOMemoryDescriptor ** descriptor ) const
{
void *desc;
if (!descriptor)
return kIOReturnBadArgument;
*descriptor = IOBufferMemoryDescriptor::withCapacity(
sizeof(DefaultMouseDesc),
kIODirectionNone,
true);
if (! *descriptor)
return kIOReturnNoMemory;
desc = ((IOBufferMemoryDescriptor *)(*descriptor))->getBytesNoCopy();
bcopy(DefaultMouseDesc, desc, sizeof(DefaultMouseDesc));
return kIOReturnSuccess;
}
IOReturn IOHIDPointingEventDevice::getReport(IOMemoryDescriptor *report,
IOHIDReportType reportType,
IOOptionBits options __unused )
{
if (!report)
return kIOReturnError;
if ( reportType != kIOHIDReportTypeInput)
return kIOReturnUnsupported;
report->writeBytes(0, _report->getBytesNoCopy(), min(report->getLength(), _report->getLength()));
return kIOReturnSuccess;
}
#define CONVERT_EV_TO_HW_BUTTONS(ev_buttons) ((ev_buttons & ~7) | ((ev_buttons & 3) << 1) | ((ev_buttons & 4) >> 2))
void IOHIDPointingEventDevice::_relativePointerEvent(
IOHIDPointingEventDevice * self,
int buttons,
int dx,
int dy,
AbsoluteTime ts __unused,
OSObject * sender __unused,
void * refcon __unused)
{
self->postMouseEvent(CONVERT_EV_TO_HW_BUTTONS(buttons), dx, dy, 0, 0);
}
void IOHIDPointingEventDevice::_absolutePointerEvent(
IOHIDPointingEventDevice * self __unused,
int buttons __unused,
IOGPoint * newLoc __unused,
IOGBounds * bounds __unused,
bool proximity __unused,
int pressure __unused,
int stylusAngle __unused,
AbsoluteTime ts __unused,
OSObject * sender __unused,
void * refcon __unused)
{
}
void IOHIDPointingEventDevice::_scrollWheelEvent(
IOHIDPointingEventDevice * self,
short deltaAxis1 __unused,
short deltaAxis2 __unused,
short deltaAxis3 __unused,
IOFixed fixedDelta1 __unused,
IOFixed fixedDelta2 __unused,
IOFixed fixedDelta3 __unused,
SInt32 pointDeltaAxis1,
SInt32 pointDeltaAxis2,
SInt32 pointDeltaAxis3 __unused,
UInt32 options __unused,
AbsoluteTime ts __unused,
OSObject * sender __unused,
void * refcon __unused)
{
self->postMouseEvent(0, 0, 0, pointDeltaAxis1, pointDeltaAxis2);
}
void IOHIDPointingEventDevice::postMouseEvent(UInt8 buttons, SInt16 x, SInt16 y, SInt16 vscroll, SInt16 hscroll)
{
GenericReport *report = (GenericReport*)_report->getBytesNoCopy();
if (!report) {
return;
}
report->buttons = buttons;
report->x = x;
report->y = y;
report->vscroll = vscroll;
report->hscroll = hscroll;
handleReport(_report);
}
OSString * IOHIDPointingEventDevice::newProductString() const
{
OSString * string = 0;
if ( !(string = super::newProductString()) )
string = OSString::withCString("Virtual Mouse");
return string;
}
OSNumber * IOHIDPointingEventDevice::newVendorIDNumber() const
{
return OSNumber::withNumber(0x5ac, 32);
}
OSNumber * IOHIDPointingEventDevice::newProductIDNumber() const
{
return OSNumber::withNumber(0xffff, 32);
}
OSString * IOHIDPointingEventDevice::newManufacturerString() const
{
return OSString::withCString("Apple");
}
IOReturn IOHIDPointingEventDevice::message(UInt32 type, IOService * provider, void * argument __unused)
{
IOReturn status = kIOReturnSuccess;
switch (type)
{
case kIOMessageServiceIsTerminated:
if (provider) {
provider->close( this );
}
break;
}
return status;
}
bool IOHIDPointingEventDevice::matchPropertyTable(OSDictionary *table, SInt32 * score __unused)
{
if (super::matchPropertyTable(table, score) == false) {
return false;
}
if (table->getObject(kIOHIDCompatibilityInterface)) {
return true;
}
return false;
}