[PATCH 1/2] Load allegro5.cfg from the directory containing the executable, not the initial working directory.

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


This change is a bit more involved than it first appears.
al_get_standard_path() is not usable before the system driver is
initialised.  Probably it was unnecessary to have it as a system driver
method.
---
 allegro5.cfg                        |    9 ++++
 include/allegro5/platform/aintosx.h |    2 +-
 include/allegro5/platform/aintwin.h |    3 +
 src/macosx/system.m                 |    9 ++--
 src/system.c                        |   76 ++++++++++++++++++++++++----------
 src/win/wsystem.c                   |    4 +-
 6 files changed, 72 insertions(+), 31 deletions(-)

diff --git a/allegro5.cfg b/allegro5.cfg
index ad52301..06ca9e9 100644
--- a/allegro5.cfg
+++ b/allegro5.cfg
@@ -1,3 +1,12 @@
+#
+#  Configuration file for the Allegro 5 library.
+#
+#  This file should be either in the same directory as your program.
+#
+#  On Unix, this file may also be stored as ~/.allegro5rc or /etc/allegro5rc.
+#  If multiple files exist, they will be merged, with values from more specific
+#  files overriding the less specific files.
+
 [graphics]
 
 # Graphics driver.
diff --git a/include/allegro5/platform/aintosx.h b/include/allegro5/platform/aintosx.h
index e6fa499..fd0bb5a 100644
--- a/include/allegro5/platform/aintosx.h
+++ b/include/allegro5/platform/aintosx.h
@@ -111,7 +111,7 @@ typedef struct
 
 int _al_osx_bootstrap_ok(void);
 
-void _al_setup_direct_shifts(void);
+ALLEGRO_PATH *_al_osx_get_path(int id);
 
 void _al_osx_keyboard_handler(int pressed, NSEvent *event, ALLEGRO_DISPLAY*);
 void _al_osx_keyboard_modifiers(unsigned int new_mods, ALLEGRO_DISPLAY*);
diff --git a/include/allegro5/platform/aintwin.h b/include/allegro5/platform/aintwin.h
index 4ceabdc..8cbe0ff 100644
--- a/include/allegro5/platform/aintwin.h
+++ b/include/allegro5/platform/aintwin.h
@@ -79,6 +79,9 @@ struct ALLEGRO_DISPLAY_WIN
 };
 
 
+/* standard path */
+ALLEGRO_PATH *_al_win_get_path(int id);
+
 /* thread routines */
 void _al_win_thread_init(void);
 void _al_win_thread_exit(void);
diff --git a/src/macosx/system.m b/src/macosx/system.m
index c5c827f..77980c5 100644
--- a/src/macosx/system.m
+++ b/src/macosx/system.m
@@ -50,7 +50,6 @@ typedef struct THREAD_AND_POOL {
 static ALLEGRO_SYSTEM* osx_sys_init(int flags);
 ALLEGRO_SYSTEM_INTERFACE *_al_system_osx_driver(void);
 static void osx_sys_exit(void);
-static ALLEGRO_PATH *osx_get_path(int id);
 
 
 /* Global variables */
@@ -511,7 +510,7 @@ ALLEGRO_SYSTEM_INTERFACE *_al_system_osx_driver(void)
       vt->create_mouse_cursor = _al_osx_create_mouse_cursor;
       vt->destroy_mouse_cursor = _al_osx_destroy_mouse_cursor;
       vt->get_cursor_position = osx_get_cursor_position;
-      vt->get_path = osx_get_path;
+      vt->get_path = _al_osx_get_path;
       vt->inhibit_screensaver = osx_inhibit_screensaver;
       vt->thread_init = osx_thread_init;
       vt->thread_exit = osx_thread_exit;
@@ -531,8 +530,8 @@ void _al_register_system_interfaces()
    *add = _al_system_osx_driver();
 }
 
-/* Implentation of get_path */
-static ALLEGRO_PATH *osx_get_path(int id)
+/* Implementation of get_path */
+ALLEGRO_PATH *_al_osx_get_path(int id)
 {
    NSString* ans = nil;
    NSArray* paths = nil;
@@ -547,7 +546,7 @@ static ALLEGRO_PATH *osx_get_path(int id)
             path = al_create_path_for_directory([ans UTF8String]);
          } else {
             /* Otherwise, return the executable pathname */
-            path = osx_get_path(ALLEGRO_EXENAME_PATH);
+            path = _al_osx_get_path(ALLEGRO_EXENAME_PATH);
             al_set_path_filename(path, NULL);
          }
          break;
diff --git a/src/system.c b/src/system.c
index 2f77b58..239e457 100644
--- a/src/system.c
+++ b/src/system.c
@@ -103,6 +103,25 @@ static void shutdown_system_driver(void)
 
 
 
+/* al_get_standard_path() does not work before the system driver is
+ * initialised.  Before that, we need to call the underlying functions
+ * directly.
+ */
+static ALLEGRO_PATH *early_get_exename_path(void)
+{
+#if defined(ALLEGRO_WINDOWS)
+   return _al_win_get_path(ALLEGRO_EXENAME_PATH);
+#elif defined(ALLEGRO_MACOSX)
+   return _al_osx_get_path(ALLEGRO_EXENAME_PATH);
+#elif defined(ALLEGRO_UNIX)
+   return _al_unix_get_path(ALLEGRO_EXENAME_PATH);
+#else
+   #error early_get_exename_path not implemented
+#endif
+}
+
+
+
 static void read_allegro_cfg(void)
 {
    /* We cannot use any logging in this function as it would cause the
@@ -110,39 +129,48 @@ static void read_allegro_cfg(void)
     * have been read in.
     */
 
-#if defined ALLEGRO_UNIX && !defined ALLEGRO_GP2XWIZ && !defined ALLEGRO_IPHONE
-   ALLEGRO_CONFIG *temp;
+   /* We assume that the stdio file interface is in effect. */
+
    ALLEGRO_PATH *path;
+   ALLEGRO_CONFIG *temp;
+
+   active_sysdrv->config = NULL;
 
+#if defined(ALLEGRO_UNIX) && !defined(ALLEGRO_GP2XWIZ) && !defined(ALLEGRO_IPHONE)
    active_sysdrv->config = al_load_config_file("/etc/allegro5rc");
 
    path = _al_unix_get_path(ALLEGRO_USER_HOME_PATH);
-   al_set_path_filename(path, "allegro5rc");
-   temp = al_load_config_file(al_path_cstr(path, '/'));
-   if (temp) {
-      if (active_sysdrv->config) {
-         al_merge_config_into(active_sysdrv->config, temp);
-         al_destroy_config(temp);
-      }
-      else {
-         active_sysdrv->config = temp;
+   if (path) {
+      al_set_path_filename(path, "allegro5rc");
+      temp = al_load_config_file(al_path_cstr(path, '/'));
+      if (temp) {
+         if (active_sysdrv->config) {
+            al_merge_config_into(active_sysdrv->config, temp);
+            al_destroy_config(temp);
+         }
+         else {
+            active_sysdrv->config = temp;
+         }
       }
+      al_destroy_path(path);
    }
-   al_destroy_path(path);
+#endif
 
-   temp = al_load_config_file("allegro5.cfg");
-   if (temp) {
-      if (active_sysdrv->config) {
-         al_merge_config_into(active_sysdrv->config, temp);
-         al_destroy_config(temp);
-      }
-      else {
-         active_sysdrv->config = temp;
+   path = early_get_exename_path();
+   if (path) {
+      al_set_path_filename(path, "allegro5.cfg");
+      temp = al_load_config_file(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP));
+      if (temp) {
+         if (active_sysdrv->config) {
+            al_merge_config_into(active_sysdrv->config, temp);
+            al_destroy_config(temp);
+         }
+         else {
+            active_sysdrv->config = temp;
+         }
       }
+      al_destroy_path(path);
    }
-#else
-   active_sysdrv->config = al_load_config_file("allegro5.cfg");
-#endif
 
    /* Always have a configuration available whether or not a config file
     * exists.
@@ -314,6 +342,8 @@ ALLEGRO_CONFIG *al_get_system_config(void)
 ALLEGRO_PATH *al_get_standard_path(int id)
 {
    ASSERT(active_sysdrv);
+   ASSERT(active_sysdrv->vt);
+   ASSERT(active_sysdrv->vt->get_path);
 
    if (active_sysdrv->vt->get_path)
       return active_sysdrv->vt->get_path(id);
diff --git a/src/win/wsystem.c b/src/win/wsystem.c
index 4ea4103..fb66f11 100644
--- a/src/win/wsystem.c
+++ b/src/win/wsystem.c
@@ -388,7 +388,7 @@ static ALLEGRO_MOUSE_DRIVER *win_get_mouse_driver(void)
  *  Returns full path to various system and user diretories
  */
 
-static ALLEGRO_PATH *win_get_path(int id)
+ALLEGRO_PATH *_al_win_get_path(int id)
 {
    char path[MAX_PATH];
    uint32_t csidl = 0;
@@ -644,7 +644,7 @@ static ALLEGRO_SYSTEM_INTERFACE *_al_system_win_driver(void)
    vt->destroy_mouse_cursor = _al_win_destroy_mouse_cursor;
    vt->get_monitor_info = win_get_monitor_info;
    vt->get_cursor_position = win_get_cursor_position;
-   vt->get_path = win_get_path;
+   vt->get_path = _al_win_get_path;
    vt->inhibit_screensaver = win_inhibit_screensaver;
    vt->open_library = win_open_library;
    vt->import_symbol = win_import_symbol;
-- 
1.7.3.2


--qMm9M+Fa2AknHoGS--




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