1 /*****************************************************************************
2 * win32_specific.c: Win32 specific features
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: win32_specific.c,v 1.27 2003/12/02 12:57:36 gbazin Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
24 #include <string.h> /* strdup() */
25 #include <stdlib.h> /* free() */
28 #include "vlc_playlist.h"
30 #ifdef WIN32 /* optind, getopt(), included in unistd.h */
31 # include "../extras/getopt.h"
34 #if !defined( UNDER_CE )
37 # include <winsock2.h>
40 /*****************************************************************************
41 * system_Init: initialize winsock and misc other things.
42 *****************************************************************************/
43 void system_Init( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
45 #if !defined( UNDER_CE )
48 /* Get our full path */
51 char psz_path[MAX_PATH];
54 GetFullPathName( ppsz_argv[0], MAX_PATH, psz_path, &psz_vlc );
56 if( psz_vlc > psz_path && psz_vlc[-1] == '\\' )
59 p_this->p_libvlc->psz_vlcpath = strdup( psz_path );
63 p_this->p_libvlc->psz_vlcpath = strdup( "" );
68 p_this->p_libvlc->psz_vlcpath = strdup( "" );
71 /* Set the default file-translation mode */
73 _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
75 /* WinSock Library Init. */
76 if( !WSAStartup( MAKEWORD( 2, 0 ), &Data ) )
78 /* Confirm that the WinSock DLL supports 2.0.*/
79 if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 0 )
81 /* We could not find a suitable WinSock DLL. */
86 /* Everything went ok. */
91 /* Let's try with WinSock 1.1 */
92 if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
94 /* Confirm that the WinSock DLL supports 1.1.*/
95 if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
97 /* We could not find a suitable WinSock DLL. */
102 /* Everything went ok. */
107 fprintf( stderr, "error: can't initialize WinSocks\n" );
114 /*****************************************************************************
115 * system_Configure: check for system specific configuration options.
116 *****************************************************************************/
117 static void IPCHelperThread( vlc_object_t * );
118 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
120 void system_Configure( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
122 #if !defined( UNDER_CE )
123 p_this->p_libvlc->b_fast_mutex = config_GetInt( p_this, "fast-mutex" );
124 p_this->p_libvlc->i_win9x_cv = config_GetInt( p_this, "win9x-cv-method" );
126 /* Raise default priority of the current process */
127 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
128 # define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
130 if( config_GetInt( p_this, "high-priority" ) )
132 if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
133 || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
135 msg_Dbg( p_this, "raised process priority" );
139 msg_Dbg( p_this, "could not raise process priority" );
143 if( config_GetInt( p_this, "one-instance" ) )
147 msg_Info( p_this, "One instance mode ENABLED");
149 /* Use a named mutex to check if another instance is already running */
150 if( ( hmutex = CreateMutex( NULL, TRUE, "VLC ipc "VERSION ) ) == NULL )
152 /* Failed for some reason. Just ignore the option and go on as
154 msg_Err( p_this, "One instance mode DISABLED "
155 "(mutex couldn't be created)" );
159 if( GetLastError() != ERROR_ALREADY_EXISTS )
161 /* We are the 1st instance. */
162 vlc_object_t *p_helper =
163 (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
165 /* Run the helper thread */
166 if( vlc_thread_create( p_helper, "IPC helper", IPCHelperThread,
167 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
169 msg_Err( p_this, "One instance mode DISABLED "
170 "(IPC helper thread couldn't be created)" );
174 /* Initialization done.
175 * Release the mutex to unblock other instances */
176 ReleaseMutex( hmutex );
180 /* Another instance is running */
184 /* Wait until the 1st instance is initialized */
185 WaitForSingleObject( hmutex, INFINITE );
187 /* Locate the window created by the IPC helper thread of the
189 if( ( ipcwindow = FindWindow( NULL, "VLC ipc "VERSION ) )
192 msg_Err( p_this, "One instance mode DISABLED "
193 "(couldn't find 1st instance of program)" );
194 ReleaseMutex( hmutex );
198 /* We assume that the remaining parameters are filenames
199 * and their input options */
200 if( *pi_argc - 1 >= optind )
202 COPYDATASTRUCT wm_data;
206 i_data = sizeof(int);
207 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
209 i_data += sizeof(int);
210 i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
213 p_data = (char *)malloc( i_data );
214 *((int *)&p_data[0]) = *pi_argc - optind;
215 i_data = sizeof(int);
216 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
218 int i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
219 *((int *)&p_data[i_data]) = i_len;
220 i_data += sizeof(int);
221 memcpy( &p_data[i_data], ppsz_argv[ i_opt ], i_len );
225 /* Send our playlist items to the 1st instance */
227 wm_data.cbData = i_data;
228 wm_data.lpData = p_data;
229 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
232 /* Initialization done.
233 * Release the mutex to unblock other instances */
234 ReleaseMutex( hmutex );
237 system_End( p_this );
245 static void IPCHelperThread( vlc_object_t *p_this )
251 CreateWindow( "STATIC", /* name of window class */
252 "VLC ipc "VERSION, /* window title bar text */
253 0, /* window style */
254 0, /* default X coordinate */
255 0, /* default Y coordinate */
256 0, /* window width */
257 0, /* window height */
258 NULL, /* no parent window */
259 NULL, /* no menu in this window */
260 GetModuleHandle(NULL), /* handle of this program instance */
261 NULL ); /* sent to WM_CREATE */
263 SetWindowLong( ipcwindow, GWL_WNDPROC, (LONG)WMCOPYWNDPROC );
264 SetWindowLong( ipcwindow, GWL_USERDATA, (LONG)p_this );
266 /* Signal the creation of the thread and events queue */
267 vlc_thread_ready( p_this );
269 while( GetMessage( &message, NULL, 0, 0 ) )
271 TranslateMessage( &message );
272 DispatchMessage( &message );
276 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
279 if( uMsg == WM_COPYDATA )
281 COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
282 vlc_object_t *p_this;
283 playlist_t *p_playlist;
285 p_this = (vlc_object_t *)GetWindowLong( hwnd, GWL_USERDATA );
287 if( !p_this ) return 0;
289 /* Add files to the playlist */
290 p_playlist = (playlist_t *)vlc_object_find( p_this,
293 if( !p_playlist ) return 0;
295 if( pwm_data->lpData )
297 int i_argc, i_data, i_opt, i_options;
299 char *p_data = (char *)pwm_data->lpData;
301 i_argc = *((int *)&p_data[0]);
302 ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
303 i_data = sizeof(int);
304 for( i_opt = 0; i_opt < i_argc; i_opt++ )
306 ppsz_argv[i_opt] = &p_data[i_data + sizeof(int)];
307 i_data += *((int *)&p_data[i_data]);
308 i_data += sizeof(int);
311 for( i_opt = 0; i_opt < i_argc; i_opt++ )
315 /* Count the input options */
316 while( i_opt + i_options + 1 < i_argc &&
317 *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
322 playlist_Add( p_playlist, ppsz_argv[ i_opt ],
323 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
324 i_options, PLAYLIST_APPEND | (i_opt? 0 : PLAYLIST_GO),
333 vlc_object_release( p_playlist );
336 return DefWindowProc( hwnd, uMsg, wParam, lParam );
339 /*****************************************************************************
340 * system_End: terminate winsock.
341 *****************************************************************************/
342 void system_End( vlc_t *p_this )
344 #if !defined( UNDER_CE )