2 * Window Maker window manager
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 * Copyright (c) 1998 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 ********/
66 makeMainWindow(WScreen
*scr
, Window window
)
69 XWindowAttributes attr
;
71 if (!XGetWindowAttributes(dpy
, window
, &attr
)) {
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
);
97 wApplicationOf(Window window
)
103 if (XFindContext(dpy
, window
, wAppWinContext
, (XPointer
*)&wapp
)!=XCSUCCESS
)
110 findDockIconFor(WDock
*dock
, Window main_window
)
112 WAppIcon
*aicon
= NULL
;
114 aicon
= wDockFindIconForWindow(dock
, main_window
);
116 wDockTrackWindowLaunch(dock
, main_window
);
117 aicon
= wDockFindIconForWindow(dock
, main_window
);
124 extractIcon(WWindow
*wwin
)
129 if (!XGetCommand(dpy
, wwin
->client_win
, &argv
, &argc
) || argc
< 1)
132 wApplicationExtractDirPackIcon(wwin
->screen_ptr
,argv
[0],
135 XFreeStringList(argv
);
140 saveIconNameFor(char *iconPath
, char *wm_instance
, char *wm_class
)
142 WMPropList
*dict
= WDWindowAttributes
->dictionary
;
143 WMPropList
*adict
, *key
, *iconk
;
150 i
+= strlen(wm_instance
);
152 i
+= strlen(wm_class
);
156 if (wm_class
&& wm_instance
) {
157 sprintf(tmp
, "%s.%s", wm_instance
, wm_class
);
160 strcat(tmp
, wm_instance
);
162 strcat(tmp
, wm_class
);
165 key
= WMCreatePLString(tmp
);
167 adict
= WMGetFromPLDictionary(dict
, key
);
169 iconk
= WMCreatePLString("Icon");
172 val
= WMGetFromPLDictionary(adict
, iconk
);
174 /* no dictionary for app, so create one */
175 adict
= WMCreatePLDictionary(NULL
, NULL
, NULL
);
176 WMPutInPLDictionary(dict
, key
, adict
);
177 WMReleasePropList(adict
);
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
);
194 wApplicationExtractDirPackIcon(WScreen
*scr
, char *path
,
195 char *wm_instance
, char *wm_class
)
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")) {
204 tmp
= wmalloc(strlen(path
)+16);
206 if (scr
->flags
.supports_tiff
) {
208 strcat(tmp
, ".tiff");
209 if (access(tmp
, R_OK
)==0)
215 if (access(tmp
, R_OK
)==0)
223 saveIconNameFor(iconPath
, wm_instance
, wm_class
);
231 extractClientIcon(WAppIcon
*icon
)
235 path
= wIconStore(icon
->icon
);
239 saveIconNameFor(path
, icon
->wm_instance
, icon
->wm_class
);
248 wApplicationCreate(WScreen
*scr
, Window main_window
)
253 if (main_window
==None
|| main_window
==scr
->root_win
) {
255 wwarning("trying to create application for %x",(unsigned)main_window
);
264 /* check if the window is valid */
265 if (!XGetGeometry(dpy
, main_window
, &root
, &foo
, &foo
, &bar
, &bar
,
271 wapp
= wApplicationOf(main_window
);
274 if (wapp
->app_icon
&& wapp
->app_icon
->docked
&&
275 wapp
->app_icon
->relaunching
&& wapp
->main_window_desc
->fake_group
) {
276 wDockFinishLaunch(wapp
->app_icon
->dock
, wapp
->app_icon
);
282 wapp
= wmalloc(sizeof(WApplication
));
283 memset(wapp
, 0, sizeof(WApplication
));
286 wapp
->last_focused
= NULL
;
288 wapp
->last_workspace
= 0;
290 wapp
->main_window
= main_window
;
291 wapp
->main_window_desc
= makeMainWindow(scr
, main_window
);
292 if (!wapp
->main_window_desc
) {
297 extractIcon(wapp
->main_window_desc
);
299 leader
= wWindowFor(main_window
);
301 leader
->main_window
= main_window
;
303 wapp
->menu
= wAppMenuGet(scr
, main_window
);
305 if (!wapp
->menu
) wapp
->menu
= wUserMenuGet(scr
, wapp
->main_window_desc
);
306 #endif /* USER_MENU */
310 * Set application wide attributes from the leader.
312 wapp
->flags
.hidden
= WFLAGP(wapp
->main_window_desc
, start_hidden
);
314 wapp
->flags
.emulated
= WFLAGP(wapp
->main_window_desc
, emulate_appicon
);
316 /* application descriptor */
317 XSaveContext(dpy
, main_window
, wAppWinContext
, (XPointer
)wapp
);
319 if (!WFLAGP(wapp
->main_window_desc
, no_appicon
)) {
320 wapp
->app_icon
= NULL
;
322 wapp
->app_icon
= findDockIconFor(scr
->last_dock
, main_window
);
323 /* check main dock if we did not find it in last dock */
324 if (!wapp
->app_icon
&& scr
->dock
) {
325 wapp
->app_icon
= findDockIconFor(scr
->dock
, main_window
);
327 /* finally check clips */
328 if (!wapp
->app_icon
) {
330 for (i
=0; i
<scr
->workspace_count
; i
++) {
331 WDock
*dock
= scr
->workspaces
[i
]->clip
;
333 wapp
->app_icon
= findDockIconFor(dock
, main_window
);
339 if (wapp
->app_icon
) {
340 WWindow
*mainw
= wapp
->main_window_desc
;
342 wapp
->app_icon
->running
= 1;
343 wapp
->app_icon
->icon
->force_paint
= 1;
344 wapp
->app_icon
->icon
->owner
= mainw
;
345 if (mainw
->wm_hints
&& (mainw
->wm_hints
->flags
&IconWindowHint
))
346 wapp
->app_icon
->icon
->icon_win
= mainw
->wm_hints
->icon_window
;
347 wAppIconPaint(wapp
->app_icon
);
349 wapp
->app_icon
= wAppIconCreate(wapp
->main_window_desc
);
352 wapp
->app_icon
= NULL
;
355 if (wapp
->app_icon
) {
356 wapp
->app_icon
->main_window
= main_window
;
359 if (wapp
->app_icon
&& !wapp
->app_icon
->docked
) {
360 WIcon
*icon
= wapp
->app_icon
->icon
;
361 WDock
*clip
= scr
->workspaces
[scr
->current_workspace
]->clip
;
364 if (clip
&& clip
->attract_icons
&& wDockFindFreeSlot(clip
, &x
, &y
)) {
365 wapp
->app_icon
->attracted
= 1;
366 if (!icon
->shadowed
) {
368 icon
->force_paint
= 1;
369 /* wAppIconPaint() is done in wDockAttachIcon() below */
371 wDockAttachIcon(clip
, wapp
->app_icon
, x
, y
);
373 PlaceIcon(scr
, &x
, &y
);
374 wAppIconMove(wapp
->app_icon
, x
, y
);
375 wLowerFrame(icon
->core
);
377 if (!clip
|| !wapp
->app_icon
->attracted
|| !clip
->collapsed
)
378 XMapWindow(dpy
, icon
->core
->window
);
381 if (wPreferences
.auto_arrange_icons
&& wapp
->app_icon
&& !wapp
->app_icon
->attracted
) {
382 wArrangeIcons(scr
, True
);
385 if (wapp
->app_icon
) {
390 tmp
= wDefaultGetIconFile(scr
, wapp
->app_icon
->wm_instance
,
391 wapp
->app_icon
->wm_class
, True
);
393 /* If the icon was saved by us from the client supplied icon, but is
394 * missing, recreate it. */
395 if (tmp
&& strstr(tmp
, ".AppInfo/WindowMaker")!=NULL
&&
396 stat(tmp
, &dummy
)!=0 && errno
==ENOENT
) {
397 wmessage(_("recreating missing icon '%s'"), tmp
);
398 path
= wIconStore(wapp
->app_icon
->icon
);
402 image
= wDefaultGetImage(scr
, wapp
->app_icon
->wm_instance
,
403 wapp
->app_icon
->wm_class
);
405 wIconChangeImage(wapp
->app_icon
->icon
, image
);
406 wAppIconPaint(wapp
->app_icon
);
408 * wIconChangeImage() should be rewriten to use retain/release
409 * The way it is now is too confusing about where the icon is
410 * finally released. -Dan */
411 /* --this is wrong at the moment-- RReleaseImage(image);*/
415 /* if the displayed icon was supplied by the client, save the icon */
417 extractClientIcon(wapp
->app_icon
);
420 wSoundPlay(WSOUND_APPSTART
);
423 printf("Created application for %x\n", (unsigned)main_window
);
430 wApplicationDestroy(WApplication
*wapp
)
440 if (wapp
->refcount
>0)
444 scr
= wapp
->main_window_desc
->screen_ptr
;
445 main_window
= wapp
->main_window
;
447 if (wapp
== scr
->wapp_list
) {
449 wapp
->next
->prev
= NULL
;
450 scr
->wapp_list
= wapp
->next
;
453 wapp
->next
->prev
= wapp
->prev
;
455 wapp
->prev
->next
= wapp
->next
;
458 XDeleteContext(dpy
, wapp
->main_window
, wAppWinContext
);
459 wAppMenuDestroy(wapp
->menu
);
460 if (wapp
->app_icon
) {
461 if (wapp
->app_icon
->docked
&& !wapp
->app_icon
->attracted
) {
462 wapp
->app_icon
->running
= 0;
463 /* since we keep it, we don't care if it was attracted or not */
464 wapp
->app_icon
->attracted
= 0;
465 wapp
->app_icon
->icon
->shadowed
= 0;
466 wapp
->app_icon
->main_window
= None
;
467 wapp
->app_icon
->pid
= 0;
468 wapp
->app_icon
->icon
->owner
= NULL
;
469 wapp
->app_icon
->icon
->icon_win
= None
;
470 wapp
->app_icon
->icon
->force_paint
= 1;
471 wAppIconPaint(wapp
->app_icon
);
472 } else if (wapp
->app_icon
->docked
) {
473 wapp
->app_icon
->running
= 0;
474 wDockDetach(wapp
->app_icon
->dock
, wapp
->app_icon
);
476 wAppIconDestroy(wapp
->app_icon
);
479 wwin
= wWindowFor(wapp
->main_window_desc
->client_win
);
481 wWindowDestroy(wapp
->main_window_desc
);
483 /* undelete client window context that was deleted in
485 XSaveContext(dpy
, wwin
->client_win
, wWinContext
,
486 (XPointer
)&wwin
->client_descriptor
);
491 printf("Destroyed application for %x\n", (unsigned)main_window
);
493 if (wPreferences
.auto_arrange_icons
) {
494 wArrangeIcons(scr
, True
);
497 wSoundPlay(WSOUND_APPEXIT
);