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