winvlc: various cleaning and comments
[vlc/vlc-skelet.git] / bin / winvlc.c
blob50cac7d69624b7fb3f011568ac6c0174747160e6
1 /*****************************************************************************
2 * winvlc.c: the Windows VLC media player
3 *****************************************************************************
4 * Copyright (C) 1998-2011 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #define UNICODE
32 #include <vlc/vlc.h>
33 #include <windows.h>
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
48 #ifndef UNDER_CE
49 static char *FromWide (const wchar_t *wide)
51 size_t len;
52 len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
54 char *out = (char *)malloc (len);
55 if (out)
56 WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
57 return out;
59 #else
60 static int parse_cmdline (char *line, char ***argvp)
62 char **argv = malloc (sizeof (char *));
63 int argc = 0;
65 while (*line != '\0')
67 char quote = 0;
69 /* Skips white spaces */
70 while (strchr ("\t ", *line))
71 line++;
72 if (!*line)
73 break;
75 /* Starts a new parameter */
76 argv = realloc (argv, (argc + 2) * sizeof (char *));
77 if (*line == '"')
79 quote = '"';
80 line++;
82 argv[argc++] = line;
84 more:
85 while (*line && !strchr ("\t ", *line))
86 line++;
88 if (line > argv[argc - 1] && line[-1] == quote)
89 /* End of quoted parameter */
90 line[-1] = 0;
91 else
92 if (*line && quote)
94 /* Space within a quote */
95 line++;
96 goto more;
98 else
99 /* End of unquoted parameter */
100 if (*line)
101 *line++ = 0;
103 argv[argc] = NULL;
104 *argvp = argv;
105 return argc;
107 #endif
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 )
117 int argc;
118 #ifndef UNDER_CE
119 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
121 /* SetProcessDEPPolicy */
122 HINSTANCE h_Kernel32 = LoadLibraryW(L"kernel32.dll");
123 if(h_Kernel32)
125 BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
126 # define PROCESS_DEP_ENABLE 1
128 mySetProcessDEPPolicy = (BOOL WINAPI (*)(DWORD))
129 GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
130 if(mySetProcessDEPPolicy)
131 mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
132 FreeLibrary(h_Kernel32);
135 /* Args */
136 wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
137 if (wargv == NULL)
138 return 1;
140 char *argv[argc + 2];
141 BOOL crash_handling = TRUE;
142 int j = 0;
144 argv[j++] = FromWide( L"--media-library" );
145 argv[j++] = FromWide( L"--no-ignore-config" );
146 for (int i = 1; i < argc; i++)
148 if(!wcscmp(wargv[i], L"--no-crashdump"))
150 crash_handling = FALSE;
151 continue; /* don't give argument to libvlc */
154 argv[j++] = FromWide (wargv[i]);
157 argc = j;
158 argv[argc] = NULL;
159 LocalFree (wargv);
161 # ifndef _WIN64
162 /* We don't know how to manage crashes on Win64 yet */
163 if(crash_handling)
165 check_crashdump();
166 SetUnhandledExceptionFilter(vlc_exception_filter);
168 # endif
170 #else /* UNDER_CE */
171 char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
173 WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
174 psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
176 argc = parse_cmdline (psz_cmdline, &argv);
177 #endif
179 /* Initialize libvlc */
180 libvlc_instance_t *vlc;
181 vlc = libvlc_new (argc, (const char **)argv);
182 if (vlc != NULL)
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);
191 for (int i = 0; i < argc; i++)
192 free (argv[i]);
194 (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
195 return 0;
198 #if !defined( UNDER_CE ) && !defined( _WIN64 )
199 /* Crashdumps handling */
200 static void get_crashdump_path(wchar_t * wdir)
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" );
207 swprintf( wdir+wcslen( wdir ), L"%s", L"\\vlc\\crashdump" );
210 static void check_crashdump()
212 wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
213 get_crashdump_path(wdir);
215 FILE * fd = _wfopen ( wdir, L"r, ccs=UTF-8" );
216 if( fd )
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);
223 if(answer == IDYES)
225 HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
226 if(Hint)
228 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT,
229 NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
230 if(ftp)
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 );
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);
247 else
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%d\n",GetLastError());
254 InternetCloseHandle(Hint);
256 else
258 MessageBox( NULL, L"There was an error while connecting to Internet. "\
259 "Thanks a lot for the help anyway.",
260 L"Report sending failed", MB_OK);
264 _wremove(wdir);
266 free((void *)wdir);
269 /*****************************************************************************
270 * vlc_exception_filter: handles unhandled exceptions, like segfaults
271 *****************************************************************************/
272 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
274 if(IsDebuggerPresent())
276 //If a debugger is present, pass the exception to the debugger with EXCEPTION_CONTINUE_SEARCH
277 return EXCEPTION_CONTINUE_SEARCH;
279 else
281 fprintf( stderr, "unhandled vlc exception\n" );
283 wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
284 get_crashdump_path(wdir);
285 FILE * fd = _wfopen ( wdir, L"w, ccs=UTF-8" );
286 free((void *)wdir);
288 if( !fd )
290 fprintf( stderr, "\nerror while opening file" );
291 exit( 1 );
294 OSVERSIONINFO osvi;
295 ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
296 osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
297 GetVersionEx( &osvi );
299 fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE, osvi.dwMajorVersion,
300 osvi.dwMinorVersion,
301 osvi.dwBuildNumber,
302 osvi.dwPlatformId,
303 osvi.szCSDVersion);
305 const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
306 const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
307 /*No nested exceptions for now*/
308 fwprintf( fd, L"\n\n[exceptions]\n%08x at %08x",pException->ExceptionCode,
309 pException->ExceptionAddress );
310 if( pException->NumberParameters > 0 )
312 unsigned int i;
313 for( i = 0; i < pException->NumberParameters; i++ )
314 fwprintf( fd, L" | %08x", pException->ExceptionInformation[i] );
317 fwprintf( fd, L"\n\n[context]\nEDI:%08x\nESI:%08x\n" \
318 "EBX:%08x\nEDX:%08x\nECX:%08x\nEAX:%08x\n" \
319 "EBP:%08x\nEIP:%08x\nESP:%08x\n",
320 pContext->Edi,pContext->Esi,pContext->Ebx,
321 pContext->Edx,pContext->Ecx,pContext->Eax,
322 pContext->Ebp,pContext->Eip,pContext->Esp );
324 fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
326 wchar_t module[ 256 ];
327 MEMORY_BASIC_INFORMATION mbi ;
328 VirtualQuery( (DWORD *)pContext->Eip, &mbi, sizeof( mbi ) ) ;
329 HINSTANCE hInstance = mbi.AllocationBase;
330 GetModuleFileName( hInstance, module, 256 ) ;
331 fwprintf( fd, L"%08x|%s\n", pContext->Eip, module );
333 DWORD pEbp = pContext->Ebp;
334 DWORD caller = *((DWORD*)pEbp + 1);
336 unsigned i_line = 0;
339 VirtualQuery( (DWORD *)caller, &mbi, sizeof( mbi ) ) ;
340 HINSTANCE hInstance = mbi.AllocationBase;
341 GetModuleFileName( hInstance, module, 256 ) ;
342 fwprintf( fd, L"%08x|%s\n", caller, module );
343 pEbp = *(DWORD*)pEbp ;
344 caller = *((DWORD*)pEbp + 1) ;
345 i_line++;
346 /*The last EBP points to NULL!*/
347 }while(caller && i_line< 100);
349 fclose( fd );
350 fflush( stderr );
351 exit( 1 );
354 #endif