]> git.sesse.net Git - vlc/blob - src/vlc.c
Move __wgetmainargs into VLC's main.
[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 <stdio.h>                                              /* fprintf() */
31 #include <stdlib.h>                                  /* putenv(), strtol(),  */
32 #ifdef HAVE_SIGNAL_H
33 #   include <signal.h>                            /* SIGHUP, SIGINT, SIGKILL */
34 #endif
35 #ifdef HAVE_TIME_H
36 #   include <time.h>                                               /* time() */
37 #endif
38
39 #include <vlc/vlc.h>
40
41 #ifdef WIN32
42 extern void __wgetmainargs(int *argc, wchar_t ***wargv, wchar_t ***wenviron,
43                            int expand_wildcards, int *startupinfo);
44 #endif
45
46 /*****************************************************************************
47  * Local prototypes.
48  *****************************************************************************/
49 #if !defined(WIN32) && !defined(UNDER_CE)
50 static void SigHandler  ( int i_signal );
51 #endif
52
53 /*****************************************************************************
54  * main: parse command line, start interface and spawn threads.
55  *****************************************************************************/
56 int main( int i_argc, char *ppsz_argv[] )
57 {
58     int i_ret;
59
60 #ifndef __APPLE__
61     /* This clutters OSX GUI error logs */
62     fprintf( stderr, "VLC media player %s\n", VLC_Version() );
63 #endif
64
65 #ifdef HAVE_PUTENV
66 #   ifdef DEBUG
67     /* Activate malloc checking routines to detect heap corruptions. */
68     putenv( (char*)"MALLOC_CHECK_=2" );
69
70     /* Disable the ugly Gnome crash dialog so that we properly segfault */
71     putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" );
72 #   endif
73
74     /* If the user isn't using VLC_VERBOSE, set it to 0 by default */
75     if( getenv( "VLC_VERBOSE" ) == NULL )
76     {
77         putenv( (char *)"VLC_VERBOSE=0" );
78     }
79 #endif
80
81 #if defined (HAVE_GETEUID) && !defined (SYS_BEOS)
82     /* FIXME: rootwrap (); */
83 #endif
84
85     /* Create a libvlc structure */
86     i_ret = VLC_Create();
87     if( i_ret < 0 )
88     {
89         return i_ret;
90     }
91
92 #if !defined(WIN32) && !defined(UNDER_CE)
93     /* Set the signal handlers. SIGTERM is not intercepted, because we need at
94      * least one method to kill the program when all other methods failed, and
95      * when we don't want to use SIGKILL.
96      * Note that we set the signals after the vlc_create call. */
97     signal( SIGINT,  SigHandler );
98     signal( SIGHUP,  SigHandler );
99     signal( SIGQUIT, SigHandler );
100
101     /* Other signals */
102     signal( SIGALRM, SIG_IGN );
103     signal( SIGPIPE, SIG_IGN );
104 #endif
105
106 #ifdef WIN32
107     /* Replace argv[1..n] with unicode for Windows NT and above */
108     if( GetVersion() < 0x80000000 )
109     {
110         wchar_t **wargv, **wenvp;
111         int i,i_wargc;
112         int si = { 0 };
113         __wgetmainargs(&i_wargc, &wargv, &wenvp, 0, &si);
114
115         for( i = 1; i < i_wargc; i++ )
116             ppsz_argv[i] = FromWide( wargv[i] );
117     }
118 #endif
119
120     /* Initialize libvlc */
121     i_ret = VLC_Init( 0, i_argc, ppsz_argv );
122     if( i_ret < 0 )
123     {
124         VLC_Destroy( 0 );
125         return i_ret == VLC_EEXITSUCCESS ? 0 : i_ret;
126     }
127
128     i_ret = VLC_AddIntf( 0, NULL, VLC_TRUE, VLC_TRUE );
129
130     /* Finish the threads */
131     VLC_CleanUp( 0 );
132
133     /* Destroy the libvlc structure */
134     VLC_Destroy( 0 );
135
136     return i_ret;
137 }
138
139 #if !defined(WIN32) && !defined(UNDER_CE)
140 /*****************************************************************************
141  * SigHandler: system signal handler
142  *****************************************************************************
143  * This function is called when a fatal signal is received by the program.
144  * It tries to end the program in a clean way.
145  *****************************************************************************/
146 static void SigHandler( int i_signal )
147 {
148     static time_t abort_time = 0;
149     static volatile vlc_bool_t b_die = VLC_FALSE;
150
151     /* Once a signal has been trapped, the termination sequence will be
152      * armed and subsequent signals will be ignored to avoid sending signals
153      * to a libvlc structure having been destroyed */
154
155     if( !b_die )
156     {
157         b_die = VLC_TRUE;
158         abort_time = time( NULL );
159
160         fprintf( stderr, "signal %d received, terminating vlc - do it "
161                          "again in case it gets stuck\n", i_signal );
162
163         /* Acknowledge the signal received */
164         VLC_Die( 0 );
165     }
166     else if( time( NULL ) > abort_time + 2 )
167     {
168         /* If user asks again 1 or 2 seconds later, die badly */
169         signal( SIGINT,  SIG_DFL );
170         signal( SIGHUP,  SIG_DFL );
171         signal( SIGQUIT, SIG_DFL );
172         signal( SIGALRM, SIG_DFL );
173         signal( SIGPIPE, SIG_DFL );
174
175         fprintf( stderr, "user insisted too much, dying badly\n" );
176
177         abort();
178     }
179 }
180 #endif
181
182 #if defined(UNDER_CE)
183 #   if defined( _MSC_VER ) && defined( UNDER_CE )
184 #       include "vlc_common.h"
185 #   endif
186 /*****************************************************************************
187  * WinMain: parse command line, start interface and spawn threads. (WinCE only)
188  *****************************************************************************/
189 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
190                     LPTSTR lpCmdLine, int nCmdShow )
191 {
192     char **argv, psz_cmdline[MAX_PATH];
193     int argc, i_ret;
194
195     WideCharToMultiByte( CP_ACP, 0, lpCmdLine, -1,
196                          psz_cmdline, MAX_PATH, NULL, NULL );
197
198     argv = vlc_parse_cmdline( psz_cmdline, &argc );
199     argv = realloc( argv, (argc + 1) * sizeof(char *) );
200     if( !argv ) return -1;
201
202     if( argc ) memmove( argv + 1, argv, argc * sizeof(char *) );
203     argv[0] = ""; /* Fake program path */
204
205     i_ret = main( argc + 1, argv );
206
207     /* No need to free the argv memory */
208     return i_ret;
209 }
210 #endif