Maximus: Use unsigned int for width/height variables
[wmaker-crm.git] / src / shutdown.c
blob0b308e2b2f5768de5d65c6a0d5d80340c135f50f
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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
22 #include "wconfig.h"
24 #include <stdlib.h>
25 #include <signal.h>
26 #include <unistd.h>
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
31 #include "WindowMaker.h"
32 #include "window.h"
33 #include "client.h"
34 #include "funcs.h"
35 #include "properties.h"
36 #include "session.h"
37 #include "winspector.h"
38 #include "wmspec.h"
40 extern Atom _XA_WM_DELETE_WINDOW;
41 extern Time LastTimestamp;
42 extern int wScreenCount;
44 static void wipeDesktop(WScreen * scr);
47 *----------------------------------------------------------------------
48 * Shutdown-
49 * Exits the window manager cleanly. If mode is WSLogoutMode,
50 * the whole X session will be closed, by killing all clients if
51 * no session manager is running or by asking a shutdown to
52 * it if its present.
54 *----------------------------------------------------------------------
56 void Shutdown(WShutdownMode mode)
58 int i;
59 extern int inotifyFD;
61 switch (mode) {
62 case WSLogoutMode:
63 #ifdef XSMP_ENABLED
64 wSessionRequestShutdown();
65 break;
66 #else
67 /* fall through */
68 #endif
69 case WSKillMode:
70 case WSExitMode:
71 /* if there is no session manager, send SAVE_YOURSELF to
72 * the clients */
73 #if 0
74 #ifdef XSMP_ENABLED
75 if (!wSessionIsManaged())
76 #endif
77 for (i = 0; i < wScreenCount; i++) {
78 WScreen *scr;
80 scr = wScreenWithNumber(i);
81 if (scr) {
82 wSessionSendSaveYourself(scr);
85 #endif
86 close(inotifyFD);
87 for (i = 0; i < wScreenCount; i++) {
88 WScreen *scr;
90 scr = wScreenWithNumber(i);
91 if (scr) {
92 if (scr->helper_pid)
93 kill(scr->helper_pid, SIGKILL);
95 /* if the session is not being managed, save restart info */
96 #ifdef XSMP_ENABLED
97 if (!wSessionIsManaged())
98 #endif
99 wSessionSaveClients(scr);
101 wScreenSaveState(scr);
103 if (mode == WSKillMode)
104 wipeDesktop(scr);
105 else
106 RestoreDesktop(scr);
109 ExecExitScript();
110 Exit(0);
111 break;
113 case WSRestartPreparationMode:
114 for (i = 0; i < wScreenCount; i++) {
115 WScreen *scr;
117 close(inotifyFD);
118 scr = wScreenWithNumber(i);
119 if (scr) {
120 if (scr->helper_pid)
121 kill(scr->helper_pid, SIGKILL);
122 wScreenSaveState(scr);
123 RestoreDesktop(scr);
126 break;
130 static void restoreWindows(WMBag * bag, WMBagIterator iter)
132 WCoreWindow *next;
133 WCoreWindow *core;
134 WWindow *wwin;
136 if (iter == NULL) {
137 core = WMBagFirst(bag, &iter);
138 } else {
139 core = WMBagNext(bag, &iter);
142 if (core == NULL)
143 return;
145 restoreWindows(bag, iter);
147 /* go to the end of the list */
148 while (core->stacking->under)
149 core = core->stacking->under;
151 while (core) {
152 next = core->stacking->above;
154 if (core->descriptor.parent_type == WCLASS_WINDOW) {
155 Window window;
157 wwin = core->descriptor.parent;
158 window = wwin->client_win;
159 wUnmanageWindow(wwin, !wwin->flags.internal_window, False);
160 XMapWindow(dpy, window);
162 core = next;
167 *----------------------------------------------------------------------
168 * RestoreDesktop--
169 * Puts the desktop in a usable state when exiting.
171 * Side effects:
172 * All frame windows are removed and windows are reparented
173 * back to root. Windows that are outside the screen are
174 * brought to a viable place.
176 *----------------------------------------------------------------------
178 void RestoreDesktop(WScreen * scr)
180 if (scr->helper_pid > 0) {
181 kill(scr->helper_pid, SIGTERM);
182 scr->helper_pid = 0;
185 XGrabServer(dpy);
186 wDestroyInspectorPanels();
188 /* reparent windows back to the root window, keeping the stacking order */
189 restoreWindows(scr->stacking_list, NULL);
191 XUngrabServer(dpy);
192 XSetInputFocus(dpy, PointerRoot, RevertToParent, CurrentTime);
193 wColormapInstallForWindow(scr, NULL);
194 PropCleanUp(scr->root_win);
195 wNETWMCleanup(scr);
196 XSync(dpy, 0);
200 *----------------------------------------------------------------------
201 * wipeDesktop--
202 * Kills all windows in a screen. Send DeleteWindow to all windows
203 * that support it and KillClient on all windows that don't.
205 * Side effects:
206 * All managed windows are closed.
208 * TODO: change to XQueryTree()
209 *----------------------------------------------------------------------
211 static void wipeDesktop(WScreen * scr)
213 WWindow *wwin;
215 wwin = scr->focused_window;
216 while (wwin) {
217 if (wwin->protocols.DELETE_WINDOW)
218 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
219 else
220 wClientKill(wwin);
221 wwin = wwin->prev;
223 XSync(dpy, False);