]> git.sesse.net Git - vlc/blobdiff - modules/services_discovery/hal.c
Removes trailing spaces. Removes tabs.
[vlc] / modules / services_discovery / hal.c
index 0ec8957c42b78c2bf9c22bc83a45eff3af135e48..5b84eeced6ebf02a60f2ba62599e5b6ea8680fc8 100644 (file)
  *****************************************************************************/
 
 #include <vlc/vlc.h>
-#include <vlc/intf.h>
+#include <vlc_playlist.h>
 
-#include <vlc/input.h>
-
-#include "network.h"
+#include <vlc_network.h>
 
 #include <errno.h>                                                 /* ENOMEM */
 
@@ -46,7 +44,7 @@
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
 /* store relation between item id and udi for ejection */
 struct udi_input_id_t
 {
@@ -57,10 +55,11 @@ struct udi_input_id_t
 
 struct services_discovery_sys_t
 {
-    LibHalContext *p_ctx;
-    playlist_item_t *p_node_cat;
-    playlist_item_t *p_node_one;
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+    LibHalContext           *p_ctx;
+    playlist_item_t         *p_node_cat;
+    playlist_item_t         *p_node_one;
+#ifdef HAVE_HAL_1
+    DBusConnection          *p_connection;
     int                     i_devices_number;
     struct udi_input_id_t   **pp_devices;
 #endif
@@ -70,7 +69,7 @@ static void Run    ( services_discovery_t *p_intf );
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
 /* HAL callbacks */
 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi );
 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi );
@@ -103,7 +102,7 @@ static int Open( vlc_object_t *p_this )
 
     playlist_t          *p_playlist;
 
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
     DBusError           dbus_error;
     DBusConnection      *p_connection;
 
@@ -115,7 +114,7 @@ static int Open( vlc_object_t *p_this )
     p_sd->pf_run = Run;
     p_sd->p_sys  = p_sys;
 
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
     dbus_error_init( &dbus_error );
 
     p_sys->p_ctx = libhal_ctx_new();
@@ -134,12 +133,13 @@ static int Open( vlc_object_t *p_this )
         return VLC_EGENERIC;
     }
     libhal_ctx_set_dbus_connection( p_sys->p_ctx, p_connection );
+    p_sys->p_connection = p_connection;
     if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
 #else
     if( !(p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
 #endif
     {
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
         msg_Err( p_sd, "hal not available : %s", dbus_error.message );
         dbus_error_free( &dbus_error );
 #else
@@ -149,7 +149,7 @@ static int Open( vlc_object_t *p_this )
         return VLC_EGENERIC;
     }
 
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
         if( !libhal_ctx_set_device_added( p_sys->p_ctx, DeviceAdded ) ||
                 !libhal_ctx_set_device_removed( p_sys->p_ctx, DeviceRemoved ) )
         {
@@ -193,12 +193,17 @@ static void Close( vlc_object_t *p_this )
         vlc_object_release( p_playlist );
     }
     free( p_sys );
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
-    while( p_sys->i_devices_number > 0 );
+#ifdef HAVE_HAL_1
+    dbus_connection_unref( p_sys->p_connection );
+
+    struct udi_input_id_t *p_udi_entry;
+
+    while( p_sys->i_devices_number > 0 )
     {
-        struct udi_input_id_t *p_udi_entry = p_sys->pp_devices[0];
+        p_udi_entry = p_sys->pp_devices[0];
         if( p_udi_entry->psz_udi ) free( p_udi_entry->psz_udi );
-        TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices, 0 );
+        TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
+                p_sys->pp_devices[0] );
         if( p_udi_entry ) free( p_udi_entry );
     }
     p_sys->pp_devices = NULL;
@@ -206,12 +211,12 @@ static void Close( vlc_object_t *p_this )
 }
 
 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
                 ,char* psz_device
 #endif
                     )
 {
-    playlist_item_t *p_item;
+    playlist_item_t *p_item_cat, *p_item_one;
     services_discovery_sys_t *p_sys  = p_sd->p_sys;
     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
@@ -220,23 +225,25 @@ static void AddItem( services_discovery_t *p_sd, input_item_t * p_input
         msg_Err( p_sd, "playlist not found" );
         return;
     }
-    p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_cat,
-                                    PLAYLIST_APPEND, PLAYLIST_END );
-    p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
-    p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_one,
-                                    PLAYLIST_APPEND, PLAYLIST_END );
-    p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
+    p_item_cat = playlist_NodeAddInput( p_playlist,
+            p_input,p_sd->p_sys->p_node_cat, PLAYLIST_APPEND, PLAYLIST_END,
+            VLC_FALSE );
+    p_item_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
+    p_item_one = playlist_NodeAddInput( p_playlist,
+            p_input,p_sd->p_sys->p_node_one, PLAYLIST_APPEND, PLAYLIST_END,
+            VLC_FALSE );
+    p_item_one->i_flags &= ~PLAYLIST_SKIP_FLAG;
 
     vlc_object_release( p_playlist );
 
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
     struct udi_input_id_t *p_udi_entry;
     p_udi_entry = malloc( sizeof( struct udi_input_id_t ) );
     if( !p_udi_entry )
     {
         return;
     }
-    p_udi_entry->i_id = p_item->i_id;
+    p_udi_entry->i_id = p_item_cat->i_id;
     p_udi_entry->psz_udi = strdup( psz_device );
     TAB_APPEND( p_sys->i_devices_number, p_sys->pp_devices, p_udi_entry );
 #endif
@@ -248,7 +255,7 @@ static void AddDvd( services_discovery_t *p_sd, char *psz_device )
     char *psz_uri;
     char *psz_blockdevice;
     input_item_t        *p_input;
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
     psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
                                         psz_device, "volume.label", NULL );
     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
@@ -267,18 +274,19 @@ static void AddDvd( services_discovery_t *p_sd, char *psz_device )
     {
         return;
     }
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
     AddItem( p_sd, p_input, psz_device );
 #else
     AddItem( p_sd, p_input );
 #endif
 }
 
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
 static void DelItem( services_discovery_t *p_sd, char* psz_udi )
 {
     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
-    int                         i;
+    int                         i,j;
+    playlist_item_t             *p_pl_item;
 
     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
@@ -289,10 +297,34 @@ static void DelItem( services_discovery_t *p_sd, char* psz_udi )
     }
 
     for( i = 0; i < p_sys->i_devices_number; i++ )
-    {
+    { /*  looks for a matching udi */
         if( strcmp( psz_udi, p_sys->pp_devices[i]->psz_udi ) == 0 )
-        {
-            playlist_DeleteFromItemId( p_playlist, p_sys->pp_devices[i]->i_id );
+        { /* delete the corresponding item */
+            p_pl_item = playlist_ItemGetById( p_playlist,
+                p_sys->pp_devices[i]->i_id, VLC_FALSE );
+            if( p_pl_item )
+            {
+                j = 0;
+                while( p_pl_item->i_children > 0 )
+                { /* delete all childs */
+                    playlist_DeleteFromInput( p_playlist,
+                        p_pl_item->pp_children[j]->p_input->i_id, VLC_FALSE );
+                }
+                /* delete parent item */
+
+                /* HACK: if i_children == 0 the item won't be deleted
+                 * That means that it _had_ children but they were deleted */
+                if( p_pl_item->i_children == 0 )
+                    p_pl_item->i_children = -1;
+
+                playlist_DeleteFromInput( p_playlist,
+                    p_pl_item->p_input->i_id, VLC_FALSE );
+            }
+
+            if( p_sys->pp_devices[i]->psz_udi )
+                free( p_sys->pp_devices[i]->psz_udi );
+            TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
+                    p_sys->pp_devices[i] );
         }
     }
 
@@ -305,7 +337,7 @@ static void AddCdda( services_discovery_t *p_sd, char *psz_device )
     char *psz_uri;
     char *psz_blockdevice;
     input_item_t     *p_input;
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
                                             psz_device, "block.device", NULL );
 #else
@@ -318,7 +350,7 @@ static void AddCdda( services_discovery_t *p_sd, char *psz_device )
     free( psz_uri );
     if( !p_input )
         return;
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
     AddItem( p_sd, p_input, psz_device );
 #else
     AddItem( p_sd, p_input );
@@ -329,7 +361,7 @@ static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
 {
     char *psz_disc_type;
     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
     if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
                                        "volume.disc.type", NULL ) )
     {
@@ -345,9 +377,9 @@ static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
                                                         psz_device,
                                                         "volume.disc.type" );
 #endif
-        if( !strcmp( psz_disc_type, "dvd_rom" ) )
+        if( !strncmp( psz_disc_type, "dvd_r", 5 ) )
         {
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
             /* hal 0.2.9.7 (HAVE_HAL) has not is_videodvd
              * but hal 0.5.0 (HAVE_HAL_1) has */
             if (libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
@@ -355,9 +387,9 @@ static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
 #endif
             AddDvd( p_sd, psz_device );
         }
-        else if( !strcmp( psz_disc_type, "cd_rom" ) )
+        else if( !strncmp( psz_disc_type, "cd_r", 4 ) )
         {
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
             if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
                                          "volume.disc.has_audio" , NULL ) )
 #else
@@ -381,7 +413,7 @@ static void Run( services_discovery_t *p_sd )
     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
 
     /* parse existing devices first */
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
     if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
 #else
     if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
@@ -390,7 +422,7 @@ static void Run( services_discovery_t *p_sd )
         for( i = 0; i < i_devices; i++ )
         {
             ParseDevice( p_sd, devices[ i ] );
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
             libhal_free_string( devices[ i ] );
 #else
             hal_free_string( devices[ i ] );
@@ -398,18 +430,18 @@ static void Run( services_discovery_t *p_sd )
 
         }
     }
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
     while( !p_sd->b_die )
     {
     /* look for events on the bus, blocking 1 second */
-    dbus_connection_read_write_dispatch(
-            libhal_ctx_get_dbus_connection(p_sys->p_ctx), 1000 );
+    dbus_connection_read_write_dispatch( p_sys->p_connection, 1000 );
+    /* HAL 0.5.8.1 can use libhal_ctx_get_dbus_connection(p_sys->p_ctx) */
     }
 #endif
 
 }
 
-#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
+#ifdef HAVE_HAL_1
 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi )
 {
         ParseDevice( p_sd_global, (char*) psz_udi );