Re: [AD] set_config_file jumping around on OS X bundles

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


Please find attached a patch to move the 'magic chdir' to earlier in the 
startup process, so that the config file will be looked for in the resource 
directory (this is {BundleName}.app/Contents/Resources/{executable-name}) if 
is present. I also made more use of the file and path manipulation functions 
provided by Cocoa, which should make it more robust.

Files in the resource directory are usually writeable (even thought it's not 
the way these things should be done) because most Macs are single user 
systems and that user either builds the app or copies it from a disk archive 
which means that he/she becomes the owner. I think so ,anyway.

In other words this is a partial solution but it is now 'not worse than' the 
Linux situation.

If someone could check over this I'd be grateful.

Pete


On Monday 22 August 2005 21:50, Peter Hull wrote:
> Back on topic ... :P
> I've groked the code now, but what is it supposed to do in the ideal
> world? I'm assuming it will look in the current directory for a 'bare'
> exe, and in the bundle's resource directory for a bundle. Is that OK
> for 4.2? I think for 4.3 a more advanced hierarchy of configs is
> needed, which works consistently cross-platform
>
> Pete
>
> On Monday, August 22, 2005, at 06:05  pm, Evert Glebbeek wrote:
> >> - Decide if this isn't a bit late to go into 4.2.0
> >
> > In my opinion, it is. I think we're beyond the `add function to vtable'
> > stage.
> >
> >> - If yes, find a way what to do for 4.2.0 (some platform specific
> >>   set_config_file/override_config_file magic)
> >
> > If at all possible, let's try to resolev this before thursday.
> >
> > Evert
> >
> >
> > -------------------------------------------------------
> > SF.Net email is Sponsored by the Better Software Conference & EXPO
> > September 19-22, 2005 * San Francisco, CA * Development Lifecycle
> > Practices
> > Agile & Plan-Driven Development * Managing Projects & Teams * Testing
> > & QA
> > Security * Process Improvement & Measurement *
> > http://www.sqe.com/bsce5sf
> > --
> > https://lists.sourceforge.net/lists/listinfo/alleg-developers
>
> -------------------------------------------------------
> SF.Net email is Sponsored by the Better Software Conference & EXPO
> September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
> Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
> Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
Index: src/macosx/main.m
===================================================================
RCS file: /cvsroot/alleg/allegro/src/macosx/main.m,v
retrieving revision 1.26
diff -u -r1.26 main.m
--- src/macosx/main.m	24 Jul 2005 04:41:18 -0000	1.26
+++ src/macosx/main.m	24 Aug 2005 20:54:45 -0000
@@ -59,8 +59,9 @@
    FSCatalogInfo processInfo;
    ProcessSerialNumber psn = { 0, kCurrentProcess };
    CFDictionaryRef mode;
-   char path[1024], *p;
-   int i;
+   NSString* exename, *resdir;
+   NSFileManager* fm;
+   BOOL isDir;
 
    /* create mutex */
     osx_event_mutex=_unix_create_mutex();
@@ -73,14 +74,28 @@
    GetProcessBundleLocation(&psn, &processRef);
    FSGetCatalogInfo(&processRef, kFSCatInfoNodeFlags, &processInfo, NULL, NULL, NULL);
    if (processInfo.nodeFlags & kFSNodeIsDirectoryMask) {
-      _al_sane_strncpy(path, __crt0_argv[0], sizeof(path));
-      for (i = 0; i < 4; i++) {
-         for (p = path + strlen(path); (p >= path) && (*p != '/'); p--);
-         *p = '\0';
-      }
-      chdir(path);
-      osx_bundle = [NSBundle mainBundle];
-      arg0 = strdup([[osx_bundle bundlePath] lossyCString]);
+       /*
+        In a bundle, so chdir to the containing directory,
+        or to the 'magic' resource directory if it exists.
+        (see the readme.osx file for more info)
+        */
+       osx_bundle = [NSBundle mainBundle];
+       exename=[[osx_bundle executablePath] lastPathComponent];
+       resdir=[[osx_bundle resourcePath] stringByAppendingPathComponent: exename];
+       resdir=[resdir stringByStandardizingPath];
+       fm=[NSFileManager defaultManager];
+       if ([fm fileExistsAtPath: resdir isDirectory: &isDir] && isDir)
+       {
+           /* Yes, it exists inside the bundle */
+           [fm changeCurrentDirectoryPath: resdir];
+       }
+       else
+       {
+           /* No, change to the directory containing the bundle*/
+           [fm changeCurrentDirectoryPath: [osx_bundle bundlePath]];
+       }
+            
+      arg0 = strdup([[osx_bundle bundlePath] fileSystemRepresentation]);
       if (arg1) {
          static char *args[2];
 	 args[0] = arg0;
@@ -93,6 +108,7 @@
          __crt0_argc = 1;
       }
    }
+   /* else: not in a bundle so don't chdir */
    
    mode = CGDisplayCurrentMode(kCGDirectMainDisplay);
    CFNumberGetValue(CFDictionaryGetValue(mode, kCGDisplayRefreshRate), kCFNumberSInt32Type, &refresh_rate);
Index: src/macosx/system.m
===================================================================
RCS file: /cvsroot/alleg/allegro/src/macosx/system.m,v
retrieving revision 1.30
diff -u -r1.30 system.m
--- src/macosx/system.m	20 Aug 2005 21:43:28 -0000	1.30
+++ src/macosx/system.m	24 Aug 2005 20:54:52 -0000
@@ -24,11 +24,9 @@
    #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 */
@@ -406,14 +404,11 @@
    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];
-      snprintf(resource_dir, 1023, "%s/%s", [[osx_bundle resourcePath] lossyCString], get_filename(exe_name));
-      if (file_exists(resource_dir, FA_DIREC, NULL))
-         chdir(resource_dir);
+   if (osx_bundle==NULL) {
+       /* If in a bundle, the dock will recognise us automatically */
+       osx_tell_dock();
    }
    
    /* Setup OS type & version */


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