2 * Window Maker window manager
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 * Copyright (c) 1998 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,
29 #include <sys/types.h>
34 #include "WindowMaker.h"
39 #endif /* USER_MENU */
42 #include "application.h"
44 #include "properties.h"
49 #include "workspace.h"
54 /******** Global variables ********/
56 extern XContext wAppWinContext
;
57 extern XContext wWinContext
;
58 extern WPreferences wPreferences
;
60 extern WDDomain
*WDWindowAttributes
;
62 /******** Local variables ********/
66 makeMainWindow(WScreen
*scr
, Window window
)
69 XWindowAttributes attr
;
71 if (!XGetWindowAttributes(dpy
, window
, &attr
)) {
75 wwin
= wWindowCreate();
76 wwin
->screen_ptr
= scr
;
77 wwin
->client_win
= window
;
78 wwin
->main_window
= window
;
79 wwin
->wm_hints
= XGetWMHints(dpy
, window
);
80 /* if (!MyXFetchName(dpy, window, &(wwin->frame->title))) {
81 wwin->frame->title = NULL;
84 PropGetWMClass(window
, &wwin
->wm_class
, &wwin
->wm_instance
);
87 wDefaultFillAttributes(scr
, wwin
->wm_instance
, wwin
->wm_class
,
88 &wwin
->user_flags
, &wwin
->defined_user_flags
, True
);
90 XSelectInput(dpy
, window
, attr
.your_event_mask
| PropertyChangeMask
91 | StructureNotifyMask
);
97 wApplicationOf(Window window
)
103 if (XFindContext(dpy
, window
, wAppWinContext
, (XPointer
*)&wapp
)!=XCSUCCESS
)
110 findDockIconFor(WDock
*dock
, Window main_window
)
112 WAppIcon
*aicon
= NULL
;
114 aicon
= wDockFindIconForWindow(dock
, main_window
);
116 wDockTrackWindowLaunch(dock
, main_window
);
117 aicon
= wDockFindIconForWindow(dock
, main_window
);
124 extractIcon(WWindow
*wwin
)
129 if (!XGetCommand(dpy
, wwin
->client_win
, &argv
, &argc
) || argc
< 1)
132 wApplicationExtractDirPackIcon(wwin
->screen_ptr
,argv
[0],
135 XFreeStringList(argv
);
140 saveIconNameFor(char *iconPath
, char *wm_instance
, char *wm_class
)
142 proplist_t dict
= WDWindowAttributes
->dictionary
;
143 proplist_t adict
, key
, iconk
;
150 i
+= strlen(wm_instance
);
152 i
+= strlen(wm_class
);
156 if (wm_class
&& wm_instance
) {
157 sprintf(tmp
, "%s.%s", wm_instance
, wm_class
);
160 strcat(tmp
, wm_instance
);
162 strcat(tmp
, wm_class
);
165 key
= PLMakeString(tmp
);
167 adict
= PLGetDictionaryEntry(dict
, key
);
169 iconk
= PLMakeString("Icon");
172 val
= PLGetDictionaryEntry(adict
, iconk
);
174 /* no dictionary for app, so create one */
175 adict
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
176 PLInsertDictionaryEntry(dict
, key
, adict
);
181 val
= PLMakeString(iconPath
);
182 PLInsertDictionaryEntry(adict
, iconk
, val
);
188 if (val
&& !wPreferences
.flags
.noupdates
)
194 wApplicationExtractDirPackIcon(WScreen
*scr
, char *path
,
195 char *wm_instance
, char *wm_class
)
198 /* Maybe the app is a .app and it has an icon in it, like
199 * /usr/local/GNUstep/Apps/WPrefs.app/WPrefs.tiff
201 if (strstr(path
, ".app")) {
204 tmp
= wmalloc(strlen(path
)+16);
206 if (scr
->flags
.supports_tiff
) {
208 strcat(tmp
, ".tiff");
209 if (access(tmp
, R_OK
)==0)
215 if (access(tmp
, R_OK
)==0)
223 saveIconNameFor(iconPath
, wm_instance
, wm_class
);
231 extractClientIcon(WAppIcon
*icon
)
235 path
= wIconStore(icon
->icon
);
239 saveIconNameFor(path
, icon
->wm_instance
, icon
->wm_class
);
248 wApplicationCreate(WScreen
*scr
, Window main_window
)
253 if (main_window
==None
|| main_window
==scr
->root_win
) {
255 wwarning("trying to create application for %x",(unsigned)main_window
);
264 /* check if the window is valid */
265 if (!XGetGeometry(dpy
, main_window
, &root
, &foo
, &foo
, &bar
, &bar
,
271 wapp
= wApplicationOf(main_window
);
277 wapp
= wmalloc(sizeof(WApplication
));
278 memset(wapp
, 0, sizeof(WApplication
));
281 wapp
->last_focused
= NULL
;
283 wapp
->last_workspace
= 0;
285 wapp
->main_window
= main_window
;
286 wapp
->main_window_desc
= makeMainWindow(scr
, main_window
);
287 if (!wapp
->main_window_desc
) {
292 extractIcon(wapp
->main_window_desc
);
294 leader
= wWindowFor(main_window
);
296 leader
->main_window
= main_window
;
298 wapp
->menu
= wAppMenuGet(scr
, main_window
);
300 if (!wapp
->menu
) wapp
->menu
= wUserMenuGet(scr
, wapp
->main_window_desc
);
301 #endif /* USER_MENU */
305 WApplication
*tmp
= scr
->wapp_list
;
310 /* append to app list */
312 scr
->wapp_list
= wapp
;
324 * Set application wide attributes from the leader.
326 wapp
->flags
.hidden
= WFLAGP(wapp
->main_window_desc
, start_hidden
);
328 wapp
->flags
.emulated
= WFLAGP(wapp
->main_window_desc
, emulate_appicon
);
330 /* application descriptor */
331 XSaveContext(dpy
, main_window
, wAppWinContext
, (XPointer
)wapp
);
333 if (!WFLAGP(wapp
->main_window_desc
, no_appicon
)) {
334 wapp
->app_icon
= NULL
;
336 wapp
->app_icon
= findDockIconFor(scr
->last_dock
, main_window
);
337 /* check main dock if we did not find it in last dock */
338 if (!wapp
->app_icon
&& scr
->dock
) {
339 wapp
->app_icon
= findDockIconFor(scr
->dock
, main_window
);
341 /* finally check clips */
342 if (!wapp
->app_icon
) {
344 for (i
=0; i
<scr
->workspace_count
; i
++) {
345 WDock
*dock
= scr
->workspaces
[i
]->clip
;
347 wapp
->app_icon
= findDockIconFor(dock
, main_window
);
353 if (wapp
->app_icon
) {
354 WWindow
*mainw
= wapp
->main_window_desc
;
356 wapp
->app_icon
->running
= 1;
357 wapp
->app_icon
->icon
->force_paint
= 1;
358 wapp
->app_icon
->icon
->owner
= mainw
;
359 if (mainw
->wm_hints
&& (mainw
->wm_hints
->flags
&IconWindowHint
))
360 wapp
->app_icon
->icon
->icon_win
= mainw
->wm_hints
->icon_window
;
361 wAppIconPaint(wapp
->app_icon
);
363 wapp
->app_icon
= wAppIconCreate(wapp
->main_window_desc
);
366 wapp
->app_icon
= NULL
;
369 if (wapp
->app_icon
) {
370 wapp
->app_icon
->main_window
= main_window
;
373 if (wapp
->app_icon
&& !wapp
->app_icon
->docked
) {
374 WIcon
*icon
= wapp
->app_icon
->icon
;
375 WDock
*clip
= scr
->workspaces
[scr
->current_workspace
]->clip
;
378 if (clip
&& clip
->attract_icons
&& wDockFindFreeSlot(clip
, &x
, &y
)) {
379 wapp
->app_icon
->attracted
= 1;
380 if (!wapp
->app_icon
->icon
->shadowed
) {
381 wapp
->app_icon
->icon
->shadowed
= 1;
382 wapp
->app_icon
->icon
->force_paint
= 1;
383 /* We don't do an wAppIconPaint() here because it's in
384 * wDockAttachIcon(). -Dan.
387 wDockAttachIcon(clip
, wapp
->app_icon
, x
, y
);
389 PlaceIcon(scr
, &x
, &y
);
390 wAppIconMove(wapp
->app_icon
, x
, y
);
391 wLowerFrame(icon
->core
);
393 if (!clip
|| !wapp
->app_icon
->attracted
|| !clip
->collapsed
)
394 XMapWindow(dpy
, icon
->core
->window
);
397 if (wPreferences
.auto_arrange_icons
&& wapp
->app_icon
&& !wapp
->app_icon
->attracted
) {
398 wArrangeIcons(scr
, True
);
401 if (wapp
->app_icon
) {
406 tmp
= wDefaultGetIconFile(scr
, wapp
->app_icon
->wm_instance
,
407 wapp
->app_icon
->wm_class
, True
);
409 /* If the icon was saved by us from the client supplied icon, but is
410 * missing, recreate it. */
411 if (tmp
&& strstr(tmp
, ".AppInfo/WindowMaker")!=NULL
&&
412 stat(tmp
, &dummy
)!=0 && errno
==ENOENT
) {
413 wmessage(_("recreating missing icon '%s'"), tmp
);
414 path
= wIconStore(wapp
->app_icon
->icon
);
418 image
= wDefaultGetImage(scr
, wapp
->app_icon
->wm_instance
,
419 wapp
->app_icon
->wm_class
);
421 wIconChangeImage(wapp
->app_icon
->icon
, image
);
422 wAppIconPaint(wapp
->app_icon
);
424 * wIconChangeImage() should be rewriten to use retain/release
425 * The way it is now is too confusing about where the icon is
426 * finally released. -Dan */
427 /* --this is wrong at the moment-- RReleaseImage(image);*/
431 /* if the displayed icon was supplied by the client, save the icon */
433 extractClientIcon(wapp
->app_icon
);
436 wSoundPlay(WSOUND_APPSTART
);
439 printf("Created application for %x\n", (unsigned)main_window
);
446 wApplicationDestroy(WApplication
*wapp
)
456 if (wapp
->refcount
>0)
460 scr
= wapp
->main_window_desc
->screen_ptr
;
461 main_window
= wapp
->main_window
;
463 if (wapp
== scr
->wapp_list
) {
465 wapp
->next
->prev
= NULL
;
466 scr
->wapp_list
= wapp
->next
;
469 wapp
->next
->prev
= wapp
->prev
;
471 wapp
->prev
->next
= wapp
->next
;
474 XDeleteContext(dpy
, wapp
->main_window
, wAppWinContext
);
475 wAppMenuDestroy(wapp
->menu
);
476 if (wapp
->app_icon
) {
477 if (wapp
->app_icon
->docked
&& !wapp
->app_icon
->attracted
) {
478 wapp
->app_icon
->running
= 0;
479 /* since we keep it, we don't care if it was attracted or not */
480 wapp
->app_icon
->attracted
= 0;
481 wapp
->app_icon
->icon
->shadowed
= 0;
482 wapp
->app_icon
->main_window
= None
;
483 wapp
->app_icon
->pid
= 0;
484 wapp
->app_icon
->icon
->owner
= NULL
;
485 wapp
->app_icon
->icon
->icon_win
= None
;
486 wapp
->app_icon
->icon
->force_paint
= 1;
487 wAppIconPaint(wapp
->app_icon
);
488 } else if (wapp
->app_icon
->docked
) {
489 wapp
->app_icon
->running
= 0;
490 wDockDetach(wapp
->app_icon
->dock
, wapp
->app_icon
);
492 wAppIconDestroy(wapp
->app_icon
);
495 wwin
= wWindowFor(wapp
->main_window_desc
->client_win
);
497 wWindowDestroy(wapp
->main_window_desc
);
499 /* undelete client window context that was deleted in
501 XSaveContext(dpy
, wwin
->client_win
, wWinContext
,
502 (XPointer
)&wwin
->client_descriptor
);
507 printf("Destroyed application for %x\n", (unsigned)main_window
);
509 if (wPreferences
.auto_arrange_icons
) {
510 wArrangeIcons(scr
, True
);
513 wSoundPlay(WSOUND_APPEXIT
);
519 wApplicationSetCollapse(WApplication
*app
, Bool flag
)
521 WApplication
*list
= app
->main_window_desc
->screen_ptr
->wapp_list
;
523 WWindow
*wwin
= app
->main_window_desc
;
525 if (WFLAGP(app
->main_window_desc
, collapse_appicons
) == flag
)
530 if (strcmp(wwin
->wm_instance
,
531 list
->main_window_desc
->wm_instance
) == 0
533 strcmp(wwin
->wm_class
,
534 list
->main_window_desc
->wm_class
) == 0)
535 WSETUFLAG(list
->main_window_desc
, collapse_appicons
, flag
);
540 if (app
->app_icon
&& flag
)
541 wAppIconMove(app
->app_icon
, app
->app_icon
->x_pos
, app
->app_icon
->y_pos
);
548 * Returns index number of the app in case there are more than
549 * one instance of the same class/name.
552 wApplicationIndexOfGroup(WApplication
*app
)
554 WApplication
*list
= app
->main_window_desc
->screen_ptr
->wapp_list
;
556 WWindow
*wwin
= app
->main_window_desc
;
558 if (!WFLAGP(wwin, collapse_appicons))
566 if (strcmp(wwin
->wm_instance
,
567 list
->main_window_desc
->wm_instance
) == 0
569 strcmp(wwin
->wm_class
,
570 list
->main_window_desc
->wm_class
) == 0)
576 puts("OH SHIT!?!?!? HOW THE FUCK DID WE GET HERE!?!?!?!?!");