aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobert Ancell <robert.ancell@canonical.com>2012-12-04 16:34:30 +1300
committerRobert Ancell <robert.ancell@canonical.com>2012-12-04 16:34:30 +1300
commit7dd1e1aaf27d3efded474eb49060131e4a12a6fe (patch)
treeed2bcd726fa981e2e12853010be8dfe748c4c910 /src
parentc9759cfd8c6b7803277bd5feb657a79fe8ddc66e (diff)
downloadayatana-indicator-bluetooth-7dd1e1aaf27d3efded474eb49060131e4a12a6fe.tar.gz
ayatana-indicator-bluetooth-7dd1e1aaf27d3efded474eb49060131e4a12a6fe.tar.bz2
ayatana-indicator-bluetooth-7dd1e1aaf27d3efded474eb49060131e4a12a6fe.zip
Use GnomeBluetooth.Killswitch instead of reading /dev/rfkill directly
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am3
-rw-r--r--src/gnome-bluetooth-1.0.vapi21
-rw-r--r--src/indicator-bluetooth.vala61
-rw-r--r--src/rfkill.vala168
4 files changed, 45 insertions, 208 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 7e4605a..2dd7e41 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -3,8 +3,7 @@ bin_PROGRAMS = indicator-bluetooth
indicator_bluetooth_SOURCES = \
config.vapi \
gnome-bluetooth-1.0.vapi \
- indicator-bluetooth.vala \
- rfkill.vala
+ indicator-bluetooth.vala
indicator_bluetooth_VALAFLAGS = \
--pkg posix \
diff --git a/src/gnome-bluetooth-1.0.vapi b/src/gnome-bluetooth-1.0.vapi
index ac60bf0..2a0387b 100644
--- a/src/gnome-bluetooth-1.0.vapi
+++ b/src/gnome-bluetooth-1.0.vapi
@@ -56,4 +56,25 @@ public void browse_address (GLib.Object? object, string address, uint timestamp,
[CCode (cheader_filename = "bluetooth-utils.h")]
public void send_to_address (string address, string alias);
+[CCode (cheader_filename = "bluetooth-killswitch.h", cprefix = "BLUETOOTH_KILLSWITCH_STATE_")]
+public enum KillswitchState
+{
+ NO_ADAPTER,
+ SOFT_BLOCKED,
+ UNBLOCKED,
+ HARD_BLOCKED
+}
+
+[CCode (cheader_filename = "bluetooth-killswitch.h")]
+public class Killswitch : GLib.Object
+{
+ public Killswitch ();
+ public signal void state_changed (KillswitchState state);
+ public bool has_killswitches ();
+ public void set_state (KillswitchState state);
+ public KillswitchState get_state ();
+ public unowned string state_to_string ();
+ public KillswitchState state { get; set; }
+}
+
}
diff --git a/src/indicator-bluetooth.vala b/src/indicator-bluetooth.vala
index 4089d9b..3f3d583 100644
--- a/src/indicator-bluetooth.vala
+++ b/src/indicator-bluetooth.vala
@@ -12,7 +12,8 @@
public class BluetoothIndicator : AppIndicator.Indicator
{
private GnomeBluetooth.Client client;
- private RFKillManager rfkill;
+ private GnomeBluetooth.Killswitch killswitch;
+ private bool updating_killswitch = false;
private Gtk.MenuItem status_item;
private Gtk.MenuItem enable_item;
private bool enable_value = false;
@@ -27,12 +28,8 @@ public class BluetoothIndicator : AppIndicator.Indicator
{
Object (id: "indicator-bluetooth", icon_name: "bluetooth-active", category: "Hardware");
- /* Monitor killswitch status */
- rfkill = new RFKillManager ();
- rfkill.open ();
- rfkill.device_added.connect (update_rfkill);
- rfkill.device_changed.connect (update_rfkill);
- rfkill.device_deleted.connect (update_rfkill);
+ killswitch = new GnomeBluetooth.Killswitch ();
+ killswitch.state_changed.connect (killswitch_state_changed_cb);
client = new GnomeBluetooth.Client ();
@@ -47,7 +44,15 @@ public class BluetoothIndicator : AppIndicator.Indicator
menu.append (status_item);
enable_item = new Gtk.MenuItem ();
- enable_item.activate.connect (toggle_enabled);
+ enable_item.activate.connect (() =>
+ {
+ if (updating_killswitch)
+ return;
+ if (killswitch.state == GnomeBluetooth.KillswitchState.UNBLOCKED)
+ killswitch.state = GnomeBluetooth.KillswitchState.SOFT_BLOCKED;
+ else
+ killswitch.state = GnomeBluetooth.KillswitchState.UNBLOCKED;
+ });
menu.append (enable_item);
visible_item = new Gtk.CheckMenuItem.with_label (_("Visible"));
@@ -94,7 +99,7 @@ public class BluetoothIndicator : AppIndicator.Indicator
settings_item.visible = true;
menu.append (settings_item);
- update_rfkill ();
+ killswitch_state_changed_cb (killswitch.state);
}
private BluetoothMenuItem? find_menu_item (DBusProxy proxy)
@@ -179,31 +184,16 @@ public class BluetoothIndicator : AppIndicator.Indicator
menu.remove (item);
}
- private void update_rfkill ()
+ private void killswitch_state_changed_cb (GnomeBluetooth.KillswitchState state)
{
- var have_lock = false;
- var software_locked = false;
- var hardware_locked = false;
+ updating_killswitch = true;
- foreach (var device in rfkill.get_devices ())
- {
- if (device.device_type != RFKillDeviceType.BLUETOOTH)
- continue;
-
- have_lock = true;
- if (device.software_lock)
- software_locked = true;
- if (device.hardware_lock)
- hardware_locked = true;
- }
- var locked = hardware_locked || software_locked;
-
- if (hardware_locked)
+ if (state == GnomeBluetooth.KillswitchState.HARD_BLOCKED)
{
status_item.label = _("Bluetooth: Disabled");
enable_item.visible = false;
}
- else if (software_locked)
+ if (state == GnomeBluetooth.KillswitchState.SOFT_BLOCKED)
{
status_item.label = _("Bluetooth: Off");
enable_item.label = _("Turn on Bluetooth");
@@ -219,16 +209,13 @@ public class BluetoothIndicator : AppIndicator.Indicator
}
/* Disable devices when locked */
- visible_item.visible = !locked;
- devices_separator.visible = !locked;
- devices_item.visible = !locked;
+ visible_item.visible = state == GnomeBluetooth.KillswitchState.UNBLOCKED;
+ devices_separator.visible = state == GnomeBluetooth.KillswitchState.UNBLOCKED;
+ devices_item.visible = state == GnomeBluetooth.KillswitchState.UNBLOCKED;
foreach (var item in device_items)
- item.visible = !locked;
- }
+ item.visible = state == GnomeBluetooth.KillswitchState.UNBLOCKED;
- private void toggle_enabled ()
- {
- rfkill.set_software_lock (RFKillDeviceType.BLUETOOTH, enable_value);
+ updating_killswitch = false;
}
}
@@ -247,10 +234,8 @@ private class BluetoothMenuItem : Gtk.MenuItem
{
this.proxy = proxy;
submenu = new Gtk.Menu ();
- submenu.visible = true;
send_item = new Gtk.MenuItem.with_label (_("Send files..."));
- send_item.visible = true;
send_item.activate.connect (() => { GnomeBluetooth.send_to_address (address, alias); });
submenu.append (send_item);
diff --git a/src/rfkill.vala b/src/rfkill.vala
deleted file mode 100644
index ed7afbd..0000000
--- a/src/rfkill.vala
+++ /dev/null
@@ -1,168 +0,0 @@
-/*
- * Copyright (C) 2012 Canonical Ltd.
- * Author: Robert Ancell <robert.ancell@canonical.com>
- *
- * 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. See http://www.gnu.org/copyleft/gpl.html the full text of the
- * license.
- */
-
-public enum RFKillDeviceType
-{
- ALL = 0,
- WLAN,
- BLUETOOTH,
- UWB,
- WIMAX,
- WMAN
-}
-
-public class RFKillDevice
-{
- public signal void changed ();
-
- public bool software_lock
- {
- get { return _software_lock; }
- set
- {
- var event = RFKillEvent ();
- event.idx = idx;
- event.op = RFKillOperation.CHANGE;
- event.soft = value ? 1 : 0;
- if (Posix.write (manager.fd, &event, 8) != 8)
- return;
- }
- }
-
- public bool hardware_lock { get { return _hardware_lock; } }
-
- public RFKillDeviceType device_type { get { return _device_type; } }
-
- internal RFKillManager manager;
- internal uint32 idx;
- internal RFKillDeviceType _device_type;
- internal bool _software_lock;
- internal bool _hardware_lock;
-
- internal RFKillDevice (RFKillManager manager, uint32 idx, RFKillDeviceType device_type, bool software_lock, bool hardware_lock)
- {
- this.manager = manager;
- this.idx = idx;
- _device_type = device_type;
- _software_lock = software_lock;
- _hardware_lock = hardware_lock;
- }
-}
-
-public class RFKillManager : Object
-{
- public signal void device_added (RFKillDevice device);
- public signal void device_changed (RFKillDevice device);
- public signal void device_deleted (RFKillDevice device);
-
- public RFKillManager ()
- {
- _devices = new List<RFKillDevice> ();
- }
-
- public void open ()
- {
- fd = Posix.open ("/dev/rfkill", Posix.O_RDWR);
- Posix.fcntl (fd, Posix.F_SETFL, Posix.O_NONBLOCK);
-
- /* Read initial state */
- while (read_event ());
-
- /* Monitor for events */
- var channel = new IOChannel.unix_new (fd);
- channel.add_watch (IOCondition.IN | IOCondition.HUP | IOCondition.ERR, () => { return read_event (); });
- }
-
- public List<RFKillDevice> get_devices ()
- {
- var devices = new List<RFKillDevice> ();
- foreach (var device in _devices)
- devices.append (device);
- return devices;
- }
-
- public void set_software_lock (RFKillDeviceType type, bool lock_enabled)
- {
- var event = RFKillEvent ();
- event.type = type;
- event.op = RFKillOperation.CHANGE_ALL;
- event.soft = lock_enabled ? 1 : 0;
- if (Posix.write (fd, &event, 8) != 8)
- return;
- }
-
- internal int fd = -1;
- private List<RFKillDevice> _devices;
-
- private bool read_event ()
- {
- var event = RFKillEvent ();
- if (Posix.read (fd, &event, 8) != 8)
- return false;
-
- switch (event.op)
- {
- case RFKillOperation.ADD:
- var device = new RFKillDevice (this, event.idx, (RFKillDeviceType) event.type, event.soft != 0, event.hard != 0);
- _devices.append (device);
- device_added (device);
- break;
- case RFKillOperation.DELETE:
- var device = get_device (event.idx);
- if (device != null)
- {
- _devices.remove (device);
- device_deleted (device);
- }
- break;
- case RFKillOperation.CHANGE:
- var device = get_device (event.idx);
- if (device != null)
- {
- device._software_lock = event.soft != 0;
- device._hardware_lock = event.hard != 0;
- device.changed ();
- device_changed (device);
- }
- break;
- }
- stderr.printf ("idx=%u type=%d op=%d soft=%d hard=%d\n", event.idx, event.type, event.op, event.soft, event.hard);
- return true;
- }
-
- private RFKillDevice? get_device (uint32 idx)
- {
- foreach (var device in _devices)
- {
- if (device.idx == idx)
- return device;
- }
-
- return null;
- }
-}
-
-private struct RFKillEvent
-{
- uint32 idx;
- uint8 type;
- uint8 op;
- uint8 soft;
- uint8 hard;
-}
-
-private enum RFKillOperation
-{
- ADD = 0,
- DELETE,
- CHANGE,
- CHANGE_ALL
-}