diff --git a/cmds/screencap/screencap.cpp b/cmds/screencap/screencap.cpp index 4ed1c8e206740..44fb6b3201024 100644 --- a/cmds/screencap/screencap.cpp +++ b/cmds/screencap/screencap.cpp @@ -45,17 +45,23 @@ using namespace android; #define COLORSPACE_SRGB 1 #define COLORSPACE_DISPLAY_P3 2 -static void usage(const char* pname, DisplayId displayId) +static void usage(const char* pname, std::optional displayId) { + std::string defaultDisplayStr = ""; + if (!displayId) { + defaultDisplayStr = ""; + } else { + defaultDisplayStr = " (default: " + to_string(*displayId) + ")"; + } fprintf(stderr, "usage: %s [-hp] [-d display-id] [FILENAME]\n" " -h: this message\n" " -p: save the file as a png.\n" - " -d: specify the display ID to capture (default: %s)\n" + " -d: specify the display ID to capture%s\n" " see \"dumpsys SurfaceFlinger --display-id\" for valid display IDs.\n" "If FILENAME ends with .png it will be saved as a png.\n" "If FILENAME is not given, the results will be printed to stdout.\n", - pname, to_string(displayId).c_str()); + pname, defaultDisplayStr.c_str()); } static int32_t flinger2bitmapFormat(PixelFormat f) @@ -121,12 +127,12 @@ static status_t notifyMediaScanner(const char* fileName) { int main(int argc, char** argv) { - std::optional displayId = SurfaceComposerClient::getInternalDisplayId(); - if (!displayId) { - fprintf(stderr, "Failed to get ID for internal display\n"); + const std::vector ids = SurfaceComposerClient::getPhysicalDisplayIds(); + if (ids.empty()) { + fprintf(stderr, "Failed to get ID for any displays.\n"); return 1; } - + std::optional displayId; const char* pname = argv[0]; bool png = false; int c; @@ -136,18 +142,32 @@ int main(int argc, char** argv) png = true; break; case 'd': - displayId = DisplayId::fromValue(atoll(optarg)); + displayId = DisplayId::fromValue(atoll(optarg)); if (!displayId) { - fprintf(stderr, "Invalid display ID\n"); + fprintf(stderr, "Invalid display ID: %s\n", optarg); return 1; } break; case '?': case 'h': - usage(pname, *displayId); + if (ids.size() == 1) { + displayId = ids.front(); + } + usage(pname, displayId); return 1; } } + + if (!displayId) { // no diplsay id is specified + if (ids.size() == 1) { + displayId = ids.front(); + } else { + fprintf(stderr, "Please specify a display ID (-d display-id) for multi-display device.\n"); + fprintf(stderr, "See \"dumpsys SurfaceFlinger --display-id\" for valid display IDs.\n"); + return 1; + } + } + argc -= optind; argv += optind; @@ -169,7 +189,7 @@ int main(int argc, char** argv) } if (fd == -1) { - usage(pname, *displayId); + usage(pname, displayId); return 1; }