LOK runs on Windows and Linux; packaging stages engines (W-01, K-01)

The kernel is the merged library on every platform; Windows and
Linux find it beside the app in libreoffice/program. All three run
unipoll: macOS through GCD, Linux by answering one request per poll,
Windows through a message-only window on the loop's thread (LOK's
own main thread had deadlocked Calc loads). Patch 0043 flushes
callbacks inline everywhere, so a request sees its result; 0042 stops
MSVC's Boost auto-link. Linux paints with cairo, BGRA; the start-up
check now paints red text and finds red in byte two on every
platform, since getTileMode does not see Skia.

ely-lok on Windows: 180 of 189 pass; on Linux 187. The rest are
listed after 0.1.3 (spelling, Calc dark tiles, show layers and
fields, a11y selection). Tests build file URLs portably and accept
CRLF where Windows writes it.

packaging/stage.sh lays out an app with its engines for Windows and
Linux; licenses.sh finds each licence on all three. ElyMark finds its
page beside the app.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-10 18:27:53 -04:00
1 parent dc4c1ed0fe
commit c96d39a342
34 files changed
+658 -214

No files matched your search

+133
View File
@@ -0,0 +1,133 @@
//! Windows: requests ride the kernel's message loop to a window.
use std::{
ffi::c_void,
io,
sync::atomic::{AtomicPtr, AtomicUsize, Ordering},
};
use windows_sys::Win32::{
Foundation::{HWND, LPARAM, LRESULT, WPARAM},
System::LibraryLoader::GetModuleHandleW,
UI::WindowsAndMessaging::{
CreateWindowExW, DefWindowProcW, HWND_MESSAGE, PostMessageW, RegisterClassW, WM_APP,
WNDCLASSW,
},
};
use super::{answer, events, lok, password, pin, quit, teardown};
use crate::ffi::Lok;
use crate::ipc;
const ASK: u32 = WM_APP + 1;
const CLOSED: u32 = WM_APP + 2;
/// The message-only window the requests go to.
static WINDOW: AtomicPtr<c_void> = AtomicPtr::new(std::ptr::null_mut());
/// Requests posted and not yet answered.
static PENDING: AtomicUsize = AtomicUsize::new(0);
pub(super) fn serve(lok: Lok, input: impl io::Read + Send + 'static) -> io::Result<()> {
let lok = Box::leak(Box::new(lok));
pin(lok);
lok.register_callback(password::on_kit);
lok.register_any_input(waiting);
WINDOW.store(window()?, Ordering::SeqCst);
std::thread::spawn(move || reader(input));
std::thread::spawn(events::push);
// Unipoll needs hooks; the Windows loop never calls them.
extern "C" fn poll(_: *mut c_void, _: i32) -> i32 {
0
}
extern "C" fn wake(_: *mut c_void) {}
lok.run_loop(Some(poll), Some(wake));
log::info!("lok-worker: the kernel's loop ended");
teardown::teardown(0);
}
/// A window on this thread that only takes messages.
fn window() -> io::Result<HWND> {
let name: Vec<u16> = "ElyLokWorker\0".encode_utf16().collect();
let instance = unsafe { GetModuleHandleW(std::ptr::null()) };
let class = WNDCLASSW {
lpfnWndProc: Some(procedure),
hInstance: instance,
lpszClassName: name.as_ptr(),
..unsafe { std::mem::zeroed() }
};
if unsafe { RegisterClassW(&class) } == 0 {
return Err(io::Error::last_os_error());
}
let made = unsafe {
CreateWindowExW(
0,
name.as_ptr(),
std::ptr::null(),
0,
0,
0,
0,
0,
HWND_MESSAGE,
std::ptr::null_mut(),
instance,
std::ptr::null(),
)
};
match made.is_null() {
true => Err(io::Error::last_os_error()),
false => Ok(made),
}
}
fn reader(mut input: impl io::Read) {
loop {
let (message, request) = match ipc::read_frame(&mut input) {
Ok(Some(request)) => {
PENDING.fetch_add(1, Ordering::SeqCst);
(ASK, Box::into_raw(Box::new(request)))
}
Ok(None) => {
log::info!("lok-worker: stdin closed");
(CLOSED, std::ptr::null_mut())
}
Err(error) => {
log::warn!("lok-worker: frame read failed: {error}");
quit(1);
}
};
let window = WINDOW.load(Ordering::SeqCst);
if unsafe { PostMessageW(window, message, 0, request as LPARAM) } == 0 {
log::error!("lok-worker: a request did not post: {}", io::Error::last_os_error());
quit(1);
}
if message == CLOSED {
return;
}
}
}
/// Idle jobs yield to a waiting request.
extern "C" fn waiting(_: *mut c_void, _: i32) -> bool {
PENDING.load(Ordering::SeqCst) > 0
}
unsafe extern "system" fn procedure(
window: HWND,
message: u32,
wparam: WPARAM,
lparam: LPARAM,
) -> LRESULT {
match message {
ASK => {
let request = unsafe { Box::from_raw(lparam as *mut (u8, u64, Vec<u8>)) };
let (tag, id, payload) = *request;
PENDING.fetch_sub(1, Ordering::SeqCst);
answer(lok(), tag, id, &payload);
0
}
CLOSED => teardown::teardown(0),
_ => unsafe { DefWindowProcW(window, message, wparam, lparam) },
}
}
+107
View File
@@ -0,0 +1,107 @@
//! Linux: LOK runs its loop; requests arrive in its poll.
use std::{
ffi::c_void,
io,
sync::{
Mutex, OnceLock,
atomic::{AtomicUsize, Ordering},
mpsc::{Receiver, RecvTimeoutError, Sender, TryRecvError, channel},
},
time::Duration,
};
use super::{answer, events, lok, password, pin, quit, teardown};
use crate::ffi::Lok;
use crate::ipc;
/// A request, the kernel's wake, or the end.
enum Note {
Ask(u8, u64, Vec<u8>),
Woken,
Closed,
}
type Notes = (Mutex<Sender<Note>>, Mutex<Receiver<Note>>);
static NOTES: OnceLock<Notes> = OnceLock::new();
/// Requests read and not yet answered.
static PENDING: AtomicUsize = AtomicUsize::new(0);
pub(super) fn serve(lok: Lok, input: impl io::Read + Send + 'static) -> io::Result<()> {
let lok = Box::leak(Box::new(lok));
pin(lok);
lok.register_callback(password::on_kit);
lok.register_any_input(waiting);
let (send, receive) = channel();
let reading = send.clone();
NOTES
.set((Mutex::new(send), Mutex::new(receive)))
.map_err(|_| io::Error::other("the loop is served twice"))?;
std::thread::spawn(move || reader(input, reading));
std::thread::spawn(events::push);
lok.run_loop(Some(poll), Some(wake));
log::info!("lok-worker: the kernel's loop ended");
teardown::teardown(0);
}
fn reader(mut input: impl io::Read, notes: Sender<Note>) {
loop {
let note = match ipc::read_frame(&mut input) {
Ok(Some((tag, id, payload))) => {
PENDING.fetch_add(1, Ordering::SeqCst);
Note::Ask(tag, id, payload)
}
Ok(None) => {
log::info!("lok-worker: stdin closed");
Note::Closed
}
Err(error) => {
log::warn!("lok-worker: frame read failed: {error}");
quit(1);
}
};
let closed = matches!(note, Note::Closed);
if notes.send(note).is_err() || closed {
return;
}
}
}
/// Idle jobs yield to a waiting request.
extern "C" fn waiting(_: *mut c_void, _: i32) -> bool {
PENDING.load(Ordering::SeqCst) > 0
}
/// The kernel's wait: one request answered, or its own wake.
extern "C" fn poll(_: *mut c_void, timeout_us: i32) -> i32 {
let notes = &NOTES.get().expect("the loop is served").1;
let notes = notes.lock().expect("the notes lock");
let note = match timeout_us {
0 => notes.try_recv().map_err(|error| error == TryRecvError::Disconnected),
wait if wait < 0 => notes.recv().map_err(|_| true),
wait => notes
.recv_timeout(Duration::from_micros(wait as u64))
.map_err(|error| error == RecvTimeoutError::Disconnected),
};
drop(notes);
match note {
Ok(Note::Ask(tag, id, payload)) => {
PENDING.fetch_sub(1, Ordering::SeqCst);
answer(lok(), tag, id, &payload);
1
}
Ok(Note::Woken) => 1,
Ok(Note::Closed) | Err(true) => teardown::teardown(0),
Err(false) => 0,
}
}
/// Another thread posted work for the kernel's loop.
extern "C" fn wake(_: *mut c_void) {
let notes = &NOTES.get().expect("the loop is served").0;
if notes.lock().expect("the notes lock").send(Note::Woken).is_err() {
log::info!("lok-worker: a wake after the loop ended");
}
}