Commit f33c7af7 authored by Henrik Brix Andersen's avatar Henrik Brix Andersen Committed by Henrik Brix Andersen

rewrote a large part of the screenshot plug-in. The plug-in now no longer

2003-08-28 Henrik Brix Andersen <brix@gimp.org>

* plug-ins/common/screenshot.c: rewrote a large part of the
screenshot plug-in. The plug-in now no longer depends on the xwd
utility, but uses Xlib/GDK functionality to obtain the screen shot.

The window to shoot is now selected prior to the time out. This
fixes bug #103852.

The ability to obtain a screen shot of a window without
decorations has had to go since this can not be done using plain
Xlib/GDK (one would have to depend on libXmu to do this).

The source is prepared for G_OS_WIN32 but the win32 specific code
is yet to be written.

* tools/pdbgen/pdb/plug_in.pdb (gimp_progress_update): improved the
inline documentation

* app/pdb/plug_in_cmds.c
* libgimp/gimpplugin_pdb.c: regenerated
parent edf64d16
2003-08-28 Henrik Brix Andersen <brix@gimp.org>
* plug-ins/common/screenshot.c: rewrote a large part of the
screenshot plug-in. The plug-in now no longer depends on the xwd
utility, but uses Xlib/GDK functionality to obtain the screen shot.
The window to shoot is now selected prior to the time out. This
fixes bug #103852.
The ability to obtain a screen shot of a window without
decorations has had to go since this can not be done using plain
Xlib/GDK (one would have to depend on libXmu to do this).
The source is prepared for G_OS_WIN32 but the win32 specific code
is yet to be written.
* tools/pdbgen/pdb/plug_in.pdb (gimp_progress_update): improved the
inline documentation
* app/pdb/plug_in_cmds.c
* libgimp/gimpplugin_pdb.c: regenerated
2003-08-28 Sven Neumann <sven@gimp.org> 2003-08-28 Sven Neumann <sven@gimp.org>
* app/core/gimpimage-convert.c (gimp_image_convert): free the * app/core/gimpimage-convert.c (gimp_image_convert): free the
...@@ -152,7 +152,7 @@ static ProcArg progress_update_inargs[] = ...@@ -152,7 +152,7 @@ static ProcArg progress_update_inargs[] =
{ {
GIMP_PDB_FLOAT, GIMP_PDB_FLOAT,
"percentage", "percentage",
"Percentage of progress completed" "Percentage of progress completed which must be between 0.0 and 1.0"
} }
}; };
......
...@@ -59,7 +59,7 @@ gimp_progress_init (const gchar *message) ...@@ -59,7 +59,7 @@ gimp_progress_init (const gchar *message)
/** /**
* gimp_progress_update: * gimp_progress_update:
* @percentage: Percentage of progress completed. * @percentage: Percentage of progress completed which must be between 0.0 and 1.0.
* *
* Updates the progress bar for the current plug-in. * Updates the progress bar for the current plug-in.
* *
......
/* /*
* ScreenShot plug-in * ScreenShot plug-in
* Copyright 1998-2000 Sven Neumann <sven@gimp.org> * Copyright 1998-2000 Sven Neumann <sven@gimp.org>
* Copyright 2003 Henrik Brix Andersen <brix@gimp.org>
* *
* Any suggestions, bug-reports or patches are very welcome. * Any suggestions, bug-reports or patches are very welcome.
* *
* This plug-in uses the X-utility xwd to grab an image from the screen
* and the xwd-plug-in created by Peter Kirchgessner (pkirchg@aol.com)
* to load this image into the gimp.
* Hence its nothing but a simple frontend to those utilities.
*/ */
/* The GIMP -- an image manipulation program /* The GIMP -- an image manipulation program
...@@ -30,56 +27,61 @@ ...@@ -30,56 +27,61 @@
#include "config.h" #include "config.h"
#include <stdio.h>
#include <errno.h>
#include <sys/wait.h>
#include <unistd.h>
#ifdef __EMX__
#include <process.h>
#endif
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <libgimp/gimp.h> #include <libgimp/gimp.h>
#include <libgimp/gimpui.h> #include <libgimp/gimpui.h>
#ifdef G_OS_WIN32
#include <windows.h>
#else /* ! G_OS_WIN32 */
#include <X11/Xlib.h>
#include <X11/cursorfont.h>
#include <gdk/gdkx.h>
#endif /* ! G_OS_WIN32 */
#include "libgimp/stdplugins-intl.h" #include "libgimp/stdplugins-intl.h"
/* Defines */ /* Defines */
#define PLUG_IN_NAME "plug_in_screenshot" #define PLUG_IN_NAME "plug_in_screenshot"
#ifndef XWD #ifdef __GNUC__
#define XWD "xwd" #ifdef GDK_NATIVE_WINDOW_POINTER
#if GLIB_SIZEOF_VOID_P != sizeof (guint32)
#warning window_id does not fit in PDB_INT32
#endif
#endif
#endif #endif
typedef struct typedef struct
{ {
gboolean root; gboolean root;
gchar *window_id; guint window_id;
guint delay; guint delay;
gboolean decor;
} ScreenShotValues; } ScreenShotValues;
static ScreenShotValues shootvals = static ScreenShotValues shootvals =
{ {
FALSE, /* root window */ FALSE, /* root window */
NULL, /* window ID */ 0, /* window ID */
0, /* delay */ 0, /* delay */
TRUE, /* decorations */
}; };
static void query (void); static void query (void);
static void run (const gchar *name, static void run (const gchar *name,
gint nparams, gint nparams,
const GimpParam *param, const GimpParam *param,
gint *nreturn_vals, gint *nreturn_vals,
GimpParam **return_vals); GimpParam **return_vals);
static GdkNativeWindow select_window (const GdkScreen *screen);
static gint32 create_image (const GdkPixbuf *pixbuf);
static void shoot (void); static void shoot (void);
static gboolean shoot_dialog (void); static gboolean shoot_dialog (void);
static void shoot_ok_callback (GtkWidget *widget, static void shoot_ok_callback (GtkWidget *widget,
gpointer data); gpointer data);
static void shoot_delay (gint32 delay); static void shoot_delay (gint32 delay);
static gboolean shoot_delay_callback (gpointer data); static gboolean shoot_delay_callback (gpointer data);
...@@ -95,10 +97,15 @@ GimpPlugInInfo PLUG_IN_INFO = ...@@ -95,10 +97,15 @@ GimpPlugInInfo PLUG_IN_INFO =
}; };
/* the image that will be returned */ /* the image that will be returned */
gint32 image_ID = -1; gint32 image_ID = -1;
gboolean run_flag = FALSE; gboolean run_flag = FALSE;
/* the screen on which we are running */
GdkScreen *cur_screen = NULL;
/* the window the user selected */
GdkNativeWindow selected_native;
/* Functions */ /* Functions */
...@@ -109,9 +116,9 @@ query (void) ...@@ -109,9 +116,9 @@ query (void)
{ {
static GimpParamDef args[] = static GimpParamDef args[] =
{ {
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" }, { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
{ GIMP_PDB_INT32, "root", "Root window { TRUE, FALSE }" }, { GIMP_PDB_INT32, "root", "Root window { TRUE, FALSE }" },
{ GIMP_PDB_STRING, "window_id", "Window id" } { GIMP_PDB_INT32, "window_id", "Window id" }
}; };
static GimpParamDef return_vals[] = static GimpParamDef return_vals[] =
...@@ -121,17 +128,14 @@ query (void) ...@@ -121,17 +128,14 @@ query (void)
gimp_install_procedure (PLUG_IN_NAME, gimp_install_procedure (PLUG_IN_NAME,
"Creates a screenshot of a single window or the whole screen", "Creates a screenshot of a single window or the whole screen",
"This extension serves as a simple frontend to the " "After specifying some options the user selects a window and "
"X-window utility xwd and the xwd-file-plug-in. " "a time out is started. At the end of the time out the window "
"After specifying some options, xwd is called, the " "is grabbed and the image is loaded into The GIMP. Alternatively "
"user selects a window, and the resulting image is " "the whole screen can be grabbed. When called non-interactively "
"loaded into the gimp. Alternatively the whole " "it may grab the root window or use the window-id passed as a parameter.",
"screen can be grabbed. When called non-interactively " "Sven Neumann <sven@gimp.org>, Henrik Brix Andersen <brix@gimp.org>",
"it may grab the root window or use the window-id " "1998 - 2003",
"passed as a parameter.", "v0.9.6 (2003/08/28)",
"Sven Neumann <sven@gimp.org>",
"1998 - 2000",
"v0.9.5 (2000/10/29)",
N_("<Toolbox>/File/Acquire/_Screen Shot..."), N_("<Toolbox>/File/Acquire/_Screen Shot..."),
NULL, NULL,
GIMP_PLUGIN, GIMP_PLUGIN,
...@@ -140,7 +144,7 @@ query (void) ...@@ -140,7 +144,7 @@ query (void)
args, return_vals); args, return_vals);
} }
static void static void
run (const gchar *name, run (const gchar *name,
gint nparams, gint nparams,
const GimpParam *param, const GimpParam *param,
...@@ -148,17 +152,17 @@ run (const gchar *name, ...@@ -148,17 +152,17 @@ run (const gchar *name,
GimpParam **return_vals) GimpParam **return_vals)
{ {
/* Get the runmode from the in-parameters */ /* Get the runmode from the in-parameters */
GimpRunMode run_mode = param[0].data.d_int32; GimpRunMode run_mode = param[0].data.d_int32;
/* status variable, use it to check for errors in invocation usualy only /* status variable, use it to check for errors in invocation usually only
* during non-interactive calling * during non-interactive calling
*/ */
GimpPDBStatusType status = GIMP_PDB_SUCCESS; GimpPDBStatusType status = GIMP_PDB_SUCCESS;
/* always return at least the status to the caller. */ /* always return at least the status to the caller. */
static GimpParam values[2]; static GimpParam values[2];
/* initialize the return of the status */ /* initialize the return of the status */
values[0].type = GIMP_PDB_STATUS; values[0].type = GIMP_PDB_STATUS;
values[0].data.d_status = status; values[0].data.d_status = status;
*nreturn_vals = 1; *nreturn_vals = 1;
...@@ -172,7 +176,7 @@ run (const gchar *name, ...@@ -172,7 +176,7 @@ run (const gchar *name,
case GIMP_RUN_INTERACTIVE: case GIMP_RUN_INTERACTIVE:
/* Possibly retrieve data from a previous run */ /* Possibly retrieve data from a previous run */
gimp_get_data (PLUG_IN_NAME, &shootvals); gimp_get_data (PLUG_IN_NAME, &shootvals);
shootvals.window_id = NULL; shootvals.window_id = 0;
/* Get information from the dialog */ /* Get information from the dialog */
if (!shoot_dialog ()) if (!shoot_dialog ())
...@@ -180,15 +184,17 @@ run (const gchar *name, ...@@ -180,15 +184,17 @@ run (const gchar *name,
break; break;
case GIMP_RUN_NONINTERACTIVE: case GIMP_RUN_NONINTERACTIVE:
if (nparams == 3) if (nparams == 3)
{ {
shootvals.root = param[1].data.d_int32; shootvals.root = param[1].data.d_int32;
shootvals.window_id = (gchar*) param[2].data.d_string; shootvals.window_id = param[2].data.d_int32;
shootvals.delay = 0; shootvals.delay = 0;
shootvals.decor = FALSE;
} }
else else
status = GIMP_PDB_CALLING_ERROR; status = GIMP_PDB_CALLING_ERROR;
if (!gdk_init_check (0, NULL))
status = GIMP_PDB_CALLING_ERROR;
break; break;
case GIMP_RUN_WITH_LAST_VALS: case GIMP_RUN_WITH_LAST_VALS:
...@@ -225,139 +231,255 @@ run (const gchar *name, ...@@ -225,139 +231,255 @@ run (const gchar *name,
values[1].data.d_image = image_ID; values[1].data.d_image = image_ID;
} }
values[0].data.d_status = status; values[0].data.d_status = status;
} }
/* Allow the user to select a window with the mouse */
/* The main ScreenShot function */ static GdkNativeWindow
static void select_window (const GdkScreen *screen)
shoot (void)
{ {
GimpParam *params; #ifdef G_OS_WIN32
gint retvals; /* MS Windows specific code goes here (yet to be written) */
gchar *tmpname;
gchar *xwdargv[7]; /* need a maximum of 7 arguments to xwd */ /* basically the code should grab the pointer using a crosshair
gdouble xres, yres; cursor, allow the user to click on a window and return the
gint pid; obtained HWND (as a GdkNativeWindow) - for more details consult
gint wpid; the X11 specific code below */
gint status;
gint i = 0; /* note to self: take a look at the winsnap plug-in for example
code */
/* get a temp name with the right extension */
tmpname = gimp_temp_name ("xwd"); #else /* ! G_OS_WIN32 */
/* X11 specific code */
/* construct the xwd arguments */ Display *x_dpy;
xwdargv[i++] = XWD; Cursor x_cursor;
xwdargv[i++] = "-out"; XEvent x_event;
xwdargv[i++] = tmpname; Window x_win;
if ( shootvals.root == TRUE ) Window x_root;
xwdargv[i++] = "-root"; gint x_scr;
else gint status;
gint buttons;
x_dpy = GDK_SCREEN_XDISPLAY (GDK_SCREEN (screen));
x_scr = GDK_SCREEN_XNUMBER (GDK_SCREEN (screen));
x_win = None;
x_root = RootWindow (x_dpy, x_scr);
x_cursor = XCreateFontCursor (x_dpy, XC_crosshair);
buttons = 0;
status = XGrabPointer (x_dpy, x_root, False,
ButtonPressMask|ButtonReleaseMask,
GrabModeSync, GrabModeAsync,
x_root, x_cursor, CurrentTime);
if (status != GrabSuccess)
{ {
if (shootvals.decor == TRUE ) g_message (_("Screen Shot: Error grabbing the pointer"));
xwdargv[i++] = "-frame"; return 0;
if (shootvals.window_id != NULL)
{
xwdargv[i++] = "-id";
xwdargv[i++] = shootvals.window_id;
}
} }
xwdargv[i] = NULL;
while ((x_win == None) || (buttons != 0))
#ifndef __EMX__
/* fork off a xwd process */
if ((pid = fork ()) < 0)
{ {
g_message ("fork() failed: %s", g_strerror (errno)); XAllowEvents (x_dpy, SyncPointer, CurrentTime);
return; XWindowEvent (x_dpy, x_root, ButtonPressMask|ButtonReleaseMask, &x_event);
switch (x_event.type)
{
case ButtonPress:
if (x_win == None)
{
x_win = x_event.xbutton.subwindow;
if (x_win == None)
x_win = x_root;
}
buttons++;
break;
case ButtonRelease:
if (buttons > 0)
buttons--;
break;
default:
g_assert_not_reached ();
}
} }
else if (pid == 0)
XUngrabPointer (x_dpy, CurrentTime);
return x_win;
#endif /* ! G_OS_WIN32 */
}
/* Create a GimpImage from a GdkPixbuf */
static gint32
create_image (const GdkPixbuf *pixbuf)
{
GimpPixelRgn pr;
GimpDrawable *drawable;
GimpParasite *parasite;
gint32 image;
gint32 layer;
gdouble xres, yres;
gchar *comment;
gint width, height;
gint rowstride;
gboolean status;
gchar *buf;
gint i;
width = gdk_pixbuf_get_width (GDK_PIXBUF (pixbuf));
height = gdk_pixbuf_get_height (GDK_PIXBUF (pixbuf));
image = gimp_image_new (width, height, GIMP_RGB);
layer = gimp_layer_new (image, _("Background"),
width, height,
GIMP_RGB_IMAGE, 100, GIMP_NORMAL_MODE);
gimp_image_add_layer (image, layer, 0);
drawable = gimp_drawable_get (layer);
gimp_pixel_rgn_init (&pr, drawable,
0, 0, width, height,
TRUE, FALSE);
/* copy the contents of the GdkPixbuf to the GimpDrawable */
rowstride = gdk_pixbuf_get_rowstride (GDK_PIXBUF (pixbuf));
buf = gdk_pixbuf_get_pixels (GDK_PIXBUF (pixbuf));
status = gimp_progress_init (_("Loading Screen Shot..."));
for (i = 0; i < height; i++)
{ {
execvp (XWD, xwdargv); gimp_pixel_rgn_set_row (&pr, buf, 0, i, width);
/* What are we doing here? exec must have failed */ buf += rowstride;
g_message ("exec failed: xwd: %s", g_strerror (errno)); /* update progress every 10 percent */
return; if (status && ((i + 1) * 100 / height) % 10 == 0)
status = gimp_progress_update ((i + 1.0) / height);
} }
else
#else /* __EMX__ */ gimp_progress_update (1.0);
pid = spawnvp (P_NOWAIT, XWD, xwdargv);
if (pid == -1) /* figure out the monitor resolution and set the image to it */
gimp_get_monitor_resolution (&xres, &yres);
gimp_image_set_resolution (image, xres, yres);
/* Set the default comment parasite */
comment = gimp_get_default_comment ();
if (comment)
{ {
g_message ("spawn failed: %s", g_strerror (errno)); parasite = gimp_parasite_new ("gimp-comment",
return; GIMP_PARASITE_PERSISTENT,
g_utf8_strlen (comment, -1) + 1,
comment);
gimp_image_parasite_attach (image_ID, parasite);
gimp_parasite_free (parasite);
g_free (comment);
} }
#endif
{
status = -1;
wpid = waitpid (pid, &status, 0);
if ((wpid < 0) || !WIFEXITED (status)) return image;
{ }
/* the tmpfile may have been created even if xwd failed */
unlink (tmpname); /* The main ScreenShot function */
g_free (tmpname);
g_message ("xwd didn't work"); static void
return; shoot (void)
} {
GdkWindow *window;
GdkPixbuf *screenshot;
GdkRectangle clip;
GdkPoint origin;
gint screen_w, screen_h;
/* use default screen if we are running non-interactively */
if (cur_screen == NULL)
cur_screen = gdk_screen_get_default ();
screen_w = gdk_screen_get_width (GDK_SCREEN (cur_screen));
screen_h = gdk_screen_get_height (GDK_SCREEN (cur_screen));
clip.x = 0;
clip.y = 0;
if (shootvals.root)
{
/* entire screen */
window = gdk_screen_get_root_window (GDK_SCREEN (cur_screen));
}
else
{
/* single window */
if (shootvals.window_id)
{
window = gdk_window_foreign_new (shootvals.window_id);
}
else
{
window = gdk_window_foreign_new (selected_native);
}
} }
/* now load the tmpfile using the xwd-plug-in */ if (!window)
params = gimp_run_procedure ("file_xwd_load",
&retvals,
GIMP_PDB_INT32, 1,
GIMP_PDB_STRING, tmpname,
GIMP_PDB_STRING, tmpname,
GIMP_PDB_END);
if (params[0].data.d_status == GIMP_PDB_SUCCESS)
{ {
image_ID = params[1].data.d_image; g_message (_("Screen Shot: Specified window not found"));
return;
} }
gimp_destroy_params (params, retvals);
/* get rid of the tmpfile */ gdk_drawable_get_size (GDK_WINDOW (window), &clip.width, &clip.height);
unlink (tmpname); gdk_window_get_origin (GDK_WINDOW (window), &origin.x, &origin.y);
g_free (tmpname);
if (image_ID != -1) /* do clipping */
if (origin.x < 0)
{ {