|
25 | 25 | #include <sstream> |
26 | 26 | #include <fstream> |
27 | 27 | #include <chrono> |
| 28 | +#ifdef __APPLE__ |
| 29 | +#include <objc/runtime.h> |
| 30 | +#include <objc/message.h> |
| 31 | +#include <dlfcn.h> |
| 32 | +#endif |
28 | 33 | #ifndef _WIN32 |
29 | 34 | #include <thread> |
30 | 35 | #endif |
@@ -3364,17 +3369,28 @@ void PApplet::run(){ |
3364 | 3369 | initPerlin(0); // initialize noise table with default seed |
3365 | 3370 |
|
3366 | 3371 | #if defined(__APPLE__) |
3367 | | - // Register with macOS window server -- required when launched as a subprocess |
3368 | | - // (e.g. from Processing IDE). Without this, GLFW cannot create windows. |
| 3372 | + // Make this process a foreground app so GLFW can create windows. |
| 3373 | + // Required when launched as a subprocess from the Processing IDE. |
| 3374 | + // Uses ObjC runtime directly -- no .m file needed. |
3369 | 3375 | { |
3370 | | - void* handle = dlopen("/System/Library/Frameworks/ApplicationServices.framework/ApplicationServices", RTLD_LAZY); |
3371 | | - if (handle) { |
3372 | | - typedef int (*TFunc)(void*); |
3373 | | - TFunc f = (TFunc)dlsym(handle, "TransformProcessType"); |
3374 | | - if (f) { |
3375 | | - // kProcessTransformToForegroundApplication = 1 |
3376 | | - unsigned int psn[2] = {0, 1}; // kCurrentProcess |
3377 | | - f(psn); |
| 3376 | + void* appkit = dlopen("/System/Library/Frameworks/AppKit.framework/AppKit", RTLD_LAZY); |
| 3377 | + if (appkit) { |
| 3378 | + // id NSApp = [NSApplication sharedApplication] |
| 3379 | + typedef void* (*id_imp)(void*, void*); |
| 3380 | + typedef void (*void_imp)(void*, void*, long); |
| 3381 | + typedef void (*void_imp2)(void*, void*, bool); |
| 3382 | + void* cls = (void*)dlsym(RTLD_DEFAULT, "OBJC_CLASS_$_NSApplication"); |
| 3383 | + if (cls) { |
| 3384 | + void* sel_shared = sel_registerName("sharedApplication"); |
| 3385 | + void* sel_policy = sel_registerName("setActivationPolicy:"); |
| 3386 | + void* sel_activate = sel_registerName("activateIgnoringOtherApps:"); |
| 3387 | + id_imp msgSend = (id_imp) objc_msgSend; |
| 3388 | + void* app = msgSend(cls, (void*)sel_shared); |
| 3389 | + if (app) { |
| 3390 | + // NSApplicationActivationPolicyRegular = 0 |
| 3391 | + ((void_imp)objc_msgSend)(app, (void*)sel_policy, 0L); |
| 3392 | + ((void_imp2)objc_msgSend)(app, (void*)sel_activate, true); |
| 3393 | + } |
3378 | 3394 | } |
3379 | 3395 | } |
3380 | 3396 | } |
|
0 commit comments