- Finished moving to the new proplist handling code in WINGs.
[wmaker-crm.git] / src / application.c
blob7f9490382ed0344b0f047fe274190eabb0a8e260
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 * Copyright (c) 1998 Dan Pascu
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <errno.h>
34 #include "WindowMaker.h"
35 #include "menu.h"
36 #include "window.h"
37 #ifdef USER_MENU
38 #include "usermenu.h"
39 #endif /* USER_MENU */
40 #include "icon.h"
41 #include "appicon.h"
42 #include "application.h"
43 #include "appmenu.h"
44 #include "properties.h"
45 #include "funcs.h"
46 #include "stacking.h"
47 #include "actions.h"
48 #include "defaults.h"
49 #include "workspace.h"
50 #include "dock.h"
51 #include "wsound.h"
54 /******** Global variables ********/
56 extern XContext wAppWinContext;
57 extern XContext wWinContext;
58 extern WPreferences wPreferences;
60 extern WDDomain *WDWindowAttributes;
62 /******** Local variables ********/
65 static WWindow*
66 makeMainWindow(WScreen *scr, Window window)
68 WWindow *wwin;
69 XWindowAttributes attr;
71 if (!XGetWindowAttributes(dpy, window, &attr)) {
72 return NULL;
75 wwin = wWindowCreate();
76 wwin->screen_ptr = scr;
77 wwin->client_win = window;
78 wwin->main_window = window;
79 wwin->wm_hints = XGetWMHints(dpy, window);
80 /* if (!MyXFetchName(dpy, window, &(wwin->frame->title))) {
81 wwin->frame->title = NULL;
84 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
87 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
88 &wwin->user_flags, &wwin->defined_user_flags, True);
90 XSelectInput(dpy, window, attr.your_event_mask | PropertyChangeMask
91 | StructureNotifyMask );
92 return wwin;
96 WApplication*
97 wApplicationOf(Window window)
99 WApplication *wapp;
101 if (window == None)
102 return NULL;
103 if (XFindContext(dpy, window, wAppWinContext, (XPointer*)&wapp)!=XCSUCCESS)
104 return NULL;
105 return wapp;
109 static WAppIcon*
110 findDockIconFor(WDock *dock, Window main_window)
112 WAppIcon *aicon = NULL;
114 aicon = wDockFindIconForWindow(dock, main_window);
115 if (!aicon) {
116 wDockTrackWindowLaunch(dock, main_window);
117 aicon = wDockFindIconForWindow(dock, main_window);
119 return aicon;
123 static void
124 extractIcon(WWindow *wwin)
126 int argc;
127 char **argv;
129 if (!XGetCommand(dpy, wwin->client_win, &argv, &argc) || argc < 1)
130 return;
132 wApplicationExtractDirPackIcon(wwin->screen_ptr,argv[0],
133 wwin->wm_instance,
134 wwin->wm_class);
135 XFreeStringList(argv);
139 static void
140 saveIconNameFor(char *iconPath, char *wm_instance, char *wm_class)
142 WMPropList *dict = WDWindowAttributes->dictionary;
143 WMPropList *adict, *key, *iconk;
144 WMPropList *val;
145 char *tmp;
146 int i;
148 i = 0;
149 if (wm_instance)
150 i += strlen(wm_instance);
151 if (wm_class)
152 i += strlen(wm_class);
154 tmp = wmalloc(i+8);
155 *tmp = 0;
156 if (wm_class && wm_instance) {
157 sprintf(tmp, "%s.%s", wm_instance, wm_class);
158 } else {
159 if (wm_instance)
160 strcat(tmp, wm_instance);
161 if (wm_class)
162 strcat(tmp, wm_class);
165 key = WMCreatePLString(tmp);
166 wfree(tmp);
167 adict = WMGetFromPLDictionary(dict, key);
169 iconk = WMCreatePLString("Icon");
171 if (adict) {
172 val = WMGetFromPLDictionary(adict, iconk);
173 } else {
174 /* no dictionary for app, so create one */
175 adict = WMCreatePLDictionary(NULL, NULL, NULL);
176 WMPutInPLDictionary(dict, key, adict);
177 WMReleasePropList(adict);
178 val = NULL;
180 if (!val) {
181 val = WMCreatePLString(iconPath);
182 WMPutInPLDictionary(adict, iconk, val);
183 WMReleasePropList(val);
185 WMReleasePropList(key);
186 WMReleasePropList(iconk);
188 if (val && !wPreferences.flags.noupdates)
189 WMWritePropListToFile(dict, WDWindowAttributes->path, True);
193 void
194 wApplicationExtractDirPackIcon(WScreen *scr, char *path,
195 char *wm_instance, char *wm_class)
197 char *iconPath=NULL;
198 /* Maybe the app is a .app and it has an icon in it, like
199 * /usr/local/GNUstep/Apps/WPrefs.app/WPrefs.tiff
201 if (strstr(path, ".app")) {
202 char *tmp;
204 tmp = wmalloc(strlen(path)+16);
206 if (scr->flags.supports_tiff) {
207 strcpy(tmp, path);
208 strcat(tmp, ".tiff");
209 if (access(tmp, R_OK)==0)
210 iconPath = tmp;
212 if (!path) {
213 strcpy(tmp, path);
214 strcat(tmp, ".xpm");
215 if (access(tmp, R_OK)==0)
216 iconPath = tmp;
218 if (!iconPath)
219 wfree(tmp);
222 if (iconPath) {
223 saveIconNameFor(iconPath, wm_instance, wm_class);
225 wfree(iconPath);
230 static Bool
231 extractClientIcon(WAppIcon *icon)
233 char *path;
235 path = wIconStore(icon->icon);
236 if (!path)
237 return False;
239 saveIconNameFor(path, icon->wm_instance, icon->wm_class);
241 wfree(path);
243 return True;
247 WApplication*
248 wApplicationCreate(WScreen *scr, Window main_window)
250 WApplication *wapp;
251 WWindow *leader;
253 if (main_window==None || main_window==scr->root_win) {
254 #ifdef DEBUG0
255 wwarning("trying to create application for %x",(unsigned)main_window);
256 #endif
257 return NULL;
261 Window root;
262 int foo;
263 unsigned int bar;
264 /* check if the window is valid */
265 if (!XGetGeometry(dpy, main_window, &root, &foo, &foo, &bar, &bar,
266 &bar, &bar)) {
267 return NULL;
271 wapp = wApplicationOf(main_window);
272 if (wapp) {
273 wapp->refcount++;
274 return wapp;
277 wapp = wmalloc(sizeof(WApplication));
278 memset(wapp, 0, sizeof(WApplication));
280 wapp->refcount = 1;
281 wapp->last_focused = NULL;
283 wapp->last_workspace = 0;
285 wapp->main_window = main_window;
286 wapp->main_window_desc = makeMainWindow(scr, main_window);
287 if (!wapp->main_window_desc) {
288 wfree(wapp);
289 return NULL;
292 extractIcon(wapp->main_window_desc);
294 leader = wWindowFor(main_window);
295 if (leader) {
296 leader->main_window = main_window;
298 wapp->menu = wAppMenuGet(scr, main_window);
299 #ifdef USER_MENU
300 if (!wapp->menu) wapp->menu = wUserMenuGet(scr, wapp->main_window_desc);
301 #endif /* USER_MENU */
305 * Set application wide attributes from the leader.
307 wapp->flags.hidden = WFLAGP(wapp->main_window_desc, start_hidden);
309 wapp->flags.emulated = WFLAGP(wapp->main_window_desc, emulate_appicon);
311 /* application descriptor */
312 XSaveContext(dpy, main_window, wAppWinContext, (XPointer)wapp);
314 if (!WFLAGP(wapp->main_window_desc, no_appicon)) {
315 wapp->app_icon = NULL;
316 if (scr->last_dock)
317 wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
318 /* check main dock if we did not find it in last dock */
319 if (!wapp->app_icon && scr->dock) {
320 wapp->app_icon = findDockIconFor(scr->dock, main_window);
322 /* finally check clips */
323 if (!wapp->app_icon) {
324 int i;
325 for (i=0; i<scr->workspace_count; i++) {
326 WDock *dock = scr->workspaces[i]->clip;
327 if (dock)
328 wapp->app_icon = findDockIconFor(dock, main_window);
329 if (wapp->app_icon)
330 break;
334 if (wapp->app_icon) {
335 WWindow *mainw = wapp->main_window_desc;
337 wapp->app_icon->running = 1;
338 wapp->app_icon->icon->force_paint = 1;
339 wapp->app_icon->icon->owner = mainw;
340 if (mainw->wm_hints && (mainw->wm_hints->flags&IconWindowHint))
341 wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
342 wAppIconPaint(wapp->app_icon);
343 } else {
344 wapp->app_icon = wAppIconCreate(wapp->main_window_desc);
346 } else {
347 wapp->app_icon = NULL;
350 if (wapp->app_icon) {
351 wapp->app_icon->main_window = main_window;
354 if (wapp->app_icon && !wapp->app_icon->docked) {
355 WIcon *icon = wapp->app_icon->icon;
356 WDock *clip = scr->workspaces[scr->current_workspace]->clip;
357 int x=0, y=0;
359 if (clip && clip->attract_icons && wDockFindFreeSlot(clip, &x, &y)) {
360 wapp->app_icon->attracted = 1;
361 if (!wapp->app_icon->icon->shadowed) {
362 wapp->app_icon->icon->shadowed = 1;
363 wapp->app_icon->icon->force_paint = 1;
364 /* We don't do an wAppIconPaint() here because it's in
365 * wDockAttachIcon(). -Dan.
368 wDockAttachIcon(clip, wapp->app_icon, x, y);
369 } else {
370 PlaceIcon(scr, &x, &y);
371 wAppIconMove(wapp->app_icon, x, y);
372 wLowerFrame(icon->core);
374 if (!clip || !wapp->app_icon->attracted || !clip->collapsed)
375 XMapWindow(dpy, icon->core->window);
378 if (wPreferences.auto_arrange_icons && wapp->app_icon && !wapp->app_icon->attracted) {
379 wArrangeIcons(scr, True);
382 if (wapp->app_icon) {
383 char *tmp, *path;
384 struct stat dummy;
385 RImage *image;
387 tmp = wDefaultGetIconFile(scr, wapp->app_icon->wm_instance,
388 wapp->app_icon->wm_class, True);
390 /* If the icon was saved by us from the client supplied icon, but is
391 * missing, recreate it. */
392 if (tmp && strstr(tmp, ".AppInfo/WindowMaker")!=NULL &&
393 stat(tmp, &dummy)!=0 && errno==ENOENT) {
394 wmessage(_("recreating missing icon '%s'"), tmp);
395 path = wIconStore(wapp->app_icon->icon);
396 if (path) {
397 wfree(path);
399 image = wDefaultGetImage(scr, wapp->app_icon->wm_instance,
400 wapp->app_icon->wm_class);
401 if (image) {
402 wIconChangeImage(wapp->app_icon->icon, image);
403 wAppIconPaint(wapp->app_icon);
404 /* TODO:
405 * wIconChangeImage() should be rewriten to use retain/release
406 * The way it is now is too confusing about where the icon is
407 * finally released. -Dan */
408 /* --this is wrong at the moment-- RReleaseImage(image);*/
412 /* if the displayed icon was supplied by the client, save the icon */
413 if (!tmp)
414 extractClientIcon(wapp->app_icon);
416 /* set the application instance index */
418 WApplication *list = scr->wapp_list;
419 WApplication *prev = NULL;
420 int index = 0;
421 WWindow *wwin = wapp->main_window_desc;
423 if (!WFLAGP(wwin, collapse_appicons))
424 return 0;
426 #define Xstreql(a, b) ((a) == (b) || (a && b && strcmp(a, b)==0))
429 /* look for a free index # */
430 while (list) {
431 if (Xstreql(wwin->wm_instance,
432 list->main_window_desc->wm_instance)
434 Xstreql(wwin->wm_class,
435 list->main_window_desc->wm_class)) {
437 if (list->index == index) {
438 index++;
440 /* restart list traversal */
441 list = scr->wapp_list;
442 prev = NULL;
443 continue;
447 prev = list;
448 list = list->next;
451 wapp->index = index;
453 wapp->next = NULL;
454 wapp->prev = NULL;
456 /* append to app list */
457 if (!prev) {
458 scr->wapp_list = wapp;
459 } else {
460 prev->next = wapp;
461 wapp->prev = prev;
466 wSoundPlay(WSOUND_APPSTART);
468 #ifdef DEBUG
469 printf("Created application for %x\n", (unsigned)main_window);
470 #endif
471 return wapp;
475 void
476 wApplicationDestroy(WApplication *wapp)
478 Window main_window;
479 WWindow *wwin;
480 WScreen *scr;
482 if (!wapp)
483 return;
485 wapp->refcount--;
486 if (wapp->refcount>0)
487 return;
490 scr = wapp->main_window_desc->screen_ptr;
491 main_window = wapp->main_window;
493 if (wapp == scr->wapp_list) {
494 if (wapp->next)
495 wapp->next->prev = NULL;
496 scr->wapp_list = wapp->next;
497 } else {
498 if (wapp->next)
499 wapp->next->prev = wapp->prev;
500 if (wapp->prev)
501 wapp->prev->next = wapp->next;
504 XDeleteContext(dpy, wapp->main_window, wAppWinContext);
505 wAppMenuDestroy(wapp->menu);
506 if (wapp->app_icon) {
507 if (wapp->app_icon->docked && !wapp->app_icon->attracted) {
508 wapp->app_icon->running = 0;
509 /* since we keep it, we don't care if it was attracted or not */
510 wapp->app_icon->attracted = 0;
511 wapp->app_icon->icon->shadowed = 0;
512 wapp->app_icon->main_window = None;
513 wapp->app_icon->pid = 0;
514 wapp->app_icon->icon->owner = NULL;
515 wapp->app_icon->icon->icon_win = None;
516 wapp->app_icon->icon->force_paint = 1;
517 wAppIconPaint(wapp->app_icon);
518 } else if (wapp->app_icon->docked) {
519 wapp->app_icon->running = 0;
520 wDockDetach(wapp->app_icon->dock, wapp->app_icon);
521 } else {
522 wAppIconDestroy(wapp->app_icon);
525 wwin = wWindowFor(wapp->main_window_desc->client_win);
527 wWindowDestroy(wapp->main_window_desc);
528 if (wwin) {
529 /* undelete client window context that was deleted in
530 * wWindowDestroy */
531 XSaveContext(dpy, wwin->client_win, wWinContext,
532 (XPointer)&wwin->client_descriptor);
534 wfree(wapp);
536 #ifdef DEBUG
537 printf("Destroyed application for %x\n", (unsigned)main_window);
538 #endif
539 if (wPreferences.auto_arrange_icons) {
540 wArrangeIcons(scr, True);
543 wSoundPlay(WSOUND_APPEXIT);
548 void
549 wApplicationSetCollapse(WApplication *app, Bool flag)
551 WApplication *list = app->main_window_desc->screen_ptr->wapp_list;
552 WWindow *wwin = app->main_window_desc;
554 if (WFLAGP(app->main_window_desc, collapse_appicons) == flag)
555 return;
558 while (list) {
559 if (strcmp(wwin->wm_instance,
560 list->main_window_desc->wm_instance) == 0
562 strcmp(wwin->wm_class,
563 list->main_window_desc->wm_class) == 0)
564 WSETUFLAG(list->main_window_desc, collapse_appicons, flag);
566 list = list->next;
569 if (app->app_icon && flag)
570 wAppIconMove(app->app_icon, app->app_icon->x_pos, app->app_icon->y_pos);
577 * Returns index number of the app in case there are more than
578 * one instance of the same class/name.
581 wApplicationIndexOfGroup(WApplication *app)
583 return app->index;