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

129 lines
3.5 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-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-01-05 11:36:46 +01:00
def systemctl(action: str, unit: str, *, root: bool = False) -> bool:
2020-03-26 11:47:41 +01:00
"""Runs systemctl."""
command = ['/usr/bin/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-01-05 11:36:46 +01:00
def default_mode(notify: bool = False) -> None:
2020-03-26 11:47:41 +01:00
"""Restores all blocked input devices."""
systemctl('stop', LAPTOP_MODE_SERVICE, root=True)
systemctl('stop', TABLET_MODE_SERVICE, root=True)
if notify:
notify_send('Default mode.', 'The system is now in default mode.')
2021-01-05 11:36:46 +01:00
def laptop_mode(notify: bool = False) -> None:
2020-03-26 11:47:41 +01:00
"""Starts the laptop mode."""
systemctl('stop', TABLET_MODE_SERVICE, root=True)
systemctl('start', LAPTOP_MODE_SERVICE, root=True)
if notify:
notify_laptop_mode()
2021-01-05 11:36:46 +01:00
def tablet_mode(notify: bool = False) -> None:
2020-03-26 11:47:41 +01:00
"""Starts the tablet mode."""
systemctl('stop', LAPTOP_MODE_SERVICE, root=True)
systemctl('start', TABLET_MODE_SERVICE, root=True)
if notify:
notify_tablet_mode()
2021-01-05 11:36:46 +01:00
def toggle_mode(notify: bool = False) -> None:
"""Toggles between laptop and tablet mode."""
if systemctl('status', TABLET_MODE_SERVICE):
laptop_mode(notify=notify)
else:
tablet_mode(notify=notify)
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
2020-03-26 11:47:41 +01:00
2021-01-05 11:36:46 +01:00
if args.mode == 'toggle':
2020-03-26 11:47:41 +01:00
toggle_mode(notify=notify)
2021-01-05 11:36:46 +01:00
elif args.mode == 'default':
2020-03-26 11:47:41 +01:00
default_mode(notify=notify)
2021-01-05 11:36:46 +01:00
elif args.mode == 'laptop':
2020-03-26 11:47:41 +01:00
laptop_mode(notify=notify)
2021-01-05 11:36:46 +01:00
elif args.mode == 'tablet':
2020-03-26 11:47:41 +01:00
tablet_mode(notify=notify)
else:
print('Must specify a mode.', file=stderr, flush=True)