Skip to content

Commit 863d9b4

Browse files
authored
feat(mac): Fix MacOS implementation (#13)
* add some logs * oops * change to `geode::ObjcHook`s * oops * error * more messages * remove ifdef for target platform * maybe fix includes * forgot cocos2d namespace * use std::string instead of std::string_view * revert ObjcHook change * cast original implementation * wrong macro argument used * cast hook * fix how events are handeled * fix Command + A crashing * ok i might be stupid * make command + A, C, V, X cross-keyboard compatible * remove logs * remove unused logs
1 parent 282f5c3 commit 863d9b4

File tree

2 files changed

+34
-67
lines changed

2 files changed

+34
-67
lines changed

src/macos.mm

Lines changed: 33 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
#ifdef GEODE_IS_MACOS
2-
31
#define CommentType CommentTypeDummy
42
#import <Foundation/Foundation.h>
53
#import <Cocoa/Cocoa.h>
6-
#import <CoreGraphics/CoreGraphics.h>
74
#undef CommentType
85

6+
#include <Carbon/Carbon.h>
7+
#import <objc/runtime.h>
8+
9+
#include <Geode/modify/CCTouchDispatcher.hpp>
910
#include <Geode/cocos/platform/mac/CCEventDispatcher.h>
1011
#import <Geode/cocos/platform/mac/EAGLView.h>
11-
#import <objc/runtime.h>
1212

1313
#include "BetterTextInputNode.hpp"
1414

@@ -33,16 +33,16 @@ inline bool keyDown(PlatformKey key, NSEvent* event)
3333

3434
#define HOOK_OBJC_METHOD(klass, type, cleanFuncName, funcName) \
3535
auto cleanFuncName ## Method = class_getInstanceMethod(klass, @selector(funcName)); \
36-
cleanFuncName ## OIMP = method_getImplementation(cleanFuncName ## Method); \
37-
method_setImplementation(cleanFuncName ## Method, (type)&funcName);
36+
cleanFuncName ## OIMP = reinterpret_cast<type>(method_getImplementation(cleanFuncName ## Method)); \
37+
method_setImplementation(cleanFuncName ## Method, reinterpret_cast<IMP>(&cleanFuncName));
3838

3939
using KeyEventType = void(*)(EAGLView*, SEL, NSEvent*);
4040

4141

4242
static KeyEventType keyDownExecOIMP;
4343
void keyDownExec(EAGLView* self, SEL sel, NSEvent* event) {
4444
if (!g_selectedInput)
45-
keyDownExecOIMP(self, sel, event);
45+
return keyDownExecOIMP(self, sel, event);
4646

4747
// on click, can be held
4848
if (
@@ -52,13 +52,11 @@ void keyDownExec(EAGLView* self, SEL sel, NSEvent* event) {
5252
switch ([event keyCode])
5353
{
5454
case kVK_Escape:
55-
g_selectedInput->deselectInput();
56-
break;
55+
return g_selectedInput->deselectInput();
5756

5857
case kVK_Delete:
5958
case kVK_ForwardDelete:
60-
g_selectedInput->onDelete(false, [event keyCode] == kVK_ForwardDelete);
61-
break;
59+
return g_selectedInput->onDelete(false, [event keyCode] == kVK_ForwardDelete);
6260

6361
default:
6462
break;
@@ -68,28 +66,33 @@ void keyDownExec(EAGLView* self, SEL sel, NSEvent* event) {
6866
switch ([event keyCode])
6967
{
7068
case kVK_RightArrow:
71-
g_selectedInput->onRightArrowKey(
69+
return g_selectedInput->onRightArrowKey(
7270
BI::platform::keyDown(BI::PlatformKey::LEFT_CONTROL, event),
7371
BI::platform::keyDown(BI::PlatformKey::LEFT_SHIFT, event)
7472
);
75-
break;
7673

7774
case kVK_LeftArrow:
78-
g_selectedInput->onLeftArrowKey(
75+
return g_selectedInput->onLeftArrowKey(
7976
BI::platform::keyDown(BI::PlatformKey::LEFT_CONTROL, event),
8077
BI::platform::keyDown(BI::PlatformKey::LEFT_SHIFT, event)
8178
);
82-
break;
8379

8480
default:
8581
break;
8682
}
8783

8884
if (
8985
![event isARepeat] &&
90-
!BI::platform::keyDown(BI::PlatformKey::LEFT_CONTROL, event) &&
91-
BI::platform::keyDown(BI::PlatformKey::LEFT_SHIFT, event)
86+
BI::platform::keyDown(BI::PlatformKey::LEFT_CONTROL, event) &&
87+
!BI::platform::keyDown(BI::PlatformKey::LEFT_SHIFT, event)
9288
) {
89+
// https://github.com/WebKit/WebKit/blob/5c8281f146cfbf4b6189b435b80c527f138b829f/Source/WebCore/platform/mac/PlatformEventFactoryMac.mm#L559
90+
int code = [[event characters] length] > 0
91+
? [[event characters] characterAtIndex:0]
92+
: [[event charactersIgnoringModifiers] length] > 0
93+
? [[event charactersIgnoringModifiers] characterAtIndex:0]
94+
: 0;
95+
9396
switch ([event keyCode])
9497
{
9598
case kVK_Delete:
@@ -101,43 +104,19 @@ void keyDownExec(EAGLView* self, SEL sel, NSEvent* event) {
101104
break;
102105
}
103106

104-
/*
105-
int code = [[event characters] length] > 0
106-
? [[event characters] characterAtIndex:0]
107-
: [[event charactersIgnoringModifiers] length] > 0
108-
? [[event charactersIgnoringModifiers] characterAtIndex:0]
109-
: 0;
110-
*/
111-
112-
int code = 0;
113-
{
114-
NSString* s = [event characters];
115-
code = [s length] > 0 ? [s characterAtIndex:0] : 0;
116-
117-
if (code == 0)
118-
{
119-
s = [event charactersIgnoringModifiers];
120-
code = [s length] > 0 ? [s characterAtIndex:0] : 0;
121-
}
122-
}
123-
124107
switch (code)
125108
{
126109
case 'a': case 'A':
127-
g_selectedInput->highlightFromToPos(0, -1);
128-
break;
110+
return g_selectedInput->highlightFromToPos(0, -1);
129111

130112
case 'c': case 'C':
131-
g_selectedInput->onCopy();
132-
break;
113+
return g_selectedInput->onCopy();
133114

134115
case 'v': case 'V':
135-
g_selectedInput->onPaste();
136-
break;
116+
return g_selectedInput->onPaste();
137117

138118
case 'x': case 'X':
139-
g_selectedInput->onCut();
140-
break;
119+
return g_selectedInput->onCut();
141120

142121
default:
143122
break;
@@ -151,40 +130,33 @@ void keyDownExec(EAGLView* self, SEL sel, NSEvent* event) {
151130
switch ([event keyCode])
152131
{
153132
case kVK_Home:
154-
g_selectedInput->onHomeKey(
133+
return g_selectedInput->onHomeKey(
155134
BI::platform::keyDown(BI::PlatformKey::LEFT_SHIFT, event)
156135
);
157-
break;
158136

159137
case kVK_End:
160-
g_selectedInput->onEndKey(
138+
return g_selectedInput->onEndKey(
161139
BI::platform::keyDown(BI::PlatformKey::LEFT_SHIFT, event)
162140
);
163-
break;
164141

165142
default:
166143
break;
167144
}
168145
}
169-
170-
if (
171-
!BI::platform::keyDown(BI::PlatformKey::LEFT_SHIFT, event) &&
172-
!BI::platform::keyDown(BI::PlatformKey::LEFT_CONTROL, event) &&
173-
[event keyCode] == kVK_Return
174-
) {
175-
keyDownExec(self, sel, event);
176-
}
177146
}
147+
148+
// key is probably a regular character, allow CCIMEDispatcher to pick up the event
149+
keyDownExecOIMP(self, sel, event);
178150
}
179151

180152
static KeyEventType keyUpExecOIMP;
181153
void keyUpExec(EAGLView* self, SEL sel, NSEvent* event) {
182154
if (!g_selectedInput)
183-
keyUpExec(self, sel, event);
155+
return keyUpExecOIMP(self, sel, event);
184156
}
185157

186158

187-
// TODO: move to hooking mouseDownExec
159+
// TODO: move to hooking mouseDownExec/mouseUpExec
188160
// handles mouse clicks
189161
struct BetterTouchDispatcher : geode::Modify<BetterTouchDispatcher, cocos2d::CCTouchDispatcher>
190162
{
@@ -199,7 +171,7 @@ void touches(cocos2d::CCSet* touches, cocos2d::CCEvent* event, unsigned int type
199171

200172
if (type == TouchMessageType::Began)
201173
{
202-
CCSize winSize = cocos2d::CCDirector::sharedDirector()->getWinSize();
174+
cocos2d::CCSize winSize = cocos2d::CCDirector::sharedDirector()->getWinSize();
203175

204176
// the touch event's origin is bottom left
205177
cocos2d::CCTouch touch{};
@@ -216,12 +188,11 @@ void touches(cocos2d::CCSet* touches, cocos2d::CCEvent* event, unsigned int type
216188
};
217189

218190

191+
// https://github.com/qimiko/click-on-steps/blob/d8a87e93b5407e5f2113a9715363a5255724c901/src/macos.mm#L101
219192
$on_mod(Loaded)
220193
{
221194
auto eaglView = objc_getClass("EAGLView");
222195

223196
HOOK_OBJC_METHOD(eaglView, KeyEventType, keyDownExec, keyDownExec:);
224197
HOOK_OBJC_METHOD(eaglView, KeyEventType, keyUpExec, keyUpExec:);
225198
}
226-
227-
#endif

src/windows.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#ifdef GEODE_IS_WINDOWS
2-
31
#include <Geode/modify/CCEGLView.hpp>
42
#include <Geode/cocos/robtop/glfw/glfw3.h>
53

@@ -145,7 +143,7 @@ struct BetterCCEGLView : Modify<BetterCCEGLView, CCEGLView>
145143
!BI::platform::keyDown(BI::PlatformKey::LEFT_CONTROL) &&
146144
key == GLFW_KEY_ENTER
147145
) {
148-
CCEGLView::onGLFWKeyCallback(window, key, scancode, action, mods);
146+
return CCEGLView::onGLFWKeyCallback(window, key, scancode, action, mods);
149147
}
150148
}
151149
}
@@ -178,5 +176,3 @@ struct BetterCCEGLView : Modify<BetterCCEGLView, CCEGLView>
178176
g_selectedInput->useUpdateBlinkPos(false);
179177
}
180178
};
181-
182-
#endif

0 commit comments

Comments
 (0)