Added option to 'configure' to control debug information for compilation
[wmaker-crm.git] / src / shutdown.c
blobd86bd0d702122e9804ea5b20fc297497d3fb49b8
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"
41 extern Atom _XA_WM_DELETE_WINDOW;
42 extern Time LastTimestamp;
43 extern int wScreenCount;
45 static void wipeDesktop(WScreen * scr);
48 *----------------------------------------------------------------------
49 * Shutdown-
50 * Exits the window manager cleanly. If mode is WSLogoutMode,
51 * the whole X session will be closed, by killing all clients if
52 * no session manager is running or by asking a shutdown to
53 * it if its present.
55 *----------------------------------------------------------------------
57 void Shutdown(WShutdownMode mode)
59 int i;
60 #ifdef HAVE_INOTIFY
61 extern int inotifyFD;
62 #endif
64 switch (mode) {
65 case WSLogoutMode:
66 case WSKillMode:
67 case WSExitMode:
68 /* if there is no session manager, send SAVE_YOURSELF to
69 * the clients */
70 #ifdef HAVE_INOTIFY
71 close(inotifyFD);
72 #endif
73 for (i = 0; i < wScreenCount; i++) {
74 WScreen *scr;
76 scr = wScreenWithNumber(i);
77 if (scr) {
78 if (scr->helper_pid)
79 kill(scr->helper_pid, SIGKILL);
81 wScreenSaveState(scr);
83 if (mode == WSKillMode)
84 wipeDesktop(scr);
85 else
86 RestoreDesktop(scr);
89 ExecExitScript();
90 Exit(0);
91 break;
93 case WSRestartPreparationMode:
94 for (i = 0; i < wScreenCount; i++) {
95 WScreen *scr;
97 #ifdef HAVE_INOTIFY
98 close(inotifyFD);
99 #endif
100 scr = wScreenWithNumber(i);
101 if (scr) {
102 if (scr->helper_pid)
103 kill(scr->helper_pid, SIGKILL);
104 wScreenSaveState(scr);
105 RestoreDesktop(scr);
108 break;
112 static void restoreWindows(WMBag * bag, WMBagIterator iter)
114 WCoreWindow *next;
115 WCoreWindow *core;
116 WWindow *wwin;
118 if (iter == NULL) {
119 core = WMBagFirst(bag, &iter);
120 } else {
121 core = WMBagNext(bag, &iter);
124 if (core == NULL)
125 return;
127 restoreWindows(bag, iter);
129 /* go to the end of the list */
130 while (core->stacking->under)
131 core = core->stacking->under;
133 while (core) {
134 next = core->stacking->above;
136 if (core->descriptor.parent_type == WCLASS_WINDOW) {
137 Window window;
139 wwin = core->descriptor.parent;
140 window = wwin->client_win;
141 wUnmanageWindow(wwin, !wwin->flags.internal_window, False);
142 XMapWindow(dpy, window);
144 core = next;
149 *----------------------------------------------------------------------
150 * RestoreDesktop--
151 * Puts the desktop in a usable state when exiting.
153 * Side effects:
154 * All frame windows are removed and windows are reparented
155 * back to root. Windows that are outside the screen are
156 * brought to a viable place.
158 *----------------------------------------------------------------------
160 void RestoreDesktop(WScreen * scr)
162 if (scr->helper_pid > 0) {
163 kill(scr->helper_pid, SIGTERM);
164 scr->helper_pid = 0;
167 XGrabServer(dpy);
168 wDestroyInspectorPanels();
170 /* reparent windows back to the root window, keeping the stacking order */
171 restoreWindows(scr->stacking_list, NULL);
173 XUngrabServer(dpy);
174 XSetInputFocus(dpy, PointerRoot, RevertToParent, CurrentTime);
175 wColormapInstallForWindow(scr, NULL);
176 PropCleanUp(scr->root_win);
177 wNETWMCleanup(scr);
178 XSync(dpy, 0);
182 *----------------------------------------------------------------------
183 * wipeDesktop--
184 * Kills all windows in a screen. Send DeleteWindow to all windows
185 * that support it and KillClient on all windows that don't.
187 * Side effects:
188 * All managed windows are closed.
190 * TODO: change to XQueryTree()
191 *----------------------------------------------------------------------
193 static void wipeDesktop(WScreen * scr)
195 WWindow *wwin;
197 wwin = scr->focused_window;
198 while (wwin) {
199 if (wwin->protocols.DELETE_WINDOW)
200 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
201 else
202 wClientKill(wwin);
203 wwin = wwin->prev;
205 XSync(dpy, False);