1 /*****************************************************************************
2 * win32_specific.c: Win32 specific features
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 /*****************************************************************************
43 * system_Init: initialize winsock and misc other things.
44 *****************************************************************************/
45 void system_Init( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
49 /* Get our full path */
50 #if !defined( UNDER_CE )
53 char psz_path[MAX_PATH];
56 GetFullPathName( ppsz_argv[0], MAX_PATH, psz_path, &psz_vlc );
58 if( psz_vlc > psz_path && psz_vlc[-1] == '\\' )
61 p_this->p_libvlc->psz_vlcpath = strdup( psz_path );
65 p_this->p_libvlc->psz_vlcpath = strdup( "" );
71 p_this->p_libvlc->psz_vlcpath = strdup( "" );
74 /* Set the default file-translation mode */
75 #if !defined( UNDER_CE )
78 _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
80 /* Call mdate() once to make sure it is initialized properly */
83 /* WinSock Library Init. */
84 if( !WSAStartup( MAKEWORD( 2, 0 ), &Data ) )
86 /* Confirm that the WinSock DLL supports 2.0.*/
87 if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 0 )
89 /* We could not find a suitable WinSock DLL. */
94 /* Everything went ok. */
99 /* Let's try with WinSock 1.1 */
100 if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
102 /* Confirm that the WinSock DLL supports 1.1.*/
103 if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
105 /* We could not find a suitable WinSock DLL. */
110 /* Everything went ok. */
115 fprintf( stderr, "error: can't initialize WinSocks\n" );
120 /*****************************************************************************
121 * system_Configure: check for system specific configuration options.
122 *****************************************************************************/
123 static void IPCHelperThread( vlc_object_t * );
124 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
126 void system_Configure( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
128 #if !defined( UNDER_CE )
129 p_this->p_libvlc->b_fast_mutex = config_GetInt( p_this, "fast-mutex" );
130 p_this->p_libvlc->i_win9x_cv = config_GetInt( p_this, "win9x-cv-method" );
132 /* Raise default priority of the current process */
133 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
134 # define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
136 if( config_GetInt( p_this, "high-priority" ) )
138 if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
139 || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
141 msg_Dbg( p_this, "raised process priority" );
145 msg_Dbg( p_this, "could not raise process priority" );
149 if( config_GetInt( p_this, "one-instance" ) )
153 msg_Info( p_this, "one instance mode ENABLED");
155 /* Use a named mutex to check if another instance is already running */
156 if( ( hmutex = CreateMutex( NULL, TRUE, "VLC ipc "VERSION ) ) == NULL )
158 /* Failed for some reason. Just ignore the option and go on as
160 msg_Err( p_this, "one instance mode DISABLED "
161 "(mutex couldn't be created)" );
165 if( GetLastError() != ERROR_ALREADY_EXISTS )
167 /* We are the 1st instance. */
168 vlc_object_t *p_helper =
169 (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
171 /* Run the helper thread */
172 if( vlc_thread_create( p_helper, "IPC helper", IPCHelperThread,
173 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
175 msg_Err( p_this, "one instance mode DISABLED "
176 "(IPC helper thread couldn't be created)" );
180 /* Initialization done.
181 * Release the mutex to unblock other instances */
182 ReleaseMutex( hmutex );
186 /* Another instance is running */
190 /* Wait until the 1st instance is initialized */
191 WaitForSingleObject( hmutex, INFINITE );
193 /* Locate the window created by the IPC helper thread of the
195 if( ( ipcwindow = FindWindow( NULL, "VLC ipc "VERSION ) )
198 msg_Err( p_this, "one instance mode DISABLED "
199 "(couldn't find 1st instance of program)" );
200 ReleaseMutex( hmutex );
204 /* We assume that the remaining parameters are filenames
205 * and their input options */
206 if( *pi_argc - 1 >= optind )
208 COPYDATASTRUCT wm_data;
212 i_data = sizeof(int);
213 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
215 i_data += sizeof(int);
216 i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
219 p_data = (char *)malloc( i_data );
220 *((int *)&p_data[0]) = *pi_argc - optind;
221 i_data = sizeof(int);
222 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
224 int i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
225 *((int *)&p_data[i_data]) = i_len;
226 i_data += sizeof(int);
227 memcpy( &p_data[i_data], ppsz_argv[ i_opt ], i_len );
231 /* Send our playlist items to the 1st instance */
233 wm_data.cbData = i_data;
234 wm_data.lpData = p_data;
235 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
238 /* Initialization done.
239 * Release the mutex to unblock other instances */
240 ReleaseMutex( hmutex );
243 system_End( p_this );
251 static void IPCHelperThread( vlc_object_t *p_this )
257 CreateWindow( "STATIC", /* name of window class */
258 "VLC ipc "VERSION, /* window title bar text */
259 0, /* window style */
260 0, /* default X coordinate */
261 0, /* default Y coordinate */
262 0, /* window width */
263 0, /* window height */
264 NULL, /* no parent window */
265 NULL, /* no menu in this window */
266 GetModuleHandle(NULL), /* handle of this program instance */
267 NULL ); /* sent to WM_CREATE */
269 SetWindowLong( ipcwindow, GWL_WNDPROC, (LONG)WMCOPYWNDPROC );
270 SetWindowLong( ipcwindow, GWL_USERDATA, (LONG)p_this );
272 /* Signal the creation of the thread and events queue */
273 vlc_thread_ready( p_this );
275 while( GetMessage( &message, NULL, 0, 0 ) )
277 TranslateMessage( &message );
278 DispatchMessage( &message );
282 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
285 if( uMsg == WM_COPYDATA )
287 COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
288 vlc_object_t *p_this;
289 playlist_t *p_playlist;
291 p_this = (vlc_object_t *)GetWindowLong( hwnd, GWL_USERDATA );
293 if( !p_this ) return 0;
295 /* Add files to the playlist */
296 p_playlist = (playlist_t *)vlc_object_find( p_this,
299 if( !p_playlist ) return 0;
301 if( pwm_data->lpData )
303 int i_argc, i_data, i_opt, i_options;
305 char *p_data = (char *)pwm_data->lpData;
307 i_argc = *((int *)&p_data[0]);
308 ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
309 i_data = sizeof(int);
310 for( i_opt = 0; i_opt < i_argc; i_opt++ )
312 ppsz_argv[i_opt] = &p_data[i_data + sizeof(int)];
313 i_data += *((int *)&p_data[i_data]);
314 i_data += sizeof(int);
317 for( i_opt = 0; i_opt < i_argc; i_opt++ )
321 /* Count the input options */
322 while( i_opt + i_options + 1 < i_argc &&
323 *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
328 playlist_AddExt( p_playlist, ppsz_argv[i_opt],ppsz_argv[i_opt],
329 PLAYLIST_APPEND | (i_opt? 0 : PLAYLIST_GO),
331 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
340 vlc_object_release( p_playlist );
343 return DefWindowProc( hwnd, uMsg, wParam, lParam );
346 /*****************************************************************************
347 * system_End: terminate winsock.
348 *****************************************************************************/
349 void system_End( vlc_t *p_this )