1 /*****************************************************************************
2 * win32_specific.c: Win32 specific features
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: win32_specific.c,v 1.26 2003/10/03 13:35:56 sam 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 )
36 # include <winsock2.h>
39 /*****************************************************************************
40 * system_Init: initialize winsock and misc other things.
41 *****************************************************************************/
42 void system_Init( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
44 #if !defined( UNDER_CE )
47 /* Get our full path */
50 char psz_path[MAX_PATH];
53 GetFullPathName( ppsz_argv[0], MAX_PATH, psz_path, &psz_vlc );
55 if( psz_vlc > psz_path && psz_vlc[-1] == '\\' )
58 p_this->p_libvlc->psz_vlcpath = strdup( psz_path );
62 p_this->p_libvlc->psz_vlcpath = strdup( "" );
67 p_this->p_libvlc->psz_vlcpath = strdup( "" );
70 /* Set the default file-translation mode */
72 _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
74 /* WinSock Library Init. */
75 if( !WSAStartup( MAKEWORD( 2, 0 ), &Data ) )
77 /* Confirm that the WinSock DLL supports 2.0.*/
78 if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 0 )
80 /* We could not find a suitable WinSock DLL. */
85 /* Everything went ok. */
90 /* Let's try with WinSock 1.1 */
91 if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
93 /* Confirm that the WinSock DLL supports 1.1.*/
94 if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
96 /* We could not find a suitable WinSock DLL. */
101 /* Everything went ok. */
106 fprintf( stderr, "error: can't initialize WinSocks\n" );
113 /*****************************************************************************
114 * system_Configure: check for system specific configuration options.
115 *****************************************************************************/
116 static void IPCHelperThread( vlc_object_t * );
117 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
119 void system_Configure( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
121 #if !defined( UNDER_CE )
122 p_this->p_libvlc->b_fast_mutex = config_GetInt( p_this, "fast-mutex" );
123 p_this->p_libvlc->i_win9x_cv = config_GetInt( p_this, "win9x-cv-method" );
125 /* Raise default priority of the current process */
126 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
127 # define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
129 if( config_GetInt( p_this, "high-priority" ) )
131 if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
132 || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
134 msg_Dbg( p_this, "raised process priority" );
138 msg_Dbg( p_this, "could not raise process priority" );
142 if( config_GetInt( p_this, "one-instance" ) )
146 msg_Info( p_this, "One instance mode ENABLED");
148 /* Use a named mutex to check if another instance is already running */
149 if( ( hmutex = CreateMutex( NULL, TRUE, "VLC ipc "VERSION ) ) == NULL )
151 /* Failed for some reason. Just ignore the option and go on as
153 msg_Err( p_this, "One instance mode DISABLED "
154 "(mutex couldn't be created)" );
158 if( GetLastError() != ERROR_ALREADY_EXISTS )
160 /* We are the 1st instance. */
161 vlc_object_t *p_helper =
162 (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
164 /* Run the helper thread */
165 if( vlc_thread_create( p_helper, "IPC helper", IPCHelperThread,
166 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
168 msg_Err( p_this, "One instance mode DISABLED "
169 "(IPC helper thread couldn't be created)" );
173 /* Initialization done.
174 * Release the mutex to unblock other instances */
175 ReleaseMutex( hmutex );
179 /* Another instance is running */
183 /* Wait until the 1st instance is initialized */
184 WaitForSingleObject( hmutex, INFINITE );
186 /* Locate the window created by the IPC helper thread of the
188 if( ( ipcwindow = FindWindow( NULL, "VLC ipc "VERSION ) )
191 msg_Err( p_this, "One instance mode DISABLED "
192 "(couldn't find 1st instance of program)" );
193 ReleaseMutex( hmutex );
197 /* We assume that the remaining parameters are filenames
198 * and their input options */
199 if( *pi_argc - 1 >= optind )
201 COPYDATASTRUCT wm_data;
205 i_data = sizeof(int);
206 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
208 i_data += sizeof(int);
209 i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
212 p_data = (char *)malloc( i_data );
213 *((int *)&p_data[0]) = *pi_argc - optind;
214 i_data = sizeof(int);
215 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
217 int i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
218 *((int *)&p_data[i_data]) = i_len;
219 i_data += sizeof(int);
220 memcpy( &p_data[i_data], ppsz_argv[ i_opt ], i_len );
224 /* Send our playlist items to the 1st instance */
226 wm_data.cbData = i_data;
227 wm_data.lpData = p_data;
228 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
231 /* Initialization done.
232 * Release the mutex to unblock other instances */
233 ReleaseMutex( hmutex );
236 system_End( p_this );
244 static void IPCHelperThread( vlc_object_t *p_this )
250 CreateWindow( "STATIC", /* name of window class */
251 "VLC ipc "VERSION, /* window title bar text */
252 0, /* window style */
253 0, /* default X coordinate */
254 0, /* default Y coordinate */
255 0, /* window width */
256 0, /* window height */
257 NULL, /* no parent window */
258 NULL, /* no menu in this window */
259 GetModuleHandle(NULL), /* handle of this program instance */
260 NULL ); /* sent to WM_CREATE */
262 SetWindowLong( ipcwindow, GWL_WNDPROC, (LONG)WMCOPYWNDPROC );
263 SetWindowLong( ipcwindow, GWL_USERDATA, (LONG)p_this );
265 /* Signal the creation of the thread and events queue */
266 vlc_thread_ready( p_this );
268 while( GetMessage( &message, NULL, 0, 0 ) )
270 TranslateMessage( &message );
271 DispatchMessage( &message );
275 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
278 if( uMsg == WM_COPYDATA )
280 COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
281 vlc_object_t *p_this;
282 playlist_t *p_playlist;
284 p_this = (vlc_object_t *)GetWindowLong( hwnd, GWL_USERDATA );
286 if( !p_this ) return 0;
288 /* Add files to the playlist */
289 p_playlist = (playlist_t *)vlc_object_find( p_this,
292 if( !p_playlist ) return 0;
294 if( pwm_data->lpData )
296 int i_argc, i_data, i_opt, i_options;
298 char *p_data = (char *)pwm_data->lpData;
300 i_argc = *((int *)&p_data[0]);
301 ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
302 i_data = sizeof(int);
303 for( i_opt = 0; i_opt < i_argc; i_opt++ )
305 ppsz_argv[i_opt] = &p_data[i_data + sizeof(int)];
306 i_data += *((int *)&p_data[i_data]);
307 i_data += sizeof(int);
310 for( i_opt = 0; i_opt < i_argc; i_opt++ )
314 /* Count the input options */
315 while( i_opt + i_options + 1 < i_argc &&
316 *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
321 playlist_Add( p_playlist, ppsz_argv[ i_opt ],
322 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
323 i_options, PLAYLIST_APPEND | (i_opt? 0 : PLAYLIST_GO),
332 vlc_object_release( p_playlist );
335 return DefWindowProc( hwnd, uMsg, wParam, lParam );
338 /*****************************************************************************
339 * system_End: terminate winsock.
340 *****************************************************************************/
341 void system_End( vlc_t *p_this )
343 #if !defined( UNDER_CE )