From d2f824447621dd8c5f4a8389041950ae62e97ebc Mon Sep 17 00:00:00 2001
From: basos <noxelia 4t gmail c0m>
Date: Thu, 30 Oct 2008 11:45:56 +0200
Subject: [PATCH] libvlc_MediaPlayerSnapshotTaken Event creation

It is emmited whenever a snapshot is taken by any means
(gui, libvlc, button ) and also passes the full path of
the new image as event data. Rev 1
---
 include/vlc/libvlc_events.h  |   11 ++++++++++-
 src/control/event.c          |    4 +++-
 src/control/media_player.c   |   38 ++++++++++++++++++++++++++++++++++++++
 src/video_output/vout_intf.c |    8 ++++++++
 4 files changed, 59 insertions(+), 2 deletions(-)

diff --git a/include/vlc/libvlc_events.h b/include/vlc/libvlc_events.h
index 1aa0f2f..1d59b9d 100644
--- a/include/vlc/libvlc_events.h
+++ b/include/vlc/libvlc_events.h
@@ -82,7 +82,10 @@ typedef enum libvlc_event_type_t {
     libvlc_MediaListPlayerStopped,
 
     libvlc_MediaDiscovererStarted,
-    libvlc_MediaDiscovererEnded
+    libvlc_MediaDiscovererEnded,
+
+   /* Added here to preserve ABI compatibility */
+   libvlc_MediaPlayerSnapshotTaken

 } libvlc_event_type_t;
 
@@ -197,6 +200,12 @@ typedef struct libvlc_event_t
             void * unused;
         } media_media_discoverer_ended;
 
+        /* snapshot taken */
+        struct
+        {
+             char* psz_filename ;
+        } media_player_snapshot_taken ;
+
     } u;
 } libvlc_event_t;
 
diff --git a/src/control/event.c b/src/control/event.c
index a98f23c..d2a91b5 100644
--- a/src/control/event.c
+++ b/src/control/event.c
@@ -268,7 +268,9 @@ static const char event_type_to_name[][35] =
     EVENT(libvlc_MediaListPlayerStopped),
 
     EVENT(libvlc_MediaDiscovererStarted),
-    EVENT(libvlc_MediaDiscovererEnded)
+    EVENT(libvlc_MediaDiscovererEnded),
+
+    EVENT(libvlc_MediaPlayerSnapshotTaken),
 #undef EVENT
 };
 
diff --git a/src/control/media_player.c b/src/control/media_player.c
index 0928900..cc913fd 100644
--- a/src/control/media_player.c
+++ b/src/control/media_player.c
@@ -49,6 +49,9 @@ input_time_changed( vlc_object_t * p_this, char const * psz_cmd,
                      vlc_value_t oldval, vlc_value_t newval,
                      void * p_userdata );
 
+static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
+                       vlc_value_t oldval, vlc_value_t newval, void *p_data );
+
 static const libvlc_state_t vlc_to_libvlc_state_array[] =
 {
     [INIT_S]        = libvlc_NothingSpecial,
@@ -375,6 +378,16 @@ libvlc_media_player_new( libvlc_instance_t * p_libvlc_instance,
     libvlc_event_manager_register_event_type( p_mi->p_event_manager,
             libvlc_MediaPlayerPausableChanged, p_e );
 
+    /* Snapshot initialization */
+    libvlc_event_manager_register_event_type( p_mi->p_event_manager,
+           libvlc_MediaPlayerSnapshotTaken, p_e );
+    /* Attach a var callback to the global object to provide the glue between
+        vout_thread that generates the event and media_player that re-emits it
+        with its own event manager
+    */
+    var_Create( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
+    var_AddCallback( p_libvlc_instance->p_libvlc_int, "vout-snapshottaken", SnapshotTakenCallback, p_mi );
+
     return p_mi;
 }
 
@@ -453,6 +466,9 @@ void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
     if( !p_mi )
         return;
 
+	/* Detach Callback from the main libvlc object */
+    var_DelCallback( p_mi->p_libvlc_instance->p_libvlc_int, "vout-snapshottaken", SnapshotTakenCallback, p_mi );
+
     p_input_thread = libvlc_get_input_thread( p_mi, &p_e );
 
     if( libvlc_exception_raised( &p_e ) )
@@ -581,6 +597,28 @@ libvlc_media_player_event_manager(
 }
 
 /**************************************************************************
+ * Trigger a snapshot Taken Event
+ *************************************************************************/
+static int SnapshotTakenCallback( vlc_object_t *p_this, char const *psz_cmd,
+                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
+{
+    VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
+    VLC_UNUSED(p_this) ;
+
+    libvlc_media_player_t* p_mi = (libvlc_media_player_t*) p_data ;
+    libvlc_event_t event ;
+    event.type = libvlc_MediaPlayerSnapshotTaken ;
+    event.u.media_player_snapshot_taken.psz_filename = newval.psz_string ;
+    /* Snapshot psz data is a vlc_variable owned by libvlc object .
+         Its memmory management is taken care by the obj*/
+    msg_Dbg( p_this, "about to emit libvlc_snapshot_taken.make psz_str=0x%x (%s)",
+          event.u.media_player_snapshot_taken.psz_filename ,event.u.media_player_snapshot_taken.psz_filename );
+    libvlc_event_send( p_mi->p_event_manager, &event );
+
+    return VLC_SUCCESS;
+}
+
+/**************************************************************************
  * Play
  **************************************************************************/
 void libvlc_media_player_play( libvlc_media_player_t *p_mi,
diff --git a/src/video_output/vout_intf.c b/src/video_output/vout_intf.c
index 73993f7..48ed597 100644
--- a/src/video_output/vout_intf.c
+++ b/src/video_output/vout_intf.c
@@ -795,6 +795,14 @@ int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
     msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
     vout_OSDMessage( VLC_OBJECT( p_vout ), DEFAULT_CHAN,
                      "%s", psz_filename );
+
+    /* Generate a media player event  - Right now just trigger a global libvlc var
+        CHECK: Could not find a more local object. The goal is to communicate
+        vout_thread with libvlc_media_player or its input_thread*/
+    val.psz_string =  psz_filename  ;
+
+    var_Set( p_vout->p_libvlc, "vout-snapshottaken", val );
+    /* var_Set duplicates data for transport so we can free*/
     free( psz_filename );
 
     /* */
-- 
1.5.6.5

