Skip to content

Commit 43860ab

Browse files
committed
Fix a whackton of warnings and clippy lints
The things remaining are all of the cursor things in the X11 implementation (there's _a lot_ of it, so there's probably a reason why it's all still there but unused), and the super unsound immutable reference to mutable reference cast in the macOS implementation that Clippy automatically errors out on.
1 parent d835175 commit 43860ab

File tree

9 files changed

+46
-148
lines changed

9 files changed

+46
-148
lines changed

src/gl/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use raw_window_handle::HasRawWindowHandle;
2-
31
use std::ffi::c_void;
42
use std::marker::PhantomData;
53

4+
// On X11 creating the context is a two step process
5+
#[cfg(not(target_os = "linux"))]
6+
use raw_window_handle::HasRawWindowHandle;
7+
68
#[cfg(target_os = "windows")]
79
mod win;
810
#[cfg(target_os = "windows")]

src/gl/win.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl GlContext {
255255

256256
let hglrc =
257257
wglCreateContextAttribsARB.unwrap()(hdc, std::ptr::null_mut(), ctx_attribs.as_ptr());
258-
if hglrc == std::ptr::null_mut() {
258+
if hglrc.is_null() {
259259
return Err(GlError::CreationFailed(()));
260260
}
261261

src/keyboard.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
//! Keyboard types.
1919
20+
#[cfg(any(target_os = "linux", target_os = "macos"))]
2021
use keyboard_types::{Code, Location};
2122

2223
#[cfg(any(target_os = "linux", target_os = "macos"))]

src/macos/window.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub struct WindowHandle {
4444

4545
impl WindowHandle {
4646
pub fn close(&mut self) {
47-
if let Some(_) = self.raw_window_handle.take() {
47+
if self.raw_window_handle.take().is_some() {
4848
self.close_requested.store(true, Ordering::Relaxed);
4949
}
5050
}

src/win/keyboard.rs

Lines changed: 10 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
2020
use std::cmp::Ordering;
2121
use std::collections::{HashMap, HashSet};
22-
use std::convert::TryInto;
2322
use std::mem;
2423
use std::ops::RangeInclusive;
2524

@@ -29,15 +28,15 @@ use winapi::shared::minwindef::{HKL, INT, LPARAM, UINT, WPARAM};
2928
use winapi::shared::ntdef::SHORT;
3029
use winapi::shared::windef::HWND;
3130
use winapi::um::winuser::{
32-
GetKeyState, GetKeyboardLayout, MapVirtualKeyExW, PeekMessageW, ToUnicodeEx, VkKeyScanW,
33-
MAPVK_VK_TO_CHAR, MAPVK_VSC_TO_VK_EX, PM_NOREMOVE, VK_ACCEPT, VK_ADD, VK_APPS, VK_ATTN,
34-
VK_BACK, VK_BROWSER_BACK, VK_BROWSER_FAVORITES, VK_BROWSER_FORWARD, VK_BROWSER_HOME,
35-
VK_BROWSER_REFRESH, VK_BROWSER_SEARCH, VK_BROWSER_STOP, VK_CANCEL, VK_CAPITAL, VK_CLEAR,
36-
VK_CONTROL, VK_CONVERT, VK_CRSEL, VK_DECIMAL, VK_DELETE, VK_DIVIDE, VK_DOWN, VK_END, VK_EREOF,
37-
VK_ESCAPE, VK_EXECUTE, VK_EXSEL, VK_F1, VK_F10, VK_F11, VK_F12, VK_F2, VK_F3, VK_F4, VK_F5,
38-
VK_F6, VK_F7, VK_F8, VK_F9, VK_FINAL, VK_HELP, VK_HOME, VK_INSERT, VK_JUNJA, VK_KANA, VK_KANJI,
39-
VK_LAUNCH_APP1, VK_LAUNCH_APP2, VK_LAUNCH_MAIL, VK_LAUNCH_MEDIA_SELECT, VK_LCONTROL, VK_LEFT,
40-
VK_LMENU, VK_LSHIFT, VK_LWIN, VK_MEDIA_NEXT_TRACK, VK_MEDIA_PLAY_PAUSE, VK_MEDIA_PREV_TRACK,
31+
GetKeyState, GetKeyboardLayout, MapVirtualKeyExW, PeekMessageW, ToUnicodeEx, MAPVK_VK_TO_CHAR,
32+
MAPVK_VSC_TO_VK_EX, PM_NOREMOVE, VK_ACCEPT, VK_ADD, VK_APPS, VK_ATTN, VK_BACK, VK_BROWSER_BACK,
33+
VK_BROWSER_FAVORITES, VK_BROWSER_FORWARD, VK_BROWSER_HOME, VK_BROWSER_REFRESH,
34+
VK_BROWSER_SEARCH, VK_BROWSER_STOP, VK_CANCEL, VK_CAPITAL, VK_CLEAR, VK_CONTROL, VK_CONVERT,
35+
VK_CRSEL, VK_DECIMAL, VK_DELETE, VK_DIVIDE, VK_DOWN, VK_END, VK_EREOF, VK_ESCAPE, VK_EXECUTE,
36+
VK_EXSEL, VK_F1, VK_F10, VK_F11, VK_F12, VK_F2, VK_F3, VK_F4, VK_F5, VK_F6, VK_F7, VK_F8,
37+
VK_F9, VK_FINAL, VK_HELP, VK_HOME, VK_INSERT, VK_JUNJA, VK_KANA, VK_KANJI, VK_LAUNCH_APP1,
38+
VK_LAUNCH_APP2, VK_LAUNCH_MAIL, VK_LAUNCH_MEDIA_SELECT, VK_LCONTROL, VK_LEFT, VK_LMENU,
39+
VK_LSHIFT, VK_LWIN, VK_MEDIA_NEXT_TRACK, VK_MEDIA_PLAY_PAUSE, VK_MEDIA_PREV_TRACK,
4140
VK_MEDIA_STOP, VK_MENU, VK_MODECHANGE, VK_MULTIPLY, VK_NEXT, VK_NONCONVERT, VK_NUMLOCK,
4241
VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3, VK_NUMPAD4, VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7,
4342
VK_NUMPAD8, VK_NUMPAD9, VK_OEM_ATTN, VK_OEM_CLEAR, VK_PAUSE, VK_PLAY, VK_PRINT, VK_PRIOR,
@@ -344,110 +343,6 @@ fn vk_to_key(vk: VkCode) -> Option<Key> {
344343
})
345344
}
346345

347-
/// Convert a key to a virtual key code.
348-
///
349-
/// The virtual key code is needed in various winapi interfaces, including
350-
/// accelerators. This provides the virtual key code in the current keyboard
351-
/// map.
352-
///
353-
/// The virtual key code can have modifiers in the higher order byte when the
354-
/// argument is a `Character` variant. See:
355-
/// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-vkkeyscanw
356-
pub(crate) fn key_to_vk(key: &Key) -> Option<i32> {
357-
Some(match key {
358-
Key::Character(s) => {
359-
if let Some(code_point) = s.chars().next() {
360-
if let Ok(wchar) = (code_point as u32).try_into() {
361-
unsafe { VkKeyScanW(wchar) as i32 }
362-
} else {
363-
return None;
364-
}
365-
} else {
366-
return None;
367-
}
368-
}
369-
Key::Cancel => VK_CANCEL,
370-
Key::Backspace => VK_BACK,
371-
Key::Tab => VK_TAB,
372-
Key::Clear => VK_CLEAR,
373-
Key::Enter => VK_RETURN,
374-
Key::Shift => VK_SHIFT,
375-
Key::Control => VK_CONTROL,
376-
Key::Alt => VK_MENU,
377-
Key::Pause => VK_PAUSE,
378-
Key::CapsLock => VK_CAPITAL,
379-
// TODO: disambiguate kana and hangul? same vk
380-
Key::KanaMode => VK_KANA,
381-
Key::JunjaMode => VK_JUNJA,
382-
Key::FinalMode => VK_FINAL,
383-
Key::KanjiMode => VK_KANJI,
384-
Key::Escape => VK_ESCAPE,
385-
Key::NonConvert => VK_NONCONVERT,
386-
Key::Accept => VK_ACCEPT,
387-
Key::PageUp => VK_PRIOR,
388-
Key::PageDown => VK_NEXT,
389-
Key::End => VK_END,
390-
Key::Home => VK_HOME,
391-
Key::ArrowLeft => VK_LEFT,
392-
Key::ArrowUp => VK_UP,
393-
Key::ArrowRight => VK_RIGHT,
394-
Key::ArrowDown => VK_DOWN,
395-
Key::Select => VK_SELECT,
396-
Key::Print => VK_PRINT,
397-
Key::Execute => VK_EXECUTE,
398-
Key::PrintScreen => VK_SNAPSHOT,
399-
Key::Insert => VK_INSERT,
400-
Key::Delete => VK_DELETE,
401-
Key::Help => VK_HELP,
402-
Key::Meta => VK_LWIN,
403-
Key::ContextMenu => VK_APPS,
404-
Key::Standby => VK_SLEEP,
405-
Key::F1 => VK_F1,
406-
Key::F2 => VK_F2,
407-
Key::F3 => VK_F3,
408-
Key::F4 => VK_F4,
409-
Key::F5 => VK_F5,
410-
Key::F6 => VK_F6,
411-
Key::F7 => VK_F7,
412-
Key::F8 => VK_F8,
413-
Key::F9 => VK_F9,
414-
Key::F10 => VK_F10,
415-
Key::F11 => VK_F11,
416-
Key::F12 => VK_F12,
417-
Key::NumLock => VK_NUMLOCK,
418-
Key::ScrollLock => VK_SCROLL,
419-
Key::BrowserBack => VK_BROWSER_BACK,
420-
Key::BrowserForward => VK_BROWSER_FORWARD,
421-
Key::BrowserRefresh => VK_BROWSER_REFRESH,
422-
Key::BrowserStop => VK_BROWSER_STOP,
423-
Key::BrowserSearch => VK_BROWSER_SEARCH,
424-
Key::BrowserFavorites => VK_BROWSER_FAVORITES,
425-
Key::BrowserHome => VK_BROWSER_HOME,
426-
Key::AudioVolumeMute => VK_VOLUME_MUTE,
427-
Key::AudioVolumeDown => VK_VOLUME_DOWN,
428-
Key::AudioVolumeUp => VK_VOLUME_UP,
429-
Key::MediaTrackNext => VK_MEDIA_NEXT_TRACK,
430-
Key::MediaTrackPrevious => VK_MEDIA_PREV_TRACK,
431-
Key::MediaStop => VK_MEDIA_STOP,
432-
Key::MediaPlayPause => VK_MEDIA_PLAY_PAUSE,
433-
Key::LaunchMail => VK_LAUNCH_MAIL,
434-
Key::LaunchMediaPlayer => VK_LAUNCH_MEDIA_SELECT,
435-
Key::LaunchApplication1 => VK_LAUNCH_APP1,
436-
Key::LaunchApplication2 => VK_LAUNCH_APP2,
437-
Key::Alphanumeric => VK_OEM_ATTN,
438-
Key::Convert => VK_CONVERT,
439-
Key::ModeChange => VK_MODECHANGE,
440-
Key::Process => VK_PROCESSKEY,
441-
Key::Attn => VK_ATTN,
442-
Key::CrSel => VK_CRSEL,
443-
Key::ExSel => VK_EXSEL,
444-
Key::EraseEof => VK_EREOF,
445-
Key::Play => VK_PLAY,
446-
Key::ZoomToggle => VK_ZOOM,
447-
_ => return None,
448-
})
449-
}
450-
451346
fn code_unit_to_key(code_unit: u32) -> Key {
452347
match code_unit {
453348
0x8 | 0x7F => Key::Backspace,
@@ -692,6 +587,7 @@ impl KeyboardState {
692587
key_state[VK_LCONTROL as usize] = if has_altgr { 0x80 } else { 0 };
693588
key_state[VK_MENU as usize] = if has_altgr { 0x80 } else { 0 };
694589
key_state[VK_RMENU as usize] = if has_altgr { 0x80 } else { 0 };
590+
#[allow(clippy::iter_overeager_cloned)]
695591
for vk in PRINTABLE_VKS.iter().cloned().flatten() {
696592
let ret = ToUnicodeEx(
697593
vk as UINT,

src/win/window.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,8 @@ unsafe extern "system" fn wnd_proc(
205205
}
206206
}
207207
WM_TIMER => {
208-
match wparam {
209-
WIN_FRAME_TIMER => {
210-
window_state.handler.on_frame(&mut window);
211-
}
212-
_ => (),
208+
if wparam == WIN_FRAME_TIMER {
209+
window_state.handler.on_frame(&mut window);
213210
}
214211
return 0;
215212
}
@@ -414,8 +411,8 @@ impl Window {
414411
break;
415412
}
416413

417-
TranslateMessage(&mut msg);
418-
DispatchMessageW(&mut msg);
414+
TranslateMessage(&msg);
415+
DispatchMessageW(&msg);
419416
}
420417
}
421418
}

src/x11/cursor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,5 @@ pub(super) fn get_xcursor(display: *mut x11::xlib::Display, cursor: MouseCursor)
101101
MouseCursor::RowResize => loadn(&[b"split_v\0", b"v_double_arrow\0"]),
102102
};
103103

104-
cursor.or(load(b"left_ptr\0")).unwrap_or(0)
104+
cursor.or_else(|| load(b"left_ptr\0")).unwrap_or(0)
105105
}

src/x11/window.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct WindowHandle {
3535

3636
impl WindowHandle {
3737
pub fn close(&mut self) {
38-
if let Some(_) = self.raw_window_handle.take() {
38+
if self.raw_window_handle.take().is_some() {
3939
// FIXME: This will need to be changed from just setting an atomic to somehow
4040
// synchronizing with the window being closed (using a synchronous channel, or
4141
// by joining on the event loop thread).
@@ -96,6 +96,7 @@ pub struct Window {
9696
xcb_connection: XcbConnection,
9797
window_id: u32,
9898
window_info: WindowInfo,
99+
// FIXME: There's all this mouse cursor logic but it's never actually used, is this correct?
99100
mouse_cursor: MouseCursor,
100101

101102
frame_interval: Duration,
@@ -135,7 +136,7 @@ impl Window {
135136

136137
let (parent_handle, mut window_handle) = ParentHandle::new();
137138

138-
let thread = thread::spawn(move || {
139+
thread::spawn(move || {
139140
Self::window_thread(Some(parent_id), options, build, tx.clone(), Some(parent_handle));
140141
});
141142

@@ -155,7 +156,7 @@ impl Window {
155156

156157
let (parent_handle, mut window_handle) = ParentHandle::new();
157158

158-
let thread = thread::spawn(move || {
159+
thread::spawn(move || {
159160
Self::window_thread(None, options, build, tx.clone(), Some(parent_handle));
160161
});
161162

@@ -174,12 +175,14 @@ impl Window {
174175
let (tx, rx) = mpsc::sync_channel::<WindowOpenResult>(1);
175176

176177
let thread = thread::spawn(move || {
177-
Self::window_thread(None, options, build, tx.clone(), None);
178+
Self::window_thread(None, options, build, tx, None);
178179
});
179180

180181
let _ = rx.recv().unwrap().unwrap();
181182

182-
thread.join();
183+
thread.join().unwrap_or_else(|err| {
184+
eprintln!("Window thread panicked: {:#?}", err);
185+
});
183186
}
184187

185188
fn window_thread<H, B>(
@@ -302,16 +305,16 @@ impl Window {
302305
title.as_bytes(),
303306
);
304307

305-
xcb_connection.atoms.wm_protocols.zip(xcb_connection.atoms.wm_delete_window).map(
306-
|(wm_protocols, wm_delete_window)| {
307-
xcb_util::icccm::set_wm_protocols(
308-
&xcb_connection.conn,
309-
window_id,
310-
wm_protocols,
311-
&[wm_delete_window],
312-
);
313-
},
314-
);
308+
if let Some((wm_protocols, wm_delete_window)) =
309+
xcb_connection.atoms.wm_protocols.zip(xcb_connection.atoms.wm_delete_window)
310+
{
311+
xcb_util::icccm::set_wm_protocols(
312+
&xcb_connection.conn,
313+
window_id,
314+
wm_protocols,
315+
&[wm_delete_window],
316+
);
317+
}
315318

316319
xcb_connection.conn.flush();
317320

@@ -636,7 +639,7 @@ impl Window {
636639

637640
handler.on_event(
638641
&mut crate::Window::new(self),
639-
Event::Keyboard(convert_key_press_event(&event)),
642+
Event::Keyboard(convert_key_press_event(event)),
640643
);
641644
}
642645

@@ -645,7 +648,7 @@ impl Window {
645648

646649
handler.on_event(
647650
&mut crate::Window::new(self),
648-
Event::Keyboard(convert_key_release_event(&event)),
651+
Event::Keyboard(convert_key_release_event(event)),
649652
);
650653
}
651654

src/x11/xcb_connection.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use super::cursor;
1111
pub(crate) struct Atoms {
1212
pub wm_protocols: Option<u32>,
1313
pub wm_delete_window: Option<u32>,
14-
pub wm_normal_hints: Option<u32>,
1514
}
1615

1716
pub struct XcbConnection {
@@ -20,6 +19,7 @@ pub struct XcbConnection {
2019

2120
pub(crate) atoms: Atoms,
2221

22+
// FIXME: Same here, there's a ton of unused cursor machinery in here
2323
pub(super) cursor_cache: HashMap<MouseCursor, u32>,
2424
}
2525

@@ -46,14 +46,13 @@ impl XcbConnection {
4646

4747
conn.set_event_queue_owner(xcb::base::EventQueueOwner::Xcb);
4848

49-
let (wm_protocols, wm_delete_window, wm_normal_hints) =
50-
intern_atoms!(&conn, WM_PROTOCOLS, WM_DELETE_WINDOW, WM_NORMAL_HINTS);
49+
let (wm_protocols, wm_delete_window) = intern_atoms!(&conn, WM_PROTOCOLS, WM_DELETE_WINDOW);
5150

5251
Ok(Self {
5352
conn,
5453
xlib_display,
5554

56-
atoms: Atoms { wm_protocols, wm_delete_window, wm_normal_hints },
55+
atoms: Atoms { wm_protocols, wm_delete_window },
5756

5857
cursor_cache: HashMap::new(),
5958
})
@@ -136,7 +135,7 @@ impl XcbConnection {
136135

137136
#[inline]
138137
pub fn get_scaling(&self) -> Option<f64> {
139-
self.get_scaling_xft().or(self.get_scaling_screen_dimensions())
138+
self.get_scaling_xft().or_else(|| self.get_scaling_screen_dimensions())
140139
}
141140

142141
#[inline]

0 commit comments

Comments
 (0)