[gnome-screenshot] screenshot: don't look for the current GdkWindow if using the shell



commit cb91896ed910e2afa04a80daa2f80f3f50d4a7a9
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Wed Jan 25 15:34:49 2012 -0500

    screenshot: don't look for the current GdkWindow if using the shell
    
    We can limit the GdkWindow-backed code path to the fallback case.

 src/gnome-screenshot.c |   44 ++++-----------------
 src/screenshot-utils.c |  101 ++++++++++++++++++++++++++++++-----------------
 src/screenshot-utils.h |    4 +-
 3 files changed, 73 insertions(+), 76 deletions(-)
---
diff --git a/src/gnome-screenshot.c b/src/gnome-screenshot.c
index 6549684..3814070 100644
--- a/src/gnome-screenshot.c
+++ b/src/gnome-screenshot.c
@@ -313,35 +313,18 @@ build_filename_ready_cb (GObject *source,
   g_free (save_uri);
 }
 
-static GdkWindow *
-find_current_window (void)
-{
-  GdkWindow *window = NULL;
-
-  if (!screenshot_grab_lock ())
-    exit (0);
-
-  if (screenshot_config->take_window_shot)
-    {
-      window = screenshot_find_current_window ();
-
-      if (window == NULL)
-        screenshot_config->take_window_shot = FALSE;
-    }
-
-  if (window == NULL)
-    window = gdk_get_default_root_window ();
-
-  return window;
-}
-
 static void
 finish_prepare_screenshot (GdkRectangle *rectangle)
 {
-  GdkWindow *window;
+  screenshot = screenshot_get_pixbuf (rectangle);
 
-  window = find_current_window ();
-  screenshot = screenshot_get_pixbuf (window, rectangle);
+  if (screenshot == NULL)
+    {
+      screenshot_show_error_dialog (NULL,
+                                    _("Unable to take a screenshot of the current window"),
+                                    NULL);
+      exit (1);
+    }
 
   if (screenshot_config->take_window_shot)
     {
@@ -359,17 +342,6 @@ finish_prepare_screenshot (GdkRectangle *rectangle)
         }
     }
 
-  /* release now the lock, it was acquired when we were finding the window */
-  screenshot_release_lock ();
-
-  if (screenshot == NULL)
-    {
-      screenshot_show_error_dialog (NULL,
-                                    _("Unable to take a screenshot of the current window"),
-                                    NULL);
-      exit (1);
-    }
-
   if (screenshot_config->copy_to_clipboard)
     {
       screenshot_save_to_clipboard ();
diff --git a/src/screenshot-utils.c b/src/screenshot-utils.c
index 879b491..7fe6d28 100644
--- a/src/screenshot-utils.c
+++ b/src/screenshot-utils.c
@@ -25,6 +25,7 @@
 #include <glib/gi18n.h>
 #include <glib/gstdio.h>
 #include <canberra-gtk.h>
+#include <stdlib.h>
 
 #ifdef HAVE_X11_EXTENSIONS_SHAPE_H
 #include <X11/extensions/shape.h>
@@ -117,38 +118,6 @@ screenshot_window_is_desktop (GdkWindow *window)
       
 }
 
-GdkWindow *
-screenshot_find_current_window ()
-{
-  GdkWindow *current_window;
-  GdkDeviceManager *manager;
-  GdkDevice *device;
-
-  current_window = screenshot_find_active_window ();
-  manager = gdk_display_get_device_manager (gdk_display_get_default ());
-  device = gdk_device_manager_get_client_pointer (manager);
-  
-  /* If there's no active window, we fall back to returning the
-   * window that the cursor is in.
-   */
-  if (!current_window)
-    current_window = gdk_device_get_window_at_position (device, NULL, NULL);
-
-  if (current_window)
-    {
-      if (screenshot_window_is_desktop (current_window))
-	/* if the current window is the desktop (e.g. nautilus), we
-	 * return NULL, as getting the whole screen makes more sense.
-         */
-        return NULL;
-
-      /* Once we have a window, we take the toplevel ancestor. */
-      current_window = gdk_window_get_toplevel (current_window);
-    }
-
-  return current_window;
-}
-
 static Window
 find_wm_window (GdkWindow *window)
 {
@@ -406,15 +375,68 @@ screenshot_fallback_fire_flash (GdkWindow *window,
   g_object_unref (flash);
 }
 
+GdkWindow *
+do_find_current_window (void)
+{
+  GdkWindow *current_window;
+  GdkDeviceManager *manager;
+  GdkDevice *device;
+
+  current_window = screenshot_find_active_window ();
+  manager = gdk_display_get_device_manager (gdk_display_get_default ());
+  device = gdk_device_manager_get_client_pointer (manager);
+  
+  /* If there's no active window, we fall back to returning the
+   * window that the cursor is in.
+   */
+  if (!current_window)
+    current_window = gdk_device_get_window_at_position (device, NULL, NULL);
+
+  if (current_window)
+    {
+      if (screenshot_window_is_desktop (current_window))
+	/* if the current window is the desktop (e.g. nautilus), we
+	 * return NULL, as getting the whole screen makes more sense.
+         */
+        return NULL;
+
+      /* Once we have a window, we take the toplevel ancestor. */
+      current_window = gdk_window_get_toplevel (current_window);
+    }
+
+  return current_window;
+}
+
+static GdkWindow *
+screenshot_fallback_find_current_window (void)
+{
+  GdkWindow *window = NULL;
+
+  if (screenshot_config->take_window_shot)
+    {
+      window = do_find_current_window ();
+
+      if (window == NULL)
+        screenshot_config->take_window_shot = FALSE;
+    }
+
+  if (window == NULL)
+    window = gdk_get_default_root_window ();
+
+  return window;
+}
+
 static GdkPixbuf *
-screenshot_fallback_get_pixbuf (GdkWindow *window,
-                                GdkRectangle *rectangle)
+screenshot_fallback_get_pixbuf (GdkRectangle *rectangle)
 {
   GdkWindow *root, *wm_window = NULL;
   GdkPixbuf *screenshot;
   GdkRectangle real_coords, screenshot_coords;
   Window wm;
   GtkBorder frame_offset = { 0, 0, 0, 0 };
+  GdkWindow *window;
+
+  window = screenshot_fallback_find_current_window ();
 
   screenshot_fallback_get_window_rect_coords (window, 
                                               screenshot_config->include_border,
@@ -614,8 +636,7 @@ screenshot_fallback_get_pixbuf (GdkWindow *window,
 }
 
 GdkPixbuf *
-screenshot_get_pixbuf (GdkWindow    *window,
-                       GdkRectangle *rectangle)
+screenshot_get_pixbuf (GdkRectangle *rectangle)
 {
   GdkPixbuf *screenshot = NULL;
   gchar *path, *filename, *tmpname;
@@ -623,6 +644,9 @@ screenshot_get_pixbuf (GdkWindow    *window,
   GVariant *method_params;
   GError *error = NULL;
 
+  if (!screenshot_grab_lock ())
+    exit (0);
+
   path = g_build_filename (g_get_user_cache_dir (), "gnome-screenshot", NULL);
   g_mkdir_with_parents (path, 0700);
 
@@ -678,12 +702,15 @@ screenshot_get_pixbuf (GdkWindow    *window,
                  "resorting to fallback X11. Error: %s", error->message);
       g_error_free (error);
 
-      screenshot = screenshot_fallback_get_pixbuf (window, rectangle);
+      screenshot = screenshot_fallback_get_pixbuf (rectangle);
     }
 
   if (screenshot != NULL)
     screenshot_play_sound_effect ();
 
+  /* release now the lock */
+  screenshot_release_lock ();
+
   g_free (path);
   g_free (tmpname);
   g_free (filename);
diff --git a/src/screenshot-utils.h b/src/screenshot-utils.h
index 674c1b6..0e98430 100644
--- a/src/screenshot-utils.h
+++ b/src/screenshot-utils.h
@@ -27,9 +27,7 @@ G_BEGIN_DECLS
 
 gboolean   screenshot_grab_lock           (void);
 void       screenshot_release_lock        (void);
-GdkWindow *screenshot_find_current_window (void);
-GdkPixbuf *screenshot_get_pixbuf          (GdkWindow *win,
-                                           GdkRectangle *rectangle);
+GdkPixbuf *screenshot_get_pixbuf          (GdkRectangle *rectangle);
 
 void       screenshot_show_error_dialog   (GtkWindow   *parent,
                                            const gchar *message,



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