Skip to content

Commit

Permalink
usb device definition
Browse files Browse the repository at this point in the history
  • Loading branch information
positiveway committed May 18, 2024
1 parent c0bd33d commit e8a76bc
Showing 1 changed file with 64 additions and 18 deletions.
82 changes: 64 additions & 18 deletions src/virtual_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,33 @@ fn convert_event_for_writing(kind: u16, code: u16, value: i32) -> Vec<u8> {
}
}

pub enum DeviceDefinitionType{
Separate,
MouseOnly,
KeyboardOnly,
None,
}

const UINPUT_NOT_LOADED_ERR: &str = "'uinput' module probably is not loaded. try: 'sudo modprobe uinput'";

impl VirtualDevice {
pub fn default() -> Result<Self> {
VirtualDevice::new(
Duration::from_millis(1),
50,
DeviceDefinitionType::None,
)
}
pub fn new(writing_interval: Duration, channel_size: usize) -> Result<Self> {
// pub fn new(writing_interval: Duration, channel_size: usize, separate_devices: bool) -> Result<(Self, Self)> {
// Ok((
// Self::_new(writing_interval, channel_size, true)?,
// Self::_new(writing_interval, channel_size, false)?,
// ))
// }

fn new(writing_interval: Duration, channel_size: usize, definition_type: DeviceDefinitionType) -> Result<Self> {
let (s, r) = bounded(channel_size);

let path = Path::new("/dev/uinput");

#[cfg(feature = "auto-acquire-permissions")]
Expand All @@ -85,34 +102,58 @@ impl VirtualDevice {
.custom_flags(libc::O_NONBLOCK)
.open(path)?;

let mut def: uinput_user_dev = unsafe { mem::zeroed() };
// Mouse:
// Bus=0003 Vendor=045e Product=07a5 Version=0111
// Keyboard:
// Bus=0011 Vendor=0001 Product=0001 Version=ab83

let usb_device = input_id {
bustype: 0x03,
vendor: 0x4711,
product: 0x0816,
version: 1,
};
def.id = usb_device;
let mut definition: uinput_user_dev = unsafe { mem::zeroed() };
let mut device_name: String;

let (s, r) = bounded(channel_size);
match definition_type {
DeviceDefinitionType::Separate => {
return Err(Box::from("Not implemented"));
}
DeviceDefinitionType::MouseOnly => {
definition.id = input_id {
bustype: 0x0003,
vendor: 0x045e,
product: 0x07a5,
version: 0x0111,
};
device_name = String::from("virtual-mouse");
}
DeviceDefinitionType::KeyboardOnly => {
definition.id = input_id {
bustype: 0x0011,
vendor: 0x0001,
product: 0x0001,
version: 0xab83,
};
device_name = String::from("virtual-keyboard");
}
DeviceDefinitionType::None => {
device_name = String::from("virtual-device");
}
}

let mut virtual_device = VirtualDevice {
writing_interval,
file,
def,
def: definition,
sender: s,
receiver: r,
};

let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
let device_name = format!("virtualdevice-{}", now.as_millis());
// let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
// let device_name = format!("virtualdevice-{}", now.as_millis());
// println!("{}", device_name);

// let device_name = String::from("virtualdevice");

virtual_device.set_name(device_name.as_str())?;
virtual_device.register_all()?;

virtual_device.register_mouse()?;
virtual_device.register_keyboard()?;

virtual_device.create()?;

Ok(virtual_device)
Expand Down Expand Up @@ -148,17 +189,22 @@ impl VirtualDevice {
Ok(())
}

fn register_all(&self) -> EmptyResult {
for code in 1..127 {
fn register_keyboard(&self) -> EmptyResult {
for code in 1..255 {
self.register_key(code)?
}
Ok(())
}

fn register_mouse(&self) -> EmptyResult {
for code in [BTN_LEFT, BTN_RIGHT, BTN_MIDDLE] {
self.register_key(code)?
}

for code in [REL_X, REL_Y, REL_HWHEEL, REL_WHEEL] {
self.register_relative(code)?
}

Ok(())
}

Expand Down

0 comments on commit e8a76bc

Please sign in to comment.