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