Skip to content

Commit

Permalink
rdev: add extra mouse button
Browse files Browse the repository at this point in the history
  • Loading branch information
jersou committed May 22, 2022
1 parent 9262903 commit 00958b1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
30 changes: 20 additions & 10 deletions rdev/src/linux/grab.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
use crate::linux::common::Display;
use crate::linux::keyboard::Keyboard;
use crate::rdev::{Button, Event, EventType, GrabError, Key, KeyboardState};
use epoll::ControlOptions::{EPOLL_CTL_ADD, EPOLL_CTL_DEL};
use evdev_rs::{
enums::{EventCode, EV_KEY, EV_REL},
Device, InputEvent, UInputDevice,
};
use inotify::{Inotify, WatchMask};
use std::ffi::{OsStr, OsString};
use std::fs::{read_dir, File};
use std::io;
Expand All @@ -18,6 +9,17 @@ use std::os::unix::{
use std::path::Path;
use std::time::SystemTime;

use epoll::ControlOptions::{EPOLL_CTL_ADD, EPOLL_CTL_DEL};
use evdev_rs::{
enums::{EventCode, EV_KEY, EV_REL},
Device, InputEvent, UInputDevice,
};
use inotify::{Inotify, WatchMask};

use crate::linux::common::Display;
use crate::linux::keyboard::Keyboard;
use crate::rdev::{Button, Event, EventType, GrabError, Key, KeyboardState};

// TODO The x, y coordinates are currently wrong !! Is there mouse acceleration
// to take into account ??

Expand Down Expand Up @@ -74,7 +76,15 @@ macro_rules! convert_buttons {
convert_buttons!(
BTN_LEFT, Left,
BTN_RIGHT, Right,
BTN_MIDDLE, Middle
BTN_MIDDLE, Middle,
BTN_SIDE, Side,
BTN_EXTRA, Extra,
BTN_FORWARD, Forward,
BTN_BACK, Back,
BTN_TASK, Task,
BTN_TRIGGER, Trigger,
BTN_THUMB, Thumb,
BTN_THUMB2, Thumb2
);

//TODO: IntlBackslash, kpDelete
Expand Down
30 changes: 27 additions & 3 deletions rdev/src/linux/simulate.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::linux::common::{FALSE, TRUE};
use crate::linux::keycodes::code_from_key;
use crate::rdev::{Button, EventType, SimulateError};
use std::convert::TryInto;
use std::os::raw::c_int;
use std::ptr::null;

use x11::xlib;
use x11::xtest;

use crate::linux::common::{FALSE, TRUE};
use crate::linux::keycodes::code_from_key;
use crate::rdev::{Button, EventType, SimulateError};

unsafe fn send_native(event_type: &EventType, display: *mut xlib::Display) -> Option<()> {
let res = match event_type {
EventType::KeyPress(key) => {
Expand All @@ -21,6 +23,17 @@ unsafe fn send_native(event_type: &EventType, display: *mut xlib::Display) -> Op
Button::Left => xtest::XTestFakeButtonEvent(display, 1, TRUE, 0),
Button::Middle => xtest::XTestFakeButtonEvent(display, 2, TRUE, 0),
Button::Right => xtest::XTestFakeButtonEvent(display, 3, TRUE, 0),

// FIXME button mapping
Button::Side => xtest::XTestFakeButtonEvent(display, 10, TRUE, 0),
Button::Extra => xtest::XTestFakeButtonEvent(display, 11, TRUE, 0),
Button::Forward => xtest::XTestFakeButtonEvent(display, 9, TRUE, 0),
Button::Back => xtest::XTestFakeButtonEvent(display, 8, TRUE, 0),
Button::Task => xtest::XTestFakeButtonEvent(display, 12, TRUE, 0),
Button::Trigger => xtest::XTestFakeButtonEvent(display, 13, TRUE, 0),
Button::Thumb => xtest::XTestFakeButtonEvent(display, 14, TRUE, 0),
Button::Thumb2 => xtest::XTestFakeButtonEvent(display, 15, TRUE, 0),

Button::Unknown(code) => {
xtest::XTestFakeButtonEvent(display, (*code).try_into().ok()?, TRUE, 0)
}
Expand All @@ -29,6 +42,17 @@ unsafe fn send_native(event_type: &EventType, display: *mut xlib::Display) -> Op
Button::Left => xtest::XTestFakeButtonEvent(display, 1, FALSE, 0),
Button::Middle => xtest::XTestFakeButtonEvent(display, 2, FALSE, 0),
Button::Right => xtest::XTestFakeButtonEvent(display, 3, FALSE, 0),

// FIXME button mapping
Button::Side => xtest::XTestFakeButtonEvent(display, 10, FALSE, 0),
Button::Extra => xtest::XTestFakeButtonEvent(display, 11, FALSE, 0),
Button::Forward => xtest::XTestFakeButtonEvent(display, 9, FALSE, 0),
Button::Back => xtest::XTestFakeButtonEvent(display, 8, FALSE, 0),
Button::Task => xtest::XTestFakeButtonEvent(display, 12, FALSE, 0),
Button::Trigger => xtest::XTestFakeButtonEvent(display, 13, FALSE, 0),
Button::Thumb => xtest::XTestFakeButtonEvent(display, 14, FALSE, 0),
Button::Thumb2 => xtest::XTestFakeButtonEvent(display, 15, FALSE, 0),

Button::Unknown(code) => {
xtest::XTestFakeButtonEvent(display, (*code).try_into().ok()?, FALSE, 0)
}
Expand Down
14 changes: 12 additions & 2 deletions rdev/src/rdev.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};
use std::time::SystemTime;
use std::{fmt, fmt::Display};

#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};

// /// Callback type to send to listen function.
// pub type Callback = dyn FnMut(Event) -> ();

Expand Down Expand Up @@ -57,6 +58,7 @@ pub enum GrabError {
SimulateError,
IoError(std::io::Error),
}

/// Errors that occur when trying to get display size.
#[non_exhaustive]
#[derive(Debug)]
Expand Down Expand Up @@ -220,6 +222,14 @@ pub enum Button {
Left,
Right,
Middle,
Side,
Extra,
Forward,
Back,
Task,
Trigger,
Thumb,
Thumb2,
Unknown(u8),
}

Expand Down

0 comments on commit 00958b1

Please sign in to comment.