-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.rs
More file actions
249 lines (233 loc) · 8.25 KB
/
Copy pathrenderer.rs
File metadata and controls
249 lines (233 loc) · 8.25 KB
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
// Copyright (C) 2026 Jeff Shee <jeffshee8969@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#[cfg(any(feature = "mpv", feature = "wpe"))]
mod gl_loader;
mod gstgtk4;
#[cfg(feature = "mpv")]
mod mpv;
#[cfg(feature = "wpe")]
mod scene;
mod web;
use enum_dispatch::enum_dispatch;
use gtk::{gdk, prelude::*, Widget};
use crate::model::{VideoRenderer, WallpaperSource, WallpaperType};
use crate::wpe::{WpePackage, WpeType};
pub use gstgtk4::GstGtk4Widget;
#[cfg(feature = "mpv")]
pub use mpv::MpvWidget;
#[cfg(feature = "wpe")]
pub use scene::SceneWidget;
pub use web::WebWidget;
/// Wrap `paintable` in an expanding Picture (optionally graphics-offloaded)
/// inside a Box, the shape every `mirror()` implementation produces.
fn picture_box(
paintable: &impl IsA<gdk::Paintable>,
enable_graphics_offload: bool,
content_fit: gtk::ContentFit,
) -> (gtk::Box, gtk::Picture) {
let widget = gtk::Box::builder().build();
let picture = gtk::Picture::builder()
.paintable(paintable)
.hexpand(true)
.vexpand(true)
.content_fit(content_fit)
.build();
if enable_graphics_offload {
let offload = gtk::GraphicsOffload::new(Some(&picture));
offload.set_enabled(gtk::GraphicsOffloadEnabled::Enabled);
widget.append(&offload);
} else {
widget.append(&picture);
}
(widget, picture)
}
/// Mirror a renderer that draws straight into its widget and exposes no
/// `gdk::Paintable` (GLArea- and WebView-based renderers) by snapshotting
/// the widget.
fn mirror_by_snapshot(
source: &impl IsA<Widget>,
enable_graphics_offload: bool,
content_fit: gtk::ContentFit,
) -> gtk::Box {
let paintable = gtk::WidgetPaintable::new(Some(source));
picture_box(&paintable, enable_graphics_offload, content_fit).0
}
#[enum_dispatch]
pub trait RendererWidget: AsRef<Widget> {
fn mirror(&self, enable_graphics_offload: bool, content_fit: gtk::ContentFit) -> gtk::Box;
fn play(&self);
fn pause(&self);
fn stop(&self);
/// Set the audio volume (0-100).
fn set_volume(&self, volume: i32);
fn set_mute(&self, mute: bool);
fn set_content_fit(&self, fit: gtk::ContentFit);
fn widget(&self) -> &Widget {
self.as_ref()
}
}
#[enum_dispatch(RendererWidget)]
#[derive(Debug)]
#[non_exhaustive]
pub enum Renderer {
Web(WebWidget),
GstGtk4(GstGtk4Widget),
#[cfg(feature = "mpv")]
Mpv(MpvWidget),
#[cfg(feature = "wpe")]
Scene(SceneWidget),
}
impl Renderer {
pub fn with_filepath(
filepath: &str,
wallpaper_type: &WallpaperType,
video_renderer: VideoRenderer,
enable_graphics_offload: bool,
) -> Self {
match wallpaper_type {
WallpaperType::Video => match resolve_video_renderer(video_renderer) {
VideoRenderer::GstGtk4 => Self::GstGtk4(GstGtk4Widget::with_filepath(
filepath,
enable_graphics_offload,
)),
#[cfg(feature = "mpv")]
VideoRenderer::Mpv => Self::Mpv(MpvWidget::with_filepath(filepath)),
#[cfg(not(feature = "mpv"))]
VideoRenderer::Mpv => unreachable!(),
},
WallpaperType::Web => Self::Web(WebWidget::with_filepath(filepath)),
WallpaperType::Wpe => Self::with_wpe(
&WallpaperSource::Filepath {
filepath: filepath.to_string(),
},
video_renderer,
enable_graphics_offload,
),
}
}
/// Build a renderer for a Wallpaper Engine package: resolve the source,
/// read its `project.json`, and delegate to the renderer its `type`
/// selects — scene packages to `SceneWidget` (linux-wallpaperengine),
/// video/web packages to hotaru's own video/web renderers.
pub fn with_wpe(
source: &WallpaperSource,
video_renderer: VideoRenderer,
enable_graphics_offload: bool,
) -> Self {
let package = match WpePackage::resolve(source) {
Ok(package) => package,
Err(e) => {
tracing::error!("Failed to load Wallpaper Engine package: {:#}", e);
return blank();
}
};
match package.kind {
WpeType::Scene => {
#[cfg(feature = "wpe")]
{
Self::Scene(SceneWidget::with_filepath(&package.dir.to_string_lossy()))
}
#[cfg(not(feature = "wpe"))]
{
scene_unsupported()
}
}
WpeType::Video => match package.entry() {
Ok(entry) => Self::with_filepath(
&entry.to_string_lossy(),
&WallpaperType::Video,
video_renderer,
enable_graphics_offload,
),
Err(e) => {
tracing::error!("Invalid Wallpaper Engine package: {:#}", e);
blank()
}
},
WpeType::Web => match package.entry() {
// Web packages get the Wallpaper Engine JS API and their
// default properties injected (see web.rs).
Ok(entry) => Self::Web(WebWidget::with_wpe(
&entry.to_string_lossy(),
&package.user_properties_json(),
&package.dir.to_string_lossy(),
)),
Err(e) => {
tracing::error!("Invalid Wallpaper Engine package: {:#}", e);
blank()
}
},
}
}
pub fn with_uri(
uri: &str,
wallpaper_type: &WallpaperType,
video_renderer: VideoRenderer,
enable_graphics_offload: bool,
) -> Self {
match wallpaper_type {
WallpaperType::Video => match resolve_video_renderer(video_renderer) {
VideoRenderer::GstGtk4 => {
Self::GstGtk4(GstGtk4Widget::with_uri(uri, enable_graphics_offload))
}
#[cfg(feature = "mpv")]
VideoRenderer::Mpv => Self::Mpv(MpvWidget::with_uri(uri)),
#[cfg(not(feature = "mpv"))]
VideoRenderer::Mpv => unreachable!(),
},
WallpaperType::Web => Self::Web(WebWidget::with_uri(uri)),
WallpaperType::Wpe => {
tracing::error!(
"wpe wallpaper cannot be a URI ({}); use filepath or workshop_id",
uri
);
blank()
}
}
}
}
/// A blank fallback renderer, used when a wallpaper cannot be constructed.
fn blank() -> Renderer {
Renderer::Web(WebWidget::with_uri("about:blank"))
}
/// Placeholder for scene packages in builds without the 'wpe' feature.
/// (WPE video/web packages still render — only the scene backend is gated.)
#[cfg(not(feature = "wpe"))]
fn scene_unsupported() -> Renderer {
tracing::error!(
"scene wallpaper requested but this build lacks the 'wpe' feature, \
showing a blank wallpaper"
);
blank()
}
/// Downgrade renderer choices this build cannot honor.
fn resolve_video_renderer(video_renderer: VideoRenderer) -> VideoRenderer {
#[cfg(not(feature = "mpv"))]
if video_renderer == VideoRenderer::Mpv {
tracing::warn!(
"mpv renderer requested but this build lacks the 'mpv' feature, \
falling back to GStreamer"
);
return VideoRenderer::GstGtk4;
}
video_renderer
}
impl AsRef<Widget> for Renderer {
fn as_ref(&self) -> &Widget {
RendererWidget::widget(self)
}
}