1 /*****************************************************************************
2 * win32_specific.c: Win32 specific features
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
5 * $Id: win32_specific.c,v 1.34 2004/01/29 19:38:17 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 /* Call mdate() once to make sure it is initialized properly */
78 /* WinSock Library Init. */
79 if( !WSAStartup( MAKEWORD( 2, 0 ), &Data ) )
81 /* Confirm that the WinSock DLL supports 2.0.*/
82 if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 0 )
84 /* We could not find a suitable WinSock DLL. */
89 /* Everything went ok. */
94 /* Let's try with WinSock 1.1 */
95 if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
97 /* Confirm that the WinSock DLL supports 1.1.*/
98 if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
100 /* We could not find a suitable WinSock DLL. */
105 /* Everything went ok. */
110 fprintf( stderr, "error: can't initialize WinSocks\n" );
117 /*****************************************************************************
118 * system_Configure: check for system specific configuration options.
119 *****************************************************************************/
120 static void IPCHelperThread( vlc_object_t * );
121 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
123 void system_Configure( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
125 #if !defined( UNDER_CE )
126 p_this->p_libvlc->b_fast_mutex = config_GetInt( p_this, "fast-mutex" );
127 p_this->p_libvlc->i_win9x_cv = config_GetInt( p_this, "win9x-cv-method" );
129 /* Raise default priority of the current process */
130 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
131 # define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
133 if( config_GetInt( p_this, "high-priority" ) )
135 if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
136 || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
138 msg_Dbg( p_this, "raised process priority" );
142 msg_Dbg( p_this, "could not raise process priority" );
146 if( config_GetInt( p_this, "one-instance" ) )
150 msg_Info( p_this, "one instance mode ENABLED");
152 /* Use a named mutex to check if another instance is already running */
153 if( ( hmutex = CreateMutex( NULL, TRUE, "VLC ipc "VERSION ) ) == NULL )
155 /* Failed for some reason. Just ignore the option and go on as
157 msg_Err( p_this, "one instance mode DISABLED "
158 "(mutex couldn't be created)" );
162 if( GetLastError() != ERROR_ALREADY_EXISTS )
164 /* We are the 1st instance. */
165 vlc_object_t *p_helper =
166 (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
168 /* Run the helper thread */
169 if( vlc_thread_create( p_helper, "IPC helper", IPCHelperThread,
170 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
172 msg_Err( p_this, "one instance mode DISABLED "
173 "(IPC helper thread couldn't be created)" );
177 /* Initialization done.
178 * Release the mutex to unblock other instances */
179 ReleaseMutex( hmutex );
183 /* Another instance is running */
187 /* Wait until the 1st instance is initialized */
188 WaitForSingleObject( hmutex, INFINITE );
190 /* Locate the window created by the IPC helper thread of the
192 if( ( ipcwindow = FindWindow( NULL, "VLC ipc "VERSION ) )
195 msg_Err( p_this, "one instance mode DISABLED "
196 "(couldn't find 1st instance of program)" );
197 ReleaseMutex( hmutex );
201 /* We assume that the remaining parameters are filenames
202 * and their input options */
203 if( *pi_argc - 1 >= optind )
205 COPYDATASTRUCT wm_data;
209 i_data = sizeof(int);
210 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
212 i_data += sizeof(int);
213 i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
216 p_data = (char *)malloc( i_data );
217 *((int *)&p_data[0]) = *pi_argc - optind;
218 i_data = sizeof(int);
219 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
221 int i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
222 *((int *)&p_data[i_data]) = i_len;
223 i_data += sizeof(int);
224 memcpy( &p_data[i_data], ppsz_argv[ i_opt ], i_len );
228 /* Send our playlist items to the 1st instance */
230 wm_data.cbData = i_data;
231 wm_data.lpData = p_data;
232 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
235 /* Initialization done.
236 * Release the mutex to unblock other instances */
237 ReleaseMutex( hmutex );
240 system_End( p_this );
248 static void IPCHelperThread( vlc_object_t *p_this )
254 CreateWindow( "STATIC", /* name of window class */
255 "VLC ipc "VERSION, /* window title bar text */
256 0, /* window style */
257 0, /* default X coordinate */
258 0, /* default Y coordinate */
259 0, /* window width */
260 0, /* window height */
261 NULL, /* no parent window */
262 NULL, /* no menu in this window */
263 GetModuleHandle(NULL), /* handle of this program instance */
264 NULL ); /* sent to WM_CREATE */
266 SetWindowLong( ipcwindow, GWL_WNDPROC, (LONG)WMCOPYWNDPROC );
267 SetWindowLong( ipcwindow, GWL_USERDATA, (LONG)p_this );
269 /* Signal the creation of the thread and events queue */
270 vlc_thread_ready( p_this );
272 while( GetMessage( &message, NULL, 0, 0 ) )
274 TranslateMessage( &message );
275 DispatchMessage( &message );
279 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
282 if( uMsg == WM_COPYDATA )
284 COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
285 vlc_object_t *p_this;
286 playlist_t *p_playlist;
288 p_this = (vlc_object_t *)GetWindowLong( hwnd, GWL_USERDATA );
290 if( !p_this ) return 0;
292 /* Add files to the playlist */
293 p_playlist = (playlist_t *)vlc_object_find( p_this,
296 if( !p_playlist ) return 0;
298 if( pwm_data->lpData )
300 int i_argc, i_data, i_opt, i_options;
302 char *p_data = (char *)pwm_data->lpData;
304 i_argc = *((int *)&p_data[0]);
305 ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
306 i_data = sizeof(int);
307 for( i_opt = 0; i_opt < i_argc; i_opt++ )
309 ppsz_argv[i_opt] = &p_data[i_data + sizeof(int)];
310 i_data += *((int *)&p_data[i_data]);
311 i_data += sizeof(int);
314 for( i_opt = 0; i_opt < i_argc; i_opt++ )
318 /* Count the input options */
319 while( i_opt + i_options + 1 < i_argc &&
320 *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
325 playlist_AddExt( p_playlist, ppsz_argv[i_opt],ppsz_argv[i_opt],
326 PLAYLIST_APPEND | (i_opt? 0 : PLAYLIST_GO),
328 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
337 vlc_object_release( p_playlist );
340 return DefWindowProc( hwnd, uMsg, wParam, lParam );
343 /*****************************************************************************
344 * system_End: terminate winsock.
345 *****************************************************************************/
346 void system_End( vlc_t *p_this )
348 #if !defined( UNDER_CE )