Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgraded raw_window_handle dependency to 0.6 #333

Merged
merged 7 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ png = "0.17"
cc = "1.0"

[dependencies]
raw-window-handle = "0.5"
raw-window-handle = "0.6"

[target.'cfg(windows)'.dependencies.winapi]
version = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_blocks = true
force_multiline_blocks = false
fn_args_layout = "Tall"
fn_params_layout = "Tall"
brace_style = "SameLineWhere"
control_brace_style = "AlwaysSameLine"
trailing_semicolon = true
Expand Down
23 changes: 0 additions & 23 deletions src/buffer_helper.rs

This file was deleted.

4 changes: 1 addition & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::error::Error as StdError;
use std::fmt;
use std::{error::Error as StdError, fmt};

/// Errors that can be returned from various operations
///
pub enum Error {
/// Returned if menu Menu function isn't supported
MenusNotSupported,
Expand Down
3 changes: 0 additions & 3 deletions src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ use std::convert::TryFrom;
#[cfg(target_os = "windows")]
use std::{ffi::OsStr, os::windows::prelude::OsStrExt, str::FromStr};

///
/// Represents a window icon
///
/// Different under Windows, Linux and MacOS
///
/// **Windows**: Icon can be created from a relative path string
///
/// **Linux / X11:** Icon can be created from an ARGB buffer
///
///
#[derive(Clone, Copy, Debug)]
pub enum Icon {
Path(*const u16),
Expand Down
5 changes: 3 additions & 2 deletions src/key_handler.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#[cfg(feature = "web")]
extern crate instant;

use crate::{InputCallback, Key, KeyRepeat};
#[cfg(feature = "web")]
use instant::{Duration, Instant};
use std::mem;
#[cfg(not(feature = "web"))]
use std::time::{Duration, Instant};

use crate::{InputCallback, Key, KeyRepeat};
use std::mem;

pub struct KeyHandler {
pub key_callback: Option<Box<dyn InputCallback>>,
prev_time: Instant,
Expand Down
65 changes: 43 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,39 @@
#[macro_use]
extern crate dlib;

use std::fmt;
use std::os::raw;

mod error;
pub use self::error::Error;
pub type Result<T> = std::result::Result<T, Error>;
pub use icon::Icon;
pub use raw_window_handle::HasRawWindowHandle;

mod key;
pub use key::Key;
mod buffer_helper;
mod icon;
mod key;
mod key_handler;
mod os;
mod rate;

use raw_window_handle::{DisplayHandle, HandleError, HasDisplayHandle, WindowHandle};
use std::{fmt, os::raw};

#[cfg(target_os = "macos")]
use self::os::macos as imp;
use os::macos as imp;
#[cfg(any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
))]
use self::os::posix as imp;
use os::posix as imp;
#[cfg(target_os = "redox")]
use self::os::redox as imp;
use os::redox as imp;
#[cfg(target_arch = "wasm32")]
use self::os::wasm as imp;
use os::wasm as imp;
#[cfg(target_os = "windows")]
use self::os::windows as imp;
use os::windows as imp;

pub use error::Error;
pub use icon::Icon;
pub use key::Key;
pub use raw_window_handle::HasWindowHandle;

pub type Result<T> = std::result::Result<T, Error>;

/// Scale will scale the frame buffer and the window that is being sent in when calling the update
/// function. This is useful if you for example want to display a 320 x 256 window on a screen with
Expand Down Expand Up @@ -174,15 +174,15 @@ impl fmt::Debug for Window {
}
}

unsafe impl raw_window_handle::HasRawWindowHandle for Window {
fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
self.0.raw_window_handle()
impl HasWindowHandle for Window {
fn window_handle(&self) -> std::result::Result<WindowHandle, HandleError> {
self.0.window_handle()
}
}

unsafe impl raw_window_handle::HasRawDisplayHandle for Window {
fn raw_display_handle(&self) -> raw_window_handle::RawDisplayHandle {
self.0.raw_display_handle()
impl HasDisplayHandle for Window {
fn display_handle(&self) -> std::result::Result<DisplayHandle, HandleError> {
self.0.display_handle()
}
}

Expand Down Expand Up @@ -1097,3 +1097,24 @@ impl Default for WindowOptions {
}
}
}

pub(crate) fn check_buffer_size(
buffer: &[u32],
buffer_width: usize,
buffer_height: usize,
buffer_stride: usize,
) -> Result<()> {
let width = usize::max(buffer_width, buffer_stride);
let buffer_size = buffer.len() * std::mem::size_of::<u32>();
let required_buffer_size = width * buffer_height * std::mem::size_of::<u32>(); // * 4 (size of u32) for 32-bit buffer

if buffer_size < required_buffer_size {
let err = format!(
"Update failed because input buffer is too small. Required size for {} ({} stride) x {} buffer is {}
bytes but the size of the input buffer has the size {} bytes",
buffer_width, buffer_stride, buffer_height, required_buffer_size, buffer_size);
Err(Error::UpdateFailed(err))
} else {
Ok(())
}
}
67 changes: 36 additions & 31 deletions src/os/macos/mod.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
#![cfg(target_os = "macos")]

use crate::error::Error;
use crate::key_handler::KeyHandler;
use crate::rate::UpdateRate;
use crate::Result;
use crate::{Key, KeyRepeat, MouseButton, MouseMode, Scale, WindowOptions};
// use MenuItem;
use crate::buffer_helper;
use crate::icon::Icon;
use crate::InputCallback;
use crate::{CursorStyle, MenuHandle, MenuItem, MenuItemHandle};
// use menu::Menu;

use std::ffi::CString;
use std::mem;
use std::os::raw;
use std::os::raw::{c_char, c_uchar, c_void};
use std::ptr;

// Table taken from GLFW and slightly modified

use crate::{
check_buffer_size, error::Error, icon::Icon, key_handler::KeyHandler, rate::UpdateRate,
CursorStyle, InputCallback, Key, KeyRepeat, MenuHandle, MenuItem, MenuItemHandle, MouseButton,
MouseMode, Result, Scale, WindowOptions,
};
use raw_window_handle::{
AppKitDisplayHandle, AppKitWindowHandle, DisplayHandle, HandleError, HasDisplayHandle,
HasWindowHandle, RawDisplayHandle, RawWindowHandle, WindowHandle,
};
use std::{
ffi::CString,
mem,
os::raw::{self, c_char, c_uchar, c_void},
ptr::{self, NonNull},
};

/// Table taken from GLFW and slightly modified
static KEY_MAPPINGS: [Key; 128] = [
/* 00 */ Key::A,
/* 01 */ Key::S,
Expand Down Expand Up @@ -259,19 +256,25 @@ unsafe extern "C" fn char_callback(window: *mut c_void, code_point: u32) {
}
}

unsafe impl raw_window_handle::HasRawWindowHandle for Window {
fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
let mut handle = raw_window_handle::AppKitWindowHandle::empty();
handle.ns_window = self.window_handle as *mut _;
handle.ns_view = self.view_handle as *mut _;
raw_window_handle::RawWindowHandle::AppKit(handle)
impl HasWindowHandle for Window {
fn window_handle(&self) -> std::result::Result<WindowHandle, HandleError> {
let raw_ns_view = self.view_handle as *mut _;
let ns_view = match NonNull::new(raw_ns_view) {
Some(ns_view) => ns_view,
None => unimplemented!("null view"),
};

let handle = AppKitWindowHandle::new(ns_view);
let raw_handle = RawWindowHandle::AppKit(handle);
unsafe { Ok(WindowHandle::borrow_raw(raw_handle)) }
}
}

unsafe impl raw_window_handle::HasRawDisplayHandle for Window {
fn raw_display_handle(&self) -> raw_window_handle::RawDisplayHandle {
let handle = raw_window_handle::AppKitDisplayHandle::empty();
raw_window_handle::RawDisplayHandle::AppKit(handle)
impl HasDisplayHandle for Window {
fn display_handle(&self) -> std::result::Result<DisplayHandle, HandleError> {
let handle = AppKitDisplayHandle::new();
let raw_handle = RawDisplayHandle::AppKit(handle);
unsafe { Ok(DisplayHandle::borrow_raw(raw_handle)) }
}
}

Expand Down Expand Up @@ -378,7 +381,7 @@ impl Window {
) -> Result<()> {
self.key_handler.update();

buffer_helper::check_buffer_size(buffer, buf_width, buf_height, buf_stride)?;
check_buffer_size(buffer, buf_width, buf_height, buf_stride)?;

unsafe {
mfb_update_with_buffer(
Expand Down Expand Up @@ -442,6 +445,7 @@ impl Window {
)
}

#[inline]
pub fn get_scroll_wheel(&self) -> Option<(f32, f32)> {
let sx = self.shared_data.scroll_x;
let sy = self.shared_data.scroll_y;
Expand Down Expand Up @@ -742,6 +746,7 @@ impl Menu {
}
}

#[inline]
pub fn add_menu_item(&mut self, item: &MenuItem) -> MenuItemHandle {
unsafe {
let item_name = CString::new(item.label.as_str()).unwrap();
Expand Down
3 changes: 1 addition & 2 deletions src/os/posix/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::Result;
use crate::{Key, MenuHandle, MenuItem, MenuItemHandle, UnixMenu, UnixMenuItem};
use crate::{Key, MenuHandle, MenuItem, MenuItemHandle, Result, UnixMenu, UnixMenuItem};

pub struct Menu {
pub internal: UnixMenu,
Expand Down
29 changes: 16 additions & 13 deletions src/os/posix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#![allow(non_upper_case_globals)]

mod common;

#[cfg(feature = "wayland")]
mod wayland;
#[cfg(feature = "x11")]
Expand All @@ -18,12 +19,14 @@ mod xkb_ffi;
#[cfg(feature = "wayland")]
mod xkb_keysyms;

use crate::icon::Icon;
use crate::Result;
use crate::{CursorStyle, MenuHandle, UnixMenu};
use crate::{InputCallback, Key, KeyRepeat, MouseButton, MouseMode, WindowOptions};
use crate::{
icon::Icon, CursorStyle, InputCallback, Key, KeyRepeat, MenuHandle, MouseButton, MouseMode,
Result, UnixMenu, WindowOptions,
};
pub use common::Menu;

use raw_window_handle::{
DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, WindowHandle,
};
use std::os::raw;

// Differentiate between Wayland and X11 at run-time
Expand Down Expand Up @@ -367,24 +370,24 @@ impl Window {
}
}

unsafe impl raw_window_handle::HasRawWindowHandle for Window {
fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
impl HasWindowHandle for Window {
fn window_handle(&self) -> std::result::Result<WindowHandle, HandleError> {
match self {
#[cfg(feature = "x11")]
Window::X11(w) => w.raw_window_handle(),
Window::X11(w) => w.window_handle(),
#[cfg(feature = "wayland")]
Window::Wayland(w) => w.raw_window_handle(),
Window::Wayland(w) => w.window_handle(),
}
}
}

unsafe impl raw_window_handle::HasRawDisplayHandle for Window {
fn raw_display_handle(&self) -> raw_window_handle::RawDisplayHandle {
impl HasDisplayHandle for Window {
fn display_handle(&self) -> std::result::Result<DisplayHandle, HandleError> {
match self {
#[cfg(feature = "x11")]
Window::X11(w) => w.raw_display_handle(),
Window::X11(w) => w.display_handle(),
#[cfg(feature = "wayland")]
Window::Wayland(w) => w.raw_display_handle(),
Window::Wayland(w) => w.display_handle(),
}
}
}
Loading
Loading