ff0f8113d7e4d66ef4d62e0da20622c29fad6c2d
[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 void metadata_callback(
32         const MafwRenderer *self G_GNUC_UNUSED,
33         const gchar *object_id G_GNUC_UNUSED,
34         GHashTable *metadata,
35         gconstpointer user_data G_GNUC_UNUSED,
36         const GError *error G_GNUC_UNUSED )
37 {
38         const gchar *artist = NULL;
39         const gchar *title = NULL;
40         HildonNotification *n = NULL;
41
42         artist = g_value_get_string( mafw_metadata_first( metadata, MAFW_METADATA_KEY_ARTIST ) );
43         title = g_value_get_string( mafw_metadata_first( metadata, MAFW_METADATA_KEY_TITLE ) );
44
45         if ( !artist || *artist == '\0' ) /* || !strcmp (artist, "")) */ {
46                 artist = dgettext( "mediaplayer", "mp_li_unknown_artist" );
47         }
48
49         if ( !title || *title == '\0' ) /* || !strcmp (title, "")) */ {
50                 title = dgettext( "mediaplayer", "mp_li_unknown_song" );
51         }
52
53         n = hildon_notification_new( artist, title, "tasklaunch_media_player", "media" );
54         hildon_notification_add_dbus_action( n, "default",
55                 "tasklaunch_media_player",
56                 "com.nokia.mediaplayer",
57                 "/com/nokia/osso/mediaplayer",
58                 "com.nokia.mediaplayer",
59                 "open_mp_now_playing",
60                 G_TYPE_NONE, NULL,
61                 -1 );
62
63         notify_notification_set_timeout( NOTIFY_NOTIFICATION( n ), 4500 /* 4.5s */ );
64         notify_notification_show( NOTIFY_NOTIFICATION( n ), NULL );
65 }
66
67 static void state_changed_cb(
68         MafwRenderer *renderer,
69         const MafwPlayState state,
70         gconstpointer user_data G_GNUC_UNUSED)
71 {
72         switch (state) {
73                 case Playing:
74                         mafw_renderer_get_current_metadata( renderer,
75                                 (MafwRendererMetadataResultCB) metadata_callback,
76                                 NULL );
77                         break;
78
79                 case Stopped:
80                 default:
81                         break;
82   }
83 }
84
85 static void renderer_removed_cb(
86         const MafwRegistry *registry2 G_GNUC_UNUSED,
87         GObject *renderer,
88         gconstpointer user_data G_GNUC_UNUSED)
89 {
90         if ( MAFW_IS_RENDERER( renderer ) ) {
91                 const gchar *name = mafw_extension_get_name( MAFW_EXTENSION( renderer ) );
92
93                 if ( !strcmp( name, WANTED_RENDERER ) ) {
94                         g_signal_handlers_disconnect_by_func( renderer, G_CALLBACK( state_changed_cb ), NULL );
95                 }
96         }
97 }
98
99 static void renderer_added_cb(
100         const MafwRegistry *registry2 G_GNUC_UNUSED,
101         GObject *renderer,
102         gconstpointer user_data G_GNUC_UNUSED)
103 {
104         if ( MAFW_IS_RENDERER( renderer ) ) {
105                 const gchar *name = mafw_extension_get_name( MAFW_EXTENSION( renderer ) );
106
107                 if ( !strcmp( name, WANTED_RENDERER ) ) {
108                         g_signal_connect( renderer, "state-changed", G_CALLBACK (state_changed_cb), NULL );
109                 }
110         }
111 }
112
113 static void mafw_deinit( void )
114 {
115     GList *renderers = NULL;
116
117         g_signal_handlers_disconnect_by_func( registry, G_CALLBACK( renderer_removed_cb ), NULL );
118         renderers = mafw_registry_get_renderers( registry );
119         while ( renderers ) {
120                 renderer_removed_cb( registry, G_OBJECT( renderers->data ), NULL );
121                 renderers = g_list_next( renderers );
122         }
123 }
124
125 static void mafw_init( void )
126 {
127     GList *renderers = NULL;
128
129         g_signal_connect( registry, "renderer-added", G_CALLBACK( renderer_added_cb ), NULL );
130         renderers = mafw_registry_get_renderers( registry );
131         while ( renderers ) {
132                 renderer_added_cb( registry, G_OBJECT( renderers->data ), NULL );
133                 renderers = g_list_next( renderers );
134         }
135 }
136
137 int main (void)
138 {
139         setlocale( LC_ALL, "" );
140
141         g_type_init();
142         notify_init("nowplayingd");
143
144         loop = g_main_loop_new( NULL, FALSE );
145         g_assert( loop );
146
147         registry = MAFW_REGISTRY( mafw_registry_get_instance() );
148         g_assert( registry );
149
150         signal( SIGINT, sig_handler );
151         signal( SIGQUIT, sig_handler );
152         signal( SIGTERM, sig_handler );
153
154         mafw_shared_init( registry, NULL );
155
156         mafw_init();
157
158         g_main_loop_run( loop );
159
160         g_main_loop_unref( loop );
161
162         mafw_deinit();
163
164         mafw_shared_deinit();
165
166         notify_uninit();
167
168         return EXIT_SUCCESS;
169 }
170