Search code examples
pythoncxlibubuntu-20.04multiple-mice

Manipulate second (auxiliary) mouse inputs with python Xlib or C-API on ubuntu 20.04 LTS


I am working on ubuntu 20.04 LTS with 2 computer mice (Microsoft Microsoft® 2.4GHz Transceiver v9.0 Mouse, id=11 and MX Vertical Mouse, id=31) attached to 2 different masters (Virtual core pointer, id=2 and Auxiliary pointer id=27), see xinput:

laptop:~/$ xinput
⎡ Virtual core pointer                      id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ Microsoft Microsoft® 2.4GHz Transceiver v9.0 Mouse    id=11   [slave  pointer  (2)]
⎜   ↳ Microsoft Microsoft® 2.4GHz Transceiver v9.0 Consumer Control id=12   [slave  pointer  (2)]
⎜   ↳ Microsoft Microsoft® 2.4GHz Transceiver v9.0 Consumer Control id=13   [slave  pointer  (2)]
⎜   ↳ ELAN9008:00 04F3:2A46                     id=17   [slave  pointer  (2)]
⎜   ↳ GDX1515:00 27C6:01F4 Mouse                id=19   [slave  pointer  (2)]
⎜   ↳ GDX1515:00 27C6:01F4 Touchpad             id=20   [slave  pointer  (2)]
⎜   ↳ GDX1515:00 27C6:01F4                      id=21   [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)]
    ↳ Video Bus                                 id=7    [slave  keyboard (3)]
    ↳ Video Bus                                 id=8    [slave  keyboard (3)]
    ↳ Power Button                              id=9    [slave  keyboard (3)]
    ↳ Microsoft Microsoft® 2.4GHz Transceiver v9.0  id=10   [slave  keyboard (3)]
    ↳ Microsoft Microsoft® 2.4GHz Transceiver v9.0 System Control   id=14   [slave  keyboard (3)]
    ↳ USB2.0 HD IR UVC WebCam: USB2.0           id=15   [slave  keyboard (3)]
    ↳ USB2.0 HD IR UVC WebCam: USB2.0           id=16   [slave  keyboard (3)]
    ↳ ELAN9008:00 04F3:2A46                     id=18   [slave  keyboard (3)]
    ↳ Intel HID events                          id=22   [slave  keyboard (3)]
    ↳ Asus WMI hotkeys                          id=23   [slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard              id=24   [slave  keyboard (3)]
    ↳ Microsoft Microsoft® 2.4GHz Transceiver v9.0 Consumer Control id=25   [slave  keyboard (3)]
    ↳ Microsoft Microsoft® 2.4GHz Transceiver v9.0 Consumer Control id=26   [slave  keyboard (3)]
⎡ Auxiliary pointer                         id=27   [master pointer  (28)]
⎜   ↳ Auxiliary XTEST pointer                   id=29   [slave  pointer  (27)]
⎜   ↳ MX Vertical Mouse                         id=31   [slave  pointer  (27)]
⎣ Auxiliary keyboard                        id=28   [master keyboard (27)]
    ↳ Auxiliary XTEST keyboard                  id=30   [slave  keyboard (28)]

Question:

How can I manipulate the Mx Vertical Mouse id=31 attached to the Auxiliary pointer (NOT Virtual core pointer) with python, e.g. using Xlib.ext.xtest.fake_input, pyautogui.moveTo, xaut etc., or alternatively directly with the C-API of the X Window System?

Issue:

Any of these Xlib-based python packages only control the Virtual core pointer-master with attached Microsoft Microsoft® 2.4GHz Transceiver v9.0 Mouse, id=11-slave. The packages all get hold of the initial Virtual core pointer-master by passing the Xlib.display.Display() to Xlib.ext.xtest.fake_input() function which does not take any further input to specify which pointer should be manipulated (...not sure about this last point though). I can get hold of the Auxiliary pointer-master with e.g. Xlib.ext.xinput.query_device(window, xinput.AllMasterDevices).devices[1] but then I have no clue where to plug it in. Also working with the Auxiliary pointer's source id id=31 did not help.

I'm thankful for any advice, ideas or solutions, thx in advance!


Solution

  • X11/extensions/XInput2.h::XIWarpPointer moves a device's pointer specified by deviceid

    The XIWarpPointer can be used to control the mouse cursor defined by the id of the master pointer (not device id of the mouse) to which the mouse/cursor is attached:

    #include <X11/extensions/XInput2.h>
    
    Bool XIWarpPointer( Display *display,
    int deviceid,
    Window src_w,
    Window dest_w,
    double src_x,
    double src_y,
    int src_width,
    int src_height,
    double dest_x,
    double dest_y);
    

    Here's one example how to integrate it in C or build a library to then interface with python using e.g. CFFI:

    enter image description here