]> git.sesse.net Git - vlc/blob - src/misc/win32_specific.c
Fix running vlc from path in windows (see http://forum.videolan.org/viewtopic.php...
[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
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 <winsock.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( !GetModuleFileName( NULL, psz_path, MAX_PATH ) )
64     {
65         psz_path[0] = '\0';
66     }
67 #endif
68
69     if( (psz_vlc = strrchr( psz_path, '\\' )) ) *psz_vlc = '\0';
70
71     p_this->p_libvlc->psz_vlcpath = strdup( psz_path );
72
73     /* Set the default file-translation mode */
74 #if !defined( UNDER_CE )
75     _fmode = _O_BINARY;
76     _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
77 #endif
78
79     /* Call mdate() once to make sure it is initialized properly */
80     mdate();
81
82     /* WinSock Library Init. */
83     if( !WSAStartup( MAKEWORD( 2, 0 ), &Data ) )
84     {
85         /* Confirm that the WinSock DLL supports 2.0.*/
86         if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 0 )
87         {
88             /* We could not find a suitable WinSock DLL. */
89             WSACleanup( );
90         }
91         else
92         {
93             /* Everything went ok. */
94             return;
95         }
96     }
97
98     /* Let's try with WinSock 1.1 */
99     if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
100     {
101         /* Confirm that the WinSock DLL supports 1.1.*/
102         if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
103         {
104             /* We could not find a suitable WinSock DLL. */
105             WSACleanup( );
106         }
107         else
108         {
109             /* Everything went ok. */
110             return;
111         }
112     }
113
114     fprintf( stderr, "error: can't initialize WinSocks\n" );
115
116     return;
117 }
118
119 /*****************************************************************************
120  * system_Configure: check for system specific configuration options.
121  *****************************************************************************/
122 static void IPCHelperThread( vlc_object_t * );
123 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
124
125 void system_Configure( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
126 {
127 #if !defined( UNDER_CE )
128     p_this->p_libvlc->b_fast_mutex = config_GetInt( p_this, "fast-mutex" );
129     p_this->p_libvlc->i_win9x_cv = config_GetInt( p_this, "win9x-cv-method" );
130
131     /* Raise default priority of the current process */
132 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
133 #   define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
134 #endif
135     if( config_GetInt( p_this, "high-priority" ) )
136     {
137         if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
138              || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
139         {
140             msg_Dbg( p_this, "raised process priority" );
141         }
142         else
143         {
144             msg_Dbg( p_this, "could not raise process priority" );
145         }
146     }
147
148     if( config_GetInt( p_this, "one-instance" ) )
149     {
150         HANDLE hmutex;
151
152         msg_Info( p_this, "one instance mode ENABLED");
153
154         /* Use a named mutex to check if another instance is already running */
155         if( !( hmutex = CreateMutex( 0, TRUE, _T("VLC ipc ") _T(VERSION) ) ) )
156         {
157             /* Failed for some reason. Just ignore the option and go on as
158              * normal. */
159             msg_Err( p_this, "one instance mode DISABLED "
160                      "(mutex couldn't be created)" );
161             return;
162         }
163
164         if( GetLastError() != ERROR_ALREADY_EXISTS )
165         {
166             /* We are the 1st instance. */
167             vlc_object_t *p_helper =
168              (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
169
170             /* Run the helper thread */
171             if( vlc_thread_create( p_helper, "IPC helper", IPCHelperThread,
172                                    VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
173             {
174                 msg_Err( p_this, "one instance mode DISABLED "
175                          "(IPC helper thread couldn't be created)" );
176
177             }
178
179             /* Initialization done.
180              * Release the mutex to unblock other instances */
181             ReleaseMutex( hmutex );
182         }
183         else
184         {
185             /* Another instance is running */
186
187             HWND ipcwindow;
188
189             /* Wait until the 1st instance is initialized */
190             WaitForSingleObject( hmutex, INFINITE );
191
192             /* Locate the window created by the IPC helper thread of the
193              * 1st instance */
194             if( !( ipcwindow = FindWindow( 0, _T("VLC ipc ") _T(VERSION) ) ) )
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( _T("STATIC"),                  /* name of window class */
256                   _T("VLC ipc ") _T(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                 if( i_opt || config_GetInt( p_this, "playlist-enqueue" ) )
326                 {
327                   playlist_AddExt( p_playlist, ppsz_argv[i_opt],
328                     ppsz_argv[i_opt], PLAYLIST_APPEND ,
329                     PLAYLIST_END, -1,
330                     (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
331                     i_options );
332                 } else {
333                   playlist_AddExt( p_playlist, ppsz_argv[i_opt],
334                     ppsz_argv[i_opt], PLAYLIST_APPEND | PLAYLIST_GO,
335                     PLAYLIST_END, -1,
336                     (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
337                     i_options );
338                 }
339
340                 i_opt += i_options;
341             }
342
343             free( ppsz_argv );
344         }
345
346         vlc_object_release( p_playlist );
347     }
348
349     return DefWindowProc( hwnd, uMsg, wParam, lParam );
350 }
351
352 /*****************************************************************************
353  * system_End: terminate winsock.
354  *****************************************************************************/
355 void system_End( vlc_t *p_this )
356 {
357     if( p_this && p_this->p_libvlc && p_this->p_libvlc->psz_vlcpath )
358     {
359         free( p_this->p_libvlc->psz_vlcpath );
360         p_this->p_libvlc->psz_vlcpath = NULL;
361     }
362
363     WSACleanup();
364 }