Score:3

How can I get absolute touchpad coordinates

ae flag

From what I understand, earlier versions of the Synaptic driver in Ubuntu had an 'absolute mode' for the trackpad which allowed the user to access absolute coordinates as well as relative coordinates. However, Synaptic decided to remove it(not sure why) and its no longer available. Is there any way to access the absolute mode in Ubuntu 20.04 LTS? I'd like to be able to get these coordinates in a C/C++ program but open to any other solutions (Rust, shell script, whatever works). I've come across similar posts but I'm still unable to find a satisfactory solution. Most recommend the evtest driver which does sport absolute mode but lacks other features(2 finger scrolling, etc.). I'm thinking reverting back to an older version of the Synaptic driver might help but wanted to know what I'd be missing out on. I mean, are there any important features in the more recent Synaptic driver that the old one with absolute mode doesn't?

Edit: I did a bit more digging and came across this page --> http://manpages.ubuntu.com/manpages/bionic/man1/python-evdev.1.html

It shows how to use a Python library, evdev, and how to use it to access system input events. Following the examples, I set it to watch my trackpad events and upon moving my finger I got a whole wall of information which unfortunately, I didn't understand. I did notice terms like ABS_X, ABS_Y but couldn't make anything of it. Regardless, it appears that I can access a lot of trackpad information through this library including something to do with absolute coordinates. How do I use this information? Can anyone show me how to write a simple Python function using evdev

def foo():
    ...
    return (x,y)

where (x,y) represents my finger postion on the trackpad?

Edit 2: Sample output of evdev monitoring my trackpad. Can any of this be used to get absolute coordinates?

absolute axis event at 1623586006.216310, ABS_MT_TRACKING_ID 
absolute axis event at 1623586006.216310, ABS_MT_POSITION_X 
absolute axis event at 1623586006.216310, ABS_MT_POSITION_Y 
absolute axis event at 1623586006.216310, ABS_MT_PRESSURE 
key event at 1623586006.216310, 330 (BTN_TOUCH), down
absolute axis event at 1623586006.216310, ABS_X 
absolute axis event at 1623586006.216310, ABS_Y 
absolute axis event at 1623586006.216310, ABS_PRESSURE 
key event at 1623586006.216310, 325 (BTN_TOOL_FINGER), down
synchronization event at 1623586006.216310, SYN_REPORT 
absolute axis event at 1623586006.231209, ABS_MT_PRESSURE 
absolute axis event at 1623586006.231209, ABS_PRESSURE
Pilot6 avatar
cn flag
It depends on hardware. Device drivers send either absolute or relative coordinates. You can also get raw information, but it also depends on how the device is connected: i2c or ps2.
Score:0
th flag

based on @Greyson Phipps answer, i created a simple script to make make touchpad behave like a drawing tablet https://github.com/Vaisakhkm2625/touchpaddraw

not ideal, as i am not a primarily python dev and this is a hacky solution, but works . works only in xorg.in wayland(gnome), this is getting 2 cursors, one with absolute and one with relative and jumbs between them....


import math
import os
import time

#get trackpad absolute coords
from evdev import InputDevice
 
import argparse
 
from pynput import mouse
from pynput.mouse import Button
 
# Initialize parser
parser = argparse.ArgumentParser()
 
# Adding optional argument
parser.add_argument("-d", "--device", help = "device (event*)")
 
# Read arguments from command line
args = parser.parse_args()

#SET THIS TO YOUR DEVICE
device = InputDevice('/dev/input/'+ args.device if args.device else 'event7')

touchpad_x_max = 1224
touchpad_y_max = 804

max_x = 1920
max_y = 1080

x = 0
y = 0

def get_xy_coords(e):
    #you may need to change this number here; i don't know
    if e.code == 53:
        global x
        x = e.value
    #this one too
    if e.code == 54:
        global y
        y = e.value
        
def mapFromTo(x,a,b,c,d):
   # y=(x-a)//(b-a)*(d-c)+c
   y=(x-a)/(b-a)*(d-c)+c
   return y

x_pos =0
y_pos =0

mouse_controller = mouse.Controller()
for event in device.read_loop():
    #rows, cols = stdscr.getmaxyx()
    get_xy_coords(event)
    if event.code == 54:
        prev_x_pos = x_pos 
        prev_y_pos = y_pos 
        x_pos =math.floor(mapFromTo(x,0,touchpad_x_max,0,max_x))
        y_pos =math.floor(mapFromTo(y,0,touchpad_y_max,0,max_y))
        if (abs(prev_x_pos-x_pos)>15 or abs(prev_y_pos-y_pos)>15):
            mouse_controller.release(Button.left)
            mouse_controller.position = (x_pos,y_pos);
        mouse_controller.press(Button.left)
        mouse_controller.position = (x_pos,y_pos);
Score:0
mz flag

Use

    >>> print(event)
    event at 1337197425.477827, code 04, type 04, val 458792

Right now you output the timestamp and code, but val should be your coordinate, see https://python-evdev.readthedocs.io/en/latest/_modules/evdev/events.html.

n00dles avatar
ae flag
So, val contains 1 value? How do I get both coordinates?
mz flag
Indeed, you need two events: ABS_MT_POSITION_X and ABS_MT_POSITION_Y. https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.