Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
015dcd3204 | ||
|
|
1e38719996 | ||
|
|
6dcae66570 | ||
|
|
649b0efb19 | ||
|
|
023c2fb118 | ||
|
|
ea57f5e18d | ||
|
|
53142d1bea | ||
|
|
7e0f2cad1c |
3
.github/workflows/deploy.yml
vendored
3
.github/workflows/deploy.yml
vendored
@@ -17,6 +17,9 @@ jobs:
|
|||||||
toolchain: stable
|
toolchain: stable
|
||||||
override: true
|
override: true
|
||||||
|
|
||||||
|
- name: Install build deps
|
||||||
|
run: sudo apt install libgtk-3-dev libgtk-layer-shell-dev
|
||||||
|
|
||||||
- name: Update CHANGELOG
|
- name: Update CHANGELOG
|
||||||
id: changelog
|
id: changelog
|
||||||
uses: Requarks/changelog-action@v1
|
uses: Requarks/changelog-action@v1
|
||||||
|
|||||||
18
CHANGELOG.md
Normal file
18
CHANGELOG.md
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# Changelog
|
||||||
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## [v0.4.0] - 2022-08-22
|
||||||
|
### :sparkles: New Features
|
||||||
|
- [`ab8f7ec`](https://github.com/JakeStanger/ironbar/commit/ab8f7ecfc8fa4b96fce78518af75794641950140) - logging support and proper error handling *(commit by [@JakeStanger](https://github.com/JakeStanger))*
|
||||||
|
|
||||||
|
### :bug: Bug Fixes
|
||||||
|
- [`f2ee2df`](https://github.com/JakeStanger/ironbar/commit/f2ee2dfe7a0f5575d0c3ec09644ca990b088cd85) - error when using with `swaybar_command` *(commit by [@JakeStanger](https://github.com/JakeStanger))*
|
||||||
|
|
||||||
|
### :wrench: Chores
|
||||||
|
- [`1d7c377`](https://github.com/JakeStanger/ironbar/commit/1d7c3772e4b97c7198043cb55fe9c71695a211ab) - **release**: v0.4.0 *(commit by [@JakeStanger](https://github.com/JakeStanger))*
|
||||||
|
|
||||||
|
|
||||||
|
[v0.4.0]: https://github.com/JakeStanger/ironbar/compare/v0.3.0...v0.4.0
|
||||||
3
Cargo.lock
generated
3
Cargo.lock
generated
@@ -1127,7 +1127,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ironbar"
|
name = "ironbar"
|
||||||
version = "0.4.0"
|
version = "0.5.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
@@ -1139,6 +1139,7 @@ dependencies = [
|
|||||||
"gtk",
|
"gtk",
|
||||||
"gtk-layer-shell",
|
"gtk-layer-shell",
|
||||||
"ksway",
|
"ksway",
|
||||||
|
"lazy_static",
|
||||||
"mpd_client",
|
"mpd_client",
|
||||||
"notify",
|
"notify",
|
||||||
"regex",
|
"regex",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ironbar"
|
name = "ironbar"
|
||||||
version = "0.4.0"
|
version = "0.5.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
description = "Customisable wlroots/sway bar"
|
description = "Customisable wlroots/sway bar"
|
||||||
@@ -25,6 +25,7 @@ serde_json = "1.0.82"
|
|||||||
serde_yaml = "0.9.4"
|
serde_yaml = "0.9.4"
|
||||||
toml = "0.5.9"
|
toml = "0.5.9"
|
||||||
cornfig = "0.2.0"
|
cornfig = "0.2.0"
|
||||||
|
lazy_static = "1.4.0"
|
||||||
regex = "1.6.0"
|
regex = "1.6.0"
|
||||||
stray = "0.1.1"
|
stray = "0.1.1"
|
||||||
dirs = "4.0.0"
|
dirs = "4.0.0"
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ use color_eyre::Result;
|
|||||||
use gtk::gdk::Monitor;
|
use gtk::gdk::Monitor;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use gtk::{Application, ApplicationWindow, Orientation};
|
use gtk::{Application, ApplicationWindow, Orientation};
|
||||||
|
use tracing::{debug, info};
|
||||||
|
|
||||||
pub fn create_bar(
|
pub fn create_bar(
|
||||||
app: &Application,
|
app: &Application,
|
||||||
@@ -41,10 +42,12 @@ pub fn create_bar(
|
|||||||
win.add(&content);
|
win.add(&content);
|
||||||
|
|
||||||
win.connect_destroy_event(|_, _| {
|
win.connect_destroy_event(|_, _| {
|
||||||
|
info!("Shutting down");
|
||||||
gtk::main_quit();
|
gtk::main_quit();
|
||||||
Inhibit(false)
|
Inhibit(false)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
debug!("Showing bar");
|
||||||
win.show_all();
|
win.show_all();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -104,6 +107,7 @@ fn add_modules(content: >k::Box, modules: Vec<ModuleConfig>, info: &ModuleInfo
|
|||||||
let widget = $module.into_widget(&info)?;
|
let widget = $module.into_widget(&info)?;
|
||||||
widget.set_widget_name($name);
|
widget.set_widget_name($name);
|
||||||
content.add(&widget);
|
content.add(&widget);
|
||||||
|
debug!("Added module of type {}", $name);
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
23
src/main.rs
23
src/main.rs
@@ -11,14 +11,13 @@ mod sway;
|
|||||||
use crate::bar::create_bar;
|
use crate::bar::create_bar;
|
||||||
use crate::config::{Config, MonitorConfig};
|
use crate::config::{Config, MonitorConfig};
|
||||||
use crate::style::load_css;
|
use crate::style::load_css;
|
||||||
use crate::sway::{get_client_error, SwayOutput};
|
use crate::sway::{get_client, SwayOutput};
|
||||||
use color_eyre::eyre::Result;
|
use color_eyre::eyre::Result;
|
||||||
use color_eyre::Report;
|
use color_eyre::Report;
|
||||||
use dirs::config_dir;
|
use dirs::config_dir;
|
||||||
use gtk::gdk::Display;
|
use gtk::gdk::Display;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use gtk::Application;
|
use gtk::Application;
|
||||||
use ksway::client::Client;
|
|
||||||
use ksway::IpcCommand;
|
use ksway::IpcCommand;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
@@ -97,24 +96,30 @@ async fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn create_bars(app: &Application, display: &Display, config: &Config) -> Result<()> {
|
fn create_bars(app: &Application, display: &Display, config: &Config) -> Result<()> {
|
||||||
let mut sway_client = match Client::connect() {
|
let outputs = {
|
||||||
Ok(client) => Ok(client),
|
let sway = get_client();
|
||||||
Err(err) => Err(get_client_error(err)),
|
let mut sway = sway.lock().expect("Failed to get lock on Sway IPC client");
|
||||||
}?;
|
|
||||||
|
|
||||||
let outputs = match sway_client.ipc(IpcCommand::GetOutputs) {
|
let outputs = sway.ipc(IpcCommand::GetOutputs);
|
||||||
Ok(outputs) => Ok(outputs),
|
|
||||||
Err(err) => Err(get_client_error(err)),
|
match outputs {
|
||||||
|
Ok(outputs) => Ok(outputs),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
let outputs = serde_json::from_slice::<Vec<SwayOutput>>(&outputs)?;
|
let outputs = serde_json::from_slice::<Vec<SwayOutput>>(&outputs)?;
|
||||||
|
|
||||||
|
debug!("Received {} outputs from Sway IPC", outputs.len());
|
||||||
|
|
||||||
let num_monitors = display.n_monitors();
|
let num_monitors = display.n_monitors();
|
||||||
|
|
||||||
for i in 0..num_monitors {
|
for i in 0..num_monitors {
|
||||||
let monitor = display.monitor(i).ok_or_else(|| Report::msg("GTK and Sway are reporting a different number of outputs - this is a severe bug and should never happen"))?;
|
let monitor = display.monitor(i).ok_or_else(|| Report::msg("GTK and Sway are reporting a different number of outputs - this is a severe bug and should never happen"))?;
|
||||||
let monitor_name = &outputs.get(i as usize).ok_or_else(|| Report::msg("GTK and Sway are reporting a different set of outputs - this is a severe bug and should never happen"))?.name;
|
let monitor_name = &outputs.get(i as usize).ok_or_else(|| Report::msg("GTK and Sway are reporting a different set of outputs - this is a severe bug and should never happen"))?.name;
|
||||||
|
|
||||||
|
info!("Creating bar on '{}'", monitor_name);
|
||||||
|
|
||||||
config.monitors.as_ref().map_or_else(
|
config.monitors.as_ref().map_or_else(
|
||||||
|| create_bar(app, &monitor, monitor_name, config.clone()),
|
|| create_bar(app, &monitor, monitor_name, config.clone()),
|
||||||
|config| {
|
|config| {
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
use crate::icon;
|
use crate::icon;
|
||||||
use crate::modules::{Module, ModuleInfo};
|
use crate::modules::{Module, ModuleInfo};
|
||||||
use crate::sway::{SwayClient, WindowEvent};
|
use crate::sway::get_client;
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
use glib::Continue;
|
use glib::Continue;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use gtk::{IconTheme, Image, Label, Orientation};
|
use gtk::{IconTheme, Image, Label, Orientation};
|
||||||
use ksway::IpcEvent;
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use tokio::task::spawn_blocking;
|
use tokio::task::spawn_blocking;
|
||||||
use tracing::error;
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Clone)]
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
pub struct FocusedModule {
|
pub struct FocusedModule {
|
||||||
@@ -42,42 +40,39 @@ impl Module<gtk::Box> for FocusedModule {
|
|||||||
container.add(&icon);
|
container.add(&icon);
|
||||||
container.add(&label);
|
container.add(&label);
|
||||||
|
|
||||||
let mut sway = SwayClient::connect()?;
|
|
||||||
|
|
||||||
let srx = sway.subscribe(vec![IpcEvent::Window])?;
|
|
||||||
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
|
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
|
||||||
|
|
||||||
let focused = sway
|
let focused = {
|
||||||
.get_open_windows()?
|
let sway = get_client();
|
||||||
.into_iter()
|
let mut sway = sway.lock().expect("Failed to get lock on Sway IPC client");
|
||||||
.find(|node| node.focused);
|
sway.get_open_windows()?
|
||||||
|
.into_iter()
|
||||||
|
.find(|node| node.focused)
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(focused) = focused {
|
if let Some(focused) = focused {
|
||||||
tx.send(focused)?;
|
tx.send(focused)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
spawn_blocking(move || loop {
|
spawn_blocking(move || {
|
||||||
while let Ok((_, payload)) = srx.try_recv() {
|
let srx = {
|
||||||
match serde_json::from_slice::<WindowEvent>(&payload) {
|
let sway = get_client();
|
||||||
Ok(payload) => {
|
let mut sway = sway.lock().expect("Failed to get lock on Sway IPC client");
|
||||||
let update = match payload.change.as_str() {
|
sway.subscribe_window()
|
||||||
"focus" => true,
|
};
|
||||||
"title" => payload.container.focused,
|
|
||||||
_ => false,
|
|
||||||
};
|
|
||||||
|
|
||||||
if update {
|
while let Ok(payload) = srx.recv() {
|
||||||
tx.send(payload.container)
|
let update = match payload.change.as_str() {
|
||||||
.expect("Failed to sendf focus update");
|
"focus" => true,
|
||||||
}
|
"title" => payload.container.focused,
|
||||||
}
|
_ => false,
|
||||||
Err(err) => error!("{:?}", err),
|
};
|
||||||
|
|
||||||
|
if update {
|
||||||
|
tx.send(payload.container)
|
||||||
|
.expect("Failed to sendf focus update");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(err) = sway.poll() {
|
|
||||||
error!("{:?}", err);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,17 +5,15 @@ use crate::collection::Collection;
|
|||||||
use crate::modules::launcher::item::{ButtonConfig, LauncherItem, LauncherWindow, OpenState};
|
use crate::modules::launcher::item::{ButtonConfig, LauncherItem, LauncherWindow, OpenState};
|
||||||
use crate::modules::launcher::popup::Popup;
|
use crate::modules::launcher::popup::Popup;
|
||||||
use crate::modules::{Module, ModuleInfo};
|
use crate::modules::{Module, ModuleInfo};
|
||||||
use crate::sway::{SwayClient, SwayNode, WindowEvent};
|
use crate::sway::{get_client, SwayNode};
|
||||||
use color_eyre::{Report, Result};
|
use color_eyre::{Report, Result};
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use gtk::{IconTheme, Orientation};
|
use gtk::{IconTheme, Orientation};
|
||||||
use ksway::IpcEvent;
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use tokio::spawn;
|
use tokio::spawn;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use tokio::task::spawn_blocking;
|
use tokio::task::spawn_blocking;
|
||||||
use tracing::error;
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Clone)]
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
pub struct LauncherModule {
|
pub struct LauncherModule {
|
||||||
@@ -210,8 +208,6 @@ impl Module<gtk::Box> for LauncherModule {
|
|||||||
icon_theme.set_custom_theme(Some(&theme));
|
icon_theme.set_custom_theme(Some(&theme));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut sway = SwayClient::connect()?;
|
|
||||||
|
|
||||||
let popup = Popup::new(
|
let popup = Popup::new(
|
||||||
"popup-launcher",
|
"popup-launcher",
|
||||||
info.app,
|
info.app,
|
||||||
@@ -237,28 +233,28 @@ impl Module<gtk::Box> for LauncherModule {
|
|||||||
button_config,
|
button_config,
|
||||||
);
|
);
|
||||||
|
|
||||||
let open_windows = sway.get_open_windows()?;
|
let open_windows = {
|
||||||
|
let sway = get_client();
|
||||||
|
let mut sway = sway.lock().expect("Failed to get lock on Sway IPC client");
|
||||||
|
sway.get_open_windows()
|
||||||
|
}?;
|
||||||
|
|
||||||
for window in open_windows {
|
for window in open_windows {
|
||||||
launcher.add_window(window);
|
launcher.add_window(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
let srx = sway.subscribe(vec![IpcEvent::Window])?;
|
|
||||||
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
|
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
|
||||||
|
|
||||||
spawn_blocking(move || loop {
|
spawn_blocking(move || {
|
||||||
while let Ok((_, payload)) = srx.try_recv() {
|
let srx = {
|
||||||
match serde_json::from_slice::<WindowEvent>(&payload) {
|
let sway = get_client();
|
||||||
Ok(payload) => {
|
let mut sway = sway.lock().expect("Failed to get lock on Sway IPC client");
|
||||||
tx.send(payload)
|
sway.subscribe_window()
|
||||||
.expect("Failed to send window event payload");
|
};
|
||||||
}
|
|
||||||
Err(err) => error!("{:?}", err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Err(err) = sway.poll() {
|
while let Ok(payload) = srx.recv() {
|
||||||
error!("{:?}", err);
|
tx.send(payload)
|
||||||
|
.expect("Failed to send window event payload");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -278,14 +274,15 @@ impl Module<gtk::Box> for LauncherModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
spawn(async move {
|
spawn(async move {
|
||||||
let mut sway = SwayClient::connect()?;
|
let sway = get_client();
|
||||||
|
|
||||||
while let Some(event) = ui_rx.recv().await {
|
while let Some(event) = ui_rx.recv().await {
|
||||||
let selector = match event {
|
let selector = match event {
|
||||||
FocusEvent::AppId(app_id) => format!("[app_id={}]", app_id),
|
FocusEvent::AppId(app_id) => format!("[app_id={}]", app_id),
|
||||||
FocusEvent::Class(class) => format!("[class={}]", class),
|
FocusEvent::Class(class) => format!("[class={}]", class),
|
||||||
FocusEvent::ConId(id) => format!("[con_id={}]", id),
|
FocusEvent::ConId(id) => format!("[con_id={}]", id),
|
||||||
};
|
};
|
||||||
|
let mut sway = sway.lock().expect("Failed to get lock on Sway IPC client");
|
||||||
sway.run(format!("{} focus", selector))?;
|
sway.run(format!("{} focus", selector))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,55 +1,67 @@
|
|||||||
|
use lazy_static::lazy_static;
|
||||||
use mpd_client::commands::responses::Status;
|
use mpd_client::commands::responses::Status;
|
||||||
use mpd_client::raw::MpdProtocolError;
|
use mpd_client::raw::MpdProtocolError;
|
||||||
use mpd_client::{Client, Connection};
|
use mpd_client::{Client, Connection};
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::net::{TcpStream, UnixStream};
|
use tokio::net::{TcpStream, UnixStream};
|
||||||
use tokio::spawn;
|
use tokio::sync::Mutex;
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
|
|
||||||
pub async fn wait_for_connection(
|
lazy_static! {
|
||||||
hosts: Vec<String>,
|
static ref CLIENTS: Arc<Mutex<HashMap<String, Arc<Client>>>> =
|
||||||
|
Arc::new(Mutex::new(HashMap::new()));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_connection(host: &str) -> Option<Arc<Client>> {
|
||||||
|
let mut clients = CLIENTS.lock().await;
|
||||||
|
|
||||||
|
match clients.get(host) {
|
||||||
|
Some(client) => Some(Arc::clone(client)),
|
||||||
|
None => {
|
||||||
|
let client = wait_for_connection(host, Duration::from_secs(5), None).await?;
|
||||||
|
let client = Arc::new(client);
|
||||||
|
clients.insert(host.to_string(), Arc::clone(&client));
|
||||||
|
Some(client)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn wait_for_connection(
|
||||||
|
host: &str,
|
||||||
interval: Duration,
|
interval: Duration,
|
||||||
max_retries: Option<usize>,
|
max_retries: Option<usize>,
|
||||||
) -> Option<Client> {
|
) -> Option<Client> {
|
||||||
let mut retries = 0;
|
let mut retries = 0;
|
||||||
|
let max_retries = max_retries.unwrap_or(usize::MAX);
|
||||||
|
|
||||||
spawn(async move {
|
loop {
|
||||||
let max_retries = max_retries.unwrap_or(usize::MAX);
|
if retries == max_retries {
|
||||||
loop {
|
break None;
|
||||||
if retries == max_retries {
|
|
||||||
break None;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(conn) = try_get_mpd_conn(&hosts).await {
|
|
||||||
break Some(conn.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
retries += 1;
|
|
||||||
sleep(interval).await;
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.await
|
if let Some(conn) = try_get_mpd_conn(host).await {
|
||||||
.expect("Error occurred while handling tasks")
|
break Some(conn.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
retries += 1;
|
||||||
|
sleep(interval).await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Cycles through each MPD host and
|
/// Cycles through each MPD host and
|
||||||
/// returns the first one which connects,
|
/// returns the first one which connects,
|
||||||
/// or none if there are none
|
/// or none if there are none
|
||||||
async fn try_get_mpd_conn(hosts: &[String]) -> Option<Connection> {
|
async fn try_get_mpd_conn(host: &str) -> Option<Connection> {
|
||||||
for host in hosts {
|
let connection = if is_unix_socket(host) {
|
||||||
let connection = if is_unix_socket(host) {
|
connect_unix(host).await
|
||||||
connect_unix(host).await
|
} else {
|
||||||
} else {
|
connect_tcp(host).await
|
||||||
connect_tcp(host).await
|
};
|
||||||
};
|
|
||||||
|
|
||||||
if let Ok(connection) = connection {
|
connection.ok()
|
||||||
return Some(connection);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_unix_socket(host: &str) -> bool {
|
fn is_unix_socket(host: &str) -> bool {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ mod client;
|
|||||||
mod popup;
|
mod popup;
|
||||||
|
|
||||||
use self::popup::Popup;
|
use self::popup::Popup;
|
||||||
use crate::modules::mpd::client::{get_duration, get_elapsed, wait_for_connection};
|
use crate::modules::mpd::client::{get_connection, get_duration, get_elapsed};
|
||||||
use crate::modules::mpd::popup::{MpdPopup, PopupEvent};
|
use crate::modules::mpd::popup::{MpdPopup, PopupEvent};
|
||||||
use crate::modules::{Module, ModuleInfo};
|
use crate::modules::{Module, ModuleInfo};
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
@@ -120,7 +120,7 @@ impl Module<Button> for MpdModule {
|
|||||||
let host = self.host.clone();
|
let host = self.host.clone();
|
||||||
let host2 = self.host.clone();
|
let host2 = self.host.clone();
|
||||||
spawn(async move {
|
spawn(async move {
|
||||||
let client = wait_for_connection(vec![host], Duration::from_secs(1), None)
|
let client = get_connection(&host)
|
||||||
.await
|
.await
|
||||||
.expect("Unexpected error when trying to connect to MPD server");
|
.expect("Unexpected error when trying to connect to MPD server");
|
||||||
|
|
||||||
@@ -145,7 +145,7 @@ impl Module<Button> for MpdModule {
|
|||||||
});
|
});
|
||||||
|
|
||||||
spawn(async move {
|
spawn(async move {
|
||||||
let client = wait_for_connection(vec![host2], Duration::from_secs(1), None)
|
let client = get_connection(&host2)
|
||||||
.await
|
.await
|
||||||
.expect("Unexpected error when trying to connect to MPD server");
|
.expect("Unexpected error when trying to connect to MPD server");
|
||||||
|
|
||||||
@@ -242,4 +242,4 @@ impl MpdModule {
|
|||||||
};
|
};
|
||||||
s.unwrap_or_default().to_string()
|
s.unwrap_or_default().to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
use crate::modules::{Module, ModuleInfo};
|
use crate::modules::{Module, ModuleInfo};
|
||||||
use crate::sway::{SwayClient, Workspace, WorkspaceEvent};
|
use crate::sway::{get_client, Workspace};
|
||||||
use color_eyre::{Report, Result};
|
use color_eyre::{Report, Result};
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use gtk::{Button, Orientation};
|
use gtk::{Button, Orientation};
|
||||||
use ksway::{IpcCommand, IpcEvent};
|
use ksway::IpcCommand;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use tokio::spawn;
|
use tokio::spawn;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use tokio::task::spawn_blocking;
|
use tokio::task::spawn_blocking;
|
||||||
use tracing::error;
|
use tracing::{debug, trace};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Clone)]
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
pub struct WorkspacesModule {
|
pub struct WorkspacesModule {
|
||||||
@@ -47,17 +47,19 @@ impl Workspace {
|
|||||||
|
|
||||||
impl Module<gtk::Box> for WorkspacesModule {
|
impl Module<gtk::Box> for WorkspacesModule {
|
||||||
fn into_widget(self, info: &ModuleInfo) -> Result<gtk::Box> {
|
fn into_widget(self, info: &ModuleInfo) -> Result<gtk::Box> {
|
||||||
let mut sway = SwayClient::connect()?;
|
|
||||||
|
|
||||||
let container = gtk::Box::new(Orientation::Horizontal, 0);
|
let container = gtk::Box::new(Orientation::Horizontal, 0);
|
||||||
|
|
||||||
let workspaces = {
|
let workspaces = {
|
||||||
|
trace!("Getting current workspaces");
|
||||||
|
let sway = get_client();
|
||||||
|
let mut sway = sway.lock().expect("Failed to get lock on Sway IPC client");
|
||||||
let raw = sway.ipc(IpcCommand::GetWorkspaces)?;
|
let raw = sway.ipc(IpcCommand::GetWorkspaces)?;
|
||||||
let workspaces = serde_json::from_slice::<Vec<Workspace>>(&raw)?;
|
let workspaces = serde_json::from_slice::<Vec<Workspace>>(&raw)?;
|
||||||
|
|
||||||
if self.all_monitors {
|
if self.all_monitors {
|
||||||
workspaces
|
workspaces
|
||||||
} else {
|
} else {
|
||||||
|
trace!("Filtering workspaces to current monitor only");
|
||||||
workspaces
|
workspaces
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|workspace| workspace.output == info.output_name)
|
.filter(|workspace| workspace.output == info.output_name)
|
||||||
@@ -71,32 +73,35 @@ impl Module<gtk::Box> for WorkspacesModule {
|
|||||||
|
|
||||||
let (ui_tx, mut ui_rx) = mpsc::channel(32);
|
let (ui_tx, mut ui_rx) = mpsc::channel(32);
|
||||||
|
|
||||||
|
trace!("Creating workspace buttons");
|
||||||
for workspace in workspaces {
|
for workspace in workspaces {
|
||||||
let item = workspace.as_button(&name_map, &ui_tx);
|
let item = workspace.as_button(&name_map, &ui_tx);
|
||||||
container.add(&item);
|
container.add(&item);
|
||||||
button_map.insert(workspace.name, item);
|
button_map.insert(workspace.name, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
let srx = sway.subscribe(vec![IpcEvent::Workspace])?;
|
|
||||||
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
|
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
|
||||||
|
|
||||||
spawn_blocking(move || loop {
|
spawn_blocking(move || {
|
||||||
while let Ok((_, payload)) = srx.try_recv() {
|
trace!("Starting workspace event listener task");
|
||||||
match serde_json::from_slice::<WorkspaceEvent>(&payload) {
|
let srx = {
|
||||||
Ok(payload) => tx.send(payload).expect("Failed to send workspace event"),
|
let sway = get_client();
|
||||||
Err(err) => error!("{:?}", err),
|
let mut sway = sway.lock().expect("Failed to get lock on Sway IPC client");
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Err(err) = sway.poll() {
|
sway.subscribe_workspace()
|
||||||
error!("{:?}", err);
|
};
|
||||||
|
|
||||||
|
while let Ok(payload) = srx.recv() {
|
||||||
|
tx.send(payload).expect("Failed to send workspace event");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
{
|
{
|
||||||
|
trace!("Setting up sway event handler");
|
||||||
let menubar = container.clone();
|
let menubar = container.clone();
|
||||||
let output_name = info.output_name.to_string();
|
let output_name = info.output_name.to_string();
|
||||||
rx.attach(None, move |event| {
|
rx.attach(None, move |event| {
|
||||||
|
debug!("Received workspace event {:?}", event);
|
||||||
match event.change.as_str() {
|
match event.change.as_str() {
|
||||||
"focus" => {
|
"focus" => {
|
||||||
let old = event.old.and_then(|old| button_map.get(&old.name));
|
let old = event.old.and_then(|old| button_map.get(&old.name));
|
||||||
@@ -108,6 +113,8 @@ impl Module<gtk::Box> for WorkspacesModule {
|
|||||||
if let Some(new) = new {
|
if let Some(new) = new {
|
||||||
new.style_context().add_class("focused");
|
new.style_context().add_class("focused");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trace!("{:?} {:?}", old, new);
|
||||||
}
|
}
|
||||||
"init" => {
|
"init" => {
|
||||||
if let Some(workspace) = event.current {
|
if let Some(workspace) = event.current {
|
||||||
@@ -120,6 +127,21 @@ impl Module<gtk::Box> for WorkspacesModule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
"move" => {
|
||||||
|
if let Some(workspace) = event.current {
|
||||||
|
if !self.all_monitors {
|
||||||
|
if workspace.output == output_name {
|
||||||
|
let item = workspace.as_button(&name_map, &ui_tx);
|
||||||
|
|
||||||
|
item.show();
|
||||||
|
menubar.add(&item);
|
||||||
|
button_map.insert(workspace.name, item);
|
||||||
|
} else if let Some(item) = button_map.get(&workspace.name) {
|
||||||
|
menubar.remove(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
"empty" => {
|
"empty" => {
|
||||||
if let Some(workspace) = event.current {
|
if let Some(workspace) = event.current {
|
||||||
if let Some(item) = button_map.get(&workspace.name) {
|
if let Some(item) = button_map.get(&workspace.name) {
|
||||||
@@ -135,8 +157,12 @@ impl Module<gtk::Box> for WorkspacesModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
spawn(async move {
|
spawn(async move {
|
||||||
let mut sway = SwayClient::connect()?;
|
trace!("Setting up UI event handler");
|
||||||
|
let sway = get_client();
|
||||||
while let Some(name) = ui_rx.recv().await {
|
while let Some(name) = ui_rx.recv().await {
|
||||||
|
let mut sway = sway
|
||||||
|
.lock()
|
||||||
|
.expect("Failed to get write lock on Sway IPC client");
|
||||||
sway.run(format!("workspace {}", name))?;
|
sway.run(format!("workspace {}", name))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
159
src/sway/mod.rs
159
src/sway/mod.rs
@@ -1,17 +1,22 @@
|
|||||||
use color_eyre::{Report, Result};
|
use color_eyre::{Report, Result};
|
||||||
|
use crossbeam_channel::Receiver;
|
||||||
use ksway::{Error, IpcCommand, IpcEvent};
|
use ksway::{Error, IpcCommand, IpcEvent};
|
||||||
|
use lazy_static::lazy_static;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
use tokio::spawn;
|
||||||
|
use tracing::{debug, info, trace};
|
||||||
|
|
||||||
pub mod node;
|
pub mod node;
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct WorkspaceEvent {
|
pub struct WorkspaceEvent {
|
||||||
pub change: String,
|
pub change: String,
|
||||||
pub old: Option<Workspace>,
|
pub old: Option<Workspace>,
|
||||||
pub current: Option<Workspace>,
|
pub current: Option<Workspace>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct Workspace {
|
pub struct Workspace {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub focused: bool,
|
pub focused: bool,
|
||||||
@@ -19,13 +24,13 @@ pub struct Workspace {
|
|||||||
pub output: String,
|
pub output: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
pub struct WindowEvent {
|
pub struct WindowEvent {
|
||||||
pub change: String,
|
pub change: String,
|
||||||
pub container: SwayNode,
|
pub container: SwayNode,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
pub struct SwayNode {
|
pub struct SwayNode {
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
pub node_type: String,
|
pub node_type: String,
|
||||||
@@ -40,7 +45,7 @@ pub struct SwayNode {
|
|||||||
pub window_properties: Option<WindowProperties>,
|
pub window_properties: Option<WindowProperties>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
pub struct WindowProperties {
|
pub struct WindowProperties {
|
||||||
pub class: Option<String>,
|
pub class: Option<String>,
|
||||||
}
|
}
|
||||||
@@ -52,49 +57,111 @@ pub struct SwayOutput {
|
|||||||
|
|
||||||
pub struct SwayClient {
|
pub struct SwayClient {
|
||||||
client: ksway::Client,
|
client: ksway::Client,
|
||||||
|
|
||||||
|
workspace_bc: Arc<Mutex<UnboundedBroadcast<WorkspaceEvent>>>,
|
||||||
|
window_bc: Arc<Mutex<UnboundedBroadcast<WindowEvent>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SwayClient {
|
impl SwayClient {
|
||||||
pub(crate) fn run(&mut self, cmd: String) -> Result<Vec<u8>> {
|
fn connect() -> Result<Self> {
|
||||||
match self.client.run(cmd) {
|
|
||||||
Ok(res) => Ok(res),
|
|
||||||
Err(err) => Err(get_client_error(err)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SwayClient {
|
|
||||||
pub fn connect() -> Result<Self> {
|
|
||||||
let client = match ksway::Client::connect() {
|
let client = match ksway::Client::connect() {
|
||||||
Ok(client) => Ok(client),
|
Ok(client) => Ok(client),
|
||||||
Err(err) => Err(get_client_error(err)),
|
Err(err) => Err(get_client_error(err)),
|
||||||
}?;
|
}?;
|
||||||
|
info!("Sway IPC client connected");
|
||||||
|
|
||||||
Ok(Self { client })
|
let workspace_bc = Arc::new(Mutex::new(UnboundedBroadcast::new()));
|
||||||
|
let window_bc = Arc::new(Mutex::new(UnboundedBroadcast::new()));
|
||||||
|
|
||||||
|
let workspace_bc2 = workspace_bc.clone();
|
||||||
|
let window_bc2 = window_bc.clone();
|
||||||
|
spawn(async move {
|
||||||
|
let mut sub_client = match ksway::Client::connect() {
|
||||||
|
Ok(client) => Ok(client),
|
||||||
|
Err(err) => Err(get_client_error(err)),
|
||||||
|
}
|
||||||
|
.expect("Failed to connect to Sway IPC server");
|
||||||
|
info!("Sway IPC subscription client connected");
|
||||||
|
|
||||||
|
let event_types = vec![IpcEvent::Window, IpcEvent::Workspace];
|
||||||
|
let rx = match sub_client.subscribe(event_types) {
|
||||||
|
Ok(res) => Ok(res),
|
||||||
|
Err(err) => Err(get_client_error(err)),
|
||||||
|
}
|
||||||
|
.expect("Failed to subscribe to Sway IPC server");
|
||||||
|
|
||||||
|
loop {
|
||||||
|
while let Ok((ev_type, payload)) = rx.try_recv() {
|
||||||
|
debug!("Received sway event {:?}", ev_type);
|
||||||
|
match ev_type {
|
||||||
|
IpcEvent::Workspace => {
|
||||||
|
let json = serde_json::from_slice::<WorkspaceEvent>(&payload).expect(
|
||||||
|
"Received invalid workspace event payload from Sway IPC server",
|
||||||
|
);
|
||||||
|
workspace_bc
|
||||||
|
.lock()
|
||||||
|
.expect("Failed to get lock on workspace event bus")
|
||||||
|
.send(json)
|
||||||
|
.expect("Failed to broadcast workspace event");
|
||||||
|
}
|
||||||
|
IpcEvent::Window => {
|
||||||
|
let json = serde_json::from_slice::<WindowEvent>(&payload).expect(
|
||||||
|
"Received invalid window event payload from Sway IPC server",
|
||||||
|
);
|
||||||
|
window_bc
|
||||||
|
.lock()
|
||||||
|
.expect("Failed to get lock on window event bus")
|
||||||
|
.send(json)
|
||||||
|
.expect("Failed to broadcast window event");
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
match sub_client.poll() {
|
||||||
|
Ok(()) => Ok(()),
|
||||||
|
Err(err) => Err(get_client_error(err)),
|
||||||
|
}
|
||||||
|
.expect("Failed to poll Sway IPC client");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
client,
|
||||||
|
workspace_bc: workspace_bc2,
|
||||||
|
window_bc: window_bc2,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ipc(&mut self, command: IpcCommand) -> Result<Vec<u8>> {
|
pub fn ipc(&mut self, command: IpcCommand) -> Result<Vec<u8>> {
|
||||||
|
debug!("Sending command: {:?}", command);
|
||||||
match self.client.ipc(command) {
|
match self.client.ipc(command) {
|
||||||
Ok(res) => Ok(res),
|
Ok(res) => Ok(res),
|
||||||
Err(err) => Err(get_client_error(err)),
|
Err(err) => Err(get_client_error(err)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subscribe(
|
pub(crate) fn run(&mut self, cmd: String) -> Result<Vec<u8>> {
|
||||||
&mut self,
|
debug!("Sending command: {}", cmd);
|
||||||
event_types: Vec<IpcEvent>,
|
match self.client.run(cmd) {
|
||||||
) -> Result<crossbeam_channel::Receiver<(IpcEvent, Vec<u8>)>> {
|
|
||||||
match self.client.subscribe(event_types) {
|
|
||||||
Ok(res) => Ok(res),
|
Ok(res) => Ok(res),
|
||||||
Err(err) => Err(get_client_error(err)),
|
Err(err) => Err(get_client_error(err)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn poll(&mut self) -> Result<()> {
|
pub fn subscribe_workspace(&mut self) -> Receiver<WorkspaceEvent> {
|
||||||
match self.client.poll() {
|
trace!("Adding new workspace subscriber");
|
||||||
Ok(()) => Ok(()),
|
self.workspace_bc
|
||||||
Err(err) => Err(get_client_error(err)),
|
.lock()
|
||||||
}
|
.expect("Failed to get lock on workspace event bus")
|
||||||
|
.subscribe()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn subscribe_window(&mut self) -> Receiver<WindowEvent> {
|
||||||
|
trace!("Adding new window subscriber");
|
||||||
|
self.window_bc
|
||||||
|
.lock()
|
||||||
|
.expect("Failed to get lock on window event bus")
|
||||||
|
.subscribe()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,3 +174,43 @@ pub fn get_client_error(error: Error) -> Report {
|
|||||||
Error::Io(err) => Report::new(err),
|
Error::Io(err) => Report::new(err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref CLIENT: Arc<Mutex<SwayClient>> = {
|
||||||
|
let client = SwayClient::connect();
|
||||||
|
match client {
|
||||||
|
Ok(client) => Arc::new(Mutex::new(client)),
|
||||||
|
Err(err) => panic!("{:?}", err),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_client() -> Arc<Mutex<SwayClient>> {
|
||||||
|
Arc::clone(&CLIENT)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct UnboundedBroadcast<T> {
|
||||||
|
channels: Vec<crossbeam_channel::Sender<T>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: 'static + Clone + Send + Sync> UnboundedBroadcast<T> {
|
||||||
|
pub const fn new() -> Self {
|
||||||
|
Self { channels: vec![] }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn subscribe(&mut self) -> Receiver<T> {
|
||||||
|
let (tx, rx) = crossbeam_channel::unbounded();
|
||||||
|
|
||||||
|
self.channels.push(tx);
|
||||||
|
|
||||||
|
rx
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn send(&self, message: T) -> Result<(), crossbeam_channel::SendError<T>> {
|
||||||
|
for c in &self.channels {
|
||||||
|
c.send(message.clone())?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user