Allow struts from all windows.
[wmaker-crm.git] / WINGs / wapplication.c
blob070e0a98caddb742cddff7e92baf813d35afd5fe
2 #include <unistd.h>
3 #include <X11/Xlocale.h>
5 #include "WINGsP.h"
6 #include "wconfig.h"
8 extern void W_InitNotificationCenter(void);
10 struct W_Application WMApplication;
12 char *_WINGS_progname = NULL;
14 Bool W_ApplicationInitialized(void)
16 return _WINGS_progname != NULL;
19 void WMInitializeApplication(char *applicationName, int *argc, char **argv)
21 int i;
23 assert(argc != NULL);
24 assert(argv != NULL);
25 assert(applicationName != NULL);
27 setlocale(LC_ALL, "");
29 #ifdef I18N
30 if (getenv("NLSPATH"))
31 bindtextdomain("WINGs", getenv("NLSPATH"));
32 else
33 bindtextdomain("WINGs", LOCALEDIR);
34 bind_textdomain_codeset("WINGs", "UTF-8");
35 #endif
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 WMSetResourcePath(char *path)
54 if (WMApplication.resourcePath)
55 wfree(WMApplication.resourcePath);
56 WMApplication.resourcePath = wstrdup(path);
59 char *WMGetApplicationName()
61 return WMApplication.applicationName;
64 static char *checkFile(char *path, char *folder, char *ext, char *resource)
66 char *ret;
67 int extralen;
68 size_t slen;
70 if (!path || !resource)
71 return NULL;
73 extralen = (ext ? strlen(ext) + 1 : 0) + (folder ? strlen(folder) + 1 : 0) + 1;
74 slen = strlen(path) + strlen(resource) + 1 + extralen;
75 ret = wmalloc(slen);
77 if (wstrlcpy(ret, path, slen) >= slen)
78 goto error;
80 if (folder &&
81 (wstrlcat(ret, "/", slen) >= slen ||
82 wstrlcat(ret, folder, slen) >= slen))
83 goto error;
85 if (ext &&
86 (wstrlcat(ret, "/", slen) >= slen ||
87 wstrlcat(ret, ext, slen) >= slen))
88 goto error;
90 if (wstrlcat(ret, "/", slen) >= slen ||
91 wstrlcat(ret, resource, slen) >= slen)
92 goto error;
94 if (access(ret, F_OK) != 0)
95 goto error;
97 return ret;
99 error:
100 if (ret)
101 wfree(ret);
102 return NULL;
105 char *WMPathForResourceOfType(char *resource, char *ext)
107 char *path, *tmp, *appdir;
108 int i;
109 size_t slen;
111 path = tmp = appdir = NULL;
114 * Paths are searched in this order:
115 * - resourcePath/ext
116 * - dirname(argv[0])/ext
117 * - GNUSTEP_USER_ROOT/Applications/ApplicationName.app/ext
118 * - ~/GNUstep/Applications/ApplicationName.app/ext
119 * - GNUSTEP_LOCAL_ROOT/Applications/ApplicationName.app/ext
120 * - /usr/local/GNUstep/Applications/ApplicationName.app/ext
121 * - GNUSTEP_SYSTEM_ROOT/Applications/ApplicationName.app/ext
122 * - /usr/GNUstep/Applications/ApplicationName.app/ext
125 if (WMApplication.resourcePath) {
126 path = checkFile(WMApplication.resourcePath, NULL, ext, resource);
127 if (path)
128 goto out;
131 if (WMApplication.argv[0]) {
132 tmp = wstrdup(WMApplication.argv[0]);
133 i = strlen(tmp);
134 while (i > 0 && tmp[i] != '/')
135 i--;
136 tmp[i] = 0;
137 if (i > 0) {
138 path = checkFile(tmp, NULL, ext, resource);
139 } else {
140 path = NULL;
142 goto out;
145 slen = strlen(WMApplication.applicationName) + sizeof("Applications/.app");
146 appdir = wmalloc(slen);
147 if (snprintf(appdir, slen, "Applications/%s.app", WMApplication.applicationName) >= slen)
148 goto out;
150 path = checkFile(getenv("GNUSTEP_USER_ROOT"), appdir, ext, resource);
151 if (path)
152 goto out;
154 path = checkFile(wusergnusteppath(), appdir, ext, resource);
155 if (path)
156 goto out;
158 path = checkFile(getenv("GNUSTEP_LOCAL_ROOT"), appdir, ext, resource);
159 if (path)
160 goto out;
162 path = checkFile("/usr/local/GNUstep", appdir, ext, resource);
163 if (path)
164 goto out;
166 path = checkFile(getenv("GNUSTEP_SYSTEM_ROOT"), appdir, ext, resource);
167 if (path)
168 goto out;
170 path = checkFile("/usr/GNUstep", appdir, ext, resource); /* falls through */
172 out:
173 if (tmp)
174 wfree(tmp);
175 if (appdir)
176 wfree(appdir);
178 return path;