Fixed some problems in 0.60.0
[wmaker-crm.git] / WINGs / wapplication.c
blob5b979492ce9cd59c7654f862fb5c9eaaada072cb
3 #include <unistd.h>
5 #include "WINGsP.h"
7 #include <X11/Xutil.h>
9 /* Xmd.h which is indirectly included by GNUstep.h defines BOOL,
10 * but libPropList also defines it. So we do this kluge to get rid of BOOL
11 * temporarily */
12 #ifdef BOOL
13 # define WINGS_BOOL
14 # undef BOOL
15 #endif
17 #include "GNUstep.h"
19 #ifdef WINGS_BOOL
20 # define BOOL
21 # undef WINGS_BOOL
22 #endif
26 extern void W_InitNotificationCenter(void);
29 typedef struct W_Application {
30 char *applicationName;
31 int argc;
32 char **argv;
33 char *resourcePath;
34 } W_Application;
37 struct W_Application WMApplication;
40 char *_WINGS_progname = NULL;
44 Bool
45 W_ApplicationInitialized(void)
47 return _WINGS_progname!=NULL;
51 void
52 WMInitializeApplication(char *applicationName, int *argc, char **argv)
54 int i;
56 assert(argc!=NULL);
57 assert(argv!=NULL);
58 assert(applicationName!=NULL);
60 _WINGS_progname = argv[0];
62 WMApplication.applicationName = wstrdup(applicationName);
63 WMApplication.argc = *argc;
65 WMApplication.argv = wmalloc((*argc+1)*sizeof(char*));
66 for (i=0; i<*argc; i++) {
67 WMApplication.argv[i] = wstrdup(argv[i]);
69 WMApplication.argv[i] = NULL;
71 /* initialize notification center */
72 W_InitNotificationCenter();
76 void
77 WMSetResourcePath(char *path)
79 if (WMApplication.resourcePath)
80 free(WMApplication.resourcePath);
81 WMApplication.resourcePath = wstrdup(path);
85 char*
86 WMGetApplicationName()
88 return WMApplication.applicationName;
92 static char*
93 checkFile(char *path, char *folder, char *ext, char *resource)
95 char *ret;
96 int extralen;
98 extralen = (ext ? strlen(ext) : 0) + (folder ? strlen(folder) : 0) + 4;
99 ret = wmalloc(strlen(path)+strlen(resource)+extralen+8);
100 strcpy(ret, path);
101 if (folder) {
102 strcat(ret, "/");
103 strcat(ret, folder);
105 if (ext) {
106 strcat(ret, "/");
107 strcat(ret, ext);
109 strcat(ret, "/");
110 strcat(ret, resource);
112 if (access(ret, F_OK)!=0) {
113 free(ret);
114 ret = NULL;
117 return ret;
121 char*
122 WMPathForResourceOfType(char *resource, char *ext)
124 char *path = NULL;
125 char *tmp, *appdir;
126 int i;
129 * Paths are searched in this order:
130 * - resourcePath/ext
131 * - argv[0]/ext
132 * - GNUSTEP_USER_ROOT/Apps/ApplicationName.app/ext
133 * - ~/GNUstep/Apps/ApplicationName.app/ext
134 * - GNUSTEP_LOCAL_ROOT/Apps/ApplicationName.app/ext
135 * - /usr/local/GNUstep/Apps/ApplicationName.app/ext
136 * - GNUSTEP_SYSTEM_ROOT/Apps/ApplicationName.app/ext
137 * - /usr/GNUstep/Apps/ApplicationName.app/ext
140 if (WMApplication.resourcePath) {
141 path = checkFile(WMApplication.resourcePath, NULL, ext, resource);
142 if (path)
143 return path;
146 if (WMApplication.argv[0]) {
147 tmp = wstrdup(WMApplication.argv[0]);
148 i = strlen(tmp);
149 while (i > 0 && tmp[i]!='/')
150 i--;
151 tmp[i] = 0;
152 if (i>0) {
153 path = checkFile(tmp, NULL, ext, resource);
154 } else {
155 path = NULL;
157 free(tmp);
158 if (path)
159 return path;
162 appdir = wmalloc(strlen(WMApplication.applicationName)+10);
163 sprintf(appdir, "Apps/%s.app", WMApplication.applicationName);
165 if (getenv("GNUSTEP_USER_ROOT")) {
166 path = checkFile(getenv("GNUSTEP_USER_ROOT"), appdir, ext, resource);
167 if (path) {
168 free(appdir);
169 return path;
173 tmp = wusergnusteppath();
174 if (tmp) {
175 path = checkFile(tmp, appdir, ext, resource);
176 if (path) {
177 free(appdir);
178 return path;
182 if (getenv("GNUSTEP_LOCAL_ROOT")) {
183 path = checkFile(getenv("GNUSTEP_LOCAL_ROOT"), appdir, ext, resource);
184 if (path) {
185 free(appdir);
186 return path;
190 path = checkFile("/usr/local/GNUstep", appdir, ext, resource);
191 if (path) {
192 free(appdir);
193 return path;
197 if (getenv("GNUSTEP_SYSTEM_ROOT")) {
198 path = checkFile(getenv("GNUSTEP_SYSTEM_ROOT"), appdir, ext, resource);
199 if (path) {
200 free(appdir);
201 return path;
205 path = checkFile("/usr/GNUstep", appdir, ext, resource);
206 if (path) {
207 free(appdir);
208 return path;
211 return NULL;
215 /********************* WMScreen related stuff ********************/
218 void
219 WMSetApplicationIconImage(WMScreen *scr, WMPixmap *icon)
221 if (scr->applicationIcon)
222 WMReleasePixmap(scr->applicationIcon);
224 scr->applicationIcon = WMRetainPixmap(icon);
226 if (scr->groupLeader) {
227 XWMHints *hints;
229 hints = XGetWMHints(scr->display, scr->groupLeader);
230 hints->flags |= IconPixmapHint|IconMaskHint;
231 hints->icon_pixmap = icon->pixmap;
232 hints->icon_mask = icon->mask;
234 XSetWMHints(scr->display, scr->groupLeader, hints);
235 XFree(hints);
240 WMPixmap*
241 WMGetApplicationIconImage(WMScreen *scr)
243 return scr->applicationIcon;
247 void
248 WMSetApplicationHasAppIcon(WMScreen *scr, Bool flag)
250 scr->aflags.hasAppIcon = flag;
254 void
255 W_InitApplication(WMScreen *scr)
257 Window leader;
258 XClassHint *classHint;
259 XWMHints *hints;
261 leader = XCreateSimpleWindow(scr->display, scr->rootWin, -1, -1,
262 1, 1, 0, 0, 0);
264 if (!scr->aflags.simpleApplication) {
265 classHint = XAllocClassHint();
266 classHint->res_name = "groupLeader";
267 classHint->res_class = WMApplication.applicationName;
268 XSetClassHint(scr->display, leader, classHint);
269 XFree(classHint);
271 XSetCommand(scr->display, leader, WMApplication.argv,
272 WMApplication.argc);
274 hints = XAllocWMHints();
276 hints->flags = WindowGroupHint;
277 hints->window_group = leader;
279 if (scr->applicationIcon) {
280 hints->flags |= IconPixmapHint;
281 hints->icon_pixmap = scr->applicationIcon->pixmap;
282 if (scr->applicationIcon->mask) {
283 hints->flags |= IconMaskHint;
284 hints->icon_mask = scr->applicationIcon->mask;
288 XSetWMHints(scr->display, leader, hints);
290 XFree(hints);
292 scr->groupLeader = leader;