]> git.sesse.net Git - vlc/blobdiff - modules/misc/notify/msn.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / modules / misc / notify / msn.c
index 7038c85a0d42dc1f9d43da63908d6e2e5f0e08e6..7a0d656f5640d8eee4bd352c5bbd256d708474b1 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * msn.c : msn title plugin
  *****************************************************************************
- * Copyright (C) 2005 the VideoLAN team
+ * Copyright (C) 2005-2010 the VideoLAN team
  * $Id$
  *
  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>                                      /* malloc(), free() */
 
-#include <vlc/vlc.h>
-#include <vlc/intf.h>
-#include <vlc_meta.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_interface.h>
+#include <vlc_meta.h>
+#include <vlc_playlist.h>
+#include <vlc_strings.h>
+#include <vlc_charset.h>
 
 /*****************************************************************************
  * intf_sys_t: description and status of log interface
@@ -47,7 +53,7 @@ static void Close   ( vlc_object_t * );
 
 static int ItemChange( vlc_object_t *, const char *,
                        vlc_value_t, vlc_value_t, void * );
-static int SendToMSN( char * psz_msg );
+static int SendToMSN( const char * psz_msg );
 
 #define MSN_MAX_LENGTH 256
 
@@ -59,23 +65,22 @@ static int SendToMSN( char * psz_msg );
  * You need to enable the "What I'm Listening To" option in MSN.
  *****************************************************************************/
 #define FORMAT_DEFAULT "{0} - {1}"
-/// \bug [String] MSN is useless
-#define FORMAT_TEXT N_("MSN Title format string")
+#define FORMAT_TEXT N_("Title format string")
 #define FORMAT_LONGTEXT N_("Format of the string to send to MSN " \
 "{0} Artist, {1} Title, {2} Album. Defaults to \"Artist - Title\" ({0} - {1}).")
 
-vlc_module_begin();
-    set_category( CAT_INTERFACE );
-    set_subcategory( SUBCAT_INTERFACE_CONTROL );
-    set_shortname( "MSN" );
-    set_description( _("MSN Now-Playing") );
+vlc_module_begin ()
+    set_category( CAT_INTERFACE )
+    set_subcategory( SUBCAT_INTERFACE_CONTROL )
+    set_shortname( "MSN" )
+    set_description( N_("MSN Now-Playing") )
 
     add_string( "msn-format", FORMAT_DEFAULT, NULL,
-                FORMAT_TEXT, FORMAT_LONGTEXT, VLC_FALSE );
+                FORMAT_TEXT, FORMAT_LONGTEXT, false )
 
-    set_capability( "interface", 0 );
-    set_callbacks( Open, Close );
-vlc_module_end();
+    set_capability( "interface", 0 )
+    set_callbacks( Open, Close )
+vlc_module_end ()
 
 /*****************************************************************************
  * Open: initialize and create stuff
@@ -85,9 +90,11 @@ static int Open( vlc_object_t *p_this )
     intf_thread_t *p_intf = (intf_thread_t *)p_this;
     playlist_t *p_playlist;
 
-    MALLOC_ERR( p_intf->p_sys, intf_sys_t );
+    p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
+    if( !p_intf->p_sys )
+        return VLC_ENOMEM;
 
-    p_intf->p_sys->psz_format = config_GetPsz( p_intf, "msn-format" );
+    p_intf->p_sys->psz_format = var_InheritString( p_intf, "msn-format" );
     if( !p_intf->p_sys->psz_format )
     {
         msg_Dbg( p_intf, "no format provided" );
@@ -95,10 +102,9 @@ static int Open( vlc_object_t *p_this )
     }
     msg_Dbg( p_intf, "using format: %s", p_intf->p_sys->psz_format );
 
-    p_playlist = pl_Yield( p_intf );
+    p_playlist = pl_Get( p_intf );
     var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
-    var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
-    pl_Release( p_intf );
+    var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );
 
     return VLC_SUCCESS;
 }
@@ -109,15 +115,14 @@ static int Open( vlc_object_t *p_this )
 static void Close( vlc_object_t *p_this )
 {
     intf_thread_t *p_intf = (intf_thread_t *)p_this;
-    playlist_t *p_playlist = pl_Yield( p_this );
+    playlist_t *p_playlist = pl_Get( p_this );
 
     /* clear the MSN stuff ... else it looks like we're still playing
      * something although VLC (or the MSN plugin) is closed */
     SendToMSN( "\\0Music\\01\\0\\0\\0\\0\\0\\0\\0" );
 
     var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
-    var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
-    pl_Release( p_this );
+    var_DelCallback( p_playlist, "item-current", ItemChange, p_intf );
 
     /* Destroy structure */
     free( p_intf->p_sys->psz_format );
@@ -130,21 +135,14 @@ static void Close( vlc_object_t *p_this )
 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
                        vlc_value_t oldval, vlc_value_t newval, void *param )
 {
+    (void)psz_var;    (void)oldval;    (void)newval;
     intf_thread_t *p_intf = (intf_thread_t *)param;
     char psz_tmp[MSN_MAX_LENGTH];
-    char *psz_title = NULL;
-    char *psz_artist = NULL;
-    char *psz_album = NULL;
-    input_thread_t *p_input;
-    playlist_t *p_playlist = pl_Yield( p_this );
-
-    p_input = p_playlist->p_input;
-    pl_Release( p_this );
+    input_thread_t *p_input =  playlist_CurrentInput( (playlist_t *) p_this );
 
     if( !p_input ) return VLC_SUCCESS;
-    vlc_object_yield( p_input );
 
-    if( p_input->b_dead || !p_input->input.p_item->psz_name )
+    if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
     {
         /* Not playing anything ... */
         SendToMSN( "\\0Music\\01\\0\\0\\0\\0\\0\\0\\0" );
@@ -153,28 +151,24 @@ static int ItemChange( vlc_object_t *p_this, const char *psz_var,
     }
 
     /* Playing something ... */
-    psz_artist = p_input->input.p_item->p_meta->psz_artist ?
-                  strdup( p_input->input.p_item->p_meta->psz_artist ) :
-                  strdup( "" );
-    psz_album = p_input->input.p_item->p_meta->psz_album ?
-                  strdup( p_input->input.p_item->p_meta->psz_album ) :
-                  strdup( "" );
-    psz_title = strdup( p_input->input.p_item->psz_name );
-    if( psz_title == NULL ) psz_title = strdup( N_("(no title)") );
-    if( psz_artist == NULL ) psz_artist = strdup( N_("(no artist)") );
-    if( psz_album == NULL ) psz_album = strdup( N_("(no album)") );
+    char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
+    char *psz_album = input_item_GetAlbum( input_GetItem( p_input ) );
+    char *psz_title = input_item_GetTitleFbName( input_GetItem( p_input ) );
+    char *psz_buf = str_format_meta( p_intf, p_intf->p_sys->psz_format );
+
     snprintf( psz_tmp,
               MSN_MAX_LENGTH,
               "\\0Music\\01\\0%s\\0%s\\0%s\\0%s\\0\\0\\0",
-              p_intf->p_sys->psz_format,
-              psz_title,
-              psz_artist,
+              psz_buf,
+              psz_artist ? psz_artist : "",
+              psz_title ? psz_title : "",
               psz_album );
+    free( psz_buf );
     free( psz_title );
     free( psz_artist );
     free( psz_album );
 
-    SendToMSN( psz_tmp );
+    SendToMSN( (const char*)psz_tmp );
     vlc_object_release( p_input );
 
     return VLC_SUCCESS;
@@ -183,23 +177,23 @@ static int ItemChange( vlc_object_t *p_this, const char *psz_var,
 /*****************************************************************************
  * SendToMSN
  *****************************************************************************/
-static int SendToMSN( char *psz_msg )
+static int SendToMSN( const char *psz_msg )
 {
     COPYDATASTRUCT msndata;
     HWND msnui = NULL;
 
-    wchar_t buffer[MSN_MAX_LENGTH];
-
-    mbstowcs( buffer, psz_msg, MSN_MAX_LENGTH );
+    wchar_t *wmsg = ToWide( psz_msg );
+    if( unlikely(wmsg == NULL) )
+        return VLC_ENOMEM;
 
     msndata.dwData = 0x547;
-    msndata.lpData = &buffer;
-    msndata.cbData = (lstrlenW(buffer)*2)+2;
+    msndata.lpData = wmsg;
+    msndata.cbData = (wcslen(wmsg) + 1) * 2;
 
     while( ( msnui = FindWindowEx( NULL, msnui, "MsnMsgrUIManager", NULL ) ) )
     {
         SendMessage(msnui, WM_COPYDATA, (WPARAM)NULL, (LPARAM)&msndata);
     }
-
+    free( wmsg );
     return VLC_SUCCESS;
 }