summarylogtreecommitdiffstats
path: root/pidgin-musictracker-mpris2.diff
blob: 33cd6719b7b6789e4bd51bf644d77b75a37ebac7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
diff -r ea549732f58b src/Makefile.am
--- a/src/Makefile.am	Sun Oct 10 20:01:21 2010 +0000
+++ b/src/Makefile.am	Fri Jul 22 18:26:40 2011 +0200
@@ -33,6 +33,7 @@
 						  listen.c \
 						  xmms2.c \
 						  mpris.c \
+						  mpris2.c \
 						  dbusbird.c \
 						  moc.c \
 						  lastfm_ws.c
diff -r ea549732f58b src/mpris2.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/mpris2.c	Fri Jul 22 18:26:40 2011 +0200
@@ -0,0 +1,161 @@
+#include "musictracker.h"
+#include "utils.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <glib.h>
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib.h>
+#include <string.h>
+
+#ifndef WIN32
+#include <config.h>
+#else
+#include <config-win32.h>
+#endif
+
+#include "gettext.h"
+#define _(String) dgettext (PACKAGE, String)
+
+#define mpris2_debug(fmt, ...)	purple_debug(PURPLE_DEBUG_INFO, "MPRIS2", \
+					fmt, ## __VA_ARGS__);
+#define mpris2_error(fmt, ...)	purple_debug(PURPLE_DEBUG_ERROR, "MPRIS2", \
+					fmt, ## __VA_ARGS__);
+
+void
+get_mpris2_info(struct TrackInfo* ti)
+{
+  DBusGConnection *connection;
+  GError *error;
+  DBusGProxy *proxy, *proxy2;
+  char **name_list;
+  char **name_list_ptr;
+  GValue get_value = {0,{{0}}};
+  GValue h = {0,{{0}}};
+  
+  error = NULL;
+  connection = dbus_g_bus_get (DBUS_BUS_SESSION,
+                               &error);
+  if (connection == NULL)
+  {
+    mpris2_error ("Failed to open connection to bus: %s\n",
+                error->message);
+    g_error_free (error);
+    return;
+  }
+  
+  proxy = dbus_g_proxy_new_for_name (connection,
+                                     DBUS_SERVICE_DBUS,
+                                     DBUS_PATH_DBUS,
+                                     DBUS_INTERFACE_DBUS);
+  if (!dbus_g_proxy_call (proxy, "ListNames", &error, G_TYPE_INVALID,
+                          G_TYPE_STRV, &name_list, G_TYPE_INVALID))
+  {
+    if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
+    {
+      mpris2_error ("Caught remote method exception %s: %s",
+                    dbus_g_error_get_name (error),
+                    error->message);
+    }
+    else
+    {
+      mpris2_error ("Error: %s\n", error->message);
+    }
+    g_error_free (error);
+    return;
+  }
+  
+  for (name_list_ptr = name_list; *name_list_ptr; name_list_ptr++)
+  {
+    if (strncmp("org.mpris.MediaPlayer2.", *name_list_ptr, strlen("org.mpris.MediaPlayer2.")) == 0)
+    {
+      mpris2_debug("Found player %s\n", *name_list_ptr);
+      proxy2 = dbus_g_proxy_new_for_name (connection,
+                                          *name_list_ptr,
+                                          "/org/mpris/MediaPlayer2",
+                                          "org.freedesktop.DBus.Properties");
+      if (!dbus_g_proxy_call (proxy2, "Get", &error, 
+                              G_TYPE_STRING, "org.mpris.MediaPlayer2.Player",
+                              G_TYPE_STRING, "Metadata",
+                              G_TYPE_INVALID,
+                              G_TYPE_VALUE, &get_value,
+                              G_TYPE_INVALID))
+      {
+        if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
+        {
+          mpris2_error ("Caught remote method exception %s: %s",
+                        dbus_g_error_get_name (error),
+                        error->message);
+        }
+        else
+        {
+          mpris2_error ("Error: %s\n", error->message);
+        }
+        g_error_free(error);
+        goto cleanup;
+      }
+      GHashTable *dict = g_value_get_boxed(&get_value);
+      gpointer tmp;
+      tmp = g_hash_table_lookup(dict, "xesam:artist");
+      if (tmp != NULL)
+      {
+        GStrv strv = g_value_get_boxed(g_hash_table_lookup(dict, "xesam:artist"));
+        g_strlcpy(ti->artist, *strv, STRLEN);
+        ti->artist[STRLEN-1] = 0;
+        g_strfreev(strv);
+      }
+
+      tmp = g_hash_table_lookup(dict, "xesam:album");
+      if (tmp != NULL)
+      {
+        g_strlcpy(ti->album,
+                  g_value_get_string(g_hash_table_lookup(dict, "xesam:album")),
+                  STRLEN);
+        ti->album[STRLEN-1] = 0;
+      }
+
+      tmp = g_hash_table_lookup(dict, "xesam:title");
+      if (tmp != NULL)
+      {
+        g_strlcpy(ti->track,
+                  g_value_get_string(g_hash_table_lookup(dict, "xesam:title")),
+                  STRLEN);
+        ti->track[STRLEN-1] = 0;
+      }
+
+      if (!dbus_g_proxy_call (proxy2, "Get", &error, 
+                              G_TYPE_STRING, "org.mpris.MediaPlayer2.Player",
+                              G_TYPE_STRING, "PlaybackStatus",
+                              G_TYPE_INVALID,
+                              G_TYPE_VALUE, &h,
+                              G_TYPE_INVALID))
+      {
+        if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
+        {
+          mpris2_error ("Caught remote method exception %s: %s",
+                        dbus_g_error_get_name (error),
+                        error->message);
+        }
+        else
+        {
+          mpris2_error ("Error: %s\n", error->message);
+        }
+        g_error_free (error);
+        goto cleanup;
+      }
+      
+      const gchar *status = g_value_get_string(&h);
+      if (g_strcmp0(status, "Playing") == 0)
+        ti->status = PLAYER_STATUS_PLAYING;
+      else if (g_strcmp0(status, "Paused") == 0)
+        ti->status = PLAYER_STATUS_PAUSED;
+      else if (g_strcmp0(status, "Stopped") == 0)
+        ti->status = PLAYER_STATUS_STOPPED;
+      goto cleanup;
+    }
+  }
+  ti->status = PLAYER_STATUS_CLOSED;
+  
+ cleanup:
+  g_strfreev(name_list);
+}
diff -r ea549732f58b src/musictracker.c
--- a/src/musictracker.c	Sun Oct 10 20:01:21 2010 +0000
+++ b/src/musictracker.c	Fri Jul 22 18:26:40 2011 +0200
@@ -64,6 +64,7 @@
  	{ "XMMS2", get_xmms2_info, get_xmms2_pref },
 #endif
  	{ "MPRIS", get_mpris_info, 0 },
+ 	{ "MPRIS2", get_mpris2_info, 0 },
  	{ "Songbird", get_dbusbird_info, 0 },
 #else
 	{ "Winamp", get_winamp_info, 0 },
diff -r ea549732f58b src/musictracker.h
--- a/src/musictracker.h	Sun Oct 10 20:01:21 2010 +0000
+++ b/src/musictracker.h	Fri Jul 22 18:26:40 2011 +0200
@@ -91,6 +91,7 @@
 void get_xmms2_info(struct TrackInfo* ti);
 void get_squeezecenter_info(struct TrackInfo* ti);
 void get_mpris_info(struct TrackInfo* ti);
+void get_mpris2_info(struct TrackInfo* ti);
 void get_dbusbird_info(struct TrackInfo* ti);
 
 void get_xmmsctrl_pref(GtkBox *box);