diff --git a/src/clipboard.rs b/src/clipboard.rs index c4f7bc4b..6dcfc99e 100644 --- a/src/clipboard.rs +++ b/src/clipboard.rs @@ -1,10 +1,3 @@ -#[cfg(target_os = "macos")] -use crate::macos as platform; -#[cfg(target_os = "windows")] -use crate::win as platform; -#[cfg(target_os = "linux")] -use crate::x11 as platform; - pub fn copy_to_clipboard(data: &str) { - platform::copy_to_clipboard(data) + crate::platform::copy_to_clipboard(data) } diff --git a/src/gl.rs b/src/gl.rs new file mode 100644 index 00000000..2e2d1b45 --- /dev/null +++ b/src/gl.rs @@ -0,0 +1,81 @@ +use crate::platform::gl::*; +use std::ffi::c_void; +use std::marker::PhantomData; +use std::panic::AssertUnwindSafe; + +#[derive(Clone, Debug, PartialEq)] +pub struct GlConfig { + pub version: (u8, u8), + pub profile: Profile, + pub red_bits: u8, + pub blue_bits: u8, + pub green_bits: u8, + pub alpha_bits: u8, + pub depth_bits: u8, + pub stencil_bits: u8, + pub samples: Option, + pub srgb: bool, + pub double_buffer: bool, + pub vsync: bool, +} + +impl Default for GlConfig { + fn default() -> Self { + GlConfig { + version: (3, 2), + profile: Profile::Core, + red_bits: 8, + blue_bits: 8, + green_bits: 8, + alpha_bits: 8, + depth_bits: 24, + stencil_bits: 8, + samples: None, + srgb: true, + double_buffer: true, + vsync: false, + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Profile { + Compatibility, + Core, +} + +#[derive(Debug)] +pub enum GlError { + InvalidWindowHandle, + VersionNotSupported, + CreationFailed(CreationFailedError), +} + +pub struct GlContext { + // AssertUnwindSafe should *not* be here, but this is needed for now to keep semver compatibility + // Remove this in 0.2 + pub(crate) inner: AssertUnwindSafe, + phantom: PhantomData<*mut ()>, +} + +impl GlContext { + pub(crate) fn new(context: crate::platform::gl::GlContext) -> GlContext { + GlContext { inner: AssertUnwindSafe(context), phantom: PhantomData } + } + + pub unsafe fn make_current(&self) { + self.inner.make_current(); + } + + pub unsafe fn make_not_current(&self) { + self.inner.make_not_current(); + } + + pub fn get_proc_address(&self, symbol: &str) -> *const c_void { + self.inner.get_proc_address(symbol) + } + + pub fn swap_buffers(&self) { + self.inner.swap_buffers(); + } +} diff --git a/src/gl/mod.rs b/src/gl/mod.rs deleted file mode 100644 index 34720290..00000000 --- a/src/gl/mod.rs +++ /dev/null @@ -1,131 +0,0 @@ -use std::ffi::c_void; -use std::marker::PhantomData; -use std::panic::AssertUnwindSafe; - -#[cfg(target_os = "windows")] -mod win; -#[cfg(target_os = "windows")] -use win as platform; - -// We need to use this directly within the X11 window creation to negotiate the correct visual -#[cfg(target_os = "linux")] -pub(crate) mod x11; -#[cfg(target_os = "linux")] -pub(crate) use self::x11 as platform; - -#[cfg(target_os = "macos")] -mod macos; -#[cfg(target_os = "macos")] -use macos as platform; - -#[derive(Clone, Debug, PartialEq)] -pub struct GlConfig { - pub version: (u8, u8), - pub profile: Profile, - pub red_bits: u8, - pub blue_bits: u8, - pub green_bits: u8, - pub alpha_bits: u8, - pub depth_bits: u8, - pub stencil_bits: u8, - pub samples: Option, - pub srgb: bool, - pub double_buffer: bool, - pub vsync: bool, -} - -impl Default for GlConfig { - fn default() -> Self { - GlConfig { - version: (3, 2), - profile: Profile::Core, - red_bits: 8, - blue_bits: 8, - green_bits: 8, - alpha_bits: 8, - depth_bits: 24, - stencil_bits: 8, - samples: None, - srgb: true, - double_buffer: true, - vsync: false, - } - } -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum Profile { - Compatibility, - Core, -} - -#[derive(Debug)] -pub enum GlError { - InvalidWindowHandle, - VersionNotSupported, - CreationFailed(platform::CreationFailedError), -} - -pub struct GlContext { - // AssertUnwindSafe should *not* be here, but this is needed for now to keep semver compatibility - // Remove this in 0.2 - context: AssertUnwindSafe, - phantom: PhantomData<*mut ()>, -} - -impl GlContext { - #[cfg(target_os = "windows")] - pub(crate) unsafe fn create( - parent: &raw_window_handle::RawWindowHandle, config: GlConfig, - ) -> Result { - platform::GlContext::create(parent, config) - .map(|context| GlContext { context: AssertUnwindSafe(context), phantom: PhantomData }) - } - - #[cfg(target_os = "macos")] - pub(crate) fn create( - parent: &objc2_app_kit::NSView, config: GlConfig, - ) -> Result { - platform::GlContext::create(parent, config) - .map(|context| GlContext { context: AssertUnwindSafe(context), phantom: PhantomData }) - } - - /// The X11 version needs to be set up in a different way compared to the Windows and macOS - /// versions. So the platform-specific versions should be used to construct the context within - /// baseview, and then this object can be passed to the user. - #[cfg(target_os = "linux")] - pub(crate) fn new(context: platform::GlContext) -> GlContext { - GlContext { context: AssertUnwindSafe(context), phantom: PhantomData } - } - - pub unsafe fn make_current(&self) { - self.context.make_current(); - } - - pub unsafe fn make_not_current(&self) { - self.context.make_not_current(); - } - - pub fn get_proc_address(&self, symbol: &str) -> *const c_void { - self.context.get_proc_address(symbol) - } - - pub fn swap_buffers(&self) { - self.context.swap_buffers(); - } - - /// On macOS the `NSOpenGLView` needs to be resized separtely from our main view. - #[cfg(target_os = "macos")] - pub(crate) fn resize(&self, size: objc2_foundation::NSSize) { - self.context.resize(size); - } - - /// Pointer to the `NSOpenGLView` this context renders into. Used by - /// the parent `NSView`'s `hitTest:` override to collapse hits on the - /// render subview to the parent, so AppKit routes `mouseDown:` on - /// first click in non-key windows. - #[cfg(target_os = "macos")] - pub(crate) fn ns_view(&self) -> &objc2_app_kit::NSView { - self.context.ns_view() - } -} diff --git a/src/lib.rs b/src/lib.rs index 29a7129a..7941c52f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,3 @@ -#[cfg(target_os = "macos")] -mod macos; -#[cfg(target_os = "windows")] -mod win; -#[cfg(target_os = "linux")] -pub(crate) mod x11; - mod clipboard; mod event; mod keyboard; @@ -13,6 +6,8 @@ mod window; mod window_info; mod window_open_options; +pub(crate) mod platform; + #[cfg(feature = "opengl")] pub mod gl; diff --git a/src/platform.rs b/src/platform.rs new file mode 100644 index 00000000..98beb8ec --- /dev/null +++ b/src/platform.rs @@ -0,0 +1,13 @@ +#[cfg(target_os = "macos")] +mod macos; +#[cfg(target_os = "macos")] +pub use macos::*; + +#[cfg(target_os = "linux")] +mod x11; +#[cfg(target_os = "linux")] +pub use x11::*; +#[cfg(target_os = "windows")] +mod win; +#[cfg(target_os = "windows")] +pub use win::*; diff --git a/src/macos/cursor.rs b/src/platform/macos/cursor.rs similarity index 100% rename from src/macos/cursor.rs rename to src/platform/macos/cursor.rs diff --git a/src/gl/macos.rs b/src/platform/macos/gl.rs similarity index 91% rename from src/gl/macos.rs rename to src/platform/macos/gl.rs index 181e4926..564f2461 100644 --- a/src/gl/macos.rs +++ b/src/platform/macos/gl.rs @@ -1,6 +1,6 @@ #![allow(deprecated)] // OpenGL is deprecated on macOS -use super::{GlConfig, GlError, Profile}; +use crate::gl::{GlConfig, GlError, Profile}; use objc2::rc::Retained; use objc2::AllocAnyThread; use objc2::{MainThreadMarker, MainThreadOnly}; @@ -18,7 +18,7 @@ use std::ptr::NonNull; pub type CreationFailedError = (); pub struct GlContext { - view: Retained, + pub(crate) view: Retained, context: Retained, } @@ -120,12 +120,4 @@ impl GlContext { self.view.setFrameSize(size); self.view.setNeedsDisplay(true); } - - /// Pointer to the `NSOpenGLView` this context renders into. Used by - /// the parent `NSView`'s `hitTest:` override to collapse hits on the - /// render subview to the parent, so AppKit routes `mouseDown:` on - /// first click in non-key windows. - pub(crate) fn ns_view(&self) -> &NSOpenGLView { - &self.view - } } diff --git a/src/macos/keyboard.rs b/src/platform/macos/keyboard.rs similarity index 100% rename from src/macos/keyboard.rs rename to src/platform/macos/keyboard.rs diff --git a/src/macos/mod.rs b/src/platform/macos/mod.rs similarity index 62% rename from src/macos/mod.rs rename to src/platform/macos/mod.rs index 8b5a303b..5cb70243 100644 --- a/src/macos/mod.rs +++ b/src/platform/macos/mod.rs @@ -4,3 +4,6 @@ mod view; mod window; pub use window::*; + +#[cfg(feature = "opengl")] +pub mod gl; diff --git a/src/macos/view.rs b/src/platform/macos/view.rs similarity index 98% rename from src/macos/view.rs rename to src/platform/macos/view.rs index 0b4dab12..53ec7fbf 100644 --- a/src/macos/view.rs +++ b/src/platform/macos/view.rs @@ -84,7 +84,8 @@ impl BaseviewView { #[cfg(feature = "opengl")] if let Some(gl_config) = options.gl_config { - let gl_context = crate::gl::GlContext::create(view.view, gl_config).unwrap(); + let gl_context = super::gl::GlContext::create(view.view, gl_config).unwrap(); + let gl_context = crate::gl::GlContext::new(gl_context); let Ok(()) = view.gl_context.set(gl_context) else { unreachable!() }; } @@ -151,7 +152,7 @@ impl BaseviewView { // macOS. #[cfg(feature = "opengl")] if let Some(gl_context) = this.gl_context.get() { - gl_context.resize(size); + gl_context.inner.resize(size); } // If this is a standalone window then we'll also need to resize the window itself @@ -275,7 +276,7 @@ impl ViewImpl for BaseviewView { /// `hitTest:` override that collapses hits on baseview's internal /// OpenGL render subview to this NSView. /// - /// `src/gl/macos.rs` attaches an `NSOpenGLView` as a subview of this + /// `src/gl/gl` attaches an `NSOpenGLView` as a subview of this /// view so the GL context is isolated from event handling. The side /// effect is that `[NSView hitTest:]` returns the GL subview for /// every click inside our frame — `NSOpenGLView` inherits the @@ -306,7 +307,7 @@ impl ViewImpl for BaseviewView { #[cfg(feature = "opengl")] { if let Some(gl_context) = this.gl_context.get() { - if super_result == gl_context.ns_view() { + if *super_result == **gl_context.inner.0.view { return Some(this.view); } } diff --git a/src/macos/window.rs b/src/platform/macos/window.rs similarity index 98% rename from src/macos/window.rs rename to src/platform/macos/window.rs index f56a0f74..8e07bf55 100644 --- a/src/macos/window.rs +++ b/src/platform/macos/window.rs @@ -18,7 +18,7 @@ use crate::{MouseCursor, Size, WindowHandler, WindowInfo, WindowOpenOptions}; #[cfg(feature = "opengl")] use crate::gl::GlContext; -use crate::macos::view::{BaseviewView, ViewParentingType}; +use crate::platform::macos::view::{BaseviewView, ViewParentingType}; use crate::wrappers::appkit::{create_window, extract_raw_window_handle, View, ViewRef}; pub struct WindowHandle { diff --git a/src/win/drop_target.rs b/src/platform/win/drop_target.rs similarity index 98% rename from src/win/drop_target.rs rename to src/platform/win/drop_target.rs index 00262d80..5d79e6b2 100644 --- a/src/win/drop_target.rs +++ b/src/platform/win/drop_target.rs @@ -18,7 +18,7 @@ use crate::{DropData, DropEffect, Event, EventStatus, MouseEvent, PhyPoint, Poin use super::WindowState; #[implement(IDropTarget)] -pub(super) struct DropTarget { +pub(crate) struct DropTarget { window_state: Weak, // These are cached since DragOver and DragLeave callbacks don't provide them, @@ -28,7 +28,7 @@ pub(super) struct DropTarget { } impl DropTarget { - pub(super) fn new(window_state: Weak) -> Self { + pub(crate) fn new(window_state: Weak) -> Self { Self { window_state, drag_position: Cell::new(Point::new(0.0, 0.0)), diff --git a/src/gl/win.rs b/src/platform/win/gl.rs similarity index 99% rename from src/gl/win.rs rename to src/platform/win/gl.rs index 52a1b153..a173daf9 100644 --- a/src/gl/win.rs +++ b/src/platform/win/gl.rs @@ -27,7 +27,7 @@ use windows_sys::{ use raw_window_handle::RawWindowHandle; -use super::{GlConfig, GlError, Profile}; +use crate::gl::*; use crate::wrappers::win32::uuid::Uuid; // See https://www.khronos.org/registry/OpenGL/extensions/ARB/WGL_ARB_create_context.txt diff --git a/src/win/hook.rs b/src/platform/win/hook.rs similarity index 99% rename from src/win/hook.rs rename to src/platform/win/hook.rs index 34a67ff3..f74af7cc 100644 --- a/src/win/hook.rs +++ b/src/platform/win/hook.rs @@ -15,7 +15,7 @@ use windows_sys::Win32::{ }, }; -use crate::win::BaseviewWindow; +use super::*; use crate::wrappers::win32::window::wnd_proc; // track all windows opened by this instance of baseview diff --git a/src/win/keyboard.rs b/src/platform/win/keyboard.rs similarity index 99% rename from src/win/keyboard.rs rename to src/platform/win/keyboard.rs index 2b6766c1..d2f03a54 100644 --- a/src/win/keyboard.rs +++ b/src/platform/win/keyboard.rs @@ -45,7 +45,7 @@ const SHIFT_STATE_ALTGR: ShiftState = 2; const N_SHIFT_STATE: ShiftState = 4; /// Per-window keyboard state. -pub(super) struct KeyboardState { +pub(crate) struct KeyboardState { hkl: HKL, // A map from (vk, is_shifted) to string val key_vals: HashMap<(VkCode, ShiftState), String>, diff --git a/src/win/mod.rs b/src/platform/win/mod.rs similarity index 64% rename from src/win/mod.rs rename to src/platform/win/mod.rs index 3a4d367d..fb1f608b 100644 --- a/src/win/mod.rs +++ b/src/platform/win/mod.rs @@ -4,3 +4,6 @@ mod keyboard; mod window; pub use window::*; + +#[cfg(feature = "opengl")] +pub mod gl; diff --git a/src/win/window.rs b/src/platform/win/window.rs similarity index 97% rename from src/win/window.rs rename to src/platform/win/window.rs index 0b53967f..4316a856 100644 --- a/src/win/window.rs +++ b/src/platform/win/window.rs @@ -26,7 +26,7 @@ use raw_window_handle::{ const BV_WINDOW_MUST_CLOSE: u32 = WM_USER + 1; -use crate::win::hook::{self, KeyboardHookHandle}; +use super::*; use crate::{ Event, EventStatus, MouseButton, MouseCursor, MouseEvent, PhyPoint, PhySize, ScrollDelta, Size, WindowEvent, WindowHandler, WindowInfo, WindowOpenOptions, WindowScalePolicy, @@ -36,8 +36,6 @@ use super::drop_target::DropTarget; use super::keyboard::KeyboardState; use crate::wrappers::win32::cursor::SystemCursor; -#[cfg(feature = "opengl")] -use crate::gl::GlContext; use crate::wrappers::win32::window::*; use crate::wrappers::win32::{ ole_initialize, run_thread_message_loop_until, Dpi, DpiAwarenessContext, ExtendedUser32, Rect, @@ -111,7 +109,7 @@ pub struct BaseviewWindow { // Things not directly used, but kept so their Drop impl runs when the window is destroyed _parent_handle: ParentHandle, - _keyboard_hook: Cell>, + _keyboard_hook: Cell>, _drop_target: Cell>>, #[cfg(feature = "opengl")] @@ -161,10 +159,11 @@ impl WindowImpl for BaseviewWindow { handle.hwnd = hwnd; let handle = RawWindowHandle::Win32(handle); - let gl_context = unsafe { GlContext::create(&handle, gl_config) } + let gl_context = unsafe { gl::GlContext::create(&handle, gl_config) } .expect("Could not create OpenGL context"); - let Ok(()) = self.window_state.gl_context.set(gl_context) else { + let Ok(()) = self.window_state.gl_context.set(crate::gl::GlContext::new(gl_context)) + else { unreachable!(); }; }; @@ -459,7 +458,7 @@ unsafe fn wnd_proc_inner( /// because of the Windows message loops' reentrant nature. Care still needs to be taken to prevent /// `handler` from indirectly triggering other events that would also need to be handled using /// `handler`. -pub(super) struct WindowState { +pub(crate) struct WindowState { /// The HWND belonging to this window. The window's actual state is stored in the `WindowState` /// struct associated with this HWND through `unsafe { GetWindowLongPtrW(self.hwnd, /// GWLP_USERDATA) } as *const WindowState`. @@ -484,7 +483,7 @@ pub(super) struct WindowState { pub deferred_tasks: RefCell>, #[cfg(feature = "opengl")] - pub gl_context: core::cell::OnceCell, + pub gl_context: core::cell::OnceCell, } impl WindowState { @@ -529,7 +528,7 @@ impl WindowState { handler.on_event(&mut window, event) } - pub(super) fn window_info(&self) -> WindowInfo { + pub(crate) fn window_info(&self) -> WindowInfo { WindowInfo::from_physical_size(self.current_size.get(), self.current_scale_factor()) } @@ -540,7 +539,7 @@ impl WindowState { } } - pub(super) fn keyboard_state(&self) -> Ref<'_, KeyboardState> { + pub(crate) fn keyboard_state(&self) -> Ref<'_, KeyboardState> { self.keyboard_state.borrow() } @@ -568,7 +567,7 @@ impl WindowState { /// Tasks that must be deferred until the end of [`wnd_proc()`] to avoid reentrant `WindowState` /// borrows. See the docstring on [`WindowState::deferred_tasks`] for more information. #[derive(Debug, Clone)] -pub(super) enum WindowTask { +pub(crate) enum WindowTask { /// Resize the window to the given size. The size is in logical pixels. DPI scaling is applied /// automatically. Resize(Size), @@ -709,7 +708,7 @@ impl Window<'_> { } #[cfg(feature = "opengl")] - pub fn gl_context(&self) -> Option<&GlContext> { + pub fn gl_context(&self) -> Option<&crate::gl::GlContext> { self.state.gl_context.get() } } diff --git a/src/x11/cursor.rs b/src/platform/x11/cursor.rs similarity index 99% rename from src/x11/cursor.rs rename to src/platform/x11/cursor.rs index 56ff0d2a..4c620c94 100644 --- a/src/x11/cursor.rs +++ b/src/platform/x11/cursor.rs @@ -42,7 +42,7 @@ fn load_first_existing_cursor( Ok(None) } -pub(super) fn get_xcursor( +pub(crate) fn get_xcursor( conn: &XCBConnection, screen: usize, cursor_handle: &CursorHandle, cursor: MouseCursor, ) -> Result> { let load = |name: &str| load_cursor(conn, cursor_handle, name); diff --git a/src/x11/drag_n_drop.rs b/src/platform/x11/drag_n_drop.rs similarity index 99% rename from src/x11/drag_n_drop.rs rename to src/platform/x11/drag_n_drop.rs index 8454bc8f..64b6880e 100644 --- a/src/x11/drag_n_drop.rs +++ b/src/platform/x11/drag_n_drop.rs @@ -18,7 +18,7 @@ use x11rb::{ }; use super::xcb_connection::{Atoms, GetPropertyError}; -use crate::x11::{Window, WindowInner}; +use super::*; use crate::{DropData, Event, MouseEvent, PhyPoint, WindowHandler}; use DragNDropState::*; diff --git a/src/x11/event_loop.rs b/src/platform/x11/event_loop.rs similarity index 98% rename from src/x11/event_loop.rs rename to src/platform/x11/event_loop.rs index 0fe6b34d..5a012867 100644 --- a/src/x11/event_loop.rs +++ b/src/platform/x11/event_loop.rs @@ -1,8 +1,9 @@ +use super::drag_n_drop::DragNDropState; +use super::keyboard::{convert_key_press_event, convert_key_release_event, key_mods}; +use super::*; + use crate::wrappers::connection_poller::{ConnectionPoller, PollStatus}; use crate::wrappers::xkbcommon::XkbcommonState; -use crate::x11::drag_n_drop::DragNDropState; -use crate::x11::keyboard::{convert_key_press_event, convert_key_release_event, key_mods}; -use crate::x11::{ParentHandle, Window, WindowInner}; use crate::{ Event, MouseButton, MouseEvent, PhyPoint, PhySize, ScrollDelta, WindowEvent, WindowHandler, WindowInfo, @@ -13,7 +14,7 @@ use std::time::{Duration, Instant}; use x11rb::connection::Connection; use x11rb::protocol::Event as XEvent; -pub(super) struct EventLoop { +pub(crate) struct EventLoop { handler: Box, window: WindowInner, parent_handle: Option, diff --git a/src/gl/x11.rs b/src/platform/x11/gl.rs similarity index 98% rename from src/gl/x11.rs rename to src/platform/x11/gl.rs index 53f60fee..eb6513bb 100644 --- a/src/gl/x11.rs +++ b/src/platform/x11/gl.rs @@ -1,14 +1,14 @@ -use super::{GlConfig, GlError}; -use crate::x11::XcbConnection; +use super::*; +use crate::gl::*; +use crate::wrappers::glx::*; +use crate::wrappers::xlib::{XErrorHandler, XLibError}; + use std::ffi::{c_void, CString}; use std::os::raw::c_ulong; use std::rc::Rc; use x11_dl::error::OpenError; use x11_dl::glx::GLXContext; -use crate::wrappers::glx::*; -use crate::wrappers::xlib::{XErrorHandler, XLibError}; - #[derive(Debug)] pub enum CreationFailedError { NoValidFBConfig, diff --git a/src/x11/keyboard.rs b/src/platform/x11/keyboard.rs similarity index 99% rename from src/x11/keyboard.rs rename to src/platform/x11/keyboard.rs index 3af346dc..6bb0a5f1 100644 --- a/src/x11/keyboard.rs +++ b/src/platform/x11/keyboard.rs @@ -377,7 +377,7 @@ fn hardware_keycode_to_code(hw_keycode: u16) -> Code { // Extracts the keyboard modifiers from, e.g., the `state` field of // `x11rb::protocol::xproto::ButtonPressEvent` -pub(super) fn key_mods(mods: KeyButMask) -> Modifiers { +pub(crate) fn key_mods(mods: KeyButMask) -> Modifiers { let mut ret = Modifiers::default(); let key_masks = [ (KeyButMask::SHIFT, Modifiers::SHIFT), @@ -398,7 +398,7 @@ pub(super) fn key_mods(mods: KeyButMask) -> Modifiers { ret } -pub(super) fn convert_key_press_event( +pub(crate) fn convert_key_press_event( key_press: &KeyPressEvent, state: &mut Option, ) -> KeyboardEvent { let hw_keycode = key_press.detail; @@ -418,7 +418,7 @@ pub(super) fn convert_key_press_event( KeyboardEvent { code, key, modifiers, location, state, repeat: false, is_composing: false } } -pub(super) fn convert_key_release_event( +pub(crate) fn convert_key_release_event( key_release: &KeyReleaseEvent, state: &mut Option, ) -> KeyboardEvent { let hw_keycode = key_release.detail; diff --git a/src/x11/mod.rs b/src/platform/x11/mod.rs similarity index 81% rename from src/x11/mod.rs rename to src/platform/x11/mod.rs index a6b884ef..de14b33d 100644 --- a/src/x11/mod.rs +++ b/src/platform/x11/mod.rs @@ -9,3 +9,6 @@ mod drag_n_drop; mod event_loop; mod keyboard; mod visual_info; + +#[cfg(feature = "opengl")] +pub mod gl; diff --git a/src/x11/visual_info.rs b/src/platform/x11/visual_info.rs similarity index 92% rename from src/x11/visual_info.rs rename to src/platform/x11/visual_info.rs index 1b355a85..943b0229 100644 --- a/src/x11/visual_info.rs +++ b/src/platform/x11/visual_info.rs @@ -1,4 +1,4 @@ -use crate::x11::xcb_connection::XcbConnection; +use super::xcb_connection::XcbConnection; use std::error::Error; use x11rb::connection::Connection; use x11rb::protocol::xproto::{ @@ -6,9 +6,9 @@ use x11rb::protocol::xproto::{ }; use x11rb::COPY_FROM_PARENT; -pub(super) struct WindowVisualConfig { +pub(crate) struct WindowVisualConfig { #[cfg(feature = "opengl")] - pub fb_config: Option, + pub fb_config: Option, pub visual_depth: u8, pub visual_id: Visualid, @@ -24,7 +24,7 @@ impl WindowVisualConfig { let Some(gl_config) = gl_config else { return Self::find_best_visual_config(connection) }; let (fb_config, window_config) = - crate::gl::platform::GlContext::get_fb_config_and_visual(connection, gl_config) + super::gl::GlContext::get_fb_config_and_visual(connection, gl_config) .expect("Could not fetch framebuffer config"); Ok(Self { diff --git a/src/x11/window.rs b/src/platform/x11/window.rs similarity index 98% rename from src/x11/window.rs rename to src/platform/x11/window.rs index e51e1011..820c4678 100644 --- a/src/x11/window.rs +++ b/src/platform/x11/window.rs @@ -27,9 +27,9 @@ use crate::{ }; #[cfg(feature = "opengl")] -use crate::gl::{platform, GlContext}; -use crate::x11::event_loop::EventLoop; -use crate::x11::visual_info::WindowVisualConfig; +use crate::gl::*; + +use super::{event_loop::EventLoop, visual_info::WindowVisualConfig}; pub struct WindowHandle { raw_window_handle: Option, @@ -280,8 +280,9 @@ impl<'a> Window<'a> { // Because of the visual negotation we had to take some extra steps to create this context let context = - platform::GlContext::create(window, Rc::clone(&xcb_connection), fb_config) + super::gl::GlContext::create(window, Rc::clone(&xcb_connection), fb_config) .expect("Could not create OpenGL context"); + GlContext::new(context) }); diff --git a/src/x11/xcb_connection.rs b/src/platform/x11/xcb_connection.rs similarity index 98% rename from src/x11/xcb_connection.rs rename to src/platform/x11/xcb_connection.rs index 0bdcc9aa..f57d03ec 100644 --- a/src/x11/xcb_connection.rs +++ b/src/platform/x11/xcb_connection.rs @@ -46,7 +46,7 @@ pub struct XcbConnection { pub(crate) atoms: Atoms, pub(crate) resources: resource_manager::Database, pub(crate) cursor_handle: CursorHandle, - pub(super) cursor_cache: RefCell>, + pub(crate) cursor_cache: RefCell>, } impl XcbConnection { diff --git a/src/x11/xcb_connection/get_property.rs b/src/platform/x11/xcb_connection/get_property.rs similarity index 99% rename from src/x11/xcb_connection/get_property.rs rename to src/platform/x11/xcb_connection/get_property.rs index 317b33cf..0da440e5 100644 --- a/src/x11/xcb_connection/get_property.rs +++ b/src/platform/x11/xcb_connection/get_property.rs @@ -78,7 +78,7 @@ impl Error for GetPropertyError {} // To test if `get_property` works correctly, set this to 1. const PROPERTY_BUFFER_SIZE: u32 = 1024; // 4k of RAM ought to be enough for anyone! -pub(super) fn get_property( +pub(crate) fn get_property( window: xproto::Window, property: xproto::Atom, property_type: xproto::Atom, conn: &XCBConnection, ) -> Result, GetPropertyError> { diff --git a/src/window.rs b/src/window.rs index 25b53d1e..2ac36603 100644 --- a/src/window.rs +++ b/src/window.rs @@ -6,14 +6,7 @@ use raw_window_handle::{ use crate::event::{Event, EventStatus}; use crate::window_open_options::WindowOpenOptions; -use crate::{MouseCursor, Size}; - -#[cfg(target_os = "macos")] -use crate::macos as platform; -#[cfg(target_os = "windows")] -use crate::win as platform; -#[cfg(target_os = "linux")] -use crate::x11 as platform; +use crate::{platform, MouseCursor, Size}; pub struct WindowHandle { window_handle: platform::WindowHandle, diff --git a/src/wrappers/glx.rs b/src/wrappers/glx.rs index be6ba4aa..06b6800c 100644 --- a/src/wrappers/glx.rs +++ b/src/wrappers/glx.rs @@ -1,6 +1,6 @@ use super::xlib::*; -use crate::gl::platform::CreationFailedError; use crate::gl::{GlConfig, GlError, Profile}; +use crate::platform::gl::CreationFailedError; use std::ffi::{c_ulong, c_void, CStr}; use std::os::raw::c_int; diff --git a/src/wrappers/xkbcommon.rs b/src/wrappers/xkbcommon.rs index 8b48df9a..53db6f20 100644 --- a/src/wrappers/xkbcommon.rs +++ b/src/wrappers/xkbcommon.rs @@ -8,7 +8,7 @@ pub struct XkbcommonState { } impl XkbcommonState { - pub fn new(xcb_connection: &crate::x11::XcbConnection) -> Option { + pub fn new(xcb_connection: &crate::platform::XcbConnection) -> Option { let xkb_common = xkbc::xkbcommon_option()?; let xkb_x11 = xkbc::x11::xkbcommon_x11_option()?;