-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
128 lines (110 loc) · 3.96 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::fs::OpenOptions;
use std::io::{self, ErrorKind, Read, Write};
use std::mem;
use std::os::unix::fs::OpenOptionsExt;
use std::fs::File;
const TOUCHPAD_DEVPATH: &str = "/dev/input/event5";
const BRIGHTNESS_PATH: &str = "/sys/class/backlight/intel_backlight/brightness";
const O_RDONLY: i32 = 0;
const EV_ABS: u16 = 0x03;
const ABS_X: u16 = 0x00;
const ABS_Y: u16 = 0x01;
const EV_KEY: u16 = 0x01;
const BTN_TOUCH: u16 = 0x14A;
#[repr(C)]
#[derive(Debug)]
struct TimeVal {
tv_sec: i64,
tv_usec: i64,
}
#[repr(C)]
#[derive(Debug)]
struct InputEvent {
time: TimeVal,
type_: u16,
code: u16,
value: i32,
}
fn main() -> io::Result<()> {
let mut touchpad_pressed: bool = false;
let mut position_x: i32 = -1;
let mut position_y: i32 = -1;
let mut file = OpenOptions::new()
.read(true)
.write(true)
.open(BRIGHTNESS_PATH)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let mut brightness: u64 = match contents.trim().parse() {
Ok(value) => value,
Err(_) => {
eprintln!("Err");
return Ok(());
}
};
println!("brightness: {}", brightness);
let mut touchpad = OpenOptions::new()
.read(true)
.custom_flags(O_RDONLY)
.open(TOUCHPAD_DEVPATH)?;
let mut buf = [0u8; mem::size_of::<InputEvent>()];
loop {
match touchpad.read(&mut buf) {
Ok(n) if n == buf.len() => {
let event: InputEvent = unsafe { mem::transmute_copy(&buf) };
//println!("{:?}", event);
// косание
if (event.type_ == EV_KEY) && (event.code == BTN_TOUCH) {
if event.value == 1 {
println!("косание начало");
touchpad_pressed = true;
}
if event.value == 0 {
println!("косание конец");
touchpad_pressed = false;
position_x = -1;
position_y = -1;
}
}
// положение X
if (event.type_ == EV_ABS) && (event.code == ABS_X) {
//println!("X = {:?}", event.value);
if position_x == -1{
position_x = event.value;
}
}
// положение Y
if (event.type_ == EV_ABS) && (event.code == ABS_Y) {
//println!("Y = {:?}", event.value);
if position_y == -1{
position_y = event.value;
} else{
if position_x > 3500 {
if event.value < position_y - 5 {
if brightness < 540 {
brightness = brightness + 5;
}
contents = brightness.to_string();
file.write_all(contents.as_bytes())?;
println!("brightness: {}", brightness);
position_y = event.value;
}
if event.value > position_y + 5 {
if brightness > 40 {
brightness = brightness - 5;
}
contents = brightness.to_string();
file.write_all(contents.as_bytes())?;
println!("brightness: {}", brightness);
position_y = event.value;
}
}
}
}
}
Err(ref e) if e.kind() == ErrorKind::WouldBlock => continue,
Err(e) => return Err(e),
_ => {}
}
}
}