Skip to content

Commit

Permalink
letsplay_retro_frontend: Move log helper to frontend/ module
Browse files Browse the repository at this point in the history
Also, "namespace" the C exports. Not really for any particular reason but it's nicer to give more unique names I suppose.
  • Loading branch information
modeco80 committed Jan 29, 2025
1 parent 01fe982 commit 7324ab3
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 41 deletions.
4 changes: 2 additions & 2 deletions crates/letsplay_retro_frontend/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ fn main() {
.emit_rerun_if_env_changed(true)
.cpp(true)
.std("c++20")
.file("src/libretro_log_helper.cpp")
.compile("retro_log_helper");
.file("src/frontend/log_helper.cpp")
.compile("letsplay_retro_frontend_cxx");
}
4 changes: 2 additions & 2 deletions crates/letsplay_retro_frontend/src/frontend/callbacks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Callbacks for libretro
use crate::{frontend::*, util};
use letsplay_libretro_sys::*;
use crate::{frontend::*, libretro_log, util};

use std::ffi;

Expand All @@ -26,7 +26,7 @@ pub(crate) unsafe extern "C" fn environment_callback(
) -> bool {
match environment_command {
ENVIRONMENT_GET_LOG_INTERFACE => {
*(data as *mut LogCallback) = libretro_log::LOG_INTERFACE.clone();
*(data as *mut LogCallback) = super::log::LOG_INTERFACE.clone();
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tracing::*;
#[allow(dead_code)] // This *is* used; just not in Rust code
#[no_mangle]
/// This recieves log messages from our C++ helper code, and pulls them out into Tracing messages.
pub extern "C" fn libretro_log_recieve(level: LogLevel, buf: *const ffi::c_char) {
pub extern "C" fn letsplay_retro_frontend_log(level: LogLevel, buf: *const ffi::c_char) {
// SAFETY: This pointer should never be null since it comes from the address of a C++ stack variable.
// we really only should get UTF-8 errors here in the case a core spits out something invalid.
unsafe {
Expand Down Expand Up @@ -42,7 +42,7 @@ extern "C" {
// because libretro_sys doesn't want it, and additionally,
// that requires nightly Rust to even do, which defeats the purpose
// of moving it into a helper.
fn libretro_log(level: LogLevel, fmt: *const ffi::c_char);
fn letsplay_retro_frontend_libretro_log(level: LogLevel, fmt: *const ffi::c_char);
}

pub static LOG_INTERFACE: LogCallback = LogCallback { log: libretro_log };
pub static LOG_INTERFACE: LogCallback = LogCallback { log: letsplay_retro_frontend_libretro_log };
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ using LibRetroLogLevel = std::uint32_t;
extern "C" {

/// This function is defined in Rust and recieves our formatted log messages.
void libretro_log_recieve(LibRetroLogLevel level, const char* buf);
void letsplay_retro_frontend_log(LibRetroLogLevel level, const char* buf);

/// This helper function is given to Rust code to implement the libretro logging
/// (because it's a C-varadic function; that requires nightly/unstable Rust)
///
/// By implementing it in C++, we can dodge all that and keep using stable rustc.
void libretro_log(LibRetroLogLevel level, const char* format, ...) {
void letsplay_retro_frontend_libretro_log(LibRetroLogLevel level, const char* format, ...) {
char buf[512]{};
va_list val;

Expand All @@ -30,6 +30,6 @@ extern "C" {
buf[n-1] = '\0';

// Call the Rust-side reciever.
return libretro_log_recieve(level, &buf[0]);
return letsplay_retro_frontend_log(level, &buf[0]);
}
}
1 change: 1 addition & 0 deletions crates/letsplay_retro_frontend/src/frontend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ mod core_variable;
pub use core_variable::*;

mod callbacks;
mod log;
5 changes: 3 additions & 2 deletions crates/letsplay_retro_frontend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! A libretro frontend as a reusable library crate.
mod libretro_log;

pub mod input_devices;
mod util;

Expand All @@ -11,3 +9,6 @@ pub mod result;
// re-export some of our useful interface
pub use frontend::{CoreVariable, Frontend, FrontendInterface};
pub use result::*;

// re-export sys crate
pub use letsplay_libretro_sys as sys;
44 changes: 15 additions & 29 deletions crates/retrodemo/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use letsplay_core::{sleep, Size, Surface};
use letsplay_retro_frontend::{
frontend::{Frontend, FrontendInterface, HwGlInitData},
input_devices::{InputDevice, RetroPad},
libretro_sys_new,
sys,
};

use minifb::Key;
Expand Down Expand Up @@ -203,68 +203,54 @@ impl FrontendInterface for App {
for key in &keys {
match key {
Key::Backslash => {
self.pad
.press_button(libretro_sys_new::DEVICE_ID_JOYPAD_SELECT, None);
self.pad.press_button(sys::DEVICE_ID_JOYPAD_SELECT, None);
}
Key::Enter => {
self.pad
.press_button(libretro_sys_new::DEVICE_ID_JOYPAD_START, None);
self.pad.press_button(sys::DEVICE_ID_JOYPAD_START, None);
}
Key::Up => {
self.pad
.press_button(libretro_sys_new::DEVICE_ID_JOYPAD_UP, None);
self.pad.press_button(sys::DEVICE_ID_JOYPAD_UP, None);
}
Key::Down => {
self.pad
.press_button(libretro_sys_new::DEVICE_ID_JOYPAD_DOWN, None);
self.pad.press_button(sys::DEVICE_ID_JOYPAD_DOWN, None);
}
Key::Left => {
self.pad
.press_button(libretro_sys_new::DEVICE_ID_JOYPAD_LEFT, None);
self.pad.press_button(sys::DEVICE_ID_JOYPAD_LEFT, None);
}
Key::Right => {
self.pad
.press_button(libretro_sys_new::DEVICE_ID_JOYPAD_RIGHT, None);
self.pad.press_button(sys::DEVICE_ID_JOYPAD_RIGHT, None);
}

Key::S => {
self.pad
.press_button(libretro_sys_new::DEVICE_ID_JOYPAD_B, None);
self.pad.press_button(sys::DEVICE_ID_JOYPAD_B, None);
}

Key::A => {
self.pad
.press_button(libretro_sys_new::DEVICE_ID_JOYPAD_A, None);
self.pad.press_button(sys::DEVICE_ID_JOYPAD_A, None);
}

Key::Q => {
self.pad
.press_button(libretro_sys_new::DEVICE_ID_JOYPAD_X, None);
self.pad.press_button(sys::DEVICE_ID_JOYPAD_X, None);
}

Key::W => {
self.pad
.press_button(libretro_sys_new::DEVICE_ID_JOYPAD_Y, None);
self.pad.press_button(sys::DEVICE_ID_JOYPAD_Y, None);
}

Key::LeftCtrl => {
self.pad
.press_button(libretro_sys_new::DEVICE_ID_JOYPAD_L, None);
self.pad.press_button(sys::DEVICE_ID_JOYPAD_L, None);
}

Key::LeftShift => {
self.pad
.press_button(libretro_sys_new::DEVICE_ID_JOYPAD_L2, None);
self.pad.press_button(sys::DEVICE_ID_JOYPAD_L2, None);
}

Key::LeftAlt => {
self.pad
.press_button(libretro_sys_new::DEVICE_ID_JOYPAD_R, None);
self.pad.press_button(sys::DEVICE_ID_JOYPAD_R, None);
}

Key::Z => {
self.pad
.press_button(libretro_sys_new::DEVICE_ID_JOYPAD_R2, None);
self.pad.press_button(sys::DEVICE_ID_JOYPAD_R2, None);
}

_ => {}
Expand Down

0 comments on commit 7324ab3

Please sign in to comment.