Qt: consistency on QSettings value for OpenDialog
[vlc.git] / bin / winvlc.c
blob66d322caa1607fb914669fed0ca95a381d2f4e24
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 #define UNICODE
32 #include <vlc/vlc.h>
33 #include <windows.h>
35 #if !defined(UNDER_CE)
36 # ifndef _WIN32_IE
37 # define _WIN32_IE 0x501
38 # endif
39 # include <shlobj.h>
40 # include <wininet.h>
41 # define PSAPI_VERSION 1
42 # include <psapi.h>
43 # define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
44 static void check_crashdump(void);
45 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
46 static const wchar_t *crashdump_path;
47 #endif
49 #ifndef UNDER_CE
50 static char *FromWide (const wchar_t *wide)
52 size_t len;
53 len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
55 char *out = (char *)malloc (len);
56 if (out)
57 WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
58 return out;
60 #else
61 static int parse_cmdline (char *line, char ***argvp)
63 char **argv = malloc (sizeof (char *));
64 int argc = 0;
66 while (*line != '\0')
68 char quote = 0;
70 /* Skips white spaces */
71 while (strchr ("\t ", *line))
72 line++;
73 if (!*line)
74 break;
76 /* Starts a new parameter */
77 argv = realloc (argv, (argc + 2) * sizeof (char *));
78 if (*line == '"')
80 quote = '"';
81 line++;
83 argv[argc++] = line;
85 more:
86 while (*line && !strchr ("\t ", *line))
87 line++;
89 if (line > argv[argc - 1] && line[-1] == quote)
90 /* End of quoted parameter */
91 line[-1] = 0;
92 else
93 if (*line && quote)
95 /* Space within a quote */
96 line++;
97 goto more;
99 else
100 /* End of unquoted parameter */
101 if (*line)
102 *line++ = 0;
104 argv[argc] = NULL;
105 *argvp = argv;
106 return argc;
108 #endif
110 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
111 #ifndef UNDER_CE
112 LPSTR lpCmdLine,
113 #else
114 LPWSTR lpCmdLine,
115 #endif
116 int nCmdShow )
118 int argc;
120 #ifndef UNDER_CE
121 #ifdef TOP_BUILDDIR
122 putenv("VLC_PLUGIN_PATH=Z:"TOP_BUILDDIR"/modules");
123 #endif
125 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
127 /* SetProcessDEPPolicy */
128 HINSTANCE h_Kernel32 = LoadLibraryW(L"kernel32.dll");
129 if(h_Kernel32)
131 BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
132 BOOL (WINAPI * mySetDllDirectoryA)(const char* lpPathName);
133 # define PROCESS_DEP_ENABLE 1
135 mySetProcessDEPPolicy = (BOOL WINAPI (*)(DWORD))
136 GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
137 if(mySetProcessDEPPolicy)
138 mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
140 /* Do NOT load any library from cwd. */
141 mySetDllDirectoryA = (BOOL WINAPI (*)(const char*)) GetProcAddress(h_Kernel32, "SetDllDirectoryA");
142 if(mySetDllDirectoryA)
143 mySetDllDirectoryA("");
145 FreeLibrary(h_Kernel32);
148 /* Args */
149 wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
150 if (wargv == NULL)
151 return 1;
153 char *argv[argc + 3];
154 BOOL crash_handling = TRUE;
155 int j = 0;
157 argv[j++] = FromWide( L"--media-library" );
158 argv[j++] = FromWide( L"--no-ignore-config" );
159 #ifdef TOP_SRCDIR
160 argv[j++] = FromWide (L"--data-path=Z:"TOP_SRCDIR"/share");
161 #endif
162 for (int i = 1; i < argc; i++)
164 if(!wcscmp(wargv[i], L"--no-crashdump"))
166 crash_handling = FALSE;
167 continue; /* don't give argument to libvlc */
170 argv[j++] = FromWide (wargv[i]);
173 argc = j;
174 argv[argc] = NULL;
175 LocalFree (wargv);
177 if(crash_handling)
179 static wchar_t path[MAX_PATH];
180 if( S_OK != SHGetFolderPathW( NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
181 NULL, SHGFP_TYPE_CURRENT, path ) )
182 fprintf( stderr, "Can't open the vlc conf PATH\n" );
183 swprintf( path+wcslen( path ), L"%s", L"\\vlc\\crashdump" );
184 crashdump_path = &path[0];
186 check_crashdump();
187 SetUnhandledExceptionFilter(vlc_exception_filter);
190 #else /* UNDER_CE */
191 char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
193 WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
194 psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
196 argc = parse_cmdline (psz_cmdline, &argv);
197 #endif
199 /* Initialize libvlc */
200 libvlc_instance_t *vlc;
201 vlc = libvlc_new (argc, (const char **)argv);
202 if (vlc != NULL)
204 libvlc_add_intf (vlc, "globalhotkeys,none");
205 libvlc_add_intf (vlc, NULL);
206 libvlc_playlist_play (vlc, -1, 0, NULL);
207 libvlc_wait (vlc);
208 libvlc_release (vlc);
211 for (int i = 0; i < argc; i++)
212 free (argv[i]);
214 (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
215 return 0;
218 #if !defined( UNDER_CE )
219 /* Crashdumps handling */
220 static void check_crashdump(void)
222 FILE * fd = _wfopen ( crashdump_path, L"r, ccs=UTF-8" );
223 if( !fd )
224 return;
225 fclose( fd );
227 int answer = MessageBox( NULL, L"VLC media player just crashed." \
228 " Do you want to send a bug report to the developers team?",
229 L"VLC crash reporting", MB_YESNO);
231 if(answer == IDYES)
233 HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
234 if(Hint)
236 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT,
237 NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
238 if(ftp)
240 SYSTEMTIME now;
241 GetSystemTime(&now);
242 wchar_t remote_file[MAX_PATH];
243 swprintf( remote_file, L"/crashes-win32/%04d%02d%02d%02d%02d%02d",
244 now.wYear, now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond );
246 if( FtpPutFile( ftp, crashdump_path, remote_file, FTP_TRANSFER_TYPE_BINARY, 0) )
247 MessageBox( NULL, L"Report sent correctly. Thanks a lot for the help.",
248 L"Report sent", MB_OK);
249 else
250 MessageBox( NULL, L"There was an error while transferring to the FTP server. "\
251 "Thanks a lot for the help anyway.",
252 L"Report sending failed", MB_OK);
253 InternetCloseHandle(ftp);
255 else
257 MessageBox( NULL, L"There was an error while connecting to the FTP server. "\
258 "Thanks a lot for the help anyway.",
259 L"Report sending failed", MB_OK);
260 fprintf(stderr,"Can't connect to FTP server 0x%08lu\n",
261 (unsigned long)GetLastError());
263 InternetCloseHandle(Hint);
265 else
267 MessageBox( NULL, L"There was an error while connecting to Internet. "\
268 "Thanks a lot for the help anyway.",
269 L"Report sending failed", MB_OK);
273 _wremove(crashdump_path);
276 /*****************************************************************************
277 * vlc_exception_filter: handles unhandled exceptions, like segfaults
278 *****************************************************************************/
279 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
281 if(IsDebuggerPresent())
283 //If a debugger is present, pass the exception to the debugger with EXCEPTION_CONTINUE_SEARCH
284 return EXCEPTION_CONTINUE_SEARCH;
286 else
288 fprintf( stderr, "unhandled vlc exception\n" );
290 FILE * fd = _wfopen ( crashdump_path, L"w, ccs=UTF-8" );
292 if( !fd )
294 fprintf( stderr, "\nerror while opening file" );
295 exit( 1 );
298 OSVERSIONINFO osvi;
299 ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
300 osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
301 GetVersionEx( &osvi );
303 fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE, osvi.dwMajorVersion,
304 osvi.dwMinorVersion,
305 osvi.dwBuildNumber,
306 osvi.dwPlatformId,
307 osvi.szCSDVersion);
309 const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
310 const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
311 /*No nested exceptions for now*/
312 fwprintf( fd, L"\n\n[exceptions]\n%08x at %px",pException->ExceptionCode,
313 pException->ExceptionAddress );
315 for( unsigned int i = 0; i < pException->NumberParameters; i++ )
316 fwprintf( fd, L" | %p", pException->ExceptionInformation[i] );
318 #ifdef WIN64
319 fwprintf( fd, L"\n\n[context]\nRDI:%px\nRSI:%px\n" \
320 "RBX:%px\nRDX:%px\nRCX:%px\nRAX:%px\n" \
321 "RBP:%px\nRIP:%px\nRSP:%px\nR8:%px\n" \
322 "R9:%px\nR10:%px\nR11:%px\nR12:%px\n" \
323 "R13:%px\nR14:%px\nR15:%px\n",
324 pContext->Rdi,pContext->Rsi,pContext->Rbx,
325 pContext->Rdx,pContext->Rcx,pContext->Rax,
326 pContext->Rbp,pContext->Rip,pContext->Rsp,
327 pContext->R8,pContext->R9,pContext->R10,
328 pContext->R11,pContext->R12,pContext->R13,
329 pContext->R14,pContext->R15 );
330 #else
331 fwprintf( fd, L"\n\n[context]\nEDI:%px\nESI:%px\n" \
332 "EBX:%px\nEDX:%px\nECX:%px\nEAX:%px\n" \
333 "EBP:%px\nEIP:%px\nESP:%px\n",
334 pContext->Edi,pContext->Esi,pContext->Ebx,
335 pContext->Edx,pContext->Ecx,pContext->Eax,
336 pContext->Ebp,pContext->Eip,pContext->Esp );
337 #endif
339 HANDLE hpid = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
340 FALSE, GetCurrentProcessId());
341 if (hpid) {
342 HMODULE mods[1024];
343 DWORD size;
344 if (EnumProcessModules(hpid, mods, sizeof(mods), &size)) {
345 fwprintf( fd, L"\n\n[modules]\n" );
346 for (unsigned int i = 0; i < size / sizeof(HMODULE); i++) {
347 wchar_t module[ 256 ];
348 GetModuleFileName(mods[i], module, 256);
349 fwprintf( fd, L"%p|%s\n", mods[i], module);
352 CloseHandle(hpid);
356 fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
358 #ifdef WIN64
359 LPCVOID caller = (LPCVOID)pContext->Rip;
360 LPVOID *pBase = (LPVOID*)pContext->Rbp;
361 #else
362 LPVOID *pBase = (LPVOID*)pContext->Ebp;
363 LPCVOID caller = (LPCVOID)pContext->Eip;
364 #endif
365 for( unsigned frame = 0; frame <= 100; frame++ )
367 MEMORY_BASIC_INFORMATION mbi;
368 wchar_t module[ 256 ];
369 VirtualQuery( caller, &mbi, sizeof( mbi ) ) ;
370 GetModuleFileName( mbi.AllocationBase, module, 256 );
371 fwprintf( fd, L"%p|%s\n", caller, module );
373 /*The last BP points to NULL!*/
374 caller = *(pBase + 1);
375 if( !caller )
376 break;
377 pBase = *pBase;
380 fclose( fd );
381 fflush( stderr );
382 exit( 1 );
385 #endif