]> Pileus Git - ~andy/gtk/blob - docs/tools/shooter.c
GtkPlug: fix handling of key events for different layouts
[~andy/gtk] / docs / tools / shooter.c
1 #include <gdk/gdk.h>
2 #include <gtk/gtk.h>
3 #include <gdkx.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <sys/wait.h>
7 #include <unistd.h>
8 #include <X11/extensions/shape.h>
9
10 #include <gdk-pixbuf/gdk-pixbuf.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/wait.h>
14 #include <signal.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <locale.h>
20 #include "widgets.h"
21 #include "shadow.h"
22
23 #define MAXIMUM_WM_REPARENTING_DEPTH 4
24 #ifndef _
25 #define _(x) (x)
26 #endif
27
28 static Window
29 find_toplevel_window (Window xid)
30 {
31   Window root, parent, *children;
32   guint nchildren;
33
34   do
35     {
36       if (XQueryTree (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xid, &root,
37                       &parent, &children, &nchildren) == 0)
38         {
39           g_warning ("Couldn't find window manager window");
40           return 0;
41         }
42
43       if (root == parent)
44         return xid;
45
46       xid = parent;
47     }
48   while (TRUE);
49 }
50
51 static GdkPixbuf *
52 add_border_to_shot (GdkPixbuf *pixbuf)
53 {
54   GdkPixbuf *retval;
55
56   retval = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
57                            gdk_pixbuf_get_width (pixbuf) + 2,
58                            gdk_pixbuf_get_height (pixbuf) + 2);
59
60   /* Fill with solid black */
61   gdk_pixbuf_fill (retval, 0xFF);
62   gdk_pixbuf_copy_area (pixbuf,
63                         0, 0,
64                         gdk_pixbuf_get_width (pixbuf),
65                         gdk_pixbuf_get_height (pixbuf),
66                         retval, 1, 1);
67
68   return retval;
69 }
70
71 static GdkPixbuf *
72 remove_shaped_area (GdkPixbuf *pixbuf,
73                     Window     window)
74 {
75   GdkPixbuf *retval;
76   XRectangle *rectangles;
77   int rectangle_count, rectangle_order;
78   int i;
79
80   retval = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
81                            gdk_pixbuf_get_width (pixbuf),
82                            gdk_pixbuf_get_height (pixbuf));
83   
84   gdk_pixbuf_fill (retval, 0);
85   rectangles = XShapeGetRectangles (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), window,
86                                     ShapeBounding, &rectangle_count, &rectangle_order);
87
88   for (i = 0; i < rectangle_count; i++)
89     {
90       int y, x;
91
92       for (y = rectangles[i].y; y < rectangles[i].y + rectangles[i].height; y++)
93         {
94           guchar *src_pixels, *dest_pixels;
95
96           src_pixels = gdk_pixbuf_get_pixels (pixbuf) +
97             y * gdk_pixbuf_get_rowstride (pixbuf) +
98             rectangles[i].x * (gdk_pixbuf_get_has_alpha (pixbuf) ? 4 : 3);
99           dest_pixels = gdk_pixbuf_get_pixels (retval) +
100             y * gdk_pixbuf_get_rowstride (retval) +
101             rectangles[i].x * 4;
102
103           for (x = rectangles[i].x; x < rectangles[i].x + rectangles[i].width; x++)
104             {
105               *dest_pixels++ = *src_pixels ++;
106               *dest_pixels++ = *src_pixels ++;
107               *dest_pixels++ = *src_pixels ++;
108               *dest_pixels++ = 255;
109
110               if (gdk_pixbuf_get_has_alpha (pixbuf))
111                 src_pixels++;
112             }
113         }
114     }
115
116   return retval;
117 }
118
119 static GdkPixbuf *
120 take_window_shot (Window   child,
121                   gboolean include_decoration)
122 {
123   GdkWindow *window;
124   Window xid;
125   gint x_orig, y_orig;
126   gint x = 0, y = 0;
127   gint width, height;
128
129   GdkPixbuf *tmp, *tmp2;
130   GdkPixbuf *retval;
131
132   if (include_decoration)
133     xid = find_toplevel_window (child);
134   else
135     xid = child;
136
137   window = gdk_x11_window_foreign_new_for_display (gdk_display_get_default (), xid);
138
139   width = gdk_window_get_width (window);
140   height = gdk_window_get_height (window);
141   gdk_window_get_origin (window, &x_orig, &y_orig);
142
143   if (x_orig < 0)
144     {
145       x = - x_orig;
146       width = width + x_orig;
147       x_orig = 0;
148     }
149
150   if (y_orig < 0)
151     {
152       y = - y_orig;
153       height = height + y_orig;
154       y_orig = 0;
155     }
156
157   if (x_orig + width > gdk_screen_width ())
158     width = gdk_screen_width () - x_orig;
159
160   if (y_orig + height > gdk_screen_height ())
161     height = gdk_screen_height () - y_orig;
162
163   tmp = gdk_pixbuf_get_from_window (window,
164                                     x, y, width, height);
165
166   if (include_decoration)
167     tmp2 = remove_shaped_area (tmp, xid);
168   else
169     tmp2 = add_border_to_shot (tmp);
170
171   retval = create_shadowed_pixbuf (tmp2);
172   g_object_unref (tmp);
173   g_object_unref (tmp2);
174
175   return retval;
176 }
177
178 int main (int argc, char **argv)
179 {
180   GList *toplevels;
181   GdkPixbuf *screenshot = NULL;
182   GList *node;
183
184   /* If there's no DISPLAY, we silently error out.  We don't want to break
185    * headless builds. */
186   if (! gtk_init_check (&argc, &argv))
187     return 0;
188
189   toplevels = get_all_widgets ();
190
191   for (node = toplevels; node; node = g_list_next (node))
192     {
193       GtkAllocation allocation;
194       GdkWindow *window;
195       WidgetInfo *info;
196       XID id;
197       char *filename;
198
199       info = node->data;
200
201       gtk_widget_show (info->window);
202
203       window = gtk_widget_get_window (info->window);
204       gtk_widget_get_allocation (info->window, &allocation);
205
206       gtk_widget_show_now (info->window);
207       gtk_widget_queue_draw_area (info->window,
208                                   allocation.x, allocation.y,
209                                   allocation.width, allocation.height);
210       gdk_window_process_updates (window, TRUE);
211
212       while (gtk_events_pending ())
213         {
214           gtk_main_iteration ();
215         }
216       sleep (1);
217
218       while (gtk_events_pending ())
219         {
220           gtk_main_iteration ();
221         }
222
223       id = gdk_x11_window_get_xid (window);
224       screenshot = take_window_shot (id, info->include_decorations);
225       filename = g_strdup_printf ("./%s.png", info->name);
226       gdk_pixbuf_save (screenshot, filename, "png", NULL, NULL);
227       g_free(filename);
228       gtk_widget_hide (info->window);
229     }
230
231   return 0;
232 }