about summary refs log tree commit diff stats
path: root/crates/libmpv2
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--crates/libmpv2/Cargo.toml1
-rw-r--r--crates/libmpv2/examples/events.rs34
-rw-r--r--crates/libmpv2/examples/opengl.rs19
-rw-r--r--crates/libmpv2/src/lib.rs2
-rw-r--r--crates/libmpv2/src/mpv.rs41
-rw-r--r--crates/libmpv2/src/mpv/errors.rs95
-rw-r--r--crates/libmpv2/src/mpv/events.rs4
-rw-r--r--crates/libmpv2/src/mpv/protocol.rs12
-rw-r--r--crates/libmpv2/src/mpv/raw_error_warning.txt5
-rw-r--r--crates/libmpv2/src/mpv/raw_error_warning.txt.license (renamed from package/blake3/add_cargo_lock.patch.license)2
-rw-r--r--crates/libmpv2/src/mpv/render.rs4
-rw-r--r--crates/libmpv2/src/tests.rs24
12 files changed, 144 insertions, 99 deletions
diff --git a/crates/libmpv2/Cargo.toml b/crates/libmpv2/Cargo.toml
index a8a4ed6..fb2f5bf 100644
--- a/crates/libmpv2/Cargo.toml
+++ b/crates/libmpv2/Cargo.toml
@@ -24,7 +24,6 @@ publish = false
 
 [dependencies]
 libmpv2-sys = { path = "libmpv2-sys" }
-thiserror = "2.0.7"
 log.workspace = true
 
 [dev-dependencies]
diff --git a/crates/libmpv2/examples/events.rs b/crates/libmpv2/examples/events.rs
index 8f7c79f..e502d5c 100644
--- a/crates/libmpv2/examples/events.rs
+++ b/crates/libmpv2/examples/events.rs
@@ -45,25 +45,27 @@ fn main() -> Result<()> {
             // Trigger `Event::EndFile`.
             mpv.command("playlist-next", &["force"]).unwrap();
         });
-        scope.spawn(move |_| loop {
-            let ev = ev_ctx.wait_event(600.).unwrap_or(Err(Error::Null));
+        scope.spawn(move |_| {
+            loop {
+                let ev = ev_ctx.wait_event(600.).unwrap_or(Err(Error::Null));
 
-            match ev {
-                Ok(Event::EndFile(r)) => {
-                    println!("Exiting! Reason: {:?}", r);
-                    break;
-                }
+                match ev {
+                    Ok(Event::EndFile(r)) => {
+                        println!("Exiting! Reason: {:?}", r);
+                        break;
+                    }
 
-                Ok(Event::PropertyChange {
-                    name: "demuxer-cache-state",
-                    change: PropertyData::Node(mpv_node),
-                    ..
-                }) => {
-                    let ranges = seekable_ranges(mpv_node);
-                    println!("Seekable ranges updated: {:?}", ranges);
+                    Ok(Event::PropertyChange {
+                        name: "demuxer-cache-state",
+                        change: PropertyData::Node(mpv_node),
+                        ..
+                    }) => {
+                        let ranges = seekable_ranges(mpv_node);
+                        println!("Seekable ranges updated: {:?}", ranges);
+                    }
+                    Ok(e) => println!("Event triggered: {:?}", e),
+                    Err(e) => println!("Event errored: {:?}", e),
                 }
-                Ok(e) => println!("Event triggered: {:?}", e),
-                Err(e) => println!("Event errored: {:?}", e),
             }
         });
     })
diff --git a/crates/libmpv2/examples/opengl.rs b/crates/libmpv2/examples/opengl.rs
index 1de307f..8eb9647 100644
--- a/crates/libmpv2/examples/opengl.rs
+++ b/crates/libmpv2/examples/opengl.rs
@@ -9,8 +9,8 @@
 // If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
 
 use libmpv2::{
-    render::{OpenGLInitParams, RenderContext, RenderParam, RenderParamApiType},
     Mpv,
+    render::{OpenGLInitParams, RenderContext, RenderParam, RenderParamApiType},
 };
 use std::{env, ffi::c_void};
 
@@ -38,16 +38,13 @@ fn main() {
         Ok(())
     })
     .unwrap();
-    let mut render_context = RenderContext::new(
-        unsafe { mpv.ctx.as_mut() },
-        vec![
-            RenderParam::ApiType(RenderParamApiType::OpenGl),
-            RenderParam::InitParams(OpenGLInitParams {
-                get_proc_address,
-                ctx: video,
-            }),
-        ],
-    )
+    let mut render_context = RenderContext::new(unsafe { mpv.ctx.as_mut() }, vec![
+        RenderParam::ApiType(RenderParamApiType::OpenGl),
+        RenderParam::InitParams(OpenGLInitParams {
+            get_proc_address,
+            ctx: video,
+        }),
+    ])
     .expect("Failed creating render context");
 
     event_subsystem
diff --git a/crates/libmpv2/src/lib.rs b/crates/libmpv2/src/lib.rs
index 4d8d18a..d47e620 100644
--- a/crates/libmpv2/src/lib.rs
+++ b/crates/libmpv2/src/lib.rs
@@ -67,8 +67,8 @@ pub mod mpv_error {
     pub use libmpv2_sys::mpv_error_MPV_ERROR_INVALID_PARAMETER as InvalidParameter;
     pub use libmpv2_sys::mpv_error_MPV_ERROR_LOADING_FAILED as LoadingFailed;
     pub use libmpv2_sys::mpv_error_MPV_ERROR_NOMEM as NoMem;
-    pub use libmpv2_sys::mpv_error_MPV_ERROR_NOTHING_TO_PLAY as NothingToPlay;
     pub use libmpv2_sys::mpv_error_MPV_ERROR_NOT_IMPLEMENTED as NotImplemented;
+    pub use libmpv2_sys::mpv_error_MPV_ERROR_NOTHING_TO_PLAY as NothingToPlay;
     pub use libmpv2_sys::mpv_error_MPV_ERROR_OPTION_ERROR as OptionError;
     pub use libmpv2_sys::mpv_error_MPV_ERROR_OPTION_FORMAT as OptionFormat;
     pub use libmpv2_sys::mpv_error_MPV_ERROR_OPTION_NOT_FOUND as OptionNotFound;
diff --git a/crates/libmpv2/src/mpv.rs b/crates/libmpv2/src/mpv.rs
index 07d0976..29dac8d 100644
--- a/crates/libmpv2/src/mpv.rs
+++ b/crates/libmpv2/src/mpv.rs
@@ -184,7 +184,7 @@ pub mod mpv_node {
 
     pub mod sys_node {
         use super::{DropWrapper, MpvNode, MpvNodeArrayIter, MpvNodeMapIter};
-        use crate::{mpv_error, mpv_format, Error, Result};
+        use crate::{Error, Result, mpv_error, mpv_format};
         use std::rc::Rc;
 
         #[derive(Debug, Clone)]
@@ -375,14 +375,14 @@ unsafe impl SetData for String {
 /// Wrapper around an `&str` returned by mpv, that properly deallocates it with mpv's allocator.
 #[derive(Debug, Hash, Eq, PartialEq)]
 pub struct MpvStr<'a>(&'a str);
-impl<'a> Deref for MpvStr<'a> {
+impl Deref for MpvStr<'_> {
     type Target = str;
 
     fn deref(&self) -> &str {
         self.0
     }
 }
-impl<'a> Drop for MpvStr<'a> {
+impl Drop for MpvStr<'_> {
     fn drop(&mut self) {
         unsafe { libmpv2_sys::mpv_free(self.0.as_ptr() as *mut u8 as _) };
     }
@@ -403,7 +403,7 @@ unsafe impl<'a> GetData for MpvStr<'a> {
     }
 }
 
-unsafe impl<'a> SetData for &'a str {
+unsafe impl SetData for &str {
     fn call_as_c_void<T, F: FnMut(*mut ctype::c_void) -> Result<T>>(self, mut fun: F) -> Result<T> {
         let string = CString::new(self)?;
         fun((&mut string.as_ptr()) as *mut *const ctype::c_char as *mut _)
@@ -511,9 +511,8 @@ impl Mpv {
         }
 
         initializer(MpvInitializer { ctx })?;
-        mpv_err((), unsafe { libmpv2_sys::mpv_initialize(ctx) }).map_err(|err| {
+        mpv_err((), unsafe { libmpv2_sys::mpv_initialize(ctx) }).inspect_err(|_| {
             unsafe { libmpv2_sys::mpv_terminate_destroy(ctx) };
-            err
         })?;
 
         let ctx = unsafe { NonNull::new_unchecked(ctx) };
@@ -526,19 +525,6 @@ impl Mpv {
         })
     }
 
-    /// Execute a command
-    pub fn execute(&self, name: &str, args: &[&str]) -> Result<()> {
-        if args.is_empty() {
-            debug!("Running mpv command: '{}'", name);
-        } else {
-            debug!("Running mpv command: '{} {}'", name, args.join(" "));
-        }
-
-        self.command(name, args)?;
-
-        Ok(())
-    }
-
     /// Load a configuration file. The path has to be absolute, and a file.
     pub fn load_config(&self, path: &str) -> Result<()> {
         let file = CString::new(path)?.into_raw();
@@ -562,7 +548,7 @@ impl Mpv {
     /// Send a command to the `Mpv` instance. This uses `mpv_command_string` internally,
     /// so that the syntax is the same as described in the [manual for the input.conf](https://mpv.io/manual/master/#list-of-input-commands).
     ///
-    /// Note that you may have to escape strings with `""` when they contain spaces.
+    /// Note that this function escapes the arguments for you.
     ///
     /// # Examples
     ///
@@ -583,12 +569,19 @@ impl Mpv {
     /// # }
     /// ```
     pub fn command(&self, name: &str, args: &[&str]) -> Result<()> {
-        let mut cmd = name.to_owned();
+        fn escape(input: &str) -> String {
+            input.replace('"', "\\\"")
+        }
+
+        let mut cmd = escape(name);
 
         for elem in args {
             cmd.push(' ');
-            cmd.push_str(elem);
+            cmd.push('"');
+            cmd.push_str(&escape(elem));
+            cmd.push('"');
         }
+        debug!("Running mpv command: '{}'", cmd);
 
         let raw = CString::new(cmd)?;
         mpv_err((), unsafe {
@@ -597,7 +590,9 @@ impl Mpv {
     }
 
     /// Set the value of a property.
-    pub fn set_property<T: SetData>(&self, name: &str, data: T) -> Result<()> {
+    pub fn set_property<T: SetData + std::fmt::Display>(&self, name: &str, data: T) -> Result<()> {
+        debug!("Setting mpv property: '{name}' = '{data}'");
+
         let name = CString::new(name)?;
         let format = T::get_format().as_mpv_format() as _;
         data.call_as_c_void(|ptr| {
diff --git a/crates/libmpv2/src/mpv/errors.rs b/crates/libmpv2/src/mpv/errors.rs
index a2baee5..a2d3dd8 100644
--- a/crates/libmpv2/src/mpv/errors.rs
+++ b/crates/libmpv2/src/mpv/errors.rs
@@ -8,36 +8,52 @@
 // You should have received a copy of the License along with this program.
 // If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
 
-use std::{ffi::NulError, os::raw as ctype, str::Utf8Error};
-
-use thiserror::Error;
+use std::{ffi::NulError, fmt::Display, os::raw as ctype, str::Utf8Error};
 
 use super::mpv_error;
 
 #[allow(missing_docs)]
 pub type Result<T> = ::std::result::Result<T, Error>;
 
-#[derive(Error, Debug)]
+#[derive(Debug)]
 pub enum Error {
-    #[error("loading file failed: {error}")]
-    Loadfile { error: String },
+    Loadfile {
+        error: String,
+    },
 
-    #[error("version mismatch detected! Linked version ({linked}) is unequal to the loaded version ({loaded})")]
     VersionMismatch {
         linked: ctype::c_ulong,
         loaded: ctype::c_ulong,
     },
 
-    #[error("invalid utf8 returned")]
     InvalidUtf8,
 
-    #[error("null pointer returned")]
     Null,
 
-    #[error("raw error returned: {}", to_string_mpv_error(*(.0)))]
     Raw(crate::MpvError),
 }
 
+impl std::error::Error for Error {}
+
+impl Display for Error {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        match self {
+            Error::Loadfile { error } => write!(f, "loading file failed: {error}"),
+            Error::VersionMismatch { linked, loaded } => write!(
+                f,
+                "version mismatch detected! Linked version ({linked}) is unequal to the loaded version ({loaded})"
+            ),
+            Error::InvalidUtf8 => f.write_str("invalid utf8 returned"),
+            Error::Null => f.write_str("null pointer returned"),
+            Error::Raw(raw) => write!(
+                f,
+                include_str!("./raw_error_warning.txt"),
+                to_string_mpv_error(*(raw))
+            ),
+        }
+    }
+}
+
 impl From<NulError> for Error {
     fn from(_other: NulError) -> Error {
         Error::Null
@@ -76,35 +92,70 @@ fn to_string_mpv_error_raw(num: crate::MpvError) -> (&'static str, &'static str)
 
         mpv_error::NoMem => ("Memory allocation failed.", ""),
 
-        mpv_error::Uninitialized => ("The mpv core wasn't configured and initialized yet", " See the notes in mpv_create()."),
+        mpv_error::Uninitialized => (
+            "The mpv core wasn't configured and initialized yet",
+            " See the notes in mpv_create().",
+        ),
 
-        mpv_error::InvalidParameter => ("Generic catch-all error if a parameter is set to an invalid or unsupported value.", "This is used if there is no better error code."),
+        mpv_error::InvalidParameter => (
+            "Generic catch-all error if a parameter is set to an invalid or unsupported value.",
+            "This is used if there is no better error code.",
+        ),
 
         mpv_error::OptionNotFound => ("Trying to set an option that doesn't exist.", ""),
-        mpv_error::OptionFormat => ("Trying to set an option using an unsupported MPV_FORMAT.", ""),
-        mpv_error::OptionError => ("Setting the option failed", " Typically this happens if the provided option value could not be parsed."),
+        mpv_error::OptionFormat => (
+            "Trying to set an option using an unsupported MPV_FORMAT.",
+            "",
+        ),
+        mpv_error::OptionError => (
+            "Setting the option failed",
+            " Typically this happens if the provided option value could not be parsed.",
+        ),
 
         mpv_error::PropertyNotFound => ("The accessed property doesn't exist.", ""),
-        mpv_error::PropertyFormat => ("Trying to set or get a property using an unsupported MPV_FORMAT.", ""),
-        mpv_error::PropertyUnavailable => ("The property exists, but is not available", "This usually happens when the associated subsystem is not active, e.g. querying audio parameters while audio is disabled."),
+        mpv_error::PropertyFormat => (
+            "Trying to set or get a property using an unsupported MPV_FORMAT.",
+            "",
+        ),
+        mpv_error::PropertyUnavailable => (
+            "The property exists, but is not available",
+            "This usually happens when the associated subsystem is not active, e.g. querying audio parameters while audio is disabled.",
+        ),
         mpv_error::PropertyError => ("Error setting or getting a property.", ""),
 
-        mpv_error::Command => ("General error when running a command with mpv_command and similar.", ""),
+        mpv_error::Command => (
+            "General error when running a command with mpv_command and similar.",
+            "",
+        ),
 
-        mpv_error::LoadingFailed => ("Generic error on loading (usually used with mpv_event_end_file.error).", ""),
+        mpv_error::LoadingFailed => (
+            "Generic error on loading (usually used with mpv_event_end_file.error).",
+            "",
+        ),
 
         mpv_error::AoInitFailed => ("Initializing the audio output failed.", ""),
         mpv_error::VoInitFailed => ("Initializing the video output failed.", ""),
 
-        mpv_error::NothingToPlay => ("There was no audio or video data to play", "This also happens if the file was recognized, but did not contain any audio or video streams, or no streams were selected."),
+        mpv_error::NothingToPlay => (
+            "There was no audio or video data to play",
+            "This also happens if the file was recognized, but did not contain any audio or video streams, or no streams were selected.",
+        ),
 
-        mpv_error::UnknownFormat => ("     * When trying to load the file, the file format could not be determined, or the file was too broken to open it.", ""),
+        mpv_error::UnknownFormat => (
+            "     * When trying to load the file, the file format could not be determined, or the file was too broken to open it.",
+            "",
+        ),
 
-        mpv_error::Generic => ("Generic error for signaling that certain system requirements are not fulfilled.", ""),
+        mpv_error::Generic => (
+            "Generic error for signaling that certain system requirements are not fulfilled.",
+            "",
+        ),
         mpv_error::NotImplemented => ("The API function which was called is a stub only", ""),
         mpv_error::Unsupported => ("Unspecified error.", ""),
 
-        mpv_error::Success => unreachable!("This is not an error. It's just here, to ensure that the 0 case marks an success'"),
+        mpv_error::Success => unreachable!(
+            "This is not an error. It's just here, to ensure that the 0 case marks an success'"
+        ),
         _ => unreachable!("Mpv seems to have changed it's constants."),
     }
 }
diff --git a/crates/libmpv2/src/mpv/events.rs b/crates/libmpv2/src/mpv/events.rs
index 6fb4683..e27da2c 100644
--- a/crates/libmpv2/src/mpv/events.rs
+++ b/crates/libmpv2/src/mpv/events.rs
@@ -11,7 +11,7 @@
 use crate::mpv_node::sys_node::SysMpvNode;
 use crate::{mpv::mpv_err, *};
 
-use std::ffi::{c_void, CString};
+use std::ffi::{CString, c_void};
 use std::os::raw as ctype;
 use std::ptr::NonNull;
 use std::slice;
@@ -86,7 +86,7 @@ impl<'a> PropertyData<'a> {
             mpv_format::Node => {
                 let sys_node = *(ptr as *mut libmpv2_sys::mpv_node);
                 let node = SysMpvNode::new(sys_node, false);
-                return Ok(PropertyData::Node(node.value().unwrap()));
+                Ok(PropertyData::Node(node.value().unwrap()))
             }
             mpv_format::None => unreachable!(),
             _ => unimplemented!(),
diff --git a/crates/libmpv2/src/mpv/protocol.rs b/crates/libmpv2/src/mpv/protocol.rs
index 31a5933..ec840d8 100644
--- a/crates/libmpv2/src/mpv/protocol.rs
+++ b/crates/libmpv2/src/mpv/protocol.rs
@@ -17,7 +17,7 @@ use std::os::raw as ctype;
 use std::panic;
 use std::panic::RefUnwindSafe;
 use std::slice;
-use std::sync::{atomic::Ordering, Mutex};
+use std::sync::{Mutex, atomic::Ordering};
 
 impl Mpv {
     /// Create a context with which custom protocols can be registered.
@@ -101,11 +101,7 @@ where
         let slice = slice::from_raw_parts_mut(buf, nbytes as _);
         ((*data).read_fn)(&mut *(*data).cookie, slice)
     });
-    if let Ok(ret) = ret {
-        ret
-    } else {
-        -1
-    }
+    ret.unwrap_or(-1)
 }
 
 unsafe extern "C" fn seek_wrapper<T, U>(cookie: *mut ctype::c_void, offset: i64) -> i64
@@ -177,8 +173,8 @@ pub struct ProtocolContext<'parent, T: RefUnwindSafe, U: RefUnwindSafe> {
     _does_not_outlive: PhantomData<&'parent Mpv>,
 }
 
-unsafe impl<'parent, T: RefUnwindSafe, U: RefUnwindSafe> Send for ProtocolContext<'parent, T, U> {}
-unsafe impl<'parent, T: RefUnwindSafe, U: RefUnwindSafe> Sync for ProtocolContext<'parent, T, U> {}
+unsafe impl<T: RefUnwindSafe, U: RefUnwindSafe> Send for ProtocolContext<'_, T, U> {}
+unsafe impl<T: RefUnwindSafe, U: RefUnwindSafe> Sync for ProtocolContext<'_, T, U> {}
 
 impl<'parent, T: RefUnwindSafe, U: RefUnwindSafe> ProtocolContext<'parent, T, U> {
     fn new(
diff --git a/crates/libmpv2/src/mpv/raw_error_warning.txt b/crates/libmpv2/src/mpv/raw_error_warning.txt
new file mode 100644
index 0000000..277500a
--- /dev/null
+++ b/crates/libmpv2/src/mpv/raw_error_warning.txt
@@ -0,0 +1,5 @@
+Raw mpv error: {}
+
+This error is directly returned from `mpv`.
+This is probably caused by a bug in `yt`, please open an issue about
+this and try to replicate it with the `-vvvv` verbosity setting.
diff --git a/package/blake3/add_cargo_lock.patch.license b/crates/libmpv2/src/mpv/raw_error_warning.txt.license
index d4d410f..7813eb6 100644
--- a/package/blake3/add_cargo_lock.patch.license
+++ b/crates/libmpv2/src/mpv/raw_error_warning.txt.license
@@ -1,6 +1,6 @@
 yt - A fully featured command line YouTube client
 
-Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
+Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
 SPDX-License-Identifier: GPL-3.0-or-later
 
 This file is part of Yt.
diff --git a/crates/libmpv2/src/mpv/render.rs b/crates/libmpv2/src/mpv/render.rs
index c3f2dc9..6457048 100644
--- a/crates/libmpv2/src/mpv/render.rs
+++ b/crates/libmpv2/src/mpv/render.rs
@@ -8,9 +8,9 @@
 // You should have received a copy of the License along with this program.
 // If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
 
-use crate::{mpv::mpv_err, Error, Result};
+use crate::{Error, Result, mpv::mpv_err};
 use std::collections::HashMap;
-use std::ffi::{c_void, CStr};
+use std::ffi::{CStr, c_void};
 use std::os::raw::c_int;
 use std::ptr;
 
diff --git a/crates/libmpv2/src/tests.rs b/crates/libmpv2/src/tests.rs
index 68753fc..6106eb2 100644
--- a/crates/libmpv2/src/tests.rs
+++ b/crates/libmpv2/src/tests.rs
@@ -54,10 +54,10 @@ fn properties() {
         0.6,
         f64::round(subg * f64::powi(10.0, 4)) / f64::powi(10.0, 4)
     );
-    mpv.command(
-        "loadfile",
-        &["test-data/speech_12kbps_mb.wav", "append-play"],
-    )
+    mpv.command("loadfile", &[
+        "test-data/speech_12kbps_mb.wav",
+        "append-play",
+    ])
     .unwrap();
     thread::sleep(Duration::from_millis(250));
 
@@ -185,10 +185,10 @@ fn events() {
 fn node_map() {
     let mpv = Mpv::new().unwrap();
 
-    mpv.command(
-        "loadfile",
-        &["test-data/speech_12kbps_mb.wav", "append-play"],
-    )
+    mpv.command("loadfile", &[
+        "test-data/speech_12kbps_mb.wav",
+        "append-play",
+    ])
     .unwrap();
 
     thread::sleep(Duration::from_millis(250));
@@ -217,10 +217,10 @@ fn node_map() {
 fn node_array() -> Result<()> {
     let mpv = Mpv::new()?;
 
-    mpv.command(
-        "loadfile",
-        &["test-data/speech_12kbps_mb.wav", "append-play"],
-    )
+    mpv.command("loadfile", &[
+        "test-data/speech_12kbps_mb.wav",
+        "append-play",
+    ])
     .unwrap();
 
     thread::sleep(Duration::from_millis(250));