[gnome-utils] screenshot: use GNOME Shell DBus screenshot interface if available



commit 18a45af6bd242fdb42207e52525be9a304a6d7cd
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Mon Sep 26 16:52:28 2011 -0400

    screenshot: use GNOME Shell DBus screenshot interface if available
    
    If we're running under GNOME Shell, use its interface to get a
    screenshot instead of poking X directly.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=657330

 gnome-screenshot/Makefile.am        |    1 +
 gnome-screenshot/gnome-screenshot.c |    4 +-
 gnome-screenshot/gnome-screenshot.h |   29 ++++++++++
 gnome-screenshot/screenshot-utils.c |  102 ++++++++++++++++++++++++++++++-----
 4 files changed, 122 insertions(+), 14 deletions(-)
---
diff --git a/gnome-screenshot/Makefile.am b/gnome-screenshot/Makefile.am
index c53a2f5..d9a11c7 100644
--- a/gnome-screenshot/Makefile.am
+++ b/gnome-screenshot/Makefile.am
@@ -13,6 +13,7 @@ bin_PROGRAMS = \
 
 gnome_screenshot_SOURCES = \
 	gnome-screenshot.c		\
+	gnome-screenshot.h		\
 	cheese-flash.c			\
 	cheese-flash.h			\
 	screenshot-area-selection.c	\
diff --git a/gnome-screenshot/gnome-screenshot.c b/gnome-screenshot/gnome-screenshot.c
index 60b58d3..60f20ec 100644
--- a/gnome-screenshot/gnome-screenshot.c
+++ b/gnome-screenshot/gnome-screenshot.c
@@ -41,6 +41,7 @@
 #include <X11/Xutil.h>
 #include <canberra-gtk.h>
 
+#include "gnome-screenshot.h"
 #include "screenshot-area-selection.h"
 #include "screenshot-config.h"
 #include "screenshot-filename-builder.h"
@@ -58,7 +59,6 @@ static GdkPixbuf *screenshot = NULL;
 
 /* Global variables*/
 static CheeseFlash *flash = NULL;
-static GDBusConnection *connection = NULL;
 static gchar *icc_profile_base64 = NULL;
 
 static void
@@ -616,6 +616,8 @@ init_dbus_session (void)
   GError *error = NULL;
   gboolean retval = TRUE;
 
+  g_assert (connection == NULL);
+
   connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
 
   if (error != NULL)
diff --git a/gnome-screenshot/gnome-screenshot.h b/gnome-screenshot/gnome-screenshot.h
new file mode 100644
index 0000000..0e6f09f
--- /dev/null
+++ b/gnome-screenshot/gnome-screenshot.h
@@ -0,0 +1,29 @@
+/* gnome-screenshot.c - Take screenshots
+ *
+ * Copyright (C) 2001 Jonathan Blandford <jrb alum mit edu>
+ * Copyright (C) 2006 Emmanuele Bassi <ebassi gnome org>
+ * Copyright (C) 2008-2011 Cosimo Cecchi <cosimoc gnome org>
+ *
+ * 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
+ */
+
+#ifndef __GNOME_SCREENSHOT_H__
+#define __GNOME_SCREENSHOT_H__
+
+/* DBus session connection */
+GDBusConnection *connection;
+
+#endif /* __GNOME_SCREENSHOT_H__ */
diff --git a/gnome-screenshot/screenshot-utils.c b/gnome-screenshot/screenshot-utils.c
index 4346f0d..1eb5626 100644
--- a/gnome-screenshot/screenshot-utils.c
+++ b/gnome-screenshot/screenshot-utils.c
@@ -28,6 +28,7 @@
 #include <X11/extensions/shape.h>
 #endif
 
+#include "gnome-screenshot.h"
 #include "screenshot-config.h"
 #include "screenshot-utils.h"
 
@@ -348,19 +349,9 @@ screenshot_get_window_rect_coords (GdkWindow *window,
     }
 }
 
-void
-screenshot_get_window_rect (GdkWindow *window,
-                            GdkRectangle *rect)
-{
-  screenshot_get_window_rect_coords (window,
-                                     screenshot_config->include_border,
-                                     NULL,
-                                     rect);
-}
-
-GdkPixbuf *
-screenshot_get_pixbuf (GdkWindow    *window,
-                       GdkRectangle *rectangle)
+static GdkPixbuf *
+screenshot_get_pixbuf_fallback (GdkWindow *window,
+                                GdkRectangle *rectangle)
 {
   GdkWindow *root, *wm_window = NULL;
   GdkPixbuf *screenshot;
@@ -557,6 +548,91 @@ screenshot_get_pixbuf (GdkWindow    *window,
 }
 
 void
+screenshot_get_window_rect (GdkWindow *window,
+                            GdkRectangle *rect)
+{
+  screenshot_get_window_rect_coords (window,
+                                     screenshot_config->include_border,
+                                     NULL,
+                                     rect);
+}
+
+GdkPixbuf *
+screenshot_get_pixbuf (GdkWindow    *window,
+                       GdkRectangle *rectangle)
+{
+  GdkPixbuf *screenshot;
+  gchar *path, *filename;
+  const gchar *method_name;
+  GVariant *method_params;
+  GError *error = NULL;
+
+  path = g_build_filename (g_get_user_cache_dir (), "gnome-screenshot", NULL);
+  g_mkdir_with_parents (path, 0700);
+  filename = g_build_filename (path, "tmp.png", NULL);
+
+  if (screenshot_config->take_window_shot)
+    {
+      method_name = "ScreenshotWindow";
+      method_params = g_variant_new ("(bs)",
+                                     screenshot_config->include_border,
+                                     filename);
+    }
+  else if (rectangle != NULL)
+    {
+      method_name = "ScreenshotArea";
+      method_params = g_variant_new ("(iiiis)",
+                                     rectangle->x, rectangle->y,
+                                     rectangle->width, rectangle->height,
+                                     filename);
+    }
+  else
+    {
+      method_name = "Screenshot";
+      method_params = g_variant_new ("(s)", filename);
+    }
+
+  g_dbus_connection_call_sync (connection,
+                               "org.gnome.Shell",
+                               "/org/gnome/Shell",
+                               "org.gnome.Shell",
+                               method_name,
+                               method_params,
+                               NULL,
+                               G_DBUS_CALL_FLAGS_NONE,
+                               -1,
+                               NULL,
+                               &error);
+
+  if (error != NULL)
+    {
+      g_warning ("Unable to use GNOME Shell's builtin screenshot interface, "
+                 "resorting to fallback X11. Error: %s", error->message);
+      g_error_free (error);
+
+      screenshot = screenshot_get_pixbuf_fallback (window, rectangle);
+    }
+  else
+    {
+      screenshot = gdk_pixbuf_new_from_file (filename, &error);
+
+      if (error != NULL)
+        {
+          g_warning ("Unable to load GNOME Shell's builtin screenshot result, "
+                     "resorting to fallback X11. Error: %s", error->message);
+          g_error_free (error);
+
+          screenshot = screenshot_get_pixbuf_fallback (window, rectangle);
+        }
+    }
+
+  g_free (path);
+  g_free (filename);
+
+  return screenshot;
+}
+
+void
 screenshot_show_error_dialog (GtkWindow   *parent,
                               const gchar *message,
                               const gchar *detail)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]