Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add playerctl support #108

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
568 changes: 379 additions & 189 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ blight = "0.7.0"
anyhow = "1.0.75"
thiserror = "1.0.49"
async-channel = "2.3.1"
mpris = "2.0.1"
runtime-format = "0.1.3"
strfmt = "0.2.4"
1 change: 1 addition & 0 deletions data/config/backend.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[input]
19 changes: 19 additions & 0 deletions data/config/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[client]
## style file for the OSD
# style = /etc/xdg/swayosd/style.css

## on which height to show the OSD
# top_margin = 0.85

## The maximum volume that can be reached in %
# max_volume = 150

## show percentage on the right of the OSD
# show_percentage = true

## set format for the media player OSD
# playerctl_format = "{artist} - {title}"
## Available values:
## artist, albumArtist, title, album, trackNumber, discNumber, autoRating

[server]
2 changes: 2 additions & 0 deletions data/icons/scalable/status/pause-large-symbolic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions data/icons/scalable/status/play-large-symbolic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions data/icons/scalable/status/playlist-consecutive-symbolic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions data/icons/scalable/status/playlist-shuffle-symbolic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions data/icons/scalable/status/seek-backward-large-symbolic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions data/icons/scalable/status/seek-forward-large-symbolic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions data/icons/scalable/status/stop-large-symbolic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions data/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ style_css = custom_target(
)

message(style_css.full_path())

install_data(['config/config.toml', 'config/backend.toml'],
install_dir : config_path
)
8 changes: 8 additions & 0 deletions data/swayosd.gresource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,13 @@
<file>icons/scalable/status/source-volume-medium-symbolic.svg</file>
<file>icons/scalable/status/source-volume-low-symbolic.svg</file>
<file>icons/scalable/status/source-volume-muted-symbolic.svg</file>

<file>icons/scalable/status/pause-large-symbolic.svg</file>
<file>icons/scalable/status/play-large-symbolic.svg</file>
<file>icons/scalable/status/seek-backward-large-symbolic.svg</file>
<file>icons/scalable/status/seek-forward-large-symbolic.svg</file>
<file>icons/scalable/status/playlist-consecutive-symbolic.svg</file>
<file>icons/scalable/status/playlist-shuffle-symbolic.svg</file>
<file>icons/scalable/status/stop-large-symbolic.svg</file>
</gresource>
</gresources>
2 changes: 1 addition & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ match_block_trailing_comma = false
blank_lines_upper_bound = 1
blank_lines_lower_bound = 0
edition = "2015"
version = "One"
style_edition = "2021"
inline_attribute_width = 0
format_generated_files = true
merge_derives = true
Expand Down
6 changes: 6 additions & 0 deletions src/argtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub enum ArgTypes {
TopMargin = isize::MIN + 1,
MaxVolume = isize::MIN + 2,
CustomIcon = isize::MIN + 3,
Player = isize::MIN + 4,
// Other
None = 0,
CapsLock = 1,
Expand All @@ -23,6 +24,7 @@ pub enum ArgTypes {
NumLock = 10,
ScrollLock = 11,
CustomMessage = 13,
Playerctl = 14,
}

impl fmt::Display for ArgTypes {
Expand All @@ -46,6 +48,8 @@ impl fmt::Display for ArgTypes {
ArgTypes::TopMargin => "TOP-MARGIN",
ArgTypes::CustomMessage => "CUSTOM-MESSAGE",
ArgTypes::CustomIcon => "CUSTOM-ICON",
ArgTypes::Playerctl => "PLAYERCTL",
ArgTypes::Player => "PLAYER",
};
return write!(f, "{}", string);
}
Expand Down Expand Up @@ -73,6 +77,8 @@ impl str::FromStr for ArgTypes {
"TOP-MARGIN" => ArgTypes::TopMargin,
"CUSTOM-MESSAGE" => ArgTypes::CustomMessage,
"CUSTOM-ICON" => ArgTypes::CustomIcon,
"PLAYERCTL" => ArgTypes::Playerctl,
"PLAYER" => ArgTypes::Player,
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 @@ -156,6 +156,16 @@ fn main() -> Result<(), glib::Error> {
"Shows brightness osd and raises or loweres all available sources of brightness device",
Some("raise|lower|(±)number"),
);

// Control players cmdline arg
app.add_main_option(
"playerctl",
glib::Char::from(0),
OptionFlags::NONE,
OptionArg::String,
"Shows Playerctl osd and runs the playerctl command",
Some("play-pause|play|pause|stop|next|prev|shuffle"),
);
app.add_main_option(
"max-volume",
glib::Char::from(0),
Expand All @@ -172,6 +182,14 @@ fn main() -> Result<(), glib::Error> {
"For which device to increase/decrease audio",
Some("Pulseaudio device name (pactl list short sinks|sources)"),
);
app.add_main_option(
"player",
glib::Char::from(0),
OptionFlags::NONE,
OptionArg::String,
"For which player to run the playerctl commands",
Some("auto|all|(playerctl -l)"),
);

app.add_main_option(
"custom-message",
Expand Down
3 changes: 2 additions & 1 deletion src/config/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ use std::path::PathBuf;
#[serde(deny_unknown_fields)]
pub struct ClientConfig {}

#[derive(Deserialize, Default, Debug)]
#[derive(Deserialize, Default, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct ServerConfig {
pub style: Option<PathBuf>,
pub top_margin: Option<f32>,
pub max_volume: Option<u8>,
pub show_percentage: Option<bool>,
pub playerctl_format: Option<String>,
}

#[derive(Deserialize, Default, Debug)]
Expand Down
23 changes: 23 additions & 0 deletions src/global_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ pub(crate) fn handle_application_args(
}
}
}
"playerctl" => {
let value = child.value().str().unwrap_or("");
match value {
"play-pause" | "play" | "pause" | "next" | "prev" | "previous" | "shuffle"
| "stop" => (),
x => {
eprintln!("Unknown Playerctl command: \"{}\"!...", x);
return (HandleLocalStatus::FAILURE, actions);
}
}

(ArgTypes::Playerctl, Some(value.to_string()))
}
"device" => {
let value = match child.value().str() {
Some(v) => v.to_string(),
Expand Down Expand Up @@ -125,6 +138,16 @@ pub(crate) fn handle_application_args(
};
(ArgTypes::CustomIcon, Some(value))
}
"player" => {
let value = match child.value().str() {
Some(v) => v.to_string(),
None => {
eprintln!("--player found but no name given");
return (HandleLocalStatus::FAILURE, actions);
}
};
(ArgTypes::Player, Some(value))
}
"top-margin" => {
let value = child.value().str().unwrap_or("").trim();
match value.parse::<f32>() {
Expand Down
Loading
Loading