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