]> git.sesse.net Git - vlc/blob - src/misc/win32_specific.c
16c21b5f8005aba8ac8881b1436141e6f2319e46
[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.26 2003/10/03 13:35:56 sam 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 <fcntl.h>
36 #   include <winsock2.h>
37 #endif
38
39 /*****************************************************************************
40  * system_Init: initialize winsock and misc other things.
41  *****************************************************************************/
42 void system_Init( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
43 {
44 #if !defined( UNDER_CE )
45     WSADATA Data;
46
47     /* Get our full path */
48     if( ppsz_argv[0] )
49     {
50         char psz_path[MAX_PATH];
51         char *psz_vlc;
52
53         GetFullPathName( ppsz_argv[0], MAX_PATH, psz_path, &psz_vlc );
54
55         if( psz_vlc > psz_path && psz_vlc[-1] == '\\' )
56         {
57             psz_vlc[-1] = '\0';
58             p_this->p_libvlc->psz_vlcpath = strdup( psz_path );
59         }
60         else
61         {
62             p_this->p_libvlc->psz_vlcpath = strdup( "" );
63         }
64     }
65     else
66     {
67         p_this->p_libvlc->psz_vlcpath = strdup( "" );
68     }
69
70     /* Set the default file-translation mode */
71     _fmode = _O_BINARY;
72     _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
73
74     /* WinSock Library Init. */
75     if( !WSAStartup( MAKEWORD( 2, 0 ), &Data ) )
76     {
77         /* Confirm that the WinSock DLL supports 2.0.*/
78         if( LOBYTE( Data.wVersion ) != 2 || HIBYTE( Data.wVersion ) != 0 )
79         {
80             /* We could not find a suitable WinSock DLL. */
81             WSACleanup( );
82         }
83         else
84         {
85             /* Everything went ok. */
86             return;
87         }
88     }
89
90     /* Let's try with WinSock 1.1 */
91     if( !WSAStartup( MAKEWORD( 1, 1 ), &Data ) )
92     {
93         /* Confirm that the WinSock DLL supports 1.1.*/
94         if( LOBYTE( Data.wVersion ) != 1 || HIBYTE( Data.wVersion ) != 1 )
95         {
96             /* We could not find a suitable WinSock DLL. */
97             WSACleanup( );
98         }
99         else
100         {
101             /* Everything went ok. */
102             return;
103         }
104     }
105
106     fprintf( stderr, "error: can't initialize WinSocks\n" );
107
108     return; 
109
110 #endif
111 }
112
113 /*****************************************************************************
114  * system_Configure: check for system specific configuration options.
115  *****************************************************************************/
116 static void IPCHelperThread( vlc_object_t * );
117 LRESULT CALLBACK WMCOPYWNDPROC( HWND, UINT, WPARAM, LPARAM );
118
119 void system_Configure( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
120 {
121 #if !defined( UNDER_CE )
122     p_this->p_libvlc->b_fast_mutex = config_GetInt( p_this, "fast-mutex" );
123     p_this->p_libvlc->i_win9x_cv = config_GetInt( p_this, "win9x-cv-method" );
124
125     /* Raise default priority of the current process */
126 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
127 #   define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
128 #endif
129     if( config_GetInt( p_this, "high-priority" ) )
130     {
131         if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
132              || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
133         {
134             msg_Dbg( p_this, "raised process priority" );
135         }
136         else
137         {
138             msg_Dbg( p_this, "could not raise process priority" );
139         }
140     }
141
142     if( config_GetInt( p_this, "one-instance" ) )
143     {
144         HANDLE hmutex;
145
146         msg_Info( p_this, "One instance mode ENABLED");
147
148         /* Use a named mutex to check if another instance is already running */
149         if( ( hmutex = CreateMutex( NULL, TRUE, "VLC ipc "VERSION ) ) == NULL )
150         {
151             /* Failed for some reason. Just ignore the option and go on as
152              * normal. */
153             msg_Err( p_this, "One instance mode DISABLED "
154                      "(mutex couldn't be created)" );
155             return;
156         }
157
158         if( GetLastError() != ERROR_ALREADY_EXISTS )
159         {
160             /* We are the 1st instance. */
161             vlc_object_t *p_helper =
162              (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
163
164             /* Run the helper thread */
165             if( vlc_thread_create( p_helper, "IPC helper", IPCHelperThread,
166                                    VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
167             {
168                 msg_Err( p_this, "One instance mode DISABLED "
169                          "(IPC helper thread couldn't be created)" );
170
171             }
172
173             /* Initialization done.
174              * Release the mutex to unblock other instances */
175             ReleaseMutex( hmutex );
176         }
177         else
178         {
179             /* Another instance is running */
180
181             HWND ipcwindow;
182
183             /* Wait until the 1st instance is initialized */
184             WaitForSingleObject( hmutex, INFINITE );
185
186             /* Locate the window created by the IPC helper thread of the
187              * 1st instance */
188             if( ( ipcwindow = FindWindow( NULL, "VLC ipc "VERSION ) )
189                 == NULL )
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( "STATIC",                      /* name of window class */
251                   "VLC ipc "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
321                 playlist_Add( p_playlist, ppsz_argv[ i_opt ],
322                     (char const **)( i_options ? &ppsz_argv[i_opt+1] : NULL ),
323                     i_options, PLAYLIST_APPEND | (i_opt? 0 : PLAYLIST_GO),
324                     PLAYLIST_END );
325
326                 i_opt += i_options;
327             }
328
329             free( ppsz_argv );
330         }
331
332         vlc_object_release( p_playlist );
333     }
334
335     return DefWindowProc( hwnd, uMsg, wParam, lParam );
336 }
337
338 /*****************************************************************************
339  * system_End: terminate winsock.
340  *****************************************************************************/
341 void system_End( vlc_t *p_this )
342 {
343 #if !defined( UNDER_CE )
344     WSACleanup();
345 #endif
346 }