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