Use entry api for `DeviceAddedEvent`s seat creation/update

This commit is contained in:
Drakulix 2017-04-15 19:19:09 +02:00
parent 10857d108e
commit ab35f140a0
1 changed files with 21 additions and 19 deletions

View File

@ -6,7 +6,7 @@ use input::{Libinput, Device, Seat as LibinputSeat, DeviceCapability};
use input::event::*; use input::event::*;
use std::io::Error as IoError; use std::io::Error as IoError;
use std::collections::hash_map::{DefaultHasher, HashMap}; use std::collections::hash_map::{DefaultHasher, Entry, HashMap};
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
pub struct LibinputInputBackend { pub struct LibinputInputBackend {
@ -73,24 +73,26 @@ impl InputBackend for LibinputInputBackend {
let device_seat = added.seat(); let device_seat = added.seat();
self.devices.push(added); self.devices.push(added);
let contains = self.seats.contains_key(&device_seat); match self.seats.entry(device_seat) {
if contains { Entry::Occupied(mut seat_entry) => {
let old_seat = self.seats.get_mut(&device_seat).unwrap(); let old_seat = seat_entry.get_mut();
{ {
let caps = old_seat.capabilities_mut(); let caps = old_seat.capabilities_mut();
caps.pointer = new_caps.pointer || caps.pointer; caps.pointer = new_caps.pointer || caps.pointer;
caps.keyboard = new_caps.keyboard || caps.keyboard; caps.keyboard = new_caps.keyboard || caps.keyboard;
caps.touch = new_caps.touch || caps.touch; caps.touch = new_caps.touch || caps.touch;
} }
if let Some(ref mut handler) = self.handler { if let Some(ref mut handler) = self.handler {
handler.on_seat_changed(old_seat); handler.on_seat_changed(old_seat);
} }
} else { },
let mut hasher = DefaultHasher::default(); Entry::Vacant(seat_entry) => {
device_seat.hash(&mut hasher); let mut hasher = DefaultHasher::default();
self.seats.insert(device_seat.clone(), Seat::new(hasher.finish(), new_caps)); seat_entry.key().hash(&mut hasher);
if let Some(ref mut handler) = self.handler { let seat = seat_entry.insert(Seat::new(hasher.finish(), new_caps));
handler.on_seat_created(self.seats.get(&device_seat).unwrap()); if let Some(ref mut handler) = self.handler {
handler.on_seat_created(seat);
}
} }
} }
}, },