tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

gdk-screenshot.cpp (5004B)


      1 /*
      2 * Copyright (c) 2009, The Mozilla Foundation
      3 * All rights reserved.
      4 *
      5 * Redistribution and use in source and binary forms, with or without
      6 * modification, are permitted provided that the following conditions are met:
      7 *     * Redistributions of source code must retain the above copyright
      8 *       notice, this list of conditions and the following disclaimer.
      9 *     * Redistributions in binary form must reproduce the above copyright
     10 *       notice, this list of conditions and the following disclaimer in the
     11 *       documentation and/or other materials provided with the distribution.
     12 *     * Neither the name of the Mozilla Foundation nor the
     13 *       names of its contributors may be used to endorse or promote products
     14 *       derived from this software without specific prior written permission.
     15 *
     16 * THIS SOFTWARE IS PROVIDED BY The Mozilla Foundation ''AS IS'' AND ANY
     17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19 * DISCLAIMED. IN NO EVENT SHALL The Mozilla Foundation BE LIABLE FOR ANY
     20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26 *
     27 * Contributors:
     28 *   Ted Mielczarek <ted.mielczarek@gmail.com>
     29 *   Karl Tomlinson <karlt+@karlt.net>
     30 */
     31 /*
     32 * gdk-screenshot.cpp: Save a screenshot of the root window in .png format.
     33 *  If a filename is specified as the first argument on the commandline,
     34 *  then the image will be saved to that filename. Otherwise, the image will
     35 *  be written to stdout.
     36 */
     37 #include <gdk/gdk.h>
     38 #ifdef MOZ_WAYLAND
     39 #  include <gdk/gdkwayland.h>
     40 #  include "mozilla/GUniquePtr.h"
     41 #  include "mozilla/UniquePtrExtensions.h"
     42 #  include "mozilla/ScopeExit.h"
     43 #endif
     44 
     45 #include <errno.h>
     46 #include <stdio.h>
     47 #include <unistd.h>
     48 
     49 gboolean save_to_stdout(const gchar* buf, gsize count, GError** error,
     50                        gpointer data) {
     51  size_t written = fwrite(buf, 1, count, stdout);
     52  if (written != count) {
     53    g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno),
     54                "Write to stdout failed: %s", g_strerror(errno));
     55    return FALSE;
     56  }
     57 
     58  return TRUE;
     59 }
     60 
     61 #if defined(MOZ_WAYLAND)
     62 static GdkPixbuf* get_screenshot_gnome(char* aAppName) {
     63  char* path =
     64      g_build_filename(g_get_user_cache_dir(), "mozilla-screenshot", nullptr);
     65  g_mkdir_with_parents(path, 0700);
     66  char* tmpname = g_strdup_printf("mozilla-screen-%d.png", g_random_int());
     67  char* filename = g_build_filename(path, tmpname, nullptr);
     68 
     69  auto autoFree = mozilla::MakeScopeExit([&] {
     70    unlink(filename);
     71    g_free(path);
     72    g_free(tmpname);
     73    g_free(filename);
     74  });
     75 
     76  mozilla::GUniquePtr<GError> error;
     77  char* argv[] = {g_strdup("/usr/bin/gnome-screenshot"), g_strdup("-f"),
     78                  filename, nullptr};
     79  gboolean ret = g_spawn_sync(
     80      nullptr, argv, nullptr, GSpawnFlags(G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
     81      nullptr, nullptr, nullptr, nullptr, nullptr, getter_Transfers(error));
     82  if (!ret || error) {
     83    fprintf(stderr, "%s: g_spawn_sync() of gnome-screenshot failed: %s\n",
     84            aAppName, error ? error->message : "");
     85    return nullptr;
     86  }
     87 
     88  GdkPixbuf* screenshot = gdk_pixbuf_new_from_file(filename, nullptr);
     89  if (!screenshot) {
     90    fprintf(stderr, "%s: gdk_pixbuf_new_from_file %s failed\n", aAppName,
     91            filename);
     92  }
     93  return screenshot;
     94 }
     95 #endif
     96 
     97 int main(int argc, char** argv) {
     98  gdk_init(&argc, &argv);
     99 
    100  GdkPixbuf* screenshot = nullptr;
    101 
    102 #if defined(MOZ_WAYLAND)
    103  GdkDisplay* display = gdk_display_get_default();
    104  if (display && GDK_IS_WAYLAND_DISPLAY(display)) {
    105    screenshot = get_screenshot_gnome(argv[0]);
    106    if (!screenshot) {
    107      fprintf(stderr, "%s: failed to create screenshot Wayland/GdkPixbuf\n",
    108              argv[0]);
    109      return 1;
    110    }
    111  }
    112 #endif
    113  if (!screenshot) {
    114    GdkWindow* window = gdk_get_default_root_window();
    115    screenshot =
    116        gdk_pixbuf_get_from_window(window, 0, 0, gdk_window_get_width(window),
    117                                   gdk_window_get_height(window));
    118    if (!screenshot) {
    119      fprintf(stderr, "%s: failed to create screenshot X11/GdkPixbuf\n",
    120              argv[0]);
    121      return 1;
    122    }
    123  }
    124 
    125  GError* error = nullptr;
    126  if (argc > 1) {
    127    gdk_pixbuf_save(screenshot, argv[1], "png", &error, nullptr);
    128  } else {
    129    gdk_pixbuf_save_to_callback(screenshot, save_to_stdout, nullptr, "png",
    130                                &error, nullptr);
    131  }
    132  if (error) {
    133    fprintf(stderr, "%s: failed to write screenshot as png: %s\n", argv[0],
    134            error->message);
    135    return error->code;
    136  }
    137 
    138  return 0;
    139 }