Moving header functions to main.h
[wmaker-crm.git] / src / shutdown.c
blob6d87306f960ec25ea5b581d16b6dfe7b9e5e75a8
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"
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 #ifdef HAVE_INOTIFY
60 extern int inotifyFD;
61 #endif
63 switch (mode) {
64 case WSLogoutMode:
65 case WSKillMode:
66 case WSExitMode:
67 /* if there is no session manager, send SAVE_YOURSELF to
68 * the clients */
69 #ifdef HAVE_INOTIFY
70 close(inotifyFD);
71 #endif
72 for (i = 0; i < wScreenCount; i++) {
73 WScreen *scr;
75 scr = wScreenWithNumber(i);
76 if (scr) {
77 if (scr->helper_pid)
78 kill(scr->helper_pid, SIGKILL);
80 wScreenSaveState(scr);
82 if (mode == WSKillMode)
83 wipeDesktop(scr);
84 else
85 RestoreDesktop(scr);
88 ExecExitScript();
89 Exit(0);
90 break;
92 case WSRestartPreparationMode:
93 for (i = 0; i < wScreenCount; i++) {
94 WScreen *scr;
96 #ifdef HAVE_INOTIFY
97 close(inotifyFD);
98 #endif
99 scr = wScreenWithNumber(i);
100 if (scr) {
101 if (scr->helper_pid)
102 kill(scr->helper_pid, SIGKILL);
103 wScreenSaveState(scr);
104 RestoreDesktop(scr);
107 break;
111 static void restoreWindows(WMBag * bag, WMBagIterator iter)
113 WCoreWindow *next;
114 WCoreWindow *core;
115 WWindow *wwin;
117 if (iter == NULL) {
118 core = WMBagFirst(bag, &iter);
119 } else {
120 core = WMBagNext(bag, &iter);
123 if (core == NULL)
124 return;
126 restoreWindows(bag, iter);
128 /* go to the end of the list */
129 while (core->stacking->under)
130 core = core->stacking->under;
132 while (core) {
133 next = core->stacking->above;
135 if (core->descriptor.parent_type == WCLASS_WINDOW) {
136 Window window;
138 wwin = core->descriptor.parent;
139 window = wwin->client_win;
140 wUnmanageWindow(wwin, !wwin->flags.internal_window, False);
141 XMapWindow(dpy, window);
143 core = next;
148 *----------------------------------------------------------------------
149 * RestoreDesktop--
150 * Puts the desktop in a usable state when exiting.
152 * Side effects:
153 * All frame windows are removed and windows are reparented
154 * back to root. Windows that are outside the screen are
155 * brought to a viable place.
157 *----------------------------------------------------------------------
159 void RestoreDesktop(WScreen * scr)
161 if (scr->helper_pid > 0) {
162 kill(scr->helper_pid, SIGTERM);
163 scr->helper_pid = 0;
166 XGrabServer(dpy);
167 wDestroyInspectorPanels();
169 /* reparent windows back to the root window, keeping the stacking order */
170 restoreWindows(scr->stacking_list, NULL);
172 XUngrabServer(dpy);
173 XSetInputFocus(dpy, PointerRoot, RevertToParent, CurrentTime);
174 wColormapInstallForWindow(scr, NULL);
175 PropCleanUp(scr->root_win);
176 wNETWMCleanup(scr);
177 XSync(dpy, 0);
181 *----------------------------------------------------------------------
182 * wipeDesktop--
183 * Kills all windows in a screen. Send DeleteWindow to all windows
184 * that support it and KillClient on all windows that don't.
186 * Side effects:
187 * All managed windows are closed.
189 * TODO: change to XQueryTree()
190 *----------------------------------------------------------------------
192 static void wipeDesktop(WScreen * scr)
194 WWindow *wwin;
196 wwin = scr->focused_window;
197 while (wwin) {
198 if (wwin->protocols.DELETE_WINDOW)
199 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
200 else
201 wClientKill(wwin);
202 wwin = wwin->prev;
204 XSync(dpy, False);