Skip to content

Commit

Permalink
added stop command
Browse files Browse the repository at this point in the history
  • Loading branch information
H3rmt committed Feb 4, 2024
1 parent 61e2efa commit 3af5255
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 58 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ bind = $mainMod, 3, exec, hyprswitch --offset 3
bind = $mainMod, 4, exec, hyprswitch --offset 4

# exit submap and kill hyprswitch
bindrt = $mainMod, SUPER_L, exec, pkill hyprswitch
bindrt = $mainMod, SUPER_L, exec, hyprswitch --stop-daemon
bindrt = $mainMod, SUPER_L, submap, reset
submap = reset
```
8 changes: 7 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Args {
#[arg(long, short)]
pub reverse: bool,

/// Restrict cycling of windows to current workspace
/// Restrict cycling of windows to the current workspace
#[arg(long)]
pub stay_workspace: bool,

Expand Down Expand Up @@ -50,6 +50,12 @@ pub struct Args {
#[cfg(feature = "daemon")]
pub daemon: bool,

/// Stops the daemon, sends stop to socket server, doesn't execute current window switch
/// Needs to be used with --daemon
#[arg(long)]
#[cfg(feature = "daemon")]
pub stop_daemon: bool,

/// Starts the daemon with the gui
/// Needs to be used with --daemon
#[arg(long)]
Expand Down
87 changes: 56 additions & 31 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,39 +68,56 @@ async fn handle_client<F>(
{
let mut buffer = Vec::new();
stream.read_to_end(&mut buffer).await.expect("Failed to read");
println!("data: {:?}", buffer);
if buffer.is_empty() {
return;
}

if buffer.len() == 10 && buffer[0] == b'w' {
let vertical_workspaces = buffer[1] == 1;
let ignore_monitors = buffer[2] == 1;
let ignore_workspaces = buffer[3] == 1;
let same_class = buffer[4] == 1;
let reverse = buffer[5] == 1;
let stay_workspace = buffer[6] == 1;
let verbose = buffer[7] == 1;
let dry_run = buffer[8] == 1;
#[cfg(feature = "toast")]
let toast = buffer[9] == 1;
println!("data: {:?}", buffer);
match buffer[0] {
b's' => {
println!("Stopping daemon");
if Path::new(PATH).exists() {
std::fs::remove_file(PATH).expect("Failed to remove socket");
}
std::process::exit(0);
}
b'w' => {
if buffer.len() == 10 {
let vertical_workspaces = buffer[1] == 1;
let ignore_monitors = buffer[2] == 1;
let ignore_workspaces = buffer[3] == 1;
let same_class = buffer[4] == 1;
let reverse = buffer[5] == 1;
let stay_workspace = buffer[6] == 1;
let verbose = buffer[7] == 1;
let dry_run = buffer[8] == 1;
#[cfg(feature = "toast")]
let toast = buffer[9] == 1;

let info = Info {
vertical_workspaces,
ignore_monitors,
ignore_workspaces,
same_class,
reverse,
stay_workspace,
verbose,
dry_run,
#[cfg(feature = "toast")]
toast,
};
let info = Info {
vertical_workspaces,
ignore_monitors,
ignore_workspaces,
same_class,
reverse,
stay_workspace,
verbose,
dry_run,
#[cfg(feature = "toast")]
toast,
};

exec(
info,
#[cfg(feature = "gui")]
data_arc,
).await;
}
exec(
info,
#[cfg(feature = "gui")]
data_arc,
).await;
}
}
_ => {
println!("Unknown command");
}
};
}

pub async fn send_command(info: Info) -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -113,7 +130,7 @@ pub async fn send_command(info: Info) -> Result<(), Box<dyn std::error::Error>>
let toast = 0;


// send 12 to identify as real command
// send 'w' to identify as real command
let buf = &[
b'w',
info.vertical_workspaces as u8,
Expand All @@ -129,4 +146,12 @@ pub async fn send_command(info: Info) -> Result<(), Box<dyn std::error::Error>>
stream.write_all(buf).await?;
stream.flush().await?;
Ok(())
}

pub async fn send_stop_daemon() -> Result<(), Box<dyn std::error::Error>> {
// send 's' to identify as stop command
let mut stream = UnixStream::connect(PATH).await?;
stream.write_all(&[b's']).await?;
stream.flush().await?;
Ok(())
}
10 changes: 6 additions & 4 deletions src/gui.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use gtk4::{ApplicationWindow, Frame, gdk, glib};
#[cfg(not(feature = "adwaita"))]
#[cfg(not(feature = "libadwaita"))]
use gtk4::Application;
use gtk4::gdk::Monitor;
use gtk4::prelude::*;
use gtk4_layer_shell::{Layer, LayerShell};
use hyprland::data::Client;
#[cfg(feature = "adwaita")]
use libadwaita::Application;
#[cfg(feature = "libadwaita")]
use adw::Application;
use tokio::sync::MutexGuard;

use crate::{Data, Info, Share};

const SIZE_FACTOR: i16 = 7;
const IMG_SIZE_FACTOR: i16 = 9;

const CSS: &str = r#"
frame.active {
background-color: rgba(0, 0, 0, 0.5);
Expand All @@ -34,7 +36,7 @@ const CSS: &str = r#"

fn client_ui(client: &Client, active: bool) -> Frame {
let icon = gtk4::Image::from_icon_name(&client.class);
let pixel_size = (client.size.1 / (SIZE_FACTOR * 2)) as i32;
let pixel_size = (client.size.1 / IMG_SIZE_FACTOR) as i32;
icon.set_pixel_size(pixel_size);

let frame = Frame::builder()
Expand Down
50 changes: 29 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,38 @@ use crate::cli::Args;

mod cli;

///
/// # Usage
///
/// * Switch between windows of same class
/// * `hyprswitch --same-class`
/// * Switch backwards
/// * `hyprswitch --reverse`
///
/// ## Special
///
/// * Cycles through window on current workspace
/// * `hyprswitch --stay-workspace`
///
/// * Ignore workspaces and sort like one big workspace
/// * `hyprswitch --ignore-workspaces`
/// * Ignore monitors and sort like one big monitor
/// * `hyprswitch --ignore-monitors`
///
/// * Display workspaces vertically on monitors
/// * `hyprswitch --vertical-workspaces`
///

fn main() {
let cli = Args::parse();

#[cfg(feature = "daemon")]
if cli.stop_daemon {
let rt = tokio::runtime::Runtime::new().expect("Failed to create tokio runtime");

rt.block_on(async {
use hyprswitch::daemon;
if cli.verbose {
println!("Stopping daemon");
}
if !daemon::daemon_running().await {
if cli.verbose {
println!("Daemon not running");
}
return;
}

daemon::send_stop_daemon().await.map_err(|_e| {
#[cfg(feature = "toast")] {
use hyprswitch::toast::toast;
if cli.toast {
toast(&format!("Failed to stop daemon: {}", _e));
}
}
}).expect("Failed to stop daemon");
});
return;
}

#[cfg(feature = "daemon")]
if cli.daemon {
let rt = tokio::runtime::Runtime::new().expect("Failed to create tokio runtime");
Expand Down

0 comments on commit 3af5255

Please sign in to comment.