Advertisement
Guest User

Untitled

a guest
Nov 4th, 2015
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. #include "musictracker.h"
  2. #include "utils.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <glib.h>
  7. #include <dbus/dbus.h>
  8. #include <dbus/dbus-glib.h>
  9. #include <string.h>
  10.  
  11. #ifndef WIN32
  12. #include <config.h>
  13. #else
  14. #include <config-win32.h>
  15. #endif
  16.  
  17. #include "gettext.h"
  18. #define _(String) dgettext (PACKAGE, String)
  19.  
  20. #define mpris2_debug(fmt, ...) purple_debug(PURPLE_DEBUG_INFO, "MPRIS2", \
  21. fmt, ## __VA_ARGS__);
  22. #define mpris2_error(fmt, ...) purple_debug(PURPLE_DEBUG_ERROR, "MPRIS2", \
  23. fmt, ## __VA_ARGS__);
  24.  
  25. void
  26. get_mpris2_info(struct TrackInfo* ti)
  27. {
  28. DBusGConnection *connection;
  29. GError *error;
  30. DBusGProxy *proxy, *proxy2;
  31. char **name_list;
  32. char **name_list_ptr;
  33. GValue get_value = {0,{{0}}};
  34. GValue h = {0,{{0}}};
  35. GValue pos = {0,{{0}}};
  36.  
  37. error = NULL;
  38. connection = dbus_g_bus_get (DBUS_BUS_SESSION,
  39. &error);
  40. if (connection == NULL)
  41. {
  42. mpris2_error ("Failed to open connection to bus: %s\n",
  43. error->message);
  44. g_error_free (error);
  45. return;
  46. }
  47.  
  48. proxy = dbus_g_proxy_new_for_name (connection,
  49. DBUS_SERVICE_DBUS,
  50. DBUS_PATH_DBUS,
  51. DBUS_INTERFACE_DBUS);
  52. if (!dbus_g_proxy_call (proxy, "ListNames", &error, G_TYPE_INVALID,
  53. G_TYPE_STRV, &name_list, G_TYPE_INVALID))
  54. {
  55. if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
  56. {
  57. mpris2_error ("Caught remote method exception %s: %s",
  58. dbus_g_error_get_name (error),
  59. error->message);
  60. }
  61. else
  62. {
  63. mpris2_error ("Error: %s\n", error->message);
  64. }
  65. g_error_free (error);
  66. return;
  67. }
  68.  
  69. for (name_list_ptr = name_list; *name_list_ptr; name_list_ptr++)
  70. {
  71. if (strncmp("org.mpris.MediaPlayer2.", *name_list_ptr, strlen("org.mpris.MediaPlayer2.")) == 0)
  72. {
  73. mpris2_debug("Found player %s\n", *name_list_ptr);
  74. proxy2 = dbus_g_proxy_new_for_name (connection,
  75. *name_list_ptr,
  76. "/org/mpris/MediaPlayer2",
  77. "org.freedesktop.DBus.Properties");
  78. if (!dbus_g_proxy_call (proxy2, "Get", &error,
  79. G_TYPE_STRING, "org.mpris.MediaPlayer2.Player",
  80. G_TYPE_STRING, "Metadata",
  81. G_TYPE_INVALID,
  82. G_TYPE_VALUE, &get_value,
  83. G_TYPE_INVALID))
  84. {
  85. if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
  86. {
  87. mpris2_error ("Caught remote method exception %s: %s",
  88. dbus_g_error_get_name (error),
  89. error->message);
  90. }
  91. else
  92. {
  93. mpris2_error ("Error: %s\n", error->message);
  94. }
  95. g_error_free(error);
  96. goto cleanup;
  97. }
  98. GHashTable *dict = g_value_get_boxed(&get_value);
  99. gpointer tmp;
  100. tmp = g_hash_table_lookup(dict, "xesam:artist");
  101. if (tmp != NULL)
  102. {
  103. GStrv strv = g_value_get_boxed(g_hash_table_lookup(dict, "xesam:artist"));
  104. g_strlcpy(ti->artist, *strv, STRLEN);
  105. ti->artist[STRLEN-1] = 0;
  106. g_strfreev(strv);
  107. }
  108.  
  109. tmp = g_hash_table_lookup(dict, "xesam:album");
  110. if (tmp != NULL)
  111. {
  112. g_strlcpy(ti->album,
  113. g_value_get_string(g_hash_table_lookup(dict, "xesam:album")),
  114. STRLEN);
  115. ti->album[STRLEN-1] = 0;
  116. }
  117.  
  118. tmp = g_hash_table_lookup(dict, "xesam:title");
  119. if (tmp != NULL)
  120. {
  121. g_strlcpy(ti->track,
  122. g_value_get_string(g_hash_table_lookup(dict, "xesam:title")),
  123. STRLEN);
  124. ti->track[STRLEN-1] = 0;
  125. }
  126.  
  127. if (!dbus_g_proxy_call (proxy2, "Get", &error,
  128. G_TYPE_STRING, "org.mpris.MediaPlayer2.Player",
  129. G_TYPE_STRING, "PlaybackStatus",
  130. G_TYPE_INVALID,
  131. G_TYPE_VALUE, &h,
  132. G_TYPE_INVALID))
  133. {
  134. if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
  135. {
  136. mpris2_error ("Caught remote method exception %s: %s",
  137. dbus_g_error_get_name (error),
  138. error->message);
  139. }
  140. else
  141. {
  142. mpris2_error ("Error: %s\n", error->message);
  143. }
  144. g_error_free (error);
  145. goto cleanup;
  146. }
  147.  
  148. // get position
  149. if (!dbus_g_proxy_call (proxy2, "Get", &error,
  150. G_TYPE_STRING, "org.mpris.MediaPlayer2.Player",
  151. G_TYPE_STRING, "Position",
  152. G_TYPE_INVALID,
  153. G_TYPE_VALUE, &pos,
  154. G_TYPE_INVALID))
  155. {
  156. if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
  157. {
  158. mpris2_error ("Caught remote method exception %s: %s",
  159. dbus_g_error_get_name (error),
  160. error->message);
  161. }
  162. else
  163. {
  164. mpris2_error ("Error: %s\n", error->message);
  165. }
  166. g_error_free (error);
  167. goto cleanup;
  168. }
  169. gint64 position = g_value_get_int64 (&pos);
  170. ti->currentSecs = position/1000000;
  171. // [end] get position
  172.  
  173. const gchar *status = g_value_get_string(&h);
  174. if (g_strcmp0(status, "Playing") == 0)
  175. ti->status = PLAYER_STATUS_PLAYING;
  176. else if (g_strcmp0(status, "Paused") == 0)
  177. ti->status = PLAYER_STATUS_PAUSED;
  178. else if (g_strcmp0(status, "Stopped") == 0)
  179. ti->status = PLAYER_STATUS_STOPPED;
  180. goto cleanup;
  181. }
  182. }
  183. ti->status = PLAYER_STATUS_CLOSED;
  184.  
  185. cleanup:
  186. g_strfreev(name_list);
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement