-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_stack.cc
More file actions
233 lines (204 loc) · 6.61 KB
/
Copy pathdevice_stack.cc
File metadata and controls
233 lines (204 loc) · 6.61 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "device_stack.h"
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
namespace {
struct ButtonSeed {
std::string control;
std::string action;
};
struct MouseSeed {
std::string id;
std::string model_id;
std::string model;
LinkType link;
std::string firmware;
int battery;
bool charging;
std::vector<ButtonSeed> buttons;
int min_dpi;
int max_dpi;
int dpi;
};
struct DeviceSeed {
Device device;
Settings settings;
};
template <class Entries>
auto FindById(Entries& entries, const std::string& device_id) {
return std::find_if(entries.begin(), entries.end(),
[&](const auto& entry) { return entry.device.id() == device_id; });
}
DeviceSeed MakeMouse(const MouseSeed& seed) {
Device device;
device.set_id(seed.id);
device.set_model_id(seed.model_id);
device.set_model(seed.model);
device.set_link(seed.link);
device.set_firmware(seed.firmware);
device.set_connected(true);
device.set_has_battery(true);
device.set_battery(seed.battery);
device.set_charging(seed.charging);
MouseSpec* spec = device.mutable_mouse();
spec->set_min_dpi(seed.min_dpi);
spec->set_max_dpi(seed.max_dpi);
Settings settings;
settings.set_device_id(seed.id);
MouseSettings* mouse = settings.mutable_mouse();
mouse->set_dpi(seed.dpi);
mouse->set_scroll_speed(3);
mouse->set_natural_scroll(false);
for (const ButtonSeed& button : seed.buttons) {
spec->add_buttons(button.control);
Binding* binding = mouse->add_bindings();
binding->set_control(button.control);
binding->set_action(button.action);
}
return {std::move(device), std::move(settings)};
}
const char* const kRemappableKeys[] = {
"esc", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "minus", "equal", "backspace",
"tab", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "bracketleft", "bracketright",
"backslash",
"capslock", "a", "s", "d", "f", "g", "h", "j", "k", "l", "semicolon", "quote", "enter",
"shiftleft", "z", "x", "c", "v", "b", "n", "m", "comma", "period", "slash", "shiftright",
"ctrlleft", "metaleft", "altleft", "space", "altright", "menu", "ctrlright",
};
DeviceSeed MakeKeyboard() {
Device device;
device.set_id("compact-keyboard");
device.set_model_id("compact-keyboard");
device.set_model("Compact Keyboard");
device.set_link(WIRED);
device.set_firmware("2.0.5");
device.set_connected(true);
device.set_has_battery(false);
KeyboardSpec* spec = device.mutable_keyboard();
spec->set_backlight(true);
Settings settings;
settings.set_device_id(device.id());
KeyboardSettings* keyboard = settings.mutable_keyboard();
keyboard->set_effect(STATIC);
keyboard->set_hue(35);
keyboard->set_brightness(70);
for (const char* key : kRemappableKeys) {
spec->add_keys(key);
Binding* binding = keyboard->add_bindings();
binding->set_control(key);
binding->set_action("default");
}
return {std::move(device), std::move(settings)};
}
} // namespace
DeviceStack::DeviceStack() {
auto add = [](std::vector<Entry>& paired, DeviceSeed seed) {
paired.push_back({std::move(seed.device), std::move(seed.settings)});
};
add(paired_, MakeMouse({
.id = "performance-mouse",
.model_id = "performance-mouse",
.model = "Performance Mouse",
.link = RECEIVER,
.firmware = "3.2.1",
.battery = 82,
.charging = true,
.buttons = {{"wheel", "middle-click"},
{"back", "back"},
{"forward", "forward"},
{"gesture", "show-desktop"}},
.min_dpi = 400,
.max_dpi = 8000,
.dpi = 1600,
}));
add(paired_, MakeMouse({
.id = "travel-mouse",
.model_id = "travel-mouse",
.model = "Travel Mouse",
.link = BLUETOOTH,
.firmware = "1.4.0",
.battery = 15,
.charging = false,
.buttons = {{"wheel", "middle-click"}},
.min_dpi = 800,
.max_dpi = 3200,
.dpi = 1200,
}));
add(paired_, MakeKeyboard());
// A second unit of the same mouse, discoverable so the pairing flow is
// usable on a fresh launch.
Entry nearby = paired_.front();
nearby.device.set_id("performance-mouse-secondary");
nearby.device.set_link(BLUETOOTH);
nearby.device.set_battery(68);
nearby.device.set_charging(false);
nearby.device.set_connected(false);
nearby.settings.set_device_id(nearby.device.id());
available_.push_back(std::move(nearby));
}
DeviceList DeviceStack::List() const {
DeviceList list;
for (const Entry& entry : paired_) {
*list.add_devices() = entry.device;
}
return list;
}
DeviceList DeviceStack::Discover() const {
DeviceList list;
for (const Entry& entry : available_) {
*list.add_devices() = entry.device;
}
return list;
}
bool DeviceStack::Pair(const std::string& device_id) {
auto entry = FindById(available_, device_id);
if (entry == available_.end()) {
return false;
}
entry->device.set_connected(true);
paired_.push_back(std::move(*entry));
available_.erase(entry);
PublishDevicesChanged();
return true;
}
bool DeviceStack::Forget(const std::string& device_id) {
auto entry = FindById(paired_, device_id);
if (entry == paired_.end() || entry->device.link() == WIRED) {
return false;
}
entry->device.set_connected(false);
available_.push_back(std::move(*entry));
paired_.erase(entry);
PublishDevicesChanged();
return true;
}
std::optional<Settings> DeviceStack::GetSettings(const std::string& device_id) const {
auto entry = FindById(paired_, device_id);
if (entry == paired_.end()) {
return std::nullopt;
}
return entry->settings;
}
bool DeviceStack::ApplySettings(const Settings& settings) {
auto entry = FindById(paired_, settings.device_id());
if (entry != paired_.end()) {
if (entry->settings.kind_case() != settings.kind_case()) {
return false;
}
entry->settings = settings;
return true;
}
// Keep an unpaired device's customizations so adding it again does not reset it.
auto available = FindById(available_, settings.device_id());
if (available == available_.end() || available->settings.kind_case() != settings.kind_case()) {
return false;
}
available->settings = settings;
return true;
}
void DeviceStack::PublishDevicesChanged() const {
if (devices_changed_) {
devices_changed_(List());
}
}