]> git.sesse.net Git - vlc/blob - modules/misc/msn.c
* modules/access/dvb/en50221.c: Implemented basic MMI support to display
[vlc] / modules / misc / msn.c
1 /*****************************************************************************
2  * msn.c : msn title plugin
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdio.h>
32
33 #include <vlc/vlc.h>
34 #include <vlc/intf.h>
35 #include <vlc_meta.h>
36
37
38 /*****************************************************************************
39  * intf_sys_t: description and status of log interface
40  *****************************************************************************/
41 struct intf_sys_t
42 {
43     char *psz_format;
44 };
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 static int  Open    ( vlc_object_t * );
50 static void Close   ( vlc_object_t * );
51 static void Run     ( intf_thread_t * );
52
53 static int ItemChange( vlc_object_t *, const char *,
54                        vlc_value_t, vlc_value_t, void * );
55 static int SendToMSN( char * psz_msg );
56
57 #define MSN_MAX_LENGTH 256
58
59 /*****************************************************************************
60  * Module descriptor
61  *****************************************************************************
62  * This module should be used on windows with MSN (i think that you need to
63  * have MSN 7 or newer) to "advertise" what you are playing in VLC.
64  * You need to enable the "What I'm Listening To" option in MSN.
65  *****************************************************************************/
66 #define FORMAT_DEFAULT "{0} - {1}"
67 #define FORMAT_TEXT N_("MSN Title format string")
68 #define FORMAT_LONGTEXT N_("MSN Title format string. " \
69 "{0} artist, {1} title, {2} album")
70
71 vlc_module_begin();
72     set_category( CAT_INTERFACE );
73     set_subcategory( SUBCAT_INTERFACE_CONTROL );
74     set_shortname( N_( "MSN" ) );
75     set_description( _("MSN Title Plugin") );
76
77     add_string( "msn-format", FORMAT_DEFAULT, NULL,
78                 FORMAT_TEXT, FORMAT_LONGTEXT, VLC_FALSE );
79
80     set_capability( "interface", 0 );
81     set_callbacks( Open, Close );
82 vlc_module_end();
83
84 /*****************************************************************************
85  * Open: initialize and create stuff
86  *****************************************************************************/
87 static int Open( vlc_object_t *p_this )
88 {
89     intf_thread_t *p_intf = (intf_thread_t *)p_this;
90     playlist_t *p_playlist;
91
92     /* Allocate instance and initialize some members */
93     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
94     if( p_intf->p_sys == NULL )
95     {
96         msg_Err( p_intf, "out of memory" );
97         return -1;
98     }
99
100     p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
101                                                 FIND_ANYWHERE );
102
103
104     p_intf->p_sys->psz_format = config_GetPsz( p_intf, "msn-format" );
105     if( !p_intf->p_sys->psz_format )
106     {
107         msg_Dbg( p_intf, "no format provided" );
108         p_intf->p_sys->psz_format = strdup( FORMAT_DEFAULT );
109     }
110     msg_Dbg( p_intf, "using format: %s", p_intf->p_sys->psz_format );
111
112     if( !p_playlist )
113     {
114         msg_Err( p_intf, "could not find playlist object" );
115         free( p_intf->p_sys->psz_format );
116         free( p_intf->p_sys );
117         return -1;
118     }
119
120     var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
121     vlc_object_release( p_playlist );
122
123     p_intf->pf_run = Run;
124
125     return 0;
126 }
127
128 /*****************************************************************************
129  * Close: destroy interface stuff
130  *****************************************************************************/
131 static void Close( vlc_object_t *p_this )
132 {
133     intf_thread_t *p_intf = (intf_thread_t *)p_this;
134
135     /* clear the MSN stuff ... else it looks like we're still playing
136      * something although VLC (or the MSN plugin) is closed */
137     SendToMSN( "\\0Music\\01\\0\\0\\0\\0\\0\\0\\0" );
138
139     /* Destroy structure */
140     free( p_intf->p_sys->psz_format );
141     free( p_intf->p_sys );
142 }
143
144 /*****************************************************************************
145  * Run
146  *****************************************************************************/
147 static void Run( intf_thread_t *p_intf )
148 {
149     msleep( INTF_IDLE_SLEEP );
150 }
151
152 /*****************************************************************************
153  * ItemChange: Playlist item change callback
154  *****************************************************************************/
155 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
156                        vlc_value_t oldval, vlc_value_t newval, void *param )
157 {
158     intf_thread_t *p_intf = (intf_thread_t *)param;
159
160     char psz_tmp[MSN_MAX_LENGTH];
161     char *psz_title = NULL;
162     char *psz_artist = NULL;
163     char *psz_album = NULL;
164
165     int i,j;
166
167     if( !p_intf->p_sys ) return VLC_SUCCESS;
168
169     input_thread_t *p_input =
170         (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
171                                            FIND_ANYWHERE );
172     if( !p_input || p_input->b_dead || !p_input->input.p_item->psz_name )
173     {
174         /* Not playing anything ... */
175         SendToMSN( "\\0Music\\01\\0\\0\\0\\0\\0\\0\\0" );
176         return VLC_SUCCESS;
177     }
178
179     /* Playing something ... */
180     for( i = 0; i < p_input->input.p_item->i_categories; i++ )
181     {
182         info_category_t *p_cat = p_input->input.p_item->pp_categories[i];
183         for( j = 0; j < p_cat->i_infos; j++ )
184         {
185             info_t *p_info = p_cat->pp_infos[j];
186
187             if( !strcmp( p_info->psz_name, VLC_META_ARTIST ) )
188             {
189                 psz_artist = strdup( p_info->psz_value );
190             }
191             else if( !strcmp( p_info->psz_name, "Album/movie/show title" ) )
192             {
193                 psz_album = strdup( p_info->psz_value );
194             }
195         }
196     }
197     psz_title = strdup( p_input->input.p_item->psz_name );
198     if( psz_title == NULL ) psz_title = strdup( N_("(no title)") );
199     if( psz_artist == NULL ) psz_artist = strdup( N_("(no artist)") );
200     if( psz_album == NULL ) psz_album = strdup( N_("(no album)") );
201     snprintf( psz_tmp,
202               MSN_MAX_LENGTH,
203               "\\0Music\\01\\0%s\\0%s\\0%s\\0%s\\0\\0\\0",
204               p_intf->p_sys->psz_format,
205               //FORMAT_DEFAULT,
206               psz_title,
207               psz_artist,
208               psz_album );
209     free( psz_title );
210     free( psz_artist );
211     free( psz_album );
212
213     SendToMSN( psz_tmp );
214
215     return VLC_SUCCESS;
216 }
217
218 /*****************************************************************************
219  * SendToMSN
220  *****************************************************************************/
221 static int SendToMSN( char *psz_msg )
222 {
223     COPYDATASTRUCT msndata;
224     HWND msnui = NULL;
225
226     wchar_t buffer[MSN_MAX_LENGTH];
227
228     mbstowcs( buffer, psz_msg, MSN_MAX_LENGTH );
229
230     msndata.dwData = 0x547;
231     msndata.lpData = &buffer;
232     msndata.cbData = (lstrlenW(buffer)*2)+2;
233
234     while( ( msnui = FindWindowEx( NULL, msnui, "MsnMsgrUIManager", NULL ) ) )
235     {
236         SendMessage(msnui, WM_COPYDATA, (WPARAM)NULL, (LPARAM)&msndata);
237     }
238
239     return VLC_SUCCESS;
240 }