blob: 3be5833d1908a77394928e098d2345c4dfb19e30 [file] [log] [blame] [edit]
#!/usr/bin/env python3
# Copyright 2022 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Factory Display Manager
Control display from command line. The target device must run with
factory toolkit enabled.
"""
import argparse
import sys
from typing import Optional
from cros.factory.goofy.plugins.display_manager import display_manager
from cros.factory.goofy.plugins import plugin_controller
from cros.factory.utils import json_utils
EXAMPLES = """Examples:
> display_manager list
List all display.
> display_manager set_mirror_mode
Enable mirror mode.
> display_manager set_mirror_mode --mode off
Disable mirror mode.
> display_manager set_main_display 12345678910
Set display with id 12345678910 to main display.
> display_manager --dut-ip 192.168.30.100 list
List all display with ip 192.168.30.100.
> display_manager display --image-url https://material.angular.io/assets/img/examples/shiba2.jpg
Display the url in the goofy UI.
> display_manager display --image-path /usr/share/chromeos-assets/animated_splash_screen/oobe_wallpaper.jpg
Display the image in the goofy UI.
> display_manager display
Stop displaying the image in the goofy UI.
"""
TIMEOUT_DESCRIPTION = (
'maximum number of seconds to wait, -1 means nonblocking.')
def ListDisplayInfo(manager: display_manager.DisplayManager, pretty: bool,
verbose: bool, **unused_kwargs):
"""Lists current display info in Chrome System Display API."""
display_info = manager.ListDisplayInfo(verbose)
print(json_utils.DumpStr(display_info, pretty, sort_keys=True))
def SetMirrorMode(manager: display_manager.DisplayManager, mode: str,
timeout: Optional[int], **unused_kwargs):
"""Sets mirror mode."""
err = manager.SetMirrorMode(mode, timeout)
if err is not None:
if isinstance(err, dict) and 'message' in err:
err_msg = err['message']
else:
err_msg = json_utils.DumpStr(err)
sys.exit(f'Failed to set mirror mode to {mode}: {err_msg}\n')
def SetMainDisplay(manager: display_manager.DisplayManager, display_id: str,
timeout: Optional[int], **unused_kwargs):
"""Sets main display."""
manager.SetMainDisplay(display_id, timeout)
def SetInternalDisplayRotation(manager: display_manager.DisplayManager,
degree: int, **unused_kwargs):
"""Sets internal display rotation."""
manager.SetInternalDisplayRotation(degree=degree)
def Display(manager: display_manager.DisplayManager, image_url: str,
image_path: str, **unused_kwargs):
"""Displays the image in the goofy UI.
Stops displaying if both image_url and image_path are not specified.
Note that your DUT must be able to access the url. Otherwise, it shows
nothing.
Note that the command looks for the file on the DUT. You must copy the file to
the DUT first if you want to run this on a remote device by using --dut-ip.
"""
if not image_path:
manager.DisplayImageUrl(image_url=image_url)
else:
manager.DisplayImageOnFilesystem(image_path=image_path)
def ParseArgument():
parser = argparse.ArgumentParser(
description=__doc__, epilog=EXAMPLES,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('--dut-ip', type=str, default='localhost')
parser.add_argument('--dut-port', type=str, default='4012')
subparsers = parser.add_subparsers(title='subcommands', dest='subcommand')
subparsers.required = True
subparser = subparsers.add_parser('list', help=ListDisplayInfo.__doc__)
subparser.set_defaults(subcommand=ListDisplayInfo)
subparser.add_argument('--no-pretty', dest='pretty', action='store_false',
help='Do not format the output.')
subparser.add_argument('--verbose', action='store_true',
help='Show full information.')
subparser = subparsers.add_parser('set_mirror_mode',
help=SetMirrorMode.__doc__)
subparser.set_defaults(subcommand=SetMirrorMode)
subparser.add_argument('--timeout', type=int, default=10,
help=TIMEOUT_DESCRIPTION)
subparser.add_argument(
'--mode', choices=[mode.name for mode in display_manager.MirrorMode],
default=display_manager.MirrorMode.normal.name)
subparser = subparsers.add_parser('set_main_display',
help=SetMainDisplay.__doc__)
subparser.set_defaults(subcommand=SetMainDisplay)
subparser.add_argument('--timeout', type=int, default=10,
help=TIMEOUT_DESCRIPTION)
subparser.add_argument('display_id', type=str)
subparser = subparsers.add_parser('set_internal_display_rotation',
help=SetInternalDisplayRotation.__doc__)
subparser.set_defaults(subcommand=SetInternalDisplayRotation)
subparser.add_argument('--degree', type=int)
subparser = subparsers.add_parser('display', help=Display.__doc__)
subparser.set_defaults(subcommand=Display)
subparser.add_argument('--image-url', type=str, default='')
subparser.add_argument('--image-path', type=str, default='')
return parser.parse_args()
def main() -> None:
args = ParseArgument()
manager: display_manager.DisplayManager = (
plugin_controller.GetPluginRPCProxy('display_manager.display_manager',
args.dut_ip, args.dut_port))
args.subcommand(manager, **args.__dict__)
if __name__ == '__main__':
main()