]> git.sesse.net Git - vlc/blob - src/misc/win32_specific.c
4565f1591a6d7d68ae1b84ce112ec0c576eb7844
[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 #include <string.h>                                              /* strdup() */
25 #include <stdlib.h>                                                /* free() */
26
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29 #include "vlc_playlist.h"
30 #include "charset.h"
31
32 #ifdef WIN32                       /* optind, getopt(), included in unistd.h */
33 #   include "../extras/getopt.h"
34 #endif
35
36 #if !defined( UNDER_CE )
37 #   include <io.h>
38 #   include <fcntl.h>
39 #endif
40
41 #include <winsock.h>
42
43 extern void __wgetmainargs(int *argc, wchar_t ***wargv, wchar_t ***wenviron,
44                            int expand_wildcards, int *startupinfo);
45
46 /*****************************************************************************
47  * system_Init: initialize winsock and misc other things.
48  *****************************************************************************/
49 void system_Init( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
50 {
51     WSADATA Data;
52
53     /* Get our full path */
54     char psz_path[MAX_PATH];
55     char *psz_vlc;
56
57 #if defined( UNDER_CE )
58     wchar_t psz_wpath[MAX_PATH];
59     if( GetModuleFileName( NULL, psz_wpath, MAX_PATH ) )
60     {
61         WideCharToMultiByte( CP_ACP, 0, psz_wpath, -1,
62                              psz_path, MAX_PATH, NULL, NULL );
63     }
64     else psz_path[0] = '\0';
65
66 #else
67     if( ppsz_argv[0] )
68     {
69         GetFullPathName( ppsz_argv[0], MAX_PATH, psz_path, &psz_vlc );
70     }
71     else if( !GetModuleFileName( NULL, psz_path, MAX_PATH ) )
72     {
73         psz_path[0] = '\0';
74     }
75 #endif
76
77     if( (psz_vlc = strrchr( psz_path, '\\' )) ) *psz_vlc = '\0';
78
79     p_this->p_libvlc_global->psz_vlcpath = strdup( psz_path );
80
81     /* Set the default file-translation mode */
82 #if !defined( UNDER_CE )
83     _fmode = _O_BINARY;
84     _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
85 #endif
86
87     /* Call mdate() once to make sure it is initialized properly */
88     mdate();
89
90     /* Replace argv[1..n] with unicode for Windows NT and above */
91     if( GetVersion() < 0x80000000 )
92     {
93         wchar_t **wargv, **wenvp;
94         int i,i_wargc;
95         int si = { 0 };
96         __wgetmainargs(&i_wargc, &wargv, &wenvp, 0, &si);
97
98         for( i = 1; i < i_wargc; i++ )
99             ppsz_argv[i] = FromWide( wargv[i] );
100     }
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
136 void system_Configure( libvlc_int_t *p_this, int *pi_argc, char *ppsz_argv[] )
137 {
138 #if !defined( UNDER_CE )
139     p_this->p_libvlc_global->b_fast_mutex = config_GetInt( p_this, "fast-mutex" );
140     p_this->p_libvlc_global->i_win9x_cv = config_GetInt( p_this, "win9x-cv-method" );
141
142     /* Raise default priority of the current process */
143 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
144 #   define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
145 #endif
146     if( config_GetInt( p_this, "high-priority" ) )
147     {
148         if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
149              || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
150         {
151             msg_Dbg( p_this, "raised process priority" );
152         }
153         else
154         {
155             msg_Dbg( p_this, "could not raise process priority" );
156         }
157     }
158
159     if( config_GetInt( p_this, "one-instance" )
160         || ( config_GetInt( p_this, "one-instance-when-started-from-file" )
161              && config_GetInt( p_this, "started-from-file" ) ) )
162     {
163         HANDLE hmutex;
164
165         msg_Info( p_this, "one instance mode ENABLED");
166
167         /* Use a named mutex to check if another instance is already running */
168         if( !( hmutex = CreateMutex( 0, TRUE, _T("VLC ipc ") _T(VERSION) ) ) )
169         {
170             /* Failed for some reason. Just ignore the option and go on as
171              * normal. */
172             msg_Err( p_this, "one instance mode DISABLED "
173                      "(mutex couldn't be created)" );
174             return;
175         }
176
177         if( GetLastError() != ERROR_ALREADY_EXISTS )
178         {
179             /* We are the 1st instance. */
180             vlc_object_t *p_helper =
181              (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
182
183             /* Run the helper thread */
184             if( vlc_thread_create( p_helper, "IPC helper", IPCHelperThread,
185                                    VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
186             {
187                 msg_Err( p_this, "one instance mode DISABLED "
188                          "(IPC helper thread couldn't be created)" );
189
190             }
191
192             /* Initialization done.
193              * Release the mutex to unblock other instances */
194             ReleaseMutex( hmutex );
195         }
196         else
197         {
198             /* Another instance is running */
199
200             HWND ipcwindow;
201
202             /* Wait until the 1st instance is initialized */
203             WaitForSingleObject( hmutex, INFINITE );
204
205             /* Locate the window created by the IPC helper thread of the
206              * 1st instance */
207             if( !( ipcwindow = FindWindow( 0, _T("VLC ipc ") _T(VERSION) ) ) )
208             {
209                 msg_Err( p_this, "one instance mode DISABLED "
210                          "(couldn't find 1st instance of program)" );
211                 ReleaseMutex( hmutex );
212                 return;
213             }
214
215             /* We assume that the remaining parameters are filenames
216              * and their input options */
217             if( *pi_argc - 1 >= optind )
218             {
219                 COPYDATASTRUCT wm_data;
220                 int i_opt, i_data;
221                 char *p_data;
222
223                 i_data = sizeof(int);
224                 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
225                 {
226                     i_data += sizeof(int);
227                     i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
228                 }
229
230                 p_data = (char *)malloc( i_data );
231                 *((int *)&p_data[0]) = *pi_argc - optind;
232                 i_data = sizeof(int);
233                 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
234                 {
235                     int i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
236                     *((int *)&p_data[i_data]) = i_len;
237                     i_data += sizeof(int);
238                     memcpy( &p_data[i_data], ppsz_argv[ i_opt ], i_len );
239                     i_data += i_len;
240                 }
241
242                 /* Send our playlist items to the 1st instance */
243                 wm_data.dwData = 0;
244                 wm_data.cbData = i_data;
245                 wm_data.lpData = p_data;
246                 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
247             }
248
249             /* Initialization done.
250              * Release the mutex to unblock other instances */
251             ReleaseMutex( hmutex );
252
253             /* Bye bye */
254             system_End( p_this );
255             exit( 0 );
256         }
257     }
258
259 #endif
260 }
261
262 static void IPCHelperThread( vlc_object_t *p_this )
263 {
264     HWND ipcwindow;
265     MSG message;
266
267     ipcwindow =
268         CreateWindow( _T("STATIC"),                  /* name of window class */
269                   _T("VLC ipc ") _T(VERSION),       /* window title bar text */
270                   0,                                         /* window style */
271                   0,                                 /* default X coordinate */
272                   0,                                 /* default Y coordinate */
273                   0,                                         /* window width */
274                   0,                                        /* window height */
275                   NULL,                                  /* no parent window */
276                   NULL,                            /* no menu in this window */
277                   GetModuleHandle(NULL),  /* handle of this program instance */
278                   NULL );                               /* sent to WM_CREATE */
279
280     SetWindowLong( ipcwindow, GWL_WNDPROC, (LONG)WMCOPYWNDPROC );
281     SetWindowLong( ipcwindow, GWL_USERDATA, (LONG)p_this );
282
283     /* Signal the creation of the thread and events queue */
284     vlc_thread_ready( p_this );
285
286     while( GetMessage( &message, NULL, 0, 0 ) )
287     {
288         TranslateMessage( &message );
289         DispatchMessage( &message );
290     }
291 }
292
293 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
294                                 LPARAM lParam )
295 {
296     if( uMsg == WM_COPYDATA )
297     {
298         COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
299         vlc_object_t *p_this;
300         playlist_t *p_playlist;
301
302         p_this = (vlc_object_t *)GetWindowLong( hwnd, GWL_USERDATA );
303
304         if( !p_this ) return 0;
305
306         /* Add files to the playlist */
307         p_playlist = (playlist_t *)vlc_object_find( p_this,
308                                                     VLC_OBJECT_PLAYLIST,
309                                                     FIND_ANYWHERE );
310         if( !p_playlist ) return 0;
311
312         if( pwm_data->lpData )
313         {
314             int i_argc, i_data, i_opt, i_options;
315             char **ppsz_argv;
316             char *p_data = (char *)pwm_data->lpData;
317
318             i_argc = *((int *)&p_data[0]);
319             ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
320             i_data = sizeof(int);
321             for( i_opt = 0; i_opt < i_argc; i_opt++ )
322             {
323                 ppsz_argv[i_opt] = &p_data[i_data + sizeof(int)];
324                 i_data += *((int *)&p_data[i_data]);
325                 i_data += sizeof(int);
326             }
327
328             for( i_opt = 0; i_opt < i_argc; i_opt++ )
329             {
330                 i_options = 0;
331
332                 /* Count the input options */
333                 while( i_opt + i_options + 1 < i_argc &&
334                        *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
335                 {
336                     i_options++;
337                 }
338                 if( i_opt || config_GetInt( p_this, "playlist-enqueue" ) )
339                 {
340                   playlist_AddExt( p_playlist, ppsz_argv[i_opt],
341                     NULL, PLAYLIST_APPEND ,
342                     PLAYLIST_END, -1,
343                     (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
344                     i_options, VLC_TRUE );
345                 } else {
346                   playlist_AddExt( p_playlist, ppsz_argv[i_opt],
347                     NULL, PLAYLIST_APPEND | PLAYLIST_GO,
348                     PLAYLIST_END, -1,
349                     (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
350                     i_options, VLC_TRUE );
351                 }
352
353                 i_opt += i_options;
354             }
355
356             free( ppsz_argv );
357         }
358
359         vlc_object_release( p_playlist );
360     }
361
362     return DefWindowProc( hwnd, uMsg, wParam, lParam );
363 }
364
365 /*****************************************************************************
366  * system_End: terminate winsock.
367  *****************************************************************************/
368 void system_End( libvlc_int_t *p_this )
369 {
370     if( p_this && p_this->p_libvlc_global && p_this->p_libvlc_global->psz_vlcpath )
371     {
372         free( p_this->p_libvlc_global->psz_vlcpath );
373         p_this->p_libvlc_global->psz_vlcpath = NULL;
374     }
375
376     WSACleanup();
377 }