]> git.sesse.net Git - vlc/blob - src/misc/win32_specific.c
* libvlc.h: hopefully fix a few warnings.
[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     /* Raise default priority of the current process */
125 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
126 #   define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
127 #endif
128     if( config_GetInt( p_this, "high-priority" ) )
129     {
130         if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
131              || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
132         {
133             msg_Dbg( p_this, "raised process priority" );
134         }
135         else
136         {
137             msg_Dbg( p_this, "could not raise process priority" );
138         }
139     }
140
141     if( config_GetInt( p_this, "one-instance" )
142         || ( config_GetInt( p_this, "one-instance-when-started-from-file" )
143              && config_GetInt( p_this, "started-from-file" ) ) )
144     {
145         HANDLE hmutex;
146
147         msg_Info( p_this, "one instance mode ENABLED");
148
149         /* Use a named mutex to check if another instance is already running */
150         if( !( hmutex = CreateMutex( 0, TRUE, _T("VLC ipc ") _T(VERSION) ) ) )
151         {
152             /* Failed for some reason. Just ignore the option and go on as
153              * normal. */
154             msg_Err( p_this, "one instance mode DISABLED "
155                      "(mutex couldn't be created)" );
156             return;
157         }
158
159         if( GetLastError() != ERROR_ALREADY_EXISTS )
160         {
161             /* We are the 1st instance. */
162             vlc_object_t *p_helper =
163              (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
164
165             /* Run the helper thread */
166             if( vlc_thread_create( p_helper, "IPC helper", IPCHelperThread,
167                                    VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
168             {
169                 msg_Err( p_this, "one instance mode DISABLED "
170                          "(IPC helper thread couldn't be created)" );
171
172             }
173
174             /* Initialization done.
175              * Release the mutex to unblock other instances */
176             ReleaseMutex( hmutex );
177         }
178         else
179         {
180             /* Another instance is running */
181
182             HWND ipcwindow;
183
184             /* Wait until the 1st instance is initialized */
185             WaitForSingleObject( hmutex, INFINITE );
186
187             /* Locate the window created by the IPC helper thread of the
188              * 1st instance */
189             if( !( ipcwindow = FindWindow( 0, _T("VLC ipc ") _T(VERSION) ) ) )
190             {
191                 msg_Err( p_this, "one instance mode DISABLED "
192                          "(couldn't find 1st instance of program)" );
193                 ReleaseMutex( hmutex );
194                 return;
195             }
196
197             /* We assume that the remaining parameters are filenames
198              * and their input options */
199             if( *pi_argc - 1 >= optind )
200             {
201                 COPYDATASTRUCT wm_data;
202                 int i_opt, i_data;
203                 char *p_data;
204
205                 i_data = sizeof(int);
206                 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
207                 {
208                     i_data += sizeof(int);
209                     i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
210                 }
211
212                 p_data = (char *)malloc( i_data );
213                 *((int *)&p_data[0]) = *pi_argc - optind;
214                 i_data = sizeof(int);
215                 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
216                 {
217                     int i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
218                     *((int *)&p_data[i_data]) = i_len;
219                     i_data += sizeof(int);
220                     memcpy( &p_data[i_data], ppsz_argv[ i_opt ], i_len );
221                     i_data += i_len;
222                 }
223
224                 /* Send our playlist items to the 1st instance */
225                 wm_data.dwData = 0;
226                 wm_data.cbData = i_data;
227                 wm_data.lpData = p_data;
228                 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
229             }
230
231             /* Initialization done.
232              * Release the mutex to unblock other instances */
233             ReleaseMutex( hmutex );
234
235             /* Bye bye */
236             system_End( p_this );
237             exit( 0 );
238         }
239     }
240
241 #endif
242 }
243
244 static void IPCHelperThread( vlc_object_t *p_this )
245 {
246     HWND ipcwindow;
247     MSG message;
248
249     ipcwindow =
250         CreateWindow( _T("STATIC"),                  /* name of window class */
251                   _T("VLC ipc ") _T(VERSION),       /* window title bar text */
252                   0,                                         /* window style */
253                   0,                                 /* default X coordinate */
254                   0,                                 /* default Y coordinate */
255                   0,                                         /* window width */
256                   0,                                        /* window height */
257                   NULL,                                  /* no parent window */
258                   NULL,                            /* no menu in this window */
259                   GetModuleHandle(NULL),  /* handle of this program instance */
260                   NULL );                               /* sent to WM_CREATE */
261
262     SetWindowLong( ipcwindow, GWL_WNDPROC, (LONG)WMCOPYWNDPROC );
263     SetWindowLong( ipcwindow, GWL_USERDATA, (LONG)p_this );
264
265     /* Signal the creation of the thread and events queue */
266     vlc_thread_ready( p_this );
267
268     while( GetMessage( &message, NULL, 0, 0 ) )
269     {
270         TranslateMessage( &message );
271         DispatchMessage( &message );
272     }
273 }
274
275 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
276                                 LPARAM lParam )
277 {
278     if( uMsg == WM_COPYDATA )
279     {
280         COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
281         vlc_object_t *p_this;
282         playlist_t *p_playlist;
283
284         p_this = (vlc_object_t *)GetWindowLong( hwnd, GWL_USERDATA );
285
286         if( !p_this ) return 0;
287
288         /* Add files to the playlist */
289         p_playlist = (playlist_t *)vlc_object_find( p_this,
290                                                     VLC_OBJECT_PLAYLIST,
291                                                     FIND_ANYWHERE );
292         if( !p_playlist ) return 0;
293
294         if( pwm_data->lpData )
295         {
296             int i_argc, i_data, i_opt, i_options;
297             char **ppsz_argv;
298             char *p_data = (char *)pwm_data->lpData;
299
300             i_argc = *((int *)&p_data[0]);
301             ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
302             i_data = sizeof(int);
303             for( i_opt = 0; i_opt < i_argc; i_opt++ )
304             {
305                 ppsz_argv[i_opt] = &p_data[i_data + sizeof(int)];
306                 i_data += *((int *)&p_data[i_data]);
307                 i_data += sizeof(int);
308             }
309
310             for( i_opt = 0; i_opt < i_argc; i_opt++ )
311             {
312                 i_options = 0;
313
314                 /* Count the input options */
315                 while( i_opt + i_options + 1 < i_argc &&
316                        *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
317                 {
318                     i_options++;
319                 }
320                 if( i_opt || config_GetInt( p_this, "playlist-enqueue" ) )
321                 {
322                   playlist_AddExt( p_playlist, ppsz_argv[i_opt],
323                     NULL, PLAYLIST_APPEND ,
324                     PLAYLIST_END, -1,
325                     (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
326                     i_options, VLC_TRUE, VLC_FALSE );
327                 } else {
328                   playlist_AddExt( p_playlist, ppsz_argv[i_opt],
329                     NULL, PLAYLIST_APPEND | PLAYLIST_GO,
330                     PLAYLIST_END, -1,
331                     (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
332                     i_options, VLC_TRUE, VLC_FALSE );
333                 }
334
335                 i_opt += i_options;
336             }
337
338             free( ppsz_argv );
339         }
340
341         vlc_object_release( p_playlist );
342     }
343
344     return DefWindowProc( hwnd, uMsg, wParam, lParam );
345 }
346
347 /*****************************************************************************
348  * system_End: terminate winsock.
349  *****************************************************************************/
350 void system_End( libvlc_int_t *p_this )
351 {
352     if( p_this && p_this->p_libvlc_global && vlc_global( p_this )->psz_vlcpath )
353     {
354         free( vlc_global( p_this )->psz_vlcpath );
355         vlc_global( p_this )->psz_vlcpath = NULL;
356     }
357
358     WSACleanup();
359 }