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