]> git.sesse.net Git - vlc/blob - bin/winvlc.c
winvlc: fix warning
[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
35 #if !defined(UNDER_CE)
36 # ifndef _WIN32_IE
37 #   define  _WIN32_IE 0x501
38 # endif
39 # include <shlobj.h>
40 # include <wininet.h>
41 # define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
42 # ifndef _WIN64
43 static void check_crashdump(void);
44 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
45 # endif
46 #endif
47
48 #ifndef UNDER_CE
49 static char *FromWide (const wchar_t *wide)
50 {
51     size_t len;
52     len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
53
54     char *out = (char *)malloc (len);
55     if (out)
56         WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
57     return out;
58 }
59 #else
60 static int parse_cmdline (char *line, char ***argvp)
61 {
62     char **argv = malloc (sizeof (char *));
63     int argc = 0;
64
65     while (*line != '\0')
66     {
67         char quote = 0;
68
69         /* Skips white spaces */
70         while (strchr ("\t ", *line))
71             line++;
72         if (!*line)
73             break;
74
75         /* Starts a new parameter */
76         argv = realloc (argv, (argc + 2) * sizeof (char *));
77         if (*line == '"')
78         {
79             quote = '"';
80             line++;
81         }
82         argv[argc++] = line;
83
84     more:
85             while (*line && !strchr ("\t ", *line))
86             line++;
87
88     if (line > argv[argc - 1] && line[-1] == quote)
89         /* End of quoted parameter */
90         line[-1] = 0;
91     else
92         if (*line && quote)
93     {
94         /* Space within a quote */
95         line++;
96         goto more;
97     }
98     else
99         /* End of unquoted parameter */
100         if (*line)
101             *line++ = 0;
102     }
103     argv[argc] = NULL;
104     *argvp = argv;
105     return argc;
106 }
107 #endif
108
109 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
110 #ifndef UNDER_CE
111                     LPSTR lpCmdLine,
112 #else
113                     LPWSTR lpCmdLine,
114 #endif
115                     int nCmdShow )
116 {
117     int argc;
118 #ifndef UNDER_CE
119     HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
120
121     /* SetProcessDEPPolicy */
122     HINSTANCE h_Kernel32 = LoadLibraryW(L"kernel32.dll");
123     if(h_Kernel32)
124     {
125         BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
126 # define PROCESS_DEP_ENABLE 1
127
128         mySetProcessDEPPolicy = (BOOL WINAPI (*)(DWORD))
129                             GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
130         if(mySetProcessDEPPolicy)
131             mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
132         FreeLibrary(h_Kernel32);
133     }
134
135     /* Args */
136     wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
137     if (wargv == NULL)
138         return 1;
139
140     char *argv[argc + 2];
141     BOOL crash_handling = TRUE;
142     int j = 0;
143
144     argv[j++] = FromWide( L"--media-library" );
145     argv[j++] = FromWide( L"--no-ignore-config" );
146     for (int i = 1; i < argc; i++)
147     {
148         if(!wcscmp(wargv[i], L"--no-crashdump"))
149         {
150             crash_handling = FALSE;
151             continue; /* don't give argument to libvlc */
152         }
153
154         argv[j++] = FromWide (wargv[i]);
155     }
156
157     argc = j;
158     argv[argc] = NULL;
159     LocalFree (wargv);
160
161 # ifndef _WIN64
162     /* We don't know how to manage crashes on Win64 yet */
163     if(crash_handling)
164     {
165         check_crashdump();
166         SetUnhandledExceptionFilter(vlc_exception_filter);
167     }
168 # endif
169
170 #else /* UNDER_CE */
171     char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
172
173     WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
174                          psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
175
176     argc = parse_cmdline (psz_cmdline, &argv);
177 #endif
178
179     /* Initialize libvlc */
180     libvlc_instance_t *vlc;
181     vlc = libvlc_new (argc, (const char **)argv);
182     if (vlc != NULL)
183     {
184         libvlc_add_intf (vlc, "globalhotkeys,none");
185         libvlc_add_intf (vlc, NULL);
186         libvlc_playlist_play (vlc, -1, 0, NULL);
187         libvlc_wait (vlc);
188         libvlc_release (vlc);
189     }
190
191     for (int i = 0; i < argc; i++)
192         free (argv[i]);
193
194     (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
195     return 0;
196 }
197
198 #if !defined( UNDER_CE ) && !defined( _WIN64 )
199 /* Crashdumps handling */
200 static void get_crashdump_path(wchar_t * wdir)
201 {
202     if( S_OK != SHGetFolderPathW( NULL,
203                         CSIDL_APPDATA | CSIDL_FLAG_CREATE,
204                         NULL, SHGFP_TYPE_CURRENT, wdir ) )
205         fprintf( stderr, "Can't open the vlc conf PATH\n" );
206
207     swprintf( wdir+wcslen( wdir ), L"%s", L"\\vlc\\crashdump" );
208 }
209
210 static void check_crashdump()
211 {
212     wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
213     get_crashdump_path(wdir);
214
215     FILE * fd = _wfopen ( wdir, L"r, ccs=UTF-8" );
216     if( fd )
217     {
218         fclose( fd );
219         int answer = MessageBox( NULL, L"VLC media player just crashed." \
220         " Do you want to send a bug report to the developers team?",
221         L"VLC crash reporting", MB_YESNO);
222
223         if(answer == IDYES)
224         {
225             HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
226             if(Hint)
227             {
228                 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT,
229                                                 NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
230                 if(ftp)
231                 {
232                     SYSTEMTIME now;
233                     GetSystemTime(&now);
234                     wchar_t remote_file[MAX_PATH];
235                     swprintf( remote_file, L"/crashs/%04d%02d%02d%02d%02d%02d",now.wYear,
236                             now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond  );
237
238                     if( FtpPutFile( ftp, wdir, remote_file, FTP_TRANSFER_TYPE_BINARY, 0) )
239                         MessageBox( NULL, L"Report sent correctly. Thanks a lot for the help.",
240                                     L"Report sent", MB_OK);
241                     else
242                         MessageBox( NULL, L"There was an error while transferring to the FTP server. "\
243                                     "Thanks a lot for the help anyway.",
244                                     L"Report sending failed", MB_OK);
245                     InternetCloseHandle(ftp);
246                 }
247                 else
248                 {
249                     MessageBox( NULL, L"There was an error while connecting to the FTP server. "\
250                                     "Thanks a lot for the help anyway.",
251                                     L"Report sending failed", MB_OK);
252                     fprintf(stderr,"Can't connect to FTP server 0x%08lu\n",
253                             (unsigned long)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"Report 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