@@ -565,45 +565,70 @@ static inline void process_modifier_changed(uint64_t timestamp, CGEventRef event
565565 if (current_modifiers & MASK_CAPS_LOCK ) {
566566 // Process as a key pressed event.
567567 unset_modifier_mask (MASK_CAPS_LOCK );
568- process_key_released (timestamp , event_ref );
568+ // Key released handled by process_system_key
569+ // FIXME Add this in with #ifndef USE_OBJC
570+ //process_key_released(timestamp, event_ref);
569571 } else {
570572 // Process as a key released event.
571573 set_modifier_mask (MASK_CAPS_LOCK );
572- process_key_pressed (timestamp , event_ref );
574+ // Key pressed handled by process_system_key
575+ // FIXME Add this in with #ifndef USE_OBJC
576+ //process_key_pressed(timestamp, event_ref);
573577 }
574578 }
575579}
576580
577581/* These events are totally undocumented for the CGEvent type, but are required to grab media and caps-lock keys.
578582 */
583+ // FIXME Omit this function with #ifndef USE_OBJC
579584static inline void process_system_key (uint64_t timestamp , CGEventRef event_ref ) {
580- if ( CGEventGetType (event_ref ) == NX_SYSDEFINED ) {
585+ if ( CGEventGetType (event_ref ) == NX_SYSDEFINED ) {
581586 #ifdef USE_OBJC
582587 // Contributed by Iván Munsuri Ibáñez <[email protected] > and Alex <[email protected] > 583588 id (* eventWithCGEvent )(id , SEL , CGEventRef ) = (id (* )(id , SEL , CGEventRef )) objc_msgSend ;
584589 id event_data = eventWithCGEvent ((id ) objc_getClass ("NSEvent" ), sel_registerName ("eventWithCGEvent:" ), event_ref );
585590
586- int (* eventWithoutCGEvent )(id , SEL ) = (int (* )(id , SEL )) objc_msgSend ;
587- int subtype = eventWithoutCGEvent (event_data , sel_registerName ("subtype" ));
588-
591+ long (* eventWithoutCGEvent )(id , SEL ) = (long (* )(id , SEL )) objc_msgSend ;
592+ int subtype = (int ) eventWithoutCGEvent (event_data , sel_registerName ("subtype" ));
589593 #else
590- CFDataRef data = CGEventCreateData (kCFAllocatorDefault , event_ref );
591- //CFIndex len = CFDataGetLength(data);
592- UInt8 * buffer = malloc (12 );
593- CFDataGetBytes (data , CFRangeMake (108 , 12 ), buffer );
594- UInt32 subtype = CFSwapInt32BigToHost (* ((UInt32 * ) buffer ));
594+ // FIXME We shouldn't be doing this.
595+ CFDataRef data_ref = CGEventCreateData (kCFAllocatorDefault , event_ref );
596+ if (data_ref == NULL ) {
597+ logger (LOG_LEVEL_ERROR , "%s [%u]: Failed to allocate memory for CGEventRef copy!\n" ,
598+ __FUNCTION__ , __LINE__ );
599+ return ;
600+ }
601+
602+ CFIndex len = CFDataGetLength (data_ref );
603+ UInt8 * buffer = malloc (20 );
604+ if (buffer == NULL ) {
605+ CFRelease (data_ref );
606+ logger (LOG_LEVEL_ERROR , "%s [%u]: Failed to allocate memory for CFData range buffer!\n" ,
607+ __FUNCTION__ , __LINE__ );
608+ return ;
609+ }
610+
611+ CFDataGetBytes (data_ref , CFRangeMake (len - 68 , 20 ), buffer );
612+ int subtype = CFSwapInt32BigToHost (* ((UInt32 * ) buffer ));
595613 #endif
596614
597615 if (subtype == 8 ) {
598616 #ifdef USE_OBJC
599617 // Contributed by Alex <[email protected] > 600- int data = eventWithoutCGEvent (event_data , sel_registerName ("data1" ));
618+ long data = eventWithoutCGEvent (event_data , sel_registerName ("data1" ));
619+ #else
620+ // FIXME We shouldn't be doing this.
621+ #ifdef __LP64__
622+ long data = CFSwapInt64BigToHost (* ((UInt64 * ) (buffer + 4 )));
623+ #else
624+ long data = CFSwapInt32BigToHost (* ((UInt32 * ) (buffer + 4 )));
625+ #endif
601626 #endif
602627
603628 int key_code = (data & 0xFFFF0000 ) >> 16 ;
604629 int key_flags = (data & 0xFFFF );
605- // int key_state = (key_flags & 0xFF00) >> 8;
606- bool key_down = (key_flags & 0x1 ) > 0 ;
630+ int key_state = (key_flags & 0xFF00 ) >> 8 ;
631+ bool key_down = (key_state & 0x1 ) > 0 ;
607632
608633 if (key_code == NX_KEYTYPE_CAPS_LOCK ) {
609634 // It doesn't appear like we can modify the event coming in, so we will fabricate a new event.
@@ -721,8 +746,9 @@ static inline void process_system_key(uint64_t timestamp, CGEventRef event_ref)
721746 }
722747
723748 #ifndef USE_OBJC
749+ // FIXME We shouldn't be doing this.
724750 free (buffer );
725- CFRelease (data );
751+ CFRelease (data_ref );
726752 #endif
727753 }
728754}
@@ -933,7 +959,7 @@ CGEventRef hook_event_proc(CGEventTapProxy tap_proxy, CGEventType type, CGEventR
933959 // Get the local system time in UTC.
934960 gettimeofday (& system_time , NULL );
935961
936- // Grab the native event timestap for use later..
962+ // Grab the native event timestamp for use later..
937963 uint64_t timestamp = (uint64_t ) CGEventGetTimestamp (event_ref );
938964
939965 // Get the event class.
@@ -950,6 +976,7 @@ CGEventRef hook_event_proc(CGEventTapProxy tap_proxy, CGEventType type, CGEventR
950976 process_modifier_changed (timestamp , event_ref );
951977 break ;
952978
979+ // FIXME Omit this case with #ifndef USE_OBJC
953980 case NX_SYSDEFINED :
954981 process_system_key (timestamp , event_ref );
955982 break ;
@@ -1184,6 +1211,7 @@ UIOHOOK_API int hook_run() {
11841211 auto_release_pool = eventWithoutCGEvent (pool , sel_registerName ("init" ));
11851212 #endif
11861213
1214+
11871215 // Start the hook thread runloop.
11881216 CFRunLoopRun ();
11891217
0 commit comments