- Added support for NET_WM_NAME/NET_WM_ICON_NAME
[wmaker-crm.git] / src / shutdown.c
blobd860b5dac31fd59adf1106c2f0814d2cb944b7c7
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>
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 "session.h"
38 #include "winspector.h"
39 #include "wmspec.h"
41 extern Atom _XA_WM_DELETE_WINDOW;
42 extern Time LastTimestamp;
43 extern int wScreenCount;
46 static void wipeDesktop(WScreen *scr);
50 *----------------------------------------------------------------------
51 * Shutdown-
52 * Exits the window manager cleanly. If mode is WSLogoutMode,
53 * the whole X session will be closed, by killing all clients if
54 * no session manager is running or by asking a shutdown to
55 * it if its present.
57 *----------------------------------------------------------------------
59 void
60 Shutdown(WShutdownMode mode)
62 int i;
64 switch (mode) {
65 case WSLogoutMode:
66 #ifdef XSMP_ENABLED
67 wSessionRequestShutdown();
68 break;
69 #else
70 /* fall through */
71 #endif
72 case WSKillMode:
73 case WSExitMode:
74 /* if there is no session manager, send SAVE_YOURSELF to
75 * the clients */
76 #if 0
77 #ifdef XSMP_ENABLED
78 if (!wSessionIsManaged())
79 #endif
80 for (i = 0; i < wScreenCount; i++) {
81 WScreen *scr;
83 scr = wScreenWithNumber(i);
84 if (scr) {
85 wSessionSendSaveYourself(scr);
88 #endif
89 for (i = 0; i < wScreenCount; i++) {
90 WScreen *scr;
92 scr = wScreenWithNumber(i);
93 if (scr) {
94 if (scr->helper_pid)
95 kill(scr->helper_pid, SIGKILL);
97 /* if the session is not being managed, save restart info */
98 #ifdef XSMP_ENABLED
99 if (!wSessionIsManaged())
100 #endif
101 wSessionSaveClients(scr);
103 wScreenSaveState(scr);
105 if (mode == WSKillMode)
106 wipeDesktop(scr);
107 else
108 RestoreDesktop(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 wScreenSaveState(scr);
124 RestoreDesktop(scr);
127 break;
132 static void
133 restoreWindows(WMBag *bag, WMBagIterator iter)
135 WCoreWindow *next;
136 WCoreWindow *core;
137 WWindow *wwin;
140 if (iter == NULL) {
141 core = WMBagFirst(bag, &iter);
142 } else {
143 core = WMBagNext(bag, &iter);
146 if (core == NULL)
147 return;
149 restoreWindows(bag, iter);
151 /* go to the end of the list */
152 while (core->stacking->under)
153 core = core->stacking->under;
155 while (core) {
156 next = core->stacking->above;
158 if (core->descriptor.parent_type==WCLASS_WINDOW) {
159 Window window;
161 wwin = core->descriptor.parent;
162 window = wwin->client_win;
163 wUnmanageWindow(wwin, !wwin->flags.internal_window, False);
164 XMapWindow(dpy, window);
166 core = next;
172 *----------------------------------------------------------------------
173 * RestoreDesktop--
174 * Puts the desktop in a usable state when exiting.
176 * Side effects:
177 * All frame windows are removed and windows are reparented
178 * back to root. Windows that are outside the screen are
179 * brought to a viable place.
181 *----------------------------------------------------------------------
183 void
184 RestoreDesktop(WScreen *scr)
186 if (scr->helper_pid > 0) {
187 kill(scr->helper_pid, SIGTERM);
188 scr->helper_pid = 0;
191 XGrabServer(dpy);
192 wDestroyInspectorPanels();
194 /* reparent windows back to the root window, keeping the stacking order */
195 restoreWindows(scr->stacking_list, NULL);
197 XUngrabServer(dpy);
198 XSetInputFocus(dpy, PointerRoot, RevertToParent, CurrentTime);
199 wColormapInstallForWindow(scr, NULL);
200 PropCleanUp(scr->root_win);
201 wNETWMCleanup(scr);
202 XSync(dpy, 0);
207 *----------------------------------------------------------------------
208 * wipeDesktop--
209 * Kills all windows in a screen. Send DeleteWindow to all windows
210 * that support it and KillClient on all windows that don't.
212 * Side effects:
213 * All managed windows are closed.
215 * TODO: change to XQueryTree()
216 *----------------------------------------------------------------------
218 static void
219 wipeDesktop(WScreen *scr)
221 WWindow *wwin;
223 wwin = scr->focused_window;
224 while (wwin) {
225 if (wwin->protocols.DELETE_WINDOW)
226 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
227 else
228 wClientKill(wwin);
229 wwin = wwin->prev;
231 XSync(dpy, False);