00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <string.h>
00025 #include <stdlib.h>
00026
00027 #include <vlc/vlc.h>
00028 #include <vlc/input.h>
00029 #include "vlc_playlist.h"
00030
00031 #ifdef WIN32
00032 # include "../extras/getopt.h"
00033 #endif
00034
00035 #if !defined( UNDER_CE )
00036 # include <io.h>
00037 # include <fcntl.h>
00038 #endif
00039
00040 #include <winsock.h>
00041
00042
00043
00044
00045 void system_Init( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
00046 {
00047 WSADATA Data;
00048
00049
00050 char psz_path[MAX_PATH];
00051 char *psz_vlc;
00052
00053 #if defined( UNDER_CE )
00054 wchar_t psz_wpath[MAX_PATH];
00055 if( GetModuleFileName( NULL, psz_wpath, MAX_PATH ) )
00056 {
00057 WideCharToMultiByte( CP_ACP, 0, psz_wpath, -1,
00058 psz_path, MAX_PATH, NULL, NULL );
00059 }
00060 else psz_path[0] = '\0';
00061
00062 #else
00063 if( ppsz_argv[0] )
00064 {
00065 GetFullPathName( ppsz_argv[0], MAX_PATH, psz_path, &psz_vlc );
00066 }
00067 else if( !GetModuleFileName( NULL, psz_path, MAX_PATH ) )
00068 {
00069 psz_path[0] = '\0';
00070 }
00071 #endif
00072
00073 if( (psz_vlc = strrchr( psz_path, '\\' )) ) *psz_vlc = '\0';
00074
00075 p_this->p_libvlc->psz_vlcpath = strdup( psz_path );
00076
00077
00078 #if !defined( UNDER_CE )
00079 _fmode = _O_BINARY;
00080 _setmode( _fileno( stdin ), _O_BINARY );
00081 #endif
00082
00083
00084 mdate();
00085
00086
00087 if( !WSAStartup( MAKEWORD( 2, 0 ), &Data ) )
00088 {
00089
00090 if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 0 )
00091 {
00092
00093 WSACleanup( );
00094 }
00095 else
00096 {
00097
00098 return;
00099 }
00100 }
00101
00102
00103 if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
00104 {
00105
00106 if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
00107 {
00108
00109 WSACleanup( );
00110 }
00111 else
00112 {
00113
00114 return;
00115 }
00116 }
00117
00118 fprintf( stderr, "error: can't initialize WinSocks\n" );
00119
00120 return;
00121 }
00122
00123
00124
00125
00126 static void IPCHelperThread( vlc_object_t * );
00127 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
00128
00129 void system_Configure( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
00130 {
00131 #if !defined( UNDER_CE )
00132 p_this->p_libvlc->b_fast_mutex = config_GetInt( p_this, "fast-mutex" );
00133 p_this->p_libvlc->i_win9x_cv = config_GetInt( p_this, "win9x-cv-method" );
00134
00135
00136 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
00137 # define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
00138 #endif
00139 if( config_GetInt( p_this, "high-priority" ) )
00140 {
00141 if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
00142 || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
00143 {
00144 msg_Dbg( p_this, "raised process priority" );
00145 }
00146 else
00147 {
00148 msg_Dbg( p_this, "could not raise process priority" );
00149 }
00150 }
00151
00152 if( config_GetInt( p_this, "one-instance" ) )
00153 {
00154 HANDLE hmutex;
00155
00156 msg_Info( p_this, "one instance mode ENABLED");
00157
00158
00159 if( !( hmutex = CreateMutex( 0, TRUE, _T("VLC ipc ") _T(VERSION) ) ) )
00160 {
00161
00162
00163 msg_Err( p_this, "one instance mode DISABLED "
00164 "(mutex couldn't be created)" );
00165 return;
00166 }
00167
00168 if( GetLastError() != ERROR_ALREADY_EXISTS )
00169 {
00170
00171 vlc_object_t *p_helper =
00172 (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
00173
00174
00175 if( vlc_thread_create( p_helper, "IPC helper", IPCHelperThread,
00176 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
00177 {
00178 msg_Err( p_this, "one instance mode DISABLED "
00179 "(IPC helper thread couldn't be created)" );
00180
00181 }
00182
00183
00184
00185 ReleaseMutex( hmutex );
00186 }
00187 else
00188 {
00189
00190
00191 HWND ipcwindow;
00192
00193
00194 WaitForSingleObject( hmutex, INFINITE );
00195
00196
00197
00198 if( !( ipcwindow = FindWindow( 0, _T("VLC ipc ") _T(VERSION) ) ) )
00199 {
00200 msg_Err( p_this, "one instance mode DISABLED "
00201 "(couldn't find 1st instance of program)" );
00202 ReleaseMutex( hmutex );
00203 return;
00204 }
00205
00206
00207
00208 if( *pi_argc - 1 >= optind )
00209 {
00210 COPYDATASTRUCT wm_data;
00211 int i_opt, i_data;
00212 char *p_data;
00213
00214 i_data = sizeof(int);
00215 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
00216 {
00217 i_data += sizeof(int);
00218 i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
00219 }
00220
00221 p_data = (char *)malloc( i_data );
00222 *((int *)&p_data[0]) = *pi_argc - optind;
00223 i_data = sizeof(int);
00224 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
00225 {
00226 int i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
00227 *((int *)&p_data[i_data]) = i_len;
00228 i_data += sizeof(int);
00229 memcpy( &p_data[i_data], ppsz_argv[ i_opt ], i_len );
00230 i_data += i_len;
00231 }
00232
00233
00234 wm_data.dwData = 0;
00235 wm_data.cbData = i_data;
00236 wm_data.lpData = p_data;
00237 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
00238 }
00239
00240
00241
00242 ReleaseMutex( hmutex );
00243
00244
00245 system_End( p_this );
00246 exit( 0 );
00247 }
00248 }
00249
00250 #endif
00251 }
00252
00253 static void IPCHelperThread( vlc_object_t *p_this )
00254 {
00255 HWND ipcwindow;
00256 MSG message;
00257
00258 ipcwindow =
00259 CreateWindow( _T("STATIC"),
00260 _T("VLC ipc ") _T(VERSION),
00261 0,
00262 0,
00263 0,
00264 0,
00265 0,
00266 NULL,
00267 NULL,
00268 GetModuleHandle(NULL),
00269 NULL );
00270
00271 SetWindowLong( ipcwindow, GWL_WNDPROC, (LONG)WMCOPYWNDPROC );
00272 SetWindowLong( ipcwindow, GWL_USERDATA, (LONG)p_this );
00273
00274
00275 vlc_thread_ready( p_this );
00276
00277 while( GetMessage( &message, NULL, 0, 0 ) )
00278 {
00279 TranslateMessage( &message );
00280 DispatchMessage( &message );
00281 }
00282 }
00283
00284 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
00285 LPARAM lParam )
00286 {
00287 if( uMsg == WM_COPYDATA )
00288 {
00289 COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
00290 vlc_object_t *p_this;
00291 playlist_t *p_playlist;
00292
00293 p_this = (vlc_object_t *)GetWindowLong( hwnd, GWL_USERDATA );
00294
00295 if( !p_this ) return 0;
00296
00297
00298 p_playlist = (playlist_t *)vlc_object_find( p_this,
00299 VLC_OBJECT_PLAYLIST,
00300 FIND_ANYWHERE );
00301 if( !p_playlist ) return 0;
00302
00303 if( pwm_data->lpData )
00304 {
00305 int i_argc, i_data, i_opt, i_options;
00306 char **ppsz_argv;
00307 char *p_data = (char *)pwm_data->lpData;
00308
00309 i_argc = *((int *)&p_data[0]);
00310 ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
00311 i_data = sizeof(int);
00312 for( i_opt = 0; i_opt < i_argc; i_opt++ )
00313 {
00314 ppsz_argv[i_opt] = &p_data[i_data + sizeof(int)];
00315 i_data += *((int *)&p_data[i_data]);
00316 i_data += sizeof(int);
00317 }
00318
00319 for( i_opt = 0; i_opt < i_argc; i_opt++ )
00320 {
00321 i_options = 0;
00322
00323
00324 while( i_opt + i_options + 1 < i_argc &&
00325 *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
00326 {
00327 i_options++;
00328 }
00329 if( i_opt || config_GetInt( p_this, "playlist-enqueue" ) )
00330 {
00331 playlist_AddExt( p_playlist, ppsz_argv[i_opt],
00332 ppsz_argv[i_opt], PLAYLIST_APPEND ,
00333 PLAYLIST_END, -1,
00334 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
00335 i_options );
00336 } else {
00337 playlist_AddExt( p_playlist, ppsz_argv[i_opt],
00338 ppsz_argv[i_opt], PLAYLIST_APPEND | PLAYLIST_GO,
00339 PLAYLIST_END, -1,
00340 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
00341 i_options );
00342 }
00343
00344 i_opt += i_options;
00345 }
00346
00347 free( ppsz_argv );
00348 }
00349
00350 vlc_object_release( p_playlist );
00351 }
00352
00353 return DefWindowProc( hwnd, uMsg, wParam, lParam );
00354 }
00355
00356
00357
00358
00359 void system_End( vlc_t *p_this )
00360 {
00361 if( p_this && p_this->p_libvlc && p_this->p_libvlc->psz_vlcpath )
00362 {
00363 free( p_this->p_libvlc->psz_vlcpath );
00364 p_this->p_libvlc->psz_vlcpath = NULL;
00365 }
00366
00367 WSACleanup();
00368 }