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 *****************************************************************************/
30 #include <vlc_common.h>
31 #include "../libvlc.h"
32 #include <vlc_playlist.h>
33 #include <vlc_charset.h>
35 #include "../extras/getopt.h"
37 #if !defined( UNDER_CE )
40 # include <mmsystem.h>
45 /*****************************************************************************
46 * system_Init: initialize winsock and misc other things.
47 *****************************************************************************/
48 void system_Init( libvlc_int_t *p_this, int *pi_argc, const char *ppsz_argv[] )
52 /* Get our full path */
53 char psz_path[MAX_PATH];
56 wchar_t psz_wpath[MAX_PATH];
57 if( GetModuleFileName( NULL, psz_wpath, MAX_PATH ) )
59 WideCharToMultiByte( CP_ACP, 0, psz_wpath, -1,
60 psz_path, MAX_PATH, NULL, NULL );
62 else psz_path[0] = '\0';
64 if( (psz_vlc = strrchr( psz_path, '\\' )) ) *psz_vlc = '\0';
68 /* remove trailing \.libs from executable dir path if seen,
69 we assume we are running vlc through libtool wrapper in build dir */
70 int offset = strlen(psz_path)-sizeof("\\.libs")+1;
73 psz_vlc = psz_path+offset;
74 if( ! strcmp(psz_vlc, "\\.libs") ) *psz_vlc = '\0';
79 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 */
89 /* Call mdate() once to make sure it is initialized properly */
92 /* WinSock Library Init. */
93 if( !WSAStartup( MAKEWORD( 2, 2 ), &Data ) )
95 /* Aah, pretty useless check, we should always have Winsock 2.2
96 * since it appeared in Win98. */
97 if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 2 )
98 /* We could not find a suitable WinSock DLL. */
101 /* Everything went ok. */
105 /* Let's try with WinSock 1.1 */
106 if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
108 /* Confirm that the WinSock DLL supports 1.1.*/
109 if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
110 /* We could not find a suitable WinSock DLL. */
113 /* Everything went ok. */
117 fprintf( stderr, "error: can't initialize WinSocks\n" );
120 /*****************************************************************************
121 * system_Configure: check for system specific configuration options.
122 *****************************************************************************/
123 static unsigned __stdcall IPCHelperThread( void * );
124 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
125 static vlc_object_t *p_helper = NULL;
126 static unsigned long hIPCHelper;
127 static HANDLE hIPCHelperReady;
136 void system_Configure( libvlc_int_t *p_this, int *pi_argc, const char *ppsz_argv[] )
138 #if !defined( UNDER_CE )
139 /* Raise default priority of the current process */
140 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
141 # define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
143 if( config_GetInt( p_this, "high-priority" ) )
145 if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
146 || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
148 msg_Dbg( p_this, "raised process priority" );
152 msg_Dbg( p_this, "could not raise process priority" );
156 if( config_GetInt( p_this, "one-instance" )
157 || ( config_GetInt( p_this, "one-instance-when-started-from-file" )
158 && config_GetInt( p_this, "started-from-file" ) ) )
162 msg_Info( p_this, "one instance mode ENABLED");
164 /* Use a named mutex to check if another instance is already running */
165 if( !( hmutex = CreateMutex( 0, TRUE, L"VLC ipc "VERSION ) ) )
167 /* Failed for some reason. Just ignore the option and go on as
169 msg_Err( p_this, "one instance mode DISABLED "
170 "(mutex couldn't be created)" );
174 if( GetLastError() != ERROR_ALREADY_EXISTS )
176 /* We are the 1st instance. */
177 static const char typename[] = "ipc helper";
179 vlc_custom_create( p_this, sizeof(vlc_object_t),
180 VLC_OBJECT_GENERIC, typename );
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 vlc_object_attach (p_helper, p_this);
196 CloseHandle( hIPCHelperReady );
198 /* Initialization done.
199 * Release the mutex to unblock other instances */
200 ReleaseMutex( hmutex );
204 /* Another instance is running */
208 /* Wait until the 1st instance is initialized */
209 WaitForSingleObject( hmutex, INFINITE );
211 /* Locate the window created by the IPC helper thread of the
213 if( !( ipcwindow = FindWindow( 0, L"VLC ipc "VERSION ) ) )
215 msg_Err( p_this, "one instance mode DISABLED "
216 "(couldn't find 1st instance of program)" );
217 ReleaseMutex( hmutex );
221 /* We assume that the remaining parameters are filenames
222 * and their input options */
223 if( *pi_argc - 1 >= optind )
225 COPYDATASTRUCT wm_data;
227 vlc_ipc_data_t *p_data;
228 size_t i_data = sizeof (*p_data);
230 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
232 i_data += sizeof (size_t);
233 i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
236 p_data = malloc( i_data );
237 p_data->argc = *pi_argc - optind;
238 p_data->enqueue = config_GetInt( p_this, "playlist-enqueue" );
240 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
242 size_t i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
243 /* Windows will never switch to an architecture
244 * with stronger alignment requirements, right. */
245 *((size_t *)(p_data->data + i_data)) = i_len;
246 i_data += sizeof (size_t);
247 memcpy( &p_data->data[i_data], ppsz_argv[ i_opt ], i_len );
250 i_data += sizeof (*p_data);
252 /* Send our playlist items to the 1st instance */
254 wm_data.cbData = i_data;
255 wm_data.lpData = p_data;
256 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
259 /* Initialization done.
260 * Release the mutex to unblock other instances */
261 ReleaseMutex( hmutex );
264 system_End( p_this );
272 static unsigned __stdcall IPCHelperThread( void *data )
274 vlc_object_t *p_this = data;
279 CreateWindow( L"STATIC", /* name of window class */
280 L"VLC ipc "VERSION, /* window title bar text */
281 0, /* window style */
282 0, /* default X coordinate */
283 0, /* default Y coordinate */
284 0, /* window width */
285 0, /* window height */
286 NULL, /* no parent window */
287 NULL, /* no menu in this window */
288 GetModuleHandle(NULL), /* handle of this program instance */
289 NULL ); /* sent to WM_CREATE */
291 SetWindowLongPtr( ipcwindow, GWLP_WNDPROC, (LRESULT)WMCOPYWNDPROC );
292 SetWindowLongPtr( ipcwindow, GWLP_USERDATA, (LONG_PTR)p_this );
294 /* Signal the creation of the thread and events queue */
295 SetEvent( hIPCHelperReady );
297 while( GetMessage( &message, NULL, 0, 0 ) )
299 TranslateMessage( &message );
300 DispatchMessage( &message );
305 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
308 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_Hold( p_this );
321 if( !p_playlist ) return 0;
323 if( pwm_data->lpData )
326 vlc_ipc_data_t *p_data = (vlc_ipc_data_t *)pwm_data->lpData;
328 int i_argc = p_data->argc, i_opt, i_options;
330 ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
331 for( i_opt = 0; i_opt < i_argc; i_opt++ )
333 ppsz_argv[i_opt] = p_data->data + i_data + sizeof(int);
334 i_data += sizeof(int) + *((int *)(p_data->data + i_data));
337 for( i_opt = 0; i_opt < i_argc; i_opt++ )
341 /* Count the input options */
342 while( i_opt + i_options + 1 < i_argc &&
343 *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
347 playlist_AddExt( p_playlist, ppsz_argv[i_opt],
348 NULL, PLAYLIST_APPEND |
349 ( ( i_opt || p_data->enqueue ) ? 0 : PLAYLIST_GO ),
351 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
352 i_options, true, pl_Unlocked );
360 vlc_object_release( p_playlist );
363 return DefWindowProc( hwnd, uMsg, wParam, lParam );
366 /*****************************************************************************
367 * system_End: terminate winsock.
368 *****************************************************************************/
369 void system_End( libvlc_int_t *p_this )
372 if( p_this && vlc_global() )
378 if( ipcwindow = FindWindow( 0, L"VLC ipc "VERSION ) )
380 SendMessage( ipcwindow, WM_QUIT, 0, 0 );
383 if (p_helper && p_helper->p_parent == VLC_OBJECT(p_this) )
385 /* FIXME: thread-safety... */
386 vlc_object_detach (p_helper);
387 vlc_object_release (p_helper);
391 #if !defined( UNDER_CE )