1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376 | diff --git a/scripts/pino-dbus/Cargo.lock b/scripts/pino-dbus/Cargo.lock
new file mode 100644
index 0000000..b13d080
--- /dev/null
+++ b/scripts/pino-dbus/Cargo.lock
@@ -0,0 +1,74 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "dbus"
+version = "0.9.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1bb21987b9fb1613058ba3843121dd18b163b254d8a6e797e144cbac14d96d1b"
+dependencies = [
+ "libc",
+ "libdbus-sys",
+ "winapi",
+]
+
+[[package]]
+name = "dbus-crossroads"
+version = "0.5.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3a4c83437187544ba5142427746835061b330446ca8902eabd70e4afb8f76de0"
+dependencies = [
+ "dbus",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.169"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
+
+[[package]]
+name = "libdbus-sys"
+version = "0.2.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "06085512b750d640299b79be4bad3d2fa90a9c00b1fd9e1b46364f66f0485c72"
+dependencies = [
+ "pkg-config",
+]
+
+[[package]]
+name = "pino-dbus"
+version = "0.1.1"
+dependencies = [
+ "dbus",
+ "dbus-crossroads",
+]
+
+[[package]]
+name = "pkg-config"
+version = "0.3.31"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2"
+
+[[package]]
+name = "winapi"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
diff --git a/scripts/pino-dbus/Cargo.toml b/scripts/pino-dbus/Cargo.toml
new file mode 100644
index 0000000..fd39406
--- /dev/null
+++ b/scripts/pino-dbus/Cargo.toml
@@ -0,0 +1,14 @@
+[package]
+edition = "2024"
+name = "pino-dbus"
+version = "0.1.1"
+description = "Dbus Server for Pino-rs (Pixel notificatoin)"
+repository = "https://github.com/pixel2175/pino-rs"
+
+[[bin]]
+name = "pino-dbus"
+path = "src/main.rs"
+
+[dependencies]
+dbus = "0.9.7"
+dbus-crossroads = "0.5.2"
diff --git a/scripts/pino-dbus/src/main.rs b/scripts/pino-dbus/src/main.rs
new file mode 100644
index 0000000..ade8962
--- /dev/null
+++ b/scripts/pino-dbus/src/main.rs
@@ -0,0 +1,270 @@
+use std::{process::Command,thread,time::Duration,error::Error};
+use dbus::{arg::{RefArg,PropMap},blocking::{Connection,stdintf::org_freedesktop_dbus::Properties}};
+use dbus_crossroads::{Crossroads, Context};
+
+const NOTIFICATIONS_INTERFACE: &str = "org.freedesktop.Notifications";
+const MEDIA_PLAYER_PATH: &str = "/org/mpris/MediaPlayer2";
+const MEDIA_PLAYER_INTERFACE: &str = "org.mpris.MediaPlayer2.Player";
+
+const MEDIA_PLAYERS: &[&str] = &[
+ "spotify",
+ "mpv",
+ "vlc",
+ "chromium",
+ "firefox",
+ "brave",
+];
+
+#[derive(Debug)]
+struct NotificationData {
+ source: String,
+ app_name: String,
+ summary: String,
+ body: String,
+ actions: Vec<String>,
+ hints: PropMap,
+}
+
+impl NotificationData {
+ fn print(&self) {
+ // Send to pino app
+ let summary = self.summary.clone();
+ let body = self.body.clone();
+
+ thread::spawn(move || {
+ let _ = Command::new("pino")
+ .arg("-t")
+ .arg(summary)
+ .arg("-m")
+ .arg(body)
+ .output()
+ .expect("Failed to execute command");
+ });
+
+
+ println!("\n=== {} Notification ===", self.source);
+ println!("App: {}", self.app_name);
+ println!("Summary: {}", self.summary);
+ if !self.body.is_empty() {
+ println!("Body: {}", self.body);
+ }
+ if !self.actions.is_empty() {
+ println!("Actions: {}", self.actions.join(", "));
+ }
+
+ // Print relevant hints if they exist
+ if !self.hints.is_empty() {
+ println!("Additional Info:");
+ for (key, value) in &self.hints {
+ if let Some(val) = value.as_str() {
+ println!(" {}: {}", key, val);
+ }
+ }
+ }
+ println!("========================\n");
+ }
+}
+
+// Media player information structure
+#[derive(Debug)]
+struct MediaInfo {
+ player: String,
+ title: Option<String>,
+ artist: Option<String>,
+ album: Option<String>,
+ status: Option<String>,
+}
+
+impl MediaInfo {
+ fn to_notification(&self) -> NotificationData {
+ let title = self.title.clone().unwrap_or_default();
+ let artist = self.artist.clone().unwrap_or_default();
+ let album = self.album.clone().unwrap_or_default();
+ let status = self.status.clone().unwrap_or_default();
+
+ let summary = format!("{} - {}", self.player, status);
+ let body = if !artist.is_empty() && !title.is_empty() {
+ format!("{} - {}", artist, title)
+ } else if !title.is_empty() {
+ title
+ } else {
+ "Now Playing".to_string()
+ };
+
+ let mut hints = PropMap::new();
+ if !album.is_empty() {
+ hints.insert("album".to_string(), dbus::arg::Variant(Box::new(album)));
+ }
+
+ NotificationData {
+ source: "Media".to_string(),
+ app_name: self.player.clone(),
+ summary,
+ body,
+ actions: vec![],
+ hints,
+ }
+ }
+
+ fn print(&self) {
+ let notification = self.to_notification();
+ notification.print();
+ }
+}
+
+// Get media information from a specific player
+fn get_media_info(conn: &Connection, player: &str) -> Result<Option<MediaInfo>, Box<dyn Error>> {
+ let proxy = conn.with_proxy(
+ format!("org.mpris.MediaPlayer2.{}", player),
+ MEDIA_PLAYER_PATH,
+ Duration::from_millis(500),
+ );
+
+ // Try to get metadata and playback status
+ let metadata_result: Result<PropMap, dbus::Error> = proxy.get(MEDIA_PLAYER_INTERFACE, "Metadata");
+ let status_result: Result<String, dbus::Error> = proxy.get(MEDIA_PLAYER_INTERFACE, "PlaybackStatus");
+
+ match metadata_result {
+ Ok(metadata) => {
+ let title = metadata.get("xesam:title")
+ .and_then(|v| v.as_str())
+ .map(String::from);
+ let artist = metadata.get("xesam:artist")
+ .and_then(|v| v.as_iter())
+ .and_then(|mut iter| iter.next())
+ .and_then(|v| v.as_str())
+ .map(String::from);
+ let album = metadata.get("xesam:album")
+ .and_then(|v| v.as_str())
+ .map(String::from);
+ let status = status_result.ok();
+
+ Ok(Some(MediaInfo {
+ player: player.to_string(),
+ title,
+ artist,
+ album,
+ status,
+ }))
+ }
+ Err(_) => Ok(None),
+ }
+}
+
+fn monitor_media_players(conn: &Connection) {
+ for player in MEDIA_PLAYERS {
+ if let Ok(Some(info)) = get_media_info(conn, player) {
+ info.print();
+ }
+ }
+}
+
+fn process_discord_notification(summary: &str, body: &str) -> NotificationData {
+ NotificationData {
+ source: "Discord".to_string(),
+ app_name: "Discord".to_string(),
+ summary: format!("Discord: {}", summary),
+ body: body.to_string(),
+ actions: vec![],
+ hints: PropMap::new(),
+ }
+}
+
+fn setup_notification_interface(cr: &mut Crossroads) -> dbus_crossroads::IfaceToken<()> {
+ cr.register(NOTIFICATIONS_INTERFACE, |b| {
+ b.method(
+ "Notify",
+ ("app_name", "replaces_id", "app_icon", "summary", "body", "actions", "hints", "expire_timeout"),
+ ("id",),
+ move |_ctx: &mut Context,
+ _: &mut (),
+ (app_name, replaces_id, _app_icon, summary, body, actions, hints, _expire_timeout):
+ (String, u32, String, String, String, Vec<String>, PropMap, i32)| {
+
+ let notification = if app_name.to_lowercase().contains("discord") {
+ process_discord_notification(&summary, &body)
+ } else {
+ NotificationData {
+ source: "System".to_string(),
+ app_name: app_name.clone(),
+ summary,
+ body,
+ actions,
+ hints,
+ }
+ };
+
+ notification.print();
+
+ if MEDIA_PLAYERS.iter().any(|&p| app_name.to_lowercase().contains(p)) {
+ if let Ok(conn) = Connection::new_session() {
+ monitor_media_players(&conn);
+ }
+ }
+
+ Ok((replaces_id,))
+ },
+ );
+
+ b.method(
+ "GetCapabilities",
+ (),
+ ("capabilities",),
+ |_: &mut Context, _: &mut (), _: ()| {
+ Ok((vec![
+ "actions",
+ "body",
+ "body-markup",
+ "icon-static",
+ ],))
+ },
+ );
+
+ b.method(
+ "CloseNotification",
+ ("id",),
+ (),
+ |_: &mut Context, _: &mut (), (_id,): (u32,)| {
+ Ok(())
+ },
+ );
+
+ b.method(
+ "GetServerInformation",
+ (),
+ ("name", "vendor", "version", "spec_version"),
+ |_: &mut Context, _: &mut (), _: ()| {
+ Ok((
+ "ComprehensiveNotificationServer",
+ "Custom",
+ "1.0",
+ "1.2",
+ ))
+ },
+ );
+ })
+}
+
+fn main() -> Result<(), Box<dyn Error>> {
+ let conn = Connection::new_session()?;
+ conn.request_name(NOTIFICATIONS_INTERFACE, false, true, false)?;
+
+ println!("\nWaiting for notifications...\n");
+
+ let mut cr = Crossroads::new();
+ let iface_token = setup_notification_interface(&mut cr);
+ cr.insert("/org/freedesktop/Notifications", &[iface_token], ());
+
+ thread::spawn(move || {
+ loop {
+ if let Ok(conn) = Connection::new_session() {
+ monitor_media_players(&conn);
+ }
+ thread::sleep(Duration::from_secs(5));
+ }
+ });
+
+ cr.serve(&conn)?;
+
+ Ok(())
+}
|