Skip to content

Commit

Permalink
Support changing brightness
Browse files Browse the repository at this point in the history
Support changing the master brightness via the dial knob, when in FX
mode.
  • Loading branch information
X3n0m0rph59 committed Oct 28, 2020
1 parent 30c1dae commit 9c6a2ef
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Table of new and noteworthy changes:
| 0.1.17 | Released a new version of the `Eruption Profile Switcher` GNOME Shell extension; please be sure to update! |
| 0.1.17 | Add a new daemon that monitors the system for certain events and acts upon them |
| 0.1.17 | Add highly experimental support for the ROCCAT Kova Aimo |
| 0.1.17 | Support changing the master brightness via the dial knob on the keyboard |
| 0.1.16 | __New Release__ |
| 0.1.16 | Released a new version of the `Eruption Profile Switcher` GNOME Shell extension; please be sure to update! |
| 0.1.16 | Add support for ROCCAT Kone Pure Ultra LED |
Expand Down
26 changes: 13 additions & 13 deletions LIBRARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,19 @@ Please Note:

Eruption currently calls the following event handler functions, if they are present in a Lua script:

| Name | Class | Parameters | Description |
| -------------------------------------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------- |
| `on_startup` | _core_ | _n/a_ | Sent on startup, e.g. when a script is loaded |
| `on_quit` | _core_ | _n/a_ | Sent on daemon exit |
| `on_tick(delta)` | _core_ | delta: Timer delta since last tick | |
| `on_key_down(key_index)` | _Keyboard_ | key_index: Key index (column major order) | |
| `on_key_up(key_index)` | _Keyboard_ | key_index: Key index (column major order) | |
| `on_mouse_down(button_index)` | _Mouse_ | button_index: Index of mouse button | |
| `on_mouse_up(button_index)` | _Mouse_ | button_index: Index of mouse button | |
| `on_mouse_wheel(direction)` | _Mouse_ | direction: 1 == up, 2 == down | |
| `on_mouse_move(rel_x, rel_y, rel_z)` | _Mouse_ | x, y, z coordinate updates | Coordinates are relative (delta values) |
| `on_hid_event(event_type, arg1)` | _Hardware_ | event_type: 0 == unknown, 1 == KeyUp, 2 == KeyDown, 3 == MuteButton, 4 == Volume knob, arg1: data payload e.g.: scan codes/status codes | |
| `on_mouse_hid_event(event_type, arg1)` | _Hardware_ | event_type: 0 == unknown, 1 == DPI changed, arg1: data payload e.g.: scan codes/status codes | |
| Name | Class | Parameters | Description |
| -------------------------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------- |
| `on_startup` | _core_ | _n/a_ | Sent on startup, e.g. when a script is loaded |
| `on_quit` | _core_ | _n/a_ | Sent on daemon exit |
| `on_tick(delta)` | _core_ | delta: Timer delta since last tick | |
| `on_key_down(key_index)` | _Keyboard_ | key_index: Key index (column major order) | |
| `on_key_up(key_index)` | _Keyboard_ | key_index: Key index (column major order) | |
| `on_mouse_down(button_index)` | _Mouse_ | button_index: Index of mouse button | |
| `on_mouse_up(button_index)` | _Mouse_ | button_index: Index of mouse button | |
| `on_mouse_wheel(direction)` | _Mouse_ | direction: 1 == up, 2 == down | |
| `on_mouse_move(rel_x, rel_y, rel_z)` | _Mouse_ | x, y, z coordinate updates | Coordinates are relative (delta values) |
| `on_hid_event(event_type, arg1)` | _Hardware_ | event_type: 0 == unknown, 1 == KeyUp, 2 == KeyDown, 3 == MuteButton, 4 == Volume knob, 5 == Brightness knob, arg1: data payload e.g.: scan codes/status codes | |
| `on_mouse_hid_event(event_type, arg1)` | _Hardware_ | event_type: 0 == unknown, 1 == DPI changed, arg1: data payload e.g.: scan codes/status codes | |
Exhaustive listing of all currently available event callbacks

## Example Code
Expand Down
34 changes: 28 additions & 6 deletions eruption/src/dbus_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
*/

use dbus::{ffidisp::BusType, ffidisp::Connection, ffidisp::NameFlag, message::SignalArgs};
use dbus_tree::{
Access, MethodErr, Signal, {EmitsChangedSignal, Factory},
};
use dbus_tree::{{EmitsChangedSignal, Factory}, Access, MethodErr, Signal};
use log::*;
use std::path::PathBuf;
use std::sync::atomic::Ordering;
Expand Down Expand Up @@ -48,12 +46,14 @@ pub enum DbusApiError {
}

/// D-Bus API support
pub struct DbusApi {
pub struct DbusApi
{
connection: Option<Arc<Connection>>,

active_slot_changed: Arc<Signal<()>>,
active_profile_changed: Arc<Signal<()>>,
profiles_changed: Arc<Signal<()>>,
brightness_changed: Arc<Signal<()>>,
}

impl DbusApi {
Expand Down Expand Up @@ -84,6 +84,12 @@ impl DbusApi {
let profiles_changed_signal = Arc::new(f.signal("ProfilesChanged", ()));
let profiles_changed_signal_clone = profiles_changed_signal.clone();

let brightness_changed_signal = Arc::new(
f.signal("BrightnessChanged", ())
.sarg::<i64, _>("current brightness"),
);
let brightness_changed_signal_clone = brightness_changed_signal.clone();

let active_slot_property = f
.property::<u64, _>("ActiveSlot", ())
.emits_changed(EmitsChangedSignal::Const)
Expand Down Expand Up @@ -177,8 +183,9 @@ impl DbusApi {
.introspectable()
.add(
f.interface("org.eruption.Config", ())
.add_s(brightness_changed_signal_clone)
.add_p(enable_sfx_property_clone)
.add_p(brightness_property_clone),
.add_p(brightness_property_clone.clone()),
),
)
.add(
Expand Down Expand Up @@ -322,14 +329,29 @@ impl DbusApi {
.unwrap_or_else(|e| error!("Could not register the tree: {}", e));
c_clone.add_handler(tree);

DbusApi {
Self {
connection: Some(c_clone),
active_slot_changed: active_slot_changed_signal,
active_profile_changed: active_profile_changed_signal,
profiles_changed: profiles_changed_signal,
brightness_changed: brightness_changed_signal,
}
}

pub fn notify_brightness_changed(&self) {
let brightness = crate::BRIGHTNESS.load(Ordering::SeqCst);

self.connection
.as_ref()
.unwrap()
.send(self.brightness_changed.emit(
&"/org/eruption/config".into(),
&"org.eruption.Config".into(),
&[brightness as i64],
))
.unwrap();
}

pub fn notify_active_slot_changed(&self) {
let active_slot = crate::ACTIVE_SLOT.load(Ordering::SeqCst);

Expand Down
5 changes: 5 additions & 0 deletions eruption/src/hwdevices/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ pub enum KeyboardHidEvent {
KeyDown { code: KeyboardHidEventCode },
KeyUp { code: KeyboardHidEventCode },

// Brightness related
BrightnessUp,
BrightnessDown,
SetBrightness(u8),

// Audio related
MuteDown,
MuteUp,
Expand Down
47 changes: 42 additions & 5 deletions eruption/src/hwdevices/roccat_vulcan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ impl Into<u8> for KeyboardHidEventCode {
}
}

#[derive(Debug, PartialEq)]
pub enum DialMode {
Volume,
Brightness,
}

#[derive(Clone)]
/// Device specific code for the ROCCAT Vulcan 100/12x series keyboards
pub struct RoccatVulcan1xx {
Expand All @@ -139,6 +145,8 @@ pub struct RoccatVulcan1xx {
pub is_opened: bool,
pub ctrl_hiddev: Arc<Mutex<Option<hidapi::HidDevice>>>,
pub led_hiddev: Arc<Mutex<Option<hidapi::HidDevice>>>,

pub dial_mode: Arc<Mutex<DialMode>>,
}

impl RoccatVulcan1xx {
Expand All @@ -156,6 +164,8 @@ impl RoccatVulcan1xx {
is_opened: false,
ctrl_hiddev: Arc::new(Mutex::new(None)),
led_hiddev: Arc::new(Mutex::new(None)),

dial_mode: Arc::new(Mutex::new(DialMode::Brightness)),
}
}

Expand Down Expand Up @@ -755,7 +765,9 @@ impl KeyboardDeviceTrait for RoccatVulcan1xx {

match ctrl_dev.read_timeout(&mut buf, millis) {
Ok(_size) => {
hexdump::hexdump_iter(&buf).for_each(|s| trace!(" {}", s));
if buf.iter().any(|e| *e != 0) {
hexdump::hexdump_iter(&buf).for_each(|s| info!(" {}", s));
}

let event = match buf[0..5] {
// Key reports, incl. KEY_FN, ..
Expand Down Expand Up @@ -788,16 +800,41 @@ impl KeyboardDeviceTrait for RoccatVulcan1xx {
_ => KeyboardHidEvent::Unknown,
},

[0x03, 0x00, 0xcc, code, _] => match code {
0x01 => KeyboardHidEvent::VolumeUp,
0xff => KeyboardHidEvent::VolumeDown,
// volume up/down adjustment is initiated by the following sequence
[0x03, 0x00, 0x0b, 0x26, _] => { *self.dial_mode.lock() = DialMode::Volume; KeyboardHidEvent::Unknown },
[0x03, 0x00, 0x0b, 0x27, _] => { *self.dial_mode.lock() = DialMode::Volume; KeyboardHidEvent::Unknown },

_ => KeyboardHidEvent::Unknown,
[0x03, 0x00, 0xcc, code, _] => {
let result = if *self.dial_mode.lock() == DialMode::Volume {
match code {
0x01 => KeyboardHidEvent::VolumeUp,
0xff => KeyboardHidEvent::VolumeDown,

_ => KeyboardHidEvent::Unknown,
}
} else {
match code {
0x01 => KeyboardHidEvent::BrightnessUp,
0xff => KeyboardHidEvent::BrightnessDown,

_ => KeyboardHidEvent::Unknown,
}
};

// default to brightness
*self.dial_mode.lock() = DialMode::Brightness;

result
},

[0x03, 0x00, 0x0c, val, _] => {
KeyboardHidEvent::SetBrightness(val)
},

[0x02, 0xe2, 0x00, 0x00, _] => KeyboardHidEvent::MuteDown,
[0x02, 0x00, 0x00, 0x00, _] => KeyboardHidEvent::MuteUp,


_ => KeyboardHidEvent::Unknown,
};

Expand Down
15 changes: 15 additions & 0 deletions eruption/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ pub enum DbusApiEvent {
ProfilesChanged,
ActiveProfileChanged,
ActiveSlotChanged,
BrightnessChanged,
}

/// Spawns the dbus thread and executes it's main loop
Expand All @@ -223,6 +224,8 @@ fn spawn_dbus_thread(
DbusApiEvent::ActiveProfileChanged => dbus.notify_active_profile_changed(),

DbusApiEvent::ActiveSlotChanged => dbus.notify_active_slot_changed(),

DbusApiEvent::BrightnessChanged => dbus.notify_brightness_changed(),
},

// ignore timeout errors
Expand Down Expand Up @@ -1522,6 +1525,8 @@ async fn run_main_loop(
// used to detect changes of the active slot
let mut saved_slot = 0;

let mut saved_brightness = 0;

// used to detect changes to the AFK state
let mut saved_afk_mode = false;

Expand Down Expand Up @@ -1563,6 +1568,16 @@ async fn run_main_loop(
failed_txs.clear();
}

// brightness changed?
let current_brightness = BRIGHTNESS.load(Ordering::SeqCst);
if current_brightness != saved_brightness {
dbus_api_tx
.send(DbusApiEvent::BrightnessChanged)
.unwrap_or_else(|e| error!("Could not send a pending dbus API event: {}", e));

saved_brightness = current_brightness;
}

// user is AFK?
let afk_mode = AFK.load(Ordering::SeqCst);
if afk_mode != saved_afk_mode {
Expand Down
33 changes: 33 additions & 0 deletions eruption/src/scripting/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,15 @@ mod callbacks {

super::FRAME_GENERATION_COUNTER.fetch_add(1, Ordering::SeqCst);
}

pub(crate) fn get_brightness() -> isize {
crate::BRIGHTNESS.load(Ordering::SeqCst)
}

pub(crate) fn set_brightness(val: isize) {
crate::BRIGHTNESS.store(val, Ordering::SeqCst);
super::FRAME_GENERATION_COUNTER.fetch_add(1, Ordering::SeqCst);
}
}

/// Action requests for `run_script`
Expand Down Expand Up @@ -894,6 +903,21 @@ pub fn run_script(
4
}

KeyboardHidEvent::BrightnessDown => {
arg1 = 1;
5
}

KeyboardHidEvent::BrightnessUp => {
arg1 = 0;
5
}

KeyboardHidEvent::SetBrightness(val) => {
arg1 = val;
6
}

_ => {
arg1 = 0;
0
Expand Down Expand Up @@ -1399,6 +1423,15 @@ fn register_support_funcs(lua_ctx: &Lua, keyboard_device: &KeyboardDevice) -> ml
})?;
globals.set("submit_color_map", submit_color_map)?;

let get_brightness = lua_ctx.create_function(move |_, ()| Ok(callbacks::get_brightness()))?;
globals.set("get_brightness", get_brightness)?;

let set_brightness = lua_ctx.create_function(move |_, val: isize| {
callbacks::set_brightness(val);
Ok(())
})?;
globals.set("set_brightness", set_brightness)?;

// finally, register Lua functions supplied by eruption plugins
let plugin_manager = plugin_manager::PLUGIN_MANAGER.read();
let plugins = plugin_manager.get_plugins();
Expand Down

0 comments on commit 9c6a2ef

Please sign in to comment.