Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
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 #include <gdk/gdkx.h>
39 #ifdef HAVE_LIBXSS
40 #include <X11/extensions/scrnsaver.h>
41 #endif
43 #include <errno.h>
44 #include <stdio.h>
45 #include "mozilla/NullPtr.h"
47 gboolean save_to_stdout(const gchar *buf, gsize count,
48 GError **error, gpointer data)
49 {
50 size_t written = fwrite(buf, 1, count, stdout);
51 if (written != count) {
52 g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno),
53 "Write to stdout failed: %s", g_strerror(errno));
54 return FALSE;
55 }
57 return TRUE;
58 }
60 int main(int argc, char** argv)
61 {
62 gdk_init(&argc, &argv);
64 // TODO GTK3
65 #if defined(HAVE_LIBXSS) && (MOZ_WIDGET_GTK == 2)
66 int event_base, error_base;
67 Bool have_xscreensaver =
68 XScreenSaverQueryExtension(GDK_DISPLAY(), &event_base, &error_base);
70 if (!have_xscreensaver) {
71 fprintf(stderr, "No XScreenSaver extension on display\n");
72 } else {
73 XScreenSaverInfo* info = XScreenSaverAllocInfo();
74 if (!info) {
75 fprintf(stderr, "%s: Out of memory\n", argv[0]);
76 return 1;
77 }
78 XScreenSaverQueryInfo(GDK_DISPLAY(), GDK_ROOT_WINDOW(), info);
80 const char* state;
81 const char* til_or_since = nullptr;
82 switch (info->state) {
83 case ScreenSaverOff:
84 state = "Off";
85 til_or_since = "XScreenSaver will activate after another %lu seconds idle time\n";
86 break;
87 case ScreenSaverOn:
88 state = "On";
89 if (info->til_or_since) {
90 til_or_since = "XScreenSaver idle timer activated %lu seconds ago\n";
91 } else {
92 til_or_since = "XScreenSaver idle activation is disabled\n";
93 }
94 break;
95 case ScreenSaverDisabled:
96 state = "Disabled";
97 break;
98 default:
99 state = "unknown";
100 }
102 const char* kind;
103 switch (info->kind) {
104 case ScreenSaverBlanked:
105 kind = "Blanked";
106 break;
107 case ScreenSaverInternal:
108 state = "Internal";
109 break;
110 case ScreenSaverExternal:
111 state = "External";
112 break;
113 default:
114 state = "unknown";
115 }
117 fprintf(stderr, "XScreenSaver state: %s\n", state);
119 if (til_or_since) {
120 fprintf(stderr, "XScreenSaver kind: %s\n", kind);
121 fprintf(stderr, til_or_since, info->til_or_since / 1000);
122 }
124 fprintf(stderr, "User input has been idle for %lu seconds\n", info->idle / 1000);
126 XFree(info);
127 }
128 #endif
130 GdkWindow* window = gdk_get_default_root_window();
131 GdkPixbuf* screenshot = nullptr;
132 // TODO GTK3
133 #if (MOZ_WIDGET_GTK == 2)
134 screenshot = gdk_pixbuf_get_from_drawable(nullptr, window, nullptr,
135 0, 0, 0, 0,
136 gdk_screen_width(),
137 gdk_screen_height());
138 #endif
139 if (!screenshot) {
140 fprintf(stderr, "%s: failed to create screenshot GdkPixbuf\n", argv[0]);
141 return 1;
142 }
144 GError* error = nullptr;
145 if (argc > 1) {
146 gdk_pixbuf_save(screenshot, argv[1], "png", &error, nullptr);
147 } else {
148 gdk_pixbuf_save_to_callback(screenshot, save_to_stdout, nullptr,
149 "png", &error, nullptr);
150 }
151 if (error) {
152 fprintf(stderr, "%s: failed to write screenshot as png: %s\n",
153 argv[0], error->message);
154 return error->code;
155 }
157 return 0;
158 }