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