Improve dockapp recognition
[wmaker-crm.git] / src / session.c
blob349e1d50262a4b060e3095e5d90ac502e41c43de
1 /* session.c - session state handling and R6 style session management
3 * Copyright (c) 1998-2003 Dan Pascu
4 * Copyright (c) 1998-2003 Alfredo Kojima
6 * Window Maker window manager
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
26 * If defined(XSMP_ENABLED) and session manager is running then
27 * do normal stuff
28 * else
29 * do pre-R6 session management stuff (save window state and relaunch)
31 * When doing a checkpoint:
33 * = Without XSMP
34 * Open "Stop"/status Dialog
35 * Send SAVE_YOURSELF to clients and wait for reply
36 * Save restart info
37 * Save state of clients
39 * = With XSMP
40 * Send checkpoint request to sm
42 * When exiting:
43 * -------------
45 * = Without XSMP
47 * Open "Exit Now"/status Dialog
48 * Send SAVE_YOURSELF to clients and wait for reply
49 * Save restart info
50 * Save state of clients
51 * Send DELETE to all clients
52 * When no more clients are left or user hit "Exit Now", exit
54 * = With XSMP
56 * Send Shutdown request to session manager
57 * if SaveYourself message received, save state of clients
58 * if the Die message is received, exit.
61 #include "wconfig.h"
63 #include <X11/Xlib.h>
64 #include <X11/Xutil.h>
66 #include <stdlib.h>
67 #include <stdio.h>
68 #include <string.h>
69 #include <unistd.h>
70 #include <time.h>
72 #include "WindowMaker.h"
73 #include "screen.h"
74 #include "window.h"
75 #include "client.h"
76 #include "session.h"
77 #include "framewin.h"
78 #include "workspace.h"
79 #include "funcs.h"
80 #include "properties.h"
81 #include "application.h"
82 #include "appicon.h"
83 #include "dock.h"
85 #include <WINGs/WUtil.h>
87 /** Global **/
89 extern Atom _XA_WM_SAVE_YOURSELF;
91 extern Time LastTimestamp;
93 static WMPropList *sApplications = NULL;
94 static WMPropList *sCommand;
95 static WMPropList *sName;
96 static WMPropList *sHost;
97 static WMPropList *sWorkspace;
98 static WMPropList *sShaded;
99 static WMPropList *sMiniaturized;
100 static WMPropList *sHidden;
101 static WMPropList *sGeometry;
102 static WMPropList *sShortcutMask;
104 static WMPropList *sDock;
105 static WMPropList *sYes, *sNo;
107 static void make_keys(void)
109 if (sApplications != NULL)
110 return;
112 sApplications = WMCreatePLString("Applications");
113 sCommand = WMCreatePLString("Command");
114 sName = WMCreatePLString("Name");
115 sHost = WMCreatePLString("Host");
116 sWorkspace = WMCreatePLString("Workspace");
117 sShaded = WMCreatePLString("Shaded");
118 sMiniaturized = WMCreatePLString("Miniaturized");
119 sHidden = WMCreatePLString("Hidden");
120 sGeometry = WMCreatePLString("Geometry");
121 sDock = WMCreatePLString("Dock");
122 sShortcutMask = WMCreatePLString("ShortcutMask");
124 sYes = WMCreatePLString("Yes");
125 sNo = WMCreatePLString("No");
128 static int getBool(WMPropList * value)
130 char *val;
132 if (!WMIsPLString(value)) {
133 return 0;
135 if (!(val = WMGetFromPLString(value))) {
136 return 0;
139 if ((val[1] == '\0' && (val[0] == 'y' || val[0] == 'Y'))
140 || strcasecmp(val, "YES") == 0) {
142 return 1;
143 } else if ((val[1] == '\0' && (val[0] == 'n' || val[0] == 'N'))
144 || strcasecmp(val, "NO") == 0) {
145 return 0;
146 } else {
147 int i;
148 if (sscanf(val, "%i", &i) == 1) {
149 return (i != 0);
150 } else {
151 wwarning(_("can't convert \"%s\" to boolean"), val);
152 return 0;
157 static unsigned getInt(WMPropList * value)
159 char *val;
160 unsigned n;
162 if (!WMIsPLString(value))
163 return 0;
164 val = WMGetFromPLString(value);
165 if (!val)
166 return 0;
167 if (sscanf(val, "%u", &n) != 1)
168 return 0;
170 return n;
173 static WMPropList *makeWindowState(WWindow * wwin, WApplication * wapp)
175 WScreen *scr = wwin->screen_ptr;
176 Window win;
177 int i;
178 unsigned mask;
179 char *class, *instance, *command = NULL, buffer[512];
180 WMPropList *win_state, *cmd, *name, *workspace;
181 WMPropList *shaded, *miniaturized, *hidden, *geometry;
182 WMPropList *dock, *shortcut;
184 if (wwin->orig_main_window != None && wwin->orig_main_window != wwin->client_win)
185 win = wwin->orig_main_window;
186 else
187 win = wwin->client_win;
189 command = GetCommandForWindow(win);
190 if (!command)
191 return NULL;
193 if (PropGetWMClass(win, &class, &instance)) {
194 if (class && instance)
195 snprintf(buffer, sizeof(buffer), "%s.%s", instance, class);
196 else if (instance)
197 snprintf(buffer, sizeof(buffer), "%s", instance);
198 else if (class)
199 snprintf(buffer, sizeof(buffer), ".%s", class);
200 else
201 snprintf(buffer, sizeof(buffer), ".");
203 name = WMCreatePLString(buffer);
204 cmd = WMCreatePLString(command);
205 /*sprintf(buffer, "%d", wwin->frame->workspace+1);
206 workspace = WMCreatePLString(buffer); */
207 workspace = WMCreatePLString(scr->workspaces[wwin->frame->workspace]->name);
208 shaded = wwin->flags.shaded ? sYes : sNo;
209 miniaturized = wwin->flags.miniaturized ? sYes : sNo;
210 hidden = wwin->flags.hidden ? sYes : sNo;
211 snprintf(buffer, sizeof(buffer), "%ix%i+%i+%i",
212 wwin->client.width, wwin->client.height, wwin->frame_x, wwin->frame_y);
213 geometry = WMCreatePLString(buffer);
215 for (mask = 0, i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
216 if (scr->shortcutWindows[i] != NULL &&
217 WMGetFirstInArray(scr->shortcutWindows[i], wwin) != WANotFound) {
218 mask |= 1 << i;
222 snprintf(buffer, sizeof(buffer), "%u", mask);
223 shortcut = WMCreatePLString(buffer);
225 win_state = WMCreatePLDictionary(sName, name,
226 sCommand, cmd,
227 sWorkspace, workspace,
228 sShaded, shaded,
229 sMiniaturized, miniaturized,
230 sHidden, hidden,
231 sShortcutMask, shortcut, sGeometry, geometry, NULL);
233 WMReleasePropList(name);
234 WMReleasePropList(cmd);
235 WMReleasePropList(workspace);
236 WMReleasePropList(geometry);
237 WMReleasePropList(shortcut);
238 if (wapp && wapp->app_icon && wapp->app_icon->dock) {
239 int i;
240 char *name;
241 if (wapp->app_icon->dock == scr->dock) {
242 name = "Dock";
243 } else {
244 for (i = 0; i < scr->workspace_count; i++)
245 if (scr->workspaces[i]->clip == wapp->app_icon->dock)
246 break;
247 assert(i < scr->workspace_count);
248 /*n = i+1; */
249 name = scr->workspaces[i]->name;
251 dock = WMCreatePLString(name);
252 WMPutInPLDictionary(win_state, sDock, dock);
253 WMReleasePropList(dock);
255 } else {
256 win_state = NULL;
259 if (instance)
260 XFree(instance);
261 if (class)
262 XFree(class);
263 if (command)
264 wfree(command);
266 return win_state;
269 void wSessionSaveState(WScreen * scr)
271 WWindow *wwin = scr->focused_window;
272 WMPropList *win_info, *wks;
273 WMPropList *list = NULL;
274 WMArray *wapp_list = NULL;
276 make_keys();
278 if (!scr->session_state) {
279 scr->session_state = WMCreatePLDictionary(NULL, NULL);
280 if (!scr->session_state)
281 return;
284 list = WMCreatePLArray(NULL);
286 wapp_list = WMCreateArray(16);
288 while (wwin) {
289 WApplication *wapp = wApplicationOf(wwin->main_window);
290 Window appId = wwin->orig_main_window;
292 if ((wwin->transient_for == None || wwin->transient_for == wwin->screen_ptr->root_win)
293 && (WMGetFirstInArray(wapp_list, (void *)appId) == WANotFound
294 || WFLAGP(wwin, shared_appicon))
295 && !WFLAGP(wwin, dont_save_session)) {
296 /* A entry for this application was not yet saved. Save one. */
297 if ((win_info = makeWindowState(wwin, wapp)) != NULL) {
298 WMAddToPLArray(list, win_info);
299 WMReleasePropList(win_info);
300 /* If we were succesful in saving the info for this window
301 * add the application the window belongs to, to the
302 * application list, so no multiple entries for the same
303 * application are saved.
305 WMAddToArray(wapp_list, (void *)appId);
308 wwin = wwin->prev;
310 WMRemoveFromPLDictionary(scr->session_state, sApplications);
311 WMPutInPLDictionary(scr->session_state, sApplications, list);
312 WMReleasePropList(list);
314 wks = WMCreatePLString(scr->workspaces[scr->current_workspace]->name);
315 WMPutInPLDictionary(scr->session_state, sWorkspace, wks);
316 WMReleasePropList(wks);
318 WMFreeArray(wapp_list);
321 void wSessionClearState(WScreen * scr)
323 make_keys();
325 if (!scr->session_state)
326 return;
328 WMRemoveFromPLDictionary(scr->session_state, sApplications);
329 WMRemoveFromPLDictionary(scr->session_state, sWorkspace);
332 static pid_t execCommand(WScreen * scr, char *command, char *host)
334 pid_t pid;
335 char **argv;
336 int argc;
338 wtokensplit(command, &argv, &argc);
340 if (!argc) {
341 return 0;
344 if ((pid = fork()) == 0) {
345 char **args;
346 int i;
348 SetupEnvironment(scr);
350 args = malloc(sizeof(char *) * (argc + 1));
351 if (!args)
352 exit(111);
353 for (i = 0; i < argc; i++) {
354 args[i] = argv[i];
356 args[argc] = NULL;
357 execvp(argv[0], args);
358 exit(111);
360 while (argc > 0)
361 wfree(argv[--argc]);
362 wfree(argv);
363 return pid;
366 static WSavedState *getWindowState(WScreen * scr, WMPropList * win_state)
368 WSavedState *state = wmalloc(sizeof(WSavedState));
369 WMPropList *value;
370 char *tmp;
371 unsigned mask;
372 int i;
374 memset(state, 0, sizeof(WSavedState));
375 state->workspace = -1;
376 value = WMGetFromPLDictionary(win_state, sWorkspace);
377 if (value && WMIsPLString(value)) {
378 tmp = WMGetFromPLString(value);
379 if (sscanf(tmp, "%i", &state->workspace) != 1) {
380 state->workspace = -1;
381 for (i = 0; i < scr->workspace_count; i++) {
382 if (strcmp(scr->workspaces[i]->name, tmp) == 0) {
383 state->workspace = i;
384 break;
387 } else {
388 state->workspace--;
391 if ((value = WMGetFromPLDictionary(win_state, sShaded)) != NULL)
392 state->shaded = getBool(value);
393 if ((value = WMGetFromPLDictionary(win_state, sMiniaturized)) != NULL)
394 state->miniaturized = getBool(value);
395 if ((value = WMGetFromPLDictionary(win_state, sHidden)) != NULL)
396 state->hidden = getBool(value);
397 if ((value = WMGetFromPLDictionary(win_state, sShortcutMask)) != NULL) {
398 mask = getInt(value);
399 state->window_shortcuts = mask;
402 value = WMGetFromPLDictionary(win_state, sGeometry);
403 if (value && WMIsPLString(value)) {
404 if (!(sscanf(WMGetFromPLString(value), "%ix%i+%i+%i",
405 &state->w, &state->h, &state->x, &state->y) == 4 && (state->w > 0 && state->h > 0))) {
406 state->w = 0;
407 state->h = 0;
411 return state;
414 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
416 void wSessionRestoreState(WScreen * scr)
418 WSavedState *state;
419 char *instance, *class, *command, *host;
420 WMPropList *win_info, *apps, *cmd, *value;
421 pid_t pid;
422 int i, count;
423 WDock *dock;
424 WAppIcon *btn = NULL;
425 int j, n, found;
426 char *tmp;
428 make_keys();
430 if (!scr->session_state)
431 return;
433 WMPLSetCaseSensitive(True);
435 apps = WMGetFromPLDictionary(scr->session_state, sApplications);
436 if (!apps)
437 return;
439 count = WMGetPropListItemCount(apps);
440 if (count == 0)
441 return;
443 for (i = 0; i < count; i++) {
444 win_info = WMGetFromPLArray(apps, i);
446 cmd = WMGetFromPLDictionary(win_info, sCommand);
447 if (!cmd || !WMIsPLString(cmd) || !(command = WMGetFromPLString(cmd))) {
448 continue;
451 value = WMGetFromPLDictionary(win_info, sName);
452 if (!value)
453 continue;
455 ParseWindowName(value, &instance, &class, "session");
456 if (!instance && !class)
457 continue;
459 value = WMGetFromPLDictionary(win_info, sHost);
460 if (value && WMIsPLString(value))
461 host = WMGetFromPLString(value);
462 else
463 host = NULL;
465 state = getWindowState(scr, win_info);
467 dock = NULL;
468 value = WMGetFromPLDictionary(win_info, sDock);
469 if (value && WMIsPLString(value) && (tmp = WMGetFromPLString(value)) != NULL) {
470 if (sscanf(tmp, "%i", &n) != 1) {
471 if (!strcasecmp(tmp, "DOCK")) {
472 dock = scr->dock;
473 } else {
474 for (j = 0; j < scr->workspace_count; j++) {
475 if (strcmp(scr->workspaces[j]->name, tmp) == 0) {
476 dock = scr->workspaces[j]->clip;
477 break;
481 } else {
482 if (n == 0) {
483 dock = scr->dock;
484 } else if (n > 0 && n <= scr->workspace_count) {
485 dock = scr->workspaces[n - 1]->clip;
490 found = 0;
491 if (dock != NULL) {
492 for (j = 0; j < dock->max_icons; j++) {
493 btn = dock->icon_array[j];
494 if (btn && SAME(instance, btn->wm_instance) &&
495 SAME(class, btn->wm_class) && SAME(command, btn->command) && !btn->launching) {
496 found = 1;
497 break;
502 if (found) {
503 wDockLaunchWithState(dock, btn, state);
504 } else if ((pid = execCommand(scr, command, host)) > 0) {
505 wWindowAddSavedState(instance, class, command, pid, state);
506 } else {
507 wfree(state);
510 if (instance)
511 wfree(instance);
512 if (class)
513 wfree(class);
515 /* clean up */
516 WMPLSetCaseSensitive(False);
519 void wSessionRestoreLastWorkspace(WScreen * scr)
521 WMPropList *wks;
522 int w, i;
523 char *tmp;
525 make_keys();
527 if (!scr->session_state)
528 return;
530 WMPLSetCaseSensitive(True);
532 wks = WMGetFromPLDictionary(scr->session_state, sWorkspace);
533 if (!wks || !WMIsPLString(wks))
534 return;
536 tmp = WMGetFromPLString(wks);
538 /* clean up */
539 WMPLSetCaseSensitive(False);
541 if (sscanf(tmp, "%i", &w) != 1) {
542 w = -1;
543 for (i = 0; i < scr->workspace_count; i++) {
544 if (strcmp(scr->workspaces[i]->name, tmp) == 0) {
545 w = i;
546 break;
549 } else {
550 w--;
553 if (w != scr->current_workspace && w < scr->workspace_count) {
554 wWorkspaceChange(scr, w);
558 static void clearWaitingAckState(WScreen * scr)
560 WWindow *wwin;
561 WApplication *wapp;
563 for (wwin = scr->focused_window; wwin != NULL; wwin = wwin->prev) {
564 wwin->flags.waiting_save_ack = 0;
565 if (wwin->main_window != None) {
566 wapp = wApplicationOf(wwin->main_window);
567 if (wapp)
568 wapp->main_window_desc->flags.waiting_save_ack = 0;
573 void wSessionSaveClients(WScreen * scr)
579 * With XSMP, this job is done by smproxy
581 void wSessionSendSaveYourself(WScreen * scr)
583 WWindow *wwin;
584 int count;
586 /* freeze client interaction with clients */
587 XGrabKeyboard(dpy, scr->root_win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
588 XGrabPointer(dpy, scr->root_win, False, ButtonPressMask | ButtonReleaseMask,
589 GrabModeAsync, GrabModeAsync, scr->root_win, None, CurrentTime);
591 clearWaitingAckState(scr);
593 count = 0;
595 /* first send SAVE_YOURSELF for everybody */
596 for (wwin = scr->focused_window; wwin != NULL; wwin = wwin->prev) {
597 WWindow *mainWin;
599 mainWin = wWindowFor(wwin->main_window);
601 if (mainWin) {
602 /* if the client is a multi-window client, only send message
603 * to the main window */
604 wwin = mainWin;
607 /* make sure the SAVE_YOURSELF flag is up-to-date */
608 PropGetProtocols(wwin->client_win, &wwin->protocols);
610 if (wwin->protocols.SAVE_YOURSELF) {
611 if (!wwin->flags.waiting_save_ack) {
612 wClientSendProtocol(wwin, _XA_WM_SAVE_YOURSELF, LastTimestamp);
614 wwin->flags.waiting_save_ack = 1;
615 count++;
617 } else {
618 wwin->flags.waiting_save_ack = 0;
622 /* then wait for acknowledge */
623 while (count > 0) {
627 XUngrabPointer(dpy, CurrentTime);
628 XUngrabKeyboard(dpy, CurrentTime);
629 XFlush(dpy);