Skip to content

Commit 4668081

Browse files
committed
Extract assets and conditionalize uinput loading to rmpp
1 parent 21d1166 commit 4668081

4 files changed

Lines changed: 51 additions & 26 deletions

File tree

src/embedded_assets.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use rust_embed::Embed;
2+
3+
#[derive(Embed)]
4+
#[folder = "prompts/"]
5+
pub struct AssetPrompts;
6+
7+
#[derive(Embed)]
8+
#[folder = "utils/"]
9+
#[include = "rmpp/uinput-*"]
10+
pub struct AssetUtils;
11+
12+
// Function to provide access to the uinput module data
13+
pub fn get_uinput_module_data(version: &str) -> Option<Vec<u8>> {
14+
let target_module_filename = format!("rmpp/uinput-{}.ko", version);
15+
AssetUtils::get(target_module_filename.as_str())
16+
.map(|asset| asset.data.to_vec())
17+
}
18+
19+
pub fn load_config(filename: &str) -> String {
20+
log::debug!("Loading config from {}", filename);
21+
22+
if std::path::Path::new(filename).exists() {
23+
std::fs::read_to_string(filename).unwrap()
24+
} else {
25+
std::str::from_utf8(AssetPrompts::get(filename).unwrap().data.as_ref())
26+
.unwrap()
27+
.to_string()
28+
}
29+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod device;
2+
pub mod embedded_assets;
23
pub mod keyboard;
34
pub mod llm_engine;
45
pub mod pen;

src/main.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ use clap::Parser;
44
use dotenv::dotenv;
55
use env_logger;
66
use log::{debug, info};
7-
use rust_embed::Embed;
87
use serde_json::Value as json;
98
use std::sync::{Arc, Mutex};
109

11-
use std::io::Write;
1210
use std::thread::sleep;
1311
use std::time::Duration;
1412

1513
use ghostwriter::{
14+
embedded_assets::load_config,
1615
keyboard::Keyboard,
1716
llm_engine::{anthropic::Anthropic, google::Google, openai::OpenAI, LLMEngine},
1817
pen::Pen,
@@ -26,14 +25,6 @@ use ghostwriter::{
2625
const VIRTUAL_WIDTH: u32 = 768;
2726
const VIRTUAL_HEIGHT: u32 = 1024;
2827

29-
#[derive(Embed)]
30-
#[folder = "prompts/"]
31-
struct AssetPrompts;
32-
33-
#[derive(Embed)]
34-
#[folder = "utils/"]
35-
#[include = "rmpp/uinput-*"]
36-
pub struct AssetUtils;
3728

3829
#[derive(Parser)]
3930
#[command(author, version)]
@@ -131,6 +122,7 @@ struct Args {
131122
log_level: String,
132123
}
133124

125+
134126
fn main() -> Result<()> {
135127
dotenv().ok();
136128

@@ -188,17 +180,6 @@ fn draw_svg(
188180
Ok(())
189181
}
190182

191-
fn load_config(filename: &str) -> String {
192-
debug!("Loading config from {}", filename);
193-
194-
if std::path::Path::new(filename).exists() {
195-
std::fs::read_to_string(filename).unwrap()
196-
} else {
197-
std::str::from_utf8(AssetPrompts::get(filename).unwrap().data.as_ref())
198-
.unwrap()
199-
.to_string()
200-
}
201-
}
202183

203184
fn ghostwriter(args: &Args) -> Result<()> {
204185
let keyboard = shared!(Keyboard::new(

src/util.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ use std::collections::HashMap;
1010
use std::io::Write;
1111
use std::sync::Arc;
1212

13+
use crate::device::DeviceModel;
14+
use crate::embedded_assets::get_uinput_module_data;
15+
1316
pub type OptionMap = HashMap<String, String>;
1417

1518
pub fn svg_to_bitmap(svg_data: &str, width: u32, height: u32) -> Result<Vec<Vec<bool>>> {
@@ -89,6 +92,16 @@ pub fn option_or_env_fallback(
8992

9093
pub fn setup_uinput() -> Result<()> {
9194
debug!("Checking for uinput module");
95+
96+
// Use DeviceModel to detect the device type
97+
let device_model = DeviceModel::detect();
98+
info!("Device model detected: {}", device_model.name());
99+
100+
if device_model == DeviceModel::Remarkable2 {
101+
info!("Device is Remarkable2, skipping uinput module check and installation");
102+
return Ok(());
103+
}
104+
92105
// Check if uinput module is loaded by looking at the lsmod output
93106
let output = std::process::Command::new("lsmod")
94107
.output()
@@ -105,8 +118,7 @@ pub fn setup_uinput() -> Result<()> {
105118
}
106119

107120
let img_version = std::env::var("IMG_VERSION".to_string())
108-
.unwrap()
109-
.to_string();
121+
.unwrap_or_default();
110122

111123
if img_version.is_empty() {
112124
return Ok(());
@@ -118,10 +130,12 @@ pub fn setup_uinput() -> Result<()> {
118130
.collect::<Vec<&str>>()
119131
.join(".");
120132

121-
let target_module_filename = format!("rmpp/uinput-{short_version}.ko");
133+
// let target_module_filename = format!("rmpp/uinput-{short_version}.ko");
122134

123-
let uinput_module_asset = crate::AssetUtils::get(target_module_filename.as_str()).unwrap();
124-
let raw_uinput_module_data = uinput_module_asset.data.as_ref();
135+
// Use the function from embedded_assets module to get the module data
136+
let uinput_module_data = get_uinput_module_data(&short_version)
137+
.expect(&format!("Uinput module for version {} not found", short_version));
138+
let raw_uinput_module_data = uinput_module_data.as_slice();
125139
let mut uinput_module_file = std::fs::File::create("/tmp/uinput.ko")?;
126140
uinput_module_file.write_all(raw_uinput_module_data)?;
127141
uinput_module_file.flush()?;

0 commit comments

Comments
 (0)