3
* BlueZ - Bluetooth protocol stack for Linux
5
* Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
6
* Copyright (C) 2006-2009 Bastien Nocera <hadess@hadess.net>
7
* Copyright (C) 2010 Intel Corp
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27
* SECTION:bluetooth-powerswitch
28
* @short_description: a Bluetooth power switch object
30
* @include: bluetooth-powerswitch.h
32
* An object to manipulate Bluetooth power switches.
40
#include "bluetooth-client.h"
41
#include "bluetooth-powerswitch.h"
48
static int signals[LAST_SIGNAL] = { 0 };
50
#define BLUETOOTH_POWERSWITCH_GET_PRIVATE(obj) \
51
(G_TYPE_INSTANCE_GET_PRIVATE((obj), BLUETOOTH_TYPE_POWERSWITCH, BluetoothPowerswitchPrivate))
53
struct _BluetoothPowerswitchPrivate {
54
BluetoothClient *client;
55
GtkTreeModel *adapters;
58
G_DEFINE_TYPE (BluetoothPowerswitch, bluetooth_powerswitch, G_TYPE_OBJECT)
61
set_state_foreach (GtkTreeModel *model,
66
DBusGProxy *proxy = NULL;
67
GValue value = { 0, };
68
PowerswitchState state = GPOINTER_TO_INT (data);
70
gtk_tree_model_get (model, iter, BLUETOOTH_COLUMN_PROXY, &proxy, -1);
74
g_value_init (&value, G_TYPE_BOOLEAN);
75
g_value_set_boolean (&value, state);
77
/* TODO: do this async to avoid blocking. Maybe _no_reply will do? */
78
dbus_g_proxy_call (proxy, "SetProperty", NULL,
79
G_TYPE_STRING, "Powered",
84
g_value_unset (&value);
85
g_object_unref (proxy);
91
bluetooth_powerswitch_set_state (BluetoothPowerswitch *powerswitch,
92
PowerswitchState state)
94
BluetoothPowerswitchPrivate *priv = BLUETOOTH_POWERSWITCH_GET_PRIVATE (powerswitch);
96
g_return_if_fail (state == POWERSWITCH_STATE_OFF ||
97
state == POWERSWITCH_STATE_ON);
99
gtk_tree_model_foreach (priv->adapters, set_state_foreach, GINT_TO_POINTER (state));
103
get_state_foreach (GtkTreeModel *model,
108
gboolean powered = FALSE;
109
gboolean *global_powered = data;
111
gtk_tree_model_get (model, iter, BLUETOOTH_COLUMN_POWERED, &powered, -1);
114
/* Found a powered adaptor, so we are "on" */
115
*global_powered = TRUE;
118
/* Found an unpowered adaptor, set "off" but continue looking */
119
*global_powered = FALSE;
125
bluetooth_powerswitch_get_state (BluetoothPowerswitch *powerswitch)
127
BluetoothPowerswitchPrivate *priv;
128
gboolean powered = FALSE;
130
g_return_val_if_fail (BLUETOOTH_IS_POWERSWITCH (powerswitch), POWERSWITCH_STATE_NO_ADAPTER);
132
priv = BLUETOOTH_POWERSWITCH_GET_PRIVATE (powerswitch);
134
if (bluetooth_powerswitch_has_powerswitches (powerswitch)) {
135
gtk_tree_model_foreach (priv->adapters, get_state_foreach, &powered);
138
return POWERSWITCH_STATE_NO_ADAPTER;
143
bluetooth_powerswitch_has_powerswitches (BluetoothPowerswitch *powerswitch)
145
BluetoothPowerswitchPrivate *priv = BLUETOOTH_POWERSWITCH_GET_PRIVATE (powerswitch);
147
g_return_val_if_fail (BLUETOOTH_IS_POWERSWITCH (powerswitch), FALSE);
149
return gtk_tree_model_iter_n_children (priv->adapters, NULL);
153
powered_notify_cb (GObject *object, GParamSpec *pspec, gpointer user_data)
155
BluetoothPowerswitch *powerswitch = BLUETOOTH_POWERSWITCH (user_data);
157
g_signal_emit (G_OBJECT (powerswitch),
158
signals[STATE_CHANGED],
159
0, bluetooth_powerswitch_get_state (powerswitch));
163
bluetooth_powerswitch_init (BluetoothPowerswitch *powerswitch)
165
BluetoothPowerswitchPrivate *priv = BLUETOOTH_POWERSWITCH_GET_PRIVATE (powerswitch);
167
powerswitch->priv = priv;
169
priv->client = bluetooth_client_new ();
170
priv->adapters = bluetooth_client_get_adapter_model(priv->client);
172
g_signal_connect (priv->client, "notify::default-adapter-powered",
173
G_CALLBACK (powered_notify_cb), powerswitch);
175
g_signal_emit (G_OBJECT (powerswitch),
176
signals[STATE_CHANGED],
177
0, bluetooth_powerswitch_get_state (powerswitch));
181
bluetooth_powerswitch_dispose (GObject *object)
183
BluetoothPowerswitchPrivate *priv = BLUETOOTH_POWERSWITCH_GET_PRIVATE (object);
186
g_object_unref (priv->client);
190
G_OBJECT_CLASS(bluetooth_powerswitch_parent_class)->dispose(object);
194
bluetooth_powerswitch_class_init(BluetoothPowerswitchClass *klass)
196
GObjectClass *object_class = (GObjectClass *) klass;
198
g_type_class_add_private(klass, sizeof(BluetoothPowerswitchPrivate));
199
object_class->dispose = bluetooth_powerswitch_dispose;
201
signals[STATE_CHANGED] =
202
g_signal_new ("state-changed",
203
G_TYPE_FROM_CLASS (klass),
205
G_STRUCT_OFFSET (BluetoothPowerswitchClass, state_changed),
207
g_cclosure_marshal_VOID__INT,
208
G_TYPE_NONE, 1, G_TYPE_INT);
212
BluetoothPowerswitch *
213
bluetooth_powerswitch_new (void)
215
return BLUETOOTH_POWERSWITCH(g_object_new (BLUETOOTH_TYPE_POWERSWITCH, NULL));