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