]> git.sesse.net Git - vlc/blob - src/win32/specific.c
Split file path functions out of vlc_charset.h into vlc_fs.h
[vlc] / src / win32 / specific.c
1 /*****************************************************************************
2  * specific.c: Win32 specific initilization
3  *****************************************************************************
4  * Copyright (C) 2001-2004 the VideoLAN team
5  *
6  * Authors: Samuel Hocevar <sam@zoy.org>
7  *          Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #define UNICODE
29 #include <vlc_common.h>
30 #include "../libvlc.h"
31 #include <vlc_playlist.h>
32
33 #include "../extras/getopt.h"
34
35 #if !defined( UNDER_CE )
36 #   include <io.h>
37 #   include <fcntl.h>
38 #   include  <mmsystem.h>
39 #endif
40
41 #include <winsock.h>
42
43 /*****************************************************************************
44  * system_Init: initialize winsock and misc other things.
45  *****************************************************************************/
46 void system_Init( libvlc_int_t *p_this, int *pi_argc, const char *ppsz_argv[] )
47 {
48     VLC_UNUSED( p_this ); VLC_UNUSED( pi_argc ); VLC_UNUSED( ppsz_argv );
49     WSADATA Data;
50     MEMORY_BASIC_INFORMATION mbi;
51
52     /* Get our full path */
53     char psz_path[MAX_PATH];
54     char *psz_vlc;
55
56     wchar_t psz_wpath[MAX_PATH];
57     if( VirtualQuery(system_Init, &mbi, sizeof(mbi) ) )
58     {
59         HMODULE hMod = (HMODULE) mbi.AllocationBase;
60         if( GetModuleFileName( hMod, psz_wpath, MAX_PATH ) )
61         {
62             WideCharToMultiByte( CP_UTF8, 0, psz_wpath, -1,
63                                 psz_path, MAX_PATH, NULL, NULL );
64         }
65         else psz_path[0] = '\0';
66     }
67
68     if( (psz_vlc = strrchr( psz_path, '\\' )) ) *psz_vlc = '\0';
69
70 #ifndef HAVE_RELEASE
71     {
72         /* remove trailing \.libs from executable dir path if seen,
73            we assume we are running vlc through libtool wrapper in build dir */
74         int offset  = strlen(psz_path)-sizeof("\\.libs")+1;
75         if( offset > 0 )
76         {
77             psz_vlc = psz_path+offset;
78             if( ! strcmp(psz_vlc, "\\.libs") ) *psz_vlc = '\0';
79         }
80     }
81 #endif
82
83     psz_vlcpath = strdup( psz_path );
84
85     /* Set the default file-translation mode */
86 #if !defined( UNDER_CE )
87     _fmode = _O_BINARY;
88     _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
89
90     timeBeginPeriod(5);
91 #endif
92
93     /* Call mdate() once to make sure it is initialized properly */
94     mdate();
95
96     /* WinSock Library Init. */
97     if( !WSAStartup( MAKEWORD( 2, 2 ), &Data ) )
98     {
99         /* Aah, pretty useless check, we should always have Winsock 2.2
100          * since it appeared in Win98. */
101         if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 2 )
102             /* We could not find a suitable WinSock DLL. */
103             WSACleanup( );
104         else
105             /* Everything went ok. */
106             return;
107     }
108
109     /* Let's try with WinSock 1.1 */
110     if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
111     {
112         /* Confirm that the WinSock DLL supports 1.1.*/
113         if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
114             /* We could not find a suitable WinSock DLL. */
115             WSACleanup( );
116         else
117             /* Everything went ok. */
118             return;
119     }
120
121     fprintf( stderr, "error: can't initialize WinSocks\n" );
122 }
123
124 /*****************************************************************************
125  * system_Configure: check for system specific configuration options.
126  *****************************************************************************/
127 static unsigned __stdcall IPCHelperThread( void * );
128 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
129 static vlc_object_t *p_helper = NULL;
130 static unsigned long hIPCHelper;
131 static HANDLE hIPCHelperReady;
132
133 typedef struct
134 {
135   int argc;
136   int enqueue;
137   char data[];
138 } vlc_ipc_data_t;
139
140 void system_Configure( libvlc_int_t *p_this, int *pi_argc, const char *ppsz_argv[] )
141 {
142 #if !defined( UNDER_CE )
143     /* Raise default priority of the current process */
144 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
145 #   define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
146 #endif
147     if( var_InheritBool( p_this, "high-priority" ) )
148     {
149         if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
150              || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
151         {
152             msg_Dbg( p_this, "raised process priority" );
153         }
154         else
155         {
156             msg_Dbg( p_this, "could not raise process priority" );
157         }
158     }
159
160     if( var_InheritBool( p_this, "one-instance" )
161      || ( var_InheritBool( p_this, "one-instance-when-started-from-file" )
162        && var_InheritBool( p_this, "started-from-file" ) ) )
163     {
164         HANDLE hmutex;
165
166         msg_Info( p_this, "one instance mode ENABLED");
167
168         /* Use a named mutex to check if another instance is already running */
169         if( !( hmutex = CreateMutex( 0, TRUE, L"VLC ipc "VERSION ) ) )
170         {
171             /* Failed for some reason. Just ignore the option and go on as
172              * normal. */
173             msg_Err( p_this, "one instance mode DISABLED "
174                      "(mutex couldn't be created)" );
175             return;
176         }
177
178         if( GetLastError() != ERROR_ALREADY_EXISTS )
179         {
180             /* We are the 1st instance. */
181             static const char typename[] = "ipc helper";
182             p_helper =
183                 vlc_custom_create( p_this, sizeof(vlc_object_t),
184                                    VLC_OBJECT_GENERIC, typename );
185
186             /* Run the helper thread */
187             hIPCHelperReady = CreateEvent( NULL, FALSE, FALSE, NULL );
188             hIPCHelper = _beginthreadex( NULL, 0, IPCHelperThread, p_helper,
189                                          0, NULL );
190             if( hIPCHelper )
191                 WaitForSingleObject( hIPCHelperReady, INFINITE );
192             else
193             {
194                 msg_Err( p_this, "one instance mode DISABLED "
195                          "(IPC helper thread couldn't be created)" );
196                 vlc_object_release (p_helper);
197                 p_helper = NULL;
198             }
199             vlc_object_attach (p_helper, p_this);
200             CloseHandle( hIPCHelperReady );
201
202             /* Initialization done.
203              * Release the mutex to unblock other instances */
204             ReleaseMutex( hmutex );
205         }
206         else
207         {
208             /* Another instance is running */
209
210             HWND ipcwindow;
211
212             /* Wait until the 1st instance is initialized */
213             WaitForSingleObject( hmutex, INFINITE );
214
215             /* Locate the window created by the IPC helper thread of the
216              * 1st instance */
217             if( !( ipcwindow = FindWindow( 0, L"VLC ipc "VERSION ) ) )
218             {
219                 msg_Err( p_this, "one instance mode DISABLED "
220                          "(couldn't find 1st instance of program)" );
221                 ReleaseMutex( hmutex );
222                 return;
223             }
224
225             /* We assume that the remaining parameters are filenames
226              * and their input options */
227             if( *pi_argc - 1 >= optind )
228             {
229                 COPYDATASTRUCT wm_data;
230                 int i_opt;
231                 vlc_ipc_data_t *p_data;
232                 size_t i_data = sizeof (*p_data);
233
234                 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
235                 {
236                     i_data += sizeof (size_t);
237                     i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
238                 }
239
240                 p_data = malloc( i_data );
241                 p_data->argc = *pi_argc - optind;
242                 p_data->enqueue = var_InheritBool( p_this, "playlist-enqueue" );
243                 i_data = 0;
244                 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
245                 {
246                     size_t i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
247                     /* Windows will never switch to an architecture
248                      * with stronger alignment requirements, right. */
249                     *((size_t *)(p_data->data + i_data)) = i_len;
250                     i_data += sizeof (size_t);
251                     memcpy( &p_data->data[i_data], ppsz_argv[ i_opt ], i_len );
252                     i_data += i_len;
253                 }
254                 i_data += sizeof (*p_data);
255
256                 /* Send our playlist items to the 1st instance */
257                 wm_data.dwData = 0;
258                 wm_data.cbData = i_data;
259                 wm_data.lpData = p_data;
260                 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
261             }
262
263             /* Initialization done.
264              * Release the mutex to unblock other instances */
265             ReleaseMutex( hmutex );
266
267             /* Bye bye */
268             system_End( p_this );
269             exit( 0 );
270         }
271     }
272
273 #endif
274 }
275
276 static unsigned __stdcall IPCHelperThread( void *data )
277 {
278     vlc_object_t *p_this = data;
279     HWND ipcwindow;
280     MSG message;
281
282     ipcwindow =
283         CreateWindow( L"STATIC",                     /* name of window class */
284                   L"VLC ipc "VERSION,               /* window title bar text */
285                   0,                                         /* window style */
286                   0,                                 /* default X coordinate */
287                   0,                                 /* default Y coordinate */
288                   0,                                         /* window width */
289                   0,                                        /* window height */
290                   NULL,                                  /* no parent window */
291                   NULL,                            /* no menu in this window */
292                   GetModuleHandle(NULL),  /* handle of this program instance */
293                   NULL );                               /* sent to WM_CREATE */
294
295     SetWindowLongPtr( ipcwindow, GWLP_WNDPROC, (LRESULT)WMCOPYWNDPROC );
296     SetWindowLongPtr( ipcwindow, GWLP_USERDATA, (LONG_PTR)p_this );
297
298     /* Signal the creation of the thread and events queue */
299     SetEvent( hIPCHelperReady );
300
301     while( GetMessage( &message, NULL, 0, 0 ) )
302     {
303         TranslateMessage( &message );
304         DispatchMessage( &message );
305     }
306     return 0;
307 }
308
309 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
310                                 LPARAM lParam )
311 {
312     if( uMsg == WM_QUIT  )
313     {
314         PostQuitMessage( 0 );
315     }
316     else if( uMsg == WM_COPYDATA )
317     {
318         COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
319         vlc_object_t *p_this;
320         playlist_t *p_playlist;
321
322         p_this = (vlc_object_t *)
323             (uintptr_t)GetWindowLongPtr( hwnd, GWLP_USERDATA );
324
325         if( !p_this ) return 0;
326
327         /* Add files to the playlist */
328         p_playlist = pl_Hold( p_this );
329         if( !p_playlist ) return 0;
330
331         if( pwm_data->lpData )
332         {
333             char **ppsz_argv;
334             vlc_ipc_data_t *p_data = (vlc_ipc_data_t *)pwm_data->lpData;
335             size_t i_data = 0;
336             int i_argc = p_data->argc, i_opt, i_options;
337
338             ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
339             for( i_opt = 0; i_opt < i_argc; i_opt++ )
340             {
341                 ppsz_argv[i_opt] = p_data->data + i_data + sizeof(int);
342                 i_data += sizeof(int) + *((int *)(p_data->data + i_data));
343             }
344
345             for( i_opt = 0; i_opt < i_argc; i_opt++ )
346             {
347                 i_options = 0;
348
349                 /* Count the input options */
350                 while( i_opt + i_options + 1 < i_argc &&
351                        *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
352                 {
353                     i_options++;
354                 }
355                 playlist_AddExt( p_playlist, ppsz_argv[i_opt],
356                   NULL, PLAYLIST_APPEND |
357                         ( ( i_opt || p_data->enqueue ) ? 0 : PLAYLIST_GO ),
358                   PLAYLIST_END, -1,
359                   i_options,
360                   (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
361                   VLC_INPUT_OPTION_TRUSTED,
362                   true, pl_Unlocked );
363
364                 i_opt += i_options;
365             }
366
367             free( ppsz_argv );
368         }
369
370         vlc_object_release( p_playlist );
371     }
372
373     return DefWindowProc( hwnd, uMsg, wParam, lParam );
374 }
375
376 /*****************************************************************************
377  * system_End: terminate winsock.
378  *****************************************************************************/
379 void system_End( libvlc_int_t *p_this )
380 {
381     HWND ipcwindow;
382     if( p_this )
383     {
384         free( psz_vlcpath );
385         psz_vlcpath = NULL;
386     }
387
388     if (p_helper && p_helper->p_parent == VLC_OBJECT(p_this) )
389     {
390         /* this is the first instance (in a one-instance system)
391          * it is the owner of the helper thread
392          */
393         if( ( ipcwindow = FindWindow( 0, L"VLC ipc "VERSION ) ) != 0 )
394         {
395             SendMessage( ipcwindow, WM_QUIT, 0, 0 );
396         }
397
398         /* FIXME: thread-safety... */
399         vlc_object_release (p_helper);
400         p_helper = NULL;
401     }
402
403 #if !defined( UNDER_CE )
404     timeEndPeriod(5);
405 #endif
406
407     WSACleanup();
408 }