Skip to content

Commit 1e83ce4

Browse files
Support for multiple (2) function layers
1 parent 94badb3 commit 1e83ce4

1 file changed

Lines changed: 74 additions & 38 deletions

File tree

src/main.rs

Lines changed: 74 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use input::{
1717
Libinput, LibinputInterface, Device as InputDevice,
1818
event::{
1919
Event, device::DeviceEvent, EventTrait,
20-
touch::{TouchEvent, TouchEventPosition, TouchEventSlot}
20+
touch::{TouchEvent, TouchEventPosition, TouchEventSlot},
21+
keyboard::{KeyboardEvent, KeyboardEventTrait, KeyState}
2122
}
2223
};
2324
use libc::{O_RDONLY, O_RDWR, O_WRONLY};
@@ -151,25 +152,49 @@ fn emit<F>(uinput: &mut UInputHandle<F>, ty: EventKind, code: u16, value: i32) w
151152
}]).unwrap();
152153
}
153154

155+
fn toggle_key<F>(uinput: &mut UInputHandle<F>, code: Key, value: i32) where F: AsRawFd {
156+
emit(uinput, EventKind::Key, code as u16, value);
157+
emit(uinput, EventKind::Synchronize, SynchronizeKind::Report as u16, 0);
158+
}
159+
154160
fn main() {
155161
let mut surface = ImageSurface::create(Format::ARgb32, DFR_HEIGHT, DFR_WIDTH).unwrap();
156-
let layer = FunctionLayer {
157-
buttons: vec![
158-
Button { text: "F1".to_string(), action: Key::F1 },
159-
Button { text: "F2".to_string(), action: Key::F2 },
160-
Button { text: "F3".to_string(), action: Key::F3 },
161-
Button { text: "F4".to_string(), action: Key::F4 },
162-
Button { text: "F5".to_string(), action: Key::F5 },
163-
Button { text: "F6".to_string(), action: Key::F6 },
164-
Button { text: "F7".to_string(), action: Key::F7 },
165-
Button { text: "F8".to_string(), action: Key::F8 },
166-
Button { text: "F9".to_string(), action: Key::F9 },
167-
Button { text: "F10".to_string(), action: Key::F10 },
168-
Button { text: "F11".to_string(), action: Key::F11 },
169-
Button { text: "F12".to_string(), action: Key::F12 }
170-
]
171-
};
172-
let mut button_state = vec![false; 12];
162+
let mut active_layer = 0;
163+
let layers = [
164+
FunctionLayer {
165+
buttons: vec![
166+
Button { text: "F1".to_string(), action: Key::F1 },
167+
Button { text: "F2".to_string(), action: Key::F2 },
168+
Button { text: "F3".to_string(), action: Key::F3 },
169+
Button { text: "F4".to_string(), action: Key::F4 },
170+
Button { text: "F5".to_string(), action: Key::F5 },
171+
Button { text: "F6".to_string(), action: Key::F6 },
172+
Button { text: "F7".to_string(), action: Key::F7 },
173+
Button { text: "F8".to_string(), action: Key::F8 },
174+
Button { text: "F9".to_string(), action: Key::F9 },
175+
Button { text: "F10".to_string(), action: Key::F10 },
176+
Button { text: "F11".to_string(), action: Key::F11 },
177+
Button { text: "F12".to_string(), action: Key::F12 }
178+
]
179+
},
180+
FunctionLayer {
181+
buttons: vec![
182+
Button { text: "B-Dn".to_string(), action: Key::BrightnessDown },
183+
Button { text: "B-Up".to_string(), action: Key::BrightnessUp },
184+
Button { text: "Mic0".to_string(), action: Key::MicMute },
185+
Button { text: "Srch".to_string(), action: Key::Search },
186+
Button { text: "BlDn".to_string(), action: Key::IllumDown },
187+
Button { text: "BlUp".to_string(), action: Key::IllumUp },
188+
Button { text: "Revd".to_string(), action: Key::PreviousSong },
189+
Button { text: "Paus".to_string(), action: Key::PlayPause },
190+
Button { text: "Fwrd".to_string(), action: Key::NextSong },
191+
Button { text: "Mute".to_string(), action: Key::Mute },
192+
Button { text: "VolD".to_string(), action: Key::VolumeDown },
193+
Button { text: "VolU".to_string(), action: Key::VolumeUp }
194+
]
195+
}
196+
];
197+
let mut button_states = [vec![false; 12], vec![false; 12]];
173198
let mut needs_redraw = true;
174199
let mut drm = DrmBackend::open_card().unwrap();
175200
let mut input_tb = Libinput::new_with_udev(Interface);
@@ -180,8 +205,10 @@ fn main() {
180205
let pollfd_main = PollFd::new(input_main.as_raw_fd(), PollFlags::POLLIN);
181206
let mut uinput = UInputHandle::new(OpenOptions::new().write(true).open("/dev/uinput").unwrap());
182207
uinput.set_evbit(EventKind::Key).unwrap();
183-
for button in &layer.buttons {
184-
uinput.set_keybit(button.action).unwrap();
208+
for layer in &layers {
209+
for button in &layer.buttons {
210+
uinput.set_keybit(button.action).unwrap();
211+
}
185212
}
186213
uinput.dev_setup(&uinput_setup {
187214
id: input_id {
@@ -217,7 +244,7 @@ fn main() {
217244
loop {
218245
if needs_redraw {
219246
needs_redraw = false;
220-
layer.draw(&surface, &button_state);
247+
layers[active_layer].draw(&surface, &button_states[active_layer]);
221248
let data = surface.data().unwrap();
222249
drm.map().unwrap().as_mut()[..data.len()].copy_from_slice(&data);
223250
drm.dirty(&[ClipRect{x1: 0, y1: 0, x2: DFR_HEIGHT as u16, y2: DFR_WIDTH as u16}]).unwrap();
@@ -234,6 +261,18 @@ fn main() {
234261
digitizer = Some(dev);
235262
}
236263
},
264+
Event::Keyboard(KeyboardEvent::Key(key)) => {
265+
if key.key() == Key::Fn as u32 {
266+
let new_layer = match key.key_state() {
267+
KeyState::Pressed => 1,
268+
KeyState::Released => 0
269+
};
270+
if active_layer != new_layer {
271+
active_layer = new_layer;
272+
needs_redraw = true;
273+
}
274+
}
275+
},
237276
Event::Touch(te) => {
238277
if Some(te.device()) != digitizer {
239278
continue
@@ -242,13 +281,12 @@ fn main() {
242281
TouchEvent::Down(dn) => {
243282
let x = dn.x_transformed(DFR_WIDTH as u32);
244283
let y = dn.y_transformed(DFR_HEIGHT as u32);
245-
let btn = (x / (DFR_WIDTH as f64 / layer.buttons.len() as f64)) as u32;
246-
if button_hit(layer.buttons.len() as u32, btn, x, y) {
247-
touches.insert(dn.seat_slot(), btn);
248-
button_state[btn as usize] = true;
284+
let btn = (x / (DFR_WIDTH as f64 / layers[active_layer].buttons.len() as f64)) as u32;
285+
if button_hit(layers[active_layer].buttons.len() as u32, btn, x, y) {
286+
touches.insert(dn.seat_slot(), (active_layer, btn));
287+
button_states[active_layer][btn as usize] = true;
249288
needs_redraw = true;
250-
emit(&mut uinput, EventKind::Key, layer.buttons[btn as usize].action as u16, 1);
251-
emit(&mut uinput, EventKind::Synchronize, SynchronizeKind::Report as u16, 0);
289+
toggle_key(&mut uinput, layers[active_layer].buttons[btn as usize].action, 1);
252290
}
253291
},
254292
TouchEvent::Motion(mtn) => {
@@ -258,25 +296,23 @@ fn main() {
258296

259297
let x = mtn.x_transformed(DFR_WIDTH as u32);
260298
let y = mtn.y_transformed(DFR_HEIGHT as u32);
261-
let btn = *touches.get(&mtn.seat_slot()).unwrap();
262-
let hit = button_hit(layer.buttons.len() as u32, btn, x, y);
263-
if button_state[btn as usize] != hit {
264-
button_state[btn as usize] = hit;
299+
let (layer, btn) = *touches.get(&mtn.seat_slot()).unwrap();
300+
let hit = button_hit(layers[layer].buttons.len() as u32, btn, x, y);
301+
if button_states[layer][btn as usize] != hit {
302+
button_states[layer][btn as usize] = hit;
265303
needs_redraw = true;
266-
emit(&mut uinput, EventKind::Key, layer.buttons[btn as usize].action as u16, hit as i32);
267-
emit(&mut uinput, EventKind::Synchronize, SynchronizeKind::Report as u16, 0);
304+
toggle_key(&mut uinput, layers[active_layer].buttons[btn as usize].action, hit as i32);
268305
}
269306
},
270307
TouchEvent::Up(up) => {
271308
if !touches.contains_key(&up.seat_slot()) {
272309
continue;
273310
}
274-
let btn = *touches.get(&up.seat_slot()).unwrap() as usize;
275-
if button_state[btn] {
276-
button_state[btn] = false;
311+
let (layer, btn) = *touches.get(&up.seat_slot()).unwrap();
312+
if button_states[layer][btn as usize] {
313+
button_states[layer][btn as usize] = false;
277314
needs_redraw = true;
278-
emit(&mut uinput, EventKind::Key, layer.buttons[btn].action as u16, 0);
279-
emit(&mut uinput, EventKind::Synchronize, SynchronizeKind::Report as u16, 0);
315+
toggle_key(&mut uinput, layers[active_layer].buttons[btn as usize].action, 0);
280316
}
281317
}
282318
_ => {}

0 commit comments

Comments
 (0)