chuwi-minibook-tablet-mode/tabletmode/cli.py

131 lines
3.7 KiB
Python
Raw Permalink Normal View History

2020-03-26 11:47:41 +01:00
"""Sets the system mode."""
2021-01-05 11:36:46 +01:00
from argparse import ArgumentParser, Namespace
from subprocess import DEVNULL
from subprocess import CalledProcessError
from subprocess import CompletedProcess
from subprocess import check_call
from subprocess import run
2020-03-26 11:47:41 +01:00
from sys import stderr
2021-01-05 11:36:46 +01:00
from typing import Optional
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 = 'Sets or toggles the system mode.'
LAPTOP_MODE_SERVICE = 'laptop-mode.service'
TABLET_MODE_SERVICE = 'tablet-mode.service'
2021-05-14 21:08:01 +02:00
SUDO = '/usr/bin/sudo'
2020-03-26 11:47:41 +01:00
2021-01-05 11:36:46 +01:00
def get_args() -> Namespace:
2020-03-26 11:47:41 +01:00
"""Returns the CLI arguments."""
parser = ArgumentParser(description=DESCRIPTION)
parser.add_argument(
'-n', '--notify', action='store_true',
help='display an on-screen notification')
subparsers = parser.add_subparsers(dest='mode')
subparsers.add_parser('toggle', help='toggles the system mode')
subparsers.add_parser('laptop', help='switch to laptop mode')
subparsers.add_parser('tablet', help='switch to tablet mode')
subparsers.add_parser('default', help='do not disable any input devices')
return parser.parse_args()
2021-05-14 21:08:01 +02:00
def systemctl(action: str, unit: str, *, root: bool = False,
sudo: str = SUDO) -> bool:
2020-03-26 11:47:41 +01:00
"""Runs systemctl."""
2021-05-14 21:08:01 +02:00
command = [sudo] if root else []
2021-01-05 11:36:46 +01:00
command += ['systemctl', action, unit]
2020-03-26 11:47:41 +01:00
try:
check_call(command, stdout=DEVNULL) # Return 0 on success.
except CalledProcessError:
return False
return True
2021-01-05 11:36:46 +01:00
def notify_send(summary: str, body: Optional[str] = None) -> CompletedProcess:
2020-03-26 11:47:41 +01:00
"""Sends the respective message."""
command = ['/usr/bin/notify-send', summary]
if body is not None:
command.append(body)
return run(command, stdout=DEVNULL, check=False)
2021-01-05 11:36:46 +01:00
def notify_laptop_mode() -> CompletedProcess:
2020-03-26 11:47:41 +01:00
"""Notifies about laptop mode."""
return notify_send('Laptop mode.', 'The system is now in laptop mode.')
2021-01-05 11:36:46 +01:00
def notify_tablet_mode() -> CompletedProcess:
2020-03-26 11:47:41 +01:00
"""Notifies about tablet mode."""
return notify_send('Tablet mode.', 'The system is now in tablet mode.')
2021-05-14 21:08:01 +02:00
def default_mode(notify: bool = False, *, sudo: str = SUDO) -> None:
2020-03-26 11:47:41 +01:00
"""Restores all blocked input devices."""
2021-05-14 21:08:01 +02:00
systemctl('stop', LAPTOP_MODE_SERVICE, root=True, sudo=sudo)
systemctl('stop', TABLET_MODE_SERVICE, root=True, sudo=sudo)
2020-03-26 11:47:41 +01:00
if notify:
notify_send('Default mode.', 'The system is now in default mode.')
2021-05-14 21:08:01 +02:00
def laptop_mode(notify: bool = False, *, sudo: str = SUDO) -> None:
2020-03-26 11:47:41 +01:00
"""Starts the laptop mode."""
2021-05-14 21:08:01 +02:00
systemctl('stop', TABLET_MODE_SERVICE, root=True, sudo=sudo)
systemctl('start', LAPTOP_MODE_SERVICE, root=True, sudo=sudo)
2020-03-26 11:47:41 +01:00
if notify:
notify_laptop_mode()
2021-05-14 21:08:01 +02:00
def tablet_mode(notify: bool = False, *, sudo: str = SUDO) -> None:
2020-03-26 11:47:41 +01:00
"""Starts the tablet mode."""
2021-05-14 21:08:01 +02:00
systemctl('stop', LAPTOP_MODE_SERVICE, root=True, sudo=sudo)
systemctl('start', TABLET_MODE_SERVICE, root=True, sudo=sudo)
2020-03-26 11:47:41 +01:00
if notify:
notify_tablet_mode()
2021-05-14 21:08:01 +02:00
def toggle_mode(notify: bool = False, *, sudo: str = SUDO) -> None:
"""Toggles between laptop and tablet mode."""
if systemctl('status', TABLET_MODE_SERVICE):
2021-05-14 21:08:01 +02:00
laptop_mode(notify=notify, sudo=sudo)
else:
2021-05-14 21:08:01 +02:00
tablet_mode(notify=notify, sudo=sudo)
2021-01-05 11:36:46 +01:00
def main() -> None:
2020-03-26 11:47:41 +01:00
"""Runs the main program."""
2021-01-05 11:36:46 +01:00
args = get_args()
config = load_config()
notify = config.get('notify', False) or args.notify
2021-05-14 21:08:01 +02:00
sudo = config.get('sudo', SUDO)
2020-03-26 11:47:41 +01:00
2021-01-05 11:36:46 +01:00
if args.mode == 'toggle':
2021-05-14 21:08:01 +02:00
toggle_mode(notify=notify, sudo=sudo)
2021-01-05 11:36:46 +01:00
elif args.mode == 'default':
2021-05-14 21:08:01 +02:00
default_mode(notify=notify, sudo=sudo)
2021-01-05 11:36:46 +01:00
elif args.mode == 'laptop':
2021-05-14 21:08:01 +02:00
laptop_mode(notify=notify, sudo=sudo)
2021-01-05 11:36:46 +01:00
elif args.mode == 'tablet':
2021-05-14 21:08:01 +02:00
tablet_mode(notify=notify, sudo=sudo)
2020-03-26 11:47:41 +01:00
else:
print('Must specify a mode.', file=stderr, flush=True)