Skip to content

Commit af142b6

Browse files
committed
chore: Handle Rgb/Hs conversion in Key/Layer, not daemon
1 parent 2e535c3 commit af142b6

File tree

6 files changed

+40
-35
lines changed

6 files changed

+40
-35
lines changed

backend/src/daemon/dummy.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use std::{
44
};
55

66
use super::{BoardId, Daemon};
7-
use crate::{Hs, Matrix};
7+
use crate::Matrix;
88

99
#[derive(Default)]
1010
struct BoardDummy {
1111
name: String,
1212
keymap: RefCell<HashMap<(u8, u8, u8), u16>>,
13-
color: Cell<Hs>,
13+
color: Cell<(u8, u8, u8)>,
1414
brightness: Cell<i32>,
1515
mode: Cell<(u8, u8)>,
1616
}
@@ -75,15 +75,15 @@ impl Daemon for DaemonDummy {
7575
Ok(Matrix::new(0, 0, Vec::new().into_boxed_slice()))
7676
}
7777

78-
fn color(&self, board: BoardId, index: u8) -> Result<Hs, String> {
78+
fn color(&self, board: BoardId, index: u8) -> Result<(u8, u8, u8), String> {
7979
// TODO implement support for per-led
8080
if index != 0xFF {
8181
return Err(format!("Can't set color index {}", index));
8282
}
8383
Ok(self.board(board)?.color.get())
8484
}
8585

86-
fn set_color(&self, board: BoardId, index: u8, color: Hs) -> Result<(), String> {
86+
fn set_color(&self, board: BoardId, index: u8, color: (u8, u8, u8)) -> Result<(), String> {
8787
if index != 0xFF {
8888
return Err(format!("Can't set color index {}", index));
8989
}

backend/src/daemon/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ mod server;
77

88
pub use self::{client::*, dummy::*, s76power::*, server::*};
99

10-
use crate::Hs;
11-
1210
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
1311
pub struct BoardId(u128);
1412

@@ -110,8 +108,8 @@ commands! {
110108
fn keymap_get(&self, board: BoardId, layer: u8, output: u8, input: u8) -> Result<u16, String>;
111109
fn keymap_set(&self, board: BoardId, layer: u8, output: u8, input: u8, value: u16) -> Result<(), String>;
112110
fn matrix_get(&self, board: BoardId) -> Result<Matrix, String>;
113-
fn color(&self, board: BoardId, index: u8) -> Result<Hs, String>;
114-
fn set_color(&self, board: BoardId, index: u8, color: Hs) -> Result<(), String>;
111+
fn color(&self, board: BoardId, index: u8) -> Result<(u8, u8, u8), String>;
112+
fn set_color(&self, board: BoardId, index: u8, color: (u8, u8, u8)) -> Result<(), String>;
115113
fn max_brightness(&self, board: BoardId) -> Result<i32, String>;
116114
fn brightness(&self, board: BoardId, index: u8) -> Result<i32, String>;
117115
fn set_brightness(&self, board: BoardId, index: u8, brightness: i32) -> Result<(), String>;

backend/src/daemon/s76power.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use glib::variant::{FromVariant, ToVariant};
77
use std::{cell::Cell, iter::Iterator};
88

99
use super::{err_str, BoardId, Daemon, Matrix};
10-
use crate::{Hs, Rgb};
10+
use crate::Rgb;
1111

1212
const DBUS_NAME: &str = "com.system76.PowerDaemon";
1313
const DBUS_KEYBOARD_IFACE: &str = "com.system76.PowerDaemon.Keyboard";
@@ -158,25 +158,24 @@ impl Daemon for DaemonS76Power {
158158
Err("Unimplemented".to_string())
159159
}
160160

161-
fn color(&self, board: BoardId, index: u8) -> Result<Hs, String> {
161+
fn color(&self, board: BoardId, index: u8) -> Result<(u8, u8, u8), String> {
162162
if index != 0xFF {
163163
return Err(format!("Can't set color index {}", index));
164164
}
165165
let color = self.board(board)?.prop::<String>("color")?;
166166
Ok(color
167167
.and_then(|c| Rgb::parse(&c))
168-
.map(|rgb| rgb.to_hs_lossy())
169-
.unwrap_or_else(|| Hs::new(0., 0.)))
168+
.map_or((0, 0, 0), |rgb| (rgb.r, rgb.g, rgb.b)))
170169
}
171170

172-
fn set_color(&self, board: BoardId, index: u8, color: Hs) -> Result<(), String> {
171+
fn set_color(&self, board: BoardId, index: u8, color: (u8, u8, u8)) -> Result<(), String> {
173172
if index != 0xFF {
174173
return Err(format!("Can't set color index {}", index));
175174
}
176175
let board = self.board(board)?;
177176
board.set_prop(
178177
"color",
179-
color.to_rgb().to_string(),
178+
Rgb::new(color.0, color.1, color.2).to_string(),
180179
&board.color_set_cancellable,
181180
)?;
182181
Ok(())

backend/src/daemon/server.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::{
1212
use uuid::Uuid;
1313

1414
use super::{err_str, BoardId, Daemon, DaemonCommand};
15-
use crate::{Hs, Matrix, Rgb};
15+
use crate::Matrix;
1616

1717
pub struct DaemonServer<R: Read, W: Write> {
1818
running: Cell<bool>,
@@ -186,26 +186,16 @@ impl<R: Read, W: Write> Daemon for DaemonServer<R, W> {
186186
Ok(Matrix::new(rows, cols, data.into_boxed_slice()))
187187
}
188188

189-
fn color(&self, board: BoardId, index: u8) -> Result<Hs, String> {
189+
fn color(&self, board: BoardId, index: u8) -> Result<(u8, u8, u8), String> {
190190
let mut ec = self.board(board)?;
191-
let color = unsafe { ec.led_get_color(index) }.map_err(err_str)?;
192-
193-
if unsafe { ec.access().is::<AccessHid>() } && index >= 0xf0 {
194-
Ok(Hs::from_ints(color.0, color.1))
195-
} else {
196-
Ok(Rgb::new(color.0, color.1, color.2).to_hs_lossy())
197-
}
191+
unsafe { ec.led_get_color(index) }.map_err(err_str)
198192
}
199193

200-
fn set_color(&self, board: BoardId, index: u8, color: Hs) -> Result<(), String> {
194+
fn set_color(&self, board: BoardId, index: u8, color: (u8, u8, u8)) -> Result<(), String> {
201195
let mut ec = self.board(board)?;
202-
203-
if unsafe { ec.access().is::<AccessHid>() } && index >= 0xf0 {
204-
let (h, s) = color.to_ints();
205-
unsafe { ec.led_set_color(index, h, s, 0).map_err(err_str) }
206-
} else {
207-
let Rgb { r, g, b } = color.to_rgb();
208-
unsafe { ec.led_set_color(index, r, g, b).map_err(err_str) }
196+
unsafe {
197+
ec.led_set_color(index, color.0, color.1, color.2)
198+
.map_err(err_str)
209199
}
210200
}
211201

backend/src/key.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl Key {
9898
let mut led_color = None;
9999
if board.layout().meta.has_mode && leds.len() > 0 {
100100
match board.daemon().color(board.board(), leds[0]) {
101-
Ok(color) => led_color = Some(color),
101+
Ok((r, g, b)) => led_color = Some(Rgb::new(r, g, b).to_hs_lossy()),
102102
Err(err) => error!("error getting key color: {}", err),
103103
}
104104
}
@@ -134,8 +134,9 @@ impl Key {
134134

135135
pub fn set_color(&self, color: Hs) -> Result<(), String> {
136136
let board = self.board();
137+
let Rgb { r, g, b } = color.to_rgb();
137138
for index in &self.leds {
138-
board.daemon().set_color(board.board(), *index, color)?;
139+
board.daemon().set_color(board.board(), *index, (r, g, b))?;
139140
}
140141
self.led_color.set(Some(color));
141142
board.set_leds_changed();

backend/src/layer.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use glib::clone::Downgrade;
22
use std::cell::Cell;
33

4-
use crate::{DaemonBoard, Hs, Mode};
4+
use crate::{DaemonBoard, Hs, Mode, Rgb};
55

66
#[derive(Debug)]
77
pub struct Layer {
@@ -42,6 +42,13 @@ impl Layer {
4242
let color = board
4343
.daemon()
4444
.color(board.board(), index)
45+
.map(|color| {
46+
if index == 0xff {
47+
Rgb::new(color.0, color.1, color.2).to_hs_lossy()
48+
} else {
49+
Hs::from_ints(color.0, color.1)
50+
}
51+
})
4552
.unwrap_or_else(|err| {
4653
error!("error getting layer color: {}", err);
4754
Hs::new(0., 0.)
@@ -95,7 +102,17 @@ impl Layer {
95102

96103
pub fn set_color(&self, color: Hs) -> Result<(), String> {
97104
let board = self.board();
98-
board.daemon().set_color(board.board(), self.index, color)?;
105+
if self.index == 0xff {
106+
let Rgb { r, g, b } = color.to_rgb();
107+
board
108+
.daemon()
109+
.set_color(board.board(), self.index, (r, g, b))?;
110+
} else {
111+
let (h, s) = color.to_ints();
112+
board
113+
.daemon()
114+
.set_color(board.board(), self.index, (h, s, 0))?;
115+
}
99116
self.color.set(color);
100117
board.set_leds_changed();
101118
Ok(())

0 commit comments

Comments
 (0)