]> git.sesse.net Git - vlc/blob - src/misc/win32_specific.c
* src/misc/win32_specific.c: mozilla plugin fix.
[vlc] / src / misc / win32_specific.c
1 /*****************************************************************************
2  * win32_specific.c: Win32 specific features
3  *****************************************************************************
4  * Copyright (C) 2001-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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
31 #ifdef WIN32                       /* optind, getopt(), included in unistd.h */
32 #   include "../extras/getopt.h"
33 #endif
34
35 #if !defined( UNDER_CE )
36 #   include <io.h>
37 #   include <fcntl.h>
38 #endif
39
40 #include <winsock2.h>
41
42 /*****************************************************************************
43  * system_Init: initialize winsock and misc other things.
44  *****************************************************************************/
45 void system_Init( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
46 {
47     WSADATA Data;
48
49     /* Get our full path */
50     char psz_path[MAX_PATH];
51     char *psz_vlc;
52
53 #if defined( UNDER_CE )
54     wchar_t psz_wpath[MAX_PATH];
55     if( GetModuleFileName( NULL, psz_wpath, MAX_PATH ) )
56     {
57         WideCharToMultiByte( CP_ACP, 0, psz_wpath, -1,
58                              psz_path, MAX_PATH, NULL, NULL );
59     }
60     else psz_path[0] = '\0';
61
62 #else
63     if( ppsz_argv[0] )
64     {
65         GetFullPathName( ppsz_argv[0], MAX_PATH, psz_path, &psz_vlc );
66     }
67     else if( !GetModuleFileName( NULL, psz_path, MAX_PATH ) )
68     {
69         psz_path[0] = '\0';
70     }
71 #endif
72
73     if( (psz_vlc = strrchr( psz_path, '\\' )) ) *psz_vlc = '\0';
74
75     p_this->p_libvlc->psz_vlcpath = strdup( psz_path );
76
77     /* Set the default file-translation mode */
78 #if !defined( UNDER_CE )
79     _fmode = _O_BINARY;
80 #endif
81     _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
82
83     /* Call mdate() once to make sure it is initialized properly */
84     mdate();
85
86     /* WinSock Library Init. */
87     if( !WSAStartup( MAKEWORD( 2, 0 ), &Data ) )
88     {
89         /* Confirm that the WinSock DLL supports 2.0.*/
90         if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 0 )
91         {
92             /* We could not find a suitable WinSock DLL. */
93             WSACleanup( );
94         }
95         else
96         {
97             /* Everything went ok. */
98             return;
99         }
100     }
101
102     /* Let's try with WinSock 1.1 */
103     if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
104     {
105         /* Confirm that the WinSock DLL supports 1.1.*/
106         if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
107         {
108             /* We could not find a suitable WinSock DLL. */
109             WSACleanup( );
110         }
111         else
112         {
113             /* Everything went ok. */
114             return;
115         }
116     }
117
118     fprintf( stderr, "error: can't initialize WinSocks\n" );
119
120     return;
121 }
122
123 /*****************************************************************************
124  * system_Configure: check for system specific configuration options.
125  *****************************************************************************/
126 static void IPCHelperThread( vlc_object_t * );
127 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
128
129 void system_Configure( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
130 {
131 #if !defined( UNDER_CE )
132     p_this->p_libvlc->b_fast_mutex = config_GetInt( p_this, "fast-mutex" );
133     p_this->p_libvlc->i_win9x_cv = config_GetInt( p_this, "win9x-cv-method" );
134
135     /* Raise default priority of the current process */
136 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
137 #   define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
138 #endif
139     if( config_GetInt( p_this, "high-priority" ) )
140     {
141         if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
142              || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
143         {
144             msg_Dbg( p_this, "raised process priority" );
145         }
146         else
147         {
148             msg_Dbg( p_this, "could not raise process priority" );
149         }
150     }
151
152     if( config_GetInt( p_this, "one-instance" ) )
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( NULL, TRUE, "VLC ipc "VERSION ) ) == NULL )
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             vlc_object_t *p_helper =
172              (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
173
174             /* Run the helper thread */
175             if( vlc_thread_create( p_helper, "IPC helper", IPCHelperThread,
176                                    VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
177             {
178                 msg_Err( p_this, "one instance mode DISABLED "
179                          "(IPC helper thread couldn't be created)" );
180
181             }
182
183             /* Initialization done.
184              * Release the mutex to unblock other instances */
185             ReleaseMutex( hmutex );
186         }
187         else
188         {
189             /* Another instance is running */
190
191             HWND ipcwindow;
192
193             /* Wait until the 1st instance is initialized */
194             WaitForSingleObject( hmutex, INFINITE );
195
196             /* Locate the window created by the IPC helper thread of the
197              * 1st instance */
198             if( ( ipcwindow = FindWindow( NULL, "VLC ipc "VERSION ) )
199                 == NULL )
200             {
201                 msg_Err( p_this, "one instance mode DISABLED "
202                          "(couldn't find 1st instance of program)" );
203                 ReleaseMutex( hmutex );
204                 return;
205             }
206
207             /* We assume that the remaining parameters are filenames
208              * and their input options */
209             if( *pi_argc - 1 >= optind )
210             {
211                 COPYDATASTRUCT wm_data;
212                 int i_opt, i_data;
213                 char *p_data;
214
215                 i_data = sizeof(int);
216                 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
217                 {
218                     i_data += sizeof(int);
219                     i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
220                 }
221
222                 p_data = (char *)malloc( i_data );
223                 *((int *)&p_data[0]) = *pi_argc - optind;
224                 i_data = sizeof(int);
225                 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
226                 {
227                     int i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
228                     *((int *)&p_data[i_data]) = i_len;
229                     i_data += sizeof(int);
230                     memcpy( &p_data[i_data], ppsz_argv[ i_opt ], i_len );
231                     i_data += i_len;
232                 }
233
234                 /* Send our playlist items to the 1st instance */
235                 wm_data.dwData = 0;
236                 wm_data.cbData = i_data;
237                 wm_data.lpData = p_data;
238                 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
239             }
240
241             /* Initialization done.
242              * Release the mutex to unblock other instances */
243             ReleaseMutex( hmutex );
244
245             /* Bye bye */
246             system_End( p_this );
247             exit( 0 );
248         }
249     }
250
251 #endif
252 }
253
254 static void IPCHelperThread( vlc_object_t *p_this )
255 {
256     HWND ipcwindow;
257     MSG message;
258
259     ipcwindow =
260         CreateWindow( "STATIC",                      /* name of window class */
261                   "VLC ipc "VERSION,                /* window title bar text */
262                   0,                                         /* window style */
263                   0,                                 /* default X coordinate */
264                   0,                                 /* default Y coordinate */
265                   0,                                         /* window width */
266                   0,                                        /* window height */
267                   NULL,                                  /* no parent window */
268                   NULL,                            /* no menu in this window */
269                   GetModuleHandle(NULL),  /* handle of this program instance */
270                   NULL );                               /* sent to WM_CREATE */
271
272     SetWindowLong( ipcwindow, GWL_WNDPROC, (LONG)WMCOPYWNDPROC );
273     SetWindowLong( ipcwindow, GWL_USERDATA, (LONG)p_this );
274
275     /* Signal the creation of the thread and events queue */
276     vlc_thread_ready( p_this );
277
278     while( GetMessage( &message, NULL, 0, 0 ) )
279     {
280         TranslateMessage( &message );
281         DispatchMessage( &message );
282     }
283 }
284
285 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
286                                 LPARAM lParam )
287 {
288     if( uMsg == WM_COPYDATA )
289     {
290         COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
291         vlc_object_t *p_this;
292         playlist_t *p_playlist;
293
294         p_this = (vlc_object_t *)GetWindowLong( hwnd, GWL_USERDATA );
295
296         if( !p_this ) return 0;
297
298         /* Add files to the playlist */
299         p_playlist = (playlist_t *)vlc_object_find( p_this,
300                                                     VLC_OBJECT_PLAYLIST,
301                                                     FIND_ANYWHERE );
302         if( !p_playlist ) return 0;
303
304         if( pwm_data->lpData )
305         {
306             int i_argc, i_data, i_opt, i_options;
307             char **ppsz_argv;
308             char *p_data = (char *)pwm_data->lpData;
309
310             i_argc = *((int *)&p_data[0]);
311             ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
312             i_data = sizeof(int);
313             for( i_opt = 0; i_opt < i_argc; i_opt++ )
314             {
315                 ppsz_argv[i_opt] = &p_data[i_data + sizeof(int)];
316                 i_data += *((int *)&p_data[i_data]);
317                 i_data += sizeof(int);
318             }
319
320             for( i_opt = 0; i_opt < i_argc; i_opt++ )
321             {
322                 i_options = 0;
323
324                 /* Count the input options */
325                 while( i_opt + i_options + 1 < i_argc &&
326                        *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
327                 {
328                     i_options++;
329                 }
330
331                 playlist_AddExt( p_playlist, ppsz_argv[i_opt],ppsz_argv[i_opt],
332                     PLAYLIST_APPEND | (i_opt? 0 : PLAYLIST_GO),
333                     PLAYLIST_END, -1,
334                     (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
335                     i_options );
336
337                 i_opt += i_options;
338             }
339
340             free( ppsz_argv );
341         }
342
343         vlc_object_release( p_playlist );
344     }
345
346     return DefWindowProc( hwnd, uMsg, wParam, lParam );
347 }
348
349 /*****************************************************************************
350  * system_End: terminate winsock.
351  *****************************************************************************/
352 void system_End( vlc_t *p_this )
353 {
354     WSACleanup();
355 }