2020-03-26 11:47:41 +01:00
|
|
|
"""System mode daemon."""
|
|
|
|
|
2021-01-05 11:36:46 +01:00
|
|
|
from argparse import ArgumentParser, Namespace
|
2020-03-26 11:47:41 +01:00
|
|
|
from logging import DEBUG, INFO, basicConfig, getLogger
|
|
|
|
from subprocess import Popen
|
2021-01-05 11:36:46 +01:00
|
|
|
from typing import Iterable
|
2020-03-26 11:47:41 +01:00
|
|
|
|
2021-01-05 11:36:46 +01:00
|
|
|
from tabletmode.config import load_config
|
2020-03-26 11:47:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
DESCRIPTION = 'Setup system for laptop or tablet mode.'
|
|
|
|
EVTEST = '/usr/bin/evtest'
|
|
|
|
LOG_FORMAT = '[%(levelname)s] %(name)s: %(message)s'
|
|
|
|
LOGGER = getLogger('sysmoded')
|
|
|
|
|
|
|
|
|
2021-01-05 11:36:46 +01:00
|
|
|
def get_args() -> Namespace:
|
2020-03-26 11:47:41 +01:00
|
|
|
"""Parses the CLI arguments."""
|
|
|
|
|
|
|
|
parser = ArgumentParser(description=DESCRIPTION)
|
|
|
|
parser.add_argument(
|
|
|
|
'-v', '--verbose', action='store_true',
|
|
|
|
help='turn on verbose logging')
|
|
|
|
subparsers = parser.add_subparsers(dest='mode')
|
|
|
|
subparsers.add_parser('laptop', help='enable laptop mode')
|
|
|
|
subparsers.add_parser('tablet', help='enable tablet mode')
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2021-01-05 11:36:46 +01:00
|
|
|
def disable_device(device: str) -> Popen:
|
2020-03-26 11:47:41 +01:00
|
|
|
"""Disables the respective device via evtest."""
|
|
|
|
|
|
|
|
return Popen((EVTEST, '--grab', device))
|
|
|
|
|
|
|
|
|
2021-01-05 11:36:46 +01:00
|
|
|
def disable_devices(devices: Iterable[str]) -> None:
|
2020-03-26 11:47:41 +01:00
|
|
|
"""Disables the given devices."""
|
|
|
|
|
|
|
|
subprocesses = []
|
|
|
|
|
|
|
|
for device in devices:
|
|
|
|
subprocess = disable_device(device)
|
|
|
|
subprocesses.append(subprocess)
|
|
|
|
|
|
|
|
for subprocess in subprocesses:
|
|
|
|
subprocess.wait()
|
|
|
|
|
|
|
|
|
2021-01-05 11:36:46 +01:00
|
|
|
def get_devices(mode: str) -> Iterable[str]:
|
2020-03-26 11:47:41 +01:00
|
|
|
"""Reads the device from the config file."""
|
|
|
|
|
2021-01-05 11:36:46 +01:00
|
|
|
config = load_config()
|
|
|
|
devices = config.get(mode) or ()
|
2020-03-26 11:47:41 +01:00
|
|
|
|
|
|
|
if not devices:
|
|
|
|
LOGGER.info('No devices configured to disable.')
|
|
|
|
|
|
|
|
return devices
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Runs the main program."""
|
|
|
|
|
2021-01-05 11:36:46 +01:00
|
|
|
arguments = get_args()
|
2020-03-26 11:47:41 +01:00
|
|
|
level = DEBUG if arguments.verbose else INFO
|
|
|
|
basicConfig(level=level, format=LOG_FORMAT)
|
|
|
|
devices = get_devices(arguments.mode)
|
|
|
|
disable_devices(devices)
|