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