Periodic bouncing for XUrgencyHint
[wmaker-crm.git] / src / application.c
blobaf0fe618f9e2beb283e269f041194ad662977057
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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <errno.h>
34 #include "WindowMaker.h"
35 #include "menu.h"
36 #include "window.h"
37 #ifdef USER_MENU
38 #include "usermenu.h"
39 #endif /* USER_MENU */
40 #include "icon.h"
41 #include "appicon.h"
42 #include "application.h"
43 #include "appmenu.h"
44 #include "properties.h"
45 #include "funcs.h"
46 #include "stacking.h"
47 #include "actions.h"
48 #include "defaults.h"
49 #include "workspace.h"
50 #include "dock.h"
52 #include "xinerama.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)
66 WWindow *wwin;
67 XWindowAttributes attr;
69 if (!XGetWindowAttributes(dpy, window, &attr)) {
70 return NULL;
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);
88 return wwin;
91 WApplication *wApplicationOf(Window window)
93 WApplication *wapp;
95 if (window == None)
96 return NULL;
97 if (XFindContext(dpy, window, wAppWinContext, (XPointer *) & wapp) != XCSUCCESS)
98 return NULL;
99 return wapp;
102 static WAppIcon *findDockIconFor(WDock * dock, Window main_window)
104 WAppIcon *aicon = NULL;
106 aicon = wDockFindIconForWindow(dock, main_window);
107 if (!aicon) {
108 wDockTrackWindowLaunch(dock, main_window);
109 aicon = wDockFindIconForWindow(dock, main_window);
111 return aicon;
114 static void extractIcon(WWindow * wwin)
116 char *progname;
118 progname = GetProgramNameForWindow(wwin->client_win);
119 if (progname) {
120 wApplicationExtractDirPackIcon(wwin->screen_ptr, progname, wwin->wm_instance, wwin->wm_class);
121 wfree(progname);
125 static void saveIconNameFor(char *iconPath, char *wm_instance, char *wm_class)
127 WMPropList *dict = WDWindowAttributes->dictionary;
128 WMPropList *adict, *key, *iconk;
129 WMPropList *val;
130 char *tmp;
131 int i;
133 i = 0;
134 if (wm_instance)
135 i += strlen(wm_instance);
136 if (wm_class)
137 i += strlen(wm_class);
139 tmp = wmalloc(i + 8);
140 *tmp = 0;
141 if (wm_class && wm_instance) {
142 sprintf(tmp, "%s.%s", wm_instance, wm_class);
143 } else {
144 if (wm_instance)
145 strcat(tmp, wm_instance);
146 if (wm_class)
147 strcat(tmp, wm_class);
150 key = WMCreatePLString(tmp);
151 wfree(tmp);
152 adict = WMGetFromPLDictionary(dict, key);
154 iconk = WMCreatePLString("Icon");
156 if (adict) {
157 val = WMGetFromPLDictionary(adict, iconk);
158 } else {
159 /* no dictionary for app, so create one */
160 adict = WMCreatePLDictionary(NULL, NULL);
161 WMPutInPLDictionary(dict, key, adict);
162 WMReleasePropList(adict);
163 val = NULL;
165 if (!val) {
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")) {
185 char *tmp;
187 tmp = wmalloc(strlen(path) + 16);
189 if (scr->flags.supports_tiff) {
190 strcpy(tmp, path);
191 strcat(tmp, ".tiff");
192 if (access(tmp, R_OK) == 0)
193 iconPath = tmp;
195 if (!path) {
196 strcpy(tmp, path);
197 strcat(tmp, ".xpm");
198 if (access(tmp, R_OK) == 0)
199 iconPath = tmp;
201 if (!iconPath)
202 wfree(tmp);
205 if (iconPath) {
206 saveIconNameFor(iconPath, wm_instance, wm_class);
208 wfree(iconPath);
212 static Bool extractClientIcon(WAppIcon * icon)
214 char *path;
216 path = wIconStore(icon->icon);
217 if (!path)
218 return False;
220 saveIconNameFor(path, icon->wm_instance, icon->wm_class);
222 wfree(path);
224 return True;
227 WApplication *wApplicationCreate(WWindow * wwin)
229 WScreen *scr = wwin->screen_ptr;
230 Window main_window = wwin->main_window;
231 WApplication *wapp;
232 WWindow *leader;
234 if (main_window == None || main_window == scr->root_win)
235 return NULL;
238 Window root;
239 int foo;
240 unsigned int bar;
241 /* check if the window is valid */
242 if (!XGetGeometry(dpy, main_window, &root, &foo, &foo, &bar, &bar, &bar, &bar)) {
243 return NULL;
247 wapp = wApplicationOf(main_window);
248 if (wapp) {
249 wapp->refcount++;
250 if (wapp->app_icon && wapp->app_icon->docked &&
251 wapp->app_icon->relaunching && wapp->main_window_desc->fake_group) {
252 wDockFinishLaunch(wapp->app_icon->dock, wapp->app_icon);
255 return wapp;
258 wapp = wmalloc(sizeof(WApplication));
259 memset(wapp, 0, sizeof(WApplication));
261 wapp->refcount = 1;
262 wapp->last_focused = NULL;
263 wapp->urgent_bounce_timer = NULL;
265 wapp->last_workspace = 0;
267 wapp->main_window = main_window;
268 wapp->main_window_desc = makeMainWindow(scr, main_window);
269 if (!wapp->main_window_desc) {
270 wfree(wapp);
271 return NULL;
274 wapp->main_window_desc->fake_group = wwin->fake_group;
275 wapp->main_window_desc->net_icon_image = RRetainImage(wwin->net_icon_image);
277 extractIcon(wapp->main_window_desc);
279 leader = wWindowFor(main_window);
280 if (leader) {
281 leader->main_window = main_window;
283 wapp->menu = wAppMenuGet(scr, main_window);
284 #ifdef USER_MENU
285 if (!wapp->menu)
286 wapp->menu = wUserMenuGet(scr, wapp->main_window_desc);
287 #endif /* USER_MENU */
290 * Set application wide attributes from the leader.
292 wapp->flags.hidden = WFLAGP(wapp->main_window_desc, start_hidden);
294 wapp->flags.emulated = WFLAGP(wapp->main_window_desc, emulate_appicon);
296 /* application descriptor */
297 XSaveContext(dpy, main_window, wAppWinContext, (XPointer) wapp);
299 if (!WFLAGP(wapp->main_window_desc, no_appicon)) {
300 wapp->app_icon = NULL;
301 if (scr->last_dock)
302 wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
303 /* check main dock if we did not find it in last dock */
304 if (!wapp->app_icon && scr->dock) {
305 wapp->app_icon = findDockIconFor(scr->dock, main_window);
307 /* finally check clips */
308 if (!wapp->app_icon) {
309 int i;
310 for (i = 0; i < scr->workspace_count; i++) {
311 WDock *dock = scr->workspaces[i]->clip;
312 if (dock)
313 wapp->app_icon = findDockIconFor(dock, main_window);
314 if (wapp->app_icon)
315 break;
319 if (wapp->app_icon) {
320 WWindow *mainw = wapp->main_window_desc;
322 wapp->app_icon->running = 1;
323 wapp->app_icon->icon->force_paint = 1;
324 wapp->app_icon->icon->owner = mainw;
325 if (mainw->wm_hints && (mainw->wm_hints->flags & IconWindowHint))
326 wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
327 wAppIconPaint(wapp->app_icon);
328 } else {
329 wapp->app_icon = wAppIconCreate(wapp->main_window_desc);
331 } else {
332 wapp->app_icon = NULL;
335 if (wapp->app_icon) {
336 wapp->app_icon->main_window = main_window;
339 if (wapp->app_icon && !wapp->app_icon->docked) {
340 WIcon *icon = wapp->app_icon->icon;
341 WDock *clip = scr->workspaces[scr->current_workspace]->clip;
342 int x = 0, y = 0;
344 if (clip && clip->attract_icons && wDockFindFreeSlot(clip, &x, &y)) {
345 wapp->app_icon->attracted = 1;
346 if (!icon->shadowed) {
347 icon->shadowed = 1;
348 icon->force_paint = 1;
349 /* wAppIconPaint() is done in wDockAttachIcon() below */
351 wDockAttachIcon(clip, wapp->app_icon, x, y);
352 } else {
353 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wapp->main_window_desc));
354 wAppIconMove(wapp->app_icon, x, y);
355 wLowerFrame(icon->core);
357 if (!clip || !wapp->app_icon->attracted || !clip->collapsed)
358 XMapWindow(dpy, icon->core->window);
361 if (wPreferences.auto_arrange_icons && wapp->app_icon && !wapp->app_icon->attracted) {
362 wArrangeIcons(scr, True);
365 if (wapp->app_icon) {
366 char *tmp, *path;
367 struct stat dummy;
368 RImage *image;
370 tmp = wDefaultGetIconFile(scr, wapp->app_icon->wm_instance, wapp->app_icon->wm_class, True);
372 /* If the icon was saved by us from the client supplied icon, but is
373 * missing, recreate it. */
374 if (tmp && strstr(tmp, "Library/WindowMaker/CachedPixmaps") != NULL &&
375 stat(tmp, &dummy) != 0 && errno == ENOENT) {
376 wmessage(_("recreating missing icon '%s'"), tmp);
377 path = wIconStore(wapp->app_icon->icon);
378 if (path) {
379 wfree(path);
381 image = wDefaultGetImage(scr, wapp->app_icon->wm_instance, wapp->app_icon->wm_class);
382 if (image) {
383 wIconChangeImage(wapp->app_icon->icon, image);
384 wAppIconPaint(wapp->app_icon);
385 /* TODO:
386 * wIconChangeImage() should be rewriten to use retain/release
387 * The way it is now is too confusing about where the icon is
388 * finally released. -Dan */
389 /* --this is wrong at the moment-- RReleaseImage(image); */
393 /* if the displayed icon was supplied by the client, save the icon */
394 if (!tmp)
395 extractClientIcon(wapp->app_icon);
397 return wapp;
400 void wApplicationDestroy(WApplication * wapp)
402 Window main_window;
403 WWindow *wwin;
404 WScreen *scr;
406 if (!wapp)
407 return;
409 wapp->refcount--;
410 if (wapp->refcount > 0)
411 return;
413 if (wapp->urgent_bounce_timer) {
414 WMDeleteTimerHandler(wapp->urgent_bounce_timer);
415 wapp->urgent_bounce_timer = NULL;
417 if (wapp->flags.bouncing) {
418 /* event.c:handleDestroyNotify forced this destroy
419 and thereby overlooked the bounce callback */
420 wapp->refcount = 1;
421 return;
424 scr = wapp->main_window_desc->screen_ptr;
425 main_window = wapp->main_window;
427 if (wapp == scr->wapp_list) {
428 if (wapp->next)
429 wapp->next->prev = NULL;
430 scr->wapp_list = wapp->next;
431 } else {
432 if (wapp->next)
433 wapp->next->prev = wapp->prev;
434 if (wapp->prev)
435 wapp->prev->next = wapp->next;
438 XDeleteContext(dpy, wapp->main_window, wAppWinContext);
439 wAppMenuDestroy(wapp->menu);
440 wApplicationDeactivate(wapp);
442 if (wapp->app_icon) {
443 if (wapp->app_icon->docked && !wapp->app_icon->attracted) {
444 wapp->app_icon->running = 0;
445 /* since we keep it, we don't care if it was attracted or not */
446 wapp->app_icon->attracted = 0;
447 wapp->app_icon->icon->shadowed = 0;
448 wapp->app_icon->main_window = None;
449 wapp->app_icon->pid = 0;
450 wapp->app_icon->icon->owner = NULL;
451 wapp->app_icon->icon->icon_win = None;
452 wapp->app_icon->icon->force_paint = 1;
453 wAppIconPaint(wapp->app_icon);
454 } else if (wapp->app_icon->docked) {
455 wapp->app_icon->running = 0;
456 wDockDetach(wapp->app_icon->dock, wapp->app_icon);
457 } else {
458 wAppIconDestroy(wapp->app_icon);
461 wwin = wWindowFor(wapp->main_window_desc->client_win);
463 wWindowDestroy(wapp->main_window_desc);
464 if (wwin) {
465 /* undelete client window context that was deleted in
466 * wWindowDestroy */
467 XSaveContext(dpy, wwin->client_win, wWinContext, (XPointer) & wwin->client_descriptor);
469 wfree(wapp);
471 if (wPreferences.auto_arrange_icons)
472 wArrangeIcons(scr, True);