Skip to content

Commit 2e05e2a

Browse files
committed
Make the trigger corner configurable
1 parent 5a42dfd commit 2e05e2a

2 files changed

Lines changed: 58 additions & 6 deletions

File tree

src/main.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use ghostwriter::{
1616
pen::Pen,
1717
screenshot::Screenshot,
1818
segmenter::analyze_image,
19-
touch::Touch,
19+
touch::{Touch, TriggerCorner},
2020
util::{setup_uinput, svg_to_bitmap, write_bitmap_to_file, OptionMap},
2121
};
2222

@@ -122,6 +122,10 @@ struct Args {
122122
/// Set the log level. Try 'debug' or 'trace'
123123
#[arg(long, default_value = "info")]
124124
log_level: String,
125+
126+
/// Sets which corner the touch trigger listens to (UR, UL, LR, LL, upper-right, upper-left, lower-right, lower-left)
127+
#[arg(long, default_value = "UR")]
128+
trigger_corner: String,
125129
}
126130

127131
fn main() -> Result<()> {
@@ -174,9 +178,10 @@ fn draw_svg(svg_data: &str, keyboard: &mut Keyboard, pen: &mut Pen, save_bitmap:
174178
}
175179

176180
fn ghostwriter(args: &Args) -> Result<()> {
181+
let trigger_corner = TriggerCorner::from_string(&args.trigger_corner)?;
177182
let keyboard = shared!(Keyboard::new(args.no_draw || args.no_keyboard, args.no_draw_progress,));
178183
let pen = shared!(Pen::new(args.no_draw));
179-
let touch = shared!(Touch::new(args.no_draw));
184+
let touch = shared!(Touch::new(args.no_draw, trigger_corner));
180185

181186
// Give time for the virtual keyboard to be plugged in
182187
sleep(Duration::from_millis(1000));
@@ -285,7 +290,15 @@ fn ghostwriter(args: &Args) -> Result<()> {
285290
if args.no_trigger {
286291
debug!("Skipping waiting for trigger");
287292
} else {
288-
info!("Waiting for trigger (hand-touch in the upper-right corner)...");
293+
info!(
294+
"Waiting for trigger (hand-touch in the {} corner)...",
295+
match TriggerCorner::from_string(&args.trigger_corner).unwrap() {
296+
TriggerCorner::UpperRight => "upper-right",
297+
TriggerCorner::UpperLeft => "upper-left",
298+
TriggerCorner::LowerRight => "lower-right",
299+
TriggerCorner::LowerLeft => "lower-left",
300+
}
301+
);
289302
lock!(touch).wait_for_trigger()?;
290303
}
291304

src/touch.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,29 @@ use std::time::Duration;
88

99
use crate::device::DeviceModel;
1010

11+
#[derive(Debug, Clone)]
12+
pub enum TriggerCorner {
13+
UpperRight,
14+
UpperLeft,
15+
LowerRight,
16+
LowerLeft,
17+
}
18+
19+
impl TriggerCorner {
20+
pub fn from_string(s: &str) -> Result<Self> {
21+
match s.to_lowercase().as_str() {
22+
"ur" | "upper-right" => Ok(TriggerCorner::UpperRight),
23+
"ul" | "upper-left" => Ok(TriggerCorner::UpperLeft),
24+
"lr" | "lower-right" => Ok(TriggerCorner::LowerRight),
25+
"ll" | "lower-left" => Ok(TriggerCorner::LowerLeft),
26+
_ => Err(anyhow::anyhow!(
27+
"Invalid trigger corner: {}. Use UR, UL, LR, LL, upper-right, upper-left, lower-right, or lower-left",
28+
s
29+
)),
30+
}
31+
}
32+
}
33+
1134
// Output dimensions remain the same for both devices
1235
const VIRTUAL_WIDTH: u16 = 768;
1336
const VIRTUAL_HEIGHT: u16 = 1024;
@@ -26,10 +49,11 @@ const ABS_MT_PRESSURE: u16 = 58;
2649
pub struct Touch {
2750
device: Option<Device>,
2851
device_model: DeviceModel,
52+
trigger_corner: TriggerCorner,
2953
}
3054

3155
impl Touch {
32-
pub fn new(no_touch: bool) -> Self {
56+
pub fn new(no_touch: bool, trigger_corner: TriggerCorner) -> Self {
3357
let device_model = DeviceModel::detect();
3458
info!("Touch using device model: {}", device_model.name());
3559

@@ -41,7 +65,11 @@ impl Touch {
4165

4266
let device = if no_touch { None } else { Some(Device::open(device_path).unwrap()) };
4367

44-
Self { device, device_model }
68+
Self {
69+
device,
70+
device_model,
71+
trigger_corner,
72+
}
4573
}
4674

4775
pub fn wait_for_trigger(&mut self) -> Result<()> {
@@ -67,7 +95,7 @@ impl Touch {
6795
if event.code() == ABS_MT_TRACKING_ID && event.value() == -1 {
6896
let (x, y) = self.input_to_virtual((position_x, position_y));
6997
debug!("Touch release detected at ({}, {}) normalized ({}, {})", position_x, position_y, x, y);
70-
if x > 700 && y < 50 {
98+
if self.is_in_trigger_zone(x, y) {
7199
debug!("Touch release in target zone!");
72100
return Ok(());
73101
}
@@ -133,6 +161,17 @@ impl Touch {
133161
Ok(())
134162
}
135163

164+
fn is_in_trigger_zone(&self, x: i32, y: i32) -> bool {
165+
const CORNER_SIZE: i32 = 68; // Size of the trigger zone (68x68 pixels)
166+
167+
match self.trigger_corner {
168+
TriggerCorner::UpperRight => x > VIRTUAL_WIDTH as i32 - CORNER_SIZE && y < CORNER_SIZE,
169+
TriggerCorner::UpperLeft => x < CORNER_SIZE && y < CORNER_SIZE,
170+
TriggerCorner::LowerRight => x > VIRTUAL_WIDTH as i32 - CORNER_SIZE && y > VIRTUAL_HEIGHT as i32 - CORNER_SIZE,
171+
TriggerCorner::LowerLeft => x < CORNER_SIZE && y > VIRTUAL_HEIGHT as i32 - CORNER_SIZE,
172+
}
173+
}
174+
136175
fn screen_width(&self) -> u32 {
137176
match self.device_model {
138177
DeviceModel::Remarkable2 => 1404,

0 commit comments

Comments
 (0)