push aea352fc3df615e3f4b48daf6f897ea93ad1fffd
[wine/hacks.git] / programs / wineboot / shutdown.c
blob5f8dadf1c1e4edfbd56182d563ce437df01c80f0
1 /*
2 * Copyright (C) 2006 Alexandre Julliard
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include <stdlib.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winuser.h"
25 #include "tlhelp32.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(wineboot);
30 struct window_info
32 HWND hwnd;
33 DWORD pid;
34 DWORD tid;
37 static UINT win_count;
38 static UINT win_max;
39 static struct window_info *windows;
40 static DWORD desktop_pid;
42 /* store a new window; callback for EnumWindows */
43 static BOOL CALLBACK enum_proc( HWND hwnd, LPARAM lp )
45 if (win_count >= win_max)
47 UINT new_count = win_max * 2;
48 struct window_info *new_win = HeapReAlloc( GetProcessHeap(), 0, windows,
49 new_count * sizeof(windows[0]) );
50 if (!new_win) return FALSE;
51 windows = new_win;
52 win_max = new_count;
54 windows[win_count].hwnd = hwnd;
55 windows[win_count].tid = GetWindowThreadProcessId( hwnd, &windows[win_count].pid );
56 win_count++;
57 return TRUE;
60 /* compare two window info structures; callback for qsort */
61 static int cmp_window( const void *ptr1, const void *ptr2 )
63 const struct window_info *info1 = ptr1;
64 const struct window_info *info2 = ptr2;
65 int ret = info1->pid - info2->pid;
66 if (!ret) ret = info1->tid - info2->tid;
67 return ret;
70 /* build the list of all windows (FIXME: handle multiple desktops) */
71 static BOOL get_all_windows(void)
73 win_count = 0;
74 win_max = 16;
75 windows = HeapAlloc( GetProcessHeap(), 0, win_max * sizeof(windows[0]) );
76 if (!windows) return FALSE;
77 if (!EnumWindows( enum_proc, 0 )) return FALSE;
78 /* sort windows by processes */
79 qsort( windows, win_count, sizeof(windows[0]), cmp_window );
80 return TRUE;
83 /* send WM_QUERYENDSESSION and WM_ENDSESSION to all windows of a given process */
84 /* FIXME: should display a confirmation dialog if process doesn't respond to the messages */
85 static DWORD_PTR send_end_session_messages( struct window_info *win, UINT count, UINT flags )
87 unsigned int i;
88 DWORD_PTR result, ret = 1;
90 /* don't kill the desktop process */
91 if (win[0].pid == desktop_pid) return 1;
93 for (i = 0; ret && i < count; i++)
95 if (SendMessageTimeoutW( win[i].hwnd, WM_QUERYENDSESSION, 0, 0, flags, 0, &result ))
97 WINE_TRACE( "sent MW_QUERYENDSESSION hwnd %p pid %04x result %ld\n",
98 win[i].hwnd, win[i].pid, result );
99 ret = result;
101 else win[i].hwnd = 0; /* ignore this window */
104 for (i = 0; i < count; i++)
106 if (!win[i].hwnd) continue;
107 WINE_TRACE( "sending WM_ENDSESSION hwnd %p pid %04x wp %ld\n", win[i].hwnd, win[i].pid, ret );
108 SendMessageTimeoutW( win[i].hwnd, WM_ENDSESSION, ret, 0, flags, 0, &result );
111 if (ret)
113 HANDLE handle = OpenProcess( PROCESS_TERMINATE, FALSE, win[0].pid );
114 if (handle)
116 WINE_TRACE( "terminating process %04x\n", win[0].pid );
117 TerminateProcess( handle, 0 );
118 CloseHandle( handle );
121 return ret;
124 /* close all top-level windows and terminate processes cleanly */
125 BOOL shutdown_close_windows( BOOL force )
127 UINT send_flags = force ? SMTO_ABORTIFHUNG : SMTO_NORMAL;
128 DWORD_PTR result = 1;
129 UINT i, n;
131 if (!get_all_windows()) return FALSE;
133 GetWindowThreadProcessId( GetDesktopWindow(), &desktop_pid );
135 for (i = n = 0; result && i < win_count; i++, n++)
137 if (n && windows[i-1].pid != windows[i].pid)
139 result = send_end_session_messages( windows + i - n, n, send_flags );
140 n = 0;
143 if (n && result)
144 result = send_end_session_messages( windows + win_count - n, n, send_flags );
146 HeapFree( GetProcessHeap(), 0, windows );
148 return (result != 0);
151 /* forcibly kill all processes without any cleanup */
152 void kill_processes( BOOL kill_desktop )
154 BOOL res;
155 UINT killed;
156 HANDLE handle, snapshot;
157 PROCESSENTRY32W process;
159 GetWindowThreadProcessId( GetDesktopWindow(), &desktop_pid );
163 if (!(snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ))) break;
165 killed = 0;
166 process.dwSize = sizeof(process);
167 for (res = Process32FirstW( snapshot, &process ); res; res = Process32NextW( snapshot, &process ))
169 if (process.th32ProcessID == GetCurrentProcessId()) continue;
170 if (process.th32ProcessID == desktop_pid) continue;
171 WINE_TRACE("killing process %04x %s\n",
172 process.th32ProcessID, wine_dbgstr_w(process.szExeFile) );
173 if (!(handle = OpenProcess( PROCESS_TERMINATE, FALSE, process.th32ProcessID )))
174 continue;
175 if (TerminateProcess( handle, 0 )) killed++;
176 CloseHandle( handle );
178 CloseHandle( snapshot );
179 } while (killed > 0);
181 if (desktop_pid && kill_desktop) /* do this last */
183 if ((handle = OpenProcess( PROCESS_TERMINATE, FALSE, desktop_pid )))
185 TerminateProcess( handle, 0 );
186 CloseHandle( handle );