Skip to content

Commit

Permalink
refactor: Use architecture compatible types for Xlib calls
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Dec 15, 2020
1 parent 246fe3d commit 04fd81e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Use architecture compatible types for Xlib calls

## [1.1.0] - 2020-12-10
### Added
Expand Down
16 changes: 9 additions & 7 deletions src/x11/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use device_query::{DeviceQuery, Keycode};
use std::ffi::CString;
use std::io::{self, Write};
use std::mem::MaybeUninit;
use std::os::raw::{c_int, c_ulong};
use std::ptr;
use std::thread;
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -94,8 +95,8 @@ impl Display {
*/
pub fn get_focused_window(&self) -> Option<Window> {
unsafe {
let mut focus_window = MaybeUninit::<u64>::uninit();
let mut focus_state = MaybeUninit::<i32>::uninit();
let mut focus_window = MaybeUninit::<c_ulong>::uninit();
let mut focus_state = MaybeUninit::<c_int>::uninit();
xlib::XGetInputFocus(
self.inner,
focus_window.as_mut_ptr(),
Expand All @@ -122,7 +123,7 @@ impl Display {
* @param focus_state
*/
#[allow(dead_code)]
pub fn set_focused_window(&self, xid: u64, focus_state: i32) {
pub fn set_focused_window(&self, xid: c_ulong, focus_state: c_int) {
unsafe {
xlib::XSetInputFocus(self.inner, xid, focus_state, xlib::CurrentTime)
};
Expand All @@ -149,9 +150,9 @@ impl Display {
* Get the corresponding key symbol from keycode.
*
* @param keycode
* @return u64
* @return c_ulong
*/
fn get_symbol_from_keycode(&self, keycode: &Keycode) -> u64 {
fn get_symbol_from_keycode(&self, keycode: &Keycode) -> c_ulong {
let mut key = format!("{:?}", keycode)
.trim_start_matches("Key")
.to_string();
Expand Down Expand Up @@ -365,14 +366,15 @@ mod tests {
assert_eq!(
u64::try_from(keysym::XK_Alt_L).unwrap(),
display.get_symbol_from_keycode(&input_state.action_keys.main_key)
as u64
);
assert_eq!(
u64::try_from(keysym::XK_Control_R).unwrap(),
display.get_symbol_from_keycode(&Keycode::RControl)
display.get_symbol_from_keycode(&Keycode::RControl) as u64
);
assert_eq!(
u64::try_from(keysym::XK_X).unwrap(),
display.get_symbol_from_keycode(&Keycode::X)
display.get_symbol_from_keycode(&Keycode::X) as u64
);
display.get_root_window().release();
}
Expand Down
25 changes: 15 additions & 10 deletions src/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::ffi::CString;
use std::fmt;
use std::io::{self, Write};
use std::mem::MaybeUninit;
use std::os::raw::{c_char, c_uint, c_ulong};
use std::ptr;
use std::slice;
use x11::xlib;
Expand All @@ -23,7 +24,7 @@ const BORDER_PADDING: u32 = 1;
/* X11 window id, geometric properties and its display */
#[derive(Clone, Copy, Debug)]
pub struct Window {
pub xid: u64,
pub xid: c_ulong,
display: Display,
gc: xlib::GC,
pub geometry: Geometry,
Expand Down Expand Up @@ -55,7 +56,7 @@ impl Window {
* @param display
* @return Window
*/
pub fn new(xid: u64, display: Display) -> Self {
pub fn new(xid: c_ulong, display: Display) -> Self {
unsafe {
Self {
xid,
Expand Down Expand Up @@ -111,7 +112,11 @@ impl Window {
*/
unsafe fn get_gc(&self) -> xlib::GC {
let gc = xlib::XCreateGC(self.display.inner, self.xid, 0, ptr::null_mut());
xlib::XSetForeground(self.display.inner, gc, self.display.settings.color);
xlib::XSetForeground(
self.display.inner,
gc,
self.display.settings.color as c_ulong,
);
xlib::XSetLineAttributes(
self.display.inner,
gc,
Expand Down Expand Up @@ -142,13 +147,13 @@ impl Window {
* @return Window (Option)
*/
pub unsafe fn get_parent(&self) -> Option<Self> {
let mut root = MaybeUninit::<u64>::uninit();
let mut parent = MaybeUninit::<u64>::uninit();
let mut children = MaybeUninit::<*mut u64>::uninit();
let mut nchildren = MaybeUninit::<u32>::uninit();
let mut root = MaybeUninit::<c_ulong>::uninit();
let mut parent = MaybeUninit::<c_ulong>::uninit();
let mut children = MaybeUninit::<*mut c_ulong>::uninit();
let mut nchildren = MaybeUninit::<c_uint>::uninit();
if xlib::XQueryTree(
self.display.inner,
self.xid as u64,
self.xid,
root.as_mut_ptr(),
parent.as_mut_ptr(),
children.as_mut_ptr(),
Expand All @@ -168,7 +173,7 @@ impl Window {
*/
pub fn get_name(&self) -> Option<String> {
unsafe {
let mut window_name = MaybeUninit::<*mut i8>::uninit();
let mut window_name = MaybeUninit::<*mut c_char>::uninit();
if xlib::XFetchName(
self.display.inner,
self.xid,
Expand Down Expand Up @@ -315,7 +320,7 @@ impl Window {
*
* @param key
*/
pub fn grab_key(&self, key: u64) {
pub fn grab_key(&self, key: c_ulong) {
unsafe {
xlib::XGrabKey(
self.display.inner,
Expand Down

0 comments on commit 04fd81e

Please sign in to comment.