Initial revision
[wmaker-crm.git] / WINGs / wapplication.c
blobfa9c637fc63b05f130e1605f33864ede0be5191b
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_ReadConfigurations(void);
28 extern void W_InitNotificationCenter(void);
31 typedef struct W_Application {
32 char *applicationName;
33 int argc;
34 char **argv;
35 char *resourcePath;
36 } W_Application;
39 struct W_Application WMApplication;
42 char *_WINGS_progname = NULL;
46 Bool
47 W_ApplicationInitialized(void)
49 return _WINGS_progname!=NULL;
53 void
54 WMInitializeApplication(char *applicationName, int *argc, char **argv)
56 int i;
58 assert(argc!=NULL);
59 assert(argv!=NULL);
60 assert(applicationName!=NULL);
62 _WINGS_progname = argv[0];
64 WMApplication.applicationName = wstrdup(applicationName);
65 WMApplication.argc = *argc;
67 WMApplication.argv = wmalloc((*argc+1)*sizeof(char*));
68 for (i=0; i<*argc; i++) {
69 WMApplication.argv[i] = wstrdup(argv[i]);
71 WMApplication.argv[i] = NULL;
73 /* initialize notification center */
74 W_InitNotificationCenter();
76 /* read general configuration data for WINGs */
77 W_ReadConfigurations();
81 void
82 WMSetResourcePath(char *path)
84 if (WMApplication.resourcePath)
85 free(WMApplication.resourcePath);
86 WMApplication.resourcePath = wstrdup(path);
90 char*
91 WMGetApplicationName()
93 return WMApplication.applicationName;
97 static char*
98 checkFile(char *path, char *folder, char *ext, char *resource)
100 char *ret;
101 int extralen;
103 extralen = (ext ? strlen(ext) : 0) + (folder ? strlen(folder) : 0) + 4;
104 ret = wmalloc(strlen(path)+strlen(resource)+extralen+8);
105 strcpy(ret, path);
106 if (folder) {
107 strcat(ret, "/");
108 strcat(ret, folder);
110 if (ext) {
111 strcat(ret, "/");
112 strcat(ret, ext);
114 strcat(ret, "/");
115 strcat(ret, resource);
117 if (access(ret, F_OK)!=0) {
118 free(ret);
119 ret = NULL;
122 return ret;
126 char*
127 WMPathForResourceOfType(char *resource, char *ext)
129 char *path = NULL;
130 char *tmp, *appdir;
131 int i;
134 * Paths are searched in this order:
135 * - resourcePath/ext
136 * - argv[0]/ext
137 * - GNUSTEP_USER_ROOT/Apps/ApplicationName.app/ext
138 * - ~/GNUstep/Apps/ApplicationName.app/ext
139 * - GNUSTEP_LOCAL_ROOT/Apps/ApplicationName.app/ext
140 * - /usr/local/GNUstep/Apps/ApplicationName.app/ext
141 * - GNUSTEP_SYSTEM_ROOT/Apps/ApplicationName.app/ext
142 * - /usr/GNUstep/Apps/ApplicationName.app/ext
145 if (WMApplication.resourcePath) {
146 path = checkFile(WMApplication.resourcePath, NULL, ext, resource);
147 if (path)
148 return path;
151 if (WMApplication.argv[0]) {
152 tmp = wstrdup(WMApplication.argv[0]);
153 i = strlen(tmp);
154 while (i > 0 && tmp[i]!='/')
155 i--;
156 tmp[i] = 0;
157 if (i>0) {
158 path = checkFile(tmp, NULL, ext, resource);
159 } else {
160 path = NULL;
162 free(tmp);
163 if (path)
164 return path;
167 appdir = wmalloc(strlen(WMApplication.applicationName)+8);
168 sprintf(appdir, "Apps/%s.app", WMApplication.applicationName);
170 if (getenv("GNUSTEP_USER_ROOT")) {
171 path = checkFile(getenv("GNUSTEP_USER_ROOT"), appdir, ext, resource);
172 if (path) {
173 free(appdir);
174 return path;
178 tmp = wusergnusteppath();
179 if (tmp) {
180 path = checkFile(tmp, appdir, ext, resource);
181 free(tmp);
182 if (path) {
183 free(appdir);
184 return path;
188 if (getenv("GNUSTEP_LOCAL_ROOT")) {
189 path = checkFile(getenv("GNUSTEP_LOCAL_ROOT"), appdir, ext, resource);
190 if (path) {
191 free(appdir);
192 return path;
196 path = checkFile("/usr/local/GNUstep", appdir, ext, resource);
197 if (path) {
198 free(appdir);
199 return path;
203 if (getenv("GNUSTEP_SYSTEM_ROOT")) {
204 path = checkFile(getenv("GNUSTEP_SYSTEM_ROOT"), appdir, ext, resource);
205 if (path) {
206 free(appdir);
207 return path;
211 path = checkFile("/usr/GNUstep", appdir, ext, resource);
212 if (path) {
213 free(appdir);
214 return path;
217 return NULL;
221 /********************* WMScreen related stuff ********************/
224 void
225 WMSetApplicationIconImage(WMScreen *scr, WMPixmap *icon)
227 if (scr->applicationIcon)
228 WMReleasePixmap(scr->applicationIcon);
230 scr->applicationIcon = WMRetainPixmap(icon);
232 if (scr->groupLeader) {
233 XWMHints *hints;
235 hints = XGetWMHints(scr->display, scr->groupLeader);
236 hints->flags |= IconPixmapHint|IconMaskHint;
237 hints->icon_pixmap = icon->pixmap;
238 hints->icon_mask = icon->mask;
240 XSetWMHints(scr->display, scr->groupLeader, hints);
241 XFree(hints);
246 WMPixmap*
247 WMGetApplicationIconImage(WMScreen *scr)
249 return scr->applicationIcon;
253 void
254 WMSetApplicationHasAppIcon(WMScreen *scr, Bool flag)
256 scr->aflags.hasAppIcon = flag;
260 void
261 W_InitApplication(WMScreen *scr)
263 Window leader;
264 XClassHint *classHint;
265 XWMHints *hints;
267 leader = XCreateSimpleWindow(scr->display, scr->rootWin, -1, -1,
268 1, 1, 0, 0, 0);
270 if (!scr->aflags.simpleApplication) {
271 classHint = XAllocClassHint();
272 classHint->res_name = "groupLeader";
273 classHint->res_class = WMApplication.applicationName;
274 XSetClassHint(scr->display, leader, classHint);
275 XFree(classHint);
277 XSetCommand(scr->display, leader, WMApplication.argv,
278 WMApplication.argc);
280 hints = XAllocWMHints();
282 hints->flags = WindowGroupHint;
283 hints->window_group = leader;
285 if (scr->applicationIcon) {
286 hints->flags |= IconPixmapHint;
287 hints->icon_pixmap = scr->applicationIcon->pixmap;
288 if (scr->applicationIcon->mask) {
289 hints->flags |= IconMaskHint;
290 hints->icon_mask = scr->applicationIcon->mask;
294 XSetWMHints(scr->display, leader, hints);
296 XFree(hints);
298 scr->groupLeader = leader;