1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
*
* Copyright (C) 2008 William Jon McCann <jmccann@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <X11/Xatom.h>
#include <gdk/gdkx.h>
#define SELECTION_NAME "_GDM_SCREENSHOT"
static GtkWidget *selection_window;
static gboolean debug_in;
/* Keep all config options for compatibility even if they are noops */
GOptionEntry options [] = {
{ "debug", 'd', 0, G_OPTION_ARG_NONE, &debug_in, N_("Debugging output"), NULL },
{ NULL }
};
/* To make sure there is only one screenshot taken at a time,
* (Imagine key repeat for the print screen key) we hold a selection
* until we are done taking the screenshot
*/
/* * Copyright (C) 2001-2006 Jonathan Blandford <jrb@alum.mit.edu> */
static gboolean
screenshot_grab_lock (void)
{
Atom selection_atom;
GdkCursor *cursor;
gboolean result = FALSE;
selection_atom = gdk_x11_get_xatom_by_name (SELECTION_NAME);
XGrabServer (GDK_DISPLAY ());
if (XGetSelectionOwner (GDK_DISPLAY(), selection_atom) != None) {
goto out;
}
selection_window = gtk_invisible_new ();
gtk_widget_show (selection_window);
if (!gtk_selection_owner_set (selection_window,
gdk_atom_intern (SELECTION_NAME, FALSE),
GDK_CURRENT_TIME)) {
gtk_widget_destroy (selection_window);
selection_window = NULL;
goto out;
}
cursor = gdk_cursor_new (GDK_WATCH);
gdk_pointer_grab (selection_window->window, FALSE, 0, NULL,
cursor, GDK_CURRENT_TIME);
gdk_cursor_unref (cursor);
result = TRUE;
out:
XUngrabServer (GDK_DISPLAY ());
gdk_flush ();
return result;
}
/* * Copyright (C) 2001-2006 Jonathan Blandford <jrb@alum.mit.edu> */
static void
screenshot_release_lock (void)
{
if (selection_window != NULL) {
gtk_widget_destroy (selection_window);
selection_window = NULL;
}
gdk_flush ();
}
/* * Copyright (C) 2001-2006 Jonathan Blandford <jrb@alum.mit.edu> */
static GdkPixbuf *
screenshot_get_pixbuf (Window w)
{
GdkWindow *window;
GdkWindow *root;
GdkPixbuf *screenshot;
int x_real_orig;
int y_real_orig;
int x_orig;
int y_orig;
int real_width;
int real_height;
int width;
int height;
window = gdk_window_foreign_new (w);
if (window == NULL) {
return NULL;
}
root = gdk_window_foreign_new (GDK_ROOT_WINDOW ());
gdk_drawable_get_size (window, &real_width, &real_height);
gdk_window_get_origin (window, &x_real_orig, &y_real_orig);
x_orig = x_real_orig;
y_orig = y_real_orig;
width = real_width;
height = real_height;
if (x_orig < 0) {
width = width + x_orig;
x_orig = 0;
}
if (y_orig < 0) {
height = height + y_orig;
y_orig = 0;
}
if (x_orig + width > gdk_screen_width ()) {
width = gdk_screen_width () - x_orig;
}
if (y_orig + height > gdk_screen_height ()) {
height = gdk_screen_height () - y_orig;
}
screenshot = gdk_pixbuf_get_from_drawable (NULL,
root,
NULL,
x_orig,
y_orig,
0,
0,
width,
height);
return screenshot;
}
static char *
screenshot_save (GdkPixbuf *pixbuf)
{
char *filename;
gboolean res;
GError *error;
const char *save_dir;
save_dir = LOCALSTATEDIR "/run/gdm";
filename = g_build_filename (save_dir,
"GDM-Screenshot.png",
NULL);
error = NULL;
res = gdk_pixbuf_save (pixbuf,
filename,
"png",
&error,
"tEXt::CREATOR", "gdm-screenshot",
NULL);
if (! res) {
g_warning ("Unable to save screenshot: %s", error->message);
g_error_free (error);
g_free (filename);
filename = NULL;
}
return filename;
}
static void
prepare_screenshot (void)
{
Window win;
GdkPixbuf *screenshot;
char *filename;
if (!screenshot_grab_lock ()) {
exit (0);
}
win = GDK_ROOT_WINDOW ();
screenshot = screenshot_get_pixbuf (win);
screenshot_release_lock ();
if (screenshot == NULL) {
/* FIXME: dialog? */
exit (1);
}
filename = screenshot_save (screenshot);
if (filename != NULL) {
g_print ("Wrote %s\n", filename);
/* FIXME: show a dialog or something */
g_free (filename);
}
}
int
main (int argc, char *argv[])
{
GOptionContext *ctx;
bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
/* Option parsing */
ctx = g_option_context_new ("- New GDM login");
g_option_context_add_main_entries (ctx, options, _("Main Options"));
g_option_context_parse (ctx, &argc, &argv, NULL);
g_option_context_free (ctx);
gtk_init (&argc, &argv);
prepare_screenshot ();
return 1;
}
|