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 *****************************************************************************/
24 /* I don't want to include lua headers here */
25 typedef struct lua_State lua_State;
27 #include "extension.h"
32 extensions_manager_t *p_mgr;
37 static void* Run( void *data );
38 static void FreeCommands( struct command_t *command );
39 static int RemoveActivated( extensions_manager_t *p_mgr, extension_t *p_ext );
42 * Activate an extension
43 * @param p_mgr This manager
44 * @param p_ext Extension to activate
45 * @return The usual VLC return codes
47 int Activate( extensions_manager_t *p_mgr, extension_t *p_ext )
49 assert( p_ext != NULL );
51 struct extension_sys_t *p_sys = p_ext->p_sys;
52 assert( p_sys != NULL );
54 msg_Dbg( p_mgr, "Activating extension '%s'", p_ext->psz_title );
56 if( IsActivated( p_mgr, p_ext ) )
58 msg_Warn( p_mgr, "Extension is already activated!" );
62 /* Add this script to the activated extensions list */
63 vlc_mutex_lock( &p_mgr->p_sys->lock );
64 ARRAY_APPEND( p_mgr->p_sys->activated_extensions, p_ext );
65 vlc_mutex_unlock( &p_mgr->p_sys->lock );
67 /* Prepare first command */
68 p_sys->command = calloc( 1, sizeof( struct command_t ) );
71 p_sys->command->i_command = CMD_ACTIVATE; /* No params */
74 p_sys->b_exiting = false;
76 if( vlc_clone( &p_sys->thread, Run, p_ext, VLC_THREAD_PRIORITY_LOW )
79 p_sys->b_exiting = true;
80 // Note: Automatically deactivating the extension...
81 Deactivate( p_mgr, p_ext );
88 /** Look for an extension in the activated extensions list
89 * @todo FIXME Should be entered with the lock held
91 bool IsActivated( extensions_manager_t *p_mgr, extension_t *p_ext )
93 assert( p_ext != NULL );
94 vlc_mutex_lock( &p_mgr->p_sys->lock );
97 FOREACH_ARRAY( p_iter, p_mgr->p_sys->activated_extensions )
101 assert( p_iter->psz_name != NULL );
102 if( !strcmp( p_iter->psz_name, p_ext->psz_name ) )
104 vlc_mutex_unlock( &p_mgr->p_sys->lock );
110 vlc_mutex_unlock( &p_mgr->p_sys->lock );
114 /** Recursively drop and free commands starting from "command" */
115 static void FreeCommands( struct command_t *command )
117 if( !command ) return;
118 struct command_t *next = command->next;
119 switch( command->i_command )
123 case CMD_CLICK: // Arg1 must not be freed
126 case CMD_TRIGGERMENU:
127 case CMD_PLAYING_CHANGED:
128 free( command->data[0] ); // Arg1 is int*, to free
135 FreeCommands( next );
138 /** Deactivate this extension: pushes immediate command and drops queued */
139 int Deactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
142 vlc_mutex_lock( &p_ext->p_sys->command_lock );
144 if( p_ext->p_sys->b_exiting )
146 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
150 if( p_ext->p_sys->progress )
152 // Extension is stuck, kill it now
153 dialog_ProgressDestroy( p_ext->p_sys->progress );
154 p_ext->p_sys->progress = NULL;
155 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
156 KillExtension( p_mgr, p_ext );
160 /* Free the list of commands */
161 if( p_ext->p_sys->command )
162 FreeCommands( p_ext->p_sys->command->next );
165 struct command_t *cmd = calloc( 1, sizeof( struct command_t ) );
166 cmd->i_command = CMD_DEACTIVATE;
167 if( p_ext->p_sys->command )
168 p_ext->p_sys->command->next = cmd;
170 p_ext->p_sys->command = cmd;
172 vlc_cond_signal( &p_ext->p_sys->wait );
173 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
178 /** Remove an extension from the activated list */
179 static int RemoveActivated( extensions_manager_t *p_mgr, extension_t *p_ext )
181 if( p_mgr->p_sys->b_killed )
183 vlc_mutex_lock( &p_mgr->p_sys->lock );
187 FOREACH_ARRAY( p_iter, p_mgr->p_sys->activated_extensions )
195 assert( p_iter->psz_name != NULL );
196 if( !strcmp( p_iter->psz_name, p_ext->psz_name ) )
203 ARRAY_REMOVE( p_mgr->p_sys->activated_extensions, i_idx );
207 msg_Dbg( p_mgr, "Can't find extension '%s' in the activated list",
211 vlc_mutex_unlock( &p_mgr->p_sys->lock );
212 return (i_idx >= 0) ? VLC_SUCCESS : VLC_EGENERIC;
215 void KillExtension( extensions_manager_t *p_mgr, extension_t *p_ext )
217 /* Cancel thread if it seems stuck for a while */
218 msg_Dbg( p_mgr, "Killing extension now" );
219 vlc_cancel( p_ext->p_sys->thread );
220 lua_ExtensionDeactivate( p_mgr, p_ext );
221 p_ext->p_sys->b_exiting = true;
222 RemoveActivated( p_mgr, p_ext );
225 /** Push a UI command */
226 int __PushCommand( extension_t *p_ext, bool b_unique, command_type_e i_command,
229 vlc_mutex_lock( &p_ext->p_sys->command_lock );
232 struct command_t *cmd = calloc( 1, sizeof( struct command_t ) );
233 cmd->i_command = i_command;
237 cmd->data[0] = va_arg( args, void* );
239 case CMD_TRIGGERMENU:
241 int *pi = malloc( sizeof( int ) );
245 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
248 *pi = va_arg( args, int );
252 case CMD_PLAYING_CHANGED:
254 int *pi = malloc( sizeof( int ) );
258 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
261 *pi = va_arg( args, int );
267 case CMD_UPDATE_META:
268 // Nothing to do here
271 msg_Dbg( p_ext->p_sys->p_mgr,
272 "Unknown command send to extension: %d", i_command );
276 /* Push command to the end of the queue */
277 struct command_t *last = p_ext->p_sys->command;
280 p_ext->p_sys->command = cmd;
285 while( last->next != NULL )
287 if( b_unique && last->i_command == i_command )
289 // Do not push this 'unique' command a second time
290 b_skip = !memcmp( last->data, cmd->data, sizeof( cmd->data ) );
308 vlc_cond_signal( &p_ext->p_sys->wait );
309 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
314 static void* Run( void *data )
316 extension_t *p_ext = data;
317 extensions_manager_t *p_mgr = p_ext->p_sys->p_mgr;
319 int cancel = vlc_savecancel();
320 vlc_mutex_lock( &p_ext->p_sys->command_lock );
322 while( !p_ext->p_sys->b_exiting )
324 /* Pop command in front */
325 struct command_t *cmd = p_ext->p_sys->command;
326 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
327 vlc_restorecancel( cancel );
332 if( LockExtension( p_ext ) )
334 mutex_cleanup_push( &p_ext->p_sys->running_lock );
335 switch( cmd->i_command )
339 if( lua_ExecuteFunction( p_mgr, p_ext, "activate", LUA_END ) < 0 )
341 msg_Err( p_mgr, "Could not activate extension!" );
342 Deactivate( p_mgr, p_ext );
350 msg_Dbg( p_mgr, "Deactivating '%s'", p_ext->psz_title );
351 if( lua_ExtensionDeactivate( p_mgr, p_ext ) < 0 )
353 msg_Warn( p_mgr, "Extension '%s' did not deactivate properly",
356 p_ext->p_sys->b_exiting = true;
357 RemoveActivated( p_mgr, p_ext );
363 lua_ExecuteFunction( p_mgr, p_ext, "close", LUA_END );
369 extension_widget_t *p_widget = cmd->data[0];
371 msg_Dbg( p_mgr, "Clicking '%s': '%s'",
372 p_ext->psz_name, p_widget->psz_text );
373 if( !lua_ExtensionWidgetClick( p_mgr, p_ext, p_widget )
376 msg_Warn( p_mgr, "Could not translate click" );
381 case CMD_TRIGGERMENU:
383 int *pi_id = cmd->data[0];
385 msg_Dbg( p_mgr, "Trigger menu %d of '%s'",
386 *pi_id, p_ext->psz_name );
387 lua_ExtensionTriggerMenu( p_mgr, p_ext, *pi_id );
393 lua_ExecuteFunction( p_mgr, p_ext, "input_changed", LUA_END );
397 case CMD_UPDATE_META:
399 lua_ExecuteFunction( p_mgr, p_ext, "meta_changed", LUA_END );
403 case CMD_PLAYING_CHANGED:
405 lua_ExecuteFunction( p_mgr, p_ext, "playing_changed",
406 LUA_NUM, *((int *)cmd->data[0]), LUA_END );
412 msg_Dbg( p_mgr, "Unknown command in extension command queue: %d",
418 UnlockExtension( p_ext );
422 cancel = vlc_savecancel();
423 vlc_mutex_lock( &p_ext->p_sys->command_lock );
424 if( p_ext->p_sys->command )
426 cmd = p_ext->p_sys->command;
427 p_ext->p_sys->command = cmd->next;
428 cmd->next = NULL; // This prevents FreeCommands from freeing next
432 if( !p_ext->p_sys->b_exiting && !p_ext->p_sys->command )
434 vlc_cond_wait( &p_ext->p_sys->wait, &p_ext->p_sys->command_lock );
438 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
439 msg_Dbg( p_mgr, "Extension thread end: '%s'", p_ext->psz_title );
440 vlc_restorecancel( cancel );
442 // Note: At this point, the extension should be deactivated