blob: a2870625e38332096d2f6e44baf6f0d1aa1403bb [file] [log] [blame]
#!/bin/bash
# Copyright 2020 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#### CATEGORY=Device management
### takes a screenshot and copies it to the host
## usage: fx screenshot [--landscape] [--png] [--trim] [-o <screencap_file>]
##
## This command invokes Fuchsia's screencap tool to create a screenshot.
## The result is written to screencap.ppm or screencap.png. To write to
## another filename, use the -o parameter.
## The --trim, --landscape and --png commands require ImageMagick to be
## installed. Unrecognized parameters will be passed to ssh.
##
## -o FILENAME Write to the given filename instead of screencap.ppm
## --png Create a .png file instead of a .ppm file
## --trim Remove black borders
## --landscape Rotate image ninety degrees
##
## Example usage:
## fx screenshot --trim --png --landscape
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/../lib/vars.sh || exit $?
fx-config-read
OUT=screencap.ppm
png_command=cat
enable_trim=0
enable_landscape=0
while [[ $# -ne 0 ]]; do
case $1 in
-h|--help)
fx-command-help
exit 0
;;
-o|--out)
shift
OUT=$1
;;
--png)
OUT=${OUT%.*}.png
png_command=pnmtopng
;;
--landscape)
enable_landscape=1
;;
--trim)
enable_trim=1
;;
*)
break
;;
esac
shift
done
set -o pipefail
TMP_FILE="$(mktemp "/tmp/fx_screenshot_XXXXX")"
fx-command-run shell "$@" screencap | $png_command > "$TMP_FILE"
if [[ $? -ne 0 ]]; then
rm -f "$TMP_FILE"
fx-error "Screenshot failed. Scenic is not running or Scenic failed to take screenshot."
exit 1
fi
mog_opts=
if [[ $enable_trim -ne 0 ]]; then
mog_opts="$mog_opts -trim +repage"
fi
if [[ $enable_landscape -ne 0 ]]; then
mog_opts="$mog_opts -rotate 90"
fi
if [[ ! -z $mog_opts ]]; then
# Also add a black border so it's easier to see the image when
# pasted into the bug database.
mogrify -bordercolor black -border 3 $mog_opts $TMP_FILE
fi
mv -f "$TMP_FILE" "$OUT"