From fae188da8c5a316ce882b7f3e78c12afd9811d37 Mon Sep 17 00:00:00 2001 From: acheronfail Date: Sun, 6 Aug 2023 20:13:21 +0930 Subject: [PATCH] add an ipc api for the light item --- IDEAS.md | 3 +-- scripts/i3.conf | 6 +++--- src/bar_items/light.rs | 39 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/IDEAS.md b/IDEAS.md index d7fc87f..0d72b8f 100644 --- a/IDEAS.md +++ b/IDEAS.md @@ -10,8 +10,7 @@ There's no guarantee they'll ever be added or implemented, and they'll likely be ## Bugs -* light bar item doesn't update when brightness changed by other means - * implement an ipc command and map that in i3, so that it's in sync +* ... ## Improvements diff --git a/scripts/i3.conf b/scripts/i3.conf index 0128d1a..17731f4 100644 --- a/scripts/i3.conf +++ b/scripts/i3.conf @@ -34,9 +34,9 @@ bindsym ctrl+shift+p exec istat-ipc --socket /tmp/istat-socket-2.dev set-theme / bindsym bracketleft exec istat-ipc --socket /tmp/istat-socket.dev custom pulse volume-down sink bindsym bracketright exec istat-ipc --socket /tmp/istat-socket.dev custom pulse volume-up sink bindsym backslash exec istat-ipc --socket /tmp/istat-socket.dev custom pulse mute-toggle sink -bindsym shift+bracketleft exec istat-ipc --socket /tmp/istat-socket.dev custom pulse volume-down source -bindsym shift+bracketright exec istat-ipc --socket /tmp/istat-socket.dev custom pulse volume-up source -bindsym shift+backslash exec istat-ipc --socket /tmp/istat-socket.dev custom pulse mute-toggle source +bindsym shift+bracketleft exec istat-ipc --socket /tmp/istat-socket.dev custom light increase +bindsym shift+bracketright exec istat-ipc --socket /tmp/istat-socket.dev custom light decrease +bindsym shift+backslash exec istat-ipc --socket /tmp/istat-socket.dev custom light set 50 # click events bindsym a exec istat-ipc --socket /tmp/istat-socket.dev click pulse scroll_down diff --git a/src/bar_items/light.rs b/src/bar_items/light.rs index ab350f9..39d3029 100644 --- a/src/bar_items/light.rs +++ b/src/bar_items/light.rs @@ -5,10 +5,12 @@ use std::path::{Path, PathBuf}; use async_trait::async_trait; +use clap::Parser; use serde_derive::{Deserialize, Serialize}; +use serde_json::json; use tokio::fs; -use crate::context::{BarEvent, BarItem, Context, StopAction}; +use crate::context::{BarEvent, BarItem, Context, CustomResponse, StopAction}; use crate::error::Result; use crate::i3::{I3Button, I3Item}; @@ -52,7 +54,7 @@ impl LightFile { /// Set the brightness of this light to a percentage pub async fn set(&self, pct: u8) -> Result<()> { let step = self.max_brightness / 100; - let value = (pct as u64) * step; + let value = (pct.clamp(0, 100) as u64) * step; fs::write(&self.brightness_file, value.to_string()).await?; Ok(()) @@ -128,6 +130,7 @@ impl BarItem for Light { loop { ctx.update_item(light.format().await?).await?; match ctx.wait_for_event(None).await { + // mouse events Some(BarEvent::Click(click)) => match click.button { I3Button::Left => light.set(1).await?, I3Button::Right => light.set(100).await?, @@ -135,8 +138,40 @@ impl BarItem for Light { I3Button::ScrollDown => light.adjust(-increment).await?, _ => {} }, + // custom ipc events + Some(BarEvent::Custom { payload, responder }) => { + let resp = match LightCommand::try_parse_from(payload) { + Ok(cmd) => { + match match cmd { + LightCommand::Increase => light.adjust(increment).await, + LightCommand::Decrease => light.adjust(-increment).await, + LightCommand::Set { pct } => light.set(pct).await, + } { + Ok(()) => CustomResponse::Json(json!(())), + Err(e) => CustomResponse::Json(json!({ + "failure": e.to_string() + })), + } + } + Err(e) => CustomResponse::Help(e.render()), + }; + + let _ = responder.send(resp); + } + // other events just trigger a refresh _ => {} } } } } + +#[derive(Debug, Parser)] +#[command(name = "light", no_binary_name = true)] +enum LightCommand { + /// Increase the brightness by the configured increment amount + Increase, + /// Decrease the brightness by the configured increment amount + Decrease, + /// Set the brightness to a specific value + Set { pct: u8 }, +}