summaryrefslogtreecommitdiff
path: root/glfw/src/lib.rs
blob: 7d2f25c6c27318ffecd6784099a36d801b1f13a5 (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
#![deny(clippy::all, clippy::pedantic)]

use std::ffi::{c_char, CStr};
use std::ptr::null;

mod ffi;
mod init;
mod util;

pub mod window;

pub use window::{Builder as WindowBuilder, Size as WindowSize, Window};

#[derive(Debug, thiserror::Error)]
pub enum Error
{
    /// The current thread is not the main thread.
    #[error("The current thread is not the main thread")]
    NotInMainThread,

    /// An internal nul byte was found in a window title.
    #[error("An internal nul byte was found in the window title")]
    InternalNulByteInWindowTitle,

    /// GLFW error.
    #[error("GLFW error {0} occured. {1}")]
    GlfwError(i32, String),
}

fn get_glfw_error() -> Result<(), Error>
{
    let mut description_ptr: *const c_char = null();

    let err = unsafe { crate::ffi::glfwGetError(&mut description_ptr) };

    if err == crate::ffi::GLFW_NO_ERROR {
        return Ok(());
    }

    // SAFETY: The description is guaranteed by GLFW to be valid UTF-8
    let desc_str = unsafe {
        std::str::from_utf8_unchecked(CStr::from_ptr(description_ptr).to_bytes())
    };

    // The description has to be copied because it is guaranteed to be valid only
    // until the next GLFW error occurs or the GLFW library is terminated.
    let description = desc_str.to_string();

    Err(Error::GlfwError(err, description))
}