Skip to content

Commit

Permalink
Add support for custom messages with icons
Browse files Browse the repository at this point in the history
  • Loading branch information
3akev committed Sep 1, 2024
1 parent d304967 commit 8e45f78
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/argtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub enum ArgTypes {
DeviceName = isize::MIN,
TopMargin = isize::MIN + 1,
MaxVolume = isize::MIN + 2,
CustomIcon = isize::MIN + 4,
// Other
None = 0,
CapsLock = 1,
Expand All @@ -21,6 +22,7 @@ pub enum ArgTypes {
BrightnessSet = 12,
NumLock = 10,
ScrollLock = 11,
CustomMessage = 13,
}

impl fmt::Display for ArgTypes {
Expand All @@ -42,6 +44,8 @@ impl fmt::Display for ArgTypes {
ArgTypes::ScrollLock => "SCROLL-LOCK",
ArgTypes::DeviceName => "DEVICE-NAME",
ArgTypes::TopMargin => "TOP-MARGIN",
ArgTypes::CustomMessage => "CUSTOM-MESSAGE",
ArgTypes::CustomIcon => "CUSTOM-ICON",
};
return write!(f, "{}", string);
}
Expand All @@ -67,6 +71,8 @@ impl str::FromStr for ArgTypes {
"SCROLL-LOCK" => ArgTypes::ScrollLock,
"DEVICE-NAME" => ArgTypes::DeviceName,
"TOP-MARGIN" => ArgTypes::TopMargin,
"CUSTOM-MESSAGE" => ArgTypes::CustomMessage,
"CUSTOM-ICON" => ArgTypes::CustomIcon,
other_type => return Err(other_type.to_owned()),
};
Ok(result)
Expand Down
18 changes: 18 additions & 0 deletions src/client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,24 @@ fn main() -> Result<(), glib::Error> {
Some("Pulseaudio device name (pactl list short sinks|sources)"),
);

app.add_main_option(
"custom-message",
glib::Char::from(0),
OptionFlags::NONE,
OptionArg::String,
"Message to display",
Some("text"),
);

app.add_main_option(
"custom-icon",
glib::Char::from(0),
OptionFlags::NONE,
OptionArg::String,
"Icon to display when using custom-message. Icon name is from Freedesktop specification (https://specifications.freedesktop.org/icon-naming-spec/latest/)",
Some("Icon name"),
);

// Parse args
app.connect_handle_local_options(move |_app, args| {
let variant = args.to_variant();
Expand Down
20 changes: 20 additions & 0 deletions src/global_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ pub(crate) fn handle_application_args(
};
(ArgTypes::DeviceName, Some(value))
}
"custom-message" => {
let value = match child.value().str() {
Some(v) => v.to_string(),
None => {
eprintln!("--custom-message found but no message given");
return (HandleLocalStatus::FAILURE, actions);
}
};
(ArgTypes::CustomMessage, Some(value))
}
"custom-icon" => {
let value = match child.value().str() {
Some(v) => v.to_string(),
None => {
eprintln!("--custom-icon found but no icon given");
return (HandleLocalStatus::FAILURE, actions);
}
};
(ArgTypes::CustomIcon, Some(value))
}
"top-margin" => {
let value = child.value().str().unwrap_or("").trim();
match value.parse::<f32>() {
Expand Down
11 changes: 11 additions & 0 deletions src/server/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,17 @@ impl SwayOSDApplication {
(ArgTypes::DeviceName, name) => {
set_device_name(name.unwrap_or(DEVICE_NAME_DEFAULT.to_string()))
}
(ArgTypes::CustomMessage, message) => {
if let Some(message) = message {
for window in osd_app.windows.borrow().to_owned() {
window.custom_message(message.as_str(), get_icon_name().as_deref());
}
}
reset_icon_name();
}
(ArgTypes::CustomIcon, icon) => {
set_icon_name(icon.unwrap_or(ICON_NAME_DEFAULT.to_string()))
}
(arg_type, data) => {
eprintln!(
"Failed to parse command... Type: {:?}, Data: {:?}",
Expand Down
14 changes: 14 additions & 0 deletions src/server/osd_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,20 @@ impl SwayosdWindow {
self.run_timeout();
}

pub fn custom_message(&self, message: &str, icon_name: Option<&str>) {
self.clear_osd();

if let Some(icon_name) = icon_name {
let icon = self.build_icon_widget(icon_name);
self.container.add(&icon);
}

let label = self.build_text_widget(Some(message));
self.container.add(&label);

self.run_timeout();
}

/// Clear all container children
fn clear_osd(&self) {
for widget in self.container.children() {
Expand Down
16 changes: 16 additions & 0 deletions src/server/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ lazy_static! {
static ref MAX_VOLUME: Mutex<u8> = Mutex::new(PRIV_MAX_VOLUME_DEFAULT);
pub static ref DEVICE_NAME_DEFAULT: &'static str = "default";
static ref DEVICE_NAME: Mutex<Option<String>> = Mutex::new(None);
pub static ref ICON_NAME_DEFAULT: &'static str = "text-x-generic";
static ref ICON_NAME: Mutex<Option<String>> = Mutex::new(None);
pub static ref TOP_MARGIN_DEFAULT: f32 = 0.85_f32;
static ref TOP_MARGIN: Mutex<f32> = Mutex::new(*TOP_MARGIN_DEFAULT);
}
Expand Down Expand Up @@ -77,6 +79,20 @@ pub fn reset_device_name() {
*global_name = None;
}

pub fn get_icon_name() -> Option<String> {
(*ICON_NAME.lock().unwrap()).clone()
}

pub fn set_icon_name(name: String) {
let mut icon_name = ICON_NAME.lock().unwrap();
*icon_name = Some(name);
}

pub fn reset_icon_name() {
let mut icon_name = ICON_NAME.lock().unwrap();
*icon_name = None;
}

pub fn get_key_lock_state(key: KeysLocks, led: Option<String>) -> bool {
const BASE_PATH: &str = "/sys/class/leds";
match fs::read_dir(BASE_PATH) {
Expand Down

0 comments on commit 8e45f78

Please sign in to comment.