testing/tools/screenshot/gdk-screenshot.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/tools/screenshot/gdk-screenshot.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,158 @@
     1.4 +/*
     1.5 + * Copyright (c) 2009, The Mozilla Foundation
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions are met:
    1.10 + *     * Redistributions of source code must retain the above copyright
    1.11 + *       notice, this list of conditions and the following disclaimer.
    1.12 + *     * Redistributions in binary form must reproduce the above copyright
    1.13 + *       notice, this list of conditions and the following disclaimer in the
    1.14 + *       documentation and/or other materials provided with the distribution.
    1.15 + *     * Neither the name of the Mozilla Foundation nor the
    1.16 + *       names of its contributors may be used to endorse or promote products
    1.17 + *       derived from this software without specific prior written permission.
    1.18 + *
    1.19 + * THIS SOFTWARE IS PROVIDED BY The Mozilla Foundation ''AS IS'' AND ANY
    1.20 + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    1.21 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    1.22 + * DISCLAIMED. IN NO EVENT SHALL The Mozilla Foundation BE LIABLE FOR ANY
    1.23 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.24 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    1.25 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    1.26 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.27 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    1.28 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.29 + *
    1.30 + * Contributors:
    1.31 + *   Ted Mielczarek <ted.mielczarek@gmail.com>
    1.32 + *   Karl Tomlinson <karlt+@karlt.net>
    1.33 + */
    1.34 +/*
    1.35 + * gdk-screenshot.cpp: Save a screenshot of the root window in .png format.
    1.36 + *  If a filename is specified as the first argument on the commandline,
    1.37 + *  then the image will be saved to that filename. Otherwise, the image will
    1.38 + *  be written to stdout.
    1.39 + */
    1.40 +#include <gdk/gdk.h>
    1.41 +#include <gdk/gdkx.h>
    1.42 +#ifdef HAVE_LIBXSS
    1.43 +#include <X11/extensions/scrnsaver.h>
    1.44 +#endif
    1.45 +
    1.46 +#include <errno.h>
    1.47 +#include <stdio.h>
    1.48 +#include "mozilla/NullPtr.h"
    1.49 +
    1.50 +gboolean save_to_stdout(const gchar *buf, gsize count,
    1.51 +                        GError **error, gpointer data)
    1.52 +{
    1.53 +  size_t written = fwrite(buf, 1, count, stdout);
    1.54 +  if (written != count) {
    1.55 +    g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno),
    1.56 +                "Write to stdout failed: %s", g_strerror(errno));
    1.57 +    return FALSE;
    1.58 +  }
    1.59 +
    1.60 +  return TRUE;
    1.61 +}
    1.62 +
    1.63 +int main(int argc, char** argv)
    1.64 +{
    1.65 +  gdk_init(&argc, &argv);
    1.66 +
    1.67 +// TODO GTK3
    1.68 +#if defined(HAVE_LIBXSS) && (MOZ_WIDGET_GTK == 2)
    1.69 +  int event_base, error_base;
    1.70 +  Bool have_xscreensaver =
    1.71 +    XScreenSaverQueryExtension(GDK_DISPLAY(), &event_base, &error_base);
    1.72 +
    1.73 +  if (!have_xscreensaver) {
    1.74 +    fprintf(stderr, "No XScreenSaver extension on display\n");
    1.75 +  } else {
    1.76 +    XScreenSaverInfo* info = XScreenSaverAllocInfo();
    1.77 +    if (!info) {
    1.78 +      fprintf(stderr, "%s: Out of memory\n", argv[0]);
    1.79 +      return 1;
    1.80 +    }
    1.81 +    XScreenSaverQueryInfo(GDK_DISPLAY(), GDK_ROOT_WINDOW(), info);
    1.82 +
    1.83 +    const char* state;
    1.84 +    const char* til_or_since = nullptr;
    1.85 +    switch (info->state) {
    1.86 +    case ScreenSaverOff:
    1.87 +      state = "Off";
    1.88 +      til_or_since = "XScreenSaver will activate after another %lu seconds idle time\n";
    1.89 +      break;
    1.90 +    case ScreenSaverOn:
    1.91 +      state = "On";
    1.92 +      if (info->til_or_since) {
    1.93 +        til_or_since = "XScreenSaver idle timer activated %lu seconds ago\n";
    1.94 +      } else {
    1.95 +        til_or_since = "XScreenSaver idle activation is disabled\n";
    1.96 +      }
    1.97 +      break;
    1.98 +    case ScreenSaverDisabled:
    1.99 +      state = "Disabled";
   1.100 +      break;
   1.101 +    default:
   1.102 +      state = "unknown";
   1.103 +    }
   1.104 +
   1.105 +    const char* kind;
   1.106 +    switch (info->kind) {
   1.107 +    case ScreenSaverBlanked:
   1.108 +      kind = "Blanked";
   1.109 +      break;
   1.110 +    case ScreenSaverInternal:
   1.111 +      state = "Internal";
   1.112 +      break;
   1.113 +    case ScreenSaverExternal:
   1.114 +      state = "External";
   1.115 +      break;
   1.116 +    default:
   1.117 +      state = "unknown";
   1.118 +    }
   1.119 +
   1.120 +    fprintf(stderr, "XScreenSaver state: %s\n", state);
   1.121 +
   1.122 +    if (til_or_since) {
   1.123 +      fprintf(stderr, "XScreenSaver kind: %s\n", kind);
   1.124 +      fprintf(stderr, til_or_since, info->til_or_since / 1000);
   1.125 +    }
   1.126 +
   1.127 +    fprintf(stderr, "User input has been idle for %lu seconds\n", info->idle / 1000);
   1.128 +
   1.129 +    XFree(info);
   1.130 +  }
   1.131 +#endif
   1.132 +
   1.133 +  GdkWindow* window = gdk_get_default_root_window();
   1.134 +  GdkPixbuf* screenshot = nullptr;
   1.135 +// TODO GTK3
   1.136 +#if (MOZ_WIDGET_GTK == 2)
   1.137 +  screenshot = gdk_pixbuf_get_from_drawable(nullptr, window, nullptr,
   1.138 +                                            0, 0, 0, 0,
   1.139 +                                            gdk_screen_width(),
   1.140 +                                            gdk_screen_height());
   1.141 +#endif
   1.142 +  if (!screenshot) {
   1.143 +    fprintf(stderr, "%s: failed to create screenshot GdkPixbuf\n", argv[0]);
   1.144 +    return 1;
   1.145 +  }
   1.146 +
   1.147 +  GError* error = nullptr;
   1.148 +  if (argc > 1) {
   1.149 +    gdk_pixbuf_save(screenshot, argv[1], "png", &error, nullptr);
   1.150 +  } else {
   1.151 +    gdk_pixbuf_save_to_callback(screenshot, save_to_stdout, nullptr,
   1.152 +                                "png", &error, nullptr);
   1.153 +  }
   1.154 +  if (error) {
   1.155 +    fprintf(stderr, "%s: failed to write screenshot as png: %s\n",
   1.156 +            argv[0], error->message);
   1.157 +    return error->code;
   1.158 +  }
   1.159 +
   1.160 +  return 0;
   1.161 +}

mercurial