--- /dev/null
+#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;
+};