Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / programs / wineboot / shutdown.c
blob6533df147d35dd5bda9c07ab17bc919de2cace93
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/unicode.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(wineboot);
31 struct window_info
33 HWND hwnd;
34 DWORD pid;
35 DWORD tid;
38 static UINT win_count;
39 static UINT win_max;
40 static struct window_info *windows;
41 static DWORD desktop_pid;
43 /* store a new window; callback for EnumWindows */
44 static BOOL CALLBACK enum_proc( HWND hwnd, LPARAM lp )
46 if (win_count >= win_max)
48 UINT new_count = win_max * 2;
49 struct window_info *new_win = HeapReAlloc( GetProcessHeap(), 0, windows,
50 new_count * sizeof(windows[0]) );
51 if (!new_win) return FALSE;
52 windows = new_win;
53 win_max = new_count;
55 windows[win_count].hwnd = hwnd;
56 windows[win_count].tid = GetWindowThreadProcessId( hwnd, &windows[win_count].pid );
57 win_count++;
58 return TRUE;
61 /* compare two window info structures; callback for qsort */
62 static int cmp_window( const void *ptr1, const void *ptr2 )
64 const struct window_info *info1 = ptr1;
65 const struct window_info *info2 = ptr2;
66 int ret = info1->pid - info2->pid;
67 if (!ret) ret = info1->tid - info2->tid;
68 return ret;
71 /* build the list of all windows (FIXME: handle multiple desktops) */
72 static BOOL get_all_windows(void)
74 win_count = 0;
75 win_max = 16;
76 windows = HeapAlloc( GetProcessHeap(), 0, win_max * sizeof(windows[0]) );
77 if (!windows) return FALSE;
78 if (!EnumWindows( enum_proc, 0 )) return FALSE;
79 /* sort windows by processes */
80 qsort( windows, win_count, sizeof(windows[0]), cmp_window );
81 return TRUE;
84 /* send WM_QUERYENDSESSION and WM_ENDSESSION to all windows of a given process */
85 /* FIXME: should display a confirmation dialog if process doesn't respond to the messages */
86 static DWORD_PTR send_end_session_messages( struct window_info *win, UINT count, UINT flags )
88 unsigned int i;
89 DWORD_PTR result, ret = 1;
91 /* don't kill the desktop process */
92 if (win[0].pid == desktop_pid) return 1;
94 for (i = 0; ret && i < count; i++)
96 if (SendMessageTimeoutW( win[i].hwnd, WM_QUERYENDSESSION, 0, 0, flags, 0, &result ))
98 WINE_TRACE( "sent MW_QUERYENDSESSION hwnd %p pid %04x result %ld\n",
99 win[i].hwnd, win[i].pid, result );
100 ret = result;
102 else win[i].hwnd = 0; /* ignore this window */
105 for (i = 0; i < count; i++)
107 if (!win[i].hwnd) continue;
108 WINE_TRACE( "sending WM_ENDSESSION hwnd %p pid %04x wp %ld\n", win[i].hwnd, win[i].pid, ret );
109 SendMessageTimeoutW( win[i].hwnd, WM_ENDSESSION, ret, 0, flags, 0, &result );
112 #if 0 /* CODEWEAVERS HACK: avoid killing winewrapper, other processes will be killed in kill_processes */
113 if (ret)
115 HANDLE handle = OpenProcess( PROCESS_TERMINATE, FALSE, win[0].pid );
116 if (handle)
118 WINE_TRACE( "terminating process %04x\n", win[0].pid );
119 TerminateProcess( handle, 0 );
120 CloseHandle( handle );
123 #endif
124 return ret;
127 /* close all top-level windows and terminate processes cleanly */
128 BOOL shutdown_close_windows( BOOL force )
130 UINT send_flags = force ? SMTO_ABORTIFHUNG : SMTO_NORMAL;
131 DWORD_PTR result = 1;
132 UINT i, n;
134 if (!get_all_windows()) return FALSE;
136 GetWindowThreadProcessId( GetDesktopWindow(), &desktop_pid );
138 for (i = n = 0; result && i < win_count; i++, n++)
140 if (n && windows[i-1].pid != windows[i].pid)
142 result = send_end_session_messages( windows + i - n, n, send_flags );
143 n = 0;
146 if (n && result)
147 result = send_end_session_messages( windows + win_count - n, n, send_flags );
149 HeapFree( GetProcessHeap(), 0, windows );
151 return (result != 0);
154 /* forcibly kill all processes without any cleanup */
155 void kill_processes( BOOL kill_desktop )
157 BOOL res;
158 UINT killed;
159 HANDLE handle, snapshot;
160 PROCESSENTRY32W process;
162 GetWindowThreadProcessId( GetDesktopWindow(), &desktop_pid );
166 if (!(snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ))) break;
168 killed = 0;
169 process.dwSize = sizeof(process);
170 for (res = Process32FirstW( snapshot, &process ); res; res = Process32NextW( snapshot, &process ))
172 static const WCHAR winewrapperW[] = {'w','i','n','e','w','r','a','p','p','e','r',0};
173 if (process.th32ProcessID == GetCurrentProcessId()) continue;
174 if (process.th32ProcessID == desktop_pid) continue;
175 /* CODEWEAVERS HACK: don't kill winewrapper so end-of-installation
176 * detection works properly */
177 if (strstrW( process.szExeFile, winewrapperW )) continue;
178 WINE_TRACE("killing process %04x %s\n",
179 process.th32ProcessID, wine_dbgstr_w(process.szExeFile) );
180 if (!(handle = OpenProcess( PROCESS_TERMINATE, FALSE, process.th32ProcessID )))
181 continue;
182 if (TerminateProcess( handle, 0 )) killed++;
183 CloseHandle( handle );
185 CloseHandle( snapshot );
186 } while (killed > 0);
188 if (desktop_pid && kill_desktop) /* do this last */
190 if ((handle = OpenProcess( PROCESS_TERMINATE, FALSE, desktop_pid )))
192 TerminateProcess( handle, 0 );
193 CloseHandle( handle );