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 /* WinSock Library Init. */
92 if( !WSAStartup( MAKEWORD( 2, 2 ), &Data ) )
94 /* Aah, pretty useless check, we should always have Winsock 2.2
95 * since it appeared in Win98. */
96 if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 2 )
97 /* We could not find a suitable WinSock DLL. */
100 /* Everything went ok. */
104 /* Let's try with WinSock 1.1 */
105 if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
107 /* Confirm that the WinSock DLL supports 1.1.*/
108 if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
109 /* We could not find a suitable WinSock DLL. */
112 /* Everything went ok. */
116 fprintf( stderr, "error: can't initialize WinSocks\n" );
119 /*****************************************************************************
120 * system_Configure: check for system specific configuration options.
121 *****************************************************************************/
122 static unsigned __stdcall IPCHelperThread( void * );
123 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
124 static vlc_object_t *p_helper = NULL;
125 static unsigned long hIPCHelper;
126 static HANDLE hIPCHelperReady;
135 void system_Configure( libvlc_int_t *p_this, int i_argc, const char *const ppsz_argv[] )
137 #if !defined( UNDER_CE )
138 /* Raise default priority of the current process */
139 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
140 # define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
142 if( var_InheritBool( p_this, "high-priority" ) )
144 if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
145 || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
147 msg_Dbg( p_this, "raised process priority" );
151 msg_Dbg( p_this, "could not raise process priority" );
155 if( var_InheritBool( p_this, "one-instance" )
156 || ( var_InheritBool( p_this, "one-instance-when-started-from-file" )
157 && var_InheritBool( p_this, "started-from-file" ) ) )
161 msg_Info( p_this, "one instance mode ENABLED");
163 /* Use a named mutex to check if another instance is already running */
164 if( !( hmutex = CreateMutex( 0, TRUE, L"VLC ipc "VERSION ) ) )
166 /* Failed for some reason. Just ignore the option and go on as
168 msg_Err( p_this, "one instance mode DISABLED "
169 "(mutex couldn't be created)" );
173 if( GetLastError() != ERROR_ALREADY_EXISTS )
175 /* We are the 1st instance. */
177 vlc_custom_create( p_this, sizeof(*p_helper), "ipc helper" );
179 /* Run the helper thread */
180 hIPCHelperReady = CreateEvent( NULL, FALSE, FALSE, NULL );
181 hIPCHelper = _beginthreadex( NULL, 0, IPCHelperThread, p_helper,
184 WaitForSingleObject( hIPCHelperReady, INFINITE );
187 msg_Err( p_this, "one instance mode DISABLED "
188 "(IPC helper thread couldn't be created)" );
189 vlc_object_release (p_helper);
192 CloseHandle( hIPCHelperReady );
194 /* Initialization done.
195 * Release the mutex to unblock other instances */
196 ReleaseMutex( hmutex );
200 /* Another instance is running */
204 /* Wait until the 1st instance is initialized */
205 WaitForSingleObject( hmutex, INFINITE );
207 /* Locate the window created by the IPC helper thread of the
209 if( !( ipcwindow = FindWindow( 0, L"VLC ipc "VERSION ) ) )
211 msg_Err( p_this, "one instance mode DISABLED "
212 "(couldn't find 1st instance of program)" );
213 ReleaseMutex( hmutex );
217 /* We assume that the remaining parameters are filenames
218 * and their input options */
221 COPYDATASTRUCT wm_data;
223 vlc_ipc_data_t *p_data;
224 size_t i_data = sizeof (*p_data);
226 for( i_opt = 0; i_opt < i_argc; i_opt++ )
228 i_data += sizeof (size_t);
229 i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
232 p_data = malloc( i_data );
233 p_data->argc = i_argc;
234 p_data->enqueue = var_InheritBool( p_this, "playlist-enqueue" );
236 for( i_opt = 0; i_opt < i_argc; i_opt++ )
238 size_t i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
239 /* Windows will never switch to an architecture
240 * with stronger alignment requirements, right. */
241 *((size_t *)(p_data->data + i_data)) = i_len;
242 i_data += sizeof (size_t);
243 memcpy( &p_data->data[i_data], ppsz_argv[ i_opt ], i_len );
246 i_data += sizeof (*p_data);
248 /* Send our playlist items to the 1st instance */
250 wm_data.cbData = i_data;
251 wm_data.lpData = p_data;
252 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
255 /* Initialization done.
256 * Release the mutex to unblock other instances */
257 ReleaseMutex( hmutex );
268 static unsigned __stdcall IPCHelperThread( void *data )
270 vlc_object_t *p_this = data;
275 CreateWindow( L"STATIC", /* name of window class */
276 L"VLC ipc "VERSION, /* window title bar text */
277 0, /* window style */
278 0, /* default X coordinate */
279 0, /* default Y coordinate */
280 0, /* window width */
281 0, /* window height */
282 NULL, /* no parent window */
283 NULL, /* no menu in this window */
284 GetModuleHandle(NULL), /* handle of this program instance */
285 NULL ); /* sent to WM_CREATE */
287 SetWindowLongPtr( ipcwindow, GWLP_WNDPROC, (LRESULT)WMCOPYWNDPROC );
288 SetWindowLongPtr( ipcwindow, GWLP_USERDATA, (LONG_PTR)p_this );
290 /* Signal the creation of the thread and events queue */
291 SetEvent( hIPCHelperReady );
293 while( GetMessage( &message, NULL, 0, 0 ) )
295 TranslateMessage( &message );
296 DispatchMessage( &message );
301 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
304 if( uMsg == WM_QUIT )
306 PostQuitMessage( 0 );
308 else if( uMsg == WM_COPYDATA )
310 COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
311 vlc_object_t *p_this;
312 playlist_t *p_playlist;
314 p_this = (vlc_object_t *)
315 (uintptr_t)GetWindowLongPtr( hwnd, GWLP_USERDATA );
317 if( !p_this ) return 0;
319 /* Add files to the playlist */
320 p_playlist = pl_Get( p_this );
322 if( pwm_data->lpData )
325 vlc_ipc_data_t *p_data = (vlc_ipc_data_t *)pwm_data->lpData;
327 int i_argc = p_data->argc, i_opt, i_options;
329 ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
330 for( i_opt = 0; i_opt < i_argc; i_opt++ )
332 ppsz_argv[i_opt] = p_data->data + i_data + sizeof(int);
333 i_data += sizeof(int) + *((int *)(p_data->data + i_data));
336 for( i_opt = 0; i_opt < i_argc; i_opt++ )
340 /* Count the input options */
341 while( i_opt + i_options + 1 < i_argc &&
342 *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
347 char *psz_URI = make_URI( ppsz_argv[i_opt], NULL );
348 playlist_AddExt( p_playlist, psz_URI,
349 NULL, PLAYLIST_APPEND |
350 ( ( i_opt || p_data->enqueue ) ? 0 : PLAYLIST_GO ),
353 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
354 VLC_INPUT_OPTION_TRUSTED,
365 return DefWindowProc( hwnd, uMsg, wParam, lParam );
368 /*****************************************************************************
369 * system_End: terminate winsock.
370 *****************************************************************************/
371 void system_End( void )
378 /* FIXME: thread-safety... */
381 if( ( ipcwindow = FindWindow( 0, L"VLC ipc "VERSION ) ) != 0 )
383 SendMessage( ipcwindow, WM_QUIT, 0, 0 );
385 vlc_object_release (p_helper);
389 #if !defined( UNDER_CE )