Code refactoring: replaced macro 'HAVE_XRANDR' by 'USE_XRANDR' for consistency
[wmaker-crm.git] / src / session.c
blob12a07443b43cc5a8014f5178d1a589df737a4fe6
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 along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If defined(XSMP_ENABLED) and session manager is running then
26 * do normal stuff
27 * else
28 * do pre-R6 session management stuff (save window state and relaunch)
30 * When doing a checkpoint:
32 * = Without XSMP
33 * Open "Stop"/status Dialog
34 * Send SAVE_YOURSELF to clients and wait for reply
35 * Save restart info
36 * Save state of clients
38 * = With XSMP
39 * Send checkpoint request to sm
41 * When exiting:
42 * -------------
44 * = Without XSMP
46 * Open "Exit Now"/status Dialog
47 * Send SAVE_YOURSELF to clients and wait for reply
48 * Save restart info
49 * Save state of clients
50 * Send DELETE to all clients
51 * When no more clients are left or user hit "Exit Now", exit
53 * = With XSMP
55 * Send Shutdown request to session manager
56 * if SaveYourself message received, save state of clients
57 * if the Die message is received, exit.
60 #include "wconfig.h"
62 #include <X11/Xlib.h>
63 #include <X11/Xutil.h>
65 #include <stdlib.h>
66 #include <stdio.h>
67 #include <string.h>
68 #include <strings.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 "main.h"
80 #include "properties.h"
81 #include "application.h"
82 #include "appicon.h"
83 #include "dock.h"
84 #include "misc.h"
86 #include <WINGs/WUtil.h>
89 static WMPropList *sApplications = NULL;
90 static WMPropList *sCommand;
91 static WMPropList *sName;
92 static WMPropList *sHost;
93 static WMPropList *sWorkspace;
94 static WMPropList *sShaded;
95 static WMPropList *sMiniaturized;
96 static WMPropList *sHidden;
97 static WMPropList *sGeometry;
98 static WMPropList *sShortcutMask;
100 static WMPropList *sDock;
101 static WMPropList *sYes, *sNo;
103 static void make_keys(void)
105 if (sApplications != NULL)
106 return;
108 sApplications = WMCreatePLString("Applications");
109 sCommand = WMCreatePLString("Command");
110 sName = WMCreatePLString("Name");
111 sHost = WMCreatePLString("Host");
112 sWorkspace = WMCreatePLString("Workspace");
113 sShaded = WMCreatePLString("Shaded");
114 sMiniaturized = WMCreatePLString("Miniaturized");
115 sHidden = WMCreatePLString("Hidden");
116 sGeometry = WMCreatePLString("Geometry");
117 sDock = WMCreatePLString("Dock");
118 sShortcutMask = WMCreatePLString("ShortcutMask");
120 sYes = WMCreatePLString("Yes");
121 sNo = WMCreatePLString("No");
124 static int getBool(WMPropList * value)
126 char *val;
128 if (!WMIsPLString(value)) {
129 return 0;
131 if (!(val = WMGetFromPLString(value))) {
132 return 0;
135 if ((val[1] == '\0' && (val[0] == 'y' || val[0] == 'Y'))
136 || strcasecmp(val, "YES") == 0) {
138 return 1;
139 } else if ((val[1] == '\0' && (val[0] == 'n' || val[0] == 'N'))
140 || strcasecmp(val, "NO") == 0) {
141 return 0;
142 } else {
143 int i;
144 if (sscanf(val, "%i", &i) == 1) {
145 return (i != 0);
146 } else {
147 wwarning(_("can't convert \"%s\" to boolean"), val);
148 return 0;
153 static unsigned getInt(WMPropList * value)
155 char *val;
156 unsigned n;
158 if (!WMIsPLString(value))
159 return 0;
160 val = WMGetFromPLString(value);
161 if (!val)
162 return 0;
163 if (sscanf(val, "%u", &n) != 1)
164 return 0;
166 return n;
169 static WMPropList *makeWindowState(WWindow * wwin, WApplication * wapp)
171 WScreen *scr = wwin->screen_ptr;
172 Window win;
173 int i;
174 unsigned mask;
175 char *class, *instance, *command = NULL, buffer[512];
176 WMPropList *win_state, *cmd, *name, *workspace;
177 WMPropList *shaded, *miniaturized, *hidden, *geometry;
178 WMPropList *dock, *shortcut;
180 if (wwin->orig_main_window != None && wwin->orig_main_window != wwin->client_win)
181 win = wwin->orig_main_window;
182 else
183 win = wwin->client_win;
185 command = GetCommandForWindow(win);
186 if (!command)
187 return NULL;
189 if (PropGetWMClass(win, &class, &instance)) {
190 if (class && instance)
191 snprintf(buffer, sizeof(buffer), "%s.%s", instance, class);
192 else if (instance)
193 snprintf(buffer, sizeof(buffer), "%s", instance);
194 else if (class)
195 snprintf(buffer, sizeof(buffer), ".%s", class);
196 else
197 snprintf(buffer, sizeof(buffer), ".");
199 name = WMCreatePLString(buffer);
200 cmd = WMCreatePLString(command);
202 workspace = WMCreatePLString(w_global.workspace.array[wwin->frame->workspace]->name);
203 shaded = wwin->flags.shaded ? sYes : sNo;
204 miniaturized = wwin->flags.miniaturized ? sYes : sNo;
205 hidden = wwin->flags.hidden ? sYes : sNo;
206 snprintf(buffer, sizeof(buffer), "%ix%i+%i+%i",
207 wwin->client.width, wwin->client.height, wwin->frame_x, wwin->frame_y);
208 geometry = WMCreatePLString(buffer);
210 for (mask = 0, i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
211 if (w_global.shortcut.windows[i] != NULL &&
212 WMGetFirstInArray(w_global.shortcut.windows[i], wwin) != WANotFound)
213 mask |= 1 << i;
216 snprintf(buffer, sizeof(buffer), "%u", mask);
217 shortcut = WMCreatePLString(buffer);
219 win_state = WMCreatePLDictionary(sName, name,
220 sCommand, cmd,
221 sWorkspace, workspace,
222 sShaded, shaded,
223 sMiniaturized, miniaturized,
224 sHidden, hidden,
225 sShortcutMask, shortcut, sGeometry, geometry, NULL);
227 WMReleasePropList(name);
228 WMReleasePropList(cmd);
229 WMReleasePropList(workspace);
230 WMReleasePropList(geometry);
231 WMReleasePropList(shortcut);
232 if (wapp && wapp->app_icon && wapp->app_icon->dock) {
233 int i;
234 char *name = NULL;
235 if (wapp->app_icon->dock == scr->dock)
236 name = "Dock";
238 /* Try the clips */
239 if (name == NULL) {
240 for (i = 0; i < w_global.workspace.count; i++)
241 if (w_global.workspace.array[i]->clip == wapp->app_icon->dock)
242 break;
243 if (i < w_global.workspace.count)
244 name = w_global.workspace.array[i]->name;
246 /* Try the drawers */
247 if (name == NULL) {
248 WDrawerChain *dc;
249 for (dc = scr->drawers; dc != NULL; dc = dc->next) {
250 if (dc->adrawer == wapp->app_icon->dock)
251 break;
253 assert(dc != NULL);
254 name = dc->adrawer->icon_array[0]->wm_instance;
256 dock = WMCreatePLString(name);
257 WMPutInPLDictionary(win_state, sDock, dock);
258 WMReleasePropList(dock);
260 } else {
261 win_state = NULL;
264 if (instance)
265 free(instance);
266 if (class)
267 free(class);
268 if (command)
269 wfree(command);
271 return win_state;
274 void wSessionSaveState(WScreen * scr)
276 WWindow *wwin = scr->focused_window;
277 WMPropList *win_info, *wks;
278 WMPropList *list = NULL;
279 WMArray *wapp_list = NULL;
281 make_keys();
283 if (!w_global.session_state) {
284 w_global.session_state = WMCreatePLDictionary(NULL, NULL);
285 if (!w_global.session_state)
286 return;
289 list = WMCreatePLArray(NULL);
291 wapp_list = WMCreateArray(16);
293 while (wwin) {
294 WApplication *wapp = wApplicationOf(wwin->main_window);
295 Window appId = wwin->orig_main_window;
297 if ((wwin->transient_for == None || wwin->transient_for == wwin->screen_ptr->root_win)
298 && (WMGetFirstInArray(wapp_list, (void *)appId) == WANotFound
299 || WFLAGP(wwin, shared_appicon))
300 && !WFLAGP(wwin, dont_save_session)) {
301 /* A entry for this application was not yet saved. Save one. */
302 if ((win_info = makeWindowState(wwin, wapp)) != NULL) {
303 WMAddToPLArray(list, win_info);
304 WMReleasePropList(win_info);
305 /* If we were succesful in saving the info for this window
306 * add the application the window belongs to, to the
307 * application list, so no multiple entries for the same
308 * application are saved.
310 WMAddToArray(wapp_list, (void *)appId);
313 wwin = wwin->prev;
316 WMRemoveFromPLDictionary(w_global.session_state, sApplications);
317 WMPutInPLDictionary(w_global.session_state, sApplications, list);
318 WMReleasePropList(list);
320 wks = WMCreatePLString(w_global.workspace.array[w_global.workspace.current]->name);
321 WMPutInPLDictionary(w_global.session_state, sWorkspace, wks);
322 WMReleasePropList(wks);
324 WMFreeArray(wapp_list);
327 void wSessionClearState(void)
329 make_keys();
331 if (!w_global.session_state)
332 return;
334 WMRemoveFromPLDictionary(w_global.session_state, sApplications);
335 WMRemoveFromPLDictionary(w_global.session_state, sWorkspace);
338 static pid_t execCommand(WScreen *scr, char *command)
340 pid_t pid;
341 char **argv;
342 int argc;
344 wtokensplit(command, &argv, &argc);
346 if (!argc) {
347 return 0;
350 if ((pid = fork()) == 0) {
351 char **args;
352 int i;
354 SetupEnvironment(scr);
356 args = malloc(sizeof(char *) * (argc + 1));
357 if (!args)
358 exit(111);
359 for (i = 0; i < argc; i++) {
360 args[i] = argv[i];
362 args[argc] = NULL;
363 execvp(argv[0], args);
364 exit(111);
366 while (argc > 0)
367 wfree(argv[--argc]);
368 wfree(argv);
369 return pid;
372 static WSavedState *getWindowState(WMPropList *win_state)
374 WSavedState *state = wmalloc(sizeof(WSavedState));
375 WMPropList *value;
376 char *tmp;
377 unsigned mask;
378 int i;
380 state->workspace = -1;
381 value = WMGetFromPLDictionary(win_state, sWorkspace);
382 if (value && WMIsPLString(value)) {
383 tmp = WMGetFromPLString(value);
384 if (sscanf(tmp, "%i", &state->workspace) != 1) {
385 state->workspace = -1;
386 for (i = 0; i < w_global.workspace.count; i++) {
387 if (strcmp(w_global.workspace.array[i]->name, tmp) == 0) {
388 state->workspace = i;
389 break;
392 } else {
393 state->workspace--;
396 if ((value = WMGetFromPLDictionary(win_state, sShaded)) != NULL)
397 state->shaded = getBool(value);
398 if ((value = WMGetFromPLDictionary(win_state, sMiniaturized)) != NULL)
399 state->miniaturized = getBool(value);
400 if ((value = WMGetFromPLDictionary(win_state, sHidden)) != NULL)
401 state->hidden = getBool(value);
402 if ((value = WMGetFromPLDictionary(win_state, sShortcutMask)) != NULL) {
403 mask = getInt(value);
404 state->window_shortcuts = mask;
407 value = WMGetFromPLDictionary(win_state, sGeometry);
408 if (value && WMIsPLString(value)) {
409 if (!(sscanf(WMGetFromPLString(value), "%ix%i+%i+%i",
410 &state->w, &state->h, &state->x, &state->y) == 4 && (state->w > 0 && state->h > 0))) {
411 state->w = 0;
412 state->h = 0;
416 return state;
419 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
421 void wSessionRestoreState(WScreen *scr)
423 WSavedState *state;
424 char *instance, *class, *command;
425 WMPropList *win_info, *apps, *cmd, *value;
426 pid_t pid;
427 int i, count;
428 WDock *dock;
429 WAppIcon *btn = NULL;
430 int j, n, found;
431 char *tmp;
433 make_keys();
435 if (!w_global.session_state)
436 return;
438 WMPLSetCaseSensitive(True);
440 apps = WMGetFromPLDictionary(w_global.session_state, sApplications);
441 if (!apps)
442 return;
444 count = WMGetPropListItemCount(apps);
445 if (count == 0)
446 return;
448 for (i = 0; i < count; i++) {
449 win_info = WMGetFromPLArray(apps, i);
451 cmd = WMGetFromPLDictionary(win_info, sCommand);
452 if (!cmd || !WMIsPLString(cmd) || !(command = WMGetFromPLString(cmd))) {
453 continue;
456 value = WMGetFromPLDictionary(win_info, sName);
457 if (!value)
458 continue;
460 ParseWindowName(value, &instance, &class, "session");
461 if (!instance && !class)
462 continue;
464 state = getWindowState(win_info);
466 dock = NULL;
467 value = WMGetFromPLDictionary(win_info, sDock);
468 if (value && WMIsPLString(value) && (tmp = WMGetFromPLString(value)) != NULL) {
469 if (sscanf(tmp, "%i", &n) != 1) {
470 if (!strcasecmp(tmp, "DOCK"))
471 dock = scr->dock;
473 /* Try the clips */
474 if (dock == NULL) {
475 for (j = 0; j < w_global.workspace.count; j++) {
476 if (strcmp(w_global.workspace.array[j]->name, tmp) == 0) {
477 dock = w_global.workspace.array[j]->clip;
478 break;
482 if (dock == NULL) // Try the drawers
484 WDrawerChain *dc;
485 for (dc = scr->drawers; dc != NULL; dc = dc->next)
487 if (strcmp(dc->adrawer->icon_array[0]->wm_instance, tmp) == 0)
489 dock = dc->adrawer;
490 break;
494 } else {
495 if (n == 0) {
496 dock = scr->dock;
497 } else if (n > 0 && n <= w_global.workspace.count) {
498 dock = w_global.workspace.array[n - 1]->clip;
503 found = 0;
504 if (dock != NULL) {
505 for (j = 0; j < dock->max_icons; j++) {
506 btn = dock->icon_array[j];
507 if (btn && SAME(instance, btn->wm_instance) &&
508 SAME(class, btn->wm_class) && SAME(command, btn->command) && !btn->launching) {
509 found = 1;
510 break;
515 if (found) {
516 wDockLaunchWithState(btn, state);
517 } else if ((pid = execCommand(scr, command)) > 0) {
518 wWindowAddSavedState(instance, class, command, pid, state);
519 } else {
520 wfree(state);
523 if (instance)
524 wfree(instance);
525 if (class)
526 wfree(class);
528 /* clean up */
529 WMPLSetCaseSensitive(False);
532 void wSessionRestoreLastWorkspace(WScreen * scr)
534 WMPropList *wks;
535 int w;
536 char *value;
538 make_keys();
540 if (!w_global.session_state)
541 return;
543 WMPLSetCaseSensitive(True);
545 wks = WMGetFromPLDictionary(w_global.session_state, sWorkspace);
546 if (!wks || !WMIsPLString(wks))
547 return;
549 value = WMGetFromPLString(wks);
551 if (!value)
552 return;
554 /* clean up */
555 WMPLSetCaseSensitive(False);
557 /* Get the workspace number for the workspace name */
558 w = wGetWorkspaceNumber(value);
560 if (w != w_global.workspace.current && w < w_global.workspace.count)
561 wWorkspaceChange(scr, w);