]> git.sesse.net Git - vlc/blob - src/win32/specific.c
win32: move single instance back-end to separate interface
[vlc] / src / win32 / specific.c
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 #define UNICODE
29 #include <vlc_common.h>
30 #include "libvlc.h"
31 #include "../lib/libvlc_internal.h"
32 #include "config/vlc_getopt.h"
33
34 #include <mmsystem.h>
35 #include <winsock.h>
36
37
38 static int system_InitWSA(int hi, int lo)
39 {
40     WSADATA data;
41
42     if (WSAStartup(MAKEWORD(hi, lo), &data) == 0)
43     {
44         if (LOBYTE(data.wVersion) == 2 && HIBYTE(data.wVersion) == 2)
45             return 0;
46         /* Winsock DLL is not usable */
47         WSACleanup( );
48     }
49     return -1;
50 }
51
52 /**
53  * Initializes MME timer, Winsock.
54  */
55 void system_Init(void)
56 {
57 #if !VLC_WINSTORE_APP
58     timeBeginPeriod(5);
59 #endif
60
61     if (system_InitWSA(2, 2) && system_InitWSA(1, 1))
62         fputs("Error: cannot initialize Winsocks\n", stderr);
63 }
64
65 /*****************************************************************************
66  * system_Configure: check for system specific configuration options.
67  *****************************************************************************/
68
69 /* Must be same as in modules/control/win_msg.c */
70 typedef struct
71 {
72     int argc;
73     int enqueue;
74     char data[];
75 } vlc_ipc_data_t;
76
77 void system_Configure( libvlc_int_t *p_this, int i_argc, const char *const ppsz_argv[] )
78 {
79 #if !VLC_WINSTORE_APP
80     /* Raise default priority of the current process */
81 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
82 #   define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
83 #endif
84     if( var_InheritBool( p_this, "high-priority" ) )
85     {
86         if( SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS )
87              || SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
88         {
89             msg_Dbg( p_this, "raised process priority" );
90         }
91         else
92         {
93             msg_Dbg( p_this, "could not raise process priority" );
94         }
95     }
96
97     if( var_InheritBool( p_this, "one-instance" )
98      || ( var_InheritBool( p_this, "one-instance-when-started-from-file" )
99        && var_InheritBool( p_this, "started-from-file" ) ) )
100     {
101         HANDLE hmutex;
102
103         msg_Info( p_this, "one instance mode ENABLED");
104
105         /* Use a named mutex to check if another instance is already running */
106         if( !( hmutex = CreateMutex( 0, TRUE, L"VLC ipc " TEXT(VERSION) ) ) )
107         {
108             /* Failed for some reason. Just ignore the option and go on as
109              * normal. */
110             msg_Err( p_this, "one instance mode DISABLED "
111                      "(mutex couldn't be created)" );
112             return;
113         }
114
115         if( GetLastError() != ERROR_ALREADY_EXISTS )
116         {
117             libvlc_InternalAddIntf( p_this, "win32msg,none" );
118             /* Initialization done.
119              * Release the mutex to unblock other instances */
120             ReleaseMutex( hmutex );
121         }
122         else
123         {
124             /* Another instance is running */
125
126             HWND ipcwindow;
127
128             /* Wait until the 1st instance is initialized */
129             WaitForSingleObject( hmutex, INFINITE );
130
131             /* Locate the window created by the IPC helper thread of the
132              * 1st instance */
133             if( !( ipcwindow = FindWindow( 0, L"VLC ipc " TEXT(VERSION) ) ) )
134             {
135                 msg_Err( p_this, "one instance mode DISABLED "
136                          "(couldn't find 1st instance of program)" );
137                 ReleaseMutex( hmutex );
138                 return;
139             }
140
141             /* We assume that the remaining parameters are filenames
142              * and their input options */
143             if( i_argc > 0 )
144             {
145                 COPYDATASTRUCT wm_data;
146                 int i_opt;
147                 vlc_ipc_data_t *p_data;
148                 size_t i_data = sizeof (*p_data);
149
150                 for( i_opt = 0; i_opt < i_argc; i_opt++ )
151                 {
152                     i_data += sizeof (size_t);
153                     i_data += strlen( ppsz_argv[ i_opt ] ) + 1;
154                 }
155
156                 p_data = malloc( i_data );
157                 p_data->argc = i_argc;
158                 p_data->enqueue = var_InheritBool( p_this, "playlist-enqueue" );
159                 i_data = 0;
160                 for( i_opt = 0; i_opt < i_argc; i_opt++ )
161                 {
162                     size_t i_len = strlen( ppsz_argv[ i_opt ] ) + 1;
163                     /* Windows will never switch to an architecture
164                      * with stronger alignment requirements, right. */
165                     *((size_t *)(p_data->data + i_data)) = i_len;
166                     i_data += sizeof (size_t);
167                     memcpy( &p_data->data[i_data], ppsz_argv[ i_opt ], i_len );
168                     i_data += i_len;
169                 }
170                 i_data += sizeof (*p_data);
171
172                 /* Send our playlist items to the 1st instance */
173                 wm_data.dwData = 0;
174                 wm_data.cbData = i_data;
175                 wm_data.lpData = p_data;
176                 SendMessage( ipcwindow, WM_COPYDATA, 0, (LPARAM)&wm_data );
177             }
178
179             /* Initialization done.
180              * Release the mutex to unblock other instances */
181             ReleaseMutex( hmutex );
182
183             /* Bye bye */
184             system_End( );
185             exit( 0 );
186         }
187     }
188 #endif
189 }
190
191 /**
192  * Cleans up after system_Init() and system_Configure().
193  */
194 void system_End(void)
195 {
196 #if !VLC_WINSTORE_APP
197     timeEndPeriod(5);
198 #endif
199
200     /* XXX: In theory, we should not call this if WSAStartup() failed. */
201     WSACleanup();
202 }