-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathrfkill.vala
More file actions
193 lines (165 loc) · 5.49 KB
/
rfkill.vala
File metadata and controls
193 lines (165 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* 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;
}
/*
* Toggle Airplane Mode
*
* Modern mobile platforms don't disable Bluetooth, we skip it too
*
* Setting airplane mode from here does all software lock, whereas setting
* from a key press does all hardware lock. We need one of these two things—
* all devices to be locked somehow—to consider it Airplane Mode
*/
public bool airplane_mode {
get {
var all_hardware_locked = true;
var all_software_locked = true;
foreach (unowned var device in get_devices ()) {
if (device.device_type == BLUETOOTH) {
continue;
}
if (!device.software_lock) {
all_software_locked = false;
}
if (!device.hardware_lock) {
all_hardware_locked = false;
}
}
return all_hardware_locked || all_software_locked;
}
set {
set_software_lock (WLAN, value);
set_software_lock (UWB, value);
set_software_lock (WIMAX, value);
set_software_lock (WMAN, value);
// Some hardware keys still turn off bluetooth
if (!value) {
set_software_lock (BLUETOOTH, false);
}
}
}
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
}