[PATCH 4/5] add a simple progress sequence helper

William Jon McCann william.jon.mccann at gmail.com
Wed Mar 4 12:22:25 PST 2009


From: William Jon McCann <jmccann at redhat.com>

This is similar to the progress bar except that it uses a
sequence of images for the stages of the progress.
---
 src/libplybootsplash/Makefile.am      |    3 +-
 src/libplybootsplash/ply-progressor.c |  330 +++++++++++++++++++++++++++++++++
 src/libplybootsplash/ply-progressor.h |   58 ++++++
 3 files changed, 390 insertions(+), 1 deletions(-)
 create mode 100644 src/libplybootsplash/ply-progressor.c
 create mode 100644 src/libplybootsplash/ply-progressor.h

diff --git a/src/libplybootsplash/Makefile.am b/src/libplybootsplash/Makefile.am
index 3fa644b..588cdb4 100644
--- a/src/libplybootsplash/Makefile.am
+++ b/src/libplybootsplash/Makefile.am
@@ -6,7 +6,7 @@ INCLUDES = -I$(top_srcdir)                                                    \
 lib_LTLIBRARIES = libplybootsplash.la
 
 libplybootsplashdir = $(includedir)/plymouth-1/plybootsplash
-libplybootsplash_HEADERS = ply-entry.h ply-image.h ply-progress-bar.h ply-text-progress-bar.h ply-text-pulser.h ply-throbber.h ply-animator.h ply-window.h ply-label.h ply-boot-splash-plugin.h ply-label-plugin.h
+libplybootsplash_HEADERS = ply-entry.h ply-image.h ply-progress-bar.h ply-text-progress-bar.h ply-text-pulser.h ply-throbber.h ply-animator.h ply-progressor.h ply-window.h ply-label.h ply-boot-splash-plugin.h ply-label-plugin.h
 
 libplybootsplash_la_CFLAGS = $(PLYMOUTH_CFLAGS)                               \
 			     $(IMAGE_CFLAGS)                                  \
@@ -26,6 +26,7 @@ libplybootsplash_la_SOURCES = \
 		    ply-progress-bar.c                                       \
 		    ply-throbber.c                                           \
 		    ply-animator.c                                           \
+		    ply-progressor.c                                         \
 		    ply-text-progress-bar.c                                  \
 		    ply-text-pulser.c                                        \
 		    ply-window.c
diff --git a/src/libplybootsplash/ply-progressor.c b/src/libplybootsplash/ply-progressor.c
new file mode 100644
index 0000000..212f012
--- /dev/null
+++ b/src/libplybootsplash/ply-progressor.c
@@ -0,0 +1,330 @@
+/* progressor.c - boot progressor
+ *
+ * Copyright (C) 2008 Red Hat, Inc.
+ *
+ * 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, 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.
+ *
+ * Written by: William Jon McCann <jmccann at redhat.com>
+ *
+ */
+#include "config.h"
+
+#include <assert.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <math.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <values.h>
+#include <unistd.h>
+#include <wchar.h>
+
+#include "ply-progressor.h"
+#include "ply-array.h"
+#include "ply-logger.h"
+#include "ply-frame-buffer.h"
+#include "ply-image.h"
+#include "ply-utils.h"
+#include "ply-window.h"
+
+#include <linux/kd.h>
+
+#ifndef BAR_HEIGHT
+#define BAR_HEIGHT 16
+#endif
+
+struct _ply_progressor
+{
+  ply_array_t *frames;
+  char *image_dir;
+  char *frames_prefix;
+
+  ply_window_t            *window;
+  ply_frame_buffer_t      *frame_buffer;
+  ply_frame_buffer_area_t  area;
+  ply_frame_buffer_area_t  frame_area;
+
+  double percent_done;
+
+  uint32_t is_hidden : 1;
+};
+
+ply_progressor_t *
+ply_progressor_new (const char *image_dir,
+                    const char *frames_prefix)
+{
+  ply_progressor_t *progressor;
+
+  assert (image_dir != NULL);
+  assert (frames_prefix != NULL);
+
+  progressor = calloc (1, sizeof (ply_progressor_t));
+
+  progressor->frames = ply_array_new ();
+  progressor->frames_prefix = strdup (frames_prefix);
+  progressor->image_dir = strdup (image_dir);
+  progressor->is_hidden = true;
+  progressor->percent_done = 0.0;
+  progressor->area.x = 0;
+  progressor->area.y = 0;
+  progressor->area.width = 0;
+  progressor->area.height = 0;
+  progressor->frame_area.x = 0;
+  progressor->frame_area.y = 0;
+  progressor->frame_area.width = 0;
+  progressor->frame_area.height = 0;
+
+  return progressor;
+}
+
+static void
+ply_progressor_remove_frames (ply_progressor_t *progressor)
+{
+  int i;
+  ply_image_t **frames;
+
+  frames = (ply_image_t **) ply_array_steal_elements (progressor->frames);
+  for (i = 0; frames[i] != NULL; i++)
+    ply_image_free (frames[i]);
+  free (frames);
+}
+
+void
+ply_progressor_free (ply_progressor_t *progressor)
+{
+  if (progressor == NULL)
+    return;
+
+  ply_progressor_remove_frames (progressor);
+  ply_array_free (progressor->frames);
+
+  free (progressor->frames_prefix);
+  free (progressor->image_dir);
+  free (progressor);
+}
+
+static void
+draw_background (ply_progressor_t *progressor)
+{
+  ply_window_erase_area (progressor->window,
+                         progressor->area.x, progressor->area.y,
+                         progressor->frame_area.width,
+                         progressor->frame_area.height);
+}
+
+void
+ply_progressor_draw (ply_progressor_t *progressor)
+{
+  int number_of_frames;
+  int frame_number;
+  ply_image_t * const * frames;
+  uint32_t *frame_data;
+
+  if (progressor->is_hidden)
+    return;
+
+  ply_window_set_mode (progressor->window, PLY_WINDOW_MODE_GRAPHICS);
+
+  number_of_frames = ply_array_get_size (progressor->frames);
+
+  if (number_of_frames == 0)
+    return;
+
+  frame_number = progressor->percent_done * (number_of_frames - 1);
+
+  ply_frame_buffer_pause_updates (progressor->frame_buffer);
+  if (progressor->frame_area.width > 0)
+    draw_background (progressor);
+
+  frames = (ply_image_t * const *) ply_array_get_elements (progressor->frames);
+
+  progressor->frame_area.x = progressor->area.x;
+  progressor->frame_area.y = progressor->area.y;
+  progressor->frame_area.width = ply_image_get_width (frames[frame_number]);
+  progressor->frame_area.height = ply_image_get_height (frames[frame_number]);
+  frame_data = ply_image_get_data (frames[frame_number]);
+
+  ply_frame_buffer_fill_with_argb32_data (progressor->frame_buffer,
+                                          &progressor->frame_area, 0, 0,
+                                          frame_data);
+
+  ply_frame_buffer_unpause_updates (progressor->frame_buffer);
+}
+
+static bool
+ply_progressor_add_frame (ply_progressor_t *progressor,
+                          const char       *filename)
+{
+  ply_image_t *image;
+
+  image = ply_image_new (filename);
+
+  if (!ply_image_load (image))
+    {
+      ply_image_free (image);
+      return false;
+    }
+
+  ply_array_add_element (progressor->frames, image);
+
+  progressor->area.width = MAX (progressor->area.width, ply_image_get_width (image));
+  progressor->area.height = MAX (progressor->area.height, ply_image_get_height (image));
+
+  return true;
+}
+
+static bool
+ply_progressor_add_frames (ply_progressor_t *progressor)
+{
+  struct dirent **entries;
+  int number_of_entries;
+  int i;
+  bool load_finished;
+
+  entries = NULL;
+
+  number_of_entries = scandir (progressor->image_dir, &entries, NULL, versionsort);
+
+  if (number_of_entries < 0)
+    return false;
+
+  load_finished = false;
+  for (i = 0; i < number_of_entries; i++)
+    {
+      if (strncmp (entries[i]->d_name,
+                   progressor->frames_prefix,
+                   strlen (progressor->frames_prefix)) == 0
+          && (strlen (entries[i]->d_name) > 4)
+          && strcmp (entries[i]->d_name + strlen (entries[i]->d_name) - 4, ".png") == 0)
+        {
+          char *filename;
+
+          filename = NULL;
+          asprintf (&filename, "%s/%s", progressor->image_dir, entries[i]->d_name);
+
+          if (!ply_progressor_add_frame (progressor, filename))
+            goto out;
+
+          free (filename);
+        }
+
+      free (entries[i]);
+      entries[i] = NULL;
+    }
+  load_finished = true;
+
+out:
+  if (!load_finished)
+    {
+      ply_progressor_remove_frames (progressor);
+
+      while (entries[i] != NULL)
+        {
+          free (entries[i]);
+          i++;
+        }
+    }
+  free (entries);
+
+  return load_finished;
+}
+
+bool
+ply_progressor_load (ply_progressor_t *progressor)
+{
+  if (ply_array_get_size (progressor->frames) != 0)
+    ply_progressor_remove_frames (progressor->frames);
+
+  if (!ply_progressor_add_frames (progressor))
+    return false;
+
+  return true;
+}
+
+void
+ply_progressor_show (ply_progressor_t *progressor,
+                     ply_window_t     *window,
+                     long              x,
+                     long              y)
+{
+  assert (progressor != NULL);
+
+  progressor->window = window;
+  progressor->frame_buffer = ply_window_get_frame_buffer (window);;
+
+  progressor->area.x = x;
+  progressor->area.y = y;
+
+  progressor->is_hidden = false;
+  ply_progressor_draw (progressor);
+}
+
+void
+ply_progressor_hide (ply_progressor_t *progressor)
+{
+  if (progressor->is_hidden)
+    return;
+
+  if (progressor->frame_area.width > 0)
+    draw_background (progressor);
+
+  progressor->frame_buffer = NULL;
+  progressor->window = NULL;
+
+  progressor->is_hidden = true;
+}
+
+bool
+ply_progressor_is_hidden (ply_progressor_t *progressor)
+{
+  return progressor->is_hidden;
+}
+
+long
+ply_progressor_get_width (ply_progressor_t *progressor)
+{
+  return progressor->area.width;
+}
+
+long
+ply_progressor_get_height (ply_progressor_t *progressor)
+{
+  return progressor->area.height;
+}
+
+void
+ply_progressor_set_percent_done (ply_progressor_t *progressor,
+                                 double            percent_done)
+{
+  progressor->percent_done = percent_done;
+}
+
+double
+ply_progressor_get_percent_done (ply_progressor_t *progressor)
+{
+  return progressor->percent_done;
+}
+
+/* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */
diff --git a/src/libplybootsplash/ply-progressor.h b/src/libplybootsplash/ply-progressor.h
new file mode 100644
index 0000000..ad98245
--- /dev/null
+++ b/src/libplybootsplash/ply-progressor.h
@@ -0,0 +1,58 @@
+/* progressor.h - simple progressor animation
+ *
+ * Copyright (C) 2009 Red Hat, Inc.
+ *
+ * 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, 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.
+ *
+ * Written By: William Jon McCann <jmccann at redhat.com>
+ */
+#ifndef PROGRESSOR_H
+#define PROGRESSOR_H
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <unistd.h>
+
+#include "ply-frame-buffer.h"
+#include "ply-window.h"
+
+typedef struct _ply_progressor ply_progressor_t;
+
+#ifndef PLY_HIDE_FUNCTION_DECLARATIONS
+ply_progressor_t *ply_progressor_new (const char *image_dir,
+                                      const char *frames_prefix);
+void ply_progressor_free (ply_progressor_t *progressor);
+
+bool ply_progressor_load (ply_progressor_t *progressor);
+void ply_progressor_show (ply_progressor_t *progressor,
+                          ply_window_t     *window,
+                          long              x,
+                          long              y);
+void ply_progressor_hide (ply_progressor_t *progressor);
+void ply_progressor_draw (ply_progressor_t *progressor);
+bool ply_progressor_is_hidden (ply_progressor_t *progressor);
+
+long ply_progressor_get_width (ply_progressor_t *progressor);
+long ply_progressor_get_height (ply_progressor_t *progressor);
+
+void ply_progressor_set_percent_done (ply_progressor_t *progressor,
+                                      double            percent_done);
+double ply_progressor_get_percent_done (ply_progressor_t *progressor);
+
+#endif
+
+#endif /* PROGRESSOR_H */
+/* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */
-- 
1.6.1.3



More information about the plymouth mailing list