- Fixed sloppy focus bug
[wmaker-crm.git] / src / application.c
blob62117ce35a00964f787e7ad43b0cf099d6720351
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
5 * Copyright (c) 1998-2003 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 UpdateDomainFile(WDWindowAttributes);
194 void
195 wApplicationExtractDirPackIcon(WScreen *scr, char *path,
196 char *wm_instance, char *wm_class)
198 char *iconPath=NULL;
199 /* Maybe the app is a .app and it has an icon in it, like
200 * /usr/local/GNUstep/Apps/WPrefs.app/WPrefs.tiff
202 if (strstr(path, ".app")) {
203 char *tmp;
205 tmp = wmalloc(strlen(path)+16);
207 if (scr->flags.supports_tiff) {
208 strcpy(tmp, path);
209 strcat(tmp, ".tiff");
210 if (access(tmp, R_OK)==0)
211 iconPath = tmp;
213 if (!path) {
214 strcpy(tmp, path);
215 strcat(tmp, ".xpm");
216 if (access(tmp, R_OK)==0)
217 iconPath = tmp;
219 if (!iconPath)
220 wfree(tmp);
223 if (iconPath) {
224 saveIconNameFor(iconPath, wm_instance, wm_class);
226 wfree(iconPath);
231 static Bool
232 extractClientIcon(WAppIcon *icon)
234 char *path;
236 path = wIconStore(icon->icon);
237 if (!path)
238 return False;
240 saveIconNameFor(path, icon->wm_instance, icon->wm_class);
242 wfree(path);
244 return True;
248 WApplication*
249 wApplicationCreate(WScreen *scr, Window main_window)
251 WApplication *wapp;
252 WWindow *leader;
254 if (main_window==None || main_window==scr->root_win) {
255 #ifdef DEBUG0
256 wwarning("trying to create application for %x",(unsigned)main_window);
257 #endif
258 return NULL;
262 Window root;
263 int foo;
264 unsigned int bar;
265 /* check if the window is valid */
266 if (!XGetGeometry(dpy, main_window, &root, &foo, &foo, &bar, &bar,
267 &bar, &bar)) {
268 return NULL;
272 wapp = wApplicationOf(main_window);
273 if (wapp) {
274 wapp->refcount++;
275 if (wapp->app_icon && wapp->app_icon->docked &&
276 wapp->app_icon->relaunching && wapp->main_window_desc->fake_group) {
277 wDockFinishLaunch(wapp->app_icon->dock, wapp->app_icon);
280 return wapp;
283 wapp = wmalloc(sizeof(WApplication));
284 memset(wapp, 0, sizeof(WApplication));
286 wapp->refcount = 1;
287 wapp->last_focused = NULL;
289 wapp->last_workspace = 0;
291 wapp->main_window = main_window;
292 wapp->main_window_desc = makeMainWindow(scr, main_window);
293 if (!wapp->main_window_desc) {
294 wfree(wapp);
295 return NULL;
298 extractIcon(wapp->main_window_desc);
300 leader = wWindowFor(main_window);
301 if (leader) {
302 leader->main_window = main_window;
304 wapp->menu = wAppMenuGet(scr, main_window);
305 #ifdef USER_MENU
306 if (!wapp->menu) wapp->menu = wUserMenuGet(scr, wapp->main_window_desc);
307 #endif /* USER_MENU */
311 * Set application wide attributes from the leader.
313 wapp->flags.hidden = WFLAGP(wapp->main_window_desc, start_hidden);
315 wapp->flags.emulated = WFLAGP(wapp->main_window_desc, emulate_appicon);
317 /* application descriptor */
318 XSaveContext(dpy, main_window, wAppWinContext, (XPointer)wapp);
320 if (!WFLAGP(wapp->main_window_desc, no_appicon)) {
321 wapp->app_icon = NULL;
322 if (scr->last_dock)
323 wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
324 /* check main dock if we did not find it in last dock */
325 if (!wapp->app_icon && scr->dock) {
326 wapp->app_icon = findDockIconFor(scr->dock, main_window);
328 /* finally check clips */
329 if (!wapp->app_icon) {
330 int i;
331 for (i=0; i<scr->workspace_count; i++) {
332 WDock *dock = scr->workspaces[i]->clip;
333 if (dock)
334 wapp->app_icon = findDockIconFor(dock, main_window);
335 if (wapp->app_icon)
336 break;
340 if (wapp->app_icon) {
341 WWindow *mainw = wapp->main_window_desc;
343 wapp->app_icon->running = 1;
344 wapp->app_icon->icon->force_paint = 1;
345 wapp->app_icon->icon->owner = mainw;
346 if (mainw->wm_hints && (mainw->wm_hints->flags&IconWindowHint))
347 wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
348 wAppIconPaint(wapp->app_icon);
349 } else {
350 wapp->app_icon = wAppIconCreate(wapp->main_window_desc);
352 } else {
353 wapp->app_icon = NULL;
356 if (wapp->app_icon) {
357 wapp->app_icon->main_window = main_window;
360 if (wapp->app_icon && !wapp->app_icon->docked) {
361 WIcon *icon = wapp->app_icon->icon;
362 WDock *clip = scr->workspaces[scr->current_workspace]->clip;
363 int x=0, y=0;
365 if (clip && clip->attract_icons && wDockFindFreeSlot(clip, &x, &y)) {
366 wapp->app_icon->attracted = 1;
367 if (!icon->shadowed) {
368 icon->shadowed = 1;
369 icon->force_paint = 1;
370 /* wAppIconPaint() is done in wDockAttachIcon() below */
372 wDockAttachIcon(clip, wapp->app_icon, x, y);
373 } else {
374 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wapp->main_window_desc));
375 wAppIconMove(wapp->app_icon, x, y);
376 wLowerFrame(icon->core);
378 if (!clip || !wapp->app_icon->attracted || !clip->collapsed)
379 XMapWindow(dpy, icon->core->window);
382 if (wPreferences.auto_arrange_icons && wapp->app_icon && !wapp->app_icon->attracted) {
383 wArrangeIcons(scr, True);
386 if (wapp->app_icon) {
387 char *tmp, *path;
388 struct stat dummy;
389 RImage *image;
391 tmp = wDefaultGetIconFile(scr, wapp->app_icon->wm_instance,
392 wapp->app_icon->wm_class, True);
394 /* If the icon was saved by us from the client supplied icon, but is
395 * missing, recreate it. */
396 if (tmp && strstr(tmp, ".AppInfo/WindowMaker")!=NULL &&
397 stat(tmp, &dummy)!=0 && errno==ENOENT) {
398 wmessage(_("recreating missing icon '%s'"), tmp);
399 path = wIconStore(wapp->app_icon->icon);
400 if (path) {
401 wfree(path);
403 image = wDefaultGetImage(scr, wapp->app_icon->wm_instance,
404 wapp->app_icon->wm_class);
405 if (image) {
406 wIconChangeImage(wapp->app_icon->icon, image);
407 wAppIconPaint(wapp->app_icon);
408 /* TODO:
409 * wIconChangeImage() should be rewriten to use retain/release
410 * The way it is now is too confusing about where the icon is
411 * finally released. -Dan */
412 /* --this is wrong at the moment-- RReleaseImage(image);*/
416 /* if the displayed icon was supplied by the client, save the icon */
417 if (!tmp)
418 extractClientIcon(wapp->app_icon);
421 wSoundPlay(WSOUND_APPSTART);
423 #ifdef DEBUG
424 printf("Created application for %x\n", (unsigned)main_window);
425 #endif
426 return wapp;
430 void
431 wApplicationDestroy(WApplication *wapp)
433 Window main_window;
434 WWindow *wwin;
435 WScreen *scr;
437 if (!wapp)
438 return;
440 wapp->refcount--;
441 if (wapp->refcount>0)
442 return;
445 scr = wapp->main_window_desc->screen_ptr;
446 main_window = wapp->main_window;
448 if (wapp == scr->wapp_list) {
449 if (wapp->next)
450 wapp->next->prev = NULL;
451 scr->wapp_list = wapp->next;
452 } else {
453 if (wapp->next)
454 wapp->next->prev = wapp->prev;
455 if (wapp->prev)
456 wapp->prev->next = wapp->next;
459 XDeleteContext(dpy, wapp->main_window, wAppWinContext);
460 wAppMenuDestroy(wapp->menu);
461 if (wapp->app_icon) {
462 if (wapp->app_icon->docked && !wapp->app_icon->attracted) {
463 wapp->app_icon->running = 0;
464 /* since we keep it, we don't care if it was attracted or not */
465 wapp->app_icon->attracted = 0;
466 wapp->app_icon->icon->shadowed = 0;
467 wapp->app_icon->main_window = None;
468 wapp->app_icon->pid = 0;
469 wapp->app_icon->icon->owner = NULL;
470 wapp->app_icon->icon->icon_win = None;
471 wapp->app_icon->icon->force_paint = 1;
472 wAppIconPaint(wapp->app_icon);
473 } else if (wapp->app_icon->docked) {
474 wapp->app_icon->running = 0;
475 wDockDetach(wapp->app_icon->dock, wapp->app_icon);
476 } else {
477 wAppIconDestroy(wapp->app_icon);
480 wwin = wWindowFor(wapp->main_window_desc->client_win);
482 wWindowDestroy(wapp->main_window_desc);
483 if (wwin) {
484 /* undelete client window context that was deleted in
485 * wWindowDestroy */
486 XSaveContext(dpy, wwin->client_win, wWinContext,
487 (XPointer)&wwin->client_descriptor);
489 wfree(wapp);
491 #ifdef DEBUG
492 printf("Destroyed application for %x\n", (unsigned)main_window);
493 #endif
494 if (wPreferences.auto_arrange_icons) {
495 wArrangeIcons(scr, True);
498 wSoundPlay(WSOUND_APPEXIT);