]> git.sesse.net Git - vlc/blobdiff - modules/misc/lua/extension_thread.c
vod: use memcmp()
[vlc] / modules / misc / lua / extension_thread.c
index ecd21452183e64c537cf4b220c6a6c48254a5c50..78a260872c8ba413948b4251d2edc787a15c1323 100644 (file)
@@ -122,8 +122,12 @@ static void FreeCommands( struct command_t *command )
             break;
 
         case CMD_TRIGGERMENU:
+        case CMD_PLAYING_CHANGED:
             free( command->data[0] ); // Arg1 is int*, to free
             break;
+
+        default:
+            break;
     }
     free( command );
     FreeCommands( next );
@@ -142,12 +146,16 @@ int Deactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
     }
 
     /* Free the list of commands */
-    FreeCommands( p_ext->p_sys->command );
+    if( p_ext->p_sys->command )
+        FreeCommands( p_ext->p_sys->command->next );
 
     /* Push command */
     struct command_t *cmd = calloc( 1, sizeof( struct command_t ) );
     cmd->i_command = CMD_DEACTIVATE;
-    p_ext->p_sys->command = cmd;
+    if( p_ext->p_sys->command )
+        p_ext->p_sys->command->next = cmd;
+    else
+        p_ext->p_sys->command = cmd;
 
     vlc_cond_signal( &p_ext->p_sys->wait );
     vlc_mutex_unlock( &p_ext->p_sys->command_lock );
@@ -195,21 +203,15 @@ static int RemoveActivated( extensions_manager_t *p_mgr, extension_t *p_ext )
 /** Wait for an extension to finish */
 void WaitForDeactivation( extension_t *p_ext )
 {
-    void *pointer = NULL;
-    vlc_cond_signal( &p_ext->p_sys->wait );
-    vlc_join( p_ext->p_sys->thread, &pointer );
+    vlc_join( p_ext->p_sys->thread, NULL );
 }
 
 /** Push a UI command */
-int PushCommand( extension_t *p_ext,
-                 int i_command,
-                 ... )
+int __PushCommand( extension_t *p_ext,  bool b_unique, int i_command,
+                   va_list args )
 {
     vlc_mutex_lock( &p_ext->p_sys->command_lock );
 
-    va_list args;
-    va_start( args, i_command );
-
     /* Create command */
     struct command_t *cmd = calloc( 1, sizeof( struct command_t ) );
     cmd->i_command = i_command;
@@ -218,9 +220,6 @@ int PushCommand( extension_t *p_ext,
         case CMD_CLICK:
             cmd->data[0] = va_arg( args, void* );
             break;
-        case CMD_CLOSE:
-            // Nothing to do here
-            break;
         case CMD_TRIGGERMENU:
             {
                 int *pi = malloc( sizeof( int ) );
@@ -234,14 +233,30 @@ int PushCommand( extension_t *p_ext,
                 cmd->data[0] = pi;
             }
             break;
+        case CMD_PLAYING_CHANGED:
+            {
+                int *pi = malloc( sizeof( int ) );
+                if( !pi )
+                {
+                    free( cmd );
+                    vlc_mutex_unlock( &p_ext->p_sys->command_lock );
+                    return VLC_ENOMEM;
+                }
+                *pi = va_arg( args, int );
+                cmd->data[0] = pi;
+            }
+            break;
+        case CMD_CLOSE:
+        case CMD_SET_INPUT:
+        case CMD_UPDATE_META:
+            // Nothing to do here
+            break;
         default:
             msg_Dbg( p_ext->p_sys->p_mgr,
                      "Unknown command send to extension: %d", i_command );
             break;
     }
 
-    va_end( args );
-
     /* Push command to the end of the queue */
     struct command_t *last = p_ext->p_sys->command;
     if( !last )
@@ -250,11 +265,28 @@ int PushCommand( extension_t *p_ext,
     }
     else
     {
+        bool b_skip = false;
         while( last->next != NULL )
         {
-            last = last->next;
+            if( b_unique && last->i_command == i_command )
+            {
+                // Do not push this 'unique' command a second time
+                b_skip = !memcmp( last->data, cmd->data, sizeof( cmd->data ) );
+                break;
+            }
+            else
+            {
+                last = last->next;
+            }
+        }
+        if( !b_skip )
+        {
+            last->next = cmd;
+        }
+        else
+        {
+            FreeCommands( cmd );
         }
-        last->next = cmd;
     }
 
     vlc_cond_signal( &p_ext->p_sys->wait );
@@ -274,11 +306,6 @@ static void* Run( void *data )
     {
         /* Pop command in front */
         struct command_t *cmd = p_ext->p_sys->command;
-        if( cmd )
-        {
-            p_ext->p_sys->command = cmd->next;
-        }
-
         vlc_mutex_unlock( &p_ext->p_sys->command_lock );
 
         /* Run command */
@@ -290,10 +317,11 @@ static void* Run( void *data )
                 {
                     case CMD_ACTIVATE:
                     {
-                        if( lua_ExecuteFunction( p_mgr, p_ext, "activate" ) < 0 )
+                        if( lua_ExecuteFunction( p_mgr, p_ext, "activate", LUA_END ) < 0 )
                         {
                             msg_Dbg( p_mgr, "Could not activate extension!" );
                             Deactivate( p_mgr, p_ext );
+                            cmd = NULL;
                         }
                         break;
                     }
@@ -313,7 +341,7 @@ static void* Run( void *data )
 
                     case CMD_CLOSE:
                     {
-                        lua_ExecuteFunction( p_mgr, p_ext, "close" );
+                        lua_ExecuteFunction( p_mgr, p_ext, "close", LUA_END );
                         break;
                     }
 
@@ -338,13 +366,25 @@ static void* Run( void *data )
                         msg_Dbg( p_mgr, "Trigger menu %d of '%s'",
                                  *pi_id, p_ext->psz_name );
                         lua_ExtensionTriggerMenu( p_mgr, p_ext, *pi_id );
-                        free( pi_id );
                         break;
                     }
 
                     case CMD_SET_INPUT:
                     {
-                        lua_ExecuteFunction( p_mgr, p_ext, "input_changed" );
+                        lua_ExecuteFunction( p_mgr, p_ext, "input_changed", LUA_END );
+                        break;
+                    }
+
+                    case CMD_UPDATE_META:
+                    {
+                        lua_ExecuteFunction( p_mgr, p_ext, "meta_changed", LUA_END );
+                        break;
+                    }
+
+                    case CMD_PLAYING_CHANGED:
+                    {
+                        lua_ExecuteFunction( p_mgr, p_ext, "playing_changed",
+                                LUA_NUM, *((int *)cmd->data[0]), LUA_END );
                         break;
                     }
 
@@ -360,6 +400,14 @@ static void* Run( void *data )
         }
 
         vlc_mutex_lock( &p_ext->p_sys->command_lock );
+        if( p_ext->p_sys->command )
+        {
+            cmd = p_ext->p_sys->command;
+            p_ext->p_sys->command = cmd->next;
+            cmd->next = NULL; // This prevents FreeCommands from freeing next
+            FreeCommands( cmd );
+        }
+
         if( !p_ext->p_sys->b_exiting && !p_ext->p_sys->command )
         {
             vlc_cond_wait( &p_ext->p_sys->wait, &p_ext->p_sys->command_lock );
@@ -367,7 +415,7 @@ static void* Run( void *data )
     }
 
     vlc_mutex_unlock( &p_ext->p_sys->command_lock );
-    msg_Dbg( p_mgr, "Extension thread ending..." );
+    msg_Dbg( p_mgr, "Extension thread end: '%s'", p_ext->psz_title );
 
     // Note: At this point, the extension should be deactivated
     return NULL;