ScreenshotActivity 53/154153/4
authorPawel Kurowski <p.kurowski2@samsung.com>
Sun, 8 Oct 2017 13:34:59 +0000 (15:34 +0200)
committerPawel Kurowski <p.kurowski2@samsung.com>
Fri, 13 Oct 2017 12:03:23 +0000 (14:03 +0200)
Change-Id: I40cf461f00ed1d6572c625fdd0935e74293b7346

CMakeLists.txt
org.tizen.universal-switch.xml
packaging/org.tizen.universal-switch.spec
src/MenuBuilder.cpp
src/ScreenshotActivity.cpp [new file with mode: 0644]

index 729118c..ad95be3 100644 (file)
@@ -27,7 +27,10 @@ pkg_check_modules(pkgs REQUIRED
     sqlite3
     tts
     tzsh-quickpanel
+    cairo
+    storage
 )
+# TODO remove cairo dependency, when there will be available screenshot service.
 
 SET(COMMON_FLAGS "-fdiagnostics-color=always -fPIC")
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON_FLAGS}")
index 3a1c8da..5911b7d 100644 (file)
@@ -17,6 +17,8 @@
         <privilege>http://tizen.org/privilege/keygrab</privilege>
         <privilege>http://tizen.org/privilege/inputgenerator</privilege>
         <privilege>http://tizen.org/privilege/volume.set</privilege>
+        <privilege>http://tizen.org/privilege/screenshot</privilege>
+        <privilege>http://tizen.org/privilege/mediastorage</privilege>
     </privileges>
 
 </manifest>
index 5573605..96fa7dd 100644 (file)
@@ -29,6 +29,8 @@ BuildRequires:  pkgconfig(capi-ui-efl-util)
 BuildRequires:  pkgconfig(gobject-2.0)
 BuildRequires:  pkgconfig(eldbus)
 BuildRequires:  pkgconfig(tzsh-quickpanel)
+BuildRequires:  pkgconfig(cairo)
+BuildRequires:  pkgconfig(storage)
 #Required for tests
 BuildRequires:  net-config
 %if %{with docs}
index f5330da..ef1a37e 100644 (file)
@@ -371,7 +371,8 @@ MenuBuilderImplementation::MenuBuilderImplementation()
                                                                                "START_LOCK_SCREEN");
        auto captureScreenshot          =       std::make_shared<MenuItemImplementation>(
                                                                                std::vector<std::string> {"IDS_CAPTURE_SCREENSHOT"},
-                                                                               defaultImg);
+                                                                               defaultImg,
+                                                                               "SCREENSHOT");
        auto rotateLeft                         =       std::make_shared<MenuItemImplementation>(
                                                                                std::vector<std::string> {"IDS_ROTATE_LEFT"},
                                                                                defaultImg);
diff --git a/src/ScreenshotActivity.cpp b/src/ScreenshotActivity.cpp
new file mode 100644 (file)
index 0000000..7224179
--- /dev/null
@@ -0,0 +1,114 @@
+#include "UIActivity.hpp"
+#include "ActivityFactory.hpp"
+#include "UniversalSwitchLog.hpp"
+#include "UniversalSwitch.hpp"
+#include "Singleton.hpp"
+#include "Window.hpp"
+
+#include <efl_util.h>
+#include <cairo.h>
+#include <storage/storage.h>
+
+#include <memory>
+#include <string>
+#include <chrono>
+#include <ctime>
+#include <iomanip>
+#include <sstream>
+
+/* TODO This class should be refactored, and should use some external functionality through, dbus or Application Control , when such funcionality will be avalible */
+
+class ScreenshotActivity : public UIActivity, private RegisterActivity<ScreenshotActivity>
+{
+public:
+       constexpr static const char *activityType = "SCREENSHOT";
+       ScreenshotActivity()
+               : UIActivity(activityType), screenshotHandle(nullptr), tbmSurface(nullptr), surface(nullptr),
+                 size(Singleton<UniversalSwitch>::instance().getMainWindow()->getDimensions().size)
+       {}
+
+       ~ScreenshotActivity()
+       {
+               if (surface)
+                       cairo_surface_destroy(surface);
+
+               if (tbmSurface)
+                       tbm_surface_destroy(tbmSurface);
+
+               if (screenshotHandle)
+                       efl_util_screenshot_deinitialize(screenshotHandle);
+       }
+
+       bool process() override
+       {
+               char *path = nullptr;
+               if (storage_get_directory(STORAGE_TYPE_INTERNAL, STORAGE_DIRECTORY_IMAGES, &path))
+                       ERROR("storage_get_directory failed");
+
+               if (path) {
+                       filename = path + ("/" + generateFileName());
+                       free(path);
+                       capture();
+               }
+               return true;
+       }
+
+private:
+       void capture()
+       {
+               screenshotHandle = efl_util_screenshot_initialize(size.width, size.height);
+               if (!screenshotHandle) {
+                       ERROR("Screenshot initialization failed");
+                       return;
+               }
+
+               tbmSurface = efl_util_screenshot_take_tbm_surface(screenshotHandle);
+               if (!tbmSurface) {
+                       ERROR("Taking surface failed");
+                       return;
+               }
+
+               tbm_surface_info_s surfaceInfo;
+               auto error = tbm_surface_get_info(tbmSurface, &surfaceInfo);
+               if (error != TBM_SURFACE_ERROR_NONE) {
+                       ERROR("Get surface info failed");
+                       return;
+               }
+
+               if (surfaceInfo.num_planes < 1) {
+                       ERROR("No planes in surface");
+                       return;
+               }
+
+               auto &plane = surfaceInfo.planes[0];
+               surface = cairo_image_surface_create_for_data(plane.ptr, CAIRO_FORMAT_ARGB32, size.width, size.height, plane.stride);
+               if (!surface) {
+                       ERROR("Create cairo surface failed");
+                       return;
+               }
+
+               error = cairo_surface_write_to_png(surface, filename.c_str());
+               if (error != CAIRO_STATUS_SUCCESS) {
+                       ERROR("Captured image failed %d", error);
+                       return;
+               }
+
+               DEBUG("Captured image: width = %d, height = %d, filename = %s", size.width, size.height, filename.c_str());
+       }
+
+       std::string generateFileName()
+       {
+               auto now = std::chrono::system_clock::now();
+               auto formated = std::chrono::system_clock::to_time_t(now);
+
+               std::stringstream ss;
+               ss << std::put_time(std::localtime(&formated), "%Y%m%d-%H%M%S") << ".png";
+               return ss.str();
+       }
+
+       efl_util_screenshot_h screenshotHandle;
+       tbm_surface_h tbmSurface;
+       cairo_surface_t *surface;
+       Size size;
+       std::string filename;
+};