1 /*****************************************************************************
2 * specific.c: Win32 specific initilization
3 *****************************************************************************
4 * Copyright (C) 2001-2004, 2010 VLC authors and VideoLAN
6 * Authors: Samuel Hocevar <sam@zoy.org>
7 * Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
29 #include <vlc_common.h>
30 #include "../libvlc.h"
31 #include <vlc_playlist.h>
34 #include "../config/vlc_getopt.h"
36 #if !defined( UNDER_CE )
37 # include <mmsystem.h>
42 /*****************************************************************************
43 * system_Init: initialize winsock and misc other things.
44 *****************************************************************************/
45 void system_Init( void )
48 MEMORY_BASIC_INFORMATION mbi;
50 /* Get our full path */
51 char psz_path[MAX_PATH];
54 wchar_t psz_wpath[MAX_PATH];
55 if( VirtualQuery(system_Init, &mbi, sizeof(mbi) ) )
57 HMODULE hMod = (HMODULE) mbi.AllocationBase;
58 if( GetModuleFileName( hMod, psz_wpath, MAX_PATH ) )
60 WideCharToMultiByte( CP_UTF8, 0, psz_wpath, -1,
61 psz_path, MAX_PATH, NULL, NULL );
63 else psz_path[0] = '\0';
65 else psz_path[0] = '\0';
67 psz_vlc = strrchr( psz_path, '\\' );
72 /* remove trailing \.libs from executable dir path if seen,
73 we assume we are running vlc through libtool wrapper in build dir */
74 size_t len = strlen(psz_path);
75 if( len >= 5 && !stricmp(psz_path + len - 5, "\\.libs" ) )
76 psz_path[len - 5] = '\0';
79 psz_vlcpath = strdup( psz_path );
81 /* Set the default file-translation mode */
82 #if !defined( UNDER_CE )
86 /* WinSock Library Init. */
87 if( !WSAStartup( MAKEWORD( 2, 2 ), &Data ) )
89 /* Aah, pretty useless check, we should always have Winsock 2.2
90 * since it appeared in Win98. */
91 if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 2 )
92 /* We could not find a suitable WinSock DLL. */
95 /* Everything went ok. */
99 /* Let's try with WinSock 1.1 */
100 if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
102 /* Confirm that the WinSock DLL supports 1.1.*/
103 if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
104 /* We could not find a suitable WinSock DLL. */
107 /* Everything went ok. */
111 fprintf( stderr, "error: can't initialize WinSocks\n" );
114 /*****************************************************************************
115 * system_Configure: check for system specific configuration options.
116 *****************************************************************************/
117 static unsigned __stdcall IPCHelperThread( void * );
118 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
119 static vlc_object_t *p_helper = NULL;
120 static unsigned long hIPCHelper;
121 static HANDLE hIPCHelperReady;
130 void system_Configure( libvlc_int_t *p_this, int i_argc, const char *const ppsz_argv[] )
132 #if !defined( UNDER_CE )
133 /* Raise default priority of the current process */
134 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
135 # define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
137 if( var_InheritBool( p_this, "high-priority" ) )
139 if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
140 || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
142 msg_Dbg( p_this, "raised process priority" );
146 msg_Dbg( p_this, "could not raise process priority" );
150 if( var_InheritBool( p_this, "one-instance" )
151 || ( var_InheritBool( p_this, "one-instance-when-started-from-file" )
152 && var_InheritBool( p_this, "started-from-file" ) ) )
156 msg_Info( p_this, "one instance mode ENABLED");
158 /* Use a named mutex to check if another instance is already running */
159 if( !( hmutex = CreateMutex( 0, TRUE, L"VLC ipc "VERSION ) ) )
161 /* Failed for some reason. Just ignore the option and go on as
163 msg_Err( p_this, "one instance mode DISABLED "
164 "(mutex couldn't be created)" );
168 if( GetLastError() != ERROR_ALREADY_EXISTS )
170 /* We are the 1st instance. */
172 vlc_custom_create( p_this, sizeof(*p_helper), "ipc helper" );
174 /* Run the helper thread */
175 hIPCHelperReady = CreateEvent( NULL, FALSE, FALSE, NULL );
176 hIPCHelper = _beginthreadex( NULL, 0, IPCHelperThread, p_helper,
179 WaitForSingleObject( hIPCHelperReady, INFINITE );
182 msg_Err( p_this, "one instance mode DISABLED "
183 "(IPC helper thread couldn't be created)" );
184 vlc_object_release (p_helper);
187 CloseHandle( hIPCHelperReady );
189 /* Initialization done.
190 * Release the mutex to unblock other instances */
191 ReleaseMutex( hmutex );
195 /* Another instance is running */
199 /* Wait until the 1st instance is initialized */
200 WaitForSingleObject( hmutex, INFINITE );
202 /* Locate the window created by the IPC helper thread of the
204 if( !( ipcwindow = FindWindow( 0, L"VLC ipc "VERSION ) ) )
206 msg_Err( p_this, "one instance mode DISABLED "
207 "(couldn't find 1st instance of program)" );
208 ReleaseMutex( hmutex );
212 /* We assume that the remaining parameters are filenames
213 * and their input options */
216 COPYDATASTRUCT wm_data;
218 vlc_ipc_data_t *p_data;
219 size_t i_data = sizeof (*p_data);
221 for( i_opt = 0; i_opt < i_argc; i_opt++ )
223 i_data += sizeof (size_t);
224 i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
227 p_data = malloc( i_data );
228 p_data->argc = i_argc;
229 p_data->enqueue = var_InheritBool( p_this, "playlist-enqueue" );
231 for( i_opt = 0; i_opt < i_argc; i_opt++ )
233 size_t i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
234 /* Windows will never switch to an architecture
235 * with stronger alignment requirements, right. */
236 *((size_t *)(p_data->data + i_data)) = i_len;
237 i_data += sizeof (size_t);
238 memcpy( &p_data->data[i_data], ppsz_argv[ i_opt ], i_len );
241 i_data += sizeof (*p_data);
243 /* Send our playlist items to the 1st instance */
245 wm_data.cbData = i_data;
246 wm_data.lpData = p_data;
247 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
250 /* Initialization done.
251 * Release the mutex to unblock other instances */
252 ReleaseMutex( hmutex );
263 static unsigned __stdcall IPCHelperThread( void *data )
265 vlc_object_t *p_this = data;
270 CreateWindow( L"STATIC", /* name of window class */
271 L"VLC ipc "VERSION, /* window title bar text */
272 0, /* window style */
273 0, /* default X coordinate */
274 0, /* default Y coordinate */
275 0, /* window width */
276 0, /* window height */
277 NULL, /* no parent window */
278 NULL, /* no menu in this window */
279 GetModuleHandle(NULL), /* handle of this program instance */
280 NULL ); /* sent to WM_CREATE */
282 SetWindowLongPtr( ipcwindow, GWLP_WNDPROC, (LRESULT)WMCOPYWNDPROC );
283 SetWindowLongPtr( ipcwindow, GWLP_USERDATA, (LONG_PTR)p_this );
285 /* Signal the creation of the thread and events queue */
286 SetEvent( hIPCHelperReady );
288 while( GetMessage( &message, NULL, 0, 0 ) )
290 TranslateMessage( &message );
291 DispatchMessage( &message );
296 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
299 if( uMsg == WM_QUIT )
301 PostQuitMessage( 0 );
303 else if( uMsg == WM_COPYDATA )
305 COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
306 vlc_object_t *p_this;
307 playlist_t *p_playlist;
309 p_this = (vlc_object_t *)
310 (uintptr_t)GetWindowLongPtr( hwnd, GWLP_USERDATA );
312 if( !p_this ) return 0;
314 /* Add files to the playlist */
315 p_playlist = pl_Get( p_this );
317 if( pwm_data->lpData )
320 vlc_ipc_data_t *p_data = (vlc_ipc_data_t *)pwm_data->lpData;
322 int i_argc = p_data->argc, i_opt, i_options;
324 ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
325 for( i_opt = 0; i_opt < i_argc; i_opt++ )
327 ppsz_argv[i_opt] = p_data->data + i_data + sizeof(int);
328 i_data += sizeof(int) + *((int *)(p_data->data + i_data));
331 for( i_opt = 0; i_opt < i_argc; i_opt++ )
335 /* Count the input options */
336 while( i_opt + i_options + 1 < i_argc &&
337 *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
342 char *psz_URI = make_URI( ppsz_argv[i_opt], NULL );
343 playlist_AddExt( p_playlist, psz_URI,
344 NULL, PLAYLIST_APPEND |
345 ( ( i_opt || p_data->enqueue ) ? 0 : PLAYLIST_GO ),
348 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
349 VLC_INPUT_OPTION_TRUSTED,
360 return DefWindowProc( hwnd, uMsg, wParam, lParam );
363 /*****************************************************************************
364 * system_End: terminate winsock.
365 *****************************************************************************/
366 void system_End( void )
373 /* FIXME: thread-safety... */
376 if( ( ipcwindow = FindWindow( 0, L"VLC ipc "VERSION ) ) != 0 )
378 SendMessage( ipcwindow, WM_QUIT, 0, 0 );
380 vlc_object_release (p_helper);
384 #if !defined( UNDER_CE )