]> git.cworth.org Git - acre/blobdiff - acre-x.c
Add support for parsing a fips data file.
[acre] / acre-x.c
index f0fac370a5d9bb45e414c7834a6e5e9087805624..bf70dd51570a50fd7b472cb4990046728d1b3a55 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-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 ();
 
-       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;
 }
@@ -126,6 +146,7 @@ handle_events(Display *dpy, Window window, Visual *visual,
                XNextEvent (dpy, &xev);
                 switch (xev.type) {
                 case KeyPress:
+                       need_redraw = true;
                        keycode = xev.xkey.keycode;
                        if (keycode == quit_code ||
                            keycode == escape_code)
@@ -153,7 +174,7 @@ handle_events(Display *dpy, Window window, Visual *visual,
                        }
                        else if (keycode == minus_code)
                        {
-                               shift = (1- 2*ZOOM) * (x_max - x_min);
+                               shift = (ZOOM/(1 - 2 * ZOOM)) * (x_max - x_min);
                                x_min -= shift;
                                x_max += shift;
                        }
@@ -161,7 +182,10 @@ handle_events(Display *dpy, Window window, Visual *visual,
                        {
                                acre_get_x_axis_data_range (acre, &x_min, &x_max);
                        }
-                       need_redraw = 1;
+                       else
+                       {
+                               need_redraw = false;
+                       }
                        break;
                 case ConfigureNotify:
                         width = xev.xconfigure.width;
@@ -176,7 +200,7 @@ handle_events(Display *dpy, Window window, Visual *visual,
 }
 
 int
-main (void)
+main (int argc, char *argv[])
 {
         Display *dpy;
         Window window, root;
@@ -190,7 +214,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);