comparison pidgin-audacious.c @ 18:dc3aa0bf24c0

moved to mpris access
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Sat, 04 Oct 2008 04:19:46 +0900
parents 14de631fa929
children 12d809123d69
comparison
equal deleted inserted replaced
17:14de631fa929 18:dc3aa0bf24c0
19 19
20 #include <stdio.h> 20 #include <stdio.h>
21 #include <stdlib.h> 21 #include <stdlib.h>
22 #include <string.h> 22 #include <string.h>
23 #include <glib.h> 23 #include <glib.h>
24 #include <dbus/dbus.h>
25 #include <dbus/dbus-glib.h>
24 26
25 #include "gtkplugin.h" 27 #include "gtkplugin.h"
26 #include "util.h" 28 #include "util.h"
27 #include "debug.h" 29 #include "debug.h"
28 #include "connection.h" 30 #include "connection.h"
29 #include "version.h" 31 #include "version.h"
30 #include "cmds.h" 32 #include "cmds.h"
31 #include <audacious/audctrl.h> 33 #include "savedstatuses.h"
32 #include <audacious/dbus.h>
33
34 extern gchar *botch_utf(const gchar *msg, gsize len, gsize *newlen) __attribute__ ((weak));
35 34
36 #define PIDGINAUD_PLUGIN_ID "pidgin_audacious" 35 #define PIDGINAUD_PLUGIN_ID "pidgin_audacious"
37 36
38 #define OPT_PIDGINAUD "/plugins/pidgin_audacious" 37 #define OPT_PIDGINAUD "/plugins/pidgin_audacious"
39 #define OPT_PROCESS_STATUS OPT_PIDGINAUD "/process_status" 38 #define OPT_PROCESS_STATUS OPT_PIDGINAUD "/process_status"
40 #define OPT_PROCESS_USERINFO OPT_PIDGINAUD "/process_userinfo" 39 #define OPT_PROCESS_USERINFO OPT_PIDGINAUD "/process_userinfo"
40 #define OPT_SONG_TEMPLATE OPT_PIDGINAUD "/song_template"
41 #define OPT_PASTE_TEMPLATE OPT_PIDGINAUD "/paste_template" 41 #define OPT_PASTE_TEMPLATE OPT_PIDGINAUD "/paste_template"
42 42
43 #define DEFAULT_SONG_TEMPLATE "%artist - %title"
43 #define SONG_TOKEN "%song" 44 #define SONG_TOKEN "%song"
44 #define NO_SONG_MESSAGE "No song being played." 45 #define NO_SONG_MESSAGE "No song being played."
46
47 #define DBUS_TYPE_G_STRING_VALUE_HASHTABLE (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE))
45 48
46 #define aud_debug(fmt, ...) purple_debug(PURPLE_DEBUG_INFO, \ 49 #define aud_debug(fmt, ...) purple_debug(PURPLE_DEBUG_INFO, \
47 "Pidgin-Audacious", \ 50 "Pidgin-Audacious", \
48 fmt, ## __VA_ARGS__); 51 fmt, ## __VA_ARGS__);
49 #define aud_error(fmt, ...) purple_debug(PURPLE_DEBUG_ERROR, \ 52 #define aud_error(fmt, ...) purple_debug(PURPLE_DEBUG_ERROR, \
50 "Pidgin-Audacious", \ 53 "Pidgin-Audacious", \
51 fmt, ## __VA_ARGS__); 54 fmt, ## __VA_ARGS__);
52 55 /* xxx move up */
53 static gint timeout_tag = 0; 56 #define TITLE "%title"
54 57 #define ARTIST "%artist"
55 GHashTable *seed_status; 58 #define ALBUM "%album"
56 GHashTable *seed_userinfo; 59 #define GENRE "%genre"
57 60
58 GHashTable *pushed_status; 61 typedef struct song_tuple {
59 GHashTable *pushed_userinfo; 62 gchar *title;
60 63 gchar *artist;
61 DBusGConnection *connection = NULL; 64 gchar *album;
62 DBusGProxy *session = NULL; 65 gchar *genre;
63 66 } song_tuple;
67
68
69 /* globals */
70 static GHashTable *seed_status;
71 static GHashTable *seed_userinfo;
72 static GHashTable *pushed_status;
73 static GHashTable *pushed_userinfo;
74 static DBusGConnection *connection = NULL;
75 static DBusGProxy *session = NULL;
64 static PurpleCmdId cmdid_paste_current_song; 76 static PurpleCmdId cmdid_paste_current_song;
65 77
78 /* prototypes */
79 extern gchar *botch_utf(const gchar *msg, gsize len, gsize *newlen) __attribute__ ((weak));
66 static void aud_process(gchar *aud_info); 80 static void aud_process(gchar *aud_info);
81 static void track_signal_cb(DBusGProxy *player_proxy, GHashTable *table, gpointer data);
82 static void status_signal_cb(DBusGProxy *player_proxy, gint status, gpointer data);
83 static gboolean is_app_running(void);
84 static GHashTable *get_song_table(void);
85 static song_tuple *get_song_tuple(GHashTable *table);
86
87
88 /* implementation */
67 89
68 static DBusGProxy * 90 static DBusGProxy *
69 get_dbus_proxy(void) 91 get_dbus_session(void)
70 { 92 {
71 GError *error = NULL; 93 GError *error = NULL;
94 DBusGProxy *proxy;
72 connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error); 95 connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
73 g_clear_error(&error); 96 g_clear_error(&error);
74 97
75 session = dbus_g_proxy_new_for_name(connection, AUDACIOUS_DBUS_SERVICE, 98 proxy = dbus_g_proxy_new_for_name(connection,
76 AUDACIOUS_DBUS_PATH, 99 "org.mpris.audacious",
77 AUDACIOUS_DBUS_INTERFACE); 100 "/Player",
101 "org.freedesktop.MediaPlayer");
78 102
79 g_clear_error(&error); 103 g_clear_error(&error);
80 return session; 104 return proxy;
81 } 105 }
82 106
107
108 static void
109 connect_dbus_signals()
110 {
111 dbus_g_proxy_add_signal(session,
112 "TrackChange",
113 DBUS_TYPE_G_STRING_VALUE_HASHTABLE,
114 G_TYPE_INVALID);
115
116 dbus_g_proxy_connect_signal(session,
117 "TrackChange",
118 G_CALLBACK(track_signal_cb),
119 NULL, NULL);
120
121 dbus_g_proxy_add_signal(session,
122 "StatusChange",
123 G_TYPE_INT, G_TYPE_INVALID);
124
125 dbus_g_proxy_connect_signal(session,
126 "StatusChange",
127 G_CALLBACK(status_signal_cb),
128 NULL, NULL);
129 }
130
131 static GHashTable *
132 get_song_table(void)
133 {
134 GHashTable *table = NULL;
135
136 status_signal_cb(NULL, -1, NULL);
137
138 if(is_app_running()) {
139 dbus_g_proxy_call(session,
140 "GetMetadata",
141 NULL,
142 G_TYPE_INVALID,
143 DBUS_TYPE_G_STRING_VALUE_HASHTABLE,
144 &table,
145 G_TYPE_INVALID);
146 }
147
148 return table;
149 }
150
151 static song_tuple *
152 get_song_tuple(GHashTable *table)
153 {
154 song_tuple *t = NULL;
155 GValue *value;
156
157 if(!table)
158 return NULL;
159
160 t = g_new0(song_tuple, 1);
161
162 value = (GValue *) g_hash_table_lookup(table, "title");
163 if (value && G_VALUE_HOLDS_STRING(value)) {
164 t->title = g_value_dup_string(value);
165 }
166 value = (GValue *) g_hash_table_lookup(table, "artist");
167 if (value != NULL && G_VALUE_HOLDS_STRING(value)) {
168 t->artist = g_value_dup_string(value);
169 }
170 value = (GValue *) g_hash_table_lookup(table, "album");
171 if (value != NULL && G_VALUE_HOLDS_STRING(value)) {
172 t->album = g_value_dup_string(value);
173 }
174 value = (GValue *) g_hash_table_lookup(table, "genre");
175 if (value != NULL && G_VALUE_HOLDS_STRING(value)) {
176 t->genre = g_value_dup_string(value);
177 }
178
179 return t;
180 }
181
182 void
183 free_song_tuple(song_tuple *t)
184 {
185 g_free(t->title);
186 g_free(t->artist);
187 g_free(t->album);
188 g_free(t->genre);
189
190 g_free(t);
191 }
192
193
194 static gchar *
195 format_song_info(song_tuple *tuple)
196 {
197 gchar *song_info = NULL, *tmp = NULL;
198
199 if(!tuple)
200 return NULL;
201
202 song_info = g_strdup(purple_prefs_get_string(OPT_SONG_TEMPLATE));
203 if(!song_info)
204 return NULL;
205
206 if(tuple->title && strstr(song_info, TITLE)) {
207 tmp = purple_strreplace(song_info, TITLE, tuple->title);
208 g_free(song_info);
209 song_info = tmp;
210 }
211 if(tuple->artist && strstr(song_info, ARTIST)) {
212 tmp = purple_strreplace(song_info, ARTIST, tuple->artist);
213 g_free(song_info);
214 song_info = tmp;
215 }
216 if(tuple->album && strstr(song_info, ALBUM)) {
217 tmp = purple_strreplace(song_info, ALBUM, tuple->album);
218 g_free(song_info);
219 song_info = tmp;
220 }
221 if(tuple->genre && strstr(song_info, GENRE)) {
222 tmp = purple_strreplace(song_info, GENRE, tuple->genre);
223 g_free(song_info);
224 song_info = tmp;
225 }
226
227 return song_info;
228 }
229
230
231
232 static void
233 track_signal_cb(DBusGProxy *player_proxy, GHashTable *table, gpointer data)
234 {
235 gchar *song_info = NULL;
236 song_tuple *tuple = get_song_tuple(table);
237
238 /* set current song */
239 purple_util_set_current_song(tuple->title ? tuple->title : "",
240 tuple->artist ? tuple->artist : "",
241 tuple->album ? tuple->album : "");
242
243 song_info = format_song_info(tuple);
244
245 aud_process(song_info);
246 free_song_tuple(tuple);
247 g_free(song_info);
248 }
249
250
251 static void
252 status_signal_cb(DBusGProxy *player_proxy, gint status, gpointer data)
253 {
254 aud_debug("StatusChange %d\n", status);
255
256 if (status != 0) {
257 aud_process("");
258
259 /* clear current song */
260 purple_util_set_current_song(NULL, NULL, NULL);
261
262 }
263 }
264
265
266 #if 0
83 static gboolean 267 static gboolean
84 watchdog_func(void) 268 watchdog_func(void)
85 { 269 {
86 gint playpos = 0; 270 gint playpos = 0;
87 gchar *song = NULL, *tmp = NULL; 271 gchar *song = NULL, *tmp = NULL;
88 272
89 gboolean rv = TRUE; 273 gboolean rv = TRUE;
90 size_t dummy; 274 size_t dummy;
91 275
92 if(!session) { 276 if(!session) {
93 session = get_dbus_proxy(); 277 session = get_dbus_session();
94 } 278 }
95 279
96 aud_debug("session = %p\n", session); 280 aud_debug("session = %p\n", session);
97 aud_debug("is_playing = %d\n", audacious_remote_is_playing(session)); 281 aud_debug("is_playing = %d\n", audacious_remote_is_playing(session));
98 282
132 g_free(title); 316 g_free(title);
133 g_free(album); 317 g_free(album);
134 318
135 return rv; 319 return rv;
136 } 320 }
321 #endif
322
323
324
325
137 326
138 static void 327 static void
139 aud_process_status(PurpleConnection *gc, gchar *aud_info) 328 aud_process_status(PurpleConnection *gc, gchar *aud_info)
140 { 329 {
141 gchar *new = NULL, *key = NULL; 330 gchar *new = NULL, *key = NULL;
320 } 509 }
321 510
322 if (purple_prefs_get_bool(OPT_PROCESS_STATUS)) { 511 if (purple_prefs_get_bool(OPT_PROCESS_STATUS)) {
323 aud_process_status(gc, aud_info); 512 aud_process_status(gc, aud_info);
324 } 513 }
325 514 }
326 } 515
327 } 516 }
517
328 static void 518 static void
329 removekey(gpointer data) 519 removekey(gpointer data)
330 { 520 {
331 g_free(data); 521 g_free(data);
332 } 522 }
334 static void 524 static void
335 removeval(gpointer data) 525 removeval(gpointer data)
336 { 526 {
337 g_free(data); 527 g_free(data);
338 } 528 }
529
530
531
532
339 533
340 static PurpleCmdRet 534 static PurpleCmdRet
341 paste_current_song(PurpleConversation *conv, const gchar *cmd, 535 paste_current_song(PurpleConversation *conv, const gchar *cmd,
342 gchar **args, gchar **error, void *data) 536 gchar **args, gchar **error, void *data)
343 { 537 {
344 gint playpos = 0;
345 gchar *song = NULL, *tmp = NULL, *tmp2 = NULL; 538 gchar *song = NULL, *tmp = NULL, *tmp2 = NULL;
346 PurpleConversationType type = purple_conversation_get_type(conv); 539 PurpleConversationType type = purple_conversation_get_type(conv);
347 size_t dummy; 540 size_t dummy;
348 const gchar *template = NULL; 541 const gchar *template = NULL;
349 542
350 /* audacious isn't playing */ 543 /* audacious isn't playing */
351 if(!audacious_remote_is_playing(session)) { 544 if(!is_app_running()) {
352 return PURPLE_CMD_RET_OK; 545 return PURPLE_CMD_RET_OK;
353 } 546 }
354 547
355 playpos = audacious_remote_get_playlist_pos(session); 548 /* dbus lookup */
356 tmp = audacious_remote_get_playlist_title(session, playpos); 549 GHashTable *table = get_song_table();
550 song_tuple *tuple = get_song_tuple(table);
551 tmp = format_song_info(tuple);
552 free_song_tuple(tuple);
553 g_hash_table_destroy(table);
357 554
358 template = purple_prefs_get_string(OPT_PASTE_TEMPLATE); 555 template = purple_prefs_get_string(OPT_PASTE_TEMPLATE);
359 556
360 if(template && strstr(template, SONG_TOKEN)) { 557 if(template && strstr(template, SONG_TOKEN)) {
361 tmp2 = purple_strreplace(template, SONG_TOKEN, tmp); 558 tmp2 = purple_strreplace(template, SONG_TOKEN, tmp);
389 586
390 g_free(song); 587 g_free(song);
391 return PURPLE_CMD_RET_OK; 588 return PURPLE_CMD_RET_OK;
392 } 589 }
393 590
591
592
593
594
595 static gboolean
596 is_app_running(void)
597 {
598 gchar *player_name = g_strconcat("org.mpris.", "audacious", NULL);
599
600 #if 0
601 if(g_strcasecmp(pidginmpris->player_name, player_name) != 0) {
602 pidginmpris->player_name = g_strdup(player_name);
603 g_object_unref(pidginmpris->player);
604 mpris_connect_dbus_signals();
605 }
606 #endif
607
608 DBusGProxy *player =
609 dbus_g_proxy_new_for_name_owner(connection,
610 player_name,
611 "/Player",
612 "org.freedesktop.MediaPlayer",
613 NULL);
614
615 if(!player)
616 return FALSE;
617
618 g_object_unref(player);
619 g_free(player_name);
620 return TRUE;
621 }
622
623 static void
624 signed_on_cb(PurpleConnection *gc, void *data)
625 {
626 gchar *song_info = NULL;
627 GHashTable *table = NULL;
628 song_tuple *tuple = NULL;
629
630 table = get_song_table();
631 tuple = get_song_tuple(table);
632
633 song_info = format_song_info(tuple);
634 free_song_tuple(tuple);
635 g_hash_table_destroy(table);
636
637 if(song_info)
638 aud_process(song_info);
639
640 g_free(song_info);
641 }
642
643
644 static void prefs_cb(const char *name, PurplePrefType type,
645 gconstpointer value, gpointer data)
646 {
647 aud_debug("settings change detected at %s\n", name);
648 signed_on_cb(NULL, NULL);
649 }
650
394 static gboolean 651 static gboolean
395 load_plugin(PurplePlugin *plugin) 652 load_plugin(PurplePlugin *plugin)
396 { 653 {
397 seed_status = g_hash_table_new_full(g_str_hash, g_str_equal, 654 seed_status = g_hash_table_new_full(g_str_hash, g_str_equal,
398 removekey, removeval); 655 removekey, removeval);
402 pushed_status = g_hash_table_new_full(g_str_hash, g_str_equal, 659 pushed_status = g_hash_table_new_full(g_str_hash, g_str_equal,
403 removekey, removeval); 660 removekey, removeval);
404 pushed_userinfo = g_hash_table_new_full(g_str_hash, g_str_equal, 661 pushed_userinfo = g_hash_table_new_full(g_str_hash, g_str_equal,
405 removekey, removeval); 662 removekey, removeval);
406 663
407 timeout_tag = g_timeout_add(15*1000, (gpointer)watchdog_func, NULL); 664
408 665 session = get_dbus_session();
666
667 /* connect to mpris signals */
668 connect_dbus_signals();
669
670 /* connect to purple signals */
671 purple_signal_connect(purple_connections_get_handle(),
672 "signed-on",
673 plugin,
674 PURPLE_CALLBACK(signed_on_cb),
675 NULL);
676
677 purple_signal_connect(purple_savedstatuses_get_handle(),
678 "savedstatus-changed",
679 plugin,
680 PURPLE_CALLBACK(signed_on_cb),
681 NULL);
682
683 purple_prefs_connect_callback(purple_prefs_get_handle(),
684 OPT_PIDGINAUD,
685 prefs_cb,
686 NULL);
687
688 status_signal_cb(NULL, -1, NULL);
689
690
691 /* register /song command */
409 cmdid_paste_current_song = 692 cmdid_paste_current_song =
410 purple_cmd_register("song", "", PURPLE_CMD_P_DEFAULT, 693 purple_cmd_register("song", "", PURPLE_CMD_P_DEFAULT,
411 PURPLE_CMD_FLAG_IM | PURPLE_CMD_FLAG_CHAT, 694 PURPLE_CMD_FLAG_IM | PURPLE_CMD_FLAG_CHAT,
412 NULL, paste_current_song, 695 NULL, paste_current_song,
413 "song: Paste currently plaing song", NULL); 696 "song: Paste currently plaing song", NULL);
418 static gboolean 701 static gboolean
419 unload_plugin(PurplePlugin *plugin) 702 unload_plugin(PurplePlugin *plugin)
420 { 703 {
421 aud_debug("pidgin-audacious unload called\n"); 704 aud_debug("pidgin-audacious unload called\n");
422 705
423 g_source_remove(timeout_tag);
424
425 g_hash_table_destroy(seed_status); 706 g_hash_table_destroy(seed_status);
426 g_hash_table_destroy(seed_userinfo); 707 g_hash_table_destroy(seed_userinfo);
427 708
428 g_hash_table_destroy(pushed_status); 709 g_hash_table_destroy(pushed_status);
429 g_hash_table_destroy(pushed_userinfo); 710 g_hash_table_destroy(pushed_userinfo);
430 711
431 purple_cmd_unregister(cmdid_paste_current_song); 712 purple_cmd_unregister(cmdid_paste_current_song);
432 713
433 g_object_unref(session); 714 if(session) {
434 session = NULL; 715 g_object_unref(session);
716 session = NULL;
717 }
435 718
436 if(connection) { 719 if(connection) {
437 dbus_g_connection_unref(connection); 720 dbus_g_connection_unref(connection);
438 connection = NULL; 721 connection = NULL;
439 } 722 }
458 purple_plugin_pref_frame_add(frame, pref); 741 purple_plugin_pref_frame_add(frame, pref);
459 742
460 pref = purple_plugin_pref_new_with_name_and_label( 743 pref = purple_plugin_pref_new_with_name_and_label(
461 OPT_PROCESS_USERINFO, 744 OPT_PROCESS_USERINFO,
462 "Expand " SONG_TOKEN " to song info in the user info"); 745 "Expand " SONG_TOKEN " to song info in the user info");
746 purple_plugin_pref_frame_add(frame, pref);
747
748 /* song template */
749 pref = purple_plugin_pref_new_with_name_and_label(
750 OPT_SONG_TEMPLATE,
751 "Song template");
463 purple_plugin_pref_frame_add(frame, pref); 752 purple_plugin_pref_frame_add(frame, pref);
464 753
465 /* paste template */ 754 /* paste template */
466 pref = purple_plugin_pref_new_with_name_and_label( 755 pref = purple_plugin_pref_new_with_name_and_label(
467 OPT_PASTE_TEMPLATE, 756 OPT_PASTE_TEMPLATE,
486 0, /**< flags */ 775 0, /**< flags */
487 NULL, /**< deps */ 776 NULL, /**< deps */
488 PURPLE_PRIORITY_DEFAULT, /**< priority */ 777 PURPLE_PRIORITY_DEFAULT, /**< priority */
489 PIDGINAUD_PLUGIN_ID, /**< id */ 778 PIDGINAUD_PLUGIN_ID, /**< id */
490 "Pidgin-Audacious", /**< name */ 779 "Pidgin-Audacious", /**< name */
491 "2.1.0d2", /**< version */ 780 "3.0.0d2", /**< version */
492 "Automatically updates your Pidgin status info with the currently " 781 "Automatically updates your Pidgin status info with the currently "
493 "playing music in Audacious.", /** summary */ 782 "playing music in Audacious.", /** summary */
494 "Automatically updates your Pidgin status info with the currently " 783 "Automatically updates your Pidgin status info with the currently "
495 "playing music in Audacious.", /** desc */ 784 "playing music in Audacious.", /** desc */
496 "Yoshiki Yazawa (yaz@honeyplanet.jp)", /**< author */ 785 "Yoshiki Yazawa (yaz@honeyplanet.jp)", /**< author */
511 800
512 /* add plugin preferences */ 801 /* add plugin preferences */
513 purple_prefs_add_none(OPT_PIDGINAUD); 802 purple_prefs_add_none(OPT_PIDGINAUD);
514 purple_prefs_add_bool(OPT_PROCESS_STATUS, TRUE); 803 purple_prefs_add_bool(OPT_PROCESS_STATUS, TRUE);
515 purple_prefs_add_bool(OPT_PROCESS_USERINFO, TRUE); 804 purple_prefs_add_bool(OPT_PROCESS_USERINFO, TRUE);
805 purple_prefs_add_string(OPT_SONG_TEMPLATE, DEFAULT_SONG_TEMPLATE);
516 purple_prefs_add_string(OPT_PASTE_TEMPLATE, SONG_TOKEN); 806 purple_prefs_add_string(OPT_PASTE_TEMPLATE, SONG_TOKEN);
517 807
518 } 808 }
519 809
520 PURPLE_INIT_PLUGIN(pidgin_audacious, init_plugin, info) 810 PURPLE_INIT_PLUGIN(pidgin_audacious, init_plugin, info)