*** empty log message ***
[wmaker-crm.git] / src / application.c
blob83813dcf9de1693a2e4cfc76c6bc667d586350c0
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
21 #include "wconfig.h"
23 #include <X11/Xlib.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "WindowMaker.h"
30 #include "menu.h"
31 #include "window.h"
32 #ifdef USER_MENU
33 #include "usermenu.h"
34 #endif /* USER_MENU */
35 #include "icon.h"
36 #include "appicon.h"
37 #include "application.h"
38 #include "appmenu.h"
39 #include "properties.h"
40 #include "funcs.h"
41 #include "stacking.h"
42 #include "actions.h"
43 #include "defaults.h"
44 #include "workspace.h"
46 #include "dock.h"
48 #ifdef WMSOUND
49 #include "wmsound.h"
50 #endif
53 /******** Global variables ********/
55 extern XContext wAppWinContext;
56 extern XContext wWinContext;
57 extern WPreferences wPreferences;
59 extern WDDomain *WDWindowAttributes;
61 /******** Local variables ********/
64 static WWindow*
65 makeMainWindow(WScreen *scr, Window window)
67 WWindow *wwin;
68 XWindowAttributes attr;
70 if (!XGetWindowAttributes(dpy, window, &attr)) {
71 return NULL;
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;
83 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
86 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
87 &wwin->user_flags, &wwin->defined_user_flags, True);
89 XSelectInput(dpy, window, attr.your_event_mask | PropertyChangeMask
90 | StructureNotifyMask );
91 return wwin;
95 WApplication*
96 wApplicationOf(Window window)
98 WApplication *wapp;
100 if (window == None)
101 return NULL;
102 if (XFindContext(dpy, window, wAppWinContext, (XPointer*)&wapp)!=XCSUCCESS)
103 return NULL;
104 return wapp;
108 static WAppIcon*
109 findDockIconFor(WDock *dock, Window main_window)
111 WAppIcon *aicon = NULL;
113 aicon = wDockFindIconFor(dock, main_window);
114 if (!aicon) {
115 wDockTrackWindowLaunch(dock, main_window);
116 aicon = wDockFindIconFor(dock, main_window);
118 return aicon;
122 static void
123 extractIcon(WWindow *wwin)
125 int argc;
126 char **argv;
128 if (!XGetCommand(dpy, wwin->client_win, &argv, &argc) || argc < 1)
129 return;
131 wApplicationExtractDirPackIcon(wwin->screen_ptr,argv[0],
132 wwin->wm_instance,
133 wwin->wm_class);
134 XFreeStringList(argv);
138 static void
139 saveIconNameFor(char *iconPath, char *wm_instance, char *wm_class)
141 proplist_t dict = WDWindowAttributes->dictionary;
142 proplist_t adict, key, iconk;
143 proplist_t val;
144 char *tmp;
145 int i;
147 i = 0;
148 if (wm_instance)
149 i += strlen(wm_instance);
150 if (wm_class)
151 i += strlen(wm_class);
153 tmp = wmalloc(i+8);
154 *tmp = 0;
155 if (wm_class && wm_instance) {
156 sprintf(tmp, "%s.%s", wm_instance, wm_class);
157 } else {
158 if (wm_instance)
159 strcat(tmp, wm_instance);
160 if (wm_class)
161 strcat(tmp, wm_class);
164 key = PLMakeString(tmp);
165 free(tmp);
166 adict = PLGetDictionaryEntry(dict, key);
168 iconk = PLMakeString("Icon");
170 if (adict) {
171 val = PLGetDictionaryEntry(adict, iconk);
172 } else {
173 /* no dictionary for app, so create one */
174 adict = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
175 PLInsertDictionaryEntry(dict, key, adict);
176 PLRelease(adict);
177 val = NULL;
179 if (!val) {
180 val = PLMakeString(iconPath);
181 PLInsertDictionaryEntry(adict, iconk, val);
182 PLRelease(val);
184 PLRelease(key);
186 if (val && !wPreferences.flags.noupdates)
187 PLSave(dict, YES);
191 void
192 wApplicationExtractDirPackIcon(WScreen *scr, char *path,
193 char *wm_instance, char *wm_class)
195 char *iconPath=NULL;
196 /* Maybe the app is a .app and it has an icon in it, like
197 * /usr/local/GNUstep/Apps/WPrefs.app/WPrefs.tiff
199 if (strstr(path, ".app")) {
200 char *tmp;
202 tmp = wmalloc(strlen(path)+16);
204 if (scr->flags.supports_tiff) {
205 strcpy(tmp, path);
206 strcat(tmp, ".tiff");
207 if (access(tmp, R_OK)==0)
208 iconPath = tmp;
210 if (!path) {
211 strcpy(tmp, path);
212 strcat(tmp, ".xpm");
213 if (access(tmp, R_OK)==0)
214 iconPath = tmp;
216 if (!iconPath)
217 free(tmp);
220 if (iconPath) {
221 saveIconNameFor(iconPath, wm_instance, wm_class);
223 free(iconPath);
228 static Bool
229 extractClientIcon(WAppIcon *icon)
231 char *path;
233 path = wIconStore(icon->icon);
234 if (!path)
235 return False;
237 saveIconNameFor(path, icon->wm_instance, icon->wm_class);
239 free(path);
241 return True;
245 WApplication*
246 wApplicationCreate(WScreen *scr, Window main_window)
248 WApplication *wapp;
249 WWindow *leader;
251 if (main_window==None || main_window==scr->root_win) {
252 #ifdef DEBUG0
253 wwarning("trying to create application for %x",(unsigned)main_window);
254 #endif
255 return NULL;
259 Window root;
260 int foo;
261 unsigned int bar;
262 /* check if the window is valid */
263 if (!XGetGeometry(dpy, main_window, &root, &foo, &foo, &bar, &bar,
264 &bar, &bar)) {
265 return NULL;
269 wapp = wApplicationOf(main_window);
270 if (wapp) {
271 wapp->refcount++;
272 return wapp;
275 wapp = wmalloc(sizeof(WApplication));
276 memset(wapp, 0, sizeof(WApplication));
278 wapp->refcount = 1;
279 wapp->last_focused = NULL;
281 wapp->last_workspace = 0;
283 wapp->main_window = main_window;
284 wapp->main_window_desc = makeMainWindow(scr, main_window);
285 if (!wapp->main_window_desc) {
286 free(wapp);
287 return NULL;
290 extractIcon(wapp->main_window_desc);
292 leader = wWindowFor(main_window);
293 if (leader) {
294 leader->main_window = main_window;
296 wapp->menu = wAppMenuGet(scr, main_window);
297 #ifdef USER_MENU
298 if (!wapp->menu) wapp->menu = wUserMenuGet(scr, wapp->main_window_desc);
299 #endif /* USER_MENU */
303 * Set application wide attributes from the leader.
305 wapp->flags.hidden = WFLAGP(wapp->main_window_desc, start_hidden);
307 wapp->flags.emulated = WFLAGP(wapp->main_window_desc, emulate_appicon);
309 /* application descriptor */
310 XSaveContext(dpy, main_window, wAppWinContext, (XPointer)wapp);
312 if (!WFLAGP(wapp->main_window_desc, no_appicon)) {
313 wapp->app_icon = NULL;
314 if (scr->last_dock)
315 wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
316 /* check main dock if we did not find it in last dock */
317 if (!wapp->app_icon && scr->dock) {
318 wapp->app_icon = findDockIconFor(scr->dock, main_window);
320 /* finally check clips */
321 if (!wapp->app_icon) {
322 int i;
323 for (i=0; i<scr->workspace_count; i++) {
324 WDock *dock = scr->workspaces[i]->clip;
325 if (dock)
326 wapp->app_icon = findDockIconFor(dock, main_window);
327 if (wapp->app_icon)
328 break;
332 if (wapp->app_icon) {
333 WWindow *mainw = wapp->main_window_desc;
335 wapp->app_icon->running = 1;
336 wapp->app_icon->icon->force_paint = 1;
337 wapp->app_icon->icon->owner = mainw;
338 if (mainw->wm_hints && (mainw->wm_hints->flags&IconWindowHint))
339 wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
340 wAppIconPaint(wapp->app_icon);
341 } else {
342 wapp->app_icon = wAppIconCreate(wapp->main_window_desc);
343 #ifdef REDUCE_APPICONS
344 /* This is so we get the appearance of invoking the app sitting
345 * on the dock. -cls */
346 if (wapp->app_icon) {
347 if (wapp->app_icon->docked && wapp->app_icon->num_apps == 1) {
348 wapp->app_icon->launching = 0;
349 wapp->app_icon->running = 1;
350 wapp->app_icon->icon->force_paint = 1;
351 wAppIconPaint(wapp->app_icon);
354 #endif
356 } else {
357 wapp->app_icon = NULL;
360 if (wapp->app_icon) {
361 wapp->app_icon->main_window = main_window;
364 #ifndef REDUCE_APPICONS
365 if (wapp->app_icon && !wapp->app_icon->docked) {
366 #else
367 if (wapp->app_icon && !wapp->app_icon->docked && wapp->app_icon->num_apps == 1) {
368 #ifdef THIS_SUCKS
370 #endif
371 #endif
372 WIcon *icon = wapp->app_icon->icon;
373 WDock *clip = scr->workspaces[scr->current_workspace]->clip;
374 int x=0, y=0;
376 if (clip && clip->attract_icons && wDockFindFreeSlot(clip, &x, &y)) {
377 wapp->app_icon->attracted = 1;
378 if (!clip->keep_attracted && !wapp->app_icon->icon->shadowed) {
379 wapp->app_icon->icon->shadowed = 1;
380 wapp->app_icon->icon->force_paint = 1;
381 /* We don't do an wAppIconPaint() here because it's in
382 * wDockAttachIcon(). -Dan.
385 wDockAttachIcon(clip, wapp->app_icon, x, y);
386 } else {
387 PlaceIcon(scr, &x, &y);
388 wAppIconMove(wapp->app_icon, x, y);
389 wLowerFrame(icon->core);
391 if (!clip || !wapp->app_icon->attracted || !clip->collapsed)
392 XMapWindow(dpy, icon->core->window);
395 if (wPreferences.auto_arrange_icons && wapp->app_icon && !wapp->app_icon->attracted) {
396 wArrangeIcons(scr, True);
399 if (wapp->app_icon) {
400 char *tmp;
402 /* if the displayed icon was supplied by the client, save the icon */
403 tmp = wDefaultGetIconFile(scr, wapp->app_icon->wm_instance,
404 wapp->app_icon->wm_class, True);
405 if (!tmp)
406 extractClientIcon(wapp->app_icon);
409 wapp->prev = NULL;
410 wapp->next = scr->wapp_list;
411 if (scr->wapp_list)
412 scr->wapp_list->prev = wapp;
413 scr->wapp_list = wapp;
415 #ifdef WMSOUND
416 wSoundPlay(WMSOUND_APPSTART);
417 #endif
419 #ifdef DEBUG
420 printf("Created application for %x\n", (unsigned)main_window);
421 #endif
422 return wapp;
426 void
427 wApplicationDestroy(WApplication *wapp)
429 Window main_window;
430 WWindow *wwin;
431 WScreen *scr;
432 #ifdef REDUCE_APPICONS
433 unsigned int napps;
434 #endif
436 if (!wapp)
437 return;
439 wapp->refcount--;
440 if (wapp->refcount>0)
441 return;
444 scr = wapp->main_window_desc->screen_ptr;
445 main_window = wapp->main_window;
446 #ifdef REDUCE_APPICONS
447 napps = wAppIconReduceAppCount(wapp);
448 #endif
450 if (wapp == scr->wapp_list) {
451 if (wapp->next)
452 wapp->next->prev = NULL;
453 scr->wapp_list = wapp->next;
454 } else {
455 if (wapp->next)
456 wapp->next->prev = wapp->prev;
457 if (wapp->prev)
458 wapp->prev->next = wapp->next;
461 XDeleteContext(dpy, wapp->main_window, wAppWinContext);
462 wAppMenuDestroy(wapp->menu);
463 if (wapp->app_icon) {
464 if (wapp->app_icon->docked
465 && (!wapp->app_icon->attracted ||
466 wapp->app_icon->dock->keep_attracted)) {
467 #ifdef REDUCE_APPICONS
468 if (napps == 0) {
469 #endif
470 wapp->app_icon->running = 0;
471 /* since we keep it, we don't care if it was attracted or not */
472 wapp->app_icon->attracted = 0;
473 wapp->app_icon->icon->shadowed = 0;
474 wapp->app_icon->main_window = None;
475 wapp->app_icon->pid = 0;
476 wapp->app_icon->icon->owner = NULL;
477 wapp->app_icon->icon->icon_win = None;
478 wapp->app_icon->icon->force_paint = 1;
479 wAppIconPaint(wapp->app_icon);
480 #ifdef REDUCE_APPICONS
482 #endif
483 } else if (wapp->app_icon->docked) {
484 #ifdef REDUCE_APPICONS
485 if (napps == 0) {
486 #endif
487 wapp->app_icon->running = 0;
488 wDockDetach(wapp->app_icon->dock, wapp->app_icon);
489 #ifdef REDUCE_APPICONS
491 #endif
492 } else {
493 #ifdef REDUCE_APPICONS
494 if (napps == 0) {
495 #endif
496 wAppIconDestroy(wapp->app_icon);
497 #ifdef REDUCE_APPICONS
499 #endif
502 wwin = wWindowFor(wapp->main_window_desc->client_win);
504 wWindowDestroy(wapp->main_window_desc);
505 if (wwin) {
506 /* undelete client window context that was deleted in
507 * wWindowDestroy */
508 XSaveContext(dpy, wwin->client_win, wWinContext,
509 (XPointer)&wwin->client_descriptor);
511 free(wapp);
513 #ifdef DEBUG
514 printf("Destroyed application for %x\n", (unsigned)main_window);
515 #endif
516 if (wPreferences.auto_arrange_icons) {
517 wArrangeIcons(scr, True);
520 #ifdef WMSOUND
521 wSoundPlay(WMSOUND_APPEXIT);
522 #endif