initial commit

This commit is contained in:
2026-08-07 21:34:05 +02:00
commit d668b02ac1
7 changed files with 5243 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

4753
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

8
Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "trekstor"
version = "0.1.0"
edition = "2024"
[dependencies]
evdev = "0.13.2"
slint = { version = "1.17.1", default-features = false, features = ["compat-1-2", "unsafe-single-threaded", "libm", "renderer-software"] }

102
src/input.rs Normal file
View File

@@ -0,0 +1,102 @@
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()
.filter(|(_, device)| device.name() == Some("silead_ts"))
.next()
.map_or(None, |(_, d)| {
d.set_nonblocking(true).ok();
Some(d)
});
Self {
device,
x: 0,
y: 0,
width: w as i32,
height: h as i32,
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)
}
}

166
src/lib.rs Normal file
View File

@@ -0,0 +1,166 @@
mod input;
use std::os::unix::fs::FileExt;
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, Rgb8Pixel, WindowSize};
use self::input::InputState;
pub const SCREEN_WIDTH: u32 = 1024;
pub const SCREEN_HEIGHT: u32 = 600;
pub const BPP: u32 = 32;
pub struct Context {
pub input: InputState,
}
pub struct Trekstor {
window: Rc<MinimalSoftwareWindow>,
queue: Queue,
quit_flag: Arc<AtomicBool>,
}
impl Trekstor {
pub fn new() -> Self {
let window = MinimalSoftwareWindow::new(RepaintBufferType::NewBuffer);
Self {
window,
queue: Arc::new(Mutex::new(Vec::new())),
quit_flag: Arc::new(AtomicBool::new(false)),
}
}
}
pub(crate) type Closure = Box<dyn FnOnce() + Send>;
pub(crate) type Queue = Arc<Mutex<Vec<Closure>>>;
struct TrekstorEventProxy {
pub(crate) queue: Queue,
pub(crate) 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 input = InputState::new(1024, 600);
let mut ctx = Context { input };
let mut buffer1 = [Rgb565Pixel(0); 614400];
self.window.set_size(WindowSize::Physical(PhysicalSize::new(
SCREEN_WIDTH,
SCREEN_HEIGHT,
)));
let fb = std::fs::File::open("/dev/fb0").unwrap();
loop {
ctx.input.handle_event();
slint::platform::update_timers_and_animations();
if ctx.input.is_just_pressed(KeyCode::BTN_TOUCH) {
self.window
.try_dispatch_event(WindowEvent::PointerPressed {
position: LogicalPosition::new(ctx.input.x as f32, ctx.input.y as f32),
button: slint::platform::PointerEventButton::Left,
})
.unwrap();
}
if ctx.input.is_just_released(KeyCode::BTN_TOUCH) {
self.window
.try_dispatch_event(WindowEvent::PointerReleased {
position: LogicalPosition::new(ctx.input.x as f32, ctx.input.y as f32),
button: slint::platform::PointerEventButton::Left,
})
.unwrap();
}
if ctx.input.is_down(KeyCode::BTN_TOUCH) {
self.window
.try_dispatch_event(WindowEvent::PointerMoved {
position: LogicalPosition::new(ctx.input.x as f32, ctx.input.y as f32),
})
.unwrap();
}
if self.quit_flag.load(Ordering::SeqCst) {
break;
}
self.window.draw_if_needed(|renderer| {
renderer.render(&mut buffer1, SCREEN_WIDTH as usize);
for (i, px) in buffer1.into_iter().enumerate() {
let x = (i % SCREEN_WIDTH as usize) as u32;
let y = (i / SCREEN_WIDTH as usize) as u32;
let location = y * SCREEN_WIDTH + x * (BPP / 8);
let pixel = Rgb8Pixel::from(px);
fb.write_at(&[pixel.r, pixel.g, pixel.b, 255], location as u64)
.ok();
// let c = Color::from((pixel.r, pixel.g, pixel.b, 255));
// self.framebuffer.set_pixel(x, y, c);
}
// self.framebuffer.flush();
});
ctx.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(),
}))
}
}

125
src/rgba.rs Normal file
View File

@@ -0,0 +1,125 @@
use embedded_graphics_core::pixelcolor::*;
use linfb::shape::Color;
use slint::platform::software_renderer::TargetPixel;
/// Simple RGBA color wrapper.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Rgba<C: RgbColor>(C, u8);
#[allow(unused)]
#[inline(always)]
fn mul_blend_u8(delta: u32, a: u32) -> u32 {
// Exact (delta * a) / 255 using the div255 trick (no slow integer division).
// Valid for 0..=65535 inputs; see Hacker's Delight 10-16.
let t = delta * a + 128;
(t + (t >> 8)) >> 8
}
#[allow(unused)]
impl<C: RgbColor> Rgba<C> {
/// Create a new RGBA color.
pub const fn new(color: C, alpha: u8) -> Self {
Self(color, alpha)
}
/// Get the color component.
pub const fn rgb(&self) -> C {
self.0
}
pub fn r(&self) -> u8 {
self.0.r()
}
pub fn g(&self) -> u8 {
self.0.g()
}
pub fn b(&self) -> u8 {
self.0.b()
}
/// Get the alpha component (0..=255).
pub const fn a(&self) -> u8 {
self.1
}
}
impl<C: RgbColor> PixelColor for Rgba<C> {
type Raw = C::Raw;
}
#[allow(unused)]
pub trait Blend<T> {
fn blend(&self, bg: T) -> T;
}
impl Blend<Rgb888> for Rgba<Rgb888> {
#[inline(always)]
fn blend(&self, bg: Rgb888) -> Rgb888 {
let a = self.a() as u32;
if a == 0 {
return bg;
}
if a == 255 {
return self.rgb();
}
let fr = self.rgb().r() as u32;
let fg = self.rgb().g() as u32;
let fb = self.rgb().b() as u32;
let br = bg.r() as u32;
let bgc = bg.g() as u32;
let bb = bg.b() as u32;
let r = (br + mul_blend_u8(fr.wrapping_sub(br), a)) as u8;
let g = (bgc + mul_blend_u8(fg.wrapping_sub(bgc), a)) as u8;
let b = (bb + mul_blend_u8(fb.wrapping_sub(bb), a)) as u8;
Rgb888::new(r, g, b)
}
}
impl TargetPixel for Rgba<Rgb888> {
fn blend(&mut self, color: slint::platform::software_renderer::PremultipliedRgbaColor) {
let a = self.a() as u32;
// if a == 0 {
// return bg;
// }
// if a == 255 {
// return self.rgb();
// }
let fr = self.rgb().r() as u32;
let fg = self.rgb().g() as u32;
let fb = self.rgb().b() as u32;
let br = color.red as u32;
let bgc = color.green as u32;
let bb = color.blue as u32;
let r = (br + mul_blend_u8(fr.wrapping_sub(br), a)) as u8;
let g = (bgc + mul_blend_u8(fg.wrapping_sub(bgc), a)) as u8;
let b = (bb + mul_blend_u8(fb.wrapping_sub(bb), a)) as u8;
self.0 = Rgb888::new(r, g, b);
self.1 = a as u8;
}
fn from_rgb(red: u8, green: u8, blue: u8) -> Self {
Self(Rgb888::new(red, green, blue), 255)
}
}
impl From<(u8, u8, u8, u8)> for Rgba<Rgb888> {
fn from(value: (u8, u8, u8, u8)) -> Self {
Self(Rgb888::new(value.0, value.1, value.2), value.3)
}
}
impl From<Color> for Rgba<Rgb888> {
fn from(value: Color) -> Self {
Self(Rgb888::new(value.red, value.green, value.blue), value.alpha)
}
}

88
src/screen.rs Normal file
View File

@@ -0,0 +1,88 @@
use embedded_graphics::{pixelcolor::Rgb888, prelude::*, primitives::Rectangle};
use linfb::{Framebuffer, shape::Color};
pub struct Screen {
pub framebuffer: Framebuffer,
width: u32,
height: u32,
}
#[allow(unused)]
impl Screen {
pub fn new() -> Self {
let framebuffer = Framebuffer::open().expect("Failed to open framebuffer");
let width = framebuffer.screen_info.xres;
let height = framebuffer.screen_info.yres;
Self {
framebuffer,
width,
height,
}
}
pub fn flush(&mut self) {
self.framebuffer.flush();
}
#[inline]
pub fn width(&self) -> u32 {
self.width
}
#[inline]
pub fn height(&self) -> u32 {
self.height
}
#[inline]
pub fn rect(&self) -> Rectangle {
Rectangle {
top_left: Point::new(0, 0),
size: Size {
width: self.width(),
height: self.height(),
},
}
}
}
impl Dimensions for Screen {
fn bounding_box(&self) -> Rectangle {
Rectangle::new(Point::new(0, 0), Size::new(self.width, self.height))
}
}
impl DrawTarget for Screen {
type Color = Rgba<Rgb888>;
type Error = Box<dyn std::error::Error>;
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = embedded_graphics::Pixel<Self::Color>>,
{
let width = self.bounding_box().size.width;
let height = self.bounding_box().size.height;
for p in pixels {
let x = p.0.x as u32;
let y = p.0.y as u32;
let color = Color {
red: p.1.r(),
green: p.1.g(),
blue: p.1.b(),
alpha: p.1.a(),
};
if !(0..width).contains(&x) || !(0..height).contains(&y) {
continue;
}
self.framebuffer.set_pixel(x, y, color);
}
Ok(())
}
}