1 /*****************************************************************************
2 * extension_thread.c: Extensions Manager, Threads manager (no Lua here)
3 *****************************************************************************
4 * Copyright (C) 2009-2010 VideoLAN and authors
7 * Authors: Jean-Philippe André < jpeg # videolan.org >
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
29 #include "extension.h"
34 extensions_manager_t *p_mgr;
39 static void* Run( void *data );
40 static void FreeCommands( struct command_t *command );
41 static int RemoveActivated( extensions_manager_t *p_mgr, extension_t *p_ext );
44 * Activate an extension
45 * @param p_mgr This manager
46 * @param p_ext Extension to activate
47 * @return The usual VLC return codes
49 int Activate( extensions_manager_t *p_mgr, extension_t *p_ext )
51 assert( p_ext != NULL );
53 struct extension_sys_t *p_sys = p_ext->p_sys;
54 assert( p_sys != NULL );
56 msg_Dbg( p_mgr, "Activating extension '%s'", p_ext->psz_title );
58 if( IsActivated( p_mgr, p_ext ) )
60 msg_Warn( p_mgr, "Extension is already activated!" );
64 /* Add this script to the activated extensions list */
65 vlc_mutex_lock( &p_mgr->p_sys->lock );
66 ARRAY_APPEND( p_mgr->p_sys->activated_extensions, p_ext );
67 vlc_mutex_unlock( &p_mgr->p_sys->lock );
69 /* Prepare first command */
70 p_sys->command = calloc( 1, sizeof( struct command_t ) );
73 p_sys->command->i_command = CMD_ACTIVATE; /* No params */
76 p_sys->b_exiting = false;
78 if( vlc_clone( &p_sys->thread, Run, p_ext, VLC_THREAD_PRIORITY_LOW )
81 p_sys->b_exiting = true;
82 // Note: Automatically deactivating the extension...
83 Deactivate( p_mgr, p_ext );
90 /** Look for an extension in the activated extensions list
91 * @todo FIXME Should be entered with the lock held
93 bool IsActivated( extensions_manager_t *p_mgr, extension_t *p_ext )
95 assert( p_ext != NULL );
96 vlc_mutex_lock( &p_mgr->p_sys->lock );
99 FOREACH_ARRAY( p_iter, p_mgr->p_sys->activated_extensions )
103 assert( p_iter->psz_name != NULL );
104 if( !strcmp( p_iter->psz_name, p_ext->psz_name ) )
106 vlc_mutex_unlock( &p_mgr->p_sys->lock );
112 vlc_mutex_unlock( &p_mgr->p_sys->lock );
116 /** Recursively drop and free commands starting from "command" */
117 static void FreeCommands( struct command_t *command )
119 if( !command ) return;
120 struct command_t *next = command->next;
121 switch( command->i_command )
125 case CMD_CLICK: // Arg1 must not be freed
128 case CMD_TRIGGERMENU:
129 case CMD_PLAYING_CHANGED:
130 free( command->data[0] ); // Arg1 is int*, to free
137 FreeCommands( next );
140 /** Deactivate this extension: pushes immediate command and drops queued */
141 int Deactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
144 vlc_mutex_lock( &p_ext->p_sys->command_lock );
146 if( p_ext->p_sys->b_exiting )
148 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
152 if( p_ext->p_sys->progress )
154 // Extension is stuck, kill it now
155 dialog_ProgressDestroy( p_ext->p_sys->progress );
156 p_ext->p_sys->progress = NULL;
157 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
158 KillExtension( p_mgr, p_ext );
162 /* Free the list of commands */
163 if( p_ext->p_sys->command )
164 FreeCommands( p_ext->p_sys->command->next );
167 struct command_t *cmd = calloc( 1, sizeof( struct command_t ) );
168 cmd->i_command = CMD_DEACTIVATE;
169 if( p_ext->p_sys->command )
170 p_ext->p_sys->command->next = cmd;
172 p_ext->p_sys->command = cmd;
174 vlc_cond_signal( &p_ext->p_sys->wait );
175 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
180 /** Remove an extension from the activated list */
181 static int RemoveActivated( extensions_manager_t *p_mgr, extension_t *p_ext )
183 if( p_mgr->p_sys->b_killed )
185 vlc_mutex_lock( &p_mgr->p_sys->lock );
189 FOREACH_ARRAY( p_iter, p_mgr->p_sys->activated_extensions )
197 assert( p_iter->psz_name != NULL );
198 if( !strcmp( p_iter->psz_name, p_ext->psz_name ) )
205 ARRAY_REMOVE( p_mgr->p_sys->activated_extensions, i_idx );
209 msg_Dbg( p_mgr, "Can't find extension '%s' in the activated list",
213 vlc_mutex_unlock( &p_mgr->p_sys->lock );
214 return (i_idx >= 0) ? VLC_SUCCESS : VLC_EGENERIC;
217 void KillExtension( extensions_manager_t *p_mgr, extension_t *p_ext )
219 /* Cancel thread if it seems stuck for a while */
220 msg_Dbg( p_mgr, "Killing extension now" );
221 vlc_cancel( p_ext->p_sys->thread );
222 lua_ExtensionDeactivate( p_mgr, p_ext );
223 p_ext->p_sys->b_exiting = true;
224 RemoveActivated( p_mgr, p_ext );
227 /** Push a UI command */
228 int __PushCommand( extension_t *p_ext, bool b_unique, command_type_e i_command,
231 vlc_mutex_lock( &p_ext->p_sys->command_lock );
234 struct command_t *cmd = calloc( 1, sizeof( struct command_t ) );
235 cmd->i_command = i_command;
239 cmd->data[0] = va_arg( args, void* );
241 case CMD_TRIGGERMENU:
243 int *pi = malloc( sizeof( int ) );
247 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
250 *pi = va_arg( args, int );
254 case CMD_PLAYING_CHANGED:
256 int *pi = malloc( sizeof( int ) );
260 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
263 *pi = va_arg( args, int );
269 case CMD_UPDATE_META:
270 // Nothing to do here
273 msg_Dbg( p_ext->p_sys->p_mgr,
274 "Unknown command send to extension: %d", i_command );
278 /* Push command to the end of the queue */
279 struct command_t *last = p_ext->p_sys->command;
282 p_ext->p_sys->command = cmd;
287 while( last->next != NULL )
289 if( b_unique && last->i_command == i_command )
291 // Do not push this 'unique' command a second time
292 b_skip = !memcmp( last->data, cmd->data, sizeof( cmd->data ) );
310 vlc_cond_signal( &p_ext->p_sys->wait );
311 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
316 static void* Run( void *data )
318 extension_t *p_ext = data;
319 extensions_manager_t *p_mgr = p_ext->p_sys->p_mgr;
321 vlc_mutex_lock( &p_ext->p_sys->command_lock );
322 mutex_cleanup_push( &p_ext->p_sys->command_lock );
324 while( !p_ext->p_sys->b_exiting )
326 struct command_t *cmd = p_ext->p_sys->command;
328 /* Pop command in front */
331 vlc_cond_wait( &p_ext->p_sys->wait, &p_ext->p_sys->command_lock );
334 p_ext->p_sys->command = cmd->next;
335 cmd->next = NULL; /* unlink command (for FreeCommands()) */
337 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
340 int cancel = vlc_savecancel();
341 if( LockExtension( p_ext ) )
343 switch( cmd->i_command )
347 if( lua_ExecuteFunction( p_mgr, p_ext, "activate", LUA_END ) < 0 )
349 msg_Err( p_mgr, "Could not activate extension!" );
350 Deactivate( p_mgr, p_ext );
357 msg_Dbg( p_mgr, "Deactivating '%s'", p_ext->psz_title );
358 if( lua_ExtensionDeactivate( p_mgr, p_ext ) < 0 )
360 msg_Warn( p_mgr, "Extension '%s' did not deactivate properly",
363 p_ext->p_sys->b_exiting = true;
364 RemoveActivated( p_mgr, p_ext );
370 lua_ExecuteFunction( p_mgr, p_ext, "close", LUA_END );
376 extension_widget_t *p_widget = cmd->data[0];
378 msg_Dbg( p_mgr, "Clicking '%s': '%s'",
379 p_ext->psz_name, p_widget->psz_text );
380 if( lua_ExtensionWidgetClick( p_mgr, p_ext, p_widget ) < 0 )
382 msg_Warn( p_mgr, "Could not translate click" );
387 case CMD_TRIGGERMENU:
389 int *pi_id = cmd->data[0];
391 msg_Dbg( p_mgr, "Trigger menu %d of '%s'",
392 *pi_id, p_ext->psz_name );
393 lua_ExtensionTriggerMenu( p_mgr, p_ext, *pi_id );
399 lua_ExecuteFunction( p_mgr, p_ext, "input_changed", LUA_END );
403 case CMD_UPDATE_META:
405 lua_ExecuteFunction( p_mgr, p_ext, "meta_changed", LUA_END );
409 case CMD_PLAYING_CHANGED:
411 lua_ExecuteFunction( p_mgr, p_ext, "playing_changed",
412 LUA_NUM, *((int *)cmd->data[0]), LUA_END );
418 msg_Dbg( p_mgr, "Unknown command in extension command queue: %d",
423 UnlockExtension( p_ext );
427 vlc_restorecancel( cancel );
428 vlc_mutex_lock( &p_ext->p_sys->command_lock );
432 msg_Dbg( p_mgr, "Extension thread end: '%s'", p_ext->psz_title );
434 // Note: At this point, the extension should be deactivated