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 *****************************************************************************/
41 extern void LocaleFree (const char *);
42 extern char *FromLocale (const char *);
45 /*****************************************************************************
47 *****************************************************************************/
50 extern void __wgetmainargs(int *argc, wchar_t ***wargv, wchar_t ***wenviron,
51 int expand_wildcards, int *startupinfo);
58 static void *SigHandler (void *set);
61 /*****************************************************************************
62 * main: parse command line, start interface and spawn threads.
63 *****************************************************************************/
64 int main( int i_argc, const char *ppsz_argv[] )
68 setlocale (LC_ALL, "");
71 /* This clutters OSX GUI error logs */
72 fprintf( stderr, "VLC media player %s\n", VLC_Version() );
77 /* Activate malloc checking routines to detect heap corruptions. */
78 putenv( (char*)"MALLOC_CHECK_=2" );
80 putenv( (char*)"MallocErrorAbort=crash_my_baby_crash" );
83 /* Disable the ugly Gnome crash dialog so that we properly segfault */
84 putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" );
88 #if defined (HAVE_GETEUID) && !defined (SYS_BEOS)
89 /* FIXME: rootwrap (); */
92 /* Create a libvlc structure */
97 #if !defined(WIN32) && !defined(UNDER_CE)
98 /* Synchronously intercepted POSIX signals.
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.
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.
122 static const int dummysigs[] = { SIGALRM, SIGPIPE, SIGCHLD };
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]);
133 /* Block all these signals */
134 pthread_sigmask (SIG_BLOCK, &set, NULL);
136 for (unsigned i = 0; i < sizeof (dummysigs) / sizeof (dummysigs[0]); i++)
137 sigdelset (&set, dummysigs[i]);
139 pthread_create (&sigth, NULL, SigHandler, &set);
143 /* Replace argv[1..n] with unicode for Windows NT and above */
144 if( GetVersion() < 0x80000000 )
146 wchar_t **wargv, **wenvp;
149 __wgetmainargs(&i_wargc, &wargv, &wenvp, 0, &si);
151 for( i = 0; i < i_wargc; i++ )
153 int len = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL);
157 char *utf8arg = (char *)malloc(len);
158 if( NULL != utf8arg )
160 WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, utf8arg, len, NULL, NULL);
161 ppsz_argv[i] = utf8arg;
171 ppsz_argv[i] = strdup ("");
184 for (int i = 0; i < i_argc; i++)
185 if ((ppsz_argv[i] = FromLocale (ppsz_argv[i])) == NULL)
189 /* Initialize libvlc */
190 i_ret = VLC_Init( id, i_argc, ppsz_argv );
194 return i_ret == VLC_EEXITSUCCESS ? 0 : -i_ret;
197 i_ret = VLC_AddIntf( 0, NULL, true, true );
199 /* Finish the threads */
202 /* Destroy the libvlc structure */
205 for (int i = 0; i < i_argc; i++)
206 LocaleFree (ppsz_argv[i]);
208 #if !defined(WIN32) && !defined(UNDER_CE)
209 pthread_cancel (sigth);
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);
216 pthread_join (sigth, NULL);
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)
231 const sigset_t *exitset = (sigset_t *)data;
233 time_t abort_time = 0;
235 pthread_sigmask (SIG_BLOCK, exitset, &fullset);
240 if( sigwait (&fullset, &i_signal) != 0 )
244 /* In Mac OS X up to 10.4.8 sigwait (among others) is not a pthread
245 * cancellation point */
246 pthread_testcancel();
249 if (!sigismember (exitset, i_signal))
250 continue; /* Ignore "dummy" signals */
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 */
256 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
257 if (abort_time == 0 || time (NULL) > abort_time)
262 fprintf (stderr, "signal %d received, terminating vlc - do it "
263 "again quickly in case it gets stuck\n", i_signal);
266 else /* time (NULL) <= abort_time */
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");
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
282 pthread_setcancelstate (state, NULL);
288 #if defined(UNDER_CE)
289 # if defined( _MSC_VER ) && defined( UNDER_CE )
290 # include "vlc_common.h"
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 )
298 char **argv, psz_cmdline[MAX_PATH];
301 WideCharToMultiByte( CP_ACP, 0, lpCmdLine, -1,
302 psz_cmdline, MAX_PATH, NULL, NULL );
304 argv = vlc_parse_cmdline( psz_cmdline, &argc );
305 argv = realloc( argv, (argc + 1) * sizeof(char *) );
306 if( !argv ) return -1;
308 if( argc ) memmove( argv + 1, argv, argc * sizeof(char *) );
309 argv[0] = ""; /* Fake program path */
311 i_ret = main( argc + 1, argv );
313 /* No need to free the argv memory */