wmaker: Moved variable Screen Count into the global namespace
[wmaker-crm.git] / src / shutdown.c
blobe6d3503bd9205b08464125ba3f1f69a54c742eeb
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 "main.h"
34 #include "properties.h"
35 #include "session.h"
36 #include "winspector.h"
37 #include "wmspec.h"
38 #include "colormap.h"
39 #include "shutdown.h"
42 static void wipeDesktop(WScreen * scr);
45 *----------------------------------------------------------------------
46 * Shutdown-
47 * Exits the window manager cleanly. If mode is WSLogoutMode,
48 * the whole X session will be closed, by killing all clients if
49 * no session manager is running or by asking a shutdown to
50 * it if its present.
52 *----------------------------------------------------------------------
54 void Shutdown(WShutdownMode mode)
56 int i;
57 #ifdef HAVE_INOTIFY
58 extern int inotifyFD;
59 #endif
61 switch (mode) {
62 case WSLogoutMode:
63 case WSKillMode:
64 case WSExitMode:
65 /* if there is no session manager, send SAVE_YOURSELF to
66 * the clients */
67 #ifdef HAVE_INOTIFY
68 close(inotifyFD);
69 #endif
70 for (i = 0; i < w_global.screen_count; i++) {
71 WScreen *scr;
73 scr = wScreenWithNumber(i);
74 if (scr) {
75 if (scr->helper_pid)
76 kill(scr->helper_pid, SIGKILL);
78 wScreenSaveState(scr);
80 if (mode == WSKillMode)
81 wipeDesktop(scr);
82 else
83 RestoreDesktop(scr);
86 ExecExitScript();
87 Exit(0);
88 break;
90 case WSRestartPreparationMode:
91 for (i = 0; i < w_global.screen_count; i++) {
92 WScreen *scr;
94 #ifdef HAVE_INOTIFY
95 close(inotifyFD);
96 #endif
97 scr = wScreenWithNumber(i);
98 if (scr) {
99 if (scr->helper_pid)
100 kill(scr->helper_pid, SIGKILL);
101 wScreenSaveState(scr);
102 RestoreDesktop(scr);
105 break;
109 static void restoreWindows(WMBag * bag, WMBagIterator iter)
111 WCoreWindow *next;
112 WCoreWindow *core;
113 WWindow *wwin;
115 if (iter == NULL) {
116 core = WMBagFirst(bag, &iter);
117 } else {
118 core = WMBagNext(bag, &iter);
121 if (core == NULL)
122 return;
124 restoreWindows(bag, iter);
126 /* go to the end of the list */
127 while (core->stacking->under)
128 core = core->stacking->under;
130 while (core) {
131 next = core->stacking->above;
133 if (core->descriptor.parent_type == WCLASS_WINDOW) {
134 Window window;
136 wwin = core->descriptor.parent;
137 window = wwin->client_win;
138 wUnmanageWindow(wwin, !wwin->flags.internal_window, False);
139 XMapWindow(dpy, window);
141 core = next;
146 *----------------------------------------------------------------------
147 * RestoreDesktop--
148 * Puts the desktop in a usable state when exiting.
150 * Side effects:
151 * All frame windows are removed and windows are reparented
152 * back to root. Windows that are outside the screen are
153 * brought to a viable place.
155 *----------------------------------------------------------------------
157 void RestoreDesktop(WScreen * scr)
159 if (scr->helper_pid > 0) {
160 kill(scr->helper_pid, SIGTERM);
161 scr->helper_pid = 0;
164 XGrabServer(dpy);
165 wDestroyInspectorPanels();
167 /* reparent windows back to the root window, keeping the stacking order */
168 restoreWindows(scr->stacking_list, NULL);
170 XUngrabServer(dpy);
171 XSetInputFocus(dpy, PointerRoot, RevertToParent, CurrentTime);
172 wColormapInstallForWindow(scr, NULL);
173 PropCleanUp(scr->root_win);
174 wNETWMCleanup(scr);
175 XSync(dpy, 0);
179 *----------------------------------------------------------------------
180 * wipeDesktop--
181 * Kills all windows in a screen. Send DeleteWindow to all windows
182 * that support it and KillClient on all windows that don't.
184 * Side effects:
185 * All managed windows are closed.
187 * TODO: change to XQueryTree()
188 *----------------------------------------------------------------------
190 static void wipeDesktop(WScreen * scr)
192 WWindow *wwin;
194 wwin = scr->focused_window;
195 while (wwin) {
196 if (wwin->protocols.DELETE_WINDOW)
197 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
198 w_global.timestamp.last_event);
199 else
200 wClientKill(wwin);
201 wwin = wwin->prev;
203 XSync(dpy, False);