Add a desktop mode to the Direct3d video output. This allow displaying video on a...
[vlc.git] / bin / winvlc.c
bloba8dd08aa79be09d19dcb3933d75dd6fc26bdfe9a
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 # define _WIN32_IE 0x500
40 # include <shlobj.h>
41 # include <tlhelp32.h>
42 # include <wininet.h>
43 static void check_crashdump();
44 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
45 #endif
47 #ifndef UNDER_CE
48 static char *FromWide (const wchar_t *wide)
50 size_t len;
51 len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
53 char *out = (char *)malloc (len);
54 if (out)
55 WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
56 return out;
58 #else
59 static int parse_cmdline (char *line, char ***argvp)
61 char **argv = malloc (sizeof (char *));
62 int argc = 0;
64 while (*line != '\0')
66 char quote = 0;
68 /* Skips white spaces */
69 while (strchr ("\t ", *line))
70 line++;
71 if (!*line)
72 break;
74 /* Starts a new parameter */
75 argv = realloc (argv, (argc + 2) * sizeof (char *));
76 if (*line == '"')
78 quote = '"';
79 line++;
81 argv[argc++] = line;
83 more:
84 while (*line && !strchr ("\t ", *line))
85 line++;
87 if (line > argv[argc - 1] && line[-1] == quote)
88 /* End of quoted parameter */
89 line[-1] = 0;
90 else
91 if (*line && quote)
93 /* Space within a quote */
94 line++;
95 goto more;
97 else
98 /* End of unquoted parameter */
99 if (*line)
100 *line++ = 0;
102 argv[argc] = NULL;
103 *argvp = argv;
104 return argc;
106 #endif
108 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
109 #ifndef UNDER_CE
110 LPSTR lpCmdLine,
111 #else
112 LPWSTR lpCmdLine,
113 #endif
114 int nCmdShow )
116 int argc, ret;
117 #ifndef UNDER_CE
118 wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
119 if (wargv == NULL)
120 return 1;
122 char *argv[argc + 1];
123 BOOL crash_handling = TRUE;
124 int j = 0;
125 for (int i = 0; i < argc; i++)
127 if(!wcscmp(wargv[i], L"--no-crashdump"))
129 crash_handling = FALSE;
131 else
133 argv[j] = FromWide (wargv[i]);
134 j++;
138 argc = j;
139 argv[argc] = NULL;
140 LocalFree (wargv);
142 if(crash_handling)
144 check_crashdump();
145 SetUnhandledExceptionFilter(vlc_exception_filter);
148 #else
149 char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
151 WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
152 psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
154 argc = parse_cmdline (psz_cmdline, &argv);
155 #endif
157 libvlc_exception_t ex, dummy;
158 libvlc_exception_init (&ex);
159 libvlc_exception_init (&dummy);
161 /* Initialize libvlc */
162 libvlc_instance_t *vlc;
163 vlc = libvlc_new (argc - 1, (const char **)argv + 1, &ex);
164 if (vlc != NULL)
166 libvlc_add_intf (vlc, "globalhotkeys,none", &ex);
167 libvlc_add_intf (vlc, NULL, &ex);
168 libvlc_playlist_play (vlc, -1, 0, NULL, &dummy);
169 libvlc_wait (vlc);
170 libvlc_release (vlc);
173 ret = libvlc_exception_raised (&ex);
174 libvlc_exception_clear (&ex);
175 libvlc_exception_clear (&dummy);
177 for (int i = 0; i < argc; i++)
178 free (argv[i]);
180 (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
181 return ret;
184 #if !defined( UNDER_CE )
186 static void get_crashdump_path(wchar_t * wdir)
188 if( S_OK != SHGetFolderPathW( NULL,
189 CSIDL_APPDATA | CSIDL_FLAG_CREATE,
190 NULL, SHGFP_TYPE_CURRENT, wdir ) )
191 fprintf( stderr, "Can't open the vlc conf PATH\n" );
193 swprintf( wdir+wcslen( wdir ), L"%s", L"\\vlc\\crashdump" );
196 static void check_crashdump()
198 wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
199 get_crashdump_path(wdir);
201 FILE * fd = _wfopen ( wdir, L"r, ccs=UTF-8" );
202 if( fd )
204 fclose( fd );
205 int answer = MessageBox( NULL, L"VLC media player just crashed." \
206 " Do you want to send a bug report to the developers team?",
207 L"VLC crash reporting", MB_YESNO);
209 if(answer == IDYES)
211 HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
212 if(Hint)
214 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT,
215 NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
216 if(ftp)
218 SYSTEMTIME now;
219 GetSystemTime(&now);
220 wchar_t remote_file[MAX_PATH];
221 swprintf( remote_file, L"/crashs/%04d%02d%02d%02d%02d%02d",now.wYear,
222 now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond );
224 FtpPutFile( ftp, wdir, remote_file, FTP_TRANSFER_TYPE_BINARY, 0);
225 InternetCloseHandle(ftp);
227 else
228 fprintf(stderr,"Can't connect to FTP server%d\n",GetLastError());
229 InternetCloseHandle(Hint);
233 _wremove(wdir);
235 free((void *)wdir);
238 /*****************************************************************************
239 * vlc_exception_filter: handles unhandled exceptions, like segfaults
240 *****************************************************************************/
241 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
243 if(IsDebuggerPresent())
245 //If a debugger is present, pass the exception to the debugger with EXCEPTION_CONTINUE_SEARCH
246 return EXCEPTION_CONTINUE_SEARCH;
248 else
250 fprintf( stderr, "unhandled vlc exception\n" );
252 wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
253 get_crashdump_path(wdir);
254 FILE * fd = _wfopen ( wdir, L"w, ccs=UTF-8" );
255 free((void *)wdir);
257 if( !fd )
259 fprintf( stderr, "\nerror while opening file" );
260 exit( 1 );
263 OSVERSIONINFO osvi;
264 ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
265 osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
266 GetVersionEx( &osvi );
268 fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE, osvi.dwMajorVersion,
269 osvi.dwMinorVersion,
270 osvi.dwBuildNumber,
271 osvi.dwPlatformId,
272 osvi.szCSDVersion);
274 const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
275 const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
276 /*No nested exceptions for now*/
277 fwprintf( fd, L"\n\n[exceptions]\n%08x at %08x",pException->ExceptionCode,
278 pException->ExceptionAddress );
279 if( pException->NumberParameters > 0 )
281 unsigned int i;
282 for( i = 0; i < pException->NumberParameters; i++ )
283 fprintf( fd, " | %08x", pException->ExceptionInformation[i] );
286 fwprintf( fd, L"\n\n[context]\nEDI:%08x\nESI:%08x\n" \
287 "EBX:%08x\nEDX:%08x\nECX:%08x\nEAX:%08x\n" \
288 "EBP:%08x\nEIP:%08x\nESP:%08x\n",
289 pContext->Edi,pContext->Esi,pContext->Ebx,
290 pContext->Edx,pContext->Ecx,pContext->Eax,
291 pContext->Ebp,pContext->Eip,pContext->Esp );
293 fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
295 wchar_t module[ 256 ];
296 MEMORY_BASIC_INFORMATION mbi ;
297 VirtualQuery( (DWORD *)pContext->Eip, &mbi, sizeof( mbi ) ) ;
298 HINSTANCE hInstance = mbi.AllocationBase;
299 GetModuleFileName( hInstance, module, 256 ) ;
300 fwprintf( fd, L"%08x|%s\n", pContext->Eip, module );
302 DWORD pEbp = pContext->Ebp;
303 DWORD caller = *((DWORD*)pEbp + 1);
307 VirtualQuery( (DWORD *)caller, &mbi, sizeof( mbi ) ) ;
308 HINSTANCE hInstance = mbi.AllocationBase;
309 GetModuleFileName( hInstance, module, 256 ) ;
310 fwprintf( fd, L"%08x|%s\n", caller, module );
311 pEbp = *(DWORD*)pEbp ;
312 caller = *((DWORD*)pEbp + 1) ;
313 /*The last EBP points to NULL!*/
314 }while(caller);
316 fclose( fd );
317 fflush( stderr );
318 exit( 1 );
321 #endif