]> git.sesse.net Git - vlc/blob - src/vlc.c
* ./include/vlc/vlc.h, ./src/libvlc.c: added VLC_Error() to the libvlc API.
[vlc] / src / vlc.c
1 /*****************************************************************************
2  * vlc.c: the vlc player
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VideoLAN
5  * $Id: vlc.c,v 1.15 2002/10/14 16:46:55 sam Exp $
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 #include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
28 #include <stdio.h>                                              /* fprintf() */
29 #include <stdlib.h>                                  /* putenv(), strtol(),  */
30 #include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
31 #include <time.h>                                                  /* time() */
32
33 #include <vlc/vlc.h>
34
35 /*****************************************************************************
36  * Local prototypes.
37  *****************************************************************************/
38 #ifndef WIN32
39 static void SigHandler  ( int i_signal );
40 #endif
41
42 /*****************************************************************************
43  * main: parse command line, start interface and spawn threads
44  *****************************************************************************/
45 int main( int i_argc, char *ppsz_argv[] )
46 {
47     int i_ret;
48
49     fprintf( stderr, "VideoLAN Client %s\n", VLC_Version() );
50
51 #ifdef HAVE_PUTENV
52 #   ifdef DEBUG
53     /* Activate malloc checking routines to detect heap corruptions. */
54     putenv( "MALLOC_CHECK_=2" );
55
56     /* Disable the ugly Gnome crash dialog so that we properly segfault */
57     putenv( "GNOME_DISABLE_CRASH_DIALOG=1" );
58 #   endif
59
60     /* If the user isn't using VLC_VERBOSE, set it to 0 by default */
61     if( getenv( "VLC_VERBOSE" ) == NULL )
62     {
63         putenv( "VLC_VERBOSE=0" );
64     }
65 #endif
66
67     /* Create a libvlc structure */
68     i_ret = VLC_Create();
69     if( i_ret < 0 )
70     {
71         return i_ret;
72     }
73
74 #ifndef WIN32
75     /* Set the signal handlers. SIGTERM is not intercepted, because we need at
76      * least one method to kill the program when all other methods failed, and
77      * when we don't want to use SIGKILL.
78      * Note that we set the signals after the vlc_create call. */
79     signal( SIGINT,  SigHandler );
80     signal( SIGHUP,  SigHandler );
81     signal( SIGQUIT, SigHandler );
82
83     /* Other signals */
84     signal( SIGALRM, SIG_IGN );
85     signal( SIGPIPE, SIG_IGN );
86 #endif
87
88     /* Initialize libvlc */
89     i_ret = VLC_Init( 0, i_argc, ppsz_argv );
90     if( i_ret < 0 )
91     {
92         VLC_Destroy( 0 );
93         return i_ret;
94     }
95
96     /* Run libvlc, in non-blocking mode */
97     i_ret = VLC_Play( 0 );
98
99     /* Add a blocking interface and keep the return value */
100     i_ret = VLC_AddIntf( 0, NULL, VLC_TRUE );
101
102     /* Finish the threads */
103     VLC_Stop( 0 );
104
105     /* Destroy the libvlc structure */
106     VLC_Destroy( 0 );
107
108     return i_ret;
109 }
110
111 #ifndef WIN32
112 /*****************************************************************************
113  * SigHandler: system signal handler
114  *****************************************************************************
115  * This function is called when a fatal signal is received by the program.
116  * It tries to end the program in a clean way.
117  *****************************************************************************/
118 static void SigHandler( int i_signal )
119 {
120     static time_t abort_time = 0;
121     static volatile vlc_bool_t b_die = VLC_FALSE;
122
123     /* Once a signal has been trapped, the termination sequence will be
124      * armed and subsequent signals will be ignored to avoid sending signals
125      * to a libvlc structure having been destroyed */
126
127     if( !b_die )
128     {
129         b_die = VLC_TRUE;
130         abort_time = time( NULL );
131
132         fprintf( stderr, "signal %d received, terminating vlc - do it "
133                          "again in case it gets stuck\n", i_signal );
134
135         /* Acknowledge the signal received */
136         VLC_Die( 0 );
137     }
138     else if( time( NULL ) > abort_time + 2 )
139     {
140         /* If user asks again 1 or 2 seconds later, die badly */
141         signal( SIGINT,  SIG_DFL );
142         signal( SIGHUP,  SIG_DFL );
143         signal( SIGQUIT, SIG_DFL );
144         signal( SIGALRM, SIG_DFL );
145         signal( SIGPIPE, SIG_DFL );
146
147         fprintf( stderr, "user insisted too much, dying badly\n" );
148
149         abort();
150     }
151 }
152 #endif
153