Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ wayland-protocols = { version = "0.32.13", features = ["client", "staging", "uns
xkbcommon = { version = "0.9.0", default-features = false }
swash = "0.2.10"
rustix = { version = "1.1.6", features = ["fs", "event", "time", "pipe", "process"] }
# Video and music: libmpv in a jailed helper that draws on the graphics card and hands its
# frames over as dmabufs (ALI-2358). On the branch of alias-playback PR #3 (stacked on #2)
# until those merge; then this moves to main.
alias-playback = { git = "https://github.com/aliasproject/alias-playback", branch = "ali-2358-gpu-media" }

[profile.release]
opt-level = 3
Expand Down
167 changes: 165 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::decode::anim::{Next, Player};
use crate::decode::{Info, Want};
use crate::loader::{Done, Job, Kind, Loader};
use crate::pixels::Pixels;
use crate::player::{self, Media, Phase};
use crate::render::{self, Level};
use crate::text::{self, Text};
use crate::theme::Colors;
Expand Down Expand Up @@ -86,6 +87,11 @@ pub struct App {
pub title: String,
pub focused: bool,
pub cache_home: PathBuf,
/// The video or song open now, if the current file is one.
pub media: Option<Media>,
/// Carried from one video or song to the next.
pub volume: f64,
pub muted: bool,
}

impl App {
Expand Down Expand Up @@ -127,6 +133,9 @@ impl App {
title: String::new(),
focused: false,
cache_home,
media: None,
volume: 100.0,
muted: false,
}
}

Expand Down Expand Up @@ -198,12 +207,101 @@ impl App {
self.ask_current();
self.grid_follow();
}
let size = self.media.as_ref().map(|m| self.media_box(m.kind));
if let (Some(m), Some(b)) = (&mut self.media, size) {
m.resize((b.w.max(1) as u32, b.h.max(1) as u32));
}
}

/// Where a video's frames or a song's cover art go, in physical px.
fn media_box(&self, kind: player::Kind) -> Rect {
let (w, h) = (self.win.0 as i32, self.win.1 as i32);
match kind {
player::Kind::Video => Rect::new(0, 0, w, h),
player::Kind::Music => {
let side = ui::px(crate::tokens::size::COVER_ART as f64, self.scale).min(w.min(h) / 2).max(1);
Rect::new((w - side) / 2, (h - side) / 2, side, side)
}
}
}

/// Opens the current file in the player if it's a video or song, closes the player if
/// it isn't, and acts on what the player has said. Called on every turn of the loop.
pub fn sync_media(&mut self, wl: &mut Wl) {
let want = if self.grid.is_some() { None } else { self.path().and_then(|p| Some((p.to_path_buf(), player::kind_of(p)?))) };
match want {
Some((path, _)) if self.media.as_ref().is_some_and(|m| m.path == path) => {}
Some((path, kind)) => {
self.close_media(wl);
// Full screen carries from one video to the next; a song has no full screen.
if kind != player::Kind::Video && wl.state.fullscreen {
wl.set_fullscreen(false);
}
let b = self.media_box(kind);
let size = (b.w.max(1) as u32, b.h.max(1) as u32);
self.media = Some(if wl.can_show_video() {
Media::open(path, kind, player::gpu::open_node(wl.render_node_path().as_deref()), size, self.volume, self.muted)
} else {
Media::failed(path, kind, "this desktop can't show video from another process (it lacks linux-dmabuf or viewporter)".into())
});
self.all();
self.a11y_dirty = true;
}
None => {
if self.media.is_some() {
self.close_media(wl);
if wl.state.fullscreen {
wl.set_fullscreen(false);
}
self.all();
self.a11y_dirty = true;
}
}
}
let Some(kind) = self.media.as_ref().map(|m| m.kind) else { return };
let b = self.media_box(kind);
let scale = self.scale;
if let Some(m) = &mut self.media {
m.replace(wl, b, scale);
}
if let Some(m) = &mut self.media
&& m.pump(wl, b, scale)
{
self.all();
self.a11y_dirty = true;
}
}

fn close_media(&mut self, wl: &mut Wl) {
if let Some(old) = self.media.take() {
if old.heard {
self.volume = old.state.volume;
self.muted = old.state.muted;
}
drop(old);
wl.video_end();
}
}

/// The line drawn in the middle of the window for a video or song that isn't showing
/// a picture yet, or can't.
fn media_message(&self) -> Option<String> {
let m = self.media.as_ref()?;
match m.phase() {
Phase::Opening => Some(format!("Opening {}", self.name())),
Phase::Failed(why) => Some(format!("Can't play {}: {why}", self.name())),
_ => None,
}
}

/// Asks for whatever the current picture still needs: its screen copy, a bigger one
/// for zoom, and then its neighbours.
pub fn ask_current(&mut self) {
let Some(path) = self.path().map(|p| p.to_path_buf()) else { return };
// Videos and songs play in the player, not the picture decoder.
if player::kind_of(&path).is_some() {
return;
}
let bx = self.win;
let entry = self.pictures.entry(path.clone()).or_default();
let stale = entry.screen.as_ref().is_some_and(|s| {
Expand Down Expand Up @@ -242,7 +340,7 @@ impl App {
}
let bx = self.win;
for i in self.neighbours() {
let Some(path) = self.files.get(i).cloned() else { continue };
let Some(path) = self.files.get(i).cloned().filter(|p| player::kind_of(p).is_none()) else { continue };
let e = self.pictures.entry(path.clone()).or_default();
if e.screen.is_none() && e.error.is_none() && !e.asked {
e.asked = true;
Expand Down Expand Up @@ -576,6 +674,11 @@ impl App {
self.drag = None;
}
Input::Close => self.quit = true,
Input::VideoReleased(slot) => {
if let Some(m) = &mut self.media {
m.released(slot);
}
}
_ => {}
}
}
Expand Down Expand Up @@ -628,6 +731,33 @@ impl App {
if self.grid.is_some() && self.grid_key(sym) {
return;
}
if self.grid.is_none()
&& let Some(m) = &mut self.media
{
match sym {
k::KEY_space => {
m.toggle();
return;
}
// In a video or song, Left and Right seek; n, p, Page Up and Page Down still
// change file (Mike, 2026-12-02).
// Seeking needs a file that's playing; otherwise they change file, as for pictures.
k::KEY_Left | k::KEY_KP_Left | k::KEY_Right | k::KEY_KP_Right if m.heard && m.error.is_none() => {
let step = crate::tokens::motion::SEEK_STEP.as_secs_f64();
m.send(player::gpu::Command::SeekBy(if matches!(sym, k::KEY_Left | k::KEY_KP_Left) { -step } else { step }));
return;
}
k::KEY_f if m.kind == player::Kind::Video => {
wl.set_fullscreen(!wl.state.fullscreen);
return;
}
k::KEY_Escape if wl.state.fullscreen && !(self.show_help || self.show_info) => {
wl.set_fullscreen(false);
return;
}
_ => {}
}
}
let img = self.img();
let zoomed = img.is_some_and(|img| self.view.zoomed(img, self.win));
let step = (64.0 * self.scale).max(self.win.0.min(self.win.1) as f64 / 10.0);
Expand Down Expand Up @@ -823,6 +953,10 @@ impl App {
}
continue;
}
// A video or song has no thumbnail yet: its tile stays empty.
if player::kind_of(path).is_some() {
continue;
}
jobs.push(Job { path: path.clone(), kind: Kind::Thumb, want: Want::Fit(inner, inner) });
}
self.thumbs_asked = jobs.iter().map(|j| j.path.clone()).collect();
Expand Down Expand Up @@ -954,13 +1088,17 @@ impl App {
if damage.is_empty() || self.frame.len() < (w * h) as usize {
return Rect::default();
}
let wants_text = self.show_info || self.show_help || self.toast.is_some() || self.grid.is_some() || self.current().is_some_and(|p| p.error.is_some()) || self.files.is_empty();
let media_message = if self.grid.is_none() { self.media_message() } else { None };
let wants_text =
self.show_info || self.show_help || self.toast.is_some() || self.grid.is_some() || self.current().is_some_and(|p| p.error.is_some()) || self.files.is_empty() || media_message.is_some();
if wants_text {
self.ensure_text();
}
let mut damage = damage;
if self.grid.is_some() {
self.paint_grid(damage);
} else if self.media.is_some() {
self.paint_media(damage, media_message.as_deref());
} else {
damage = self.paint_picture(damage);
}
Expand Down Expand Up @@ -1034,6 +1172,20 @@ impl App {
damage
}

/// The window under a video or song: the background, and the line saying it's opening
/// or why it can't play. The frames are in the subsurface above.
fn paint_media(&mut self, damage: Rect, message: Option<&str>) {
let (w, h) = (self.win.0 as i32, self.win.1 as i32);
let mut c = Canvas::new(&mut self.frame, w, h);
c.set_clip(damage);
c.fill(damage, self.colors.base);
self.drawn = None;
let (Some(msg), Some(t)) = (message, self.text.as_mut()) else { return };
let tw = t.measure(msg);
let x = ((w as f32 - tw) / 2.0).max(ui::px(ui::MARGIN, self.scale) as f32);
t.draw_fitted(&mut c, msg, x, h / 2, (w - ui::px(ui::MARGIN, self.scale) * 2) as f32, self.colors.foreground);
}

fn paint_grid(&mut self, damage: Rect) {
let (w, h) = (self.win.0 as i32, self.win.1 as i32);
let layout = self.grid_layout();
Expand Down Expand Up @@ -1109,6 +1261,17 @@ impl App {
let title = self.window_title();
let picture = match self.current() {
_ if self.files.is_empty() => "No pictures left".to_string(),
_ if self.media.is_some() => {
let state = match self.media.as_ref().map(Media::phase) {
Some(Phase::Opening) => "opening".to_string(),
Some(Phase::Playing) => "playing".to_string(),
Some(Phase::Paused) => "paused".to_string(),
Some(Phase::Ended) => "ended".to_string(),
Some(Phase::Failed(why)) => format!("can't be played: {why}"),
None => String::new(),
};
format!("{}, {} of {}, {state}", self.name(), self.cur + 1, self.files.len())
}
Some(p) if p.error.is_some() => format!("{}, {} of {}, can't be shown: {}", self.name(), self.cur + 1, self.files.len(), p.error.as_deref().unwrap_or("")),
Some(p) if p.screen.is_none() => format!("{}, {} of {}, loading", self.name(), self.cur + 1, self.files.len()),
_ => format!("{}, {} of {}", self.name(), self.cur + 1, self.files.len()),
Expand Down
2 changes: 1 addition & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub fn list_folder(dir: &Path, include: Option<&Path>) -> Vec<PathBuf> {
let name = e.file_name();
let s = name.to_string_lossy();
let wanted = include.is_some_and(|p| p.file_name() == Some(name.as_os_str()));
wanted || (!s.starts_with('.') && is_picture_name(&s) && e.path().is_file())
wanted || (!s.starts_with('.') && (is_picture_name(&s) || crate::player::kind_of_name(&s).is_some()) && e.path().is_file())
})
.map(|e| e.file_name())
.collect();
Expand Down
36 changes: 29 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod decode;
mod exif;
mod loader;
mod pixels;
mod player;
mod render;
mod sort;
mod text;
Expand Down Expand Up @@ -74,6 +75,11 @@ fn restart_with_fixed_mmap_threshold() {
fn main() {
restart_with_fixed_mmap_threshold();
let argv: Vec<_> = std::env::args_os().skip(1).collect();
// The video player's helper, inside its jail (src/player/gpu.rs).
if argv.first().is_some_and(|a| a == player::gpu::HELPER_ARG) {
let rest: Vec<String> = argv[1..].iter().map(|a| a.to_string_lossy().into_owned()).collect();
player::gpu::helper_main(&rest);
}
if argv.len() == 2 && argv[0] == decode::heif::HELPER_ARG {
std::process::exit(decode::heif::helper_main(std::path::Path::new(&argv[1])));
}
Expand Down Expand Up @@ -178,7 +184,11 @@ fn run(o: Opening) -> i32 {
let (info_tx, info_rx) = channel::<Info>();
let (box_tx, box_rx) = channel::<Want>();
let asked: Arc<Mutex<Want>> = Arc::new(Mutex::new(Want::Fit(1920, 1080)));
{
// A video or song opens in the player once the window is up, not in the decoder.
let first_media = player::kind_of(&first).is_some();
if first_media {
drop(info_tx);
} else {
let (done, asked, first) = (loader.sender(), asked.clone(), first.clone());
let _ = std::thread::Builder::new().name("alias-view-open".into()).spawn(move || {
let result = decode::decode_with(&first, threads, |info| {
Expand Down Expand Up @@ -209,17 +219,22 @@ fn run(o: Opening) -> i32 {
let (screen, scale) = wl.likely_output().map(|o| o.logical_and_scale()).unwrap_or(((1920, 1080), 1.0));
// A broken file says nothing here and closes the channel; the window still opens
// to say what's wrong.
let info = info_rx.recv_timeout(Duration::from_secs(5)).ok();
let img = info.as_ref().map(|i| (i.width, i.height)).filter(|&(w, h)| w > 0 && h > 0).unwrap_or((1, 1));
let info = if first_media { None } else { info_rx.recv_timeout(Duration::from_secs(5)).ok() };
// A video's size is known only once the player has looked: its window starts at 16:9,
// as big as the screen share allows, and keeps its size, as for pictures.
let fallback = if first_media { (7680, 4320) } else { (1, 1) };
let img = info.as_ref().map(|i| (i.width, i.height)).filter(|&(w, h)| w > 0 && h > 0).unwrap_or(fallback);
let (lw, lh) = window_for(img, screen, scale);
let phys = ((lw as f64 * scale).round() as u32, (lh as f64 * scale).round() as u32);
let _ = box_tx.send(Want::Fit(phys.0, phys.1));
mark(stamp, "headers read");

let mut app = App::new(o.files, o.start, colors, loader, threads);
let entry = app.pictures.entry(first.clone()).or_default();
entry.asked = true;
entry.info = info;
if !first_media {
let entry = app.pictures.entry(first.clone()).or_default();
entry.asked = true;
entry.info = info;
}
app.title = app.window_title();
wl.open(&app.title, APP_ID, lw, lh, scale);
let (pw, ph) = wl.physical_size();
Expand Down Expand Up @@ -268,6 +283,9 @@ fn run(o: Opening) -> i32 {
app.set_files(files);
folder = None;
}
if wl.state.configured {
app.sync_media(&mut wl);
}
if let Some(a) = &a11y {
let reqs: Vec<_> = a.actions.try_iter().collect();
for r in reqs {
Expand Down Expand Up @@ -297,7 +315,7 @@ fn run(o: Opening) -> i32 {
if wl.state.configured && configured_at.is_none() {
configured_at = Some(now);
}
let waiting_for_first = first_frame && app.current().is_some_and(|p| p.screen.is_none() && p.error.is_none()) && configured_at.is_some_and(|t| now < t + FIRST_FRAME_WAIT);
let waiting_for_first = first_frame && !first_media && app.current().is_some_and(|p| p.screen.is_none() && p.error.is_none()) && configured_at.is_some_and(|t| now < t + FIRST_FRAME_WAIT);
if wl.state.configured && !waiting_for_first && (wl.must_paint() || (wl.can_paint() && !app.damage.is_empty())) {
if wl.must_paint() || first_frame {
app.damage = canvas::Rect::new(0, 0, app.win.0 as i32, app.win.1 as i32);
Expand Down Expand Up @@ -384,6 +402,10 @@ fn run(o: Opening) -> i32 {
fds.push(PollFd::from_borrowed_fd(w.as_fd(), PollFlags::IN));
fds.len() - 1
});
// The player's messages: read on the next turn, by sync_media.
if let Some(f) = app.media.as_ref().and_then(|m| m.session_fd()) {
fds.push(PollFd::from_borrowed_fd(f, PollFlags::IN));
}
let _ = poll(&mut fds, timeout.as_ref());
let ready = |i: usize| fds[i].revents().intersects(PollFlags::IN | PollFlags::ERR | PollFlags::HUP);
let wl_ready = ready(0);
Expand Down
Loading
Loading