* @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) {
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);
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;
}