haimgel / display-switch

Turn a $30 USB switch into a full-featured multi-monitor KVM switch
https://haim.dev/posts/2020-07-28-dual-monitor-kvm/
MIT License
2.82k stars 110 forks source link

Support multiple command executions with `on_usb_<event>_execute` #133

Open bil0u opened 7 months ago

bil0u commented 7 months ago

Hi there, thanks for your work on this project.

I'm using a custom version of m1ddc to send switching input commands to my displays when my USB switch is disconnected, and the binary needs to be called for each display.

It would be great to support execution of multiple commands when usb events are fired.

An easy implementation (tested) would be to split the command string on the ; delimiter, and call each command successively.


https://github.com/haimgel/display-switch/blob/a5b76e0d04a84051b7fc6dac212143bce5760b41/src/display_control.rs#L180-L181

would be replaced by

for subcommand in execute_command.split(";") {
    let subcommand = subcommand.trim();
    if !subcommand.is_empty() {
        try_run_command(subcommand).unwrap_or_else(|err| error!("Error executing external command '{}': {}", subcommand, err));
    }
}

What do you think ?

zerosym commented 5 months ago

I am also using m1ddc to swap multiple monitors at the same time. You can chain commands in this fashion without needing anything additional if you wish:

# Example
on_usb_connect_execute = "sh -c \"m1ddc display 1 set input 1 && m1ddc display 2 set input 1\""

Unsure if escaping the inner quotations is needed here, but this seems to work for me.