Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 2009, The Mozilla Foundation |
michael@0 | 3 | * All rights reserved. |
michael@0 | 4 | * |
michael@0 | 5 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 6 | * modification, are permitted provided that the following conditions are met: |
michael@0 | 7 | * * Redistributions of source code must retain the above copyright |
michael@0 | 8 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 9 | * * Redistributions in binary form must reproduce the above copyright |
michael@0 | 10 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 11 | * documentation and/or other materials provided with the distribution. |
michael@0 | 12 | * * Neither the name of the Mozilla Foundation nor the |
michael@0 | 13 | * names of its contributors may be used to endorse or promote products |
michael@0 | 14 | * derived from this software without specific prior written permission. |
michael@0 | 15 | * |
michael@0 | 16 | * THIS SOFTWARE IS PROVIDED BY The Mozilla Foundation ''AS IS'' AND ANY |
michael@0 | 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
michael@0 | 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
michael@0 | 19 | * DISCLAIMED. IN NO EVENT SHALL The Mozilla Foundation BE LIABLE FOR ANY |
michael@0 | 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
michael@0 | 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
michael@0 | 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
michael@0 | 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
michael@0 | 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 26 | * |
michael@0 | 27 | * Contributors: |
michael@0 | 28 | * Ted Mielczarek <ted.mielczarek@gmail.com> |
michael@0 | 29 | * Karl Tomlinson <karlt+@karlt.net> |
michael@0 | 30 | */ |
michael@0 | 31 | /* |
michael@0 | 32 | * gdk-screenshot.cpp: Save a screenshot of the root window in .png format. |
michael@0 | 33 | * If a filename is specified as the first argument on the commandline, |
michael@0 | 34 | * then the image will be saved to that filename. Otherwise, the image will |
michael@0 | 35 | * be written to stdout. |
michael@0 | 36 | */ |
michael@0 | 37 | #include <gdk/gdk.h> |
michael@0 | 38 | #include <gdk/gdkx.h> |
michael@0 | 39 | #ifdef HAVE_LIBXSS |
michael@0 | 40 | #include <X11/extensions/scrnsaver.h> |
michael@0 | 41 | #endif |
michael@0 | 42 | |
michael@0 | 43 | #include <errno.h> |
michael@0 | 44 | #include <stdio.h> |
michael@0 | 45 | #include "mozilla/NullPtr.h" |
michael@0 | 46 | |
michael@0 | 47 | gboolean save_to_stdout(const gchar *buf, gsize count, |
michael@0 | 48 | GError **error, gpointer data) |
michael@0 | 49 | { |
michael@0 | 50 | size_t written = fwrite(buf, 1, count, stdout); |
michael@0 | 51 | if (written != count) { |
michael@0 | 52 | g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno), |
michael@0 | 53 | "Write to stdout failed: %s", g_strerror(errno)); |
michael@0 | 54 | return FALSE; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | return TRUE; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | int main(int argc, char** argv) |
michael@0 | 61 | { |
michael@0 | 62 | gdk_init(&argc, &argv); |
michael@0 | 63 | |
michael@0 | 64 | // TODO GTK3 |
michael@0 | 65 | #if defined(HAVE_LIBXSS) && (MOZ_WIDGET_GTK == 2) |
michael@0 | 66 | int event_base, error_base; |
michael@0 | 67 | Bool have_xscreensaver = |
michael@0 | 68 | XScreenSaverQueryExtension(GDK_DISPLAY(), &event_base, &error_base); |
michael@0 | 69 | |
michael@0 | 70 | if (!have_xscreensaver) { |
michael@0 | 71 | fprintf(stderr, "No XScreenSaver extension on display\n"); |
michael@0 | 72 | } else { |
michael@0 | 73 | XScreenSaverInfo* info = XScreenSaverAllocInfo(); |
michael@0 | 74 | if (!info) { |
michael@0 | 75 | fprintf(stderr, "%s: Out of memory\n", argv[0]); |
michael@0 | 76 | return 1; |
michael@0 | 77 | } |
michael@0 | 78 | XScreenSaverQueryInfo(GDK_DISPLAY(), GDK_ROOT_WINDOW(), info); |
michael@0 | 79 | |
michael@0 | 80 | const char* state; |
michael@0 | 81 | const char* til_or_since = nullptr; |
michael@0 | 82 | switch (info->state) { |
michael@0 | 83 | case ScreenSaverOff: |
michael@0 | 84 | state = "Off"; |
michael@0 | 85 | til_or_since = "XScreenSaver will activate after another %lu seconds idle time\n"; |
michael@0 | 86 | break; |
michael@0 | 87 | case ScreenSaverOn: |
michael@0 | 88 | state = "On"; |
michael@0 | 89 | if (info->til_or_since) { |
michael@0 | 90 | til_or_since = "XScreenSaver idle timer activated %lu seconds ago\n"; |
michael@0 | 91 | } else { |
michael@0 | 92 | til_or_since = "XScreenSaver idle activation is disabled\n"; |
michael@0 | 93 | } |
michael@0 | 94 | break; |
michael@0 | 95 | case ScreenSaverDisabled: |
michael@0 | 96 | state = "Disabled"; |
michael@0 | 97 | break; |
michael@0 | 98 | default: |
michael@0 | 99 | state = "unknown"; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | const char* kind; |
michael@0 | 103 | switch (info->kind) { |
michael@0 | 104 | case ScreenSaverBlanked: |
michael@0 | 105 | kind = "Blanked"; |
michael@0 | 106 | break; |
michael@0 | 107 | case ScreenSaverInternal: |
michael@0 | 108 | state = "Internal"; |
michael@0 | 109 | break; |
michael@0 | 110 | case ScreenSaverExternal: |
michael@0 | 111 | state = "External"; |
michael@0 | 112 | break; |
michael@0 | 113 | default: |
michael@0 | 114 | state = "unknown"; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | fprintf(stderr, "XScreenSaver state: %s\n", state); |
michael@0 | 118 | |
michael@0 | 119 | if (til_or_since) { |
michael@0 | 120 | fprintf(stderr, "XScreenSaver kind: %s\n", kind); |
michael@0 | 121 | fprintf(stderr, til_or_since, info->til_or_since / 1000); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | fprintf(stderr, "User input has been idle for %lu seconds\n", info->idle / 1000); |
michael@0 | 125 | |
michael@0 | 126 | XFree(info); |
michael@0 | 127 | } |
michael@0 | 128 | #endif |
michael@0 | 129 | |
michael@0 | 130 | GdkWindow* window = gdk_get_default_root_window(); |
michael@0 | 131 | GdkPixbuf* screenshot = nullptr; |
michael@0 | 132 | // TODO GTK3 |
michael@0 | 133 | #if (MOZ_WIDGET_GTK == 2) |
michael@0 | 134 | screenshot = gdk_pixbuf_get_from_drawable(nullptr, window, nullptr, |
michael@0 | 135 | 0, 0, 0, 0, |
michael@0 | 136 | gdk_screen_width(), |
michael@0 | 137 | gdk_screen_height()); |
michael@0 | 138 | #endif |
michael@0 | 139 | if (!screenshot) { |
michael@0 | 140 | fprintf(stderr, "%s: failed to create screenshot GdkPixbuf\n", argv[0]); |
michael@0 | 141 | return 1; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | GError* error = nullptr; |
michael@0 | 145 | if (argc > 1) { |
michael@0 | 146 | gdk_pixbuf_save(screenshot, argv[1], "png", &error, nullptr); |
michael@0 | 147 | } else { |
michael@0 | 148 | gdk_pixbuf_save_to_callback(screenshot, save_to_stdout, nullptr, |
michael@0 | 149 | "png", &error, nullptr); |
michael@0 | 150 | } |
michael@0 | 151 | if (error) { |
michael@0 | 152 | fprintf(stderr, "%s: failed to write screenshot as png: %s\n", |
michael@0 | 153 | argv[0], error->message); |
michael@0 | 154 | return error->code; |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | return 0; |
michael@0 | 158 | } |