Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 27e1a79b authored by Daniel Sandler's avatar Daniel Sandler
Browse files

On-device screenshot support.

This comes in the form of a command, `screenshot`, which
will read /dev/graphics/fb0 (in a manner very similar to
adb's framebuffer_service) and write to the specified PNG
file.

Additionally, dumpstate now accepts a -p flag (mnemonic:
"picture" or "png") that, when specified, will cause a
screenshot to be captured in the same directory as the
bugreport.

Future work: invoke `dumpstate -p` when the bugreport
keychord is pressed, giving users a convenient way to attach
screenshots to bug reports (or simply take screenshots at
all without developer tools).

Bug: 2216571 (and probably plenty of others)
Change-Id: I36afbc55a0308a7bc01112ef39c4c62777efb203
parent d664df2d
Loading
Loading
Loading
Loading
+22 −7
Original line number Diff line number Diff line
@@ -39,6 +39,8 @@
static char cmdline_buf[16384] = "(unknown)";
static const char *dump_traces_path = NULL;

static char screenshot_path[PATH_MAX] = "";

/* dumps the current system state to stdout */
static void dumpstate() {
    time_t now = time(NULL);
@@ -76,6 +78,12 @@ static void dumpstate() {
    dump_file("SLAB INFO", "/proc/slabinfo");
    dump_file("ZONEINFO", "/proc/zoneinfo");

    if (screenshot_path[0]) {
        LOGI("taking screenshot\n");
        run_command(NULL, 5, "su", "root", "screenshot", screenshot_path, NULL);
        LOGI("wrote screenshot: %s\n", screenshot_path);
    }

    run_command("SYSTEM LOG", 20, "logcat", "-v", "time", "-d", "*:v", NULL);

    /* show the traces we collected in main(), if that was done */
@@ -167,14 +175,15 @@ static void dumpstate() {
}

static void usage() {
    fprintf(stderr, "usage: dumpstate [-b file] [-d] [-e file] [-o file] [-s] "
            "[-z]\n"
            "  -b: play sound file instead of vibrate, at beginning of job\n"
            "  -d: append date to filename (requires -o)\n"
            "  -e: play sound file instead of vibrate, at end of job\n"
    fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s]\n"
            "  -o: write to file (instead of stdout)\n"
            "  -d: append date to filename (requires -o)\n"
            "  -z: gzip output (requires -o)\n"
            "  -p: capture screenshot to filename.png (requires -o)\n"
            "  -s: write output to control socket (for init)\n"
            "  -z: gzip output (requires -o)\n");
            "  -b: play sound file instead of vibrate, at beginning of job\n"
            "  -e: play sound file instead of vibrate, at end of job\n"
		);
}

int main(int argc, char *argv[]) {
@@ -184,6 +193,7 @@ int main(int argc, char *argv[]) {
    char* begin_sound = 0;
    char* end_sound = 0;
    int use_socket = 0;
    int do_fb = 0;

    LOGI("begin\n");

@@ -199,7 +209,7 @@ int main(int argc, char *argv[]) {
    dump_traces_path = dump_vm_traces();

    int c;
    while ((c = getopt(argc, argv, "b:de:ho:svz")) != -1) {
    while ((c = getopt(argc, argv, "b:de:ho:svzp")) != -1) {
        switch (c) {
            case 'b': begin_sound = optarg;  break;
            case 'd': do_add_date = 1;       break;
@@ -208,6 +218,7 @@ int main(int argc, char *argv[]) {
            case 's': use_socket = 1;        break;
            case 'v': break;  // compatibility no-op
            case 'z': do_compress = 6;       break;
            case 'p': do_fb = 1;             break;
            case '?': printf("\n");
            case 'h':
                usage();
@@ -244,6 +255,10 @@ int main(int argc, char *argv[]) {
            strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
            strlcat(path, date, sizeof(path));
        }
        if (do_fb) {
            strlcpy(screenshot_path, path, sizeof(screenshot_path));
            strlcat(screenshot_path, ".png", sizeof(screenshot_path));
        }
        strlcat(path, ".txt", sizeof(path));
        if (do_compress) strlcat(path, ".gz", sizeof(path));
        strlcpy(tmp_path, path, sizeof(tmp_path));
+16 −0
Original line number Diff line number Diff line
ifneq ($(TARGET_SIMULATOR),true)

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES := screenshot.c

LOCAL_MODULE := screenshot

LOCAL_SHARED_LIBRARIES := libcutils libz
LOCAL_STATIC_LIBRARIES := libpng
LOCAL_C_INCLUDES += external/zlib

include $(BUILD_EXECUTABLE)

endif
+118 −0
Original line number Diff line number Diff line
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>

#include <linux/fb.h>

#include <zlib.h>
#include <libpng/png.h>

#include "private/android_filesystem_config.h"

#define LOG_TAG "screenshot"
#include <utils/Log.h>

void take_screenshot(FILE *fb_in, FILE *fb_out) {
    int fb;
    char imgbuf[0x10000];
    struct fb_var_screeninfo vinfo;
    png_structp png;
    png_infop info;
    unsigned int r,c,rowlen;
    unsigned int bytespp,offset;

    fb = fileno(fb_in);
    if(fb < 0) {
        LOGE("failed to open framebuffer\n");
        return;
    }
    fb_in = fdopen(fb, "r");

    if(ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) < 0) {
        LOGE("failed to get framebuffer info\n");
        return;
    }
    fcntl(fb, F_SETFD, FD_CLOEXEC);

    png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (png == NULL) {
        LOGE("failed png_create_write_struct\n");
        fclose(fb_in);
        return;
    }

    png_init_io(png, fb_out);
    info = png_create_info_struct(png);
    if (info == NULL) {
        LOGE("failed png_create_info_struct\n");
        png_destroy_write_struct(&png, NULL);
        fclose(fb_in);
        return;
    }
    if (setjmp(png_jmpbuf(png))) {
        LOGE("failed png setjmp\n");
        png_destroy_write_struct(&png, NULL);
        fclose(fb_in);
        return;
    }

    bytespp = vinfo.bits_per_pixel / 8;
    png_set_IHDR(png, info,
        vinfo.xres, vinfo.yres, vinfo.bits_per_pixel / 4, 
        PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
        PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
    png_write_info(png, info);

    rowlen=vinfo.xres * bytespp;
    if (rowlen > sizeof(imgbuf)) {
        LOGE("crazy rowlen: %d\n", rowlen);
        png_destroy_write_struct(&png, NULL);
        fclose(fb_in);
        return;
    }

    offset = vinfo.xoffset * bytespp + vinfo.xres * vinfo.yoffset * bytespp;
    fseek(fb_in, offset, SEEK_SET);

    for(r=0; r<vinfo.yres; r++) {
        int len = fread(imgbuf, 1, rowlen, fb_in);
        if (len <= 0) break;
        png_write_row(png, (png_bytep)imgbuf);
    }

    png_write_end(png, info);
    fclose(fb_in);
    png_destroy_write_struct(&png, NULL);
}

int main(int argc, char**argv) {
    FILE *png = NULL;
    FILE *fb_in = NULL;
    if (argc < 2) {
        fprintf(stderr, "usage: screenshot filename.png\n");
        exit(1);
    }
    fb_in = fopen("/dev/graphics/fb0", "r");
    if (!fb_in) {
        fprintf(stderr, "error: could not read framebuffer\n");
        exit(1);
    }

    /* switch to non-root user and group */
    gid_t groups[] = { AID_LOG, AID_SDCARD_RW };
    setgroups(sizeof(groups)/sizeof(groups[0]), groups);
    setuid(AID_SHELL);

    png = fopen(argv[1], "w");
    if (!png) {
        fprintf(stderr, "error: writing file %s: %s\n", argv[1], strerror(errno));
        exit(1);
    }

    take_screenshot(fb_in, png);

    exit(0);
}