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