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