3
* gnome-bluetooth - Bluetooth integration for GNOME
5
* Copyright (C) 2012 Bastien Nocera <hadess@hadess.net>
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30
#include <sys/types.h>
37
#include "rfkill-glib.h"
44
static int signals[LAST_SIGNAL] = { 0 };
46
#define CC_RFKILL_GLIB_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
47
CC_RFKILL_TYPE_GLIB, CcRfkillGlibPrivate))
49
struct CcRfkillGlibPrivate {
55
G_DEFINE_TYPE(CcRfkillGlib, cc_rfkill_glib, G_TYPE_OBJECT)
58
cc_rfkill_glib_send_event (CcRfkillGlib *rfkill, struct rfkill_event *event)
60
g_return_val_if_fail (RFKILL_IS_GLIB (rfkill), -1);
61
g_return_val_if_fail (rfkill->priv->fd > 0, -1);
63
return write (rfkill->priv->fd, event, sizeof(struct rfkill_event));
67
type_to_string (unsigned int type)
72
case RFKILL_TYPE_WLAN:
74
case RFKILL_TYPE_BLUETOOTH:
78
case RFKILL_TYPE_WIMAX:
80
case RFKILL_TYPE_WWAN:
88
op_to_string (unsigned int op)
95
case RFKILL_OP_CHANGE:
97
case RFKILL_OP_CHANGE_ALL:
100
g_assert_not_reached ();
105
print_event (struct rfkill_event *event)
107
g_debug ("RFKILL event: idx %u type %u (%s) op %u (%s) soft %u hard %u",
109
event->type, type_to_string (event->type),
110
event->op, op_to_string (event->op),
111
event->soft, event->hard);
115
emit_changed_signal_and_free (CcRfkillGlib *rfkill,
121
g_signal_emit (G_OBJECT (rfkill),
124
g_list_free_full (events, g_free);
128
event_cb (GIOChannel *source,
129
GIOCondition condition,
130
CcRfkillGlib *rfkill)
136
if (condition & G_IO_IN) {
138
struct rfkill_event event;
141
status = g_io_channel_read_chars (source,
147
while (status == G_IO_STATUS_NORMAL && read == sizeof(event)) {
148
struct rfkill_event *event_ptr;
150
print_event (&event);
152
event_ptr = g_memdup (&event, sizeof(event));
153
events = g_list_prepend (events, event_ptr);
155
status = g_io_channel_read_chars (source,
161
events = g_list_reverse (events);
163
g_debug ("something else happened");
167
emit_changed_signal_and_free (rfkill, events);
173
cc_rfkill_glib_init (CcRfkillGlib *rfkill)
175
CcRfkillGlibPrivate *priv;
177
priv = CC_RFKILL_GLIB_GET_PRIVATE (rfkill);
179
rfkill->priv->fd = -1;
183
cc_rfkill_glib_open (CcRfkillGlib *rfkill)
185
CcRfkillGlibPrivate *priv;
190
g_return_val_if_fail (RFKILL_IS_GLIB (rfkill), -1);
191
g_return_val_if_fail (rfkill->priv->fd == -1, -1);
195
fd = open("/dev/rfkill", O_RDWR);
198
g_warning ("Could not open RFKILL control device, please verify your installation");
202
ret = fcntl(fd, F_SETFL, O_NONBLOCK);
204
g_debug ("Can't set RFKILL control device to non-blocking");
212
struct rfkill_event event;
213
struct rfkill_event *event_ptr;
216
len = read(fd, &event, sizeof(event));
220
g_debug ("Reading of RFKILL events failed");
224
if (len != RFKILL_EVENT_SIZE_V1) {
225
g_warning ("Wrong size of RFKILL event\n");
229
if (event.op != RFKILL_OP_ADD)
232
g_debug ("Read killswitch of type '%s' (idx=%d): soft %d hard %d",
233
type_to_string (event.type),
234
event.idx, event.soft, event.hard);
236
event_ptr = g_memdup (&event, sizeof(event));
237
events = g_list_prepend (events, event_ptr);
240
/* Setup monitoring */
242
priv->channel = g_io_channel_unix_new (priv->fd);
243
priv->watch_id = g_io_add_watch (priv->channel,
244
G_IO_IN | G_IO_HUP | G_IO_ERR,
248
events = g_list_reverse (events);
249
emit_changed_signal_and_free (rfkill, events);
255
cc_rfkill_glib_finalize (GObject *object)
257
CcRfkillGlib *rfkill;
258
CcRfkillGlibPrivate *priv;
260
rfkill = CC_RFKILL_GLIB (object);
263
/* cleanup monitoring */
264
if (priv->watch_id > 0) {
265
g_source_remove (priv->watch_id);
267
g_io_channel_shutdown (priv->channel, FALSE, NULL);
268
g_io_channel_unref (priv->channel);
273
G_OBJECT_CLASS(cc_rfkill_glib_parent_class)->finalize(object);
277
cc_rfkill_glib_class_init(CcRfkillGlibClass *klass)
279
GObjectClass *object_class = (GObjectClass *) klass;
281
g_type_class_add_private(klass, sizeof(CcRfkillGlibPrivate));
282
object_class->finalize = cc_rfkill_glib_finalize;
285
g_signal_new ("changed",
286
G_TYPE_FROM_CLASS (klass),
288
G_STRUCT_OFFSET (CcRfkillGlibClass, changed),
291
G_TYPE_NONE, 1, G_TYPE_POINTER);
296
cc_rfkill_glib_new (void)
298
return CC_RFKILL_GLIB (g_object_new (CC_RFKILL_TYPE_GLIB, NULL));