]> git.sesse.net Git - vlc/blob - src/misc/win32_specific.c
ALL: MSVC compilation fixes to libvlc.
[vlc] / src / misc / win32_specific.c
1 /*****************************************************************************
2  * win32_specific.c: Win32 specific features
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: win32_specific.c,v 1.27 2003/12/02 12:57:36 gbazin Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Gildas Bazin <gbazin@netcourrier.com>
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_playlist.h"
29
30 #ifdef WIN32                       /* optind, getopt(), included in unistd.h */
31 #   include "../extras/getopt.h"
32 #endif
33
34 #if !defined( UNDER_CE )
35 #   include <io.h>
36 #   include <fcntl.h>
37 #   include <winsock2.h>
38 #endif
39
40 /*****************************************************************************
41  * system_Init: initialize winsock and misc other things.
42  *****************************************************************************/
43 void system_Init( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
44 {
45 #if !defined( UNDER_CE )
46     WSADATA Data;
47
48     /* Get our full path */
49     if( ppsz_argv[0] )
50     {
51         char psz_path[MAX_PATH];
52         char *psz_vlc;
53
54         GetFullPathName( ppsz_argv[0], MAX_PATH, psz_path, &psz_vlc );
55
56         if( psz_vlc > psz_path && psz_vlc[-1] == '\\' )
57         {
58             psz_vlc[-1] = '\0';
59             p_this->p_libvlc->psz_vlcpath = strdup( psz_path );
60         }
61         else
62         {
63             p_this->p_libvlc->psz_vlcpath = strdup( "" );
64         }
65     }
66     else
67     {
68         p_this->p_libvlc->psz_vlcpath = strdup( "" );
69     }
70
71     /* Set the default file-translation mode */
72     _fmode = _O_BINARY;
73     _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
74
75     /* WinSock Library Init. */
76     if( !WSAStartup( MAKEWORD( 2, 0 ), &Data ) )
77     {
78         /* Confirm that the WinSock DLL supports 2.0.*/
79         if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 0 )
80         {
81             /* We could not find a suitable WinSock DLL. */
82             WSACleanup( );
83         }
84         else
85         {
86             /* Everything went ok. */
87             return;
88         }
89     }
90
91     /* Let's try with WinSock 1.1 */
92     if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
93     {
94         /* Confirm that the WinSock DLL supports 1.1.*/
95         if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
96         {
97             /* We could not find a suitable WinSock DLL. */
98             WSACleanup( );
99         }
100         else
101         {
102             /* Everything went ok. */
103             return;
104         }
105     }
106
107     fprintf( stderr, "error: can't initialize WinSocks\n" );
108
109     return; 
110
111 #endif
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     {
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( NULL, TRUE, "VLC ipc "VERSION ) ) == NULL )
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( NULL, "VLC ipc "VERSION ) )
190                 == NULL )
191             {
192                 msg_Err( p_this, "One instance mode DISABLED "
193                          "(couldn't find 1st instance of program)" );
194                 ReleaseMutex( hmutex );
195                 return;
196             }
197
198             /* We assume that the remaining parameters are filenames
199              * and their input options */
200             if( *pi_argc - 1 >= optind )
201             {
202                 COPYDATASTRUCT wm_data;
203                 int i_opt, i_data;
204                 char *p_data;
205
206                 i_data = sizeof(int);
207                 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
208                 {
209                     i_data += sizeof(int);
210                     i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
211                 }
212
213                 p_data = (char *)malloc( i_data );
214                 *((int *)&p_data[0]) = *pi_argc - optind;
215                 i_data = sizeof(int);
216                 for( i_opt = optind; i_opt < *pi_argc; i_opt++ )
217                 {
218                     int i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
219                     *((int *)&p_data[i_data]) = i_len;
220                     i_data += sizeof(int);
221                     memcpy( &p_data[i_data], ppsz_argv[ i_opt ], i_len );
222                     i_data += i_len;
223                 }
224
225                 /* Send our playlist items to the 1st instance */
226                 wm_data.dwData = 0;
227                 wm_data.cbData = i_data;
228                 wm_data.lpData = p_data;
229                 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
230             }
231
232             /* Initialization done.
233              * Release the mutex to unblock other instances */
234             ReleaseMutex( hmutex );
235
236             /* Bye bye */
237             system_End( p_this );
238             exit( 0 );
239         }
240     }
241
242 #endif
243 }
244
245 static void IPCHelperThread( vlc_object_t *p_this )
246 {
247     HWND ipcwindow;
248     MSG message;
249
250     ipcwindow =
251         CreateWindow( "STATIC",                      /* name of window class */
252                   "VLC ipc "VERSION,                /* window title bar text */
253                   0,                                         /* window style */
254                   0,                                 /* default X coordinate */
255                   0,                                 /* default Y coordinate */
256                   0,                                         /* window width */
257                   0,                                        /* window height */
258                   NULL,                                  /* no parent window */
259                   NULL,                            /* no menu in this window */
260                   GetModuleHandle(NULL),  /* handle of this program instance */
261                   NULL );                               /* sent to WM_CREATE */
262
263     SetWindowLong( ipcwindow, GWL_WNDPROC, (LONG)WMCOPYWNDPROC );
264     SetWindowLong( ipcwindow, GWL_USERDATA, (LONG)p_this );
265
266     /* Signal the creation of the thread and events queue */
267     vlc_thread_ready( p_this );
268
269     while( GetMessage( &message, NULL, 0, 0 ) )
270     {
271         TranslateMessage( &message );
272         DispatchMessage( &message );
273     }
274 }
275
276 LRESULT CALLBACK WMCOPYWNDPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
277                                 LPARAM lParam )
278 {
279     if( uMsg == WM_COPYDATA )
280     {
281         COPYDATASTRUCT *pwm_data = (COPYDATASTRUCT*)lParam;
282         vlc_object_t *p_this;
283         playlist_t *p_playlist;
284
285         p_this = (vlc_object_t *)GetWindowLong( hwnd, GWL_USERDATA );
286
287         if( !p_this ) return 0;
288
289         /* Add files to the playlist */
290         p_playlist = (playlist_t *)vlc_object_find( p_this,
291                                                     VLC_OBJECT_PLAYLIST,
292                                                     FIND_ANYWHERE );
293         if( !p_playlist ) return 0;
294
295         if( pwm_data->lpData )
296         {
297             int i_argc, i_data, i_opt, i_options;
298             char **ppsz_argv;
299             char *p_data = (char *)pwm_data->lpData;
300
301             i_argc = *((int *)&p_data[0]);
302             ppsz_argv = (char **)malloc( i_argc * sizeof(char *) );
303             i_data = sizeof(int);
304             for( i_opt = 0; i_opt < i_argc; i_opt++ )
305             {
306                 ppsz_argv[i_opt] = &p_data[i_data + sizeof(int)];
307                 i_data += *((int *)&p_data[i_data]);
308                 i_data += sizeof(int);
309             }
310
311             for( i_opt = 0; i_opt < i_argc; i_opt++ )
312             {
313                 i_options = 0;
314
315                 /* Count the input options */
316                 while( i_opt + i_options + 1 < i_argc &&
317                        *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
318                 {
319                     i_options++;
320                 }
321
322                 playlist_Add( p_playlist, ppsz_argv[ i_opt ],
323                     (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
324                     i_options, PLAYLIST_APPEND | (i_opt? 0 : PLAYLIST_GO),
325                     PLAYLIST_END );
326
327                 i_opt += i_options;
328             }
329
330             free( ppsz_argv );
331         }
332
333         vlc_object_release( p_playlist );
334     }
335
336     return DefWindowProc( hwnd, uMsg, wParam, lParam );
337 }
338
339 /*****************************************************************************
340  * system_End: terminate winsock.
341  *****************************************************************************/
342 void system_End( vlc_t *p_this )
343 {
344 #if !defined( UNDER_CE )
345     WSACleanup();
346 #endif
347 }