Skip to content

Commit

Permalink
fix: added new version check command
Browse files Browse the repository at this point in the history
Signed-off-by: Enrico Stemmer <[email protected]>
  • Loading branch information
H3rmt committed Jan 13, 2025
1 parent 267363e commit 67e5563
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
21 changes: 17 additions & 4 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,30 @@ use crate::{
get_socket_path_buff, Command, Config, GuiConfig, Submap, Transfer, TransferType, DRY,
};

pub fn send_check_command() -> anyhow::Result<bool> {
pub fn send_version_check_command() -> anyhow::Result<bool> {
let send_struct = Transfer {
transfer: TransferType::Check,
transfer: TransferType::VersionCheck,
version: option_env!("CARGO_PKG_VERSION")
.unwrap_or("?.?.?")
.to_string(),
};
debug!("Sending check command");
debug!("Sending version_check command");
let serialized = serde_json::to_string(&send_struct)
.with_context(|| format!("Failed to serialize transfer {send_struct:?}"))?;
send(&serialized).with_context(|| format!("Failed to send check command {serialized}"))
send(&serialized).with_context(|| format!("Failed to send version_check command {serialized}"))
}

pub fn send_active_command() -> anyhow::Result<bool> {
let send_struct = Transfer {
transfer: TransferType::Active,
version: option_env!("CARGO_PKG_VERSION")
.unwrap_or("?.?.?")
.to_string(),
};
debug!("Sending active command");
let serialized = serde_json::to_string(&send_struct)
.with_context(|| format!("Failed to serialize transfer {send_struct:?}"))?;
send(&serialized).with_context(|| format!("Failed to send active command {serialized}"))
}

///
Expand Down
8 changes: 6 additions & 2 deletions src/daemon/handle_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,12 @@ pub(super) fn handle_client_transfer(
.expect("Failed to lock ACTIVE");

match transfer.transfer {
TransferType::Check => {
info!("Received running? command");
TransferType::VersionCheck => {
info!("Received version check command");
return_success(true, &mut stream)?;
}
TransferType::Active => {
info!("Received active command");
return_success(active, &mut stream)?;
}
TransferType::Init(config, gui_config, submap_config) => {
Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,16 @@ pub struct GuiConfig {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TransferType {
// switch to next/prev workspace/monitor/client or next selection in launcher
Switch(Command),
// init with config, gui_config and submap
Init(Config, GuiConfig, Submap),
// close command with kill
Close(bool),
Check,
// check if versions match (always succeeds)
VersionCheck,
// check if daemon is active (gui is open)
Active,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
11 changes: 7 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ fn main() -> anyhow::Result<()> {
return Ok(());
}
cli::Command::Close { kill } => {
info!("Stopping daemon");
hyprswitch::client::send_version_check_command()
.context("Failed to send check command to daemon")?;

if !hyprswitch::client::daemon_running() {
warn!("Daemon not running");
Expand All @@ -101,6 +102,9 @@ fn main() -> anyhow::Result<()> {
.context("Failed to send kill command to daemon")?;
}
cli::Command::Dispatch { simple_opts } => {
hyprswitch::client::send_version_check_command()
.context("Failed to send check command to daemon")?;

let command = Command::from(simple_opts);
hyprswitch::client::send_switch_command(command).with_context(|| {
format!("Failed to send switch command with command {command:?} to daemon")
Expand All @@ -113,8 +117,9 @@ fn main() -> anyhow::Result<()> {
submap_info,
reverse_key,
} => {
hyprswitch::client::send_check_command()
hyprswitch::client::send_version_check_command()
.context("Failed to send check command to daemon")?;

if !hyprswitch::client::daemon_running() {
let _ = Notification::new()
.summary(&format!("Hyprswitch ({}) Error", option_env!("CARGO_PKG_VERSION").unwrap_or("?.?.?")))
Expand All @@ -125,8 +130,6 @@ fn main() -> anyhow::Result<()> {
return Err(anyhow::anyhow!("Daemon not running"));
}

// Daemon is not running
info!("initialising daemon");
let config = Config::from(simple_config);
let gui_config = GuiConfig::from(gui_conf);
let submap_config = submap_conf
Expand Down

0 comments on commit 67e5563

Please sign in to comment.