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.
28 #include <sys/types.h>
33 #include "WindowMaker.h"
38 #endif /* USER_MENU */
41 #include "application.h"
43 #include "properties.h"
48 #include "workspace.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
)
66 XWindowAttributes attr
;
68 if (!XGetWindowAttributes(dpy
, window
, &attr
)) {
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
);
90 WApplication
*wApplicationOf(Window window
)
96 if (XFindContext(dpy
, window
, wAppWinContext
, (XPointer
*) & wapp
) != XCSUCCESS
)
101 static WAppIcon
*findDockIconFor(WDock
* dock
, Window main_window
)
103 WAppIcon
*aicon
= NULL
;
105 aicon
= wDockFindIconForWindow(dock
, main_window
);
107 wDockTrackWindowLaunch(dock
, main_window
);
108 aicon
= wDockFindIconForWindow(dock
, main_window
);
113 static void extractIcon(WWindow
* wwin
)
117 progname
= GetProgramNameForWindow(wwin
->client_win
);
119 wApplicationExtractDirPackIcon(wwin
->screen_ptr
, progname
, wwin
->wm_instance
, wwin
->wm_class
);
124 void wApplicationSaveIconPathFor(char *iconPath
, char *wm_instance
, char *wm_class
)
126 WMPropList
*dict
= WDWindowAttributes
->dictionary
;
127 WMPropList
*adict
, *key
, *iconk
;
134 i
+= strlen(wm_instance
);
136 i
+= strlen(wm_class
);
138 tmp
= wmalloc(i
+ 8);
140 if (wm_class
&& wm_instance
) {
141 sprintf(tmp
, "%s.%s", wm_instance
, wm_class
);
144 strcat(tmp
, wm_instance
);
146 strcat(tmp
, wm_class
);
149 key
= WMCreatePLString(tmp
);
151 adict
= WMGetFromPLDictionary(dict
, key
);
153 iconk
= WMCreatePLString("Icon");
156 val
= WMGetFromPLDictionary(adict
, iconk
);
158 /* no dictionary for app, so create one */
159 adict
= WMCreatePLDictionary(NULL
, NULL
);
160 WMPutInPLDictionary(dict
, key
, adict
);
161 WMReleasePropList(adict
);
165 val
= WMCreatePLString(iconPath
);
166 WMPutInPLDictionary(adict
, iconk
, val
);
167 WMReleasePropList(val
);
171 WMReleasePropList(key
);
172 WMReleasePropList(iconk
);
174 if (val
&& !wPreferences
.flags
.noupdates
) {
175 UpdateDomainFile(WDWindowAttributes
);
179 void wApplicationExtractDirPackIcon(WScreen
* scr
, char *path
, char *wm_instance
, char *wm_class
)
181 char *iconPath
= NULL
;
182 /* Maybe the app is a .app and it has an icon in it, like
183 * /usr/local/GNUstep/Applications/WPrefs.app/WPrefs.tiff
185 if (strstr(path
, ".app")) {
188 tmp
= wmalloc(strlen(path
) + 16);
190 if (scr
->flags
.supports_tiff
) {
192 strcat(tmp
, ".tiff");
193 if (access(tmp
, R_OK
) == 0)
200 if (access(tmp
, R_OK
) == 0)
209 wApplicationSaveIconPathFor(iconPath
, wm_instance
, wm_class
);
214 WApplication
*wApplicationCreate(WWindow
* wwin
)
216 WScreen
*scr
= wwin
->screen_ptr
;
217 Window main_window
= wwin
->main_window
;
221 if (main_window
== None
|| main_window
== scr
->root_win
)
228 /* check if the window is valid */
229 if (!XGetGeometry(dpy
, main_window
, &root
, &foo
, &foo
, &bar
, &bar
, &bar
, &bar
)) {
234 wapp
= wApplicationOf(main_window
);
237 if (wapp
->app_icon
&& wapp
->app_icon
->docked
&&
238 wapp
->app_icon
->relaunching
&& wapp
->main_window_desc
->fake_group
) {
239 wDockFinishLaunch(wapp
->app_icon
->dock
, wapp
->app_icon
);
245 wapp
= wmalloc(sizeof(WApplication
));
246 memset(wapp
, 0, sizeof(WApplication
));
249 wapp
->last_focused
= NULL
;
250 wapp
->urgent_bounce_timer
= NULL
;
252 wapp
->last_workspace
= 0;
254 wapp
->main_window
= main_window
;
255 wapp
->main_window_desc
= makeMainWindow(scr
, main_window
);
256 if (!wapp
->main_window_desc
) {
261 wapp
->main_window_desc
->fake_group
= wwin
->fake_group
;
262 wapp
->main_window_desc
->net_icon_image
= RRetainImage(wwin
->net_icon_image
);
264 extractIcon(wapp
->main_window_desc
);
266 leader
= wWindowFor(main_window
);
268 leader
->main_window
= main_window
;
270 wapp
->menu
= wAppMenuGet(scr
, main_window
);
273 wapp
->menu
= wUserMenuGet(scr
, wapp
->main_window_desc
);
274 #endif /* USER_MENU */
277 * Set application wide attributes from the leader.
279 wapp
->flags
.hidden
= WFLAGP(wapp
->main_window_desc
, start_hidden
);
280 wapp
->flags
.emulated
= WFLAGP(wapp
->main_window_desc
, emulate_appicon
);
282 /* application descriptor */
283 XSaveContext(dpy
, main_window
, wAppWinContext
, (XPointer
) wapp
);
285 if (!WFLAGP(wapp
->main_window_desc
, no_appicon
)) {
286 wapp
->app_icon
= NULL
;
288 wapp
->app_icon
= findDockIconFor(scr
->last_dock
, main_window
);
290 /* check main dock if we did not find it in last dock */
291 if (!wapp
->app_icon
&& scr
->dock
)
292 wapp
->app_icon
= findDockIconFor(scr
->dock
, main_window
);
294 /* finally check clips */
295 if (!wapp
->app_icon
) {
297 for (i
= 0; i
< scr
->workspace_count
; i
++) {
298 WDock
*dock
= scr
->workspaces
[i
]->clip
;
300 wapp
->app_icon
= findDockIconFor(dock
, main_window
);
306 if (wapp
->app_icon
) {
307 WWindow
*mainw
= wapp
->main_window_desc
;
309 wapp
->app_icon
->running
= 1;
310 wapp
->app_icon
->icon
->force_paint
= 1;
311 wapp
->app_icon
->icon
->owner
= mainw
;
312 if (mainw
->wm_hints
&& (mainw
->wm_hints
->flags
& IconWindowHint
))
313 wapp
->app_icon
->icon
->icon_win
= mainw
->wm_hints
->icon_window
;
314 wAppIconPaint(wapp
->app_icon
);
315 wAppIconSave(wapp
->app_icon
);
317 wapp
->app_icon
= wAppIconCreate(wapp
->main_window_desc
);
320 wapp
->app_icon
= NULL
;
324 wapp
->app_icon
->main_window
= main_window
;
326 if (wapp
->app_icon
&& !wapp
->app_icon
->docked
) {
327 WIcon
*icon
= wapp
->app_icon
->icon
;
328 WDock
*clip
= scr
->workspaces
[scr
->current_workspace
]->clip
;
331 if (clip
&& clip
->attract_icons
&& wDockFindFreeSlot(clip
, &x
, &y
)) {
332 wapp
->app_icon
->attracted
= 1;
333 if (!icon
->shadowed
) {
335 icon
->force_paint
= 1;
336 /* wAppIconPaint() is done in wDockAttachIcon() below */
338 wDockAttachIcon(clip
, wapp
->app_icon
, x
, y
);
340 PlaceIcon(scr
, &x
, &y
, wGetHeadForWindow(wapp
->main_window_desc
));
341 wAppIconMove(wapp
->app_icon
, x
, y
);
342 wLowerFrame(icon
->core
);
345 if (!clip
|| !wapp
->app_icon
->attracted
|| !clip
->collapsed
)
346 XMapWindow(dpy
, icon
->core
->window
);
349 if (wPreferences
.auto_arrange_icons
&& wapp
->app_icon
&& !wapp
->app_icon
->attracted
)
350 wArrangeIcons(scr
, True
);
352 if (wapp
->app_icon
) {
356 tmp
= wDefaultGetIconFile(scr
, wapp
->app_icon
->wm_instance
, wapp
->app_icon
->wm_class
, True
);
358 /* If the icon was saved by us from the client supplied icon, but is
359 * missing, recreate it. */
360 if (tmp
&& strstr(tmp
, "Library/WindowMaker/CachedPixmaps") != NULL
&&
361 stat(tmp
, &dummy
) != 0 && errno
== ENOENT
) {
362 wmessage(_("recreating missing icon '%s'"), tmp
);
363 path
= wIconStore(wapp
->app_icon
->icon
);
367 wIconUpdate(wapp
->app_icon
->icon
);
368 wAppIconPaint(wapp
->app_icon
);
371 /* if the displayed icon was supplied by the client, save the icon */
372 if (!tmp
|| strstr(tmp
, "Library/WindowMaker/CachedPixmaps") != NULL
)
373 wAppIconSave(wapp
->app_icon
);
378 void wApplicationDestroy(WApplication
* wapp
)
387 if (wapp
->refcount
> 0)
390 if (wapp
->urgent_bounce_timer
) {
391 WMDeleteTimerHandler(wapp
->urgent_bounce_timer
);
392 wapp
->urgent_bounce_timer
= NULL
;
394 if (wapp
->flags
.bouncing
) {
395 /* event.c:handleDestroyNotify forced this destroy
396 and thereby overlooked the bounce callback */
401 scr
= wapp
->main_window_desc
->screen_ptr
;
403 if (wapp
== scr
->wapp_list
) {
405 wapp
->next
->prev
= NULL
;
406 scr
->wapp_list
= wapp
->next
;
409 wapp
->next
->prev
= wapp
->prev
;
411 wapp
->prev
->next
= wapp
->next
;
414 XDeleteContext(dpy
, wapp
->main_window
, wAppWinContext
);
415 wAppMenuDestroy(wapp
->menu
);
416 wApplicationDeactivate(wapp
);
418 if (wapp
->app_icon
) {
419 if (wapp
->app_icon
->docked
&& !wapp
->app_icon
->attracted
) {
420 wapp
->app_icon
->running
= 0;
421 /* since we keep it, we don't care if it was attracted or not */
422 wapp
->app_icon
->attracted
= 0;
423 wapp
->app_icon
->icon
->shadowed
= 0;
424 wapp
->app_icon
->main_window
= None
;
425 wapp
->app_icon
->pid
= 0;
426 wapp
->app_icon
->icon
->owner
= NULL
;
427 wapp
->app_icon
->icon
->icon_win
= None
;
428 wapp
->app_icon
->icon
->force_paint
= 1;
429 wAppIconPaint(wapp
->app_icon
);
430 } else if (wapp
->app_icon
->docked
) {
431 wapp
->app_icon
->running
= 0;
432 wDockDetach(wapp
->app_icon
->dock
, wapp
->app_icon
);
434 wAppIconDestroy(wapp
->app_icon
);
437 wwin
= wWindowFor(wapp
->main_window_desc
->client_win
);
439 wWindowDestroy(wapp
->main_window_desc
);
441 /* undelete client window context that was deleted in
443 XSaveContext(dpy
, wwin
->client_win
, wWinContext
, (XPointer
) & wwin
->client_descriptor
);
447 if (wPreferences
.auto_arrange_icons
)
448 wArrangeIcons(scr
, True
);
451 void wApplicationActivate(WApplication
*wapp
)
454 if (wapp
->app_icon
) {
455 wIconSetHighlited(wapp
->app_icon
->icon
, True
);
456 wAppIconPaint(wapp
->app_icon
);
461 void wApplicationDeactivate(WApplication
*wapp
)
464 if (wapp
->app_icon
) {
465 wIconSetHighlited(wapp
->app_icon
->icon
, False
);
466 wAppIconPaint(wapp
->app_icon
);