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