2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
18 * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
19 * @brief Main source file of screenshooter
26 #include <tbm_surface.h>
28 const char *DEFAULT_SCREENSHOT_FILENAME = "screenshot.png";
29 const int DEFAULT_SCREENSHOT_WITDH = 1920;
30 const int DEFAULT_SCREENSHOT_HEIGHT = 1080;
38 Command parseArguments(int argc, const char **argv, int *width, int *height, char **filename) {
39 Command ret = Capture;
43 struct poptOption optionsTable[] = {
45 { "version", 'v', POPT_ARG_NONE, NULL, 'v', "show version of this binary and exit", NULL },
47 { "width", 'w', POPT_ARG_INT, width, 'w', "set width of captured image", NULL },
48 { "height", 'h', POPT_ARG_INT, height, 'h', "set height of captured image", NULL },
49 { "file", 'f', POPT_ARG_STRING, filename, 'f', "file name to store captured image in", NULL },
51 { NULL, 0, 0, NULL, 0, NULL, NULL }
54 optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
55 poptSetOtherOptionHelp(optCon, "command");
57 while ((c = poptGetNextOpt(optCon)) >= 0) {
66 fprintf(stderr, "%s: %s\n", poptBadOption(optCon, POPT_BADOPTION_NOALIAS), poptStrerror(c));
70 poptFreeContext(optCon);
75 void showVersion(void) {
76 printf("screenshooter %s\n", VERSION);
79 int capture(int width, int height, char *filename) {
80 efl_util_screenshot_h screenshot = NULL;
81 tbm_surface_h tbm_surface = NULL;
82 int ret = EXIT_SUCCESS;
84 screenshot = efl_util_screenshot_initialize(width, height);
85 if (screenshot == NULL) {
86 fprintf(stderr, "Screenshot initialization failed!\n");
90 tbm_surface = efl_util_screenshot_take_tbm_surface(screenshot);
92 fprintf(stderr, "Taking surface failed!\n");
94 goto deinitialize_screenshot;
97 tbm_surface_info_s surface_info;
98 ret = tbm_surface_get_info(tbm_surface, &surface_info);
99 if (ret != TBM_SURFACE_ERROR_NONE) {
100 fprintf(stderr, "Get surface info failed!\n");
102 goto destroy_surface;
105 int stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, width);
106 cairo_surface_t *surface = cairo_image_surface_create_for_data(surface_info.planes[0].ptr,
108 width, height, stride);
110 fprintf(stderr, "Create cairo surface failed!\n");
112 goto destroy_surface;
115 cairo_surface_write_to_png(surface, filename);
116 cairo_surface_destroy(surface);
117 fprintf(stdout, "Captured image: width = %d, height = %d, filename = %s\n", width, height, filename);
120 tbm_surface_destroy(tbm_surface);
122 deinitialize_screenshot:
123 efl_util_screenshot_deinitialize(screenshot);
128 int main(int argc, const char **argv) {
129 int ret = EXIT_SUCCESS;
130 int width = DEFAULT_SCREENSHOT_WITDH;
131 int height = DEFAULT_SCREENSHOT_HEIGHT;
132 char *filename = (char *)DEFAULT_SCREENSHOT_FILENAME;
134 Command command = parseArguments(argc, argv, &width, &height, &filename);
137 ret = capture(width, height, filename);