comparison src/main.rs @ 12:d43c31aff57c

Split uinput helpers into another module.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 01 Nov 2020 16:01:00 +0100
parents 0193041f01d4
children 97e543f50f62
comparison
equal deleted inserted replaced
11:0193041f01d4 12:d43c31aff57c
17 use gio::prelude::*; 17 use gio::prelude::*;
18 use glib::clone; 18 use glib::clone;
19 use gtk::prelude::*; 19 use gtk::prelude::*;
20 20
21 use std::env::args; 21 use std::env::args;
22 use std::fs::{File, OpenOptions};
23 use std::io::ErrorKind; 22 use std::io::ErrorKind;
24 use std::sync::Arc; 23 use std::sync::Arc;
24 use input_linux::Key;
25 25
26 use input_linux::{ 26 mod uinput;
27 sys::input_event, sys::timeval, AbsoluteAxis, AbsoluteInfo, AbsoluteInfoSetup, EventKind,
28 InputId, InputProperty, Key, MiscKind, SynchronizeKind, UInputHandle,
29 };
30
31 mod state; 27 mod state;
32 use state::State; 28 use state::State;
33 29
34 const WIDTH: i32 = 320; 30 const WIDTH: i32 = 320;
35 const HEIGHT: i32 = 180; 31 const HEIGHT: i32 = 180;
36 32
37 const MAX_X: i32 = 69920; 33 const MAX_X: i32 = 69920;
38 const MAX_Y: i32 = 39980; 34 const MAX_Y: i32 = 39980;
39
40 fn create_uinput_device() -> std::io::Result<UInputHandle<File>> {
41 let file = OpenOptions::new().write(true).open("/dev/uinput")?;
42 let dev = UInputHandle::new(file);
43
44 dev.set_evbit(EventKind::Synchronize)?;
45 dev.set_evbit(EventKind::Key)?;
46 dev.set_evbit(EventKind::Absolute)?;
47 dev.set_evbit(EventKind::Misc)?;
48 dev.set_keybit(Key::ButtonToolPen)?;
49 dev.set_keybit(Key::ButtonToolRubber)?;
50 dev.set_keybit(Key::ButtonToolBrush)?;
51 dev.set_keybit(Key::ButtonToolPencil)?;
52 dev.set_keybit(Key::ButtonToolAirbrush)?;
53 dev.set_keybit(Key::ButtonTouch)?;
54 dev.set_keybit(Key::ButtonStylus)?;
55 dev.set_keybit(Key::ButtonStylus2)?;
56 dev.set_keybit(Key::ButtonStylus3)?;
57 dev.set_mscbit(MiscKind::Serial)?;
58 dev.set_propbit(InputProperty::Direct)?;
59
60 dev.set_absbit(AbsoluteAxis::X)?;
61 dev.set_absbit(AbsoluteAxis::Y)?;
62 dev.set_absbit(AbsoluteAxis::Z)?;
63 dev.set_absbit(AbsoluteAxis::Wheel)?;
64 dev.set_absbit(AbsoluteAxis::Pressure)?;
65 dev.set_absbit(AbsoluteAxis::Distance)?;
66 dev.set_absbit(AbsoluteAxis::TiltX)?;
67 dev.set_absbit(AbsoluteAxis::TiltY)?;
68 dev.set_absbit(AbsoluteAxis::Misc)?;
69
70 let id = InputId {
71 bustype: 3,
72 vendor: 0x56a,
73 product: 0x350,
74 version: 0xb,
75 };
76
77 let x = AbsoluteInfoSetup {
78 axis: AbsoluteAxis::X,
79 info: AbsoluteInfo {
80 value: 0,
81 minimum: 0,
82 maximum: MAX_X,
83 fuzz: 0,
84 flat: 0,
85 resolution: 200,
86 },
87 };
88 let y = AbsoluteInfoSetup {
89 axis: AbsoluteAxis::Y,
90 info: AbsoluteInfo {
91 value: 0,
92 minimum: 0,
93 maximum: MAX_Y,
94 fuzz: 0,
95 flat: 0,
96 resolution: 200,
97 },
98 };
99 let z = AbsoluteInfoSetup {
100 axis: AbsoluteAxis::Z,
101 info: AbsoluteInfo {
102 value: 0,
103 minimum: -900,
104 maximum: 899,
105 fuzz: 0,
106 flat: 0,
107 resolution: 287,
108 },
109 };
110 let wheel = AbsoluteInfoSetup {
111 axis: AbsoluteAxis::Wheel,
112 info: AbsoluteInfo {
113 value: 0,
114 minimum: 0,
115 maximum: 2047,
116 fuzz: 0,
117 flat: 0,
118 resolution: 0,
119 },
120 };
121 let pressure = AbsoluteInfoSetup {
122 axis: AbsoluteAxis::Pressure,
123 info: AbsoluteInfo {
124 value: 0,
125 minimum: 0,
126 maximum: 8196,
127 fuzz: 0,
128 flat: 0,
129 resolution: 0,
130 },
131 };
132 let distance = AbsoluteInfoSetup {
133 axis: AbsoluteAxis::Distance,
134 info: AbsoluteInfo {
135 value: 0,
136 minimum: 0,
137 maximum: 63,
138 fuzz: 0,
139 flat: 0,
140 resolution: 0,
141 },
142 };
143 let tilt_x = AbsoluteInfoSetup {
144 axis: AbsoluteAxis::TiltX,
145 info: AbsoluteInfo {
146 value: 0,
147 minimum: -64,
148 maximum: 63,
149 fuzz: 0,
150 flat: 0,
151 resolution: 57,
152 },
153 };
154 let tilt_y = AbsoluteInfoSetup {
155 axis: AbsoluteAxis::TiltY,
156 info: AbsoluteInfo {
157 value: 0,
158 minimum: -64,
159 maximum: 63,
160 fuzz: 0,
161 flat: 0,
162 resolution: 57,
163 },
164 };
165 let misc = AbsoluteInfoSetup {
166 axis: AbsoluteAxis::Misc,
167 info: AbsoluteInfo {
168 value: 0,
169 minimum: 0,
170 maximum: 0,
171 fuzz: 0,
172 flat: 0,
173 resolution: 0,
174 },
175 };
176
177 dev.create(
178 &id,
179 b"TabletEmu",
180 0,
181 &[x, y, z, wheel, pressure, distance, tilt_x, tilt_y, misc],
182 )?;
183 Ok(dev)
184 }
185
186 fn input_event_new(type_: EventKind, code: u16, value: i32) -> input_event {
187 input_event {
188 time: timeval {
189 tv_sec: 0,
190 tv_usec: 0,
191 },
192 type_: type_ as u16,
193 code,
194 value,
195 }
196 }
197
198 fn input_axis_new(code: AbsoluteAxis, value: i32) -> input_event {
199 input_event_new(EventKind::Absolute, code as u16, value)
200 }
201
202 fn input_key_new(code: Key, value: i32) -> input_event {
203 input_event_new(EventKind::Key, code as u16, value)
204 }
205
206 fn input_misc_new(code: MiscKind, value: i32) -> input_event {
207 input_event_new(EventKind::Misc, code as u16, value)
208 }
209
210 fn input_synchronize_new(code: SynchronizeKind, value: i32) -> input_event {
211 input_event_new(EventKind::Synchronize, code as u16, value)
212 }
213 35
214 fn build_main_menu(application: &gtk::Application) { 36 fn build_main_menu(application: &gtk::Application) {
215 let quit = gio::SimpleAction::new("quit", None); 37 let quit = gio::SimpleAction::new("quit", None);
216 application.set_accels_for_action("app.quit", &["<Control>q"]); 38 application.set_accels_for_action("app.quit", &["<Control>q"]);
217 application.add_action(&quit); 39 application.add_action(&quit);