From 1dcaf2973826b8a2b4e1182317fc3b69fab12d2a Mon Sep 17 00:00:00 2001 From: Simon Wollwage Date: Sat, 22 Nov 2014 19:38:04 +0100 Subject: [PATCH 1/2] updated XClientMessageEvent union-like functionality to act according to format flag --- src/xlib.rs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/xlib.rs b/src/xlib.rs index d1faa7f..28a4a75 100644 --- a/src/xlib.rs +++ b/src/xlib.rs @@ -733,7 +733,44 @@ pub struct XClientMessageEvent { pub window: Window, pub message_type: Atom, pub format: c_int, - pub data: union_unnamed2, + pub data: [int32_t, ..5], +} + +impl XClientMessageEvent { + pub fn get_b(&self) -> Option<&[int8_t]> { + match self.format { + 8 => Some(unsafe { mem::transmute_copy(&self.data) }), + _ => None + } + } + + pub fn get_s(&self) -> Option<&[int16_t]> { + match self.format { + 16 => Some(unsafe { mem::transmute_copy(&self.data) }), + _ => None + } + } + + pub fn get_l(&self) -> Option<&[int32_t]> { + match self.format { + 32 => Some(unsafe { mem::transmute_copy(&self.data) }), + _ => None + } + } + + pub fn set_b(&mut self, v: &[int8_t]) { + self.format = 8; + self.data = unsafe { mem::transmute_copy(&v) }; + } + + pub fn set_s(&mut self, v: &[int16_t]) { + self.format = 16; + self.data = unsafe { mem::transmute_copy(&v) }; + } + pub fn set_l(&mut self, v: &[int32_t]) { + self.format = 32; + self.data = unsafe { mem::transmute_copy(&v) }; + } } #[repr(C)] From ccf1c05cf6a0a6b16590713b32e4571958b71f13 Mon Sep 17 00:00:00 2001 From: Simon Wollwage Date: Sat, 22 Nov 2014 19:38:04 +0100 Subject: [PATCH 2/2] updated XClientMessageEvent union-like functionality to act according to format flag --- Cargo.lock | 4 ++++ src/xlib.rs | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 Cargo.lock diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..106d52c --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "xlib" +version = "0.1.0" + diff --git a/src/xlib.rs b/src/xlib.rs index d1faa7f..d999b68 100644 --- a/src/xlib.rs +++ b/src/xlib.rs @@ -11,6 +11,7 @@ #![allow(non_camel_case_types)] use libc::*; +use std::mem; pub type XID = c_ulong; @@ -733,7 +734,44 @@ pub struct XClientMessageEvent { pub window: Window, pub message_type: Atom, pub format: c_int, - pub data: union_unnamed2, + pub data: [int32_t, ..5], +} + +impl XClientMessageEvent { + pub fn get_b(&self) -> Option<&[int8_t]> { + match self.format { + 8 => Some(unsafe { mem::transmute_copy(&self.data) }), + _ => None + } + } + + pub fn get_s(&self) -> Option<&[int16_t]> { + match self.format { + 16 => Some(unsafe { mem::transmute_copy(&self.data) }), + _ => None + } + } + + pub fn get_l(&self) -> Option<&[int32_t]> { + match self.format { + 32 => Some(unsafe { mem::transmute_copy(&self.data) }), + _ => None + } + } + + pub fn set_b(&mut self, v: &[int8_t]) { + self.format = 8; + self.data = unsafe { mem::transmute_copy(&v) }; + } + + pub fn set_s(&mut self, v: &[int16_t]) { + self.format = 16; + self.data = unsafe { mem::transmute_copy(&v) }; + } + pub fn set_l(&mut self, v: &[int32_t]) { + self.format = 32; + self.data = unsafe { mem::transmute_copy(&v) }; + } } #[repr(C)]