demux: mp4: set bitmap mask when possible
[vlc.git] / src / os2 / specific.c
blob264b66a4ff5f591d4a9300b442b6f0cd19b25662
1 /*****************************************************************************
2 * specific.c: OS/2 specific features
3 *****************************************************************************
4 * Copyright (C) 2010 KO Myung-Hun
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <vlc_common.h>
26 #include "../libvlc.h"
27 #include <vlc_playlist_legacy.h>
28 #include <vlc_input.h>
29 #include <vlc_interface.h>
30 #include <vlc_url.h>
32 #include <fcntl.h>
33 #include <io.h>
35 #define VLC_IPC_PIPE "\\PIPE\\VLC\\IPC\\"VERSION
37 #define IPC_CMD_GO 0x00
38 #define IPC_CMD_ENQUEUE 0x01
39 #define IPC_CMD_QUIT 0xFF
41 extern int _fmode_bin;
43 static HPIPE hpipeIPC = NULLHANDLE;
44 static int tidIPCFirst = -1;
45 static int tidIPCHelper = -1;
47 static void IPCHelperThread( void *arg )
49 libvlc_int_t *libvlc = arg;
51 ULONG ulCmd;
52 int i_argc;
53 char **ppsz_argv;
54 size_t i_len;
55 ULONG cbActual;
56 int i_options;
58 /* Add files to the playlist */
59 playlist_t *p_playlist;
63 DosConnectNPipe( hpipeIPC );
65 /* Read command */
66 DosRead( hpipeIPC, &ulCmd, sizeof( ulCmd ), &cbActual );
67 if( ulCmd == IPC_CMD_QUIT )
68 continue;
70 /* Read a count of arguments */
71 DosRead( hpipeIPC, &i_argc, sizeof( i_argc ), &cbActual );
73 ppsz_argv = vlc_alloc( i_argc, sizeof( *ppsz_argv ));
75 for( int i_opt = 0; i_opt < i_argc; i_opt++ )
77 /* Read a length of argv */
78 DosRead( hpipeIPC, &i_len, sizeof( i_len ), &cbActual );
80 ppsz_argv[ i_opt ] = malloc( i_len );
82 /* Read argv */
83 DosRead( hpipeIPC, ppsz_argv[ i_opt ], i_len, &cbActual );
86 p_playlist = libvlc_priv(libvlc)->playlist;
88 for( int i_opt = 0; i_opt < i_argc;)
90 i_options = 0;
92 /* Count the input options */
93 while( i_opt + i_options + 1 < i_argc &&
94 *ppsz_argv[ i_opt + i_options + 1 ] == ':' )
95 i_options++;
98 if( p_playlist )
100 playlist_AddExt( p_playlist, ppsz_argv[ i_opt ], NULL,
101 i_opt == 0 && ulCmd != IPC_CMD_ENQUEUE,
102 i_options,
103 ( char const ** )
104 ( i_options ? &ppsz_argv[ i_opt + 1 ] :
105 NULL ),
106 VLC_INPUT_OPTION_TRUSTED );
109 for( ; i_options >= 0; i_options-- )
110 free( ppsz_argv[ i_opt++ ]);
113 free( ppsz_argv );
114 } while( !DosDisConnectNPipe( hpipeIPC ) && ulCmd != IPC_CMD_QUIT );
116 DosClose( hpipeIPC );
117 hpipeIPC = NULLHANDLE;
119 tidIPCFirst = -1;
120 tidIPCHelper = -1;
123 void system_Init( void )
125 /* Set the default file-translation mode */
126 _fmode_bin = 1;
127 setmode( fileno( stdin ), O_BINARY ); /* Needed for pipes */
130 void system_Configure( libvlc_int_t *p_this, int i_argc, const char *const ppsz_argv[] )
132 if( var_InheritBool( p_this, "high-priority" ) )
134 if( !DosSetPriority( PRTYS_PROCESS, PRTYC_REGULAR, PRTYD_MAXIMUM, 0 ) )
136 msg_Dbg( p_this, "raised process priority" );
138 else
140 msg_Dbg( p_this, "could not raise process priority" );
144 if( var_InheritBool( p_this, "one-instance" )
145 || ( var_InheritBool( p_this, "one-instance-when-started-from-file" )
146 && var_InheritBool( p_this, "started-from-file" ) ) )
148 HPIPE hpipe;
149 ULONG ulAction;
150 ULONG rc;
152 msg_Info( p_this, "one instance mode ENABLED");
154 /* Use a named pipe to check if another instance is already running */
155 for(;;)
157 rc = DosOpen( VLC_IPC_PIPE, &hpipe, &ulAction, 0, 0,
158 OPEN_ACTION_OPEN_IF_EXISTS,
159 OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYREADWRITE |
160 OPEN_FLAGS_FAIL_ON_ERROR,
161 NULL );
163 if( rc == ERROR_PIPE_BUSY )
164 DosWaitNPipe( VLC_IPC_PIPE, -1 );
165 else
166 break;
169 if( rc )
171 rc = DosCreateNPipe( VLC_IPC_PIPE, &hpipeIPC,
172 NP_ACCESS_DUPLEX,
173 NP_WAIT | NP_TYPE_MESSAGE |
174 NP_READMODE_MESSAGE | 0x01,
175 32768, 32768, 0 );
176 if( rc )
178 /* Failed to create a named pipe. Just ignore the option and
179 * go on as normal. */
180 msg_Err( p_this, "one instance mode DISABLED "
181 "(a named pipe couldn't be created)" );
182 return;
185 /* We are the 1st instance. */
187 /* Save the tid of the first instance */
188 tidIPCFirst = _gettid();
190 /* Run the helper thread */
191 tidIPCHelper = _beginthread( IPCHelperThread, NULL, 1024 * 1024,
192 p_this );
193 if( tidIPCHelper == -1 )
195 msg_Err( p_this, "one instance mode DISABLED "
196 "(IPC helper thread couldn't be created)");
198 tidIPCFirst = -1;
201 else
203 /* Another instance is running */
204 ULONG ulCmd = var_InheritBool( p_this, "playlist-enqueue") ?
205 IPC_CMD_ENQUEUE : IPC_CMD_GO;
206 ULONG cbActual;
208 /* Write a command */
209 DosWrite( hpipe, &ulCmd, sizeof( ulCmd ), &cbActual );
211 /* We assume that the remaining parameters are filenames
212 * and their input options */
214 /* Write a count of arguments */
215 DosWrite( hpipe, &i_argc, sizeof( i_argc ), &cbActual );
217 for( int i_opt = 0; i_opt < i_argc; i_opt++ )
219 /* We need to resolve relative paths in this instance */
220 char *mrl;
221 if( strstr( ppsz_argv[ i_opt ], "://" ))
222 mrl = strdup( ppsz_argv[ i_opt ] );
223 else
224 mrl = vlc_path2uri( ppsz_argv[ i_opt ], NULL );
226 if( !mrl )
227 mrl = ( char * )ppsz_argv[ i_opt ];
229 size_t i_len = strlen( mrl ) + 1;
231 /* Write a length of an argument */
232 DosWrite( hpipe, &i_len, sizeof( i_len ), &cbActual );
234 /* Write an argument */
235 DosWrite( hpipe, mrl, i_len, &cbActual );
237 if( mrl != ppsz_argv[ i_opt ])
238 free( mrl );
241 /* Close a named pipe of a client side */
242 DosClose( hpipe );
244 /* Bye bye */
245 system_End();
246 exit( 0 );
252 * Cleans up after system_Init() and system_Configure().
254 void system_End(void)
256 if( tidIPCFirst == _gettid())
258 HPIPE hpipe;
259 ULONG ulAction;
260 ULONG cbActual;
261 ULONG rc;
265 rc = DosOpen( VLC_IPC_PIPE, &hpipe, &ulAction, 0, 0,
266 OPEN_ACTION_OPEN_IF_EXISTS,
267 OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYREADWRITE |
268 OPEN_FLAGS_FAIL_ON_ERROR,
269 NULL );
271 if( rc == ERROR_PIPE_BUSY )
272 DosWaitNPipe( VLC_IPC_PIPE, -1 );
273 else if( rc )
274 DosSleep( 1 );
275 } while( rc );
277 /* Ask for IPCHelper to quit */
278 ULONG ulCmd = IPC_CMD_QUIT;
279 DosWrite( hpipe, &ulCmd, sizeof( ulCmd ), &cbActual );
281 DosClose( hpipe );
283 TID tid = tidIPCHelper;
284 DosWaitThread( &tid, DCWW_WAIT );