1 /*****************************************************************************
2 * specific.c: Win32 specific initilization
3 *****************************************************************************
4 * Copyright (C) 2001-2004 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>
32 #include <vlc_charset.h>
34 #include "../extras/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( libvlc_int_t *p_this, int *pi_argc, const char *ppsz_argv[] )
49 VLC_UNUSED( p_this ); VLC_UNUSED( pi_argc ); VLC_UNUSED( ppsz_argv );
51 MEMORY_BASIC_INFORMATION mbi;
53 /* Get our full path */
54 char psz_path[MAX_PATH];
57 wchar_t psz_wpath[MAX_PATH];
58 if( VirtualQuery(system_Init, &mbi, sizeof(mbi) ) )
60 HMODULE hMod = (HMODULE) mbi.AllocationBase;
61 if( GetModuleFileName( hMod, psz_wpath, MAX_PATH ) )
63 WideCharToMultiByte( CP_UTF8, 0, psz_wpath, -1,
64 psz_path, MAX_PATH, NULL, NULL );
66 else psz_path[0] = '\0';
69 if( (psz_vlc = strrchr( psz_path, '\\' )) ) *psz_vlc = '\0';
73 /* remove trailing \.libs from executable dir path if seen,
74 we assume we are running vlc through libtool wrapper in build dir */
75 int offset = strlen(psz_path)-sizeof("\\.libs")+1;
78 psz_vlc = psz_path+offset;
79 if( ! strcmp(psz_vlc, "\\.libs") ) *psz_vlc = '\0';
84 psz_vlcpath = strdup( psz_path );
86 /* Set the default file-translation mode */
87 #if !defined( UNDER_CE )
89 _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
94 /* Call mdate() once to make sure it is initialized properly */
97 /* WinSock Library Init. */
98 if( !WSAStartup( MAKEWORD( 2, 2 ), &Data ) )
100 /* Aah, pretty useless check, we should always have Winsock 2.2
101 * since it appeared in Win98. */
102 if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 2 )
103 /* We could not find a suitable WinSock DLL. */
106 /* Everything went ok. */
110 /* Let's try with WinSock 1.1 */
111 if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
113 /* Confirm that the WinSock DLL supports 1.1.*/
114 if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
115 /* We could not find a suitable WinSock DLL. */
118 /* Everything went ok. */
122 fprintf( stderr, "error: can't initialize WinSocks\n" );
125 /*****************************************************************************
126 * system_Configure: check for system specific configuration options.
127 *****************************************************************************/
128 static unsigned __stdcall IPCHelperThread( void * );
129 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
130 static vlc_object_t *p_helper = NULL;
131 static unsigned long hIPCHelper;
132 static HANDLE hIPCHelperReady;
141 void system_Configure( libvlc_int_t *p_this, int *pi_argc, const char *ppsz_argv[] )
143 #if !defined( UNDER_CE )
144 /* Raise default priority of the current process */
145 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
146 # define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
148 if( var_InheritBool( p_this, "high-priority" ) )
150 if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
151 || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
153 msg_Dbg( p_this, "raised process priority" );
157 msg_Dbg( p_this, "could not raise process priority" );
161 if( var_InheritBool( p_this, "one-instance" )
162 || ( var_InheritBool( p_this, "one-instance-when-started-from-file" )
163 && var_InheritBool( p_this, "started-from-file" ) ) )
167 msg_Info( p_this, "one instance mode ENABLED");
169 /* Use a named mutex to check if another instance is already running */
170 if( !( hmutex = CreateMutex( 0, TRUE, L"VLC ipc "VERSION ) ) )
172 /* Failed for some reason. Just ignore the option and go on as
174 msg_Err( p_this, "one instance mode DISABLED "
175 "(mutex couldn't be created)" );
179 if( GetLastError() != ERROR_ALREADY_EXISTS )
181 /* We are the 1st instance. */
182 static const char typename[] = "ipc helper";
184 vlc_custom_create( p_this, sizeof(vlc_object_t),
185 VLC_OBJECT_GENERIC, typename );
187 /* Run the helper thread */
188 hIPCHelperReady = CreateEvent( NULL, FALSE, FALSE, NULL );
189 hIPCHelper = _beginthreadex( NULL, 0, IPCHelperThread, p_helper,
192 WaitForSingleObject( hIPCHelperReady, INFINITE );
195 msg_Err( p_this, "one instance mode DISABLED "
196 "(IPC helper thread couldn't be created)" );
197 vlc_object_release (p_helper);
200 vlc_object_attach (p_helper, p_this);
201 CloseHandle( hIPCHelperReady );
203 /* Initialization done.
204 * Release the mutex to unblock other instances */
205 ReleaseMutex( hmutex );
209 /* Another instance is running */
213 /* Wait until the 1st instance is initialized */
214 WaitForSingleObject( hmutex, INFINITE );
216 /* Locate the window created by the IPC helper thread of the
218 if( !( ipcwindow = FindWindow( 0, L"VLC ipc "VERSION ) ) )
220 msg_Err( p_this, "one instance mode DISABLED "
221 "(couldn't find 1st instance of program)" );
222 ReleaseMutex( hmutex );
226 /* We assume that the remaining parameters are filenames
227 * and their input options */
228 if( *pi_argc - 1 >= optind )
230 COPYDATASTRUCT wm_data;
232 vlc_ipc_data_t *p_data;
233 size_t i_data = sizeof (*p_data);
235 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
237 i_data += sizeof (size_t);
238 i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
241 p_data = malloc( i_data );
242 p_data->argc = *pi_argc - optind;
243 p_data->enqueue = var_InheritBool( p_this, "playlist-enqueue" );
245 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
247 size_t i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
248 /* Windows will never switch to an architecture
249 * with stronger alignment requirements, right. */
250 *((size_t *)(p_data->data + i_data)) = i_len;
251 i_data += sizeof (size_t);
252 memcpy( &p_data->data[i_data], ppsz_argv[ i_opt ], i_len );
255 i_data += sizeof (*p_data);
257 /* Send our playlist items to the 1st instance */
259 wm_data.cbData = i_data;
260 wm_data.lpData = p_data;
261 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
264 /* Initialization done.
265 * Release the mutex to unblock other instances */
266 ReleaseMutex( hmutex );
269 system_End( p_this );
277 static unsigned __stdcall IPCHelperThread( void *data )
279 vlc_object_t *p_this = data;
284 CreateWindow( L"STATIC", /* name of window class */
285 L"VLC ipc "VERSION, /* window title bar text */
286 0, /* window style */
287 0, /* default X coordinate */
288 0, /* default Y coordinate */
289 0, /* window width */
290 0, /* window height */
291 NULL, /* no parent window */
292 NULL, /* no menu in this window */
293 GetModuleHandle(NULL), /* handle of this program instance */
294 NULL ); /* sent to WM_CREATE */
296 SetWindowLongPtr( ipcwindow, GWLP_WNDPROC, (LRESULT)WMCOPYWNDPROC );
297 SetWindowLongPtr( ipcwindow, GWLP_USERDATA, (LONG_PTR)p_this );
299 /* Signal the creation of the thread and events queue */
300 SetEvent( hIPCHelperReady );
302 while( GetMessage( &message, NULL, 0, 0 ) )
304 TranslateMessage( &message );
305 DispatchMessage( &message );
310 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
313 if( uMsg == WM_QUIT )
315 PostQuitMessage( 0 );
317 else if( uMsg == WM_COPYDATA )
319 COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
320 vlc_object_t *p_this;
321 playlist_t *p_playlist;
323 p_this = (vlc_object_t *)
324 (uintptr_t)GetWindowLongPtr( hwnd, GWLP_USERDATA );
326 if( !p_this ) return 0;
328 /* Add files to the playlist */
329 p_playlist = pl_Hold( p_this );
330 if( !p_playlist ) return 0;
332 if( pwm_data->lpData )
335 vlc_ipc_data_t *p_data = (vlc_ipc_data_t *)pwm_data->lpData;
337 int i_argc = p_data->argc, i_opt, i_options;
339 ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
340 for( i_opt = 0; i_opt < i_argc; i_opt++ )
342 ppsz_argv[i_opt] = p_data->data + i_data + sizeof(int);
343 i_data += sizeof(int) + *((int *)(p_data->data + i_data));
346 for( i_opt = 0; i_opt < i_argc; i_opt++ )
350 /* Count the input options */
351 while( i_opt + i_options + 1 < i_argc &&
352 *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
356 playlist_AddExt( p_playlist, ppsz_argv[i_opt],
357 NULL, PLAYLIST_APPEND |
358 ( ( i_opt || p_data->enqueue ) ? 0 : PLAYLIST_GO ),
361 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
362 VLC_INPUT_OPTION_TRUSTED,
371 vlc_object_release( p_playlist );
374 return DefWindowProc( hwnd, uMsg, wParam, lParam );
377 /*****************************************************************************
378 * system_End: terminate winsock.
379 *****************************************************************************/
380 void system_End( libvlc_int_t *p_this )
389 if (p_helper && p_helper->p_parent == VLC_OBJECT(p_this) )
391 /* this is the first instance (in a one-instance system)
392 * it is the owner of the helper thread
394 if( ( ipcwindow = FindWindow( 0, L"VLC ipc "VERSION ) ) != 0 )
396 SendMessage( ipcwindow, WM_QUIT, 0, 0 );
399 /* FIXME: thread-safety... */
400 vlc_object_release (p_helper);
404 #if !defined( UNDER_CE )