Skip to content
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
6 changes: 4 additions & 2 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
/src/platform/ios.rs @madsmtm
/src/platform/macos.rs @madsmtm
/src/platform_impl/apple @madsmtm
/winit-common/src/core_foundation @madsmtm
/winit-common/src/event_handler.rs @madsmtm

# Unix
/src/platform_impl/linux/mod.rs @kchibisov
# XKB
/winit-common/src/xkb @kchibisov

# Wayland
/src/platform/wayland.rs @kchibisov
Expand Down
13 changes: 12 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,18 @@ jobs:

- name: Test winit Android
if: contains(matrix.platform.target, 'android')
run: cargo $CMD test -p winit-android --target=${{ matrix.platform.target }} --features native-activity --no-run
run: cargo $CMD test -p winit-android --features native-activity --no-run

- name: Test winit Common (EventHandler)
run: cargo $CMD test -p winit-common --features event-handler --no-run

- name: Test winit Common (CF)
if: contains(matrix.platform.target, 'apple')
run: cargo $CMD test -p winit-common --features core-foundation --no-run

- name: Test winit Common (XKB)
if: contains(matrix.platform.target, 'linux')
run: cargo $CMD test -p winit-common --features xkb,x11,wayland --no-run

- name: Test winit Orbital
if: contains(matrix.platform.target, 'redox')
Expand Down
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ rust-version = "1.80"
# `winit` has no version here to allow using it in dev deps for docs.
winit = { path = "." }
winit-android = { version = "0.0.0", path = "winit-android" }
winit-common = { version = "0.0.0", path = "winit-common" }
winit-core = { version = "0.0.0", path = "winit-core" }
winit-orbital = { version = "0.0.0", path = "winit-orbital" }
winit-win32 = { version = "0.0.0", path = "winit-win32" }
Expand Down Expand Up @@ -172,12 +173,13 @@ wayland = [
"sctk",
"ahash",
"memmap2",
"winit-common/wayland",
]
wayland-csd-adwaita = ["sctk-adwaita", "sctk-adwaita/ab_glyph"]
wayland-csd-adwaita-crossfont = ["sctk-adwaita", "sctk-adwaita/crossfont"]
wayland-csd-adwaita-notitle = ["sctk-adwaita"]
wayland-dlopen = ["wayland-backend/dlopen"]
x11 = ["x11-dl", "bytemuck", "percent-encoding", "xkbcommon-dl/x11", "x11rb"]
x11 = ["x11-dl", "bytemuck", "percent-encoding", "xkbcommon-dl/x11", "x11rb", "winit-common/x11"]

[build-dependencies]
cfg_aliases.workspace = true
Expand Down Expand Up @@ -208,6 +210,7 @@ winit-android.workspace = true
block2.workspace = true
dispatch2.workspace = true
objc2.workspace = true
winit-common = { workspace = true, features = ["core-foundation", "event-handler"] }

# AppKit
[target.'cfg(target_os = "macos")'.dependencies]
Expand Down Expand Up @@ -358,6 +361,7 @@ wayland-backend = { workspace = true, optional = true }
wayland-client = { workspace = true, optional = true }
wayland-protocols = { workspace = true, optional = true }
wayland-protocols-plasma = { workspace = true, optional = true }
winit-common = { workspace = true, features = ["xkb"] }
x11-dl = { workspace = true, optional = true }
x11rb = { workspace = true, optional = true, features = [
"allow-unsafe-code",
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/apple/appkit/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use dispatch2::MainThreadBound;
use objc2::MainThreadMarker;
use objc2_app_kit::{NSApplication, NSApplicationActivationPolicy, NSRunningApplication};
use objc2_foundation::NSNotification;
use winit_common::core_foundation::EventLoopProxy;
use winit_common::event_handler::EventHandler;
use winit_core::application::ApplicationHandler;
use winit_core::event::{StartCause, WindowEvent};
use winit_core::event_loop::ControlFlow;
use winit_core::window::WindowId;

use super::super::event_handler::EventHandler;
use super::super::event_loop_proxy::EventLoopProxy;
use super::event_loop::{notify_windows_of_exit, stop_app_immediately, ActiveEventLoop};
use super::menu;
use super::observer::{EventLoopWaker, RunLoop};
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/apple/appkit/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ use winit_core::event_loop::{
use winit_core::monitor::MonitorHandle as CoreMonitorHandle;
use winit_core::window::Theme;

use super::super::notification_center::create_observer;
use super::app::override_send_event;
use super::app_state::AppState;
use super::cursor::CustomCursor;
use super::event::dummy_event;
use super::monitor;
use super::notification_center::create_observer;
use super::observer::setup_control_flow_observers;
use crate::platform::macos::ActivationPolicy;
use crate::platform_impl::Window;
Expand Down
1 change: 1 addition & 0 deletions src/platform_impl/apple/appkit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod event_loop;
mod ffi;
mod menu;
mod monitor;
mod notification_center;
mod observer;
mod view;
mod window;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// NOTE: This is symlinked to be contained in both the AppKit and UIKit implementations.
use std::ptr::NonNull;

use block2::RcBlock;
Expand All @@ -11,7 +12,7 @@ use objc2_foundation::{
///
/// This is used in Winit as an alternative to declaring an application delegate, as we want to
/// give the user full control over those.
pub fn create_observer(
pub(crate) fn create_observer(
center: &NSNotificationCenter,
name: &NSNotificationName,
handler: impl Fn(&NSNotification) + 'static,
Expand Down
5 changes: 0 additions & 5 deletions src/platform_impl/apple/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
//! Apple/Darwin-specific implementations

#[cfg(target_os = "macos")]
mod appkit;
mod event_handler;
mod event_loop_proxy;
mod notification_center;
#[cfg(not(target_os = "macos"))]
mod uikit;

Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/apple/uikit/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ use objc2_core_foundation::{
CGSize,
};
use objc2_ui_kit::{UIApplication, UICoordinateSpace, UIView};
use winit_common::core_foundation::EventLoopProxy;
use winit_common::event_handler::EventHandler;
use winit_core::application::ApplicationHandler;
use winit_core::event::{StartCause, SurfaceSizeWriter, WindowEvent};
use winit_core::event_loop::ControlFlow;
use winit_core::window::WindowId;

use super::super::event_handler::EventHandler;
use super::super::event_loop_proxy::EventLoopProxy;
use super::window::WinitUIWindow;
use super::ActiveEventLoop;

Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/apple/uikit/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use winit_core::event_loop::{
use winit_core::monitor::MonitorHandle as CoreMonitorHandle;
use winit_core::window::{Theme, Window as CoreWindow};

use super::super::notification_center::create_observer;
use super::app_state::{send_occluded_event_for_all_windows, AppState};
use super::notification_center::create_observer;
use super::{app_state, monitor, MonitorHandle};
use crate::platform_impl::Window;

Expand Down
1 change: 1 addition & 0 deletions src/platform_impl/apple/uikit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod app_state;
mod event_loop;
mod monitor;
mod notification_center;
mod view;
mod view_controller;
mod window;
Expand Down
1 change: 1 addition & 0 deletions src/platform_impl/apple/uikit/notification_center.rs
1 change: 0 additions & 1 deletion src/platform_impl/linux/common/mod.rs

This file was deleted.

3 changes: 1 addition & 2 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ use std::time::Duration;

#[cfg(x11_platform)]
use dpi::Size;
pub(crate) use winit_common::xkb::{physicalkey_to_scancode, scancode_to_physicalkey};
use winit_core::application::ApplicationHandler;
use winit_core::error::{EventLoopError, NotSupportedError};
use winit_core::event_loop::pump_events::PumpStatus;
use winit_core::event_loop::ActiveEventLoop;
use winit_core::window::ActivationToken;

pub(crate) use self::common::xkb::{physicalkey_to_scancode, scancode_to_physicalkey};
#[cfg(x11_platform)]
use crate::platform::x11::WindowType as XWindowType;

pub(crate) mod common;
#[cfg(wayland_platform)]
pub(crate) mod wayland;
#[cfg(x11_platform)]
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/linux/wayland/seat/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use sctk::reexports::client::protocol::wl_keyboard::{
use sctk::reexports::client::protocol::wl_seat::WlSeat;
use sctk::reexports::client::{Connection, Dispatch, Proxy, QueueHandle, WEnum};
use tracing::warn;
use winit_common::xkb::Context;
use winit_core::event::{ElementState, WindowEvent};
use winit_core::keyboard::ModifiersState;

use crate::platform_impl::common::xkb::Context;
use crate::platform_impl::wayland::event_loop::sink::EventSink;
use crate::platform_impl::wayland::state::WinitState;
use crate::platform_impl::wayland::{self, WindowId};
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/linux/wayland/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ impl CoreWindow for Window {
}

fn reset_dead_keys(&self) {
crate::platform_impl::common::xkb::reset_dead_keys()
winit_common::xkb::reset_dead_keys()
}

fn surface_position(&self) -> PhysicalPosition<i32> {
Expand Down
3 changes: 1 addition & 2 deletions src/platform_impl/linux/x11/event_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::slice;
use std::sync::{Arc, Mutex};

use dpi::{PhysicalPosition, PhysicalSize};
use winit_common::xkb::{self, Context, XkbState};
use winit_core::application::ApplicationHandler;
use winit_core::event::{
ButtonSource, DeviceEvent, DeviceId, ElementState, FingerId, Ime, MouseButton,
Expand All @@ -28,8 +29,6 @@ use x11rb::protocol::xproto::{self, ConnectionExt as _, ModMask};
use x11rb::x11_utils::{ExtensionInformation, Serialize};
use xkbcommon_dl::xkb_mod_mask_t;

use crate::platform_impl::common::xkb::{self, XkbState};
use crate::platform_impl::platform::common::xkb::Context;
use crate::platform_impl::platform::x11::ime::{ImeEvent, ImeEventReceiver, ImeRequest};
use crate::platform_impl::platform::x11::ActiveEventLoop;
use crate::platform_impl::x11::atoms::*;
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/linux/x11/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use calloop::ping::Ping;
use calloop::{EventLoop as Loop, Readiness};
use libc::{setlocale, LC_CTYPE};
use tracing::warn;
use winit_common::xkb::Context;
use winit_core::application::ApplicationHandler;
use winit_core::cursor::{CustomCursor as CoreCustomCursor, CustomCursorSource};
use winit_core::error::{EventLoopError, RequestError};
Expand All @@ -35,7 +36,6 @@ use x11rb::x11_utils::X11Error as LogicalError;
use x11rb::xcb_ffi::ReplyOrIdError;

use crate::platform::x11::XlibErrorHook;
use crate::platform_impl::common::xkb::Context;
use crate::platform_impl::platform::min_timeout;
use crate::platform_impl::x11::window::Window;

Expand Down
3 changes: 1 addition & 2 deletions src/platform_impl/linux/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use super::{
XConnection,
};
use crate::platform::x11::{WindowAttributesX11, WindowType};
use crate::platform_impl::common;
use crate::platform_impl::x11::atoms::*;
use crate::platform_impl::x11::util::rgba_to_cardinals;
use crate::platform_impl::x11::{
Expand Down Expand Up @@ -85,7 +84,7 @@ impl CoreWindow for Window {
}

fn reset_dead_keys(&self) {
common::xkb::reset_dead_keys();
winit_common::xkb::reset_dead_keys();
}

fn surface_position(&self) -> PhysicalPosition<i32> {
Expand Down
42 changes: 42 additions & 0 deletions winit-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[package]
description = "Winit implementation helpers"
documentation = "https://docs.rs/winit-common"
edition.workspace = true
license.workspace = true
name = "winit-common"
repository.workspace = true
rust-version.workspace = true
version = "0.0.0"

[features]
# Event Handler
event-handler = []

# XKB
wayland = ["dep:memmap2"]
x11 = ["xkbcommon-dl?/x11", "dep:x11-dl"]
xkb = ["dep:xkbcommon-dl", "dep:smol_str"]

# CoreFoundation
core-foundation = ["dep:objc2", "dep:objc2-core-foundation"]

[dependencies]
smol_str = { workspace = true, optional = true }
tracing.workspace = true
winit-core.workspace = true

# XKB
memmap2 = { workspace = true, optional = true }
x11-dl = { workspace = true, optional = true }
xkbcommon-dl = { workspace = true, optional = true }

# CoreFoundation
objc2 = { workspace = true, optional = true }
objc2-core-foundation = { workspace = true, optional = true, features = [
"std",
"CFRunLoop",
"CFString",
] }

[package.metadata.docs.rs]
all-features = true
1 change: 1 addition & 0 deletions winit-common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use winit_core::event_loop::EventLoopProxyProvider;
///
/// See <https://developer.apple.com/documentation/corefoundation/cfrunloopsource?language=objc>.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct EventLoopProxy {
pub struct EventLoopProxy {
source: CFRetained<CFRunLoopSource>,
/// Cached value of `CFRunLoopGetMain`.
main_loop: CFRetained<CFRunLoop>,
Expand All @@ -28,7 +28,7 @@ impl EventLoopProxy {
/// Create a new proxy, registering it to be performed on the main thread.
///
/// The provided closure should call `proxy_wake_up` on the application.
pub(crate) fn new<F: Fn() + 'static>(mtm: MainThreadMarker, signaller: F) -> Self {
pub fn new<F: Fn() + 'static>(mtm: MainThreadMarker, signaller: F) -> Self {
// We use an `Arc` here to make sure that the reference-counting of the signal container is
// atomic (`Retained`/`CFRetained` would be valid alternatives too).
let signaller = Arc::new(signaller);
Expand Down Expand Up @@ -99,8 +99,7 @@ impl EventLoopProxy {

// FIXME(madsmtm): Use this on macOS too.
// More difficult there, since the user can re-start the event loop.
#[cfg_attr(target_os = "macos", allow(dead_code))]
pub(crate) fn invalidate(&self) {
pub fn invalidate(&self) {
// NOTE: We do NOT fire this on `Drop`, since we want the proxy to be cloneable, such that
// we only need to register a single source even if there's multiple proxies in use.
self.source.invalidate();
Expand Down
3 changes: 3 additions & 0 deletions winit-common/src/core_foundation/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod event_loop_proxy;

pub use event_loop_proxy::*;
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use winit_core::application::ApplicationHandler;
/// A helper type for storing a reference to `ApplicationHandler`, allowing interior mutable access
/// to it within the execution of a closure.
#[derive(Default)]
pub(crate) struct EventHandler {
pub struct EventHandler {
/// This can be in the following states:
/// - Not registered by the event loop, or terminated (None).
/// - Present (Some(handler)).
Expand All @@ -26,7 +26,7 @@ impl fmt::Debug for EventHandler {
}

impl EventHandler {
pub(crate) fn new() -> Self {
pub fn new() -> Self {
Self { inner: RefCell::new(None) }
}

Expand All @@ -35,7 +35,7 @@ impl EventHandler {
/// This is similar to using the `scoped-tls` or `scoped-tls-hkt` crates
/// to store the handler in a thread local, such that it can be accessed
/// from within the closure.
pub(crate) fn set<'handler, R>(
pub fn set<'handler, R>(
&self,
app: Box<dyn ApplicationHandler + 'handler>,
closure: impl FnOnce() -> R,
Expand Down Expand Up @@ -104,16 +104,15 @@ impl EventHandler {
// soundness.
}

#[cfg(target_os = "macos")]
pub(crate) fn in_use(&self) -> bool {
pub fn in_use(&self) -> bool {
self.inner.try_borrow().is_err()
}

pub(crate) fn ready(&self) -> bool {
pub fn ready(&self) -> bool {
matches!(self.inner.try_borrow().as_deref(), Ok(Some(_)))
}

pub(crate) fn handle(&self, callback: impl FnOnce(&mut (dyn ApplicationHandler + '_))) {
pub fn handle(&self, callback: impl FnOnce(&mut (dyn ApplicationHandler + '_))) {
match self.inner.try_borrow_mut().as_deref_mut() {
Ok(Some(ref mut user_app)) => {
// It is important that we keep the reference borrowed here,
Expand All @@ -137,7 +136,7 @@ impl EventHandler {
}
}

pub(crate) fn terminate(&self) {
pub fn terminate(&self) {
match self.inner.try_borrow_mut().as_deref_mut() {
Ok(data @ Some(_)) => {
let handler = data.take();
Expand Down
Loading