Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added @pend internal command #220

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions swhkd/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub const MODE_STATEMENT: &str = "mode";
pub const MODE_END_STATEMENT: &str = "endmode";
pub const MODE_ENTER_STATEMENT: &str = "@enter";
pub const MODE_ESCAPE_STATEMENT: &str = "@escape";
pub const MODE_PEND_STATEMENT: &str = "@pend";
pub const MODE_SWALLOW_STATEMENT: &str = "swallow";
pub const MODE_ONEOFF_STATEMENT: &str = "oneoff";

Expand Down
92 changes: 77 additions & 15 deletions swhkd/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,44 +97,103 @@ async fn main() -> Result<(), Box<dyn Error>> {

let mut modes = load_config();
let mut mode_stack: Vec<usize> = vec![0];
let mut command_stack: Vec<String> = Vec::new();
let mut pending_mode_stack: Vec<usize> = Vec::new();

macro_rules! send_command {
($hotkey: expr, $socket_path: expr) => {
log::info!("Hotkey pressed: {:#?}", $hotkey);
let command = $hotkey.command;
($command: expr, $socket_path: expr) => {
let mut command = $command;
let mut commands_to_send = String::new();
if modes[mode_stack[mode_stack.len() - 1]].options.oneoff {
if !pending_mode_stack.is_empty()
&& pending_mode_stack[pending_mode_stack.len() - 1]
!= mode_stack[mode_stack.len() - 1]
{
mode_stack.pop();
continue;
}
command = format!("{} && {}", command, command_stack.pop().unwrap());
mode_stack.pop();
pending_mode_stack.pop();
command_stack.pop();
}
if command.contains('@') {
let commands = command.split("&&").map(|s| s.trim()).collect::<Vec<_>>();
for cmd in commands {
let mut commands = command.split("&&").map(|s| s.trim()).collect::<Vec<_>>();
let mut pending: bool = false;
let mut i: usize = 0;
let mut len: usize = commands.len();
let mut pending_command = String::new();
let mut commands_to_push = String::new();
if !command_stack.is_empty() {
pending_command = command_stack[command_stack.len() - 1].clone();
}

while i < len {
let cmd = commands[i];
if pending {
commands_to_push.push_str(format!("{cmd} && ").as_str());
i += 1;
continue;
}
match cmd.split(' ').next().unwrap() {
config::MODE_ENTER_STATEMENT => {
let enter_mode = cmd.split(' ').nth(1).unwrap();
for (i, mode) in modes.iter().enumerate() {
if mode.name == enter_mode {
mode_stack.push(i);
break;
}
}
let index = modes.iter().position(|m| m.name == enter_mode).unwrap();
mode_stack.push(index);
log::info!(
"Entering mode: {}",
modes[mode_stack[mode_stack.len() - 1]].name
);
}
config::MODE_PEND_STATEMENT => {
let enter_mode = cmd.split(' ').nth(1).unwrap();
let index = modes.iter().position(|m| m.name == enter_mode).unwrap();
mode_stack.push(index);
log::info!(
"Entering mode: {}",
modes[mode_stack[mode_stack.len() - 1]].name
);
pending = true;
pending_mode_stack.push(index);
}
config::MODE_ESCAPE_STATEMENT => {
if !pending_mode_stack.is_empty()
&& pending_mode_stack[pending_mode_stack.len() - 1]
!= mode_stack[mode_stack.len() - 1]
{
mode_stack.pop();
i += 1;
continue;
}
let pending_commands: Vec<&str> =
pending_command.split("&&").map(|s| s.trim()).collect();
commands.extend(pending_commands);
len = commands.len();
mode_stack.pop();
pending_mode_stack.pop();
command_stack.pop();
}
_ => commands_to_send.push_str(format!("{cmd} &&").as_str()),
_ => commands_to_send.push_str(format!("{cmd} && ").as_str()),
}
i += 1;
}
if pending {
commands_to_push = commands_to_push.trim().to_string();
if commands_to_push.ends_with(" &&") {
commands_to_push =
commands_to_push.strip_suffix(" &&").unwrap().to_string();
}
log::info!("pending command: {}", commands_to_push);
command_stack.push(commands_to_push);
}
} else {
commands_to_send = command;
}
commands_to_send = commands_to_send.trim().to_string();
if commands_to_send.ends_with(" &&") {
commands_to_send = commands_to_send.strip_suffix(" &&").unwrap().to_string();
}
log::info!("commands_to_send: {}", commands_to_send);
if let Err(e) = socket_write(&commands_to_send, $socket_path.to_path_buf()) {
log::error!("Failed to send command to swhks through IPC.");
log::error!("Please make sure that swhks is running.");
Expand Down Expand Up @@ -239,7 +298,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
if hotkey.keybinding.on_release {
continue;
}
send_command!(hotkey.clone(), &socket_file_path);
log::info!("Hotkey pressed: {:#?}", hotkey);
send_command!(hotkey.clone().command, &socket_file_path);
hotkey_repeat_timer.as_mut().reset(Instant::now() + Duration::from_millis(repeat_cooldown_duration));
}

Expand Down Expand Up @@ -359,7 +419,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
0 => {
if last_hotkey.is_some() && pending_release {
pending_release = false;
send_command!(last_hotkey.clone().unwrap(), &socket_file_path);
log::info!("Hotkey pressed: {:#?}", last_hotkey.clone().unwrap());
send_command!(last_hotkey.clone().unwrap().command, &socket_file_path);
last_hotkey = None;
}
if let Some(modifier) = modifiers_map.get(&key) {
Expand Down Expand Up @@ -422,7 +483,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
pending_release = true;
break;
}
send_command!(hotkey.clone(), &socket_file_path);
log::info!("Hotkey pressed: {:#?}", hotkey);
send_command!(hotkey.clone().command, &socket_file_path);
hotkey_repeat_timer.as_mut().reset(Instant::now() + Duration::from_millis(repeat_cooldown_duration));
continue;
}
Expand Down
Loading