1 /*****************************************************************************
2 * specific.c: Win32 specific initilization
3 *****************************************************************************
4 * Copyright (C) 2001-2004, 2010 the VideoLAN team
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
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, 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 )
39 # include <mmsystem.h>
44 /*****************************************************************************
45 * system_Init: initialize winsock and misc other things.
46 *****************************************************************************/
47 void system_Init( void )
50 MEMORY_BASIC_INFORMATION mbi;
52 /* Get our full path */
53 char psz_path[MAX_PATH];
56 wchar_t psz_wpath[MAX_PATH];
57 if( VirtualQuery(system_Init, &mbi, sizeof(mbi) ) )
59 HMODULE hMod = (HMODULE) mbi.AllocationBase;
60 if( GetModuleFileName( hMod, psz_wpath, MAX_PATH ) )
62 WideCharToMultiByte( CP_UTF8, 0, psz_wpath, -1,
63 psz_path, MAX_PATH, NULL, NULL );
65 else psz_path[0] = '\0';
67 else psz_path[0] = '\0';
69 psz_vlc = strrchr( psz_path, '\\' );
74 /* remove trailing \.libs from executable dir path if seen,
75 we assume we are running vlc through libtool wrapper in build dir */
76 size_t len = strlen(psz_path);
77 if( len >= 5 && !stricmp(psz_path + len - 5, "\\.libs" ) )
78 psz_path[len - 5] = '\0';
81 psz_vlcpath = strdup( psz_path );
83 /* Set the default file-translation mode */
84 #if !defined( UNDER_CE )
86 _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
91 /* Call mdate() once to make sure it is initialized properly */
94 /* WinSock Library Init. */
95 if( !WSAStartup( MAKEWORD( 2, 2 ), &Data ) )
97 /* Aah, pretty useless check, we should always have Winsock 2.2
98 * since it appeared in Win98. */
99 if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 2 )
100 /* We could not find a suitable WinSock DLL. */
103 /* Everything went ok. */
107 /* Let's try with WinSock 1.1 */
108 if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
110 /* Confirm that the WinSock DLL supports 1.1.*/
111 if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
112 /* We could not find a suitable WinSock DLL. */
115 /* Everything went ok. */
119 fprintf( stderr, "error: can't initialize WinSocks\n" );
122 /*****************************************************************************
123 * system_Configure: check for system specific configuration options.
124 *****************************************************************************/
125 static unsigned __stdcall IPCHelperThread( void * );
126 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
127 static vlc_object_t *p_helper = NULL;
128 static unsigned long hIPCHelper;
129 static HANDLE hIPCHelperReady;
138 void system_Configure( libvlc_int_t *p_this, int i_argc, const char *const ppsz_argv[] )
140 #if !defined( UNDER_CE )
141 /* Raise default priority of the current process */
142 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
143 # define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
145 if( var_InheritBool( p_this, "high-priority" ) )
147 if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
148 || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
150 msg_Dbg( p_this, "raised process priority" );
154 msg_Dbg( p_this, "could not raise process priority" );
158 if( var_InheritBool( p_this, "one-instance" )
159 || ( var_InheritBool( p_this, "one-instance-when-started-from-file" )
160 && var_InheritBool( p_this, "started-from-file" ) ) )
164 msg_Info( p_this, "one instance mode ENABLED");
166 /* Use a named mutex to check if another instance is already running */
167 if( !( hmutex = CreateMutex( 0, TRUE, L"VLC ipc "VERSION ) ) )
169 /* Failed for some reason. Just ignore the option and go on as
171 msg_Err( p_this, "one instance mode DISABLED "
172 "(mutex couldn't be created)" );
176 if( GetLastError() != ERROR_ALREADY_EXISTS )
178 /* We are the 1st instance. */
180 vlc_custom_create( p_this, sizeof(*p_helper), "ipc helper" );
182 /* Run the helper thread */
183 hIPCHelperReady = CreateEvent( NULL, FALSE, FALSE, NULL );
184 hIPCHelper = _beginthreadex( NULL, 0, IPCHelperThread, p_helper,
187 WaitForSingleObject( hIPCHelperReady, INFINITE );
190 msg_Err( p_this, "one instance mode DISABLED "
191 "(IPC helper thread couldn't be created)" );
192 vlc_object_release (p_helper);
195 CloseHandle( hIPCHelperReady );
197 /* Initialization done.
198 * Release the mutex to unblock other instances */
199 ReleaseMutex( hmutex );
203 /* Another instance is running */
207 /* Wait until the 1st instance is initialized */
208 WaitForSingleObject( hmutex, INFINITE );
210 /* Locate the window created by the IPC helper thread of the
212 if( !( ipcwindow = FindWindow( 0, L"VLC ipc "VERSION ) ) )
214 msg_Err( p_this, "one instance mode DISABLED "
215 "(couldn't find 1st instance of program)" );
216 ReleaseMutex( hmutex );
220 /* We assume that the remaining parameters are filenames
221 * and their input options */
224 COPYDATASTRUCT wm_data;
226 vlc_ipc_data_t *p_data;
227 size_t i_data = sizeof (*p_data);
229 for( i_opt = 0; i_opt < i_argc; i_opt++ )
231 i_data += sizeof (size_t);
232 i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
235 p_data = malloc( i_data );
236 p_data->argc = i_argc;
237 p_data->enqueue = var_InheritBool( p_this, "playlist-enqueue" );
239 for( i_opt = 0; i_opt < i_argc; i_opt++ )
241 size_t i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
242 /* Windows will never switch to an architecture
243 * with stronger alignment requirements, right. */
244 *((size_t *)(p_data->data + i_data)) = i_len;
245 i_data += sizeof (size_t);
246 memcpy( &p_data->data[i_data], ppsz_argv[ i_opt ], i_len );
249 i_data += sizeof (*p_data);
251 /* Send our playlist items to the 1st instance */
253 wm_data.cbData = i_data;
254 wm_data.lpData = p_data;
255 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
258 /* Initialization done.
259 * Release the mutex to unblock other instances */
260 ReleaseMutex( hmutex );
263 system_End( p_this );
271 static unsigned __stdcall IPCHelperThread( void *data )
273 vlc_object_t *p_this = data;
278 CreateWindow( L"STATIC", /* name of window class */
279 L"VLC ipc "VERSION, /* window title bar text */
280 0, /* window style */
281 0, /* default X coordinate */
282 0, /* default Y coordinate */
283 0, /* window width */
284 0, /* window height */
285 NULL, /* no parent window */
286 NULL, /* no menu in this window */
287 GetModuleHandle(NULL), /* handle of this program instance */
288 NULL ); /* sent to WM_CREATE */
290 SetWindowLongPtr( ipcwindow, GWLP_WNDPROC, (LRESULT)WMCOPYWNDPROC );
291 SetWindowLongPtr( ipcwindow, GWLP_USERDATA, (LONG_PTR)p_this );
293 /* Signal the creation of the thread and events queue */
294 SetEvent( hIPCHelperReady );
296 while( GetMessage( &message, NULL, 0, 0 ) )
298 TranslateMessage( &message );
299 DispatchMessage( &message );
304 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
307 if( uMsg == WM_QUIT )
309 PostQuitMessage( 0 );
311 else if( uMsg == WM_COPYDATA )
313 COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
314 vlc_object_t *p_this;
315 playlist_t *p_playlist;
317 p_this = (vlc_object_t *)
318 (uintptr_t)GetWindowLongPtr( hwnd, GWLP_USERDATA );
320 if( !p_this ) return 0;
322 /* Add files to the playlist */
323 p_playlist = pl_Get( p_this );
325 if( pwm_data->lpData )
328 vlc_ipc_data_t *p_data = (vlc_ipc_data_t *)pwm_data->lpData;
330 int i_argc = p_data->argc, i_opt, i_options;
332 ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
333 for( i_opt = 0; i_opt < i_argc; i_opt++ )
335 ppsz_argv[i_opt] = p_data->data + i_data + sizeof(int);
336 i_data += sizeof(int) + *((int *)(p_data->data + i_data));
339 for( i_opt = 0; i_opt < i_argc; i_opt++ )
343 /* Count the input options */
344 while( i_opt + i_options + 1 < i_argc &&
345 *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
350 char *psz_URI = make_URI( ppsz_argv[i_opt], NULL );
351 playlist_AddExt( p_playlist, psz_URI,
352 NULL, PLAYLIST_APPEND |
353 ( ( i_opt || p_data->enqueue ) ? 0 : PLAYLIST_GO ),
356 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
357 VLC_INPUT_OPTION_TRUSTED,
368 return DefWindowProc( hwnd, uMsg, wParam, lParam );
371 /*****************************************************************************
372 * system_End: terminate winsock.
373 *****************************************************************************/
374 void system_End( libvlc_int_t *p_this )
383 if (p_helper && p_helper->p_parent == VLC_OBJECT(p_this) )
385 /* this is the first instance (in a one-instance system)
386 * it is the owner of the helper thread
388 if( ( ipcwindow = FindWindow( 0, L"VLC ipc "VERSION ) ) != 0 )
390 SendMessage( ipcwindow, WM_QUIT, 0, 0 );
393 /* FIXME: thread-safety... */
394 vlc_object_release (p_helper);
398 #if !defined( UNDER_CE )