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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//! Just jotting down differences from sokol_app here:
//!  * C's "everything is an int" is still awful.
//!  * RAII makes life SO MUCH EASIER
//!  * Because of that we don't need wonky multi-stage constructors and destructors
//!  * Or state variables to keep track of whether or not something is initialized
//!  * Not allowing things to create invalid values is so much easier -- Option where you need it,
//!  dawg
//!  * The Default trait makes life so much easier
//!  * Structs of function pointers as vtables is sometimes honestly more convenient than traits
//!  * And sometimes not, they're basically trait objects
//!  * Having a real stdlib with things like Vec::with_capacity() and mem::zeroed() and panic!()
//!  are real effin' nice -- lots of C code reinvents the same damn wheels over and over.
//!  * C encouraging everything to be static's is basically horrible...
//!  * ...and part of that is due to lack of generics and other powerful type systems
//!  * People get antsy when cargo downloads 200 crates but nobody seems to care when `apt install
//!  xorg-dev` chucks 30 MB of header files into random places
//!  * ifdef's are basically awful, and C's lack of modules makes them more common
//!  * The best C API doc generator is `rustdoc`

#[cfg(target_os = "linux")]
pub mod x11;

pub const MAX_TOUCHPOINTS: usize = 8;
pub const MAX_MOUSEBUTTONS: usize = 3;
pub const MAX_KEYCODES: usize = 512;

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum EventType {
    Invalid,
    KeyDown,
    KeyUp,
    Char,
    MouseDown,
    MouseUp,
    MouseScroll,
    MouseMove,
    MouseEnter,
    MouseLeave,
    TouchesBegan,
    TouchesMoved,
    TouchesEnded,
    TouchesCancelled,
    Resized,
    Iconified,
    Restored,
    Suspended,
    Resumed,
    UpdateCursor,
    QuitRequested,
    ClipboardPasted,
}

impl Default for EventType {
    fn default() -> Self {
        EventType::Invalid
    }
}

/// key codes are the same names and values as GLFW
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Keycode {
    Invalid = 0,
    Space = 32,
    Apostrophe = 39, /* ' */
    Comma = 44,      /* , */
    Minus = 45,      /* - */
    Period = 46,     /* . */
    Slash = 47,      /* / */
    Num0 = 48,
    Num1 = 49,
    Num2 = 50,
    Num3 = 51,
    Num4 = 52,
    Num5 = 53,
    Num6 = 54,
    Num7 = 55,
    Num8 = 56,
    Num9 = 57,
    Semicolon = 59, /* ; */
    Equal = 61,     /* = */
    A = 65,
    B = 66,
    C = 67,
    D = 68,
    E = 69,
    F = 70,
    G = 71,
    H = 72,
    I = 73,
    J = 74,
    K = 75,
    L = 76,
    M = 77,
    N = 78,
    O = 79,
    P = 80,
    Q = 81,
    R = 82,
    S = 83,
    T = 84,
    U = 85,
    V = 86,
    W = 87,
    X = 88,
    Y = 89,
    Z = 90,
    LeftBracket = 91,  /* [ */
    Backslash = 92,    /* \ */
    RightBracket = 93, /* ] */
    GraveAccent = 96,  /* ` */
    World1 = 161,      /* non-us #1 */
    World2 = 162,      /* non-us #2 */
    Escape = 256,
    Enter = 257,
    Tab = 258,
    Backspace = 259,
    Insert = 260,
    Delete = 261,
    Right = 262,
    Left = 263,
    Down = 264,
    Up = 265,
    PageUp = 266,
    PageDown = 267,
    Home = 268,
    End = 269,
    CapsLock = 280,
    ScrollLock = 281,
    NumLock = 282,
    PrintScreen = 283,
    Pause = 284,
    F1 = 290,
    F2 = 291,
    F3 = 292,
    F4 = 293,
    F5 = 294,
    F6 = 295,
    F7 = 296,
    F8 = 297,
    F9 = 298,
    F10 = 299,
    F11 = 300,
    F12 = 301,
    F13 = 302,
    F14 = 303,
    F15 = 304,
    F16 = 305,
    F17 = 306,
    F18 = 307,
    F19 = 308,
    F20 = 309,
    F21 = 310,
    F22 = 311,
    F23 = 312,
    F24 = 313,
    F25 = 314,
    Kp0 = 320,
    Kp1 = 321,
    Kp2 = 322,
    Kp3 = 323,
    Kp4 = 324,
    Kp5 = 325,
    Kp6 = 326,
    Kp7 = 327,
    Kp8 = 328,
    Kp9 = 329,
    KpDecimal = 330,
    KpDivide = 331,
    KpMultiply = 332,
    KpSubtract = 333,
    KpAdd = 334,
    KpEnter = 335,
    KpEqual = 336,
    LeftShift = 340,
    LeftControl = 341,
    LeftAlt = 342,
    LeftSuper = 343,
    RightShift = 344,
    RightControl = 345,
    RightAlt = 346,
    RightSuper = 347,
    Menu = 348,
}

impl Default for Keycode {
    fn default() -> Self {
        Self::Invalid
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Default)]
pub struct TouchPoint {
    pub identifier: usize,
    pub pos_x: f32,
    pub pos_y: f32,
    pub changed: bool,
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum MouseButton {
    Invalid = -1,
    Left = 0,
    Right = 1,
    Middle = 2,
}

impl Default for MouseButton {
    fn default() -> Self {
        Self::Invalid
    }
}

/// Modifier keys.  TODO: Bitflags crate???
pub const MOD_SHIFT: u32 = 1 << 0;
pub const MOD_CTRL: u32 = 1 << 1;
pub const MOD_ALT: u32 = 1 << 2;
pub const MOD_SUPER: u32 = 1 << 3;

/// TODO: Not make it heckin' C
#[derive(Clone, PartialEq, Debug, Default)]
pub struct Event {
    pub frame_count: u64,
    pub typ: EventType,

    pub keycode: Keycode,
    pub char_code: char,
    pub key_repeat: bool,
    pub modifiers: u32,

    pub mouse_button: MouseButton,
    pub mouse_x: f32,
    pub mouse_y: f32,
    pub scroll_x: f32,
    pub scroll_y: f32,

    pub num_touches: u32,
    pub touches: [TouchPoint; MAX_TOUCHPOINTS],

    pub window_width: u32,
    pub window_height: u32,
    pub framebuffer_width: u32,
    pub framebuffer_height: u32,
}

/// Semi-experimental refactor of Event type to use real enums
/// instead of C BS.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Eventt {
    Invalid,
    KeyDown {
        char_code: char,
        key_repeat: bool,
        modifiers: u32,
    },
    KeyUp,
    Char,
    MouseDown {
        mouse_button: MouseButton,
        mouse_x: f32,
        mouse_y: f32,
        scroll_x: f32,
        scroll_y: f32,
    },
    MouseUp,
    MouseScroll,
    MouseMove,
    MouseEnter,
    MouseLeave,
    TouchesBegan {
        num_touches: u32,
        touches: [TouchPoint; MAX_TOUCHPOINTS],
    },
    TouchesMoved,
    TouchesEnded,
    TouchesCancelled,
    Resized {
        window_width: u32,
        window_height: u32,
        framebuffer_width: u32,
        framebuffer_height: u32,
    },
    Iconified,
    Restored,
    Suspended,
    Resumed,
    UpdateCursor,
    QuitRequested,
    ClipboardPasted,
}

/// sapp_desc type
/// TODO: Pull functions out into a trait or such
///
/// TODO: Add user data.  C's concept of initialization
/// and ownership doesn't work well for us here.
#[derive(Clone)]
pub struct Desc {
    pub init_cb: fn(),
    pub frame_cb: fn(),
    pub cleanup_cb: fn(),
    pub event_cb: fn(&Event),
    pub fail_cb: fn(),

    pub width: u32,
    pub height: u32,
    pub sample_count: u32,
    pub swap_interval: u32,
    pub high_dpi: bool,
    pub alpha: bool,
    pub window_title: String,
    pub user_cursor: bool,
    pub enable_clipboard: bool,
    pub clipboard_size: usize,
    // TODO: HTML5 stuff, iOS stuff, gl_force_gles2
}

/// Basic application context, should be toplevel type where state lives.
/// This is `_sapp_state`, give or take some.
/// Basically all the window state that *isn't* part of a particular backend.
pub struct WFApp {
    pub window_width: u32,
    pub window_height: u32,
    pub framebuffer_width: u32,
    pub framebuffer_height: u32,
    pub sample_count: u32,
    pub swap_interval: u32,
    pub dpi_scale: f32,
    //pub gles2_fallback: bool,
    pub quit_requested: bool,
    pub quit_ordered: bool,
    pub event_consumed: bool,
    //pub html5_canvas_name: String,
    //pub html5_ask_leave_site: bool,
    pub window_title: String,
    pub frame_count: u64,
    pub mouse_x: f32,
    pub mouse_y: f32,
    pub win32_mouse_tracked: bool,
    pub onscreen_keyboard_shown: bool,
    pub event: Event,
    pub desc: Desc,
    pub keycodes: [Keycode; MAX_KEYCODES],
    pub clipboard_enabled: bool,
    pub clipboard_size: usize,
    pub clipboard: Vec<u8>,
}

/// Most of the `_sapp_*` functions live here
impl WFApp {
    pub fn call_event(&mut self, e: &Event) -> bool {
        (self.desc.event_cb)(e);
        if self.event_consumed {
            self.event_consumed = false;
            true
        } else {
            false
        }
    }

    pub fn new(desc: &Desc) -> Self {
        let clipboard = if desc.enable_clipboard {
            Vec::with_capacity(desc.clipboard_size)
        } else {
            Vec::new()
        };
        let app = Self {
            desc: (*desc).clone(),
            window_width: desc.width,
            window_height: desc.height,
            framebuffer_width: desc.width,
            framebuffer_height: desc.height,
            sample_count: desc.sample_count,
            swap_interval: desc.swap_interval,
            clipboard_enabled: desc.enable_clipboard,
            clipboard_size: desc.clipboard_size,
            clipboard,
            dpi_scale: 1.0,
            event_consumed: false,
            frame_count: 0,
            keycodes: [Keycode::Invalid; MAX_KEYCODES],
            mouse_x: 0.0,
            mouse_y: 0.0,
            event: Event::default(),
            quit_ordered: false,
            onscreen_keyboard_shown: false,
            quit_requested: false,
            win32_mouse_tracked: false,
            window_title: String::from("Winflip window"),
            //html5_canvas_name: String::from("TODO: canvas"),
            //html5_ask_leave_site: false,
        };
        // TODO: Backend-specific init probably needs to happen here.

        // TODO: Function pointer may be null in original
        (app.desc.init_cb)();
        app
    }

    /// TODO: This is basically just Drop
    pub fn discard_state(&mut self) {
        self.clipboard = Vec::new()
    }

    pub fn new_event(&self, typ: EventType) -> Event {
        Event {
            typ: typ,
            frame_count: self.frame_count,
            window_width: self.window_width,
            window_height: self.window_height,
            framebuffer_width: self.framebuffer_width,
            framebuffer_height: self.framebuffer_height,
            ..Event::default()
        }
    }

    pub fn events_enabled(&self) -> bool {
        // TODO: why is this even here
        true
    }

    /// TODO: Just use TryFrom?
    pub fn translate_key(&self, scancode: usize) -> Option<Keycode> {
        self.keycodes.get(scancode).cloned()
    }

    pub fn frame(&mut self) {
        (self.desc.frame_cb)();
        self.frame_count += 1;
    }
}

/// Basically the public API, the sapp_*() functions in Sokol
/// Backends implement this, for now.
pub trait Window {
    fn is_valid() -> bool {
        unimplemented!()
    }
    fn width() -> u32 {
        unimplemented!()
    }
    fn height() -> u32 {
        unimplemented!()
    }
    fn high_dpi() -> bool {
        unimplemented!()
    }
    fn dpi_scale() -> f32 {
        unimplemented!()
    }
    fn show_keyboard(_visible: bool) {
        unimplemented!()
    }
    fn keyboard_shown() -> bool {
        unimplemented!()
    }
    fn show_mouse(_visible: bool) {
        unimplemented!()
    }
    fn mouse_shown() -> bool {
        unimplemented!()
    }
    // TODO: sapp_userdata()

    fn query_desc() -> Desc {
        unimplemented!()
    }

    fn request_quit() {
        unimplemented!()
    }

    fn cancel_quit() {
        unimplemented!()
    }

    fn quit() {
        unimplemented!()
    }

    fn consume_event() {
        unimplemented!()
    }

    fn frame_count() -> u64 {
        unimplemented!()
    }

    fn set_clipboard_string(_: &str) {
        unimplemented!()
    }

    fn get_clipboard_string() -> String {
        unimplemented!()
    }
}

/// Framebuffer config info for OpenGL
#[derive(Default, Copy, Clone, Debug)]
pub struct GlFbConfig {
    pub red_bits: i32,
    pub green_bits: i32,
    pub blue_bits: i32,
    pub alpha_bits: i32,
    pub depth_bits: i32,
    pub stencil_bits: i32,
    pub samples: i32,
    pub doublebuffer: bool,
    pub handle: usize,
}

// TODO: Platform specific stuff: GL, HTML5, metal/ios, D3D, android, ...

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}