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