Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions calibrate-touchscreen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ impl FullscreenSurface {
let width = self.width;
let height = self.height;

let mut file = tempfile::tempfile().unwrap();
let mut file = match tempfile::tempfile() {
Ok(f) => f,
Err(e) => {
eprintln!("Failed to create temporary buffer file: {}", e);
return;
}
};

draw_with_target(&mut file, (width, height), TARGETS[targets_index]);
let pool = wl_shm.create_pool(file.as_fd(), width * height * 4, qh, ());
let buffer = pool.create_buffer(
Expand All @@ -79,6 +86,7 @@ impl FullscreenSurface {
self.wl_surface.damage(0, 0, width, height);
self.wl_surface.commit();
buffer.destroy();
pool.destroy();
}
}

Expand Down Expand Up @@ -191,6 +199,7 @@ impl Dispatch<WlOutput, ()> for State {
fn draw_with_target(tmp: &mut File, (buf_x, buf_y): (i32, i32), (target_x, target_y): (f64, f64)) {
let centre_x = (buf_x as f64 * target_x) as i64;
let centre_y = (buf_y as f64 * target_y) as i64;
// Target circle size is 1/80th of the screen size for easy visibility and accurate touching
let target_size = (max(buf_x, buf_y) / 80) as i64;
use std::io::Write;
let mut buf = std::io::BufWriter::new(tmp);
Expand All @@ -199,13 +208,16 @@ fn draw_with_target(tmp: &mut File, (buf_x, buf_y): (i32, i32), (target_x, targe
let distance_squared =
(x - centre_x) * (x - centre_x) + (y - centre_y) * (y - centre_y);
if distance_squared >= target_size * target_size {
// Outside target circle: draw gradient background
// ARGB8888 format: [B, G, R, A]
let a = 0xFF;
let r = 0x3F;
let g = 0x3F * y as i32 / buf_y;
let b = 0x3F * y as i32 / buf_y;
buf.write_all(&[b as u8, g as u8, r as u8, a as u8])
.unwrap();
} else {
// Inside target circle: draw white circle with smooth edges
let intensity = 0xFF - 0x40 * (distance_squared / (target_size * target_size / 4));
let a = 0xFF;
buf.write_all(&[intensity as u8, intensity as u8, intensity as u8, a as u8])
Expand Down Expand Up @@ -280,6 +292,13 @@ impl InitialisedState {
* (self.touches[1].1 - self.touches[2].1)
- (self.touches[1].0 - self.touches[2].0) * (self.touches[0].1 - self.touches[2].1);

// Check for collinear points (determinant close to zero)
if k.abs() < 1e-6 {
eprintln!("Error: touched points are collinear or too close together. Cannot calibrate.");
eprintln!("Please touch the three targets more accurately and ensure they form a triangle.");
return;
}

let ak = (TARGETS[0].0 - TARGETS[2].0) * (self.touches[1].1 - self.touches[2].1)
- (TARGETS[1].0 - TARGETS[2].0) * (self.touches[0].1 - self.touches[2].1);

Expand Down Expand Up @@ -512,11 +531,9 @@ impl Dispatch<xdg_surface::XdgSurface, ()> for State {
match event {
xdg_surface::Event::Configure { serial } => {
proxy.ack_configure(serial);
state
.initialised
.as_mut()
.unwrap()
.draw_fullscreen_surfaces(qhandle);
if let Some(init) = state.initialised.as_mut() {
init.draw_fullscreen_surfaces(qhandle);
}
}
_ => {}
}
Expand Down
54 changes: 28 additions & 26 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: libinput
version: "22.04"
base: core22
version: "24.04"
base: core24
summary: libinput-tools helpful in debugging libinput
description: |
- list-devices:
Expand Down Expand Up @@ -28,26 +28,29 @@ description: |
Bonus utility to help with touchscreen calibration.
https://mir-server.io/docs/howto-calibrate-a-touchscreen-device

website: https://github.com/AlanGriffiths/libinput
title: libinput
website: https://github.com/AlanGriffiths/libinput
source-code: https://github.com/AlanGriffiths/libinput
issues: https://github.com/AlanGriffiths/libinput/issues
contact: https://github.com/AlanGriffiths/libinput/issues
license: Apache-2.0

grade: stable
confinement: strict

architectures:
- build-on: amd64
- build-on: armhf
- build-on: arm64
- build-on: ppc64el
platforms:
amd64:
armhf:
arm64:
ppc64el:

layout:
/usr/share/drirc.d:
bind: $SNAP/graphics/drirc.d
/usr/share/X11/xkb:
bind: $SNAP/usr/share/X11/xkb
/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/gdk-pixbuf-2.0:
bind: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/gdk-pixbuf-2.0
/usr/lib/${CRAFT_ARCH_TRIPLET_BUILD_FOR}/gdk-pixbuf-2.0:
bind: $SNAP/usr/lib/${CRAFT_ARCH_TRIPLET_BUILD_FOR}/gdk-pixbuf-2.0
/usr/share/mime:
bind: $SNAP/usr/share/mime
/etc/gtk-3.0:
Expand All @@ -58,10 +61,10 @@ layout:
symlink: $SNAP/usr/share/libinput

plugs:
graphics-core22:
gpu-2404:
interface: content
target: $SNAP/graphics
default-provider: mesa-core22
target: $SNAP/gpu-2404
default-provider: mesa-2404

apps:
list-devices:
Expand All @@ -80,7 +83,7 @@ apps:
- raw-input
- wayland
command-chain:
- bin/graphics-core22-wrapper
- bin/gpu-2404-wrapper
- bin/wayland-launch
command: usr/bin/libinput debug-gui

Expand Down Expand Up @@ -108,7 +111,7 @@ apps:
plugs:
- wayland
command-chain:
- bin/graphics-core22-wrapper
- bin/gpu-2404-wrapper
- bin/wayland-launch
command: bin/calibrate-touchscreen

Expand Down Expand Up @@ -136,32 +139,31 @@ parts:
- shared-mime-info
override-build: |
# Update mime database
update-mime-database ${SNAPCRAFT_PART_INSTALL}/usr/share/mime
update-mime-database ${CRAFT_PART_INSTALL}/usr/share/mime
stage-packages:
- librsvg2-common
- gsettings-desktop-schemas
- libglib2.0-bin
override-prime: |
snapcraftctl prime
craftctl default
# Compile the gsettings schemas
/usr/lib/${SNAPCRAFT_ARCH_TRIPLET}/glib-2.0/glib-compile-schemas "$SNAPCRAFT_PRIME/usr/share/glib-2.0/schemas"
/usr/lib/${CRAFT_ARCH_TRIPLET_BUILD_FOR}/glib-2.0/glib-compile-schemas "$CRAFT_PRIME/usr/share/glib-2.0/schemas"
# Index the pixbuf loaders
LOADERS_PATH=$(echo ${SNAPCRAFT_PRIME}/usr/lib/${SNAPCRAFT_ARCH_TRIPLET}/gdk-pixbuf-2.0/*/loaders)
QUERY_LOADERS=/usr/lib/${SNAPCRAFT_ARCH_TRIPLET}/gdk-pixbuf-2.0/gdk-pixbuf-query-loaders
LOADERS_PATH=$(echo ${CRAFT_PRIME}/usr/lib/${CRAFT_ARCH_TRIPLET_BUILD_FOR}/gdk-pixbuf-2.0/*/loaders)
QUERY_LOADERS=/usr/lib/${CRAFT_ARCH_TRIPLET_BUILD_FOR}/gdk-pixbuf-2.0/gdk-pixbuf-query-loaders
GDK_PIXBUF_MODULEDIR=${LOADERS_PATH} ${QUERY_LOADERS} > ${LOADERS_PATH}/../loaders.cache
sed s!$SNAPCRAFT_PRIME!!g --in-place ${LOADERS_PATH}/../loaders.cache
sed s!$CRAFT_PRIME!!g --in-place ${LOADERS_PATH}/../loaders.cache

graphics-core22:
gpu-2404:
after:
- scripts
- libinput-tools
- gtkstuff
- calibrate-touchscreen
source: https://github.com/MirServer/graphics-core22.git
source: https://github.com/canonical/gpu-snap.git
plugin: dump
override-prime: |
craftctl default
${CRAFT_PART_SRC}/bin/graphics-core22-cleanup mesa-core22
rm -rf ${CRAFT_PRIME}/usr/share/{doc,man}
${CRAFT_PART_SRC}/bin/gpu-2404-cleanup mesa-2404 nvidia-2404
prime:
- bin/graphics-core22-wrapper
- bin/gpu-2404-wrapper