Update for 0.51.0
[wmaker-crm.git] / WINGs / wapplication.c
blobb55d9d5ef1fcb7d0c6f67c9e91e35540be5150e6
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 free(tmp);
177 if (path) {
178 free(appdir);
179 return path;
183 if (getenv("GNUSTEP_LOCAL_ROOT")) {
184 path = checkFile(getenv("GNUSTEP_LOCAL_ROOT"), appdir, ext, resource);
185 if (path) {
186 free(appdir);
187 return path;
191 path = checkFile("/usr/local/GNUstep", appdir, ext, resource);
192 if (path) {
193 free(appdir);
194 return path;
198 if (getenv("GNUSTEP_SYSTEM_ROOT")) {
199 path = checkFile(getenv("GNUSTEP_SYSTEM_ROOT"), appdir, ext, resource);
200 if (path) {
201 free(appdir);
202 return path;
206 path = checkFile("/usr/GNUstep", appdir, ext, resource);
207 if (path) {
208 free(appdir);
209 return path;
212 return NULL;
216 /********************* WMScreen related stuff ********************/
219 void
220 WMSetApplicationIconImage(WMScreen *scr, WMPixmap *icon)
222 if (scr->applicationIcon)
223 WMReleasePixmap(scr->applicationIcon);
225 scr->applicationIcon = WMRetainPixmap(icon);
227 if (scr->groupLeader) {
228 XWMHints *hints;
230 hints = XGetWMHints(scr->display, scr->groupLeader);
231 hints->flags |= IconPixmapHint|IconMaskHint;
232 hints->icon_pixmap = icon->pixmap;
233 hints->icon_mask = icon->mask;
235 XSetWMHints(scr->display, scr->groupLeader, hints);
236 XFree(hints);
241 WMPixmap*
242 WMGetApplicationIconImage(WMScreen *scr)
244 return scr->applicationIcon;
248 void
249 WMSetApplicationHasAppIcon(WMScreen *scr, Bool flag)
251 scr->aflags.hasAppIcon = flag;
255 void
256 W_InitApplication(WMScreen *scr)
258 Window leader;
259 XClassHint *classHint;
260 XWMHints *hints;
262 leader = XCreateSimpleWindow(scr->display, scr->rootWin, -1, -1,
263 1, 1, 0, 0, 0);
265 if (!scr->aflags.simpleApplication) {
266 classHint = XAllocClassHint();
267 classHint->res_name = "groupLeader";
268 classHint->res_class = WMApplication.applicationName;
269 XSetClassHint(scr->display, leader, classHint);
270 XFree(classHint);
272 XSetCommand(scr->display, leader, WMApplication.argv,
273 WMApplication.argc);
275 hints = XAllocWMHints();
277 hints->flags = WindowGroupHint;
278 hints->window_group = leader;
280 if (scr->applicationIcon) {
281 hints->flags |= IconPixmapHint;
282 hints->icon_pixmap = scr->applicationIcon->pixmap;
283 if (scr->applicationIcon->mask) {
284 hints->flags |= IconMaskHint;
285 hints->icon_mask = scr->applicationIcon->mask;
289 XSetWMHints(scr->display, leader, hints);
291 XFree(hints);
293 scr->groupLeader = leader;