Project setup and basic event logging

This commit is contained in:
Ryze 2023-02-04 03:04:26 +03:00
parent f0d6b7e52f
commit dfc4b30d4a
6 changed files with 89 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
/Cargo.lock

11
Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "mpv-rpc"
version = "0.1.0"
edition = "2021"
[lib]
name = "mpv_rpc"
crate-type = ["cdylib"]
[dependencies]
mpv-client = "0.4.0"

10
install_lib.sh Executable file
View file

@ -0,0 +1,10 @@
#!/bin/bash
TARGET=$1
if [[ -z "$TARGET" ]]
then
TARGET="debug"
fi
echo "Copying library to mpv scripts directory..."
cp "target/$TARGET/libmpv_rpc.so" "$HOME/.config/mpv/scripts/"

2
rm_lib.sh Executable file
View file

@ -0,0 +1,2 @@
#!/bin/bash
rm "$HOME/.config/mpv/scripts/libmpv_rpc.so" > /dev/null 2>&1

12
src/lib.rs Normal file
View file

@ -0,0 +1,12 @@
use mpv_client::mpv_handle;
mod mpv_event_handler;
#[no_mangle]
fn mpv_open_cplugin(handle: *mut mpv_handle) -> std::os::raw::c_int {
let client = mpv_event_handler::MpvHandler::from_ptr(handle);
while client.poll_events() {
}
return 0;
}

52
src/mpv_event_handler.rs Normal file
View file

@ -0,0 +1,52 @@
use mpv_client::{Handle, Event, Property, mpv_handle};
pub struct MpvHandler {
mpv: Handle
}
impl MpvHandler {
pub fn new(mpv: Handle) -> Self {
Self {
mpv
}
}
pub fn from_ptr(handle: *mut mpv_handle) -> Self {
MpvHandler::new(Handle::from_ptr(handle))
}
#[allow(dead_code)]
fn observe_property(&self, id: u64, name: &str, format: i32) -> Result<(), String>{
match self.mpv.observe_property(id, name, format) {
Ok(_) => Ok(()),
Err(_) => Err(format!("Couldn't observe property: {name} (id: {id})"))
}
}
#[allow(dead_code)]
fn on_property_change(&self, prop_id: u64, _prop: Property) {
println!("[RPC] Property changed: {prop_id}");
match prop_id {
_ => ()
}
}
fn handle_event(&self, event: Event) {
println!("[RPC] Event: {event}");
match event {
_ => ()
}
}
// TODO!
// Add logging
pub fn poll_events(&self) -> bool {
match self.mpv.wait_event(0.0) {
Event::None => (),
Event::Shutdown => return false,
Event::PropertyChange(prop_id, prop) => self.on_property_change(prop_id, prop),
event => self.handle_event(event)
}
true
}
}