How to make a program that finds id's of xinput devices and sets xinput some settings

前端 未结 6 1852
天命终不由人
天命终不由人 2020-12-24 07:30

I have a G700 mouse connected to my computer. The problem with this mouse in Linux (Ubuntu) is that the sensitivity is very high. I also don\'t like mouse acceleration, so I

6条回答
  •  北海茫月
    2020-12-24 07:56

    Currently I am working on a script for a question over at askubuntu.com , which requires something similar, and I thought I'd share the simple python script that does pretty much what this question asks - find device ids and set properties:

    The Script

    from __future__ import print_function
    import subprocess
    import sys
    
    def run_cmd(cmdlist):
        """ Reusable function for running shell commands"""
        try:
            stdout = subprocess.check_output(cmdlist)
        except subprocess.CalledProcessError as pserror:
            sys.exit(1)
        else:
            if stdout:
                return stdout.decode().strip()
    
    def list_ids(mouse_name):
        """ Returns list of ids for the same device"""
        while True:
            mouse_ids = []
            for dev_id in run_cmd(['xinput','list','--id-only']).split('\n'):
                if mouse_name in run_cmd(['xinput','list','--name-only',dev_id]):
                    mouse_ids.append(dev_id)
            if mouse_ids:
               break
        return mouse_ids
    
    """dictionary of propery-value pairs"""
    props = { 'Device Accel Profile':'-1',
              'Device Accel Constant Deceleration':'2.5',
              'Device Accel Velocity Scaling':'1.0'   }
    
    """ set all property-value pair per each device id
        Uncomment the print function if you wish to know
        which ids have been altered for double-checking
        with xinput list-props"""
    for dev_id in list_ids(sys.argv[1]):
        # print(dev_id)
        for prop,value in props.items():
            run_cmd(['xinput','set-prop',dev_id,prop,value]) 
    

    Usage

    Provide quoted name of the mouse as first command line argument:

    python set_xinput_props.py 'Logitech G700 Laser Mouse'
    

    If everything is OK, script exits silently, with exit status of 0 , or 1 if any xinput command failed. You can uncomment print statement to show which ids are being configured (to later double check with xinput that values are set alright)

    How it works:

    Essentially, list_ids function lists all device ids , finds those devices that have the same name as user's mouse name and returns a list of those ids. Next we simply loop over each one of them, and of each one we set all the property-value pairs that are defined in props dictionary. Could be done with list of tuples as well, but dictionary is my choice here.

提交回复
热议问题