Update local copy of GPLv2 and FSF address in copyrights
[wmaker-crm.git] / src / application.c
blob778d7d5eac9bc5240080f5fe6efb87571b05602a
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
5 * Copyright (c) 1998-2003 Dan Pascu
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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "wconfig.h"
23 #include <X11/Xlib.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <errno.h>
33 #include "WindowMaker.h"
34 #include "menu.h"
35 #include "window.h"
36 #ifdef USER_MENU
37 #include "usermenu.h"
38 #endif /* USER_MENU */
39 #include "icon.h"
40 #include "appicon.h"
41 #include "application.h"
42 #include "appmenu.h"
43 #include "properties.h"
44 #include "funcs.h"
45 #include "stacking.h"
46 #include "actions.h"
47 #include "defaults.h"
48 #include "workspace.h"
49 #include "dock.h"
51 #include "xinerama.h"
53 /******** Global variables ********/
55 extern XContext wAppWinContext;
56 extern XContext wWinContext;
57 extern WPreferences wPreferences;
59 extern WDDomain *WDWindowAttributes;
61 /******** Local variables ********/
63 static WWindow *makeMainWindow(WScreen * scr, Window window)
65 WWindow *wwin;
66 XWindowAttributes attr;
68 if (!XGetWindowAttributes(dpy, window, &attr)) {
69 return NULL;
72 wwin = wWindowCreate();
73 wwin->screen_ptr = scr;
74 wwin->client_win = window;
75 wwin->main_window = window;
76 wwin->wm_hints = XGetWMHints(dpy, window);
77 /* if (!MyXFetchName(dpy, window, &(wwin->frame->title))) {
78 wwin->frame->title = NULL;
81 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
83 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
84 &wwin->user_flags, &wwin->defined_user_flags, True);
86 XSelectInput(dpy, window, attr.your_event_mask | PropertyChangeMask | StructureNotifyMask);
87 return wwin;
90 WApplication *wApplicationOf(Window window)
92 WApplication *wapp;
94 if (window == None)
95 return NULL;
96 if (XFindContext(dpy, window, wAppWinContext, (XPointer *) & wapp) != XCSUCCESS)
97 return NULL;
98 return wapp;
101 static WAppIcon *findDockIconFor(WDock * dock, Window main_window)
103 WAppIcon *aicon = NULL;
105 aicon = wDockFindIconForWindow(dock, main_window);
106 if (!aicon) {
107 wDockTrackWindowLaunch(dock, main_window);
108 aicon = wDockFindIconForWindow(dock, main_window);
110 return aicon;
113 static void extractIcon(WWindow * wwin)
115 char *progname;
117 progname = GetProgramNameForWindow(wwin->client_win);
118 if (progname) {
119 wApplicationExtractDirPackIcon(wwin->screen_ptr, progname, wwin->wm_instance, wwin->wm_class);
120 wfree(progname);
124 static void saveIconNameFor(char *iconPath, char *wm_instance, char *wm_class)
126 WMPropList *dict = WDWindowAttributes->dictionary;
127 WMPropList *adict, *key, *iconk;
128 WMPropList *val;
129 char *tmp;
130 int i;
132 i = 0;
133 if (wm_instance)
134 i += strlen(wm_instance);
135 if (wm_class)
136 i += strlen(wm_class);
138 tmp = wmalloc(i + 8);
139 *tmp = 0;
140 if (wm_class && wm_instance) {
141 sprintf(tmp, "%s.%s", wm_instance, wm_class);
142 } else {
143 if (wm_instance)
144 strcat(tmp, wm_instance);
145 if (wm_class)
146 strcat(tmp, wm_class);
149 key = WMCreatePLString(tmp);
150 wfree(tmp);
151 adict = WMGetFromPLDictionary(dict, key);
153 iconk = WMCreatePLString("Icon");
155 if (adict) {
156 val = WMGetFromPLDictionary(adict, iconk);
157 } else {
158 /* no dictionary for app, so create one */
159 adict = WMCreatePLDictionary(NULL, NULL);
160 WMPutInPLDictionary(dict, key, adict);
161 WMReleasePropList(adict);
162 val = NULL;
164 if (!val) {
165 val = WMCreatePLString(iconPath);
166 WMPutInPLDictionary(adict, iconk, val);
167 WMReleasePropList(val);
169 WMReleasePropList(key);
170 WMReleasePropList(iconk);
172 if (val && !wPreferences.flags.noupdates) {
173 UpdateDomainFile(WDWindowAttributes);
177 void wApplicationExtractDirPackIcon(WScreen * scr, char *path, char *wm_instance, char *wm_class)
179 char *iconPath = NULL;
180 /* Maybe the app is a .app and it has an icon in it, like
181 * /usr/local/GNUstep/Applications/WPrefs.app/WPrefs.tiff
183 if (strstr(path, ".app")) {
184 char *tmp;
186 tmp = wmalloc(strlen(path) + 16);
188 if (scr->flags.supports_tiff) {
189 strcpy(tmp, path);
190 strcat(tmp, ".tiff");
191 if (access(tmp, R_OK) == 0)
192 iconPath = tmp;
194 if (!iconPath) {
195 strcpy(tmp, path);
196 strcat(tmp, ".xpm");
197 if (access(tmp, R_OK) == 0)
198 iconPath = tmp;
200 if (!iconPath)
201 wfree(tmp);
204 if (iconPath) {
205 saveIconNameFor(iconPath, wm_instance, wm_class);
207 wfree(iconPath);
211 static Bool extractClientIcon(WAppIcon * icon)
213 char *path;
215 path = wIconStore(icon->icon);
216 if (!path)
217 return False;
219 saveIconNameFor(path, icon->wm_instance, icon->wm_class);
221 wfree(path);
223 return True;
226 WApplication *wApplicationCreate(WWindow * wwin)
228 WScreen *scr = wwin->screen_ptr;
229 Window main_window = wwin->main_window;
230 WApplication *wapp;
231 WWindow *leader;
233 if (main_window == None || main_window == scr->root_win)
234 return NULL;
237 Window root;
238 int foo;
239 unsigned int bar;
240 /* check if the window is valid */
241 if (!XGetGeometry(dpy, main_window, &root, &foo, &foo, &bar, &bar, &bar, &bar)) {
242 return NULL;
246 wapp = wApplicationOf(main_window);
247 if (wapp) {
248 wapp->refcount++;
249 if (wapp->app_icon && wapp->app_icon->docked &&
250 wapp->app_icon->relaunching && wapp->main_window_desc->fake_group) {
251 wDockFinishLaunch(wapp->app_icon->dock, wapp->app_icon);
254 return wapp;
257 wapp = wmalloc(sizeof(WApplication));
258 memset(wapp, 0, sizeof(WApplication));
260 wapp->refcount = 1;
261 wapp->last_focused = NULL;
262 wapp->urgent_bounce_timer = NULL;
264 wapp->last_workspace = 0;
266 wapp->main_window = main_window;
267 wapp->main_window_desc = makeMainWindow(scr, main_window);
268 if (!wapp->main_window_desc) {
269 wfree(wapp);
270 return NULL;
273 wapp->main_window_desc->fake_group = wwin->fake_group;
274 wapp->main_window_desc->net_icon_image = RRetainImage(wwin->net_icon_image);
276 extractIcon(wapp->main_window_desc);
278 leader = wWindowFor(main_window);
279 if (leader) {
280 leader->main_window = main_window;
282 wapp->menu = wAppMenuGet(scr, main_window);
283 #ifdef USER_MENU
284 if (!wapp->menu)
285 wapp->menu = wUserMenuGet(scr, wapp->main_window_desc);
286 #endif /* USER_MENU */
289 * Set application wide attributes from the leader.
291 wapp->flags.hidden = WFLAGP(wapp->main_window_desc, start_hidden);
293 wapp->flags.emulated = WFLAGP(wapp->main_window_desc, emulate_appicon);
295 /* application descriptor */
296 XSaveContext(dpy, main_window, wAppWinContext, (XPointer) wapp);
298 if (!WFLAGP(wapp->main_window_desc, no_appicon)) {
299 wapp->app_icon = NULL;
300 if (scr->last_dock)
301 wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
302 /* check main dock if we did not find it in last dock */
303 if (!wapp->app_icon && scr->dock) {
304 wapp->app_icon = findDockIconFor(scr->dock, main_window);
306 /* finally check clips */
307 if (!wapp->app_icon) {
308 int i;
309 for (i = 0; i < scr->workspace_count; i++) {
310 WDock *dock = scr->workspaces[i]->clip;
311 if (dock)
312 wapp->app_icon = findDockIconFor(dock, main_window);
313 if (wapp->app_icon)
314 break;
318 if (wapp->app_icon) {
319 WWindow *mainw = wapp->main_window_desc;
321 wapp->app_icon->running = 1;
322 wapp->app_icon->icon->force_paint = 1;
323 wapp->app_icon->icon->owner = mainw;
324 if (mainw->wm_hints && (mainw->wm_hints->flags & IconWindowHint))
325 wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
326 wAppIconPaint(wapp->app_icon);
327 } else {
328 wapp->app_icon = wAppIconCreate(wapp->main_window_desc);
330 } else {
331 wapp->app_icon = NULL;
334 if (wapp->app_icon) {
335 wapp->app_icon->main_window = main_window;
338 if (wapp->app_icon && !wapp->app_icon->docked) {
339 WIcon *icon = wapp->app_icon->icon;
340 WDock *clip = scr->workspaces[scr->current_workspace]->clip;
341 int x = 0, y = 0;
343 if (clip && clip->attract_icons && wDockFindFreeSlot(clip, &x, &y)) {
344 wapp->app_icon->attracted = 1;
345 if (!icon->shadowed) {
346 icon->shadowed = 1;
347 icon->force_paint = 1;
348 /* wAppIconPaint() is done in wDockAttachIcon() below */
350 wDockAttachIcon(clip, wapp->app_icon, x, y);
351 } else {
352 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wapp->main_window_desc));
353 wAppIconMove(wapp->app_icon, x, y);
354 wLowerFrame(icon->core);
356 if (!clip || !wapp->app_icon->attracted || !clip->collapsed)
357 XMapWindow(dpy, icon->core->window);
360 if (wPreferences.auto_arrange_icons && wapp->app_icon && !wapp->app_icon->attracted) {
361 wArrangeIcons(scr, True);
364 if (wapp->app_icon) {
365 char *tmp, *path;
366 struct stat dummy;
367 RImage *image;
369 tmp = wDefaultGetIconFile(scr, wapp->app_icon->wm_instance, wapp->app_icon->wm_class, True);
371 /* If the icon was saved by us from the client supplied icon, but is
372 * missing, recreate it. */
373 if (tmp && strstr(tmp, "Library/WindowMaker/CachedPixmaps") != NULL &&
374 stat(tmp, &dummy) != 0 && errno == ENOENT) {
375 wmessage(_("recreating missing icon '%s'"), tmp);
376 path = wIconStore(wapp->app_icon->icon);
377 if (path) {
378 wfree(path);
380 image = wDefaultGetImage(scr, wapp->app_icon->wm_instance, wapp->app_icon->wm_class);
381 if (image) {
382 wIconChangeImage(wapp->app_icon->icon, image);
383 wAppIconPaint(wapp->app_icon);
384 /* TODO:
385 * wIconChangeImage() should be rewriten to use retain/release
386 * The way it is now is too confusing about where the icon is
387 * finally released. -Dan */
388 /* --this is wrong at the moment-- RReleaseImage(image); */
392 /* if the displayed icon was supplied by the client, save the icon */
393 if (!tmp)
394 extractClientIcon(wapp->app_icon);
396 return wapp;
399 void wApplicationDestroy(WApplication * wapp)
401 Window main_window;
402 WWindow *wwin;
403 WScreen *scr;
405 if (!wapp)
406 return;
408 wapp->refcount--;
409 if (wapp->refcount > 0)
410 return;
412 if (wapp->urgent_bounce_timer) {
413 WMDeleteTimerHandler(wapp->urgent_bounce_timer);
414 wapp->urgent_bounce_timer = NULL;
416 if (wapp->flags.bouncing) {
417 /* event.c:handleDestroyNotify forced this destroy
418 and thereby overlooked the bounce callback */
419 wapp->refcount = 1;
420 return;
423 scr = wapp->main_window_desc->screen_ptr;
424 main_window = wapp->main_window;
426 if (wapp == scr->wapp_list) {
427 if (wapp->next)
428 wapp->next->prev = NULL;
429 scr->wapp_list = wapp->next;
430 } else {
431 if (wapp->next)
432 wapp->next->prev = wapp->prev;
433 if (wapp->prev)
434 wapp->prev->next = wapp->next;
437 XDeleteContext(dpy, wapp->main_window, wAppWinContext);
438 wAppMenuDestroy(wapp->menu);
439 wApplicationDeactivate(wapp);
441 if (wapp->app_icon) {
442 if (wapp->app_icon->docked && !wapp->app_icon->attracted) {
443 wapp->app_icon->running = 0;
444 /* since we keep it, we don't care if it was attracted or not */
445 wapp->app_icon->attracted = 0;
446 wapp->app_icon->icon->shadowed = 0;
447 wapp->app_icon->main_window = None;
448 wapp->app_icon->pid = 0;
449 wapp->app_icon->icon->owner = NULL;
450 wapp->app_icon->icon->icon_win = None;
451 wapp->app_icon->icon->force_paint = 1;
452 wAppIconPaint(wapp->app_icon);
453 } else if (wapp->app_icon->docked) {
454 wapp->app_icon->running = 0;
455 wDockDetach(wapp->app_icon->dock, wapp->app_icon);
456 } else {
457 wAppIconDestroy(wapp->app_icon);
460 wwin = wWindowFor(wapp->main_window_desc->client_win);
462 wWindowDestroy(wapp->main_window_desc);
463 if (wwin) {
464 /* undelete client window context that was deleted in
465 * wWindowDestroy */
466 XSaveContext(dpy, wwin->client_win, wWinContext, (XPointer) & wwin->client_descriptor);
468 wfree(wapp);
470 if (wPreferences.auto_arrange_icons)
471 wArrangeIcons(scr, True);