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