Remove code duplication by calling get_name_for_instance_class()
[wmaker-crm.git] / src / application.c
blobca47203109a7684a4844ccab0c16af12aa9b4709
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>
25 #include <string.h>
26 #include <unistd.h>
28 #include "WindowMaker.h"
29 #include "menu.h"
30 #include "window.h"
31 #ifdef USER_MENU
32 #include "usermenu.h"
33 #endif /* USER_MENU */
34 #include "appicon.h"
35 #include "application.h"
36 #include "appmenu.h"
37 #include "properties.h"
38 #include "stacking.h"
39 #include "actions.h"
40 #include "workspace.h"
41 #include "dock.h"
43 /******** Global variables ********/
45 extern XContext wAppWinContext;
46 extern XContext wWinContext;
47 extern WPreferences wPreferences;
48 extern WDDomain *WDWindowAttributes;
50 /******** Local variables ********/
52 static WWindow *makeMainWindow(WScreen * scr, Window window)
54 WWindow *wwin;
55 XWindowAttributes attr;
57 if (!XGetWindowAttributes(dpy, window, &attr))
58 return NULL;
60 wwin = wWindowCreate();
61 wwin->screen_ptr = scr;
62 wwin->client_win = window;
63 wwin->main_window = window;
64 wwin->wm_hints = XGetWMHints(dpy, window);
66 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
68 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
69 &wwin->user_flags, &wwin->defined_user_flags, True);
71 XSelectInput(dpy, window, attr.your_event_mask | PropertyChangeMask | StructureNotifyMask);
72 return wwin;
75 WApplication *wApplicationOf(Window window)
77 WApplication *wapp;
79 if (window == None)
80 return NULL;
81 if (XFindContext(dpy, window, wAppWinContext, (XPointer *) & wapp) != XCSUCCESS)
82 return NULL;
83 return wapp;
86 static WAppIcon *findDockIconFor(WDock * dock, Window main_window)
88 WAppIcon *aicon = NULL;
90 aicon = wDockFindIconForWindow(dock, main_window);
91 if (!aicon) {
92 wDockTrackWindowLaunch(dock, main_window);
93 aicon = wDockFindIconForWindow(dock, main_window);
95 return aicon;
98 static void extractIcon(WWindow * wwin)
100 char *progname;
102 /* Get the application name */
103 progname = GetProgramNameForWindow(wwin->client_win);
104 if (progname) {
105 /* Save the icon path if the application is ".app" */
106 wApplicationExtractDirPackIcon(wwin->screen_ptr, progname, wwin->wm_instance, wwin->wm_class);
107 wfree(progname);
111 void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, char *wm_class)
113 WMPropList *dict = WDWindowAttributes->dictionary;
114 WMPropList *adict, *key, *iconk;
115 WMPropList *val;
116 char *tmp;
118 tmp = get_name_for_instance_class(wm_instance, wm_class);
119 key = WMCreatePLString(tmp);
120 wfree(tmp);
122 adict = WMGetFromPLDictionary(dict, key);
123 iconk = WMCreatePLString("Icon");
125 if (adict) {
126 val = WMGetFromPLDictionary(adict, iconk);
127 } else {
128 /* no dictionary for app, so create one */
129 adict = WMCreatePLDictionary(NULL, NULL);
130 WMPutInPLDictionary(dict, key, adict);
131 WMReleasePropList(adict);
132 val = NULL;
135 if (!val) {
136 val = WMCreatePLString(iconPath);
137 WMPutInPLDictionary(adict, iconk, val);
138 WMReleasePropList(val);
139 } else {
140 val = NULL;
143 WMReleasePropList(key);
144 WMReleasePropList(iconk);
146 if (val && !wPreferences.flags.noupdates)
147 UpdateDomainFile(WDWindowAttributes);
150 /* This function is used if the application is a .app. It checks if it has an icon in it
151 * like for example /usr/local/GNUstep/Applications/WPrefs.app/WPrefs.tiff
153 void wApplicationExtractDirPackIcon(WScreen * scr, char *path, char *wm_instance, char *wm_class)
155 char *iconPath = NULL;
156 char *tmp = NULL;
158 if (strstr(path, ".app")) {
159 tmp = wmalloc(strlen(path) + 16);
161 if (scr->flags.supports_tiff) {
162 strcpy(tmp, path);
163 strcat(tmp, ".tiff");
164 if (access(tmp, R_OK) == 0)
165 iconPath = tmp;
168 if (!iconPath) {
169 strcpy(tmp, path);
170 strcat(tmp, ".xpm");
171 if (access(tmp, R_OK) == 0)
172 iconPath = tmp;
175 if (!iconPath)
176 wfree(tmp);
178 if (iconPath) {
179 wApplicationSaveIconPathFor(iconPath, wm_instance, wm_class);
180 wfree(iconPath);
185 static void app_icon_create_from_docks(WWindow *wwin, WApplication *wapp, Window main_window)
187 WScreen *scr = wwin->screen_ptr;
189 if (scr->last_dock)
190 wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
192 /* check main dock if we did not find it in last dock */
193 if (!wapp->app_icon && scr->dock)
194 wapp->app_icon = findDockIconFor(scr->dock, main_window);
196 /* finally check clips */
197 if (!wapp->app_icon) {
198 int i;
199 for (i = 0; i < scr->workspace_count; i++) {
200 WDock *dock = scr->workspaces[i]->clip;
201 if (dock)
202 wapp->app_icon = findDockIconFor(dock, main_window);
203 if (wapp->app_icon)
204 break;
208 /* If created, then set some flags */
209 if (wapp->app_icon) {
210 WWindow *mainw = wapp->main_window_desc;
212 wapp->app_icon->running = 1;
213 wapp->app_icon->icon->force_paint = 1;
214 wapp->app_icon->icon->owner = mainw;
215 if (mainw->wm_hints && (mainw->wm_hints->flags & IconWindowHint))
216 wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
218 wAppIconPaint(wapp->app_icon);
219 wAppIconSave(wapp->app_icon);
220 } else {
221 wapp->app_icon = wAppIconCreate(wapp->main_window_desc);
225 WApplication *wApplicationCreate(WWindow * wwin)
227 WScreen *scr = wwin->screen_ptr;
228 Window main_window = wwin->main_window;
229 WApplication *wapp;
230 WWindow *leader;
232 if (main_window == None || main_window == scr->root_win)
233 return NULL;
236 Window root;
237 int foo;
238 unsigned int bar;
239 /* check if the window is valid */
240 if (!XGetGeometry(dpy, main_window, &root, &foo, &foo, &bar, &bar, &bar, &bar))
241 return NULL;
244 wapp = wApplicationOf(main_window);
245 if (wapp) {
246 wapp->refcount++;
247 if (wapp->app_icon && wapp->app_icon->docked &&
248 wapp->app_icon->relaunching && wapp->main_window_desc->fake_group)
249 wDockFinishLaunch(wapp->app_icon->dock, wapp->app_icon);
251 return wapp;
254 wapp = wmalloc(sizeof(WApplication));
256 wapp->refcount = 1;
257 wapp->last_focused = NULL;
258 wapp->urgent_bounce_timer = NULL;
260 wapp->last_workspace = 0;
262 wapp->main_window = main_window;
263 wapp->main_window_desc = makeMainWindow(scr, main_window);
264 if (!wapp->main_window_desc) {
265 wfree(wapp);
266 return NULL;
269 wapp->main_window_desc->fake_group = wwin->fake_group;
270 wapp->main_window_desc->net_icon_image = RRetainImage(wwin->net_icon_image);
272 extractIcon(wapp->main_window_desc);
274 leader = wWindowFor(main_window);
275 if (leader)
276 leader->main_window = main_window;
278 wapp->menu = wAppMenuGet(scr, main_window);
279 #ifdef USER_MENU
280 if (!wapp->menu)
281 wapp->menu = wUserMenuGet(scr, wapp->main_window_desc);
282 #endif /* USER_MENU */
285 * Set application wide attributes from the leader.
287 wapp->flags.hidden = WFLAGP(wapp->main_window_desc, start_hidden);
288 wapp->flags.emulated = WFLAGP(wapp->main_window_desc, emulate_appicon);
290 /* application descriptor */
291 XSaveContext(dpy, main_window, wAppWinContext, (XPointer) wapp);
293 /* Create the application icon */
294 wapp->app_icon = NULL;
295 if (!WFLAGP(wapp->main_window_desc, no_appicon)) {
296 /* Create the application icon using the icon from docks
297 * If not found in docks, create a new icon
298 * using the function wAppIconCreate() */
299 app_icon_create_from_docks(wwin, wapp, main_window);
301 /* Now, paint the icon */
302 paint_app_icon(wapp);
304 /* Save the app_icon in a file */
305 save_app_icon(wwin, wapp);
308 return wapp;
311 void wApplicationDestroy(WApplication * wapp)
313 WWindow *wwin;
314 WScreen *scr;
316 if (!wapp)
317 return;
319 wapp->refcount--;
320 if (wapp->refcount > 0)
321 return;
323 if (wapp->urgent_bounce_timer) {
324 WMDeleteTimerHandler(wapp->urgent_bounce_timer);
325 wapp->urgent_bounce_timer = NULL;
327 if (wapp->flags.bouncing) {
328 /* event.c:handleDestroyNotify forced this destroy
329 and thereby overlooked the bounce callback */
330 wapp->refcount = 1;
331 return;
334 scr = wapp->main_window_desc->screen_ptr;
336 if (wapp == scr->wapp_list) {
337 if (wapp->next)
338 wapp->next->prev = NULL;
339 scr->wapp_list = wapp->next;
340 } else {
341 if (wapp->next)
342 wapp->next->prev = wapp->prev;
343 if (wapp->prev)
344 wapp->prev->next = wapp->next;
347 XDeleteContext(dpy, wapp->main_window, wAppWinContext);
348 wAppMenuDestroy(wapp->menu);
349 wApplicationDeactivate(wapp);
351 /* Remove application icon */
352 removeAppIconFor(wapp);
354 wwin = wWindowFor(wapp->main_window_desc->client_win);
356 wWindowDestroy(wapp->main_window_desc);
357 if (wwin) {
358 /* undelete client window context that was deleted in
359 * wWindowDestroy */
360 XSaveContext(dpy, wwin->client_win, wWinContext, (XPointer) & wwin->client_descriptor);
362 wfree(wapp);
365 void wApplicationActivate(WApplication *wapp)
367 #ifdef NEWAPPICON
368 if (wapp->app_icon) {
369 wIconSetHighlited(wapp->app_icon->icon, True);
370 wAppIconPaint(wapp->app_icon);
372 #endif
375 void wApplicationDeactivate(WApplication *wapp)
377 #ifdef NEWAPPICON
378 if (wapp->app_icon) {
379 wIconSetHighlited(wapp->app_icon->icon, False);
380 wAppIconPaint(wapp->app_icon);
382 #endif