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>
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
)
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
)
128 if (!WMIsPLString(value
)) {
131 if (!(val
= WMGetFromPLString(value
))) {
135 if ((val
[1] == '\0' && (val
[0] == 'y' || val
[0] == 'Y'))
136 || strcasecmp(val
, "YES") == 0) {
139 } else if ((val
[1] == '\0' && (val
[0] == 'n' || val
[0] == 'N'))
140 || strcasecmp(val
, "NO") == 0) {
144 if (sscanf(val
, "%i", &i
) == 1) {
147 wwarning(_("can't convert \"%s\" to boolean"), val
);
153 static unsigned getInt(WMPropList
* value
)
158 if (!WMIsPLString(value
))
160 val
= WMGetFromPLString(value
);
163 if (sscanf(val
, "%u", &n
) != 1)
169 static WMPropList
*makeWindowState(WWindow
* wwin
, WApplication
* wapp
)
171 WScreen
*scr
= wwin
->screen_ptr
;
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
;
183 win
= wwin
->client_win
;
185 command
= GetCommandForWindow(win
);
189 if (PropGetWMClass(win
, &class, &instance
)) {
190 if (class && instance
)
191 snprintf(buffer
, sizeof(buffer
), "%s.%s", instance
, class);
193 snprintf(buffer
, sizeof(buffer
), "%s", instance
);
195 snprintf(buffer
, sizeof(buffer
), ".%s", class);
197 snprintf(buffer
, sizeof(buffer
), ".");
199 name
= WMCreatePLString(buffer
);
200 cmd
= WMCreatePLString(command
);
201 workspace
= WMCreatePLString(scr
->workspaces
[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 (scr
->shortcutWindows
[i
] != NULL
&&
212 WMGetFirstInArray(scr
->shortcutWindows
[i
], wwin
) != WANotFound
)
216 snprintf(buffer
, sizeof(buffer
), "%u", mask
);
217 shortcut
= WMCreatePLString(buffer
);
219 win_state
= WMCreatePLDictionary(sName
, name
,
221 sWorkspace
, workspace
,
223 sMiniaturized
, miniaturized
,
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
) {
235 if (wapp
->app_icon
->dock
== scr
->dock
)
240 for (i
= 0; i
< scr
->workspace_count
; i
++)
241 if (scr
->workspaces
[i
]->clip
== wapp
->app_icon
->dock
)
243 if (i
< scr
->workspace_count
)
244 name
= scr
->workspaces
[i
]->name
;
246 /* Try the drawers */
249 for (dc
= scr
->drawers
; dc
!= NULL
; dc
= dc
->next
) {
250 if (dc
->adrawer
== wapp
->app_icon
->dock
)
254 name
= dc
->adrawer
->icon_array
[0]->wm_instance
;
256 dock
= WMCreatePLString(name
);
257 WMPutInPLDictionary(win_state
, sDock
, dock
);
258 WMReleasePropList(dock
);
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
;
283 if (!scr
->session_state
) {
284 scr
->session_state
= WMCreatePLDictionary(NULL
, NULL
);
285 if (!scr
->session_state
)
289 list
= WMCreatePLArray(NULL
);
291 wapp_list
= WMCreateArray(16);
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
);
315 WMRemoveFromPLDictionary(scr
->session_state
, sApplications
);
316 WMPutInPLDictionary(scr
->session_state
, sApplications
, list
);
317 WMReleasePropList(list
);
319 wks
= WMCreatePLString(scr
->workspaces
[scr
->current_workspace
]->name
);
320 WMPutInPLDictionary(scr
->session_state
, sWorkspace
, wks
);
321 WMReleasePropList(wks
);
323 WMFreeArray(wapp_list
);
326 void wSessionClearState(WScreen
* scr
)
330 if (!scr
->session_state
)
333 WMRemoveFromPLDictionary(scr
->session_state
, sApplications
);
334 WMRemoveFromPLDictionary(scr
->session_state
, sWorkspace
);
337 static pid_t
execCommand(WScreen
*scr
, char *command
)
343 wtokensplit(command
, &argv
, &argc
);
349 if ((pid
= fork()) == 0) {
353 SetupEnvironment(scr
);
355 args
= malloc(sizeof(char *) * (argc
+ 1));
358 for (i
= 0; i
< argc
; i
++) {
362 execvp(argv
[0], args
);
371 static WSavedState
*getWindowState(WScreen
* scr
, WMPropList
* win_state
)
373 WSavedState
*state
= wmalloc(sizeof(WSavedState
));
379 state
->workspace
= -1;
380 value
= WMGetFromPLDictionary(win_state
, sWorkspace
);
381 if (value
&& WMIsPLString(value
)) {
382 tmp
= WMGetFromPLString(value
);
383 if (sscanf(tmp
, "%i", &state
->workspace
) != 1) {
384 state
->workspace
= -1;
385 for (i
= 0; i
< scr
->workspace_count
; i
++) {
386 if (strcmp(scr
->workspaces
[i
]->name
, tmp
) == 0) {
387 state
->workspace
= i
;
395 if ((value
= WMGetFromPLDictionary(win_state
, sShaded
)) != NULL
)
396 state
->shaded
= getBool(value
);
397 if ((value
= WMGetFromPLDictionary(win_state
, sMiniaturized
)) != NULL
)
398 state
->miniaturized
= getBool(value
);
399 if ((value
= WMGetFromPLDictionary(win_state
, sHidden
)) != NULL
)
400 state
->hidden
= getBool(value
);
401 if ((value
= WMGetFromPLDictionary(win_state
, sShortcutMask
)) != NULL
) {
402 mask
= getInt(value
);
403 state
->window_shortcuts
= mask
;
406 value
= WMGetFromPLDictionary(win_state
, sGeometry
);
407 if (value
&& WMIsPLString(value
)) {
408 if (!(sscanf(WMGetFromPLString(value
), "%ix%i+%i+%i",
409 &state
->w
, &state
->h
, &state
->x
, &state
->y
) == 4 && (state
->w
> 0 && state
->h
> 0))) {
418 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
420 void wSessionRestoreState(WScreen
*scr
)
423 char *instance
, *class, *command
;
424 WMPropList
*win_info
, *apps
, *cmd
, *value
;
428 WAppIcon
*btn
= NULL
;
434 if (!scr
->session_state
)
437 WMPLSetCaseSensitive(True
);
439 apps
= WMGetFromPLDictionary(scr
->session_state
, sApplications
);
443 count
= WMGetPropListItemCount(apps
);
447 for (i
= 0; i
< count
; i
++) {
448 win_info
= WMGetFromPLArray(apps
, i
);
450 cmd
= WMGetFromPLDictionary(win_info
, sCommand
);
451 if (!cmd
|| !WMIsPLString(cmd
) || !(command
= WMGetFromPLString(cmd
))) {
455 value
= WMGetFromPLDictionary(win_info
, sName
);
459 ParseWindowName(value
, &instance
, &class, "session");
460 if (!instance
&& !class)
463 state
= getWindowState(scr
, win_info
);
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"))
474 for (j
= 0; j
< scr
->workspace_count
; j
++) {
475 if (strcmp(scr
->workspaces
[j
]->name
, tmp
) == 0) {
476 dock
= scr
->workspaces
[j
]->clip
;
481 if (dock
== NULL
) // Try the drawers
484 for (dc
= scr
->drawers
; dc
!= NULL
; dc
= dc
->next
)
486 if (strcmp(dc
->adrawer
->icon_array
[0]->wm_instance
, tmp
) == 0)
496 } else if (n
> 0 && n
<= scr
->workspace_count
) {
497 dock
= scr
->workspaces
[n
- 1]->clip
;
504 for (j
= 0; j
< dock
->max_icons
; j
++) {
505 btn
= dock
->icon_array
[j
];
506 if (btn
&& SAME(instance
, btn
->wm_instance
) &&
507 SAME(class, btn
->wm_class
) && SAME(command
, btn
->command
) && !btn
->launching
) {
515 wDockLaunchWithState(btn
, state
);
516 } else if ((pid
= execCommand(scr
, command
)) > 0) {
517 wWindowAddSavedState(instance
, class, command
, pid
, state
);
528 WMPLSetCaseSensitive(False
);
531 void wSessionRestoreLastWorkspace(WScreen
* scr
)
539 if (!scr
->session_state
)
542 WMPLSetCaseSensitive(True
);
544 wks
= WMGetFromPLDictionary(scr
->session_state
, sWorkspace
);
545 if (!wks
|| !WMIsPLString(wks
))
548 value
= WMGetFromPLString(wks
);
554 WMPLSetCaseSensitive(False
);
556 /* Get the workspace number for the workspace name */
557 w
= wGetWorkspaceNumber(scr
, value
);
559 if (w
!= scr
->current_workspace
&& w
< scr
->workspace_count
)
560 wWorkspaceChange(scr
, w
);