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
28 * do pre-R6 session management stuff (save window state and relaunch)
30 * When doing a checkpoint:
33 * Open "Stop"/status Dialog
34 * Send SAVE_YOURSELF to clients and wait for reply
36 * Save state of clients
39 * Send checkpoint request to sm
46 * Open "Exit Now"/status Dialog
47 * Send SAVE_YOURSELF to clients and wait for reply
49 * Save state of clients
50 * Send DELETE to all clients
51 * When no more clients are left or user hit "Exit Now", exit
55 * Send Shutdown request to session manager
56 * if SaveYourself message received, save state of clients
57 * if the Die message is received, exit.
63 #include <X11/Xutil.h>
72 #include "WindowMaker.h"
78 #include "workspace.h"
80 #include "properties.h"
81 #include "application.h"
86 #include <WINGs/WUtil.h>
90 extern Atom _XA_WM_SAVE_YOURSELF
;
92 extern Time LastTimestamp
;
94 static WMPropList
*sApplications
= NULL
;
95 static WMPropList
*sCommand
;
96 static WMPropList
*sName
;
97 static WMPropList
*sHost
;
98 static WMPropList
*sWorkspace
;
99 static WMPropList
*sShaded
;
100 static WMPropList
*sMiniaturized
;
101 static WMPropList
*sHidden
;
102 static WMPropList
*sGeometry
;
103 static WMPropList
*sShortcutMask
;
105 static WMPropList
*sDock
;
106 static WMPropList
*sYes
, *sNo
;
108 static void make_keys(void)
110 if (sApplications
!= NULL
)
113 sApplications
= WMCreatePLString("Applications");
114 sCommand
= WMCreatePLString("Command");
115 sName
= WMCreatePLString("Name");
116 sHost
= WMCreatePLString("Host");
117 sWorkspace
= WMCreatePLString("Workspace");
118 sShaded
= WMCreatePLString("Shaded");
119 sMiniaturized
= WMCreatePLString("Miniaturized");
120 sHidden
= WMCreatePLString("Hidden");
121 sGeometry
= WMCreatePLString("Geometry");
122 sDock
= WMCreatePLString("Dock");
123 sShortcutMask
= WMCreatePLString("ShortcutMask");
125 sYes
= WMCreatePLString("Yes");
126 sNo
= WMCreatePLString("No");
129 static int getBool(WMPropList
* value
)
133 if (!WMIsPLString(value
)) {
136 if (!(val
= WMGetFromPLString(value
))) {
140 if ((val
[1] == '\0' && (val
[0] == 'y' || val
[0] == 'Y'))
141 || strcasecmp(val
, "YES") == 0) {
144 } else if ((val
[1] == '\0' && (val
[0] == 'n' || val
[0] == 'N'))
145 || strcasecmp(val
, "NO") == 0) {
149 if (sscanf(val
, "%i", &i
) == 1) {
152 wwarning(_("can't convert \"%s\" to boolean"), val
);
158 static unsigned getInt(WMPropList
* value
)
163 if (!WMIsPLString(value
))
165 val
= WMGetFromPLString(value
);
168 if (sscanf(val
, "%u", &n
) != 1)
174 static WMPropList
*makeWindowState(WWindow
* wwin
, WApplication
* wapp
)
176 WScreen
*scr
= wwin
->screen_ptr
;
180 char *class, *instance
, *command
= NULL
, buffer
[512];
181 WMPropList
*win_state
, *cmd
, *name
, *workspace
;
182 WMPropList
*shaded
, *miniaturized
, *hidden
, *geometry
;
183 WMPropList
*dock
, *shortcut
;
185 if (wwin
->orig_main_window
!= None
&& wwin
->orig_main_window
!= wwin
->client_win
)
186 win
= wwin
->orig_main_window
;
188 win
= wwin
->client_win
;
190 command
= GetCommandForWindow(win
);
194 if (PropGetWMClass(win
, &class, &instance
)) {
195 if (class && instance
)
196 snprintf(buffer
, sizeof(buffer
), "%s.%s", instance
, class);
198 snprintf(buffer
, sizeof(buffer
), "%s", instance
);
200 snprintf(buffer
, sizeof(buffer
), ".%s", class);
202 snprintf(buffer
, sizeof(buffer
), ".");
204 name
= WMCreatePLString(buffer
);
205 cmd
= WMCreatePLString(command
);
206 /*sprintf(buffer, "%d", wwin->frame->workspace+1);
207 workspace = WMCreatePLString(buffer); */
208 workspace
= WMCreatePLString(scr
->workspaces
[wwin
->frame
->workspace
]->name
);
209 shaded
= wwin
->flags
.shaded
? sYes
: sNo
;
210 miniaturized
= wwin
->flags
.miniaturized
? sYes
: sNo
;
211 hidden
= wwin
->flags
.hidden
? sYes
: sNo
;
212 snprintf(buffer
, sizeof(buffer
), "%ix%i+%i+%i",
213 wwin
->client
.width
, wwin
->client
.height
, wwin
->frame_x
, wwin
->frame_y
);
214 geometry
= WMCreatePLString(buffer
);
216 for (mask
= 0, i
= 0; i
< MAX_WINDOW_SHORTCUTS
; i
++) {
217 if (scr
->shortcutWindows
[i
] != NULL
&&
218 WMGetFirstInArray(scr
->shortcutWindows
[i
], wwin
) != WANotFound
)
222 snprintf(buffer
, sizeof(buffer
), "%u", mask
);
223 shortcut
= WMCreatePLString(buffer
);
225 win_state
= WMCreatePLDictionary(sName
, name
,
227 sWorkspace
, workspace
,
229 sMiniaturized
, miniaturized
,
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
) {
241 if (wapp
->app_icon
->dock
== scr
->dock
) {
244 if (name
== NULL
) // Try the clips
246 for (i
= 0; i
< scr
->workspace_count
; i
++)
247 if (scr
->workspaces
[i
]->clip
== wapp
->app_icon
->dock
)
249 if (i
< scr
->workspace_count
)
250 name
= scr
->workspaces
[i
]->name
;
252 if (name
== NULL
) // Try the drawers
255 for (dc
= scr
->drawers
; dc
!= NULL
; dc
= dc
->next
)
257 if (dc
->adrawer
== wapp
->app_icon
->dock
)
261 name
= dc
->adrawer
->icon_array
[0]->wm_instance
;
263 dock
= WMCreatePLString(name
);
264 WMPutInPLDictionary(win_state
, sDock
, dock
);
265 WMReleasePropList(dock
);
281 void wSessionSaveState(WScreen
* scr
)
283 WWindow
*wwin
= scr
->focused_window
;
284 WMPropList
*win_info
, *wks
;
285 WMPropList
*list
= NULL
;
286 WMArray
*wapp_list
= NULL
;
290 if (!scr
->session_state
) {
291 scr
->session_state
= WMCreatePLDictionary(NULL
, NULL
);
292 if (!scr
->session_state
)
296 list
= WMCreatePLArray(NULL
);
298 wapp_list
= WMCreateArray(16);
301 WApplication
*wapp
= wApplicationOf(wwin
->main_window
);
302 Window appId
= wwin
->orig_main_window
;
304 if ((wwin
->transient_for
== None
|| wwin
->transient_for
== wwin
->screen_ptr
->root_win
)
305 && (WMGetFirstInArray(wapp_list
, (void *)appId
) == WANotFound
306 || WFLAGP(wwin
, shared_appicon
))
307 && !WFLAGP(wwin
, dont_save_session
)) {
308 /* A entry for this application was not yet saved. Save one. */
309 if ((win_info
= makeWindowState(wwin
, wapp
)) != NULL
) {
310 WMAddToPLArray(list
, win_info
);
311 WMReleasePropList(win_info
);
312 /* If we were succesful in saving the info for this window
313 * add the application the window belongs to, to the
314 * application list, so no multiple entries for the same
315 * application are saved.
317 WMAddToArray(wapp_list
, (void *)appId
);
322 WMRemoveFromPLDictionary(scr
->session_state
, sApplications
);
323 WMPutInPLDictionary(scr
->session_state
, sApplications
, list
);
324 WMReleasePropList(list
);
326 wks
= WMCreatePLString(scr
->workspaces
[scr
->current_workspace
]->name
);
327 WMPutInPLDictionary(scr
->session_state
, sWorkspace
, wks
);
328 WMReleasePropList(wks
);
330 WMFreeArray(wapp_list
);
333 void wSessionClearState(WScreen
* scr
)
337 if (!scr
->session_state
)
340 WMRemoveFromPLDictionary(scr
->session_state
, sApplications
);
341 WMRemoveFromPLDictionary(scr
->session_state
, sWorkspace
);
344 static pid_t
execCommand(WScreen
*scr
, char *command
)
350 wtokensplit(command
, &argv
, &argc
);
356 if ((pid
= fork()) == 0) {
360 SetupEnvironment(scr
);
362 args
= malloc(sizeof(char *) * (argc
+ 1));
365 for (i
= 0; i
< argc
; i
++) {
369 execvp(argv
[0], args
);
378 static WSavedState
*getWindowState(WScreen
* scr
, WMPropList
* win_state
)
380 WSavedState
*state
= wmalloc(sizeof(WSavedState
));
386 state
->workspace
= -1;
387 value
= WMGetFromPLDictionary(win_state
, sWorkspace
);
388 if (value
&& WMIsPLString(value
)) {
389 tmp
= WMGetFromPLString(value
);
390 if (sscanf(tmp
, "%i", &state
->workspace
) != 1) {
391 state
->workspace
= -1;
392 for (i
= 0; i
< scr
->workspace_count
; i
++) {
393 if (strcmp(scr
->workspaces
[i
]->name
, tmp
) == 0) {
394 state
->workspace
= i
;
402 if ((value
= WMGetFromPLDictionary(win_state
, sShaded
)) != NULL
)
403 state
->shaded
= getBool(value
);
404 if ((value
= WMGetFromPLDictionary(win_state
, sMiniaturized
)) != NULL
)
405 state
->miniaturized
= getBool(value
);
406 if ((value
= WMGetFromPLDictionary(win_state
, sHidden
)) != NULL
)
407 state
->hidden
= getBool(value
);
408 if ((value
= WMGetFromPLDictionary(win_state
, sShortcutMask
)) != NULL
) {
409 mask
= getInt(value
);
410 state
->window_shortcuts
= mask
;
413 value
= WMGetFromPLDictionary(win_state
, sGeometry
);
414 if (value
&& WMIsPLString(value
)) {
415 if (!(sscanf(WMGetFromPLString(value
), "%ix%i+%i+%i",
416 &state
->w
, &state
->h
, &state
->x
, &state
->y
) == 4 && (state
->w
> 0 && state
->h
> 0))) {
425 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
427 void wSessionRestoreState(WScreen
*scr
)
430 char *instance
, *class, *command
;
431 WMPropList
*win_info
, *apps
, *cmd
, *value
;
435 WAppIcon
*btn
= NULL
;
441 if (!scr
->session_state
)
444 WMPLSetCaseSensitive(True
);
446 apps
= WMGetFromPLDictionary(scr
->session_state
, sApplications
);
450 count
= WMGetPropListItemCount(apps
);
454 for (i
= 0; i
< count
; i
++) {
455 win_info
= WMGetFromPLArray(apps
, i
);
457 cmd
= WMGetFromPLDictionary(win_info
, sCommand
);
458 if (!cmd
|| !WMIsPLString(cmd
) || !(command
= WMGetFromPLString(cmd
))) {
462 value
= WMGetFromPLDictionary(win_info
, sName
);
466 ParseWindowName(value
, &instance
, &class, "session");
467 if (!instance
&& !class)
470 state
= getWindowState(scr
, win_info
);
473 value
= WMGetFromPLDictionary(win_info
, sDock
);
474 if (value
&& WMIsPLString(value
) && (tmp
= WMGetFromPLString(value
)) != NULL
) {
475 if (sscanf(tmp
, "%i", &n
) != 1) {
476 if (!strcasecmp(tmp
, "DOCK")) {
479 if (dock
== NULL
) // Try the clips
481 for (j
= 0; j
< scr
->workspace_count
; j
++) {
482 if (strcmp(scr
->workspaces
[j
]->name
, tmp
) == 0) {
483 dock
= scr
->workspaces
[j
]->clip
;
488 if (dock
== NULL
) // Try the drawers
491 for (dc
= scr
->drawers
; dc
!= NULL
; dc
= dc
->next
)
493 if (strcmp(dc
->adrawer
->icon_array
[0]->wm_instance
, tmp
) == 0)
503 } else if (n
> 0 && n
<= scr
->workspace_count
) {
504 dock
= scr
->workspaces
[n
- 1]->clip
;
511 for (j
= 0; j
< dock
->max_icons
; j
++) {
512 btn
= dock
->icon_array
[j
];
513 if (btn
&& SAME(instance
, btn
->wm_instance
) &&
514 SAME(class, btn
->wm_class
) && SAME(command
, btn
->command
) && !btn
->launching
) {
522 wDockLaunchWithState(dock
, btn
, state
);
523 } else if ((pid
= execCommand(scr
, command
)) > 0) {
524 wWindowAddSavedState(instance
, class, command
, pid
, state
);
535 WMPLSetCaseSensitive(False
);
538 void wSessionRestoreLastWorkspace(WScreen
* scr
)
546 if (!scr
->session_state
)
549 WMPLSetCaseSensitive(True
);
551 wks
= WMGetFromPLDictionary(scr
->session_state
, sWorkspace
);
552 if (!wks
|| !WMIsPLString(wks
))
555 value
= WMGetFromPLString(wks
);
561 WMPLSetCaseSensitive(False
);
563 /* Get the workspace number for the workspace name */
564 w
= wGetWorkspaceNumber(scr
, value
);
566 if (w
!= scr
->current_workspace
&& w
< scr
->workspace_count
)
567 wWorkspaceChange(scr
, w
);