Patch for EE



I had trouble finding a non-interactive screen shot program (besides
`xwd -root', which wasn't working properly), so I added a --screenshot
option to EE.  It stores a screenshot into the current directory with
a timestamp-based name, so you can take a bunch of shots in quick
succession.

A user might like to add an `ee --screenshot' window manager key binding to
PrintScreen, or similar.

Best regards,
Sean


Index: functions.h
===================================================================
RCS file: /cvs/gnome/ee/functions.h,v
retrieving revision 1.1
diff -u -r1.1 functions.h
--- functions.h	1998/09/18 01:32:19	1.1
+++ functions.h	1998/12/08 21:58:11
@@ -9,6 +9,8 @@
 #include <gdk_imlib.h>
 #include <gnome.h>
 
+gint
+func_autograb(void);
 void
 func_open_image(GtkWidget *widget, gpointer data);
 void


Index: functions.c
===================================================================
RCS file: /cvs/gnome/ee/functions.c,v
retrieving revision 1.7
diff -u -r1.7 functions.c
--- functions.c	1998/11/29 15:29:02	1.7
+++ functions.c	1998/12/08 21:42:37
@@ -16,8 +16,151 @@
 #include <ee_conf.h>
 #include <gdk/gdkx.h>
 
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
 static gchar loadsave = 0;
 static gchar *oldfile = NULL;
+
+/* Atomically get a lock file.  Returns true if you got it, else false. */
+/* This should really be in glib or somewhere general like that */
+static gint
+func_get_lock(gchar *lock_file_name)
+{
+  gint  lock_failed;
+  gchar *tmp_file_name;
+  gint  tmp_file_fd;
+
+  g_return_val_if_fail(lock_file_name != NULL, 0);
+
+  tmp_file_name = g_copy_strings(lock_file_name, ".lock_tmp", NULL);
+
+  errno = 0;
+  tmp_file_fd = open(tmp_file_name, O_WRONLY|O_CREAT|O_EXCL, 0666);
+  if (tmp_file_fd < 0)
+    {
+      g_warning("Could not open lock file %s: %s", tmp_file_name, 
+                g_strerror(errno));
+      return 0;
+    }
+  close(tmp_file_fd);
+  tmp_file_fd = -1;
+
+  /* So it will work on NFS */
+  lock_failed = link(tmp_file_name, lock_file_name);
+
+  if (unlink(tmp_file_name) < 0)
+    g_warning("Could not delete lock temp file %s: %s", tmp_file_name, 
+              g_strerror(errno));
+
+  g_free(tmp_file_name);
+  return !lock_failed;
+}
+
+/* Removes a (lock) file */
+static void
+func_release_lock(gchar *lock_file_name)
+{
+  g_return_if_fail(lock_file_name != NULL);
+  if (unlink(lock_file_name) < 0)
+    g_warning("Could not delete lock file %s: %s", lock_file_name,
+              g_strerror(errno));
+}
+
+/* Returns true if a file exists, else false */
+/* This should really be in glib or somewhere general like that */
+static gint
+func_file_exists(gchar *fname)
+{
+  gint fd;
+
+  g_return_val_if_fail(fname != NULL, 0);
+  errno = 0;
+  fd = open(fname, O_RDONLY);
+  if (fd < 0)
+    {
+      if (errno != ENOENT)
+        g_warning("Could not open file %s: %s", fname, g_strerror(errno));
+      return 0;
+    }
+  close(fd);
+  return 1;
+}
+
+/* Returns the next available grab file name, after setting a lock
+ * file for that name.  You must call func_unlock_autograb_filename()
+ * with the name of the grab file to remove the lock file.
+ */
+static gchar*
+func_lock_next_autograb_filename(void)
+{
+  gint  start;
+  glong now;
+  gchar img_fname[40], lock_fname[40];
+
+  for (start = now = time(NULL); now < start + 1000; now++)
+    {
+      g_snprintf(lock_fname, sizeof(lock_fname), ".DisplayGrab-%ld.ppm.lock", 
+                 now);
+      if (func_get_lock(lock_fname))
+        {
+          g_snprintf(img_fname, sizeof(img_fname), "DisplayGrab-%ld.ppm", 
+                     now);
+          if (!func_file_exists(img_fname))
+            return g_strdup(img_fname);
+          func_release_lock(lock_fname);
+        }
+    }
+  g_warning("Could not get autograb file name");
+  return NULL;
+}
+
+/* Removes the lock file associated with grab_fname */
+static void
+func_unlock_autograb_filename(gchar *grab_fname)
+{
+  gchar *lock_fname = NULL;
+  
+  g_return_if_fail(grab_fname != NULL);
+  
+  lock_fname = g_copy_strings(".", grab_fname, ".lock", NULL);
+  func_release_lock(lock_fname);
+  g_free(lock_fname);
+}
+
+/* Tries to save a screenshot into DisplayGrab-TIMESTAMP.ppm, where
+ * TIMESTAMP is >= current time.
+ * Returns true if successful, else false.
+ */
+gint
+func_autograb(void)
+{
+  GdkImlibImage *im;
+  gint          w, h;
+  gchar         *fname = NULL;
+  gint          ret;
+  
+  w = gdk_screen_width();  
+  h = gdk_screen_height();  
+  im = gdk_imlib_create_image_from_drawable((GdkWindow *)&gdk_root_parent, 
+                                            NULL, 0, 0, w, h);
+
+  fname = func_lock_next_autograb_filename();
+  if (fname == NULL)
+    {
+      g_warning("Could not get file for autograb");
+      return 0;
+    }
+  ret = gdk_imlib_save_image_to_ppm(im, fname);
+  if (!ret)
+    g_warning("Could not save autograb");
+  func_unlock_autograb_filename(fname);
+  g_free(fname);
+  return ret;
+}
 
 static void
 func_img_open(GtkWidget * widget, gpointer * data)


Index: main.c
===================================================================
RCS file: /cvs/gnome/ee/main.c,v
retrieving revision 1.29
diff -u -r1.29 main.c
--- main.c	1998/11/02 20:33:41	1.29
+++ main.c	1998/12/08 21:42:39
@@ -7,6 +7,7 @@
 
 #include <errno.h>
 
+static gint        do_autograb_and_quit = 0;
 static gint        set_root_and_quit = 0;
 static gchar       *current_dir;
 static gchar      **start_img_list = NULL;
@@ -15,6 +16,9 @@
 struct poptOption options[] = {
 	{"root", 'r', POPT_ARG_NONE, &set_root_and_quit, 0,
 	 N_("Set root window image and leave"), NULL},
+	{"screenshot", 's', POPT_ARG_NONE, &do_autograb_and_quit, 0,
+	 N_("Save a screenshot into DisplayGrab-TIMESTAMP.ppm and leave"), 
+         NULL},
 	{NULL, '\0', 0, NULL, 0}
 };
 
@@ -306,6 +310,9 @@
   gtk_widget_push_visual(gdk_imlib_get_visual());
   gtk_widget_push_colormap(gdk_imlib_get_colormap());
   ee_conf_load();
+
+  if (do_autograb_and_quit)
+      return !func_autograb();
   
   ee_thumb_init_dirs();
   



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