Skip to content

Commit

Permalink
feat(mac): Fix MacOS implementation
Browse files Browse the repository at this point in the history
doubt this even compiles
  • Loading branch information
SpaghettDev committed Nov 29, 2024
1 parent 885c334 commit 0847d25
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 127 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/geode-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
5 changes: 3 additions & 2 deletions mod.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
246 changes: 126 additions & 120 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@
#include <Geode/modify/CCEGLView.hpp>
#include <Geode/cocos/robtop/glfw/glfw3.h>
#elif defined(GEODE_IS_MACOS)
#include <Geode/modify/CCKeyboardDispatcher.hpp>
#include <Geode/modify/CCTouchDispatcher.hpp>
#define CommentType CommentTypeDummy
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import <CoreGraphics/CoreGraphics.h>
#undef CommentType

#include <Geode/cocos/platform/mac/CCEventDispatcher.h>
#import <Geode/cocos/platform/mac/EAGLView.h>
#import <objc/runtime.h>

#include "types/TouchMessageType.hpp"
#endif
Expand Down Expand Up @@ -1427,72 +1434,121 @@ struct BetterCCEGLView : Modify<BetterCCEGLView, CCEGLView>

#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<klass>)&funcName);

static ModifierKeysState g_modifier_keys_state;
#define CALL_OIMP(funcName) reinterpret_cast<decltype(&funcName)>(funcName ## OIMP)(self, sel, event)

// doesnt work. lol!
struct BetterKeyboardDispatcher : Modify<BetterKeyboardDispatcher, CCKeyboardDispatcher>
{
bool dispatchKeyboardMSG(cocos2d::enumKeyCodes key, bool isKeyDown, bool isKeyRepeat)
{
if (!g_selectedInput)
return CCKeyboardDispatcher::dispatchKeyboardMSG(key, isKeyDown, isKeyRepeat);
template <typename T>
using EventType = void(*)(T*, SEL, NSEvent*);

if (key == enumKeyCodes::KEY_LeftControl)

static EventType<EAGLView> 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;

Expand All @@ -1502,80 +1558,23 @@ struct BetterKeyboardDispatcher : Modify<BetterKeyboardDispatcher, CCKeyboardDis
}

if (
isKeyDown &&
g_modifier_keys_state.ctrlDown &&
!g_modifier_keys_state.shiftDown
!BI::platform::keyDown(BI::PlatformKey::LEFT_SHIFT, event) &&
!BI::platform::keyDown(BI::PlatformKey::LEFT_CONTROL, event) &&
[event keyCode] == kVK_Return
) {
switch (key)
{
case enumKeyCodes::KEY_A:
g_selectedInput->highlightFromToPos(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<EAGLView> 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<BetterTouchDispatcher, CCTouchDispatcher>
{
Expand Down Expand Up @@ -1606,4 +1605,11 @@ struct BetterTouchDispatcher : Modify<BetterTouchDispatcher, CCTouchDispatcher>
}
};


$on_mod(Loaded)
{
HOOK_OBJC_METHOD(EAGLView, keyDownExec, keyDownExec:);
HOOK_OBJC_METHOD(EAGLView, keyUpExec, keyUpExec:);
}

#endif
21 changes: 18 additions & 3 deletions src/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include <string>
#include <string_view>

#ifdef GEODE_IS_WINDOWS
#include <Geode/cocos/robtop/glfw/glfw3.h>

#ifdef GEODE_IS_WINDOWS
#include <WinUser.h> // virtual keys
#elif defined(GEODE_IS_MACOS)
#include <AppKit/NSEvent.h>
#endif

#include <Geode/cocos/cocoa/CCGeometry.h>
Expand Down Expand Up @@ -137,20 +139,33 @@ namespace BI
};
namespace platform
{
#ifdef GEODE_IS_WINDOWS
inline bool keyDown(PlatformKey key)
{
#ifdef GEODE_IS_WINDOWS
switch (key)
{
case BI::PlatformKey::LEFT_CONTROL:
return GetKeyState(VK_CONTROL) & 0x8000;
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
Expand Down

0 comments on commit 0847d25

Please sign in to comment.