Add screen capturing functionality 28/95228/2 master
authorAdam Malinowski <a.malinowsk2@partner.samsung.com>
Fri, 10 Jun 2016 07:33:03 +0000 (09:33 +0200)
committerAdam Malinowski <a.malinowsk2@partner.samsung.com>
Thu, 17 Nov 2016 10:17:42 +0000 (11:17 +0100)
Change-Id: Iedd49df3f7e14e755225fa8d8067a679cb783c73

packaging/screenshooter.spec
src/CMakeLists.txt
src/main.c

index acbf852..6537e14 100644 (file)
@@ -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
 
index 61fb96d..e50460e 100644 (file)
@@ -20,6 +20,8 @@ FIND_PACKAGE(PkgConfig)
 
 PKG_CHECK_MODULES(SCREENSHOOTER_DEP
     REQUIRED
+    cairo
+    capi-ui-efl-util
     popt
     )
 
index c5382ef..b6b241a 100644 (file)
  * @brief       Main source file of screenshooter
  */
 
+#include <cairo.h>
+#include <efl_util.h>
 #include <popt.h>
 #include <stdlib.h>
+#include <tbm_surface.h>
+
+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;
   }