]> git.sesse.net Git - vlc/blob - bin/winvlc.c
de9a8d15342be13a24ffe17b8c1b1fd5d9381e40
[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) && defined ( NDEBUG )
39 #   define  _WIN32_IE 0x500
40 #   include <shlobj.h>
41 #   include <tlhelp32.h>
42 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
43 #endif
44
45 #ifndef UNDER_CE
46 static char *FromWide (const wchar_t *wide)
47 {
48     size_t len;
49     len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
50
51     char *out = (char *)malloc (len);
52     if (out)
53         WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
54     return out;
55 }
56 #else
57 static int parse_cmdline (char *line, char ***argvp)
58 {
59     char **argv = malloc (sizeof (char *));
60     int argc = 0;
61
62     while (*line != '\0')
63     {
64         char quote = 0;
65
66         /* Skips white spaces */
67         while (strchr ("\t ", *line))
68             line++;
69         if (!*line)
70             break;
71
72         /* Starts a new parameter */
73         argv = realloc (argv, (argc + 2) * sizeof (char *));
74         if (*line == '"')
75         {
76             quote = '"';
77             line++;
78         }
79         argv[argc++] = line;
80
81     more:
82             while (*line && !strchr ("\t ", *line))
83             line++;
84
85     if (line > argv[argc - 1] && line[-1] == quote)
86         /* End of quoted parameter */
87         line[-1] = 0;
88     else
89         if (*line && quote)
90     {
91         /* Space within a quote */
92         line++;
93         goto more;
94     }
95     else
96         /* End of unquoted parameter */
97         if (*line)
98             *line++ = 0;
99     }
100     argv[argc] = NULL;
101     *argvp = argv;
102     return argc;
103 }
104 #endif
105
106 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
107 #ifndef UNDER_CE
108                     LPSTR lpCmdLine,
109 #else
110                     LPWSTR lpCmdLine,
111 #endif
112                     int nCmdShow )
113 {
114     int argc, ret;
115 #ifndef UNDER_CE
116     wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
117     if (wargv == NULL)
118         return 1;
119
120     char *argv[argc + 1];
121     for (int i = 0; i < argc; i++)
122         argv[i] = FromWide (wargv[i]);
123     argv[argc] = NULL;
124     LocalFree (wargv);
125 #else
126     char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
127
128     WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
129                          psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
130
131     argc = parse_cmdline (psz_cmdline, &argv);
132 #endif
133
134     libvlc_exception_t ex, dummy;
135     libvlc_exception_init (&ex);
136     libvlc_exception_init (&dummy);
137
138 #if !defined( UNDER_CE ) && defined ( NDEBUG )
139     SetUnhandledExceptionFilter(vlc_exception_filter);
140 #endif
141
142     /* Initialize libvlc */
143     libvlc_instance_t *vlc;
144     vlc = libvlc_new (argc - 1, (const char **)argv + 1, &ex);
145     if (vlc != NULL)
146     {
147         libvlc_add_intf (vlc, NULL, &ex);
148         libvlc_playlist_play (vlc, -1, 0, NULL, &dummy);
149         libvlc_wait (vlc);
150         libvlc_release (vlc);
151     }
152
153     ret = libvlc_exception_raised (&ex);
154     libvlc_exception_clear (&ex);
155     libvlc_exception_clear (&dummy);
156
157     for (int i = 0; i < argc; i++)
158         free (argv[i]);
159
160     (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
161     return ret;
162 }
163
164 #if !defined( UNDER_CE ) && defined ( NDEBUG )
165 /*****************************************************************************
166  * vlc_exception_filter: handles unhandled exceptions, like segfaults
167  *****************************************************************************/
168 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
169 {
170     wchar_t wdir[MAX_PATH];
171
172     fprintf( stderr, "unhandled vlc exception\n" );
173
174     if( S_OK != SHGetFolderPathW( NULL,
175                          CSIDL_APPDATA | CSIDL_FLAG_CREATE,
176                                   NULL, SHGFP_TYPE_CURRENT, wdir ) )
177             fprintf( stderr, "Can't open the vlc conf PATH\n" );
178
179     swprintf( wdir+wcslen( wdir ), L"%s", L"\\vlc\\crashdump" );
180
181     FILE * fd = _wfopen ( wdir, L"w, ccs=UTF-8" );
182
183     if( !fd )
184         fprintf( stderr, "\nerror while opening file" );
185
186     OSVERSIONINFO osvi;
187     ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
188     osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
189     GetVersionEx( &osvi );
190
191     fwprintf( fd, L"[Version]\n0S=%d.%d.%d.%d.%s\nVLC=%s", osvi.dwMajorVersion,
192                                                            osvi.dwMinorVersion,
193                                                            osvi.dwBuildNumber,
194                                                            osvi.dwPlatformId,
195                                                            osvi.szCSDVersion,
196                                                            VERSION_MESSAGE);
197
198     const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
199     const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
200     /*No nested exceptions for now*/
201     fwprintf( fd, L"\n\n[Exceptions]\n%08x at %08x",pException->ExceptionCode,
202                                             pException->ExceptionAddress );
203     if( pException->NumberParameters > 0 )
204     {
205         unsigned int i;
206         for( i = 0; i < pException->NumberParameters; i++ )
207             fprintf( fd, " | %08x", pException->ExceptionInformation[i] );
208     }
209
210     fwprintf( fd, L"\n\n[CONTEXT]\nEDI:%08x\nESI:%08x\n" \
211                 "EBX:%08x\nEDX:%08xn\nECX:%08x\nEAX:%08x\n" \
212                 "EBP:%08x\nEIP:%08x\nESP:%08x\n",
213                     pContext->Edi,pContext->Esi,pContext->Ebx,
214                     pContext->Edx,pContext->Ecx,pContext->Eax,
215                     pContext->Ebp,pContext->Eip,pContext->Esp );
216
217     fwprintf( fd, L"\n\n[STACKTRACE]\n#EIP|base|module\n" );
218
219     DWORD pEbp = pContext->Ebp;
220     DWORD caller = *((DWORD*)pEbp + 1) ;
221
222     wchar_t module[ 256 ];
223
224     do
225     {
226         MEMORY_BASIC_INFORMATION mbi ;
227         VirtualQuery( (DWORD *)caller, &mbi, sizeof( mbi ) ) ;
228         HINSTANCE hInstance = mbi.AllocationBase;
229         GetModuleFileName( hInstance, module, 256 ) ;
230         fwprintf( fd, L"%08x|%08x|%s\n", caller, hInstance, module );
231         pEbp = *(DWORD*)pEbp ;
232         caller = *((DWORD*)pEbp + 1) ;
233         /*The last EBP points to NULL!*/
234     }while(caller);
235
236     fclose( fd );
237     fflush( stderr );
238     exit( 1 );
239 }
240 #endif