use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicU64, Ordering};
use super::{
DeviceCapabilities, JobId, JobStatus, PrinterId, PrinterInfo, SpoolJob, SpoolPayload, Spooler,
};
use crate::print::paper::{Margins, PaperSize};
use crate::print::{Duplex, Orientation, PageRange, PrintError, PrintOptions, Scaling};
#[derive(Debug, Default)]
pub struct CupsSpooler {
_private: (),
}
impl CupsSpooler {
#[must_use]
pub fn new() -> Self {
Self { _private: () }
}
}
impl Spooler for CupsSpooler {
fn printers(&self) -> Result<Vec<PrinterInfo>, PrintError> {
let names = capture("lpstat", ["-e"]).unwrap_or_default();
let states = capture("lpstat", ["-p"]).unwrap_or_default();
let default = capture("lpstat", ["-d"]).unwrap_or_default();
Ok(parse_printers(&names, &states, &default))
}
fn capabilities(&self, printer: &PrinterId) -> Result<DeviceCapabilities, PrintError> {
let text = capture("lpoptions", ["-p", printer.as_str(), "-l"]).ok_or_else(|| {
PrintError::NoSuchPrinter(format!(
"{printer}: lpoptions is unavailable or the queue does not exist"
))
})?;
Ok(parse_lpoptions(&text))
}
fn submit(&self, job: SpoolJob<'_>) -> Result<JobId, PrintError> {
match job.payload {
SpoolPayload::PassThroughPdf(bytes) => {
let args = build_lp_args(
&job.printer,
&job.title,
job.options,
LpMode::PassThroughPdf,
&[],
);
let stdout = run_lp(&args, Some(bytes))?;
job_id_from_lp(&stdout)
}
SpoolPayload::Sheets {
session,
sheets,
compose,
} => {
let staging = staging_dir()?;
let result = self.submit_sheets(&job, session, sheets, compose, &staging);
let _ = std::fs::remove_dir_all(&staging);
result
}
}
}
fn status(&self, job: &JobId) -> Result<JobStatus, PrintError> {
let Some(text) = capture("lpstat", ["-W", "not-completed", "-o", job.0.as_str()]) else {
return Ok(JobStatus::Unknown);
};
Ok(parse_job_status(&text, job))
}
fn cancel(&self, job: &JobId) -> Result<(), PrintError> {
let output = Command::new("cancel")
.arg(&job.0)
.output()
.map_err(|e| PrintError::Spool(format!("cancel {job}: {e}")))?;
if output.status.success() {
Ok(())
} else {
Err(PrintError::Spool(format!(
"cancel {job} failed: {}",
String::from_utf8_lossy(&output.stderr).trim()
)))
}
}
}
impl CupsSpooler {
fn submit_sheets(
&self,
job: &SpoolJob<'_>,
session: &crate::reader::RenderSession,
sheets: &[crate::print::Sheet],
compose: crate::print::ComposeOptions,
staging: &Path,
) -> Result<JobId, PrintError> {
if sheets.is_empty() {
return Err(PrintError::EmptyRange);
}
let mut files = Vec::with_capacity(sheets.len());
for (n, sheet) in sheets.iter().enumerate() {
let path = staging.join(format!("sheet-{:04}.png", n + 1));
super::file::write_composed_sheet_png(&path, session, sheet, &compose)?;
files.push(path);
}
let args = build_lp_args(
&job.printer,
&job.title,
job.options,
LpMode::ComposedSheets,
&files,
);
let stdout = run_lp(&args, None)?;
job_id_from_lp(&stdout)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LpMode {
PassThroughPdf,
ComposedSheets,
}
#[must_use]
pub fn build_lp_args(
printer: &PrinterId,
title: &str,
options: &PrintOptions,
mode: LpMode,
files: &[PathBuf],
) -> Vec<String> {
let pass_through = mode == LpMode::PassThroughPdf;
let mut args = Vec::new();
args.push("-d".to_string());
args.push(printer.as_str().to_string());
if !title.is_empty() {
args.push("-t".to_string());
args.push(title.to_string());
}
if options.copies > 1 {
args.push("-n".to_string());
args.push(options.copies.to_string());
opt(
&mut args,
format!("Collate={}", if options.collate { "True" } else { "False" }),
);
}
opt(&mut args, format!("sides={}", sides_value(options.duplex)));
if let Some(media) = media_value(options.paper) {
opt(&mut args, format!("media={media}"));
}
if pass_through {
match options.orientation {
Orientation::Landscape => opt(&mut args, "orientation-requested=4".to_string()),
Orientation::Portrait => opt(&mut args, "orientation-requested=3".to_string()),
Orientation::Auto => {}
}
}
match (pass_through, options.scaling) {
(_, Scaling::FitToPage | Scaling::ShrinkToFit | Scaling::FillPage) => {
opt(&mut args, "fit-to-page".to_string());
}
(true, Scaling::Percent(p)) => {
opt(&mut args, format!("scaling={}", format_number(p * 100.0)));
}
(_, Scaling::ActualSize) | (false, Scaling::Percent(_)) => {
if !pass_through {
opt(&mut args, "fit-to-page".to_string());
}
}
}
if options.grayscale {
opt(&mut args, "print-color-mode=monochrome".to_string());
}
if pass_through {
match &options.range {
PageRange::All => {}
PageRange::Odd => opt(&mut args, "page-set=odd".to_string()),
PageRange::Even => opt(&mut args, "page-set=even".to_string()),
PageRange::Spans(spans) => {
if let Some(value) = page_ranges_value(spans) {
opt(&mut args, format!("page-ranges={value}"));
}
}
}
if options.reverse {
opt(&mut args, "outputorder=reverse".to_string());
}
}
for file in files {
args.push(file.display().to_string());
}
args
}
fn opt(args: &mut Vec<String>, value: String) {
args.push("-o".to_string());
args.push(value);
}
fn sides_value(duplex: Duplex) -> &'static str {
match duplex {
Duplex::None => "one-sided",
Duplex::LongEdge => "two-sided-long-edge",
Duplex::ShortEdge => "two-sided-short-edge",
}
}
fn media_value(paper: PaperSize) -> Option<String> {
if let Some(name) = paper.ipp_name() {
return Some(name.to_string());
}
let (w, h) = paper.size();
Some(format!("Custom.{}x{}", format_number(w), format_number(h)))
}
fn page_ranges_value(spans: &[(u32, u32)]) -> Option<String> {
if spans.is_empty() {
return None;
}
let mut parts = Vec::with_capacity(spans.len());
for &(start, end) in spans {
let start = start.max(1);
if start == end {
parts.push(start.to_string());
} else {
parts.push(format!("{start}-{end}"));
}
}
Some(parts.join(","))
}
fn format_number(value: f64) -> String {
let text = format!("{value:.2}");
let trimmed = text.trim_end_matches('0').trim_end_matches('.');
if trimmed.is_empty() {
"0".to_string()
} else {
trimmed.to_string()
}
}
#[must_use]
pub fn parse_printers(lpstat_e: &str, lpstat_p: &str, lpstat_d: &str) -> Vec<PrinterInfo> {
let mut names = parse_queue_names(lpstat_e);
if names.is_empty() {
names = lpstat_p
.lines()
.filter(|line| !line.starts_with(char::is_whitespace))
.filter_map(|line| line.split_whitespace().nth(1))
.map(str::to_string)
.collect();
names.dedup();
}
let default = parse_default_queue(lpstat_d);
names
.into_iter()
.map(|name| {
let accepting = queue_is_enabled(lpstat_p, &name);
PrinterInfo {
is_default: default.as_deref() == Some(name.as_str()),
id: PrinterId::new(name),
description: None,
location: None,
accepting_jobs: accepting,
}
})
.collect()
}
#[must_use]
pub fn parse_queue_names(lpstat_e: &str) -> Vec<String> {
lpstat_e
.lines()
.map(str::trim)
.filter(|line| !line.is_empty() && !line.contains(char::is_whitespace))
.map(str::to_string)
.collect()
}
#[must_use]
pub fn parse_default_queue(lpstat_d: &str) -> Option<String> {
lpstat_d.lines().find_map(|line| {
let (_, tail) = line.rsplit_once(':')?;
let tail = tail.trim();
if tail.is_empty() || tail.contains(char::is_whitespace) {
return None;
}
Some(tail.to_string())
})
}
fn queue_is_enabled(lpstat_p: &str, name: &str) -> bool {
for line in lpstat_p.lines() {
if line.starts_with(char::is_whitespace) {
continue;
}
let mut tokens = line.split_whitespace();
if !tokens.any(|token| token == name) {
continue;
}
return !line
.split_whitespace()
.any(|token| token.eq_ignore_ascii_case("disabled"));
}
true
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LpOption {
pub keyword: String,
pub choices: Vec<String>,
pub current: Option<String>,
}
#[must_use]
pub fn parse_lpoptions_entries(text: &str) -> Vec<LpOption> {
let mut out = Vec::new();
for line in text.lines() {
let Some((left, right)) = line.split_once(':') else {
continue;
};
let keyword = left.split('/').next().unwrap_or(left).trim();
if keyword.is_empty() || keyword.contains(char::is_whitespace) {
continue;
}
let mut choices = Vec::new();
let mut current = None;
for token in right.split_whitespace() {
if let Some(starred) = token.strip_prefix('*') {
if starred.is_empty() {
continue;
}
current = Some(starred.to_string());
choices.push(starred.to_string());
} else {
choices.push(token.to_string());
}
}
if choices.is_empty() {
continue;
}
out.push(LpOption {
keyword: keyword.to_string(),
choices,
current,
});
}
out
}
#[must_use]
pub fn parse_lpoptions(text: &str) -> DeviceCapabilities {
let entries = parse_lpoptions_entries(text);
let find = |names: &[&str]| {
entries
.iter()
.find(|e| names.iter().any(|n| e.keyword.eq_ignore_ascii_case(n)))
};
let supports_duplex = find(&["Duplex", "sides", "JCLDuplex"]).is_some_and(|entry| {
entry
.choices
.iter()
.any(|choice| !is_simplex_choice(choice))
});
let supports_color = find(&["ColorModel", "print-color-mode", "ColorMode"])
.is_none_or(|entry| entry.choices.iter().any(|choice| !is_mono_choice(choice)));
let resolution_dpi = find(&["Resolution", "printer-resolution"]).and_then(|entry| {
entry.current.as_deref().and_then(parse_dpi).or_else(|| {
entry
.choices
.iter()
.filter_map(|c| parse_dpi(c))
.fold(None, |acc, dpi| {
Some(acc.map_or(dpi, |best: f64| best.max(dpi)))
})
})
});
DeviceCapabilities {
hardware_margins: Margins::uniform(crate::print::paper::DEFAULT_HARDWARE_MARGIN_PT),
supports_duplex,
supports_color,
resolution_dpi,
accepts_pdf: true,
}
}
fn is_simplex_choice(choice: &str) -> bool {
matches!(
choice.to_ascii_lowercase().as_str(),
"none" | "one-sided" | "simplex" | "false" | "off"
)
}
fn is_mono_choice(choice: &str) -> bool {
matches!(
choice.to_ascii_lowercase().as_str(),
"gray"
| "grayscale"
| "greyscale"
| "mono"
| "monochrome"
| "black"
| "bi-level"
| "auto-monochrome"
| "process-monochrome"
| "kgray"
| "w"
)
}
fn parse_dpi(choice: &str) -> Option<f64> {
let digits: String = choice.chars().take_while(char::is_ascii_digit).collect();
if digits.is_empty() {
return None;
}
digits.parse::<f64>().ok().filter(|d| *d > 0.0)
}
#[must_use]
pub fn parse_lp_job_id(stdout: &str) -> Option<JobId> {
stdout
.split_whitespace()
.rfind(|token| {
let Some((name, number)) = token.rsplit_once('-') else {
return false;
};
!name.is_empty() && !number.is_empty() && number.chars().all(|c| c.is_ascii_digit())
})
.map(|token| JobId(token.to_string()))
}
#[must_use]
pub fn parse_job_status(lpstat_o: &str, job: &JobId) -> JobStatus {
let listed = lpstat_o
.lines()
.any(|line| line.split_whitespace().any(|token| token == job.0));
if listed {
JobStatus::Processing
} else {
JobStatus::Completed
}
}
fn capture<I, S>(program: &str, args: I) -> Option<String>
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
let output = Command::new(program)
.args(args)
.stdin(Stdio::null())
.output()
.ok()?;
if output.status.success() {
Some(String::from_utf8_lossy(&output.stdout).into_owned())
} else {
None
}
}
fn run_lp(args: &[String], stdin_bytes: Option<&[u8]>) -> Result<String, PrintError> {
use std::io::Write as _;
let mut command = Command::new("lp");
command
.args(args)
.stdout(Stdio::piped())
.stderr(Stdio::piped());
command.stdin(if stdin_bytes.is_some() {
Stdio::piped()
} else {
Stdio::null()
});
let mut child = command
.spawn()
.map_err(|e| PrintError::Spool(format!("could not run lp: {e}")))?;
if let Some(bytes) = stdin_bytes {
let Some(mut stdin) = child.stdin.take() else {
return Err(PrintError::Spool("lp stdin was not available".to_string()));
};
stdin
.write_all(bytes)
.map_err(|e| PrintError::Spool(format!("writing the document to lp failed: {e}")))?;
drop(stdin);
}
let output = child
.wait_with_output()
.map_err(|e| PrintError::Spool(format!("waiting for lp failed: {e}")))?;
if !output.status.success() {
return Err(PrintError::Spool(format!(
"lp exited with {}: {}",
output.status,
String::from_utf8_lossy(&output.stderr).trim()
)));
}
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
}
fn job_id_from_lp(stdout: &str) -> Result<JobId, PrintError> {
parse_lp_job_id(stdout).ok_or_else(|| {
PrintError::Spool(format!(
"lp accepted the job but printed no request id: {:?}",
stdout.trim()
))
})
}
fn staging_dir() -> Result<PathBuf, PrintError> {
static COUNTER: AtomicU64 = AtomicU64::new(0);
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or_default();
let seq = COUNTER.fetch_add(1, Ordering::Relaxed);
let dir =
std::env::temp_dir().join(format!("exprim-print-{}-{nanos}-{seq}", std::process::id()));
std::fs::create_dir_all(&dir)?;
Ok(dir)
}