Skip to content

Commit 6669e0f

Browse files
Merge pull request #40 from jamesbt365/udev
Add generate udev command
2 parents 1697b81 + 19d9930 commit 6669e0f

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod snippets;
2+
pub mod udev;
23
pub mod utils;
34

45
pub(crate) const ACCENT_COLOUR: Colour = Colour(0x8957e5);

src/commands/udev.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use crate::{Context, Error};
2+
3+
use std::fmt::Write;
4+
5+
use poise::serenity_prelude::CreateAttachment;
6+
use poise::CreateReply;
7+
8+
/// Generates udev rules for the given vendor and product Ids.
9+
#[poise::command(
10+
rename = "generate-udev",
11+
aliases("udev"),
12+
slash_command,
13+
prefix_command
14+
)]
15+
pub async fn generate_udev(
16+
ctx: Context<'_>,
17+
#[description = "The Vendor Id in decimal."] vendor_id: u64,
18+
#[description = "The Product Id in decimal."] product_id: u64,
19+
libinput_override: Option<bool>,
20+
) -> Result<(), Error> {
21+
let udev = gen_udev(vendor_id, product_id, libinput_override.unwrap_or(true));
22+
23+
let attachment = CreateAttachment::bytes(udev, "70-opentabletdriver.rules");
24+
ctx.send(
25+
CreateReply::default()
26+
.content("place this file in `/etc/udev/rules.d/70-opentabletdriver.rules` then run the following:\n \
27+
```\nsudo udevadm control --reload-rules && sudo udevadm trigger\n```")
28+
.attachment(attachment),
29+
)
30+
.await?;
31+
32+
Ok(())
33+
}
34+
35+
const REQUIRED_UDEV_STR: &str = r#"
36+
KERNEL=="uinput", SUBSYSTEM=="misc", OPTIONS+="static_node=uinput", TAG+="uaccess", TAG+="udev-acl"
37+
KERNEL=="js[0-9]*", SUBSYSTEM=="input", ATTRS{name}=="OpenTabletDriver Virtual Tablet", RUN+="/usr/bin/env rm %E{DEVNAME}"
38+
"#;
39+
40+
fn gen_udev(id_vendor: u64, id_product: u64, libinput_override: bool) -> String {
41+
let mut udev_rules = format!(
42+
"KERNEL==\"hidraw*\", ATTRS{{idVendor}}==\"{id_vendor:X}\", ATTRS{{idProduct}}==\"{id_product:X}\", TAG+=\"uaccess\", TAG+=\"udev-acl\"\n\
43+
SUBSYSTEM==\"usb\", ATTRS{{idVendor}}==\"{id_vendor:X}\", ATTRS{{idProduct}}==\"{id_product:X}\", TAG+=\"uaccess\", TAG+=\"udev-acl\""
44+
);
45+
46+
if libinput_override {
47+
write!(
48+
udev_rules,
49+
"\nSUBSYSTEM==\"input\", ATTRS{{idVendor}}==\"{id_vendor:X}\", ATTRS{{idProduct}}==\"{id_product:X}\""
50+
).unwrap();
51+
}
52+
53+
format!("{REQUIRED_UDEV_STR}\n# Generated by TabletBot\n{udev_rules}")
54+
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ async fn main() {
9191
commands::utils::add_repo(),
9292
commands::utils::remove_repo(),
9393
commands::utils::list_repos(),
94+
commands::udev::generate_udev(),
9495
],
9596
prefix_options: poise::PrefixFrameworkOptions {
9697
prefix: Some("!".into()),

0 commit comments

Comments
 (0)