Improve dockapp recognition
[wmaker-crm.git] / src / shutdown.c
blob8b5e86b9447089d09fd3b0c959a14c6c0494f325
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;
44 static void wipeDesktop(WScreen * scr);
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.
54 *----------------------------------------------------------------------
56 void Shutdown(WShutdownMode mode)
58 int i;
59 #ifdef HAVE_INOTIFY
60 extern int inotifyFD;
61 #endif
63 switch (mode) {
64 case WSLogoutMode:
65 case WSKillMode:
66 case WSExitMode:
67 /* if there is no session manager, send SAVE_YOURSELF to
68 * the clients */
69 #ifdef HAVE_INOTIFY
70 close(inotifyFD);
71 #endif
72 for (i = 0; i < wScreenCount; i++) {
73 WScreen *scr;
75 scr = wScreenWithNumber(i);
76 if (scr) {
77 if (scr->helper_pid)
78 kill(scr->helper_pid, SIGKILL);
80 /* if the session is not being managed, save restart info */
81 wSessionSaveClients(scr);
83 wScreenSaveState(scr);
85 if (mode == WSKillMode)
86 wipeDesktop(scr);
87 else
88 RestoreDesktop(scr);
91 ExecExitScript();
92 Exit(0);
93 break;
95 case WSRestartPreparationMode:
96 for (i = 0; i < wScreenCount; i++) {
97 WScreen *scr;
99 #ifdef HAVE_INOTIFY
100 close(inotifyFD);
101 #endif
102 scr = wScreenWithNumber(i);
103 if (scr) {
104 if (scr->helper_pid)
105 kill(scr->helper_pid, SIGKILL);
106 wScreenSaveState(scr);
107 RestoreDesktop(scr);
110 break;
114 static void restoreWindows(WMBag * bag, WMBagIterator iter)
116 WCoreWindow *next;
117 WCoreWindow *core;
118 WWindow *wwin;
120 if (iter == NULL) {
121 core = WMBagFirst(bag, &iter);
122 } else {
123 core = WMBagNext(bag, &iter);
126 if (core == NULL)
127 return;
129 restoreWindows(bag, iter);
131 /* go to the end of the list */
132 while (core->stacking->under)
133 core = core->stacking->under;
135 while (core) {
136 next = core->stacking->above;
138 if (core->descriptor.parent_type == WCLASS_WINDOW) {
139 Window window;
141 wwin = core->descriptor.parent;
142 window = wwin->client_win;
143 wUnmanageWindow(wwin, !wwin->flags.internal_window, False);
144 XMapWindow(dpy, window);
146 core = next;
151 *----------------------------------------------------------------------
152 * RestoreDesktop--
153 * Puts the desktop in a usable state when exiting.
155 * Side effects:
156 * All frame windows are removed and windows are reparented
157 * back to root. Windows that are outside the screen are
158 * brought to a viable place.
160 *----------------------------------------------------------------------
162 void RestoreDesktop(WScreen * scr)
164 if (scr->helper_pid > 0) {
165 kill(scr->helper_pid, SIGTERM);
166 scr->helper_pid = 0;
169 XGrabServer(dpy);
170 wDestroyInspectorPanels();
172 /* reparent windows back to the root window, keeping the stacking order */
173 restoreWindows(scr->stacking_list, NULL);
175 XUngrabServer(dpy);
176 XSetInputFocus(dpy, PointerRoot, RevertToParent, CurrentTime);
177 wColormapInstallForWindow(scr, NULL);
178 PropCleanUp(scr->root_win);
179 wNETWMCleanup(scr);
180 XSync(dpy, 0);
184 *----------------------------------------------------------------------
185 * wipeDesktop--
186 * Kills all windows in a screen. Send DeleteWindow to all windows
187 * that support it and KillClient on all windows that don't.
189 * Side effects:
190 * All managed windows are closed.
192 * TODO: change to XQueryTree()
193 *----------------------------------------------------------------------
195 static void wipeDesktop(WScreen * scr)
197 WWindow *wwin;
199 wwin = scr->focused_window;
200 while (wwin) {
201 if (wwin->protocols.DELETE_WINDOW)
202 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
203 else
204 wClientKill(wwin);
205 wwin = wwin->prev;
207 XSync(dpy, False);