Change to the linux kernel coding style
[wmaker-crm.git] / src / shutdown.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001/*
9af1c6c4 dan1998-10-21 14:43:47 +00002 * Window Maker window manager
6830b057 dan2004-10-12 21:28:27 +00003 *
4153e2fd dan2003-01-16 23:30:45 +00004 * Copyright (c) 1997-2003 Alfredo K. Kojima
6830b057 dan2004-10-12 21:28:27 +00005 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00006 * 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
6830b057 dan2004-10-12 21:28:27 +000018 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
9d2e6ef9 scottc1998-09-29 22:36:29 +000019 * USA.
20 */
21
22#include "wconfig.h"
23
24#include <stdlib.h>
0261c326 dan1999-01-06 15:22:33 +000025#include <signal.h>
26#include <unistd.h>
9d2e6ef9 scottc1998-09-29 22:36:29 +000027
28#include <X11/Xlib.h>
29#include <X11/Xutil.h>
9d2e6ef9 scottc1998-09-29 22:36:29 +000030
31#include "WindowMaker.h"
32#include "window.h"
33#include "client.h"
34#include "funcs.h"
35#include "properties.h"
52917e53 dan1999-04-16 20:05:26 +000036#include "session.h"
9d2e6ef9 scottc1998-09-29 22:36:29 +000037#include "winspector.h"
70555308 kojima2004-10-16 22:05:04 +000038#include "wmspec.h"
9d2e6ef9 scottc1998-09-29 22:36:29 +000039
40extern Atom _XA_WM_DELETE_WINDOW;
41extern Time LastTimestamp;
9007a657 dan1998-11-23 11:32:19 +000042extern int wScreenCount;
9d2e6ef9 scottc1998-09-29 22:36:29 +000043
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020044static void wipeDesktop(WScreen * scr);
0261c326 dan1999-01-06 15:22:33 +000045
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.
6830b057 dan2004-10-12 21:28:27 +000053 *
0261c326 dan1999-01-06 15:22:33 +000054 *----------------------------------------------------------------------
55 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020056void Shutdown(WShutdownMode mode)
0261c326 dan1999-01-06 15:22:33 +000057{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020058 int i;
59 extern int inotifyFD;
0261c326 dan1999-01-06 15:22:33 +000060
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020061 switch (mode) {
62 case WSLogoutMode:
416e3a82 dan1999-01-25 19:06:50 +000063#ifdef XSMP_ENABLED
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020064 wSessionRequestShutdown();
65 break;
0261c326 dan1999-01-06 15:22:33 +000066#else
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020067 /* fall through */
0261c326 dan1999-01-06 15:22:33 +000068#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020069 case WSKillMode:
70 case WSExitMode:
71 /* if there is no session manager, send SAVE_YOURSELF to
72 * the clients */
416e3a82 dan1999-01-25 19:06:50 +000073#if 0
74#ifdef XSMP_ENABLED
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020075 if (!wSessionIsManaged())
416e3a82 dan1999-01-25 19:06:50 +000076#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020077 for (i = 0; i < wScreenCount; i++) {
78 WScreen *scr;
79
80 scr = wScreenWithNumber(i);
81 if (scr) {
82 wSessionSendSaveYourself(scr);
83 }
84 }
416e3a82 dan1999-01-25 19:06:50 +000085#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020086 close(inotifyFD);
87 for (i = 0; i < wScreenCount; i++) {
88 WScreen *scr;
416e3a82 dan1999-01-25 19:06:50 +000089
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020090 scr = wScreenWithNumber(i);
91 if (scr) {
92 if (scr->helper_pid)
93 kill(scr->helper_pid, SIGKILL);
416e3a82 dan1999-01-25 19:06:50 +000094
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020095 /* if the session is not being managed, save restart info */
416e3a82 dan1999-01-25 19:06:50 +000096#ifdef XSMP_ENABLED
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020097 if (!wSessionIsManaged())
416e3a82 dan1999-01-25 19:06:50 +000098#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020099 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 }
0261c326 dan1999-01-06 15:22:33 +0000128}
129
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200130static void restoreWindows(WMBag * bag, WMBagIterator iter)
0b87b7fa kojima2000-04-08 23:53:22 +0000131{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200132 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 }
0b87b7fa kojima2000-04-08 23:53:22 +0000164}
ccbb6e8e dan2000-09-24 02:31:58 +0000165
9d2e6ef9 scottc1998-09-29 22:36:29 +0000166/*
167 *----------------------------------------------------------------------
168 * RestoreDesktop--
169 * Puts the desktop in a usable state when exiting.
9007a657 dan1998-11-23 11:32:19 +0000170 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000171 * Side effects:
172 * All frame windows are removed and windows are reparented
6830b057 dan2004-10-12 21:28:27 +0000173 * back to root. Windows that are outside the screen are
174 * brought to a viable place.
175 *
176 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000177 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200178void RestoreDesktop(WScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000179{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200180 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);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000197}
198
9d2e6ef9 scottc1998-09-29 22:36:29 +0000199/*
200 *----------------------------------------------------------------------
0261c326 dan1999-01-06 15:22:33 +0000201 * wipeDesktop--
9d2e6ef9 scottc1998-09-29 22:36:29 +0000202 * Kills all windows in a screen. Send DeleteWindow to all windows
203 * that support it and KillClient on all windows that don't.
6830b057 dan2004-10-12 21:28:27 +0000204 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000205 * Side effects:
206 * All managed windows are closed.
6830b057 dan2004-10-12 21:28:27 +0000207 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000208 * TODO: change to XQueryTree()
6830b057 dan2004-10-12 21:28:27 +0000209 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000210 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200211static void wipeDesktop(WScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000212{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200213 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);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000224}