Skip to content

Commit

Permalink
add an ipc api for the light item
Browse files Browse the repository at this point in the history
  • Loading branch information
acheronfail committed Aug 6, 2023
1 parent fae178a commit fae188d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
3 changes: 1 addition & 2 deletions IDEAS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions scripts/i3.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 37 additions & 2 deletions src/bar_items/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -128,15 +130,48 @@ 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?,
I3Button::ScrollUp => light.adjust(increment).await?,
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 },
}

0 comments on commit fae188d

Please sign in to comment.