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 /* Raise default priority of the current process */
125 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
126 # define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
128 if( config_GetInt( p_this, "high-priority" ) )
130 if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
131 || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
133 msg_Dbg( p_this, "raised process priority" );
137 msg_Dbg( p_this, "could not raise process priority" );
141 if( config_GetInt( p_this, "one-instance" )
142 || ( config_GetInt( p_this, "one-instance-when-started-from-file" )
143 && config_GetInt( p_this, "started-from-file" ) ) )
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( 0, TRUE, _T("VLC ipc ") _T(VERSION) ) ) )
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( 0, _T("VLC ipc ") _T(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( _T("STATIC"), /* name of window class */
251 _T("VLC ipc ") _T(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 ] == ':' )
320 if( i_opt || config_GetInt( p_this, "playlist-enqueue" ) )
322 playlist_AddExt( p_playlist, ppsz_argv[i_opt],
323 NULL, PLAYLIST_APPEND ,
325 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
326 i_options, VLC_TRUE, VLC_FALSE );
328 playlist_AddExt( p_playlist, ppsz_argv[i_opt],
329 NULL, PLAYLIST_APPEND | PLAYLIST_GO,
331 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
332 i_options, VLC_TRUE, VLC_FALSE );
341 vlc_object_release( p_playlist );
344 return DefWindowProc( hwnd, uMsg, wParam, lParam );
347 /*****************************************************************************
348 * system_End: terminate winsock.
349 *****************************************************************************/
350 void system_End( libvlc_int_t *p_this )
352 if( p_this && p_this->p_libvlc_global && vlc_global( p_this )->psz_vlcpath )
354 free( vlc_global( p_this )->psz_vlcpath );
355 vlc_global( p_this )->psz_vlcpath = NULL;