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 *****************************************************************************/
24 #include <string.h> /* strdup() */
25 #include <stdlib.h> /* free() */
28 #include "../libvlc.h"
29 #include <vlc_playlist.h>
30 #include <vlc_charset.h>
32 #ifdef WIN32 /* optind, getopt(), included in unistd.h */
33 # include "../extras/getopt.h"
36 #if !defined( UNDER_CE )
43 /*****************************************************************************
44 * system_Init: initialize winsock and misc other things.
45 *****************************************************************************/
46 void system_Init( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
50 /* Get our full path */
51 char psz_path[MAX_PATH];
54 #if defined( UNDER_CE )
55 wchar_t psz_wpath[MAX_PATH];
56 if( GetModuleFileName( NULL, psz_wpath, MAX_PATH ) )
58 WideCharToMultiByte( CP_ACP, 0, psz_wpath, -1,
59 psz_path, MAX_PATH, NULL, NULL );
61 else psz_path[0] = '\0';
66 GetFullPathName( ppsz_argv[0], MAX_PATH, psz_path, &psz_vlc );
68 else if( !GetModuleFileName( NULL, psz_path, MAX_PATH ) )
74 if( (psz_vlc = strrchr( psz_path, '\\' )) ) *psz_vlc = '\0';
76 vlc_global( p_this )->psz_vlcpath = strdup( psz_path );
78 /* Set the default file-translation mode */
79 #if !defined( UNDER_CE )
81 _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
84 /* Call mdate() once to make sure it is initialized properly */
87 /* WinSock Library Init. */
88 if( !WSAStartup( MAKEWORD( 2, 2 ), &Data ) )
90 /* Aah, pretty useless check, we should always have Winsock 2.2
91 * since it appeared in Win98. */
92 if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 2 )
93 /* We could not find a suitable WinSock DLL. */
96 /* Everything went ok. */
100 /* Let's try with WinSock 1.1 */
101 if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
103 /* Confirm that the WinSock DLL supports 1.1.*/
104 if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
105 /* We could not find a suitable WinSock DLL. */
108 /* Everything went ok. */
112 fprintf( stderr, "error: can't initialize WinSocks\n" );
115 /*****************************************************************************
116 * system_Configure: check for system specific configuration options.
117 *****************************************************************************/
118 static void IPCHelperThread( vlc_object_t * );
119 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
121 void system_Configure( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
123 #if !defined( UNDER_CE )
124 p_this->p_libvlc_global->b_fast_mutex = config_GetInt( p_this, "fast-mutex" );
125 p_this->p_libvlc_global->i_win9x_cv = config_GetInt( p_this, "win9x-cv-method" );
127 /* Raise default priority of the current process */
128 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
129 # define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
131 if( config_GetInt( p_this, "high-priority" ) )
133 if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
134 || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
136 msg_Dbg( p_this, "raised process priority" );
140 msg_Dbg( p_this, "could not raise process priority" );
144 if( config_GetInt( p_this, "one-instance" )
145 || ( config_GetInt( p_this, "one-instance-when-started-from-file" )
146 && config_GetInt( p_this, "started-from-file" ) ) )
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( 0, TRUE, _T("VLC ipc ") _T(VERSION) ) ) )
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( 0, _T("VLC ipc ") _T(VERSION) ) ) )
194 msg_Err( p_this, "one instance mode DISABLED "
195 "(couldn't find 1st instance of program)" );
196 ReleaseMutex( hmutex );
200 /* We assume that the remaining parameters are filenames
201 * and their input options */
202 if( *pi_argc - 1 >= optind )
204 COPYDATASTRUCT wm_data;
208 i_data = sizeof(int);
209 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
211 i_data += sizeof(int);
212 i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
215 p_data = (char *)malloc( i_data );
216 *((int *)&p_data[0]) = *pi_argc - optind;
217 i_data = sizeof(int);
218 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
220 int i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
221 *((int *)&p_data[i_data]) = i_len;
222 i_data += sizeof(int);
223 memcpy( &p_data[i_data], ppsz_argv[ i_opt ], i_len );
227 /* Send our playlist items to the 1st instance */
229 wm_data.cbData = i_data;
230 wm_data.lpData = p_data;
231 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
234 /* Initialization done.
235 * Release the mutex to unblock other instances */
236 ReleaseMutex( hmutex );
239 system_End( p_this );
247 static void IPCHelperThread( vlc_object_t *p_this )
253 CreateWindow( _T("STATIC"), /* name of window class */
254 _T("VLC ipc ") _T(VERSION), /* window title bar text */
255 0, /* window style */
256 0, /* default X coordinate */
257 0, /* default Y coordinate */
258 0, /* window width */
259 0, /* window height */
260 NULL, /* no parent window */
261 NULL, /* no menu in this window */
262 GetModuleHandle(NULL), /* handle of this program instance */
263 NULL ); /* sent to WM_CREATE */
265 SetWindowLong( ipcwindow, GWL_WNDPROC, (LONG)WMCOPYWNDPROC );
266 SetWindowLong( ipcwindow, GWL_USERDATA, (LONG)p_this );
268 /* Signal the creation of the thread and events queue */
269 vlc_thread_ready( p_this );
271 while( GetMessage( &message, NULL, 0, 0 ) )
273 TranslateMessage( &message );
274 DispatchMessage( &message );
278 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
281 if( uMsg == WM_COPYDATA )
283 COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
284 vlc_object_t *p_this;
285 playlist_t *p_playlist;
287 p_this = (vlc_object_t *)GetWindowLong( hwnd, GWL_USERDATA );
289 if( !p_this ) return 0;
291 /* Add files to the playlist */
292 p_playlist = (playlist_t *)vlc_object_find( p_this,
295 if( !p_playlist ) return 0;
297 if( pwm_data->lpData )
299 int i_argc, i_data, i_opt, i_options;
301 char *p_data = (char *)pwm_data->lpData;
303 i_argc = *((int *)&p_data[0]);
304 ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
305 i_data = sizeof(int);
306 for( i_opt = 0; i_opt < i_argc; i_opt++ )
308 ppsz_argv[i_opt] = &p_data[i_data + sizeof(int)];
309 i_data += *((int *)&p_data[i_data]);
310 i_data += sizeof(int);
313 for( i_opt = 0; i_opt < i_argc; i_opt++ )
317 /* Count the input options */
318 while( i_opt + i_options + 1 < i_argc &&
319 *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
323 if( i_opt || config_GetInt( p_this, "playlist-enqueue" ) )
325 playlist_AddExt( p_playlist, ppsz_argv[i_opt],
326 NULL, PLAYLIST_APPEND ,
328 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
329 i_options, VLC_TRUE, VLC_FALSE );
331 playlist_AddExt( p_playlist, ppsz_argv[i_opt],
332 NULL, PLAYLIST_APPEND | PLAYLIST_GO,
334 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
335 i_options, VLC_TRUE, VLC_FALSE );
344 vlc_object_release( p_playlist );
347 return DefWindowProc( hwnd, uMsg, wParam, lParam );
350 /*****************************************************************************
351 * system_End: terminate winsock.
352 *****************************************************************************/
353 void system_End( libvlc_int_t *p_this )
355 if( p_this && p_this->p_libvlc_global && vlc_global( p_this )->psz_vlcpath )
357 free( vlc_global( p_this )->psz_vlcpath );
358 vlc_global( p_this )->psz_vlcpath = NULL;