]> git.cworth.org Git - acre/blobdiff - acre-x.c
Draw frame times as a bar chart.
[acre] / acre-x.c
index 4e2cd0fa5e157a6786f98bc3eee090239dd4e90e..80b86dee2c234f9e49b0e52d8f65035927ba71c4 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 ();
+       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;
 }
 
 static void
 draw (Display *dpy, Window window, Visual *visual, acre_t *acre,
-      int width, int height)
+      int width, int height, double x_min, double x_max)
 {
        cairo_t *cr;
        cairo_surface_t *surface;
@@ -88,6 +109,7 @@ draw (Display *dpy, Window window, Visual *visual, acre_t *acre,
        cairo_set_source_rgb (cr, 1, 1, 1);
        cairo_paint (cr);
 
+       acre_set_x_axis_range (acre, x_min, x_max);
        acre_draw (acre, cr, width, height);
 
        cairo_destroy (cr);
@@ -102,19 +124,70 @@ handle_events(Display *dpy, Window window, Visual *visual,
 {
         XEvent xev;
         KeyCode quit_code = XKeysymToKeycode (dpy, XStringToKeysym("Q"));
+        KeyCode escape_code = XKeysymToKeycode (dpy, XStringToKeysym("Escape"));
+        KeyCode left_code = XKeysymToKeycode (dpy, XStringToKeysym("Left"));
+        KeyCode right_code = XKeysymToKeycode (dpy, XStringToKeysym("Right"));
+        KeyCode plus_code = XKeysymToKeycode (dpy, XStringToKeysym("plus"));
+        KeyCode equal_code = XKeysymToKeycode (dpy, XStringToKeysym("equal"));
+        KeyCode minus_code = XKeysymToKeycode (dpy, XStringToKeysym("minus"));
+        KeyCode home_code = XKeysymToKeycode (dpy, XStringToKeysym("Home"));
+       KeyCode keycode;
        bool need_redraw = false;
+       double x_min, x_max, shift;
+
+       acre_get_x_axis_data_range (acre, &x_min, &x_max);
 
         while (1) {
                 if (! XPending (dpy) && need_redraw)
-                       draw (dpy, window, visual, acre, width, height);
+                       draw (dpy, window, visual, acre,
+                             width, height, x_min, x_max);
 
+#define PAN  0.05
+#define ZOOM PAN
                XNextEvent (dpy, &xev);
                 switch (xev.type) {
                 case KeyPress:
-                        if (xev.xkey.keycode == quit_code) {
+                       need_redraw = true;
+                       keycode = xev.xkey.keycode;
+                       if (keycode == quit_code ||
+                           keycode == escape_code)
+                       {
                                 return;
-                        }
-                        break;
+                       }
+                       else if (keycode == left_code)
+                       {
+                               shift = PAN * (x_max - x_min);
+                               x_min += shift;
+                               x_max += shift;
+                       }
+                       else if (keycode == right_code)
+                       {
+                               shift = PAN * (x_max - x_min);
+                               x_min -= shift;
+                               x_max -= shift;
+                       }
+                       else if (keycode == plus_code ||
+                                  keycode == equal_code)
+                       {
+                               shift = ZOOM * (x_max - x_min);
+                               x_min += shift;
+                               x_max -= shift;
+                       }
+                       else if (keycode == minus_code)
+                       {
+                               shift = (ZOOM/(1 - 2 * ZOOM)) * (x_max - x_min);
+                               x_min -= shift;
+                               x_max += shift;
+                       }
+                       else if (keycode == home_code)
+                       {
+                               acre_get_x_axis_data_range (acre, &x_min, &x_max);
+                       }
+                       else
+                       {
+                               need_redraw = false;
+                       }
+                       break;
                 case ConfigureNotify:
                         width = xev.xconfigure.width;
                         height = xev.xconfigure.height;
@@ -128,7 +201,7 @@ handle_events(Display *dpy, Window window, Visual *visual,
 }
 
 int
-main (void)
+main (int argc, char *argv[])
 {
         Display *dpy;
         Window window, root;
@@ -142,7 +215,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);