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 "WindowMaker.h"
33 #endif /* USER_MENU */
35 #include "application.h"
37 #include "properties.h"
40 #include "workspace.h"
43 /******** Global variables ********/
45 extern XContext wAppWinContext
;
46 extern XContext wWinContext
;
47 extern WPreferences wPreferences
;
48 extern WDDomain
*WDWindowAttributes
;
50 /******** Local variables ********/
52 static WWindow
*makeMainWindow(WScreen
* scr
, Window window
)
55 XWindowAttributes attr
;
57 if (!XGetWindowAttributes(dpy
, window
, &attr
))
60 wwin
= wWindowCreate();
61 wwin
->screen_ptr
= scr
;
62 wwin
->client_win
= window
;
63 wwin
->main_window
= window
;
64 wwin
->wm_hints
= XGetWMHints(dpy
, window
);
66 PropGetWMClass(window
, &wwin
->wm_class
, &wwin
->wm_instance
);
68 wDefaultFillAttributes(scr
, wwin
->wm_instance
, wwin
->wm_class
,
69 &wwin
->user_flags
, &wwin
->defined_user_flags
, True
);
71 XSelectInput(dpy
, window
, attr
.your_event_mask
| PropertyChangeMask
| StructureNotifyMask
);
75 WApplication
*wApplicationOf(Window window
)
81 if (XFindContext(dpy
, window
, wAppWinContext
, (XPointer
*) & wapp
) != XCSUCCESS
)
86 static WAppIcon
*findDockIconFor(WDock
* dock
, Window main_window
)
88 WAppIcon
*aicon
= NULL
;
90 aicon
= wDockFindIconForWindow(dock
, main_window
);
92 wDockTrackWindowLaunch(dock
, main_window
);
93 aicon
= wDockFindIconForWindow(dock
, main_window
);
98 static void extractIcon(WWindow
* wwin
)
102 /* Get the application name */
103 progname
= GetProgramNameForWindow(wwin
->client_win
);
105 /* Save the icon path if the application is ".app" */
106 wApplicationExtractDirPackIcon(wwin
->screen_ptr
, progname
, wwin
->wm_instance
, wwin
->wm_class
);
111 void wApplicationSaveIconPathFor(char *iconPath
, char *wm_instance
, char *wm_class
)
113 WMPropList
*dict
= WDWindowAttributes
->dictionary
;
114 WMPropList
*adict
, *key
, *iconk
;
118 tmp
= get_name_for_instance_class(wm_instance
, wm_class
);
119 key
= WMCreatePLString(tmp
);
122 adict
= WMGetFromPLDictionary(dict
, key
);
123 iconk
= WMCreatePLString("Icon");
126 val
= WMGetFromPLDictionary(adict
, iconk
);
128 /* no dictionary for app, so create one */
129 adict
= WMCreatePLDictionary(NULL
, NULL
);
130 WMPutInPLDictionary(dict
, key
, adict
);
131 WMReleasePropList(adict
);
136 val
= WMCreatePLString(iconPath
);
137 WMPutInPLDictionary(adict
, iconk
, val
);
138 WMReleasePropList(val
);
143 WMReleasePropList(key
);
144 WMReleasePropList(iconk
);
146 if (val
&& !wPreferences
.flags
.noupdates
)
147 UpdateDomainFile(WDWindowAttributes
);
150 /* This function is used if the application is a .app. It checks if it has an icon in it
151 * like for example /usr/local/GNUstep/Applications/WPrefs.app/WPrefs.tiff
153 void wApplicationExtractDirPackIcon(WScreen
* scr
, char *path
, char *wm_instance
, char *wm_class
)
155 char *iconPath
= NULL
;
158 if (strstr(path
, ".app")) {
159 tmp
= wmalloc(strlen(path
) + 16);
161 if (scr
->flags
.supports_tiff
) {
163 strcat(tmp
, ".tiff");
164 if (access(tmp
, R_OK
) == 0)
171 if (access(tmp
, R_OK
) == 0)
179 wApplicationSaveIconPathFor(iconPath
, wm_instance
, wm_class
);
185 static void app_icon_create_from_docks(WWindow
*wwin
, WApplication
*wapp
, Window main_window
)
187 WScreen
*scr
= wwin
->screen_ptr
;
190 wapp
->app_icon
= findDockIconFor(scr
->last_dock
, main_window
);
192 /* check main dock if we did not find it in last dock */
193 if (!wapp
->app_icon
&& scr
->dock
)
194 wapp
->app_icon
= findDockIconFor(scr
->dock
, main_window
);
196 /* finally check clips */
197 if (!wapp
->app_icon
) {
199 for (i
= 0; i
< scr
->workspace_count
; i
++) {
200 WDock
*dock
= scr
->workspaces
[i
]->clip
;
202 wapp
->app_icon
= findDockIconFor(dock
, main_window
);
208 /* If created, then set some flags */
209 if (wapp
->app_icon
) {
210 WWindow
*mainw
= wapp
->main_window_desc
;
212 wapp
->app_icon
->running
= 1;
213 wapp
->app_icon
->icon
->force_paint
= 1;
214 wapp
->app_icon
->icon
->owner
= mainw
;
215 if (mainw
->wm_hints
&& (mainw
->wm_hints
->flags
& IconWindowHint
))
216 wapp
->app_icon
->icon
->icon_win
= mainw
->wm_hints
->icon_window
;
218 wAppIconPaint(wapp
->app_icon
);
219 wAppIconSave(wapp
->app_icon
);
221 wapp
->app_icon
= wAppIconCreate(wapp
->main_window_desc
);
225 WApplication
*wApplicationCreate(WWindow
* wwin
)
227 WScreen
*scr
= wwin
->screen_ptr
;
228 Window main_window
= wwin
->main_window
;
232 if (main_window
== None
|| main_window
== scr
->root_win
)
239 /* check if the window is valid */
240 if (!XGetGeometry(dpy
, main_window
, &root
, &foo
, &foo
, &bar
, &bar
, &bar
, &bar
))
244 wapp
= wApplicationOf(main_window
);
247 if (wapp
->app_icon
&& wapp
->app_icon
->docked
&&
248 wapp
->app_icon
->relaunching
&& wapp
->main_window_desc
->fake_group
)
249 wDockFinishLaunch(wapp
->app_icon
->dock
, wapp
->app_icon
);
254 wapp
= wmalloc(sizeof(WApplication
));
257 wapp
->last_focused
= NULL
;
258 wapp
->urgent_bounce_timer
= NULL
;
260 wapp
->last_workspace
= 0;
262 wapp
->main_window
= main_window
;
263 wapp
->main_window_desc
= makeMainWindow(scr
, main_window
);
264 if (!wapp
->main_window_desc
) {
269 wapp
->main_window_desc
->fake_group
= wwin
->fake_group
;
270 wapp
->main_window_desc
->net_icon_image
= RRetainImage(wwin
->net_icon_image
);
272 extractIcon(wapp
->main_window_desc
);
274 leader
= wWindowFor(main_window
);
276 leader
->main_window
= main_window
;
278 wapp
->menu
= wAppMenuGet(scr
, main_window
);
281 wapp
->menu
= wUserMenuGet(scr
, wapp
->main_window_desc
);
282 #endif /* USER_MENU */
285 * Set application wide attributes from the leader.
287 wapp
->flags
.hidden
= WFLAGP(wapp
->main_window_desc
, start_hidden
);
288 wapp
->flags
.emulated
= WFLAGP(wapp
->main_window_desc
, emulate_appicon
);
290 /* application descriptor */
291 XSaveContext(dpy
, main_window
, wAppWinContext
, (XPointer
) wapp
);
293 /* Create the application icon */
294 wapp
->app_icon
= NULL
;
295 if (!WFLAGP(wapp
->main_window_desc
, no_appicon
)) {
296 /* Create the application icon using the icon from docks
297 * If not found in docks, create a new icon
298 * using the function wAppIconCreate() */
299 app_icon_create_from_docks(wwin
, wapp
, main_window
);
301 /* Now, paint the icon */
302 paint_app_icon(wapp
);
304 /* Save the app_icon in a file */
305 save_app_icon(wwin
, wapp
);
311 void wApplicationDestroy(WApplication
* wapp
)
320 if (wapp
->refcount
> 0)
323 if (wapp
->urgent_bounce_timer
) {
324 WMDeleteTimerHandler(wapp
->urgent_bounce_timer
);
325 wapp
->urgent_bounce_timer
= NULL
;
327 if (wapp
->flags
.bouncing
) {
328 /* event.c:handleDestroyNotify forced this destroy
329 and thereby overlooked the bounce callback */
334 scr
= wapp
->main_window_desc
->screen_ptr
;
336 if (wapp
== scr
->wapp_list
) {
338 wapp
->next
->prev
= NULL
;
339 scr
->wapp_list
= wapp
->next
;
342 wapp
->next
->prev
= wapp
->prev
;
344 wapp
->prev
->next
= wapp
->next
;
347 XDeleteContext(dpy
, wapp
->main_window
, wAppWinContext
);
348 wAppMenuDestroy(wapp
->menu
);
349 wApplicationDeactivate(wapp
);
351 /* Remove application icon */
352 removeAppIconFor(wapp
);
354 wwin
= wWindowFor(wapp
->main_window_desc
->client_win
);
356 wWindowDestroy(wapp
->main_window_desc
);
358 /* undelete client window context that was deleted in
360 XSaveContext(dpy
, wwin
->client_win
, wWinContext
, (XPointer
) & wwin
->client_descriptor
);
365 void wApplicationActivate(WApplication
*wapp
)
368 if (wapp
->app_icon
) {
369 wIconSetHighlited(wapp
->app_icon
->icon
, True
);
370 wAppIconPaint(wapp
->app_icon
);
375 void wApplicationDeactivate(WApplication
*wapp
)
378 if (wapp
->app_icon
) {
379 wIconSetHighlited(wapp
->app_icon
->icon
, False
);
380 wAppIconPaint(wapp
->app_icon
);