Skip to content

Commit c798dbd

Browse files
authored
AirplaneMode: use RFKill (#340)
1 parent 5ae27af commit c798dbd

3 files changed

Lines changed: 54 additions & 15 deletions

File tree

src/Indicator.vala

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class Network.Indicator : Wingpanel.Indicator {
2222

2323
NetworkMonitor network_monitor;
2424

25+
private RFKillManager rfkill;
2526
private Gtk.GestureMultiPress gesture_click;
2627
private SimpleAction airplane_action;
2728

@@ -65,19 +66,16 @@ public class Network.Indicator : Wingpanel.Indicator {
6566
});
6667
}
6768

68-
airplane_action = new SimpleAction.stateful ("airplane-mode", null, new Variant.boolean (popover_widget.nm_client.networking_get_enabled ()));
69+
rfkill = new RFKillManager ();
70+
rfkill.open ();
71+
72+
airplane_action = new SimpleAction.stateful ("airplane-mode", null, new Variant.boolean (rfkill.airplane_mode));
6973
airplane_action.activate.connect (() => {
70-
popover_widget.nm_client.dbus_call.begin (
71-
NM.DBUS_PATH, NM.DBUS_INTERFACE,
72-
"Enable", new Variant.tuple ({new Variant.boolean (!popover_widget.nm_client.networking_get_enabled ())}),
73-
null, -1, null, (obj, res) => {
74-
try {
75-
((NM.Client) obj).dbus_set_property.end (res);
76-
} catch (Error e) {
77-
warning ("Error setting airplane mode: %s", e.message);
78-
}
79-
}
80-
);
74+
rfkill.airplane_mode = !rfkill.airplane_mode;
75+
});
76+
77+
rfkill.device_changed.connect (() => {
78+
airplane_action.set_state (new Variant.boolean (rfkill.airplane_mode));
8179
});
8280

8381
var action_group = (SimpleActionGroup) popover_widget.get_action_group ("network");
@@ -102,8 +100,6 @@ public class Network.Indicator : Wingpanel.Indicator {
102100

103101
display_widget.update_state (popover_widget.state, popover_widget.secure, popover_widget.extra_info);
104102

105-
airplane_action.set_state (new Variant.boolean (!popover_widget.nm_client.networking_get_enabled ()));
106-
107103
update_tooltip ();
108104
}
109105

src/Widgets/PopoverWidget.vala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public class Network.Widgets.PopoverWidget : Gtk.Box {
6565

6666
var action_group = new SimpleActionGroup ();
6767
action_group.action_state_changed.connect ((action_name, state) => {
68-
critical ("got a state change");
6968
if (action_name == "airplane-mode") {
7069
if (state.get_boolean ()) {
7170
airplane_toggle.icon_name = "airplane-mode-symbolic";

src/rfkill.vala

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,50 @@ public class RFKillManager : Object {
8080
return devices;
8181
}
8282

83+
/*
84+
* Toggle Airplane Mode
85+
*
86+
* Modern mobile platforms don't disable Bluetooth, we skip it too
87+
*
88+
* Setting airplane mode from here does all software lock, whereas setting
89+
* from a key press does all hardware lock. We need one of these two things—
90+
* all devices to be locked somehow—to consider it Airplane Mode
91+
*/
92+
public bool airplane_mode {
93+
get {
94+
var all_hardware_locked = true;
95+
var all_software_locked = true;
96+
97+
foreach (unowned var device in get_devices ()) {
98+
if (device.device_type == BLUETOOTH) {
99+
continue;
100+
}
101+
102+
if (!device.software_lock) {
103+
all_software_locked = false;
104+
}
105+
106+
if (!device.hardware_lock) {
107+
all_hardware_locked = false;
108+
}
109+
}
110+
111+
return all_hardware_locked || all_software_locked;
112+
}
113+
114+
set {
115+
set_software_lock (WLAN, value);
116+
set_software_lock (UWB, value);
117+
set_software_lock (WIMAX, value);
118+
set_software_lock (WMAN, value);
119+
120+
// Some hardware keys still turn off bluetooth
121+
if (!value) {
122+
set_software_lock (BLUETOOTH, false);
123+
}
124+
}
125+
}
126+
83127
public void set_software_lock (RFKillDeviceType type, bool lock_enabled) {
84128
var event = RFKillEvent ();
85129
event.type = type;

0 commit comments

Comments
 (0)