使用libevent模拟图形输入板

使用libevent模拟图形输入板

我想从脚本模拟图形输入板(如 wacom 的图形输入板)。继续的“好”方法似乎是使用 libevent,它是 uinput 之上的抽象层。所以我尝试编写一个脚本,使用 的 python 库发送一些事件,如EV_ABS.ABS_X或。不幸的是,当我使用 Krita/Gimp/... 来测试它时,它绘制的线不会根据压力改变其形状,并且不会显示任何事件。知道为什么吗?EV_ABS.ABS_PRESSURElibeventxinput test-xi2pressure

谢谢!

重现步骤

执行下面的代码:

sudo pip3 install libevdev
chmod +x ./simulate_graphics_tablet.py
sudo ./simulate_graphics_tablet.py

然后你有 30 秒的时间:

  • 要么运行xinput list然后xinput test-xi2 <number of Tablet alone>
  • 或者打开 Gimp,转到“编辑/输入设备”并将设备“仅平板电脑”配置为“屏幕”,保存并关闭弹出窗口,创建一个新文件(Ctrl-N),缩放并按“tab”键覆盖大部分屏幕的绘图表面。使用“p”键切换到画笔,确保画笔设置为Pressure size

我得到的是:一条统一的线,在 xinput 上我没有任何关于压力的参考,我只有这样的东西:

EVENT type 17 (RawMotion)
    device: 11 (11)
    detail: 0
    flags: 
    valuators:
          0: 29897.54 (29897.54)
          1: 29897.54 (29897.54)

我期望的是:一条大小不恒定的线(脚本线性增加压力),或者在 xinput 上看到一些与压力相关的事件。

#!/usr/bin/env python3
import sys
import libevdev
import time

def main(args):
    dev = libevdev.Device()
    dev.name = "Tablet alone"
    dev.enable(libevdev.EV_ABS.ABS_X,
               libevdev.InputAbsInfo(minimum=0, maximum=32767))
    dev.enable(libevdev.EV_ABS.ABS_Y,
               libevdev.InputAbsInfo(minimum=0, maximum=32767))
    dev.enable(libevdev.EV_ABS.ABS_Z,
               libevdev.InputAbsInfo(minimum=0, maximum=8191))
    # dev.enable(libevdev.EV_ABS.ABS_0B,
    #            libevdev.InputAbsInfo(minimum=0, maximum=8191))
    # dev.enable(libevdev.EV_ABS.ABS_DISTANCE,
    #            libevdev.InputAbsInfo(minimum=0, maximum=8191))
    dev.enable(libevdev.EV_ABS.ABS_PRESSURE,
               libevdev.InputAbsInfo(minimum=0, maximum=8191))
    dev.enable(libevdev.EV_MSC.MSC_SCAN)
    dev.enable(libevdev.EV_KEY.KEY_P)
    dev.enable(libevdev.EV_KEY.BTN_LEFT)
    dev.enable(libevdev.EV_KEY.BTN_RIGHT)
    dev.enable(libevdev.EV_KEY.BTN_MIDDLE)
    dev.enable(libevdev.EV_KEY.BTN_TOUCH)
    dev.enable(libevdev.EV_SYN.SYN_REPORT)
    dev.enable(libevdev.EV_SYN.SYN_CONFIG)
    dev.enable(libevdev.EV_SYN.SYN_MT_REPORT)
    dev.enable(libevdev.EV_SYN.SYN_DROPPED)
    dev.enable(libevdev.EV_SYN.SYN_04)
    dev.enable(libevdev.EV_SYN.SYN_05)
    dev.enable(libevdev.EV_SYN.SYN_06)
    dev.enable(libevdev.EV_SYN.SYN_07)
    dev.enable(libevdev.EV_SYN.SYN_08)
    dev.enable(libevdev.EV_SYN.SYN_09)
    dev.enable(libevdev.EV_SYN.SYN_0A)
    dev.enable(libevdev.EV_SYN.SYN_0B)
    dev.enable(libevdev.EV_SYN.SYN_0C)
    dev.enable(libevdev.EV_SYN.SYN_0D)
    dev.enable(libevdev.EV_SYN.SYN_0E)
    dev.enable(libevdev.EV_SYN.SYN_MAX)
    try:
        uinput = dev.create_uinput_device()
        print("New device at {} ({})".format(uinput.devnode, uinput.syspath))
        # Sleep for a bit so udev, libinput, Xorg, Wayland, ...
        # all have had a chance to see the device and initialize
        # it. Otherwise the event will be sent by the kernel but
        # nothing is ready to listen to the device yet.
        print("Waiting 30s to let you:")
        print("1) open Gimp")
        print("2) Go to 'Edit/Input device' and configure the device 'Tablet alone' to 'Screen'.")
        print("3) Save and close the pop up")
        print("4) Create a new file (Ctrl-N)")
        print("5) Zoom and press 'tab' to have a drawing surface coverint most of the screen.")
        print("6) Switch to brush using 'p' key.")
        time.sleep(30)

        pc = 0
        direc = +1
        already_pressed_one = False
        # uinput.send_events([
        #     libevdev.InputEvent(libevdev.EV_KEY.KEY_P, 1),
        #     libevdev.InputEvent(libevdev.EV_SYN.SYN_REPORT, 0),
        # ])
        # time.sleep(0.1)
        # uinput.send_events([
        #     libevdev.InputEvent(libevdev.EV_KEY.KEY_P, 0),
        #     libevdev.InputEvent(libevdev.EV_SYN.SYN_REPORT, 0),
        # ])
        for i in range(250):
            pc_ = pc/100
            val_x = int(pc_*10000 + (1-pc_)*17767)
            val_y = int(pc_*5000 + (1-pc_)*22767)
            val_pres = int(pc_*10 + (1-pc_)*6000)
            print("Will send: x={}, y={}, press={} (pc={})".format(
                val_x,
                val_y,
                val_pres,
                pc))
            uinput.send_events([
                libevdev.InputEvent(libevdev.EV_ABS.ABS_PRESSURE, val_pres),
                libevdev.InputEvent(libevdev.EV_ABS.ABS_X, val_y),
                libevdev.InputEvent(libevdev.EV_ABS.ABS_Y, val_y),
                libevdev.InputEvent(libevdev.EV_SYN.SYN_REPORT, 0),
            ])
            pc += direc
            if not already_pressed_one:
                print("Press!")
                uinput.send_events([
                    libevdev.InputEvent(libevdev.EV_KEY.BTN_LEFT, 1),
                    libevdev.InputEvent(libevdev.EV_SYN.SYN_REPORT, 0),
                ])                
                already_pressed_one = True
            if pc >= 100 or pc <=0 :
                print("Release click.")
                uinput.send_events([
                    libevdev.InputEvent(libevdev.EV_KEY.BTN_LEFT, 0),
                    libevdev.InputEvent(libevdev.EV_SYN.SYN_REPORT, 0),
                ])
                if pc >= 100:
                    pc = 100
                    direc = -1
                if pc <= 0:
                    pc = 0
                    direc = +1
                time.sleep(10)
                print("Press!")
                uinput.send_events([
                    libevdev.InputEvent(libevdev.EV_KEY.BTN_LEFT, 1),
                    libevdev.InputEvent(libevdev.EV_SYN.SYN_REPORT, 0),
                ])
                already_pressed_one = True
            time.sleep(0.1)

    except KeyboardInterrupt:
        pass
    except OSError as e:
        print(e)


if __name__ == "__main__":
    if len(sys.argv) > 2:
        print("Usage: {}")
        sys.exit(1)
    main(sys.argv)

编辑:我尝试按照解释使用 ABS_KEY.BTN_TOOL_PEN这里,但我不知道为什么当我启用它时,它就不再被检测到xinput list

dev.enable(libevdev.EV_KEY.BTN_TOOL_PEN)

答案1

经过一段时间和文档/代码读着读着,终于找到了解决办法。

如果您只想测试脚本(在本答案的末尾),只需以 root 身份运行它即可:

$ chmod +x completely_fake_tablet.py
$ sudo pip3 install libevdev
$ sudo ./completely_fake_tablet.py

然后打开gimp,将设备添加为输入设备,选择画笔动态喜欢Pencil Generic并享受。如果您想了解更多详细信息,请阅读以下内容:

总而言之,系统非常挑剔,您需要确保:

  • 您可以libevdev.INPUT_PROP_DIRECT说您拥有类似平板电脑的设备(请参阅上面的文档链接)
  • 您可以启用所有类似平板电脑的工具,例如:

    • libevdev.EV_KEY.BTN_TOOL_PEN这将用于表示笔何时靠近平板电脑,libevdev.EV_KEY.BTN_TOUCH表示何时单击
    • libevdev.EV_KEY.BTN_STYLUS/libevdev.EV_KEY.BTN_STYLUS2对应笔上的按钮
    • libevdev.EV_ABS.ABS_{X,Y}对于位置(确保指定最小值、最大值、和分辨率:没有分辨率,设备将永远不会被检测到!)
    • libevdev.EV_ABS.ABS_PRESSURE为了压力
    • libevdev.EV_SYN.SYN_REPORT每次发送信息块时都必须发送该信息。如果你不发送这个,内核将不会处理事件,或者以非常慢的速率(如 1/s)处理。

您还想确保启用设备后至少等待一秒钟在发送任何事件之前,否则平板电脑将来将无法被识别。我还注意到,xinput在我发送第一个事件之前,鼠标的笔部分不会列出。请注意,xinput将列出两种设备,一种用于按钮,我猜是键盘,另一种作为笔(名为Tablet alone Pen (0)Tablet alone

$ xinput list
⎡ Virtual core pointer                          id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ ETPS/2 Elantech Touchpad                  id=17   [slave  pointer  (2)]
⎜   ↳ lircd-uinput                              id=18   [slave  pointer  (2)]
⎜   ↳ Tablet alone Pen (0)                      id=12   [slave  pointer  (2)]
⎣ Virtual core keyboard                         id=3    [master keyboard (2)]
    ↳ Virtual core XTEST keyboard               id=5    [slave  keyboard (3)]
    ↳ Power Button                              id=6    [slave  keyboard (3)]
    ↳ Asus Wireless Radio Control               id=7    [slave  keyboard (3)]
    ↳ Video Bus                                 id=8    [slave  keyboard (3)]
    ↳ Video Bus                                 id=9    [slave  keyboard (3)]
    ↳ Sleep Button                              id=10   [slave  keyboard (3)]
    ↳ USB2.0 HD UVC WebCam: USB2.0 HD           id=14   [slave  keyboard (3)]
    ↳ Asus WMI hotkeys                          id=15   [slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard              id=16   [slave  keyboard (3)]
    ↳ lircd-uinput                              id=19   [slave  keyboard (3)]
    ↳ Tablet alone                              id=11   [slave  keyboard (3)]

如果您使用 Gimp 进行测试,请确保打开您的软件 xinput能够列出它,否则它不会在输入设备中列出,并且您将需要重新启动 gimp(请注意,您可以重新启动脚本而不重新启动 gimp)。您还需要在Edit/input device设备中将其设置Tablet aloneScreen,并选择一个画笔,其动态设置类似于Pencil Generic屏幕截图中的内容。为了使脚本正常工作,您可能还需要按Tab以获得更宽的绘图区域(Tab再次返回到正常窗口),并缩放直到覆盖所有区域。

在此输入图像描述

脚本:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import libevdev
import time
## Some doc needed for this project
# http://www.infradead.org/~mchehab/kernel_docs_pdf/linux-input.pdf

## Some code to get inspiration from
# https://github.com/linuxwacom/input-wacom/blob/master/4.5/wacom_w8001.c

## Some doc to read at some point in my life:
# https://lwn.net/Kernel/LDD3/
# https://www.kernel.org/doc/html/v4.11/driver-api/index.html

def main(args):
    dev = libevdev.Device()
    dev.name = "Tablet alone"
    ### NB: all the following information needs to be enabled
    ### in order to recognize the device as a tablet.
    # Say that the device will send "absolute" values
    dev.enable(libevdev.INPUT_PROP_DIRECT)
    # Say that we are using the pen (not the erasor), and should be set to 1 when we are at proximity to the device.
    # See http://www.infradead.org/~mchehab/kernel_docs_pdf/linux-input.pdf page 9 (=13) and guidelines page 12 (=16), or the https://github.com/linuxwacom/input-wacom/blob/master/4.5/wacom_w8001.c (rdy=proximity)
    dev.enable(libevdev.EV_KEY.BTN_TOOL_PEN)
    dev.enable(libevdev.EV_KEY.BTN_TOOL_RUBBER)
    # Click
    dev.enable(libevdev.EV_KEY.BTN_TOUCH)
    # Press button 1 on pen
    dev.enable(libevdev.EV_KEY.BTN_STYLUS)
    # Press button 2 on pen, see great doc
    dev.enable(libevdev.EV_KEY.BTN_STYLUS2)
    # Send absolute X coordinate
    dev.enable(libevdev.EV_ABS.ABS_X,
               libevdev.InputAbsInfo(minimum=0, maximum=32767, resolution=100))
    # Send absolute Y coordinate
    dev.enable(libevdev.EV_ABS.ABS_Y,
               libevdev.InputAbsInfo(minimum=0, maximum=32767, resolution=100))
    # Send absolute pressure
    dev.enable(libevdev.EV_ABS.ABS_PRESSURE,
               libevdev.InputAbsInfo(minimum=0, maximum=8191))
    # Use to confirm that we finished to send the informations
    # (to be sent after every burst of information, otherwise
    # the kernel does not proceed the information)
    dev.enable(libevdev.EV_SYN.SYN_REPORT)
    # Report buffer overflow
    dev.enable(libevdev.EV_SYN.SYN_DROPPED)
    try:
        uinput = dev.create_uinput_device()
        print("New device at {} ({})".format(uinput.devnode, uinput.syspath))
        # Sleep for a bit so udev, libinput, Xorg, Wayland, ...
        # all have had a chance to see the device and initialize
        # it. Otherwise the event will be sent by the kernel but
        # nothing is ready to listen to the device yet. And it
        # will never be detected in the futur ;-)
        time.sleep(1) 
        # Reports that the PEN is close to the surface
        # Important to make sure xinput can detect (and list)
        # the pen. Otherwise, it won't write anything in gimp.
        uinput.send_events([
            libevdev.InputEvent(libevdev.EV_KEY.BTN_TOUCH,
                                value=0),
            libevdev.InputEvent(libevdev.EV_KEY.BTN_TOOL_PEN,
                                value=1),
            libevdev.InputEvent(libevdev.EV_SYN.SYN_REPORT,
                                value=0),
        ])
        # Says that the pen it out of range of the tablet. Useful
        # to make sure you can move your mouse, and to avoid
        # strange things during the first draw.
        uinput.send_events([
            libevdev.InputEvent(libevdev.EV_KEY.BTN_TOUCH,
                                value=0),
            libevdev.InputEvent(libevdev.EV_KEY.BTN_TOOL_PEN,
                                value=0),
            libevdev.InputEvent(libevdev.EV_SYN.SYN_REPORT,
                                value=0),
        ])
        print("Waiting 30s to let you:")
        print("1) open Gimp")
        print("2) Go to 'Edit/Input device' and configure the device 'Tablet alone' to 'Screen'.")
        print("3) Save and close the pop up")
        print("4) Create a new file (Ctrl-N)")
        print("5) Zoom and press 'tab' to have a drawing surface coverint most of the screen.")
        print("6) Switch to brush using 'p' key.")
        time.sleep(25)

        pc = 0
        direc = +1
        already_pressed_one = False
        for i in range(250):
            pc_ = pc/100
            val_x = int(pc_*10000 + (1-pc_)*17767)
            val_y = int(pc_*5000 + (1-pc_)*22767)
            val_pres = int(pc_*10 + (1-pc_)*6000)
            print("Will send: x={}, y={}, press={} (pc={})".format(
                val_x,
                val_y,
                val_pres,
                pc))
            uinput.send_events([
                libevdev.InputEvent(libevdev.EV_ABS.ABS_X,
                                    value=val_y),
                libevdev.InputEvent(libevdev.EV_ABS.ABS_Y,
                                    value=val_y),
                libevdev.InputEvent(libevdev.EV_ABS.ABS_PRESSURE,
                                    value=val_pres),
                libevdev.InputEvent(libevdev.EV_KEY.BTN_TOUCH,
                                    value=1),
                libevdev.InputEvent(libevdev.EV_KEY.BTN_STYLUS,
                                    value=0),
                libevdev.InputEvent(libevdev.EV_KEY.BTN_STYLUS2,
                                    value=0),
                libevdev.InputEvent(libevdev.EV_KEY.BTN_TOOL_PEN,
                                    value=1),
                libevdev.InputEvent(libevdev.EV_SYN.SYN_REPORT,
                                    value=0),
            ])
            pc += direc
            if not already_pressed_one:
                print("Press!")
                uinput.send_events([
                    # Pen close to device
                    libevdev.InputEvent(libevdev.EV_KEY.BTN_TOOL_PEN,
                                        value=1),
                    libevdev.InputEvent(libevdev.EV_KEY.BTN_TOUCH,
                                        value=1),
                    libevdev.InputEvent(libevdev.EV_SYN.SYN_REPORT,
                                        value=0),
                ])                
                already_pressed_one = True
            if pc >= 100 or pc <=0 :
                print("Release click.")
                uinput.send_events([
                    libevdev.InputEvent(libevdev.EV_KEY.BTN_TOUCH,
                                        value=0),
                    # Pen outside of the position
                    libevdev.InputEvent(libevdev.EV_KEY.BTN_TOOL_PEN,
                                        value=0),
                    libevdev.InputEvent(libevdev.EV_SYN.SYN_REPORT,
                                        value=0),
                ])
                if pc >= 100:
                    pc = 100
                    direc = -1
                if pc <= 0:
                    pc = 0
                    direc = +1
                time.sleep(5)
                print("Press!")
                uinput.send_events([
                    # Pen close to device
                    libevdev.InputEvent(libevdev.EV_KEY.BTN_TOOL_PEN,
                                        value=1),
                    libevdev.InputEvent(libevdev.EV_KEY.BTN_TOUCH,
                                        value=1),
                    libevdev.InputEvent(libevdev.EV_SYN.SYN_REPORT,
                                        value=0),
                ])
                already_pressed_one = True
            time.sleep(0.1)

    except KeyboardInterrupt:
        pass
    except OSError as e:
        print(e)


if __name__ == "__main__":
    if len(sys.argv) > 2:
        print("Usage: {}")
        sys.exit(1)
    main(sys.argv)

请注意,现在xinput test <id you get with xinput list>还显示压力:

$ xinput test 12
motion a[0]=4151295 a[1]=4151295 a[2]=241 
motion a[0]=4060671 a[1]=4060671 a[2]=226 
motion a[0]=3969535 a[1]=3969535 a[2]=211 
motion a[0]=3878399 a[1]=3878399 a[2]=196 
motion a[0]=3787775 a[1]=3787775 a[2]=181 
motion a[0]=3696639 a[1]=3696639 a[2]=166 
motion a[0]=3605503 a[1]=3605503 a[2]=151 
motion a[0]=3514879 a[1]=3514879 a[2]=137 
motion a[0]=3423743 a[1]=3423743 a[2]=122 
motion a[0]=3332607 a[1]=3332607 a[2]=107 
motion a[0]=3241983 a[1]=3241983 a[2]=92 
motion a[0]=3150847 a[1]=3150847 a[2]=77 
motion a[0]=3059711 a[1]=3059711 a[2]=62 
motion a[0]=2969087 a[1]=2969087 a[2]=47 
motion a[0]=2877951 a[1]=2877951 a[2]=32 
motion a[0]=2650623 a[1]=2650623 a[2]=17 
button release 1

相关内容