1
0
Fork 0
forked from wry/wry

all: remove c_variadic feature

This commit is contained in:
Julian Orth 2024-02-22 23:11:47 +01:00
parent 53aa762239
commit b57555584d
11 changed files with 89 additions and 108 deletions

8
Cargo.lock generated
View file

@ -196,12 +196,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.83" version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" checksum = "7f9fa1897e4325be0d68d48df6aa1a71ac2ed4d27723887e7754192705350730"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
@ -501,6 +498,7 @@ dependencies = [
"bincode", "bincode",
"bstr", "bstr",
"byteorder", "byteorder",
"cc",
"chrono", "chrono",
"clap", "clap",
"clap_complete", "clap_complete",

View file

@ -52,6 +52,7 @@ repc = "0.1.1"
anyhow = "1.0.79" anyhow = "1.0.79"
bstr = { version = "1.9.0", default-features = false, features = ["std"] } bstr = { version = "1.9.0", default-features = false, features = ["std"] }
shaderc = "0.8.3" shaderc = "0.8.3"
cc = "1.0.86"
#[profile.dev.build-override] #[profile.dev.build-override]
#opt-level = 3 #opt-level = 3

View file

@ -24,6 +24,7 @@ use std::{
mod egl; mod egl;
mod enums; mod enums;
mod logging;
mod tokens; mod tokens;
mod vulkan; mod vulkan;
mod wire; mod wire;
@ -49,6 +50,7 @@ fn main() -> anyhow::Result<()> {
enums::main()?; enums::main()?;
egl::main()?; egl::main()?;
vulkan::main()?; vulkan::main()?;
logging::main()?;
println!("cargo:rerun-if-changed=build/build.rs"); println!("cargo:rerun-if-changed=build/build.rs");
Ok(()) Ok(())

5
build/logging.rs Normal file
View file

@ -0,0 +1,5 @@
pub fn main() -> anyhow::Result<()> {
println!("cargo:rerun-if-changed=src/bridge.c");
cc::Build::new().file("src/bridge.c").compile("bridge");
Ok(())
}

49
src/bridge.c Normal file
View file

@ -0,0 +1,49 @@
#define _GNU_SOURCE
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
static char *fmt(const char *format, va_list args) {
char *line;
int ret = vasprintf(&line, format, args);
if (ret < 0) {
return 0;
} else {
return line;
}
}
void jay_libinput_log_handler(
void *libinput,
int priority,
const char *line
);
void jay_libinput_log_handler_bridge(
void *libinput,
int priority,
const char *format,
va_list args
) {
char *line = fmt(format, args);
jay_libinput_log_handler(libinput, priority, line);
free(line);
}
void jay_xkbcommon_log_handler(
void *ctx,
int xkb_log_level,
const char *line
);
void jay_xkbcommon_log_handler_bridge(
void *ctx,
int xkb_log_level,
const char *format,
va_list args
) {
char *line = fmt(format, args);
jay_xkbcommon_log_handler(ctx, xkb_log_level, line);
free(line);
}

View file

@ -22,16 +22,11 @@ use {
}, },
}, },
udev::UdevError, udev::UdevError,
utils::{ utils::{errorfmt::ErrorFmt, oserror::OsError, ptr_ext::PtrExt, trim::AsciiTrim},
errorfmt::ErrorFmt, oserror::OsError, ptr_ext::PtrExt, trim::AsciiTrim,
vasprintf::vasprintf_,
},
}, },
bstr::ByteSlice, bstr::ByteSlice,
std::{ isnt::std_1::primitive::IsntConstPtrExt,
ffi::{CStr, VaList}, std::{ffi::CStr, rc::Rc},
rc::Rc,
},
thiserror::Error, thiserror::Error,
uapi::{c, Errno, IntoUstr, OwnedFd}, uapi::{c, Errno, IntoUstr, OwnedFd},
}; };
@ -91,6 +86,10 @@ pub struct LibInput {
li: *mut libinput, li: *mut libinput,
} }
extern "C" {
fn jay_libinput_log_handler_bridge();
}
impl LibInput { impl LibInput {
pub fn new(adapter: Rc<dyn LibInputAdapter>) -> Result<Self, LibInputError> { pub fn new(adapter: Rc<dyn LibInputAdapter>) -> Result<Self, LibInputError> {
let mut ud = Box::new(UserData { adapter }); let mut ud = Box::new(UserData { adapter });
@ -101,7 +100,7 @@ impl LibInput {
return Err(LibInputError::New); return Err(LibInputError::New);
} }
unsafe { unsafe {
libinput_log_set_handler(li, log_handler); libinput_log_set_handler(li, jay_libinput_log_handler_bridge);
let priority = if log::log_enabled!(log::Level::Debug) { let priority = if log::log_enabled!(log::Level::Debug) {
LIBINPUT_LOG_PRIORITY_DEBUG LIBINPUT_LOG_PRIORITY_DEBUG
} else if log::log_enabled!(log::Level::Info) { } else if log::log_enabled!(log::Level::Info) {
@ -167,19 +166,14 @@ impl Drop for LibInput {
} }
} }
unsafe extern "C" fn log_handler( #[no_mangle]
unsafe extern "C" fn jay_libinput_log_handler(
_libinput: *mut libinput, _libinput: *mut libinput,
priority: libinput_log_priority, priority: libinput_log_priority,
format: *const c::c_char, line: *const c::c_char,
args: VaList,
) { ) {
let str = match vasprintf_(format, args) { assert!(line.is_not_null());
Some(s) => s, let str = CStr::from_ptr(line);
_ => {
log::error!("Could not format log message");
return;
}
};
let priority = match LogPriority(priority as _) { let priority = match LogPriority(priority as _) {
LIBINPUT_LOG_PRIORITY_DEBUG => log::Level::Debug, LIBINPUT_LOG_PRIORITY_DEBUG => log::Level::Debug,
LIBINPUT_LOG_PRIORITY_INFO => log::Level::Info, LIBINPUT_LOG_PRIORITY_INFO => log::Level::Info,

View file

@ -1,13 +1,8 @@
use {std::ffi::VaList, uapi::c}; use uapi::c;
include!(concat!(env!("OUT_DIR"), "/libinput_tys.rs")); include!(concat!(env!("OUT_DIR"), "/libinput_tys.rs"));
pub type libinput_log_handler = unsafe extern "C" fn( pub type libinput_log_handler = unsafe extern "C" fn();
libinput: *mut libinput,
priority: libinput_log_priority,
format: *const c::c_char,
args: VaList,
);
#[repr(transparent)] #[repr(transparent)]
pub struct libinput(u8); pub struct libinput(u8);

View file

@ -1,6 +1,3 @@
#![feature(
c_variadic, // https://github.com/rust-lang/rust/issues/44930
)]
#![allow( #![allow(
clippy::len_zero, clippy::len_zero,
clippy::needless_lifetimes, clippy::needless_lifetimes,

View file

@ -39,7 +39,6 @@ pub mod toplevel_identifier;
pub mod tri; pub mod tri;
pub mod trim; pub mod trim;
pub mod unlink_on_drop; pub mod unlink_on_drop;
pub mod vasprintf;
pub mod vec_ext; pub mod vec_ext;
pub mod vecstorage; pub mod vecstorage;
pub mod windows; pub mod windows;

View file

@ -1,42 +0,0 @@
use {
std::{
ffi::{CStr, VaList},
ops::Deref,
ptr,
},
uapi::c,
};
extern "C" {
fn vasprintf(strp: *mut *mut c::c_char, fmt: *const c::c_char, ap: VaList) -> c::c_int;
}
pub struct OwnedCStr {
val: &'static CStr,
}
impl Deref for OwnedCStr {
type Target = CStr;
fn deref(&self) -> &Self::Target {
self.val
}
}
impl Drop for OwnedCStr {
fn drop(&mut self) {
unsafe {
c::free(self.val.as_ptr() as _);
}
}
}
pub unsafe fn vasprintf_(fmt: *const c::c_char, ap: VaList) -> Option<OwnedCStr> {
let mut res = ptr::null_mut();
if vasprintf(&mut res, fmt, ap) == -1 {
return None;
}
Some(OwnedCStr {
val: CStr::from_ptr(res),
})
}

View file

@ -7,13 +7,8 @@ include!(concat!(env!("OUT_DIR"), "/xkbcommon_tys.rs"));
pub use consts::*; pub use consts::*;
use { use {
bstr::{BStr, ByteSlice}, bstr::{BStr, ByteSlice},
std::{ isnt::std_1::primitive::IsntConstPtrExt,
ffi::{CStr, VaList}, std::{ffi::CStr, io::Write, ops::Deref, ptr, rc::Rc},
io::Write,
ops::Deref,
ptr,
rc::Rc,
},
}; };
use { use {
@ -69,15 +64,7 @@ extern "C" {
fn xkb_context_new(flags: xkb_context_flags) -> *mut xkb_context; fn xkb_context_new(flags: xkb_context_flags) -> *mut xkb_context;
fn xkb_context_unref(context: *mut xkb_context); fn xkb_context_unref(context: *mut xkb_context);
fn xkb_context_set_log_verbosity(context: *mut xkb_context, verbosity: c::c_int); fn xkb_context_set_log_verbosity(context: *mut xkb_context, verbosity: c::c_int);
fn xkb_context_set_log_fn( fn xkb_context_set_log_fn(context: *mut xkb_context, log_fn: unsafe extern "C" fn());
context: *mut xkb_context,
log_fn: unsafe extern "C" fn(
context: *mut xkb_context,
level: xkb_log_level,
format: *const c::c_char,
args: VaList,
),
);
fn xkb_keymap_new_from_buffer( fn xkb_keymap_new_from_buffer(
context: *mut xkb_context, context: *mut xkb_context,
buffer: *const u8, buffer: *const u8,
@ -116,6 +103,10 @@ pub struct XkbContext {
context: *mut xkb_context, context: *mut xkb_context,
} }
extern "C" {
fn jay_xkbcommon_log_handler_bridge();
}
impl XkbContext { impl XkbContext {
pub fn new() -> Result<Self, XkbCommonError> { pub fn new() -> Result<Self, XkbCommonError> {
let res = unsafe { xkb_context_new(XKB_CONTEXT_NO_FLAGS.raw() as _) }; let res = unsafe { xkb_context_new(XKB_CONTEXT_NO_FLAGS.raw() as _) };
@ -124,7 +115,7 @@ impl XkbContext {
} }
unsafe { unsafe {
xkb_context_set_log_verbosity(res, 10); xkb_context_set_log_verbosity(res, 10);
xkb_context_set_log_fn(res, xkbcommon_logger); xkb_context_set_log_fn(res, jay_xkbcommon_log_handler_bridge);
} }
Ok(Self { context: res }) Ok(Self { context: res })
} }
@ -303,22 +294,14 @@ impl Drop for XkbState {
} }
} }
unsafe extern "C" fn xkbcommon_logger( #[no_mangle]
unsafe extern "C" fn jay_xkbcommon_log_handler(
_ctx: *mut xkb_context, _ctx: *mut xkb_context,
level: xkb_log_level, level: xkb_log_level,
format: *const c::c_char, line: *const c::c_char,
args: VaList,
) { ) {
extern "C" { assert!(line.is_not_null());
fn vasprintf(buf: *mut *mut c::c_char, fmt: *const c::c_char, args: VaList) -> c::c_int; let buf = CStr::from_ptr(line);
}
let mut buf = ptr::null_mut();
let res = vasprintf(&mut buf, format, args);
if res < 0 {
log::error!("Could not vasprintf");
return;
}
let buf = std::slice::from_raw_parts(buf as *const u8, res as usize);
let level = match XkbLogLevel(level) { let level = match XkbLogLevel(level) {
XKB_LOG_LEVEL_CRITICAL | XKB_LOG_LEVEL_ERROR => log::Level::Error, XKB_LOG_LEVEL_CRITICAL | XKB_LOG_LEVEL_ERROR => log::Level::Error,
XKB_LOG_LEVEL_WARNING => log::Level::Warn, XKB_LOG_LEVEL_WARNING => log::Level::Warn,
@ -326,5 +309,5 @@ unsafe extern "C" fn xkbcommon_logger(
XKB_LOG_LEVEL_DEBUG => log::Level::Debug, XKB_LOG_LEVEL_DEBUG => log::Level::Debug,
_ => log::Level::Error, _ => log::Level::Error,
}; };
log::log!(level, "xkbcommon: {}", buf.trim_end().as_bstr()); log::log!(level, "xkbcommon: {}", buf.to_bytes().trim_end().as_bstr());
} }