]> git.sesse.net Git - vlc/blob - src/vlc.c
Remember not to include anything before vlc/vlc.h
[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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 #include "config.h"
29
30 #include <vlc/vlc.h>
31 #include <locale.h>
32
33 /* Explicit HACK */
34 extern void LocaleFree (const char *);
35 extern char *FromLocale (const char *);
36
37
38 /*****************************************************************************
39  * Local prototypes.
40  *****************************************************************************/
41 #ifdef WIN32
42 #include <windows.h>
43 extern void __wgetmainargs(int *argc, wchar_t ***wargv, wchar_t ***wenviron,
44                            int expand_wildcards, int *startupinfo);
45 static inline void Kill(void) { }
46 #else
47
48 # include <signal.h>
49 # include <time.h>
50 # include <pthread.h>
51
52 static void Kill (void);
53 static void *SigHandler (void *set);
54 #endif
55
56 /*****************************************************************************
57  * main: parse command line, start interface and spawn threads.
58  *****************************************************************************/
59 int main( int i_argc, char *ppsz_argv[] )
60 {
61     int i_ret;
62
63     setlocale (LC_ALL, "");
64
65 #ifndef __APPLE__
66     /* This clutters OSX GUI error logs */
67     fprintf( stderr, "VLC media player %s\n", VLC_Version() );
68 #endif
69
70 #ifdef HAVE_PUTENV
71 #   ifdef DEBUG
72     /* Activate malloc checking routines to detect heap corruptions. */
73     putenv( (char*)"MALLOC_CHECK_=2" );
74
75     /* Disable the ugly Gnome crash dialog so that we properly segfault */
76     putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" );
77 #   endif
78
79     /* If the user isn't using VLC_VERBOSE, set it to 0 by default */
80     if( getenv( "VLC_VERBOSE" ) == NULL )
81     {
82         putenv( (char *)"VLC_VERBOSE=0" );
83     }
84 #endif
85
86 #if defined (HAVE_GETEUID) && !defined (SYS_BEOS)
87     /* FIXME: rootwrap (); */
88 #endif
89
90     /* Create a libvlc structure */
91     i_ret = VLC_Create();
92     if( i_ret < 0 )
93     {
94         return -i_ret;
95     }
96
97 #if !defined(WIN32) && !defined(UNDER_CE)
98     /* Synchronously intercepted signals. Thy request a clean shutdown,
99      * and force an unclean shutdown if they are triggered again 2+ seconds
100      * later. We have to handle SIGTERM cleanly because of daemon mode.
101      * Note that we set the signals after the vlc_create call. */
102     static const int exitsigs[] = { SIGINT, SIGHUP, SIGQUIT, SIGTERM };
103     static const int dummysigs[] = { SIGALRM, SIGPIPE, SIGCHLD };
104
105     sigset_t set;
106     pthread_t sigth;
107
108     sigemptyset (&set);
109     for (unsigned i = 0; i < sizeof (exitsigs) / sizeof (exitsigs[0]); i++)
110         sigaddset (&set, exitsigs[i]);
111     for (unsigned i = 0; i < sizeof (dummysigs) / sizeof (dummysigs[0]); i++)
112         sigaddset (&set, dummysigs[i]);
113
114     /* Block all these signals */
115     pthread_sigmask (SIG_BLOCK, &set, NULL);
116
117     for (unsigned i = 0; i < sizeof (dummysigs) / sizeof (dummysigs[0]); i++)
118         sigdelset (&set, dummysigs[i]);
119
120     pthread_create (&sigth, NULL, SigHandler, &set);
121 #endif
122
123 #ifdef WIN32
124     /* Replace argv[1..n] with unicode for Windows NT and above */
125     if( GetVersion() < 0x80000000 )
126     {
127         wchar_t **wargv, **wenvp;
128         int i,i_wargc;
129         int si = { 0 };
130         __wgetmainargs(&i_wargc, &wargv, &wenvp, 0, &si);
131
132         for( i = 0; i < i_wargc; i++ )
133         {
134             int len = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL);
135             if( len > 0 )
136             {
137                 if( len > 1 ) {
138                     char *utf8arg = (char *)malloc(len);
139                     if( NULL != utf8arg )
140                     {
141                         WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, utf8arg, len, NULL, NULL);
142                         ppsz_argv[i] = utf8arg;
143                     }
144                     else
145                     {
146                         /* failed!, quit */
147                         return 1;
148                     }
149                 }
150                 else
151                 {
152                     ppsz_argv[i] = strdup ("");
153                 }
154             }
155             else
156             {
157                 /* failed!, quit */
158                 return 1;
159             }
160         }
161     }
162     else
163 #endif
164     {
165         for (int i = 0; i < i_argc; i++)
166             if ((ppsz_argv[i] = FromLocale (ppsz_argv[i])) == NULL)
167                 return 1; // BOOM!
168     }
169
170     /* Initialize libvlc */
171     i_ret = VLC_Init( 0, i_argc, ppsz_argv );
172     if( i_ret < 0 )
173     {
174         VLC_Destroy( 0 );
175         return i_ret == VLC_EEXITSUCCESS ? 0 : -i_ret;
176     }
177
178     i_ret = VLC_AddIntf( 0, NULL, VLC_TRUE, VLC_TRUE );
179
180     /* Finish the threads */
181     VLC_CleanUp( 0 );
182
183     Kill ();
184
185     /* Destroy the libvlc structure */
186     VLC_Destroy( 0 );
187
188     for (int i = 0; i < i_argc; i++)
189         LocaleFree (ppsz_argv[i]);
190
191 #if !defined(WIN32) && !defined(UNDER_CE)
192     pthread_cancel (sigth);
193 # ifdef __APPLE__
194     /* In Mac OS X up to 10.4.8 sigwait (among others) is not a pthread
195      * cancellation point, so we throw a dummy quit signal to end
196      * sigwait() in the sigth thread */
197     pthread_kill (sigth, SIGQUIT);
198 # endif
199     pthread_join (sigth, NULL);
200 #endif
201
202     return -i_ret;
203 }
204
205 #if !defined(WIN32) && !defined(UNDER_CE)
206 /*****************************************************************************
207  * SigHandler: system signal handler
208  *****************************************************************************
209  * This thread receives all handled signals synchronously.
210  * It tries to end the program in a clean way.
211  *****************************************************************************/
212 static void *SigHandler (void *data)
213 {
214     const sigset_t *set = (sigset_t *)data;
215     time_t abort_time = 0;
216
217     for (;;)
218     {
219         int i_signal, state;
220         (void)sigwait (set, &i_signal);
221
222 #ifdef __APPLE__
223         /* In Mac OS X up to 10.4.8 sigwait (among others) is not a pthread
224          * cancellation point */
225         pthread_testcancel();
226 #endif
227
228         /* Once a signal has been trapped, the termination sequence will be
229          * armed and subsequent signals will be ignored to avoid sending
230          * signals to a libvlc structure having been destroyed */
231
232         pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
233         if (abort_time == 0)
234         {
235             time (&abort_time);
236             abort_time += 2;
237
238             fprintf (stderr, "signal %d received, terminating vlc - do it "
239                             "again in case it gets stuck\n", i_signal);
240
241             /* Acknowledge the signal received */
242             Kill ();
243         }
244         else
245         if (time (NULL) <= abort_time)
246         {
247             /* If user asks again more than 2 seconds later, die badly */
248             pthread_sigmask (SIG_UNBLOCK, set, NULL);
249             fprintf (stderr, "user insisted too much, dying badly\n");
250             abort ();
251         }
252         pthread_setcancelstate (state, NULL);
253     }
254     /* Never reached */
255 }
256
257
258 static void KillOnce (void)
259 {
260     VLC_Die (0);
261 }
262
263 static void Kill (void)
264 {
265     static pthread_once_t once = PTHREAD_ONCE_INIT;
266     pthread_once (&once, KillOnce);
267 }
268
269 #endif
270
271 #if defined(UNDER_CE)
272 #   if defined( _MSC_VER ) && defined( UNDER_CE )
273 #       include "vlc_common.h"
274 #   endif
275 /*****************************************************************************
276  * WinMain: parse command line, start interface and spawn threads. (WinCE only)
277  *****************************************************************************/
278 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
279                     LPTSTR lpCmdLine, int nCmdShow )
280 {
281     char **argv, psz_cmdline[MAX_PATH];
282     int argc, i_ret;
283
284     WideCharToMultiByte( CP_ACP, 0, lpCmdLine, -1,
285                          psz_cmdline, MAX_PATH, NULL, NULL );
286
287     argv = vlc_parse_cmdline( psz_cmdline, &argc );
288     argv = realloc( argv, (argc + 1) * sizeof(char *) );
289     if( !argv ) return -1;
290
291     if( argc ) memmove( argv + 1, argv, argc * sizeof(char *) );
292     argv[0] = ""; /* Fake program path */
293
294     i_ret = main( argc + 1, argv );
295
296     /* No need to free the argv memory */
297     return i_ret;
298 }
299 #endif