Change to the linux kernel coding style
[wmaker-crm.git] / src / shutdown.c
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997-2003 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.
10 *
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.
15 *
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.
20 */
21
22 #include "wconfig.h"
23
24 #include <stdlib.h>
25 #include <signal.h>
26 #include <unistd.h>
27
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
30
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"
39
40 extern Atom _XA_WM_DELETE_WINDOW;
41 extern Time LastTimestamp;
42 extern int wScreenCount;
43
44 static void wipeDesktop(WScreen * scr);
45
46 /*
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.
53 *
54 *----------------------------------------------------------------------
55 */
56 void Shutdown(WShutdownMode mode)
57 {
58 int i;
59 extern int inotifyFD;
60
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;
79
80 scr = wScreenWithNumber(i);
81 if (scr) {
82 wSessionSendSaveYourself(scr);
83 }
84 }
85 #endif
86 close(inotifyFD);
87 for (i = 0; i < wScreenCount; i++) {
88 WScreen *scr;
89
90 scr = wScreenWithNumber(i);
91 if (scr) {
92 if (scr->helper_pid)
93 kill(scr->helper_pid, SIGKILL);
94
95 /* if the session is not being managed, save restart info */
96 #ifdef XSMP_ENABLED
97 if (!wSessionIsManaged())
98 #endif
99 wSessionSaveClients(scr);
100
101 wScreenSaveState(scr);
102
103 if (mode == WSKillMode)
104 wipeDesktop(scr);
105 else
106 RestoreDesktop(scr);
107 }
108 }
109 ExecExitScript();
110 Exit(0);
111 break;
112
113 case WSRestartPreparationMode:
114 for (i = 0; i < wScreenCount; i++) {
115 WScreen *scr;
116
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);
124 }
125 }
126 break;
127 }
128 }
129
130 static void restoreWindows(WMBag * bag, WMBagIterator iter)
131 {
132 WCoreWindow *next;
133 WCoreWindow *core;
134 WWindow *wwin;
135
136 if (iter == NULL) {
137 core = WMBagFirst(bag, &iter);
138 } else {
139 core = WMBagNext(bag, &iter);
140 }
141
142 if (core == NULL)
143 return;
144
145 restoreWindows(bag, iter);
146
147 /* go to the end of the list */
148 while (core->stacking->under)
149 core = core->stacking->under;
150
151 while (core) {
152 next = core->stacking->above;
153
154 if (core->descriptor.parent_type == WCLASS_WINDOW) {
155 Window window;
156
157 wwin = core->descriptor.parent;
158 window = wwin->client_win;
159 wUnmanageWindow(wwin, !wwin->flags.internal_window, False);
160 XMapWindow(dpy, window);
161 }
162 core = next;
163 }
164 }
165
166 /*
167 *----------------------------------------------------------------------
168 * RestoreDesktop--
169 * Puts the desktop in a usable state when exiting.
170 *
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.
175 *
176 *----------------------------------------------------------------------
177 */
178 void RestoreDesktop(WScreen * scr)
179 {
180 if (scr->helper_pid > 0) {
181 kill(scr->helper_pid, SIGTERM);
182 scr->helper_pid = 0;
183 }
184
185 XGrabServer(dpy);
186 wDestroyInspectorPanels();
187
188 /* reparent windows back to the root window, keeping the stacking order */
189 restoreWindows(scr->stacking_list, NULL);
190
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);
197 }
198
199 /*
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.
204 *
205 * Side effects:
206 * All managed windows are closed.
207 *
208 * TODO: change to XQueryTree()
209 *----------------------------------------------------------------------
210 */
211 static void wipeDesktop(WScreen * scr)
212 {
213 WWindow *wwin;
214
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;
222 }
223 XSync(dpy, False);
224 }