1 /*****************************************************************************
2 * vlc.c: the vlc player
3 *****************************************************************************
4 * Copyright (C) 1998-2004 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
37 extern void LocaleFree (const char *);
38 extern char *FromLocale (const char *);
41 /*****************************************************************************
43 *****************************************************************************/
46 extern void __wgetmainargs(int *argc, wchar_t ***wargv, wchar_t ***wenviron,
47 int expand_wildcards, int *startupinfo);
48 static inline void Kill(void) { }
55 static void Kill (void);
56 static void *SigHandler (void *set);
59 /*****************************************************************************
60 * main: parse command line, start interface and spawn threads.
61 *****************************************************************************/
62 int main( int i_argc, char *ppsz_argv[] )
66 setlocale (LC_ALL, "");
69 /* This clutters OSX GUI error logs */
70 fprintf( stderr, "VLC media player %s\n", VLC_Version() );
75 /* Activate malloc checking routines to detect heap corruptions. */
76 putenv( (char*)"MALLOC_CHECK_=2" );
78 /* Disable the ugly Gnome crash dialog so that we properly segfault */
79 putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" );
82 /* If the user isn't using VLC_VERBOSE, set it to 0 by default */
83 if( getenv( "VLC_VERBOSE" ) == NULL )
85 putenv( (char *)"VLC_VERBOSE=0" );
89 #if defined (HAVE_GETEUID) && !defined (SYS_BEOS)
90 /* FIXME: rootwrap (); */
93 /* Create a libvlc structure */
100 #if !defined(WIN32) && !defined(UNDER_CE)
101 /* Synchronously intercepted signals. They request a clean shutdown,
102 * and force an unclean shutdown if they are triggered again 2+ seconds
103 * later. We have to handle SIGTERM cleanly because of daemon mode.
104 * Note that we set the signals after the vlc_create call. */
105 static const int exitsigs[] = { SIGINT, SIGHUP, SIGQUIT, SIGTERM };
106 static const int dummysigs[] = { SIGALRM, SIGPIPE, SIGCHLD };
112 for (unsigned i = 0; i < sizeof (exitsigs) / sizeof (exitsigs[0]); i++)
113 sigaddset (&set, exitsigs[i]);
114 for (unsigned i = 0; i < sizeof (dummysigs) / sizeof (dummysigs[0]); i++)
115 sigaddset (&set, dummysigs[i]);
117 /* Block all these signals */
118 pthread_sigmask (SIG_BLOCK, &set, NULL);
120 for (unsigned i = 0; i < sizeof (dummysigs) / sizeof (dummysigs[0]); i++)
121 sigdelset (&set, dummysigs[i]);
123 pthread_create (&sigth, NULL, SigHandler, &set);
127 /* Replace argv[1..n] with unicode for Windows NT and above */
128 if( GetVersion() < 0x80000000 )
130 wchar_t **wargv, **wenvp;
133 __wgetmainargs(&i_wargc, &wargv, &wenvp, 0, &si);
135 for( i = 0; i < i_wargc; i++ )
137 int len = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL);
141 char *utf8arg = (char *)malloc(len);
142 if( NULL != utf8arg )
144 WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, utf8arg, len, NULL, NULL);
145 ppsz_argv[i] = utf8arg;
155 ppsz_argv[i] = strdup ("");
168 for (int i = 0; i < i_argc; i++)
169 if ((ppsz_argv[i] = FromLocale (ppsz_argv[i])) == NULL)
173 /* Initialize libvlc */
174 i_ret = VLC_Init( 0, i_argc, ppsz_argv );
178 return i_ret == VLC_EEXITSUCCESS ? 0 : -i_ret;
181 i_ret = VLC_AddIntf( 0, NULL, VLC_TRUE, VLC_TRUE );
183 /* Finish the threads */
188 /* Destroy the libvlc structure */
191 for (int i = 0; i < i_argc; i++)
192 LocaleFree (ppsz_argv[i]);
194 #if !defined(WIN32) && !defined(UNDER_CE)
195 pthread_cancel (sigth);
197 /* In Mac OS X up to 10.4.8 sigwait (among others) is not a pthread
198 * cancellation point, so we throw a dummy quit signal to end
199 * sigwait() in the sigth thread */
200 pthread_kill (sigth, SIGQUIT);
202 pthread_join (sigth, NULL);
208 #if !defined(WIN32) && !defined(UNDER_CE)
209 /*****************************************************************************
210 * SigHandler: system signal handler
211 *****************************************************************************
212 * This thread receives all handled signals synchronously.
213 * It tries to end the program in a clean way.
214 *****************************************************************************/
215 static void *SigHandler (void *data)
217 const sigset_t *set = (sigset_t *)data;
218 time_t abort_time = 0;
223 (void)sigwait (set, &i_signal);
226 /* In Mac OS X up to 10.4.8 sigwait (among others) is not a pthread
227 * cancellation point */
228 pthread_testcancel();
231 /* Once a signal has been trapped, the termination sequence will be
232 * armed and subsequent signals will be ignored to avoid sending
233 * signals to a libvlc structure having been destroyed */
235 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
241 fprintf (stderr, "signal %d received, terminating vlc - do it "
242 "again in case it gets stuck\n", i_signal);
244 /* Acknowledge the signal received */
248 if (time (NULL) <= abort_time)
250 /* If user asks again more than 2 seconds later, die badly */
251 pthread_sigmask (SIG_UNBLOCK, set, NULL);
252 fprintf (stderr, "user insisted too much, dying badly\n");
255 pthread_setcancelstate (state, NULL);
261 static void KillOnce (void)
266 static void Kill (void)
268 static pthread_once_t once = PTHREAD_ONCE_INIT;
269 pthread_once (&once, KillOnce);
274 #if defined(UNDER_CE)
275 # if defined( _MSC_VER ) && defined( UNDER_CE )
276 # include "vlc_common.h"
278 /*****************************************************************************
279 * WinMain: parse command line, start interface and spawn threads. (WinCE only)
280 *****************************************************************************/
281 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
282 LPTSTR lpCmdLine, int nCmdShow )
284 char **argv, psz_cmdline[MAX_PATH];
287 WideCharToMultiByte( CP_ACP, 0, lpCmdLine, -1,
288 psz_cmdline, MAX_PATH, NULL, NULL );
290 argv = vlc_parse_cmdline( psz_cmdline, &argc );
291 argv = realloc( argv, (argc + 1) * sizeof(char *) );
292 if( !argv ) return -1;
294 if( argc ) memmove( argv + 1, argv, argc * sizeof(char *) );
295 argv[0] = ""; /* Fake program path */
297 i_ret = main( argc + 1, argv );
299 /* No need to free the argv memory */