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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
29 #include <sys/types.h>
34 #include "WindowMaker.h"
39 #endif /* USER_MENU */
42 #include "application.h"
44 #include "properties.h"
49 #include "workspace.h"
54 /******** Global variables ********/
56 extern XContext wAppWinContext
;
57 extern XContext wWinContext
;
58 extern WPreferences wPreferences
;
60 extern WDDomain
*WDWindowAttributes
;
62 /******** Local variables ********/
64 static WWindow
*makeMainWindow(WScreen
* scr
, Window window
)
67 XWindowAttributes attr
;
69 if (!XGetWindowAttributes(dpy
, window
, &attr
)) {
73 wwin
= wWindowCreate();
74 wwin
->screen_ptr
= scr
;
75 wwin
->client_win
= window
;
76 wwin
->main_window
= window
;
77 wwin
->wm_hints
= XGetWMHints(dpy
, window
);
78 /* if (!MyXFetchName(dpy, window, &(wwin->frame->title))) {
79 wwin->frame->title = NULL;
82 PropGetWMClass(window
, &wwin
->wm_class
, &wwin
->wm_instance
);
84 wDefaultFillAttributes(scr
, wwin
->wm_instance
, wwin
->wm_class
,
85 &wwin
->user_flags
, &wwin
->defined_user_flags
, True
);
87 XSelectInput(dpy
, window
, attr
.your_event_mask
| PropertyChangeMask
| StructureNotifyMask
);
91 WApplication
*wApplicationOf(Window window
)
97 if (XFindContext(dpy
, window
, wAppWinContext
, (XPointer
*) & wapp
) != XCSUCCESS
)
102 static WAppIcon
*findDockIconFor(WDock
* dock
, Window main_window
)
104 WAppIcon
*aicon
= NULL
;
106 aicon
= wDockFindIconForWindow(dock
, main_window
);
108 wDockTrackWindowLaunch(dock
, main_window
);
109 aicon
= wDockFindIconForWindow(dock
, main_window
);
114 static void extractIcon(WWindow
* wwin
)
118 progname
= GetProgramNameForWindow(wwin
->client_win
);
120 wApplicationExtractDirPackIcon(wwin
->screen_ptr
, progname
, wwin
->wm_instance
, wwin
->wm_class
);
125 static void saveIconNameFor(char *iconPath
, char *wm_instance
, char *wm_class
)
127 WMPropList
*dict
= WDWindowAttributes
->dictionary
;
128 WMPropList
*adict
, *key
, *iconk
;
135 i
+= strlen(wm_instance
);
137 i
+= strlen(wm_class
);
139 tmp
= wmalloc(i
+ 8);
141 if (wm_class
&& wm_instance
) {
142 sprintf(tmp
, "%s.%s", wm_instance
, wm_class
);
145 strcat(tmp
, wm_instance
);
147 strcat(tmp
, wm_class
);
150 key
= WMCreatePLString(tmp
);
152 adict
= WMGetFromPLDictionary(dict
, key
);
154 iconk
= WMCreatePLString("Icon");
157 val
= WMGetFromPLDictionary(adict
, iconk
);
159 /* no dictionary for app, so create one */
160 adict
= WMCreatePLDictionary(NULL
, NULL
);
161 WMPutInPLDictionary(dict
, key
, adict
);
162 WMReleasePropList(adict
);
166 val
= WMCreatePLString(iconPath
);
167 WMPutInPLDictionary(adict
, iconk
, val
);
168 WMReleasePropList(val
);
170 WMReleasePropList(key
);
171 WMReleasePropList(iconk
);
173 if (val
&& !wPreferences
.flags
.noupdates
) {
174 UpdateDomainFile(WDWindowAttributes
);
178 void wApplicationExtractDirPackIcon(WScreen
* scr
, char *path
, char *wm_instance
, char *wm_class
)
180 char *iconPath
= NULL
;
181 /* Maybe the app is a .app and it has an icon in it, like
182 * /usr/local/GNUstep/Applications/WPrefs.app/WPrefs.tiff
184 if (strstr(path
, ".app")) {
187 tmp
= wmalloc(strlen(path
) + 16);
189 if (scr
->flags
.supports_tiff
) {
191 strcat(tmp
, ".tiff");
192 if (access(tmp
, R_OK
) == 0)
198 if (access(tmp
, R_OK
) == 0)
206 saveIconNameFor(iconPath
, wm_instance
, wm_class
);
212 static Bool
extractClientIcon(WAppIcon
* icon
)
216 path
= wIconStore(icon
->icon
);
220 saveIconNameFor(path
, icon
->wm_instance
, icon
->wm_class
);
227 WApplication
*wApplicationCreate(WWindow
* wwin
)
229 WScreen
*scr
= wwin
->screen_ptr
;
230 Window main_window
= wwin
->main_window
;
234 if (main_window
== None
|| main_window
== scr
->root_win
) {
236 wwarning("trying to create application for %x", (unsigned)main_window
);
245 /* check if the window is valid */
246 if (!XGetGeometry(dpy
, main_window
, &root
, &foo
, &foo
, &bar
, &bar
, &bar
, &bar
)) {
251 wapp
= wApplicationOf(main_window
);
254 if (wapp
->app_icon
&& wapp
->app_icon
->docked
&&
255 wapp
->app_icon
->relaunching
&& wapp
->main_window_desc
->fake_group
) {
256 wDockFinishLaunch(wapp
->app_icon
->dock
, wapp
->app_icon
);
262 wapp
= wmalloc(sizeof(WApplication
));
263 memset(wapp
, 0, sizeof(WApplication
));
266 wapp
->last_focused
= NULL
;
268 wapp
->last_workspace
= 0;
270 wapp
->main_window
= main_window
;
271 wapp
->main_window_desc
= makeMainWindow(scr
, main_window
);
272 if (!wapp
->main_window_desc
) {
277 wapp
->main_window_desc
->fake_group
= wwin
->fake_group
;
278 wapp
->main_window_desc
->net_icon_image
= RRetainImage(wwin
->net_icon_image
);
280 extractIcon(wapp
->main_window_desc
);
282 leader
= wWindowFor(main_window
);
284 leader
->main_window
= main_window
;
286 wapp
->menu
= wAppMenuGet(scr
, main_window
);
289 wapp
->menu
= wUserMenuGet(scr
, wapp
->main_window_desc
);
290 #endif /* USER_MENU */
293 * Set application wide attributes from the leader.
295 wapp
->flags
.hidden
= WFLAGP(wapp
->main_window_desc
, start_hidden
);
297 wapp
->flags
.emulated
= WFLAGP(wapp
->main_window_desc
, emulate_appicon
);
299 /* application descriptor */
300 XSaveContext(dpy
, main_window
, wAppWinContext
, (XPointer
) wapp
);
302 if (!WFLAGP(wapp
->main_window_desc
, no_appicon
)) {
303 wapp
->app_icon
= NULL
;
305 wapp
->app_icon
= findDockIconFor(scr
->last_dock
, main_window
);
306 /* check main dock if we did not find it in last dock */
307 if (!wapp
->app_icon
&& scr
->dock
) {
308 wapp
->app_icon
= findDockIconFor(scr
->dock
, main_window
);
310 /* finally check clips */
311 if (!wapp
->app_icon
) {
313 for (i
= 0; i
< scr
->workspace_count
; i
++) {
314 WDock
*dock
= scr
->workspaces
[i
]->clip
;
316 wapp
->app_icon
= findDockIconFor(dock
, main_window
);
322 if (wapp
->app_icon
) {
323 WWindow
*mainw
= wapp
->main_window_desc
;
325 wapp
->app_icon
->running
= 1;
326 wapp
->app_icon
->icon
->force_paint
= 1;
327 wapp
->app_icon
->icon
->owner
= mainw
;
328 if (mainw
->wm_hints
&& (mainw
->wm_hints
->flags
& IconWindowHint
))
329 wapp
->app_icon
->icon
->icon_win
= mainw
->wm_hints
->icon_window
;
330 wAppIconPaint(wapp
->app_icon
);
332 wapp
->app_icon
= wAppIconCreate(wapp
->main_window_desc
);
335 wapp
->app_icon
= NULL
;
338 if (wapp
->app_icon
) {
339 wapp
->app_icon
->main_window
= main_window
;
342 if (wapp
->app_icon
&& !wapp
->app_icon
->docked
) {
343 WIcon
*icon
= wapp
->app_icon
->icon
;
344 WDock
*clip
= scr
->workspaces
[scr
->current_workspace
]->clip
;
347 if (clip
&& clip
->attract_icons
&& wDockFindFreeSlot(clip
, &x
, &y
)) {
348 wapp
->app_icon
->attracted
= 1;
349 if (!icon
->shadowed
) {
351 icon
->force_paint
= 1;
352 /* wAppIconPaint() is done in wDockAttachIcon() below */
354 wDockAttachIcon(clip
, wapp
->app_icon
, x
, y
);
356 PlaceIcon(scr
, &x
, &y
, wGetHeadForWindow(wapp
->main_window_desc
));
357 wAppIconMove(wapp
->app_icon
, x
, y
);
358 wLowerFrame(icon
->core
);
360 if (!clip
|| !wapp
->app_icon
->attracted
|| !clip
->collapsed
)
361 XMapWindow(dpy
, icon
->core
->window
);
364 if (wPreferences
.auto_arrange_icons
&& wapp
->app_icon
&& !wapp
->app_icon
->attracted
) {
365 wArrangeIcons(scr
, True
);
368 if (wapp
->app_icon
) {
373 tmp
= wDefaultGetIconFile(scr
, wapp
->app_icon
->wm_instance
, wapp
->app_icon
->wm_class
, True
);
375 /* If the icon was saved by us from the client supplied icon, but is
376 * missing, recreate it. */
377 if (tmp
&& strstr(tmp
, "Library/WindowMaker/CachedPixmaps") != NULL
&&
378 stat(tmp
, &dummy
) != 0 && errno
== ENOENT
) {
379 wmessage(_("recreating missing icon '%s'"), tmp
);
380 path
= wIconStore(wapp
->app_icon
->icon
);
384 image
= wDefaultGetImage(scr
, wapp
->app_icon
->wm_instance
, wapp
->app_icon
->wm_class
);
386 wIconChangeImage(wapp
->app_icon
->icon
, image
);
387 wAppIconPaint(wapp
->app_icon
);
389 * wIconChangeImage() should be rewriten to use retain/release
390 * The way it is now is too confusing about where the icon is
391 * finally released. -Dan */
392 /* --this is wrong at the moment-- RReleaseImage(image); */
396 /* if the displayed icon was supplied by the client, save the icon */
398 extractClientIcon(wapp
->app_icon
);
403 printf("Created application for %x\n", (unsigned)main_window
);
408 void wApplicationDestroy(WApplication
* wapp
)
418 if (wapp
->refcount
> 0)
421 scr
= wapp
->main_window_desc
->screen_ptr
;
422 main_window
= wapp
->main_window
;
424 if (wapp
== scr
->wapp_list
) {
426 wapp
->next
->prev
= NULL
;
427 scr
->wapp_list
= wapp
->next
;
430 wapp
->next
->prev
= wapp
->prev
;
432 wapp
->prev
->next
= wapp
->next
;
435 XDeleteContext(dpy
, wapp
->main_window
, wAppWinContext
);
436 wAppMenuDestroy(wapp
->menu
);
437 if (wapp
->app_icon
) {
438 if (wapp
->app_icon
->docked
&& !wapp
->app_icon
->attracted
) {
439 wapp
->app_icon
->running
= 0;
440 /* since we keep it, we don't care if it was attracted or not */
441 wapp
->app_icon
->attracted
= 0;
442 wapp
->app_icon
->icon
->shadowed
= 0;
443 wapp
->app_icon
->main_window
= None
;
444 wapp
->app_icon
->pid
= 0;
445 wapp
->app_icon
->icon
->owner
= NULL
;
446 wapp
->app_icon
->icon
->icon_win
= None
;
447 wapp
->app_icon
->icon
->force_paint
= 1;
448 wAppIconPaint(wapp
->app_icon
);
449 } else if (wapp
->app_icon
->docked
) {
450 wapp
->app_icon
->running
= 0;
451 wDockDetach(wapp
->app_icon
->dock
, wapp
->app_icon
);
453 wAppIconDestroy(wapp
->app_icon
);
456 wwin
= wWindowFor(wapp
->main_window_desc
->client_win
);
458 wWindowDestroy(wapp
->main_window_desc
);
460 /* undelete client window context that was deleted in
462 XSaveContext(dpy
, wwin
->client_win
, wWinContext
, (XPointer
) & wwin
->client_descriptor
);
467 printf("Destroyed application for %x\n", (unsigned)main_window
);
469 if (wPreferences
.auto_arrange_icons
) {
470 wArrangeIcons(scr
, True
);