annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 use gio::prelude::*;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 use gtk::prelude::*;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 use std::env::args;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 use std::fs::{File, OpenOptions};
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 use std::io::ErrorKind;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 use std::sync::{Arc, Mutex};
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 use input_linux::{
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 sys::input_event, sys::timeval, AbsoluteAxis, AbsoluteInfo, AbsoluteInfoSetup, EventKind,
1
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
11 InputId, InputProperty, Key, MiscKind, SynchronizeKind, UInputHandle,
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 const WIDTH: i32 = 320;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 const HEIGHT: i32 = 180;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 const MAX_X: i32 = 69920;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 const MAX_Y: i32 = 39980;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 fn create_uinput_device() -> std::io::Result<UInputHandle<File>> {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 let file = OpenOptions::new().write(true).open("/dev/uinput")?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 let dev = UInputHandle::new(file);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 dev.set_evbit(EventKind::Synchronize)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 dev.set_evbit(EventKind::Key)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 dev.set_evbit(EventKind::Absolute)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 dev.set_evbit(EventKind::Misc)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 dev.set_keybit(Key::ButtonToolPen)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 dev.set_keybit(Key::ButtonToolRubber)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 dev.set_keybit(Key::ButtonToolBrush)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 dev.set_keybit(Key::ButtonToolPencil)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 dev.set_keybit(Key::ButtonToolAirbrush)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 dev.set_keybit(Key::ButtonTouch)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 dev.set_keybit(Key::ButtonStylus)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 dev.set_keybit(Key::ButtonStylus2)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 dev.set_keybit(Key::ButtonStylus3)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 dev.set_mscbit(MiscKind::Serial)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 dev.set_propbit(InputProperty::Direct)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 dev.set_absbit(AbsoluteAxis::X)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 dev.set_absbit(AbsoluteAxis::Y)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 dev.set_absbit(AbsoluteAxis::Z)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 dev.set_absbit(AbsoluteAxis::Wheel)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 dev.set_absbit(AbsoluteAxis::Pressure)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 dev.set_absbit(AbsoluteAxis::Distance)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 dev.set_absbit(AbsoluteAxis::TiltX)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 dev.set_absbit(AbsoluteAxis::TiltY)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 dev.set_absbit(AbsoluteAxis::Misc)?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 let id = InputId {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 bustype: 3,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 vendor: 0x56a,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 product: 0x350,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 version: 0xb,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 let x = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 axis: AbsoluteAxis::X,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 minimum: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 maximum: MAX_X,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 resolution: 200,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 let y = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 axis: AbsoluteAxis::Y,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 minimum: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 maximum: MAX_Y,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 resolution: 200,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 let z = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 axis: AbsoluteAxis::Z,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 minimum: -900,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 maximum: 899,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 resolution: 287,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 let wheel = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 axis: AbsoluteAxis::Wheel,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 minimum: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 maximum: 2047,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 resolution: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 let pressure = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 axis: AbsoluteAxis::Pressure,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 minimum: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 maximum: 8196,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 resolution: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 let distance = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 axis: AbsoluteAxis::Distance,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
115 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 minimum: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
117 maximum: 63,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120 resolution: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123 let tilt_x = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124 axis: AbsoluteAxis::TiltX,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 minimum: -64,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 maximum: 63,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
130 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 resolution: 57,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134 let tilt_y = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135 axis: AbsoluteAxis::TiltY,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
136 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
138 minimum: -64,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139 maximum: 63,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142 resolution: 57,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 let misc = AbsoluteInfoSetup {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 axis: AbsoluteAxis::Misc,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 info: AbsoluteInfo {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148 value: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149 minimum: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150 maximum: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151 fuzz: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152 flat: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153 resolution: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
156
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 dev.create(
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158 &id,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159 b"TabletEmu",
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
160 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161 &[x, y, z, wheel, pressure, distance, tilt_x, tilt_y, misc],
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
162 )?;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163 Ok(dev)
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
164 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
165
1
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
166 fn input_event_new(type_: EventKind, code: u16, value: i32) -> input_event {
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167 input_event {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168 time: timeval {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169 tv_sec: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170 tv_usec: 0,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 },
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 type_: type_ as u16,
1
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
173 code,
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
174 value,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
175 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177
1
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
178 fn input_axis_new(code: AbsoluteAxis, value: i32) -> input_event {
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
179 input_event_new(EventKind::Absolute, code as u16, value)
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
180 }
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
181
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
182 fn input_misc_new(code: MiscKind, value: i32) -> input_event {
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
183 input_event_new(EventKind::Absolute, code as u16, value)
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
184 }
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
185
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
186 fn input_synchronize_new(code: SynchronizeKind, value: i32) -> input_event {
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
187 input_event_new(EventKind::Absolute, code as u16, value)
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
188 }
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
189
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
190 fn build_ui(application: &gtk::Application) {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
191 let dev = match create_uinput_device() {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
192 Ok(dev) => Arc::new(dev),
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
193 Err(err) => {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
194 match err.kind() {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
195 ErrorKind::NotFound => {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
196 eprintln!("Couldn’t find /dev/uinput: {}", err);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
197 eprintln!("Maybe you forgot to `modprobe uinput`?");
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
198 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
199 ErrorKind::PermissionDenied => {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
200 eprintln!("Couldn’t open /dev/uinput for writing: {}", err);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
201 eprintln!("Maybe you aren’t allowed to create input devices?");
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
202 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
203 _ => eprintln!("Couldn’t open /dev/uinput for writing: {}", err),
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
204 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
205 return;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
206 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
207 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
208 println!(
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
209 "New device at {:?} ({:?})",
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
210 dev.evdev_path().unwrap(),
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
211 dev.sys_path().unwrap()
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
212 );
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
213
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
214 let window = gtk::ApplicationWindow::new(application);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
215
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
216 window.set_title("tablet-emu");
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
217 window.set_position(gtk::WindowPosition::Center);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
218
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
219 let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 0);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
220 let tools_box = gtk::Box::new(gtk::Orientation::Vertical, 0);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
221
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
222 let tool_name = Arc::new(Mutex::new(String::from("Pen")));
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
223 let tool1 = gtk::Button::with_label("Pen");
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
224 let tool2 = gtk::Button::with_label("Rubber");
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
225 let tool3 = gtk::Button::with_label("Brush");
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
226 let tool4 = gtk::Button::with_label("Pencil");
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
227 let tool5 = gtk::Button::with_label("Airbrush");
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
228
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
229 macro_rules! impl_tool_signal {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
230 ($tool:ident) => {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
231 let tool_name_weak = Arc::downgrade(&tool_name);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
232 $tool.connect_clicked(move |b| {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
233 let tool_name = tool_name_weak.upgrade().unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
234 let mut tool_name = tool_name.lock().unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
235 *tool_name = b.get_label().unwrap().to_string();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
236 });
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
237 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
238 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
239 impl_tool_signal!(tool1);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
240 impl_tool_signal!(tool2);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
241 impl_tool_signal!(tool3);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
242 impl_tool_signal!(tool4);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
243 impl_tool_signal!(tool5);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
244
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
245 tools_box.add(&tool1);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
246 tools_box.add(&tool2);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
247 tools_box.add(&tool3);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
248 tools_box.add(&tool4);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
249 tools_box.add(&tool5);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
250
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
251 let current_size = Arc::new(Mutex::new((WIDTH as f64, HEIGHT as f64)));
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
252 let pressed = Arc::new(Mutex::new(false));
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
253
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
254 let drawing_area = gtk::DrawingArea::new();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
255 drawing_area.set_size_request(WIDTH, HEIGHT);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
256 drawing_area.set_hexpand(true);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
257 drawing_area.set_events(
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
258 gdk::EventMask::BUTTON_PRESS_MASK
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
259 | gdk::EventMask::BUTTON_RELEASE_MASK
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
260 | gdk::EventMask::POINTER_MOTION_MASK,
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
261 );
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
262 let current_size_weak = Arc::downgrade(&current_size);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
263 drawing_area.connect_configure_event(move |_, event| {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
264 let current_size = current_size_weak.upgrade().unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
265 let mut current_size = current_size.lock().unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
266 *current_size = match event.get_size() {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
267 (width, height) => (width as f64, height as f64),
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
268 };
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
269 true
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
270 });
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
271 let dev_weak = Arc::downgrade(&dev);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
272 let current_size_weak = Arc::downgrade(&current_size);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
273 let pressed_weak = Arc::downgrade(&pressed);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
274 drawing_area.connect_button_press_event(move |_, event| {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
275 if event.get_button() != 1 {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
276 return Inhibit(false);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
277 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
278
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
279 let dev = dev_weak.upgrade().unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
280 let pressed = pressed_weak.upgrade().unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
281 let mut pressed = pressed.lock().unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
282 *pressed = true;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
283 let (x, y) = event.get_position();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
284 println!("press tool {} at {}, {}", tool_name.lock().unwrap(), x, y);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
285 let current_size = current_size_weak.upgrade().unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
286 let current_size = current_size.lock().unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
287 let (width, height) = *current_size;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
288 dev.write(&[
1
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
289 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / width) as i32),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
290 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / height) as i32),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
291 input_axis_new(AbsoluteAxis::Z, 0),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
292 input_axis_new(AbsoluteAxis::Wheel, 0),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
293 input_axis_new(AbsoluteAxis::Pressure, 1024),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
294 input_axis_new(AbsoluteAxis::Distance, 0),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
295 input_axis_new(AbsoluteAxis::TiltX, 16),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
296 input_axis_new(AbsoluteAxis::TiltY, 0),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
297 input_misc_new(MiscKind::Serial, 0),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
298 input_synchronize_new(SynchronizeKind::Report, 0),
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
299 ])
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
300 .unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
301 Inhibit(false)
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
302 });
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
303 let dev_weak = Arc::downgrade(&dev);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
304 let current_size_weak = Arc::downgrade(&current_size);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
305 let pressed_weak = Arc::downgrade(&pressed);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
306 drawing_area.connect_button_release_event(move |_, event| {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
307 if event.get_button() != 1 {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
308 return Inhibit(false);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
309 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
310
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
311 let dev = dev_weak.upgrade().unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
312 let (x, y) = event.get_position();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
313 let pressed = pressed_weak.upgrade().unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
314 let mut pressed = pressed.lock().unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
315 *pressed = false;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
316 //println!("release {}, {}", x, y);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
317 let current_size = current_size_weak.upgrade().unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
318 let current_size = current_size.lock().unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
319 let (width, height) = *current_size;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
320 dev.write(&[
1
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
321 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / width) as i32),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
322 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / height) as i32),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
323 input_axis_new(AbsoluteAxis::Z, 0),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
324 input_axis_new(AbsoluteAxis::Wheel, 0),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
325 input_axis_new(AbsoluteAxis::Pressure, 0),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
326 input_axis_new(AbsoluteAxis::Distance, 16),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
327 input_axis_new(AbsoluteAxis::TiltX, 16),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
328 input_axis_new(AbsoluteAxis::TiltY, 0),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
329 input_misc_new(MiscKind::Serial, 0),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
330 input_synchronize_new(SynchronizeKind::Report, 0),
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
331 ])
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
332 .unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
333 Inhibit(false)
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
334 });
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
335 drawing_area.connect_motion_notify_event(move |_, event| {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
336 let (x, y) = event.get_position();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
337 let pressed = pressed.lock().unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
338 //println!("motion {}, {}", x, y);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
339 let current_size = current_size.lock().unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
340 let (width, height) = *current_size;
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
341 dev.write(&[
1
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
342 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / width) as i32),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
343 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / height) as i32),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
344 input_axis_new(AbsoluteAxis::Z, 0),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
345 input_axis_new(AbsoluteAxis::Wheel, 0),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
346 input_axis_new(AbsoluteAxis::Pressure, if *pressed { 2048 } else { 0 }),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
347 input_axis_new(AbsoluteAxis::Distance, if *pressed { 0 } else { 32 }),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
348 input_axis_new(AbsoluteAxis::TiltX, 16),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
349 input_axis_new(AbsoluteAxis::TiltY, 0),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
350 input_misc_new(MiscKind::Serial, 0),
6dbe2bbeef70 Improve input event types with helper functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
351 input_synchronize_new(SynchronizeKind::Report, 0),
0
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
352 ])
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
353 .unwrap();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
354 Inhibit(false)
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
355 });
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
356 drawing_area.connect_draw(move |_, ctx| {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
357 //println!("drawing {}", drawing_area);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
358 ctx.set_source_rgb(1., 0., 0.);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
359 ctx.set_operator(cairo::Operator::Screen);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
360 ctx.paint();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
361 Inhibit(false)
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
362 });
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
363
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
364 hbox.add(&tools_box);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
365 hbox.add(&drawing_area);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
366
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
367 window.add(&hbox);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
368
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
369 window.show_all();
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
370 }
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
371
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
372 fn main() {
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
373 let application = gtk::Application::new(
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
374 Some("fr.linkmauve.TabletEmu"),
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
375 gio::ApplicationFlags::empty(),
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
376 )
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
377 .expect("Initialisation failed…");
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
378 application.connect_activate(build_ui);
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
379 application.run(&args().collect::<Vec<_>>());
816237b684ea Hello world!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
380 }