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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//! Combined device monitoring module.
//!
//! Monitors USB and Bluetooth keyboards together through a single IOKit
//! event-driven monitor, managing multiple keyboard-profile mappings with
//! priority support.
use crate::error::{AppError, Result};
#[cfg(target_os = "macos")]
use crate::karabiner::KarabinerController;
#[cfg(target_os = "macos")]
use crate::monitor::{DeviceEvent, DeviceMonitor, iokit::IoKitMonitor};
use crate::monitor::{DeviceIdentifier, KeyboardMapping};
#[cfg(target_os = "macos")]
use std::sync::{Arc, Mutex};
#[derive(Debug, Clone)]
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
struct ConnectedDevice {
identifier: DeviceIdentifier,
mapping: KeyboardMapping,
}
pub struct CombinedMonitor {
mappings: Vec<KeyboardMapping>,
default_profile: String,
verbose: bool,
}
impl CombinedMonitor {
pub fn new(mappings: Vec<KeyboardMapping>, default_profile: impl Into<String>) -> Self {
CombinedMonitor {
mappings,
default_profile: default_profile.into(),
verbose: false,
}
}
pub fn with_verbose(mut self, verbose: bool) -> Self {
self.verbose = verbose;
self
}
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
fn get_highest_priority_profile(
connected: &[ConnectedDevice],
default_profile: &str,
) -> String {
connected
.iter()
.max_by_key(|d| d.mapping.priority)
.map(|d| d.mapping.profile.clone())
.unwrap_or_else(|| default_profile.to_string())
}
/// Find the highest-priority configured mapping that matches `device`, or
/// `None` if no mapping matches.
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
fn best_matching_mapping<'a>(
mappings: &'a [KeyboardMapping],
device: &DeviceIdentifier,
) -> Option<&'a KeyboardMapping> {
mappings
.iter()
.filter(|m| m.device.matches(device))
.max_by_key(|m| m.priority)
}
/// Apply `target` via Karabiner only if it differs from the last applied
/// profile. The cache lock is kept while switching so that concurrent events
/// (should they ever overlap) serialize and observe a consistent cache.
#[cfg(target_os = "macos")]
fn apply_target_profile(
target: &str,
last_applied: &Mutex<Option<String>>,
karabiner: &KarabinerController,
verbose: bool,
) -> Result<()> {
let mut applied = last_applied.lock().expect("last_applied mutex poisoned");
if applied.as_deref() == Some(target) {
if verbose {
println!("[DEBUG] Profile already '{}', skipping switch", target);
}
return Ok(());
}
// `switch_profile` itself prints "Switched to profile: ..." on success,
// so we intentionally do not print a separate "Switching" line here.
karabiner.switch_profile(target)?;
*applied = Some(target.to_string());
Ok(())
}
pub fn start_monitoring(mut self) -> Result<()> {
let mut usb_product_ids: Vec<u16> = Vec::new();
let mut bluetooth_names: Vec<String> = Vec::new();
for mapping in &self.mappings {
match &mapping.device {
DeviceIdentifier::Usb { product_id } => usb_product_ids.push(*product_id),
DeviceIdentifier::Bluetooth { device_name } => {
bluetooth_names.push(device_name.clone())
}
}
}
if usb_product_ids.is_empty() && bluetooth_names.is_empty() {
return Err(AppError::Config(
"No devices configured for monitoring".to_string(),
));
}
println!("Monitoring started. Press Ctrl+C to stop.");
println!("Default profile: {}", self.default_profile);
println!("Configured mappings (sorted by priority):");
// Sort once (descending) for display; the handler below reuses the same
// ordering. `max_by_key` is stable, so the chosen mapping is unaffected.
self.mappings.sort_by(|a, b| b.priority.cmp(&a.priority));
for mapping in &self.mappings {
println!(
" - {} [priority: {}] -> Profile: {}",
mapping.device.display_name(),
mapping.priority,
mapping.profile
);
}
if self.verbose {
println!("[DEBUG] Verbose logging enabled");
}
#[cfg(target_os = "macos")]
{
let karabiner = KarabinerController::new();
let mappings = Arc::new(self.mappings);
let default_profile = Arc::new(self.default_profile);
let connected_devices: Arc<Mutex<Vec<ConnectedDevice>>> =
Arc::new(Mutex::new(Vec::new()));
let last_applied: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None));
let verbose = self.verbose;
let handler = move |event: DeviceEvent| -> Result<()> {
let target_profile: Option<String> = {
let mut connected = connected_devices
.lock()
.expect("connected_devices mutex poisoned");
match event {
DeviceEvent::Initial(devices) => {
for device in &devices {
// Skip devices already tracked: one physical
// keyboard can expose several HID nodes that each
// appear in the initial set.
if connected
.iter()
.any(|d| d.identifier.is_same_device(device))
{
continue;
}
// Bind to the highest-priority matching mapping,
// not merely the first one in config order.
if let Some(mapping) =
Self::best_matching_mapping(&mappings, device)
{
if verbose {
println!(
"[DEBUG] Initial device matched: {} -> {}",
device.display_name(),
mapping.name
);
}
connected.push(ConnectedDevice {
identifier: device.clone(),
mapping: mapping.clone(),
});
}
}
// Always reconcile at startup: when no monitored
// keyboard is connected this resolves to the default
// profile, correcting any stale profile left active.
let profile =
Self::get_highest_priority_profile(&connected, &default_profile);
println!("Initial state resolved, target profile: {}", profile);
Some(profile)
}
DeviceEvent::Connected(device) => {
let Some(mapping) = Self::best_matching_mapping(&mappings, &device)
else {
if verbose {
println!(
"[DEBUG] Device connected but no matching mapping: {}",
device.display_name()
);
}
return Ok(());
};
if verbose {
println!(
"[DEBUG] Device matched: {} -> {} (priority: {})",
device.display_name(),
mapping.name,
mapping.priority
);
}
// Avoid duplicate entries when one physical keyboard
// exposes multiple HID nodes (each fires Connected).
if !connected
.iter()
.any(|d| d.identifier.is_same_device(&device))
{
connected.push(ConnectedDevice {
identifier: device.clone(),
mapping: mapping.clone(),
});
}
let profile =
Self::get_highest_priority_profile(&connected, &default_profile);
println!(
"Device connected: {}, target profile: {}",
device.display_name(),
profile
);
Some(profile)
}
DeviceEvent::Disconnected(device) => {
let before_len = connected.len();
// Use exact identity (not partial-name `matches`) so a
// disconnect only removes the device that actually
// disconnected, never a still-connected keyboard whose
// name is a substring of it (e.g. "Keychron K2" vs
// "Keychron K2 Pro").
connected.retain(|d| !d.identifier.is_same_device(&device));
if before_len == connected.len() {
if verbose {
println!(
"[DEBUG] Device disconnected but was not tracked: {}",
device.display_name()
);
}
return Ok(());
}
let profile =
Self::get_highest_priority_profile(&connected, &default_profile);
println!(
"Device disconnected: {}, target profile: {}",
device.display_name(),
profile
);
Some(profile)
}
}
};
if let Some(profile) = target_profile {
Self::apply_target_profile(&profile, &last_applied, &karabiner, verbose)?;
}
Ok(())
};
let monitor = IoKitMonitor::new(usb_product_ids, bluetooth_names);
monitor.start_monitoring(handler)
}
#[cfg(not(target_os = "macos"))]
{
let _ = (usb_product_ids, bluetooth_names);
Err(AppError::Monitor(
"Device monitoring requires macOS (Karabiner-Elements is macOS-only)".to_string(),
))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_highest_priority_profile() {
let connected = vec![
ConnectedDevice {
identifier: DeviceIdentifier::usb(1234),
mapping: KeyboardMapping::with_priority(
"Low priority",
DeviceIdentifier::usb(1234),
"Profile A",
1,
),
},
ConnectedDevice {
identifier: DeviceIdentifier::usb(5678),
mapping: KeyboardMapping::with_priority(
"High priority",
DeviceIdentifier::usb(5678),
"Profile B",
10,
),
},
];
let profile = CombinedMonitor::get_highest_priority_profile(&connected, "Default");
assert_eq!(profile, "Profile B");
}
#[test]
fn test_get_highest_priority_profile_empty() {
let connected: Vec<ConnectedDevice> = vec![];
let profile = CombinedMonitor::get_highest_priority_profile(&connected, "Default");
assert_eq!(profile, "Default");
}
#[test]
fn test_device_identifier_matches_usb() {
let device1 = DeviceIdentifier::usb(1234);
let device2 = DeviceIdentifier::usb(1234);
let device3 = DeviceIdentifier::usb(5678);
assert!(device1.matches(&device2));
assert!(!device1.matches(&device3));
}
#[test]
fn test_device_identifier_matches_bluetooth_partial() {
let config_name = DeviceIdentifier::bluetooth("HHKB");
let actual_name = DeviceIdentifier::bluetooth("HHKB-BT");
// Partial match should work in both directions
assert!(config_name.matches(&actual_name));
assert!(actual_name.matches(&config_name));
}
#[test]
fn test_device_identifier_matches_bluetooth_case_insensitive() {
let config_name = DeviceIdentifier::bluetooth("Magic Keyboard");
let actual_name = DeviceIdentifier::bluetooth("magic keyboard");
assert!(config_name.matches(&actual_name));
}
#[test]
fn test_device_identifier_empty_bluetooth_name_matches_nothing() {
let empty = DeviceIdentifier::bluetooth("");
let any_device = DeviceIdentifier::bluetooth("Magic Keyboard");
// An empty configured name must NOT match every device.
assert!(!empty.matches(&any_device));
assert!(!any_device.matches(&empty));
}
#[test]
fn test_is_same_device_requires_exact_identity() {
// Partial-name `matches` is true both ways for overlapping names...
let k2 = DeviceIdentifier::bluetooth("Keychron K2");
let k2_pro = DeviceIdentifier::bluetooth("Keychron K2 Pro");
assert!(k2.matches(&k2_pro));
// ...but they are NOT the same physical device.
assert!(!k2.is_same_device(&k2_pro));
// Same name (case-insensitive) is the same device.
assert!(k2.is_same_device(&DeviceIdentifier::bluetooth("keychron k2")));
// USB exact match.
assert!(DeviceIdentifier::usb(1234).is_same_device(&DeviceIdentifier::usb(1234)));
assert!(!DeviceIdentifier::usb(1234).is_same_device(&DeviceIdentifier::usb(5678)));
}
#[test]
fn test_disconnect_does_not_evict_overlapping_named_device() {
// Two distinct BT keyboards with overlapping names are both tracked.
let mut connected = vec![
ConnectedDevice {
identifier: DeviceIdentifier::bluetooth("Keychron K2"),
mapping: KeyboardMapping::new(
"K2",
DeviceIdentifier::bluetooth("Keychron K2"),
"Profile K2",
),
},
ConnectedDevice {
identifier: DeviceIdentifier::bluetooth("Keychron K2 Pro"),
mapping: KeyboardMapping::new(
"K2 Pro",
DeviceIdentifier::bluetooth("Keychron K2 Pro"),
"Profile K2 Pro",
),
},
];
// Disconnecting "Keychron K2" must leave "Keychron K2 Pro" tracked.
let disconnected = DeviceIdentifier::bluetooth("Keychron K2");
connected.retain(|d| !d.identifier.is_same_device(&disconnected));
assert_eq!(connected.len(), 1);
assert_eq!(
connected[0].identifier,
DeviceIdentifier::bluetooth("Keychron K2 Pro")
);
}
}