Skip to content

Commit be068ef

Browse files
authored
Addressing bug kwhat#90 (kwhat#93)
1 parent a62dfd6 commit be068ef

File tree

4 files changed

+54
-17
lines changed

4 files changed

+54
-17
lines changed

.github/workflows/package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Nightly Build
22
on:
33
push:
44
branches:
5-
- 1.2
5+
- '**'
66

77
jobs:
88
apple-x86_64:

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,20 @@ elseif(APPLE)
244244
target_link_libraries(uiohook "${IOKIT}")
245245
endif()
246246

247+
# FIXME Change USE_OBJC flag to USE_APPKIT
248+
#option(USE_APPKIT "AppKit framework (default: ON)" ON)
247249
option(USE_OBJC "Objective-C API (default: ON)" ON)
248250
if(USE_OBJC)
251+
# FIXME Drop USE_OBJC as it is included in AppKit
249252
find_library(OBJC objc REQUIRED)
250253
add_compile_definitions(USE_OBJC)
251254
target_include_directories(uiohook PRIVATE "${OBJC}")
252255
target_link_libraries(uiohook "${OBJC}")
256+
257+
find_library(APPKIT AppKit REQUIRED)
258+
add_compile_definitions(USE_APPKIT)
259+
target_include_directories(uiohook PRIVATE "${APPKIT}")
260+
target_link_libraries(uiohook "${APPKIT}")
253261
endif()
254262

255263
option(USE_CARBON_LEGACY "Legacy Carbon framework functionality (default: OFF)" OFF)

src/darwin/input_helper.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ UniCharCount keycode_to_unicode(CGEventRef event_ref, UniChar *buffer, UniCharCo
112112
}
113113
}
114114
#elif defined(USE_APPLICATION_SERVICES)
115+
// TODO Try https://developer.apple.com/documentation/coregraphics/1456120-cgeventkeyboardgetunicodestring?language=objc
115116
if (CFEqual(CFRunLoopGetCurrent(), CFRunLoopGetMain())) {
116117
// NOTE The following block must execute on the main runloop,
117118
// Ex: CFEqual(CFRunLoopGetCurrent(), CFRunLoopGetMain()) to avoid

src/darwin/input_hook.c

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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
579584
static 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

Comments
 (0)