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