Update to Window Maker 0.50.2
[wmaker-crm.git] / src / shutdown.c
blob79bfad400acb4e365cd10d9d065847b5742138d2
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 *
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>
30 #include <X11/Intrinsic.h>
32 #include "WindowMaker.h"
33 #include "window.h"
34 #include "client.h"
35 #include "funcs.h"
36 #include "properties.h"
37 #include "winspector.h"
38 #ifdef KWM_HINTS
39 #include "kwm.h"
40 #endif
42 extern Atom _XA_WM_DELETE_WINDOW;
43 extern Time LastTimestamp;
44 extern int wScreenCount;
47 static void wipeDesktop(WScreen *scr);
51 *----------------------------------------------------------------------
52 * Shutdown-
53 * Exits the window manager cleanly. If mode is WSLogoutMode,
54 * the whole X session will be closed, by killing all clients if
55 * no session manager is running or by asking a shutdown to
56 * it if its present.
58 *----------------------------------------------------------------------
60 void
61 Shutdown(WShutdownMode mode)
63 int i;
65 switch (mode) {
66 case WSExitMode:
67 for (i=0; i<wScreenCount; i++) {
68 WScreen *scr;
70 scr = wScreenWithNumber(i);
71 if (scr) {
72 if (scr->helper_pid)
73 kill(scr->helper_pid, SIGKILL);
75 #ifdef KWM_HINTS
76 wKWMShutdown(scr, True);
77 #endif
78 wScreenSaveState(scr);
80 RestoreDesktop(scr);
84 ExecExitScript();
85 Exit(0);
86 break;
88 case WSLogoutMode:
89 #ifdef R6SM
90 wSessionRequestShutdown();
91 break;
92 #else
93 /* fall through */
94 #endif
95 case WSKillMode:
96 for (i=0; i<wScreenCount; i++) {
97 WScreen *scr;
99 scr = wScreenWithNumber(i);
100 if (scr) {
101 if (scr->helper_pid)
102 kill(scr->helper_pid, SIGKILL);
103 #ifdef KWM_HINTS
104 wKWMShutdown(scr, True);
105 #endif
106 wScreenSaveState(scr);
108 wipeDesktop(scr);
111 ExecExitScript();
112 Exit(0);
113 break;
115 case WSRestartPreparationMode:
116 for (i=0; i<wScreenCount; i++) {
117 WScreen *scr;
119 scr = wScreenWithNumber(i);
120 if (scr) {
121 if (scr->helper_pid)
122 kill(scr->helper_pid, SIGKILL);
123 #ifdef KWM_HINTS
124 wKWMShutdown(scr, False);
125 #endif
126 wScreenSaveState(scr);
128 RestoreDesktop(scr);
131 break;
139 *----------------------------------------------------------------------
140 * RestoreDesktop--
141 * Puts the desktop in a usable state when exiting.
143 * Side effects:
144 * All frame windows are removed and windows are reparented
145 * back to root. Windows that are outside the screen are
146 * brought to a viable place.
148 *----------------------------------------------------------------------
150 void
151 RestoreDesktop(WScreen *scr)
153 int i;
155 if (scr->helper_pid) {
156 kill(scr->helper_pid, SIGTERM);
157 scr->helper_pid = 0;
160 XGrabServer(dpy);
161 wDestroyInspectorPanels();
163 /* reparent windows back to the root window, keeping the stacking order */
164 for (i=0; i<MAX_WINDOW_LEVELS; i++) {
165 WCoreWindow *core, *next;
166 WWindow *wwin;
168 if (!scr->stacking_list[i])
169 continue;
171 core = scr->stacking_list[i];
172 /* go to the end of the list */
173 while (core->stacking->under)
174 core = core->stacking->under;
176 while (core) {
177 next = core->stacking->above;
179 if (core->descriptor.parent_type==WCLASS_WINDOW) {
180 wwin = core->descriptor.parent;
181 wwin->flags.mapped=1;
182 wUnmanageWindow(wwin, !wwin->flags.internal_window);
184 core = next;
188 XUngrabServer(dpy);
189 XSetInputFocus(dpy, PointerRoot, RevertToParent, CurrentTime);
190 wColormapInstallForWindow(scr, NULL);
191 PropCleanUp(scr->root_win);
192 XSync(dpy, 0);
197 *----------------------------------------------------------------------
198 * wipeDesktop--
199 * Kills all windows in a screen. Send DeleteWindow to all windows
200 * that support it and KillClient on all windows that don't.
202 * Side effects:
203 * All managed windows are closed.
205 * TODO: change to XQueryTree()
206 *----------------------------------------------------------------------
208 static void
209 wipeDesktop(WScreen *scr)
211 WWindow *wwin;
213 wwin = scr->focused_window;
214 while (wwin) {
215 if (wwin->protocols.DELETE_WINDOW)
216 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
217 else
218 wClientKill(wwin);
219 wwin = wwin->prev;
221 XSync(dpy, False);