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