From 0847d2592c4bddd1b0fb379cf364597848017b34 Mon Sep 17 00:00:00 2001 From: SpaghettDev <37266659+SpaghettDev@users.noreply.github.com> Date: Thu, 28 Nov 2024 20:36:23 -0500 Subject: [PATCH] feat(mac): Fix MacOS implementation doubt this even compiles --- .github/workflows/geode-build.yml | 4 +- mod.json | 5 +- src/main.cpp | 246 +++++++++++++++--------------- src/utils.hpp | 21 ++- 4 files changed, 149 insertions(+), 127 deletions(-) diff --git a/.github/workflows/geode-build.yml b/.github/workflows/geode-build.yml index af5e52d..820be14 100644 --- a/.github/workflows/geode-build.yml +++ b/.github/workflows/geode-build.yml @@ -15,8 +15,8 @@ jobs: - name: Windows os: windows-latest - # - name: macOS - # os: macos-latest + - name: macOS + os: macos-latest name: ${{ matrix.config.name }} runs-on: ${{ matrix.config.os }} diff --git a/mod.json b/mod.json index 39cd826..5900953 100644 --- a/mod.json +++ b/mod.json @@ -1,7 +1,8 @@ { - "geode": "4.0.0-beta.1", + "geode": "4.0.1", "gd": { - "win": "2.2074" + "win": "2.2074", + "mac": "2.2074" }, "version": "v4.2.0-beta.3", "id": "spaghettdev.betterinputs", diff --git a/src/main.cpp b/src/main.cpp index 69bf7b3..ee678b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,8 +8,15 @@ #include #include #elif defined(GEODE_IS_MACOS) -#include -#include +#define CommentType CommentTypeDummy +#import +#import +#import +#undef CommentType + +#include +#import +#import #include "types/TouchMessageType.hpp" #endif @@ -1427,72 +1434,121 @@ struct BetterCCEGLView : Modify #elif defined(GEODE_IS_MACOS) -// handles ctrl and shift -struct ModifierKeysState -{ - bool ctrlDown; - bool shiftDown; -}; +#define HOOK_OBJC_METHOD(klass, cleanFuncName, funcName) \ + auto cleanFuncName ## Method = class_getInstanceMethod(objc_getClass(#klass), @selector(funcName)); \ + cleanFuncName ## OIMP = method_getImplementation(cleanFuncName ## Method); \ + method_setImplementation(cleanFuncName ## Method, (EventType)&funcName); -static ModifierKeysState g_modifier_keys_state; +#define CALL_OIMP(funcName) reinterpret_cast(funcName ## OIMP)(self, sel, event) -// doesnt work. lol! -struct BetterKeyboardDispatcher : Modify -{ - bool dispatchKeyboardMSG(cocos2d::enumKeyCodes key, bool isKeyDown, bool isKeyRepeat) - { - if (!g_selectedInput) - return CCKeyboardDispatcher::dispatchKeyboardMSG(key, isKeyDown, isKeyRepeat); +template +using EventType = void(*)(T*, SEL, NSEvent*); - if (key == enumKeyCodes::KEY_LeftControl) + +static EventType keyDownExecOIMP; +void keyDownExec(EAGLView* self, SEL sel, NSEvent* event) { + if (!g_selectedInput) + CALL_OIMP(keyDownExec); + + // on click, can be held + if ( + !BI::platform::keyDown(BI::PlatformKey::LEFT_CONTROL, event) && + !BI::platform::keyDown(BI::PlatformKey::LEFT_SHIFT, event) + ) { + switch ([event keyCode]) { - if (isKeyDown || isKeyRepeat) - g_modifier_keys_state.ctrlDown = true; - else - g_modifier_keys_state.ctrlDown = false; + case kVK_Escape: + g_selectedInput->deselectInput(); + break; + + case kVK_Delete: + case kVK_ForwardDelete: + g_selectedInput->onDelete(false, [event keyCode] == kVK_ForwardDelete); + break; + + default: + break; } + } + + switch ([event keyCode]) + { + case kVK_RightArrow: + g_selectedInput->onRightArrowKey( + BI::platform::keyDown(BI::PlatformKey::LEFT_CONTROL, event), + BI::platform::keyDown(BI::PlatformKey::LEFT_SHIFT, event) + ); + break; + + case kVK_LeftArrow: + g_selectedInput->onLeftArrowKey( + BI::platform::keyDown(BI::PlatformKey::LEFT_CONTROL, event), + BI::platform::keyDown(BI::PlatformKey::LEFT_SHIFT, event) + ); + break; - if (key == enumKeyCodes::KEY_LeftShift) + default: + break; + } + + if ( + ![event isARepeat] && + !BI::platform::keyDown(BI::PlatformKey::LEFT_CONTROL, event) && + BI::platform::keyDown(BI::PlatformKey::LEFT_SHIFT, event) + ) { + int code = [[event characters] length] > 0 + ? [[event characters] characterAtIndex:0]; + : [[event charactersIgnoringModifiers] characterAtIndex:0]; + + switch ([event keyCode]) { - if (isKeyDown || isKeyRepeat) - g_modifier_keys_state.shiftDown = true; - else - g_modifier_keys_state.shiftDown = false; + case kVK_Delete: + case kVK_ForwardDelete: + g_selectedInput->onDelete(true, [event keyCode] == kVK_ForwardDelete); + break; + + default: + break; } - if (isKeyDown || isKeyRepeat) + switch (code) { - if (!g_modifier_keys_state.ctrlDown && !g_modifier_keys_state.shiftDown) - { - switch (key) - { - case enumKeyCodes::KEY_Escape: - g_selectedInput->deselectInput(); - break; + case 'a': case 'A': + g_selectedInput->highlightFromToPos(0, -1); + break; - case enumKeyCodes::KEY_Backspace: - case enumKeyCodes::KEY_Delete: - g_selectedInput->onDelete(false, key == enumKeyCodes::KEY_Delete); - break; + case 'c': case 'C': + g_selectedInput->onCopy(); + break; - default: - break; - } - } + case 'v': case 'V': + g_selectedInput->onPaste(); + break; - switch (key) + case 'x': case 'X': + g_selectedInput->onCut(); + break; + + default: + break; + } + } + + if (![event isARepeat]) + { + if (!BI::platform::keyDown(BI::PlatformKey::LEFT_CONTROL, event)) + { + switch ([event keyCode]) { - case enumKeyCodes::KEY_Right: - g_selectedInput->onRightArrowKey( - g_modifier_keys_state.ctrlDown, - g_modifier_keys_state.shiftDown + case kVK_Home: + g_selectedInput->onHomeKey( + BI::platform::keyDown(BI::PlatformKey::LEFT_SHIFT, event) ); break; - case enumKeyCodes::KEY_Left: - g_selectedInput->onLeftArrowKey( - g_modifier_keys_state.ctrlDown, - g_modifier_keys_state.shiftDown + case kVK_End: + g_selectedInput->onEndKey( + BI::platform::keyDown(BI::PlatformKey::LEFT_SHIFT) ); break; @@ -1502,80 +1558,23 @@ struct BetterKeyboardDispatcher : ModifyhighlightFromToPos(0, -1); - break; - - case enumKeyCodes::KEY_Insert: - case enumKeyCodes::KEY_C: - g_selectedInput->onCopy(); - break; - - case enumKeyCodes::KEY_V: - g_selectedInput->onPaste(); - break; - - case enumKeyCodes::KEY_X: - g_selectedInput->onCut(); - break; - - case enumKeyCodes::KEY_Backspace: - case enumKeyCodes::KEY_Delete: - g_selectedInput->onDelete(true, key == enumKeyCodes::KEY_Delete); - break; - - default: - break; - } + CALL_OIMP(keyDownExec); } + } +} - if (isKeyDown) - { - if (!g_modifier_keys_state.ctrlDown) - { - switch (key) - { - case enumKeyCodes::KEY_Home: - g_selectedInput->onHomeKey( - g_modifier_keys_state.shiftDown - ); - break; - - case enumKeyCodes::KEY_End: - g_selectedInput->onEndKey( - g_modifier_keys_state.ctrlDown - ); - break; - - default: - break; - } - } - - if (g_modifier_keys_state.shiftDown && !g_modifier_keys_state.ctrlDown) - { - switch (key) - { - case enumKeyCodes::KEY_Insert: - g_selectedInput->onPaste(); - break; - - default: - break; - } - } - } +static EventType keyUpExecOIMP; +void keyUpExec(EAGLView* self, SEL sel, NSEvent* event) { + if (!g_selectedInput) + CALL_OIMP(keyUpExec); +} - return false; - } -}; +// TODO: move to hooking mouseDownExec // handles mouse clicks struct BetterTouchDispatcher : Modify { @@ -1606,4 +1605,11 @@ struct BetterTouchDispatcher : Modify } }; + +$on_mod(Loaded) +{ + HOOK_OBJC_METHOD(EAGLView, keyDownExec, keyDownExec:); + HOOK_OBJC_METHOD(EAGLView, keyUpExec, keyUpExec:); +} + #endif diff --git a/src/utils.hpp b/src/utils.hpp index 35e61a1..46b4b7b 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -1,10 +1,12 @@ #include #include +#ifdef GEODE_IS_WINDOWS #include -#ifdef GEODE_IS_WINDOWS #include // virtual keys +#elif defined(GEODE_IS_MACOS) +#include #endif #include @@ -137,9 +139,9 @@ namespace BI }; namespace platform { +#ifdef GEODE_IS_WINDOWS inline bool keyDown(PlatformKey key) { -#ifdef GEODE_IS_WINDOWS switch (key) { case BI::PlatformKey::LEFT_CONTROL: @@ -147,10 +149,23 @@ namespace BI case BI::PlatformKey::LEFT_SHIFT: return GetKeyState(VK_SHIFT) & 0x8000; } + + return false; + } #elif defined(GEODE_IS_MACOS) + inline bool keyDown(PlatformKey key, NSEvent* event) + { + switch (key) + { + case BI::PlatformKey::LEFT_CONTROL: + return ([event modifierFlags] & NSCommandKeyMask); + case BI::PlatformKey::LEFT_SHIFT: + return ([event modifierFlags] & NSShiftKeyMask); + } + return false; -#endif } +#endif } namespace geode