1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use acrylic::app::Application;
use acrylic::app::Style;
use acrylic::bitmap::RGBA;
use acrylic::node::Event;
use acrylic::BlitKey;
use acrylic::Point;
use acrylic::Size;
use acrylic::Spot;

use std::collections::HashMap;

extern "C" {
    fn raw_log(s: *const u8, l: usize);
    fn raw_set_request_url(s: *const u8, l: usize);
    fn raw_set_request_url_prefix(s: *const u8, l: usize);
    fn raw_update_blit(
        x: isize,
        y: isize,
        w: isize,
        h: isize,
        px: *const u8,
        d: isize,
        s: *const u8,
        l: usize,
    );
    fn raw_set_blit_dirty(s: *const u8, l: usize);
    fn raw_is_request_pending() -> usize;
}

pub fn log(s: &str) {
    unsafe { raw_log(s.as_ptr(), s.len()) };
}

pub fn set_blit_dirty(h: u64) {
    let s = format!("{:#02X}", h);
    unsafe { raw_set_blit_dirty(s.as_ptr(), s.len()) };
}

pub fn update_blit(p: Point, s: Size, px: &[u8], d: usize, hash: u64) {
    let w = s.w as isize;
    let h = s.h as isize;
    let d = d as isize;
    let s = format!("{:#02X}", hash);
    unsafe { raw_update_blit(p.x, p.y, w, h, px.as_ptr(), d, s.as_ptr(), s.len()) };
}

pub fn set_request_url(s: &str) {
    unsafe { raw_set_request_url(s.as_ptr(), s.len()) };
}

pub fn set_request_url_prefix(s: &str) {
    unsafe { raw_set_request_url_prefix(s.as_ptr(), s.len()) };
}

pub fn is_request_pending() -> bool {
    unsafe { raw_is_request_pending() != 0 }
}

pub fn ensure_pending_request(app: &Application) {
    if !is_request_pending() {
        if let Some(data_request) = app.data_requests.last() {
            set_request_url(&data_request.name);
        }
    }
}

#[allow(dead_code)]
pub static mut APPLICATION: Option<Application> = None;
pub static mut RESPONSE_BYTES: Option<Vec<u8>> = None;
pub static mut BLITS_PIXELS: Option<HashMap<BlitKey, (Spot, Vec<u8>)>> = None;

pub fn blit(spot: Spot, key: BlitKey) -> Option<(&'static mut [u8], usize, bool)> {
    let (position, size) = spot;
    let (depth, hash) = match key {
        BlitKey::Node(depth, hash) => (depth, hash),
        BlitKey::Overlay => (0, 0),
        BlitKey::Background => (999999, u64::MAX),
    };
    let (saved_spot, slice) = unsafe {
        let total_pixels = size.w * size.h * RGBA;
        let blits = BLITS_PIXELS.as_mut().unwrap();
        if let None = blits.get(&key) {
            let pixels = vec![0; total_pixels];
            let spot = (Point::zero(), Size::zero());
            blits.insert(key, (spot, pixels));
        }
        let (spot, vec) = blits.get_mut(&key).unwrap();
        vec.resize(total_pixels, 0);
        (spot, vec.as_mut_slice())
    };
    if *saved_spot != spot {
        *saved_spot = spot;
        update_blit(position, size, slice, depth, hash);
    } else {
        set_blit_dirty(hash);
    }
    Some((slice, 0, true))
}

#[export_name = "alloc_response_bytes"]
pub extern "C" fn alloc_response_bytes(len: usize) -> *const u8 {
    let mut vec = Vec::with_capacity(len);
    unsafe { vec.set_len(len) };
    let ptr = vec.as_ptr();
    unsafe { RESPONSE_BYTES = Some(vec) };
    ptr
}

#[export_name = "process_response"]
pub extern "C" fn process_response(app: &mut Application) {
    let request = app.data_requests.pop().unwrap();
    let node = app.get_node(&request.node).unwrap();
    let mut node = node.lock().unwrap();
    let data = unsafe { RESPONSE_BYTES.as_ref().unwrap() };
    let _ = node.loaded(app, &request.node, &request.name, 0, data);
}

#[export_name = "drop_response_bytes"]
pub extern "C" fn drop_response_bytes() {
    unsafe {
        RESPONSE_BYTES = None;
    }
}

#[export_name = "discard_request"]
pub extern "C" fn discard_request(app: &mut Application) {
    app.data_requests.pop().unwrap();
}

#[export_name = "set_output_size"]
pub extern "C" fn set_output_size(app: &mut Application, w: usize, h: usize) {
    let spot = (Point::zero(), Size::new(w, h));
    app.set_spot(spot);
}

#[export_name = "pointing_at"]
pub extern "C" fn pointing_at(app: &mut Application, x: isize, y: isize) {
    app.pointing_at(Point::new(x, y));
}

#[export_name = "quick_action"]
pub extern "C" fn quick_action(app: &mut Application, action: usize) {
    let event = match action {
        1 => Event::QuickAction1,
        2 => Event::QuickAction2,
        3 => Event::QuickAction3,
        4 => Event::QuickAction4,
        5 => Event::QuickAction5,
        6 => Event::QuickAction6,
        _ => unreachable!(),
    };
    let _ = app.fire_event(&event);
}

#[export_name = "frame"]
pub extern "C" fn frame(app: &mut Application) {
    app.render();
    ensure_pending_request(app);
}

pub fn wasm_init(assets: &str, mut app: Application) -> &'static Application {
    app.set_styles(vec![
        Style {
            background: [47, 49, 54, 255],
            foreground: [220, 221, 222, 255],
            border: [0; RGBA],
        },
        Style {
            background: [32, 34, 37, 255],
            foreground: [255; RGBA],
            border: [0; RGBA],
        },
        Style {
            background: [54, 57, 63, 255],
            foreground: [220, 221, 222, 255],
            border: [0; RGBA],
        },
        Style {
            background: [59, 165, 93, 255],
            foreground: [255; RGBA],
            border: [0; RGBA],
        },
        Style {
            background: [220, 220, 220, 255],
            foreground: [40, 40, 40, 255],
            border: [0; RGBA],
        },
    ]);
    unsafe {
        set_request_url_prefix(&String::from(assets));
        BLITS_PIXELS = Some(HashMap::new());
        APPLICATION = Some(app);
        &APPLICATION.as_ref().unwrap()
    }
}

#[macro_export]
macro_rules! app {
    ($path: literal, $init: block) => {
        #[export_name = "init"]
        pub extern "C" fn init() -> &'static Application {
            std::panic::set_hook(Box::new(|panic_info| {
                let dbg = format!("{}", panic_info);
                log(&dbg);
            }));
            platform::wasm_init($path, $init)
        }
    };
}