Change X11 events based on multitouch option

* If `multitouch` is enabled, touch input generates touch events.
* If `multitouch` is disabled, touch input generates emulated mouse events.
This commit is contained in:
Matt Brubeck 2015-10-22 16:29:48 -07:00
parent 6cefaa12be
commit 8c0dfe19c2
2 changed files with 20 additions and 4 deletions

View file

@ -5,6 +5,8 @@ use std::{mem, ptr};
use std::ffi::CString; use std::ffi::CString;
use std::slice::from_raw_parts; use std::slice::from_raw_parts;
use WindowAttributes;
use events::Event; use events::Event;
use super::{events, ffi}; use super::{events, ffi};
@ -46,11 +48,13 @@ pub struct XInputEventHandler {
window: ffi::Window, window: ffi::Window,
ic: ffi::XIC, ic: ffi::XIC,
axis_list: Vec<Axis>, axis_list: Vec<Axis>,
current_state: InputState current_state: InputState,
multitouch: bool,
} }
impl XInputEventHandler { impl XInputEventHandler {
pub fn new(display: &Arc<XConnection>, window: ffi::Window, ic: ffi::XIC) -> XInputEventHandler { pub fn new(display: &Arc<XConnection>, window: ffi::Window, ic: ffi::XIC,
window_attrs: &WindowAttributes) -> XInputEventHandler {
// query XInput support // query XInput support
let mut opcode: libc::c_int = 0; let mut opcode: libc::c_int = 0;
let mut event: libc::c_int = 0; let mut event: libc::c_int = 0;
@ -113,7 +117,8 @@ impl XInputEventHandler {
current_state: InputState { current_state: InputState {
cursor_pos: (0.0, 0.0), cursor_pos: (0.0, 0.0),
axis_values: Vec::new() axis_values: Vec::new()
} },
multitouch: window_attrs.multitouch,
} }
} }
@ -174,6 +179,10 @@ impl XInputEventHandler {
match cookie.evtype { match cookie.evtype {
ffi::XI_ButtonPress | ffi::XI_ButtonRelease => { ffi::XI_ButtonPress | ffi::XI_ButtonRelease => {
let event_data: &ffi::XIDeviceEvent = unsafe{mem::transmute(cookie.data)}; let event_data: &ffi::XIDeviceEvent = unsafe{mem::transmute(cookie.data)};
if self.multitouch && (event_data.flags & ffi::XIPointerEmulated) != 0 {
// Deliver multi-touch events instead of emulated mouse events.
return None
}
let state = if cookie.evtype == ffi::XI_ButtonPress { let state = if cookie.evtype == ffi::XI_ButtonPress {
Pressed Pressed
} else { } else {
@ -205,6 +214,10 @@ impl XInputEventHandler {
}, },
ffi::XI_Motion => { ffi::XI_Motion => {
let event_data: &ffi::XIDeviceEvent = unsafe{mem::transmute(cookie.data)}; let event_data: &ffi::XIDeviceEvent = unsafe{mem::transmute(cookie.data)};
if self.multitouch && (event_data.flags & ffi::XIPointerEmulated) != 0 {
// Deliver multi-touch events instead of emulated mouse events.
return None
}
let axis_state = event_data.valuators; let axis_state = event_data.valuators;
let mask = unsafe{ from_raw_parts(axis_state.mask, axis_state.mask_len as usize) }; let mask = unsafe{ from_raw_parts(axis_state.mask, axis_state.mask_len as usize) };
let mut axis_count = 0; let mut axis_count = 0;
@ -246,6 +259,9 @@ impl XInputEventHandler {
ffi::XI_FocusIn => Some(Focused(true)), ffi::XI_FocusIn => Some(Focused(true)),
ffi::XI_FocusOut => Some(Focused(false)), ffi::XI_FocusOut => Some(Focused(false)),
ffi::XI_TouchBegin | ffi::XI_TouchUpdate | ffi::XI_TouchEnd => { ffi::XI_TouchBegin | ffi::XI_TouchUpdate | ffi::XI_TouchEnd => {
if !self.multitouch {
return None
}
let event_data: &ffi::XIDeviceEvent = unsafe{mem::transmute(cookie.data)}; let event_data: &ffi::XIDeviceEvent = unsafe{mem::transmute(cookie.data)};
let phase = match cookie.evtype { let phase = match cookie.evtype {
ffi::XI_TouchBegin => TouchPhase::Started, ffi::XI_TouchBegin => TouchPhase::Started,

View file

@ -562,7 +562,7 @@ impl Window {
current_size: Cell::new((0, 0)), current_size: Cell::new((0, 0)),
pending_events: Mutex::new(VecDeque::new()), pending_events: Mutex::new(VecDeque::new()),
cursor_state: Mutex::new(CursorState::Normal), cursor_state: Mutex::new(CursorState::Normal),
input_handler: Mutex::new(XInputEventHandler::new(display, window, ic)) input_handler: Mutex::new(XInputEventHandler::new(display, window, ic, window_attrs))
}; };
// returning // returning