upgrade winit

This commit is contained in:
2025-11-04 23:32:55 -06:00
parent 98c95b55f4
commit eca9c3236d
7 changed files with 1650 additions and 660 deletions
Generated
+1413 -451
View File
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -59,11 +59,11 @@ impl<'a> PairExt for Pair<'a> {
pub trait PairsExt {
/// Peek the next sibling but only if it matches a given rule: used for things
/// with optional modifiers following
fn next_if_rule(&mut self, rule: PestRule) -> Option<Pair>;
fn next_if_rule(&mut self, rule: PestRule) -> Option<Pair<'_>>;
}
impl PairsExt for Peekable<Pairs<'_>> {
fn next_if_rule(&mut self, rule: PestRule) -> Option<Pair> {
fn next_if_rule(&mut self, rule: PestRule) -> Option<Pair<'_>> {
self.next_if(|p| p.as_rule() == rule)
}
}
+2 -2
View File
@@ -10,5 +10,5 @@ vcore = { path = "../vcore" }
vgfx = { path = "../vgfx" }
vasm_core = { path = "../vasm_core" }
rand = "0.8.0"
winit = "0.26.1"
pixels = "0.9.0"
winit = "0.30.12"
pixels = "0.15.0"
+108 -107
View File
@@ -1,117 +1,118 @@
use winit::event::VirtualKeyCode;
use winit::keyboard::{KeyCode};
pub fn convert_keycode(vkey: VirtualKeyCode) -> u8 {
pub fn convert_keycode(vkey: KeyCode) -> u8 {
use KeyCode::*;
match vkey {
VirtualKeyCode::A => 0x04,
VirtualKeyCode::B => 0x05,
VirtualKeyCode::C => 0x06,
VirtualKeyCode::D => 0x07,
VirtualKeyCode::E => 0x08,
VirtualKeyCode::F => 0x09,
VirtualKeyCode::G => 0x0a,
VirtualKeyCode::H => 0x0b,
VirtualKeyCode::I => 0x0c,
VirtualKeyCode::J => 0x0d,
VirtualKeyCode::K => 0x0e,
VirtualKeyCode::L => 0x0f,
VirtualKeyCode::M => 0x10,
VirtualKeyCode::N => 0x11,
VirtualKeyCode::O => 0x12,
VirtualKeyCode::P => 0x13,
VirtualKeyCode::Q => 0x14,
VirtualKeyCode::R => 0x15,
VirtualKeyCode::S => 0x16,
VirtualKeyCode::T => 0x17,
VirtualKeyCode::U => 0x18,
VirtualKeyCode::V => 0x19,
VirtualKeyCode::W => 0x1a,
VirtualKeyCode::X => 0x1b,
VirtualKeyCode::Y => 0x1c,
VirtualKeyCode::Z => 0x1d,
VirtualKeyCode::Key1 => 0x1e,
VirtualKeyCode::Key2 => 0x1f,
VirtualKeyCode::Key3 => 0x20,
VirtualKeyCode::Key4 => 0x21,
VirtualKeyCode::Key5 => 0x22,
VirtualKeyCode::Key6 => 0x23,
VirtualKeyCode::Key7 => 0x24,
VirtualKeyCode::Key8 => 0x25,
VirtualKeyCode::Key9 => 0x26,
VirtualKeyCode::Key0 => 0x27,
VirtualKeyCode::Return => 0x28,
VirtualKeyCode::Escape => 0x29,
VirtualKeyCode::Back => 0x2a,
VirtualKeyCode::Tab => 0x2b,
VirtualKeyCode::Space => 0x2c,
VirtualKeyCode::Minus => 0x2d,
VirtualKeyCode::Equals => 0x2e,
VirtualKeyCode::LBracket => 0x2f,
VirtualKeyCode::RBracket => 0x30,
VirtualKeyCode::Backslash => 0x31,
KeyA => 0x04,
KeyB => 0x05,
KeyC => 0x06,
KeyD => 0x07,
KeyE => 0x08,
KeyF => 0x09,
KeyG => 0x0a,
KeyH => 0x0b,
KeyI => 0x0c,
KeyJ => 0x0d,
KeyK => 0x0e,
KeyL => 0x0f,
KeyM => 0x10,
KeyN => 0x11,
KeyO => 0x12,
KeyP => 0x13,
KeyQ => 0x14,
KeyR => 0x15,
KeyS => 0x16,
KeyT => 0x17,
KeyU => 0x18,
KeyV => 0x19,
KeyW => 0x1a,
KeyX => 0x1b,
KeyY => 0x1c,
KeyZ => 0x1d,
Digit1 => 0x1e,
Digit2 => 0x1f,
Digit3 => 0x20,
Digit4 => 0x21,
Digit5 => 0x22,
Digit6 => 0x23,
Digit7 => 0x24,
Digit8 => 0x25,
Digit9 => 0x26,
Digit0 => 0x27,
Enter => 0x28,
Escape => 0x29,
BrowserBack => 0x2a,
Tab => 0x2b,
Space => 0x2c,
Minus => 0x2d,
Equal => 0x2e,
BracketLeft => 0x2f,
BracketRight => 0x30,
Backslash => 0x31,
// 0x32 is a non-US keyboard key: hash and tilde
VirtualKeyCode::Semicolon => 0x33,
VirtualKeyCode::Apostrophe => 0x34,
VirtualKeyCode::Grave => 0x35,
VirtualKeyCode::Comma => 0x36,
VirtualKeyCode::Period => 0x37,
VirtualKeyCode::Slash => 0x38,
VirtualKeyCode::Capital => 0x39,
VirtualKeyCode::F1 => 0x3a,
VirtualKeyCode::F2 => 0x3b,
VirtualKeyCode::F3 => 0x3c,
VirtualKeyCode::F4 => 0x3d,
VirtualKeyCode::F5 => 0x3e,
VirtualKeyCode::F6 => 0x3f,
VirtualKeyCode::F7 => 0x40,
VirtualKeyCode::F8 => 0x41,
VirtualKeyCode::F9 => 0x42,
VirtualKeyCode::F10 => 0x43,
VirtualKeyCode::F11 => 0x44,
VirtualKeyCode::F12 => 0x45,
VirtualKeyCode::Sysrq => 0x46,
VirtualKeyCode::Scroll => 0x47,
VirtualKeyCode::Pause => 0x48,
VirtualKeyCode::Insert => 0x49,
VirtualKeyCode::Home => 0x4a,
VirtualKeyCode::PageUp => 0x4b,
VirtualKeyCode::Delete => 0x4c,
VirtualKeyCode::End => 0x4d,
VirtualKeyCode::PageDown => 0x4e,
VirtualKeyCode::Right => 0x4f,
VirtualKeyCode::Left => 0x50,
VirtualKeyCode::Down => 0x51,
VirtualKeyCode::Up => 0x52,
VirtualKeyCode::Numlock => 0x53,
VirtualKeyCode::NumpadDivide => 0x54,
VirtualKeyCode::NumpadMultiply => 0x55,
VirtualKeyCode::NumpadSubtract => 0x56,
VirtualKeyCode::NumpadAdd => 0x57,
VirtualKeyCode::NumpadEnter => 0x58,
VirtualKeyCode::Numpad1 => 0x59,
VirtualKeyCode::Numpad2 => 0x5a,
VirtualKeyCode::Numpad3 => 0x5b,
VirtualKeyCode::Numpad4 => 0x5c,
VirtualKeyCode::Numpad5 => 0x5d,
VirtualKeyCode::Numpad6 => 0x5e,
VirtualKeyCode::Numpad7 => 0x5f,
VirtualKeyCode::Numpad8 => 0x60,
VirtualKeyCode::Numpad9 => 0x61,
VirtualKeyCode::Numpad0 => 0x62,
VirtualKeyCode::NumpadDecimal => 0x63,
Semicolon => 0x33,
Quote => 0x34,
Backquote => 0x35,
Comma => 0x36,
Period => 0x37,
Slash => 0x38,
CapsLock => 0x39,
F1 => 0x3a,
F2 => 0x3b,
F3 => 0x3c,
F4 => 0x3d,
F5 => 0x3e,
F6 => 0x3f,
F7 => 0x40,
F8 => 0x41,
F9 => 0x42,
F10 => 0x43,
F11 => 0x44,
F12 => 0x45,
PrintScreen => 0x46,
ScrollLock => 0x47,
Pause => 0x48,
Insert => 0x49,
Home => 0x4a,
PageUp => 0x4b,
Delete => 0x4c,
End => 0x4d,
PageDown => 0x4e,
ArrowRight => 0x4f,
ArrowLeft => 0x50,
ArrowDown => 0x51,
ArrowUp => 0x52,
NumLock => 0x53,
NumpadDivide => 0x54,
NumpadMultiply => 0x55,
NumpadSubtract => 0x56,
NumpadAdd => 0x57,
NumpadEnter => 0x58,
Numpad1 => 0x59,
Numpad2 => 0x5a,
Numpad3 => 0x5b,
Numpad4 => 0x5c,
Numpad5 => 0x5d,
Numpad6 => 0x5e,
Numpad7 => 0x5f,
Numpad8 => 0x60,
Numpad9 => 0x61,
Numpad0 => 0x62,
NumpadDecimal => 0x63,
// 0x64 is a non-US keyboard key: backslash and bar
VirtualKeyCode::Compose => 0x65,
Hiragana => 0x65,
// 0x66 is a non-standard key: power
VirtualKeyCode::NumpadEquals => 0x67,
NumpadEqual => 0x67,
// 0x68-0x73 is extended function keys
// 0x74-0xdf is a bunch of international stuff and keypad stuff
VirtualKeyCode::LControl => 0xe0,
VirtualKeyCode::LShift => 0xe1,
VirtualKeyCode::LAlt => 0xe2,
VirtualKeyCode::LWin => 0xe3,
VirtualKeyCode::RControl => 0xe4,
VirtualKeyCode::RShift => 0xe5,
VirtualKeyCode::RAlt => 0xe6,
VirtualKeyCode::RWin => 0xe7,
ControlLeft => 0xe0,
ShiftLeft => 0xe1,
AltLeft => 0xe2,
SuperLeft => 0xe3,
ControlRight => 0xe4,
ShiftRight => 0xe5,
AltRight => 0xe6,
SuperRight => 0xe7,
_ => 0xff,
}
}
+109 -82
View File
@@ -4,128 +4,155 @@ use winit::{
dpi::LogicalSize,
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
use crate::keyboard::convert_keycode;
use pixels::{Pixels, SurfaceTexture};
use std::collections::VecDeque;
use std::sync::Arc;
use std::time::{Duration, Instant};
use winit::application::ApplicationHandler;
use vasm_core::assemble_snippet;
use vcore::cpu::CPU;
use vcore::memory::{Memory, PeekPoke};
use vcore::word::Word;
use winit::event::{DeviceEvent, ElementState};
use winit::window::Window;
use winit::event::{DeviceEvent, DeviceId, ElementState, StartCause};
use winit::event_loop::ActiveEventLoop;
use winit::keyboard::PhysicalKey;
use winit::window::{Window, WindowId};
use vgfx::display;
fn main() {
let event_loop = EventLoop::new();
struct App<'a> {
cpu: CPU,
pixels: Option<Pixels<'a>>,
window: Option<Arc<Window>>,
interrupt_events: VecDeque<(usize, Option<Word>)>,
focused: bool
}
let window = {
impl<'a> App<'a> {
fn new() -> Self {
let rng = rand::thread_rng();
let memory = Memory::from(rng);
Self {
cpu: CPU::new(memory),
pixels: None,
window: None,
interrupt_events: VecDeque::new(),
focused: false
}
}
}
impl<'a> ApplicationHandler for App<'a> {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let size = LogicalSize::new(640, 480);
WindowBuilder::new()
let attrs = Window::default_attributes()
.with_title("Vulcan")
.with_inner_size(size)
.with_min_inner_size(size)
.build(&event_loop)
.unwrap()
};
.with_min_inner_size(size);
let pixels = {
let winit::dpi::PhysicalSize { width, height } = window.inner_size();
let surface_texture = SurfaceTexture::new(width, height, &window);
Pixels::new(640, 480, surface_texture).unwrap()
};
self.window = Some(event_loop.create_window(attrs).expect("Failed to create window").into());
let rng = rand::thread_rng();
display::init_gfx(&mut self.cpu);
let memory = Memory::from(rng);
let mut cpu = CPU::new(memory);
display::init_gfx(&mut cpu);
let code = assemble_snippet(include_str!("typewriter.asm").lines().map(String::from))
.expect("Assemble error");
// let code = assemble_file("init.asm").expect("Assemble error");
// let mut file = File::open("4th.rom").expect("ROM not found");
// let mut code = Vec::new();
// file.read_to_end(&mut code).expect("Couldn't read ROM");
println!("ROM size: {} bytes", code.len());
cpu.poke_slice(0x400.into(), code.as_slice());
cpu.start();
window_loop(event_loop, window, pixels, cpu)
self.cpu.poke_slice(0x400.into(), code.as_slice());
self.cpu.start();
let winit::dpi::PhysicalSize { width, height } = self.window.as_ref().unwrap().inner_size();
let surface_texture = SurfaceTexture::new(width, height, self.window.as_ref().unwrap().clone());
self.pixels = Some(Pixels::new(640, 480, surface_texture).unwrap());
event_loop.set_control_flow(ControlFlow::WaitUntil(Instant::now() + Duration::from_millis(15)))
}
fn window_loop(event_loop: EventLoop<()>, window: Window, mut pixels: Pixels, mut cpu: CPU) -> ! {
let mut interrupt_events: VecDeque<(usize, Option<Word>)> = VecDeque::new();
let mut focused = true;
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => *control_flow = ControlFlow::Exit,
Event::WindowEvent {
event: WindowEvent::Resized(new_size),
window_id,
} if window_id == window.id() => {
pixels.resize_surface(new_size.width, new_size.height);
}
// These are sent regardless of whether the window is focused or not, and are the same regardless of keyboard
// layout, so, we have to separately track focus and this will only work as expected for normal US Sholes
// keyboards.
// If we use the WindowEvent equivalent of this, however, shifted non-letter characters will send no vkeys
// at all on Linux. Which is actually worse: this way, someone with a "normal" keyboard can use the app on
// any OS, and there are probably more Linux users than Dvorak users.
// Also, this won't actually affect me, even on Linux, because the KMAC handles the dvorak mapping in the
// keyboard itself, rather than an input map; it generates scancodes as though it were a Sholes board.
// See this bug: https://github.com/rust-windowing/winit/issues/1443
Event::DeviceEvent {
event: DeviceEvent::Key(input),
device_id: _device_id,
} => {
if let (Some(vk), state, true) = (input.virtual_keycode, input.state, focused) {
let byte = convert_keycode(vk);
let word = Word::from_bytes([
byte,
if state == ElementState::Pressed { 1 } else { 0 },
0,
]);
interrupt_events.push_back((5, Some(word)))
}
}
Event::WindowEvent {
event: WindowEvent::Focused(new_focus),
window_id,
} if window_id == window.id() => focused = new_focus,
Event::MainEventsCleared => {
fn new_events(&mut self, event_loop: &ActiveEventLoop, cause: StartCause) {
if let StartCause::ResumeTimeReached { .. } = cause {
event_loop.set_control_flow(ControlFlow::WaitUntil(Instant::now() + Duration::from_millis(15)));
let start = Instant::now();
draw(pixels.get_frame(), &mut cpu);
pixels.render().expect("Problem displaying framebuffer");
draw(self.pixels.as_mut().unwrap().frame_mut(), &mut self.cpu);
self.pixels.as_mut().unwrap().render().expect("Problem displaying framebuffer");
loop {
// TODO: this will run the CPU at 100%, need to not spin while halted
if let Some((int, arg)) = interrupt_events.pop_front() {
cpu.interrupt(int, arg)
if let Some((int, arg)) = self.interrupt_events.pop_front() {
self.cpu.interrupt(int, arg)
}
if cpu.running() {
if self.cpu.running() {
for _ in 1..1000 {
cpu.tick()
self.cpu.tick()
}
} else {
break
}
if Instant::now() > start + Duration::from_millis(14) {
break
}
}
if Instant::now() > start + Duration::from_millis(25) {
break;
}
}
fn window_event(&mut self, event_loop: &ActiveEventLoop, window_id: WindowId, event: WindowEvent) {
match event {
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::Resized(new_size) => {
self.pixels.as_mut().unwrap().resize_surface(new_size.width, new_size.height).expect("Resize");
}
WindowEvent::Focused(new_focus) => self.focused = new_focus,
WindowEvent::KeyboardInput { event, .. } => {
if let PhysicalKey::Code(key_code) = event.physical_key {
let byte = convert_keycode(key_code);
let word = Word::from_bytes([
byte,
if event.state == ElementState::Pressed { 1 } else { 0 },
0,
]);
self.interrupt_events.push_back((5, Some(word)))
}
}
_ => {}
}
})
}
// fn device_event(&mut self, event_loop: &ActiveEventLoop, device_id: DeviceId, event: DeviceEvent) {
// match event {
// // These are sent regardless of whether the window is focused or not, and are the same regardless of keyboard
// // layout, so, we have to separately track focus and this will only work as expected for normal US Sholes
// // keyboards.
// // If we use the WindowEvent equivalent of this, however, shifted non-letter characters will send no vkeys
// // at all on Linux. Which is actually worse: this way, someone with a "normal" keyboard can use the app on
// // any OS, and there are probably more Linux users than Dvorak users.
// // Also, this won't actually affect me, even on Linux, because the KMAC handles the dvorak mapping in the
// // keyboard itself, rather than an input map; it generates scancodes as though it were a Sholes board.
// // See this bug: https://github.com/rust-windowing/winit/issues/1443
// DeviceEvent::Key(input) => {
// println!("foo");
//
// }
// _ => {}
// }
// }
}
fn main() {
let event_loop = EventLoop::new().expect("Failed to create event loop");
let mut app = App::new();
if let Err(e) = event_loop.run_app(&mut app) {
println!("Event loop error: {}", e.to_string())
}
}
fn draw(frame: &mut [u8], cpu: &mut CPU) {
+1 -1
View File
@@ -5,7 +5,7 @@ use vcore::memory::{PeekPoke, PeekPokeExt};
use std::iter::FromIterator;
#[mlua::lua_module]
fn libvlua(lua: &Lua) -> LuaResult<LuaTable> {
fn libvlua(lua: &Lua) -> LuaResult<LuaTable<'_>> {
let exports = lua.create_table()?;
let cpu_constructor = lua.create_function(|_, _: ()| Ok(LuaCPU(CPU::new_random())))?;
exports.set("new", cpu_constructor)?;
+1 -1
View File
@@ -14,7 +14,7 @@ vgfx = { path = "../vgfx" }
vasm_core = { path = "../vasm_core" }
forge_core = { path = "../forge_core" }
novaforth = { path = "../novaforth" }
wasm-bindgen = "0.2"
wasm-bindgen = "0.2.105"
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"