vlc.c 10.3 KB
Newer Older
1
2
3
/*****************************************************************************
 * vlc.c: the vlc player
 *****************************************************************************
4
 * Copyright (C) 1998-2004 the VideoLAN team
5
 * $Id$
6
7
8
 *
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
 *          Samuel Hocevar <sam@zoy.org>
9
 *          Gildas Bazin <gbazin@videolan.org>
hartman's avatar
hartman committed
10
 *          Derk-Jan Hartman <hartman at videolan dot org>
11
 *          Lots of other people, see the libvlc AUTHORS file
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * 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
dionoea's avatar
dionoea committed
25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26
 *****************************************************************************/
27

28
29
30
31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <vlc/vlc.h>
Christophe Mutricy's avatar
Christophe Mutricy committed
33
34
#include <stdio.h>
#include <stdlib.h>
35
#include <locale.h>
36

Rafaël Carré's avatar
Rafaël Carré committed
37

38
39
40
41
/* Explicit HACK */
extern void LocaleFree (const char *);
extern char *FromLocale (const char *);

42

43
44
45
/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
46
#ifdef WIN32
damienf's avatar
damienf committed
47
#include <windows.h>
48
49
extern void __wgetmainargs(int *argc, wchar_t ***wargv, wchar_t ***wenviron,
                           int expand_wildcards, int *startupinfo);
50
#else
51

52
53
54
55
56
# include <signal.h>
# include <time.h>
# include <pthread.h>

static void *SigHandler (void *set);
57
58
#endif

59
/*****************************************************************************
hartman's avatar
hartman committed
60
 * main: parse command line, start interface and spawn threads.
61
 *****************************************************************************/
62
int main( int i_argc, const char *ppsz_argv[] )
63
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
64
    int i_ret;
65

66
67
    setlocale (LC_ALL, "");

68
#ifndef __APPLE__
hartman's avatar
hartman committed
69
    /* This clutters OSX GUI error logs */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
70
    fprintf( stderr, "VLC media player %s\n", libvlc_get_version() );
hartman's avatar
hartman committed
71
#endif
72

73
#ifdef HAVE_PUTENV
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
74
#   ifndef NDEBUG
75
    /* Activate malloc checking routines to detect heap corruptions. */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
76
    putenv( (char*)"MALLOC_CHECK_=2" );
Rafaël Carré's avatar
Rafaël Carré committed
77
#       ifdef __APPLE__
78
79
    putenv( (char*)"MallocErrorAbort=crash_my_baby_crash" );
#       endif
80
81

    /* Disable the ugly Gnome crash dialog so that we properly segfault */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
82
    putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" );
83
84
85
#   endif
#endif

86
#if defined (HAVE_GETEUID) && !defined (SYS_BEOS)
87
    /* FIXME: rootwrap (); */
88
89
#endif

gbazin's avatar
gbazin committed
90
#if !defined(WIN32) && !defined(UNDER_CE)
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
    /* Synchronously intercepted POSIX signals.
     *
     * In a threaded program such as VLC, the only sane way to handle signals
     * is to block them in all thread but one - this is the only way to
     * predict which thread will receive them. If any piece of code depends
     * on delivery of one of this signal it is intrinsically not thread-safe
     * and MUST NOT be used in VLC, whether we like it or not.
     * There is only one exception: if the signal is raised with
     * pthread_kill() - we do not use this in LibVLC but some pthread
     * implementations use them internally. You should really use conditions
     * for thread synchronization anyway.
     *
     * Signal that request a clean shutdown, and force an unclean shutdown
     * if they are triggered again 2+ seconds later.
     * We have to handle SIGTERM cleanly because of daemon mode.
106
     * Note that we set the signals after the vlc_create call. */
107
    static const int exitsigs[] = { SIGINT, SIGHUP, SIGQUIT, SIGTERM };
108
109
110
111
112
113
114
    /* Signals that cause a no-op:
     * - SIGALRM should not happen, but lets stay on the safe side.
     * - SIGPIPE might happen with sockets and would crash VLC. It MUST be
     *   blocked by any LibVLC-dependant application, in addition to VLC.
     * - SIGCHLD is comes after exec*() (such as httpd CGI support) and must
     *   be dequeued to cleanup zombie processes.
     */
115
    static const int dummysigs[] = { SIGALRM, SIGPIPE, SIGCHLD };
116
117
118
119
120

    sigset_t set;
    pthread_t sigth;

    sigemptyset (&set);
121
122
123
124
    for (unsigned i = 0; i < sizeof (exitsigs) / sizeof (exitsigs[0]); i++)
        sigaddset (&set, exitsigs[i]);
    for (unsigned i = 0; i < sizeof (dummysigs) / sizeof (dummysigs[0]); i++)
        sigaddset (&set, dummysigs[i]);
125
126
127

    /* Block all these signals */
    pthread_sigmask (SIG_BLOCK, &set, NULL);
128

129
130
    for (unsigned i = 0; i < sizeof (dummysigs) / sizeof (dummysigs[0]); i++)
        sigdelset (&set, dummysigs[i]);
131
132

    pthread_create (&sigth, NULL, SigHandler, &set);
133
134
#endif

135
136
137
138
139
140
141
142
143
#ifdef WIN32
    /* Replace argv[1..n] with unicode for Windows NT and above */
    if( GetVersion() < 0x80000000 )
    {
        wchar_t **wargv, **wenvp;
        int i,i_wargc;
        int si = { 0 };
        __wgetmainargs(&i_wargc, &wargv, &wenvp, 0, &si);

144
        for( i = 0; i < i_wargc; i++ )
damienf's avatar
damienf committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
        {
            int len = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL);
            if( len > 0 )
            {
                if( len > 1 ) {
                    char *utf8arg = (char *)malloc(len);
                    if( NULL != utf8arg )
                    {
                        WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, utf8arg, len, NULL, NULL);
                        ppsz_argv[i] = utf8arg;
                    }
                    else
                    {
                        /* failed!, quit */
159
                        return 1;
damienf's avatar
damienf committed
160
161
162
163
                    }
                }
                else
                {
164
                    ppsz_argv[i] = strdup ("");
damienf's avatar
damienf committed
165
166
167
168
169
                }
            }
            else
            {
                /* failed!, quit */
170
                return 1;
damienf's avatar
damienf committed
171
172
            }
        }
173
    }
174
    else
175
#endif
176
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
177
        /* Note that FromLocale() can be used before libvlc is initialized */
178
179
180
181
        for (int i = 0; i < i_argc; i++)
            if ((ppsz_argv[i] = FromLocale (ppsz_argv[i])) == NULL)
                return 1; // BOOM!
    }
182

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
183
184
185
    libvlc_exception_t ex;
    libvlc_exception_init (&ex);

186
    /* Initialize libvlc */
187
    libvlc_instance_t *vlc = libvlc_new (i_argc, ppsz_argv, &ex);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
188
    if (vlc != NULL)
189
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
190
191
        libvlc_run_interface (vlc, NULL, &ex);
        libvlc_release (vlc);
192
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
193
194
    i_ret = libvlc_exception_raised (&ex);
    libvlc_exception_clear (&ex);
195

196
197
198
    for (int i = 0; i < i_argc; i++)
        LocaleFree (ppsz_argv[i]);

199
200
#if !defined(WIN32) && !defined(UNDER_CE)
    pthread_cancel (sigth);
201
202
203
204
205
206
# ifdef __APPLE__
    /* In Mac OS X up to 10.4.8 sigwait (among others) is not a pthread
     * cancellation point, so we throw a dummy quit signal to end
     * sigwait() in the sigth thread */
    pthread_kill (sigth, SIGQUIT);
# endif
207
208
209
    pthread_join (sigth, NULL);
#endif

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
210
    return i_ret;
211
212
}

gbazin's avatar
gbazin committed
213
#if !defined(WIN32) && !defined(UNDER_CE)
214
215
216
/*****************************************************************************
 * SigHandler: system signal handler
 *****************************************************************************
217
 * This thread receives all handled signals synchronously.
218
219
 * It tries to end the program in a clean way.
 *****************************************************************************/
220
static void *SigHandler (void *data)
221
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
222
223
    const sigset_t *exitset = (sigset_t *)data;
    sigset_t fullset;
224
    time_t abort_time = 0;
225

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
226
227
    pthread_sigmask (SIG_BLOCK, exitset, &fullset);

228
    for (;;)
229
    {
230
        int i_signal, state;
231
232
        if( sigwait (&fullset, &i_signal) != 0 )
            continue;
233

234
235
236
237
238
239
#ifdef __APPLE__
        /* In Mac OS X up to 10.4.8 sigwait (among others) is not a pthread
         * cancellation point */
        pthread_testcancel();
#endif

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
240
241
242
        if (!sigismember (exitset, i_signal))
            continue; /* Ignore "dummy" signals */

243
244
245
        /* Once a signal has been trapped, the termination sequence will be
         * armed and subsequent signals will be ignored to avoid sending
         * signals to a libvlc structure having been destroyed */
246

247
        pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
248
        if (abort_time == 0 || time (NULL) > abort_time)
249
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
250
251
            time (&abort_time);
            abort_time += 2;
252

253
            fprintf (stderr, "signal %d received, terminating vlc - do it "
254
                            "again quickly in case it gets stuck\n", i_signal);
255
            //VLC_Die( 0 );
256
        }
257
        else /* time (NULL) <= abort_time */
dionoea's avatar
dionoea committed
258
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
259
            /* If user asks again more than 2 seconds later, die badly */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
260
            pthread_sigmask (SIG_UNBLOCK, exitset, NULL);
261
            fprintf (stderr, "user insisted too much, dying badly\n");
262
263
264
265
266
267
268
269
#ifdef __APPLE__
            /* On Mac OS X, use exit(-1) as it doesn't trigger
             * backtrace generation, whereas abort() does.
             * The backtrace generation trigger a Crash Dialog
             * And takes way too much time, which is not what
             * we want. */
            exit (-1);
#else
270
            abort ();
271
#endif
272
273
        }
        pthread_setcancelstate (state, NULL);
274
    }
275
    /* Never reached */
276
277
}
#endif
gbazin's avatar
gbazin committed
278
279

#if defined(UNDER_CE)
280
281
282
#   if defined( _MSC_VER ) && defined( UNDER_CE )
#       include "vlc_common.h"
#   endif
gbazin's avatar
gbazin committed
283
284
285
286
287
288
289
290
291
/*****************************************************************************
 * WinMain: parse command line, start interface and spawn threads. (WinCE only)
 *****************************************************************************/
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPTSTR lpCmdLine, int nCmdShow )
{
    char **argv, psz_cmdline[MAX_PATH];
    int argc, i_ret;

292
    WideCharToMultiByte( CP_ACP, 0, lpCmdLine, -1,
gbazin's avatar
gbazin committed
293
294
295
296
297
298
                         psz_cmdline, MAX_PATH, NULL, NULL );

    argv = vlc_parse_cmdline( psz_cmdline, &argc );
    argv = realloc( argv, (argc + 1) * sizeof(char *) );
    if( !argv ) return -1;

299
300
    if( argc ) memmove( argv + 1, argv, argc * sizeof(char *) );
    argv[0] = ""; /* Fake program path */
gbazin's avatar
gbazin committed
301

302
    i_ret = main( argc + 1, argv );
gbazin's avatar
gbazin committed
303
304
305
306
307

    /* No need to free the argv memory */
    return i_ret;
}
#endif