Skip to content

Commit

Permalink
wayland: simplify code and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
zefr0x committed Aug 3, 2024
1 parent 65737d2 commit a7e2db5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Simple, light-weight, easy to use, and effective [Linux](https://en.wikipedia.or

## Requirements

- [Wayland Compositor](<https://en.wikipedia.org/wiki/Wayland_(protocol)#Wayland_compositors>) that implements [`ext_idle_notifier_v1`](https://wayland.app/protocols/ext-idle-notify-v1)
- [Wayland Compositor](<https://en.wikipedia.org/wiki/Wayland_(protocol)#Wayland_compositors>) that optionally implements [`ext_idle_notifier_v1`](https://wayland.app/protocols/ext-idle-notify-v1)
- [Notification Daemon](https://wiki.archlinux.org/title/Desktop_notifications#Notification_servers) that implements [`org.freedesktop.Notifications`](https://specifications.freedesktop.org/notification-spec/notification-spec-latest.html)
- [libdbus-1.so](https://www.freedesktop.org/wiki/Software/dbus/) installed in your system
- [Linux libc](https://en.wikipedia.org/wiki/C_standard_library) via either [glibc](https://www.gnu.org/software/libc/) or [musl libc](https://musl.libc.org/)
Expand Down
36 changes: 18 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,24 +129,6 @@ fn main() {
// timer loop cycle for an aleady resumed idle state.
let (signal_sender, signal_receiver) = mpsc::sync_channel(2);

// Create main state for the app to store shared things.
let mut state = wayland::State::new(signal_sender);

// Connect to Wayland server
let conn = wayland_client::Connection::connect_to_env()
.expect("Not able to detect a wayland compositor");

let mut event_queue = conn.new_event_queue::<wayland::State>();
let queue_handle = event_queue.handle();

let display = conn.display();

let _registry = display.get_registry(&queue_handle, ());

event_queue
.roundtrip(&mut state)
.expect("Failed to cause a synchronous round trip with the wayland server");

// Timer thread
std::thread::spawn(move || {
let pause_duration = core::cmp::min(
Expand Down Expand Up @@ -215,6 +197,24 @@ fn main() {
}
});

// Connect to Wayland server
let conn = wayland_client::Connection::connect_to_env()
.expect("Not able to detect a wayland compositor");

let mut event_queue = conn.new_event_queue::<wayland::State>();
let queue_handle = event_queue.handle();

let display = conn.display();

let _registry = display.get_registry(&queue_handle, ());

// Create main state for the app to store shared things.
let mut state = wayland::State::new(signal_sender);

event_queue
.roundtrip(&mut state)
.expect("Failed to cause a synchronous round trip with the wayland server");

// Main loop.
loop {
event_queue
Expand Down
56 changes: 23 additions & 33 deletions src/wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,21 @@ use wayland_protocols::ext::idle_notify::v1::client::{

use crate::CONFIG;

// TODO: Simplify type.
enum IdleInterface {
IdleNotifier(ext_idle_notifier_v1::ExtIdleNotifierV1),
}

#[derive(Debug, Eq, PartialEq)]
pub enum Signal {
Idled,
Resumed,
}

pub struct State {
idle_interface: Option<IdleInterface>,
idle_notifier: Option<ext_idle_notifier_v1::ExtIdleNotifierV1>,
signal_sender: mpsc::SyncSender<Signal>,
}

impl State {
pub const fn new(signal_sender: mpsc::SyncSender<Signal>) -> Self {
Self {
idle_interface: None,
idle_notifier: None,
signal_sender,
}
}
Expand All @@ -37,7 +32,7 @@ impl wayland_client::Dispatch<wl_registry::WlRegistry, ()> for State {
state: &mut Self,
registry: &wl_registry::WlRegistry,
event: wl_registry::Event,
&(): &(),
_data: &(),
_conn: &wayland_client::Connection,
queue_handle: &wayland_client::QueueHandle<Self>,
) {
Expand All @@ -49,20 +44,17 @@ impl wayland_client::Dispatch<wl_registry::WlRegistry, ()> for State {
"wl_seat" => {
registry.bind::<wl_seat::WlSeat, _, _>(name, 1, queue_handle, ());
}
// First one to be offered by the compositor will be used.
"ext_idle_notifier_v1" => {
if state.idle_interface.is_none() {
state.idle_interface = Some(IdleInterface::IdleNotifier(
registry.bind::<ext_idle_notifier_v1::ExtIdleNotifierV1, _, _>(
name,
1,
queue_handle,
(),
),
));
state.idle_notifier = Some(
registry.bind::<ext_idle_notifier_v1::ExtIdleNotifierV1, _, _>(
name,
1,
queue_handle,
(),
),
);

eprintln!("Binded to ext_idle_notifier_v1");
}
eprintln!("Binded to ext_idle_notifier_v1");
}
_ => {}
}
Expand All @@ -75,21 +67,19 @@ impl wayland_client::Dispatch<wl_seat::WlSeat, ()> for State {
state: &mut Self,
seat: &wl_seat::WlSeat,
_event: wl_seat::Event,
&(): &(),
_data: &(),
_conn: &wayland_client::Connection,
queue_handle: &wayland_client::QueueHandle<Self>,
) {
if let Some(idle_interface) = &state.idle_interface {
match idle_interface {
IdleInterface::IdleNotifier(idle_notifier) => {
idle_notifier.get_idle_notification(
CONFIG.timer.idle_timeout * 1000, // milli seconds
seat,
queue_handle,
(),
);
}
}
if let Some(idle_notifier) = &state.idle_notifier {
idle_notifier.get_idle_notification(
CONFIG.timer.idle_timeout * 1000, // milli seconds
seat,
queue_handle,
(),
);

eprintln!("Created ext_idle_notification_v1");
}
}
}
Expand All @@ -112,7 +102,7 @@ impl wayland_client::Dispatch<ext_idle_notification_v1::ExtIdleNotificationV1, (
state: &mut Self,
_idle_notification: &ext_idle_notification_v1::ExtIdleNotificationV1,
event: ext_idle_notification_v1::Event,
&(): &(),
_data: &(),
_conn: &wayland_client::Connection,
_queue_handle: &wayland_client::QueueHandle<Self>,
) {
Expand Down

0 comments on commit a7e2db5

Please sign in to comment.