]> git.sesse.net Git - vlc/blob - bin/winvlc.c
Only load globalhotkeys if requested by caller
[vlc] / bin / winvlc.c
1 /*****************************************************************************
2  * winvlc.c: the Windows VLC player
3  *****************************************************************************
4  * Copyright (C) 1998-2008 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 <string.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <windows.h>
37
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
46
47 #ifndef UNDER_CE
48 static char *FromWide (const wchar_t *wide)
49 {
50     size_t len;
51     len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
52
53     char *out = (char *)malloc (len);
54     if (out)
55         WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
56     return out;
57 }
58 #else
59 static int parse_cmdline (char *line, char ***argvp)
60 {
61     char **argv = malloc (sizeof (char *));
62     int argc = 0;
63
64     while (*line != '\0')
65     {
66         char quote = 0;
67
68         /* Skips white spaces */
69         while (strchr ("\t ", *line))
70             line++;
71         if (!*line)
72             break;
73
74         /* Starts a new parameter */
75         argv = realloc (argv, (argc + 2) * sizeof (char *));
76         if (*line == '"')
77         {
78             quote = '"';
79             line++;
80         }
81         argv[argc++] = line;
82
83     more:
84             while (*line && !strchr ("\t ", *line))
85             line++;
86
87     if (line > argv[argc - 1] && line[-1] == quote)
88         /* End of quoted parameter */
89         line[-1] = 0;
90     else
91         if (*line && quote)
92     {
93         /* Space within a quote */
94         line++;
95         goto more;
96     }
97     else
98         /* End of unquoted parameter */
99         if (*line)
100             *line++ = 0;
101     }
102     argv[argc] = NULL;
103     *argvp = argv;
104     return argc;
105 }
106 #endif
107
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 )
115 {
116     int argc, ret;
117 #ifndef UNDER_CE
118     wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
119     if (wargv == NULL)
120         return 1;
121
122     char *argv[argc + 1];
123     for (int i = 0; i < argc; i++)
124         argv[i] = FromWide (wargv[i]);
125     argv[argc] = NULL;
126     LocalFree (wargv);
127 #else
128     char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
129
130     WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
131                          psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
132
133     argc = parse_cmdline (psz_cmdline, &argv);
134 #endif
135
136     libvlc_exception_t ex, dummy;
137     libvlc_exception_init (&ex);
138     libvlc_exception_init (&dummy);
139
140 #if !defined( UNDER_CE )
141     check_crashdump();
142     SetUnhandledExceptionFilter(vlc_exception_filter);
143 #endif
144
145     /* Initialize libvlc */
146     libvlc_instance_t *vlc;
147     vlc = libvlc_new (argc - 1, (const char **)argv + 1, &ex);
148     if (vlc != NULL)
149     {
150         libvlc_add_intf (vlc, "globalhotkeys,none", &ex);
151         libvlc_add_intf (vlc, NULL, &ex);
152         libvlc_playlist_play (vlc, -1, 0, NULL, &dummy);
153         libvlc_wait (vlc);
154         libvlc_release (vlc);
155     }
156
157     ret = libvlc_exception_raised (&ex);
158     libvlc_exception_clear (&ex);
159     libvlc_exception_clear (&dummy);
160
161     for (int i = 0; i < argc; i++)
162         free (argv[i]);
163
164     (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
165     return ret;
166 }
167
168 #if !defined( UNDER_CE )
169
170 static void get_crashdump_path(wchar_t * wdir)
171 {
172     if( S_OK != SHGetFolderPathW( NULL,
173                         CSIDL_APPDATA | CSIDL_FLAG_CREATE,
174                         NULL, SHGFP_TYPE_CURRENT, wdir ) )
175         fprintf( stderr, "Can't open the vlc conf PATH\n" );
176
177     swprintf( wdir+wcslen( wdir ), L"%s", L"\\vlc\\crashdump" );
178 }
179
180 static void check_crashdump()
181 {
182     wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
183     get_crashdump_path(wdir);
184
185     FILE * fd = _wfopen ( wdir, L"r, ccs=UTF-8" );
186     if( fd )
187     {
188         fclose( fd );
189         int answer = MessageBox( NULL, L"VLC media player just crashed." \
190         " Do you want to send a bug report to the developers team?",
191         L"VLC crash reporting", MB_YESNO);
192
193         if(answer == IDYES)
194         {
195             HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
196             if(Hint)
197             {
198                 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT,
199                                                 NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
200                 if(ftp)
201                 {
202                     SYSTEMTIME now;
203                     GetSystemTime(&now);
204                     wchar_t remote_file[MAX_PATH];
205                     swprintf( remote_file, L"/crashs/%04d%02d%02d%02d%02d%02d",now.wYear,
206                             now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond  );
207
208                     FtpPutFile( ftp, wdir, remote_file, FTP_TRANSFER_TYPE_BINARY, 0);
209                     InternetCloseHandle(ftp);
210                 }
211                 else
212                     fprintf(stderr,"Can't connect to FTP server%d\n",GetLastError());
213                 InternetCloseHandle(Hint);
214             }
215         }
216
217         _wremove(wdir);
218     }
219     free((void *)wdir);
220 }
221
222 /*****************************************************************************
223  * vlc_exception_filter: handles unhandled exceptions, like segfaults
224  *****************************************************************************/
225 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
226 {
227     if(IsDebuggerPresent())
228     {
229         //If a debugger is present, pass the exception to the debugger with EXCEPTION_CONTINUE_SEARCH
230         return EXCEPTION_CONTINUE_SEARCH;
231     }
232     else
233     {
234         fprintf( stderr, "unhandled vlc exception\n" );
235
236         wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
237         get_crashdump_path(wdir);
238         FILE * fd = _wfopen ( wdir, L"w, ccs=UTF-8" );
239         free((void *)wdir);
240
241         if( !fd )
242         {
243             fprintf( stderr, "\nerror while opening file" );
244             exit( 1 );
245         }
246
247         OSVERSIONINFO osvi;
248         ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
249         osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
250         GetVersionEx( &osvi );
251
252         fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE, osvi.dwMajorVersion,
253                                                                osvi.dwMinorVersion,
254                                                                osvi.dwBuildNumber,
255                                                                osvi.dwPlatformId,
256                                                                osvi.szCSDVersion);
257
258         const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
259         const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
260         /*No nested exceptions for now*/
261         fwprintf( fd, L"\n\n[exceptions]\n%08x at %08x",pException->ExceptionCode,
262                                                 pException->ExceptionAddress );
263         if( pException->NumberParameters > 0 )
264         {
265             unsigned int i;
266             for( i = 0; i < pException->NumberParameters; i++ )
267                 fprintf( fd, " | %08x", pException->ExceptionInformation[i] );
268         }
269
270         fwprintf( fd, L"\n\n[context]\nEDI:%08x\nESI:%08x\n" \
271                     "EBX:%08x\nEDX:%08x\nECX:%08x\nEAX:%08x\n" \
272                     "EBP:%08x\nEIP:%08x\nESP:%08x\n",
273                         pContext->Edi,pContext->Esi,pContext->Ebx,
274                         pContext->Edx,pContext->Ecx,pContext->Eax,
275                         pContext->Ebp,pContext->Eip,pContext->Esp );
276
277         fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
278
279         wchar_t module[ 256 ];
280         MEMORY_BASIC_INFORMATION mbi ;
281         VirtualQuery( (DWORD *)pContext->Eip, &mbi, sizeof( mbi ) ) ;
282         HINSTANCE hInstance = mbi.AllocationBase;
283         GetModuleFileName( hInstance, module, 256 ) ;
284         fwprintf( fd, L"%08x|%s\n", pContext->Eip, module );
285
286         DWORD pEbp = pContext->Ebp;
287         DWORD caller = *((DWORD*)pEbp + 1);
288
289         do
290         {
291             VirtualQuery( (DWORD *)caller, &mbi, sizeof( mbi ) ) ;
292             HINSTANCE hInstance = mbi.AllocationBase;
293             GetModuleFileName( hInstance, module, 256 ) ;
294             fwprintf( fd, L"%08x|%s\n", caller, module );
295             pEbp = *(DWORD*)pEbp ;
296             caller = *((DWORD*)pEbp + 1) ;
297             /*The last EBP points to NULL!*/
298         }while(caller);
299
300         fclose( fd );
301         fflush( stderr );
302         exit( 1 );
303     }
304 }
305 #endif