comparison gtk/dw.c @ 2857:59106bf7f9f4

GTK2/3/4: Refactor dw_init(), splitting off path detection into _dw_init_path(). Using argv[0] to detect the executable path is very inaccurate, so we will add platform specific code for Linux and FreeBSD to query the executable path. If platform specific code fails, fall back to argv[0] like before.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 15 Nov 2022 10:18:52 +0000
parents 86286f528adf
children ecfbc48e933a
comparison
equal deleted inserted replaced
2856:de1a0cd26691 2857:59106bf7f9f4
42 # endif 42 # endif
43 #endif 43 #endif
44 44
45 #if GTK_MAJOR_VERSION > 1 45 #if GTK_MAJOR_VERSION > 1
46 #include <gdk-pixbuf/gdk-pixbuf.h> 46 #include <gdk-pixbuf/gdk-pixbuf.h>
47 #endif
48
49 #ifdef __FreeBSD__
50 #include <sys/param.h>
51 #include <sys/sysctl.h>
47 #endif 52 #endif
48 53
49 #if __STDC_VERSION__ < 199901L 54 #if __STDC_VERSION__ < 199901L
50 # if __GNUC__ >= 2 55 # if __GNUC__ >= 2
51 # define __func__ __FUNCTION__ 56 # define __func__ __FUNCTION__
2090 } 2095 }
2091 return dw_init(newthread, argc, argv); 2096 return dw_init(newthread, argc, argv);
2092 } 2097 }
2093 #endif 2098 #endif
2094 2099
2095 /* 2100 void _dw_init_path(char *arg)
2096 * Initializes the Dynamic Windows engine. 2101 {
2097 * Parameters: 2102 char path[PATH_MAX+1] = {0};
2098 * newthread: True if this is the only thread. 2103
2099 * False if there is already a message loop running. 2104 #ifdef __linux__
2100 */ 2105 if(readlink("/proc/self/exe", path, PATH_MAX) == -1)
2101 int dw_init(int newthread, int argc, char *argv[]) 2106 #elif defined(__FreeBSD__)
2102 { 2107 int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
2103 int z; 2108 size_t length = PATH_MAX;
2104 char *fname; 2109
2105 static char * _dw_test_xpm[] = { 2110 if(sysctl(name, 4, exe, &length, NULL, 0) == -1 || length <= 1)
2106 "1 1 1 1", 2111 #endif
2107 " c None", 2112 strncpy(path, arg ? arg : "", PATH_MAX);
2108 " "}; 2113
2109 2114 if(path[0])
2110 2115 {
2111 /* Setup the private data directory */ 2116 char *pos = strrchr(path, '/');
2112 if(argc > 0 && argv[0]) 2117 char *binname = path;
2113 {
2114 char *pathcopy = strdup(argv[0]);
2115 char *pos = strrchr(pathcopy, '/');
2116 char *binname = pathcopy;
2117 2118
2118 /* If we have a / then... 2119 /* If we have a / then...
2119 * the binary name should be at the end. 2120 * the binary name should be at the end.
2120 */ 2121 */
2121 if(pos) 2122 if(pos)
2124 *pos = 0; 2125 *pos = 0;
2125 } 2126 }
2126 2127
2127 if(*binname) 2128 if(*binname)
2128 { 2129 {
2129 char *binpos = strstr(pathcopy, "/bin"); 2130 char *binpos = strstr(path, "/bin");
2130 2131
2131 if(binpos) 2132 if(binpos)
2132 strncpy(_dw_share_path, pathcopy, (size_t)(binpos - pathcopy)); 2133 strncpy(_dw_share_path, path, (size_t)(binpos - path));
2133 else 2134 else
2134 strcpy(_dw_share_path, "/usr/local"); 2135 strcpy(_dw_share_path, "/usr/local");
2135 strcat(_dw_share_path, "/share/"); 2136 strcat(_dw_share_path, "/share/");
2136 strcat(_dw_share_path, binname); 2137 strcat(_dw_share_path, binname);
2137 #if GLIB_CHECK_VERSION(2,28,0) 2138 #if GLIB_CHECK_VERSION(2,28,0)
2140 /* If we have a binary name, use that for the Application ID instead. */ 2141 /* If we have a binary name, use that for the Application ID instead. */
2141 snprintf(_dw_app_id, _DW_APP_ID_SIZE, "%s.%s", DW_APP_DOMAIN_DEFAULT, binname); 2142 snprintf(_dw_app_id, _DW_APP_ID_SIZE, "%s.%s", DW_APP_DOMAIN_DEFAULT, binname);
2142 } 2143 }
2143 #endif 2144 #endif
2144 } 2145 }
2145 if(pathcopy)
2146 free(pathcopy);
2147 } 2146 }
2148 /* If that failed... just get the current directory */ 2147 /* If that failed... just get the current directory */
2149 if(!_dw_share_path[0] && !getcwd(_dw_share_path, PATH_MAX)) 2148 if(!_dw_share_path[0] && !getcwd(_dw_share_path, PATH_MAX))
2150 _dw_share_path[0] = '/'; 2149 _dw_share_path[0] = '/';
2150 }
2151
2152 /*
2153 * Initializes the Dynamic Windows engine.
2154 * Parameters:
2155 * newthread: True if this is the only thread.
2156 * False if there is already a message loop running.
2157 */
2158 int dw_init(int newthread, int argc, char *argv[])
2159 {
2160 int z;
2161 char *fname;
2162 static char * _dw_test_xpm[] = {
2163 "1 1 1 1",
2164 " c None",
2165 " "};
2166
2167
2168 /* Setup the private data directory */
2169 _dw_init_path(argc > 0 ? argv[0] : NULL);
2151 2170
2152 gtk_set_locale(); 2171 gtk_set_locale();
2153 #if !GLIB_CHECK_VERSION(2,32,0) 2172 #if !GLIB_CHECK_VERSION(2,32,0)
2154 g_thread_init(NULL); 2173 g_thread_init(NULL);
2155 #endif 2174 #endif