Skip to content

Commit

Permalink
feat: keyboard config command
Browse files Browse the repository at this point in the history
  • Loading branch information
WeirdTreeThing committed Dec 18, 2024
1 parent d5c5794 commit 0eca00a
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
68 changes: 68 additions & 0 deletions crosec/src/commands/get_keyboard_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use super::CrosEcCmd;
use crate::{ec_command::ec_command_bytemuck, EcCmdResult};
use bytemuck::{Pod, Zeroable};
use num_derive::FromPrimitive;
use std::os::fd::AsRawFd;

const MAX_TOP_ROW_KEYS: usize = 15;

/// Is the keyboard capable of sending function keys *in addition to*
/// action keys. This is possible for e.g. if the keyboard has a
/// dedicated Fn key.
pub const KEYBD_CAP_FUNCTION_KEYS: u8 = 1;
/// Whether the keyboard has a dedicated numeric keyboard.
pub const KEYBD_CAP_NUMERIC_KEYPAD: u8 = 2;
/// Whether the keyboard has a screenlock key.
pub const KEYBD_CAP_SCRNLOCK_KEY: u8 = 4;
/// Whether the keyboard has an assistant key.
pub const KEYBD_CAP_ASSISTANT_KEY: u8 = 8;

#[repr(C)]
#[derive(Clone, Copy, Debug, FromPrimitive)]
pub enum ActionKey {
TkAbsent = 0,
TkBack = 1,
TkForward = 2,
TkRefresh = 3,
TkFullscreen = 4,
TkOverview = 5,
TkBrightnessDown = 6,
TkBrightnessUp = 7,
TkVolMute = 8,
TkVolDown = 9,
TkVolUp = 10,
TkSnapshot = 11,
TkPrivacyScrnToggle = 12,
TkKbdBklightDown = 13,
TkKbdBklightUp = 14,
TkPlayPause = 15,
TkNextTrack = 16,
TkPrevTrack = 17,
TkKbdBklightToggle = 18,
TkMicmute = 19,
TkMenu = 20,
TkDictate = 21,
TkAccessibility = 22,
TkDonotdisturb = 23,
}

#[repr(C)]
#[derive(Pod, Zeroable, Clone, Copy, Debug)]
pub struct EcResponseKeybdConfig {
/// Number of top row keys, excluding Esc and Screenlock.
/// If this is 0, all Vivaldi keyboard code is disabled.
/// (i.e. does not expose any tables to the kernel).
pub num_top_row_keys: u8,

/// Empty log entries have both the cause and timestamp set to zero.
pub action_keys: [u8; MAX_TOP_ROW_KEYS],

/// Capability flags
pub capabilities: u8,
}

pub fn ec_cmd_get_keyboard_config<File: AsRawFd>(
file: &mut File,
) -> EcCmdResult<EcResponseKeybdConfig> {
ec_command_bytemuck(CrosEcCmd::GetKeybdConfig, 0, &(), file.as_raw_fd())
}
2 changes: 2 additions & 0 deletions crosec/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub enum CrosEcCmd {
ConsoleSnapshot = 0x0097,
ConsoleRead = 0x0098,
GetUptimeInfo = 0x0121,
GetKeybdConfig = 0x012A,
FpMode = 0x0402,
FpInfo = 0x0403,
FpFrame = 0x0404,
Expand All @@ -41,6 +42,7 @@ pub mod fp_upload_template;
pub mod get_chip_info;
pub mod get_cmd_versions;
pub mod get_features;
pub mod get_keyboard_config;
pub mod get_protocol_info;
pub mod get_uptime_info;
pub mod hello;
Expand Down
54 changes: 54 additions & 0 deletions ectool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ use crosec::battery::battery;
use crosec::commands::board_version::ec_cmd_board_version;
use crosec::commands::get_cmd_versions::ec_cmd_get_cmd_versions;
use crosec::commands::get_features::{ec_cmd_get_features, EC_FEATURE_PWM_FAN};
use crosec::commands::get_keyboard_config::{
ec_cmd_get_keyboard_config, ActionKey, KEYBD_CAP_ASSISTANT_KEY, KEYBD_CAP_FUNCTION_KEYS,
KEYBD_CAP_NUMERIC_KEYPAD, KEYBD_CAP_SCRNLOCK_KEY,
};
use crosec::commands::set_fan_target_rpm::ec_cmd_set_fan_target_rpm;
use crosec::commands::{
get_chip_info::ec_cmd_get_chip_info, hello::ec_cmd_hello, version::ec_cmd_version, CrosEcCmd,
Expand Down Expand Up @@ -92,6 +96,8 @@ enum Commands {
},
/// Get supported features
GetFeatures,
// Gets vivaldi keyboarc configuration
GetKeybdConfig,
/// Get number of fans
GetNumberOfFans,
/// Get the speed of fans, in RPM
Expand Down Expand Up @@ -207,6 +213,54 @@ fn main() -> Result<()> {
let features = ec_cmd_get_features(&mut file)?;
println!("EC supported features: {features:#b}");
}
Commands::GetKeybdConfig => {
let mut file = File::open(CROS_EC_PATH)?;
let config = ec_cmd_get_keyboard_config(&mut file)?;
println!("Number of top row keys: {}", config.num_top_row_keys);
println!("Keys:");
for i in 0..config.num_top_row_keys as usize {
match FromPrimitive::from_u8(config.action_keys[i]) {
Some(ActionKey::TkAbsent) => println!("Absent"),
Some(ActionKey::TkBack) => println!("Back"),
Some(ActionKey::TkForward) => println!("Forward"),
Some(ActionKey::TkRefresh) => println!("Refresh"),
Some(ActionKey::TkFullscreen) => println!("Fullscreen"),
Some(ActionKey::TkOverview) => println!("Overview"),
Some(ActionKey::TkBrightnessDown) => println!("Brightness Down"),
Some(ActionKey::TkBrightnessUp) => println!("Brightness Up"),
Some(ActionKey::TkVolMute) => println!("Volume Mute"),
Some(ActionKey::TkVolDown) => println!("Volume Down"),
Some(ActionKey::TkVolUp) => println!("Volume Up"),
Some(ActionKey::TkSnapshot) => println!("Snapshot"),
Some(ActionKey::TkPrivacyScrnToggle) => println!("Privacy Screen Toggle"),
Some(ActionKey::TkKbdBklightDown) => println!("Keyboard Backlight Down"),
Some(ActionKey::TkKbdBklightUp) => println!("Keyboard Backlight Up"),
Some(ActionKey::TkPlayPause) => println!("Play/Pause"),
Some(ActionKey::TkNextTrack) => println!("Next Track"),
Some(ActionKey::TkPrevTrack) => println!("Previous Track"),
Some(ActionKey::TkKbdBklightToggle) => println!("Keyboard Backlight Toggle"),
Some(ActionKey::TkMicmute) => println!("Mic Mute"),
Some(ActionKey::TkMenu) => println!("Menu"),
Some(ActionKey::TkDictate) => println!("Dictate"),
Some(ActionKey::TkAccessibility) => println!("Accessibility"),
Some(ActionKey::TkDonotdisturb) => println!("No Not Disturb"),
None => println!("Unknown"),
}
}
println!("Capabilities: {:#x}", config.capabilities);
if config.capabilities & KEYBD_CAP_FUNCTION_KEYS >= 1 {
println!("KEYBD_CAP_FUNCTION_KEYS");
}
if config.capabilities & KEYBD_CAP_NUMERIC_KEYPAD >= 1 {
println!("KEYBD_CAP_NUMERIC_KEYPAD");
}
if config.capabilities & KEYBD_CAP_SCRNLOCK_KEY >= 1 {
println!("KEYBD_CAP_SCRNLOCK_KEY");
}
if config.capabilities & KEYBD_CAP_ASSISTANT_KEY >= 1 {
println!("KEYBD_CAP_ASSISTANT_KEY");
}
}
Commands::GetNumberOfFans => {
let mut file = File::open(CROS_EC_PATH)?;
let number_of_fans = get_number_of_fans(&mut file).unwrap();
Expand Down

0 comments on commit 0eca00a

Please sign in to comment.