Skip to content

Commit

Permalink
unix::WindowExt no longer returns pointers for things that aren't act…
Browse files Browse the repository at this point in the history
…ually pointers

Fixes rust-windowing#256

`get_xlib_window` and `get_xlib_screen_id` previously returned `Option<*mut c_void>` by
casting integer IDs into pointers, which while producing no functionality issues, is
semantically incorrect and rather surprising. Worse still, the docs for `get_xlib_window`
stated that it was in fact a valid pointer.

Additionally, now all `unix::WindowExt` methods return `std::os::raw` types rather than
`libc` types; note that the two versions of `c_void` are not interchangeable in the eyes of
the compiler, so those wanting the `libc` type will need to explicitly cast.

This is a breaking change, and will require some trivial changes to glutin.
  • Loading branch information
francesca64 committed Dec 15, 2017
1 parent 8f18dab commit 32d76ef
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Add support for `Touch` for emscripten backend.
- Added support for `DroppedFile`, `HoveredFile`, and `HoveredFileCancelled` to X11 backend.
- **Breaking:** `unix::WindowExt` no longer returns pointers for things that aren't actually pointers; `get_xlib_window` now returns `Option<std::os::raw::c_ulong>` and `get_xlib_screen_id` returns `Option<std::os::raw::c_int>`. Additionally, methods that previously returned `libc::c_void` have been changed to return `std::os::raw::c_void`, which are not interchangeable types, so users wanting the former will need to explicitly cast.

# Version 0.9.0 (2017-12-01)

Expand Down
32 changes: 15 additions & 17 deletions src/os/unix.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))]

use std::os::raw;
use std::sync::Arc;
use std::ptr;
use libc;
use EventsLoop;
use MonitorId;
use Window;
Expand Down Expand Up @@ -78,46 +78,44 @@ impl EventsLoopExt for EventsLoop {

/// Additional methods on `Window` that are specific to Unix.
pub trait WindowExt {
/// Returns a pointer to the `Window` object of xlib that is used by this window.
/// Returns the ID of the `Window` xlib object that is used by this window.
///
/// Returns `None` if the window doesn't use xlib (if it uses wayland for example).
///
/// The pointer will become invalid when the glutin `Window` is destroyed.
fn get_xlib_window(&self) -> Option<*mut libc::c_void>;
fn get_xlib_window(&self) -> Option<raw::c_ulong>;

/// Returns a pointer to the `Display` object of xlib that is used by this window.
///
/// Returns `None` if the window doesn't use xlib (if it uses wayland for example).
///
/// The pointer will become invalid when the glutin `Window` is destroyed.
fn get_xlib_display(&self) -> Option<*mut libc::c_void>;
fn get_xlib_display(&self) -> Option<*mut raw::c_void>;

fn get_xlib_screen_id(&self) -> Option<*mut libc::c_void>;
fn get_xlib_screen_id(&self) -> Option<raw::c_int>;

fn get_xlib_xconnection(&self) -> Option<Arc<XConnection>>;

fn send_xim_spot(&self, x: i16, y: i16);

/// This function returns the underlying `xcb_connection_t` of an xlib `Display`.
///
/// Returns `None` if the window doesn't use xlib (if it uses wayland for example).
///
/// The pointer will become invalid when the glutin `Window` is destroyed.
fn get_xcb_connection(&self) -> Option<*mut libc::c_void>;
fn get_xcb_connection(&self) -> Option<*mut raw::c_void>;

/// Returns a pointer to the `wl_surface` object of wayland that is used by this window.
///
/// Returns `None` if the window doesn't use wayland (if it uses xlib for example).
///
/// The pointer will become invalid when the glutin `Window` is destroyed.
fn get_wayland_surface(&self) -> Option<*mut libc::c_void>;
fn get_wayland_surface(&self) -> Option<*mut raw::c_void>;

/// Returns a pointer to the `wl_display` object of wayland that is used by this window.
///
/// Returns `None` if the window doesn't use wayland (if it uses xlib for example).
///
/// The pointer will become invalid when the glutin `Window` is destroyed.
fn get_wayland_display(&self) -> Option<*mut libc::c_void>;
fn get_wayland_display(&self) -> Option<*mut raw::c_void>;

/// Check if the window is ready for drawing
///
Expand All @@ -131,22 +129,22 @@ pub trait WindowExt {

impl WindowExt for Window {
#[inline]
fn get_xlib_window(&self) -> Option<*mut libc::c_void> {
fn get_xlib_window(&self) -> Option<raw::c_ulong> {
match self.window {
LinuxWindow::X(ref w) => Some(w.get_xlib_window()),
_ => None
}
}

#[inline]
fn get_xlib_display(&self) -> Option<*mut libc::c_void> {
fn get_xlib_display(&self) -> Option<*mut raw::c_void> {
match self.window {
LinuxWindow::X(ref w) => Some(w.get_xlib_display()),
_ => None
}
}

fn get_xlib_screen_id(&self) -> Option<*mut libc::c_void> {
fn get_xlib_screen_id(&self) -> Option<raw::c_int> {
match self.window {
LinuxWindow::X(ref w) => Some(w.get_xlib_screen_id()),
_ => None
Expand All @@ -160,7 +158,7 @@ impl WindowExt for Window {
}
}

fn get_xcb_connection(&self) -> Option<*mut libc::c_void> {
fn get_xcb_connection(&self) -> Option<*mut raw::c_void> {
match self.window {
LinuxWindow::X(ref w) => Some(w.get_xcb_connection()),
_ => None
Expand All @@ -174,7 +172,7 @@ impl WindowExt for Window {
}

#[inline]
fn get_wayland_surface(&self) -> Option<*mut libc::c_void> {
fn get_wayland_surface(&self) -> Option<*mut raw::c_void> {
use wayland_client::Proxy;
match self.window {
LinuxWindow::Wayland(ref w) => Some(w.get_surface().ptr() as *mut _),
Expand All @@ -183,7 +181,7 @@ impl WindowExt for Window {
}

#[inline]
fn get_wayland_display(&self) -> Option<*mut libc::c_void> {
fn get_wayland_display(&self) -> Option<*mut raw::c_void> {
use wayland_client::Proxy;
match self.window {
LinuxWindow::Wayland(ref w) => Some(w.get_display().ptr() as *mut _),
Expand Down
20 changes: 10 additions & 10 deletions src/platform/linux/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use libc;
use std::borrow::Borrow;
use std::{mem, cmp};
use std::sync::{Arc, Mutex};
use std::os::raw::{c_int, c_long, c_uchar};
use std::os::raw::{c_int, c_long, c_uchar, c_ulong, c_void};
use std::thread;
use std::time::Duration;

Expand Down Expand Up @@ -537,13 +537,13 @@ impl Window2 {
}

#[inline]
pub fn get_xlib_display(&self) -> *mut libc::c_void {
self.x.display.display as *mut libc::c_void
pub fn get_xlib_display(&self) -> *mut c_void {
self.x.display.display as _
}

#[inline]
pub fn get_xlib_screen_id(&self) -> *mut libc::c_void {
self.x.screen_id as *mut libc::c_void
pub fn get_xlib_screen_id(&self) -> c_int {
self.x.screen_id
}

#[inline]
Expand All @@ -553,20 +553,20 @@ impl Window2 {

#[inline]
pub fn platform_display(&self) -> *mut libc::c_void {
self.x.display.display as *mut libc::c_void
self.x.display.display as _
}

#[inline]
pub fn get_xlib_window(&self) -> *mut libc::c_void {
self.x.window as *mut libc::c_void
pub fn get_xlib_window(&self) -> c_ulong {
self.x.window
}

#[inline]
pub fn platform_window(&self) -> *mut libc::c_void {
self.x.window as *mut libc::c_void
self.x.window as _
}

pub fn get_xcb_connection(&self) -> *mut libc::c_void {
pub fn get_xcb_connection(&self) -> *mut c_void {
unsafe {
(self.x.display.xlib_xcb.XGetXCBConnection)(self.get_xlib_display() as *mut _) as *mut _
}
Expand Down

0 comments on commit 32d76ef

Please sign in to comment.