1 /*****************************************************************************
2 * win32_specific.c: Win32 specific features
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
24 #include <string.h> /* strdup() */
25 #include <stdlib.h> /* free() */
28 #include <vlc/input.h>
29 #include "vlc_playlist.h"
32 #ifdef WIN32 /* optind, getopt(), included in unistd.h */
33 # include "../extras/getopt.h"
36 #if !defined( UNDER_CE )
43 extern void __wgetmainargs(int *argc, wchar_t ***wargv, wchar_t ***wenviron,
44 int expand_wildcards, int *startupinfo);
46 /*****************************************************************************
47 * system_Init: initialize winsock and misc other things.
48 *****************************************************************************/
49 void system_Init( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
53 /* Get our full path */
54 char psz_path[MAX_PATH];
57 #if defined( UNDER_CE )
58 wchar_t psz_wpath[MAX_PATH];
59 if( GetModuleFileName( NULL, psz_wpath, MAX_PATH ) )
61 WideCharToMultiByte( CP_ACP, 0, psz_wpath, -1,
62 psz_path, MAX_PATH, NULL, NULL );
64 else psz_path[0] = '\0';
69 GetFullPathName( ppsz_argv[0], MAX_PATH, psz_path, &psz_vlc );
71 else if( !GetModuleFileName( NULL, psz_path, MAX_PATH ) )
77 if( (psz_vlc = strrchr( psz_path, '\\' )) ) *psz_vlc = '\0';
79 p_this->p_libvlc_global->psz_vlcpath = strdup( psz_path );
81 /* Set the default file-translation mode */
82 #if !defined( UNDER_CE )
84 _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
87 /* Call mdate() once to make sure it is initialized properly */
90 /* Replace argv[1..n] with unicode for Windows NT and above */
91 if( GetVersion() < 0x80000000 )
93 wchar_t **wargv, **wenvp;
96 __wgetmainargs(&i_wargc, &wargv, &wenvp, 0, &si);
98 for( i = 1; i < i_wargc; i++ )
99 ppsz_argv[i] = FromWide( wargv[i] );
102 /* WinSock Library Init. */
103 if( !WSAStartup( MAKEWORD( 2, 2 ), &Data ) )
105 /* Aah, pretty useless check, we should always have Winsock 2.2
106 * since it appeared in Win98. */
107 if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 2 )
108 /* We could not find a suitable WinSock DLL. */
111 /* Everything went ok. */
115 /* Let's try with WinSock 1.1 */
116 if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
118 /* Confirm that the WinSock DLL supports 1.1.*/
119 if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
120 /* We could not find a suitable WinSock DLL. */
123 /* Everything went ok. */
127 fprintf( stderr, "error: can't initialize WinSocks\n" );
130 /*****************************************************************************
131 * system_Configure: check for system specific configuration options.
132 *****************************************************************************/
133 static void IPCHelperThread( vlc_object_t * );
134 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
136 void system_Configure( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
138 #if !defined( UNDER_CE )
139 p_this->p_libvlc_global->b_fast_mutex = config_GetInt( p_this, "fast-mutex" );
140 p_this->p_libvlc_global->i_win9x_cv = config_GetInt( p_this, "win9x-cv-method" );
142 /* Raise default priority of the current process */
143 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
144 # define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
146 if( config_GetInt( p_this, "high-priority" ) )
148 if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
149 || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
151 msg_Dbg( p_this, "raised process priority" );
155 msg_Dbg( p_this, "could not raise process priority" );
159 if( config_GetInt( p_this, "one-instance" )
160 || ( config_GetInt( p_this, "one-instance-when-started-from-file" )
161 && config_GetInt( p_this, "started-from-file" ) ) )
165 msg_Info( p_this, "one instance mode ENABLED");
167 /* Use a named mutex to check if another instance is already running */
168 if( !( hmutex = CreateMutex( 0, TRUE, _T("VLC ipc ") _T(VERSION) ) ) )
170 /* Failed for some reason. Just ignore the option and go on as
172 msg_Err( p_this, "one instance mode DISABLED "
173 "(mutex couldn't be created)" );
177 if( GetLastError() != ERROR_ALREADY_EXISTS )
179 /* We are the 1st instance. */
180 vlc_object_t *p_helper =
181 (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
183 /* Run the helper thread */
184 if( vlc_thread_create( p_helper, "IPC helper", IPCHelperThread,
185 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
187 msg_Err( p_this, "one instance mode DISABLED "
188 "(IPC helper thread couldn't be created)" );
192 /* Initialization done.
193 * Release the mutex to unblock other instances */
194 ReleaseMutex( hmutex );
198 /* Another instance is running */
202 /* Wait until the 1st instance is initialized */
203 WaitForSingleObject( hmutex, INFINITE );
205 /* Locate the window created by the IPC helper thread of the
207 if( !( ipcwindow = FindWindow( 0, _T("VLC ipc ") _T(VERSION) ) ) )
209 msg_Err( p_this, "one instance mode DISABLED "
210 "(couldn't find 1st instance of program)" );
211 ReleaseMutex( hmutex );
215 /* We assume that the remaining parameters are filenames
216 * and their input options */
217 if( *pi_argc - 1 >= optind )
219 COPYDATASTRUCT wm_data;
223 i_data = sizeof(int);
224 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
226 i_data += sizeof(int);
227 i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
230 p_data = (char *)malloc( i_data );
231 *((int *)&p_data[0]) = *pi_argc - optind;
232 i_data = sizeof(int);
233 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
235 int i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
236 *((int *)&p_data[i_data]) = i_len;
237 i_data += sizeof(int);
238 memcpy( &p_data[i_data], ppsz_argv[ i_opt ], i_len );
242 /* Send our playlist items to the 1st instance */
244 wm_data.cbData = i_data;
245 wm_data.lpData = p_data;
246 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
249 /* Initialization done.
250 * Release the mutex to unblock other instances */
251 ReleaseMutex( hmutex );
254 system_End( p_this );
262 static void IPCHelperThread( vlc_object_t *p_this )
268 CreateWindow( _T("STATIC"), /* name of window class */
269 _T("VLC ipc ") _T(VERSION), /* window title bar text */
270 0, /* window style */
271 0, /* default X coordinate */
272 0, /* default Y coordinate */
273 0, /* window width */
274 0, /* window height */
275 NULL, /* no parent window */
276 NULL, /* no menu in this window */
277 GetModuleHandle(NULL), /* handle of this program instance */
278 NULL ); /* sent to WM_CREATE */
280 SetWindowLong( ipcwindow, GWL_WNDPROC, (LONG)WMCOPYWNDPROC );
281 SetWindowLong( ipcwindow, GWL_USERDATA, (LONG)p_this );
283 /* Signal the creation of the thread and events queue */
284 vlc_thread_ready( p_this );
286 while( GetMessage( &message, NULL, 0, 0 ) )
288 TranslateMessage( &message );
289 DispatchMessage( &message );
293 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
296 if( uMsg == WM_COPYDATA )
298 COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
299 vlc_object_t *p_this;
300 playlist_t *p_playlist;
302 p_this = (vlc_object_t *)GetWindowLong( hwnd, GWL_USERDATA );
304 if( !p_this ) return 0;
306 /* Add files to the playlist */
307 p_playlist = (playlist_t *)vlc_object_find( p_this,
310 if( !p_playlist ) return 0;
312 if( pwm_data->lpData )
314 int i_argc, i_data, i_opt, i_options;
316 char *p_data = (char *)pwm_data->lpData;
318 i_argc = *((int *)&p_data[0]);
319 ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
320 i_data = sizeof(int);
321 for( i_opt = 0; i_opt < i_argc; i_opt++ )
323 ppsz_argv[i_opt] = &p_data[i_data + sizeof(int)];
324 i_data += *((int *)&p_data[i_data]);
325 i_data += sizeof(int);
328 for( i_opt = 0; i_opt < i_argc; i_opt++ )
332 /* Count the input options */
333 while( i_opt + i_options + 1 < i_argc &&
334 *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
338 if( i_opt || config_GetInt( p_this, "playlist-enqueue" ) )
340 playlist_AddExt( p_playlist, ppsz_argv[i_opt],
341 NULL, PLAYLIST_APPEND ,
343 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
344 i_options, VLC_TRUE );
346 playlist_AddExt( p_playlist, ppsz_argv[i_opt],
347 NULL, PLAYLIST_APPEND | PLAYLIST_GO,
349 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
350 i_options, VLC_TRUE );
359 vlc_object_release( p_playlist );
362 return DefWindowProc( hwnd, uMsg, wParam, lParam );
365 /*****************************************************************************
366 * system_End: terminate winsock.
367 *****************************************************************************/
368 void system_End( libvlc_int_t *p_this )
370 if( p_this && p_this->p_libvlc_global && p_this->p_libvlc_global->psz_vlcpath )
372 free( p_this->p_libvlc_global->psz_vlcpath );
373 p_this->p_libvlc_global->psz_vlcpath = NULL;