]> git.sesse.net Git - vlc/blob - src/vlc.c
-
[vlc] / src / vlc.c
1 /*****************************************************************************
2  * vlc.c: the vlc player
3  *****************************************************************************
4  * Copyright (C) 1998-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@netcourrier.com>
10  *          Derk-Jan Hartman <hartman at videolan dot org>
11  *          Lots of other people, see the libvlc AUTHORS file
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
26  *****************************************************************************/
27
28 #include "config.h"
29
30 #include <stdio.h>                                              /* fprintf() */
31 #include <stdlib.h>                                  /* putenv(), strtol(),  */
32 #ifdef HAVE_SIGNAL_H
33 #   include <signal.h>                            /* SIGHUP, SIGINT, SIGKILL */
34 #endif
35 #ifdef HAVE_TIME_H
36 #   include <time.h>                                               /* time() */
37 #endif
38
39 #include <vlc/vlc.h>
40
41 /*****************************************************************************
42  * Local prototypes.
43  *****************************************************************************/
44 #ifndef WIN32
45 static void SigHandler  ( int i_signal );
46 #endif
47
48 /*****************************************************************************
49  * main: parse command line, start interface and spawn threads.
50  *****************************************************************************/
51 int main( int i_argc, char *ppsz_argv[] )
52 {
53     int i_ret;
54     int b_cli = VLC_FALSE ;
55
56 #ifndef SYS_DARWIN
57     /* This clutters OSX GUI error logs */
58     fprintf( stderr, "VLC media player %s\n", VLC_Version() );
59 #endif
60
61 #ifdef HAVE_PUTENV
62 #   ifdef DEBUG
63     /* Activate malloc checking routines to detect heap corruptions. */
64     putenv( "MALLOC_CHECK_=2" );
65
66     /* Disable the ugly Gnome crash dialog so that we properly segfault */
67     putenv( "GNOME_DISABLE_CRASH_DIALOG=1" );
68 #   endif
69
70     /* If the user isn't using VLC_VERBOSE, set it to 0 by default */
71     if( getenv( "VLC_VERBOSE" ) == NULL )
72     {
73         putenv( "VLC_VERBOSE=0" );
74     }
75 #endif
76
77     /* Create a libvlc structure */
78     i_ret = VLC_Create();
79     if( i_ret < 0 )
80     {
81         return i_ret;
82     }
83
84 #ifndef WIN32
85     /* Set the signal handlers. SIGTERM is not intercepted, because we need at
86      * least one method to kill the program when all other methods failed, and
87      * when we don't want to use SIGKILL.
88      * Note that we set the signals after the vlc_create call. */
89     signal( SIGINT,  SigHandler );
90     signal( SIGHUP,  SigHandler );
91     signal( SIGQUIT, SigHandler );
92
93     /* Other signals */
94     signal( SIGALRM, SIG_IGN );
95     signal( SIGPIPE, SIG_IGN );
96 #endif
97
98     /* Initialize libvlc */
99     i_ret = VLC_Init( 0, i_argc, ppsz_argv );
100     if( i_ret < 0 )
101     {
102         VLC_Destroy( 0 );
103         return i_ret;
104     }
105
106     i_ret = VLC_AddIntf( 0, NULL, VLC_TRUE, VLC_TRUE );
107
108     /* Finish the threads */
109     VLC_CleanUp( 0 );
110
111     /* Destroy the libvlc structure */
112     VLC_Destroy( 0 );
113
114     return i_ret;
115 }
116
117 #ifndef WIN32
118 /*****************************************************************************
119  * SigHandler: system signal handler
120  *****************************************************************************
121  * This function is called when a fatal signal is received by the program.
122  * It tries to end the program in a clean way.
123  *****************************************************************************/
124 static void SigHandler( int i_signal )
125 {
126     static time_t abort_time = 0;
127     static volatile vlc_bool_t b_die = VLC_FALSE;
128
129     /* Once a signal has been trapped, the termination sequence will be
130      * armed and subsequent signals will be ignored to avoid sending signals
131      * to a libvlc structure having been destroyed */
132
133     if( !b_die )
134     {
135         b_die = VLC_TRUE;
136         abort_time = time( NULL );
137
138         fprintf( stderr, "signal %d received, terminating vlc - do it "
139                          "again in case it gets stuck\n", i_signal );
140
141         /* Acknowledge the signal received */
142         VLC_Die( 0 );
143     }
144     else if( time( NULL ) > abort_time + 2 )
145     {
146         /* If user asks again 1 or 2 seconds later, die badly */
147         signal( SIGINT,  SIG_DFL );
148         signal( SIGHUP,  SIG_DFL );
149         signal( SIGQUIT, SIG_DFL );
150         signal( SIGALRM, SIG_DFL );
151         signal( SIGPIPE, SIG_DFL );
152
153         fprintf( stderr, "user insisted too much, dying badly\n" );
154
155         abort();
156     }
157 }
158 #endif
159