NEWS: Add note about new global defaults directory configuration.
[wmaker-crm.git] / src / application.c
blob6ff3d3fcdf4caba1b466c340c985fca216f90d28
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>
24 #include <string.h>
26 #include "WindowMaker.h"
27 #include "menu.h"
28 #include "window.h"
29 #ifdef USER_MENU
30 #include "usermenu.h"
31 #endif /* USER_MENU */
32 #include "appicon.h"
33 #include "application.h"
34 #include "appmenu.h"
35 #include "properties.h"
36 #include "workspace.h"
37 #include "dock.h"
38 #include "defaults.h"
41 /******** Local variables ********/
43 static WWindow *makeMainWindow(WScreen * scr, Window window)
45 WWindow *wwin;
46 XWindowAttributes attr;
48 if (!XGetWindowAttributes(dpy, window, &attr))
49 return NULL;
51 wwin = wWindowCreate();
52 wwin->screen_ptr = scr;
53 wwin->client_win = window;
54 wwin->main_window = window;
55 wwin->wm_hints = XGetWMHints(dpy, window);
57 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
59 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->user_flags,
60 &wwin->defined_user_flags, True);
62 XSelectInput(dpy, window, attr.your_event_mask | PropertyChangeMask | StructureNotifyMask);
63 return wwin;
66 WApplication *wApplicationOf(Window window)
68 WApplication *wapp;
70 if (window == None)
71 return NULL;
72 if (XFindContext(dpy, window, w_global.context.app_win, (XPointer *) & wapp) != XCSUCCESS)
73 return NULL;
74 return wapp;
77 WApplication *wApplicationCreate(WWindow * wwin)
79 WScreen *scr = wwin->screen_ptr;
80 Window main_window = wwin->main_window;
81 WApplication *wapp;
82 WWindow *leader;
84 if (main_window == None || main_window == scr->root_win)
85 return NULL;
88 Window root;
89 int foo;
90 unsigned int bar;
91 /* check if the window is valid */
92 if (!XGetGeometry(dpy, main_window, &root, &foo, &foo, &bar, &bar, &bar, &bar))
93 return NULL;
96 wapp = wApplicationOf(main_window);
97 if (wapp) {
98 wapp->refcount++;
99 if (wapp->app_icon && wapp->app_icon->docked &&
100 wapp->app_icon->relaunching && wapp->main_window_desc->fake_group)
101 wDockFinishLaunch(wapp->app_icon);
103 return wapp;
106 wapp = wmalloc(sizeof(WApplication));
108 wapp->refcount = 1;
109 wapp->last_focused = NULL;
110 wapp->urgent_bounce_timer = NULL;
112 wapp->last_workspace = 0;
114 wapp->main_window = main_window;
115 wapp->main_window_desc = makeMainWindow(scr, main_window);
116 if (!wapp->main_window_desc) {
117 wfree(wapp);
118 return NULL;
121 wapp->main_window_desc->fake_group = wwin->fake_group;
122 wapp->main_window_desc->net_icon_image = RRetainImage(wwin->net_icon_image);
124 leader = wWindowFor(main_window);
125 if (leader)
126 leader->main_window = main_window;
128 wapp->menu = wAppMenuGet(scr, main_window);
129 #ifdef USER_MENU
130 if (!wapp->menu)
131 wapp->menu = wUserMenuGet(scr, wapp->main_window_desc);
133 wAppMenuMap(wapp->menu, wwin);
134 #endif
136 /* Set application wide attributes from the leader */
137 wapp->flags.hidden = WFLAGP(wapp->main_window_desc, start_hidden);
138 wapp->flags.emulated = WFLAGP(wapp->main_window_desc, emulate_appicon);
140 /* application descriptor */
141 XSaveContext(dpy, main_window, w_global.context.app_win, (XPointer) wapp);
143 create_appicon_for_application(wapp, wwin);
145 return wapp;
148 void wApplicationDestroy(WApplication *wapp)
150 WWindow *wwin;
151 WScreen *scr;
153 if (!wapp)
154 return;
156 wapp->refcount--;
157 if (wapp->refcount > 0)
158 return;
160 if (wapp->urgent_bounce_timer) {
161 WMDeleteTimerHandler(wapp->urgent_bounce_timer);
162 wapp->urgent_bounce_timer = NULL;
164 if (wapp->flags.bouncing) {
165 /* event.c:handleDestroyNotify forced this destroy
166 and thereby overlooked the bounce callback */
167 wapp->refcount = 1;
168 return;
171 scr = wapp->main_window_desc->screen_ptr;
173 if (wapp == scr->wapp_list) {
174 if (wapp->next)
175 wapp->next->prev = NULL;
176 scr->wapp_list = wapp->next;
177 } else {
178 if (wapp->next)
179 wapp->next->prev = wapp->prev;
180 if (wapp->prev)
181 wapp->prev->next = wapp->next;
184 XDeleteContext(dpy, wapp->main_window, w_global.context.app_win);
185 wAppMenuDestroy(wapp->menu);
187 /* Remove application icon */
188 removeAppIconFor(wapp);
190 wwin = wWindowFor(wapp->main_window_desc->client_win);
192 wWindowDestroy(wapp->main_window_desc);
193 if (wwin) {
194 /* undelete client window context that was deleted in
195 * wWindowDestroy */
196 XSaveContext(dpy, wwin->client_win, w_global.context.client_win, (XPointer) & wwin->client_descriptor);
198 wfree(wapp);
201 void wApplicationActivate(WApplication *wapp)
203 if (wapp->app_icon) {
204 wIconSetHighlited(wapp->app_icon->icon, True);
205 wAppIconPaint(wapp->app_icon);
209 void wApplicationDeactivate(WApplication *wapp)
211 if (wapp->app_icon) {
212 wIconSetHighlited(wapp->app_icon->icon, False);
213 wAppIconPaint(wapp->app_icon);