Skip to content

Commit 41f67ea

Browse files
committed
Another pass at no_panic on Windows/MacOS/Linux.
1 parent 855f6b0 commit 41f67ea

File tree

8 files changed

+55
-33
lines changed

8 files changed

+55
-33
lines changed

examples/display.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rdev::display_size;
22
fn main() {
3-
let (w, h) = display_size();
3+
let (w, h) = display_size().unwrap();
44

55
println!("Your screen is {:?}x{:?}", w, h);
66
}

src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
//! ```no_run
6363
//! use rdev::{display_size};
6464
//!
65-
//! let (w, h) = display_size();
65+
//! let (w, h) = display_size().unwrap();
6666
//! assert!(w > 0);
6767
//! assert!(h > 0);
6868
//! ```
@@ -82,7 +82,8 @@
8282
//! Serialization and deserialization. (Requires `serialize` feature).
8383
mod rdev;
8484
pub use crate::rdev::{
85-
Button, Callback, Event, EventType, GrabCallback, GrabError, Key, ListenError, SimulateError,
85+
Button, Callback, DisplayError, Event, EventType, GrabCallback, GrabError, Key, ListenError,
86+
SimulateError,
8687
};
8788

8889
#[cfg(target_os = "macos")]
@@ -169,10 +170,10 @@ pub fn simulate(event_type: &EventType) -> Result<(), SimulateError> {
169170
/// ```no_run
170171
/// use rdev::{display_size};
171172
///
172-
/// let (w, h )= display_size();
173+
/// let (w, h) = display_size().unwrap();
173174
/// println!("My screen size : {:?}x{:?}", w, h);
174175
/// ```
175-
pub fn display_size() -> (u64, u64) {
176+
pub fn display_size() -> Result<(u64, u64), DisplayError> {
176177
_display_size()
177178
}
178179

src/linux/common.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::linux::keyboard_state::KeyboardState;
22
use crate::linux::keycodes::key_from_code;
33
use crate::rdev::{Button, Event, EventType};
4+
use std::convert::TryInto;
45
use std::os::raw::{c_int, c_uchar, c_uint};
56
use std::ptr::null;
67
use std::time::SystemTime;
@@ -67,29 +68,39 @@ pub fn convert(code: c_uint, state: c_uint, type_: c_int, x: f64, y: f64) -> Opt
6768
}
6869
}
6970

70-
struct Display {
71-
display: i32,
71+
pub struct Display {
72+
display: *mut xlib::Display,
7273
}
7374

7475
impl Display {
75-
fn new() -> Option<Display> {
76-
let display = xlib::XOpenDisplay(null());
77-
if display.is_null() {
78-
return None;
76+
pub fn new() -> Option<Display> {
77+
unsafe {
78+
let display = xlib::XOpenDisplay(null());
79+
if display.is_null() {
80+
return None;
81+
}
82+
Some(Display { display })
7983
}
80-
Some(Display { display })
8184
}
8285

83-
fn get_screen() -> Option<Screen> {
84-
let screen_ptr = xlib::XDefaultScreenOfDisplay(dpy);
85-
if screen_ptr.is_null() {
86-
return None;
86+
pub fn get_size(&self) -> Option<(u64, u64)> {
87+
unsafe {
88+
let screen_ptr = xlib::XDefaultScreenOfDisplay(self.display);
89+
if screen_ptr.is_null() {
90+
return None;
91+
}
92+
let screen = *screen_ptr;
93+
Some((
94+
screen.width.try_into().ok()?,
95+
screen.height.try_into().ok()?,
96+
))
8797
}
88-
Some(*screen_ptr);
8998
}
9099
}
91100
impl Drop for Display {
92-
fn drop(&self) {
93-
xlib::XCloseDisplay(self.display)
101+
fn drop(&mut self) {
102+
unsafe {
103+
xlib::XCloseDisplay(self.display);
104+
}
94105
}
95106
}

src/linux/display.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
use crate::linux::common::Display;
22
use crate::rdev::DisplayError;
3-
use std::convert::TryInto;
43

54
pub fn display_size() -> Result<(u64, u64), DisplayError> {
6-
let dpy = Display::new().map_err(|| DisplayError::NoDisplay)?;
7-
let screen = dpy.get_screen()?;
8-
Ok(
9-
screen.width.try_into().unwrap(),
10-
screen.height.try_into().unwrap(),
11-
)
5+
let display = Display::new().ok_or(DisplayError::NoDisplay)?;
6+
display.get_size().ok_or(DisplayError::NoDisplay)
127
}

src/macos/display.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use crate::rdev::DisplayError;
12
use core_graphics::display::CGDisplay;
23

3-
pub fn display_size() -> (u64, u64) {
4+
pub fn display_size() -> Result<(u64, u64), DisplayError> {
45
let main = CGDisplay::main();
5-
(main.pixels_wide(), main.pixels_high())
6+
Ok((main.pixels_wide(), main.pixels_high()))
67
}

src/rdev.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ pub enum GrabError {
4040
LinuxNotSupported,
4141
}
4242
/// Errors that occur when trying to get display size.
43+
#[non_exhaustive]
4344
#[derive(Debug)]
4445
pub enum DisplayError {
45-
MissingDisplayError,
46+
NoDisplay,
47+
ConversionError,
4648
}
4749

4850
/// Marking an error when we tried to simulate and event

src/windows/display.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
use crate::rdev::DisplayError;
12
use std::convert::TryInto;
23
use winapi::um::winuser::{GetSystemMetrics, SM_CXSCREEN, SM_CYSCREEN};
34

4-
pub fn display_size() -> (u64, u64) {
5-
let w = unsafe { GetSystemMetrics(SM_CXSCREEN).try_into().unwrap() };
6-
let h = unsafe { GetSystemMetrics(SM_CYSCREEN).try_into().unwrap() };
7-
(w, h)
5+
pub fn display_size() -> Result<(u64, u64), DisplayError> {
6+
let w = unsafe {
7+
GetSystemMetrics(SM_CXSCREEN)
8+
.try_into()
9+
.map_err(|_| DisplayError::ConversionError)?
10+
};
11+
let h = unsafe {
12+
GetSystemMetrics(SM_CYSCREEN)
13+
.try_into()
14+
.map_err(|_| DisplayError::ConversionError)?
15+
};
16+
Ok((w, h))
817
}

src/windows/simulate.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ pub fn simulate(event_type: &EventType) -> Result<(), SimulateError> {
118118
EventType::MouseMove { x, y } => {
119119
let width = unsafe { GetSystemMetrics(SM_CXVIRTUALSCREEN) };
120120
let height = unsafe { GetSystemMetrics(SM_CYVIRTUALSCREEN) };
121+
if width == 0 || height == 0 {
122+
return Err(SimulateError);
123+
}
121124

122125
sim_mouse_event(
123126
MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_VIRTUALDESK,

0 commit comments

Comments
 (0)