Merge mlt++/CUSTOMISING into docs/melted++.
[melted] / src / miracle / miracle_commands.c
index 8a492d1..c95a4d0 100644 (file)
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <dirent.h>
 #include <pthread.h>
 
-#include "dvunit.h"
-#include "global_commands.h"
-#include "raw1394util.h"
-#include <libavc1394/rom1394.h>
-#include "log.h"
+#include "miracle_unit.h"
+#include "miracle_commands.h"
+#include "miracle_log.h"
 
-static dv_unit g_units[MAX_UNITS];
+static miracle_unit g_units[MAX_UNITS];
 
 
-/** Return the dv_unit given a numeric index.
+/** Return the miracle_unit given a numeric index.
 */
 
-dv_unit dv1394d_get_unit( int n )
+miracle_unit miracle_get_unit( int n )
 {
        if (n < MAX_UNITS)
                return g_units[n];
@@ -53,19 +47,19 @@ dv_unit dv1394d_get_unit( int n )
                return NULL;
 }
 
-/** Destroy the dv_unit given its numeric index.
+/** Destroy the miracle_unit given its numeric index.
 */
 
-void dv1394d_delete_unit( int n )
+void miracle_delete_unit( int n )
 {
        if (n < MAX_UNITS)
        {
-               dv_unit unit = dv1394d_get_unit(n);
+               miracle_unit unit = miracle_get_unit(n);
                if (unit != NULL)
                {
-                       dv_unit_close( unit );
+                       miracle_unit_close( unit );
                        g_units[ n ] = NULL;
-                       dv1394d_log( LOG_NOTICE, "Deleted unit U%d.", n ); 
+                       miracle_log( LOG_NOTICE, "Deleted unit U%d.", n ); 
                }
        }
 }
@@ -73,67 +67,40 @@ void dv1394d_delete_unit( int n )
 /** Destroy all allocated units on the server.
 */
 
-void dv1394d_delete_all_units( void )
+void miracle_delete_all_units( void )
 {
        int i;
        for (i = 0; i < MAX_UNITS; i++)
-               if ( dv1394d_get_unit(i) != NULL )
+       {
+               if ( miracle_get_unit(i) != NULL )
                {
-                       dv_unit_close( dv1394d_get_unit(i) );
-                       dv1394d_log( LOG_NOTICE, "Deleted unit U%d.", i ); 
+                       miracle_unit_close( miracle_get_unit(i) );
+                       miracle_log( LOG_NOTICE, "Deleted unit U%d.", i ); 
                }
+       }
 }
 
 /** Add a DV virtual vtr to the server.
 */
-response_codes dv1394d_add_unit( command_argument cmd_arg )
+response_codes miracle_add_unit( command_argument cmd_arg )
 {
-       int i;
-       int channel = -1;
-       char *guid_str = (char*) cmd_arg->argument;
-       octlet_t guid;
-       uint32_t guid_hi;
-       uint32_t guid_lo;
-       
-       sscanf( guid_str, "%08x%08x", &guid_hi, &guid_lo );
-       guid = (octlet_t)guid_hi << 32 | (octlet_t) guid_lo;
+       int i = 0;
+       for ( i = 0; i < MAX_UNITS; i ++ )
+               if ( g_units[ i ] == NULL )
+                       break;
 
-       if ( dv_tokeniser_count( cmd_arg->tokeniser ) == 3 )
-               channel = atoi( dv_tokeniser_get_string( cmd_arg->tokeniser, 2 ) );
-
-       /* make sure unit does not already exit */
-       for (i = 0; i < MAX_UNITS; i++)
-       {
-               if (g_units[i] != NULL)
-                       if ( dv_unit_get_guid( g_units[i] ) == guid )
-                       {
-                               dv_response_printf( cmd_arg->response, 1024, "a unit already exists for that node\n\n" );
-                               return RESPONSE_ERROR;
-                       }
-       }
-       
-       for (i = 0; i < MAX_UNITS; i++)
+       if ( i < MAX_UNITS )
        {
-               if (g_units[i] == NULL)
+               char *arg = cmd_arg->argument;
+               g_units[ i ] = miracle_unit_init( i, arg );
+               if ( g_units[ i ] != NULL )
                {
-               
-                       g_units[ i ] = dv_unit_init( guid, channel );
-                       if ( g_units[ i ] == NULL )
-                       {
-                               dv_response_printf( cmd_arg->response, 1024, "failed to allocate unit\n" );
-                               return RESPONSE_ERROR;
-                       }
-                       g_units[ i ]->unit = i;
-                       dv_unit_set_notifier( g_units[ i ], dv_parser_get_notifier( cmd_arg->parser ), cmd_arg->root_dir );
-
-                       dv1394d_log( LOG_NOTICE, "added unit %d to send to node %d over channel %d", 
-                               i, dv_unit_get_nodeid( g_units[i] ), dv_unit_get_channel( g_units[i] ) );
-                       dv_response_printf( cmd_arg->response, 10, "U%1d\n\n", i );
-                       return RESPONSE_SUCCESS_N;
+                       miracle_unit_set_notifier( g_units[ i ], valerie_parser_get_notifier( cmd_arg->parser ), cmd_arg->root_dir );
+                       valerie_response_printf( cmd_arg->response, 10, "U%1d\n\n", i );
                }
+               return g_units[ i ] != NULL ? RESPONSE_SUCCESS_N : RESPONSE_ERROR;
        }
-       
-       dv_response_printf( cmd_arg->response, 1024, "no more units can be created\n\n" );
+       valerie_response_printf( cmd_arg->response, 1024, "no more units can be created\n\n" );
 
        return RESPONSE_ERROR;
 }
@@ -141,79 +108,45 @@ response_codes dv1394d_add_unit( command_argument cmd_arg )
 
 /** List all AV/C nodes on the bus.
 */
-response_codes dv1394d_list_nodes( command_argument cmd_arg )
+response_codes miracle_list_nodes( command_argument cmd_arg )
 {
        response_codes error = RESPONSE_SUCCESS_N;
-       raw1394handle_t handle;
-       int i, j;
-       char line[1024];
-       octlet_t guid;
-       rom1394_directory dir;
-
-       for ( j = 0; j < raw1394_get_num_ports(); j++ )
-       {
-               handle = raw1394_open(j);
-               for ( i = 0; i < raw1394_get_nodecount(handle); ++i )
-               {
-                       rom1394_get_directory( handle, i, &dir);
-                       if ( (rom1394_get_node_type(&dir) == ROM1394_NODE_TYPE_AVC) )
-                       {
-                               guid = rom1394_get_guid(handle, i);
-                               if (dir.label != NULL)
-                               {
-                                       snprintf( line, 1023, "%02d %08x%08x \"%s\"\n", i, 
-                                               (quadlet_t) (guid>>32), (quadlet_t) (guid & 0xffffffff), dir.label );
-                               } else {
-                                       snprintf( line, 1023, "%02d %08x%08x \"Unlabeled Node %d\"\n", i, 
-                                               (quadlet_t) (guid>>32), (quadlet_t) (guid & 0xffffffff), i );
-                               }
-                               dv_response_write( cmd_arg->response, line, strlen(line) );
-                               rom1394_free_directory( &dir);
-                       }
-               }
-               raw1394_close( handle );
-       }
-       dv_response_write( cmd_arg->response, "\n", 1 );
        return error;
 }
 
 
 /** List units already added to server.
 */
-response_codes dv1394d_list_units( command_argument cmd_arg )
+response_codes miracle_list_units( command_argument cmd_arg )
 {
        response_codes error = RESPONSE_SUCCESS_N;
-       char line[1024];
-       int i;
-       
-       for (i = 0; i < MAX_UNITS; i++)
+       int i = 0;
+
+       for ( i = 0; i < MAX_UNITS; i ++ )
        {
-               if (dv1394d_get_unit(i) != NULL)
+               miracle_unit unit = miracle_get_unit( i );
+               if ( unit != NULL )
                {
-                       snprintf( line, 1023, "U%d %02d %08x%08x %d\n", i, dv_unit_get_nodeid(g_units[i]),
-                       (quadlet_t) (dv_unit_get_guid(g_units[i]) >> 32), 
-                       (quadlet_t) (dv_unit_get_guid(g_units[i]) & 0xffffffff),
-                       !dv_unit_is_offline( g_units[i] ) );
-                       dv_response_write( cmd_arg->response, line, strlen(line) );
+                       mlt_properties properties = unit->properties;
+                       char *constructor = mlt_properties_get( properties, "constructor" );
+                       int node = mlt_properties_get_int( properties, "node" );
+                       int online = !mlt_properties_get_int( properties, "offline" );
+                       valerie_response_printf( cmd_arg->response, 1024, "U%d %02d %s %d\n", i, node, constructor, online );
                }
        }
-       dv_response_write( cmd_arg->response, "\n", 1 );
+       valerie_response_printf( cmd_arg->response, 1024, "\n" );
 
        return error;
 }
 
-static int
-filter_files( const struct dirent *de )
+static int filter_files( const struct dirent *de )
 {
-       if ( de->d_name[ 0 ] != '.' )
-               return 1;
-       else
-               return 0;
+       return de->d_name[ 0 ] != '.';
 }
 
 /** List clips in a directory.
 */
-response_codes dv1394d_list_clips( command_argument cmd_arg )
+response_codes miracle_list_clips( command_argument cmd_arg )
 {
        response_codes error = RESPONSE_BAD_FILE;
        const char *dir_name = (const char*) cmd_arg->argument;
@@ -233,19 +166,19 @@ response_codes dv1394d_list_clips( command_argument cmd_arg )
                {
                        snprintf( fullname, 1023, "%s%s/%s", cmd_arg->root_dir, dir_name, de[i]->d_name );
                        if ( stat( fullname, &info ) == 0 && S_ISDIR( info.st_mode ) )
-                               dv_response_printf( cmd_arg->response, 1024, "\"%s/\"\n", de[i]->d_name );
+                               valerie_response_printf( cmd_arg->response, 1024, "\"%s/\"\n", de[i]->d_name );
                }
                for (i = 0; i < n; i++ )
                {
                        snprintf( fullname, 1023, "%s%s/%s", cmd_arg->root_dir, dir_name, de[i]->d_name );
                        if ( lstat( fullname, &info ) == 0 && 
-                                ( S_ISREG( info.st_mode ) || ( strstr( fullname, ".clip" ) && info.st_mode | S_IXUSR ) ) )
-                               dv_response_printf( cmd_arg->response, 1024, "\"%s\" %llu\n", de[i]->d_name, (unsigned long long) info.st_size );
+                                ( S_ISREG( info.st_mode ) || S_ISLNK( info.st_mode ) || ( strstr( fullname, ".clip" ) && info.st_mode | S_IXUSR ) ) )
+                               valerie_response_printf( cmd_arg->response, 1024, "\"%s\" %llu\n", de[i]->d_name, (unsigned long long) info.st_size );
                        free( de[ i ] );
                }
                free( de );
                closedir( dir );
-               dv_response_write( cmd_arg->response, "\n", 1 );
+               valerie_response_write( cmd_arg->response, "\n", 1 );
        }
 
        return error;
@@ -254,7 +187,7 @@ response_codes dv1394d_list_clips( command_argument cmd_arg )
 /** Set a server configuration property.
 */
 
-response_codes dv1394d_set_global_property( command_argument cmd_arg )
+response_codes miracle_set_global_property( command_argument cmd_arg )
 {
        char *key = (char*) cmd_arg->argument;
        char *value = NULL;
@@ -264,7 +197,7 @@ response_codes dv1394d_set_global_property( command_argument cmd_arg )
                return RESPONSE_OUT_OF_RANGE;
        *value = 0;
        value++;
-       dv1394d_log( LOG_DEBUG, "SET %s = %s", key, value );
+       miracle_log( LOG_DEBUG, "SET %s = %s", key, value );
 
        if ( strncasecmp( key, "root", 1024) == 0 )
        {
@@ -275,14 +208,14 @@ response_codes dv1394d_set_global_property( command_argument cmd_arg )
                for (i = 0; i < MAX_UNITS; i++)
                {
                        if (g_units[i] != NULL)
-                               dv_unit_terminate( g_units[i] );
+                               miracle_unit_terminate( g_units[i] );
                }
 
                /* set the property */
                strncpy( cmd_arg->root_dir, value, 1023 );
 
                /* add a trailing slash if needed */
-               if ( cmd_arg->root_dir[ len - 1 ] != '/')
+               if ( len && cmd_arg->root_dir[ len - 1 ] != '/')
                {
                        cmd_arg->root_dir[ len ] = '/';
                        cmd_arg->root_dir[ len + 1 ] = '\0';
@@ -297,13 +230,13 @@ response_codes dv1394d_set_global_property( command_argument cmd_arg )
 /** Get a server configuration property.
 */
 
-response_codes dv1394d_get_global_property( command_argument cmd_arg )
+response_codes miracle_get_global_property( command_argument cmd_arg )
 {
        char *key = (char*) cmd_arg->argument;
 
        if ( strncasecmp( key, "root", 1024) == 0 )
        {
-               dv_response_write( cmd_arg->response, cmd_arg->root_dir, strlen(cmd_arg->root_dir) );
+               valerie_response_write( cmd_arg->response, cmd_arg->root_dir, strlen(cmd_arg->root_dir) );
                return RESPONSE_SUCCESS_1;
        }
        else
@@ -312,142 +245,4 @@ response_codes dv1394d_get_global_property( command_argument cmd_arg )
        return RESPONSE_SUCCESS;
 }
 
-/** IEEE 1394 Bus Reset handler 
-
-    This is included here for now due to all the unit management involved.
-*/
-
-static int reset_handler( raw1394handle_t h, unsigned int generation )
-{
-       int i, j, count, retry = 3;
-       int port = (int) raw1394_get_userdata( h );
-       
-       raw1394_update_generation( h, generation );
-       dv1394d_log( LOG_NOTICE, "bus reset on port %d", port );
-
-       while ( retry-- > 0 ) 
-       {
-               raw1394handle_t handle = raw1394_open( port );
-               count = raw1394_get_nodecount( handle );
-               
-               if ( count > 0 )
-               {
-                       dv1394d_log( LOG_DEBUG, "bus reset, checking units" );
-                       
-                       /* suspend all units on this port */
-                       for ( j = MAX_UNITS; j > 0; j-- )
-                       {
-                               if ( g_units[ j-1 ] != NULL && dv_unit_get_port( g_units[ j-1 ] ) == port )
-                                       dv_unit_suspend( g_units[ j-1 ] );
-                       }
-                       dv1394d_log( LOG_DEBUG, "All units are now stopped" );
-                       
-                       /* restore units with known guid, take others offline */
-                       for ( j = 0; j < MAX_UNITS; j++ )
-                       {
-                               if ( g_units[j] != NULL && 
-                                       ( dv_unit_get_port( g_units[ j ] ) == port || dv_unit_get_port( g_units[ j ] ) == -1 ) )
-                               {
-                                       int found = 0;
-                                       for ( i = 0; i < count; i++ )
-                                       {
-                                               octlet_t guid;
-                                               dv1394d_log( LOG_DEBUG, "attempting to get guid for node %d", i );
-                                               guid = rom1394_get_guid( handle, i );
-                                               if ( guid == g_units[ j ]->guid )
-                                               {
-                                                       dv1394d_log( LOG_NOTICE, "unit with GUID %08x%08x found", 
-                                                               (quadlet_t) (g_units[j]->guid>>32), (quadlet_t) (g_units[j]->guid & 0xffffffff));
-                                                       if ( dv_unit_is_offline( g_units[ j ] ) )
-                                                               dv_unit_online( g_units[ j ] );
-                                                       else
-                                                               dv_unit_restore( g_units[ j ] );
-                                                       found = 1;
-                                                       break;
-                                               }
-                                       }
-                                       if ( found == 0 )
-                                               dv_unit_offline( g_units[ j ] );
-                               }
-                       }
-                       dv1394d_log( LOG_DEBUG, "completed bus reset handler");
-                       raw1394_close( handle );
-                       return 0;
-               }
-               raw1394_close( handle );
-       }
-       dv1394d_log( LOG_CRIT, "raw1394 reported zero nodes on the bus!" );
-       return 0;
-}
-
-
-/** One pthread per IEEE 1394 port
-*/
-
-static pthread_t raw1394service_thread[4];
-
-/** One raw1394 handle for each pthread/port
-*/
-
-static raw1394handle_t raw1394service_handle[4];
-
-/** The service thread that polls raw1394 for new events.
-*/
-
-static void* raw1394_service( void *arg )
-{
-       raw1394handle_t handle = (raw1394handle_t) arg;
-       struct pollfd raw1394_poll;
-       raw1394_poll.fd = raw1394_get_fd( handle );
-       raw1394_poll.events = POLLIN;
-       raw1394_poll.revents = 0;
-       while ( 1 )
-       {
-               if ( poll( &raw1394_poll, 1, 200) > 0 )
-               {
-                       if ( (raw1394_poll.revents & POLLIN) 
-                                       || (raw1394_poll.revents & POLLPRI) )
-                               raw1394_loop_iterate( handle );
-               }
-               pthread_testcancel();
-       }
-       
-}
-
-
-/** Start the raw1394 service threads for handling bus reset.
-
-    One thread is launched per port on the system.
-*/
-
-void raw1394_start_service_threads( void )
-{
-       int port;
-       for ( port = 0; port < raw1394_get_num_ports(); port++ )
-       {
-               raw1394service_handle[port] = raw1394_open( port );
-               raw1394_set_bus_reset_handler( raw1394service_handle[port], reset_handler );
-               pthread_create( &(raw1394service_thread[port]), NULL, raw1394_service, raw1394service_handle[port] );
-       }
-       for ( ; port < 4; port++ )
-               raw1394service_handle[port] = NULL;
-}
-
-/** Shutdown all the raw1394 service threads.
-*/
-
-void raw1394_stop_service_threads( void )
-{
-       int i;
-       for ( i = 0; i < 4; i++ )
-       {
-               if ( raw1394service_handle[i] != NULL )
-               {
-                       pthread_cancel( raw1394service_thread[i] );
-                       pthread_join( raw1394service_thread[i], NULL );
-                       raw1394_close( raw1394service_handle[i] );
-               }
-       }
-}
-