Added option to 'configure' to control debug information for compilation
[wmaker-crm.git] / src / session.c
blob703b5fa7a349512682fe5c77b22bc046f63ceb30
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"
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;
221 snprintf(buffer, sizeof(buffer), "%u", mask);
222 shortcut = WMCreatePLString(buffer);
224 win_state = WMCreatePLDictionary(sName, name,
225 sCommand, cmd,
226 sWorkspace, workspace,
227 sShaded, shaded,
228 sMiniaturized, miniaturized,
229 sHidden, hidden,
230 sShortcutMask, shortcut, sGeometry, geometry, NULL);
232 WMReleasePropList(name);
233 WMReleasePropList(cmd);
234 WMReleasePropList(workspace);
235 WMReleasePropList(geometry);
236 WMReleasePropList(shortcut);
237 if (wapp && wapp->app_icon && wapp->app_icon->dock) {
238 int i;
239 char *name;
240 if (wapp->app_icon->dock == scr->dock) {
241 name = "Dock";
242 } else {
243 for (i = 0; i < scr->workspace_count; i++)
244 if (scr->workspaces[i]->clip == wapp->app_icon->dock)
245 break;
246 assert(i < scr->workspace_count);
247 /*n = i+1; */
248 name = scr->workspaces[i]->name;
250 dock = WMCreatePLString(name);
251 WMPutInPLDictionary(win_state, sDock, dock);
252 WMReleasePropList(dock);
254 } else {
255 win_state = NULL;
258 if (instance)
259 free(instance);
260 if (class)
261 free(class);
262 if (command)
263 wfree(command);
265 return win_state;
268 void wSessionSaveState(WScreen * scr)
270 WWindow *wwin = scr->focused_window;
271 WMPropList *win_info, *wks;
272 WMPropList *list = NULL;
273 WMArray *wapp_list = NULL;
275 make_keys();
277 if (!scr->session_state) {
278 scr->session_state = WMCreatePLDictionary(NULL, NULL);
279 if (!scr->session_state)
280 return;
283 list = WMCreatePLArray(NULL);
285 wapp_list = WMCreateArray(16);
287 while (wwin) {
288 WApplication *wapp = wApplicationOf(wwin->main_window);
289 Window appId = wwin->orig_main_window;
291 if ((wwin->transient_for == None || wwin->transient_for == wwin->screen_ptr->root_win)
292 && (WMGetFirstInArray(wapp_list, (void *)appId) == WANotFound
293 || WFLAGP(wwin, shared_appicon))
294 && !WFLAGP(wwin, dont_save_session)) {
295 /* A entry for this application was not yet saved. Save one. */
296 if ((win_info = makeWindowState(wwin, wapp)) != NULL) {
297 WMAddToPLArray(list, win_info);
298 WMReleasePropList(win_info);
299 /* If we were succesful in saving the info for this window
300 * add the application the window belongs to, to the
301 * application list, so no multiple entries for the same
302 * application are saved.
304 WMAddToArray(wapp_list, (void *)appId);
307 wwin = wwin->prev;
309 WMRemoveFromPLDictionary(scr->session_state, sApplications);
310 WMPutInPLDictionary(scr->session_state, sApplications, list);
311 WMReleasePropList(list);
313 wks = WMCreatePLString(scr->workspaces[scr->current_workspace]->name);
314 WMPutInPLDictionary(scr->session_state, sWorkspace, wks);
315 WMReleasePropList(wks);
317 WMFreeArray(wapp_list);
320 void wSessionClearState(WScreen * scr)
322 make_keys();
324 if (!scr->session_state)
325 return;
327 WMRemoveFromPLDictionary(scr->session_state, sApplications);
328 WMRemoveFromPLDictionary(scr->session_state, sWorkspace);
331 static pid_t execCommand(WScreen * scr, char *command, char *host)
333 pid_t pid;
334 char **argv;
335 int argc;
337 wtokensplit(command, &argv, &argc);
339 if (!argc) {
340 return 0;
343 if ((pid = fork()) == 0) {
344 char **args;
345 int i;
347 SetupEnvironment(scr);
349 args = malloc(sizeof(char *) * (argc + 1));
350 if (!args)
351 exit(111);
352 for (i = 0; i < argc; i++) {
353 args[i] = argv[i];
355 args[argc] = NULL;
356 execvp(argv[0], args);
357 exit(111);
359 while (argc > 0)
360 wfree(argv[--argc]);
361 wfree(argv);
362 return pid;
365 static WSavedState *getWindowState(WScreen * scr, WMPropList * win_state)
367 WSavedState *state = wmalloc(sizeof(WSavedState));
368 WMPropList *value;
369 char *tmp;
370 unsigned mask;
371 int i;
373 state->workspace = -1;
374 value = WMGetFromPLDictionary(win_state, sWorkspace);
375 if (value && WMIsPLString(value)) {
376 tmp = WMGetFromPLString(value);
377 if (sscanf(tmp, "%i", &state->workspace) != 1) {
378 state->workspace = -1;
379 for (i = 0; i < scr->workspace_count; i++) {
380 if (strcmp(scr->workspaces[i]->name, tmp) == 0) {
381 state->workspace = i;
382 break;
385 } else {
386 state->workspace--;
389 if ((value = WMGetFromPLDictionary(win_state, sShaded)) != NULL)
390 state->shaded = getBool(value);
391 if ((value = WMGetFromPLDictionary(win_state, sMiniaturized)) != NULL)
392 state->miniaturized = getBool(value);
393 if ((value = WMGetFromPLDictionary(win_state, sHidden)) != NULL)
394 state->hidden = getBool(value);
395 if ((value = WMGetFromPLDictionary(win_state, sShortcutMask)) != NULL) {
396 mask = getInt(value);
397 state->window_shortcuts = mask;
400 value = WMGetFromPLDictionary(win_state, sGeometry);
401 if (value && WMIsPLString(value)) {
402 if (!(sscanf(WMGetFromPLString(value), "%ix%i+%i+%i",
403 &state->w, &state->h, &state->x, &state->y) == 4 && (state->w > 0 && state->h > 0))) {
404 state->w = 0;
405 state->h = 0;
409 return state;
412 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
414 void wSessionRestoreState(WScreen * scr)
416 WSavedState *state;
417 char *instance, *class, *command, *host;
418 WMPropList *win_info, *apps, *cmd, *value;
419 pid_t pid;
420 int i, count;
421 WDock *dock;
422 WAppIcon *btn = NULL;
423 int j, n, found;
424 char *tmp;
426 make_keys();
428 if (!scr->session_state)
429 return;
431 WMPLSetCaseSensitive(True);
433 apps = WMGetFromPLDictionary(scr->session_state, sApplications);
434 if (!apps)
435 return;
437 count = WMGetPropListItemCount(apps);
438 if (count == 0)
439 return;
441 for (i = 0; i < count; i++) {
442 win_info = WMGetFromPLArray(apps, i);
444 cmd = WMGetFromPLDictionary(win_info, sCommand);
445 if (!cmd || !WMIsPLString(cmd) || !(command = WMGetFromPLString(cmd))) {
446 continue;
449 value = WMGetFromPLDictionary(win_info, sName);
450 if (!value)
451 continue;
453 ParseWindowName(value, &instance, &class, "session");
454 if (!instance && !class)
455 continue;
457 value = WMGetFromPLDictionary(win_info, sHost);
458 if (value && WMIsPLString(value))
459 host = WMGetFromPLString(value);
460 else
461 host = NULL;
463 state = getWindowState(scr, win_info);
465 dock = NULL;
466 value = WMGetFromPLDictionary(win_info, sDock);
467 if (value && WMIsPLString(value) && (tmp = WMGetFromPLString(value)) != NULL) {
468 if (sscanf(tmp, "%i", &n) != 1) {
469 if (!strcasecmp(tmp, "DOCK")) {
470 dock = scr->dock;
471 } else {
472 for (j = 0; j < scr->workspace_count; j++) {
473 if (strcmp(scr->workspaces[j]->name, tmp) == 0) {
474 dock = scr->workspaces[j]->clip;
475 break;
479 } else {
480 if (n == 0) {
481 dock = scr->dock;
482 } else if (n > 0 && n <= scr->workspace_count) {
483 dock = scr->workspaces[n - 1]->clip;
488 found = 0;
489 if (dock != NULL) {
490 for (j = 0; j < dock->max_icons; j++) {
491 btn = dock->icon_array[j];
492 if (btn && SAME(instance, btn->wm_instance) &&
493 SAME(class, btn->wm_class) && SAME(command, btn->command) && !btn->launching) {
494 found = 1;
495 break;
500 if (found) {
501 wDockLaunchWithState(dock, btn, state);
502 } else if ((pid = execCommand(scr, command, host)) > 0) {
503 wWindowAddSavedState(instance, class, command, pid, state);
504 } else {
505 wfree(state);
508 if (instance)
509 wfree(instance);
510 if (class)
511 wfree(class);
513 /* clean up */
514 WMPLSetCaseSensitive(False);
517 void wSessionRestoreLastWorkspace(WScreen * scr)
519 WMPropList *wks;
520 int w;
521 char *value;
523 make_keys();
525 if (!scr->session_state)
526 return;
528 WMPLSetCaseSensitive(True);
530 wks = WMGetFromPLDictionary(scr->session_state, sWorkspace);
531 if (!wks || !WMIsPLString(wks))
532 return;
534 value = WMGetFromPLString(wks);
536 if (!value)
537 return;
539 /* clean up */
540 WMPLSetCaseSensitive(False);
542 /* Get the workspace number for the workspace name */
543 w = wGetWorkspaceNumber(scr, value);
545 if (w != scr->current_workspace && w < scr->workspace_count)
546 wWorkspaceChange(scr, w);