Prevent crash when switchpanel is not initialised.
[wmaker-crm.git] / src / shutdown.c
blob67c39abd6f1794f9bb546ac8655e0ed63efecda7
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "wconfig.h"
23 #include <stdlib.h>
24 #include <signal.h>
25 #include <unistd.h>
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
30 #include "WindowMaker.h"
31 #include "window.h"
32 #include "client.h"
33 #include "funcs.h"
34 #include "main.h"
35 #include "properties.h"
36 #include "session.h"
37 #include "winspector.h"
38 #include "wmspec.h"
39 #include "colormap.h"
40 #include "shutdown.h"
42 extern Atom _XA_WM_DELETE_WINDOW;
43 extern Time LastTimestamp;
44 extern int wScreenCount;
46 static void wipeDesktop(WScreen * scr);
49 *----------------------------------------------------------------------
50 * Shutdown-
51 * Exits the window manager cleanly. If mode is WSLogoutMode,
52 * the whole X session will be closed, by killing all clients if
53 * no session manager is running or by asking a shutdown to
54 * it if its present.
56 *----------------------------------------------------------------------
58 void Shutdown(WShutdownMode mode)
60 int i;
61 #ifdef HAVE_INOTIFY
62 extern int inotifyFD;
63 #endif
65 switch (mode) {
66 case WSLogoutMode:
67 case WSKillMode:
68 case WSExitMode:
69 /* if there is no session manager, send SAVE_YOURSELF to
70 * the clients */
71 #ifdef HAVE_INOTIFY
72 close(inotifyFD);
73 #endif
74 for (i = 0; i < wScreenCount; i++) {
75 WScreen *scr;
77 scr = wScreenWithNumber(i);
78 if (scr) {
79 if (scr->helper_pid)
80 kill(scr->helper_pid, SIGKILL);
82 wScreenSaveState(scr);
84 if (mode == WSKillMode)
85 wipeDesktop(scr);
86 else
87 RestoreDesktop(scr);
90 ExecExitScript();
91 Exit(0);
92 break;
94 case WSRestartPreparationMode:
95 for (i = 0; i < wScreenCount; i++) {
96 WScreen *scr;
98 #ifdef HAVE_INOTIFY
99 close(inotifyFD);
100 #endif
101 scr = wScreenWithNumber(i);
102 if (scr) {
103 if (scr->helper_pid)
104 kill(scr->helper_pid, SIGKILL);
105 wScreenSaveState(scr);
106 RestoreDesktop(scr);
109 break;
113 static void restoreWindows(WMBag * bag, WMBagIterator iter)
115 WCoreWindow *next;
116 WCoreWindow *core;
117 WWindow *wwin;
119 if (iter == NULL) {
120 core = WMBagFirst(bag, &iter);
121 } else {
122 core = WMBagNext(bag, &iter);
125 if (core == NULL)
126 return;
128 restoreWindows(bag, iter);
130 /* go to the end of the list */
131 while (core->stacking->under)
132 core = core->stacking->under;
134 while (core) {
135 next = core->stacking->above;
137 if (core->descriptor.parent_type == WCLASS_WINDOW) {
138 Window window;
140 wwin = core->descriptor.parent;
141 window = wwin->client_win;
142 wUnmanageWindow(wwin, !wwin->flags.internal_window, False);
143 XMapWindow(dpy, window);
145 core = next;
150 *----------------------------------------------------------------------
151 * RestoreDesktop--
152 * Puts the desktop in a usable state when exiting.
154 * Side effects:
155 * All frame windows are removed and windows are reparented
156 * back to root. Windows that are outside the screen are
157 * brought to a viable place.
159 *----------------------------------------------------------------------
161 void RestoreDesktop(WScreen * scr)
163 if (scr->helper_pid > 0) {
164 kill(scr->helper_pid, SIGTERM);
165 scr->helper_pid = 0;
168 XGrabServer(dpy);
169 wDestroyInspectorPanels();
171 /* reparent windows back to the root window, keeping the stacking order */
172 restoreWindows(scr->stacking_list, NULL);
174 XUngrabServer(dpy);
175 XSetInputFocus(dpy, PointerRoot, RevertToParent, CurrentTime);
176 wColormapInstallForWindow(scr, NULL);
177 PropCleanUp(scr->root_win);
178 wNETWMCleanup(scr);
179 XSync(dpy, 0);
183 *----------------------------------------------------------------------
184 * wipeDesktop--
185 * Kills all windows in a screen. Send DeleteWindow to all windows
186 * that support it and KillClient on all windows that don't.
188 * Side effects:
189 * All managed windows are closed.
191 * TODO: change to XQueryTree()
192 *----------------------------------------------------------------------
194 static void wipeDesktop(WScreen * scr)
196 WWindow *wwin;
198 wwin = scr->focused_window;
199 while (wwin) {
200 if (wwin->protocols.DELETE_WINDOW)
201 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
202 else
203 wClientKill(wwin);
204 wwin = wwin->prev;
206 XSync(dpy, False);