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