Change to the linux kernel coding style
[wmaker-crm.git] / src / application.c
1 /*
2  *  Window Maker window manager
3  *
4  *  Copyright (c) 1997-2003 Alfredo K. Kojima
5  *  Copyright (c) 1998-2003 Dan Pascu
6  *
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.
11  *
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.
16  *
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.
21  */
22 #include "wconfig.h"
23
24 #include <X11/Xlib.h>
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <errno.h>
33
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"
51 #include "wsound.h"
52
53 #include "xinerama.h"
54
55 /******** Global variables ********/
56
57 extern XContext wAppWinContext;
58 extern XContext wWinContext;
59 extern WPreferences wPreferences;
60
61 extern WDDomain *WDWindowAttributes;
62
63 /******** Local variables ********/
64
65 static WWindow *makeMainWindow(WScreen * scr, Window window)
66 {
67         WWindow *wwin;
68         XWindowAttributes attr;
69
70         if (!XGetWindowAttributes(dpy, window, &attr)) {
71                 return NULL;
72         }
73
74         wwin = wWindowCreate();
75         wwin->screen_ptr = scr;
76         wwin->client_win = window;
77         wwin->main_window = window;
78         wwin->wm_hints = XGetWMHints(dpy, window);
79         /*    if (!MyXFetchName(dpy, window, &(wwin->frame->title))) {
80            wwin->frame->title = NULL;
81            }
82          */
83         PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
84
85         wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
86                                &wwin->user_flags, &wwin->defined_user_flags, True);
87
88         XSelectInput(dpy, window, attr.your_event_mask | PropertyChangeMask | StructureNotifyMask);
89         return wwin;
90 }
91
92 WApplication *wApplicationOf(Window window)
93 {
94         WApplication *wapp;
95
96         if (window == None)
97                 return NULL;
98         if (XFindContext(dpy, window, wAppWinContext, (XPointer *) & wapp) != XCSUCCESS)
99                 return NULL;
100         return wapp;
101 }
102
103 static WAppIcon *findDockIconFor(WDock * dock, Window main_window)
104 {
105         WAppIcon *aicon = NULL;
106
107         aicon = wDockFindIconForWindow(dock, main_window);
108         if (!aicon) {
109                 wDockTrackWindowLaunch(dock, main_window);
110                 aicon = wDockFindIconForWindow(dock, main_window);
111         }
112         return aicon;
113 }
114
115 static void extractIcon(WWindow * wwin)
116 {
117         char *progname;
118
119         progname = GetProgramNameForWindow(wwin->client_win);
120         if (progname) {
121                 wApplicationExtractDirPackIcon(wwin->screen_ptr, progname, wwin->wm_instance, wwin->wm_class);
122                 wfree(progname);
123         }
124 }
125
126 static void saveIconNameFor(char *iconPath, char *wm_instance, char *wm_class)
127 {
128         WMPropList *dict = WDWindowAttributes->dictionary;
129         WMPropList *adict, *key, *iconk;
130         WMPropList *val;
131         char *tmp;
132         int i;
133
134         i = 0;
135         if (wm_instance)
136                 i += strlen(wm_instance);
137         if (wm_class)
138                 i += strlen(wm_class);
139
140         tmp = wmalloc(i + 8);
141         *tmp = 0;
142         if (wm_class && wm_instance) {
143                 sprintf(tmp, "%s.%s", wm_instance, wm_class);
144         } else {
145                 if (wm_instance)
146                         strcat(tmp, wm_instance);
147                 if (wm_class)
148                         strcat(tmp, wm_class);
149         }
150
151         key = WMCreatePLString(tmp);
152         wfree(tmp);
153         adict = WMGetFromPLDictionary(dict, key);
154
155         iconk = WMCreatePLString("Icon");
156
157         if (adict) {
158                 val = WMGetFromPLDictionary(adict, iconk);
159         } else {
160                 /* no dictionary for app, so create one */
161                 adict = WMCreatePLDictionary(NULL, NULL);
162                 WMPutInPLDictionary(dict, key, adict);
163                 WMReleasePropList(adict);
164                 val = NULL;
165         }
166         if (!val) {
167                 val = WMCreatePLString(iconPath);
168                 WMPutInPLDictionary(adict, iconk, val);
169                 WMReleasePropList(val);
170         }
171         WMReleasePropList(key);
172         WMReleasePropList(iconk);
173
174         if (val && !wPreferences.flags.noupdates) {
175                 UpdateDomainFile(WDWindowAttributes);
176         }
177 }
178
179 void wApplicationExtractDirPackIcon(WScreen * scr, char *path, char *wm_instance, char *wm_class)
180 {
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
184          */
185         if (strstr(path, ".app")) {
186                 char *tmp;
187
188                 tmp = wmalloc(strlen(path) + 16);
189
190                 if (scr->flags.supports_tiff) {
191                         strcpy(tmp, path);
192                         strcat(tmp, ".tiff");
193                         if (access(tmp, R_OK) == 0)
194                                 iconPath = tmp;
195                 }
196                 if (!path) {
197                         strcpy(tmp, path);
198                         strcat(tmp, ".xpm");
199                         if (access(tmp, R_OK) == 0)
200                                 iconPath = tmp;
201                 }
202                 if (!iconPath)
203                         wfree(tmp);
204         }
205
206         if (iconPath) {
207                 saveIconNameFor(iconPath, wm_instance, wm_class);
208
209                 wfree(iconPath);
210         }
211 }
212
213 static Bool extractClientIcon(WAppIcon * icon)
214 {
215         char *path;
216
217         path = wIconStore(icon->icon);
218         if (!path)
219                 return False;
220
221         saveIconNameFor(path, icon->wm_instance, icon->wm_class);
222
223         wfree(path);
224
225         return True;
226 }
227
228 WApplication *wApplicationCreate(WWindow * wwin)
229 {
230         WScreen *scr = wwin->screen_ptr;
231         Window main_window = wwin->main_window;
232         WApplication *wapp;
233         WWindow *leader;
234
235         if (main_window == None || main_window == scr->root_win) {
236 #ifdef DEBUG0
237                 wwarning("trying to create application for %x", (unsigned)main_window);
238 #endif
239                 return NULL;
240         }
241
242         {
243                 Window root;
244                 int foo;
245                 unsigned int bar;
246                 /* check if the window is valid */
247                 if (!XGetGeometry(dpy, main_window, &root, &foo, &foo, &bar, &bar, &bar, &bar)) {
248                         return NULL;
249                 }
250         }
251
252         wapp = wApplicationOf(main_window);
253         if (wapp) {
254                 wapp->refcount++;
255                 if (wapp->app_icon && wapp->app_icon->docked &&
256                     wapp->app_icon->relaunching && wapp->main_window_desc->fake_group) {
257                         wDockFinishLaunch(wapp->app_icon->dock, wapp->app_icon);
258                 }
259
260                 return wapp;
261         }
262
263         wapp = wmalloc(sizeof(WApplication));
264         memset(wapp, 0, sizeof(WApplication));
265
266         wapp->refcount = 1;
267         wapp->last_focused = NULL;
268
269         wapp->last_workspace = 0;
270
271         wapp->main_window = main_window;
272         wapp->main_window_desc = makeMainWindow(scr, main_window);
273         if (!wapp->main_window_desc) {
274                 wfree(wapp);
275                 return NULL;
276         }
277
278         wapp->main_window_desc->fake_group = wwin->fake_group;
279 #ifdef NETWM_HINTS
280         wapp->main_window_desc->net_icon_image = RRetainImage(wwin->net_icon_image);
281 #endif
282
283         extractIcon(wapp->main_window_desc);
284
285         leader = wWindowFor(main_window);
286         if (leader) {
287                 leader->main_window = main_window;
288         }
289         wapp->menu = wAppMenuGet(scr, main_window);
290 #ifdef USER_MENU
291         if (!wapp->menu)
292                 wapp->menu = wUserMenuGet(scr, wapp->main_window_desc);
293 #endif                          /* USER_MENU */
294
295         /*
296          * Set application wide attributes from the leader.
297          */
298         wapp->flags.hidden = WFLAGP(wapp->main_window_desc, start_hidden);
299
300         wapp->flags.emulated = WFLAGP(wapp->main_window_desc, emulate_appicon);
301
302         /* application descriptor */
303         XSaveContext(dpy, main_window, wAppWinContext, (XPointer) wapp);
304
305         if (!WFLAGP(wapp->main_window_desc, no_appicon)) {
306                 wapp->app_icon = NULL;
307                 if (scr->last_dock)
308                         wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
309                 /* check main dock if we did not find it in last dock */
310                 if (!wapp->app_icon && scr->dock) {
311                         wapp->app_icon = findDockIconFor(scr->dock, main_window);
312                 }
313                 /* finally check clips */
314                 if (!wapp->app_icon) {
315                         int i;
316                         for (i = 0; i < scr->workspace_count; i++) {
317                                 WDock *dock = scr->workspaces[i]->clip;
318                                 if (dock)
319                                         wapp->app_icon = findDockIconFor(dock, main_window);
320                                 if (wapp->app_icon)
321                                         break;
322                         }
323                 }
324
325                 if (wapp->app_icon) {
326                         WWindow *mainw = wapp->main_window_desc;
327
328                         wapp->app_icon->running = 1;
329                         wapp->app_icon->icon->force_paint = 1;
330                         wapp->app_icon->icon->owner = mainw;
331                         if (mainw->wm_hints && (mainw->wm_hints->flags & IconWindowHint))
332                                 wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
333                         wAppIconPaint(wapp->app_icon);
334                 } else {
335                         wapp->app_icon = wAppIconCreate(wapp->main_window_desc);
336                 }
337         } else {
338                 wapp->app_icon = NULL;
339         }
340
341         if (wapp->app_icon) {
342                 wapp->app_icon->main_window = main_window;
343         }
344
345         if (wapp->app_icon && !wapp->app_icon->docked) {
346                 WIcon *icon = wapp->app_icon->icon;
347                 WDock *clip = scr->workspaces[scr->current_workspace]->clip;
348                 int x = 0, y = 0;
349
350                 if (clip && clip->attract_icons && wDockFindFreeSlot(clip, &x, &y)) {
351                         wapp->app_icon->attracted = 1;
352                         if (!icon->shadowed) {
353                                 icon->shadowed = 1;
354                                 icon->force_paint = 1;
355                                 /* wAppIconPaint() is done in wDockAttachIcon() below */
356                         }
357                         wDockAttachIcon(clip, wapp->app_icon, x, y);
358                 } else {
359                         PlaceIcon(scr, &x, &y, wGetHeadForWindow(wapp->main_window_desc));
360                         wAppIconMove(wapp->app_icon, x, y);
361                         wLowerFrame(icon->core);
362                 }
363                 if (!clip || !wapp->app_icon->attracted || !clip->collapsed)
364                         XMapWindow(dpy, icon->core->window);
365         }
366
367         if (wPreferences.auto_arrange_icons && wapp->app_icon && !wapp->app_icon->attracted) {
368                 wArrangeIcons(scr, True);
369         }
370
371         if (wapp->app_icon) {
372                 char *tmp, *path;
373                 struct stat dummy;
374                 RImage *image;
375
376                 tmp = wDefaultGetIconFile(scr, wapp->app_icon->wm_instance, wapp->app_icon->wm_class, True);
377
378                 /* If the icon was saved by us from the client supplied icon, but is
379                  * missing, recreate it. */
380                 if (tmp && strstr(tmp, "Library/WindowMaker/CachedPixmaps") != NULL &&
381                     stat(tmp, &dummy) != 0 && errno == ENOENT) {
382                         wmessage(_("recreating missing icon '%s'"), tmp);
383                         path = wIconStore(wapp->app_icon->icon);
384                         if (path) {
385                                 wfree(path);
386                         }
387                         image = wDefaultGetImage(scr, wapp->app_icon->wm_instance, wapp->app_icon->wm_class);
388                         if (image) {
389                                 wIconChangeImage(wapp->app_icon->icon, image);
390                                 wAppIconPaint(wapp->app_icon);
391                                 /* TODO:
392                                  * wIconChangeImage() should be rewriten to use retain/release
393                                  * The way it is now is too confusing about where the icon is
394                                  * finally released.  -Dan */
395                                 /* --this is wrong at the moment-- RReleaseImage(image); */
396                         }
397                 }
398
399                 /* if the displayed icon was supplied by the client, save the icon */
400                 if (!tmp)
401                         extractClientIcon(wapp->app_icon);
402         }
403
404         wSoundPlay(WSOUND_APPSTART);
405
406 #ifdef DEBUG
407         printf("Created application for %x\n", (unsigned)main_window);
408 #endif
409         return wapp;
410 }
411
412 void wApplicationDestroy(WApplication * wapp)
413 {
414         Window main_window;
415         WWindow *wwin;
416         WScreen *scr;
417
418         if (!wapp)
419                 return;
420
421         wapp->refcount--;
422         if (wapp->refcount > 0)
423                 return;
424
425         scr = wapp->main_window_desc->screen_ptr;
426         main_window = wapp->main_window;
427
428         if (wapp == scr->wapp_list) {
429                 if (wapp->next)
430                         wapp->next->prev = NULL;
431                 scr->wapp_list = wapp->next;
432         } else {
433                 if (wapp->next)
434                         wapp->next->prev = wapp->prev;
435                 if (wapp->prev)
436                         wapp->prev->next = wapp->next;
437         }
438
439         XDeleteContext(dpy, wapp->main_window, wAppWinContext);
440         wAppMenuDestroy(wapp->menu);
441         if (wapp->app_icon) {
442                 if (wapp->app_icon->docked && !wapp->app_icon->attracted) {
443                         wapp->app_icon->running = 0;
444                         /* since we keep it, we don't care if it was attracted or not */
445                         wapp->app_icon->attracted = 0;
446                         wapp->app_icon->icon->shadowed = 0;
447                         wapp->app_icon->main_window = None;
448                         wapp->app_icon->pid = 0;
449                         wapp->app_icon->icon->owner = NULL;
450                         wapp->app_icon->icon->icon_win = None;
451                         wapp->app_icon->icon->force_paint = 1;
452                         wAppIconPaint(wapp->app_icon);
453                 } else if (wapp->app_icon->docked) {
454                         wapp->app_icon->running = 0;
455                         wDockDetach(wapp->app_icon->dock, wapp->app_icon);
456                 } else {
457                         wAppIconDestroy(wapp->app_icon);
458                 }
459         }
460         wwin = wWindowFor(wapp->main_window_desc->client_win);
461
462         wWindowDestroy(wapp->main_window_desc);
463         if (wwin) {
464                 /* undelete client window context that was deleted in
465                  * wWindowDestroy */
466                 XSaveContext(dpy, wwin->client_win, wWinContext, (XPointer) & wwin->client_descriptor);
467         }
468         wfree(wapp);
469
470 #ifdef DEBUG
471         printf("Destroyed application for %x\n", (unsigned)main_window);
472 #endif
473         if (wPreferences.auto_arrange_icons) {
474                 wArrangeIcons(scr, True);
475         }
476
477         wSoundPlay(WSOUND_APPEXIT);
478 }