You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.6 KiB
Python
102 lines
2.6 KiB
Python
4 years ago
|
#!/usr/bin/python3
|
||
|
# -*- coding: utf-8 -*-
|
||
4 years ago
|
# key-mapper - GUI for device specific keyboard mappings
|
||
4 years ago
|
# Copyright (C) 2020 sezanzeb <proxima@hip70890b.de>
|
||
|
#
|
||
4 years ago
|
# This file is part of key-mapper.
|
||
4 years ago
|
#
|
||
4 years ago
|
# key-mapper is free software: you can redistribute it and/or modify
|
||
4 years ago
|
# it under the terms of the GNU General Public License as published by
|
||
|
# the Free Software Foundation, either version 3 of the License, or
|
||
|
# (at your option) any later version.
|
||
|
#
|
||
4 years ago
|
# key-mapper is distributed in the hope that it will be useful,
|
||
4 years ago
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
4 years ago
|
# along with key-mapper. If not, see <https://www.gnu.org/licenses/>.
|
||
4 years ago
|
|
||
|
|
||
|
"""Helperfunctions to find device ids, names, and to load configs."""
|
||
|
|
||
|
|
||
4 years ago
|
import os
|
||
|
import subprocess
|
||
4 years ago
|
|
||
4 years ago
|
from keymapper.logger import logger
|
||
4 years ago
|
from keymapper.config import get_config
|
||
4 years ago
|
|
||
|
|
||
4 years ago
|
def get_xinput_list(type):
|
||
|
"""Run xinput and get the result as list.
|
||
|
|
||
|
Parameters
|
||
|
----------
|
||
|
type : string
|
||
|
Ine of 'id' or 'name'
|
||
|
"""
|
||
|
output = subprocess.check_output(['xinput', 'list', f'--{type}-only'])
|
||
|
return [line for line in output.decode().split('\n') if line != '']
|
||
|
|
||
|
|
||
4 years ago
|
def find_devices():
|
||
|
"""Get a list of (id, name) for each input device."""
|
||
|
# `xinput list`
|
||
4 years ago
|
ids = get_xinput_list('id')
|
||
|
names = get_xinput_list('name')
|
||
|
|
||
|
# names contains duplicates and "Virtual"-somethings, filter those
|
||
|
known_names = []
|
||
|
result = []
|
||
|
for (id, name) in zip(ids, names):
|
||
|
if name not in known_names and not name.startswith('Virtual'):
|
||
|
known_names.append(name)
|
||
|
result.append((id, name))
|
||
|
return result
|
||
4 years ago
|
|
||
|
|
||
|
def get_presets(device):
|
||
4 years ago
|
"""Get all configured presets for the device.
|
||
|
|
||
|
Parameters
|
||
|
----------
|
||
|
device : string
|
||
|
"""
|
||
4 years ago
|
print('get_presets not yet implemented')
|
||
|
return []
|
||
|
|
||
|
|
||
|
def create_preset(device, name=None):
|
||
|
"""Create an empty preset."""
|
||
|
existing_names = get_presets(device)
|
||
|
if name is None:
|
||
|
name = 'new preset'
|
||
|
|
||
|
# find a name that is not already taken
|
||
|
i = 1
|
||
|
while name in existing_names:
|
||
|
i += 1
|
||
|
name = f'new preset {i}'
|
||
|
|
||
|
# trigger the creation of a new config file:
|
||
|
get_config(device, name)
|
||
4 years ago
|
|
||
|
|
||
|
def get_mappings(device, preset):
|
||
4 years ago
|
"""Get all configured buttons of the preset.
|
||
|
|
||
|
Parameters
|
||
|
----------
|
||
|
device : string
|
||
|
preset : string
|
||
|
"""
|
||
4 years ago
|
pass
|
||
4 years ago
|
|
||
|
|
||
|
def find_newest_preset():
|
||
|
"""Get the device and present that was most recently modified."""
|
||
|
print('find_newest_preset not yet implemented')
|
||
|
return None, None
|