]> git.cworth.org Git - acre/blobdiff - acre-x.c
Add SVG export.
[acre] / acre-x.c
index c375c36efddee95a1d7afd4e74262a3d740f4cbd..e62c5967601b44b246cd1ff8e1c505332b904c87 100644 (file)
--- a/acre-x.c
+++ b/acre-x.c
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  */
 
+#include <stdlib.h>
 #include <stdbool.h>
+#include <string.h>
+#include <errno.h>
+#include <inttypes.h>
 
 #include <X11/Xlib.h>
+
+#include <cairo-svg.h>
 #include <cairo-xlib.h>
 
 #include "acre.h"
 #include "math.h"
 
+#define STRNCMP_LITERAL(var, literal) \
+    strncmp ((var), (literal), sizeof (literal) - 1)
+
 static acre_t *
-load_chart (void)
+load_fips_data (const char *filename)
 {
+       FILE *file;
        acre_t *acre;
-       acre_data_t *data0, *data1, *data2;
-       int i;
+       acre_data_t *frame_time;
+       char *line = NULL, *s;
+       size_t line_size;
+       ssize_t bytes;
+
+       file = fopen (filename, "r");
+       if (file == NULL) {
+               fprintf (stderr, "Failed to open %s: %s\n",
+                        filename, strerror (errno));
+               return NULL;
+       }
 
        acre = acre_create ();
-       acre_set_x_axis_label (acre, "X axis");
-       acre_set_y_axis_label (acre, "Y axis");
 
-       data0 = acre_data_create ();
-       data1 = acre_data_create ();
-       data2 = acre_data_create ();
+       acre_set_title (acre, filename);
+       acre_set_x_axis_label (acre, "Frame #");
+       acre_set_y_axis_label (acre, "Frame time (ms)");
 
-       acre_data_set_name (data0, "Data 0");
-       acre_data_set_name (data1, "Data 1");
-       acre_data_set_name (data2, "Data 2");
+       frame_time = acre_data_create ();
+       acre_data_set_style (frame_time, ACRE_STYLE_BARS_OR_LINE);
 
-       for (i = 0; i <= 100; i++) {
-               acre_data_add_point_2d (data0, i,   0 - (i/3.0)*(i/3.0));
-       }
+       while (1) {
+               int scanned;
 
-       for (i = 0; i < 100; i++) {
-               double t = 1.0 - (i / 100.0);
-               acre_data_add_point_2d (data1, i, -1000 * (1.0 - t*t*t));
-       }
-       
-       for (i = 0; i <= 1000; i++) {
-               double t, x, y;
-               t = i/10.0 - 50;
-               x = t + 50;
-               if (t == 0.0)
-                       y = -200;
-               else
-                       y = -1200 + 1000 * sin(t) / t;
-               acre_data_add_point_2d (data2, x, y);
+               bytes = getline (&line, &line_size, file);
+               if (bytes == -1)
+                       break;
+
+               s = line;
+
+               if (STRNCMP_LITERAL (s, "frame-time: ") == 0) {
+                       unsigned frame;
+                       int64_t frame_time_ns;
+
+                       s += strlen("frame-time: ");
+
+                       scanned = sscanf (s, "%d %" SCNu64,
+                                         &frame, &frame_time_ns);
+                       if (scanned != 2) {
+                               fprintf (stderr, "Warning: Failed to parse line: %s\n", line);
+                               continue;
+                       }
+
+                       acre_data_add_point_2d (frame_time, frame,
+                                               frame_time_ns / 1e6);
+               } else {
+                       /* Ignoring all other lines. */
+               }
        }
 
-       acre_add_data (acre, data0);
-       acre_add_data (acre, data1);
-       acre_add_data (acre, data2);
+       free (line);
 
-       acre_set_title (acre, "All the data");
+       acre_add_data (acre, frame_time);
 
        return acre;
 }
@@ -96,6 +119,24 @@ draw (Display *dpy, Window window, Visual *visual, acre_t *acre,
        cairo_surface_destroy (surface);
 }
 
+static void
+draw_svg (acre_t *acre, int width, int height, double x_min, double x_max)
+{
+       cairo_t *cr;
+       cairo_surface_t *surface;
+
+       surface = cairo_svg_surface_create ("acre-fips.svg", width, height);
+
+       cr = cairo_create (surface);
+
+       acre_set_x_axis_range (acre, x_min, x_max);
+       acre_draw (acre, cr, width, height);
+
+       cairo_destroy (cr);
+
+       cairo_surface_destroy (surface);
+}
+
 
 static void
 handle_events(Display *dpy, Window window, Visual *visual,
@@ -110,6 +151,7 @@ handle_events(Display *dpy, Window window, Visual *visual,
         KeyCode equal_code = XKeysymToKeycode (dpy, XStringToKeysym("equal"));
         KeyCode minus_code = XKeysymToKeycode (dpy, XStringToKeysym("minus"));
         KeyCode home_code = XKeysymToKeycode (dpy, XStringToKeysym("Home"));
+        KeyCode svg_code = XKeysymToKeycode (dpy, XStringToKeysym("S"));
        KeyCode keycode;
        bool need_redraw = false;
        double x_min, x_max, shift;
@@ -162,6 +204,11 @@ handle_events(Display *dpy, Window window, Visual *visual,
                        {
                                acre_get_x_axis_data_range (acre, &x_min, &x_max);
                        }
+                       else if (keycode == svg_code)
+                       {
+                               need_redraw = false;
+                               draw_svg (acre, width, height, x_min, x_max);
+                       }
                        else
                        {
                                need_redraw = false;
@@ -180,7 +227,7 @@ handle_events(Display *dpy, Window window, Visual *visual,
 }
 
 int
-main (void)
+main (int argc, char *argv[])
 {
         Display *dpy;
         Window window, root;
@@ -194,7 +241,16 @@ main (void)
        int width = 800;
        int height = 600;
 
-       acre = load_chart ();
+       if (argc < 2) {
+               fprintf (stderr, "Usage: acre-x data-file\n");
+               fprintf (stderr, "Where the data file is the output from fips (with frame-timing feature.\n");
+               exit (1);
+       }
+
+       acre = load_fips_data (argv[1]);
+
+       if (acre == NULL)
+               return 1;
 
         dpy = XOpenDisplay (NULL);