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 <vlc/input.h>
29 #include "vlc_playlist.h"
31 #ifdef WIN32 /* optind, getopt(), included in unistd.h */
32 # include "../extras/getopt.h"
35 #if !defined( UNDER_CE )
42 extern void __wgetmainargs(int *argc, wchar_t ***wargv, wchar_t ***wenviron,
43 int expand_wildcards, int *startupinfo);
45 /*****************************************************************************
46 * system_Init: initialize winsock and misc other things.
47 *****************************************************************************/
48 void system_Init( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
52 /* Get our full path */
53 char psz_path[MAX_PATH];
56 #if defined( UNDER_CE )
57 wchar_t psz_wpath[MAX_PATH];
58 if( GetModuleFileName( NULL, psz_wpath, MAX_PATH ) )
60 WideCharToMultiByte( CP_ACP, 0, psz_wpath, -1,
61 psz_path, MAX_PATH, NULL, NULL );
63 else psz_path[0] = '\0';
68 GetFullPathName( ppsz_argv[0], MAX_PATH, psz_path, &psz_vlc );
70 else if( !GetModuleFileName( NULL, psz_path, MAX_PATH ) )
76 if( (psz_vlc = strrchr( psz_path, '\\' )) ) *psz_vlc = '\0';
78 p_this->p_libvlc_global->psz_vlcpath = strdup( psz_path );
80 /* Set the default file-translation mode */
81 #if !defined( UNDER_CE )
83 _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
86 /* Call mdate() once to make sure it is initialized properly */
89 /* WinSock Library Init. */
90 if( !WSAStartup( MAKEWORD( 2, 2 ), &Data ) )
92 /* Aah, pretty useless check, we should always have Winsock 2.2
93 * since it appeared in Win98. */
94 if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 2 )
95 /* We could not find a suitable WinSock DLL. */
98 /* Everything went ok. */
102 /* Let's try with WinSock 1.1 */
103 if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
105 /* Confirm that the WinSock DLL supports 1.1.*/
106 if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
107 /* We could not find a suitable WinSock DLL. */
110 /* Everything went ok. */
114 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( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
125 #if !defined( UNDER_CE )
126 p_this->p_libvlc_global->b_fast_mutex = config_GetInt( p_this, "fast-mutex" );
127 p_this->p_libvlc_global->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" )
147 || ( config_GetInt( p_this, "one-instance-when-started-from-file" )
148 && config_GetInt( p_this, "started-from-file" ) ) )
152 msg_Info( p_this, "one instance mode ENABLED");
154 /* Use a named mutex to check if another instance is already running */
155 if( !( hmutex = CreateMutex( 0, TRUE, _T("VLC ipc ") _T(VERSION) ) ) )
157 /* Failed for some reason. Just ignore the option and go on as
159 msg_Err( p_this, "one instance mode DISABLED "
160 "(mutex couldn't be created)" );
164 if( GetLastError() != ERROR_ALREADY_EXISTS )
166 /* We are the 1st instance. */
167 vlc_object_t *p_helper =
168 (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
170 /* Run the helper thread */
171 if( vlc_thread_create( p_helper, "IPC helper", IPCHelperThread,
172 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
174 msg_Err( p_this, "one instance mode DISABLED "
175 "(IPC helper thread couldn't be created)" );
179 /* Initialization done.
180 * Release the mutex to unblock other instances */
181 ReleaseMutex( hmutex );
185 /* Another instance is running */
189 /* Wait until the 1st instance is initialized */
190 WaitForSingleObject( hmutex, INFINITE );
192 /* Locate the window created by the IPC helper thread of the
194 if( !( ipcwindow = FindWindow( 0, _T("VLC ipc ") _T(VERSION) ) ) )
196 msg_Err( p_this, "one instance mode DISABLED "
197 "(couldn't find 1st instance of program)" );
198 ReleaseMutex( hmutex );
202 /* We assume that the remaining parameters are filenames
203 * and their input options */
204 if( *pi_argc - 1 >= optind )
206 COPYDATASTRUCT wm_data;
209 wchar_t **wargv, **wenvp;
212 i_data = sizeof(int);
213 if( GetVersion() < 0x80000000 )
215 /* use unicode argv[] for Windows NT and above */
216 __wgetmainargs(&i_opt, &wargv, &wenvp, 0, &si);
217 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
219 i_data += sizeof(int);
220 i_data += WideCharToMultiByte( CP_UTF8, 0,
221 wargv[ i_opt ], -1, NULL, 0, NULL, NULL ) + 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 = WideCharToMultiByte( CP_UTF8, 0,
229 wargv[ i_opt ], -1, NULL, 0, NULL, NULL ) + 1;
230 *((int *)&p_data[i_data]) = i_len;
231 i_data += sizeof(int);
232 WideCharToMultiByte( CP_UTF8, 0, wargv[ i_opt ], -1,
233 &p_data[i_data], i_len, NULL, NULL );
240 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
242 i_data += sizeof(int);
243 i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
246 p_data = (char *)malloc( i_data );
247 *((int *)&p_data[0]) = *pi_argc - optind;
248 i_data = sizeof(int);
249 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
251 int i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
252 *((int *)&p_data[i_data]) = i_len;
253 i_data += sizeof(int);
254 memcpy( &p_data[i_data], ppsz_argv[ i_opt ], i_len );
259 /* Send our playlist items to the 1st instance */
261 wm_data.cbData = i_data;
262 wm_data.lpData = p_data;
263 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
266 /* Initialization done.
267 * Release the mutex to unblock other instances */
268 ReleaseMutex( hmutex );
271 system_End( p_this );
279 static void IPCHelperThread( vlc_object_t *p_this )
285 CreateWindow( _T("STATIC"), /* name of window class */
286 _T("VLC ipc ") _T(VERSION), /* window title bar text */
287 0, /* window style */
288 0, /* default X coordinate */
289 0, /* default Y coordinate */
290 0, /* window width */
291 0, /* window height */
292 NULL, /* no parent window */
293 NULL, /* no menu in this window */
294 GetModuleHandle(NULL), /* handle of this program instance */
295 NULL ); /* sent to WM_CREATE */
297 SetWindowLong( ipcwindow, GWL_WNDPROC, (LONG)WMCOPYWNDPROC );
298 SetWindowLong( ipcwindow, GWL_USERDATA, (LONG)p_this );
300 /* Signal the creation of the thread and events queue */
301 vlc_thread_ready( p_this );
303 while( GetMessage( &message, NULL, 0, 0 ) )
305 TranslateMessage( &message );
306 DispatchMessage( &message );
310 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
313 if( uMsg == WM_COPYDATA )
315 COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
316 vlc_object_t *p_this;
317 playlist_t *p_playlist;
319 p_this = (vlc_object_t *)GetWindowLong( hwnd, GWL_USERDATA );
321 if( !p_this ) return 0;
323 /* Add files to the playlist */
324 p_playlist = (playlist_t *)vlc_object_find( p_this,
327 if( !p_playlist ) return 0;
329 if( pwm_data->lpData )
331 int i_argc, i_data, i_opt, i_options;
333 char *p_data = (char *)pwm_data->lpData;
335 i_argc = *((int *)&p_data[0]);
336 ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
337 i_data = sizeof(int);
338 for( i_opt = 0; i_opt < i_argc; i_opt++ )
340 ppsz_argv[i_opt] = &p_data[i_data + sizeof(int)];
341 i_data += *((int *)&p_data[i_data]);
342 i_data += sizeof(int);
345 for( i_opt = 0; i_opt < i_argc; i_opt++ )
349 /* Count the input options */
350 while( i_opt + i_options + 1 < i_argc &&
351 *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
355 if( i_opt || config_GetInt( p_this, "playlist-enqueue" ) )
357 playlist_PlaylistAddExt( p_playlist, ppsz_argv[i_opt],
358 NULL, PLAYLIST_APPEND ,
360 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
363 playlist_PlaylistAddExt( p_playlist, ppsz_argv[i_opt],
364 NULL, PLAYLIST_APPEND | PLAYLIST_GO,
366 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
376 vlc_object_release( p_playlist );
379 return DefWindowProc( hwnd, uMsg, wParam, lParam );
382 /*****************************************************************************
383 * system_End: terminate winsock.
384 *****************************************************************************/
385 void system_End( libvlc_int_t *p_this )
387 if( p_this && p_this->p_libvlc_global && p_this->p_libvlc_global->psz_vlcpath )
389 free( p_this->p_libvlc_global->psz_vlcpath );
390 p_this->p_libvlc_global->psz_vlcpath = NULL;