summaryrefslogtreecommitdiff
path: root/glfw/src/window.rs
blob: 079b2fb4057b5f27dbbb2314eecd4c4722dfd9db (plain)
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
use std::cell::RefCell;
use std::ffi::{c_double, c_int, CString};
use std::hint::unreachable_unchecked;
use std::io::{stdout, Write};
use std::panic::catch_unwind;
use std::ptr::null_mut;

use bitflags::bitflags;

use crate::init::{initialize, Glfw};
use crate::util::enum_from_repr;
use crate::{get_glfw_error, Error};

#[derive(Debug)]
pub struct Window
{
    _init: Glfw,
    handle: *mut crate::ffi::GLFWwindow,
}

impl Window
{
    /// Makes the context of the window current for the calling thread.
    ///
    /// # Errors
    /// Will return `Err` if a GLFW platform error occurs or if no OpenGL context is
    /// present.
    #[cfg(feature = "opengl")]
    pub fn make_context_current(&self) -> Result<(), Error>
    {
        unsafe { crate::ffi::glfwMakeContextCurrent(self.handle) };

        get_glfw_error()?;

        Ok(())
    }

    /// Returns the address of the specified OpenGL function, if it is supported by the
    /// current context.
    ///
    /// # Errors
    /// Will return `Err` if a GLFW platform error occurs or if no current context has
    /// been set.
    #[cfg(feature = "opengl")]
    pub fn get_proc_address(
        &self,
        proc_name: &std::ffi::CStr,
    ) -> Result<unsafe extern "C" fn(), Error>
    {
        let proc_addr = unsafe { crate::ffi::glfwGetProcAddress(proc_name.as_ptr()) };

        get_glfw_error()?;

        // SAFETY: Is only None when a error has occured and that case is handled above
        Ok(unsafe { proc_addr.unwrap_unchecked() })
    }

    /// Processes all pending events.
    ///
    /// # Errors
    /// Will return `Err` if a GLFW platform error occurs.
    pub fn poll_events(&self) -> Result<(), Error>
    {
        // SAFETY: The initialize function (called when the window is created) makes sure
        // the current thread is the main thread
        unsafe { crate::ffi::glfwPollEvents() };

        get_glfw_error()?;

        Ok(())
    }

    /// Swaps the front and back buffers of the window.
    ///
    /// # Errors
    /// Will return `Err` if a GLFW platform error occurs or if no OpenGL window context
    /// is present.
    pub fn swap_buffers(&self) -> Result<(), Error>
    {
        unsafe {
            crate::ffi::glfwSwapBuffers(self.handle);
        };

        get_glfw_error()?;

        Ok(())
    }

    /// Returns whether or not the window should close.
    #[must_use]
    pub fn should_close(&self) -> bool
    {
        let should_close = unsafe { crate::ffi::glfwWindowShouldClose(self.handle) };

        should_close == crate::ffi::GLFW_TRUE
    }

    /// Retrieves the size of the window.
    ///
    /// # Errors
    /// Will return `Err` if a GLFW platform error occurs.
    pub fn size(&self) -> Result<Size, Error>
    {
        let mut width = 0;
        let mut height = 0;

        // SAFETY: The initialize function (called when the window is created) makes sure
        // the current thread is the main thread
        unsafe { crate::ffi::glfwGetWindowSize(self.handle, &mut width, &mut height) };

        get_glfw_error()?;

        #[allow(clippy::cast_sign_loss)]
        Ok(Size {
            width: width as u32,
            height: height as u32,
        })
    }

    pub fn set_framebuffer_size_callback(&self, callback: impl Fn(Size) + 'static)
    {
        FRAMEBUFFER_SIZE_CB.with_borrow_mut(|framebuffer_size_cb| {
            *framebuffer_size_cb = Some(Box::new(callback));
        });

        // SAFETY: The initialize function (called when the window is created) makes sure
        // the current thread is the main thread
        unsafe {
            crate::ffi::glfwSetFramebufferSizeCallback(
                self.handle,
                Some(framebuffer_size_callback),
            );
        }
    }

    pub fn set_key_callback(
        &self,
        callback: impl Fn(Key, i32, KeyState, KeyModifiers) + 'static,
    )
    {
        KEY_CALLBACK.with_borrow_mut(|key_callback| {
            *key_callback = Some(Box::new(callback));
        });

        // SAFETY: The initialize function (called when the window is created) makes sure
        // the current thread is the main thread
        unsafe {
            crate::ffi::glfwSetKeyCallback(self.handle, Some(key_callback));
        }
    }

    /// Sets the cursor position callback.
    ///
    /// The callback is provided with the position, in screen coordinates, relative to the
    /// upper-left corner of the content area of the window.
    pub fn set_cursor_pos_callback(&self, callback: impl Fn(CursorPosition) + 'static)
    {
        CURSOR_POS_CALLBACK.with_borrow_mut(|cursor_pos_callback| {
            *cursor_pos_callback = Some(Box::new(callback));
        });

        // SAFETY: The initialize function (called when the window is created) makes sure
        // the current thread is the main thread
        unsafe {
            crate::ffi::glfwSetCursorPosCallback(self.handle, Some(cursor_pos_callback));
        }
    }

    /// Sets the mouse button callback.
    ///
    /// The callback is called when a mouse button is pressed.
    pub fn set_mouse_button_callback(
        &self,
        callback: impl Fn(MouseButton, MouseButtonState, KeyModifiers) + 'static,
    )
    {
        MOUSE_BUTTON_CALLBACK.with_borrow_mut(|mouse_button_callback| {
            *mouse_button_callback = Some(Box::new(callback));
        });

        // SAFETY: The initialize function (called when the window is created) makes sure
        // the current thread is the main thread
        unsafe {
            crate::ffi::glfwSetMouseButtonCallback(
                self.handle,
                Some(mouse_button_callback),
            );
        }
    }

    /// Returns the last reported state of a keyboard key.
    ///
    /// # Errors
    /// Will return `Err` if a GLFW error occurs.
    pub fn get_key(&self, key: Key) -> Result<KeyState, Error>
    {
        // SAFETY: The initialize function (called when the window is created) makes sure
        // the current thread is the main thread
        let state = unsafe { crate::ffi::glfwGetKey(self.handle, key as i32) };

        get_glfw_error()?;

        Ok(match state {
            crate::ffi::GLFW_PRESS => KeyState::Pressed,
            crate::ffi::GLFW_RELEASE => KeyState::Released,
            _ => {
                // SAFETY: glfwGetKey can only return GLFW_PRESS or GLFW_RELEASE
                unsafe {
                    unreachable_unchecked();
                }
            }
        })
    }

    /// Returns the last reported state of a mouse button.
    ///
    /// # Errors
    /// Will return `Err` if a GLFW error occurs.
    pub fn get_mouse_button(
        &self,
        mouse_button: MouseButton,
    ) -> Result<MouseButtonState, Error>
    {
        // SAFETY: The initialize function (called when the window is created) makes sure
        // the current thread is the main thread
        let state =
            unsafe { crate::ffi::glfwGetMouseButton(self.handle, mouse_button as i32) };

        get_glfw_error()?;

        Ok(match state {
            crate::ffi::GLFW_PRESS => MouseButtonState::Pressed,
            crate::ffi::GLFW_RELEASE => MouseButtonState::Released,
            _ => {
                // SAFETY: glfwGetMouseButton can only return GLFW_PRESS or GLFW_RELEASE
                unsafe {
                    unreachable_unchecked();
                }
            }
        })
    }

    /// Returns the position of the cursor, in screen coordinates, relative to the
    /// upper-left corner of the content area of the window.
    ///
    /// # Errors
    /// Will return `Err` if a GLFW error occurs.
    pub fn get_cursor_position(&self) -> Result<CursorPosition, Error>
    {
        let mut x_pos = 0.0;
        let mut y_pos = 0.0;

        // SAFETY: The initialize function (called when the window is created) makes sure
        // the current thread is the main thread
        unsafe {
            crate::ffi::glfwGetCursorPos(self.handle, &mut x_pos, &mut y_pos);
        }

        get_glfw_error()?;

        Ok(CursorPosition { x: x_pos, y: y_pos })
    }

    /// Sets a input mode option.
    ///
    /// # Errors
    /// Will return `Err` if a GLFW error occurs.
    pub fn set_input_mode(
        &self,
        input_mode: InputMode,
        enabled: bool,
    ) -> Result<(), Error>
    {
        // SAFETY: The initialize function (called when the window is created) makes sure
        // the current thread is the main thread
        unsafe {
            crate::ffi::glfwSetInputMode(
                self.handle,
                input_mode as i32,
                i32::from(enabled),
            );
        }

        get_glfw_error()?;

        Ok(())
    }

    /// Sets the cursor mode.
    ///
    /// # Errors
    /// Will return `Err` if a GLFW error occurs.
    pub fn set_cursor_mode(&self, cursor_mode: CursorMode) -> Result<(), Error>
    {
        // SAFETY: The initialize function (called when the window is created) makes sure
        // the current thread is the main thread
        unsafe {
            crate::ffi::glfwSetInputMode(
                self.handle,
                crate::ffi::GLFW_CURSOR,
                cursor_mode as i32,
            );
        }

        get_glfw_error()?;

        Ok(())
    }

    /// Returns whether or not raw mouse motion is supported.
    ///
    /// # Errors
    /// Will return `Err` if a GLFW error occurs.
    pub fn is_raw_mouse_motion_supported(&self) -> Result<bool, Error>
    {
        // SAFETY: The initialize function (called when the window is created) makes sure
        // the current thread is the main thread
        let supported = unsafe { crate::ffi::glfwRawMouseMotionSupported() };

        get_glfw_error()?;

        Ok(supported == crate::ffi::GLFW_TRUE)
    }
}

/// [`Window`] builder.
#[derive(Debug, Clone, Default)]
pub struct Builder
{
    hints: Vec<(Hint, i32)>,
}

impl Builder
{
    #[must_use]
    pub fn new() -> Self
    {
        Self { hints: Vec::new() }
    }

    /// Adds a window creation hint to set.
    #[must_use]
    pub fn hint(mut self, hint: Hint, value: i32) -> Self
    {
        self.hints.push((hint, value));

        self
    }

    /// Sets the window hints to set.
    pub fn hints(mut self, hints: impl IntoIterator<Item = (Hint, i32)>)
    {
        self.hints = hints.into_iter().collect();
    }

    /// Creates a new window.
    ///
    /// # Errors
    /// Will return `Err` if
    /// - The title contains an internal nul byte
    /// - A GLFW error occurs
    pub fn create(&self, size: &Size, title: &str) -> Result<Window, Error>
    {
        let c_title =
            CString::new(title).map_err(|_| Error::InternalNulByteInWindowTitle)?;

        let init = initialize()?;

        for (hint, value) in &self.hints {
            // SAFETY: The initialize function makes sure the current thread is the main
            // thread
            //
            // Error is not checked for after since the two possible errors
            // (GLFW_NOT_INITIALIZED and GLFW_INVALID_ENUM) cannot occur.
            unsafe {
                crate::ffi::glfwWindowHint(hint.to_glfw(), *value);
            }
        }

        // SAFETY: The initialize function makes sure the current thread is the main
        // thread
        let handle = unsafe {
            #[allow(clippy::cast_possible_wrap)]
            crate::ffi::glfwCreateWindow(
                size.width as i32,
                size.height as i32,
                c_title.as_ptr(),
                null_mut(),
                null_mut(),
            )
        };

        get_glfw_error()?;

        Ok(Window { _init: init, handle })
    }
}

/// Window creation hint
#[derive(Debug, Clone, Copy)]
pub enum Hint
{
    OpenGLDebugContext,
}

impl Hint
{
    fn to_glfw(self) -> c_int
    {
        match self {
            Self::OpenGLDebugContext => crate::ffi::GLFW_OPENGL_DEBUG_CONTEXT,
        }
    }
}

/// Window size.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Size
{
    pub width: u32,
    pub height: u32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(i32)]
pub enum InputMode
{
    /// When sticky keys mode is enabled, the pollable state of a key will remain
    /// [`KeyState::Pressed`] until the state of that key is polled with
    /// [`Window::get_key`]. Once it has been polled, if a key release event had been
    /// processed in the meantime, the state will reset to [`KeyState::Released`],
    /// otherwise it will remain [`KeyState::Pressed`].
    StickyKeys = crate::ffi::GLFW_STICKY_KEYS,

    /// When sticky mouse buttons mode is enabled, the pollable state of a mouse button
    /// will remain [`MouseButtonState::Pressed`] until the state of that button is
    /// polled with [`Window::get_mouse_button`]. Once it has been polled, if a mouse
    /// button release event had been processed in the meantime, the state will reset
    /// to [`MouseButtonState::Released`], otherwise it will remain
    /// [`MouseButton::Pressed`].
    StickyMouseButtons = crate::ffi::GLFW_STICKY_MOUSE_BUTTONS,

    LockKeyMods = crate::ffi::GLFW_LOCK_KEY_MODS,

    /// When the cursor is disabled, raw (unscaled and unaccelerated) mouse motion can be
    /// enabled if available.
    ///
    /// Raw mouse motion is closer to the actual motion of the mouse across a surface. It
    /// is not affected by the scaling and acceleration applied to the motion of the
    /// desktop cursor. That processing is suitable for a cursor while raw motion is
    /// better for controlling for example a 3D camera. Because of this, raw mouse motion
    /// is only provided when the cursor is disabled.
    RawMouseMotion = crate::ffi::GLFW_RAW_MOUSE_MOTION,
}

#[derive(Debug, Clone, Copy)]
#[repr(i32)]
pub enum CursorMode
{
    /// Hides and grabs the cursor, providing virtual and unlimited cursor movement. This
    /// is useful for implementing for example 3D camera controls.
    Disabled = crate::ffi::GLFW_CURSOR_DISABLED,

    /// Makes the cursor invisible when it is over the content area of the window but
    /// does not restrict the cursor from leaving.
    Hidden = crate::ffi::GLFW_CURSOR_HIDDEN,

    /// Makes the cursor visible and behaving normally.
    Normal = crate::ffi::GLFW_CURSOR_NORMAL,
}

enum_from_repr!(
    #[repr(i32)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum Key
    {
        // Unknown = crate::ffi::GLFW_KEY_UNKNOWN,
        Space = crate::ffi::GLFW_KEY_SPACE,
        Apostrophe = crate::ffi::GLFW_KEY_APOSTROPHE,
        Comma = crate::ffi::GLFW_KEY_COMMA,
        Minus = crate::ffi::GLFW_KEY_MINUS,
        Period = crate::ffi::GLFW_KEY_PERIOD,
        Slash = crate::ffi::GLFW_KEY_SLASH,
        Digit0 = crate::ffi::GLFW_KEY_0,
        Digit1 = crate::ffi::GLFW_KEY_1,
        Digit2 = crate::ffi::GLFW_KEY_2,
        Digit3 = crate::ffi::GLFW_KEY_3,
        Digit4 = crate::ffi::GLFW_KEY_4,
        Digit5 = crate::ffi::GLFW_KEY_5,
        Digit6 = crate::ffi::GLFW_KEY_6,
        Digit7 = crate::ffi::GLFW_KEY_7,
        Digit8 = crate::ffi::GLFW_KEY_8,
        Digit9 = crate::ffi::GLFW_KEY_9,
        Semicolon = crate::ffi::GLFW_KEY_SEMICOLON,
        Equal = crate::ffi::GLFW_KEY_EQUAL,
        A = crate::ffi::GLFW_KEY_A,
        B = crate::ffi::GLFW_KEY_B,
        C = crate::ffi::GLFW_KEY_C,
        D = crate::ffi::GLFW_KEY_D,
        E = crate::ffi::GLFW_KEY_E,
        F = crate::ffi::GLFW_KEY_F,
        G = crate::ffi::GLFW_KEY_G,
        H = crate::ffi::GLFW_KEY_H,
        I = crate::ffi::GLFW_KEY_I,
        J = crate::ffi::GLFW_KEY_J,
        K = crate::ffi::GLFW_KEY_K,
        L = crate::ffi::GLFW_KEY_L,
        M = crate::ffi::GLFW_KEY_M,
        N = crate::ffi::GLFW_KEY_N,
        O = crate::ffi::GLFW_KEY_O,
        P = crate::ffi::GLFW_KEY_P,
        Q = crate::ffi::GLFW_KEY_Q,
        R = crate::ffi::GLFW_KEY_R,
        S = crate::ffi::GLFW_KEY_S,
        T = crate::ffi::GLFW_KEY_T,
        U = crate::ffi::GLFW_KEY_U,
        V = crate::ffi::GLFW_KEY_V,
        W = crate::ffi::GLFW_KEY_W,
        X = crate::ffi::GLFW_KEY_X,
        Y = crate::ffi::GLFW_KEY_Y,
        Z = crate::ffi::GLFW_KEY_Z,
        LeftBracket = crate::ffi::GLFW_KEY_LEFT_BRACKET,
        Backslash = crate::ffi::GLFW_KEY_BACKSLASH,
        RightBracket = crate::ffi::GLFW_KEY_RIGHT_BRACKET,
        GraveAccent = crate::ffi::GLFW_KEY_GRAVE_ACCENT,
        World1 = crate::ffi::GLFW_KEY_WORLD_1,
        World2 = crate::ffi::GLFW_KEY_WORLD_2,
        Escape = crate::ffi::GLFW_KEY_ESCAPE,
        Enter = crate::ffi::GLFW_KEY_ENTER,
        Tab = crate::ffi::GLFW_KEY_TAB,
        Backspace = crate::ffi::GLFW_KEY_BACKSPACE,
        Insert = crate::ffi::GLFW_KEY_INSERT,
        Delete = crate::ffi::GLFW_KEY_DELETE,
        Right = crate::ffi::GLFW_KEY_RIGHT,
        Left = crate::ffi::GLFW_KEY_LEFT,
        Down = crate::ffi::GLFW_KEY_DOWN,
        Up = crate::ffi::GLFW_KEY_UP,
        PageUp = crate::ffi::GLFW_KEY_PAGE_UP,
        PageDown = crate::ffi::GLFW_KEY_PAGE_DOWN,
        Home = crate::ffi::GLFW_KEY_HOME,
        End = crate::ffi::GLFW_KEY_END,
        CapsLock = crate::ffi::GLFW_KEY_CAPS_LOCK,
        ScrollLock = crate::ffi::GLFW_KEY_SCROLL_LOCK,
        NumLock = crate::ffi::GLFW_KEY_NUM_LOCK,
        PrintScreen = crate::ffi::GLFW_KEY_PRINT_SCREEN,
        Pause = crate::ffi::GLFW_KEY_PAUSE,
        F1 = crate::ffi::GLFW_KEY_F1,
        F2 = crate::ffi::GLFW_KEY_F2,
        F3 = crate::ffi::GLFW_KEY_F3,
        F4 = crate::ffi::GLFW_KEY_F4,
        F5 = crate::ffi::GLFW_KEY_F5,
        F6 = crate::ffi::GLFW_KEY_F6,
        F7 = crate::ffi::GLFW_KEY_F7,
        F8 = crate::ffi::GLFW_KEY_F8,
        F9 = crate::ffi::GLFW_KEY_F9,
        F10 = crate::ffi::GLFW_KEY_F10,
        F11 = crate::ffi::GLFW_KEY_F11,
        F12 = crate::ffi::GLFW_KEY_F12,
        F13 = crate::ffi::GLFW_KEY_F13,
        F14 = crate::ffi::GLFW_KEY_F14,
        F15 = crate::ffi::GLFW_KEY_F15,
        F16 = crate::ffi::GLFW_KEY_F16,
        F17 = crate::ffi::GLFW_KEY_F17,
        F18 = crate::ffi::GLFW_KEY_F18,
        F19 = crate::ffi::GLFW_KEY_F19,
        F20 = crate::ffi::GLFW_KEY_F20,
        F21 = crate::ffi::GLFW_KEY_F21,
        F22 = crate::ffi::GLFW_KEY_F22,
        F23 = crate::ffi::GLFW_KEY_F23,
        F24 = crate::ffi::GLFW_KEY_F24,
        F25 = crate::ffi::GLFW_KEY_F25,
        Kp0 = crate::ffi::GLFW_KEY_KP_0,
        Kp1 = crate::ffi::GLFW_KEY_KP_1,
        Kp2 = crate::ffi::GLFW_KEY_KP_2,
        Kp3 = crate::ffi::GLFW_KEY_KP_3,
        Kp4 = crate::ffi::GLFW_KEY_KP_4,
        Kp5 = crate::ffi::GLFW_KEY_KP_5,
        Kp6 = crate::ffi::GLFW_KEY_KP_6,
        Kp7 = crate::ffi::GLFW_KEY_KP_7,
        Kp8 = crate::ffi::GLFW_KEY_KP_8,
        Kp9 = crate::ffi::GLFW_KEY_KP_9,
        KpDecimal = crate::ffi::GLFW_KEY_KP_DECIMAL,
        KpDivide = crate::ffi::GLFW_KEY_KP_DIVIDE,
        KpMultiply = crate::ffi::GLFW_KEY_KP_MULTIPLY,
        KpSubtract = crate::ffi::GLFW_KEY_KP_SUBTRACT,
        KpAdd = crate::ffi::GLFW_KEY_KP_ADD,
        KpEnter = crate::ffi::GLFW_KEY_KP_ENTER,
        KpEqual = crate::ffi::GLFW_KEY_KP_EQUAL,
        LeftShift = crate::ffi::GLFW_KEY_LEFT_SHIFT,
        LeftControl = crate::ffi::GLFW_KEY_LEFT_CONTROL,
        LeftAlt = crate::ffi::GLFW_KEY_LEFT_ALT,
        LeftSuper = crate::ffi::GLFW_KEY_LEFT_SUPER,
        RightShift = crate::ffi::GLFW_KEY_RIGHT_SHIFT,
        RightControl = crate::ffi::GLFW_KEY_RIGHT_CONTROL,
        RightAlt = crate::ffi::GLFW_KEY_RIGHT_ALT,
        RightSuper = crate::ffi::GLFW_KEY_RIGHT_SUPER,
        Menu = crate::ffi::GLFW_KEY_MENU,
    }
);

enum_from_repr!(
    #[repr(i32)]
    #[derive(Debug, Clone, Copy)]
    pub enum KeyState
    {
        Pressed = crate::ffi::GLFW_PRESS,
        Released = crate::ffi::GLFW_RELEASE,
        Repeat = crate::ffi::GLFW_REPEAT,
    }
);

bitflags! {
    #[derive(Debug, Clone, Copy)]
    pub struct KeyModifiers: i32 {
        const SHIFT = crate::ffi::GLFW_MOD_SHIFT;
        const CONTROL = crate::ffi::GLFW_MOD_CONTROL;
        const ALT = crate::ffi::GLFW_MOD_ALT;
        const SUPER = crate::ffi::GLFW_MOD_SUPER;
        const CAPS_LOCK = crate::ffi::GLFW_MOD_CAPS_LOCK;
        const NUM_LOCK = crate::ffi::GLFW_MOD_NUM_LOCK;
    }
}

enum_from_repr!(
    #[repr(i32)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum MouseButton
    {
        One = crate::ffi::GLFW_MOUSE_BUTTON_1,
        Two = crate::ffi::GLFW_MOUSE_BUTTON_2,
        Three = crate::ffi::GLFW_MOUSE_BUTTON_3,
        Four = crate::ffi::GLFW_MOUSE_BUTTON_4,
        Five = crate::ffi::GLFW_MOUSE_BUTTON_5,
        Six = crate::ffi::GLFW_MOUSE_BUTTON_6,
        Seven = crate::ffi::GLFW_MOUSE_BUTTON_7,
        Eight = crate::ffi::GLFW_MOUSE_BUTTON_8,
    }
);

impl MouseButton
{
    pub const LEFT: Self = Self::One;
    pub const MIDDLE: Self = Self::Three;
    pub const RIGHT: Self = Self::Two;
}

enum_from_repr!(
    #[repr(i32)]
    #[derive(Debug, Clone, Copy)]
    pub enum MouseButtonState
    {
        Pressed = crate::ffi::GLFW_PRESS,
        Released = crate::ffi::GLFW_RELEASE,
    }
);

#[derive(Debug, Clone)]
pub struct CursorPosition
{
    pub x: f64,
    pub y: f64,
}

type FramebufferSizeCb = Box<dyn Fn(Size)>;
type KeyCallback = Box<dyn Fn(Key, i32, KeyState, KeyModifiers)>;
type CursorPositionCallback = Box<dyn Fn(CursorPosition)>;
type MouseButtonCallback = Box<dyn Fn(MouseButton, MouseButtonState, KeyModifiers)>;

thread_local! {
static FRAMEBUFFER_SIZE_CB: RefCell<Option<FramebufferSizeCb>> = RefCell::new(None);
static KEY_CALLBACK: RefCell<Option<KeyCallback>> = RefCell::new(None);
static CURSOR_POS_CALLBACK: RefCell<Option<CursorPositionCallback>> = RefCell::new(None);
static MOUSE_BUTTON_CALLBACK: RefCell<Option<MouseButtonCallback>> = RefCell::new(None);
}

extern "C" fn framebuffer_size_callback(
    _window: *mut crate::ffi::GLFWwindow,
    c_width: c_int,
    c_height: c_int,
)
{
    // Width and height can't possibly have their sign bit set
    let width = u32::from_le_bytes(c_width.to_le_bytes());
    let height = u32::from_le_bytes(c_height.to_le_bytes());

    // Unwinds are catched because unwinding from Rust code into foreign code is UB.
    let res = catch_unwind(|| {
        FRAMEBUFFER_SIZE_CB
            .try_with(|framebuffer_size_cb| {
                if let Some(cb) = framebuffer_size_cb.borrow().as_deref() {
                    cb(Size { width, height });
                }
            })
            .ok();
    });

    if res.is_err() {
        println!("ERROR: Panic in framebuffer size callback");
    }
}

extern "C" fn key_callback(
    _window: *mut crate::ffi::GLFWwindow,
    key_raw: c_int,
    scancode: c_int,
    action_raw: c_int,
    mods: c_int,
)
{
    let Some(key) = Key::from_repr(key_raw) else {
        write!(stdout(), "Unknown key {key_raw}").ok();
        return;
    };

    let Some(key_state) = KeyState::from_repr(action_raw) else {
        write!(stdout(), "Unknown key state {action_raw}").ok();
        return;
    };

    let Some(key_modifiers) = KeyModifiers::from_bits(mods) else {
        write!(
            stdout(),
            "Key modifiers {action_raw} contain one or more unknown bit(s)"
        )
        .ok();

        return;
    };

    // Unwinds are catched because unwinding from Rust code into foreign code is UB.
    let res = catch_unwind(|| {
        KEY_CALLBACK
            .try_with(|key_callback| {
                if let Some(cb) = key_callback.borrow().as_deref() {
                    cb(key, scancode, key_state, key_modifiers);
                }
            })
            .ok();
    });

    if res.is_err() {
        write!(stdout(), "ERROR: Panic in key callback").ok();
    }
}

extern "C" fn cursor_pos_callback(
    _window: *mut crate::ffi::GLFWwindow,
    x_pos: c_double,
    y_pos: c_double,
)
{
    // Unwinds are catched because unwinding from Rust code into foreign code is UB.
    let res = catch_unwind(|| {
        CURSOR_POS_CALLBACK
            .try_with(|cursor_pos_callback| {
                if let Some(cb) = cursor_pos_callback.borrow().as_deref() {
                    cb(CursorPosition { x: x_pos, y: y_pos });
                }
            })
            .ok();
    });

    if res.is_err() {
        write!(
            stdout(),
            "ERROR: Cursor position callback panicked: {res:?}"
        )
        .ok();
    }
}

extern "C" fn mouse_button_callback(
    _window: *mut crate::ffi::GLFWwindow,
    mouse_button_raw: c_int,
    mouse_action: c_int,
    modifier_keys: c_int,
)
{
    let Some(mouse_button) = MouseButton::from_repr(mouse_button_raw) else {
        write!(stdout(), "Unknown mouse button {mouse_button_raw}").ok();
        return;
    };

    let Some(mouse_button_state) = MouseButtonState::from_repr(mouse_action) else {
        write!(stdout(), "Unknown mouse action {mouse_action}").ok();
        return;
    };

    let Some(key_modifiers) = KeyModifiers::from_bits(modifier_keys) else {
        write!(
            stdout(),
            "Key modifiers {modifier_keys:#b} contain one or more unknown bit(s)"
        )
        .ok();

        return;
    };

    // Unwinds are catched because unwinding from Rust code into foreign code is UB.
    let res = catch_unwind(|| {
        MOUSE_BUTTON_CALLBACK
            .try_with(|mouse_button_callback| {
                if let Some(cb) = mouse_button_callback.borrow().as_deref() {
                    cb(mouse_button, mouse_button_state, key_modifiers);
                }
            })
            .ok();
    });

    if res.is_err() {
        write!(stdout(), "ERROR: Mouse button callback panicked: {res:?}").ok();
    }
}