]> git.sesse.net Git - vlc/blob - src/vlc.c
same NEWS change
[vlc] / src / vlc.c
1 /*****************************************************************************
2  * vlc.c: the vlc player
3  *****************************************************************************
4  * Copyright (C) 1998-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@videolan.org>
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 #if !defined(WIN32) && !defined(UNDER_CE)
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
55 #ifndef SYS_DARWIN
56     /* This clutters OSX GUI error logs */
57     fprintf( stderr, "VLC media player %s\n", VLC_Version() );
58 #endif
59
60 #ifdef HAVE_PUTENV
61 #   ifdef DEBUG
62     /* Activate malloc checking routines to detect heap corruptions. */
63     putenv( "MALLOC_CHECK_=2" );
64
65     /* Disable the ugly Gnome crash dialog so that we properly segfault */
66     putenv( "GNOME_DISABLE_CRASH_DIALOG=1" );
67 #   endif
68
69     /* If the user isn't using VLC_VERBOSE, set it to 0 by default */
70     if( getenv( "VLC_VERBOSE" ) == NULL )
71     {
72         putenv( "VLC_VERBOSE=0" );
73     }
74 #endif
75
76     /* Create a libvlc structure */
77     i_ret = VLC_Create();
78     if( i_ret < 0 )
79     {
80         return i_ret;
81     }
82
83 #if !defined(WIN32) && !defined(UNDER_CE)
84     /* Set the signal handlers. SIGTERM is not intercepted, because we need at
85      * least one method to kill the program when all other methods failed, and
86      * when we don't want to use SIGKILL.
87      * Note that we set the signals after the vlc_create call. */
88     signal( SIGINT,  SigHandler );
89     signal( SIGHUP,  SigHandler );
90     signal( SIGQUIT, SigHandler );
91
92     /* Other signals */
93     signal( SIGALRM, SIG_IGN );
94     signal( SIGPIPE, SIG_IGN );
95 #endif
96
97     /* Initialize libvlc */
98     i_ret = VLC_Init( 0, i_argc, ppsz_argv );
99     if( i_ret < 0 )
100     {
101         VLC_Destroy( 0 );
102         return i_ret;
103     }
104
105     i_ret = VLC_AddIntf( 0, NULL, VLC_TRUE, VLC_TRUE );
106
107     /* Finish the threads */
108     VLC_CleanUp( 0 );
109
110     /* Destroy the libvlc structure */
111     VLC_Destroy( 0 );
112
113     return i_ret;
114 }
115
116 #if !defined(WIN32) && !defined(UNDER_CE)
117 /*****************************************************************************
118  * SigHandler: system signal handler
119  *****************************************************************************
120  * This function is called when a fatal signal is received by the program.
121  * It tries to end the program in a clean way.
122  *****************************************************************************/
123 static void SigHandler( int i_signal )
124 {
125     static time_t abort_time = 0;
126     static volatile vlc_bool_t b_die = VLC_FALSE;
127
128     /* Once a signal has been trapped, the termination sequence will be
129      * armed and subsequent signals will be ignored to avoid sending signals
130      * to a libvlc structure having been destroyed */
131
132     if( !b_die )
133     {
134         b_die = VLC_TRUE;
135         abort_time = time( NULL );
136
137         fprintf( stderr, "signal %d received, terminating vlc - do it "
138                          "again in case it gets stuck\n", i_signal );
139
140         /* Acknowledge the signal received */
141         VLC_Die( 0 );
142     }
143     else if( time( NULL ) > abort_time + 2 )
144     {
145         /* If user asks again 1 or 2 seconds later, die badly */
146         signal( SIGINT,  SIG_DFL );
147         signal( SIGHUP,  SIG_DFL );
148         signal( SIGQUIT, SIG_DFL );
149         signal( SIGALRM, SIG_DFL );
150         signal( SIGPIPE, SIG_DFL );
151
152         fprintf( stderr, "user insisted too much, dying badly\n" );
153
154         abort();
155     }
156 }
157 #endif
158
159 #if defined(UNDER_CE)
160 #   if defined( _MSC_VER ) && defined( UNDER_CE )
161 #       include "vlc_common.h"
162 #   endif
163 /*****************************************************************************
164  * WinMain: parse command line, start interface and spawn threads. (WinCE only)
165  *****************************************************************************/
166 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
167                     LPTSTR lpCmdLine, int nCmdShow )
168 {
169     char **argv, psz_cmdline[MAX_PATH];
170     int argc, i_ret;
171
172     WideCharToMultiByte( CP_ACP, 0, lpCmdLine, -1,
173                          psz_cmdline, MAX_PATH, NULL, NULL );
174
175     argv = vlc_parse_cmdline( psz_cmdline, &argc );
176     argv = realloc( argv, (argc + 1) * sizeof(char *) );
177     if( !argv ) return -1;
178
179     if( argc ) memmove( argv + 1, argv, argc * sizeof(char *) );
180     argv[0] = ""; /* Fake program path */
181
182     i_ret = main( argc + 1, argv );
183
184     /* No need to free the argv memory */
185     return i_ret;
186 }
187 #endif