2026-01-06 22:52:41 +01:00
|
|
|
use std::env;
|
2026-01-02 02:44:00 +01:00
|
|
|
use std::fs::{File, OpenOptions};
|
|
|
|
|
use std::io::{Read, Write};
|
2026-01-06 22:52:41 +01:00
|
|
|
use std::path::Path;
|
2025-12-24 02:07:21 +01:00
|
|
|
|
|
|
|
|
use const_gen::*;
|
2026-01-02 02:44:00 +01:00
|
|
|
use json::JsonValue;
|
2025-12-29 19:36:00 +01:00
|
|
|
use slint_build::{CompilerConfiguration, EmbedResourcesKind};
|
2025-12-24 02:07:21 +01:00
|
|
|
use xz2::read::XzEncoder;
|
|
|
|
|
|
|
|
|
|
fn main() {
|
2026-01-06 22:52:41 +01:00
|
|
|
if let Ok(repo) = gix::discover(
|
|
|
|
|
env::var_os("CARGO_MANIFEST_DIR")
|
|
|
|
|
.unwrap()
|
|
|
|
|
.into_string()
|
|
|
|
|
.unwrap(),
|
|
|
|
|
) {
|
2025-12-31 22:24:26 +01:00
|
|
|
let commit_hash = repo.head_commit().unwrap().short_id().unwrap();
|
|
|
|
|
println!("cargo:rustc-env=GIT_COMMIT_HASH={}", commit_hash);
|
2026-01-06 22:52:41 +01:00
|
|
|
println!(
|
|
|
|
|
"cargo:rustc-env=GIT_COMMIT={}",
|
2025-12-31 22:24:26 +01:00
|
|
|
repo.find_tag(repo.head_id().unwrap())
|
|
|
|
|
.ok()
|
|
|
|
|
.map(|tag| format!("{} ({})", tag.decode().unwrap().name, commit_hash))
|
|
|
|
|
.unwrap_or_else(|| commit_hash.to_string())
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-12-31 00:54:48 +01:00
|
|
|
|
2025-12-24 02:07:21 +01:00
|
|
|
// Generate vial config at the root of project
|
|
|
|
|
println!("cargo:rerun-if-changed=vial.json");
|
|
|
|
|
generate_vial_config();
|
|
|
|
|
|
|
|
|
|
println!("cargo:rustc-link-arg-bins=-Tlinkall.x");
|
|
|
|
|
|
|
|
|
|
// Set the extra linker script from defmt
|
|
|
|
|
// println!("cargo:rustc-link-arg=-Tdefmt.x");
|
2025-12-29 19:36:00 +01:00
|
|
|
|
2026-01-10 00:08:14 +01:00
|
|
|
// TODO: Make it a submodule and use relative paths.
|
|
|
|
|
//
|
2026-01-05 04:16:05 +01:00
|
|
|
// 1. Tell cargo where to find the library
|
|
|
|
|
// let lib_path = PathBuf::from(manifest_dir).join("libs");
|
|
|
|
|
// println!(r#"cargo:rustc-link-search=native={}"#, lib_path.display());
|
2026-01-06 22:52:41 +01:00
|
|
|
println!(
|
2026-01-10 00:08:14 +01:00
|
|
|
r#"cargo:rustc-link-search=native=C:\Users\Limeth\workspace\c\libxkbcommon-linux\build-debug-redefined-syms"#
|
2026-01-06 22:52:41 +01:00
|
|
|
);
|
2026-01-05 04:16:05 +01:00
|
|
|
|
|
|
|
|
// 2. Link the static library (strip the 'lib' prefix and '.a' extension)
|
2026-01-10 00:08:14 +01:00
|
|
|
println!("cargo:rustc-link-lib=static=xkbcommon");
|
2026-01-05 04:16:05 +01:00
|
|
|
|
|
|
|
|
// 3. Re-run if build.rs or the library changes
|
2026-01-05 04:16:08 +01:00
|
|
|
// println!(r#"cargo:rerun-if-changed=C:\Users\Limeth\workspace\c\libxkbcommon-linux\build-debug\libxkbcommon.a"#);
|
2026-01-06 22:52:41 +01:00
|
|
|
println!(
|
2026-01-10 00:08:14 +01:00
|
|
|
r#"cargo:rerun-if-changed=C:\Users\Limeth\workspace\c\libxkbcommon-linux\build-debug-redefined-syms\libxkbcommon.a"#
|
2026-01-06 22:52:41 +01:00
|
|
|
);
|
2026-01-05 04:16:05 +01:00
|
|
|
|
2025-12-29 19:36:00 +01:00
|
|
|
let slint_config = CompilerConfiguration::new()
|
2025-12-31 22:24:26 +01:00
|
|
|
// .with_scale_factor(4.0)
|
|
|
|
|
.with_style("cosmic-dark".to_string())
|
2025-12-29 19:36:00 +01:00
|
|
|
.embed_resources(EmbedResourcesKind::EmbedForSoftwareRenderer);
|
|
|
|
|
slint_build::compile_with_config("ui/main.slint", slint_config).expect("Slint build failed");
|
|
|
|
|
slint_build::print_rustc_flags().unwrap()
|
2025-12-24 02:07:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn generate_vial_config() {
|
|
|
|
|
// Generated vial config file
|
2026-01-02 02:44:00 +01:00
|
|
|
let path = Path::new(&env::var_os("OUT_DIR").unwrap()).join("config_generated.rs");
|
2026-01-06 22:52:41 +01:00
|
|
|
let mut out_file = OpenOptions::new()
|
|
|
|
|
.create(true)
|
|
|
|
|
.write(true)
|
|
|
|
|
.truncate(true)
|
|
|
|
|
.open(path)
|
|
|
|
|
.unwrap();
|
2025-12-24 02:07:21 +01:00
|
|
|
|
|
|
|
|
let p = Path::new("vial.json");
|
|
|
|
|
let mut content = String::new();
|
|
|
|
|
match File::open(p) {
|
|
|
|
|
Ok(mut file) => {
|
2026-01-06 22:52:41 +01:00
|
|
|
file.read_to_string(&mut content)
|
|
|
|
|
.expect("Cannot read vial.json");
|
2025-12-24 02:07:21 +01:00
|
|
|
}
|
|
|
|
|
Err(e) => println!("Cannot find vial.json {p:?}: {e}"),
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-02 02:44:00 +01:00
|
|
|
let vial_cfg = json::parse(&content).unwrap();
|
|
|
|
|
let vial_cfg_string = json::stringify(vial_cfg.clone());
|
2025-12-24 02:07:21 +01:00
|
|
|
let mut keyboard_def_compressed: Vec<u8> = Vec::new();
|
2026-01-02 02:44:00 +01:00
|
|
|
XzEncoder::new(vial_cfg_string.as_bytes(), 6)
|
2025-12-24 02:07:21 +01:00
|
|
|
.read_to_end(&mut keyboard_def_compressed)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let keyboard_id: Vec<u8> = vec![0xB9, 0xBC, 0x09, 0xB2, 0x9D, 0x37, 0x4C, 0xEA];
|
|
|
|
|
let const_declarations = [
|
|
|
|
|
const_declaration!(pub VIAL_KEYBOARD_DEF = keyboard_def_compressed),
|
|
|
|
|
const_declaration!(pub VIAL_KEYBOARD_ID = keyboard_id),
|
|
|
|
|
]
|
|
|
|
|
.map(|s| "#[allow(clippy::redundant_static_lifetimes)]\n".to_owned() + s.as_str())
|
|
|
|
|
.join("\n");
|
2026-01-02 02:44:00 +01:00
|
|
|
|
|
|
|
|
writeln!(out_file, "{}", const_declarations).unwrap();
|
|
|
|
|
|
2026-01-10 19:21:13 +01:00
|
|
|
writeln!(out_file, "#[repr(u8)] pub enum CustomKeycodes {{").unwrap();
|
2026-01-02 02:44:00 +01:00
|
|
|
|
2026-01-10 19:21:13 +01:00
|
|
|
// const CUSTOM_KEYCODE_FIRST: u16 = 0x840;
|
2026-01-02 02:44:00 +01:00
|
|
|
|
|
|
|
|
#[allow(clippy::collapsible_if)]
|
|
|
|
|
if let JsonValue::Object(vial_cfg) = vial_cfg {
|
|
|
|
|
if let Some(JsonValue::Array(custom_keycodes)) = vial_cfg.get("customKeycodes") {
|
|
|
|
|
for (index, custom_keycode) in custom_keycodes.iter().enumerate() {
|
|
|
|
|
if let JsonValue::Object(custom_keycode) = custom_keycode {
|
2026-01-06 22:52:41 +01:00
|
|
|
let name = custom_keycode
|
|
|
|
|
.get("name")
|
|
|
|
|
.expect("A custom keycode in vial.json is missing a name.")
|
|
|
|
|
.as_str()
|
|
|
|
|
.expect("A custom keycode's name must be a string.");
|
|
|
|
|
writeln!(
|
|
|
|
|
out_file,
|
|
|
|
|
" {} = {},",
|
|
|
|
|
name,
|
2026-01-10 19:21:13 +01:00
|
|
|
// CUSTOM_KEYCODE_FIRST + index as u16
|
|
|
|
|
index as u8
|
2026-01-06 22:52:41 +01:00
|
|
|
)
|
|
|
|
|
.unwrap();
|
2026-01-02 02:44:00 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeln!(out_file, "}}").unwrap();
|
2025-12-24 02:07:21 +01:00
|
|
|
}
|