Compare commits

...

17 Commits

Author SHA1 Message Date
Francesca Frangipane
282770f11a Release winit 0.15.0 (#530) 2018-05-22 14:17:41 -04:00
Francesca Frangipane
17373a4e91 X11: Fix primary monitor fallback regression (#532) 2018-05-22 09:07:46 -04:00
Francesca Frangipane
a34147b602 macOS: Fix keyboard regressions (#533)
* Emit `ReceivedCharacter` for key repeats
* Enter emits `\r` instead of `\n`
2018-05-22 09:05:33 -04:00
Francesca Frangipane
cebd15bfd1 X11: Improve hint support (#529)
Fixes #257
2018-05-20 10:47:22 -04:00
Francesca Frangipane
f51f7c0ca8 Add option to make window "always on top" (#528)
* macOS: always_on_top

* Windows: always_on_top

* X11: always_on_top

* Stub set_always_on_top on other platforms
2018-05-20 10:24:05 -04:00
Francesca Frangipane
f6d26df64d Windows: CursorState improvements (#526)
* Windows: CursorState improvements

Fixes #523

Prior to changing the cursor state, we now check the current grab
state, since it can be invalidated by alt-tabbing and other things.

`CursorState::Hide` is also implemented now.

The cursor name is now wrapped in a `Cursor` struct to allow
multithreaded access.

`Window::set_cursor_state` has been reworked to use
`execute_in_thread`. Two unneeded `transmute` calls were also
removed.

The `WM_SETCURSOR` handler is much more readable now.

`MonitorId::get_adapter_name` has been removed, since it's dead
code and appears to be a relic from 4 years ago.

* Windows: CursorState::Grab no longer hides cursor

`MouseCursor::NoneCursor` has been implemented to allow for
equivalent behavior to the older implementation.

Windows and X11 now have consistent cursor grabbing behavior.
macOS still needs to be updated.

* Windows: Grabbing auto-hides again (for now)

This API needs more work, so let's stick to a bug fix and some
refactoring. However, it now hides using a different technique
than it did originally, which applies instantly instead of after
mouse movement.
2018-05-19 12:02:57 -04:00
Francesca Frangipane
fddfb2e2d6 Windows: Fix detection of Pause and Scroll keys (#525)
Fixes #524
2018-05-18 18:48:19 -04:00
Francesca Frangipane
dec728cfa2 macOS: Implement NSTextInputClient (#518)
Fixes #263
2018-05-17 21:28:30 -04:00
Francesca Frangipane
8440091a4e macOS: Implement with_resize_increments (#519)
Fixes #135
2018-05-16 10:16:36 -04:00
Francesca Frangipane
2464a135b3 macOS: Fix Window::get_current_monitor (#521)
* macOS: Implement MonitorId::get_position

* macOS: Fix Window::get_current_monitor
2018-05-16 09:41:45 -04:00
Francesca Frangipane
87fa120ebb macOS: Fix re-enabling decorations after the window is built without them (#520)
Fixes #517
2018-05-16 08:51:56 -04:00
Francesca Frangipane
d86f53a02c X11: Fix get_current_monitor (#515)
* X11: Fix get_current_monitor

Fixes #64

* impl Debug for MonitorId on all platforms
2018-05-14 08:14:57 -04:00
Francesca Frangipane
15a4fec3d9 X11: Fix scroll wheel delta on i3/etc. (#514)
Fixes #447
2018-05-13 08:44:23 -04:00
OJ Kwon
1819be1173 fix(mac_platform): forward keyevent to system (#511)
* fix(mac_platform): forward keyevent to system

* doc(changelog): update changelog
2018-05-12 22:10:57 -04:00
Victor Berger
ffa9b51d27 wayland: improve diagnostic of failed init (#512) 2018-05-12 07:58:11 -04:00
tinaun
b4a8c08f43 compile with icon_loading feature on docs.rs (#509)
* compile with icon_loading feature on docs.rs

these functions should be more visible now.

* Explicitly document which functions require icon_loading
2018-05-11 10:33:06 -04:00
Francesca Frangipane
741bcc4672 Correct privacy for Icon::to_cardinals (#510) 2018-05-10 18:42:41 -04:00
35 changed files with 1831 additions and 512 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
Cargo.lock
target/
rls/
*~
#*#

View File

@@ -1,5 +1,31 @@
# Unreleased
# Version 0.15.0 (2018-05-22)
- `Icon::to_cardinals` is no longer public, since it was never supposed to be.
- Wayland: improve diagnostics if initialization fails
- Fix some system event key doesn't work when focused, do not block keyevent forward to system on macOS
- On X11, the scroll wheel position is now correctly reset on i3 and other WMs that have the same quirk.
- On X11, `Window::get_current_monitor` now reliably returns the correct monitor.
- On X11, `Window::hidpi_factor` returns values from XRandR rather than the inaccurate values previously queried from the core protocol.
- On X11, the primary monitor is detected correctly even when using versions of XRandR less than 1.5.
- `MonitorId` now implements `Debug`.
- Fixed bug on macOS where using `with_decorations(false)` would cause `set_decorations(true)` to produce a transparent titlebar with no title.
- Implemented `MonitorId::get_position` on macOS.
- On macOS, `Window::get_current_monitor` now returns accurate values.
- Added `WindowBuilderExt::with_resize_increments` to macOS.
- **Breaking:** On X11, `WindowBuilderExt::with_resize_increments` and `WindowBuilderExt::with_base_size` now take `u32` values rather than `i32`.
- macOS keyboard handling has been overhauled, allowing for the use of dead keys, IME, etc. Right modifier keys are also no longer reported as being left.
- Added the `Window::set_ime_spot(x: i32, y: i32)` method, which is implemented on X11 and macOS.
- **Breaking**: `os::unix::WindowExt::send_xim_spot(x: i16, y: i16)` no longer exists. Switch to the new `Window::set_ime_spot(x: i32, y: i32)`, which has equivalent functionality.
- Fixed detection of `Pause` and `Scroll` keys on Windows.
- On Windows, alt-tabbing while the cursor is grabbed no longer makes it impossible to re-grab the cursor.
- On Windows, using `CursorState::Hide` when the cursor is grabbed now ungrabs the cursor first.
- Implemented `MouseCursor::NoneCursor` on Windows.
- Added `WindowBuilder::with_always_on_top` and `Window::set_always_on_top`. Implemented on Windows, macOS, and X11.
- On X11, `WindowBuilderExt` now has `with_class`, `with_override_redirect`, and `with_x11_window_type` to allow for more control over window creation. `WindowExt` additionally has `set_urgent`.
- More hints are set by default on X11, including `_NET_WM_PID` and `WM_CLIENT_MACHINE`. Note that prior to this, the `WM_CLASS` hint was automatically set to whatever value was passed to `with_title`. It's now set to the executable name to better conform to expectations and the specification; if this is undesirable, you must explicitly use `WindowBuilderExt::with_class`.
# Version 0.14.0 (2018-05-09)
- Created the `Copy`, `Paste` and `Cut` `VirtualKeyCode`s and added support for them on X11 and Wayland

View File

@@ -1,6 +1,6 @@
[package]
name = "winit"
version = "0.14.0"
version = "0.15.0"
authors = ["The winit contributors, Pierre Krieger <pierre.krieger1708@gmail.com>"]
description = "Cross-platform window creation library."
keywords = ["windowing"]
@@ -10,6 +10,9 @@ repository = "https://github.com/tomaka/winit"
documentation = "https://docs.rs/winit"
categories = ["gui"]
[package.metadata.docs.rs]
features = ["icon_loading"]
[features]
icon_loading = ["image"]
@@ -26,9 +29,9 @@ objc = "0.2"
[target.'cfg(target_os = "macos")'.dependencies]
objc = "0.2"
cocoa = "0.14"
core-foundation = "0.5"
core-graphics = "0.13"
cocoa = "0.15"
core-foundation = "0.6"
core-graphics = "0.14"
[target.'cfg(target_os = "windows")'.dependencies.winapi]
version = "0.3"
@@ -48,7 +51,7 @@ features = [
]
[target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))'.dependencies]
wayland-client = { version = "0.20.4", features = [ "dlopen", "egl", "cursor"] }
wayland-client = { version = "0.20.6", features = [ "dlopen", "egl", "cursor"] }
smithay-client-toolkit = "0.2.1"
x11-dl = "2.17.5"
parking_lot = "0.5"

View File

@@ -9,7 +9,7 @@
```toml
[dependencies]
winit = "0.14"
winit = "0.15"
```
## [Documentation](https://docs.rs/winit)

View File

@@ -104,12 +104,16 @@ impl Icon {
#[cfg(feature = "icon_loading")]
/// Loads an `Icon` from the path of an image on the filesystem.
///
/// Requires the `icon_loading` feature.
pub fn from_path<P: AsRef<Path>>(path: P) -> image::ImageResult<Self> {
image::open(path).map(Into::into)
}
#[cfg(feature = "icon_loading")]
/// Loads an `Icon` from anything implementing `BufRead` and `Seek`.
///
/// Requires the `icon_loading` feature.
pub fn from_reader<R: BufRead + Seek>(
reader: R,
format: image::ImageFormat,
@@ -120,12 +124,16 @@ impl Icon {
#[cfg(feature = "icon_loading")]
/// Loads an `Icon` from the unprocessed bytes of an image file.
/// Uses heuristics to determine format.
///
/// Requires the `icon_loading` feature.
pub fn from_bytes(bytes: &[u8]) -> image::ImageResult<Self> {
image::load_from_memory(bytes).map(Into::into)
}
#[cfg(feature = "icon_loading")]
/// Loads an `Icon` from the unprocessed bytes of an image.
///
/// Requires the `icon_loading` feature.
pub fn from_bytes_with_format(
bytes: &[u8],
format: image::ImageFormat,
@@ -135,6 +143,7 @@ impl Icon {
}
#[cfg(feature = "icon_loading")]
/// Requires the `icon_loading` feature.
impl From<image::DynamicImage> for Icon {
fn from(image: image::DynamicImage) -> Self {
use image::{GenericImage, Pixel};
@@ -148,6 +157,7 @@ impl From<image::DynamicImage> for Icon {
}
#[cfg(feature = "icon_loading")]
/// Requires the `icon_loading` feature.
impl From<image::RgbaImage> for Icon {
fn from(buf: image::RgbaImage) -> Self {
let (width, height) = buf.dimensions();

View File

@@ -15,7 +15,7 @@
//! - Calling `Window::new(&events_loop)`.
//! - Calling `let builder = WindowBuilder::new()` then `builder.build(&events_loop)`.
//!
//! The first way is the simpliest way and will give you default values for everything.
//! The first way is the simplest way and will give you default values for everything.
//!
//! The second way allows you to customize the way your window will look and behave by modifying
//! the fields of the `WindowBuilder` object before you create the window.
@@ -80,7 +80,6 @@
//! to create an OpenGL/Vulkan/DirectX/Metal/etc. context that will draw on the window.
//!
#[cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd", target_os = "windows"))]
#[macro_use]
extern crate lazy_static;
extern crate libc;
@@ -442,6 +441,11 @@ pub struct WindowAttributes {
/// The default is `true`.
pub decorations: bool,
/// Whether the window should always be on top of other windows.
///
/// The default is `false`.
pub always_on_top: bool,
/// The window icon.
///
/// The default is `None`.
@@ -465,6 +469,7 @@ impl Default for WindowAttributes {
visible: true,
transparent: false,
decorations: true,
always_on_top: false,
window_icon: None,
multitouch: false,
}

View File

@@ -71,64 +71,72 @@ impl From<ActivationPolicy> for NSApplicationActivationPolicy {
/// - `with_titlebar_buttons_hidden`
/// - `with_fullsize_content_view`
pub trait WindowBuilderExt {
/// Sets the activation policy for the window being built.
fn with_activation_policy(self, activation_policy: ActivationPolicy) -> WindowBuilder;
/// Enables click-and-drag behavior for the entire window, not just the titlebar.
fn with_movable_by_window_background(self, movable_by_window_background: bool) -> WindowBuilder;
/// Makes the titlebar transparent and allows the content to appear behind it.
fn with_titlebar_transparent(self, titlebar_transparent: bool) -> WindowBuilder;
/// Hides the window title.
fn with_title_hidden(self, title_hidden: bool) -> WindowBuilder;
/// Hides the window titlebar.
fn with_titlebar_hidden(self, titlebar_hidden: bool) -> WindowBuilder;
/// Hides the window titlebar buttons.
fn with_titlebar_buttons_hidden(self, titlebar_buttons_hidden: bool) -> WindowBuilder;
/// Makes the window content appear behind the titlebar.
fn with_fullsize_content_view(self, fullsize_content_view: bool) -> WindowBuilder;
/// Build window with `resizeIncrements` property. Values must not be 0.
fn with_resize_increments(self, width_inc: u32, height_inc: u32) -> WindowBuilder;
}
impl WindowBuilderExt for WindowBuilder {
/// Sets the activation policy for the window being built
#[inline]
fn with_activation_policy(mut self, activation_policy: ActivationPolicy) -> WindowBuilder {
self.platform_specific.activation_policy = activation_policy;
self
}
/// Enables click-and-drag behavior for the entire window, not just the titlebar
#[inline]
fn with_movable_by_window_background(mut self, movable_by_window_background: bool) -> WindowBuilder {
self.platform_specific.movable_by_window_background = movable_by_window_background;
self
}
/// Makes the titlebar transparent and allows the content to appear behind it
#[inline]
fn with_titlebar_transparent(mut self, titlebar_transparent: bool) -> WindowBuilder {
self.platform_specific.titlebar_transparent = titlebar_transparent;
self
}
/// Hides the window titlebar
#[inline]
fn with_titlebar_hidden(mut self, titlebar_hidden: bool) -> WindowBuilder {
self.platform_specific.titlebar_hidden = titlebar_hidden;
self
}
/// Hides the window titlebar buttons
#[inline]
fn with_titlebar_buttons_hidden(mut self, titlebar_buttons_hidden: bool) -> WindowBuilder {
self.platform_specific.titlebar_buttons_hidden = titlebar_buttons_hidden;
self
}
/// Hides the window title
#[inline]
fn with_title_hidden(mut self, title_hidden: bool) -> WindowBuilder {
self.platform_specific.title_hidden = title_hidden;
self
}
/// Makes the window content appear behind the titlebar
#[inline]
fn with_fullsize_content_view(mut self, fullsize_content_view: bool) -> WindowBuilder {
self.platform_specific.fullsize_content_view = fullsize_content_view;
self
}
#[inline]
fn with_resize_increments(mut self, width_inc: u32, height_inc: u32) -> WindowBuilder {
self.platform_specific.resize_increments = Some((width_inc, height_inc));
self
}
}
/// Additional methods on `MonitorId` that are specific to MacOS.

View File

@@ -17,6 +17,7 @@ use platform::x11::ffi::XVisualInfo;
pub use platform::x11;
pub use platform::XNotSupported;
pub use platform::x11::util::WindowType as XWindowType;
/// Additional methods on `EventsLoop` that are specific to Linux.
pub trait EventsLoopExt {
@@ -94,7 +95,8 @@ pub trait WindowExt {
fn get_xlib_xconnection(&self) -> Option<Arc<XConnection>>;
fn send_xim_spot(&self, x: i16, y: i16);
/// Set window urgency hint (`XUrgencyHint`). Only relevant on X.
fn set_urgent(&self, is_urgent: bool);
/// This function returns the underlying `xcb_connection_t` of an xlib `Display`.
///
@@ -144,6 +146,7 @@ impl WindowExt for Window {
}
}
#[inline]
fn get_xlib_screen_id(&self) -> Option<raw::c_int> {
match self.window {
LinuxWindow::X(ref w) => Some(w.get_xlib_screen_id()),
@@ -151,6 +154,7 @@ impl WindowExt for Window {
}
}
#[inline]
fn get_xlib_xconnection(&self) -> Option<Arc<XConnection>> {
match self.window {
LinuxWindow::X(ref w) => Some(w.get_xlib_xconnection()),
@@ -158,6 +162,7 @@ impl WindowExt for Window {
}
}
#[inline]
fn get_xcb_connection(&self) -> Option<*mut raw::c_void> {
match self.window {
LinuxWindow::X(ref w) => Some(w.get_xcb_connection()),
@@ -165,9 +170,10 @@ impl WindowExt for Window {
}
}
fn send_xim_spot(&self, x: i16, y: i16) {
#[inline]
fn set_urgent(&self, is_urgent: bool) {
if let LinuxWindow::X(ref w) = self.window {
w.send_xim_spot(x, y);
w.set_urgent(is_urgent);
}
}
@@ -198,10 +204,16 @@ pub trait WindowBuilderExt {
fn with_x11_visual<T>(self, visual_infos: *const T) -> WindowBuilder;
fn with_x11_screen(self, screen_id: i32) -> WindowBuilder;
/// Build window with `WM_CLASS` hint; defaults to the name of the binary. Only relevant on X11.
fn with_class(self, class: String, instance: String) -> WindowBuilder;
/// Build window with override-redirect flag; defaults to false. Only relevant on X11.
fn with_override_redirect(self, override_redirect: bool) -> WindowBuilder;
/// Build window with `_NET_WM_WINDOW_TYPE` hint; defaults to `Normal`. Only relevant on X11.
fn with_x11_window_type(self, x11_window_type: XWindowType) -> WindowBuilder;
/// Build window with resize increment hint. Only implemented on X11.
fn with_resize_increments(self, width_inc: i32, height_inc: i32) -> WindowBuilder;
fn with_resize_increments(self, width_inc: u32, height_inc: u32) -> WindowBuilder;
/// Build window with base size hint. Only implemented on X11.
fn with_base_size(self, base_width: i32, base_height: i32) -> WindowBuilder;
fn with_base_size(self, base_width: u32, base_height: u32) -> WindowBuilder;
}
impl WindowBuilderExt for WindowBuilder {
@@ -220,13 +232,31 @@ impl WindowBuilderExt for WindowBuilder {
}
#[inline]
fn with_resize_increments(mut self, width_inc: i32, height_inc: i32) -> WindowBuilder {
fn with_class(mut self, instance: String, class: String) -> WindowBuilder {
self.platform_specific.class = Some((instance, class));
self
}
#[inline]
fn with_override_redirect(mut self, override_redirect: bool) -> WindowBuilder {
self.platform_specific.override_redirect = override_redirect;
self
}
#[inline]
fn with_x11_window_type(mut self, x11_window_type: XWindowType) -> WindowBuilder {
self.platform_specific.x11_window_type = x11_window_type;
self
}
#[inline]
fn with_resize_increments(mut self, width_inc: u32, height_inc: u32) -> WindowBuilder {
self.platform_specific.resize_increments = Some((width_inc, height_inc));
self
}
#[inline]
fn with_base_size(mut self, base_width: i32, base_height: i32) -> WindowBuilder {
fn with_base_size(mut self, base_width: u32, base_height: u32) -> WindowBuilder {
self.platform_specific.base_size = Some((base_width, base_height));
self
}

View File

@@ -164,7 +164,7 @@ pub struct Window {
native_window: *const c_void,
}
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct MonitorId;
mod ffi;
@@ -323,11 +323,21 @@ impl Window {
// N/A
}
#[inline]
pub fn set_always_on_top(&self, _always_on_top: bool) {
// N/A
}
#[inline]
pub fn set_window_icon(&self, _icon: Option<::Icon>) {
// N/A
}
#[inline]
pub fn set_ime_spot(&self, _x: i32, _y: i32) {
// N/A
}
#[inline]
pub fn get_current_monitor(&self) -> RootMonitorId {
RootMonitorId{inner: MonitorId}

View File

@@ -24,7 +24,7 @@ pub struct DeviceId;
#[derive(Clone, Default)]
pub struct PlatformSpecificHeadlessBuilderAttributes;
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct MonitorId;
impl MonitorId {
@@ -543,11 +543,21 @@ impl Window {
// N/A
}
#[inline]
pub fn set_always_on_top(&self, _always_on_top: bool) {
// N/A
}
#[inline]
pub fn set_window_icon(&self, _icon: Option<::Icon>) {
// N/A
}
#[inline]
pub fn set_ime_spot(&self, _x: i32, _y: i32) {
// N/A
}
#[inline]
pub fn get_current_monitor(&self) -> ::MonitorId {
::MonitorId{inner: MonitorId}

View File

@@ -96,7 +96,7 @@ use self::ffi::{
static mut jmpbuf: [c_int;27] = [0;27];
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct MonitorId;
pub struct Window {
@@ -370,11 +370,21 @@ impl Window {
// N/A
}
#[inline]
pub fn set_always_on_top(&self, _always_on_top: bool) {
// N/A
}
#[inline]
pub fn set_window_icon(&self, _icon: Option<::Icon>) {
// N/A
}
#[inline]
pub fn set_ime_spot(&self, _x: i32, _y: i32) {
// N/A
}
#[inline]
pub fn get_current_monitor(&self) -> RootMonitorId {
RootMonitorId{inner: MonitorId}

View File

@@ -6,6 +6,8 @@ use std::ffi::CStr;
use std::os::raw::*;
use std::sync::Arc;
use sctk::reexports::client::ConnectError;
// `std::os::raw::c_void` and `libc::c_void` are NOT interchangeable!
use libc;
@@ -40,8 +42,11 @@ const BACKEND_PREFERENCE_ENV_VAR: &str = "WINIT_UNIX_BACKEND";
pub struct PlatformSpecificWindowBuilderAttributes {
pub visual_infos: Option<XVisualInfo>,
pub screen_id: Option<i32>,
pub resize_increments: Option<(i32, i32)>,
pub base_size: Option<(i32, i32)>,
pub resize_increments: Option<(u32, u32)>,
pub base_size: Option<(u32, u32)>,
pub class: Option<(String, String)>,
pub override_redirect: bool,
pub x11_window_type: x11::util::WindowType,
}
lazy_static!(
@@ -67,7 +72,7 @@ pub enum DeviceId {
Wayland(wayland::DeviceId)
}
#[derive(Clone)]
#[derive(Debug, Clone)]
pub enum MonitorId {
X(x11::MonitorId),
Wayland(wayland::MonitorId),
@@ -300,6 +305,14 @@ impl Window {
}
}
#[inline]
pub fn set_always_on_top(&self, always_on_top: bool) {
match self {
&Window::X(ref w) => w.set_always_on_top(always_on_top),
&Window::Wayland(_) => (),
}
}
#[inline]
pub fn set_window_icon(&self, window_icon: Option<Icon>) {
match self {
@@ -308,6 +321,14 @@ impl Window {
}
}
#[inline]
pub fn set_ime_spot(&self, x: i32, y: i32) {
match self {
&Window::X(ref w) => w.send_xim_spot(x as i16, y as i16),
&Window::Wayland(_) => (),
}
}
#[inline]
pub fn get_current_monitor(&self) -> RootMonitorId {
match self {
@@ -338,6 +359,8 @@ unsafe extern "C" fn x_error_callback(
minor_code: (*event).minor_code,
};
eprintln!("[winit X11 error] {:#?}", error);
*xconn.latest_error.lock() = Some(error);
}
// Fun fact: this return value is completely ignored.
@@ -395,10 +418,9 @@ r#"Failed to initialize any backend!
panic!(err_string);
}
pub fn new_wayland() -> Result<EventsLoop, ()> {
pub fn new_wayland() -> Result<EventsLoop, ConnectError> {
wayland::EventsLoop::new()
.map(EventsLoop::Wayland)
.ok_or(())
}
pub fn new_x11() -> Result<EventsLoop, XNotSupported> {

View File

@@ -1,5 +1,6 @@
use std::cell::RefCell;
use std::collections::VecDeque;
use std::fmt;
use std::sync::{Arc, Mutex, Weak};
use std::sync::atomic::{AtomicBool, Ordering};
@@ -10,7 +11,7 @@ use super::window::WindowStore;
use sctk::Environment;
use sctk::output::OutputMgr;
use sctk::reexports::client::{Display, EventQueue, GlobalEvent, Proxy};
use sctk::reexports::client::{Display, EventQueue, GlobalEvent, Proxy, ConnectError};
use sctk::reexports::client::commons::Implementation;
use sctk::reexports::client::protocol::{wl_keyboard, wl_output, wl_pointer, wl_registry, wl_seat,
wl_touch};
@@ -100,11 +101,8 @@ impl EventsLoopProxy {
}
impl EventsLoop {
pub fn new() -> Option<EventsLoop> {
let (display, mut event_queue) = match Display::connect_to_env() {
Ok(ret) => ret,
Err(_) => return None,
};
pub fn new() -> Result<EventsLoop, ConnectError> {
let (display, mut event_queue) = Display::connect_to_env()?;
let sink = Arc::new(Mutex::new(EventsLoopSink::new()));
let store = Arc::new(Mutex::new(WindowStore::new()));
@@ -120,7 +118,7 @@ impl EventsLoop {
},
).unwrap();
Some(EventsLoop {
Ok(EventsLoop {
display: Arc::new(display),
evq: RefCell::new(event_queue),
sink: sink,
@@ -430,6 +428,29 @@ impl Clone for MonitorId {
}
}
impl fmt::Debug for MonitorId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
#[derive(Debug)]
struct MonitorId {
name: Option<String>,
native_identifier: u32,
dimensions: (u32, u32),
position: (i32, i32),
hidpi_factor: f32,
}
let monitor_id_proxy = MonitorId {
name: self.get_name(),
native_identifier: self.get_native_identifier(),
dimensions: self.get_dimensions(),
position: self.get_position(),
hidpi_factor: self.get_hidpi_factor(),
};
monitor_id_proxy.fmt(f)
}
}
impl MonitorId {
pub fn get_name(&self) -> Option<String> {
self.mgr.with_info(&self.proxy, |_, info| {

View File

@@ -7,9 +7,14 @@ mod window;
mod xdisplay;
mod dnd;
mod ime;
mod util;
pub mod util;
pub use self::monitor::{MonitorId, get_available_monitors, get_primary_monitor};
pub use self::monitor::{
MonitorId,
get_available_monitors,
get_monitor_for_window,
get_primary_monitor,
};
pub use self::window::{Window2, XWindow};
pub use self::xdisplay::{XConnection, XNotSupported, XError};
@@ -46,6 +51,7 @@ pub struct EventsLoop {
ime_receiver: ImeReceiver,
ime_sender: ImeSender,
ime: RefCell<Ime>,
randr_event_offset: c_int,
windows: Arc<Mutex<HashMap<WindowId, WindowData>>>,
// Please don't laugh at this type signature
shared_state: RefCell<HashMap<WindowId, Weak<Mutex<window::SharedState>>>>,
@@ -67,6 +73,8 @@ pub struct EventsLoopProxy {
impl EventsLoop {
pub fn new(display: Arc<XConnection>) -> EventsLoop {
let root = unsafe { (display.xlib.XDefaultRootWindow)(display.display) };
let wm_delete_window = unsafe { util::get_atom(&display, b"WM_DELETE_WINDOW\0") }
.expect("Failed to call XInternAtom (WM_DELETE_WINDOW)");
@@ -85,6 +93,9 @@ impl EventsLoop {
result.expect("Failed to set input method destruction callback")
});
let randr_event_offset = monitor::select_input(&display, root)
.expect("Failed to query XRandR extension");
let xi2ext = unsafe {
let mut result = XExtension {
opcode: mem::uninitialized(),
@@ -119,7 +130,6 @@ impl EventsLoop {
}
}
let root = unsafe { (display.xlib.XDefaultRootWindow)(display.display) };
util::update_cached_wm_info(&display, root);
let wakeup_dummy_window = unsafe {
@@ -146,6 +156,7 @@ impl EventsLoop {
ime_receiver,
ime_sender,
ime,
randr_event_offset,
windows: Arc::new(Mutex::new(HashMap::new())),
shared_state: RefCell::new(HashMap::new()),
devices: RefCell::new(HashMap::new()),
@@ -804,15 +815,19 @@ impl EventsLoop {
let window_id = mkwid(xev.event);
let device_id = mkdid(xev.deviceid);
let mut devices = self.devices.borrow_mut();
let physical_device = match devices.get_mut(&DeviceId(xev.sourceid)) {
Some(device) => device,
None => return,
};
if let Some(all_info) = DeviceInfo::get(&self.display, ffi::XIAllDevices) {
let mut devices = self.devices.borrow_mut();
for device_info in all_info.iter() {
if device_info.deviceid == xev.sourceid {
physical_device.reset_scroll_position(device_info);
if device_info.deviceid == xev.sourceid
// This is needed for resetting to work correctly on i3, and
// presumably some other WMs. On those, `XI_Enter` doesn't include
// the physical device ID, so both `sourceid` and `deviceid` are
// the virtual device.
|| device_info.attachment == xev.sourceid {
let device_id = DeviceId(device_info.deviceid);
if let Some(device) = devices.get_mut(&device_id) {
device.reset_scroll_position(device_info);
}
}
}
}
@@ -1037,7 +1052,12 @@ impl EventsLoop {
_ => {}
}
},
_ => (),
_ => {
if event_type == self.randr_event_offset {
// In the future, it would be quite easy to emit monitor hotplug events.
monitor::invalidate_cached_monitor_list();
}
},
}
match self.ime_receiver.try_recv() {

View File

@@ -1,9 +1,46 @@
use std::os::raw::*;
use std::sync::Arc;
use std::slice;
use super::XConnection;
use parking_lot::Mutex;
#[derive(Clone)]
use super::ffi::{
RRCrtcChangeNotifyMask,
RROutputPropertyNotifyMask,
RRScreenChangeNotifyMask,
True,
Window,
XRRScreenResources,
};
use super::{util, XConnection, XError};
// Used to test XRandR < 1.5 code path. This should always be committed as false.
const FORCE_RANDR_COMPAT: bool = false;
// Also used for testing. This should always be committed as false.
const DISABLE_MONITOR_LIST_CACHING: bool = false;
lazy_static! {
static ref XRANDR_VERSION: Mutex<Option<(c_int, c_int)>> = Mutex::default();
static ref MONITORS: Mutex<Option<Vec<MonitorId>>> = Mutex::default();
}
fn version_is_at_least(major: c_int, minor: c_int) -> bool {
if let Some((avail_major, avail_minor)) = *XRANDR_VERSION.lock() {
if avail_major == major {
avail_minor >= minor
} else {
avail_major > major
}
} else {
unreachable!();
}
}
pub fn invalidate_cached_monitor_list() -> Option<Vec<MonitorId>> {
// We update this lazily.
(*MONITORS.lock()).take()
}
#[derive(Debug, Clone)]
pub struct MonitorId {
/// The actual id
id: u32,
@@ -15,96 +52,34 @@ pub struct MonitorId {
position: (i32, i32),
/// If the monitor is the primary one
primary: bool,
/// The DPI scaling factor
hidpi_factor: f32,
}
pub fn get_available_monitors(x: &Arc<XConnection>) -> Vec<MonitorId> {
let mut available = Vec::new();
unsafe {
let root = (x.xlib.XDefaultRootWindow)(x.display);
let resources = (x.xrandr.XRRGetScreenResources)(x.display, root);
if let Some(ref xrandr_1_5) = x.xrandr_1_5 {
// We're in XRandR >= 1.5, enumerate Monitors to handle things like MST and videowalls
let mut nmonitors = 0;
let monitors = (xrandr_1_5.XRRGetMonitors)(x.display, root, 1, &mut nmonitors);
for i in 0..nmonitors {
let monitor = *(monitors.offset(i as isize));
let output = (xrandr_1_5.XRRGetOutputInfo)(x.display, resources, *(monitor.outputs.offset(0)));
let nameslice = slice::from_raw_parts((*output).name as *mut u8, (*output).nameLen as usize);
let name = String::from_utf8_lossy(nameslice).into_owned();
let hidpi_factor = {
let x_mm = (*output).mm_width as f32;
let y_mm = (*output).mm_height as f32;
let x_px = monitor.width as f32;
let y_px = monitor.height as f32;
let ppmm = ((x_px * y_px) / (x_mm * y_mm)).sqrt();
// Quantize 1/12 step size
((ppmm * (12.0 * 25.4 / 96.0)).round() / 12.0).max(1.0)
};
(xrandr_1_5.XRRFreeOutputInfo)(output);
available.push(MonitorId{
id: i as u32,
name,
hidpi_factor,
dimensions: (monitor.width as u32, monitor.height as u32),
position: (monitor.x as i32, monitor.y as i32),
primary: (monitor.primary != 0),
});
}
(xrandr_1_5.XRRFreeMonitors)(monitors);
} else {
// We're in XRandR < 1.5, enumerate CRTCs. Everything will work but MST and
// videowall setups will show more monitors than the logical groups the user
// cares about
for i in 0..(*resources).ncrtc {
let crtcid = *((*resources).crtcs.offset(i as isize));
let crtc = (x.xrandr.XRRGetCrtcInfo)(x.display, resources, crtcid);
if (*crtc).width > 0 && (*crtc).height > 0 && (*crtc).noutput > 0 {
let output = (x.xrandr.XRRGetOutputInfo)(x.display, resources, *((*crtc).outputs.offset(0)));
let nameslice = slice::from_raw_parts((*output).name as *mut u8, (*output).nameLen as usize);
let name = String::from_utf8_lossy(nameslice).into_owned();
let hidpi_factor = {
let x_mm = (*output).mm_width as f32;
let y_mm = (*output).mm_height as f32;
let x_px = (*crtc).width as f32;
let y_px = (*crtc).height as f32;
let ppmm = ((x_px * y_px) / (x_mm * y_mm)).sqrt();
// Quantize 1/12 step size
((ppmm * (12.0 * 25.4 / 96.0)).round() / 12.0).max(1.0)
};
(x.xrandr.XRRFreeOutputInfo)(output);
available.push(MonitorId{
id: crtcid as u32,
name,
hidpi_factor,
dimensions: ((*crtc).width as u32, (*crtc).height as u32),
position: ((*crtc).x as i32, (*crtc).y as i32),
primary: true,
});
}
(x.xrandr.XRRFreeCrtcInfo)(crtc);
}
}
(x.xrandr.XRRFreeScreenResources)(resources);
}
available
}
#[inline]
pub fn get_primary_monitor(x: &Arc<XConnection>) -> MonitorId {
get_available_monitors(x).into_iter().find(|m| m.primary)
// 'no primary' case is better handled picking some existing monitor
.or_else(|| get_available_monitors(x).into_iter().next())
.expect("[winit] Failed to find any x11 monitor")
/// The DPI scale factor
pub(crate) hidpi_factor: f32,
/// Used to determine which windows are on this monitor
pub(crate) rect: util::Rect,
}
impl MonitorId {
fn from_repr(
xconn: &Arc<XConnection>,
resources: *mut XRRScreenResources,
id: u32,
repr: util::MonitorRepr,
primary: bool,
) -> Self {
let (name, hidpi_factor) = unsafe { util::get_output_info(xconn, resources, &repr) };
let (dimensions, position) = unsafe { (repr.get_dimensions(), repr.get_position()) };
let rect = util::Rect::new(position, dimensions);
MonitorId {
id,
name,
hidpi_factor,
dimensions,
position,
primary,
rect,
}
}
pub fn get_name(&self) -> Option<String> {
Some(self.name.clone())
}
@@ -127,3 +102,169 @@ impl MonitorId {
self.hidpi_factor
}
}
pub fn get_monitor_for_window(
xconn: &Arc<XConnection>,
window_rect: Option<util::Rect>,
) -> MonitorId {
let monitors = get_available_monitors(xconn);
let default = monitors
.get(0)
.expect("[winit] Failed to find any monitors using XRandR.");
let window_rect = match window_rect {
Some(rect) => rect,
None => return default.to_owned(),
};
let mut largest_overlap = 0;
let mut matched_monitor = default;
for monitor in &monitors {
let overlapping_area = window_rect.get_overlapping_area(&monitor.rect);
if overlapping_area > largest_overlap {
largest_overlap = overlapping_area;
matched_monitor = &monitor;
}
}
matched_monitor.to_owned()
}
fn query_monitor_list(xconn: &Arc<XConnection>) -> Vec<MonitorId> {
unsafe {
let root = (xconn.xlib.XDefaultRootWindow)(xconn.display);
// WARNING: this function is supposedly very slow, on the order of hundreds of ms.
// Upon failure, `resources` will be null.
let resources = (xconn.xrandr.XRRGetScreenResources)(xconn.display, root);
if resources.is_null() {
panic!("[winit] `XRRGetScreenResources` returned NULL. That should only happen if the root window doesn't exist.");
}
let mut available;
let mut has_primary = false;
if xconn.xrandr_1_5.is_some() && version_is_at_least(1, 5) && !FORCE_RANDR_COMPAT {
// We're in XRandR >= 1.5, enumerate monitors. This supports things like MST and
// videowalls.
let xrandr_1_5 = xconn.xrandr_1_5.as_ref().unwrap();
let mut monitor_count = 0;
let monitors = (xrandr_1_5.XRRGetMonitors)(xconn.display, root, 1, &mut monitor_count);
assert!(monitor_count >= 0);
available = Vec::with_capacity(monitor_count as usize);
for monitor_index in 0..monitor_count {
let monitor = monitors.offset(monitor_index as isize);
let is_primary = (*monitor).primary != 0;
has_primary |= is_primary;
available.push(MonitorId::from_repr(
xconn,
resources,
monitor_index as u32,
monitor.into(),
is_primary,
));
}
(xrandr_1_5.XRRFreeMonitors)(monitors);
} else {
// We're in XRandR < 1.5, enumerate CRTCs. Everything will work except MST and
// videowall setups will also show monitors that aren't in the logical groups the user
// cares about.
let primary = (xconn.xrandr.XRRGetOutputPrimary)(xconn.display, root);
available = Vec::with_capacity((*resources).ncrtc as usize);
for crtc_index in 0..(*resources).ncrtc {
let crtc_id = *((*resources).crtcs.offset(crtc_index as isize));
let crtc = (xconn.xrandr.XRRGetCrtcInfo)(xconn.display, resources, crtc_id);
let is_active = (*crtc).width > 0 && (*crtc).height > 0 && (*crtc).noutput > 0;
if is_active {
let crtc = util::MonitorRepr::from(crtc);
let is_primary = crtc.get_output() == primary;
has_primary |= is_primary;
available.push(MonitorId::from_repr(
xconn,
resources,
crtc_id as u32,
crtc,
is_primary,
));
}
(xconn.xrandr.XRRFreeCrtcInfo)(crtc);
}
}
// If no monitors were detected as being primary, we just pick one ourselves!
if !has_primary {
if let Some(ref mut fallback) = available.first_mut() {
// Setting this here will come in handy if we ever add an `is_primary` method.
fallback.primary = true;
}
}
(xconn.xrandr.XRRFreeScreenResources)(resources);
available
}
}
pub fn get_available_monitors(xconn: &Arc<XConnection>) -> Vec<MonitorId> {
let mut monitors_lock = MONITORS.lock();
(*monitors_lock)
.as_ref()
.cloned()
.or_else(|| {
let monitors = Some(query_monitor_list(xconn));
if !DISABLE_MONITOR_LIST_CACHING {
(*monitors_lock) = monitors.clone();
}
monitors
})
.unwrap()
}
#[inline]
pub fn get_primary_monitor(xconn: &Arc<XConnection>) -> MonitorId {
get_available_monitors(xconn)
.into_iter()
.find(|monitor| monitor.primary)
.expect("[winit] Failed to find any monitors using XRandR.")
}
pub fn select_input(xconn: &Arc<XConnection>, root: Window) -> Result<c_int, XError> {
{
let mut version_lock = XRANDR_VERSION.lock();
if version_lock.is_none() {
let mut major = 0;
let mut minor = 0;
let has_extension = unsafe {
(xconn.xrandr.XRRQueryVersion)(
xconn.display,
&mut major,
&mut minor,
)
};
if has_extension != True {
panic!("[winit] XRandR extension not available.");
}
*version_lock = Some((major, minor));
}
}
let mut event_offset = 0;
let mut error_offset = 0;
let status = unsafe {
(xconn.xrandr.XRRQueryExtension)(
xconn.display,
&mut event_offset,
&mut error_offset,
)
};
if status != True {
xconn.check_errors()?;
unreachable!("[winit] `XRRQueryExtension` failed but no error was received.");
}
let mask = RRCrtcChangeNotifyMask
| RROutputPropertyNotifyMask
| RRScreenChangeNotifyMask;
unsafe { (xconn.xrandr.XRRSelectInput)(xconn.display, root, mask) };
Ok(event_offset)
}

View File

@@ -1,5 +1,40 @@
use std::cmp;
use super::*;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Rect {
left: i64,
right: i64,
top: i64,
bottom: i64,
}
impl Rect {
pub fn new((x, y): (i32, i32), (width, height): (u32, u32)) -> Self {
let (x, y) = (x as i64, y as i64);
let (width, height) = (width as i64, height as i64);
Rect {
left: x,
right: x + width,
top: y,
bottom: y + height,
}
}
pub fn get_overlapping_area(&self, other: &Self) -> i64 {
let x_overlap = cmp::max(
0,
cmp::min(self.right, other.right) - cmp::max(self.left, other.left),
);
let y_overlap = cmp::max(
0,
cmp::min(self.bottom, other.bottom) - cmp::max(self.top, other.top),
);
x_overlap * y_overlap
}
}
#[derive(Debug)]
pub struct TranslatedCoords {
pub x_rel_root: c_int,

View File

@@ -18,3 +18,51 @@ impl From<bool> for StateOperation {
}
}
}
/// X window type. Maps directly to
/// [`_NET_WM_WINDOW_TYPE`](https://specifications.freedesktop.org/wm-spec/1.3/ar01s05.html).
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum WindowType {
/// A desktop feature. This can include a single window containing desktop icons with the same dimensions as the
/// screen, allowing the desktop environment to have full control of the desktop, without the need for proxying
/// root window clicks.
Desktop,
/// A dock or panel feature. Typically a Window Manager would keep such windows on top of all other windows.
Dock,
/// Toolbar windows. "Torn off" from the main application.
Toolbar,
/// Pinnable menu windows. "Torn off" from the main application.
Menu,
/// A small persistent utility window, such as a palette or toolbox.
Utility,
/// The window is a splash screen displayed as an application is starting up.
Splash,
/// This is a dialog window.
Dialog,
/// This is a normal, top-level window.
Normal,
}
impl Default for WindowType {
fn default() -> Self {
WindowType::Normal
}
}
impl WindowType {
pub(crate) fn as_atom(&self, xconn: &Arc<XConnection>) -> ffi::Atom {
use self::WindowType::*;
let atom_name: &[u8] = match self {
&Desktop => b"_NET_WM_WINDOW_TYPE_DESKTOP\0",
&Dock => b"_NET_WM_WINDOW_TYPE_DOCK\0",
&Toolbar => b"_NET_WM_WINDOW_TYPE_TOOLBAR\0",
&Menu => b"_NET_WM_WINDOW_TYPE_MENU\0",
&Utility => b"_NET_WM_WINDOW_TYPE_UTILITY\0",
&Splash => b"_NET_WM_WINDOW_TYPE_SPLASH\0",
&Dialog => b"_NET_WM_WINDOW_TYPE_DIALOG\0",
&Normal => b"_NET_WM_WINDOW_TYPE_NORMAL\0",
};
unsafe { get_atom(xconn, atom_name) }
.expect("Failed to get atom for `WindowType`")
}
}

View File

@@ -17,7 +17,7 @@ impl Pixel {
}
impl Icon {
pub fn to_cardinals(&self) -> Vec<Cardinal> {
pub(crate) fn to_cardinals(&self) -> Vec<Cardinal> {
assert_eq!(self.rgba.len() % PIXEL_SIZE, 0);
let pixel_count = self.rgba.len() / PIXEL_SIZE;
assert_eq!(pixel_count, (self.width * self.height) as usize);

View File

@@ -6,6 +6,7 @@ mod geometry;
mod hint;
mod icon;
mod input;
mod randr;
mod window_property;
mod wm;
@@ -14,6 +15,7 @@ pub use self::geometry::*;
pub use self::hint::*;
pub use self::icon::*;
pub use self::input::*;
pub use self::randr::*;
pub use self::window_property::*;
pub use self::wm::*;

View File

@@ -0,0 +1,84 @@
use std::slice;
use super::*;
use super::ffi::{
RROutput,
XRRCrtcInfo,
XRRMonitorInfo,
XRRScreenResources,
};
pub enum MonitorRepr {
Monitor(*mut XRRMonitorInfo),
Crtc(*mut XRRCrtcInfo),
}
impl MonitorRepr {
pub unsafe fn get_output(&self) -> RROutput {
match *self {
// Same member names, but different locations within the struct...
MonitorRepr::Monitor(monitor) => *((*monitor).outputs.offset(0)),
MonitorRepr::Crtc(crtc) => *((*crtc).outputs.offset(0)),
}
}
pub unsafe fn get_dimensions(&self) -> (u32, u32) {
match *self {
MonitorRepr::Monitor(monitor) => ((*monitor).width as u32, (*monitor).height as u32),
MonitorRepr::Crtc(crtc) => ((*crtc).width as u32, (*crtc).height as u32),
}
}
pub unsafe fn get_position(&self) -> (i32, i32) {
match *self {
MonitorRepr::Monitor(monitor) => ((*monitor).x as i32, (*monitor).y as i32),
MonitorRepr::Crtc(crtc) => ((*crtc).x as i32, (*crtc).y as i32),
}
}
}
impl From<*mut XRRMonitorInfo> for MonitorRepr {
fn from(monitor: *mut XRRMonitorInfo) -> Self {
MonitorRepr::Monitor(monitor)
}
}
impl From<*mut XRRCrtcInfo> for MonitorRepr {
fn from(crtc: *mut XRRCrtcInfo) -> Self {
MonitorRepr::Crtc(crtc)
}
}
pub fn calc_dpi_factor(
(width_px, height_px): (u32, u32),
(width_mm, height_mm): (u64, u64),
) -> f64 {
let ppmm = (
(width_px as f64 * height_px as f64) / (width_mm as f64 * height_mm as f64)
).sqrt();
// Quantize 1/12 step size
((ppmm * (12.0 * 25.4 / 96.0)).round() / 12.0).max(1.0)
}
pub unsafe fn get_output_info(
xconn: &Arc<XConnection>,
resources: *mut XRRScreenResources,
repr: &MonitorRepr,
) -> (String, f32) {
let output_info = (xconn.xrandr.XRRGetOutputInfo)(
xconn.display,
resources,
repr.get_output(),
);
let name_slice = slice::from_raw_parts(
(*output_info).name as *mut u8,
(*output_info).nameLen as usize,
);
let name = String::from_utf8_lossy(name_slice).into();
let hidpi_factor = calc_dpi_factor(
repr.get_dimensions(),
((*output_info).mm_width as u64, (*output_info).mm_height as u64),
) as f32;
(xconn.xrandr.XRRFreeOutputInfo)(output_info);
(name, hidpi_factor)
}

View File

@@ -1,7 +1,8 @@
use std::{cmp, mem};
use std::{cmp, env, mem};
use std::borrow::Borrow;
use std::ffi::CString;
use std::os::raw::*;
use std::path::Path;
use std::sync::Arc;
use libc;
@@ -12,7 +13,7 @@ use CreationError::{self, OsError};
use platform::MonitorId as PlatformMonitorId;
use platform::PlatformSpecificWindowBuilderAttributes;
use platform::x11::MonitorId as X11MonitorId;
use platform::x11::monitor::get_available_monitors;
use platform::x11::monitor::get_monitor_for_window;
use window::MonitorId as RootMonitorId;
use super::{ffi, util, XConnection, XError, WindowId, EventsLoop};
@@ -115,6 +116,10 @@ impl Window2 {
window_attributes |= ffi::CWBackPixel;
}
if pl_attribs.override_redirect {
window_attributes |= ffi::CWOverrideRedirect;
}
// finally creating the window
let window = unsafe {
(xconn.xlib.XCreateWindow)(
@@ -127,12 +132,12 @@ impl Window2 {
0,
match pl_attribs.visual_infos {
Some(vi) => vi.depth,
None => ffi::CopyFromParent
None => ffi::CopyFromParent,
},
ffi::InputOutput as c_uint,
match pl_attribs.visual_infos {
Some(vi) => vi.visual,
None => ffi::CopyFromParent as *mut _
None => ffi::CopyFromParent as *mut ffi::Visual,
},
window_attributes,
&mut set_win_attr,
@@ -178,17 +183,42 @@ impl Window2 {
)
}.queue();
// Set ICCCM WM_CLASS property based on initial window title
// Must be done *before* mapping the window by ICCCM 4.1.2.5
// WM_CLASS must be set *before* mapping the window, as per ICCCM!
{
let name = CString::new(window_attrs.title.as_str())
.expect("Window title contained null byte");
let mut class_hints = {
let class_hints = unsafe { (xconn.xlib.XAllocClassHint)() };
util::XSmartPointer::new(xconn, class_hints)
}.expect("XAllocClassHint returned null; out of memory");
(*class_hints).res_name = name.as_ptr() as *mut c_char;
(*class_hints).res_class = name.as_ptr() as *mut c_char;
}.expect("`XAllocClassHint` returned null; out of memory");
let (class, instance) = if let Some((instance, class)) = pl_attribs.class {
let instance = CString::new(instance.as_str())
.expect("`WM_CLASS` instance contained null byte");
let class = CString::new(class.as_str())
.expect("`WM_CLASS` class contained null byte");
(instance, class)
} else {
let class = env::args()
.next()
.as_ref()
// Default to the name of the binary (via argv[0])
.and_then(|path| Path::new(path).file_name())
.and_then(|bin_name| bin_name.to_str())
.map(|bin_name| bin_name.to_owned())
.or_else(|| Some(window_attrs.title.clone()))
.and_then(|string| CString::new(string.as_str()).ok())
.expect("Default `WM_CLASS` class contained null byte");
// This environment variable is extraordinarily unlikely to actually be used...
let instance = env::var("RESOURCE_NAME")
.ok()
.and_then(|instance| CString::new(instance.as_str()).ok())
.or_else(|| Some(class.clone()))
.expect("Default `WM_CLASS` instance contained null byte");
(instance, class)
};
(*class_hints).res_name = class.as_ptr() as *mut c_char;
(*class_hints).res_class = instance.as_ptr() as *mut c_char;
unsafe {
(xconn.xlib.XSetClassHint)(
xconn.display,
@@ -198,6 +228,17 @@ impl Window2 {
}//.queue();
}
Window2::set_pid(xconn, x_window.window)
.map(|flusher| flusher.queue());
if pl_attribs.x11_window_type != Default::default() {
Window2::set_window_type(
xconn,
x_window.window,
pl_attribs.x11_window_type,
).queue();
}
// set size hints
{
let mut size_hints = {
@@ -299,8 +340,15 @@ impl Window2 {
}.queue();
// These properties must be set after mapping
window.set_maximized_inner(window_attrs.maximized).queue();
window.set_fullscreen_inner(window_attrs.fullscreen.clone()).queue();
if window_attrs.maximized {
window.set_maximized_inner(window_attrs.maximized).queue();
}
if window_attrs.fullscreen.is_some() {
window.set_fullscreen_inner(window_attrs.fullscreen.clone()).queue();
}
if window_attrs.always_on_top {
window.set_always_on_top_inner(window_attrs.always_on_top).queue();
}
if window_attrs.visible {
unsafe {
@@ -331,6 +379,96 @@ impl Window2 {
))
}
fn set_pid(xconn: &Arc<XConnection>, window: ffi::Window) -> Option<util::Flusher> {
let pid_atom = unsafe { util::get_atom(xconn, b"_NET_WM_PID\0") }
.expect("Failed to call XInternAtom (_NET_WM_PID)");
let client_machine_atom = unsafe { util::get_atom(xconn, b"WM_CLIENT_MACHINE\0") }
.expect("Failed to call XInternAtom (WM_CLIENT_MACHINE)");
unsafe {
let (hostname, hostname_length) = {
// 64 would suffice for Linux, but 256 will be enough everywhere (as per SUSv2). For instance, this is
// the limit defined by OpenBSD.
const MAXHOSTNAMELEN: usize = 256;
let mut hostname: [c_char; MAXHOSTNAMELEN] = mem::uninitialized();
let status = libc::gethostname(hostname.as_mut_ptr(), hostname.len());
if status != 0 { return None; }
hostname[MAXHOSTNAMELEN - 1] = '\0' as c_char; // a little extra safety
let hostname_length = libc::strlen(hostname.as_ptr());
(hostname, hostname_length as usize)
};
util::change_property(
xconn,
window,
pid_atom,
ffi::XA_CARDINAL,
util::Format::Long,
util::PropMode::Replace,
&[libc::getpid() as util::Cardinal],
).queue();
let flusher = util::change_property(
xconn,
window,
client_machine_atom,
ffi::XA_STRING,
util::Format::Char,
util::PropMode::Replace,
&hostname[0..hostname_length],
);
Some(flusher)
}
}
fn set_window_type(
xconn: &Arc<XConnection>,
window: ffi::Window,
window_type: util::WindowType,
) -> util::Flusher {
let hint_atom = unsafe { util::get_atom(xconn, b"_NET_WM_WINDOW_TYPE\0") }
.expect("Failed to call XInternAtom (_NET_WM_WINDOW_TYPE)");
let window_type_atom = window_type.as_atom(xconn);
unsafe {
util::change_property(
xconn,
window,
hint_atom,
ffi::XA_ATOM,
util::Format::Long,
util::PropMode::Replace,
&[window_type_atom],
)
}
}
pub fn set_urgent(&self, is_urgent: bool) {
let xconn = &self.x.display;
let mut wm_hints = {
let mut wm_hints = unsafe {
(xconn.xlib.XGetWMHints)(xconn.display, self.x.window)
};
xconn.check_errors().expect("`XGetWMHints` failed");
if wm_hints.is_null() {
wm_hints = unsafe { (xconn.xlib.XAllocWMHints)() };
}
util::XSmartPointer::new(xconn, wm_hints)
}.expect("`XAllocWMHints` returned null; out of memory");
if is_urgent {
(*wm_hints).flags |= ffi::XUrgencyHint;
} else {
(*wm_hints).flags &= !ffi::XUrgencyHint;
}
unsafe {
(xconn.xlib.XSetWMHints)(
xconn.display,
self.x.window,
wm_hints.ptr,
);
util::flush_requests(xconn).expect("Failed to set urgency hint");
}
}
fn set_netwm(
xconn: &Arc<XConnection>,
window: ffi::Window,
@@ -395,41 +533,17 @@ impl Window2 {
self.invalidate_cached_frame_extents();
}
pub fn get_current_monitor(&self) -> X11MonitorId {
let monitors = get_available_monitors(&self.x.display);
let default = monitors[0].clone();
let (wx,wy) = match self.get_position() {
Some(val) => (cmp::max(0,val.0) as u32, cmp::max(0,val.1) as u32),
None=> return default,
};
let (ww,wh) = match self.get_outer_size() {
Some(val) => val,
None=> return default,
};
// Opposite corner coordinates
let (wxo, wyo) = (wx+ww-1, wy+wh-1);
// Find the monitor with the biggest overlap with the window
let mut overlap = 0;
let mut find = default;
for monitor in monitors {
let (mx, my) = monitor.get_position();
let mx = mx as u32;
let my = my as u32;
let (mw, mh) = monitor.get_dimensions();
let (mxo, myo) = (mx+mw-1, my+mh-1);
let (ox, oy) = (cmp::max(wx, mx), cmp::max(wy, my));
let (oxo, oyo) = (cmp::min(wxo, mxo), cmp::min(wyo, myo));
let osize = if ox <= oxo || oy <= oyo { 0 } else { (oxo-ox)*(oyo-oy) };
if osize > overlap {
overlap = osize;
find = monitor;
}
fn get_rect(&self) -> Option<util::Rect> {
// TODO: This might round-trip more times than needed.
if let (Some(position), Some(size)) = (self.get_position(), self.get_outer_size()) {
Some(util::Rect::new(position, size))
} else {
None
}
}
find
pub fn get_current_monitor(&self) -> X11MonitorId {
get_monitor_for_window(&self.x.display, self.get_rect()).to_owned()
}
fn set_maximized_inner(&self, maximized: bool) -> util::Flusher {
@@ -522,6 +636,27 @@ impl Window2 {
self.invalidate_cached_frame_extents();
}
fn set_always_on_top_inner(&self, always_on_top: bool) -> util::Flusher {
let xconn = &self.x.display;
let above_atom = unsafe { util::get_atom(xconn, b"_NET_WM_STATE_ABOVE\0") }
.expect("Failed to call XInternAtom (_NET_WM_STATE_ABOVE)");
Window2::set_netwm(
xconn,
self.x.window,
self.x.root,
(above_atom as c_long, 0, 0, 0),
always_on_top.into(),
)
}
pub fn set_always_on_top(&self, always_on_top: bool) {
self.set_always_on_top_inner(always_on_top)
.flush()
.expect("Failed to set always-on-top state");
}
fn set_icon_inner(&self, icon: Icon) -> util::Flusher {
let xconn = &self.x.display;
@@ -663,13 +798,13 @@ impl Window2 {
}
#[inline]
pub fn set_inner_size(&self, x: u32, y: u32) {
pub fn set_inner_size(&self, width: u32, height: u32) {
unsafe {
(self.x.display.xlib.XResizeWindow)(
self.x.display.display,
self.x.window,
x as c_uint,
y as c_uint,
width as c_uint,
height as c_uint,
);
util::flush_requests(&self.x.display)
}.expect("Failed to call XResizeWindow");
@@ -972,14 +1107,7 @@ impl Window2 {
}
pub fn hidpi_factor(&self) -> f32 {
unsafe {
let x_px = (self.x.display.xlib.XDisplayWidth)(self.x.display.display, self.x.screen_id);
let y_px = (self.x.display.xlib.XDisplayHeight)(self.x.display.display, self.x.screen_id);
let x_mm = (self.x.display.xlib.XDisplayWidthMM)(self.x.display.display, self.x.screen_id);
let y_mm = (self.x.display.xlib.XDisplayHeightMM)(self.x.display.display, self.x.screen_id);
let ppmm = ((x_px as f32 * y_px as f32) / (x_mm as f32 * y_mm as f32)).sqrt();
((ppmm * (12.0 * 25.4 / 96.0)).round() / 12.0).max(1.0) // quantize with 1/12 step size.
}
self.get_current_monitor().hidpi_factor
}
pub fn set_cursor_position(&self, x: i32, y: i32) -> Result<(), ()> {

View File

@@ -6,6 +6,7 @@ use std::collections::VecDeque;
use std::sync::{Arc, Mutex, Weak};
use super::window::Window2;
use std;
use std::os::raw::*;
use super::DeviceId;
@@ -294,10 +295,7 @@ impl EventsLoop {
// FIXME: Document this. Why do we do this? Seems like it passes on events to window/app.
// If we don't do this, window does not become main for some reason.
match event_type {
appkit::NSKeyDown => (),
_ => appkit::NSApp().sendEvent_(ns_event),
}
appkit::NSApp().sendEvent_(ns_event);
let windows = self.shared.windows.lock().unwrap();
let maybe_window = windows.iter()
@@ -318,127 +316,50 @@ impl EventsLoop {
});
match event_type {
appkit::NSKeyDown => {
let mut events = std::collections::VecDeque::new();
let received_c_str = foundation::NSString::UTF8String(ns_event.characters());
let received_str = std::ffi::CStr::from_ptr(received_c_str);
let vkey = to_virtual_key_code(NSEvent::keyCode(ns_event));
let state = ElementState::Pressed;
let code = NSEvent::keyCode(ns_event) as u32;
let window_event = WindowEvent::KeyboardInput {
device_id: DEVICE_ID,
input: KeyboardInput {
state: state,
scancode: code,
virtual_keycode: vkey,
modifiers: event_mods(ns_event),
},
};
for received_char in std::str::from_utf8(received_str.to_bytes()).unwrap().chars() {
let window_event = WindowEvent::ReceivedCharacter(received_char);
events.push_back(into_event(window_event));
}
self.shared.pending_events.lock().unwrap().extend(events.into_iter());
Some(into_event(window_event))
},
appkit::NSKeyUp => {
let vkey = to_virtual_key_code(NSEvent::keyCode(ns_event));
let state = ElementState::Released;
let code = NSEvent::keyCode(ns_event) as u32;
let window_event = WindowEvent::KeyboardInput {
device_id: DEVICE_ID,
input: KeyboardInput {
state: state,
scancode: code,
virtual_keycode: vkey,
modifiers: event_mods(ns_event),
},
};
Some(into_event(window_event))
},
appkit::NSFlagsChanged => {
unsafe fn modifier_event(event: cocoa::base::id,
keymask: NSEventModifierFlags,
key: events::VirtualKeyCode,
key_pressed: bool) -> Option<WindowEvent>
{
if !key_pressed && NSEvent::modifierFlags(event).contains(keymask) {
let state = ElementState::Pressed;
let code = NSEvent::keyCode(event) as u32;
let window_event = WindowEvent::KeyboardInput {
device_id: DEVICE_ID,
input: KeyboardInput {
state: state,
scancode: code,
virtual_keycode: Some(key),
modifiers: event_mods(event),
},
};
Some(window_event)
} else if key_pressed && !NSEvent::modifierFlags(event).contains(keymask) {
let state = ElementState::Released;
let code = NSEvent::keyCode(event) as u32;
let window_event = WindowEvent::KeyboardInput {
device_id: DEVICE_ID,
input: KeyboardInput {
state: state,
scancode: code,
virtual_keycode: Some(key),
modifiers: event_mods(event),
},
};
Some(window_event)
} else {
None
}
}
let mut events = std::collections::VecDeque::new();
if let Some(window_event) = modifier_event(ns_event,
NSEventModifierFlags::NSShiftKeyMask,
events::VirtualKeyCode::LShift,
self.modifiers.shift_pressed)
{
if let Some(window_event) = modifier_event(
ns_event,
NSEventModifierFlags::NSShiftKeyMask,
self.modifiers.shift_pressed,
) {
self.modifiers.shift_pressed = !self.modifiers.shift_pressed;
events.push_back(into_event(window_event));
}
if let Some(window_event) = modifier_event(ns_event,
NSEventModifierFlags::NSControlKeyMask,
events::VirtualKeyCode::LControl,
self.modifiers.ctrl_pressed)
{
if let Some(window_event) = modifier_event(
ns_event,
NSEventModifierFlags::NSControlKeyMask,
self.modifiers.ctrl_pressed,
) {
self.modifiers.ctrl_pressed = !self.modifiers.ctrl_pressed;
events.push_back(into_event(window_event));
}
if let Some(window_event) = modifier_event(ns_event,
NSEventModifierFlags::NSCommandKeyMask,
events::VirtualKeyCode::LWin,
self.modifiers.win_pressed)
{
if let Some(window_event) = modifier_event(
ns_event,
NSEventModifierFlags::NSCommandKeyMask,
self.modifiers.win_pressed,
) {
self.modifiers.win_pressed = !self.modifiers.win_pressed;
events.push_back(into_event(window_event));
}
if let Some(window_event) = modifier_event(ns_event,
NSEventModifierFlags::NSAlternateKeyMask,
events::VirtualKeyCode::LAlt,
self.modifiers.alt_pressed)
{
if let Some(window_event) = modifier_event(
ns_event,
NSEventModifierFlags::NSAlternateKeyMask,
self.modifiers.alt_pressed,
) {
self.modifiers.alt_pressed = !self.modifiers.alt_pressed;
events.push_back(into_event(window_event));
}
let event = events.pop_front();
self.shared.pending_events.lock().unwrap().extend(events.into_iter());
self.shared.pending_events
.lock()
.unwrap()
.extend(events.into_iter());
event
},
@@ -621,8 +542,7 @@ impl Proxy {
}
}
fn to_virtual_key_code(code: u16) -> Option<events::VirtualKeyCode> {
pub fn to_virtual_key_code(code: c_ushort) -> Option<events::VirtualKeyCode> {
Some(match code {
0x00 => events::VirtualKeyCode::A,
0x01 => events::VirtualKeyCode::S,
@@ -758,7 +678,7 @@ fn to_virtual_key_code(code: u16) -> Option<events::VirtualKeyCode> {
})
}
fn event_mods(event: cocoa::base::id) -> ModifiersState {
pub fn event_mods(event: cocoa::base::id) -> ModifiersState {
let flags = unsafe {
NSEvent::modifierFlags(event)
};
@@ -770,5 +690,30 @@ fn event_mods(event: cocoa::base::id) -> ModifiersState {
}
}
unsafe fn modifier_event(
ns_event: cocoa::base::id,
keymask: NSEventModifierFlags,
key_pressed: bool,
) -> Option<WindowEvent> {
if !key_pressed && NSEvent::modifierFlags(ns_event).contains(keymask)
|| key_pressed && !NSEvent::modifierFlags(ns_event).contains(keymask) {
let state = ElementState::Released;
let keycode = NSEvent::keyCode(ns_event);
let scancode = keycode as u32;
let virtual_keycode = to_virtual_key_code(keycode);
Some(WindowEvent::KeyboardInput {
device_id: DEVICE_ID,
input: KeyboardInput {
state,
scancode,
virtual_keycode,
modifiers: event_mods(ns_event),
},
})
} else {
None
}
}
// Constant device ID, to be removed when this backend is updated to report real device IDs.
const DEVICE_ID: ::DeviceId = ::DeviceId(DeviceId);
pub const DEVICE_ID: ::DeviceId = ::DeviceId(DeviceId);

107
src/platform/macos/ffi.rs Normal file
View File

@@ -0,0 +1,107 @@
// TODO: Upstream these
#![allow(dead_code, non_snake_case, non_upper_case_globals)]
use cocoa::base::{class, id};
use cocoa::foundation::{NSInteger, NSUInteger};
use objc;
pub const NSNotFound: NSInteger = NSInteger::max_value();
#[repr(C)]
pub struct NSRange {
pub location: NSUInteger,
pub length: NSUInteger,
}
impl NSRange {
#[inline]
pub fn new(location: NSUInteger, length: NSUInteger) -> NSRange {
NSRange { location, length }
}
}
unsafe impl objc::Encode for NSRange {
fn encode() -> objc::Encoding {
let encoding = format!(
// TODO: Verify that this is correct
"{{NSRange={}{}}}",
NSUInteger::encode().as_str(),
NSUInteger::encode().as_str(),
);
unsafe { objc::Encoding::from_str(&encoding) }
}
}
pub trait NSMutableAttributedString: Sized {
unsafe fn alloc(_: Self) -> id {
msg_send![class("NSMutableAttributedString"), alloc]
}
unsafe fn init(self) -> id; // *mut NSMutableAttributedString
unsafe fn initWithString(self, string: id) -> id;
unsafe fn initWithAttributedString(self, string: id) -> id;
unsafe fn string(self) -> id; // *mut NSString
unsafe fn mutableString(self) -> id; // *mut NSMutableString
unsafe fn length(self) -> NSUInteger;
}
impl NSMutableAttributedString for id {
unsafe fn init(self) -> id {
msg_send![self, init]
}
unsafe fn initWithString(self, string: id) -> id {
msg_send![self, initWithString:string]
}
unsafe fn initWithAttributedString(self, string: id) -> id {
msg_send![self, initWithAttributedString:string]
}
unsafe fn string(self) -> id {
msg_send![self, string]
}
unsafe fn mutableString(self) -> id {
msg_send![self, mutableString]
}
unsafe fn length(self) -> NSUInteger {
msg_send![self, length]
}
}
pub const kCGBaseWindowLevelKey: NSInteger = 0;
pub const kCGMinimumWindowLevelKey: NSInteger = 1;
pub const kCGDesktopWindowLevelKey: NSInteger = 2;
pub const kCGBackstopMenuLevelKey: NSInteger = 3;
pub const kCGNormalWindowLevelKey: NSInteger = 4;
pub const kCGFloatingWindowLevelKey: NSInteger = 5;
pub const kCGTornOffMenuWindowLevelKey: NSInteger = 6;
pub const kCGDockWindowLevelKey: NSInteger = 7;
pub const kCGMainMenuWindowLevelKey: NSInteger = 8;
pub const kCGStatusWindowLevelKey: NSInteger = 9;
pub const kCGModalPanelWindowLevelKey: NSInteger = 10;
pub const kCGPopUpMenuWindowLevelKey: NSInteger = 11;
pub const kCGDraggingWindowLevelKey: NSInteger = 12;
pub const kCGScreenSaverWindowLevelKey: NSInteger = 13;
pub const kCGMaximumWindowLevelKey: NSInteger = 14;
pub const kCGOverlayWindowLevelKey: NSInteger = 15;
pub const kCGHelpWindowLevelKey: NSInteger = 16;
pub const kCGUtilityWindowLevelKey: NSInteger = 17;
pub const kCGDesktopIconWindowLevelKey: NSInteger = 18;
pub const kCGCursorWindowLevelKey: NSInteger = 19;
pub const kCGNumberOfWindowLevelKeys: NSInteger = 20;
pub enum NSWindowLevel {
NSNormalWindowLevel = kCGBaseWindowLevelKey as _,
NSFloatingWindowLevel = kCGFloatingWindowLevelKey as _,
NSTornOffMenuWindowLevel = kCGTornOffMenuWindowLevelKey as _,
NSModalPanelWindowLevel = kCGModalPanelWindowLevelKey as _,
NSMainMenuWindowLevel = kCGMainMenuWindowLevelKey as _,
NSStatusWindowLevel = kCGStatusWindowLevelKey as _,
NSPopUpMenuWindowLevel = kCGPopUpMenuWindowLevelKey as _,
NSScreenSaverWindowLevel = kCGScreenSaverWindowLevelKey as _,
}

View File

@@ -38,5 +38,8 @@ impl Window {
}
mod events_loop;
mod ffi;
mod monitor;
mod util;
mod view;
mod window;

View File

@@ -1,8 +1,9 @@
use cocoa::appkit::NSScreen;
use cocoa::base::{id, nil};
use cocoa::foundation::{NSString, NSUInteger};
use core_graphics::display::{CGDirectDisplayID, CGDisplay};
use core_graphics::display::{CGDirectDisplayID, CGDisplay, CGDisplayBounds};
use std::collections::VecDeque;
use std::fmt;
use super::EventsLoop;
use super::window::IdRef;
@@ -32,6 +33,29 @@ impl EventsLoop {
}
}
impl fmt::Debug for MonitorId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
#[derive(Debug)]
struct MonitorId {
name: Option<String>,
native_identifier: u32,
dimensions: (u32, u32),
position: (i32, i32),
hidpi_factor: f32,
}
let monitor_id_proxy = MonitorId {
name: self.get_name(),
native_identifier: self.get_native_identifier(),
dimensions: self.get_dimensions(),
position: self.get_position(),
hidpi_factor: self.get_hidpi_factor(),
};
monitor_id_proxy.fmt(f)
}
}
impl MonitorId {
pub fn get_name(&self) -> Option<String> {
let MonitorId(display_id) = *self;
@@ -57,7 +81,8 @@ impl MonitorId {
#[inline]
pub fn get_position(&self) -> (i32, i32) {
unimplemented!()
let bounds = unsafe { CGDisplayBounds(self.get_native_identifier()) };
(bounds.origin.x as i32, bounds.origin.y as i32)
}
pub fn get_hidpi_factor(&self) -> f32 {

View File

@@ -0,0 +1,38 @@
use cocoa::appkit::NSWindowStyleMask;
use cocoa::base::{class, id, nil};
use cocoa::foundation::{NSRect, NSUInteger};
use core_graphics::display::CGDisplay;
use platform::platform::ffi;
use platform::platform::window::IdRef;
pub const EMPTY_RANGE: ffi::NSRange = ffi::NSRange {
location: ffi::NSNotFound as NSUInteger,
length: 0,
};
// For consistency with other platforms, this will...
// 1. translate the bottom-left window corner into the top-left window corner
// 2. translate the coordinate from a bottom-left origin coordinate system to a top-left one
pub fn bottom_left_to_top_left(rect: NSRect) -> i32 {
(CGDisplay::main().pixels_high() as f64 - (rect.origin.y + rect.size.height)) as _
}
pub unsafe fn set_style_mask(window: id, view: id, mask: NSWindowStyleMask) {
use cocoa::appkit::NSWindow;
window.setStyleMask_(mask);
// If we don't do this, key handling will break. Therefore, never call `setStyleMask` directly!
window.makeFirstResponder_(view);
}
pub unsafe fn create_input_context(view: id) -> IdRef {
let input_context: id = msg_send![class("NSTextInputContext"), alloc];
let input_context: id = msg_send![input_context, initWithClient:view];
IdRef::new(input_context)
}
#[allow(dead_code)]
pub unsafe fn open_emoji_picker() {
let app: id = msg_send![class("NSApplication"), sharedApplication];
let _: () = msg_send![app, orderFrontCharacterPalette:nil];
}

450
src/platform/macos/view.rs Normal file
View File

@@ -0,0 +1,450 @@
// This is a pretty close port of the implementation in GLFW:
// https://github.com/glfw/glfw/blob/7ef34eb06de54dd9186d3d21a401b2ef819b59e7/src/cocoa_window.m
use std::{slice, str};
use std::boxed::Box;
use std::collections::VecDeque;
use std::os::raw::*;
use std::sync::Weak;
use cocoa::base::{class, id, nil};
use cocoa::appkit::NSWindow;
use cocoa::foundation::{NSPoint, NSRect, NSSize, NSString, NSUInteger};
use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Protocol, Sel, BOOL};
use {ElementState, Event, KeyboardInput, WindowEvent, WindowId};
use platform::platform::events_loop::{DEVICE_ID, event_mods, Shared, to_virtual_key_code};
use platform::platform::util;
use platform::platform::ffi::*;
use platform::platform::window::{get_window_id, IdRef};
struct ViewState {
window: id,
shared: Weak<Shared>,
ime_spot: Option<(i32, i32)>,
raw_characters: Option<String>,
last_insert: Option<String>,
}
pub fn new_view(window: id, shared: Weak<Shared>) -> IdRef {
let state = ViewState {
window,
shared,
ime_spot: None,
raw_characters: None,
last_insert: None,
};
unsafe {
// This is free'd in `dealloc`
let state_ptr = Box::into_raw(Box::new(state)) as *mut c_void;
let view: id = msg_send![VIEW_CLASS.0, alloc];
IdRef::new(msg_send![view, initWithWinit:state_ptr])
}
}
pub fn set_ime_spot(view: id, input_context: id, x: i32, y: i32) {
unsafe {
let state_ptr: *mut c_void = *(*view).get_mut_ivar("winitState");
let state = &mut *(state_ptr as *mut ViewState);
let content_rect = NSWindow::contentRectForFrameRect_(
state.window,
NSWindow::frame(state.window),
);
let base_x = content_rect.origin.x as i32;
let base_y = (content_rect.origin.y + content_rect.size.height) as i32;
state.ime_spot = Some((base_x + x, base_y - y));
let _: () = msg_send![input_context, invalidateCharacterCoordinates];
}
}
struct ViewClass(*const Class);
unsafe impl Send for ViewClass {}
unsafe impl Sync for ViewClass {}
lazy_static! {
static ref VIEW_CLASS: ViewClass = unsafe {
let superclass = Class::get("NSView").unwrap();
let mut decl = ClassDecl::new("WinitView", superclass).unwrap();
decl.add_method(sel!(dealloc), dealloc as extern fn(&Object, Sel));
decl.add_method(
sel!(initWithWinit:),
init_with_winit as extern fn(&Object, Sel, *mut c_void) -> id,
);
decl.add_method(sel!(hasMarkedText), has_marked_text as extern fn(&Object, Sel) -> BOOL);
decl.add_method(
sel!(markedRange),
marked_range as extern fn(&Object, Sel) -> NSRange,
);
decl.add_method(sel!(selectedRange), selected_range as extern fn(&Object, Sel) -> NSRange);
decl.add_method(
sel!(setMarkedText:selectedRange:replacementRange:),
set_marked_text as extern fn(&mut Object, Sel, id, NSRange, NSRange),
);
decl.add_method(sel!(unmarkText), unmark_text as extern fn(&Object, Sel));
decl.add_method(
sel!(validAttributesForMarkedText),
valid_attributes_for_marked_text as extern fn(&Object, Sel) -> id,
);
decl.add_method(
sel!(attributedSubstringForProposedRange:actualRange:),
attributed_substring_for_proposed_range
as extern fn(&Object, Sel, NSRange, *mut c_void) -> id,
);
decl.add_method(
sel!(insertText:replacementRange:),
insert_text as extern fn(&Object, Sel, id, NSRange),
);
decl.add_method(
sel!(characterIndexForPoint:),
character_index_for_point as extern fn(&Object, Sel, NSPoint) -> NSUInteger,
);
decl.add_method(
sel!(firstRectForCharacterRange:actualRange:),
first_rect_for_character_range
as extern fn(&Object, Sel, NSRange, *mut c_void) -> NSRect,
);
decl.add_method(
sel!(doCommandBySelector:),
do_command_by_selector as extern fn(&Object, Sel, Sel),
);
decl.add_method(sel!(keyDown:), key_down as extern fn(&Object, Sel, id));
decl.add_method(sel!(keyUp:), key_up as extern fn(&Object, Sel, id));
decl.add_method(sel!(insertTab:), insert_tab as extern fn(&Object, Sel, id));
decl.add_method(sel!(insertBackTab:), insert_back_tab as extern fn(&Object, Sel, id));
decl.add_ivar::<*mut c_void>("winitState");
decl.add_ivar::<id>("markedText");
let protocol = Protocol::get("NSTextInputClient").unwrap();
decl.add_protocol(&protocol);
ViewClass(decl.register())
};
}
extern fn dealloc(this: &Object, _sel: Sel) {
unsafe {
let state: *mut c_void = *this.get_ivar("winitState");
let marked_text: id = *this.get_ivar("markedText");
let _: () = msg_send![marked_text, release];
Box::from_raw(state as *mut ViewState);
}
}
extern fn init_with_winit(this: &Object, _sel: Sel, state: *mut c_void) -> id {
unsafe {
let this: id = msg_send![this, init];
if this != nil {
(*this).set_ivar("winitState", state);
let marked_text = <id as NSMutableAttributedString>::init(
NSMutableAttributedString::alloc(nil),
);
(*this).set_ivar("markedText", marked_text);
}
this
}
}
extern fn has_marked_text(this: &Object, _sel: Sel) -> BOOL {
//println!("hasMarkedText");
unsafe {
let marked_text: id = *this.get_ivar("markedText");
(marked_text.length() > 0) as i8
}
}
extern fn marked_range(this: &Object, _sel: Sel) -> NSRange {
//println!("markedRange");
unsafe {
let marked_text: id = *this.get_ivar("markedText");
let length = marked_text.length();
if length > 0 {
NSRange::new(0, length - 1)
} else {
util::EMPTY_RANGE
}
}
}
extern fn selected_range(_this: &Object, _sel: Sel) -> NSRange {
//println!("selectedRange");
util::EMPTY_RANGE
}
extern fn set_marked_text(
this: &mut Object,
_sel: Sel,
string: id,
_selected_range: NSRange,
_replacement_range: NSRange,
) {
//println!("setMarkedText");
unsafe {
let marked_text_ref: &mut id = this.get_mut_ivar("markedText");
let _: () = msg_send![(*marked_text_ref), release];
let marked_text = NSMutableAttributedString::alloc(nil);
let has_attr = msg_send![string, isKindOfClass:class("NSAttributedString")];
if has_attr {
marked_text.initWithAttributedString(string);
} else {
marked_text.initWithString(string);
};
*marked_text_ref = marked_text;
}
}
extern fn unmark_text(this: &Object, _sel: Sel) {
//println!("unmarkText");
unsafe {
let marked_text: id = *this.get_ivar("markedText");
let mutable_string = marked_text.mutableString();
let _: () = msg_send![mutable_string, setString:""];
let input_context: id = msg_send![this, inputContext];
let _: () = msg_send![input_context, discardMarkedText];
}
}
extern fn valid_attributes_for_marked_text(_this: &Object, _sel: Sel) -> id {
//println!("validAttributesForMarkedText");
unsafe { msg_send![class("NSArray"), array] }
}
extern fn attributed_substring_for_proposed_range(
_this: &Object,
_sel: Sel,
_range: NSRange,
_actual_range: *mut c_void, // *mut NSRange
) -> id {
//println!("attributedSubstringForProposedRange");
nil
}
extern fn character_index_for_point(_this: &Object, _sel: Sel, _point: NSPoint) -> NSUInteger {
//println!("characterIndexForPoint");
0
}
extern fn first_rect_for_character_range(
this: &Object,
_sel: Sel,
_range: NSRange,
_actual_range: *mut c_void, // *mut NSRange
) -> NSRect {
//println!("firstRectForCharacterRange");
unsafe {
let state_ptr: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state_ptr as *mut ViewState);
let (x, y) = state.ime_spot.unwrap_or_else(|| {
let content_rect = NSWindow::contentRectForFrameRect_(
state.window,
NSWindow::frame(state.window),
);
let x = content_rect.origin.x;
let y = util::bottom_left_to_top_left(content_rect);
(x as i32, y as i32)
});
NSRect::new(
NSPoint::new(x as _, y as _),
NSSize::new(0.0, 0.0),
)
}
}
extern fn insert_text(this: &Object, _sel: Sel, string: id, _replacement_range: NSRange) {
//println!("insertText");
unsafe {
let state_ptr: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state_ptr as *mut ViewState);
let has_attr = msg_send![string, isKindOfClass:class("NSAttributedString")];
let characters = if has_attr {
// This is a *mut NSAttributedString
msg_send![string, string]
} else {
// This is already a *mut NSString
string
};
let slice = slice::from_raw_parts(
characters.UTF8String() as *const c_uchar,
characters.len(),
);
let string = str::from_utf8_unchecked(slice);
state.last_insert = Some(string.to_owned());
// We don't need this now, but it's here if that changes.
//let event: id = msg_send![class("NSApp"), currentEvent];
let mut events = VecDeque::with_capacity(characters.len());
for character in string.chars() {
events.push_back(Event::WindowEvent {
window_id: WindowId(get_window_id(state.window)),
event: WindowEvent::ReceivedCharacter(character),
});
}
if let Some(shared) = state.shared.upgrade() {
shared.pending_events
.lock()
.unwrap()
.append(&mut events);
}
}
}
extern fn do_command_by_selector(this: &Object, _sel: Sel, command: Sel) {
//println!("doCommandBySelector");
// Basically, we're sent this message whenever a keyboard event that doesn't generate a "human readable" character
// happens, i.e. newlines, tabs, and Ctrl+C.
unsafe {
let state_ptr: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state_ptr as *mut ViewState);
let shared = if let Some(shared) = state.shared.upgrade() {
shared
} else {
return;
};
let mut events = VecDeque::with_capacity(1);
if command == sel!(insertNewline:) {
// The `else` condition would emit the same character, but I'm keeping this here both...
// 1) as a reminder for how `doCommandBySelector` works
// 2) to make our use of carriage return explicit
events.push_back(Event::WindowEvent {
window_id: WindowId(get_window_id(state.window)),
event: WindowEvent::ReceivedCharacter('\r'),
});
} else {
let raw_characters = state.raw_characters.take();
if let Some(raw_characters) = raw_characters {
for character in raw_characters.chars() {
events.push_back(Event::WindowEvent {
window_id: WindowId(get_window_id(state.window)),
event: WindowEvent::ReceivedCharacter(character),
});
}
}
};
shared.pending_events
.lock()
.unwrap()
.append(&mut events);
}
}
extern fn key_down(this: &Object, _sel: Sel, event: id) {
//println!("keyDown");
unsafe {
let state_ptr: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state_ptr as *mut ViewState);
let window_id = WindowId(get_window_id(state.window));
let keycode: c_ushort = msg_send![event, keyCode];
let virtual_keycode = to_virtual_key_code(keycode);
let scancode = keycode as u32;
let is_repeat = msg_send![event, isARepeat];
let window_event = Event::WindowEvent {
window_id,
event: WindowEvent::KeyboardInput {
device_id: DEVICE_ID,
input: KeyboardInput {
state: ElementState::Pressed,
scancode,
virtual_keycode,
modifiers: event_mods(event),
},
},
};
state.raw_characters = {
let characters: id = msg_send![event, characters];
let slice = slice::from_raw_parts(
characters.UTF8String() as *const c_uchar,
characters.len(),
);
let string = str::from_utf8_unchecked(slice);
Some(string.to_owned())
};
if let Some(shared) = state.shared.upgrade() {
shared.pending_events
.lock()
.unwrap()
.push_back(window_event);
// Emit `ReceivedCharacter` for key repeats
if is_repeat && state.last_insert.is_some() {
let last_insert = state.last_insert.as_ref().unwrap();
for character in last_insert.chars() {
let window_event = Event::WindowEvent {
window_id,
event: WindowEvent::ReceivedCharacter(character),
};
shared.pending_events
.lock()
.unwrap()
.push_back(window_event);
}
}
}
let array: id = msg_send![class("NSArray"), arrayWithObject:event];
let (): _ = msg_send![this, interpretKeyEvents:array];
}
}
extern fn key_up(this: &Object, _sel: Sel, event: id) {
//println!("keyUp");
unsafe {
let state_ptr: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state_ptr as *mut ViewState);
state.last_insert = None;
let keycode: c_ushort = msg_send![event, keyCode];
let virtual_keycode = to_virtual_key_code(keycode);
let scancode = keycode as u32;
let window_event = Event::WindowEvent {
window_id: WindowId(get_window_id(state.window)),
event: WindowEvent::KeyboardInput {
device_id: DEVICE_ID,
input: KeyboardInput {
state: ElementState::Released,
scancode,
virtual_keycode,
modifiers: event_mods(event),
},
},
};
if let Some(shared) = state.shared.upgrade() {
shared.pending_events
.lock()
.unwrap()
.push_back(window_event);
}
}
}
extern fn insert_tab(this: &Object, _sel: Sel, _sender: id) {
unsafe {
let window: id = msg_send![this, window];
let first_responder: id = msg_send![window, firstResponder];
let this_ptr = this as *const _ as *mut _;
if first_responder == this_ptr {
let (): _ = msg_send![window, selectNextKeyView:this];
}
}
}
extern fn insert_back_tab(this: &Object, _sel: Sel, _sender: id) {
unsafe {
let window: id = msg_send![this, window];
let first_responder: id = msg_send![window, firstResponder];
let this_ptr = this as *const _ as *mut _;
if first_responder == this_ptr {
let (): _ = msg_send![window, selectPreviousKeyView:this];
}
}
}

View File

@@ -25,6 +25,9 @@ use std::sync::Weak;
use std::cell::{Cell, RefCell};
use super::events_loop::{EventsLoop, Shared};
use platform::platform::ffi;
use platform::platform::util;
use platform::platform::view::{new_view, set_ime_spot};
use window::MonitorId as RootMonitorId;
@@ -56,15 +59,14 @@ impl DelegateState {
let curr_mask = self.window.styleMask();
if !curr_mask.contains(NSWindowStyleMask::NSTitledWindowMask) {
self.window
.setStyleMask_(NSWindowStyleMask::NSResizableWindowMask);
util::set_style_mask(*self.window, *self.view, NSWindowStyleMask::NSResizableWindowMask);
}
let is_zoomed: BOOL = msg_send![*self.window, isZoomed];
// Roll back temp styles
if !curr_mask.contains(NSWindowStyleMask::NSTitledWindowMask) {
self.window.setStyleMask_(curr_mask);
util::set_style_mask(*self.window, *self.view, curr_mask);
}
is_zoomed != 0
@@ -79,7 +81,7 @@ impl DelegateState {
let save_style_opt = self.save_style_mask.take();
if let Some(save_style) = save_style_opt {
self.window.setStyleMask_(save_style);
util::set_style_mask(*self.window, *self.view, save_style);
}
win_attribs.maximized
@@ -167,7 +169,7 @@ impl WindowDelegate {
unsafe fn emit_move_event(state: &mut DelegateState) {
let frame_rect = NSWindow::frame(*state.window);
let x = frame_rect.origin.x as _;
let y = Window2::bottom_left_to_top_left(frame_rect);
let y = util::bottom_left_to_top_left(frame_rect);
let moved = state.previous_position != Some((x, y));
if moved {
state.previous_position = Some((x, y));
@@ -326,7 +328,7 @@ impl WindowDelegate {
unsafe {
let state: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state as *mut DelegateState);
state.win_attribs.borrow_mut().fullscreen = Some(get_current_monitor());
state.win_attribs.borrow_mut().fullscreen = Some(get_current_monitor(*state.window));
state.handle_with_fullscreen = false;
}
@@ -415,13 +417,13 @@ impl WindowDelegate {
// callbacks for drag and drop events
decl.add_method(sel!(draggingEntered:),
dragging_entered as extern fn(&Object, Sel, id) -> BOOL);
decl.add_method(sel!(prepareForDragOperation:),
decl.add_method(sel!(prepareForDragOperation:),
prepare_for_drag_operation as extern fn(&Object, Sel, id));
decl.add_method(sel!(performDragOperation:),
decl.add_method(sel!(performDragOperation:),
perform_drag_operation as extern fn(&Object, Sel, id) -> BOOL);
decl.add_method(sel!(concludeDragOperation:),
decl.add_method(sel!(concludeDragOperation:),
conclude_drag_operation as extern fn(&Object, Sel, id));
decl.add_method(sel!(draggingExited:),
decl.add_method(sel!(draggingExited:),
dragging_exited as extern fn(&Object, Sel, id));
// callbacks for fullscreen events
@@ -488,29 +490,26 @@ pub struct PlatformSpecificWindowBuilderAttributes {
pub titlebar_hidden: bool,
pub titlebar_buttons_hidden: bool,
pub fullsize_content_view: bool,
pub resize_increments: Option<(u32, u32)>,
}
pub struct Window2 {
pub view: IdRef,
pub window: IdRef,
pub delegate: WindowDelegate,
pub input_context: IdRef,
}
unsafe impl Send for Window2 {}
unsafe impl Sync for Window2 {}
/// Helpper funciton to convert NSScreen::mainScreen to MonitorId
unsafe fn get_current_monitor() -> RootMonitorId {
let screen = NSScreen::mainScreen(nil);
unsafe fn get_current_monitor(window: id) -> RootMonitorId {
let screen: id = msg_send![window, screen];
let desc = NSScreen::deviceDescription(screen);
let key = IdRef::new(NSString::alloc(nil).init_str("NSScreenNumber"));
let value = NSDictionary::valueForKey_(desc, *key);
let display_id = msg_send![value, unsignedIntegerValue];
RootMonitorId {
inner: EventsLoop::make_monitor_from_display(display_id),
}
RootMonitorId { inner: EventsLoop::make_monitor_from_display(display_id) }
}
impl Drop for Window2 {
@@ -573,28 +572,29 @@ impl Window2 {
let app = match Window2::create_app(pl_attribs.activation_policy) {
Some(app) => app,
None => {
None => {
let _: () = unsafe { msg_send![autoreleasepool, drain] };
return Err(OsError(format!("Couldn't create NSApplication")));
},
};
let window = match Window2::create_window(&win_attribs, &pl_attribs)
{
Some(window) => window,
None => {
let window = match Window2::create_window(&win_attribs, &pl_attribs) {
Some(res) => res,
None => {
let _: () = unsafe { msg_send![autoreleasepool, drain] };
return Err(OsError(format!("Couldn't create NSWindow")));
},
};
let view = match Window2::create_view(*window) {
let view = match Window2::create_view(*window, Weak::clone(&shared)) {
Some(view) => view,
None => {
None => {
let _: () = unsafe { msg_send![autoreleasepool, drain] };
return Err(OsError(format!("Couldn't create NSView")));
},
};
let input_context = unsafe { util::create_input_context(*view) };
unsafe {
if win_attribs.transparent {
(*window as id).setOpaque_(NO);
@@ -633,12 +633,13 @@ impl Window2 {
view: view,
window: window,
delegate: WindowDelegate::new(ds),
input_context,
};
// Set fullscreen mode after we setup everything
if let Some(ref monitor) = win_attribs.fullscreen {
unsafe {
if monitor.inner != get_current_monitor().inner {
if monitor.inner != get_current_monitor(*window.window).inner {
unimplemented!();
}
}
@@ -775,9 +776,15 @@ impl Window2 {
window.setMovableByWindowBackground_(YES);
}
if !attrs.decorations {
window.setTitleVisibility_(appkit::NSWindowTitleVisibility::NSWindowTitleHidden);
window.setTitlebarAppearsTransparent_(YES);
if attrs.always_on_top {
let _: () = msg_send![*window, setLevel:ffi::NSWindowLevel::NSFloatingWindowLevel];
}
if let Some((x, y)) = pl_attrs.resize_increments {
if x >= 1 && y >= 1 {
let size = NSSize::new(x as _, y as _);
window.setResizeIncrements_(size);
}
}
window.center();
@@ -788,12 +795,13 @@ impl Window2 {
}
}
fn create_view(window: id) -> Option<IdRef> {
fn create_view(window: id, shared: Weak<Shared>) -> Option<IdRef> {
unsafe {
let view = IdRef::new(NSView::init(NSView::alloc(nil)));
let view = new_view(window, shared);
view.non_nil().map(|view| {
view.setWantsBestResolutionOpenGLSurface_(YES);
window.setContentView_(*view);
window.makeFirstResponder_(*view);
view
})
}
@@ -816,18 +824,11 @@ impl Window2 {
unsafe { NSWindow::orderOut_(*self.window, nil); }
}
// For consistency with other platforms, this will...
// 1. translate the bottom-left window corner into the top-left window corner
// 2. translate the coordinate from a bottom-left origin coordinate system to a top-left one
fn bottom_left_to_top_left(rect: NSRect) -> i32 {
(CGDisplay::main().pixels_high() as f64 - (rect.origin.y + rect.size.height)) as _
}
pub fn get_position(&self) -> Option<(i32, i32)> {
let frame_rect = unsafe { NSWindow::frame(*self.window) };
Some((
frame_rect.origin.x as i32,
Self::bottom_left_to_top_left(frame_rect),
util::bottom_left_to_top_left(frame_rect),
))
}
@@ -840,7 +841,7 @@ impl Window2 {
};
Some((
content_rect.origin.x as i32,
Self::bottom_left_to_top_left(content_rect),
util::bottom_left_to_top_left(content_rect),
))
}
@@ -1029,10 +1030,9 @@ impl Window2 {
let curr_mask = state.window.styleMask();
if !curr_mask.contains(NSWindowStyleMask::NSTitledWindowMask) {
state.window.setStyleMask_(
NSWindowStyleMask::NSTitledWindowMask
| NSWindowStyleMask::NSResizableWindowMask,
);
let mask = NSWindowStyleMask::NSTitledWindowMask
| NSWindowStyleMask::NSResizableWindowMask;
util::set_style_mask(*self.window, *self.view, mask);
state.save_style_mask.set(Some(curr_mask));
}
}
@@ -1067,25 +1067,42 @@ impl Window2 {
} else {
NSWindowStyleMask::NSBorderlessWindowMask
};
util::set_style_mask(*state.window, *state.view, new_mask);
}
}
state.window.setStyleMask_(new_mask);
#[inline]
pub fn set_always_on_top(&self, always_on_top: bool) {
unsafe {
let level = if always_on_top {
ffi::NSWindowLevel::NSFloatingWindowLevel
} else {
ffi::NSWindowLevel::NSNormalWindowLevel
};
let _: () = msg_send![*self.window, setLevel:level];
}
}
#[inline]
pub fn set_window_icon(&self, _icon: Option<::Icon>) {
// macOS doesn't have window icons. Though, there is `setRepresentedFilename`, but that's
// semantically distinct and should only be used when the window is in some representing a
// specific file/directory. For instance, Terminal.app uses this for the CWD. Anyway, that
// should eventually be implemented as `WindowBuilderExt::with_represented_file` or
// something, and doesn't have anything to do with this.
// semantically distinct and should only be used when the window is in some way
// representing a specific file/directory. For instance, Terminal.app uses this for the
// CWD. Anyway, that should eventually be implemented as
// `WindowBuilderExt::with_represented_file` or something, and doesn't have anything to do
// with `set_window_icon`.
// https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/WinPanel/Tasks/SettingWindowTitle.html
}
#[inline]
pub fn set_ime_spot(&self, x: i32, y: i32) {
set_ime_spot(*self.view, *self.input_context, x, y);
}
#[inline]
pub fn get_current_monitor(&self) -> RootMonitorId {
unsafe {
self::get_current_monitor()
self::get_current_monitor(*self.window)
}
}
}
@@ -1169,8 +1186,7 @@ impl Drop for IdRef {
fn drop(&mut self) {
if self.0 != nil {
unsafe {
let autoreleasepool =
NSAutoreleasePool::new(nil);
let autoreleasepool = NSAutoreleasePool::new(nil);
let _ : () = msg_send![self.0, release];
let _ : () = msg_send![autoreleasepool, release];
};

View File

@@ -205,31 +205,49 @@ pub fn vkey_to_winit_vkey(vkey: c_int) -> Option<VirtualKeyCode> {
}
}
pub fn vkey_left_right(vkey: c_int, scancode: UINT, extended: bool) -> c_int {
match vkey {
winuser::VK_SHIFT => unsafe { winuser::MapVirtualKeyA(
scancode,
winuser::MAPVK_VSC_TO_VK_EX,
) as _ },
winuser::VK_CONTROL => if extended {
winuser::VK_RCONTROL
} else {
winuser::VK_LCONTROL
},
winuser::VK_MENU => if extended {
winuser::VK_RMENU
} else {
winuser::VK_LMENU
},
_ => vkey,
}
pub fn handle_extended_keys(vkey: c_int, mut scancode: UINT, extended: bool) -> Option<(c_int, UINT)> {
// Welcome to hell https://blog.molecular-matters.com/2011/09/05/properly-handling-keyboard-input/
let vkey = match vkey {
winuser::VK_SHIFT => unsafe { winuser::MapVirtualKeyA(
scancode,
winuser::MAPVK_VSC_TO_VK_EX,
) as _ },
winuser::VK_CONTROL => if extended {
winuser::VK_RCONTROL
} else {
winuser::VK_LCONTROL
},
winuser::VK_MENU => if extended {
winuser::VK_RMENU
} else {
winuser::VK_LMENU
},
_ => match scancode {
// This is only triggered when using raw input. Without this check, we get two events whenever VK_PAUSE is
// pressed, the first one having scancode 0x1D but vkey VK_PAUSE...
0x1D if vkey == winuser::VK_PAUSE => return None,
// ...and the second having scancode 0x45 but an unmatched vkey!
0x45 => winuser::VK_PAUSE,
// VK_PAUSE and VK_SCROLL have the same scancode when using modifiers, alongside incorrect vkey values.
0x46 => {
if extended {
scancode = 0x45;
winuser::VK_PAUSE
} else {
winuser::VK_SCROLL
}
},
_ => vkey,
},
};
Some((vkey, scancode))
}
pub fn vkeycode_to_element(wparam: WPARAM, lparam: LPARAM) -> (ScanCode, Option<VirtualKeyCode>) {
pub fn process_key_params(wparam: WPARAM, lparam: LPARAM) -> Option<(ScanCode, Option<VirtualKeyCode>)> {
let scancode = ((lparam >> 16) & 0xff) as UINT;
let extended = (lparam & 0x01000000) != 0;
let vkey = vkey_left_right(wparam as _, scancode, extended);
(scancode, vkey_to_winit_vkey(vkey))
handle_extended_keys(wparam as _, scancode, extended)
.map(|(vkey, scancode)| (scancode, vkey_to_winit_vkey(vkey)))
}
// This is needed as windows doesn't properly distinguish

View File

@@ -34,7 +34,7 @@ use winapi::um::winnt::{LONG, SHORT};
use events::DeviceEvent;
use platform::platform::{event, Cursor, WindowId, DEVICE_ID, wrap_device_id, util};
use platform::platform::event::{vkey_to_winit_vkey, vkey_left_right};
use platform::platform::event::{handle_extended_keys, process_key_params, vkey_to_winit_vkey};
use platform::platform::raw_input::*;
use platform::platform::window::adjust_size;
@@ -586,26 +586,27 @@ pub unsafe extern "system" fn callback(window: HWND, msg: UINT,
if msg == winuser::WM_SYSKEYDOWN && wparam as i32 == winuser::VK_F4 {
winuser::DefWindowProcW(window, msg, wparam, lparam)
} else {
let (scancode, vkey) = event::vkeycode_to_element(wparam, lparam);
send_event(Event::WindowEvent {
window_id: SuperWindowId(WindowId(window)),
event: WindowEvent::KeyboardInput {
device_id: DEVICE_ID,
input: KeyboardInput {
state: Pressed,
scancode: scancode,
virtual_keycode: vkey,
modifiers: event::get_key_mods(),
}
}
});
// Windows doesn't emit a delete character by default, but in order to make it
// consistent with the other platforms we'll emit a delete character here.
if vkey == Some(VirtualKeyCode::Delete) {
if let Some((scancode, vkey)) = process_key_params(wparam, lparam) {
send_event(Event::WindowEvent {
window_id: SuperWindowId(WindowId(window)),
event: WindowEvent::ReceivedCharacter('\u{7F}'),
event: WindowEvent::KeyboardInput {
device_id: DEVICE_ID,
input: KeyboardInput {
state: Pressed,
scancode: scancode,
virtual_keycode: vkey,
modifiers: event::get_key_mods(),
}
}
});
// Windows doesn't emit a delete character by default, but in order to make it
// consistent with the other platforms we'll emit a delete character here.
if vkey == Some(VirtualKeyCode::Delete) {
send_event(Event::WindowEvent {
window_id: SuperWindowId(WindowId(window)),
event: WindowEvent::ReceivedCharacter('\u{7F}'),
});
}
}
0
}
@@ -613,19 +614,20 @@ pub unsafe extern "system" fn callback(window: HWND, msg: UINT,
winuser::WM_KEYUP | winuser::WM_SYSKEYUP => {
use events::ElementState::Released;
let (scancode, vkey) = event::vkeycode_to_element(wparam, lparam);
send_event(Event::WindowEvent {
window_id: SuperWindowId(WindowId(window)),
event: WindowEvent::KeyboardInput {
device_id: DEVICE_ID,
input: KeyboardInput {
state: Released,
scancode: scancode,
virtual_keycode: vkey,
modifiers: event::get_key_mods(),
},
}
});
if let Some((scancode, vkey)) = process_key_params(wparam, lparam) {
send_event(Event::WindowEvent {
window_id: SuperWindowId(WindowId(window)),
event: WindowEvent::KeyboardInput {
device_id: DEVICE_ID,
input: KeyboardInput {
state: Released,
scancode: scancode,
virtual_keycode: vkey,
modifiers: event::get_key_mods(),
},
}
});
}
0
},
@@ -836,23 +838,25 @@ pub unsafe extern "system" fn callback(window: HWND, msg: UINT,
};
let scancode = keyboard.MakeCode as _;
let extended = util::has_flag(keyboard.Flags, winuser::RI_KEY_E0 as _);
let vkey = vkey_left_right(
let extended = util::has_flag(keyboard.Flags, winuser::RI_KEY_E0 as _)
| util::has_flag(keyboard.Flags, winuser::RI_KEY_E1 as _);
if let Some((vkey, scancode)) = handle_extended_keys(
keyboard.VKey as _,
scancode,
extended,
);
let virtual_keycode = vkey_to_winit_vkey(vkey);
) {
let virtual_keycode = vkey_to_winit_vkey(vkey);
send_event(Event::DeviceEvent {
device_id,
event: Key(KeyboardInput {
scancode,
state,
virtual_keycode,
modifiers: event::get_key_mods(),
}),
});
send_event(Event::DeviceEvent {
device_id,
event: Key(KeyboardInput {
scancode,
state,
virtual_keycode,
modifiers: event::get_key_mods(),
}),
});
}
}
}
}
@@ -922,30 +926,24 @@ pub unsafe extern "system" fn callback(window: HWND, msg: UINT,
winuser::WM_SETCURSOR => {
let call_def_window_proc = CONTEXT_STASH.with(|context_stash| {
let cstash = context_stash.borrow();
let mut call_def_window_proc = false;
if let Some(cstash) = cstash.as_ref() {
if let Some(w_stash) = cstash.windows.get(&window) {
if let Ok(window_state) = w_stash.lock() {
if window_state.mouse_in_window {
match window_state.cursor_state {
CursorState::Normal => {
winuser::SetCursor(winuser::LoadCursorW(
ptr::null_mut(),
window_state.cursor));
},
CursorState::Grab | CursorState::Hide => {
winuser::SetCursor(ptr::null_mut());
}
}
} else {
call_def_window_proc = true;
}
context_stash
.borrow()
.as_ref()
.and_then(|cstash| cstash.windows.get(&window))
.map(|window_state_mutex| {
let window_state = window_state_mutex.lock().unwrap();
if window_state.mouse_in_window {
let cursor = winuser::LoadCursorW(
ptr::null_mut(),
window_state.cursor.0,
);
winuser::SetCursor(cursor);
false
} else {
true
}
}
}
call_def_window_proc
})
.unwrap_or(true)
});
if call_def_window_proc {

View File

@@ -16,8 +16,11 @@ pub struct PlatformSpecificWindowBuilderAttributes {
unsafe impl Send for PlatformSpecificWindowBuilderAttributes {}
unsafe impl Sync for PlatformSpecificWindowBuilderAttributes {}
// TODO: document what this means
pub type Cursor = *const winapi::ctypes::wchar_t;
// Cursor name in UTF-16. Used to set cursor in `WM_SETCURSOR`.
#[derive(Debug, Clone)]
pub struct Cursor(pub *const winapi::ctypes::wchar_t);
unsafe impl Send for Cursor {}
unsafe impl Sync for Cursor {}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeviceId(u32);

View File

@@ -9,7 +9,7 @@ use std::{mem, ptr};
use super::{EventsLoop, util};
/// Win32 implementation of the main `MonitorId` object.
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct MonitorId {
/// The system name of the adapter.
adapter_name: [wchar_t; 32],
@@ -39,7 +39,7 @@ pub struct MonitorId {
// For more info see:
// https://github.com/retep998/winapi-rs/issues/360
// https://github.com/retep998/winapi-rs/issues/396
#[derive(Clone)]
#[derive(Debug, Clone)]
struct HMonitor(HMONITOR);
unsafe impl Send for HMonitor {}
@@ -152,13 +152,6 @@ impl MonitorId {
self.dimensions
}
/// This is a Win32-only function for `MonitorId` that returns the system name of the adapter
/// device.
#[inline]
pub fn get_adapter_name(&self) -> &[wchar_t] {
&self.adapter_name
}
/// A window that is positioned at these coordinates will overlap the monitor.
#[inline]
pub fn get_position(&self) -> (i32, i32) {

View File

@@ -3,6 +3,7 @@ use std::ops::BitAnd;
use winapi::ctypes::wchar_t;
use winapi::shared::minwindef::DWORD;
use winapi::shared::windef::RECT;
use winapi::um::errhandlingapi::GetLastError;
use winapi::um::winbase::{
FormatMessageW,
@@ -32,6 +33,15 @@ pub fn wchar_to_string(wchar: &[wchar_t]) -> String {
.to_string()
}
// This won't be needed anymore if we just add a derive to winapi.
pub fn rect_eq(a: &RECT, b: &RECT) -> bool {
let left_eq = a.left == b.left;
let right_eq = a.right == b.right;
let top_eq = a.top == b.top;
let bottom_eq = a.bottom == b.bottom;
left_eq && right_eq && top_eq && bottom_eq
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct WinError(Option<String>);

View File

@@ -8,9 +8,9 @@ use std::os::windows::ffi::OsStrExt;
use std::sync::{Arc, Mutex};
use std::sync::mpsc::channel;
use winapi::shared::minwindef::{BOOL, DWORD, UINT};
use winapi::shared::windef::{HDC, HWND, POINT, RECT};
use winapi::um::{combaseapi, dwmapi, libloaderapi, processthreadsapi, winuser};
use winapi::shared::minwindef::{BOOL, DWORD, FALSE, TRUE, UINT};
use winapi::shared::windef::{HDC, HWND, LPPOINT, POINT, RECT};
use winapi::um::{combaseapi, dwmapi, libloaderapi, winuser};
use winapi::um::objbase::{COINIT_MULTITHREADED};
use winapi::um::unknwnbase::{IUnknown, IUnknownVtbl};
use winapi::um::winnt::{HRESULT, LONG, LPCWSTR};
@@ -22,10 +22,11 @@ use MonitorId as RootMonitorId;
use MouseCursor;
use WindowAttributes;
use platform::platform::{EventsLoop, PlatformSpecificWindowBuilderAttributes, WindowId};
use platform::platform::{Cursor, EventsLoop, PlatformSpecificWindowBuilderAttributes, WindowId};
use platform::platform::events_loop::{self, DESTROY_MSG_ID};
use platform::platform::icon::{self, IconType, WinIcon};
use platform::platform::raw_input::register_all_mice_and_keyboards_for_raw_input;
use platform::platform::util;
/// The Win32 implementation of the main `Window` object.
pub struct Window {
@@ -275,70 +276,108 @@ impl Window {
MouseCursor::Wait => winuser::IDC_WAIT,
MouseCursor::Progress => winuser::IDC_APPSTARTING,
MouseCursor::Help => winuser::IDC_HELP,
MouseCursor::NoneCursor => ptr::null(),
_ => winuser::IDC_ARROW, // use arrow for the missing cases.
};
let mut cur = self.window_state.lock().unwrap();
cur.cursor = cursor_id;
cur.cursor = Cursor(cursor_id);
}
// TODO: it should be possible to rework this function by using the `execute_in_thread` method
// of the events loop.
pub fn set_cursor_state(&self, state: CursorState) -> Result<(), String> {
let mut current_state = self.window_state.lock().unwrap();
unsafe fn cursor_is_grabbed(&self) -> Result<bool, String> {
let mut client_rect: RECT = mem::uninitialized();
let mut clip_rect: RECT = mem::uninitialized();
if winuser::GetClientRect(self.window.0, &mut client_rect) == 0 {
return Err("`GetClientRect` failed".to_owned());
}
// A `POINT` is two `LONG`s (x, y), and the `RECT` field after `left` is `top`.
if winuser::ClientToScreen(self.window.0, &mut client_rect.left as *mut _ as LPPOINT) == 0 {
return Err("`ClientToScreen` (left, top) failed".to_owned());
}
if winuser::ClientToScreen(self.window.0, &mut client_rect.right as *mut _ as LPPOINT) == 0 {
return Err("`ClientToScreen` (right, bottom) failed".to_owned());
}
if winuser::GetClipCursor(&mut clip_rect) == 0 {
return Err("`GetClipCursor` failed".to_owned());
}
Ok(util::rect_eq(&client_rect, &clip_rect))
}
let foreground_thread_id = unsafe { winuser::GetWindowThreadProcessId(self.window.0, ptr::null_mut()) };
let current_thread_id = unsafe { processthreadsapi::GetCurrentThreadId() };
fn change_cursor_state(
window: &WindowWrapper,
current_state: CursorState,
state: CursorState,
) -> Result<CursorState, String> {
match (current_state, state) {
(CursorState::Normal, CursorState::Normal)
| (CursorState::Hide, CursorState::Hide)
| (CursorState::Grab, CursorState::Grab) => (), // no-op
unsafe { winuser::AttachThreadInput(foreground_thread_id, current_thread_id, 1) };
let res = match (state, current_state.cursor_state) {
(CursorState::Normal, CursorState::Normal) => Ok(()),
(CursorState::Hide, CursorState::Hide) => Ok(()),
(CursorState::Grab, CursorState::Grab) => Ok(()),
(CursorState::Hide, CursorState::Normal) => {
current_state.cursor_state = CursorState::Hide;
Ok(())
(CursorState::Normal, CursorState::Hide) => unsafe {
winuser::ShowCursor(FALSE);
},
(CursorState::Normal, CursorState::Hide) => {
current_state.cursor_state = CursorState::Normal;
Ok(())
},
(CursorState::Grab, CursorState::Normal) | (CursorState::Grab, CursorState::Hide) => {
unsafe {
let mut rect = mem::uninitialized();
if winuser::GetClientRect(self.window.0, &mut rect) == 0 {
return Err(format!("GetWindowRect failed"));
}
winuser::ClientToScreen(self.window.0, mem::transmute(&mut rect.left));
winuser::ClientToScreen(self.window.0, mem::transmute(&mut rect.right));
if winuser::ClipCursor(&rect) == 0 {
return Err(format!("ClipCursor failed"));
}
current_state.cursor_state = CursorState::Grab;
Ok(())
(CursorState::Grab, CursorState::Hide) => unsafe {
if winuser::ClipCursor(ptr::null()) == 0 {
return Err("`ClipCursor` failed".to_owned());
}
},
(CursorState::Normal, CursorState::Grab) => {
unsafe {
if winuser::ClipCursor(ptr::null()) == 0 {
return Err(format!("ClipCursor failed"));
}
current_state.cursor_state = CursorState::Normal;
Ok(())
(CursorState::Hide, CursorState::Normal) => unsafe {
winuser::ShowCursor(TRUE);
},
(CursorState::Normal, CursorState::Grab)
| (CursorState::Hide, CursorState::Grab) => unsafe {
let mut rect = mem::uninitialized();
if winuser::GetClientRect(window.0, &mut rect) == 0 {
return Err("`GetClientRect` failed".to_owned());
}
if winuser::ClientToScreen(window.0, &mut rect.left as *mut _ as LPPOINT) == 0 {
return Err("`ClientToScreen` (left, top) failed".to_owned());
}
if winuser::ClientToScreen(window.0, &mut rect.right as *mut _ as LPPOINT) == 0 {
return Err("`ClientToScreen` (right, bottom) failed".to_owned());
}
if winuser::ClipCursor(&rect) == 0 {
return Err("`ClipCursor` failed".to_owned());
}
if current_state != CursorState::Hide {
winuser::ShowCursor(FALSE);
}
},
_ => unimplemented!(),
(CursorState::Grab, CursorState::Normal) => unsafe {
if winuser::ClipCursor(ptr::null()) == 0 {
return Err("`ClipCursor` failed".to_owned());
}
winuser::ShowCursor(TRUE);
},
};
Ok(state)
}
unsafe { winuser::AttachThreadInput(foreground_thread_id, current_thread_id, 0) };
res
pub fn set_cursor_state(&self, state: CursorState) -> Result<(), String> {
let is_grabbed = unsafe { self.cursor_is_grabbed() }?;
let (tx, rx) = channel();
let window = self.window.clone();
let window_state = Arc::clone(&self.window_state);
self.events_loop_proxy.execute_in_thread(move |_| {
let mut window_state_lock = window_state.lock().unwrap();
// We should probably also check if the cursor is hidden,
// but `GetCursorInfo` isn't in winapi-rs yet, and it doesn't seem to matter as much.
let current_state = match window_state_lock.cursor_state {
CursorState::Normal if is_grabbed => CursorState::Grab,
CursorState::Grab if !is_grabbed => CursorState::Normal,
current_state => current_state,
};
let result = Self::change_cursor_state(&window, current_state, state)
.map(|_| {
window_state_lock.cursor_state = state;
});
let _ = tx.send(result);
});
rx.recv().unwrap()
}
#[inline]
@@ -611,6 +650,38 @@ impl Window {
}
}
#[inline]
pub fn set_always_on_top(&self, always_on_top: bool) {
if let Ok(mut window_state) = self.window_state.lock() {
if window_state.attributes.always_on_top == always_on_top {
return;
}
let window = self.window.clone();
self.events_loop_proxy.execute_in_thread(move |_| {
let insert_after = if always_on_top {
winuser::HWND_TOPMOST
} else {
winuser::HWND_NOTOPMOST
};
unsafe {
winuser::SetWindowPos(
window.0,
insert_after,
0,
0,
0,
0,
winuser::SWP_ASYNCWINDOWPOS | winuser::SWP_NOMOVE | winuser::SWP_NOSIZE,
);
winuser::UpdateWindow(window.0);
}
});
window_state.attributes.always_on_top = always_on_top;
}
}
#[inline]
pub fn get_current_monitor(&self) -> RootMonitorId {
RootMonitorId {
@@ -643,6 +714,11 @@ impl Window {
}
self.taskbar_icon.replace(taskbar_icon);
}
#[inline]
pub fn set_ime_spot(&self, _x: i32, _y: i32) {
unimplemented!();
}
}
impl Drop for Window {
@@ -721,7 +797,7 @@ unsafe fn init(
};
// computing the style and extended style of the window
let (ex_style, style) = if !window.decorations {
let (mut ex_style, style) = if !window.decorations {
(winuser::WS_EX_APPWINDOW,
//winapi::WS_POPUP is incompatible with winapi::WS_CHILD
if pl_attribs.parent.is_some() {
@@ -736,6 +812,10 @@ unsafe fn init(
winuser::WS_OVERLAPPEDWINDOW | winuser::WS_CLIPSIBLINGS | winuser::WS_CLIPCHILDREN)
};
if window.always_on_top {
ex_style |= winuser::WS_EX_TOPMOST;
}
// adjusting the window coordinates using the style
winuser::AdjustWindowRectEx(&mut rect, style, 0, ex_style);
@@ -808,7 +888,7 @@ unsafe fn init(
// Creating a mutex to track the current window state
let window_state = Arc::new(Mutex::new(events_loop::WindowState {
cursor: winuser::IDC_ARROW, // use arrow by default
cursor: Cursor(winuser::IDC_ARROW), // use arrow by default
cursor_state: CursorState::Normal,
attributes: window,
mouse_in_window: false,

View File

@@ -92,6 +92,13 @@ impl WindowBuilder {
self
}
/// Sets whether or not the window will always be on top of other windows.
#[inline]
pub fn with_always_on_top(mut self, always_on_top: bool) -> WindowBuilder {
self.window.always_on_top = always_on_top;
self
}
/// Sets the window icon. On Windows and X11, this is typically the small icon in the top-left
/// corner of the titlebar.
///
@@ -363,6 +370,12 @@ impl Window {
self.window.set_decorations(decorations)
}
/// Change whether or not the window will always be on top of other windows.
#[inline]
pub fn set_always_on_top(&self, always_on_top: bool) {
self.window.set_always_on_top(always_on_top)
}
/// Sets the window icon. On Windows and X11, this is typically the small icon in the top-left
/// corner of the titlebar.
///
@@ -376,6 +389,12 @@ impl Window {
self.window.set_window_icon(window_icon)
}
/// Sets location of IME candidate box in client area coordinates relative to the top left.
#[inline]
pub fn set_ime_spot(&self, x: i32, y: i32) {
self.window.set_ime_spot(x, y)
}
/// Returns the monitor on which the window currently resides
pub fn get_current_monitor(&self) -> MonitorId {
self.window.get_current_monitor()
@@ -409,7 +428,7 @@ impl Iterator for AvailableMonitorsIter {
}
/// Identifier for a monitor.
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct MonitorId {
pub(crate) inner: platform::MonitorId
}