]> git.sesse.net Git - vlc/blob - bin/winvlc.c
874116035847621e195a4aae3262b5224efe6ac8
[vlc] / bin / winvlc.c
1 /*****************************************************************************
2  * winvlc.c: the Windows VLC player
3  *****************************************************************************
4  * Copyright (C) 1998-2008 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 <string.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <windows.h>
37
38 #if !defined(UNDER_CE)
39 # ifndef _WIN32_IE
40 #   define  _WIN32_IE 0x501
41 # endif
42 #   include <shlobj.h>
43 #   include <tlhelp32.h>
44 #   include <wininet.h>
45 # ifndef _WIN64
46 static void check_crashdump(void);
47 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
48 # endif
49 typedef enum _HEAP_INFORMATION_CLASS {
50         HeapCompatibilityInformation,
51         HeapEnableTerminationOnCorruption
52 } HEAP_INFORMATION_CLASS;
53 WINBASEAPI BOOL WINAPI HeapSetInformation(HANDLE,HEAP_INFORMATION_CLASS,PVOID,SIZE_T);
54 #define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
55 #endif
56
57 #ifndef UNDER_CE
58 static char *FromWide (const wchar_t *wide)
59 {
60     size_t len;
61     len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
62
63     char *out = (char *)malloc (len);
64     if (out)
65         WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
66     return out;
67 }
68 #else
69 static int parse_cmdline (char *line, char ***argvp)
70 {
71     char **argv = malloc (sizeof (char *));
72     int argc = 0;
73
74     while (*line != '\0')
75     {
76         char quote = 0;
77
78         /* Skips white spaces */
79         while (strchr ("\t ", *line))
80             line++;
81         if (!*line)
82             break;
83
84         /* Starts a new parameter */
85         argv = realloc (argv, (argc + 2) * sizeof (char *));
86         if (*line == '"')
87         {
88             quote = '"';
89             line++;
90         }
91         argv[argc++] = line;
92
93     more:
94             while (*line && !strchr ("\t ", *line))
95             line++;
96
97     if (line > argv[argc - 1] && line[-1] == quote)
98         /* End of quoted parameter */
99         line[-1] = 0;
100     else
101         if (*line && quote)
102     {
103         /* Space within a quote */
104         line++;
105         goto more;
106     }
107     else
108         /* End of unquoted parameter */
109         if (*line)
110             *line++ = 0;
111     }
112     argv[argc] = NULL;
113     *argvp = argv;
114     return argc;
115 }
116 #endif
117
118 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
119 #ifndef UNDER_CE
120                     LPSTR lpCmdLine,
121 #else
122                     LPWSTR lpCmdLine,
123 #endif
124                     int nCmdShow )
125 {
126     int argc, ret;
127 #ifndef UNDER_CE
128     HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
129     wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
130     if (wargv == NULL)
131         return 1;
132
133     char *argv[argc + 1];
134     BOOL crash_handling = TRUE;
135     int j = 0;
136
137     argv[j++] = FromWide( L"--no-ignore-config" );
138     for (int i = 1; i < argc; i++)
139     {
140         if(!wcscmp(wargv[i], L"--no-crashdump"))
141         {
142             crash_handling = FALSE;
143         }
144         else
145         {
146             argv[j] = FromWide (wargv[i]);
147             j++;
148         }
149     }
150
151     argc = j;
152     argv[argc] = NULL;
153     LocalFree (wargv);
154
155 # ifndef _WIN64
156     if(crash_handling)
157     {
158         check_crashdump();
159         SetUnhandledExceptionFilter(vlc_exception_filter);
160     }
161 # endif /* WIN64 */
162
163 #else
164     char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
165
166     WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
167                          psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
168
169     argc = parse_cmdline (psz_cmdline, &argv);
170 #endif
171
172     libvlc_exception_t ex;
173     libvlc_exception_init (&ex);
174
175     /* Initialize libvlc */
176     libvlc_instance_t *vlc;
177     vlc = libvlc_new (argc, (const char **)argv, &ex);
178     if (vlc != NULL)
179     {
180         libvlc_add_intf (vlc, "globalhotkeys,none");
181         libvlc_add_intf (vlc, NULL);
182         libvlc_playlist_play (vlc, -1, 0, NULL);
183         libvlc_wait (vlc);
184         libvlc_release (vlc);
185     }
186
187     ret = libvlc_exception_raised (&ex);
188     libvlc_exception_clear (&ex);
189
190     for (int i = 0; i < argc; i++)
191         free (argv[i]);
192
193     (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
194     return ret;
195 }
196
197 #if !defined( UNDER_CE ) && !defined( _WIN64 )
198
199 static void get_crashdump_path(wchar_t * wdir)
200 {
201     if( S_OK != SHGetFolderPathW( NULL,
202                         CSIDL_APPDATA | CSIDL_FLAG_CREATE,
203                         NULL, SHGFP_TYPE_CURRENT, wdir ) )
204         fprintf( stderr, "Can't open the vlc conf PATH\n" );
205
206     swprintf( wdir+wcslen( wdir ), L"%s", L"\\vlc\\crashdump" );
207 }
208
209 static void check_crashdump()
210 {
211     wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
212     get_crashdump_path(wdir);
213
214     FILE * fd = _wfopen ( wdir, L"r, ccs=UTF-8" );
215     if( fd )
216     {
217         fclose( fd );
218         int answer = MessageBox( NULL, L"VLC media player just crashed." \
219         " Do you want to send a bug report to the developers team?",
220         L"VLC crash reporting", MB_YESNO);
221
222         if(answer == IDYES)
223         {
224             HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
225             if(Hint)
226             {
227                 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT,
228                                                 NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
229                 if(ftp)
230                 {
231                     SYSTEMTIME now;
232                     GetSystemTime(&now);
233                     wchar_t remote_file[MAX_PATH];
234                     swprintf( remote_file, L"/crashs/%04d%02d%02d%02d%02d%02d",now.wYear,
235                             now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond  );
236
237                     FtpPutFile( ftp, wdir, remote_file, FTP_TRANSFER_TYPE_BINARY, 0);
238                     InternetCloseHandle(ftp);
239                 }
240                 else
241                     fprintf(stderr,"Can't connect to FTP server%d\n",GetLastError());
242                 InternetCloseHandle(Hint);
243             }
244         }
245
246         _wremove(wdir);
247     }
248     free((void *)wdir);
249 }
250
251 /*****************************************************************************
252  * vlc_exception_filter: handles unhandled exceptions, like segfaults
253  *****************************************************************************/
254 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
255 {
256     if(IsDebuggerPresent())
257     {
258         //If a debugger is present, pass the exception to the debugger with EXCEPTION_CONTINUE_SEARCH
259         return EXCEPTION_CONTINUE_SEARCH;
260     }
261     else
262     {
263         fprintf( stderr, "unhandled vlc exception\n" );
264
265         wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
266         get_crashdump_path(wdir);
267         FILE * fd = _wfopen ( wdir, L"w, ccs=UTF-8" );
268         free((void *)wdir);
269
270         if( !fd )
271         {
272             fprintf( stderr, "\nerror while opening file" );
273             exit( 1 );
274         }
275
276         OSVERSIONINFO osvi;
277         ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
278         osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
279         GetVersionEx( &osvi );
280
281         fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE, osvi.dwMajorVersion,
282                                                                osvi.dwMinorVersion,
283                                                                osvi.dwBuildNumber,
284                                                                osvi.dwPlatformId,
285                                                                osvi.szCSDVersion);
286
287         const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
288         const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
289         /*No nested exceptions for now*/
290         fwprintf( fd, L"\n\n[exceptions]\n%08x at %08x",pException->ExceptionCode,
291                                                 pException->ExceptionAddress );
292         if( pException->NumberParameters > 0 )
293         {
294             unsigned int i;
295             for( i = 0; i < pException->NumberParameters; i++ )
296                 fprintf( fd, " | %08x", pException->ExceptionInformation[i] );
297         }
298
299         fwprintf( fd, L"\n\n[context]\nEDI:%08x\nESI:%08x\n" \
300                     "EBX:%08x\nEDX:%08x\nECX:%08x\nEAX:%08x\n" \
301                     "EBP:%08x\nEIP:%08x\nESP:%08x\n",
302                         pContext->Edi,pContext->Esi,pContext->Ebx,
303                         pContext->Edx,pContext->Ecx,pContext->Eax,
304                         pContext->Ebp,pContext->Eip,pContext->Esp );
305
306         fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
307
308         wchar_t module[ 256 ];
309         MEMORY_BASIC_INFORMATION mbi ;
310         VirtualQuery( (DWORD *)pContext->Eip, &mbi, sizeof( mbi ) ) ;
311         HINSTANCE hInstance = mbi.AllocationBase;
312         GetModuleFileName( hInstance, module, 256 ) ;
313         fwprintf( fd, L"%08x|%s\n", pContext->Eip, module );
314
315         DWORD pEbp = pContext->Ebp;
316         DWORD caller = *((DWORD*)pEbp + 1);
317
318         do
319         {
320             VirtualQuery( (DWORD *)caller, &mbi, sizeof( mbi ) ) ;
321             HINSTANCE hInstance = mbi.AllocationBase;
322             GetModuleFileName( hInstance, module, 256 ) ;
323             fwprintf( fd, L"%08x|%s\n", caller, module );
324             pEbp = *(DWORD*)pEbp ;
325             caller = *((DWORD*)pEbp + 1) ;
326             /*The last EBP points to NULL!*/
327         }while(caller);
328
329         fclose( fd );
330         fflush( stderr );
331         exit( 1 );
332     }
333 }
334 #endif