skins2: use new vout_window_ReportSize
[vlc.git] / bin / winvlc.c
blob7fdf8dd25352b62fb8089e259e3d107d461d85d5
1 /*****************************************************************************
2 * winvlc.c: the Windows VLC media player
3 *****************************************************************************
4 * Copyright (C) 1998-2011 the VideoLAN team
6 * Authors: Vincent Seguin <seguin@via.ecp.fr>
7 * Samuel Hocevar <sam@zoy.org>
8 * Gildas Bazin <gbazin@videolan.org>
9 * Derk-Jan Hartman <hartman at videolan dot org>
10 * Lots of other people, see the libvlc AUTHORS file
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #ifndef UNICODE
32 #define UNICODE
33 #endif
35 #include <vlc/vlc.h>
36 #include <windows.h>
37 #include <shellapi.h>
39 #ifndef _WIN32_IE
40 # define _WIN32_IE 0x501
41 #endif
42 #include <fcntl.h>
43 #include <io.h>
44 #include <shlobj.h>
45 #include <wininet.h>
46 #define PSAPI_VERSION 1
47 #include <psapi.h>
48 #define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
49 static void check_crashdump(void);
50 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
51 static const wchar_t *crashdump_path;
53 static char *FromWide (const wchar_t *wide)
55 size_t len;
56 len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
58 char *out = (char *)malloc (len);
59 if (out)
60 WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
61 return out;
64 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
65 LPSTR lpCmdLine,
66 int nCmdShow )
68 int argc;
70 /* VLC does not change the thread locale, so gettext/libintil will use the
71 * user default locale as reference. */
72 /* gettext versions 0.18-0.18.1 will use the Windows Vista locale name
73 * if the GETTEXT_MUI environment variable is set. If not set or if running
74 * on Windows 2000/XP/2003 an hard-coded language ID list is used. This
75 * putenv() call may become redundant with later versions of gettext. */
76 putenv("GETTEXT_MUI=1");
77 #ifdef TOP_BUILDDIR
78 putenv("VLC_PLUGIN_PATH=Z:"TOP_BUILDDIR"/modules");
79 putenv("VLC_DATA_PATH=Z:"TOP_SRCDIR"/share");
80 #endif
82 SetErrorMode(SEM_FAILCRITICALERRORS);
83 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
85 /* SetProcessDEPPolicy */
86 HINSTANCE h_Kernel32 = LoadLibraryW(L"kernel32.dll");
87 if(h_Kernel32)
89 BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
90 BOOL (WINAPI * mySetDllDirectoryA)(const char* lpPathName);
91 # define PROCESS_DEP_ENABLE 1
93 mySetProcessDEPPolicy = (BOOL WINAPI (*)(DWORD))
94 GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
95 if(mySetProcessDEPPolicy)
96 mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
98 /* Do NOT load any library from cwd. */
99 mySetDllDirectoryA = (BOOL WINAPI (*)(const char*))
100 GetProcAddress(h_Kernel32, "SetDllDirectoryA");
101 if(mySetDllDirectoryA)
102 mySetDllDirectoryA("");
104 FreeLibrary(h_Kernel32);
107 /* Args */
108 wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
109 if (wargv == NULL)
110 return 1;
112 char *argv[argc + 4];
113 BOOL crash_handling = TRUE;
114 int j = 0;
115 char *lang = NULL;
117 argv[j++] = FromWide( L"--media-library" );
118 argv[j++] = FromWide( L"--stats" );
119 argv[j++] = FromWide( L"--no-ignore-config" );
120 for (int i = 1; i < argc; i++)
122 if(!wcscmp(wargv[i], L"--no-crashdump"))
124 crash_handling = FALSE;
125 continue; /* don't give argument to libvlc */
127 if (!wcsncmp(wargv[i], L"--language", 10) )
129 if (i < argc - 1 && wcsncmp( wargv[i + 1], L"--", 2 ))
130 lang = FromWide (wargv[++i]);
131 continue;
134 argv[j++] = FromWide (wargv[i]);
137 argc = j;
138 argv[argc] = NULL;
139 LocalFree (wargv);
141 if(crash_handling)
143 static wchar_t path[MAX_PATH];
144 if( S_OK != SHGetFolderPathW( NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
145 NULL, SHGFP_TYPE_CURRENT, path ) )
146 fprintf( stderr, "Can't open the vlc conf PATH\n" );
147 _snwprintf( path+wcslen( path ), MAX_PATH, L"%s", L"\\vlc\\crashdump" );
148 crashdump_path = &path[0];
150 check_crashdump();
151 SetUnhandledExceptionFilter(vlc_exception_filter);
154 _setmode( STDIN_FILENO, _O_BINARY ); /* Needed for pipes */
156 /* */
157 if (!lang)
159 HKEY h_key;
160 if( RegOpenKeyEx( HKEY_CURRENT_USER, TEXT("Software\\VideoLAN\\VLC\\"), 0, KEY_READ, &h_key )
161 == ERROR_SUCCESS )
163 TCHAR szData[256];
164 DWORD len = 256;
165 if( RegQueryValueEx( h_key, TEXT("Lang"), NULL, NULL, (LPBYTE) &szData, &len ) == ERROR_SUCCESS )
166 lang = FromWide( szData );
170 if (lang && strncmp( lang, "auto", 4 ) )
172 char tmp[11];
173 snprintf(tmp, 11, "LANG=%s", lang);
174 putenv(tmp);
176 free(lang);
178 /* Initialize libvlc */
179 libvlc_instance_t *vlc;
180 vlc = libvlc_new (argc, (const char **)argv);
181 if (vlc != NULL)
183 libvlc_set_app_id (vlc, "org.VideoLAN.VLC", PACKAGE_VERSION,
184 PACKAGE_NAME);
185 libvlc_set_user_agent (vlc, "VLC media player", "VLC/"PACKAGE_VERSION);
186 libvlc_add_intf (vlc, "hotkeys,none");
187 libvlc_add_intf (vlc, "globalhotkeys,none");
188 libvlc_add_intf (vlc, NULL);
189 libvlc_playlist_play (vlc, -1, 0, NULL);
190 libvlc_wait (vlc);
191 libvlc_release (vlc);
193 else
194 MessageBox (NULL, TEXT("VLC media player could not start.\n"
195 "Either the command line options were invalid or no plugins were found.\n"),
196 TEXT("VLC media player"),
197 MB_OK|MB_ICONERROR);
200 for (int i = 0; i < argc; i++)
201 free (argv[i]);
203 (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
204 return 0;
207 /* Crashdumps handling */
208 static void check_crashdump(void)
210 wchar_t mv_crashdump_path[MAX_PATH];
211 wcscpy (mv_crashdump_path, crashdump_path);
212 wcscat (mv_crashdump_path, L".mv");
214 if (_wrename (crashdump_path, mv_crashdump_path))
215 return;
217 FILE * fd = _wfopen ( mv_crashdump_path, L"r, ccs=UTF-8" );
218 if( !fd )
219 return;
220 fclose( fd );
222 int answer = MessageBox( NULL, L"Ooops: VLC media player just crashed.\n" \
223 "Would you like to send a bug report to the developers team?",
224 L"VLC crash reporting", MB_YESNO);
226 if(answer == IDYES)
228 HINTERNET Hint = InternetOpen(L"VLC Crash Reporter",
229 INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
230 if(Hint)
232 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org",
233 INTERNET_DEFAULT_FTP_PORT, NULL, NULL,
234 INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
235 if(ftp)
237 SYSTEMTIME now;
238 GetSystemTime(&now);
239 wchar_t remote_file[MAX_PATH];
240 _snwprintf(remote_file, MAX_PATH,
241 L"/crashes-win32/%04d%02d%02d%02d%02d%02d",
242 now.wYear, now.wMonth, now.wDay, now.wHour,
243 now.wMinute, now.wSecond );
245 if( FtpPutFile( ftp, mv_crashdump_path, remote_file,
246 FTP_TRANSFER_TYPE_BINARY, 0) )
247 MessageBox( NULL, L"Report sent correctly. Thanks a lot " \
248 "for the help.", L"Report sent", MB_OK);
249 else
250 MessageBox( NULL, L"There was an error while "\
251 "transferring the data to the FTP server.\n"\
252 "Thanks a lot for the help.",
253 L"Report sending failed", MB_OK);
254 InternetCloseHandle(ftp);
256 else
258 MessageBox( NULL, L"There was an error while connecting to " \
259 "the FTP server. "\
260 "Thanks a lot for the help.",
261 L"Report sending failed", MB_OK);
262 fprintf(stderr,"Can't connect to FTP server 0x%08lu\n",
263 (unsigned long)GetLastError());
265 InternetCloseHandle(Hint);
267 else
269 MessageBox( NULL, L"There was an error while connecting to the Internet.\n"\
270 "Thanks a lot for the help anyway.",
271 L"Report sending failed", MB_OK);
275 _wremove(mv_crashdump_path);
278 /*****************************************************************************
279 * vlc_exception_filter: handles unhandled exceptions, like segfaults
280 *****************************************************************************/
281 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
283 if(IsDebuggerPresent())
285 //If a debugger is present, pass the exception to the debugger
286 //with EXCEPTION_CONTINUE_SEARCH
287 return EXCEPTION_CONTINUE_SEARCH;
289 else
291 fprintf( stderr, "unhandled vlc exception\n" );
293 FILE * fd = _wfopen ( crashdump_path, L"w, ccs=UTF-8" );
295 if( !fd )
297 fprintf( stderr, "\nerror while opening file" );
298 exit( 1 );
301 OSVERSIONINFO osvi;
302 ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
303 osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
304 GetVersionEx( &osvi );
306 fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%ls\nVLC=" VERSION_MESSAGE,
307 osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber,
308 osvi.dwPlatformId, osvi.szCSDVersion);
310 const CONTEXT *const pContext = (const CONTEXT *)
311 lpExceptionInfo->ContextRecord;
312 const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)
313 lpExceptionInfo->ExceptionRecord;
314 /* No nested exceptions for now */
315 fwprintf( fd, L"\n\n[exceptions]\n%08x at %px",
316 pException->ExceptionCode, pException->ExceptionAddress );
318 for( unsigned int i = 0; i < pException->NumberParameters; i++ )
319 fwprintf( fd, L" | %p", pException->ExceptionInformation[i] );
321 #ifdef _WIN64
322 fwprintf( fd, L"\n\n[context]\nRDI:%px\nRSI:%px\n" \
323 "RBX:%px\nRDX:%px\nRCX:%px\nRAX:%px\n" \
324 "RBP:%px\nRIP:%px\nRSP:%px\nR8:%px\n" \
325 "R9:%px\nR10:%px\nR11:%px\nR12:%px\n" \
326 "R13:%px\nR14:%px\nR15:%px\n",
327 pContext->Rdi,pContext->Rsi,pContext->Rbx,
328 pContext->Rdx,pContext->Rcx,pContext->Rax,
329 pContext->Rbp,pContext->Rip,pContext->Rsp,
330 pContext->R8,pContext->R9,pContext->R10,
331 pContext->R11,pContext->R12,pContext->R13,
332 pContext->R14,pContext->R15 );
333 #else
334 fwprintf( fd, L"\n\n[context]\nEDI:%px\nESI:%px\n" \
335 "EBX:%px\nEDX:%px\nECX:%px\nEAX:%px\n" \
336 "EBP:%px\nEIP:%px\nESP:%px\n",
337 pContext->Edi,pContext->Esi,pContext->Ebx,
338 pContext->Edx,pContext->Ecx,pContext->Eax,
339 pContext->Ebp,pContext->Eip,pContext->Esp );
340 #endif
342 fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
344 #ifdef _WIN64
345 LPCVOID caller = (LPCVOID)pContext->Rip;
346 LPVOID *pBase = (LPVOID*)pContext->Rbp;
347 #else
348 LPVOID *pBase = (LPVOID*)pContext->Ebp;
349 LPCVOID caller = (LPCVOID)pContext->Eip;
350 #endif
351 for( unsigned frame = 0; frame <= 100; frame++ )
353 MEMORY_BASIC_INFORMATION mbi;
354 wchar_t module[ 256 ];
355 VirtualQuery( caller, &mbi, sizeof( mbi ) ) ;
356 GetModuleFileName( mbi.AllocationBase, module, 256 );
357 fwprintf( fd, L"%p|%ls\n", caller, module );
359 if( IsBadReadPtr( pBase, 2 * sizeof( void* ) ) )
360 break;
362 /*The last BP points to NULL!*/
363 caller = *(pBase + 1);
364 if( !caller )
365 break;
366 pBase = *pBase;
367 if( !pBase )
368 break;
371 HANDLE hpid = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
372 FALSE, GetCurrentProcessId());
373 if (hpid) {
374 HMODULE mods[1024];
375 DWORD size;
376 if (EnumProcessModules(hpid, mods, sizeof(mods), &size)) {
377 fwprintf( fd, L"\n\n[modules]\n" );
378 for (unsigned int i = 0; i < size / sizeof(HMODULE); i++) {
379 wchar_t module[ 256 ];
380 GetModuleFileName(mods[i], module, 256);
381 fwprintf( fd, L"%p|%ls\n", mods[i], module);
384 CloseHandle(hpid);
387 fclose( fd );
388 fflush( stderr );
389 exit( 1 );