New function save_app_icon
[wmaker-crm.git] / src / application.c
blob311651439a2f226537b82174d624d5e8ace10150
1 /*
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.
21 #include "wconfig.h"
23 #include <X11/Xlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include "WindowMaker.h"
29 #include "menu.h"
30 #include "window.h"
31 #ifdef USER_MENU
32 #include "usermenu.h"
33 #endif /* USER_MENU */
34 #include "appicon.h"
35 #include "application.h"
36 #include "appmenu.h"
37 #include "properties.h"
38 #include "stacking.h"
39 #include "actions.h"
40 #include "workspace.h"
41 #include "dock.h"
43 #include "xinerama.h"
45 /******** Global variables ********/
47 extern XContext wAppWinContext;
48 extern XContext wWinContext;
49 extern WPreferences wPreferences;
51 extern WDDomain *WDWindowAttributes;
53 /******** Local variables ********/
55 static WWindow *makeMainWindow(WScreen * scr, Window window)
57 WWindow *wwin;
58 XWindowAttributes attr;
60 if (!XGetWindowAttributes(dpy, window, &attr)) {
61 return NULL;
64 wwin = wWindowCreate();
65 wwin->screen_ptr = scr;
66 wwin->client_win = window;
67 wwin->main_window = window;
68 wwin->wm_hints = XGetWMHints(dpy, window);
69 /* if (!MyXFetchName(dpy, window, &(wwin->frame->title))) {
70 wwin->frame->title = NULL;
73 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
75 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
76 &wwin->user_flags, &wwin->defined_user_flags, True);
78 XSelectInput(dpy, window, attr.your_event_mask | PropertyChangeMask | StructureNotifyMask);
79 return wwin;
82 WApplication *wApplicationOf(Window window)
84 WApplication *wapp;
86 if (window == None)
87 return NULL;
88 if (XFindContext(dpy, window, wAppWinContext, (XPointer *) & wapp) != XCSUCCESS)
89 return NULL;
90 return wapp;
93 static WAppIcon *findDockIconFor(WDock * dock, Window main_window)
95 WAppIcon *aicon = NULL;
97 aicon = wDockFindIconForWindow(dock, main_window);
98 if (!aicon) {
99 wDockTrackWindowLaunch(dock, main_window);
100 aicon = wDockFindIconForWindow(dock, main_window);
102 return aicon;
105 static void extractIcon(WWindow * wwin)
107 char *progname;
109 /* Get the application name */
110 progname = GetProgramNameForWindow(wwin->client_win);
111 if (progname) {
112 /* Save the icon path if the application is ".app" */
113 wApplicationExtractDirPackIcon(wwin->screen_ptr, progname, wwin->wm_instance, wwin->wm_class);
114 wfree(progname);
118 void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, char *wm_class)
120 WMPropList *dict = WDWindowAttributes->dictionary;
121 WMPropList *adict, *key, *iconk;
122 WMPropList *val;
123 char *tmp;
124 int i;
126 i = 0;
127 if (wm_instance)
128 i += strlen(wm_instance);
129 if (wm_class)
130 i += strlen(wm_class);
132 tmp = wmalloc(i + 8);
133 *tmp = 0;
134 if (wm_class && wm_instance) {
135 sprintf(tmp, "%s.%s", wm_instance, wm_class);
136 } else {
137 if (wm_instance)
138 strcat(tmp, wm_instance);
139 if (wm_class)
140 strcat(tmp, wm_class);
143 key = WMCreatePLString(tmp);
144 wfree(tmp);
146 adict = WMGetFromPLDictionary(dict, key);
147 iconk = WMCreatePLString("Icon");
149 if (adict) {
150 val = WMGetFromPLDictionary(adict, iconk);
151 } else {
152 /* no dictionary for app, so create one */
153 adict = WMCreatePLDictionary(NULL, NULL);
154 WMPutInPLDictionary(dict, key, adict);
155 WMReleasePropList(adict);
156 val = NULL;
159 if (!val) {
160 val = WMCreatePLString(iconPath);
161 WMPutInPLDictionary(adict, iconk, val);
162 WMReleasePropList(val);
163 } else {
164 val = NULL;
167 WMReleasePropList(key);
168 WMReleasePropList(iconk);
170 if (val && !wPreferences.flags.noupdates)
171 UpdateDomainFile(WDWindowAttributes);
174 /* This function is used if the application is a .app. It checks if it has an icon in it
175 * like for example /usr/local/GNUstep/Applications/WPrefs.app/WPrefs.tiff
177 void wApplicationExtractDirPackIcon(WScreen * scr, char *path, char *wm_instance, char *wm_class)
179 char *iconPath = NULL;
180 char *tmp = NULL;
182 if (strstr(path, ".app")) {
183 tmp = wmalloc(strlen(path) + 16);
185 if (scr->flags.supports_tiff) {
186 strcpy(tmp, path);
187 strcat(tmp, ".tiff");
188 if (access(tmp, R_OK) == 0)
189 iconPath = tmp;
192 if (!iconPath) {
193 strcpy(tmp, path);
194 strcat(tmp, ".xpm");
195 if (access(tmp, R_OK) == 0)
196 iconPath = tmp;
199 if (!iconPath)
200 wfree(tmp);
202 if (iconPath) {
203 wApplicationSaveIconPathFor(iconPath, wm_instance, wm_class);
204 wfree(iconPath);
209 WApplication *wApplicationCreate(WWindow * wwin)
211 WScreen *scr = wwin->screen_ptr;
212 Window main_window = wwin->main_window;
213 WApplication *wapp;
214 WWindow *leader;
216 if (main_window == None || main_window == scr->root_win)
217 return NULL;
220 Window root;
221 int foo;
222 unsigned int bar;
223 /* check if the window is valid */
224 if (!XGetGeometry(dpy, main_window, &root, &foo, &foo, &bar, &bar, &bar, &bar)) {
225 return NULL;
229 wapp = wApplicationOf(main_window);
230 if (wapp) {
231 wapp->refcount++;
232 if (wapp->app_icon && wapp->app_icon->docked &&
233 wapp->app_icon->relaunching && wapp->main_window_desc->fake_group) {
234 wDockFinishLaunch(wapp->app_icon->dock, wapp->app_icon);
237 return wapp;
240 wapp = wmalloc(sizeof(WApplication));
242 wapp->refcount = 1;
243 wapp->last_focused = NULL;
244 wapp->urgent_bounce_timer = NULL;
246 wapp->last_workspace = 0;
248 wapp->main_window = main_window;
249 wapp->main_window_desc = makeMainWindow(scr, main_window);
250 if (!wapp->main_window_desc) {
251 wfree(wapp);
252 return NULL;
255 wapp->main_window_desc->fake_group = wwin->fake_group;
256 wapp->main_window_desc->net_icon_image = RRetainImage(wwin->net_icon_image);
258 extractIcon(wapp->main_window_desc);
260 leader = wWindowFor(main_window);
261 if (leader)
262 leader->main_window = main_window;
264 wapp->menu = wAppMenuGet(scr, main_window);
265 #ifdef USER_MENU
266 if (!wapp->menu)
267 wapp->menu = wUserMenuGet(scr, wapp->main_window_desc);
268 #endif /* USER_MENU */
271 * Set application wide attributes from the leader.
273 wapp->flags.hidden = WFLAGP(wapp->main_window_desc, start_hidden);
274 wapp->flags.emulated = WFLAGP(wapp->main_window_desc, emulate_appicon);
276 /* application descriptor */
277 XSaveContext(dpy, main_window, wAppWinContext, (XPointer) wapp);
279 wapp->app_icon = NULL;
280 if (!WFLAGP(wapp->main_window_desc, no_appicon)) {
281 if (scr->last_dock)
282 wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
284 /* check main dock if we did not find it in last dock */
285 if (!wapp->app_icon && scr->dock)
286 wapp->app_icon = findDockIconFor(scr->dock, main_window);
288 /* finally check clips */
289 if (!wapp->app_icon) {
290 int i;
291 for (i = 0; i < scr->workspace_count; i++) {
292 WDock *dock = scr->workspaces[i]->clip;
293 if (dock)
294 wapp->app_icon = findDockIconFor(dock, main_window);
295 if (wapp->app_icon)
296 break;
300 if (wapp->app_icon) {
301 WWindow *mainw = wapp->main_window_desc;
303 wapp->app_icon->running = 1;
304 wapp->app_icon->icon->force_paint = 1;
305 wapp->app_icon->icon->owner = mainw;
306 if (mainw->wm_hints && (mainw->wm_hints->flags & IconWindowHint))
307 wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
308 wAppIconPaint(wapp->app_icon);
309 wAppIconSave(wapp->app_icon);
310 } else {
311 wapp->app_icon = wAppIconCreate(wapp->main_window_desc);
315 if (wapp->app_icon)
316 wapp->app_icon->main_window = main_window;
318 if (wapp->app_icon && !wapp->app_icon->docked) {
319 WIcon *icon = wapp->app_icon->icon;
320 WDock *clip = scr->workspaces[scr->current_workspace]->clip;
321 int x = 0, y = 0;
323 if (clip && clip->attract_icons && wDockFindFreeSlot(clip, &x, &y)) {
324 wapp->app_icon->attracted = 1;
325 if (!icon->shadowed) {
326 icon->shadowed = 1;
327 icon->force_paint = 1;
328 /* wAppIconPaint() is done in wDockAttachIcon() below */
330 wDockAttachIcon(clip, wapp->app_icon, x, y);
331 } else {
332 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wapp->main_window_desc));
333 wAppIconMove(wapp->app_icon, x, y);
334 wLowerFrame(icon->core);
337 if (!clip || !wapp->app_icon->attracted || !clip->collapsed)
338 XMapWindow(dpy, icon->core->window);
341 if (wPreferences.auto_arrange_icons && wapp->app_icon && !wapp->app_icon->attracted)
342 wArrangeIcons(scr, True);
344 /* Save the app_icon in a file */
345 save_app_icon(wwin, wapp);
347 return wapp;
350 void wApplicationDestroy(WApplication * wapp)
352 WWindow *wwin;
353 WScreen *scr;
355 if (!wapp)
356 return;
358 wapp->refcount--;
359 if (wapp->refcount > 0)
360 return;
362 if (wapp->urgent_bounce_timer) {
363 WMDeleteTimerHandler(wapp->urgent_bounce_timer);
364 wapp->urgent_bounce_timer = NULL;
366 if (wapp->flags.bouncing) {
367 /* event.c:handleDestroyNotify forced this destroy
368 and thereby overlooked the bounce callback */
369 wapp->refcount = 1;
370 return;
373 scr = wapp->main_window_desc->screen_ptr;
375 if (wapp == scr->wapp_list) {
376 if (wapp->next)
377 wapp->next->prev = NULL;
378 scr->wapp_list = wapp->next;
379 } else {
380 if (wapp->next)
381 wapp->next->prev = wapp->prev;
382 if (wapp->prev)
383 wapp->prev->next = wapp->next;
386 XDeleteContext(dpy, wapp->main_window, wAppWinContext);
387 wAppMenuDestroy(wapp->menu);
388 wApplicationDeactivate(wapp);
390 if (wapp->app_icon) {
391 if (wapp->app_icon->docked && !wapp->app_icon->attracted) {
392 wapp->app_icon->running = 0;
393 /* since we keep it, we don't care if it was attracted or not */
394 wapp->app_icon->attracted = 0;
395 wapp->app_icon->icon->shadowed = 0;
396 wapp->app_icon->main_window = None;
397 wapp->app_icon->pid = 0;
398 wapp->app_icon->icon->owner = NULL;
399 wapp->app_icon->icon->icon_win = None;
400 wapp->app_icon->icon->force_paint = 1;
401 wAppIconPaint(wapp->app_icon);
402 } else if (wapp->app_icon->docked) {
403 wapp->app_icon->running = 0;
404 wDockDetach(wapp->app_icon->dock, wapp->app_icon);
405 } else {
406 wAppIconDestroy(wapp->app_icon);
409 wwin = wWindowFor(wapp->main_window_desc->client_win);
411 wWindowDestroy(wapp->main_window_desc);
412 if (wwin) {
413 /* undelete client window context that was deleted in
414 * wWindowDestroy */
415 XSaveContext(dpy, wwin->client_win, wWinContext, (XPointer) & wwin->client_descriptor);
417 wfree(wapp);
419 if (wPreferences.auto_arrange_icons)
420 wArrangeIcons(scr, True);
423 void wApplicationActivate(WApplication *wapp)
425 #ifdef NEWAPPICON
426 if (wapp->app_icon) {
427 wIconSetHighlited(wapp->app_icon->icon, True);
428 wAppIconPaint(wapp->app_icon);
430 #endif
433 void wApplicationDeactivate(WApplication *wapp)
435 #ifdef NEWAPPICON
436 if (wapp->app_icon) {
437 wIconSetHighlited(wapp->app_icon->icon, False);
438 wAppIconPaint(wapp->app_icon);
440 #endif