[gnome-bluetooth/meego-dev: 3/15] Add a bluez-backed power switch implementation



commit 4da9c6c5cf81e7b69d7925ca6eed46dfe1b344be
Author: Ross Burton <ross linux intel com>
Date:   Mon Jan 25 17:22:03 2010 +0000

    Add a bluez-backed power switch implementation

 moblin/Makefile.am             |    4 +-
 moblin/bluetooth-powerswitch.c |  169 ++++++++++++++++++++++++++++++++++++++++
 moblin/bluetooth-powerswitch.h |   75 ++++++++++++++++++
 3 files changed, 247 insertions(+), 1 deletions(-)
---
diff --git a/moblin/Makefile.am b/moblin/Makefile.am
index e1787a7..641d551 100644
--- a/moblin/Makefile.am
+++ b/moblin/Makefile.am
@@ -2,7 +2,9 @@ SUBDIRS = moblin-copy-n-paste theme moblin-icons
 
 bin_PROGRAMS = bluetooth-panel
 
-bluetooth_panel_SOURCES = main.c moblin-panel.c moblin-panel.h
+bluetooth_panel_SOURCES = main.c \
+	moblin-panel.c moblin-panel.h \
+	bluetooth-powerswitch.c bluetooth-powerswitch.h
 
 bluetooth_panel_LDADD = $(top_builddir)/lib/libcommon.la $(top_builddir)/lib/libgnome-bluetooth.la \
 			$(top_builddir)/moblin/moblin-copy-n-paste/libmoblin.la \
diff --git a/moblin/bluetooth-powerswitch.c b/moblin/bluetooth-powerswitch.c
new file mode 100644
index 0000000..d062784
--- /dev/null
+++ b/moblin/bluetooth-powerswitch.c
@@ -0,0 +1,169 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2005-2008  Marcel Holtmann <marcel holtmann org>
+ *  Copyright (C) 2006-2009  Bastien Nocera <hadess hadess net>
+ *
+ *
+ *  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 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+/**
+ * SECTION:bluetooth-powerswitch
+ * @short_description: a Bluetooth power switch object
+ * @stability: Stable
+ * @include: bluetooth-powerswitch.h
+ *
+ * An object to manipulate Bluetooth power switches.
+ **/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib.h>
+#include "bluetooth-client.h"
+#include "bluetooth-powerswitch.h"
+
+enum {
+	STATE_CHANGED,
+	LAST_SIGNAL
+};
+
+static int signals[LAST_SIGNAL] = { 0 };
+
+#define BLUETOOTH_POWERSWITCH_GET_PRIVATE(obj)				\
+	(G_TYPE_INSTANCE_GET_PRIVATE((obj), BLUETOOTH_TYPE_POWERSWITCH, BluetoothPowerswitchPrivate))
+
+struct _BluetoothPowerswitchPrivate {
+	BluetoothClient *client;
+};
+
+G_DEFINE_TYPE(BluetoothPowerswitch, bluetooth_powerswitch, G_TYPE_OBJECT)
+
+void
+bluetooth_powerswitch_set_state (BluetoothPowerswitch *powerswitch,
+				PowerswitchState state)
+{
+	BluetoothPowerswitchPrivate *priv = BLUETOOTH_POWERSWITCH_GET_PRIVATE (powerswitch);
+
+	g_return_if_fail (state == POWERSWITCH_STATE_OFF ||
+			  state == POWERSWITCH_STATE_ON);
+
+	g_object_set (priv->client, "default-adapter-powered", state, NULL);
+}
+
+PowerswitchState
+bluetooth_powerswitch_get_state (BluetoothPowerswitch *powerswitch)
+{
+	BluetoothPowerswitchPrivate *priv;
+	gboolean powered;
+
+	g_return_val_if_fail (BLUETOOTH_IS_POWERSWITCH (powerswitch), POWERSWITCH_STATE_NO_ADAPTER);
+
+	priv = BLUETOOTH_POWERSWITCH_GET_PRIVATE (powerswitch);
+
+	if (bluetooth_powerswitch_has_powerswitches (powerswitch)) {
+		g_object_get (priv->client, "default-adapter-powered", &powered, NULL);
+		return powered;
+	} else {
+		return POWERSWITCH_STATE_NO_ADAPTER;
+	}
+}
+
+gboolean
+bluetooth_powerswitch_has_powerswitches (BluetoothPowerswitch *powerswitch)
+{
+	BluetoothPowerswitchPrivate *priv = BLUETOOTH_POWERSWITCH_GET_PRIVATE (powerswitch);
+	char *adapter = NULL;
+
+	g_return_val_if_fail (BLUETOOTH_IS_POWERSWITCH (powerswitch), FALSE);
+
+	g_object_get (priv->client, "default-adapter", &adapter, NULL);
+
+	if (adapter) {
+		g_free (adapter);
+		return TRUE;
+	} else {
+		return FALSE;
+	}
+}
+
+static void
+powered_notify_cb (GObject *object, GParamSpec *pspec, gpointer user_data)
+{
+	BluetoothPowerswitch *powerswitch = BLUETOOTH_POWERSWITCH (user_data);
+
+	g_signal_emit (G_OBJECT (powerswitch),
+		       signals[STATE_CHANGED],
+		       0, bluetooth_powerswitch_get_state (powerswitch));
+}
+
+static void
+bluetooth_powerswitch_init (BluetoothPowerswitch *powerswitch)
+{
+	BluetoothPowerswitchPrivate *priv = BLUETOOTH_POWERSWITCH_GET_PRIVATE (powerswitch);
+
+	powerswitch->priv = priv;
+
+	priv->client = bluetooth_client_new ();
+
+	g_signal_connect (priv->client, "notify::default-adapter-powered",
+			  G_CALLBACK (powered_notify_cb), powerswitch);
+
+	g_signal_emit (G_OBJECT (powerswitch),
+		       signals[STATE_CHANGED],
+		       0, bluetooth_powerswitch_get_state (powerswitch));
+}
+
+static void
+bluetooth_powerswitch_dispose (GObject *object)
+{
+	BluetoothPowerswitchPrivate *priv = BLUETOOTH_POWERSWITCH_GET_PRIVATE (object);
+
+	if (priv->client) {
+		g_object_unref (priv->client);
+		priv->client = NULL;
+	}
+
+	G_OBJECT_CLASS(bluetooth_powerswitch_parent_class)->dispose(object);
+}
+
+static void
+bluetooth_powerswitch_class_init(BluetoothPowerswitchClass *klass)
+{
+	GObjectClass *object_class = (GObjectClass *) klass;
+
+	g_type_class_add_private(klass, sizeof(BluetoothPowerswitchPrivate));
+	object_class->dispose = bluetooth_powerswitch_dispose;
+
+	signals[STATE_CHANGED] =
+		g_signal_new ("state-changed",
+			      G_TYPE_FROM_CLASS (klass),
+			      G_SIGNAL_RUN_LAST,
+			      G_STRUCT_OFFSET (BluetoothPowerswitchClass, state_changed),
+			      NULL, NULL,
+			      g_cclosure_marshal_VOID__INT,
+			      G_TYPE_NONE, 1, G_TYPE_INT);
+
+}
+
+BluetoothPowerswitch *
+bluetooth_powerswitch_new (void)
+{
+	return BLUETOOTH_POWERSWITCH(g_object_new (BLUETOOTH_TYPE_POWERSWITCH, NULL));
+}
diff --git a/moblin/bluetooth-powerswitch.h b/moblin/bluetooth-powerswitch.h
new file mode 100644
index 0000000..9afa24a
--- /dev/null
+++ b/moblin/bluetooth-powerswitch.h
@@ -0,0 +1,75 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2005-2008  Marcel Holtmann <marcel holtmann org>
+ *  Copyright (C) 2006-2009  Bastien Nocera <hadess hadess net>
+ *
+ *
+ *  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 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef __BLUETOOTH_POWERSWITCH_H
+#define __BLUETOOTH_POWERSWITCH_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+typedef enum {
+	POWERSWITCH_STATE_NO_ADAPTER = -1,
+	POWERSWITCH_STATE_OFF = FALSE,
+	POWERSWITCH_STATE_ON = TRUE
+} PowerswitchState;
+
+#define BLUETOOTH_TYPE_POWERSWITCH (bluetooth_powerswitch_get_type())
+#define BLUETOOTH_POWERSWITCH(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \
+					BLUETOOTH_TYPE_POWERSWITCH, BluetoothPowerswitch))
+#define BLUETOOTH_POWERSWITCH_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), \
+					BLUETOOTH_TYPE_POWERSWITCH, BluetoothPowerswitchClass))
+#define BLUETOOTH_IS_POWERSWITCH(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), \
+							BLUETOOTH_TYPE_POWERSWITCH))
+#define BLUETOOTH_IS_POWERSWITCH_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), \
+							BLUETOOTH_TYPE_POWERSWITCH))
+#define BLUETOOTH_GET_POWERSWITCH_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), \
+					BLUETOOTH_TYPE_POWERSWITCH, BluetoothPowerswitchClass))
+
+typedef struct _BluetoothPowerswitch BluetoothPowerswitch;
+typedef struct _BluetoothPowerswitchClass BluetoothPowerswitchClass;
+typedef struct _BluetoothPowerswitchPrivate BluetoothPowerswitchPrivate;
+
+struct _BluetoothPowerswitch {
+	GObject parent;
+	BluetoothPowerswitchPrivate *priv;
+};
+
+struct _BluetoothPowerswitchClass {
+	GObjectClass parent_class;
+
+	void (*state_changed) (BluetoothPowerswitch *powerswitch, PowerswitchState state);
+};
+
+GType bluetooth_powerswitch_get_type(void);
+
+BluetoothPowerswitch * bluetooth_powerswitch_new (void);
+
+gboolean bluetooth_powerswitch_has_powerswitches (BluetoothPowerswitch *powerswitch);
+void bluetooth_powerswitch_set_state (BluetoothPowerswitch *powerswitch, PowerswitchState state);
+PowerswitchState bluetooth_powerswitch_get_state (BluetoothPowerswitch *powerswitch);
+
+G_END_DECLS
+
+#endif /* __BLUETOOTH_POWERSWITCH_H */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]