1 /*****************************************************************************
2 * specific.c: Win32 specific initilization
3 *****************************************************************************
4 * Copyright (C) 2001-2004, 2010 VLC authors and VideoLAN
6 * Authors: Samuel Hocevar <sam@zoy.org>
7 * Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
31 #include <vlc_common.h>
33 #include "../lib/libvlc_internal.h"
34 #include "config/vlc_getopt.h"
38 #if VLC_WINSTORE_APP && !defined(__MINGW32__)
39 typedef UINT MMRESULT
;
42 DWORD LoadLibraryFlags
= 0;
44 static int system_InitWSA(int hi
, int lo
)
48 if (WSAStartup(MAKEWORD(hi
, lo
), &data
) == 0)
50 if (LOBYTE(data
.wVersion
) == 2 && HIBYTE(data
.wVersion
) == 2)
52 /* Winsock DLL is not usable */
59 * Initializes MME timer, Winsock.
61 static HMODULE hWinmm
= INVALID_HANDLE_VALUE
;
62 void system_Init(void)
64 hWinmm
= LoadLibrary(TEXT("winmm.dll"));
67 MMRESULT (WINAPI
* timeBeginPeriod
)(UINT
);
68 timeBeginPeriod
= (void*)GetProcAddress(hWinmm
, "timeBeginPeriod");
73 if (system_InitWSA(2, 2) && system_InitWSA(1, 1))
74 fputs("Error: cannot initialize Winsocks\n", stderr
);
77 # if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
78 if (GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),
79 "SetDefaultDllDirectories") != NULL
)
80 # endif /* FIXME: not reentrant */
81 LoadLibraryFlags
= LOAD_LIBRARY_SEARCH_SYSTEM32
;
85 /*****************************************************************************
86 * system_Configure: check for system specific configuration options.
87 *****************************************************************************/
89 /* Must be same as in modules/control/win_msg.c */
97 void system_Configure( libvlc_int_t
*p_this
, int i_argc
, const char *const ppsz_argv
[] )
100 /* Raise default priority of the current process */
101 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
102 # define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
104 if( var_InheritBool( p_this
, "high-priority" ) )
106 if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS
)
107 || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS
) )
109 msg_Dbg( p_this
, "raised process priority" );
113 msg_Dbg( p_this
, "could not raise process priority" );
117 if( var_InheritBool( p_this
, "one-instance" )
118 || ( var_InheritBool( p_this
, "one-instance-when-started-from-file" )
119 && var_InheritBool( p_this
, "started-from-file" ) ) )
123 msg_Info( p_this
, "one instance mode ENABLED");
125 /* Use a named mutex to check if another instance is already running */
126 if( !( hmutex
= CreateMutex( 0, TRUE
, L
"VLC ipc " TEXT(VERSION
) ) ) )
128 /* Failed for some reason. Just ignore the option and go on as
130 msg_Err( p_this
, "one instance mode DISABLED "
131 "(mutex couldn't be created)" );
135 if( GetLastError() != ERROR_ALREADY_EXISTS
)
137 libvlc_InternalAddIntf( p_this
, "win_msg,none" );
138 /* Initialization done.
139 * Release the mutex to unblock other instances */
140 ReleaseMutex( hmutex
);
144 /* Another instance is running */
148 /* Wait until the 1st instance is initialized */
149 WaitForSingleObject( hmutex
, INFINITE
);
151 /* Locate the window created by the IPC helper thread of the
153 if( !( ipcwindow
= FindWindow( 0, L
"VLC ipc " TEXT(VERSION
) ) ) )
155 msg_Err( p_this
, "one instance mode DISABLED "
156 "(couldn't find 1st instance of program)" );
157 ReleaseMutex( hmutex
);
161 /* We assume that the remaining parameters are filenames
162 * and their input options */
165 COPYDATASTRUCT wm_data
;
167 vlc_ipc_data_t
*p_data
;
168 size_t i_data
= sizeof (*p_data
);
170 for( i_opt
= 0; i_opt
< i_argc
; i_opt
++ )
172 i_data
+= sizeof (size_t);
173 i_data
+= strlen( ppsz_argv
[ i_opt
] ) + 1;
176 p_data
= malloc( i_data
);
177 p_data
->argc
= i_argc
;
178 p_data
->enqueue
= var_InheritBool( p_this
, "playlist-enqueue" );
180 for( i_opt
= 0; i_opt
< i_argc
; i_opt
++ )
182 size_t i_len
= strlen( ppsz_argv
[ i_opt
] ) + 1;
183 /* Windows will never switch to an architecture
184 * with stronger alignment requirements, right. */
185 *((size_t *)(p_data
->data
+ i_data
)) = i_len
;
186 i_data
+= sizeof (size_t);
187 memcpy( &p_data
->data
[i_data
], ppsz_argv
[ i_opt
], i_len
);
190 i_data
+= sizeof (*p_data
);
192 /* Send our playlist items to the 1st instance */
194 wm_data
.cbData
= i_data
;
195 wm_data
.lpData
= p_data
;
196 SendMessage( ipcwindow
, WM_COPYDATA
, 0, (LPARAM
)&wm_data
);
199 /* Initialization done.
200 * Release the mutex to unblock other instances */
201 ReleaseMutex( hmutex
);
212 * Cleans up after system_Init() and system_Configure().
214 void system_End(void)
218 MMRESULT (WINAPI
* timeEndPeriod
)(UINT
);
219 timeEndPeriod
= (void*)GetProcAddress(hWinmm
, "timeEndPeriod");
225 /* XXX: In theory, we should not call this if WSAStartup() failed. */