Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
shiro committed Dec 16, 2023
1 parent d59512d commit f8bb816
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
12 changes: 8 additions & 4 deletions examples/wasd_mouse_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import time
import threading

map2.default(layout = "us")
map2.default(layout="us")

# an easy to use interval utility that allows us to run a function on a timer


class setInterval:
def __init__(self, interval, action):
self.interval = interval / 1000
self.action = action
self.stopEvent = threading.Event()
thread = threading.Thread(target = self.__setInterval)
thread = threading.Thread(target=self.__setInterval)
thread.start()

def __setInterval(self):
Expand All @@ -26,15 +28,16 @@ def __setInterval(self):
def cancel(self):
self.stopEvent.set()


# read from keyboard
reader_kbd = map2.Reader(patterns=["/dev/input/by-id/example-keyboard"])

# to move the mouse programmatically, we need a mouse reader we can write into
reader_mouse = map2.Reader()

# add new virtual output devices
writer_kbd = map2.Writer(clone_from = "/dev/input/by-id/example-keyboard")
writer_mouse = map2.Writer(capabilities = {"rel": True, "buttons": True})
writer_kbd = map2.Writer(clone_from="/dev/input/by-id/example-keyboard")
writer_mouse = map2.Writer(capabilities={"rel": True, "buttons": True})

# add mapper
mapper_kbd = map2.Mapper()
Expand All @@ -47,6 +50,7 @@ def cancel(self):
# we keep a map of intervals that maps each key to the associated interval
intervals = {}


def mouse_ctrl(key, state, axis, multiplier):
def inner_fn():
# if the key was released, remove and cancel the corresponding interval
Expand Down
56 changes: 31 additions & 25 deletions src/xkb_transformer_registry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashMap;
use crate::xkb::XKBTransformer;
use crate::*;

use std::collections::HashMap;

#[derive(Clone, Eq, PartialEq, Hash)]
pub struct TransformerParams {
Expand All @@ -23,49 +22,55 @@ impl TransformerParams {
let layout = layout.unwrap_or(default.layout.clone());
let variant = variant.or(default.variant.clone());
let options = options.or(default.options.clone());
Self { model, layout, variant, options }
Self {
model,
layout,
variant,
options,
}
}
}

impl Default for TransformerParams {
fn default() -> Self {
Self { model: "pc105".to_string(), layout: "us".to_string(), variant: None, options: None }
Self {
model: "pc105".to_string(),
layout: "us".to_string(),
variant: None,
options: None,
}
}
}


pub struct XKBTransformerRegistry {
registry: Mutex<HashMap<TransformerParams, Weak<XKBTransformer>>>,
}

impl XKBTransformerRegistry {
pub fn new() -> Self {
Self { registry: Mutex::new(HashMap::new()) }
Self {
registry: Mutex::new(HashMap::new()),
}
}

pub fn get(
&self,
params: &TransformerParams,
) -> Result<Arc<XKBTransformer>> {
pub fn get(&self, params: &TransformerParams) -> Result<Arc<XKBTransformer>> {
let mut registry = self.registry.lock().unwrap();
let res = registry.get(&params);

match res {
Some(f) => {
match f.upgrade() {
Some(transformer) => Ok(transformer),
None => {
let transformer = Arc::new(XKBTransformer::new(
&params.model,
&params.layout,
params.variant.as_deref(),
params.options.clone(),
)?);
registry.insert(params.clone(), Arc::downgrade(&transformer));
Ok(transformer)
}
Some(f) => match f.upgrade() {
Some(transformer) => Ok(transformer),
None => {
let transformer = Arc::new(XKBTransformer::new(
&params.model,
&params.layout,
params.variant.as_deref(),
params.options.clone(),
)?);
registry.insert(params.clone(), Arc::downgrade(&transformer));
Ok(transformer)
}
}
},
None => {
let transformer = Arc::new(XKBTransformer::new(
&params.model,
Expand All @@ -82,4 +87,5 @@ impl XKBTransformerRegistry {

lazy_static! {
pub static ref XKB_TRANSFORMER_REGISTRY: XKBTransformerRegistry = XKBTransformerRegistry::new();
}
}

0 comments on commit f8bb816

Please sign in to comment.