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 *****************************************************************************/
26 #include "../libvlc.h"
27 #include <vlc_playlist.h>
28 #include <vlc_charset.h>
30 #ifdef WIN32 /* optind, getopt(), included in unistd.h */
31 # include "../extras/getopt.h"
34 #if !defined( UNDER_CE )
41 /*****************************************************************************
42 * system_Init: initialize winsock and misc other things.
43 *****************************************************************************/
44 void system_Init( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
48 /* Get our full path */
49 char psz_path[MAX_PATH];
52 #if defined( UNDER_CE )
53 wchar_t psz_wpath[MAX_PATH];
54 if( GetModuleFileName( NULL, psz_wpath, MAX_PATH ) )
56 WideCharToMultiByte( CP_ACP, 0, psz_wpath, -1,
57 psz_path, MAX_PATH, NULL, NULL );
59 else psz_path[0] = '\0';
64 GetFullPathName( ppsz_argv[0], MAX_PATH, psz_path, &psz_vlc );
66 else if( !GetModuleFileName( NULL, psz_path, MAX_PATH ) )
72 if( (psz_vlc = strrchr( psz_path, '\\' )) ) *psz_vlc = '\0';
76 /* remove trailing \.libs from executable dir path if seen,
77 we assume we are running vlc through libtool wrapper in build dir */
78 int offset = strlen(psz_path)-sizeof("\\.libs")+1;
81 psz_vlc = psz_path+offset;
82 if( ! strcmp(psz_vlc, "\\.libs") ) *psz_vlc = '\0';
87 vlc_global()->psz_vlcpath = strdup( psz_path );
89 /* Set the default file-translation mode */
90 #if !defined( UNDER_CE )
92 _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
95 /* Call mdate() once to make sure it is initialized properly */
98 /* WinSock Library Init. */
99 if( !WSAStartup( MAKEWORD( 2, 2 ), &Data ) )
101 /* Aah, pretty useless check, we should always have Winsock 2.2
102 * since it appeared in Win98. */
103 if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 2 )
104 /* We could not find a suitable WinSock DLL. */
107 /* Everything went ok. */
111 /* Let's try with WinSock 1.1 */
112 if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
114 /* Confirm that the WinSock DLL supports 1.1.*/
115 if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
116 /* We could not find a suitable WinSock DLL. */
119 /* Everything went ok. */
123 fprintf( stderr, "error: can't initialize WinSocks\n" );
126 /*****************************************************************************
127 * system_Configure: check for system specific configuration options.
128 *****************************************************************************/
129 static void IPCHelperThread( vlc_object_t * );
130 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
132 void system_Configure( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
134 #if !defined( UNDER_CE )
135 /* Raise default priority of the current process */
136 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
137 # define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
139 if( config_GetInt( p_this, "high-priority" ) )
141 if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
142 || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
144 msg_Dbg( p_this, "raised process priority" );
148 msg_Dbg( p_this, "could not raise process priority" );
152 if( config_GetInt( p_this, "one-instance" )
153 || ( config_GetInt( p_this, "one-instance-when-started-from-file" )
154 && config_GetInt( p_this, "started-from-file" ) ) )
158 msg_Info( p_this, "one instance mode ENABLED");
160 /* Use a named mutex to check if another instance is already running */
161 if( !( hmutex = CreateMutex( 0, TRUE, _T("VLC ipc ") _T(VERSION) ) ) )
163 /* Failed for some reason. Just ignore the option and go on as
165 msg_Err( p_this, "one instance mode DISABLED "
166 "(mutex couldn't be created)" );
170 if( GetLastError() != ERROR_ALREADY_EXISTS )
172 /* We are the 1st instance. */
173 vlc_object_t *p_helper =
174 (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
176 /* Run the helper thread */
177 if( vlc_thread_create( p_helper, "IPC helper", IPCHelperThread,
178 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
180 msg_Err( p_this, "one instance mode DISABLED "
181 "(IPC helper thread couldn't be created)" );
185 /* Initialization done.
186 * Release the mutex to unblock other instances */
187 ReleaseMutex( hmutex );
191 /* Another instance is running */
195 /* Wait until the 1st instance is initialized */
196 WaitForSingleObject( hmutex, INFINITE );
198 /* Locate the window created by the IPC helper thread of the
200 if( !( ipcwindow = FindWindow( 0, _T("VLC ipc ") _T(VERSION) ) ) )
202 msg_Err( p_this, "one instance mode DISABLED "
203 "(couldn't find 1st instance of program)" );
204 ReleaseMutex( hmutex );
208 /* We assume that the remaining parameters are filenames
209 * and their input options */
210 if( *pi_argc - 1 >= optind )
212 COPYDATASTRUCT wm_data;
216 i_data = sizeof(int);
217 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
219 i_data += sizeof(int);
220 i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
223 p_data = (char *)malloc( i_data );
224 *((int *)&p_data[0]) = *pi_argc - optind;
225 i_data = sizeof(int);
226 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
228 int i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
229 *((int *)&p_data[i_data]) = i_len;
230 i_data += sizeof(int);
231 memcpy( &p_data[i_data], ppsz_argv[ i_opt ], i_len );
235 /* Send our playlist items to the 1st instance */
237 wm_data.cbData = i_data;
238 wm_data.lpData = p_data;
239 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
242 /* Initialization done.
243 * Release the mutex to unblock other instances */
244 ReleaseMutex( hmutex );
247 system_End( p_this );
255 static void IPCHelperThread( vlc_object_t *p_this )
261 CreateWindow( _T("STATIC"), /* name of window class */
262 _T("VLC ipc ") _T(VERSION), /* window title bar text */
263 0, /* window style */
264 0, /* default X coordinate */
265 0, /* default Y coordinate */
266 0, /* window width */
267 0, /* window height */
268 NULL, /* no parent window */
269 NULL, /* no menu in this window */
270 GetModuleHandle(NULL), /* handle of this program instance */
271 NULL ); /* sent to WM_CREATE */
273 SetWindowLong( ipcwindow, GWL_WNDPROC, (LONG)WMCOPYWNDPROC );
274 SetWindowLong( ipcwindow, GWL_USERDATA, (LONG)p_this );
276 /* Signal the creation of the thread and events queue */
277 vlc_thread_ready( p_this );
279 while( GetMessage( &message, NULL, 0, 0 ) )
281 TranslateMessage( &message );
282 DispatchMessage( &message );
286 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
289 if( uMsg == WM_COPYDATA )
291 COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
292 vlc_object_t *p_this;
293 playlist_t *p_playlist;
295 p_this = (vlc_object_t *)GetWindowLong( hwnd, GWL_USERDATA );
297 if( !p_this ) return 0;
299 /* Add files to the playlist */
300 p_playlist = (playlist_t *)vlc_object_find( p_this,
303 if( !p_playlist ) return 0;
305 if( pwm_data->lpData )
307 int i_argc, i_data, i_opt, i_options;
309 char *p_data = (char *)pwm_data->lpData;
311 i_argc = *((int *)&p_data[0]);
312 ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
313 i_data = sizeof(int);
314 for( i_opt = 0; i_opt < i_argc; i_opt++ )
316 ppsz_argv[i_opt] = &p_data[i_data + sizeof(int)];
317 i_data += *((int *)&p_data[i_data]);
318 i_data += sizeof(int);
321 for( i_opt = 0; i_opt < i_argc; i_opt++ )
325 /* Count the input options */
326 while( i_opt + i_options + 1 < i_argc &&
327 *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
331 if( i_opt || config_GetInt( p_this, "playlist-enqueue" ) )
333 playlist_AddExt( p_playlist, ppsz_argv[i_opt],
334 NULL, PLAYLIST_APPEND ,
336 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
337 i_options, VLC_TRUE, VLC_FALSE );
339 playlist_AddExt( p_playlist, ppsz_argv[i_opt],
340 NULL, PLAYLIST_APPEND | PLAYLIST_GO,
342 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
343 i_options, VLC_TRUE, VLC_FALSE );
352 vlc_object_release( p_playlist );
355 return DefWindowProc( hwnd, uMsg, wParam, lParam );
358 /*****************************************************************************
359 * system_End: terminate winsock.
360 *****************************************************************************/
361 void system_End( libvlc_int_t *p_this )
363 if( p_this && vlc_global() )
365 free( vlc_global()->psz_vlcpath );
366 vlc_global()->psz_vlcpath = NULL;