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