Re: Fwd: [AD] OSX Dead bootstrap fix

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On 2005-07-23, Peter Hull <peter.hull90@xxxxxxxxxx> wrote:
> >
> >On 2005-07-12, Peter Hull <peter.hull90@xxxxxxxxxx> wrote:
> >>Please find below a patch to avoid the dead bootstrap issue as
> >>discussed on a.cc. It also cleans up the dock notification code, and
...
> Here is the patch as an attachment:

Thanks.  Committed it with changes (attached).  Hope I didn't break
anything.

> I thought putting patches inline was better, so that subscribers to the 
> daily summary can see them too. Is this not the case?

I don't know, but inline patches are a pain.

Peter
Index: include/allegro/platform/aintosx.h
===================================================================
RCS file: /cvsroot/alleg/allegro/include/allegro/platform/aintosx.h,v
retrieving revision 1.28
diff -u -r1.28 aintosx.h
--- include/allegro/platform/aintosx.h	26 Jun 2005 14:50:51 -0000	1.28
+++ include/allegro/platform/aintosx.h	24 Jul 2005 04:35:58 -0000
@@ -119,6 +119,7 @@
 
 
 void osx_event_handler(void);
+int osx_bootstrap_ok(void);
 
 void setup_direct_shifts(void);
 void osx_init_fade_system(void);
Index: src/macosx/main.m
===================================================================
RCS file: /cvsroot/alleg/allegro/src/macosx/main.m,v
retrieving revision 1.25
diff -u -r1.25 main.m
--- src/macosx/main.m	26 Jun 2005 14:50:51 -0000	1.25
+++ src/macosx/main.m	24 Jul 2005 04:35:58 -0000
@@ -37,18 +37,6 @@
 static int refresh_rate = 70;
 
 
-/* These are used to warn the dock about the application */
-typedef struct CPSProcessSerNum
-{
-   UInt32 lo;
-   UInt32 hi;
-} CPSProcessSerNum;
-
-extern OSErr CPSGetCurrentProcess( CPSProcessSerNum *psn);
-extern OSErr CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
-extern OSErr CPSSetFrontProcess( CPSProcessSerNum *psn);
-
-
 
 @implementation AllegroAppDelegate
 
@@ -157,17 +145,22 @@
 
 
 
+/* Call the user main() */
+static void call_user_main(void)
+{
+   int (*real_main)(int, char*[]) = (int (*)(int, char*[])) _mangled_main_address;
+   exit(real_main(__crt0_argc, __crt0_argv));
+}
+
+
+
 /* app_main:
  *  Thread dedicated to the user program; real main() gets called here.
  */
 + (void)app_main: (id)arg
 {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-   int (*real_main) (int, char*[]) = (int (*) (int, char*[])) _mangled_main_address;
-   
-   /* Call the user main() */
-   exit(real_main(__crt0_argc, __crt0_argv));
-   
+   call_user_main();
    [pool release];
 }
 
@@ -206,22 +199,15 @@
 {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    AllegroAppDelegate *app_delegate = [[AllegroAppDelegate alloc] init];
-   CPSProcessSerNum psn;
    NSMenu *menu;
    NSMenuItem *menu_item, *temp_item;
    
    __crt0_argc = argc;
    __crt0_argv = argv;
    
-   [NSApplication sharedApplication];
+   if (!osx_bootstrap_ok()) /* not safe to use NSApplication */
+      call_user_main();
       
-   /* Tell the dock about us; the origins of this hack are unknown, but it's
-    * currently the only way to make a Cocoa app to work when started from a
-    * console.
-    */
-   if ((!CPSGetCurrentProcess(&psn)) &&
-       (!CPSEnableForegroundOperation(&psn, 0x03, 0x3C, 0x2C, 0x1103)) &&
-       (!CPSSetFrontProcess(&psn)))
       [NSApplication sharedApplication];
    
    /* Creates a custom application menu */
Index: src/macosx/system.m
===================================================================
RCS file: /cvsroot/alleg/allegro/src/macosx/system.m,v
retrieving revision 1.28
diff -u -r1.28 system.m
--- src/macosx/system.m	26 Jun 2005 14:50:51 -0000	1.28
+++ src/macosx/system.m	24 Jul 2005 04:35:59 -0000
@@ -24,6 +24,22 @@
    #error something is wrong with the makefile
 #endif
 
+#include <CoreFoundation/CoreFoundation.h>
+#include <mach/mach_port.h>
+#include <servers/bootstrap.h>
+
+
+/* These are used to warn the dock about the application */
+struct CPSProcessSerNum
+{
+   UInt32 lo;
+   UInt32 hi;
+};
+extern OSErr CPSGetCurrentProcess(struct CPSProcessSerNum *psn);
+extern OSErr CPSEnableForegroundOperation(struct CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
+extern OSErr CPSSetFrontProcess(struct CPSProcessSerNum *psn);
+
+
 
 static int osx_sys_init(void);
 static void osx_sys_exit(void);
@@ -322,6 +338,49 @@
 
 
 
+/* osx_tell_dock:
+ *  Tell the dock about us; the origins of this hack are unknown, but it's
+ *  currently the only way to make a Cocoa app to work when started from a
+ *  console.
+ *  For the future, (10.3 and above) investigate TranformProcessType in the 
+ *  HIServices framework.
+ */
+static void osx_tell_dock(void)
+{
+   struct CPSProcessSerNum psn;
+
+   if ((!CPSGetCurrentProcess(&psn)) &&
+       (!CPSEnableForegroundOperation(&psn, 0x03, 0x3C, 0x2C, 0x1103)) &&
+       (!CPSSetFrontProcess(&psn)))
+      [NSApplication sharedApplication];
+}
+
+
+
+/* osx_bootstrap_ok:
+ *  Check if the current bootstrap context is privilege. If it's not, we can't
+ *  use NSApplication, and instead have to go directly to main.
+ *  Returns 1 if ok, 0 if not.
+ */
+int osx_bootstrap_ok(void)
+{
+   static int _ok = -1;
+   mach_port_t bp;
+   kern_return_t ret;
+   CFMachPortRef cfport;
+
+   /* If have tested once, just return that answer */
+   if (_ok >= 0)
+      return _ok;
+   cfport = CFMachPortCreate(NULL, NULL, NULL, NULL);
+   task_get_bootstrap_port(mach_task_self(), &bp);
+   ret = bootstrap_register(bp, "bootstrap-ok-test", CFMachPortGetPort(cfport));
+   _ok = (ret == KERN_SUCCESS) ? 1 : 0;
+   return _ok;
+}
+
+
+
 /* osx_sys_init:
  *  Initalizes the MacOS X system driver.
  */
@@ -331,6 +390,11 @@
    AL_CONST char *exe_name;
    char resource_dir[1024];
    
+   /* If we're in the 'dead bootstrap' environment, the Mac driver won't work. */
+   if (!osx_bootstrap_ok()) {
+      return -1;
+   }
+   
    /* Install emergency-exit signal handlers */
    old_sig_abrt = signal(SIGABRT, osx_signal_handler);
    old_sig_fpe  = signal(SIGFPE,  osx_signal_handler);
@@ -340,6 +404,8 @@
    old_sig_int  = signal(SIGINT,  osx_signal_handler);
    old_sig_quit = signal(SIGQUIT, osx_signal_handler);
    
+   osx_tell_dock();
+
    /* Get into bundle resource directory if appropriate */
    if (osx_bundle) {
       exe_name = [[osx_bundle executablePath] lossyCString];


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/