]> git.sesse.net Git - vlc/blob - modules/misc/notify/notify.c
Remove stdlib.h
[vlc] / modules / misc / notify / notify.c
1 /*****************************************************************************
2  * notify.c : libnotify notification plugin
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Mutricy <xtophe -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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>
28
29 #include <libnotify/notify.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc_interface.h>
33 #include <vlc_playlist.h>
34 #include <vlc_meta.h>
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int  Open    ( vlc_object_t * );
40 static void Close   ( vlc_object_t * );
41
42 static int ItemChange( vlc_object_t *, const char *,
43                        vlc_value_t, vlc_value_t, void * );
44 static int Notify( vlc_object_t *, const char * );
45 #define MAX_LENGTH 256
46
47 struct intf_sys_t
48 {
49     vlc_mutex_t     lock;
50 };
51
52 /*****************************************************************************
53  * Module descriptor
54  ****************************************************************************/
55
56 #define APPLICATION_NAME "VLC media player"
57
58 #define TIMEOUT_TEXT N_("Timeout (ms)")
59 #define TIMEOUT_LONGTEXT N_("How long the notification will be displayed ")
60
61 vlc_module_begin();
62     set_category( CAT_INTERFACE );
63     set_subcategory( SUBCAT_INTERFACE_CONTROL );
64     set_shortname( _( "Notify" ) );
65     set_description( _("LibNotify Notification Plugin") );
66
67     add_integer( "notify-timeout", 4000,NULL,
68                 TIMEOUT_TEXT, TIMEOUT_LONGTEXT, VLC_TRUE );
69
70     set_capability( "interface", 0 );
71     set_callbacks( Open, Close );
72 vlc_module_end();
73
74 /*****************************************************************************
75  * Open: initialize and create stuff
76  *****************************************************************************/
77 static int Open( vlc_object_t *p_this )
78 {
79     intf_thread_t   *p_intf = (intf_thread_t *)p_this;
80     playlist_t      *p_playlist;
81     intf_sys_t      *p_sys  = malloc( sizeof( intf_sys_t ) );
82     
83     if( !p_sys )
84     {
85         msg_Err( p_intf, "Out of memory" );
86         return VLC_ENOMEM;
87     }
88
89     if( !notify_init( APPLICATION_NAME ) )
90     {
91         msg_Err( p_intf, "can't find notification daemon" );
92         return VLC_EGENERIC;
93     }
94
95     vlc_mutex_init( p_this, &p_sys->lock );
96
97     p_intf->p_sys = p_sys;
98
99     p_playlist = pl_Yield( p_intf );
100     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
101     pl_Release( p_intf );
102
103     return VLC_SUCCESS;
104 }
105
106 /*****************************************************************************
107  * Close: destroy interface stuff
108  *****************************************************************************/
109 static void Close( vlc_object_t *p_this )
110 {
111     intf_thread_t   *p_intf = ( intf_thread_t* ) p_this;
112     intf_sys_t      *p_sys  = p_intf->p_sys;
113
114     playlist_t *p_playlist = pl_Yield( p_this );
115     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_this );
116     pl_Release( p_this );
117
118     vlc_mutex_destroy( &p_sys->lock );
119     free( p_sys );
120     notify_uninit();
121 }
122
123 /*****************************************************************************
124  * ItemChange: Playlist item change callback
125  *****************************************************************************/
126 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
127                        vlc_value_t oldval, vlc_value_t newval, void *param )
128 {
129     char                psz_tmp[MAX_LENGTH];
130     char                *psz_title      = NULL;
131     char                *psz_artist     = NULL;
132     char                *psz_album      = NULL;
133     input_thread_t      *p_input        = NULL;
134     playlist_t          * p_playlist    = pl_Yield( p_this );
135     intf_thread_t       *p_intf         = ( intf_thread_t* ) param;
136     intf_sys_t          *p_sys          = p_intf->p_sys;
137
138     p_input = p_playlist->p_input;
139     vlc_object_release( p_playlist );
140
141     if( !p_input ) return VLC_SUCCESS;
142     vlc_object_yield( p_input );
143
144     if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
145     {
146         /* Not playing anything ... */
147         vlc_object_release( p_input );
148         return VLC_SUCCESS;
149     }
150
151     /* Playing something ... */
152     psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
153     if( psz_artist == NULL ) psz_artist = strdup( _("no artist") );
154     psz_album = input_item_GetAlbum( input_GetItem( p_input ) ) ;
155     if( psz_album == NULL ) psz_album = strdup( _("no album") );
156     psz_title = input_item_GetName( input_GetItem( p_input ) );
157
158     vlc_object_release( p_input );
159
160     if( psz_title == NULL ) psz_title = strdup( N_("(no title)") );
161     snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>\n%s - %s",
162               psz_title, psz_artist, psz_album );
163     free( psz_title );
164     free( psz_artist );
165     free( psz_album );
166
167     vlc_mutex_lock( &p_sys->lock );
168
169     Notify( p_this, psz_tmp );
170
171     vlc_mutex_unlock( &p_sys->lock );
172
173     return VLC_SUCCESS;
174 }
175
176 static int Notify( vlc_object_t *p_this, const char *psz_temp )
177 {
178     NotifyNotification * notification;
179     notification = notify_notification_new( _("Now Playing"),
180                              psz_temp,
181                              DATA_PATH "/vlc48x48.png",NULL);
182     notify_notification_set_timeout( notification,
183                                      config_GetInt(p_this, "notify-timeout") );
184     notify_notification_set_urgency( notification, NOTIFY_URGENCY_LOW );
185     notify_notification_show( notification, NULL);
186     return VLC_SUCCESS;
187 }
188