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