]> git.sesse.net Git - vlc/blob - src/misc/win32_specific.c
Fix playlist enqueue RPC
[vlc] / src / misc / win32_specific.c
1 /*****************************************************************************
2  * win32_specific.c: Win32 specific features
3  *****************************************************************************
4  * Copyright (C) 2001-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include <vlc/vlc.h>
26 #include "../libvlc.h"
27 #include <vlc_playlist.h>
28 #include <vlc_charset.h>
29
30 #ifdef WIN32                       /* optind, getopt(), included in unistd.h */
31 #   include "../extras/getopt.h"
32 #endif
33
34 #if !defined( UNDER_CE )
35 #   include <io.h>
36 #   include <fcntl.h>
37 #endif
38
39 #include <winsock.h>
40
41 /*****************************************************************************
42  * system_Init: initialize winsock and misc other things.
43  *****************************************************************************/
44 void system_Init( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
45 {
46     WSADATA Data;
47
48     /* Get our full path */
49     char psz_path[MAX_PATH];
50     char *psz_vlc;
51
52 #if defined( UNDER_CE )
53     wchar_t psz_wpath[MAX_PATH];
54     if( GetModuleFileName( NULL, psz_wpath, MAX_PATH ) )
55     {
56         WideCharToMultiByte( CP_ACP, 0, psz_wpath, -1,
57                              psz_path, MAX_PATH, NULL, NULL );
58     }
59     else psz_path[0] = '\0';
60
61 #else
62     if( ppsz_argv[0] )
63     {
64         GetFullPathName( ppsz_argv[0], MAX_PATH, psz_path, &psz_vlc );
65     }
66     else if( !GetModuleFileName( NULL, psz_path, MAX_PATH ) )
67     {
68         psz_path[0] = '\0';
69     }
70 #endif
71
72     if( (psz_vlc = strrchr( psz_path, '\\' )) ) *psz_vlc = '\0';
73
74 #ifndef HAVE_RELEASE
75     {
76         /* remove trailing \.libs from executable dir path if seen,
77            we assume we are running vlc through libtool wrapper in build dir */
78         int offset  = strlen(psz_path)-sizeof("\\.libs")+1;
79         if( offset > 0 )
80         {
81             psz_vlc = psz_path+offset;
82             if( ! strcmp(psz_vlc, "\\.libs") ) *psz_vlc = '\0';
83         }
84     }
85 #endif
86
87     vlc_global()->psz_vlcpath = strdup( psz_path );
88
89     /* Set the default file-translation mode */
90 #if !defined( UNDER_CE )
91     _fmode = _O_BINARY;
92     _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
93 #endif
94
95     /* Call mdate() once to make sure it is initialized properly */
96     mdate();
97
98     /* WinSock Library Init. */
99     if( !WSAStartup( MAKEWORD( 2, 2 ), &Data ) )
100     {
101         /* Aah, pretty useless check, we should always have Winsock 2.2
102          * since it appeared in Win98. */
103         if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 2 )
104             /* We could not find a suitable WinSock DLL. */
105             WSACleanup( );
106         else
107             /* Everything went ok. */
108             return;
109     }
110
111     /* Let's try with WinSock 1.1 */
112     if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
113     {
114         /* Confirm that the WinSock DLL supports 1.1.*/
115         if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
116             /* We could not find a suitable WinSock DLL. */
117             WSACleanup( );
118         else
119             /* Everything went ok. */
120             return;
121     }
122
123     fprintf( stderr, "error: can't initialize WinSocks\n" );
124 }
125
126 /*****************************************************************************
127  * system_Configure: check for system specific configuration options.
128  *****************************************************************************/
129 static void IPCHelperThread( vlc_object_t * );
130 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
131 typedef struct
132 {
133   int argc;
134   int enqueue;
135   char data[0];
136 } vlc_ipc_data_t;
137
138 void system_Configure( libvlc_int_t *p_this, int *pi_argc, char *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( config_GetInt( 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( config_GetInt( p_this, "one-instance" )
159         || ( config_GetInt( p_this, "one-instance-when-started-from-file" )
160              && config_GetInt( 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, _T("VLC ipc ") _T(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             vlc_object_t *p_helper =
180              (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
181
182             /* Run the helper thread */
183             if( vlc_thread_create( p_helper, "IPC helper", IPCHelperThread,
184                                    VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
185             {
186                 msg_Err( p_this, "one instance mode DISABLED "
187                          "(IPC helper thread couldn't be created)" );
188
189             }
190
191             /* Initialization done.
192              * Release the mutex to unblock other instances */
193             ReleaseMutex( hmutex );
194         }
195         else
196         {
197             /* Another instance is running */
198
199             HWND ipcwindow;
200
201             /* Wait until the 1st instance is initialized */
202             WaitForSingleObject( hmutex, INFINITE );
203
204             /* Locate the window created by the IPC helper thread of the
205              * 1st instance */
206             if( !( ipcwindow = FindWindow( 0, _T("VLC ipc ") _T(VERSION) ) ) )
207             {
208                 msg_Err( p_this, "one instance mode DISABLED "
209                          "(couldn't find 1st instance of program)" );
210                 ReleaseMutex( hmutex );
211                 return;
212             }
213
214             /* We assume that the remaining parameters are filenames
215              * and their input options */
216             if( *pi_argc - 1 >= optind )
217             {
218                 COPYDATASTRUCT wm_data;
219                 int i_opt;
220                 vlc_ipc_data_t *p_data;
221                 size_t i_data = sizeof (*p_data);
222
223                 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
224                 {
225                     i_data += sizeof (size_t);
226                     i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
227                 }
228
229                 p_data = malloc( i_data );
230                 p_data->argc = *pi_argc - optind;
231                 p_data->enqueue = config_GetInt( p_this, "playlist-enqueue" );
232                 i_data = 0;
233                 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
234                 {
235                     size_t i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
236                     /* Windows will never switch to an architecture
237                      * with stronger alignment requirements, right. */
238                     *((size_t *)(p_data->data + i_data)) = i_len;
239                     i_data += sizeof (size_t);
240                     memcpy( &p_data[i_data], ppsz_argv[ i_opt ], i_len );
241                     i_data += i_len;
242                 }
243                 i_data += sizeof (*p_data);
244
245                 /* Send our playlist items to the 1st instance */
246                 wm_data.dwData = 0;
247                 wm_data.cbData = i_data;
248                 wm_data.lpData = p_data;
249                 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
250             }
251
252             /* Initialization done.
253              * Release the mutex to unblock other instances */
254             ReleaseMutex( hmutex );
255
256             /* Bye bye */
257             system_End( p_this );
258             exit( 0 );
259         }
260     }
261
262 #endif
263 }
264
265 static void IPCHelperThread( vlc_object_t *p_this )
266 {
267     HWND ipcwindow;
268     MSG message;
269
270     ipcwindow =
271         CreateWindow( _T("STATIC"),                  /* name of window class */
272                   _T("VLC ipc ") _T(VERSION),       /* window title bar text */
273                   0,                                         /* window style */
274                   0,                                 /* default X coordinate */
275                   0,                                 /* default Y coordinate */
276                   0,                                         /* window width */
277                   0,                                        /* window height */
278                   NULL,                                  /* no parent window */
279                   NULL,                            /* no menu in this window */
280                   GetModuleHandle(NULL),  /* handle of this program instance */
281                   NULL );                               /* sent to WM_CREATE */
282
283     SetWindowLong( ipcwindow, GWL_WNDPROC, (LONG)WMCOPYWNDPROC );
284     SetWindowLong( ipcwindow, GWL_USERDATA, (LONG)p_this );
285
286     /* Signal the creation of the thread and events queue */
287     vlc_thread_ready( p_this );
288
289     while( GetMessage( &message, NULL, 0, 0 ) )
290     {
291         TranslateMessage( &message );
292         DispatchMessage( &message );
293     }
294 }
295
296 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
297                                 LPARAM lParam )
298 {
299     if( uMsg == WM_COPYDATA )
300     {
301         COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
302         vlc_object_t *p_this;
303         playlist_t *p_playlist;
304
305         p_this = (vlc_object_t *)GetWindowLong( hwnd, GWL_USERDATA );
306
307         if( !p_this ) return 0;
308
309         /* Add files to the playlist */
310         p_playlist = (playlist_t *)vlc_object_find( p_this,
311                                                     VLC_OBJECT_PLAYLIST,
312                                                     FIND_ANYWHERE );
313         if( !p_playlist ) return 0;
314
315         if( pwm_data->lpData )
316         {
317             char **ppsz_argv;
318             vlc_ipc_data_t *p_data = (vlc_ipc_data_t *)pwm_data->lpData;
319             size_t i_data = 0;
320             int i_argc = p_data->argc, i_opt, i_options;
321
322             ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
323             for( i_opt = 0; i_opt < i_argc; i_opt++ )
324             {
325                 ppsz_argv[i_opt] = p_data->data + i_data + sizeof(int);
326                 i_data += sizeof(int) + *((int *)(p_data->data + i_data));
327             }
328
329             for( i_opt = 0; i_opt < i_argc; i_opt++ )
330             {
331                 i_options = 0;
332
333                 /* Count the input options */
334                 while( i_opt + i_options + 1 < i_argc &&
335                        *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
336                 {
337                     i_options++;
338                 }
339                 playlist_AddExt( p_playlist, ppsz_argv[i_opt],
340                   NULL, PLAYLIST_APPEND |
341                         ( ( i_opt || p_data->enqueue ) ? 0 : PLAYLIST_GO ),
342                   PLAYLIST_END, -1,
343                   (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
344                   i_options, VLC_TRUE, VLC_FALSE );
345
346                 i_opt += i_options;
347             }
348
349             free( ppsz_argv );
350         }
351
352         vlc_object_release( p_playlist );
353     }
354
355     return DefWindowProc( hwnd, uMsg, wParam, lParam );
356 }
357
358 /*****************************************************************************
359  * system_End: terminate winsock.
360  *****************************************************************************/
361 void system_End( libvlc_int_t *p_this )
362 {
363     if( p_this && vlc_global() )
364     {
365         free( vlc_global()->psz_vlcpath );
366         vlc_global()->psz_vlcpath = NULL;
367     }
368
369     WSACleanup();
370 }