Skip to content
This repository has been archived by the owner on Feb 15, 2025. It is now read-only.

Commit

Permalink
[Refactor] Change the keymap.
Browse files Browse the repository at this point in the history
Change the kepmap from string->int to string->string.
Make it more flexible.

And change the code accordingly to match it.
  • Loading branch information
philia897 committed Feb 9, 2025
1 parent 12ca8c0 commit 79cacc9
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 144 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ resolver = "2"
members = [
"liz",
"andthe",
"bluebird",
"bluebird",
]
20 changes: 10 additions & 10 deletions bluebird/src/tools/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl UserDataTable {
let mut file = File::open(keymap_path).expect("Unable to open keymap file");
let mut contents = String::new();
file.read_to_string(&mut contents).expect("Unable to read keymap file");
let key_event_codes: HashMap<String, usize> = serde_json::from_str(&contents).expect("Unable to parse JSON");
let key_event_codes: HashMap<String, String> = serde_json::from_str(&contents).expect("Unable to parse JSON");

let mut new_table = DataTable::new();
for row in &self.data {
Expand Down Expand Up @@ -250,11 +250,11 @@ impl UserDataTable {
* Convert shortcut string to key presses, using the keymap to map key to keycode
* For example:
* meta+pageup tab 123!@# tab ABC
* => 126.1 104.1 104.0 126.0 15.1 15.0 <str>type 123!@#<str> 15.1 15.0 <str>type ABC<str>
* => 126.1 104.1 104.0 126.0 15.1 15.0 (str)type 123!@#(str) 15.1 15.0 (str)type ABC(str)
* Where keycode of meta is 126, pageup (104), tab (15)
* type 123!@ means directly type these characters "123!@".
*/
fn convert_shortcut_to_key_presses(shortcut: &str, key_event_codes: &HashMap<String, usize>) -> Option<String> {
fn convert_shortcut_to_key_presses(shortcut: &str, key_event_codes: &HashMap<String, String>) -> Option<String> {
let mut result = Vec::new();

let ss: Vec<&str> = shortcut.split("(str)").collect();
Expand All @@ -265,7 +265,7 @@ fn convert_shortcut_to_key_presses(shortcut: &str, key_event_codes: &HashMap<Str
}
if s.starts_with("+") {
let type_str: &str = &s[2..];
result.push(format!("<str>+ {}<str>", type_str.trim()));
result.push(format!("(str)+ {}(str)", type_str.trim()));
} else {
// Split the input by spaces
let parts: Vec<&str> = s.split_whitespace().collect();
Expand All @@ -278,29 +278,29 @@ fn convert_shortcut_to_key_presses(shortcut: &str, key_event_codes: &HashMap<Str
let keys: Vec<&str> = part.split('+').collect();
for key in &keys {
let key: String = key.trim().to_lowercase();
if let Some(&event_code) = key_event_codes.get(&key) {
if let Some(event_code) = key_event_codes.get(&key) {
result.push(format!("{}.1", event_code));
} else {
eprintln!("{} does not exist!", key);
result.push(format!("{}.1", key));
return None;
}
}
for key in keys.iter().rev() {
let key: String = key.trim().to_lowercase();
if let Some(&event_code) = key_event_codes.get(&key) {
if let Some(event_code) = key_event_codes.get(&key) {
result.push(format!("{}.0", event_code));
} else {
eprintln!("{} does not exist!", key);
result.push(format!("{}.0", key));
return None;
}
}
} else {
let key = part.trim().to_lowercase();
if let Some(&event_code) = key_event_codes.get(&key) {
if let Some(event_code) = key_event_codes.get(&key) {
result.push(format!("{}.1", event_code));
result.push(format!("{}.0", event_code));
} else {
result.push(format!("<str>+ {}<str>", part.trim()));
result.push(format!("(str)+ {}(str)", part.trim()));
}
}
}
Expand Down
44 changes: 43 additions & 1 deletion bluebird/src/tools/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,49 @@ fn get_args(shortcut: &str) -> Vec<&str> {
}

pub fn execute_shortcut_ydotool(shortcut_str: &str, delay_ms: u64, ydotool_socket_path: &str) -> Result<(), Box<dyn std::error::Error>> {
let shortcuts: Vec<&str> = shortcut_str.split("<str>").collect();
let shortcuts: Vec<&str> = shortcut_str.split("(str)").collect();

for shortcut in shortcuts {
if shortcut.is_empty() {
continue;
}

sleep(Duration::from_millis(delay_ms)); // Sleep for the specified delay

let args: Vec<&str> = get_args(shortcut);

// Execute the command
let result: Result<std::process::Output, std::io::Error> = Command::new("ydotool")
.env("YDOTOOL_SOCKET", ydotool_socket_path)
.args(args)
.output();

match result {
Ok(output) => { // Log the output and error
if output.status.success() {
// println!("Command executed successfully: {:?}", output);
} else {
// Explicitly return an Err with the error message
let error_message: String = format!(
"ydotool command failed with status: {:?}, stderr: {:?}",
output.status,
String::from_utf8_lossy(&output.stderr)
);
return Err(Box::new(io::Error::new(io::ErrorKind::Other, error_message)))
}
}
Err(e) => {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, format!("Failed to execute command: {}", e))))
}
}
}

Ok(())
}


pub fn execute_shortcut_ydotool(shortcut_str: &str, delay_ms: u64, ydotool_socket_path: &str) -> Result<(), Box<dyn std::error::Error>> {
let shortcuts: Vec<&str> = shortcut_str.split("(str)").collect();

for shortcut in shortcuts {
if shortcut.is_empty() {
Expand Down
4 changes: 2 additions & 2 deletions bluebird/src/tools/rhythm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ impl Rhythm {

let content: String = fs::read_to_string(rhythm_path)?;
let rhythm: Rhythm = toml::de::from_str(&content).unwrap_or_default();
eprintln!("Reading Config:\n{:?}", rhythm);

println!("Reading Config:\n{:?}", rhythm);

Ok(rhythm)
}
Expand Down
255 changes: 127 additions & 128 deletions data/keymap.json
Original file line number Diff line number Diff line change
@@ -1,129 +1,128 @@
{
"esc": 1,
"1": 2,
"2": 3,
"3": 4,
"4": 5,
"5": 6,
"6": 7,
"7": 8,
"8": 9,
"9": 10,
"0": 11,
"minus": 12,
"-": 12,
"equal": 13,
"=": 13,
"backspace": 14,
"tab": 15,
"q": 16,
"w": 17,
"e": 18,
"r": 19,
"t": 20,
"y": 21,
"u": 22,
"i": 23,
"o": 24,
"p": 25,
"leftbrace": 26,
"[": 26,
"rightbrace": 27,
"]": 27,
"enter": 28,
"leftctrl": 29,
"ctrl": 29,
"a": 30,
"s": 31,
"d": 32,
"f": 33,
"g": 34,
"h": 35,
"j": 36,
"k": 37,
"l": 38,
"semicolon": 39,
";": 39,
"apostrophe": 40,
"'": 40,
"grave": 41,
"`": 41,
"leftshift": 42,
"shift": 42,
"backslash": 43,
"\\": 43,
"z": 44,
"x": 45,
"c": 46,
"v": 47,
"b": 48,
"n": 49,
"m": 50,
"comma": 51,
",": 51,
"dot": 52,
".": 52,
"slash": 53,
"/": 53,
"rightshift": 54,
"kpasterisk": 55,
"leftalt": 56,
"alt": 56,
"space": 57,
"capslock": 58,
"f1": 59,
"f2": 60,
"f3": 61,
"f4": 62,
"f5": 63,
"f6": 64,
"f7": 65,
"f8": 66,
"f9": 67,
"f10": 68,
"numlock": 69,
"scrolllock": 70,
"kp7": 71,
"kp8": 72,
"kp9": 73,
"kpminus": 74,
"kp4": 75,
"kp5": 76,
"kp6": 77,
"kpplus": 78,
"kp1": 79,
"kp2": 80,
"kp3": 81,
"kp0": 82,
"kpdot": 83,
"zenkakuhankaku": 85,
"102nd": 86,
"f11": 87,
"f12": 88,
"ro": 89,
"katakana": 90,
"hiragana": 91,
"henkan": 92,
"katakanahiragana": 93,
"muhenkan": 94,
"kpjpcomma": 95,
"kpenter": 96,
"rightctrl": 97,
"kpslash": 98,
"sysrq": 99,
"rightalt": 100,
"home": 102,
"up": 103,
"pageup": 104,
"left": 105,
"right": 106,
"end": 107,
"down": 108,
"pagedown": 109,
"insert": 110,
"delete": 111,

"leftmeta": 125,
"rightmeta": 126,
"meta": 126
}
"esc": "1",
"1": "2",
"2": "3",
"3": "4",
"4": "5",
"5": "6",
"6": "7",
"7": "8",
"8": "9",
"9": "10",
"0": "11",
"minus": "12",
"-": "12",
"equal": "13",
"=": "13",
"backspace": "14",
"tab": "15",
"q": "16",
"w": "17",
"e": "18",
"r": "19",
"t": "20",
"y": "21",
"u": "22",
"i": "23",
"o": "24",
"p": "25",
"leftbrace": "26",
"[": "26",
"rightbrace": "27",
"]": "27",
"enter": "28",
"leftctrl": "29",
"ctrl": "29",
"a": "30",
"s": "31",
"d": "32",
"f": "33",
"g": "34",
"h": "35",
"j": "36",
"k": "37",
"l": "38",
"semicolon": "39",
";": "39",
"apostrophe": "40",
"'": "40",
"grave": "41",
"`": "41",
"leftshift": "42",
"shift": "42",
"backslash": "43",
"\\": "43",
"z": "44",
"x": "45",
"c": "46",
"v": "47",
"b": "48",
"n": "49",
"m": "50",
"comma": "51",
",": "51",
"dot": "52",
".": "52",
"slash": "53",
"/": "53",
"rightshift": "54",
"kpasterisk": "55",
"leftalt": "56",
"alt": "56",
"space": "57",
"capslock": "58",
"f1": "59",
"f2": "60",
"f3": "61",
"f4": "62",
"f5": "63",
"f6": "64",
"f7": "65",
"f8": "66",
"f9": "67",
"f10": "68",
"numlock": "69",
"scrolllock": "70",
"kp7": "71",
"kp8": "72",
"kp9": "73",
"kpminus": "74",
"kp4": "75",
"kp5": "76",
"kp6": "77",
"kpplus": "78",
"kp1": "79",
"kp2": "80",
"kp3": "81",
"kp0": "82",
"kpdot": "83",
"zenkakuhankaku": "85",
"102nd": "86",
"f11": "87",
"f12": "88",
"ro": "89",
"katakana": "90",
"hiragana": "91",
"henkan": "92",
"katakanahiragana": "93",
"muhenkan": "94",
"kpjpcomma": "95",
"kpenter": "96",
"rightctrl": "97",
"kpslash": "98",
"sysrq": "99",
"rightalt": "100",
"home": "102",
"up": "103",
"pageup": "104",
"left": "105",
"right": "106",
"end": "107",
"down": "108",
"pagedown": "109",
"insert": "110",
"delete": "111",
"leftmeta": "125",
"rightmeta": "126",
"meta": "126"
}
1 change: 0 additions & 1 deletion data/music_sheet.lock

This file was deleted.

Loading

0 comments on commit 79cacc9

Please sign in to comment.