Accept D-Bus signals with playing song and publish tune.
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Mon, 18 May 2009 06:57:19 +0000 (03:57 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Mon, 18 May 2009 06:57:19 +0000 (03:57 -0300)
Makefile
status.c

index d28414d..b0bdb71 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 CC = gcc
 CC = gcc
-CFLAGS = -g -Wall `pkg-config --cflags iksemel glib-2.0`
-LIBS = `pkg-config --libs iksemel glib-2.0`
+CFLAGS = -g -Wall `pkg-config --cflags iksemel glib-2.0 dbus-glib-1`
+LIBS = `pkg-config --libs iksemel glib-2.0 dbus-glib-1`
 OBJECTS = status.o
 
 .c.o:
 OBJECTS = status.o
 
 .c.o:
index db14f74..afb71bd 100644 (file)
--- a/status.c
+++ b/status.c
@@ -23,6 +23,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib-lowlevel.h>
 
 static char * server = "vespa.holoscopio.com";
 static char * username = "pubsub";
 
 static char * server = "vespa.holoscopio.com";
 static char * username = "pubsub";
@@ -415,12 +417,91 @@ handler (GIOChannel *channel, GIOCondition cond, gpointer data)
   return TRUE;
 }
 
   return TRUE;
 }
 
+void
+tune_add_dbus_arg (iks *tune, DBusMessageIter *args)
+{
+  DBusMessageIter entry;
+  DBusMessageIter var;
+  char *strkey = NULL;
+  char *strval = NULL;
+  dbus_message_iter_recurse (args, &entry);
+  if (dbus_message_iter_get_arg_type (&entry) == DBUS_TYPE_STRING)
+    {
+      dbus_message_iter_get_basic (&entry, &strkey);
+      dbus_message_iter_next (&entry);
+      if (dbus_message_iter_get_arg_type (&entry) == DBUS_TYPE_VARIANT)
+        {
+          dbus_message_iter_recurse (&entry, &var);
+          if (dbus_message_iter_get_arg_type (&var) == DBUS_TYPE_STRING)
+            {
+              dbus_message_iter_get_basic (&var, &strval);
+            }
+        }
+    }
+  else
+    printf ("%c\n", dbus_message_iter_get_arg_type (&entry));
+  if (strkey && strval)
+    {
+      iks_insert_cdata (iks_insert (tune, strkey), strval, 0);
+    }
+}
+
+iks *
+tune_from_dbus (DBusMessage *msg)
+{
+  DBusMessageIter args;
+  iks *tune;
+  tune = iks_new ("tune");
+  iks_insert_attrib (tune, "xmlns", "http://jabber.org/protocol/tune");
+  dbus_message_iter_init (msg, &args);
+  if (dbus_message_iter_get_arg_type (&args) == DBUS_TYPE_ARRAY)
+    {
+      DBusMessageIter dict;
+      dbus_message_iter_recurse (&args, &dict);
+      while (dbus_message_iter_get_arg_type (&dict) ==
+             DBUS_TYPE_DICT_ENTRY)
+        {
+          tune_add_dbus_arg (tune, &dict);
+          dbus_message_iter_next (&dict);
+        }
+    }
+  return tune;
+}
+
+DBusHandlerResult
+gettune (DBusConnection *conn, DBusMessage *msg, void *data)
+{
+  iks *tune;
+  if (dbus_message_is_signal (msg, "org.MetaPlayer.tag", "playing"))
+    {
+      tune = tune_from_dbus (msg);
+      pushtune (data, "http://jabber.org/protocol/tune", tune);
+      return DBUS_HANDLER_RESULT_HANDLED;
+    }
+  else
+    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+static void
+prepare_dbus (gpointer parser)
+{
+  DBusConnection *conn;
+  conn = dbus_bus_get (DBUS_BUS_SESSION, NULL);
+  dbus_bus_register (conn, NULL);
+  dbus_bus_add_match (conn,
+                      "type='signal'", NULL);
+  dbus_connection_flush (conn);
+  dbus_connection_setup_with_g_main (conn, g_main_context_default ());
+  dbus_connection_add_filter (conn, gettune, parser, NULL);
+}
+
 void
 loop (iksparser *parser)
 {
   GIOChannel *channel;
   channel = g_io_channel_unix_new (iks_fd (parser));
   g_io_add_watch (channel, G_IO_IN, handler, parser);
 void
 loop (iksparser *parser)
 {
   GIOChannel *channel;
   channel = g_io_channel_unix_new (iks_fd (parser));
   g_io_add_watch (channel, G_IO_IN, handler, parser);
+  prepare_dbus (parser);
   g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
 }
 
   g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
 }