2013-09-30 01:59:22 +00:00
|
|
|
#include <gdk/gdk.h>
|
2004-08-23 21:22:55 +00:00
|
|
|
#include <gtk/gtk.h>
|
2013-09-30 01:59:22 +00:00
|
|
|
#include <gdkx.h>
|
2016-11-20 10:43:09 +00:00
|
|
|
#include <cairo-xlib.h>
|
2013-09-30 01:59:22 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <X11/extensions/shape.h>
|
|
|
|
|
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <locale.h>
|
2004-08-23 21:22:55 +00:00
|
|
|
#include "widgets.h"
|
2013-09-30 01:59:22 +00:00
|
|
|
#include "shadow.h"
|
|
|
|
|
|
|
|
#define MAXIMUM_WM_REPARENTING_DEPTH 4
|
|
|
|
#ifndef _
|
|
|
|
#define _(x) (x)
|
|
|
|
#endif
|
|
|
|
|
2014-02-13 16:37:46 +00:00
|
|
|
static void queue_show (void);
|
|
|
|
|
2013-09-30 01:59:22 +00:00
|
|
|
static Window
|
|
|
|
find_toplevel_window (Window xid)
|
|
|
|
{
|
|
|
|
Window root, parent, *children;
|
|
|
|
guint nchildren;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (XQueryTree (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xid, &root,
|
|
|
|
&parent, &children, &nchildren) == 0)
|
|
|
|
{
|
|
|
|
g_warning ("Couldn't find window manager window");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (root == parent)
|
|
|
|
return xid;
|
2004-08-23 21:22:55 +00:00
|
|
|
|
2013-09-30 01:59:22 +00:00
|
|
|
xid = parent;
|
|
|
|
}
|
|
|
|
while (TRUE);
|
|
|
|
}
|
2004-08-23 21:22:55 +00:00
|
|
|
|
2013-09-30 01:59:22 +00:00
|
|
|
static GdkPixbuf *
|
|
|
|
add_border_to_shot (GdkPixbuf *pixbuf)
|
2004-08-23 21:22:55 +00:00
|
|
|
{
|
2013-09-30 01:59:22 +00:00
|
|
|
GdkPixbuf *retval;
|
2014-02-13 17:08:03 +00:00
|
|
|
GdkColorspace colorspace;
|
|
|
|
int bits;
|
2013-09-30 01:59:22 +00:00
|
|
|
|
2014-02-13 17:08:03 +00:00
|
|
|
colorspace = gdk_pixbuf_get_colorspace (pixbuf);
|
|
|
|
bits = gdk_pixbuf_get_bits_per_sample (pixbuf);
|
|
|
|
retval = gdk_pixbuf_new (colorspace, TRUE, bits,
|
2013-09-30 01:59:22 +00:00
|
|
|
gdk_pixbuf_get_width (pixbuf) + 2,
|
|
|
|
gdk_pixbuf_get_height (pixbuf) + 2);
|
|
|
|
|
|
|
|
/* Fill with solid black */
|
|
|
|
gdk_pixbuf_fill (retval, 0xFF);
|
|
|
|
gdk_pixbuf_copy_area (pixbuf,
|
|
|
|
0, 0,
|
|
|
|
gdk_pixbuf_get_width (pixbuf),
|
|
|
|
gdk_pixbuf_get_height (pixbuf),
|
|
|
|
retval, 1, 1);
|
2004-08-23 21:22:55 +00:00
|
|
|
|
2013-09-30 01:59:22 +00:00
|
|
|
return retval;
|
2004-08-23 21:22:55 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 01:59:22 +00:00
|
|
|
static GdkPixbuf *
|
|
|
|
remove_shaped_area (GdkPixbuf *pixbuf,
|
|
|
|
Window window)
|
2004-08-23 21:22:55 +00:00
|
|
|
{
|
2013-09-30 01:59:22 +00:00
|
|
|
GdkPixbuf *retval;
|
|
|
|
XRectangle *rectangles;
|
|
|
|
int rectangle_count, rectangle_order;
|
|
|
|
int i;
|
2014-02-13 17:08:03 +00:00
|
|
|
GdkColorspace colorspace;
|
|
|
|
int bits;
|
2013-09-30 01:59:22 +00:00
|
|
|
|
2014-02-13 17:08:03 +00:00
|
|
|
colorspace = gdk_pixbuf_get_colorspace (pixbuf);
|
|
|
|
bits = gdk_pixbuf_get_bits_per_sample (pixbuf);
|
|
|
|
retval = gdk_pixbuf_new (colorspace, TRUE, bits,
|
2013-09-30 01:59:22 +00:00
|
|
|
gdk_pixbuf_get_width (pixbuf),
|
|
|
|
gdk_pixbuf_get_height (pixbuf));
|
|
|
|
|
|
|
|
gdk_pixbuf_fill (retval, 0);
|
|
|
|
rectangles = XShapeGetRectangles (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), window,
|
|
|
|
ShapeBounding, &rectangle_count, &rectangle_order);
|
|
|
|
|
|
|
|
for (i = 0; i < rectangle_count; i++)
|
2004-08-23 21:22:55 +00:00
|
|
|
{
|
2013-09-30 01:59:22 +00:00
|
|
|
int y, x;
|
|
|
|
|
|
|
|
for (y = rectangles[i].y; y < rectangles[i].y + rectangles[i].height; y++)
|
|
|
|
{
|
|
|
|
guchar *src_pixels, *dest_pixels;
|
|
|
|
|
|
|
|
src_pixels = gdk_pixbuf_get_pixels (pixbuf) +
|
|
|
|
y * gdk_pixbuf_get_rowstride (pixbuf) +
|
|
|
|
rectangles[i].x * (gdk_pixbuf_get_has_alpha (pixbuf) ? 4 : 3);
|
|
|
|
dest_pixels = gdk_pixbuf_get_pixels (retval) +
|
|
|
|
y * gdk_pixbuf_get_rowstride (retval) +
|
|
|
|
rectangles[i].x * 4;
|
|
|
|
|
|
|
|
for (x = rectangles[i].x; x < rectangles[i].x + rectangles[i].width; x++)
|
|
|
|
{
|
|
|
|
*dest_pixels++ = *src_pixels ++;
|
|
|
|
*dest_pixels++ = *src_pixels ++;
|
|
|
|
*dest_pixels++ = *src_pixels ++;
|
|
|
|
*dest_pixels++ = 255;
|
|
|
|
|
|
|
|
if (gdk_pixbuf_get_has_alpha (pixbuf))
|
|
|
|
src_pixels++;
|
|
|
|
}
|
|
|
|
}
|
2004-08-23 21:22:55 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 01:59:22 +00:00
|
|
|
return retval;
|
2004-08-23 21:22:55 +00:00
|
|
|
}
|
|
|
|
|
2014-02-13 18:26:17 +00:00
|
|
|
typedef enum {
|
|
|
|
DECOR_NONE,
|
|
|
|
DECOR_FRAME,
|
|
|
|
DECOR_WINDOW_FRAME
|
|
|
|
} DecorationType;
|
|
|
|
|
2013-09-30 01:59:22 +00:00
|
|
|
static GdkPixbuf *
|
2014-02-13 18:26:17 +00:00
|
|
|
take_window_shot (Window child,
|
|
|
|
DecorationType decor)
|
2004-08-23 21:22:55 +00:00
|
|
|
{
|
2016-11-20 10:43:09 +00:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
XWindowAttributes attrs;
|
2013-09-30 01:59:22 +00:00
|
|
|
Window xid;
|
2016-11-20 10:43:09 +00:00
|
|
|
Display *dpy;
|
2013-09-30 01:59:22 +00:00
|
|
|
gint x = 0, y = 0;
|
|
|
|
gint width, height;
|
|
|
|
|
|
|
|
GdkPixbuf *tmp, *tmp2;
|
2014-02-13 16:37:46 +00:00
|
|
|
GdkPixbuf *retval = NULL;
|
2013-09-30 01:59:22 +00:00
|
|
|
|
2014-02-13 18:26:17 +00:00
|
|
|
if (decor == DECOR_WINDOW_FRAME)
|
2013-09-30 01:59:22 +00:00
|
|
|
xid = find_toplevel_window (child);
|
|
|
|
else
|
|
|
|
xid = child;
|
|
|
|
|
2016-11-20 10:43:09 +00:00
|
|
|
dpy = gdk_x11_display_get_xdisplay (gdk_display_get_default ());
|
|
|
|
XGetWindowAttributes (dpy, xid, &attrs);
|
2013-09-30 01:59:22 +00:00
|
|
|
|
2016-11-20 10:43:09 +00:00
|
|
|
width = attrs.width;
|
|
|
|
height = attrs.height;
|
2013-09-30 01:59:22 +00:00
|
|
|
|
2016-11-20 10:43:09 +00:00
|
|
|
if (attrs.x < 0)
|
2004-08-23 21:22:55 +00:00
|
|
|
{
|
2016-11-20 10:43:09 +00:00
|
|
|
x = - attrs.x;
|
|
|
|
width = width + attrs.x;
|
2004-08-23 21:22:55 +00:00
|
|
|
}
|
|
|
|
|
2016-11-20 10:43:09 +00:00
|
|
|
if (attrs.y < 0)
|
2013-09-30 01:59:22 +00:00
|
|
|
{
|
2016-11-20 10:43:09 +00:00
|
|
|
y = - attrs.y;
|
|
|
|
height = height + attrs.y;
|
2013-09-30 01:59:22 +00:00
|
|
|
}
|
|
|
|
|
2016-11-20 10:43:09 +00:00
|
|
|
if (attrs.x + x + width > WidthOfScreen (DefaultScreenOfDisplay (dpy)))
|
|
|
|
width = WidthOfScreen (DefaultScreenOfDisplay (dpy)) - attrs.x - x;
|
2013-09-30 01:59:22 +00:00
|
|
|
|
2016-11-20 10:43:09 +00:00
|
|
|
if (attrs.y + y + height > HeightOfScreen (DefaultScreenOfDisplay (dpy)))
|
|
|
|
height = HeightOfScreen (DefaultScreenOfDisplay (dpy)) - attrs.y - y;
|
2013-09-30 01:59:22 +00:00
|
|
|
|
2016-11-20 10:43:09 +00:00
|
|
|
surface = cairo_xlib_surface_create (dpy,
|
|
|
|
xid,
|
|
|
|
attrs.visual,
|
|
|
|
attrs.width,
|
|
|
|
attrs.height);
|
|
|
|
tmp = gdk_pixbuf_get_from_surface (surface,
|
|
|
|
x, y,
|
|
|
|
width, height);
|
|
|
|
cairo_surface_destroy (surface);
|
2004-08-23 21:22:55 +00:00
|
|
|
|
2014-02-13 16:37:46 +00:00
|
|
|
if (tmp != NULL)
|
|
|
|
{
|
2014-02-13 18:26:17 +00:00
|
|
|
if (decor == DECOR_WINDOW_FRAME)
|
2014-02-13 16:37:46 +00:00
|
|
|
tmp2 = remove_shaped_area (tmp, xid);
|
2014-02-13 18:26:17 +00:00
|
|
|
else if (decor == DECOR_FRAME)
|
2014-02-13 16:37:46 +00:00
|
|
|
tmp2 = add_border_to_shot (tmp);
|
2014-02-13 18:26:17 +00:00
|
|
|
else
|
|
|
|
tmp2 = g_object_ref (tmp);
|
2014-02-13 16:37:46 +00:00
|
|
|
|
|
|
|
g_object_unref (tmp);
|
|
|
|
|
|
|
|
if (tmp2 != NULL)
|
|
|
|
{
|
|
|
|
retval = create_shadowed_pixbuf (tmp2);
|
|
|
|
g_object_unref (tmp2);
|
|
|
|
}
|
|
|
|
}
|
2013-09-30 01:59:22 +00:00
|
|
|
|
|
|
|
return retval;
|
2004-08-23 21:22:55 +00:00
|
|
|
}
|
|
|
|
|
2014-02-13 16:37:46 +00:00
|
|
|
static GList *toplevels;
|
|
|
|
static guint shot_id;
|
2014-02-13 18:26:17 +00:00
|
|
|
|
2016-11-20 10:43:09 +00:00
|
|
|
static gboolean
|
2018-03-20 10:40:08 +00:00
|
|
|
window_is_csd (GdkSurface *window)
|
2014-02-13 18:26:17 +00:00
|
|
|
{
|
2020-03-03 20:25:41 +00:00
|
|
|
return TRUE;
|
2014-02-13 18:26:17 +00:00
|
|
|
}
|
2014-02-13 16:37:46 +00:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
shoot_one (WidgetInfo *info)
|
2004-08-23 21:22:55 +00:00
|
|
|
{
|
2018-03-20 10:40:08 +00:00
|
|
|
GdkSurface *window;
|
2014-02-13 16:37:46 +00:00
|
|
|
XID id;
|
2013-09-30 01:59:22 +00:00
|
|
|
GdkPixbuf *screenshot = NULL;
|
2014-02-13 18:26:17 +00:00
|
|
|
DecorationType decor = DECOR_FRAME;
|
2004-08-23 21:22:55 +00:00
|
|
|
|
2014-02-13 16:37:46 +00:00
|
|
|
if (g_list_find (toplevels, info) == NULL)
|
|
|
|
{
|
|
|
|
g_warning ("Widget not found in queue");
|
2020-02-10 01:06:58 +00:00
|
|
|
exit (1);
|
2014-02-13 16:37:46 +00:00
|
|
|
}
|
2004-08-23 21:22:55 +00:00
|
|
|
|
2019-05-20 00:38:08 +00:00
|
|
|
window = gtk_native_get_surface (GTK_NATIVE (info->window));
|
2018-03-20 10:40:08 +00:00
|
|
|
id = gdk_x11_surface_get_xid (window);
|
2014-02-13 18:26:17 +00:00
|
|
|
if (window_is_csd (window))
|
|
|
|
decor = (info->include_decorations) ? DECOR_NONE : DECOR_WINDOW_FRAME;
|
|
|
|
screenshot = take_window_shot (id, decor);
|
2014-02-13 16:37:46 +00:00
|
|
|
if (screenshot != NULL)
|
2004-08-23 21:22:55 +00:00
|
|
|
{
|
|
|
|
char *filename;
|
2014-02-13 16:37:46 +00:00
|
|
|
filename = g_strdup_printf ("./%s.png", info->name);
|
|
|
|
gdk_pixbuf_save (screenshot, filename, "png", NULL, NULL);
|
|
|
|
g_free (filename);
|
|
|
|
g_object_unref (screenshot);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_warning ("unable to save shot of %s", info->name);
|
|
|
|
}
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (info->window));
|
2004-08-23 21:22:55 +00:00
|
|
|
|
2014-02-13 16:37:46 +00:00
|
|
|
shot_id = 0;
|
2004-08-23 21:22:55 +00:00
|
|
|
|
2014-02-13 16:37:46 +00:00
|
|
|
/* remove from the queue and try to load up another */
|
|
|
|
toplevels = g_list_remove (toplevels, info);
|
|
|
|
if (toplevels == NULL)
|
2020-02-10 01:06:58 +00:00
|
|
|
exit (0);
|
2014-02-13 16:37:46 +00:00
|
|
|
else
|
|
|
|
queue_show ();
|
2004-08-23 21:22:55 +00:00
|
|
|
|
2014-02-13 16:37:46 +00:00
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
2013-09-30 01:59:22 +00:00
|
|
|
|
2014-02-13 16:37:46 +00:00
|
|
|
static void
|
|
|
|
on_show (WidgetInfo *info)
|
|
|
|
{
|
|
|
|
if (shot_id != 0)
|
|
|
|
return;
|
2013-09-30 01:59:22 +00:00
|
|
|
|
2014-02-13 23:19:36 +00:00
|
|
|
shot_id = g_timeout_add (500, (GSourceFunc) shoot_one, info);
|
2014-02-13 16:37:46 +00:00
|
|
|
}
|
2013-09-30 01:59:22 +00:00
|
|
|
|
2014-02-13 16:37:46 +00:00
|
|
|
static gboolean
|
|
|
|
show_one (void)
|
|
|
|
{
|
|
|
|
WidgetInfo *info = toplevels->data;
|
2013-09-30 01:59:22 +00:00
|
|
|
|
2014-02-13 16:37:46 +00:00
|
|
|
g_message ("shooting %s", info->name);
|
|
|
|
|
|
|
|
g_signal_connect_swapped (info->window,
|
|
|
|
"show",
|
|
|
|
G_CALLBACK (on_show),
|
|
|
|
info);
|
|
|
|
|
|
|
|
gtk_widget_show (info->window);
|
|
|
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
queue_show (void)
|
|
|
|
{
|
|
|
|
g_idle_add ((GSourceFunc) show_one, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
/* If there's no DISPLAY, we silently error out. We don't want to break
|
|
|
|
* headless builds. */
|
2016-12-28 13:53:22 +00:00
|
|
|
if (! gtk_init_check ())
|
2014-02-13 16:37:46 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
toplevels = get_all_widgets ();
|
|
|
|
|
|
|
|
queue_show ();
|
2020-02-10 03:24:47 +00:00
|
|
|
while (TRUE)
|
|
|
|
g_main_context_iteration (NULL, TRUE);
|
2004-08-23 21:22:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|