]> git.sesse.net Git - vlc/blob - bin/winvlc.c
Win32: forgotten error GUI messages
[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 #define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
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 #ifndef UNDER_CE
123     HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
124
125     HINSTANCE h_Kernel32 = LoadLibraryW(L"kernel32.dll");
126     if(h_Kernel32)
127     {
128         BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
129 # define PROCESS_DEP_ENABLE 1
130
131         mySetProcessDEPPolicy = (BOOL WINAPI (*)(DWORD))
132                             GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
133         if(mySetProcessDEPPolicy)
134             mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
135         FreeLibrary(h_Kernel32);
136     }
137
138     wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
139     if (wargv == NULL)
140         return 1;
141
142     char *argv[argc + 2];
143     BOOL crash_handling = TRUE;
144     int j = 0;
145
146     argv[j++] = FromWide( L"--media-library" );
147     argv[j++] = FromWide( L"--no-ignore-config" );
148     for (int i = 1; i < argc; i++)
149     {
150         if(!wcscmp(wargv[i], L"--no-crashdump"))
151         {
152             crash_handling = FALSE;
153             continue; /* don't give argument to libvlc */
154         }
155
156         argv[j++] = FromWide (wargv[i]);
157     }
158
159     argc = j;
160     argv[argc] = NULL;
161     LocalFree (wargv);
162
163 # ifndef _WIN64
164     if(crash_handling)
165     {
166         check_crashdump();
167         SetUnhandledExceptionFilter(vlc_exception_filter);
168     }
169 # endif /* WIN64 */
170
171 #else
172     char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
173
174     WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
175                          psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
176
177     argc = parse_cmdline (psz_cmdline, &argv);
178 #endif
179
180     /* Initialize libvlc */
181     libvlc_instance_t *vlc;
182     vlc = libvlc_new (argc, (const char **)argv);
183     if (vlc != NULL)
184     {
185         libvlc_add_intf (vlc, "globalhotkeys,none");
186         libvlc_add_intf (vlc, NULL);
187         libvlc_playlist_play (vlc, -1, 0, NULL);
188         libvlc_wait (vlc);
189         libvlc_release (vlc);
190     }
191
192     for (int i = 0; i < argc; i++)
193         free (argv[i]);
194
195     (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
196     return 0;
197 }
198
199 #if !defined( UNDER_CE ) && !defined( _WIN64 )
200
201 static void get_crashdump_path(wchar_t * wdir)
202 {
203     if( S_OK != SHGetFolderPathW( NULL,
204                         CSIDL_APPDATA | CSIDL_FLAG_CREATE,
205                         NULL, SHGFP_TYPE_CURRENT, wdir ) )
206         fprintf( stderr, "Can't open the vlc conf PATH\n" );
207
208     swprintf( wdir+wcslen( wdir ), L"%s", L"\\vlc\\crashdump" );
209 }
210
211 static void check_crashdump()
212 {
213     wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
214     get_crashdump_path(wdir);
215
216     FILE * fd = _wfopen ( wdir, L"r, ccs=UTF-8" );
217     if( fd )
218     {
219         fclose( fd );
220         int answer = MessageBox( NULL, L"VLC media player just crashed." \
221         " Do you want to send a bug report to the developers team?",
222         L"VLC crash reporting", MB_YESNO);
223
224         if(answer == IDYES)
225         {
226             HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
227             if(Hint)
228             {
229                 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT,
230                                                 NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
231                 if(ftp)
232                 {
233                     SYSTEMTIME now;
234                     GetSystemTime(&now);
235                     wchar_t remote_file[MAX_PATH];
236                     swprintf( remote_file, L"/crashs/%04d%02d%02d%02d%02d%02d",now.wYear,
237                             now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond  );
238
239                     if( FtpPutFile( ftp, wdir, remote_file, FTP_TRANSFER_TYPE_BINARY, 0) )
240                         MessageBox( NULL, L"Report sent correctly. Thanks a lot for the help.",
241                                     L"Report sent", MB_OK);
242                     else
243                         MessageBox( NULL, L"There was an error while transferring to the FTP server. "\
244                                     "Thanks a lot for the help anyway.",
245                                     L"Report sending failed", MB_OK);
246                     InternetCloseHandle(ftp);
247                 }
248                 else
249                 {
250                     MessageBox( NULL, L"There was an error while connecting to the FTP server. "\
251                                     "Thanks a lot for the help anyway.",
252                                     L"Report sending failed", MB_OK);
253                     fprintf(stderr,"Can't connect to FTP server%d\n",GetLastError());
254                 }
255                 InternetCloseHandle(Hint);
256             }
257             else
258             {
259                   MessageBox( NULL, L"There was an error while connecting to Internet. "\
260                                     "Thanks a lot for the help anyway.",
261                                     L"Reporting sending failed", MB_OK);
262             }
263         }
264
265         _wremove(wdir);
266     }
267     free((void *)wdir);
268 }
269
270 /*****************************************************************************
271  * vlc_exception_filter: handles unhandled exceptions, like segfaults
272  *****************************************************************************/
273 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
274 {
275     if(IsDebuggerPresent())
276     {
277         //If a debugger is present, pass the exception to the debugger with EXCEPTION_CONTINUE_SEARCH
278         return EXCEPTION_CONTINUE_SEARCH;
279     }
280     else
281     {
282         fprintf( stderr, "unhandled vlc exception\n" );
283
284         wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
285         get_crashdump_path(wdir);
286         FILE * fd = _wfopen ( wdir, L"w, ccs=UTF-8" );
287         free((void *)wdir);
288
289         if( !fd )
290         {
291             fprintf( stderr, "\nerror while opening file" );
292             exit( 1 );
293         }
294
295         OSVERSIONINFO osvi;
296         ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
297         osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
298         GetVersionEx( &osvi );
299
300         fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE, osvi.dwMajorVersion,
301                                                                osvi.dwMinorVersion,
302                                                                osvi.dwBuildNumber,
303                                                                osvi.dwPlatformId,
304                                                                osvi.szCSDVersion);
305
306         const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
307         const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
308         /*No nested exceptions for now*/
309         fwprintf( fd, L"\n\n[exceptions]\n%08x at %08x",pException->ExceptionCode,
310                                                 pException->ExceptionAddress );
311         if( pException->NumberParameters > 0 )
312         {
313             unsigned int i;
314             for( i = 0; i < pException->NumberParameters; i++ )
315                 fwprintf( fd, L" | %08x", pException->ExceptionInformation[i] );
316         }
317
318         fwprintf( fd, L"\n\n[context]\nEDI:%08x\nESI:%08x\n" \
319                     "EBX:%08x\nEDX:%08x\nECX:%08x\nEAX:%08x\n" \
320                     "EBP:%08x\nEIP:%08x\nESP:%08x\n",
321                         pContext->Edi,pContext->Esi,pContext->Ebx,
322                         pContext->Edx,pContext->Ecx,pContext->Eax,
323                         pContext->Ebp,pContext->Eip,pContext->Esp );
324
325         fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
326
327         wchar_t module[ 256 ];
328         MEMORY_BASIC_INFORMATION mbi ;
329         VirtualQuery( (DWORD *)pContext->Eip, &mbi, sizeof( mbi ) ) ;
330         HINSTANCE hInstance = mbi.AllocationBase;
331         GetModuleFileName( hInstance, module, 256 ) ;
332         fwprintf( fd, L"%08x|%s\n", pContext->Eip, module );
333
334         DWORD pEbp = pContext->Ebp;
335         DWORD caller = *((DWORD*)pEbp + 1);
336
337         unsigned i_line = 0;
338         do
339         {
340             VirtualQuery( (DWORD *)caller, &mbi, sizeof( mbi ) ) ;
341             HINSTANCE hInstance = mbi.AllocationBase;
342             GetModuleFileName( hInstance, module, 256 ) ;
343             fwprintf( fd, L"%08x|%s\n", caller, module );
344             pEbp = *(DWORD*)pEbp ;
345             caller = *((DWORD*)pEbp + 1) ;
346             i_line++;
347             /*The last EBP points to NULL!*/
348         }while(caller && i_line< 100);
349
350         fclose( fd );
351         fflush( stderr );
352         exit( 1 );
353     }
354 }
355 #endif