Support for sipr decoder from FFmpeg
[vlc/solaris.git] / bin / winvlc.c
blob5bec350613542c3323f935adc6be9b83c39a8c05
1 /*****************************************************************************
2 * winvlc.c: the Windows VLC player
3 *****************************************************************************
4 * Copyright (C) 1998-2008 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 <string.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <windows.h>
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 #endif
51 #ifndef UNDER_CE
52 static char *FromWide (const wchar_t *wide)
54 size_t len;
55 len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
57 char *out = (char *)malloc (len);
58 if (out)
59 WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
60 return out;
62 #else
63 static int parse_cmdline (char *line, char ***argvp)
65 char **argv = malloc (sizeof (char *));
66 int argc = 0;
68 while (*line != '\0')
70 char quote = 0;
72 /* Skips white spaces */
73 while (strchr ("\t ", *line))
74 line++;
75 if (!*line)
76 break;
78 /* Starts a new parameter */
79 argv = realloc (argv, (argc + 2) * sizeof (char *));
80 if (*line == '"')
82 quote = '"';
83 line++;
85 argv[argc++] = line;
87 more:
88 while (*line && !strchr ("\t ", *line))
89 line++;
91 if (line > argv[argc - 1] && line[-1] == quote)
92 /* End of quoted parameter */
93 line[-1] = 0;
94 else
95 if (*line && quote)
97 /* Space within a quote */
98 line++;
99 goto more;
101 else
102 /* End of unquoted parameter */
103 if (*line)
104 *line++ = 0;
106 argv[argc] = NULL;
107 *argvp = argv;
108 return argc;
110 #endif
112 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
113 #ifndef UNDER_CE
114 LPSTR lpCmdLine,
115 #else
116 LPWSTR lpCmdLine,
117 #endif
118 int nCmdShow )
120 int argc, ret;
121 #ifndef UNDER_CE
122 wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
123 if (wargv == NULL)
124 return 1;
126 char *argv[argc + 1];
127 BOOL crash_handling = TRUE;
128 int j = 0;
130 argv[j++] = FromWide( L"--no-ignore-config" );
131 for (int i = 1; i < argc; i++)
133 if(!wcscmp(wargv[i], L"--no-crashdump"))
135 crash_handling = FALSE;
137 else
139 argv[j] = FromWide (wargv[i]);
140 j++;
144 argc = j;
145 argv[argc] = NULL;
146 LocalFree (wargv);
148 # ifndef _WIN64
149 if(crash_handling)
151 check_crashdump();
152 SetUnhandledExceptionFilter(vlc_exception_filter);
154 # endif /* WIN64 */
156 #else
157 char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
159 WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
160 psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
162 argc = parse_cmdline (psz_cmdline, &argv);
163 #endif
165 libvlc_exception_t ex, dummy;
166 libvlc_exception_init (&ex);
167 libvlc_exception_init (&dummy);
169 /* Initialize libvlc */
170 libvlc_instance_t *vlc;
171 vlc = libvlc_new (argc, (const char **)argv, &ex);
172 if (vlc != NULL)
174 libvlc_add_intf (vlc, "globalhotkeys,none");
175 libvlc_add_intf (vlc, NULL);
176 libvlc_playlist_play (vlc, -1, 0, NULL, &dummy);
177 libvlc_wait (vlc);
178 libvlc_release (vlc);
181 ret = libvlc_exception_raised (&ex);
182 libvlc_exception_clear (&ex);
183 libvlc_exception_clear (&dummy);
185 for (int i = 0; i < argc; i++)
186 free (argv[i]);
188 (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
189 return ret;
192 #if !defined( UNDER_CE ) && !defined( _WIN64 )
194 static void get_crashdump_path(wchar_t * wdir)
196 if( S_OK != SHGetFolderPathW( NULL,
197 CSIDL_APPDATA | CSIDL_FLAG_CREATE,
198 NULL, SHGFP_TYPE_CURRENT, wdir ) )
199 fprintf( stderr, "Can't open the vlc conf PATH\n" );
201 swprintf( wdir+wcslen( wdir ), L"%s", L"\\vlc\\crashdump" );
204 static void check_crashdump()
206 wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
207 get_crashdump_path(wdir);
209 FILE * fd = _wfopen ( wdir, L"r, ccs=UTF-8" );
210 if( fd )
212 fclose( fd );
213 int answer = MessageBox( NULL, L"VLC media player just crashed." \
214 " Do you want to send a bug report to the developers team?",
215 L"VLC crash reporting", MB_YESNO);
217 if(answer == IDYES)
219 HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
220 if(Hint)
222 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT,
223 NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
224 if(ftp)
226 SYSTEMTIME now;
227 GetSystemTime(&now);
228 wchar_t remote_file[MAX_PATH];
229 swprintf( remote_file, L"/crashs/%04d%02d%02d%02d%02d%02d",now.wYear,
230 now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond );
232 FtpPutFile( ftp, wdir, remote_file, FTP_TRANSFER_TYPE_BINARY, 0);
233 InternetCloseHandle(ftp);
235 else
236 fprintf(stderr,"Can't connect to FTP server%d\n",GetLastError());
237 InternetCloseHandle(Hint);
241 _wremove(wdir);
243 free((void *)wdir);
246 /*****************************************************************************
247 * vlc_exception_filter: handles unhandled exceptions, like segfaults
248 *****************************************************************************/
249 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
251 if(IsDebuggerPresent())
253 //If a debugger is present, pass the exception to the debugger with EXCEPTION_CONTINUE_SEARCH
254 return EXCEPTION_CONTINUE_SEARCH;
256 else
258 fprintf( stderr, "unhandled vlc exception\n" );
260 wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
261 get_crashdump_path(wdir);
262 FILE * fd = _wfopen ( wdir, L"w, ccs=UTF-8" );
263 free((void *)wdir);
265 if( !fd )
267 fprintf( stderr, "\nerror while opening file" );
268 exit( 1 );
271 OSVERSIONINFO osvi;
272 ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
273 osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
274 GetVersionEx( &osvi );
276 fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE, osvi.dwMajorVersion,
277 osvi.dwMinorVersion,
278 osvi.dwBuildNumber,
279 osvi.dwPlatformId,
280 osvi.szCSDVersion);
282 const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
283 const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
284 /*No nested exceptions for now*/
285 fwprintf( fd, L"\n\n[exceptions]\n%08x at %08x",pException->ExceptionCode,
286 pException->ExceptionAddress );
287 if( pException->NumberParameters > 0 )
289 unsigned int i;
290 for( i = 0; i < pException->NumberParameters; i++ )
291 fprintf( fd, " | %08x", pException->ExceptionInformation[i] );
294 fwprintf( fd, L"\n\n[context]\nEDI:%08x\nESI:%08x\n" \
295 "EBX:%08x\nEDX:%08x\nECX:%08x\nEAX:%08x\n" \
296 "EBP:%08x\nEIP:%08x\nESP:%08x\n",
297 pContext->Edi,pContext->Esi,pContext->Ebx,
298 pContext->Edx,pContext->Ecx,pContext->Eax,
299 pContext->Ebp,pContext->Eip,pContext->Esp );
301 fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
303 wchar_t module[ 256 ];
304 MEMORY_BASIC_INFORMATION mbi ;
305 VirtualQuery( (DWORD *)pContext->Eip, &mbi, sizeof( mbi ) ) ;
306 HINSTANCE hInstance = mbi.AllocationBase;
307 GetModuleFileName( hInstance, module, 256 ) ;
308 fwprintf( fd, L"%08x|%s\n", pContext->Eip, module );
310 DWORD pEbp = pContext->Ebp;
311 DWORD caller = *((DWORD*)pEbp + 1);
315 VirtualQuery( (DWORD *)caller, &mbi, sizeof( mbi ) ) ;
316 HINSTANCE hInstance = mbi.AllocationBase;
317 GetModuleFileName( hInstance, module, 256 ) ;
318 fwprintf( fd, L"%08x|%s\n", caller, module );
319 pEbp = *(DWORD*)pEbp ;
320 caller = *((DWORD*)pEbp + 1) ;
321 /*The last EBP points to NULL!*/
322 }while(caller);
324 fclose( fd );
325 fflush( stderr );
326 exit( 1 );
329 #endif