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 bool IsActivated( extensions_manager_t *p_mgr, extension_t *p_ext )
91 assert( p_ext != NULL );
92 vlc_mutex_lock( &p_mgr->p_sys->lock );
95 FOREACH_ARRAY( p_iter, p_mgr->p_sys->activated_extensions )
99 assert( p_iter->psz_name != NULL );
100 if( !strcmp( p_iter->psz_name, p_ext->psz_name ) )
102 vlc_mutex_unlock( &p_mgr->p_sys->lock );
108 vlc_mutex_unlock( &p_mgr->p_sys->lock );
112 /** Recursively drop and free commands starting from "command" */
113 static void FreeCommands( struct command_t *command )
115 if( !command ) return;
116 struct command_t *next = command->next;
117 switch( command->i_command )
121 case CMD_CLICK: // Arg1 must not be freed
124 case CMD_TRIGGERMENU:
125 free( command->data[0] ); // Arg1 is int*, to free
132 FreeCommands( next );
135 /** Deactivate this extension: pushes immediate command and drops queued */
136 int Deactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
139 vlc_mutex_lock( &p_ext->p_sys->command_lock );
141 if( p_ext->p_sys->b_exiting )
143 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
147 /* Free the list of commands */
148 FreeCommands( p_ext->p_sys->command );
151 struct command_t *cmd = calloc( 1, sizeof( struct command_t ) );
152 cmd->i_command = CMD_DEACTIVATE;
153 p_ext->p_sys->command = cmd;
155 vlc_cond_signal( &p_ext->p_sys->wait );
156 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
161 /** Remove an extension from the activated list */
162 static int RemoveActivated( extensions_manager_t *p_mgr, extension_t *p_ext )
164 if( p_mgr->p_sys->b_killed )
166 vlc_mutex_lock( &p_mgr->p_sys->lock );
170 FOREACH_ARRAY( p_iter, p_mgr->p_sys->activated_extensions )
178 assert( p_iter->psz_name != NULL );
179 if( !strcmp( p_iter->psz_name, p_ext->psz_name ) )
186 ARRAY_REMOVE( p_mgr->p_sys->activated_extensions, i_idx );
190 msg_Dbg( p_mgr, "Can't find extension '%s' in the activated list",
194 vlc_mutex_unlock( &p_mgr->p_sys->lock );
195 return (i_idx >= 0) ? VLC_SUCCESS : VLC_EGENERIC;
198 /** Wait for an extension to finish */
199 void WaitForDeactivation( extension_t *p_ext )
201 vlc_join( p_ext->p_sys->thread, NULL );
204 /** Push a UI command */
205 int __PushCommand( extension_t *p_ext, bool b_unique, int i_command,
208 vlc_mutex_lock( &p_ext->p_sys->command_lock );
211 struct command_t *cmd = calloc( 1, sizeof( struct command_t ) );
212 cmd->i_command = i_command;
216 cmd->data[0] = va_arg( args, void* );
218 case CMD_TRIGGERMENU:
220 int *pi = malloc( sizeof( int ) );
224 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
227 *pi = va_arg( args, int );
233 case CMD_UPDATE_META:
234 // Nothing to do here
237 msg_Dbg( p_ext->p_sys->p_mgr,
238 "Unknown command send to extension: %d", i_command );
242 /* Push command to the end of the queue */
243 struct command_t *last = p_ext->p_sys->command;
246 p_ext->p_sys->command = cmd;
251 while( last->next != NULL )
253 if( b_unique && last->i_command == i_command )
255 // Do not push this 'unique' command a second time
268 vlc_cond_signal( &p_ext->p_sys->wait );
269 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
274 static void* Run( void *data )
276 extension_t *p_ext = data;
277 extensions_manager_t *p_mgr = p_ext->p_sys->p_mgr;
279 vlc_mutex_lock( &p_ext->p_sys->command_lock );
281 while( !p_ext->p_sys->b_exiting )
283 /* Pop command in front */
284 struct command_t *cmd = p_ext->p_sys->command;
287 p_ext->p_sys->command = cmd->next;
288 cmd->next = NULL; // This prevents FreeCommands from freeing next
291 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
296 if( LockExtension( p_ext ) )
298 switch( cmd->i_command )
302 if( lua_ExecuteFunction( p_mgr, p_ext, "activate", LUA_END ) < 0 )
304 msg_Dbg( p_mgr, "Could not activate extension!" );
305 Deactivate( p_mgr, p_ext );
312 msg_Dbg( p_mgr, "Deactivating '%s'", p_ext->psz_title );
313 if( lua_ExtensionDeactivate( p_mgr, p_ext ) < 0 )
315 msg_Warn( p_mgr, "Extension '%s' did not deactivate properly",
318 p_ext->p_sys->b_exiting = true;
319 RemoveActivated( p_mgr, p_ext );
325 lua_ExecuteFunction( p_mgr, p_ext, "close", LUA_END );
331 extension_widget_t *p_widget = cmd->data[0];
333 msg_Dbg( p_mgr, "Clicking '%s': '%s'",
334 p_ext->psz_name, p_widget->psz_text );
335 if( !lua_ExtensionWidgetClick( p_mgr, p_ext, p_widget )
338 msg_Warn( p_mgr, "Could not translate click" );
343 case CMD_TRIGGERMENU:
345 int *pi_id = cmd->data[0];
347 msg_Dbg( p_mgr, "Trigger menu %d of '%s'",
348 *pi_id, p_ext->psz_name );
349 lua_ExtensionTriggerMenu( p_mgr, p_ext, *pi_id );
355 lua_ExecuteFunction( p_mgr, p_ext, "input_changed", LUA_END );
359 case CMD_UPDATE_META:
361 lua_ExecuteFunction( p_mgr, p_ext, "meta_changed", LUA_END );
367 msg_Dbg( p_mgr, "Unknown command in extension command queue: %d",
372 UnlockExtension( p_ext );
378 vlc_mutex_lock( &p_ext->p_sys->command_lock );
379 if( !p_ext->p_sys->b_exiting && !p_ext->p_sys->command )
381 vlc_cond_wait( &p_ext->p_sys->wait, &p_ext->p_sys->command_lock );
385 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
386 msg_Dbg( p_mgr, "Extension thread end: '%s'", p_ext->psz_title );
388 // Note: At this point, the extension should be deactivated