v2
This commit is contained in:
1
trekstor/.gitignore
vendored
Normal file
1
trekstor/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
4747
trekstor/Cargo.lock
generated
Normal file
4747
trekstor/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
10
trekstor/Cargo.toml
Normal file
10
trekstor/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "trekstor"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
slint.workspace = true
|
||||
|
||||
evdev = "0.13.2"
|
||||
libc = "0.2.189"
|
||||
74
trekstor/src/framebuffer/ffi.rs
Normal file
74
trekstor/src/framebuffer/ffi.rs
Normal file
@@ -0,0 +1,74 @@
|
||||
//! Kernel ABI: framebuffer + Kindle EPDC structs and ioctl numbers.
|
||||
|
||||
// Standard Linux framebuffer ioctl numbers (see <linux/fb.h>).
|
||||
// Typed as c_ulong (not libc::Ioctl) so the crate still type-checks on
|
||||
// non-Linux dev hosts where libc::Ioctl isn't defined, like macos
|
||||
pub(super) const FBIOGET_VSCREENINFO: libc::c_ulong = 0x4600;
|
||||
pub(super) const FBIOGET_FSCREENINFO: libc::c_ulong = 0x4602;
|
||||
|
||||
// These structs mirror the kernel's `fb_var_screeninfo` and `fb_fix_screeninfo`.
|
||||
// We only read from them, fields we care about are `xres`, `yres` (visible
|
||||
// resolution) and `line_length` (stride in bytes per row, which may be larger
|
||||
// than xres due to alignment padding).
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Default)]
|
||||
pub(super) struct FbBitfield {
|
||||
pub(super) offset: u32,
|
||||
pub(super) length: u32,
|
||||
pub(super) msb_right: u32,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Default)]
|
||||
pub(super) struct FbVarScreeninfo {
|
||||
pub(super) xres: u32,
|
||||
pub(super) yres: u32,
|
||||
pub(super) xres_virtual: u32,
|
||||
pub(super) yres_virtual: u32,
|
||||
pub(super) xoffset: u32,
|
||||
pub(super) yoffset: u32,
|
||||
pub(super) bits_per_pixel: u32,
|
||||
pub(super) grayscale: u32,
|
||||
pub(super) red: FbBitfield,
|
||||
pub(super) green: FbBitfield,
|
||||
pub(super) blue: FbBitfield,
|
||||
pub(super) transp: FbBitfield,
|
||||
pub(super) nonstd: u32,
|
||||
pub(super) activate: u32,
|
||||
pub(super) height: u32,
|
||||
pub(super) width: u32,
|
||||
pub(super) accel_flags: u32,
|
||||
pub(super) pixclock: u32,
|
||||
pub(super) left_margin: u32,
|
||||
pub(super) right_margin: u32,
|
||||
pub(super) upper_margin: u32,
|
||||
pub(super) lower_margin: u32,
|
||||
pub(super) hsync_len: u32,
|
||||
pub(super) vsync_len: u32,
|
||||
pub(super) sync: u32,
|
||||
pub(super) vmode: u32,
|
||||
pub(super) rotate: u32,
|
||||
pub(super) colorspace: u32,
|
||||
pub(super) reserved: [u32; 4],
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Default)]
|
||||
pub(super) struct FbFixScreeninfo {
|
||||
pub(super) id: [u8; 16],
|
||||
pub(super) smem_start: libc::c_ulong,
|
||||
pub(super) smem_len: u32,
|
||||
pub(super) type_: u32,
|
||||
pub(super) type_aux: u32,
|
||||
pub(super) visual: u32,
|
||||
pub(super) xpanstep: u16,
|
||||
pub(super) ypanstep: u16,
|
||||
pub(super) ywrapstep: u16,
|
||||
pub(super) line_length: u32,
|
||||
pub(super) mmio_start: libc::c_ulong,
|
||||
pub(super) mmio_len: u32,
|
||||
pub(super) accel: u32,
|
||||
pub(super) capabilities: u16,
|
||||
pub(super) reserved: [u16; 2],
|
||||
}
|
||||
135
trekstor/src/framebuffer/mod.rs
Normal file
135
trekstor/src/framebuffer/mod.rs
Normal file
@@ -0,0 +1,135 @@
|
||||
mod ffi;
|
||||
|
||||
use std::os::fd::AsRawFd;
|
||||
|
||||
use ffi::{FBIOGET_FSCREENINFO, FBIOGET_VSCREENINFO, FbFixScreeninfo, FbVarScreeninfo};
|
||||
|
||||
/// Memory-mapped handle to the Kindle's e-ink framebuffer.
|
||||
///
|
||||
/// Pixel format is 8-bit grayscale (one byte per pixel). The `stride` may be
|
||||
/// wider than `width` due to hardware alignment requirements.
|
||||
pub(crate) struct Framebuffer {
|
||||
map: *mut u8,
|
||||
len: usize,
|
||||
pub(crate) width: u32,
|
||||
pub(crate) height: u32,
|
||||
}
|
||||
|
||||
// SAFETY: The mmap is process-wide and we only access it from the event loop thread.
|
||||
unsafe impl Send for Framebuffer {}
|
||||
|
||||
impl Framebuffer {
|
||||
/// Open the framebuffer device and query its geometry from the kernel.
|
||||
///
|
||||
/// This works on any Kindle model - the resolution and stride are read at
|
||||
/// runtime rather than being hardcoded.
|
||||
pub(crate) fn open() -> std::io::Result<Self> {
|
||||
let file = std::fs::OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.open("/dev/fb0")?;
|
||||
|
||||
let fd = file.as_raw_fd();
|
||||
|
||||
let mut vinfo = FbVarScreeninfo::default();
|
||||
if unsafe {
|
||||
libc::ioctl(
|
||||
fd,
|
||||
FBIOGET_VSCREENINFO as _,
|
||||
&mut vinfo as *mut _ as *mut libc::c_void,
|
||||
)
|
||||
} == -1
|
||||
{
|
||||
return Err(std::io::Error::last_os_error());
|
||||
}
|
||||
|
||||
let mut finfo = FbFixScreeninfo::default();
|
||||
if unsafe {
|
||||
libc::ioctl(
|
||||
fd,
|
||||
FBIOGET_FSCREENINFO as _,
|
||||
&mut finfo as *mut _ as *mut libc::c_void,
|
||||
)
|
||||
} == -1
|
||||
{
|
||||
return Err(std::io::Error::last_os_error());
|
||||
}
|
||||
|
||||
let width = vinfo.xres;
|
||||
let height = vinfo.yres;
|
||||
let stride = finfo.line_length as usize;
|
||||
|
||||
// The whole render path treats the mmap as four bytes per pixel. A
|
||||
// different depth would silently produce garbled output, so reject it
|
||||
// with a clear error instead.
|
||||
if vinfo.bits_per_pixel != 32 {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
format!(
|
||||
"unsupported framebuffer depth: {} bpp (expected 8-bit grayscale)",
|
||||
vinfo.bits_per_pixel
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
if width == 0 || height == 0 || stride < width as usize {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
format!("invalid framebuffer geometry: {width}x{height}, stride={stride}"),
|
||||
));
|
||||
}
|
||||
|
||||
let len = stride * height as usize;
|
||||
|
||||
let map = unsafe {
|
||||
libc::mmap(
|
||||
std::ptr::null_mut(),
|
||||
len,
|
||||
libc::PROT_READ | libc::PROT_WRITE,
|
||||
libc::MAP_SHARED,
|
||||
fd,
|
||||
0,
|
||||
)
|
||||
};
|
||||
|
||||
if map == libc::MAP_FAILED {
|
||||
return Err(std::io::Error::last_os_error());
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
map: map as *mut u8,
|
||||
len,
|
||||
width,
|
||||
height,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn write_buffer(
|
||||
&mut self,
|
||||
pixels: &[slint::platform::software_renderer::Rgb565Pixel],
|
||||
) {
|
||||
let dst = unsafe { std::slice::from_raw_parts_mut(self.map as *mut u32, pixels.len()) };
|
||||
|
||||
for (dst, src) in dst.iter_mut().zip(pixels) {
|
||||
let c = src.0; // assuming Rgb565Pixel(pub u16)
|
||||
|
||||
let r5 = ((c >> 11) & 0x1f) as u8;
|
||||
let g6 = ((c >> 5) & 0x3f) as u8;
|
||||
let b5 = (c & 0x1f) as u8;
|
||||
|
||||
// Expand to 8 bits.
|
||||
let r = (r5 << 3) | (r5 >> 2);
|
||||
let g = (g6 << 2) | (g6 >> 4);
|
||||
let b = (b5 << 3) | (b5 >> 2);
|
||||
|
||||
// BGRA8888
|
||||
*dst = u32::from_le_bytes([b, g, r, 0xff]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Framebuffer {
|
||||
fn drop(&mut self) {
|
||||
unsafe { libc::munmap(self.map.cast::<libc::c_void>(), self.len) };
|
||||
}
|
||||
}
|
||||
101
trekstor/src/input.rs
Normal file
101
trekstor/src/input.rs
Normal file
@@ -0,0 +1,101 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use evdev::{AbsoluteAxisCode, Device, EventSummary, KeyCode};
|
||||
|
||||
pub struct InputState {
|
||||
device: Option<Device>,
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
width: i32,
|
||||
height: i32,
|
||||
keys_down: HashSet<KeyCode>,
|
||||
keys_pressed_this_update: HashSet<KeyCode>,
|
||||
keys_released_this_update: HashSet<KeyCode>,
|
||||
}
|
||||
|
||||
impl InputState {
|
||||
pub fn new(w: u32, h: u32) -> Self {
|
||||
let device = evdev::enumerate()
|
||||
.find(|(_, device)| device.name() == Some("silead_ts"))
|
||||
.map(|(_, d)| {
|
||||
d.set_nonblocking(true).ok();
|
||||
d
|
||||
});
|
||||
|
||||
Self {
|
||||
device,
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: w.cast_signed(),
|
||||
height: h.cast_signed(),
|
||||
keys_down: HashSet::new(),
|
||||
keys_pressed_this_update: HashSet::new(),
|
||||
keys_released_this_update: HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn finish(&mut self) {
|
||||
self.keys_pressed_this_update.clear();
|
||||
self.keys_released_this_update.clear();
|
||||
}
|
||||
|
||||
pub(super) fn handle_event(&mut self) {
|
||||
let Some(device) = &mut self.device else {
|
||||
return;
|
||||
};
|
||||
|
||||
let Ok(events) = device.fetch_events() else {
|
||||
return;
|
||||
};
|
||||
|
||||
for event in events {
|
||||
match event.destructure() {
|
||||
EventSummary::AbsoluteAxis(_, AbsoluteAxisCode::ABS_X, value) => {
|
||||
self.x = ((((value - 13) as f32 / 882.) * (self.width as f32)) as i32)
|
||||
.max(0)
|
||||
.min(self.width);
|
||||
}
|
||||
EventSummary::AbsoluteAxis(_, AbsoluteAxisCode::ABS_Y, value) => {
|
||||
self.y = ((((value - 13) as f32 / 624.) * (self.height as f32)) as i32)
|
||||
.max(0)
|
||||
.min(self.width);
|
||||
}
|
||||
EventSummary::Key(_, key, value) => match value {
|
||||
1 => {
|
||||
if !self.keys_down.contains(&key) {
|
||||
self.keys_pressed_this_update.insert(key);
|
||||
}
|
||||
self.keys_down.insert(key);
|
||||
}
|
||||
0 => {
|
||||
if self.keys_down.contains(&key) {
|
||||
self.keys_released_this_update.insert(key);
|
||||
}
|
||||
self.keys_down.remove(&key);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
impl InputState {
|
||||
pub fn is_just_pressed(&self, key: KeyCode) -> bool {
|
||||
self.keys_pressed_this_update.contains(&key)
|
||||
}
|
||||
|
||||
pub fn is_down(&self, key: KeyCode) -> bool {
|
||||
self.keys_down.contains(&key)
|
||||
}
|
||||
|
||||
pub fn is_just_released(&self, key: KeyCode) -> bool {
|
||||
self.keys_released_this_update.contains(&key)
|
||||
}
|
||||
|
||||
pub fn is_up(&self, key: KeyCode) -> bool {
|
||||
!self.is_down(key)
|
||||
}
|
||||
}
|
||||
153
trekstor/src/lib.rs
Normal file
153
trekstor/src/lib.rs
Normal file
@@ -0,0 +1,153 @@
|
||||
#![allow(clippy::borrow_as_ptr)]
|
||||
|
||||
mod framebuffer;
|
||||
mod input;
|
||||
|
||||
use std::rc::Rc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use evdev::KeyCode;
|
||||
use slint::platform::software_renderer::{MinimalSoftwareWindow, RepaintBufferType, Rgb565Pixel};
|
||||
use slint::platform::{EventLoopProxy, Platform, WindowEvent};
|
||||
use slint::{EventLoopError, LogicalPosition, PhysicalSize, PlatformError, WindowSize};
|
||||
|
||||
use self::framebuffer::Framebuffer;
|
||||
use self::input::InputState;
|
||||
|
||||
pub struct Trekstor {
|
||||
window: Rc<MinimalSoftwareWindow>,
|
||||
queue: Queue,
|
||||
quit_flag: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl Trekstor {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Trekstor {
|
||||
fn default() -> Self {
|
||||
let window = MinimalSoftwareWindow::new(RepaintBufferType::NewBuffer);
|
||||
|
||||
Self {
|
||||
window,
|
||||
queue: Arc::new(Mutex::new(Vec::new())),
|
||||
quit_flag: Arc::new(AtomicBool::new(false)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type Closure = Box<dyn FnOnce() + Send>;
|
||||
type Queue = Arc<Mutex<Vec<Closure>>>;
|
||||
|
||||
struct TrekstorEventProxy {
|
||||
queue: Queue,
|
||||
quit_flag: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl EventLoopProxy for TrekstorEventProxy {
|
||||
fn quit_event_loop(&self) -> Result<(), EventLoopError> {
|
||||
self.quit_flag.store(true, Ordering::SeqCst);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn invoke_from_event_loop(&self, event: Closure) -> Result<(), EventLoopError> {
|
||||
// Don't queue if the loop is shutting down, the closure would never
|
||||
// run, and any caller blocking on a channel send inside it would hang.
|
||||
if self.quit_flag.load(Ordering::SeqCst) {
|
||||
return Err(EventLoopError::EventLoopTerminated);
|
||||
}
|
||||
self.queue
|
||||
.lock()
|
||||
.expect("event loop closure queue poisoned")
|
||||
.push(event);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Platform for Trekstor {
|
||||
fn create_window_adapter(
|
||||
&self,
|
||||
) -> Result<Rc<dyn slint::platform::WindowAdapter>, slint::PlatformError> {
|
||||
Ok(self.window.clone())
|
||||
}
|
||||
|
||||
fn run_event_loop(&self) -> Result<(), slint::PlatformError> {
|
||||
let mut frame_buffer = Framebuffer::open()
|
||||
.map_err(|e| PlatformError::Other(format!("failed to open /dev/fb0: {e}")))?;
|
||||
|
||||
let mut input = InputState::new(frame_buffer.width, frame_buffer.height);
|
||||
|
||||
#[allow(clippy::large_stack_arrays)] // causes stack overflow if rgba8
|
||||
let mut buffer1 = [Rgb565Pixel(0); 614_400];
|
||||
|
||||
self.window.set_size(WindowSize::Physical(PhysicalSize::new(
|
||||
frame_buffer.width,
|
||||
frame_buffer.height,
|
||||
)));
|
||||
|
||||
loop {
|
||||
input.handle_event();
|
||||
|
||||
slint::platform::update_timers_and_animations();
|
||||
|
||||
if input.is_just_pressed(KeyCode::BTN_TOUCH) {
|
||||
self.window
|
||||
.try_dispatch_event(WindowEvent::PointerPressed {
|
||||
position: LogicalPosition::new(input.x as f32, input.y as f32),
|
||||
button: slint::platform::PointerEventButton::Left,
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if input.is_just_released(KeyCode::BTN_TOUCH) {
|
||||
self.window
|
||||
.try_dispatch_event(WindowEvent::PointerReleased {
|
||||
position: LogicalPosition::new(input.x as f32, input.y as f32),
|
||||
button: slint::platform::PointerEventButton::Left,
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if input.is_down(KeyCode::BTN_TOUCH) {
|
||||
self.window
|
||||
.try_dispatch_event(WindowEvent::PointerMoved {
|
||||
position: LogicalPosition::new(input.x as f32, input.y as f32),
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if self.quit_flag.load(Ordering::SeqCst) {
|
||||
break;
|
||||
}
|
||||
|
||||
self.window.draw_if_needed(|renderer| {
|
||||
renderer.render(&mut buffer1, frame_buffer.width as usize);
|
||||
frame_buffer.write_buffer(&buffer1);
|
||||
});
|
||||
|
||||
input.finish();
|
||||
|
||||
let pending: Vec<_> = self
|
||||
.queue
|
||||
.lock()
|
||||
.expect("event loop closure queue poisoned")
|
||||
.drain(..)
|
||||
.collect();
|
||||
for c in pending {
|
||||
c();
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn new_event_loop_proxy(&self) -> Option<Box<dyn EventLoopProxy>> {
|
||||
Some(Box::new(TrekstorEventProxy {
|
||||
queue: self.queue.clone(),
|
||||
quit_flag: self.quit_flag.clone(),
|
||||
}))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user