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"
85 #include <WINGs/WUtil.h>
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
)
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
)
132 if (!WMIsPLString(value
)) {
135 if (!(val
= WMGetFromPLString(value
))) {
139 if ((val
[1] == '\0' && (val
[0] == 'y' || val
[0] == 'Y'))
140 || strcasecmp(val
, "YES") == 0) {
143 } else if ((val
[1] == '\0' && (val
[0] == 'n' || val
[0] == 'N'))
144 || strcasecmp(val
, "NO") == 0) {
148 if (sscanf(val
, "%i", &i
) == 1) {
151 wwarning(_("can't convert \"%s\" to boolean"), val
);
157 static unsigned getInt(WMPropList
* value
)
162 if (!WMIsPLString(value
))
164 val
= WMGetFromPLString(value
);
167 if (sscanf(val
, "%u", &n
) != 1)
173 static WMPropList
*makeWindowState(WWindow
* wwin
, WApplication
* wapp
)
175 WScreen
*scr
= wwin
->screen_ptr
;
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
;
187 win
= wwin
->client_win
;
189 command
= GetCommandForWindow(win
);
193 if (PropGetWMClass(win
, &class, &instance
)) {
194 if (class && instance
)
195 snprintf(buffer
, sizeof(buffer
), "%s.%s", instance
, class);
197 snprintf(buffer
, sizeof(buffer
), "%s", instance
);
199 snprintf(buffer
, sizeof(buffer
), ".%s", class);
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
)
221 snprintf(buffer
, sizeof(buffer
), "%u", mask
);
222 shortcut
= WMCreatePLString(buffer
);
224 win_state
= WMCreatePLDictionary(sName
, name
,
226 sWorkspace
, workspace
,
228 sMiniaturized
, miniaturized
,
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
) {
240 if (wapp
->app_icon
->dock
== scr
->dock
) {
243 if (name
== NULL
) // Try the clips
245 for (i
= 0; i
< scr
->workspace_count
; i
++)
246 if (scr
->workspaces
[i
]->clip
== wapp
->app_icon
->dock
)
248 if (i
< scr
->workspace_count
)
249 name
= scr
->workspaces
[i
]->name
;
251 if (name
== NULL
) // Try the drawers
254 for (dc
= scr
->drawers
; dc
!= NULL
; dc
= dc
->next
)
256 if (dc
->adrawer
== wapp
->app_icon
->dock
)
260 name
= dc
->adrawer
->icon_array
[0]->wm_instance
;
262 dock
= WMCreatePLString(name
);
263 WMPutInPLDictionary(win_state
, sDock
, dock
);
264 WMReleasePropList(dock
);
280 void wSessionSaveState(WScreen
* scr
)
282 WWindow
*wwin
= scr
->focused_window
;
283 WMPropList
*win_info
, *wks
;
284 WMPropList
*list
= NULL
;
285 WMArray
*wapp_list
= NULL
;
289 if (!scr
->session_state
) {
290 scr
->session_state
= WMCreatePLDictionary(NULL
, NULL
);
291 if (!scr
->session_state
)
295 list
= WMCreatePLArray(NULL
);
297 wapp_list
= WMCreateArray(16);
300 WApplication
*wapp
= wApplicationOf(wwin
->main_window
);
301 Window appId
= wwin
->orig_main_window
;
303 if ((wwin
->transient_for
== None
|| wwin
->transient_for
== wwin
->screen_ptr
->root_win
)
304 && (WMGetFirstInArray(wapp_list
, (void *)appId
) == WANotFound
305 || WFLAGP(wwin
, shared_appicon
))
306 && !WFLAGP(wwin
, dont_save_session
)) {
307 /* A entry for this application was not yet saved. Save one. */
308 if ((win_info
= makeWindowState(wwin
, wapp
)) != NULL
) {
309 WMAddToPLArray(list
, win_info
);
310 WMReleasePropList(win_info
);
311 /* If we were succesful in saving the info for this window
312 * add the application the window belongs to, to the
313 * application list, so no multiple entries for the same
314 * application are saved.
316 WMAddToArray(wapp_list
, (void *)appId
);
321 WMRemoveFromPLDictionary(scr
->session_state
, sApplications
);
322 WMPutInPLDictionary(scr
->session_state
, sApplications
, list
);
323 WMReleasePropList(list
);
325 wks
= WMCreatePLString(scr
->workspaces
[scr
->current_workspace
]->name
);
326 WMPutInPLDictionary(scr
->session_state
, sWorkspace
, wks
);
327 WMReleasePropList(wks
);
329 WMFreeArray(wapp_list
);
332 void wSessionClearState(WScreen
* scr
)
336 if (!scr
->session_state
)
339 WMRemoveFromPLDictionary(scr
->session_state
, sApplications
);
340 WMRemoveFromPLDictionary(scr
->session_state
, sWorkspace
);
343 static pid_t
execCommand(WScreen
*scr
, char *command
)
349 wtokensplit(command
, &argv
, &argc
);
355 if ((pid
= fork()) == 0) {
359 SetupEnvironment(scr
);
361 args
= malloc(sizeof(char *) * (argc
+ 1));
364 for (i
= 0; i
< argc
; i
++) {
368 execvp(argv
[0], args
);
377 static WSavedState
*getWindowState(WScreen
* scr
, WMPropList
* win_state
)
379 WSavedState
*state
= wmalloc(sizeof(WSavedState
));
385 state
->workspace
= -1;
386 value
= WMGetFromPLDictionary(win_state
, sWorkspace
);
387 if (value
&& WMIsPLString(value
)) {
388 tmp
= WMGetFromPLString(value
);
389 if (sscanf(tmp
, "%i", &state
->workspace
) != 1) {
390 state
->workspace
= -1;
391 for (i
= 0; i
< scr
->workspace_count
; i
++) {
392 if (strcmp(scr
->workspaces
[i
]->name
, tmp
) == 0) {
393 state
->workspace
= i
;
401 if ((value
= WMGetFromPLDictionary(win_state
, sShaded
)) != NULL
)
402 state
->shaded
= getBool(value
);
403 if ((value
= WMGetFromPLDictionary(win_state
, sMiniaturized
)) != NULL
)
404 state
->miniaturized
= getBool(value
);
405 if ((value
= WMGetFromPLDictionary(win_state
, sHidden
)) != NULL
)
406 state
->hidden
= getBool(value
);
407 if ((value
= WMGetFromPLDictionary(win_state
, sShortcutMask
)) != NULL
) {
408 mask
= getInt(value
);
409 state
->window_shortcuts
= mask
;
412 value
= WMGetFromPLDictionary(win_state
, sGeometry
);
413 if (value
&& WMIsPLString(value
)) {
414 if (!(sscanf(WMGetFromPLString(value
), "%ix%i+%i+%i",
415 &state
->w
, &state
->h
, &state
->x
, &state
->y
) == 4 && (state
->w
> 0 && state
->h
> 0))) {
424 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
426 void wSessionRestoreState(WScreen
*scr
)
429 char *instance
, *class, *command
;
430 WMPropList
*win_info
, *apps
, *cmd
, *value
;
434 WAppIcon
*btn
= NULL
;
440 if (!scr
->session_state
)
443 WMPLSetCaseSensitive(True
);
445 apps
= WMGetFromPLDictionary(scr
->session_state
, sApplications
);
449 count
= WMGetPropListItemCount(apps
);
453 for (i
= 0; i
< count
; i
++) {
454 win_info
= WMGetFromPLArray(apps
, i
);
456 cmd
= WMGetFromPLDictionary(win_info
, sCommand
);
457 if (!cmd
|| !WMIsPLString(cmd
) || !(command
= WMGetFromPLString(cmd
))) {
461 value
= WMGetFromPLDictionary(win_info
, sName
);
465 ParseWindowName(value
, &instance
, &class, "session");
466 if (!instance
&& !class)
469 state
= getWindowState(scr
, win_info
);
472 value
= WMGetFromPLDictionary(win_info
, sDock
);
473 if (value
&& WMIsPLString(value
) && (tmp
= WMGetFromPLString(value
)) != NULL
) {
474 if (sscanf(tmp
, "%i", &n
) != 1) {
475 if (!strcasecmp(tmp
, "DOCK")) {
478 if (dock
== NULL
) // Try the clips
480 for (j
= 0; j
< scr
->workspace_count
; j
++) {
481 if (strcmp(scr
->workspaces
[j
]->name
, tmp
) == 0) {
482 dock
= scr
->workspaces
[j
]->clip
;
487 if (dock
== NULL
) // Try the drawers
490 for (dc
= scr
->drawers
; dc
!= NULL
; dc
= dc
->next
)
492 if (strcmp(dc
->adrawer
->icon_array
[0]->wm_instance
, tmp
) == 0)
502 } else if (n
> 0 && n
<= scr
->workspace_count
) {
503 dock
= scr
->workspaces
[n
- 1]->clip
;
510 for (j
= 0; j
< dock
->max_icons
; j
++) {
511 btn
= dock
->icon_array
[j
];
512 if (btn
&& SAME(instance
, btn
->wm_instance
) &&
513 SAME(class, btn
->wm_class
) && SAME(command
, btn
->command
) && !btn
->launching
) {
521 wDockLaunchWithState(dock
, btn
, state
);
522 } else if ((pid
= execCommand(scr
, command
)) > 0) {
523 wWindowAddSavedState(instance
, class, command
, pid
, state
);
534 WMPLSetCaseSensitive(False
);
537 void wSessionRestoreLastWorkspace(WScreen
* scr
)
545 if (!scr
->session_state
)
548 WMPLSetCaseSensitive(True
);
550 wks
= WMGetFromPLDictionary(scr
->session_state
, sWorkspace
);
551 if (!wks
|| !WMIsPLString(wks
))
554 value
= WMGetFromPLString(wks
);
560 WMPLSetCaseSensitive(False
);
562 /* Get the workspace number for the workspace name */
563 w
= wGetWorkspaceNumber(scr
, value
);
565 if (w
!= scr
->current_workspace
&& w
< scr
->workspace_count
)
566 wWorkspaceChange(scr
, w
);