michael@0: /* michael@0: * Copyright (c) 2009, The Mozilla Foundation michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions are met: michael@0: * * Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * * Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * * Neither the name of the Mozilla Foundation nor the michael@0: * names of its contributors may be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY The Mozilla Foundation ''AS IS'' AND ANY michael@0: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED michael@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE michael@0: * DISCLAIMED. IN NO EVENT SHALL The Mozilla Foundation BE LIABLE FOR ANY michael@0: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES michael@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; michael@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND michael@0: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: * michael@0: * Contributors: michael@0: * Ted Mielczarek michael@0: * Karl Tomlinson michael@0: */ michael@0: /* michael@0: * gdk-screenshot.cpp: Save a screenshot of the root window in .png format. michael@0: * If a filename is specified as the first argument on the commandline, michael@0: * then the image will be saved to that filename. Otherwise, the image will michael@0: * be written to stdout. michael@0: */ michael@0: #include michael@0: #include michael@0: #ifdef HAVE_LIBXSS michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include "mozilla/NullPtr.h" michael@0: michael@0: gboolean save_to_stdout(const gchar *buf, gsize count, michael@0: GError **error, gpointer data) michael@0: { michael@0: size_t written = fwrite(buf, 1, count, stdout); michael@0: if (written != count) { michael@0: g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno), michael@0: "Write to stdout failed: %s", g_strerror(errno)); michael@0: return FALSE; michael@0: } michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: int main(int argc, char** argv) michael@0: { michael@0: gdk_init(&argc, &argv); michael@0: michael@0: // TODO GTK3 michael@0: #if defined(HAVE_LIBXSS) && (MOZ_WIDGET_GTK == 2) michael@0: int event_base, error_base; michael@0: Bool have_xscreensaver = michael@0: XScreenSaverQueryExtension(GDK_DISPLAY(), &event_base, &error_base); michael@0: michael@0: if (!have_xscreensaver) { michael@0: fprintf(stderr, "No XScreenSaver extension on display\n"); michael@0: } else { michael@0: XScreenSaverInfo* info = XScreenSaverAllocInfo(); michael@0: if (!info) { michael@0: fprintf(stderr, "%s: Out of memory\n", argv[0]); michael@0: return 1; michael@0: } michael@0: XScreenSaverQueryInfo(GDK_DISPLAY(), GDK_ROOT_WINDOW(), info); michael@0: michael@0: const char* state; michael@0: const char* til_or_since = nullptr; michael@0: switch (info->state) { michael@0: case ScreenSaverOff: michael@0: state = "Off"; michael@0: til_or_since = "XScreenSaver will activate after another %lu seconds idle time\n"; michael@0: break; michael@0: case ScreenSaverOn: michael@0: state = "On"; michael@0: if (info->til_or_since) { michael@0: til_or_since = "XScreenSaver idle timer activated %lu seconds ago\n"; michael@0: } else { michael@0: til_or_since = "XScreenSaver idle activation is disabled\n"; michael@0: } michael@0: break; michael@0: case ScreenSaverDisabled: michael@0: state = "Disabled"; michael@0: break; michael@0: default: michael@0: state = "unknown"; michael@0: } michael@0: michael@0: const char* kind; michael@0: switch (info->kind) { michael@0: case ScreenSaverBlanked: michael@0: kind = "Blanked"; michael@0: break; michael@0: case ScreenSaverInternal: michael@0: state = "Internal"; michael@0: break; michael@0: case ScreenSaverExternal: michael@0: state = "External"; michael@0: break; michael@0: default: michael@0: state = "unknown"; michael@0: } michael@0: michael@0: fprintf(stderr, "XScreenSaver state: %s\n", state); michael@0: michael@0: if (til_or_since) { michael@0: fprintf(stderr, "XScreenSaver kind: %s\n", kind); michael@0: fprintf(stderr, til_or_since, info->til_or_since / 1000); michael@0: } michael@0: michael@0: fprintf(stderr, "User input has been idle for %lu seconds\n", info->idle / 1000); michael@0: michael@0: XFree(info); michael@0: } michael@0: #endif michael@0: michael@0: GdkWindow* window = gdk_get_default_root_window(); michael@0: GdkPixbuf* screenshot = nullptr; michael@0: // TODO GTK3 michael@0: #if (MOZ_WIDGET_GTK == 2) michael@0: screenshot = gdk_pixbuf_get_from_drawable(nullptr, window, nullptr, michael@0: 0, 0, 0, 0, michael@0: gdk_screen_width(), michael@0: gdk_screen_height()); michael@0: #endif michael@0: if (!screenshot) { michael@0: fprintf(stderr, "%s: failed to create screenshot GdkPixbuf\n", argv[0]); michael@0: return 1; michael@0: } michael@0: michael@0: GError* error = nullptr; michael@0: if (argc > 1) { michael@0: gdk_pixbuf_save(screenshot, argv[1], "png", &error, nullptr); michael@0: } else { michael@0: gdk_pixbuf_save_to_callback(screenshot, save_to_stdout, nullptr, michael@0: "png", &error, nullptr); michael@0: } michael@0: if (error) { michael@0: fprintf(stderr, "%s: failed to write screenshot as png: %s\n", michael@0: argv[0], error->message); michael@0: return error->code; michael@0: } michael@0: michael@0: return 0; michael@0: }