]> git.sesse.net Git - vlc/blob - bin/winvlc.c
--stats: default to false
[vlc] / bin / winvlc.c
1 /*****************************************************************************
2  * winvlc.c: the Windows VLC media player
3  *****************************************************************************
4  * Copyright (C) 1998-2011 the VideoLAN team
5  *
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
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #define UNICODE
32 #include <vlc/vlc.h>
33 #include <windows.h>
34 #include <shellapi.h>
35
36 #ifndef _WIN32_IE
37 #  define  _WIN32_IE 0x501
38 #endif
39 #include <fcntl.h>
40 #include <io.h>
41 #include <shlobj.h>
42 #include <wininet.h>
43 #define PSAPI_VERSION 1
44 #include <psapi.h>
45 #define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
46 static void check_crashdump(void);
47 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
48 static const wchar_t *crashdump_path;
49
50 static char *FromWide (const wchar_t *wide)
51 {
52     size_t len;
53     len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
54
55     char *out = (char *)malloc (len);
56     if (out)
57         WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
58     return out;
59 }
60
61 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
62                     LPSTR lpCmdLine,
63                     int nCmdShow )
64 {
65     int argc;
66
67     /* VLC does not change the thread locale, so gettext/libintil will use the
68      * user default locale as reference. */
69     /* gettext versions 0.18-0.18.1 will use the Windows Vista locale name
70      * if the GETTEXT_MUI environment variable is set. If not set or if running
71      * on Windows 2000/XP/2003 an hard-coded language ID list is used. This
72      * putenv() call may become redundant with later versions of gettext. */
73     putenv("GETTEXT_MUI=1");
74 #ifdef TOP_BUILDDIR
75     putenv("VLC_PLUGIN_PATH=Z:"TOP_BUILDDIR"/modules");
76     putenv("VLC_DATA_PATH=Z:"TOP_SRCDIR"/share");
77 #endif
78
79     HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
80
81     /* SetProcessDEPPolicy */
82     HINSTANCE h_Kernel32 = LoadLibraryW(L"kernel32.dll");
83     if(h_Kernel32)
84     {
85         BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
86         BOOL (WINAPI * mySetDllDirectoryA)(const char* lpPathName);
87 # define PROCESS_DEP_ENABLE 1
88
89         mySetProcessDEPPolicy = (BOOL WINAPI (*)(DWORD))
90                             GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
91         if(mySetProcessDEPPolicy)
92             mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
93
94         /* Do NOT load any library from cwd. */
95         mySetDllDirectoryA = (BOOL WINAPI (*)(const char*))
96                             GetProcAddress(h_Kernel32, "SetDllDirectoryA");
97         if(mySetDllDirectoryA)
98             mySetDllDirectoryA("");
99
100         FreeLibrary(h_Kernel32);
101     }
102
103     /* Args */
104     wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
105     if (wargv == NULL)
106         return 1;
107
108     char *argv[argc + 4];
109     BOOL crash_handling = TRUE;
110     int j = 0;
111     char *lang = NULL;
112
113     argv[j++] = FromWide( L"--media-library" );
114     argv[j++] = FromWide( L"--stats" );
115     argv[j++] = FromWide( L"--no-ignore-config" );
116     for (int i = 1; i < argc; i++)
117     {
118         if(!wcscmp(wargv[i], L"--no-crashdump"))
119         {
120             crash_handling = FALSE;
121             continue; /* don't give argument to libvlc */
122         }
123         if (!wcsncmp(wargv[i], L"--language", 10) )
124         {
125             if (i < argc - 1 && wcsncmp( wargv[i + 1], L"--", 2 ))
126                 lang = FromWide (wargv[++i]);
127             continue;
128         }
129
130         argv[j++] = FromWide (wargv[i]);
131     }
132
133     argc = j;
134     argv[argc] = NULL;
135     LocalFree (wargv);
136
137     if(crash_handling)
138     {
139         static wchar_t path[MAX_PATH];
140         if( S_OK != SHGetFolderPathW( NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
141                     NULL, SHGFP_TYPE_CURRENT, path ) )
142             fprintf( stderr, "Can't open the vlc conf PATH\n" );
143         _snwprintf( path+wcslen( path ), MAX_PATH,  L"%s", L"\\vlc\\crashdump" );
144         crashdump_path = &path[0];
145
146         check_crashdump();
147         SetUnhandledExceptionFilter(vlc_exception_filter);
148     }
149
150     _setmode( STDIN_FILENO, _O_BINARY ); /* Needed for pipes */
151
152     /* */
153     if (!lang)
154     {
155         HKEY h_key;
156         if( RegOpenKeyEx( HKEY_CURRENT_USER, TEXT("Software\\VideoLAN\\VLC\\"), 0, KEY_READ, &h_key )
157                 == ERROR_SUCCESS )
158         {
159             TCHAR szData[256];
160             DWORD len = 256;
161             if( RegQueryValueEx( h_key, TEXT("Lang"), NULL, NULL, (LPBYTE) &szData, &len ) == ERROR_SUCCESS )
162                 lang = FromWide( szData );
163         }
164     }
165
166     if (lang && strncmp( lang, "auto", 4 ) )
167     {
168         char tmp[11];
169         snprintf(tmp, 11, "LANG=%s", lang);
170         putenv(tmp);
171     }
172     free(lang);
173
174     /* Initialize libvlc */
175     libvlc_instance_t *vlc;
176     vlc = libvlc_new (argc, (const char **)argv);
177     if (vlc != NULL)
178     {
179         libvlc_set_app_id (vlc, "org.VideoLAN.VLC", PACKAGE_VERSION,
180                            PACKAGE_NAME);
181         libvlc_set_user_agent (vlc, "VLC media player", "VLC/"PACKAGE_VERSION);
182         libvlc_add_intf (vlc, "hotkeys,none");
183         libvlc_add_intf (vlc, "globalhotkeys,none");
184         libvlc_add_intf (vlc, NULL);
185         libvlc_playlist_play (vlc, -1, 0, NULL);
186         libvlc_wait (vlc);
187         libvlc_release (vlc);
188     }
189     else
190         MessageBox (NULL, TEXT("VLC media player could not start.\n"
191                     "Either the command line options were invalid or no plugins were found.\n"),
192                     TEXT("VLC media player"),
193                     MB_OK|MB_ICONERROR);
194
195
196     for (int i = 0; i < argc; i++)
197         free (argv[i]);
198
199     (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
200     return 0;
201 }
202
203 /* Crashdumps handling */
204 static void check_crashdump(void)
205 {
206     FILE * fd = _wfopen ( crashdump_path, L"r, ccs=UTF-8" );
207     if( !fd )
208         return;
209     fclose( fd );
210
211     int answer = MessageBox( NULL, L"Ooops: VLC media player just crashed.\n" \
212     "Would you like to send a bug report to the developers team?",
213     L"VLC crash reporting", MB_YESNO);
214
215     if(answer == IDYES)
216     {
217         HINTERNET Hint = InternetOpen(L"VLC Crash Reporter",
218                 INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
219         if(Hint)
220         {
221             HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org",
222                         INTERNET_DEFAULT_FTP_PORT, NULL, NULL,
223                         INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
224             if(ftp)
225             {
226                 SYSTEMTIME now;
227                 GetSystemTime(&now);
228                 wchar_t remote_file[MAX_PATH];
229                 _snwprintf(remote_file, MAX_PATH,
230                         L"/crashes-win32/%04d%02d%02d%02d%02d%02d",
231                         now.wYear, now.wMonth, now.wDay, now.wHour,
232                         now.wMinute, now.wSecond );
233
234                 if( FtpPutFile( ftp, crashdump_path, remote_file,
235                             FTP_TRANSFER_TYPE_BINARY, 0) )
236                     MessageBox( NULL, L"Report sent correctly. Thanks a lot " \
237                                 "for the help.", L"Report sent", MB_OK);
238                 else
239                     MessageBox( NULL, L"There was an error while "\
240                                 "transferring the data to the FTP server.\n"\
241                                 "Thanks a lot for the help.",
242                                 L"Report sending failed", MB_OK);
243                 InternetCloseHandle(ftp);
244             }
245             else
246             {
247                 MessageBox( NULL, L"There was an error while connecting to " \
248                                 "the FTP server. "\
249                                 "Thanks a lot for the help.",
250                                 L"Report sending failed", MB_OK);
251                 fprintf(stderr,"Can't connect to FTP server 0x%08lu\n",
252                         (unsigned long)GetLastError());
253             }
254             InternetCloseHandle(Hint);
255         }
256         else
257         {
258               MessageBox( NULL, L"There was an error while connecting to the Internet.\n"\
259                                 "Thanks a lot for the help anyway.",
260                                 L"Report sending failed", MB_OK);
261         }
262     }
263
264     _wremove(crashdump_path);
265 }
266
267 /*****************************************************************************
268  * vlc_exception_filter: handles unhandled exceptions, like segfaults
269  *****************************************************************************/
270 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
271 {
272     if(IsDebuggerPresent())
273     {
274         //If a debugger is present, pass the exception to the debugger
275         //with EXCEPTION_CONTINUE_SEARCH
276         return EXCEPTION_CONTINUE_SEARCH;
277     }
278     else
279     {
280         fprintf( stderr, "unhandled vlc exception\n" );
281
282         FILE * fd = _wfopen ( crashdump_path, L"w, ccs=UTF-8" );
283
284         if( !fd )
285         {
286             fprintf( stderr, "\nerror while opening file" );
287             exit( 1 );
288         }
289
290         OSVERSIONINFO osvi;
291         ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
292         osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
293         GetVersionEx( &osvi );
294
295         fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE,
296                 osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber,
297                 osvi.dwPlatformId, osvi.szCSDVersion);
298
299         const CONTEXT *const pContext = (const CONTEXT *)
300             lpExceptionInfo->ContextRecord;
301         const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)
302             lpExceptionInfo->ExceptionRecord;
303         /* No nested exceptions for now */
304         fwprintf( fd, L"\n\n[exceptions]\n%08x at %px", 
305                 pException->ExceptionCode, pException->ExceptionAddress );
306
307         for( unsigned int i = 0; i < pException->NumberParameters; i++ )
308             fwprintf( fd, L" | %p", pException->ExceptionInformation[i] );
309
310 #ifdef _WIN64
311         fwprintf( fd, L"\n\n[context]\nRDI:%px\nRSI:%px\n" \
312                     "RBX:%px\nRDX:%px\nRCX:%px\nRAX:%px\n" \
313                     "RBP:%px\nRIP:%px\nRSP:%px\nR8:%px\n" \
314                     "R9:%px\nR10:%px\nR11:%px\nR12:%px\n" \
315                     "R13:%px\nR14:%px\nR15:%px\n",
316                         pContext->Rdi,pContext->Rsi,pContext->Rbx,
317                         pContext->Rdx,pContext->Rcx,pContext->Rax,
318                         pContext->Rbp,pContext->Rip,pContext->Rsp,
319                         pContext->R8,pContext->R9,pContext->R10,
320                         pContext->R11,pContext->R12,pContext->R13,
321                         pContext->R14,pContext->R15 );
322 #else
323         fwprintf( fd, L"\n\n[context]\nEDI:%px\nESI:%px\n" \
324                     "EBX:%px\nEDX:%px\nECX:%px\nEAX:%px\n" \
325                     "EBP:%px\nEIP:%px\nESP:%px\n",
326                         pContext->Edi,pContext->Esi,pContext->Ebx,
327                         pContext->Edx,pContext->Ecx,pContext->Eax,
328                         pContext->Ebp,pContext->Eip,pContext->Esp );
329 #endif
330
331         fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
332
333 #ifdef _WIN64
334         LPCVOID caller = (LPCVOID)pContext->Rip;
335         LPVOID *pBase  = (LPVOID*)pContext->Rbp;
336 #else
337         LPVOID *pBase  = (LPVOID*)pContext->Ebp;
338         LPCVOID caller = (LPCVOID)pContext->Eip;
339 #endif
340         for( unsigned frame = 0; frame <= 100; frame++ )
341         {
342             MEMORY_BASIC_INFORMATION mbi;
343             wchar_t module[ 256 ];
344             VirtualQuery( caller, &mbi, sizeof( mbi ) ) ;
345             GetModuleFileName( mbi.AllocationBase, module, 256 );
346             fwprintf( fd, L"%p|%s\n", caller, module );
347
348             /*The last BP points to NULL!*/
349             caller = *(pBase + 1);
350             if( !caller )
351                 break;
352             pBase = *pBase;
353         }
354
355         HANDLE hpid = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
356                                         FALSE, GetCurrentProcessId());
357         if (hpid) {
358             HMODULE mods[1024];
359             DWORD size;
360             if (EnumProcessModules(hpid, mods, sizeof(mods), &size)) {
361                 fwprintf( fd, L"\n\n[modules]\n" );
362                 for (unsigned int i = 0; i < size / sizeof(HMODULE); i++) {
363                     wchar_t module[ 256 ];
364                     GetModuleFileName(mods[i], module, 256);
365                     fwprintf( fd, L"%p|%s\n", mods[i], module);
366                 }
367             }
368             CloseHandle(hpid);
369         }
370
371         fclose( fd );
372         fflush( stderr );
373         exit( 1 );
374     }
375 }