-
Notifications
You must be signed in to change notification settings - Fork 4
/
IOHIPointing.cpp
1775 lines (1452 loc) · 59.1 KB
/
IOHIPointing.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
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
603
604
605
606
607
608
609
610
611
612
613
614
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* @APPLE_LICENSE_HEADER_START@
*
* Copyright (c) 1999-2009 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@
*/
/*
* 17 July 1998 sdouglas Initial creation
* 01 April 2002 ryepez added support for scroll acceleration
*/
#include <IOKit/IOLib.h>
#include <IOKit/assert.h>
#include <IOKit/hidsystem/IOHIPointing.h>
#include <IOKit/hidsystem/IOHIDParameter.h>
#include <IOKit/pwr_mgt/RootDomain.h>
#include <libkern/OSByteOrder.h>
#include "IOHIDSystem.h"
#include "IOHIDPointingDevice.h"
#include "IOHIDevicePrivateKeys.h"
#include "ev_private.h"
#ifndef abs
#define abs(_a) ((_a >= 0) ? _a : -_a)
#endif
#ifndef IOFixedSquared
#define IOFixedSquared(a) IOFixedMultiply(a, a)
#endif
#define FRAME_RATE (67 << 16)
#define SCREEN_RESOLUTION (96 << 16)
#define MAX_DEVICE_THRESHOLD 0x7fffffff
#define SCROLL_DEFAULT_RESOLUTION 0x00090000
#define SCROLL_CONSUME_RESOLUTION 0x00640000
#define SCROLL_CONSUME_COUNT_MULTIPLIER 3
#define SCROLL_EVENT_THRESHOLD_MS 0x960000
#define SCROLL_EVENT_THRESHOLD_MS_LL 150ULL
#define SCROLL_CLEAR_THRESHOLD_MS_LL 500ULL
#define SCROLL_MULTIPLIER_RANGE 0x00018000
#define SCROLL_MULTIPLIER_A 0x00000002 /*IOFixedDivide(SCROLL_MULTIPLIER_RANGE,SCROLL_EVENT_THRESHOLD_MS*2)*/
#define SCROLL_MULTIPLIER_B 0x000003bb /*IOFixedDivide(SCROLL_MULTIPLIER_RANGE*3,(SCROLL_EVENT_THRESHOLD_MS^2)*2)*/
#define SCROLL_MULTIPLIER_C 0x00018041
#define SCROLL_WHEEL_TO_PIXEL_SCALE 0x000a0000/* IOFixedDivide(SCREEN_RESOLUTION, SCROLL_DEFAULT_RESOLUTION) */
#define SCROLL_PIXEL_TO_WHEEL_SCALE 0x0000199a/* IOFixedDivide(SCREEN_RESOLUTION, SCROLL_DEFAULT_RESOLUTION) */
#define CONVERT_SCROLL_FIXED_TO_FRACTION(fixed, fraction) \
{ \
if( fixed >= 0) \
fraction = fixed & 0xffff; \
else \
fraction = fixed | 0xffff0000; \
}
#define CONVERT_SCROLL_FIXED_TO_INTEGER(fixedAxis, integer) \
{ \
SInt32 tempInt = 0; \
if((fixedAxis < 0) && (fixedAxis & 0xffff)) \
tempInt = (fixedAxis >> 16) + 1; \
else \
tempInt = (fixedAxis >> 16); \
integer = tempInt; \
}
#define CONVERT_SCROLL_FIXED_TO_COARSE(fixedAxis, coarse) \
{ \
SInt32 tempCoarse = 0; \
CONVERT_SCROLL_FIXED_TO_INTEGER(fixedAxis, tempCoarse) \
if (!tempCoarse && (fixedAxis & 0xffff)) \
tempCoarse = (fixedAxis < 0) ? -1 : 1; \
coarse = tempCoarse; \
}
#define _scrollButtonMask _reserved->scrollButtonMask
#define _scrollType _reserved->scrollType
#define _scrollZoomMask _reserved->scrollZoomMask
#define _scrollOff _reserved->scrollOff
#define _lastScrollWasZoom _reserved->lastScrollWasZoom
#define _scrollWheelInfo _reserved->scrollWheelInfo
#define _scrollPointerInfo _reserved->scrollPointerInfo
#define _scrollFixedDeltaAxis1 _reserved->scrollFixedDeltaAxis1
#define _scrollFixedDeltaAxis2 _reserved->scrollFixedDeltaAxis2
#define _scrollFixedDeltaAxis3 _reserved->scrollFixedDeltaAxis3
#define _scrollPointDeltaAxis1 _reserved->scrollPointDeltaAxis1
#define _scrollPointDeltaAxis2 _reserved->scrollPointDeltaAxis2
#define _scrollPointDeltaAxis3 _reserved->scrollPointDeltaAxis3
#define _hidPointingNub _reserved->hidPointingNub
#define _isSeized _reserved->isSeized
#define _openClient _reserved->openClient
#define _accelerateMode _reserved->accelerateMode
#define DEVICE_LOCK IOLockLock( _deviceLock )
#define DEVICE_UNLOCK IOLockUnlock( _deviceLock )
enum {
kAccelTypeGlobal = -1,
kAccelTypeY = 0, //delta axis 1
kAccelTypeX = 1, //delta axis 2
kAccelTypeZ = 2 //delta axis 3
};
struct CursorDeviceSegment {
SInt32 devUnits;
SInt32 slope;
SInt32 intercept;
};
typedef struct CursorDeviceSegment CursorDeviceSegment;
#define SCROLL_TIME_DELTA_COUNT 8
struct ScaleDataState
{
UInt8 deltaIndex;
IOFixed deltaTime[SCROLL_TIME_DELTA_COUNT];
IOFixed deltaAxis[SCROLL_TIME_DELTA_COUNT];
IOFixed fraction;
};
typedef ScaleDataState ScaleDataState;
struct ScaleConsumeState
{
UInt32 consumeCount;
IOFixed consumeAccum;
};
typedef ScaleConsumeState ScaleConsumeState;
struct ScrollAxisAccelInfo
{
AbsoluteTime lastEventTime;
void * scaleSegments;
IOItemCount scaleSegCount;
ScaleDataState state;
ScaleConsumeState consumeState;
SInt32 lastValue;
UInt32 consumeClearThreshold;
UInt32 consumeCountThreshold;
bool isHighResScroll;
};
typedef ScrollAxisAccelInfo ScrollAxisAccelInfo;
struct ScrollAccelInfo
{
ScrollAxisAccelInfo axis[3];
IOFixed rateMultiplier;
UInt32 zoom:1;
};
typedef ScrollAccelInfo ScrollAccelInfo;
static bool SetupAcceleration (OSData * data, IOFixed desired, IOFixed devScale, IOFixed crsrScale, void ** scaleSegments, IOItemCount * scaleSegCount);
static void ScaleAxes (void * scaleSegments, int * axis1p, IOFixed *axis1Fractp, int * axis2p, IOFixed *axis2Fractp);
#define super IOHIDevice
OSDefineMetaClassAndStructors(IOHIPointing, IOHIDevice);
bool IOHIPointing::init(OSDictionary * properties)
{
if (!super::init(properties)) return false;
/*
* Initialize minimal state.
*/
_reserved = IONew(ExpansionData, 1);
if (!_reserved) return false;
bzero(_reserved, sizeof(ExpansionData));
// Initialize pointer accel items
_scaleSegments = 0;
_scaleSegCount = 0;
_fractX = 0;
_fractY = 0;
_acceleration = -1;
_accelerateMode = ( kAccelScroll | kAccelMouse );
_convertAbsoluteToRelative = false;
_contactToMove = false;
_hadContact = false;
_pressureThresholdToClick = 128;
_previousLocation.x = 0;
_previousLocation.y = 0;
_hidPointingNub = 0;
_isSeized = false;
// default to right mouse button generating unique events
_buttonMode = NX_RightButton;
_scrollWheelInfo = (ScrollAccelInfo *) IOMalloc(sizeof(ScrollAccelInfo));
if (!_scrollWheelInfo) return false;
bzero(_scrollWheelInfo, sizeof(ScrollAccelInfo));
_scrollPointerInfo = (ScrollAccelInfo *) IOMalloc(sizeof(ScrollAccelInfo));
if (!_scrollPointerInfo) return false;
bzero(_scrollPointerInfo, sizeof(ScrollAccelInfo));
_deviceLock = IOLockAlloc();
if (!_deviceLock) return false;
return true;
}
bool IOHIPointing::start(IOService * provider)
{
static const char * defaultSettings = "(<00000000>, <00002000>, <00005000>,"
"<00008000>, <0000b000>, <0000e000>,"
"<00010000>)";
if (!super::start(provider)) return false;
// default acceleration settings
if (!getProperty(kIOHIDPointerAccelerationTypeKey))
setProperty(kIOHIDPointerAccelerationTypeKey, kIOHIDMouseAccelerationType);
if (!getProperty(kIOHIDPointerAccelerationSettingsKey))
{
OSObject * obj = OSUnserialize(defaultSettings, 0);
if (obj) {
setProperty(kIOHIDPointerAccelerationSettingsKey, obj);
obj->release();
}
}
if (!getProperty(kIOHIDScrollAccelerationTypeKey))
setProperty(kIOHIDScrollAccelerationTypeKey, kIOHIDMouseScrollAccelerationKey);
if (!getProperty(kIOHIDDisallowRemappingOfPrimaryClickKey))
if (provider->getProperty(kIOHIDDisallowRemappingOfPrimaryClickKey))
setProperty(kIOHIDDisallowRemappingOfPrimaryClickKey, provider->getProperty(kIOHIDDisallowRemappingOfPrimaryClickKey));
/*
* RY: Publish a property containing the button Count. This will
* will be used to determine whether or not the button
* behaviors can be modified.
*/
if (buttonCount() > 1)
{
setProperty(kIOHIDPointerButtonCountKey, buttonCount(), 32);
}
OSNumber * number;
if (number = OSDynamicCast(OSNumber, getProperty(kIOHIDScrollMouseButtonKey)))
{
UInt32 value = number->unsigned32BitValue();
if (!value)
_scrollButtonMask = 0;
else
_scrollButtonMask = (1 << (value-1));
}
// create a IOHIDPointingDevice to post events to the HID Manager
_hidPointingNub = IOHIDPointingDevice::newPointingDeviceAndStart(this, buttonCount(), resolution() >> 16);
/*
* IOHIPointing serves both as a service and a nub (we lead a double
* life). Register ourselves as a nub to kick off matching.
*/
registerService(kIOServiceSynchronous);
return true;
}
void IOHIPointing::free()
// Description: Go Away. Be careful when freeing the lock.
{
if (_deviceLock)
{
IOLock * lock;
IOLockLock(_deviceLock);
lock = _deviceLock;
_deviceLock = NULL;
IOLockUnlock(lock);
IOLockFree(lock);
}
if(_scaleSegments && _scaleSegCount)
IODelete( _scaleSegments, CursorDeviceSegment, _scaleSegCount );
if ( _scrollWheelInfo )
{
UInt32 type;
for (type=kAccelTypeY; type<=kAccelTypeZ; type++) {
if(_scrollWheelInfo->axis[type].scaleSegments && _scrollWheelInfo->axis[type].scaleSegCount)
IODelete( _scrollWheelInfo->axis[type].scaleSegments, CursorDeviceSegment, _scrollWheelInfo->axis[type].scaleSegCount );
}
IOFree(_scrollWheelInfo, sizeof(ScrollAccelInfo));
_scrollWheelInfo = 0;
}
if ( _scrollPointerInfo )
{
UInt32 type;
for (type=kAccelTypeY; type<=kAccelTypeZ; type++) {
if(_scrollPointerInfo->axis[type].scaleSegments && _scrollPointerInfo->axis[type].scaleSegCount)
IODelete( _scrollPointerInfo->axis[type].scaleSegments, CursorDeviceSegment, _scrollPointerInfo->axis[type].scaleSegCount );
}
IOFree(_scrollPointerInfo, sizeof(ScrollAccelInfo));
_scrollPointerInfo = 0;
}
if ( _hidPointingNub )
{
_hidPointingNub->release();
_hidPointingNub = 0;
}
if (_reserved) {
IODelete(_reserved, ExpansionData, 1);
}
super::free();
}
bool IOHIPointing::open(IOService * client,
IOOptionBits options,
RelativePointerEventAction rpeAction,
AbsolutePointerEventAction apeAction,
ScrollWheelEventAction sweAction)
{
if (client == this) {
return super::open(_openClient, options);
}
return open(client,
options,
0,
(RelativePointerEventCallback)rpeAction,
(AbsolutePointerEventCallback)apeAction,
(ScrollWheelEventCallback)sweAction);
}
bool IOHIPointing::open(IOService * client,
IOOptionBits options,
void * refcon,
RelativePointerEventCallback rpeCallback,
AbsolutePointerEventCallback apeCallback,
ScrollWheelEventCallback sweCallback)
{
if (client == this) return true;
_openClient = client;
bool returnValue = open(this, options,
(RelativePointerEventAction)_relativePointerEvent,
(AbsolutePointerEventAction)_absolutePointerEvent,
(ScrollWheelEventAction)_scrollWheelEvent);
if (!returnValue)
return false;
// Note: client object is already retained by superclass' open()
_relativePointerEventTarget = client;
_relativePointerEventAction = (RelativePointerEventAction)rpeCallback;
_absolutePointerEventTarget = client;
_absolutePointerEventAction = (AbsolutePointerEventAction)apeCallback;
_scrollWheelEventTarget = client;
_scrollWheelEventAction = (ScrollWheelEventAction)sweCallback;
return true;
}
void IOHIPointing::close(IOService * client, IOOptionBits)
{
_relativePointerEventAction = NULL;
_relativePointerEventTarget = 0;
_absolutePointerEventAction = NULL;
_absolutePointerEventTarget = 0;
super::close(client);
}
IOReturn IOHIPointing::message( UInt32 type, IOService * provider,
void * argument)
{
IOReturn ret = kIOReturnSuccess;
switch(type)
{
case kIOHIDSystemDeviceSeizeRequestMessage:
if (OSDynamicCast(IOHIDDevice, provider))
{
_isSeized = (bool)argument;
}
break;
default:
ret = super::message(type, provider, argument);
break;
}
return ret;
}
IOReturn IOHIPointing::powerStateWillChangeTo( IOPMPowerFlags powerFlags,
unsigned long newState, IOService * device )
{
return( super::powerStateWillChangeTo( powerFlags, newState, device ));
}
IOReturn IOHIPointing::powerStateDidChangeTo( IOPMPowerFlags powerFlags,
unsigned long newState, IOService * device )
{
return( super::powerStateDidChangeTo( powerFlags, newState, device ));
}
IOHIDKind IOHIPointing::hidKind()
{
return kHIRelativePointingDevice;
}
static void AccelerateScrollAxis( IOFixed * axisp,
ScrollAxisAccelInfo * scaleInfo,
AbsoluteTime timeStamp,
IOFixed rateMultiplier,
bool clear = false)
{
IOFixed absAxis = 0;
IOFixed avgIndex = 0;
IOFixed avgCount = 0;
IOFixed avgAxis = 0;
IOFixed timeDeltaMS = 0;
IOFixed avgTimeDeltaMS = 0;
IOFixed scrollMultiplier = 0;
UInt64 currentTimeNSLL = 0;
UInt64 lastEventTimeNSLL = 0;
UInt64 timeDeltaMSLL = 0;
if (!scaleInfo || !scaleInfo->scaleSegments)
return;
absAxis = abs(*axisp);
if( absAxis == 0 )
return;
absolutetime_to_nanoseconds(timeStamp, ¤tTimeNSLL);
absolutetime_to_nanoseconds(scaleInfo->lastEventTime, &lastEventTimeNSLL);
scaleInfo->lastEventTime = timeStamp;
timeDeltaMSLL = (currentTimeNSLL - lastEventTimeNSLL) / 1000000;
// RY: To compensate for non continual motion, we have added a second
// threshold. This whill allow a user with a standard scroll wheel
// to continue with acceleration when lifting the finger within a
// predetermined time. We should also clear out the last time deltas
// if the direction has changed.
if ((timeDeltaMSLL > SCROLL_CLEAR_THRESHOLD_MS_LL) || clear)
{
bzero(&(scaleInfo->state), sizeof(ScaleDataState));
}
timeDeltaMSLL = ((timeDeltaMSLL > SCROLL_EVENT_THRESHOLD_MS_LL) || clear) ?
SCROLL_EVENT_THRESHOLD_MS_LL : timeDeltaMSLL;
timeDeltaMS = ((UInt32) timeDeltaMSLL) << 16;
scaleInfo->state.deltaTime[scaleInfo->state.deltaIndex] = timeDeltaMS;
scaleInfo->state.deltaAxis[scaleInfo->state.deltaIndex] = absAxis;
// Bump the next index
scaleInfo->state.deltaIndex = (scaleInfo->state.deltaIndex + 1) % SCROLL_TIME_DELTA_COUNT;
// RY: To eliminate jerkyness associated with the scroll acceleration,
// we scroll based on the average of the last n events. This has the
// effect of make acceleration smoother with accel and decel.
for (avgIndex=0; avgIndex < SCROLL_TIME_DELTA_COUNT; avgIndex++)
{
if (scaleInfo->state.deltaTime[avgIndex] == 0)
continue;
avgAxis += abs(scaleInfo->state.deltaAxis[avgIndex]);
avgTimeDeltaMS += scaleInfo->state.deltaTime[avgIndex];
avgCount ++;
}
avgAxis = (avgCount) ? IOFixedDivide(avgAxis, (avgCount<<16)) : 0;
avgTimeDeltaMS = (avgCount) ? IOFixedDivide(avgTimeDeltaMS, (avgCount<<16)) : 0;
avgTimeDeltaMS = IOFixedMultiply(avgTimeDeltaMS, rateMultiplier);
avgTimeDeltaMS = (avgTimeDeltaMS > SCROLL_EVENT_THRESHOLD_MS) ? SCROLL_EVENT_THRESHOLD_MS : avgTimeDeltaMS;
// RY: Since we want scroll acceleration to work with the
// time delta and the accel curves, we have come up with
// this approach:
//
// scrollMultiplier = (SCROLL_MULTIPLIER_A * (avgTimeDeltaMS^2)) +
// (SCROLL_MULTIPLIER_B * avgTimeDeltaMS) +
// SCROLL_MULTIPLIER_C
//
// scrollMultiplier *= avgDeviceDelta
//
// The boost curve follows a quadratic/parabolic curve which
// results in a smoother boost.
//
// The resulting multipler is applied to the average axis
// magnitude and then compared against the accleration curve.
//
// The value acquired from the graph will then be multiplied
// to the current axis delta.
scrollMultiplier = IOFixedMultiply(SCROLL_MULTIPLIER_A, IOFixedSquared(avgTimeDeltaMS)) -
IOFixedMultiply(SCROLL_MULTIPLIER_B, avgTimeDeltaMS) +
SCROLL_MULTIPLIER_C;
scrollMultiplier = IOFixedMultiply(scrollMultiplier, rateMultiplier);
scrollMultiplier = IOFixedMultiply(scrollMultiplier, avgAxis);
CursorDeviceSegment *segment;
// scale
for(
segment = (CursorDeviceSegment *) scaleInfo->scaleSegments;
scrollMultiplier > segment->devUnits;
segment++) {}
scrollMultiplier = IOFixedDivide(
segment->intercept + IOFixedMultiply( scrollMultiplier, segment->slope ),
absAxis );
*axisp = IOFixedMultiply(*axisp, scrollMultiplier);
}
void IOHIPointing::setPointingMode(UInt32 accelerateMode)
{
_accelerateMode = accelerateMode;
_convertAbsoluteToRelative = ((accelerateMode & kAbsoluteConvertMouse) != 0);
}
UInt32 IOHIPointing::getPointingMode()
{
return _accelerateMode;
}
void IOHIPointing::setScrollType(UInt32 scrollType)
{
_scrollType = scrollType;
}
UInt32 IOHIPointing::getScrollType()
{
return _scrollType;
}
void IOHIPointing::scalePointer(int * dxp, int * dyp)
// Description: Perform pointer acceleration computations here.
// Given the resolution, dx, dy, and time, compute the velocity
// of the pointer over a Manhatten distance in inches/second.
// Using this velocity, do a lookup in the pointerScaling table
// to select a scaling factor. Scale dx and dy up as appropriate.
// Preconditions:
// * _deviceLock should be held on entry
{
ScaleAxes(_scaleSegments, dxp, &_fractX, dyp, &_fractY);
}
/*
Routine: Interpolate
This routine interpolates to find a point on the line [x1,y1] [x2,y2] which
is intersected by the line [x3,y3] [x3,y"]. The resulting y' is calculated
by interpolating between y3 and y", towards the higher acceleration curve.
*/
static SInt32 Interpolate( SInt32 x1, SInt32 y1,
SInt32 x2, SInt32 y2,
SInt32 x3, SInt32 y3,
SInt32 scale, Boolean lower )
{
SInt32 slope;
SInt32 intercept;
SInt32 resultY;
slope = (x2 == x1) ? 0 : IOFixedDivide( y2 - y1, x2 - x1 );
intercept = y1 - IOFixedMultiply( slope, x1 );
resultY = intercept + IOFixedMultiply( slope, x3 );
if( lower)
resultY = y3 - IOFixedMultiply( scale, y3 - resultY );
else
resultY = resultY + IOFixedMultiply( scale, y3 - resultY );
return( resultY );
}
void IOHIPointing::setupForAcceleration( IOFixed desired )
{
IOFixed devScale = IOFixedDivide( resolution(), FRAME_RATE );
IOFixed crsrScale = IOFixedDivide( SCREEN_RESOLUTION, FRAME_RATE );
OSData * table = copyAccelerationTable();
if (SetupAcceleration (table, desired, devScale, crsrScale, &_scaleSegments, &_scaleSegCount))
{
_acceleration = desired;
_fractX = _fractY = 0;
if (table) table->release();
}
}
void IOHIPointing::setupScrollForAcceleration( IOFixed desired )
{
IOFixed devScale = 0;
IOFixed scrScale = 0;
IOFixed resolution = 0;
IOFixed reportRate = scrollReportRate();
OSData * accelTable = NULL;
UInt32 type = 0;
_scrollWheelInfo->rateMultiplier = IOFixedDivide(reportRate, FRAME_RATE);
_scrollPointerInfo->rateMultiplier = IOFixedDivide(reportRate, FRAME_RATE);
for ( type=kAccelTypeY; type<=kAccelTypeZ; type++) {
resolution = scrollResolutionForType(type);
if ( resolution ) {
accelTable = copyScrollAccelerationTableForType(type);
// Setup pixel scroll wheel acceleration table
devScale = IOFixedDivide( resolution, reportRate );
scrScale = IOFixedDivide( SCREEN_RESOLUTION, FRAME_RATE );
if (SetupAcceleration (accelTable, desired, devScale, scrScale, &(_scrollWheelInfo->axis[type].scaleSegments), &(_scrollWheelInfo->axis[type].scaleSegCount)))
{
bzero(&(_scrollWheelInfo->axis[type].state), sizeof(ScaleDataState));
clock_get_uptime(&(_scrollWheelInfo->axis[type].lastEventTime));
}
_scrollWheelInfo->axis[type].isHighResScroll = resolution > (SCROLL_DEFAULT_RESOLUTION * 2);
_scrollWheelInfo->axis[type].consumeClearThreshold = (IOFixedDivide(resolution, SCROLL_CONSUME_RESOLUTION) >> 16) * 2;
_scrollWheelInfo->axis[type].consumeCountThreshold = _scrollWheelInfo->axis[type].consumeClearThreshold * SCROLL_CONSUME_COUNT_MULTIPLIER;
bzero(&(_scrollWheelInfo->axis[type].consumeState), sizeof(ScaleConsumeState));
// Grab the pointer resolution
resolution = this->resolution();
reportRate = FRAME_RATE;
// Setup pixel pointer drag/scroll acceleration table
devScale = IOFixedDivide( resolution, reportRate );
scrScale = IOFixedDivide( SCREEN_RESOLUTION, FRAME_RATE );
if (SetupAcceleration (accelTable, desired, devScale, scrScale, &(_scrollPointerInfo->axis[type].scaleSegments), &(_scrollPointerInfo->axis[type].scaleSegCount)))
{
bzero(&(_scrollPointerInfo->axis[type].state), sizeof(ScaleDataState));
clock_get_uptime(&(_scrollPointerInfo->axis[type].lastEventTime));
}
_scrollPointerInfo->axis[type].isHighResScroll = resolution > (SCROLL_DEFAULT_RESOLUTION * 2);
_scrollPointerInfo->axis[type].consumeClearThreshold = (IOFixedDivide(resolution, SCROLL_CONSUME_RESOLUTION) >> 16) * 2;
_scrollPointerInfo->axis[type].consumeCountThreshold = _scrollPointerInfo->axis[type].consumeClearThreshold * SCROLL_CONSUME_COUNT_MULTIPLIER;
bzero(&(_scrollPointerInfo->axis[type].consumeState), sizeof(ScaleConsumeState));
if (accelTable)
accelTable->release();
}
}
}
bool IOHIPointing::resetPointer()
{
DEVICE_LOCK;
_buttonMode = NX_RightButton;
setupForAcceleration(EV_DEFAULTPOINTERACCELLEVEL);
updateProperties();
DEVICE_UNLOCK;
return true;
}
bool IOHIPointing::resetScroll()
{
DEVICE_LOCK;
setupScrollForAcceleration(EV_DEFAULTSCROLLACCELLEVEL);
DEVICE_UNLOCK;
return true;
}
static void ScalePressure(int *pressure, int pressureMin, int pressureMax)
{
// scaled pressure value; MAX=(2^16)-1, MIN=0
*pressure = ((pressureMin != pressureMax)) ?
(((unsigned)(*pressure - pressureMin) * 65535LL) /
(unsigned)( pressureMax - pressureMin)) : 0;
}
void IOHIPointing::dispatchAbsolutePointerEvent(IOGPoint * newLoc,
IOGBounds * bounds,
UInt32 buttonState,
bool proximity,
int pressure,
int pressureMin,
int pressureMax,
int stylusAngle,
AbsoluteTime ts)
{
int buttons = 0;
int dx, dy;
DEVICE_LOCK;
if( buttonState & 1)
buttons |= EV_LB;
//if( buttonCount() > 1) {
if( buttonState & 2) // any others down
buttons |= EV_RB;
// Other magic bit reshuffling stuff. It seems there was space
// left over at some point for a "middle" mouse button between EV_LB and EV_RB
if(buttonState & 4)
buttons |= 2;
// Add in the rest of the buttons in a linear fasion...
buttons |= buttonState & ~0x7;
// }
/* There should not be a threshold applied to pressure for generating a button event.
As soon as the pen hits the tablet, a mouse down should occur
if ((_pressureThresholdToClick < 255) && ((pressure - pressureMin) > ((pressureMax - pressureMin) * _pressureThresholdToClick / 256))) {
buttons |= EV_LB;
}
*/
if ( pressure > pressureMin )
{
buttons |= EV_LB;
}
if (_buttonMode == NX_OneButton) {
if ((buttons & (EV_LB|EV_RB)) != 0) {
buttons = EV_LB;
}
}
if (_convertAbsoluteToRelative) {
dx = newLoc->x - _previousLocation.x;
dy = newLoc->y - _previousLocation.y;
if ((_contactToMove && !_hadContact && (pressure > pressureMin)) || (abs(dx) > ((bounds->maxx - bounds->minx) / 20)) || (abs(dy) > ((bounds->maxy - bounds->miny) / 20))) {
dx = 0;
dy = 0;
} else {
scalePointer(&dx, &dy);
}
_previousLocation.x = newLoc->x;
_previousLocation.y = newLoc->y;
}
DEVICE_UNLOCK;
_hadContact = (pressure > pressureMin);
if (!_contactToMove || (pressure > pressureMin)) {
ScalePressure(&pressure, pressureMin, pressureMax);
if (_convertAbsoluteToRelative) {
_relativePointerEvent( this,
buttons,
dx,
dy,
ts);
} else {
_absolutePointerEvent( this,
buttons,
newLoc,
bounds,
proximity,
pressure,
stylusAngle,
ts);
}
}
return;
}
void IOHIPointing::dispatchRelativePointerEvent(int dx,
int dy,
UInt32 buttonState,
AbsoluteTime ts)
{
int buttons;
DEVICE_LOCK;
// post the raw event to the IOHIDPointingDevice
if (_hidPointingNub)
_hidPointingNub->postMouseEvent(buttonState, dx, dy, 0);
if (_isSeized)
{
DEVICE_UNLOCK;
return;
}
buttons = 0;
if( buttonState & 1)
buttons |= EV_LB;
//if( buttonCount() > 1) {
if( buttonState & 2) // any others down
buttons |= EV_RB;
// Other magic bit reshuffling stuff. It seems there was space
// left over at some point for a "middle" mouse button between EV_LB and EV_RB
if(buttonState & 4)
buttons |= 2;
// Add in the rest of the buttons in a linear fasion...
buttons |= buttonState & ~0x7;
//}
if ( _scrollButtonMask & buttonState )
{
DEVICE_UNLOCK;
dispatchScrollWheelEventWithAccelInfo(-dy, -dx, 0, _scrollPointerInfo, ts);
return;
}
// Perform pointer acceleration computations
if ( _accelerateMode & kAccelMouse ) {
int oldDx = dx;
int oldDy = dy;
scalePointer(&dx, &dy);
if (((oldDx < 0) && (dx > 0)) || ((oldDx > 0) && (dx < 0))) {
IOLog("IOHIPointing::dispatchRelativePointerEvent: Unwanted Direction Change X: oldDx=%d dx=%d\n", oldDy, dy);
}
if (((oldDy < 0) && (dy > 0)) || ((oldDy > 0) && (dy < 0))) {
IOLog("IOHIPointing::dispatchRelativePointerEvent: Unwanted Direction Change Y: oldDy=%d dy=%d\n", oldDy, dy);
}
}
// Perform button tying and mapping. This
// stuff applies to relative posn devices (mice) only.
if ( _buttonMode == NX_OneButton )
{
// Remap both Left and Right (but no others?) to Left.
if ( (buttons & (EV_LB|EV_RB)) != 0 ) {
buttons |= EV_LB;
buttons &= ~EV_RB;
}
}
else if ( (buttonCount() > 1) && (_buttonMode == NX_LeftButton) )
// Menus on left button. Swap!
{
int temp = 0;
if ( buttons & EV_LB )
temp = EV_RB;
if ( buttons & EV_RB )
temp |= EV_LB;
// Swap Left and Right, preserve everything else
buttons = (buttons & ~(EV_LB|EV_RB)) | temp;
}
DEVICE_UNLOCK;
_relativePointerEvent(this,
/* buttons */ buttons,
/* deltaX */ dx,
/* deltaY */ dy,
/* atTime */ ts);
}
void IOHIPointing::dispatchScrollWheelEvent(short deltaAxis1,
short deltaAxis2,
short deltaAxis3,
AbsoluteTime ts)
{
dispatchScrollWheelEventWithAccelInfo(deltaAxis1, deltaAxis2, deltaAxis3, _scrollWheelInfo, ts);
}
void IOHIPointing::dispatchScrollWheelEventWithAccelInfo(
SInt32 deltaAxis1,
SInt32 deltaAxis2,
SInt32 deltaAxis3,
ScrollAccelInfo * info,
AbsoluteTime ts)
{
Boolean isHighResScroll = FALSE;
IOHIDSystem * hidSystem = IOHIDSystem::instance();
UInt32 eventFlags = (hidSystem ? hidSystem->eventFlags() : 0);
DEVICE_LOCK;
// Change the report descriptor for the IOHIDPointingDevice
// to include a scroll whell
if (_hidPointingNub && !_hidPointingNub->isScrollPresent())
{
IOHIDPointingDevice * nub = _hidPointingNub;
_hidPointingNub = 0;
DEVICE_UNLOCK;
// nub->terminate(kIOServiceSynchronous);
nub->terminate(kIOServiceAsynchronous); // rdar://8810574
nub->release();
nub = IOHIDPointingDevice::newPointingDeviceAndStart(this, buttonCount(), resolution() >> 16, true);
DEVICE_LOCK;
_hidPointingNub = nub;
}
// Post the raw event to IOHIDPointingDevice
if (_hidPointingNub)
_hidPointingNub->postMouseEvent(0, 0, 0, deltaAxis1);
if (_isSeized) {
DEVICE_UNLOCK;
return;
}
if (_scrollZoomMask) {
bool isModifiedToZoom = ((SPECIALKEYS_MODIFIER_MASK & eventFlags) == _scrollZoomMask);
bool isMomentum = (0 != (_scrollType & kScrollTypeMomentumAny));
if ((isMomentum && _lastScrollWasZoom) || (isModifiedToZoom && !isMomentum)) {
_lastScrollWasZoom = true;
_scrollType |= kScrollTypeZoom;
}
else {
_lastScrollWasZoom = false;
}
}
else {
_lastScrollWasZoom = false;
}
if (!(_scrollType & kScrollTypeZoom) && _scrollOff ) {
DEVICE_UNLOCK;
return;
}
_scrollFixedDeltaAxis1 = deltaAxis1 << 16;
_scrollFixedDeltaAxis2 = deltaAxis2 << 16;
_scrollFixedDeltaAxis3 = deltaAxis3 << 16;
CONVERT_SCROLL_FIXED_TO_COARSE(IOFixedMultiply(_scrollFixedDeltaAxis1, SCROLL_WHEEL_TO_PIXEL_SCALE), _scrollPointDeltaAxis1);
CONVERT_SCROLL_FIXED_TO_COARSE(IOFixedMultiply(_scrollFixedDeltaAxis2, SCROLL_WHEEL_TO_PIXEL_SCALE), _scrollPointDeltaAxis2);
CONVERT_SCROLL_FIXED_TO_COARSE(IOFixedMultiply(_scrollFixedDeltaAxis3, SCROLL_WHEEL_TO_PIXEL_SCALE), _scrollPointDeltaAxis3);
// Perform pointer acceleration computations
if ( _accelerateMode & kAccelScroll )
{
bool directionChange[3] = {0,0,0};
bool typeChange = FALSE;
SInt32* pDeltaAxis[3] = {&deltaAxis1, &deltaAxis2, &deltaAxis3};
SInt32* pScrollFixedDeltaAxis[3] = {&_scrollFixedDeltaAxis1, &_scrollFixedDeltaAxis2, &_scrollFixedDeltaAxis3};
IOFixed* pScrollPointDeltaAxis[3] = {&_scrollPointDeltaAxis1, &_scrollPointDeltaAxis2, &_scrollPointDeltaAxis3};