@@ -34,6 +34,7 @@ @interface HSmouse : NSObject
3434@property (readonly , getter =getNames) NSArray <NSString *> *names;
3535@property (getter =getAbsolutePosition, setter =setAbsolutePosition:) NSPoint absolutePosition;
3636@property (readonly , getter =getTrackingSpeed) double trackingSpeed;
37+ @property (readonly , getter =getTrackpadTrackingSpeed) double trackpadTrackingSpeed;
3738@property (readonly , getter =getScrollDirectionNatural) BOOL isScrollDirectionNatural;
3839
3940-(BOOL )hasInternalMouse ;
@@ -42,10 +43,12 @@ -(int)getCount;
4243-(NSPoint )getAbsolutePosition ;
4344-(void )setAbsolutePosition : (NSPoint )absolutePosition ;
4445-(double )getTrackingSpeed ;
46+ -(double )getTrackpadTrackingSpeed ;
4547-(io_service_t )createIOHIDSystem ;
4648-(NSDictionary *)getIOHIDParametersFromService : (io_service_t )service ;
4749-(NSDictionary *)getIOHIDParameters ;
4850-(kern_return_t )setTrackingSpeed : (double )trackingSpeed ;
51+ -(kern_return_t )setTrackpadTrackingSpeed : (double )trackingSpeed ;
4952@end
5053
5154@implementation HSmouse
@@ -129,25 +132,47 @@ -(NSDictionary *)getIOHIDParameters {
129132 return parameters;
130133}
131134
132- -(double )getTrackingSpeed {
133- NSDictionary *parameters = [self getIOHIDParameters ];
134- NSNumber *accel = parameters[@" HIDMouseAcceleration" ];
135- return accel.doubleValue / MOUSE_TRACKING_FACTOR;
135+ -(double )getTrackingSpeedForKey : (NSString *)key {
136+ // NSDictionary *parameters = [self getIOHIDParameters];
137+ // NSNumber *accel = parameters[key];
138+ // return accel.doubleValue / MOUSE_TRACKING_FACTOR;
139+ double speed = 0 ;
140+ IOHIDGetAccelerationWithKey (NXOpenEventStatus (), (__bridge CFStringRef)key, &speed) ;
141+ return speed ;
136142}
137143
138- -(kern_return_t )setTrackingSpeed : (double )trackingSpeed {
139- io_service_t service = [self createIOHIDSystem ];
140-
141- NSDictionary *parameters = [self getIOHIDParametersFromService: service];
144+ -(double )getTrackingSpeed {
145+ return [self getTrackingSpeedForKey: @(kIOHIDMouseAccelerationType )] ;
146+ }
142147
143- NSMutableDictionary *newParameters = [parameters mutableCopy ];
144- newParameters[@" HIDMouseAcceleration" ] = @(trackingSpeed * MOUSE_TRACKING_FACTOR);
148+ -(double )getTrackpadTrackingSpeed {
149+ return [self getTrackingSpeedForKey: @(kIOHIDTrackpadAccelerationType )] ;
150+ }
145151
146- kern_return_t result = IORegistryEntrySetCFProperty (service, CFSTR (kIOHIDParametersKey ), (__bridge CFDictionaryRef)newParameters);
152+ -(kern_return_t )setTrackingSpeed : (double )trackingSpeed forKey : (NSString *)key {
153+ // io_service_t service = [self createIOHIDSystem];
154+ //
155+ // NSDictionary *parameters = [self getIOHIDParametersFromService:service];
156+ //
157+ // NSMutableDictionary *newParameters = [parameters mutableCopy];
158+ //
159+ // newParameters[key] = @([NSNumber numberWithDouble:trackingSpeed * MOUSE_TRACKING_FACTOR].integerValue);
160+ //
161+ // kern_return_t result = IORegistryEntrySetCFProperty(service, CFSTR(kIOHIDParametersKey), (__bridge CFDictionaryRef)newParameters);
162+ //
163+ // IOObjectRelease(service);
164+ // return result;
165+
166+ kern_return_t result = IOHIDSetAccelerationWithKey (NXOpenEventStatus (), (__bridge CFStringRef)key, trackingSpeed) ;
167+ return result;
168+ }
147169
148- IOObjectRelease (service);
170+ -(kern_return_t )setTrackingSpeed : (double )trackingSpeed {
171+ return [self setTrackingSpeed: trackingSpeed forKey: @(kIOHIDMouseAccelerationType )] ;
172+ }
149173
150- return result;
174+ -(kern_return_t )setTrackpadTrackingSpeed : (double )trackingSpeed {
175+ return [self setTrackingSpeed: trackingSpeed forKey: @(kIOHIDTrackpadAccelerationType )] ;
151176}
152177@end
153178
@@ -224,33 +249,42 @@ static int mouse_absolutePosition(lua_State *L) {
224249 return 1 ;
225250}
226251
227- // / hs.mouse.trackingSpeed([speed]) -> number
252+ // / hs.mouse.trackingSpeed([speed], [trackpad] ) -> number
228253// / Function
229- // / Gets/Sets the current system mouse tracking speed setting
254+ // / Gets/Sets the current system mouse or trackpad tracking speed setting
230255// /
231256// / Parameters:
232257// / * speed - An optional number containing the new tracking speed to set. If this is omitted, the current setting is returned
258+ // / * trackpad - An optional boolean, default false, indicating whether or not this function affects the mouse tracking speed (false) or the trackpad tracking speed (true)
233259// /
234260// / Returns:
235- // / * A number indicating the current tracking speed setting for mice
261+ // / * A number indicating the current tracking speed setting for mouse or trackpad
236262// /
237263// / Notes:
238- // / * This is represented in the System Preferences as the "Tracking speed" setting for mice
264+ // / * This is represented in the System Preferences as the "Tracking speed" setting for Mouse or Trackpad
239265// / * Note that not all values will work, they should map to the steps defined in the System Preferences app, which are:
240266// / * 0.0, 0.125, 0.5, 0.6875, 0.875, 1.0, 1.5, 2.0, 2.5, 3.0
241267// / * Note that changes to this value will not be noticed immediately by macOS
242268static int mouse_mouseAcceleration (lua_State *L) {
243- LuaSkin *skin = LS_API (LS_TNUMBER | LS_TOPTIONAL, LS_TBREAK);
269+ LuaSkin *skin = LS_API (LS_TNUMBER | LS_TBOOLEAN | LS_TNIL | LS_TOPTIONAL, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK);
270+
244271 HSmouse *mouseManager = [[HSmouse alloc ] init ];
245272
246- if (lua_type (skin.L , 1 ) == LUA_TNUMBER) {
247- kern_return_t result = [mouseManager setTrackingSpeed: lua_tonumber (skin.L, 1 )];
273+ BOOL isTrackpad = NO ;
274+ if (lua_gettop (L) > 0 ) {
275+ isTrackpad = (lua_type (L, -1 ) != LUA_TNUMBER) ? (BOOL )(lua_toboolean (L, -1 )) : NO ;
276+ }
277+
278+ if (lua_type (L, 1 ) == LUA_TNUMBER) {
279+ kern_return_t result = isTrackpad ? [mouseManager setTrackpadTrackingSpeed: lua_tonumber (L, 1 )] :
280+ [mouseManager setTrackingSpeed: lua_tonumber (L, 1 )] ;
281+
248282 if (result != KERN_SUCCESS) {
249- [skin logError: [NSString stringWithFormat: @" Unable to set mouse tracking speed: %d " , result]];
283+ [skin logError: [NSString stringWithFormat: @" Unable to set %s tracking speed: %d " , (isTrackpad ? " trackpad " : " mouse " ) , result]];
250284 }
251285 }
252286
253- lua_pushnumber (skin. L , mouseManager.trackingSpeed );
287+ lua_pushnumber (L, isTrackpad ? mouseManager. trackpadTrackingSpeed : mouseManager.trackingSpeed );
254288 return 1 ;
255289}
256290
@@ -286,20 +320,20 @@ static int mouse_scrollDirection(lua_State *L) {
286320// / * This function can also return daVinciResolveHorizontalArrows, when hovering over mouse-draggable text-boxes in DaVinci Resolve. This is determined using the "hotspot" value of the cursor.
287321static int mouse_currentCursorType (lua_State *L) {
288322 LuaSkin *skin = LS_API (LS_TBREAK);
289-
323+
290324 NSString *value = @" unknown" ;
291-
325+
292326 NSCursor *currentCursor = [NSCursor currentSystemCursor ];
293-
327+
294328 // Abort if the current cursor can't be detected:
295329 if (currentCursor == nil ) {
296330 [skin pushNSObject: value];
297331 return 1 ;
298332 }
299-
333+
300334 NSImage *currentCursorImage = [currentCursor image ];
301335 NSData *currentCursorData = [currentCursorImage TIFFRepresentation ];
302-
336+
303337 // NOTE: Whilst you can just compare [NSCursor currentCursor] values using ==, the same is not true for [NSCursor currentSystemCursor],
304338 // for some weird reason, hence why the only solution I could come up with was to compare the image data.
305339 if ([currentCursorData isEqualToData: [[[NSCursor arrowCursor ] image ] TIFFRepresentation ]]) { value = @" arrowCursor" ; }
@@ -326,7 +360,7 @@ static int mouse_currentCursorType(lua_State *L) {
326360 value = @" daVinciResolveHorizontalArrows" ;
327361 }
328362 }
329-
363+
330364 [skin pushNSObject: value];
331365 return 1 ;
332366}
0 commit comments