3 #include <X11/Xlocale.h>
7 #include "userdefaults.h"
10 struct W_Application WMApplication
;
12 char *_WINGS_progname
= NULL
;
14 Bool
W_ApplicationInitialized(void)
16 return _WINGS_progname
!= NULL
;
19 void WMInitializeApplication(const char *applicationName
, int *argc
, char **argv
)
25 assert(applicationName
!= NULL
);
27 setlocale(LC_ALL
, "");
30 if (getenv("NLSPATH"))
31 bindtextdomain("WINGs", getenv("NLSPATH"));
33 bindtextdomain("WINGs", LOCALEDIR
);
34 bind_textdomain_codeset("WINGs", "UTF-8");
37 _WINGS_progname
= argv
[0];
39 WMApplication
.applicationName
= wstrdup(applicationName
);
40 WMApplication
.argc
= *argc
;
42 WMApplication
.argv
= wmalloc((*argc
+ 1) * sizeof(char *));
43 for (i
= 0; i
< *argc
; i
++) {
44 WMApplication
.argv
[i
] = wstrdup(argv
[i
]);
46 WMApplication
.argv
[i
] = NULL
;
48 /* initialize notification center */
49 W_InitNotificationCenter();
52 void WMReleaseApplication(void) {
56 * We save the configuration on exit, this used to be handled
57 * through an 'atexit' registered function but if application
58 * properly calls WMReleaseApplication then the info to that
59 * will have been freed by us.
61 w_save_defaults_changes();
63 W_ReleaseNotificationCenter();
65 if (WMApplication
.applicationName
) {
66 wfree(WMApplication
.applicationName
);
67 WMApplication
.applicationName
= NULL
;
70 if (WMApplication
.argv
) {
71 for (i
= 0; i
< WMApplication
.argc
; i
++)
72 wfree(WMApplication
.argv
[i
]);
74 wfree(WMApplication
.argv
);
75 WMApplication
.argv
= NULL
;
79 void WMSetResourcePath(const char *path
)
81 if (WMApplication
.resourcePath
)
82 wfree(WMApplication
.resourcePath
);
83 WMApplication
.resourcePath
= wstrdup(path
);
86 char *WMGetApplicationName()
88 return WMApplication
.applicationName
;
91 static char *checkFile(const char *path
, const char *folder
, const char *ext
, const char *resource
)
97 if (!path
|| !resource
)
100 extralen
= (ext
? strlen(ext
) + 1 : 0) + (folder
? strlen(folder
) + 1 : 0) + 1;
101 slen
= strlen(path
) + strlen(resource
) + 1 + extralen
;
104 if (wstrlcpy(ret
, path
, slen
) >= slen
)
108 (wstrlcat(ret
, "/", slen
) >= slen
||
109 wstrlcat(ret
, folder
, slen
) >= slen
))
113 (wstrlcat(ret
, "/", slen
) >= slen
||
114 wstrlcat(ret
, ext
, slen
) >= slen
))
117 if (wstrlcat(ret
, "/", slen
) >= slen
||
118 wstrlcat(ret
, resource
, slen
) >= slen
)
121 if (access(ret
, F_OK
) != 0)
132 char *WMPathForResourceOfType(const char *resource
, const char *ext
)
134 char *path
, *tmp
, *appdir
;
138 path
= tmp
= appdir
= NULL
;
141 * Paths are searched in this order:
143 * - dirname(argv[0])/ext
144 * - GNUSTEP_USER_ROOT/Applications/ApplicationName.app/ext
145 * - ~/GNUstep/Applications/ApplicationName.app/ext
146 * - GNUSTEP_LOCAL_ROOT/Applications/ApplicationName.app/ext
147 * - /usr/local/GNUstep/Applications/ApplicationName.app/ext
148 * - GNUSTEP_SYSTEM_ROOT/Applications/ApplicationName.app/ext
149 * - /usr/GNUstep/Applications/ApplicationName.app/ext
152 if (WMApplication
.resourcePath
) {
153 path
= checkFile(WMApplication
.resourcePath
, NULL
, ext
, resource
);
158 if (WMApplication
.argv
[0]) {
159 tmp
= wstrdup(WMApplication
.argv
[0]);
161 while (i
> 0 && tmp
[i
] != '/')
165 path
= checkFile(tmp
, NULL
, ext
, resource
);
172 slen
= strlen(WMApplication
.applicationName
) + sizeof("Applications/.app");
173 appdir
= wmalloc(slen
);
174 if (snprintf(appdir
, slen
, "Applications/%s.app", WMApplication
.applicationName
) >= slen
)
177 path
= checkFile(getenv("GNUSTEP_USER_ROOT"), appdir
, ext
, resource
);
181 path
= checkFile(wusergnusteppath(), appdir
, ext
, resource
);
185 path
= checkFile(getenv("GNUSTEP_LOCAL_ROOT"), appdir
, ext
, resource
);
189 path
= checkFile("/usr/local/GNUstep", appdir
, ext
, resource
);
193 path
= checkFile(getenv("GNUSTEP_SYSTEM_ROOT"), appdir
, ext
, resource
);
197 path
= checkFile("/usr/GNUstep", appdir
, ext
, resource
); /* falls through */