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