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

走远了吗. 提交于 2019-11-30 00:17:30

You can do something like the following.

if [ "$SEARCH" = "" ]; then 
    exit 1
fi

ids=$(xinput --list | awk -v search="$SEARCH" \
    '$0 ~ search {match($0, /id=[0-9]+/);\
                  if (RSTART) \
                    print substr($0, RSTART+3, RLENGTH-3)\
                 }'\
     )

for i in $ids
do
    xinput set-prop $i 'Device Accel Profile' -1
    xinput set-prop $i 'Device Accel Constant Deceleration' 2.5
    xinput set-prop $i 'Device Accel Velocity Scaling' 1.0
done

So with this you first find all the IDs which match the search pattern $SEARCH and store them in $ids. Then you loop over the IDs and execute the three xinput commands.

You should make sure that $SEARCH does not match to much, since this could result in undesired behavior.

If the device name is always the same, in this case Logitech G700 Laser Mouse, you can search for matching device IDs by running

xinput list --id-only 'Logitech G700 Laser Mouse'

My 2 cents for a Logitech Gaming Mouse G502

#!/bin/sh


for id in `xinput --list|grep 'Logitech Gaming Mouse G502'|perl -ne 'while (m/id=(\d+)/g){print "$1\n";}'`; do
    # echo "setting device ID $id"
    notify-send -t 50000  'Mouse fixed'
    xinput set-prop $id "Device Accel Velocity Scaling" 1
    xinput set-prop $id "Device Accel Constant Deceleration" 3
done 
Schoenix

I did it like the Answer of Raphael Ahrens but used grep and sed instead of awk and The command is now something like my_script part_of_device_name part_of_property_name_(spaces with \space) value:

#!/bin/sh

DEVICE=$1
PROP=$2
VAL=$3

DEFAULT="Default"

if [ "$DEVICE" = "" ]; then 
    exit 1
fi

if [ "$PROP" = "" ]; then 
    exit 1
fi

if [ "$VAL" = "" ]; then 
    exit 1
fi

devlist=$(xinput --list | grep "$DEVICE" | sed -n 's/.*id=\([0-9]\+\).*/\1/p')

for dev in $devlist
do
    props=$(xinput list-props $dev | grep "$PROP" | grep -v $DEFAULT | sed -n 's/.*(\([0-9]\+\)).*/\1/p')

    for prop in $props
    do
        echo $prop
        xinput set-prop $dev $prop $VAL 
    done 
done

For the fun of it, same answer, but simpler way to parse and get ids:

for id in $(xinput list | grep 'Logitech USB Receiver' |  grep pointer | cut -d '=' -f 2 | cut -f 1); do xinput --set-button-map $id 3 2 1; done

Took me a while to figure out this can get the ids:

xinput | cut -d '=' -f 2 | cut -f 1

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!