winvlc.c 4.08 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
static int parse_cmdline (char *line, char ***argvp)
39
{
40 41
    char **argv = malloc (sizeof (char *));
    int argc = 0;
42

43
    while (*line != '\0')
44
    {
45
        char quote = 0;
46

47 48 49 50 51
        /* Skips white spaces */
        while (strchr ("\t ", *line))
            line++;
        if (!*line)
            break;
52

53 54 55
        /* Starts a new parameter */
        argv = realloc (argv, (argc + 2) * sizeof (char *));
        if (*line == '"')
56
        {
57 58 59 60
            quote = '"';
            line++;
        }
        argv[argc++] = line;
61

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

66 67 68
        if (line > argv[argc - 1] && line[-1] == quote)
            /* End of quoted parameter */
            line[-1] = 0;
69
        else
70
        if (*line && quote)
71
        {
72 73 74
            /* Space within a quote */
            line++;
            goto more;
75
        }
76 77 78 79
        else
        /* End of unquoted parameter */
        if (*line)
            *line++ = 0;
80
    }
81 82 83
    argv[argc] = NULL;
    *argvp = argv;
    return argc;
84 85 86 87 88 89 90 91 92 93
}

#ifdef UNDER_CE
# define wWinMain WinMain
#endif

/*****************************************************************************
 * wWinMain: parse command line, start interface and spawn threads.
 *****************************************************************************/
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
94
                     LPWSTR lpCmdLine, int nCmdShow )
95
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
96
    char **argv, psz_cmdline[wcslen(lpCmdLine) * 4 + 1];
97 98 99 100 101
    int argc, ret;

    (void)hInstance; (void)hPrevInstance; (void)nCmdShow;

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

    argc = parse_cmdline (psz_cmdline, &argv);
105

106
    libvlc_exception_t ex, dummy;
107
    libvlc_exception_init (&ex);
108
    libvlc_exception_init (&dummy);
109 110 111 112 113

    /* Initialize libvlc */
    libvlc_instance_t *vlc = libvlc_new (argc, (const char **)argv, &ex);
    if (vlc != NULL)
    {
114
        libvlc_add_intf (vlc, NULL, &ex);
115
        libvlc_playlist_play (vlc, -1, 0, NULL, &dummy);
116
        libvlc_wait (vlc);
117 118
        libvlc_release (vlc);
    }
119
    free (argv);
120 121 122

    ret = libvlc_exception_raised (&ex);
    libvlc_exception_clear (&ex);
123
    libvlc_exception_clear (&dummy);
124 125
    return ret;
}
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
126 127 128 129 130 131

#ifndef IF_MINGW_SUPPORTED_UNICODE
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPSTR args, int nCmdShow)
{
    /* This makes little sense, but at least it links properly */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
132 133
    wchar_t lpCmdLine[(strlen (args) + 1) * 3];
    MultiByteToWideChar (CP_ACP, 0, args, -1, lpCmdLine, sizeof (lpCmdLine));
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
134 135 136
    return wWinMain (hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}
#endif