winvlc.c 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*****************************************************************************
 * winvlc.c: the Windows VLC player
 *****************************************************************************
 * Copyright (C) 1998-2008 the VideoLAN team
 *
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
 *          Samuel Hocevar <sam@zoy.org>
 *          Gildas Bazin <gbazin@videolan.org>
 *          Derk-Jan Hartman <hartman at videolan dot org>
 *          Lots of other people, see the libvlc AUTHORS file
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#define UNICODE
#include <vlc/vlc.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

38
#if !defined(UNDER_CE)
39 40 41
# ifndef _WIN32_IE
#   define  _WIN32_IE 0x501
# endif
42 43
#   include <shlobj.h>
#   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
#define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
50 51
#endif

52
#ifndef UNDER_CE
53
static char *FromWide (const wchar_t *wide)
54
{
55 56
    size_t len;
    len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
57

58 59 60 61
    char *out = (char *)malloc (len);
    if (out)
        WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
    return out;
62
}
63 64 65 66 67 68 69 70 71
#else
static int parse_cmdline (char *line, char ***argvp)
{
    char **argv = malloc (sizeof (char *));
    int argc = 0;

    while (*line != '\0')
    {
        char quote = 0;
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        /* Skips white spaces */
        while (strchr ("\t ", *line))
            line++;
        if (!*line)
            break;

        /* Starts a new parameter */
        argv = realloc (argv, (argc + 2) * sizeof (char *));
        if (*line == '"')
        {
            quote = '"';
            line++;
        }
        argv[argc++] = line;

    more:
            while (*line && !strchr ("\t ", *line))
            line++;

    if (line > argv[argc - 1] && line[-1] == quote)
        /* End of quoted parameter */
        line[-1] = 0;
    else
        if (*line && quote)
    {
        /* Space within a quote */
        line++;
        goto more;
    }
    else
        /* End of unquoted parameter */
        if (*line)
            *line++ = 0;
    }
    argv[argc] = NULL;
    *argvp = argv;
    return argc;
}
#endif
112

113 114 115 116 117 118 119
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
#ifndef UNDER_CE
                    LPSTR lpCmdLine,
#else
                    LPWSTR lpCmdLine,
#endif
                    int nCmdShow )
120
{
121
    int argc;
122
#ifndef UNDER_CE
123
    HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
124 125 126 127 128 129 130 131 132 133 134 135 136 137

    HINSTANCE h_Kernel32 = LoadLibraryW(L"kernel32.dll");
    if(h_Kernel32)
    {
        BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
# define PROCESS_DEP_ENABLE 1

        mySetProcessDEPPolicy = (BOOL WINAPI (*)(DWORD))
                            GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
        if(mySetProcessDEPPolicy)
            mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
        FreeLibrary(h_Kernel32);
    }

138 139 140
    wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
    if (wargv == NULL)
        return 1;
141

142
    char *argv[argc + 2];
143 144
    BOOL crash_handling = TRUE;
    int j = 0;
145

146
    argv[j++] = FromWide( L"--media-library" );
147 148
    argv[j++] = FromWide( L"--no-ignore-config" );
    for (int i = 1; i < argc; i++)
149 150 151 152
    {
        if(!wcscmp(wargv[i], L"--no-crashdump"))
        {
            crash_handling = FALSE;
Rafaël Carré's avatar
Rafaël Carré committed
153
            continue; /* don't give argument to libvlc */
154
        }
Rafaël Carré's avatar
Rafaël Carré committed
155 156

        argv[j++] = FromWide (wargv[i]);
157 158 159
    }

    argc = j;
160 161
    argv[argc] = NULL;
    LocalFree (wargv);
162

163
# ifndef _WIN64
164 165 166 167 168
    if(crash_handling)
    {
        check_crashdump();
        SetUnhandledExceptionFilter(vlc_exception_filter);
    }
169
# endif /* WIN64 */
170

171 172 173 174 175 176 177 178
#else
    char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];

    WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
                         psz_cmdline, sizeof (psz_cmdline), NULL, NULL );

    argc = parse_cmdline (psz_cmdline, &argv);
#endif
179 180

    /* Initialize libvlc */
181
    libvlc_instance_t *vlc;
182
    vlc = libvlc_new (argc, (const char **)argv);
183 184
    if (vlc != NULL)
    {
185 186
        libvlc_add_intf (vlc, "globalhotkeys,none");
        libvlc_add_intf (vlc, NULL);
187
        libvlc_playlist_play (vlc, -1, 0, NULL);
188 189 190 191
        libvlc_wait (vlc);
        libvlc_release (vlc);
    }

192 193 194 195
    for (int i = 0; i < argc; i++)
        free (argv[i]);

    (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
196
    return 0;
197
}
198

199
#if !defined( UNDER_CE ) && !defined( _WIN64 )
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

static void get_crashdump_path(wchar_t * wdir)
{
    if( S_OK != SHGetFolderPathW( NULL,
                        CSIDL_APPDATA | CSIDL_FLAG_CREATE,
                        NULL, SHGFP_TYPE_CURRENT, wdir ) )
        fprintf( stderr, "Can't open the vlc conf PATH\n" );

    swprintf( wdir+wcslen( wdir ), L"%s", L"\\vlc\\crashdump" );
}

static void check_crashdump()
{
    wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
    get_crashdump_path(wdir);

    FILE * fd = _wfopen ( wdir, L"r, ccs=UTF-8" );
    if( fd )
    {
        fclose( fd );
        int answer = MessageBox( NULL, L"VLC media player just crashed." \
        " Do you want to send a bug report to the developers team?",
        L"VLC crash reporting", MB_YESNO);

        if(answer == IDYES)
        {
            HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
            if(Hint)
            {
                HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT,
                                                NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
                if(ftp)
                {
                    SYSTEMTIME now;
                    GetSystemTime(&now);
                    wchar_t remote_file[MAX_PATH];
                    swprintf( remote_file, L"/crashs/%04d%02d%02d%02d%02d%02d",now.wYear,
                            now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond  );

                    FtpPutFile( ftp, wdir, remote_file, FTP_TRANSFER_TYPE_BINARY, 0);
                    InternetCloseHandle(ftp);
                }
                else
                    fprintf(stderr,"Can't connect to FTP server%d\n",GetLastError());
                InternetCloseHandle(Hint);
            }
        }

        _wremove(wdir);
    }
    free((void *)wdir);
}

253 254 255 256 257
/*****************************************************************************
 * vlc_exception_filter: handles unhandled exceptions, like segfaults
 *****************************************************************************/
LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
{
258
    if(IsDebuggerPresent())
259
    {
260 261
        //If a debugger is present, pass the exception to the debugger with EXCEPTION_CONTINUE_SEARCH
        return EXCEPTION_CONTINUE_SEARCH;
262
    }
263
    else
264
    {
265
        fprintf( stderr, "unhandled vlc exception\n" );
266

267 268 269 270 271 272 273 274 275 276
        wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
        get_crashdump_path(wdir);
        FILE * fd = _wfopen ( wdir, L"w, ccs=UTF-8" );
        free((void *)wdir);

        if( !fd )
        {
            fprintf( stderr, "\nerror while opening file" );
            exit( 1 );
        }
277

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
        OSVERSIONINFO osvi;
        ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
        osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
        GetVersionEx( &osvi );

        fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE, osvi.dwMajorVersion,
                                                               osvi.dwMinorVersion,
                                                               osvi.dwBuildNumber,
                                                               osvi.dwPlatformId,
                                                               osvi.szCSDVersion);

        const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
        const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
        /*No nested exceptions for now*/
        fwprintf( fd, L"\n\n[exceptions]\n%08x at %08x",pException->ExceptionCode,
                                                pException->ExceptionAddress );
        if( pException->NumberParameters > 0 )
        {
            unsigned int i;
            for( i = 0; i < pException->NumberParameters; i++ )
                fprintf( fd, " | %08x", pException->ExceptionInformation[i] );
        }
300

301 302 303 304 305 306
        fwprintf( fd, L"\n\n[context]\nEDI:%08x\nESI:%08x\n" \
                    "EBX:%08x\nEDX:%08x\nECX:%08x\nEAX:%08x\n" \
                    "EBP:%08x\nEIP:%08x\nESP:%08x\n",
                        pContext->Edi,pContext->Esi,pContext->Ebx,
                        pContext->Edx,pContext->Ecx,pContext->Eax,
                        pContext->Ebp,pContext->Eip,pContext->Esp );
307

308
        fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
309

310 311 312
        wchar_t module[ 256 ];
        MEMORY_BASIC_INFORMATION mbi ;
        VirtualQuery( (DWORD *)pContext->Eip, &mbi, sizeof( mbi ) ) ;
313 314
        HINSTANCE hInstance = mbi.AllocationBase;
        GetModuleFileName( hInstance, module, 256 ) ;
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
        fwprintf( fd, L"%08x|%s\n", pContext->Eip, module );

        DWORD pEbp = pContext->Ebp;
        DWORD caller = *((DWORD*)pEbp + 1);

        do
        {
            VirtualQuery( (DWORD *)caller, &mbi, sizeof( mbi ) ) ;
            HINSTANCE hInstance = mbi.AllocationBase;
            GetModuleFileName( hInstance, module, 256 ) ;
            fwprintf( fd, L"%08x|%s\n", caller, module );
            pEbp = *(DWORD*)pEbp ;
            caller = *((DWORD*)pEbp + 1) ;
            /*The last EBP points to NULL!*/
        }while(caller);

        fclose( fd );
        fflush( stderr );
        exit( 1 );
    }
335 336
}
#endif