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