comparison src/main.rs @ 1:6dbe2bbeef70

Improve input event types with helper functions.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 09 Oct 2020 00:18:05 +0200
parents 816237b684ea
children 39f87ef69b2a
comparison
equal deleted inserted replaced
0:816237b684ea 1:6dbe2bbeef70
6 use std::io::ErrorKind; 6 use std::io::ErrorKind;
7 use std::sync::{Arc, Mutex}; 7 use std::sync::{Arc, Mutex};
8 8
9 use input_linux::{ 9 use input_linux::{
10 sys::input_event, sys::timeval, AbsoluteAxis, AbsoluteInfo, AbsoluteInfoSetup, EventKind, 10 sys::input_event, sys::timeval, AbsoluteAxis, AbsoluteInfo, AbsoluteInfoSetup, EventKind,
11 InputId, InputProperty, Key, MiscKind, UInputHandle, 11 InputId, InputProperty, Key, MiscKind, SynchronizeKind, UInputHandle,
12 }; 12 };
13 13
14 const WIDTH: i32 = 320; 14 const WIDTH: i32 = 320;
15 const HEIGHT: i32 = 180; 15 const HEIGHT: i32 = 180;
16 16
161 &[x, y, z, wheel, pressure, distance, tilt_x, tilt_y, misc], 161 &[x, y, z, wheel, pressure, distance, tilt_x, tilt_y, misc],
162 )?; 162 )?;
163 Ok(dev) 163 Ok(dev)
164 } 164 }
165 165
166 fn input_event_new(type_: EventKind, code: AbsoluteAxis, value: i32) -> input_event { 166 fn input_event_new(type_: EventKind, code: u16, value: i32) -> input_event {
167 input_event { 167 input_event {
168 time: timeval { 168 time: timeval {
169 tv_sec: 0, 169 tv_sec: 0,
170 tv_usec: 0, 170 tv_usec: 0,
171 }, 171 },
172 type_: type_ as u16, 172 type_: type_ as u16,
173 code: code as u16, 173 code,
174 value, 174 value,
175 } 175 }
176 }
177
178 fn input_axis_new(code: AbsoluteAxis, value: i32) -> input_event {
179 input_event_new(EventKind::Absolute, code as u16, value)
180 }
181
182 fn input_misc_new(code: MiscKind, value: i32) -> input_event {
183 input_event_new(EventKind::Absolute, code as u16, value)
184 }
185
186 fn input_synchronize_new(code: SynchronizeKind, value: i32) -> input_event {
187 input_event_new(EventKind::Absolute, code as u16, value)
176 } 188 }
177 189
178 fn build_ui(application: &gtk::Application) { 190 fn build_ui(application: &gtk::Application) {
179 let dev = match create_uinput_device() { 191 let dev = match create_uinput_device() {
180 Ok(dev) => Arc::new(dev), 192 Ok(dev) => Arc::new(dev),
272 println!("press tool {} at {}, {}", tool_name.lock().unwrap(), x, y); 284 println!("press tool {} at {}, {}", tool_name.lock().unwrap(), x, y);
273 let current_size = current_size_weak.upgrade().unwrap(); 285 let current_size = current_size_weak.upgrade().unwrap();
274 let current_size = current_size.lock().unwrap(); 286 let current_size = current_size.lock().unwrap();
275 let (width, height) = *current_size; 287 let (width, height) = *current_size;
276 dev.write(&[ 288 dev.write(&[
277 input_event_new( 289 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / width) as i32),
278 EventKind::Absolute, 290 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / height) as i32),
279 AbsoluteAxis::X, 291 input_axis_new(AbsoluteAxis::Z, 0),
280 (x * MAX_X as f64 / width) as i32, 292 input_axis_new(AbsoluteAxis::Wheel, 0),
281 ), 293 input_axis_new(AbsoluteAxis::Pressure, 1024),
282 input_event_new( 294 input_axis_new(AbsoluteAxis::Distance, 0),
283 EventKind::Absolute, 295 input_axis_new(AbsoluteAxis::TiltX, 16),
284 AbsoluteAxis::Y, 296 input_axis_new(AbsoluteAxis::TiltY, 0),
285 (y * MAX_Y as f64 / height) as i32, 297 input_misc_new(MiscKind::Serial, 0),
286 ), 298 input_synchronize_new(SynchronizeKind::Report, 0),
287 input_event_new(EventKind::Absolute, AbsoluteAxis::Z, 0),
288 input_event_new(EventKind::Absolute, AbsoluteAxis::Wheel, 0),
289 input_event_new(EventKind::Absolute, AbsoluteAxis::Pressure, 1024),
290 input_event_new(EventKind::Absolute, AbsoluteAxis::Distance, 0),
291 input_event_new(EventKind::Absolute, AbsoluteAxis::TiltX, 16),
292 input_event_new(EventKind::Absolute, AbsoluteAxis::TiltY, 0),
293 // TODO: Change the type of the second parameter here.
294 input_event_new(EventKind::Misc, AbsoluteAxis::X, 0),
295 input_event_new(EventKind::Synchronize, AbsoluteAxis::X, 0),
296 ]) 299 ])
297 .unwrap(); 300 .unwrap();
298 Inhibit(false) 301 Inhibit(false)
299 }); 302 });
300 let dev_weak = Arc::downgrade(&dev); 303 let dev_weak = Arc::downgrade(&dev);
313 //println!("release {}, {}", x, y); 316 //println!("release {}, {}", x, y);
314 let current_size = current_size_weak.upgrade().unwrap(); 317 let current_size = current_size_weak.upgrade().unwrap();
315 let current_size = current_size.lock().unwrap(); 318 let current_size = current_size.lock().unwrap();
316 let (width, height) = *current_size; 319 let (width, height) = *current_size;
317 dev.write(&[ 320 dev.write(&[
318 input_event_new( 321 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / width) as i32),
319 EventKind::Absolute, 322 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / height) as i32),
320 AbsoluteAxis::X, 323 input_axis_new(AbsoluteAxis::Z, 0),
321 (x * MAX_X as f64 / width) as i32, 324 input_axis_new(AbsoluteAxis::Wheel, 0),
322 ), 325 input_axis_new(AbsoluteAxis::Pressure, 0),
323 input_event_new( 326 input_axis_new(AbsoluteAxis::Distance, 16),
324 EventKind::Absolute, 327 input_axis_new(AbsoluteAxis::TiltX, 16),
325 AbsoluteAxis::Y, 328 input_axis_new(AbsoluteAxis::TiltY, 0),
326 (y * MAX_Y as f64 / height) as i32, 329 input_misc_new(MiscKind::Serial, 0),
327 ), 330 input_synchronize_new(SynchronizeKind::Report, 0),
328 input_event_new(EventKind::Absolute, AbsoluteAxis::Z, 0),
329 input_event_new(EventKind::Absolute, AbsoluteAxis::Wheel, 0),
330 input_event_new(EventKind::Absolute, AbsoluteAxis::Pressure, 0),
331 input_event_new(EventKind::Absolute, AbsoluteAxis::Distance, 16),
332 input_event_new(EventKind::Absolute, AbsoluteAxis::TiltX, 16),
333 input_event_new(EventKind::Absolute, AbsoluteAxis::TiltY, 0),
334 // TODO: Change the type of the second parameter here.
335 input_event_new(EventKind::Misc, AbsoluteAxis::X, 0),
336 input_event_new(EventKind::Synchronize, AbsoluteAxis::X, 0),
337 ]) 331 ])
338 .unwrap(); 332 .unwrap();
339 Inhibit(false) 333 Inhibit(false)
340 }); 334 });
341 drawing_area.connect_motion_notify_event(move |_, event| { 335 drawing_area.connect_motion_notify_event(move |_, event| {
343 let pressed = pressed.lock().unwrap(); 337 let pressed = pressed.lock().unwrap();
344 //println!("motion {}, {}", x, y); 338 //println!("motion {}, {}", x, y);
345 let current_size = current_size.lock().unwrap(); 339 let current_size = current_size.lock().unwrap();
346 let (width, height) = *current_size; 340 let (width, height) = *current_size;
347 dev.write(&[ 341 dev.write(&[
348 input_event_new( 342 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / width) as i32),
349 EventKind::Absolute, 343 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / height) as i32),
350 AbsoluteAxis::X, 344 input_axis_new(AbsoluteAxis::Z, 0),
351 (x * MAX_X as f64 / width) as i32, 345 input_axis_new(AbsoluteAxis::Wheel, 0),
352 ), 346 input_axis_new(AbsoluteAxis::Pressure, if *pressed { 2048 } else { 0 }),
353 input_event_new( 347 input_axis_new(AbsoluteAxis::Distance, if *pressed { 0 } else { 32 }),
354 EventKind::Absolute, 348 input_axis_new(AbsoluteAxis::TiltX, 16),
355 AbsoluteAxis::Y, 349 input_axis_new(AbsoluteAxis::TiltY, 0),
356 (y * MAX_Y as f64 / height) as i32, 350 input_misc_new(MiscKind::Serial, 0),
357 ), 351 input_synchronize_new(SynchronizeKind::Report, 0),
358 input_event_new(EventKind::Absolute, AbsoluteAxis::Z, 0),
359 input_event_new(EventKind::Absolute, AbsoluteAxis::Wheel, 0),
360 input_event_new(
361 EventKind::Absolute,
362 AbsoluteAxis::Pressure,
363 if *pressed { 2048 } else { 0 },
364 ),
365 input_event_new(
366 EventKind::Absolute,
367 AbsoluteAxis::Distance,
368 if *pressed { 0 } else { 32 },
369 ),
370 input_event_new(EventKind::Absolute, AbsoluteAxis::TiltX, 16),
371 input_event_new(EventKind::Absolute, AbsoluteAxis::TiltY, 0),
372 // TODO: Change the type of the second parameter here.
373 input_event_new(EventKind::Misc, AbsoluteAxis::X, 0),
374 input_event_new(EventKind::Synchronize, AbsoluteAxis::X, 0),
375 ]) 352 ])
376 .unwrap(); 353 .unwrap();
377 Inhibit(false) 354 Inhibit(false)
378 }); 355 });
379 drawing_area.connect_draw(move |_, ctx| { 356 drawing_area.connect_draw(move |_, ctx| {