diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d6da36c --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,284 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anstream" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" + +[[package]] +name = "anstyle-parse" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +dependencies = [ + "anstyle", + "windows-sys", +] + +[[package]] +name = "cc" +version = "1.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41c270e7540d725e65ac7f1b212ac8ce349719624d7bcff99f8e2e488e8cf03f" + +[[package]] +name = "clap" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" + +[[package]] +name = "colorchoice" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" + +[[package]] +name = "g203_rs" +version = "0.1.0" +dependencies = [ + "clap", + "rusb", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] +name = "libusb1-sys" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da050ade7ac4ff1ba5379af847a10a10a8e284181e060105bf8d86960ce9ce0f" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "pkg-config" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" + +[[package]] +name = "proc-macro2" +version = "1.0.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec96c6a92621310b51366f1e28d05ef11489516e93be030060e5fc12024a49d6" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rusb" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab9f9ff05b63a786553a4c02943b74b34a988448671001e9a27e2f0565cc05a4" +dependencies = [ + "libc", + "libusb1-sys", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..ccf3484 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "g203_rs" +version = "0.1.0" +edition = "2021" +author = "Carlos Menezes " +license = "MIT" +description = "CLI and library for controlling the Logitech G203 Lightsync mouse" + +[lib] +name = "g203_lib" +path = "src/lib.rs" + +[[bin]] +name = "g203ctl" +path = "src/main.rs" + +[dependencies] +clap = { version = "4.5.4", features = ["cargo", "derive"] } +rusb = "0.9" diff --git a/README.md b/README.md new file mode 100644 index 0000000..c2175d8 --- /dev/null +++ b/README.md @@ -0,0 +1,63 @@ +# g203-rs + +
+ +
+ +This is a command-line interface and library for controlling the Logitech G203 Lightsync mouse. It is built in Rust and uses the `g203_lib` library for device control and the `clap` crate for command-line argument parsing. + +Only tested in MacBook Pro M1 2020 (Sonoma 14). + +## Installation + +You need `libusb` installed. + +```sh +git clone https://github.com/carlos-menezes/g203-rs.git +cd g203ctl +cargo build --release +``` + +The executable will be located in the `target/release` directory. + +## Usage + +### Library + +You can see an example on how to use the library in `main.rs`. + +### CLI + +```sh +Usage: g203ctl + +Commands: + solid + breathe + cycle + triple + wave + blend + help Print this message or the help of the given subcommand(s) +``` + +### Examples + +```sh +./g203ctl solid ff0000 +./g203ctl blend 10 100 +./g203ctl cycle +./g203ctl breathe 00ff00 100 50 +``` + +## License + +MIT License + +Copyright (c) 2024 Carlos Menezes + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..cd99022 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,269 @@ +use std::time::Duration; + +use rusb::{DeviceHandle, GlobalContext}; + +const INTERFACE_ID: u8 = 0x01; +const CONFIGURATION_ID: u8 = 0; +const VENDOR_ID: u16 = 0x046D; +const PRODUCT_ID: u16 = 0xC09D; + +// Direction enum to represent the direction of the wave effect +#[derive(Clone)] +pub enum Direction { + Left = 0x01, + Right = 0x06, +} + +#[derive(Debug)] +pub struct Controller { + inner: DeviceHandle, + timeout: Duration, +} + +impl Controller { + pub fn new_with_timeout(timeout: Duration) -> rusb::Result { + let handle = rusb::open_device_with_vid_pid(VENDOR_ID, PRODUCT_ID); + match handle { + Some(handle) => { + handle.set_active_configuration(CONFIGURATION_ID)?; + Ok(Self { + inner: handle, + timeout, + }) + } + None => Err(rusb::Error::NoDevice), + } + } + + pub fn new() -> rusb::Result { + Self::new_with_timeout(Duration::from_secs(2)) + } + + // Takes an array of three 8-bit unsigned integers representing RGB color values. + // Returns a Result type from the rusb crate, which will be Ok(()) if the command was successful, or an Err containing the error if not. + pub fn set_solid(&self, rgb: [u8; 3]) -> rusb::Result<()> { + let [red, green, blue] = rgb; + self.command( + &[ + 0x11, 0xff, 0x0e, 0x1b, 0x00, 0x01, red, green, blue, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + ], + true, + ) + } + + pub fn set_breathe(&self, rgb: [u8; 3], rate: u16, brightness: u8) -> rusb::Result<()> { + let [red, green, blue] = rgb; + let rate_bytes = rate.to_be_bytes(); + self.command( + &[ + 0x11, + 0xff, + 0x0e, + 0x1b, + 0x00, + 0x04, + red, + green, + blue, + rate_bytes[0], + rate_bytes[1], + 0x00, + brightness, + 0x00, + 0x00, + 0x00, + 0x01, + 0x00, + 0x00, + 0x00, + ], + false, + ) + } + + pub fn set_cycle(&self, rate: u16, brightness: u8) -> rusb::Result<()> { + let rate_bytes = rate.to_be_bytes(); + self.command( + &[ + 0x11, + 0xff, + 0x0e, + 0x1b, + 0x00, + 0x02, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + rate_bytes[0], + rate_bytes[1], + brightness, + 0x00, + 0x00, + 0x01, + 0x00, + 0x00, + 0x00, + ], + true, + ) + } + + pub fn set_triple(&self, colors: [[u8; 3]; 3]) -> rusb::Result<()> { + self.command( + &[ + 0x11, + 0xff, + 0x12, + 0x1b, + 0x01, + colors[0][0], + colors[0][1], + colors[0][2], + 0x02, + colors[1][0], + colors[1][1], + colors[1][2], + 0x03, + colors[2][0], + colors[2][1], + colors[2][2], + 0x00, + 0x00, + 0x00, + 0x00, + ], + false, + ) + } + + pub fn set_wave(&self, rate: u16, brightness: u8, direction: Direction) -> rusb::Result<()> { + let rate_bytes = rate.to_be_bytes(); + self.command( + &[ + 0x11, + 0xff, + 0x0e, + 0x1b, + 0x00, + 0x03, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + rate_bytes[0], + direction as u8, + brightness, + rate_bytes[1], + 0x01, + 0x00, + 0x00, + 0x00, + ], + true, + ) + } + + pub fn set_blend(&self, rate: u16, brightness: u8) -> rusb::Result<()> { + let rate_bytes = rate.to_be_bytes(); + self.command( + &[ + 0x11, + 0xff, + 0x0e, + 0x1b, + 0x00, + 0x06, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + rate_bytes[0], + rate_bytes[1], + brightness, + 0x00, + 0x01, + 0x00, + 0x00, + 0x00, + ], + true, + ) + } + + // This function is called before sending a command to the device. + // It detaches the kernel driver from the interface and claims the interface for the program. + // This is necessary to ensure that the program has exclusive access to the device. + fn command_prologue(&self) -> rusb::Result<()> { + // Detach the kernel driver from the interface. + // This allows the program to have exclusive access to the device. + self.inner.detach_kernel_driver(INTERFACE_ID)?; + // Claim the interface. + // This tells the operating system that the program is now in control of the device. + self.inner.claim_interface(INTERFACE_ID)?; + Ok(()) + } + + // This function is called after a command has been sent to the device. + // It releases the interface and reattaches the kernel driver. + // This is necessary to allow other programs to access the device. + fn command_epilogue(&self) -> rusb::Result<()> { + // Release the interface. + // This tells the operating system that the program is no longer in control of the device. + self.inner.release_interface(INTERFACE_ID)?; + // Reattach the kernel driver to the interface. + // This allows other programs to access the device. + self.inner.attach_kernel_driver(INTERFACE_ID)?; + Ok(()) + } + + pub fn command(&self, data: &[u8], disable_ls_memory: bool) -> rusb::Result<()> { + // Call the command prologue function to prepare the device for the command. + self.command_prologue()?; + + // If the disable_ls_memory flag is true, send a specific command to the device to disable LS memory. + if disable_ls_memory { + self.inner + .write_control( + 0x21, + 0x09, + 0x210, + 0x01, + &[0x10, 0xff, 0x0e, 0x5b, 0x01, 0x03, 0x05], + self.timeout, + ) + .unwrap(); + } + + // Send the command data to the device. + self.inner + .write_control(0x21, 0x09, 0x211, 0x01, data, self.timeout) + .unwrap(); + + // Check if the first four bytes of the command data matches a specific sequence. + // If it does, send an additional command to the device in order to apply the command. + let is_triple_command = data[0..4] == [0x11, 0xff, 0x12, 0x1b]; + if is_triple_command { + self.inner.write_control( + 0x21, + 0x09, + 0x211, + 0x01, + &[ + 0x11, 0xff, 0x12, 0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + self.timeout, + )?; + } + + self.command_epilogue()?; + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..24f5170 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,117 @@ +mod util; + +use clap::{command, Parser, Subcommand}; +use g203_lib::{Controller, Direction}; + +#[derive(Parser)] +#[command(author, version, about, long_about = None)] +struct Cli { + #[command(subcommand)] + command: Commands, +} + +#[derive(Subcommand)] +enum Commands { + Solid { + /// the RGB color value + #[arg(required = true, help = "e.g. FF0000")] + rgb: String, + }, + Breathe { + /// the RGB color value + #[arg(required = true, help = "e.g. FF0000")] + rgb: String, + /// the rate of the effect + #[arg(default_value = "1000", help = "the rate of the effect")] + rate: u16, + /// the brightness of the effect + #[arg(default_value = "100", help = "the brightness of the effect")] + brightness: u8, + }, + Cycle { + /// the rate of the effect + #[arg(default_value = "1000", help = "the rate of the effect")] + rate: u16, + /// the brightness of the effect + #[arg(default_value = "100", help = "the brightness of the effect")] + brightness: u8, + }, + Triple { + /// the RGB color value + #[arg(required = true, help = "e.g. FF0000")] + rgb_left: String, + + /// the RGB color value + #[arg(required = true, help = "e.g. FF0000")] + rgb_center: String, + + /// the RGB color value + #[arg(required = true, help = "e.g. FF0000")] + rgb_right: String, + }, + Wave { + /// the rate of the effect + #[arg(default_value = "1000", help = "the rate of the effect")] + rate: u16, + /// the brightness of the effect + #[arg(default_value = "100", help = "the brightness of the effect")] + brightness: u8, + /// the direction of the effect + #[arg(default_value = "l", help = "the direction of the effect")] + direction: String, + }, + Blend { + /// the rate of the effect + #[arg(default_value = "1000", help = "the rate of the effect")] + rate: u16, + /// the brightness of the effect + #[arg(default_value = "100", help = "the brightness of the effect")] + brightness: u8, + }, +} + +fn main() { + let controller = Controller::new().unwrap(); + let cli = Cli::parse(); + + match cli.command { + Commands::Solid { rgb } => { + let hex = util::hex_to_rgb(&rgb).unwrap(); + controller.set_solid(hex).unwrap() + } + Commands::Breathe { + rgb, + rate, + brightness, + } => { + let hex = util::hex_to_rgb(&rgb).unwrap(); + controller.set_breathe(hex, rate, brightness).unwrap() + } + Commands::Cycle { rate, brightness } => controller.set_cycle(rate, brightness).unwrap(), + Commands::Triple { + rgb_left, + rgb_center, + rgb_right, + } => { + let hex_left = util::hex_to_rgb(&rgb_left).unwrap(); + let hex_center = util::hex_to_rgb(&rgb_center).unwrap(); + let hex_right = util::hex_to_rgb(&rgb_right).unwrap(); + controller + .set_triple([hex_left, hex_center, hex_right]) + .unwrap() + } + Commands::Wave { + rate, + brightness, + direction, + } => { + let direction = match direction.as_str() { + "l" => Direction::Left, + "r" => Direction::Right, + _ => panic!("Invalid direction"), + }; + controller.set_wave(rate, brightness, direction).unwrap() + } + Commands::Blend { rate, brightness } => controller.set_blend(rate, brightness).unwrap(), + } +} diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..718224e --- /dev/null +++ b/src/util.rs @@ -0,0 +1,6 @@ +pub fn hex_to_rgb(hex: &str) -> Result<[u8; 3], std::num::ParseIntError> { + let r = u8::from_str_radix(&hex[0..2], 16)?; + let g = u8::from_str_radix(&hex[2..4], 16)?; + let b = u8::from_str_radix(&hex[4..6], 16)?; + Ok([r, g, b]) +}