diff options
author | Pavel Roskin <1317472+proski@users.noreply.github.com> | 2023-11-21 21:12:56 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-22 05:12:56 +0000 |
commit | 1a047d44c32b95dd86e4444508b72e7846d6a2eb (patch) | |
tree | a2b9aec02f0a7c3f547bb4bfb440e7af44efae08 | |
parent | f8d9f5f6ef2a4252778eff41c3549f17f2cec484 (diff) | |
download | alacritty-1a047d44c32b95dd86e4444508b72e7846d6a2eb.tar.gz alacritty-1a047d44c32b95dd86e4444508b72e7846d6a2eb.zip |
Add error handling for OpenGL connection details
-rw-r--r-- | alacritty/src/renderer/mod.rs | 44 |
1 files changed, 36 insertions, 8 deletions
diff --git a/alacritty/src/renderer/mod.rs b/alacritty/src/renderer/mod.rs index df3ca0b7..d8fd64fa 100644 --- a/alacritty/src/renderer/mod.rs +++ b/alacritty/src/renderer/mod.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; use std::collections::HashSet; use std::ffi::{CStr, CString}; use std::sync::atomic::{AtomicBool, Ordering}; @@ -47,12 +48,16 @@ pub static GL_FUNS_LOADED: AtomicBool = AtomicBool::new(false); pub enum Error { /// Shader error. Shader(ShaderError), + + /// Other error. + Other(String), } impl std::error::Error for Error { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { Error::Shader(err) => err.source(), + Error::Other(_) => None, } } } @@ -63,6 +68,9 @@ impl fmt::Display for Error { Error::Shader(err) => { write!(f, "There was an error initializing the shaders: {}", err) }, + Error::Other(err) => { + write!(f, "{}", err) + }, } } } @@ -73,6 +81,12 @@ impl From<ShaderError> for Error { } } +impl From<String> for Error { + fn from(val: String) -> Self { + Error::Other(val) + } +} + #[derive(Debug)] enum TextRendererProvider { Gles2(Gles2Renderer), @@ -85,6 +99,25 @@ pub struct Renderer { rect_renderer: RectRenderer, } +/// Wrapper around gl::GetString with error checking and reporting. +fn gl_get_string( + string_id: gl::types::GLenum, + description: &str, +) -> Result<Cow<'static, str>, Error> { + unsafe { + let string_ptr = gl::GetString(string_id); + match gl::GetError() { + gl::NO_ERROR if !string_ptr.is_null() => { + Ok(CStr::from_ptr(string_ptr as *const _).to_string_lossy()) + }, + gl::INVALID_ENUM => { + Err(format!("OpenGL error requesting {}: invalid enum", description).into()) + }, + error_id => Err(format!("OpenGL error {} requesting {}", error_id, description).into()), + } + } +} + impl Renderer { /// Create a new renderer. /// @@ -104,14 +137,9 @@ impl Renderer { }); } - let (shader_version, gl_version, renderer) = unsafe { - let renderer = CStr::from_ptr(gl::GetString(gl::RENDERER) as *mut _).to_string_lossy(); - let shader_version = - CStr::from_ptr(gl::GetString(gl::SHADING_LANGUAGE_VERSION) as *mut _) - .to_string_lossy(); - let gl_version = CStr::from_ptr(gl::GetString(gl::VERSION) as *mut _).to_string_lossy(); - (shader_version, gl_version, renderer) - }; + let shader_version = gl_get_string(gl::SHADING_LANGUAGE_VERSION, "shader version")?; + let gl_version = gl_get_string(gl::VERSION, "OpenGL version")?; + let renderer = gl_get_string(gl::RENDERER, "renderer version")?; info!("Running on {renderer}"); info!("OpenGL version {gl_version}, shader_version {shader_version}"); |