Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rfkill bus #129

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ shared_module(
'src/Services/Adapter.vala',
'src/Services/Device.vala',
'src/Services/Manager.vala',
'src/Services/rfkill.vala',
'src/Services/RfkillBus.vala',
'src/Widgets/Device.vala',
'src/Widgets/DisplayWidget.vala',
'src/Widgets/PopoverWidget.vala',
Expand All @@ -39,6 +41,7 @@ shared_module(
dependency('granite'),
dependency('gtk+-3.0'),
dependency('libnotify'),
meson.get_compiler('vala').find_library('posix'),
wingpanel_dep
],
install: true,
Expand Down
9 changes: 7 additions & 2 deletions src/Services/Manager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ public class BluetoothIndicator.Services.ObjectManager : Object {
public bool retrieve_finished { get; private set; default = false; }
private Settings settings;
private GLib.DBusObjectManagerClient object_manager;

private BluetoothIndicator.Services.Rfkill rfkillbus;
public bool is_powered {get; private set; default = false; }
public bool is_connected {get; private set; default = false; }

construct {
settings = new Settings ("io.elementary.desktop.wingpanel.bluetooth");
rfkillbus = new BluetoothIndicator.Services.Rfkill ();
create_manager.begin ();
}

Expand Down Expand Up @@ -198,7 +199,11 @@ public class BluetoothIndicator.Services.ObjectManager : Object {
public async void set_global_state (bool state) {
/* `is_powered` and `connected` properties will be set by the check_global state () callback when adapter or device
* properties change. Do not set now so that global_state_changed signal will be emitted. */

try {
rfkillbus.bluetooth_airplane_mode (state);
} catch (Error e) {
error (e.message);
}
var adapters = get_adapters ();
foreach (var adapter in adapters) {
adapter.powered = state;
Expand Down
60 changes: 60 additions & 0 deletions src/Services/RfkillBus.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*-
* Copyright (c) {2020} torikulhabib (https://github.com/torikulhabib)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published by
* the Free Software Foundation, either version 2.1 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

[DBus (name = "io.elementary.bluetooth.rfkill")]
public class BluetoothIndicator.Services.Rfkill : GLib.Object {
public RFKillManager rfkill;
public bool software_locked {get; private set; default = false; }
public bool hardware_locked {get; private set; default = false; }

public Rfkill () {
rfkill = new RFKillManager ();
rfkill.open ();
rfkill.device_added.connect (()=>{ try { load_rfkill (); } catch (Error e) { error (e.message); } });
rfkill.device_deleted.connect (()=>{ try { load_rfkill (); } catch (Error e) { error (e.message); } });
rfkill.device_changed.connect (()=>{ try { load_rfkill (); } catch (Error e) { error (e.message); } });
Bus.own_name (
BusType.SESSION,
"io.elementary.bluetooth.rfkill",
GLib.BusNameOwnerFlags.NONE,
(conn)=>{
try {
conn.register_object ("/io/elementary/bluetooth/rfkill", this);
} catch (Error e) {
error (e.message);
}
}
);
}

public void load_rfkill () throws GLib.Error {
foreach (var device in rfkill.get_devices ()) {
if (device.device_type != RFKillDeviceType.BLUETOOTH) {
continue;
}
software_locked = device.software_lock;
hardware_locked = device.hardware_lock;
}
}

public void bluetooth_airplane_mode (bool state) throws GLib.Error {
load_rfkill ();
if (software_locked == state) {
rfkill.set_software_lock (RFKillDeviceType.BLUETOOTH, !state);
}
}
}
149 changes: 149 additions & 0 deletions src/Services/rfkill.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright (C) 2012 Canonical Ltd.
* Author: Robert Ancell <[email protected]>
*
* 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;
}
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
}