-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathprint_events.rs
154 lines (131 loc) · 4.69 KB
/
print_events.rs
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
use souvlaki::{MediaControlEvent, MediaControls, MediaMetadata, PlatformConfig};
fn main() {
#[cfg(not(target_os = "windows"))]
let hwnd = None;
#[cfg(target_os = "windows")]
let (hwnd, _dummy_window) = {
let dummy_window = windows::DummyWindow::new().unwrap();
let handle = Some(dummy_window.handle.0 as _);
(handle, dummy_window)
};
let config = PlatformConfig {
dbus_name: "my_player",
display_name: "My Player",
hwnd,
};
let mut controls = MediaControls::new(config).unwrap();
// The closure must be Send and have a static lifetime.
controls
.attach(|event: MediaControlEvent| println!("Event received: {:?}", event))
.unwrap();
// Update the media metadata.
controls
.set_metadata(MediaMetadata {
title: Some("Souvlaki Space Station"),
artist: Some("Slowdive"),
album: Some("Souvlaki"),
..Default::default()
})
.unwrap();
// Your actual logic goes here.
loop {
std::thread::sleep(std::time::Duration::from_millis(100));
// this must be run repeatedly by your program to ensure
// the Windows event queue is processed by your application
#[cfg(target_os = "windows")]
windows::pump_event_queue();
}
// The controls automatically detach on drop.
}
// demonstrates how to make a minimal window to allow use of media keys on the command line
#[cfg(target_os = "windows")]
mod windows {
use std::io::Error;
use std::mem;
use windows::core::PCWSTR;
use windows::w;
use windows::Win32::Foundation::{HWND, LPARAM, LRESULT, WPARAM};
use windows::Win32::System::LibraryLoader::GetModuleHandleW;
use windows::Win32::UI::WindowsAndMessaging::{
CreateWindowExW, DefWindowProcW, DestroyWindow, DispatchMessageW, GetAncestor,
IsDialogMessageW, PeekMessageW, RegisterClassExW, TranslateMessage, GA_ROOT, MSG,
PM_REMOVE, WINDOW_EX_STYLE, WINDOW_STYLE, WM_QUIT, WNDCLASSEXW,
};
pub struct DummyWindow {
pub handle: HWND,
}
impl DummyWindow {
pub fn new() -> Result<DummyWindow, String> {
let class_name = w!("SimpleTray");
let handle_result = unsafe {
let instance = GetModuleHandleW(None)
.map_err(|e| (format!("Getting module handle failed: {e}")))?;
let wnd_class = WNDCLASSEXW {
cbSize: mem::size_of::<WNDCLASSEXW>() as u32,
hInstance: instance,
lpszClassName: PCWSTR::from(class_name),
lpfnWndProc: Some(Self::wnd_proc),
..Default::default()
};
if RegisterClassExW(&wnd_class) == 0 {
return Err(format!(
"Registering class failed: {}",
Error::last_os_error()
));
}
let handle = CreateWindowExW(
WINDOW_EX_STYLE::default(),
class_name,
w!(""),
WINDOW_STYLE::default(),
0,
0,
0,
0,
None,
None,
instance,
None,
);
if handle.0 == 0 {
Err(format!(
"Message only window creation failed: {}",
Error::last_os_error()
))
} else {
Ok(handle)
}
};
handle_result.map(|handle| DummyWindow { handle })
}
extern "system" fn wnd_proc(
hwnd: HWND,
msg: u32,
wparam: WPARAM,
lparam: LPARAM,
) -> LRESULT {
unsafe { DefWindowProcW(hwnd, msg, wparam, lparam) }
}
}
impl Drop for DummyWindow {
fn drop(&mut self) {
unsafe {
DestroyWindow(self.handle);
}
}
}
pub fn pump_event_queue() -> bool {
unsafe {
let mut msg: MSG = std::mem::zeroed();
let mut has_message = PeekMessageW(&mut msg, None, 0, 0, PM_REMOVE).as_bool();
while msg.message != WM_QUIT && has_message {
if !IsDialogMessageW(GetAncestor(msg.hwnd, GA_ROOT), &msg).as_bool() {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
has_message = PeekMessageW(&mut msg, None, 0, 0, PM_REMOVE).as_bool();
}
msg.message == WM_QUIT
}
}
}