From 7259bea2be5a603001ce20b936967a3e080813df Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Fri, 10 Jun 2016 09:33:03 +0200 Subject: [PATCH] Add screen capturing functionality Change-Id: Iedd49df3f7e14e755225fa8d8067a679cb783c73 --- packaging/screenshooter.spec | 3 ++ src/CMakeLists.txt | 2 + src/main.c | 92 ++++++++++++++++++++++++++++++++++---------- 3 files changed, 76 insertions(+), 21 deletions(-) diff --git a/packaging/screenshooter.spec b/packaging/screenshooter.spec index acbf852..6537e14 100644 --- a/packaging/screenshooter.spec +++ b/packaging/screenshooter.spec @@ -7,7 +7,10 @@ License: Apache-2.0 URL: http://www.tizen.org Source0: %{name}_%{version}.tar.gz BuildRequires: cmake +BuildRequires: pkgconfig(cairo) +BuildRequires: pkgconfig(capi-ui-efl-util) BuildRequires: pkgconfig(popt) +BuildRequires: pkgconfig(wayland-egl) BuildRoot: %{_tmppath}/%{name}_%{version}-build diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 61fb96d..e50460e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -20,6 +20,8 @@ FIND_PACKAGE(PkgConfig) PKG_CHECK_MODULES(SCREENSHOOTER_DEP REQUIRED + cairo + capi-ui-efl-util popt ) diff --git a/src/main.c b/src/main.c index c5382ef..b6b241a 100644 --- a/src/main.c +++ b/src/main.c @@ -19,14 +19,20 @@ * @brief Main source file of screenshooter */ +#include +#include #include #include +#include + +const char *DEFAULT_SCREENSHOT_FILENAME = "screenshot.png"; +const int DEFAULT_SCREENSHOT_WITDH = 1920; +const int DEFAULT_SCREENSHOT_HEIGHT = 1080; typedef enum { Capture, Version, - Error, - Exit + Error } Command; Command parseArguments(int argc, const char **argv, int *width, int *height, char **filename) { @@ -47,24 +53,18 @@ Command parseArguments(int argc, const char **argv, int *width, int *height, cha optCon = poptGetContext(NULL, argc, argv, optionsTable, 0); poptSetOtherOptionHelp(optCon, "command"); - if (argc < 2) { - poptPrintUsage(optCon, stderr, 0); - ret = Error; - } - if (ret != Exit) { - while ((c = poptGetNextOpt(optCon)) >= 0) { - switch (c) { - case 'v': - ret = Version; - break; - } + while ((c = poptGetNextOpt(optCon)) >= 0) { + switch (c) { + case 'v': + ret = Version; + break; } + } - if (c < -1) { - fprintf(stderr, "%s: %s\n", poptBadOption(optCon, POPT_BADOPTION_NOALIAS), poptStrerror(c)); - ret = Error; - } + if (c < -1) { + fprintf(stderr, "%s: %s\n", poptBadOption(optCon, POPT_BADOPTION_NOALIAS), poptStrerror(c)); + ret = Error; } poptFreeContext(optCon); @@ -76,21 +76,71 @@ void showVersion(void) { printf("screenshooter %s\n", VERSION); } +int capture(int width, int height, char *filename) { + efl_util_screenshot_h screenshot = NULL; + tbm_surface_h tbm_surface = NULL; + int ret = EXIT_SUCCESS; + + screenshot = efl_util_screenshot_initialize(width, height); + if (screenshot == NULL) { + fprintf(stderr, "Screenshot initialization failed!\n"); + return EXIT_FAILURE; + } + + tbm_surface = efl_util_screenshot_take_tbm_surface(screenshot); + if (!tbm_surface) { + fprintf(stderr, "Taking surface failed!\n"); + ret = EXIT_FAILURE; + goto deinitialize_screenshot; + } + + tbm_surface_info_s surface_info; + ret = tbm_surface_get_info(tbm_surface, &surface_info); + if (ret != TBM_SURFACE_ERROR_NONE) { + fprintf(stderr, "Get surface info failed!\n"); + ret = EXIT_FAILURE; + goto destroy_surface; + } + + int stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, width); + cairo_surface_t *surface = cairo_image_surface_create_for_data(surface_info.planes[0].ptr, + CAIRO_FORMAT_ARGB32, + width, height, stride); + if (!surface) { + fprintf(stderr, "Create cairo surface failed!\n"); + ret = EXIT_FAILURE; + goto destroy_surface; + } + + cairo_surface_write_to_png(surface, filename); + cairo_surface_destroy(surface); + fprintf(stdout, "Captured image: width = %d, height = %d, filename = %s\n", width, height, filename); + +destroy_surface: + tbm_surface_destroy(tbm_surface); + +deinitialize_screenshot: + efl_util_screenshot_deinitialize(screenshot); + + return ret; +} + int main(int argc, const char **argv) { - int width, height, ret = EXIT_SUCCESS; - char *filename; + int ret = EXIT_SUCCESS; + int width = DEFAULT_SCREENSHOT_WITDH; + int height = DEFAULT_SCREENSHOT_HEIGHT; + char *filename = (char *)DEFAULT_SCREENSHOT_FILENAME; Command command = parseArguments(argc, argv, &width, &height, &filename); switch (command) { case Capture: - // TODO: add cupturing function + ret = capture(width, height, filename); break; case Version: showVersion(); break; case Error: ret = EXIT_FAILURE; - case Exit: break; } -- 2.7.4