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., 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 char psz_path[MAX_PATH];
53 #if defined( UNDER_CE )
54 wchar_t psz_wpath[MAX_PATH];
55 if( GetModuleFileName( NULL, psz_wpath, MAX_PATH ) )
57 WideCharToMultiByte( CP_ACP, 0, psz_wpath, -1,
58 psz_path, MAX_PATH, NULL, NULL );
60 else psz_path[0] = '\0';
65 GetFullPathName( ppsz_argv[0], MAX_PATH, psz_path, &psz_vlc );
67 else if( !GetModuleFileName( NULL, psz_path, MAX_PATH ) )
73 if( (psz_vlc = strrchr( psz_path, '\\' )) ) *psz_vlc = '\0';
75 p_this->p_libvlc->psz_vlcpath = strdup( psz_path );
77 /* Set the default file-translation mode */
78 #if !defined( UNDER_CE )
80 _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
83 /* Call mdate() once to make sure it is initialized properly */
86 /* WinSock Library Init. */
87 if( !WSAStartup( MAKEWORD( 2, 0 ), &Data ) )
89 /* Confirm that the WinSock DLL supports 2.0.*/
90 if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 0 )
92 /* We could not find a suitable WinSock DLL. */
97 /* 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 )
108 /* We could not find a suitable WinSock DLL. */
113 /* Everything went ok. */
118 fprintf( stderr, "error: can't initialize WinSocks\n" );
123 /*****************************************************************************
124 * system_Configure: check for system specific configuration options.
125 *****************************************************************************/
126 static void IPCHelperThread( vlc_object_t * );
127 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
129 void system_Configure( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
131 #if !defined( UNDER_CE )
132 p_this->p_libvlc->b_fast_mutex = config_GetInt( p_this, "fast-mutex" );
133 p_this->p_libvlc->i_win9x_cv = config_GetInt( p_this, "win9x-cv-method" );
135 /* Raise default priority of the current process */
136 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
137 # define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
139 if( config_GetInt( p_this, "high-priority" ) )
141 if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
142 || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
144 msg_Dbg( p_this, "raised process priority" );
148 msg_Dbg( p_this, "could not raise process priority" );
152 if( config_GetInt( p_this, "one-instance" ) )
156 msg_Info( p_this, "one instance mode ENABLED");
158 /* Use a named mutex to check if another instance is already running */
159 if( !( hmutex = CreateMutex( 0, TRUE, _T("VLC ipc ") _T(VERSION) ) ) )
161 /* Failed for some reason. Just ignore the option and go on as
163 msg_Err( p_this, "one instance mode DISABLED "
164 "(mutex couldn't be created)" );
168 if( GetLastError() != ERROR_ALREADY_EXISTS )
170 /* We are the 1st instance. */
171 vlc_object_t *p_helper =
172 (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
174 /* Run the helper thread */
175 if( vlc_thread_create( p_helper, "IPC helper", IPCHelperThread,
176 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
178 msg_Err( p_this, "one instance mode DISABLED "
179 "(IPC helper thread couldn't be created)" );
183 /* Initialization done.
184 * Release the mutex to unblock other instances */
185 ReleaseMutex( hmutex );
189 /* Another instance is running */
193 /* Wait until the 1st instance is initialized */
194 WaitForSingleObject( hmutex, INFINITE );
196 /* Locate the window created by the IPC helper thread of the
198 if( !( ipcwindow = FindWindow( 0, _T("VLC ipc ") _T(VERSION) ) ) )
200 msg_Err( p_this, "one instance mode DISABLED "
201 "(couldn't find 1st instance of program)" );
202 ReleaseMutex( hmutex );
206 /* We assume that the remaining parameters are filenames
207 * and their input options */
208 if( *pi_argc - 1 >= optind )
210 COPYDATASTRUCT wm_data;
214 i_data = sizeof(int);
215 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
217 i_data += sizeof(int);
218 i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
221 p_data = (char *)malloc( i_data );
222 *((int *)&p_data[0]) = *pi_argc - optind;
223 i_data = sizeof(int);
224 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
226 int i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
227 *((int *)&p_data[i_data]) = i_len;
228 i_data += sizeof(int);
229 memcpy( &p_data[i_data], ppsz_argv[ i_opt ], i_len );
233 /* Send our playlist items to the 1st instance */
235 wm_data.cbData = i_data;
236 wm_data.lpData = p_data;
237 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
240 /* Initialization done.
241 * Release the mutex to unblock other instances */
242 ReleaseMutex( hmutex );
245 system_End( p_this );
253 static void IPCHelperThread( vlc_object_t *p_this )
259 CreateWindow( _T("STATIC"), /* name of window class */
260 _T("VLC ipc ") _T(VERSION), /* window title bar text */
261 0, /* window style */
262 0, /* default X coordinate */
263 0, /* default Y coordinate */
264 0, /* window width */
265 0, /* window height */
266 NULL, /* no parent window */
267 NULL, /* no menu in this window */
268 GetModuleHandle(NULL), /* handle of this program instance */
269 NULL ); /* sent to WM_CREATE */
271 SetWindowLong( ipcwindow, GWL_WNDPROC, (LONG)WMCOPYWNDPROC );
272 SetWindowLong( ipcwindow, GWL_USERDATA, (LONG)p_this );
274 /* Signal the creation of the thread and events queue */
275 vlc_thread_ready( p_this );
277 while( GetMessage( &message, NULL, 0, 0 ) )
279 TranslateMessage( &message );
280 DispatchMessage( &message );
284 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
287 /* zcot's screensaver patch */
288 if( uMsg == WM_SYSCOMMAND )
290 if( ( wParam & 0xFFF0 ) == SC_SCREENSAVE )
292 return DefWindowProc( hwnd, uMsg, wParam, lParam );
294 /* End zcot's screensaver patch */
296 if( uMsg == WM_COPYDATA )
298 COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
299 vlc_object_t *p_this;
300 playlist_t *p_playlist;
302 p_this = (vlc_object_t *)GetWindowLong( hwnd, GWL_USERDATA );
304 if( !p_this ) return 0;
306 /* Add files to the playlist */
307 p_playlist = (playlist_t *)vlc_object_find( p_this,
310 if( !p_playlist ) return 0;
312 if( pwm_data->lpData )
314 int i_argc, i_data, i_opt, i_options;
316 char *p_data = (char *)pwm_data->lpData;
318 i_argc = *((int *)&p_data[0]);
319 ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
320 i_data = sizeof(int);
321 for( i_opt = 0; i_opt < i_argc; i_opt++ )
323 ppsz_argv[i_opt] = &p_data[i_data + sizeof(int)];
324 i_data += *((int *)&p_data[i_data]);
325 i_data += sizeof(int);
328 for( i_opt = 0; i_opt < i_argc; i_opt++ )
332 /* Count the input options */
333 while( i_opt + i_options + 1 < i_argc &&
334 *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
338 if( i_opt || config_GetInt( p_this, "playlist-enqueue" ) )
340 playlist_AddExt( p_playlist, ppsz_argv[i_opt],
341 ppsz_argv[i_opt], PLAYLIST_APPEND ,
343 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
346 playlist_AddExt( p_playlist, ppsz_argv[i_opt],
347 ppsz_argv[i_opt], PLAYLIST_APPEND | PLAYLIST_GO,
349 (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
359 vlc_object_release( p_playlist );
362 return DefWindowProc( hwnd, uMsg, wParam, lParam );
365 /*****************************************************************************
366 * system_End: terminate winsock.
367 *****************************************************************************/
368 void system_End( vlc_t *p_this )
370 if( p_this && p_this->p_libvlc && p_this->p_libvlc->psz_vlcpath )
372 free( p_this->p_libvlc->psz_vlcpath );
373 p_this->p_libvlc->psz_vlcpath = NULL;