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