From f33febed1514e69659012c692b5615f7c5815c40 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 17 Jun 2019 22:28:27 +1000 Subject: [PATCH] Menubar partially works on OSX now --- src/InputHandler.c | 1 + src/Window.c | 28 ++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/InputHandler.c b/src/InputHandler.c index 4751ccb4d..a0bf9405c 100644 --- a/src/InputHandler.c +++ b/src/InputHandler.c @@ -416,6 +416,7 @@ static void InputHandler_MouseWheel(void* obj, float delta) { static void InputHandler_MouseMove(void* obj, int xDelta, int yDelta) { struct Screen* active = Gui_GetActiveScreen(); + /* In case MouseMove is called before game is fully initialised */ if (active) Elem_HandlesMouseMove(active, Mouse_X, Mouse_Y); } diff --git a/src/Window.c b/src/Window.c index 753f88033..28bc697e2 100644 --- a/src/Window.c +++ b/src/Window.c @@ -1436,6 +1436,7 @@ void Window_DisableRawMouse(void) { Window_DefaultDisableRawMouse(); } #ifdef CC_BUILD_CARBON #include #include +#include static WindowRef win_handle; static bool win_fullscreen; @@ -1659,6 +1660,8 @@ static OSStatus Window_EventHandler(EventHandlerCallRef inCaller, EventRef inEve return eventNotHandledErr; } +typedef EventTargetRef (*GetMenuBarEventTarget_Func)(void); + static void Window_ConnectEvents(void) { static EventTypeSpec eventTypes[] = { { kEventClassApplication, kEventAppActivated }, @@ -1685,13 +1688,30 @@ static void Window_ConnectEvents(void) { { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent }, { kEventClassAppleEvent, kEventAppleEvent } }; + GetMenuBarEventTarget_Func getMenuBarEventTarget; EventTargetRef target; - OSStatus res; target = GetWindowEventTarget(win_handle); - res = InstallEventHandler(target, NewEventHandlerUPP(Window_EventHandler), - Array_Elems(eventTypes), eventTypes, NULL, NULL); - if (res) Logger_Abort2(res, "Connecting events"); + InstallEventHandler(target, NewEventHandlerUPP(Window_EventHandler), + Array_Elems(eventTypes), eventTypes, NULL, NULL); + + /* The code below is to get the menubar working. */ + /* The documentation for 'RunApplicationEventLoop' states that it installs */ + /* the standard application event handler which lets the menubar work. */ + /* However, we cannot use that since the event loop is managed by us instead. */ + /* Unfortunately, there is no proper API to duplicate that behaviour, so reply */ + /* on the undocumented GetMenuBarEventTarget to achieve similar behaviour. */ + getMenuBarEventTarget = dlsym(RTLD_DEFAULT, "GetMenuBarEventTarget"); + InstallStandardEventHandler(GetApplicationEventTarget()); + + /* TODO: Why does this not work properly? and why does it break with quit? */ + if (getMenuBarEventTarget) { + InstallStandardEventHandler(getMenuBarEventTarget()); + } else { + Platform_LogConst("MenuBar won't work!"); + } + /* MenuRef menu = AcquireRootMenu(); */ + /* InstallStandardEventHandler(GetMenuEventTarget(menu)); */ }