Skip to content

Commit f277322

Browse files
committed
Moved the libinput backend listening logic out of the constructor
1 parent cd87063 commit f277322

File tree

1 file changed

+68
-60
lines changed

1 file changed

+68
-60
lines changed

src/server/application.rs

Lines changed: 68 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::global_utils::{handle_application_args, HandleLocalStatus};
44
use crate::osd_window::SwayosdWindow;
55
use crate::playerctl::*;
66
use crate::utils::{self, *};
7-
use async_channel::Receiver;
8-
use gtk::gio::ListModel;
7+
use async_channel::{Receiver, Sender};
8+
use gtk::gio::{DBusConnection, ListModel};
99
use gtk::{
1010
gdk,
1111
gio::{
@@ -133,8 +133,8 @@ impl SwayOSDApplication {
133133
}
134134
));
135135

136+
// Listen for any actions sent from swayosd-client
136137
let server_config_shared = server_config.clone();
137-
138138
MainContext::default().spawn_local(clone!(
139139
#[strong]
140140
osd_app,
@@ -152,9 +152,8 @@ impl SwayOSDApplication {
152152
));
153153

154154
let server_config_shared = server_config.clone();
155-
156-
// Listen to the LibInput Backend and activate the Application action
157155
let (sender, receiver) = async_channel::bounded::<(u16, i32)>(1);
156+
// Listen to the LibInput Backend and activate the Application action
158157
MainContext::default().spawn_local(clone!(
159158
#[strong]
160159
osd_app,
@@ -189,71 +188,80 @@ impl SwayOSDApplication {
189188
sender,
190189
#[strong]
191190
signal_id,
192-
move |connection, _, _| {
193-
println!("Connecting to the SwayOSD LibInput Backend");
194-
let mut mutex = match signal_id.lock() {
195-
Ok(mut mutex) => match mutex.as_mut() {
196-
Some(_) => return,
197-
None => mutex,
198-
},
199-
Err(error) => return println!("Mutex lock Error: {}", error),
200-
};
201-
mutex.replace(connection.signal_subscribe(
202-
Some(config::DBUS_BACKEND_NAME),
203-
Some(config::DBUS_BACKEND_NAME),
204-
Some("KeyPressed"),
205-
Some(config::DBUS_PATH),
206-
None,
207-
DBusSignalFlags::NONE,
208-
clone!(
209-
#[strong]
210-
sender,
211-
move |_, _, _, _, _, variant| {
212-
let key_code = variant.try_child_get::<u16>(0);
213-
let state = variant.try_child_get::<i32>(1);
214-
match (key_code, state) {
215-
(Ok(Some(key_code)), Ok(Some(state))) => {
216-
MainContext::default().spawn_local(clone!(
217-
#[strong]
218-
sender,
219-
async move {
220-
if let Err(error) =
221-
sender.send((key_code, state)).await
222-
{
223-
eprintln!("Channel Send error: {}", error);
224-
}
225-
}
226-
));
227-
}
228-
variables => {
229-
return eprintln!("Variables don't match: {:?}", variables)
230-
}
231-
};
232-
}
233-
),
234-
));
235-
}
191+
move |connection, _, _| Self::libinput_backend_appeared(
192+
&sender, &signal_id, connection
193+
)
236194
),
237195
clone!(
238196
#[strong]
239197
signal_id,
240-
move |connection, _| {
241-
eprintln!("SwayOSD LibInput Backend isn't available, waiting...");
242-
match signal_id.lock() {
243-
Ok(mut mutex) => {
244-
if let Some(sig_id) = mutex.take() {
245-
connection.signal_unsubscribe(sig_id);
246-
}
247-
}
248-
Err(error) => println!("Mutex lock Error: {}", error),
249-
}
250-
}
198+
move |connection, _| Self::libinput_backend_vanished(&signal_id, connection)
251199
),
252200
);
253201

254202
osd_app
255203
}
256204

205+
fn libinput_backend_appeared(
206+
sender: &Sender<(u16, i32)>,
207+
signal_id: &Arc<Mutex<Option<SignalSubscriptionId>>>,
208+
connection: DBusConnection,
209+
) {
210+
println!("Connecting to the SwayOSD LibInput Backend");
211+
let mut mutex = match signal_id.lock() {
212+
Ok(mut mutex) => match mutex.as_mut() {
213+
Some(_) => return,
214+
None => mutex,
215+
},
216+
Err(error) => return println!("Mutex lock Error: {}", error),
217+
};
218+
mutex.replace(connection.signal_subscribe(
219+
Some(config::DBUS_BACKEND_NAME),
220+
Some(config::DBUS_BACKEND_NAME),
221+
Some("KeyPressed"),
222+
Some(config::DBUS_PATH),
223+
None,
224+
DBusSignalFlags::NONE,
225+
clone!(
226+
#[strong]
227+
sender,
228+
move |_, _, _, _, _, variant| {
229+
let key_code = variant.try_child_get::<u16>(0);
230+
let state = variant.try_child_get::<i32>(1);
231+
match (key_code, state) {
232+
(Ok(Some(key_code)), Ok(Some(state))) => {
233+
MainContext::default().spawn_local(clone!(
234+
#[strong]
235+
sender,
236+
async move {
237+
if let Err(error) = sender.send((key_code, state)).await {
238+
eprintln!("Channel Send error: {}", error);
239+
}
240+
}
241+
));
242+
}
243+
variables => return eprintln!("Variables don't match: {:?}", variables),
244+
};
245+
}
246+
),
247+
));
248+
}
249+
250+
fn libinput_backend_vanished(
251+
signal_id: &Arc<Mutex<Option<SignalSubscriptionId>>>,
252+
connection: DBusConnection,
253+
) {
254+
eprintln!("SwayOSD LibInput Backend isn't available, waiting...");
255+
match signal_id.lock() {
256+
Ok(mut mutex) => {
257+
if let Some(sig_id) = mutex.take() {
258+
connection.signal_unsubscribe(sig_id);
259+
}
260+
}
261+
Err(error) => println!("Mutex lock Error: {}", error),
262+
}
263+
}
264+
257265
pub fn start(&self) -> i32 {
258266
let osd_app = self.clone();
259267
self.app.connect_activate(move |_| {

0 commit comments

Comments
 (0)