]> git.sesse.net Git - vlc/blob - src/misc/win32_specific.c
vlc_os_specific.h: Use const for argv.
[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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc/vlc.h>
30 #include "../libvlc.h"
31 #include <vlc_playlist.h>
32 #include <vlc_charset.h>
33
34 #ifdef WIN32                       /* optind, getopt(), included in unistd.h */
35 #   include "../extras/getopt.h"
36 #endif
37
38 #if !defined( UNDER_CE )
39 #   include <io.h>
40 #   include <fcntl.h>
41 #endif
42
43 #include <winsock.h>
44
45 /*****************************************************************************
46  * system_Init: initialize winsock and misc other things.
47  *****************************************************************************/
48 void system_Init( libvlc_int_t *p_this, int *pi_argc, const char *ppsz_argv[] )
49 {
50     WSADATA Data;
51
52     /* Get our full path */
53     char psz_path[MAX_PATH];
54     char *psz_vlc;
55
56 #if defined( UNDER_CE )
57     wchar_t psz_wpath[MAX_PATH];
58     if( GetModuleFileName( NULL, psz_wpath, MAX_PATH ) )
59     {
60         WideCharToMultiByte( CP_ACP, 0, psz_wpath, -1,
61                              psz_path, MAX_PATH, NULL, NULL );
62     }
63     else psz_path[0] = '\0';
64
65 #else
66     if( ppsz_argv[0] )
67     {
68         GetFullPathName( ppsz_argv[0], MAX_PATH, psz_path, &psz_vlc );
69     }
70     else if( !GetModuleFileName( NULL, psz_path, MAX_PATH ) )
71     {
72         psz_path[0] = '\0';
73     }
74 #endif
75
76     if( (psz_vlc = strrchr( psz_path, '\\' )) ) *psz_vlc = '\0';
77
78 #ifndef HAVE_RELEASE
79     {
80         /* remove trailing \.libs from executable dir path if seen,
81            we assume we are running vlc through libtool wrapper in build dir */
82         int offset  = strlen(psz_path)-sizeof("\\.libs")+1;
83         if( offset > 0 )
84         {
85             psz_vlc = psz_path+offset;
86             if( ! strcmp(psz_vlc, "\\.libs") ) *psz_vlc = '\0';
87         }
88     }
89 #endif
90
91     vlc_global()->psz_vlcpath = strdup( psz_path );
92
93     /* Set the default file-translation mode */
94 #if !defined( UNDER_CE )
95     _fmode = _O_BINARY;
96     _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
97 #endif
98
99     /* Call mdate() once to make sure it is initialized properly */
100     mdate();
101
102     /* WinSock Library Init. */
103     if( !WSAStartup( MAKEWORD( 2, 2 ), &Data ) )
104     {
105         /* Aah, pretty useless check, we should always have Winsock 2.2
106          * since it appeared in Win98. */
107         if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 2 )
108             /* We could not find a suitable WinSock DLL. */
109             WSACleanup( );
110         else
111             /* Everything went ok. */
112             return;
113     }
114
115     /* Let's try with WinSock 1.1 */
116     if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
117     {
118         /* Confirm that the WinSock DLL supports 1.1.*/
119         if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
120             /* We could not find a suitable WinSock DLL. */
121             WSACleanup( );
122         else
123             /* Everything went ok. */
124             return;
125     }
126
127     fprintf( stderr, "error: can't initialize WinSocks\n" );
128 }
129
130 /*****************************************************************************
131  * system_Configure: check for system specific configuration options.
132  *****************************************************************************/
133 static void IPCHelperThread( vlc_object_t * );
134 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
135 typedef struct
136 {
137   int argc;
138   int enqueue;
139   char data[0];
140 } vlc_ipc_data_t;
141
142 void system_Configure( libvlc_int_t *p_this, int *pi_argc, const char *ppsz_argv[] )
143 {
144 #if !defined( UNDER_CE )
145     /* Raise default priority of the current process */
146 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
147 #   define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
148 #endif
149     if( config_GetInt( p_this, "high-priority" ) )
150     {
151         if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
152              || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
153         {
154             msg_Dbg( p_this, "raised process priority" );
155         }
156         else
157         {
158             msg_Dbg( p_this, "could not raise process priority" );
159         }
160     }
161
162     if( config_GetInt( p_this, "one-instance" )
163         || ( config_GetInt( p_this, "one-instance-when-started-from-file" )
164              && config_GetInt( p_this, "started-from-file" ) ) )
165     {
166         HANDLE hmutex;
167
168         msg_Info( p_this, "one instance mode ENABLED");
169
170         /* Use a named mutex to check if another instance is already running */
171         if( !( hmutex = CreateMutex( 0, TRUE, _T("VLC ipc ") _T(VERSION) ) ) )
172         {
173             /* Failed for some reason. Just ignore the option and go on as
174              * normal. */
175             msg_Err( p_this, "one instance mode DISABLED "
176                      "(mutex couldn't be created)" );
177             return;
178         }
179
180         if( GetLastError() != ERROR_ALREADY_EXISTS )
181         {
182             /* We are the 1st instance. */
183             vlc_object_t *p_helper =
184              (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
185
186             /* Run the helper thread */
187             if( vlc_thread_create( p_helper, "IPC helper", IPCHelperThread,
188                                    VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
189             {
190                 msg_Err( p_this, "one instance mode DISABLED "
191                          "(IPC helper thread couldn't be created)" );
192
193             }
194
195             /* Initialization done.
196              * Release the mutex to unblock other instances */
197             ReleaseMutex( hmutex );
198         }
199         else
200         {
201             /* Another instance is running */
202
203             HWND ipcwindow;
204
205             /* Wait until the 1st instance is initialized */
206             WaitForSingleObject( hmutex, INFINITE );
207
208             /* Locate the window created by the IPC helper thread of the
209              * 1st instance */
210             if( !( ipcwindow = FindWindow( 0, _T("VLC ipc ") _T(VERSION) ) ) )
211             {
212                 msg_Err( p_this, "one instance mode DISABLED "
213                          "(couldn't find 1st instance of program)" );
214                 ReleaseMutex( hmutex );
215                 return;
216             }
217
218             /* We assume that the remaining parameters are filenames
219              * and their input options */
220             if( *pi_argc - 1 >= optind )
221             {
222                 COPYDATASTRUCT wm_data;
223                 int i_opt;
224                 vlc_ipc_data_t *p_data;
225                 size_t i_data = sizeof (*p_data);
226
227                 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
228                 {
229                     i_data += sizeof (size_t);
230                     i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
231                 }
232
233                 p_data = malloc( i_data );
234                 p_data->argc = *pi_argc - optind;
235                 p_data->enqueue = config_GetInt( p_this, "playlist-enqueue" );
236                 i_data = 0;
237                 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
238                 {
239                     size_t i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
240                     /* Windows will never switch to an architecture
241                      * with stronger alignment requirements, right. */
242                     *((size_t *)(p_data->data + i_data)) = i_len;
243                     i_data += sizeof (size_t);
244                     memcpy( &p_data->data[i_data], ppsz_argv[ i_opt ], i_len );
245                     i_data += i_len;
246                 }
247                 i_data += sizeof (*p_data);
248
249                 /* Send our playlist items to the 1st instance */
250                 wm_data.dwData = 0;
251                 wm_data.cbData = i_data;
252                 wm_data.lpData = p_data;
253                 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
254             }
255
256             /* Initialization done.
257              * Release the mutex to unblock other instances */
258             ReleaseMutex( hmutex );
259
260             /* Bye bye */
261             system_End( p_this );
262             exit( 0 );
263         }
264     }
265
266 #endif
267 }
268
269 static void IPCHelperThread( vlc_object_t *p_this )
270 {
271     HWND ipcwindow;
272     MSG message;
273
274     ipcwindow =
275         CreateWindow( _T("STATIC"),                  /* name of window class */
276                   _T("VLC ipc ") _T(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     SetWindowLong( ipcwindow, GWL_WNDPROC, (LONG)WMCOPYWNDPROC );
288     SetWindowLong( ipcwindow, GWL_USERDATA, (LONG)p_this );
289
290     /* Signal the creation of the thread and events queue */
291     vlc_thread_ready( p_this );
292
293     while( GetMessage( &message, NULL, 0, 0 ) )
294     {
295         TranslateMessage( &message );
296         DispatchMessage( &message );
297     }
298 }
299
300 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
301                                 LPARAM lParam )
302 {
303     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 *)GetWindowLong( hwnd, GWL_USERDATA );
310
311         if( !p_this ) return 0;
312
313         /* Add files to the playlist */
314         p_playlist = (playlist_t *)vlc_object_find( p_this,
315                                                     VLC_OBJECT_PLAYLIST,
316                                                     FIND_ANYWHERE );
317         if( !p_playlist ) return 0;
318
319         if( pwm_data->lpData )
320         {
321             char **ppsz_argv;
322             vlc_ipc_data_t *p_data = (vlc_ipc_data_t *)pwm_data->lpData;
323             size_t i_data = 0;
324             int i_argc = p_data->argc, i_opt, i_options;
325
326             ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
327             for( i_opt = 0; i_opt < i_argc; i_opt++ )
328             {
329                 ppsz_argv[i_opt] = p_data->data + i_data + sizeof(int);
330                 i_data += sizeof(int) + *((int *)(p_data->data + i_data));
331             }
332
333             for( i_opt = 0; i_opt < i_argc; i_opt++ )
334             {
335                 i_options = 0;
336
337                 /* Count the input options */
338                 while( i_opt + i_options + 1 < i_argc &&
339                        *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
340                 {
341                     i_options++;
342                 }
343                 playlist_AddExt( p_playlist, ppsz_argv[i_opt],
344                   NULL, PLAYLIST_APPEND |
345                         ( ( i_opt || p_data->enqueue ) ? 0 : PLAYLIST_GO ),
346                   PLAYLIST_END, -1,
347                   (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
348                   i_options, VLC_TRUE, VLC_FALSE );
349
350                 i_opt += i_options;
351             }
352
353             free( ppsz_argv );
354         }
355
356         vlc_object_release( p_playlist );
357     }
358
359     return DefWindowProc( hwnd, uMsg, wParam, lParam );
360 }
361
362 /*****************************************************************************
363  * system_End: terminate winsock.
364  *****************************************************************************/
365 void system_End( libvlc_int_t *p_this )
366 {
367     if( p_this && vlc_global() )
368     {
369         free( vlc_global()->psz_vlcpath );
370         vlc_global()->psz_vlcpath = NULL;
371     }
372
373     WSACleanup();
374 }
375
376 /*****************************************************************************
377  *  system_VLCPath
378  *  **************************************************************************/
379 const char* system_VLCPath( void )
380 {
381     libvlc_global_data_t* libvlc_global = vlc_global();
382     return libvlc_global->psz_vlcpath;
383 }