8dfbe8dd1617b3664860690f8137d336a39720bc
[nowplayingd] / nowplaying.c
1 /** Code is released under GPLv2
2  *
3  * See debian/copyright for copyright info
4  */
5
6 /** Compile binary only with:
7  * gcc -Wall `pkg-config --cflags --libs hildon-notify glib-2.0 mafw mafw-shared` nowplaying.c -o nowplayingd
8  */
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <glib.h>
13 #include <libintl.h>
14 #include <locale.h>
15 #include <hildon/hildon-notification.h>
16 #include <libmafw/mafw.h>
17 #include <libmafw-shared/mafw-shared.h>
18
19 #define WANTED_RENDERER "Mafw-Gst-Renderer"
20
21 static GMainLoop *loop = NULL;
22 static MafwRegistry *registry = NULL;
23
24 static void sig_handler( int sig G_GNUC_UNUSED )
25 {
26         if ( g_main_loop_is_running( loop ) ) {
27                 g_main_loop_quit( loop );
28         }
29 }
30
31 static gboolean close_notification( gpointer n )
32 {
33         notify_notification_close( NOTIFY_NOTIFICATION( n ), NULL );
34
35         return FALSE;
36 }
37
38 static void metadata_callback(
39         const MafwRenderer *self G_GNUC_UNUSED,
40         const gchar *object_id G_GNUC_UNUSED,
41         GHashTable *metadata,
42         gconstpointer user_data G_GNUC_UNUSED,
43         const GError *error G_GNUC_UNUSED )
44 {
45         const gchar *artist = NULL;
46         const gchar *title = NULL;
47         HildonNotification *n = NULL;
48
49         artist = g_value_get_string( mafw_metadata_first( metadata, MAFW_METADATA_KEY_ARTIST ) );
50         title = g_value_get_string( mafw_metadata_first( metadata, MAFW_METADATA_KEY_TITLE ) );
51
52         if ( !artist || *artist == '\0' ) /* || !strcmp (artist, "")) */ {
53                 artist = dgettext( "mediaplayer", "mp_li_unknown_artist" );
54         }
55
56         if ( !title || *title == '\0' ) /* || !strcmp (title, "")) */ {
57                 title = dgettext( "mediaplayer", "mp_li_unknown_song" );
58         }
59
60         n = hildon_notification_new( artist, title, "tasklaunch_media_player", "media" );
61         hildon_notification_add_dbus_action( n, "default",
62                 "tasklaunch_media_player",
63                 "com.nokia.mediaplayer",
64                 "/com/nokia/osso/mediaplayer",
65                 "com.nokia.mediaplayer",
66                 "open_mp_now_playing",
67                 G_TYPE_NONE, NULL,
68                 -1 );
69                 
70         notify_notification_show( NOTIFY_NOTIFICATION( n ), NULL );
71         g_timeout_add_seconds( 5, close_notification, n );
72 }
73
74 static void state_changed_cb(
75         MafwRenderer *renderer,
76         const MafwPlayState state,
77         gconstpointer user_data G_GNUC_UNUSED)
78 {
79         switch (state) {
80                 case Playing:
81                         mafw_renderer_get_current_metadata( renderer,
82                                 (MafwRendererMetadataResultCB) metadata_callback,
83                                 NULL );
84                         break;
85
86                 case Stopped:
87                 default:
88                         break;
89   }
90 }
91
92 static void renderer_added_cb(
93         const MafwRegistry *registry2 G_GNUC_UNUSED,
94         GObject *renderer,
95         gconstpointer user_data G_GNUC_UNUSED)
96 {
97         if ( MAFW_IS_RENDERER( renderer ) ) {
98                 const gchar *name = mafw_extension_get_name( MAFW_EXTENSION( renderer ) );
99
100                 if ( !strcmp( name, WANTED_RENDERER ) ) {
101                         g_signal_connect( renderer, "state-changed", G_CALLBACK (state_changed_cb), NULL );
102                 }
103         }
104 }
105
106 static void mafw_init( void )
107 {
108     GList *renderers = NULL;
109
110         g_signal_connect( registry, "renderer-added", G_CALLBACK( renderer_added_cb ), NULL );
111         renderers = mafw_registry_get_renderers( registry );
112         while ( renderers ) {
113                 renderer_added_cb( registry, G_OBJECT( renderers->data ), NULL );
114                 renderers = g_list_next( renderers );
115         }
116 }
117
118 int main (void)
119 {
120         setlocale( LC_ALL, "" );
121
122         g_type_init();
123         notify_init("nowplayingd");
124
125         loop = g_main_loop_new( NULL, FALSE );
126         g_assert( loop );
127
128         registry = MAFW_REGISTRY( mafw_registry_get_instance() );
129         g_assert( registry );
130
131         signal( SIGINT, sig_handler );
132         signal( SIGQUIT, sig_handler );
133         signal( SIGTERM, sig_handler );
134
135         mafw_shared_init( registry, NULL );
136
137         mafw_init();
138
139         g_main_loop_run( loop );
140
141         g_main_loop_unref( loop );
142
143         mafw_shared_deinit();
144
145         return EXIT_SUCCESS;
146 }
147