Fix periodic focus bug
[wmaker-crm.git] / src / shutdown.c
blob5beec3d6f0f3269ce4fb13fdbd6b34213ae1ac38
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>
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"
40 extern Atom _XA_WM_DELETE_WINDOW;
41 extern Time LastTimestamp;
42 extern int wScreenCount;
45 static void wipeDesktop(WScreen *scr);
49 *----------------------------------------------------------------------
50 * Shutdown-
51 * Exits the window manager cleanly. If mode is WSLogoutMode,
52 * the whole X session will be closed, by killing all clients if
53 * no session manager is running or by asking a shutdown to
54 * it if its present.
56 *----------------------------------------------------------------------
58 void
59 Shutdown(WShutdownMode mode)
61 int i;
63 switch (mode) {
64 case WSLogoutMode:
65 #ifdef XSMP_ENABLED
66 wSessionRequestShutdown();
67 break;
68 #else
69 /* fall through */
70 #endif
71 case WSKillMode:
72 case WSExitMode:
73 /* if there is no session manager, send SAVE_YOURSELF to
74 * the clients */
75 #if 0
76 #ifdef XSMP_ENABLED
77 if (!wSessionIsManaged())
78 #endif
79 for (i = 0; i < wScreenCount; i++) {
80 WScreen *scr;
82 scr = wScreenWithNumber(i);
83 if (scr) {
84 wSessionSendSaveYourself(scr);
87 #endif
88 for (i = 0; i < wScreenCount; i++) {
89 WScreen *scr;
91 scr = wScreenWithNumber(i);
92 if (scr) {
93 if (scr->helper_pid)
94 kill(scr->helper_pid, SIGKILL);
96 /* if the session is not being managed, save restart info */
97 #ifdef XSMP_ENABLED
98 if (!wSessionIsManaged())
99 #endif
100 wSessionSaveClients(scr);
102 wScreenSaveState(scr);
104 if (mode == WSKillMode)
105 wipeDesktop(scr);
106 else
107 RestoreDesktop(scr);
110 ExecExitScript();
111 Exit(0);
112 break;
114 case WSRestartPreparationMode:
115 for (i=0; i<wScreenCount; i++) {
116 WScreen *scr;
118 scr = wScreenWithNumber(i);
119 if (scr) {
120 if (scr->helper_pid)
121 kill(scr->helper_pid, SIGKILL);
122 wScreenSaveState(scr);
123 RestoreDesktop(scr);
126 break;
131 static void
132 restoreWindows(WMBag *bag, WMBagIterator iter)
134 WCoreWindow *next;
135 WCoreWindow *core;
136 WWindow *wwin;
139 if (iter == NULL) {
140 core = WMBagFirst(bag, &iter);
141 } else {
142 core = WMBagNext(bag, &iter);
145 if (core == NULL)
146 return;
148 restoreWindows(bag, iter);
150 /* go to the end of the list */
151 while (core->stacking->under)
152 core = core->stacking->under;
154 while (core) {
155 next = core->stacking->above;
157 if (core->descriptor.parent_type==WCLASS_WINDOW) {
158 Window window;
160 wwin = core->descriptor.parent;
161 window = wwin->client_win;
162 wUnmanageWindow(wwin, !wwin->flags.internal_window, False);
163 XMapWindow(dpy, window);
165 core = next;
171 *----------------------------------------------------------------------
172 * RestoreDesktop--
173 * Puts the desktop in a usable state when exiting.
175 * Side effects:
176 * All frame windows are removed and windows are reparented
177 * back to root. Windows that are outside the screen are
178 * brought to a viable place.
180 *----------------------------------------------------------------------
182 void
183 RestoreDesktop(WScreen *scr)
185 if (scr->helper_pid > 0) {
186 kill(scr->helper_pid, SIGTERM);
187 scr->helper_pid = 0;
190 XGrabServer(dpy);
191 wDestroyInspectorPanels();
193 /* reparent windows back to the root window, keeping the stacking order */
194 restoreWindows(scr->stacking_list, NULL);
196 XUngrabServer(dpy);
197 XSetInputFocus(dpy, PointerRoot, RevertToParent, CurrentTime);
198 wColormapInstallForWindow(scr, NULL);
199 PropCleanUp(scr->root_win);
200 wNETWMCleanup(scr);
201 XSync(dpy, 0);
206 *----------------------------------------------------------------------
207 * wipeDesktop--
208 * Kills all windows in a screen. Send DeleteWindow to all windows
209 * that support it and KillClient on all windows that don't.
211 * Side effects:
212 * All managed windows are closed.
214 * TODO: change to XQueryTree()
215 *----------------------------------------------------------------------
217 static void
218 wipeDesktop(WScreen *scr)
220 WWindow *wwin;
222 wwin = scr->focused_window;
223 while (wwin) {
224 if (wwin->protocols.DELETE_WINDOW)
225 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
226 else
227 wClientKill(wwin);
228 wwin = wwin->prev;
230 XSync(dpy, False);