1 /*****************************************************************************
2  * specific.c: Win32 specific initilization
3  *****************************************************************************
4  * Copyright (C) 2001-2004, 2010 VLC authors and VideoLAN
5  *
6  * Authors: Samuel Hocevar <sam@zoy.org>
7  *          Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 
28 #ifndef UNICODE
29 # define UNICODE
30 #endif
31 #include <vlc_common.h>
32 #include "libvlc.h"
33 #include "../lib/libvlc_internal.h"
34 #include "config/vlc_getopt.h"
35 
36 #include <mmsystem.h>
37 #include <winsock.h>
38 #if VLC_WINSTORE_APP && !defined(__MINGW32__)
39 typedef UINT MMRESULT;
40 #endif
41 
42 DWORD LoadLibraryFlags = 0;
43 
system_InitWSA(int hi,int lo)44 static int system_InitWSA(int hi, int lo)
45 {
46     WSADATA data;
47 
48     if (WSAStartup(MAKEWORD(hi, lo), &data) == 0)
49     {
50         if (LOBYTE(data.wVersion) == 2 && HIBYTE(data.wVersion) == 2)
51             return 0;
52         /* Winsock DLL is not usable */
53         WSACleanup( );
54     }
55     return -1;
56 }
57 
58 /**
59  * Initializes MME timer, Winsock.
60  */
system_Init(void)61 void system_Init(void)
62 {
63     if (system_InitWSA(2, 2) && system_InitWSA(1, 1))
64         fputs("Error: cannot initialize Winsocks\n", stderr);
65 
66 #if !VLC_WINSTORE_APP
67 # if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
68     if (GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),
69                                        "SetDefaultDllDirectories") != NULL)
70 # endif /* FIXME: not reentrant */
71         LoadLibraryFlags = LOAD_LIBRARY_SEARCH_SYSTEM32;
72 #endif
73 }
74 
75 /*****************************************************************************
76  * system_Configure: check for system specific configuration options.
77  *****************************************************************************/
78 
79 /* Must be same as in modules/control/win_msg.c */
80 typedef struct
81 {
82     int argc;
83     int enqueue;
84     char data[];
85 } vlc_ipc_data_t;
86 
system_Configure(libvlc_int_t * p_this,int i_argc,const char * const ppsz_argv[])87 void system_Configure( libvlc_int_t *p_this, int i_argc, const char *const ppsz_argv[] )
88 {
89 #if !VLC_WINSTORE_APP
90     if( var_InheritBool( p_this, "one-instance" )
91      || ( var_InheritBool( p_this, "one-instance-when-started-from-file" )
92        && var_InheritBool( p_this, "started-from-file" ) ) )
93     {
94         HANDLE hmutex;
95 
96         msg_Info( p_this, "one instance mode ENABLED");
97 
98         /* Use a named mutex to check if another instance is already running */
99         if( !( hmutex = CreateMutex( 0, TRUE, L"VLC ipc " TEXT(VERSION) ) ) )
100         {
101             /* Failed for some reason. Just ignore the option and go on as
102              * normal. */
103             msg_Err( p_this, "one instance mode DISABLED "
104                      "(mutex couldn't be created)" );
105             return;
106         }
107 
108         if( GetLastError() != ERROR_ALREADY_EXISTS )
109         {
110             libvlc_InternalAddIntf( p_this, "win_msg,none" );
111             /* Initialization done.
112              * Release the mutex to unblock other instances */
113             ReleaseMutex( hmutex );
114         }
115         else
116         {
117             /* Another instance is running */
118 
119             HWND ipcwindow;
120 
121             /* Wait until the 1st instance is initialized */
122             WaitForSingleObject( hmutex, INFINITE );
123 
124             /* Locate the window created by the IPC helper thread of the
125              * 1st instance */
126             if( !( ipcwindow = FindWindow( 0, L"VLC ipc " TEXT(VERSION) ) ) )
127             {
128                 msg_Err( p_this, "one instance mode DISABLED "
129                          "(couldn't find 1st instance of program)" );
130                 ReleaseMutex( hmutex );
131                 return;
132             }
133 
134             /* We assume that the remaining parameters are filenames
135              * and their input options */
136             if( i_argc > 0 )
137             {
138                 COPYDATASTRUCT wm_data;
139                 int i_opt;
140                 vlc_ipc_data_t *p_data;
141                 size_t i_data = sizeof (*p_data);
142 
143                 for( i_opt = 0; i_opt < i_argc; i_opt++ )
144                 {
145                     i_data += sizeof (size_t);
146                     i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
147                 }
148 
149                 p_data = malloc( i_data );
150                 p_data->argc = i_argc;
151                 p_data->enqueue = var_InheritBool( p_this, "playlist-enqueue" );
152                 i_data = 0;
153                 for( i_opt = 0; i_opt < i_argc; i_opt++ )
154                 {
155                     size_t i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
156                     /* Windows will never switch to an architecture
157                      * with stronger alignment requirements, right. */
158                     *((size_t *)(p_data->data + i_data)) = i_len;
159                     i_data += sizeof (size_t);
160                     memcpy( &p_data->data[i_data], ppsz_argv[ i_opt ], i_len );
161                     i_data += i_len;
162                 }
163                 i_data += sizeof (*p_data);
164 
165                 /* Send our playlist items to the 1st instance */
166                 wm_data.dwData = 0;
167                 wm_data.cbData = i_data;
168                 wm_data.lpData = p_data;
169                 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
170             }
171 
172             /* Initialization done.
173              * Release the mutex to unblock other instances */
174             ReleaseMutex( hmutex );
175 
176             /* Bye bye */
177             system_End( );
178             exit( 0 );
179         }
180     }
181 #endif
182 }
183 
184 /**
185  * Cleans up after system_Init() and system_Configure().
186  */
system_End(void)187 void system_End(void)
188 {
189     /* XXX: In theory, we should not call this if WSAStartup() failed. */
190     WSACleanup();
191 }
192