zserge / tray

Cross-platform, super tiny C99 implementation of a system tray icon with a popup menu.
MIT License
493 stars 84 forks source link

Compatibility with GLFW? #13

Closed wormyrocks closed 4 years ago

wormyrocks commented 4 years ago

Hi,

I'm having trouble getting tray.h to work with a GLFW project - as I understand it, both GLFW and tray.h attach their own AppDelegate to the NSApp instance - so if I call glfwInit() after tray_init(), all the tray callbacks stop working, and if I call tray_init() after glfwInit(), all the GLFW callbacks stop working.

Ideally I want my code to stay as crossplatform (mostly because I have no experience with Objective-C) as possible. So if there's a simple way to fix this without doing too much heavy modification (I don't know if it's possible to attach an additional function to an existing delegate at runtime without clobbering it?) it'd be great to know.

Thanks so much!

wormyrocks commented 4 years ago

BTW, got this working with method swizzling, working from this solution (used this code in the tray_init() function) - it's a total hack but it seems to work fine.

id origClass = ((id(*)(id, SEL))objc_msgSend)((id)orig_delegate, sel_registerName("className"));
char* delegateType = ((char*(*)(id,SEL))objc_msgSend)(origClass, sel_registerName("UTF8String"));
Class original_c = objc_getClass(delegateType);
Method swizzled_m = class_getInstanceMethod(trayDelegateClass, sel_registerName("menuCallback:"));
bool didAddMethod = class_addMethod(original_c, sel_registerName("menuCallback:"), method_getImplementation(swizzled_m), method_getTypeEncoding(swizzled_m));