]> git.sesse.net Git - vlc/blob - src/vlc.c
* ./configure.ac.in: duplicated arguments to AM_INIT_AUTOMAKE to fix
[vlc] / src / vlc.c
1 /*****************************************************************************
2  * vlc.c: the vlc player
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VideoLAN
5  * $Id: vlc.c,v 1.12 2002/10/03 18:56:09 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 #include "config.h"
36
37 /*****************************************************************************
38  * Local prototypes.
39  *****************************************************************************/
40 #ifndef WIN32
41 static void SigHandler  ( int i_signal );
42 #endif
43
44 /*****************************************************************************
45  * main: parse command line, start interface and spawn threads
46  *****************************************************************************/
47 int main( int i_argc, char *ppsz_argv[] )
48 {
49     vlc_error_t err;
50
51     fprintf( stderr, COPYRIGHT_MESSAGE "\n" );
52
53 #ifdef HAVE_PUTENV
54 #   ifdef DEBUG
55     /* Activate malloc checking routines to detect heap corruptions. */
56     putenv( "MALLOC_CHECK_=2" );
57
58     /* Disable the ugly Gnome crash dialog so that we properly segfault */
59     putenv( "GNOME_DISABLE_CRASH_DIALOG=1" );
60 #   endif
61
62     /* If the user isn't using VLC_VERBOSE, set it to 1 by default */
63     if( getenv( "VLC_VERBOSE" ) == NULL )
64     {
65         putenv( "VLC_VERBOSE=1" );
66     }
67 #endif
68
69     /* Create a libvlc structure */
70     err = vlc_create();
71     if( err != VLC_SUCCESS )
72     {
73         return err;
74     }
75
76 #ifndef WIN32
77     /* Set the signal handlers. SIGTERM is not intercepted, because we need at
78      * least one method to kill the program when all other methods failed, and
79      * when we don't want to use SIGKILL.
80      * Note that we set the signals after the vlc_create call. */
81     signal( SIGINT,  SigHandler );
82     signal( SIGHUP,  SigHandler );
83     signal( SIGQUIT, SigHandler );
84
85     /* Other signals */
86     signal( SIGALRM, SIG_IGN );
87     signal( SIGPIPE, SIG_IGN );
88 #endif
89
90     /* Initialize libvlc */
91     err = vlc_init( i_argc, ppsz_argv );
92     if( err != VLC_SUCCESS )
93     {
94         vlc_destroy();
95         return err;
96     }
97
98     /* Run libvlc, in non-blocking mode */
99     err = vlc_play();
100
101     /* Add background interfaces */
102 #if 0
103     { int i; for( i=10; i--; ) vlc_add_intf( NULL, "dummy", 0 ); }
104     vlc_add_intf( NULL, "dummy", VLC_FALSE );
105     vlc_add_intf( NULL, "logger", VLC_FALSE );
106     vlc_add_intf( NULL, "xosd", VLC_FALSE );
107     vlc_add_intf( NULL, "gtk", VLC_FALSE );
108     vlc_add_intf( NULL, "kde", VLC_FALSE );
109     vlc_add_intf( "rc", VLC_FALSE );
110 #endif
111
112     /* Add a blocking interface and keep the return value */
113     err = vlc_add_intf( NULL, VLC_TRUE );
114
115     /* Finish the threads */
116     vlc_stop();
117
118     /* Destroy the libvlc structure */
119     vlc_destroy();
120
121     return err;
122 }
123
124 #ifndef WIN32
125 /*****************************************************************************
126  * SigHandler: system signal handler
127  *****************************************************************************
128  * This function is called when a fatal signal is received by the program.
129  * It tries to end the program in a clean way.
130  *****************************************************************************/
131 static void SigHandler( int i_signal )
132 {
133     static time_t abort_time = 0;
134     static volatile vlc_bool_t b_die = VLC_FALSE;
135
136     /* Once a signal has been trapped, the termination sequence will be
137      * armed and subsequent signals will be ignored to avoid sending signals
138      * to a libvlc structure having been destroyed */
139
140     if( !b_die )
141     {
142         b_die = VLC_TRUE;
143         abort_time = time( NULL );
144
145         fprintf( stderr, "signal %d received, terminating vlc - do it "
146                          "again in case it gets stuck\n", i_signal );
147
148         /* Acknowledge the signal received */
149         vlc_die();
150     }
151     else if( time( NULL ) > abort_time + 2 )
152     {
153         /* If user asks again 1 or 2 seconds later, die badly */
154         signal( SIGINT,  SIG_DFL );
155         signal( SIGHUP,  SIG_DFL );
156         signal( SIGQUIT, SIG_DFL );
157         signal( SIGALRM, SIG_DFL );
158         signal( SIGPIPE, SIG_DFL );
159
160         fprintf( stderr, "user insisted too much, dying badly\n" );
161
162         abort();
163     }
164 }
165 #endif
166