dbus.c 2.72 KB
Newer Older
1 2 3 4
/* gcompris - timer.c
 *
 * Copyright (C) 2007 Bruno coudoin
 *
5 6 7 8
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
9
 *
10 11 12 13
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
14
 *
15 16
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.
17 18
 */

19 20
/*
 * To test it:
21 22 23 24
 * run GCompris with  --sugarBundleId a --sugarActivityId a
 * dbus-send --dest='org.laptop.Activitya'
 *           /org/laptop/Activity/a org.laptop.Activity.SetActive boolean:true
 *
25
 */
26 27
#include "gcompris.h"
#include <gc_core.h>
28
#include "gc-marshal.h"
29 30 31 32 33 34 35 36 37 38 39 40 41 42

#if USE_DBUS
#include <dbus/dbus-glib.h>
#include <glib-object.h>
#include <dbus/dbus.h>
#define	SUPPORT_OR_RETURN(rv)	{}
#else
#define	SUPPORT_OR_RETURN(rv)	{ return rv; }
#endif

#define _ACTIVITY_SERVICE_NAME	"org.laptop.Activity"
#define _ACTIVITY_SERVICE_PATH	"/org/laptop/Activity"
#define _ACTIVITY_INTERFACE	"org.laptop.Activity"

43
#if USE_DBUS
44
void _SetActive(DBusGProxy *proxy, gint active, void *dummy)
45
{
46
  printf("_SetActive %d\n", active);
47 48 49 50 51 52
  if(active)
    gc_sound_close();
  else
    gc_sound_reopen();

}
53
#endif
54 55 56 57 58 59 60 61 62 63 64 65 66

void
gc_dbus_init(gchar *sugarActivityId)
{
  SUPPORT_OR_RETURN();

#ifdef USE_DBUS
  DBusGConnection *connection;
  GError *error;
  DBusGProxy *proxy;

  g_type_init ();

67 68
  if (!g_thread_supported ()) g_thread_init (NULL);

69 70
  dbus_g_thread_init ();

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  error = NULL;
  connection = dbus_g_bus_get (DBUS_BUS_SESSION,
                               &error);
  if (connection == NULL)
    {
      g_printerr ("Failed to open connection to bus: %s\n",
                  error->message);
      g_error_free (error);
      exit (1);
    }

  gchar *service_name = g_strdup_printf("%s%s",
					_ACTIVITY_SERVICE_NAME,
					sugarActivityId);
  gchar *object_path  = g_strdup_printf("%s/%s",
					_ACTIVITY_SERVICE_PATH,
					sugarActivityId);

89 90 91
  dbus_g_object_register_marshaller (gc_marshal_VOID__BOOLEAN,
				     G_TYPE_NONE, G_TYPE_BOOLEAN, G_TYPE_INVALID);

92 93 94 95 96
  proxy = dbus_g_proxy_new_for_name (connection,
                                     service_name,
                                     object_path,
                                     _ACTIVITY_INTERFACE);

97 98 99
  dbus_g_proxy_add_signal(proxy, "SetActive", G_TYPE_BOOLEAN, G_TYPE_INVALID);
  dbus_g_proxy_connect_signal(proxy, "SetActive",
			      G_CALLBACK(_SetActive), NULL, NULL);
100 101
#endif
}