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