+int selected_dive = 0;
+
+typedef enum { STABLE, SLOW, MODERATE, FAST, CRAZY } velocity_t;
+
+/* Plot info with smoothing, velocity indication
+ * and one-, two- and three-minute minimums and maximums */
+struct plot_info {
+ int nr;
+ int maxtime;
+ int meandepth, maxdepth;
+ int maxpressure;
+ int mintemp, maxtemp;
+ struct plot_data {
+ unsigned int same_cylinder:1;
+ unsigned int cylinderindex;
+ int sec;
+ /* pressure[0] is sensor pressure
+ * pressure[1] is interpolated pressure */
+ int pressure[2];
+ int temperature;
+ /* Depth info */
+ int depth;
+ int smoothed;
+ velocity_t velocity;
+ struct plot_data *min[3];
+ struct plot_data *max[3];
+ int avg[3];
+ } entry[];
+};
+
+#define SENSOR_PR 0
+#define INTERPOLATED_PR 1
+#define SENSOR_PRESSURE(_entry) (_entry)->pressure[SENSOR_PR]
+#define INTERPOLATED_PRESSURE(_entry) (_entry)->pressure[INTERPOLATED_PR]
+#define GET_PRESSURE(_entry) (SENSOR_PRESSURE(_entry) ? : INTERPOLATED_PRESSURE(_entry))
+
+#define SAC_COLORS_START_IDX SAC_1
+#define SAC_COLORS 9
+#define VELOCITY_COLORS_START_IDX VELO_STABLE
+#define VELOCITY_COLORS 5
+
+typedef enum {
+ /* SAC colors. Order is important, the SAC_COLORS_START_IDX define above. */
+ SAC_1, SAC_2, SAC_3, SAC_4, SAC_5, SAC_6, SAC_7, SAC_8, SAC_9,
+
+ /* Velocity colors. Order is still important, ref VELOCITY_COLORS_START_IDX. */
+ VELO_STABLE, VELO_SLOW, VELO_MODERATE, VELO_FAST, VELO_CRAZY,
+
+ /* Other colors */
+ TEXT_BACKGROUND, ALERT_BG, ALERT_FG, EVENTS, SAMPLE_DEEP, SAMPLE_SHALLOW,
+ SMOOTHED, MINUTE, TIME_GRID, TIME_TEXT, DEPTH_GRID, MEAN_DEPTH, DEPTH_TOP,
+ DEPTH_BOTTOM, TEMP_TEXT, TEMP_PLOT, SAC_DEFAULT, BOUNDING_BOX, PRESSURE_TEXT, BACKGROUND
+} color_indice_t;
+
+typedef struct {
+ /* media[0] is screen, and media[1] is printer */
+ struct rgba {
+ double r,g,b,a;
+ } media[2];
+} color_t;
+
+/* [color indice] = {{screen color, printer color}} */
+static const color_t profile_color[] = {
+ [SAC_1] = {{FUNGREEN1, BLACK1_LOW_TRANS}},
+ [SAC_2] = {{APPLE1, BLACK1_LOW_TRANS}},
+ [SAC_3] = {{ATLANTIS1, BLACK1_LOW_TRANS}},
+ [SAC_4] = {{ATLANTIS2, BLACK1_LOW_TRANS}},
+ [SAC_5] = {{EARLSGREEN1, BLACK1_LOW_TRANS}},
+ [SAC_6] = {{HOKEYPOKEY1, BLACK1_LOW_TRANS}},
+ [SAC_7] = {{TUSCANY1, BLACK1_LOW_TRANS}},
+ [SAC_8] = {{CINNABAR1, BLACK1_LOW_TRANS}},
+ [SAC_9] = {{REDORANGE1, BLACK1_LOW_TRANS}},
+
+ [VELO_STABLE] = {{CAMARONE1, BLACK1_LOW_TRANS}},
+ [VELO_SLOW] = {{LIMENADE1, BLACK1_LOW_TRANS}},
+ [VELO_MODERATE] = {{RIOGRANDE1, BLACK1_LOW_TRANS}},
+ [VELO_FAST] = {{PIRATEGOLD1, BLACK1_LOW_TRANS}},
+ [VELO_CRAZY] = {{RED1, BLACK1_LOW_TRANS}},
+
+ [TEXT_BACKGROUND] = {{CONCRETE1_LOWER_TRANS, WHITE1}},
+ [ALERT_BG] = {{BROOM1_LOWER_TRANS, BLACK1_LOW_TRANS}},
+ [ALERT_FG] = {{BLACK1_LOW_TRANS, BLACK1_LOW_TRANS}},
+ [EVENTS] = {{REDORANGE1, BLACK1_LOW_TRANS}},
+ [SAMPLE_DEEP] = {{PERSIANRED1, BLACK1_LOW_TRANS}},
+ [SAMPLE_SHALLOW] = {{PERSIANRED1, BLACK1_LOW_TRANS}},
+ [SMOOTHED] = {{REDORANGE1_HIGH_TRANS, BLACK1_LOW_TRANS}},
+ [MINUTE] = {{MEDIUMREDVIOLET1_HIGHER_TRANS, BLACK1_LOW_TRANS}},
+ [TIME_GRID] = {{WHITE1, TUNDORA1_MED_TRANS}},
+ [TIME_TEXT] = {{FORESTGREEN1, BLACK1_LOW_TRANS}},
+ [DEPTH_GRID] = {{WHITE1, TUNDORA1_MED_TRANS}},
+ [MEAN_DEPTH] = {{REDORANGE1_MED_TRANS, BLACK1_LOW_TRANS}},
+ [DEPTH_BOTTOM] = {{GOVERNORBAY1_MED_TRANS, TUNDORA1_MED_TRANS}},
+ [DEPTH_TOP] = {{MERCURY1_MED_TRANS, WHITE1_MED_TRANS}},
+ [TEMP_TEXT] = {{GOVERNORBAY2, BLACK1_LOW_TRANS}},
+ [TEMP_PLOT] = {{ROYALBLUE2_LOW_TRANS, BLACK1_LOW_TRANS}},
+ [SAC_DEFAULT] = {{WHITE1, BLACK1_LOW_TRANS}},
+ [BOUNDING_BOX] = {{WHITE1, BLACK1_LOW_TRANS}},
+ [PRESSURE_TEXT] = {{KILLARNEY1, BLACK1_LOW_TRANS}},
+ [BACKGROUND] = {{SPRINGWOOD1, BLACK1_LOW_TRANS}},
+};
+
+#define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
+
+/* Scale to 0,0 -> maxx,maxy */
+#define SCALEX(gc,x) (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
+#define SCALEY(gc,y) (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
+#define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
+
+static void move_to(struct graphics_context *gc, double x, double y)
+{
+ cairo_move_to(gc->cr, SCALE(gc, x, y));
+}
+
+static void line_to(struct graphics_context *gc, double x, double y)
+{
+ cairo_line_to(gc->cr, SCALE(gc, x, y));
+}
+
+static void set_source_rgba(struct graphics_context *gc, color_indice_t c)
+{
+ const color_t *col = &profile_color[c];
+ struct rgba rgb = col->media[gc->printer];
+ double r = rgb.r;
+ double g = rgb.g;
+ double b = rgb.b;
+ double a = rgb.a;
+
+ cairo_set_source_rgba(gc->cr, r, g, b, a);
+}
+
+void init_profile_background(struct graphics_context *gc)
+{
+ set_source_rgba(gc, BACKGROUND);
+}
+
+void pattern_add_color_stop_rgba(struct graphics_context *gc, cairo_pattern_t *pat, double o, color_indice_t c)
+{
+ const color_t *col = &profile_color[c];
+ struct rgba rgb = col->media[gc->printer];
+ cairo_pattern_add_color_stop_rgba(pat, o, rgb.r, rgb.g, rgb.b, rgb.a);
+}
+
+#define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
+
+/* debugging tool - not normally used */
+static void dump_pi (struct plot_info *pi)
+{
+ int i;
+
+ printf("pi:{nr:%d maxtime:%d meandepth:%d maxdepth:%d \n"
+ " maxpressure:%d mintemp:%d maxtemp:%d\n",
+ pi->nr, pi->maxtime, pi->meandepth, pi->maxdepth,
+ pi->maxpressure, pi->mintemp, pi->maxtemp);
+ for (i = 0; i < pi->nr; i++)
+ printf(" entry[%d]:{same_cylinder:%d cylinderindex:%d sec:%d pressure:{%d,%d}\n"
+ " time:%d:%02d temperature:%d depth:%d smoothed:%d}\n",
+ i, pi->entry[i].same_cylinder, pi->entry[i].cylinderindex, pi->entry[i].sec,
+ pi->entry[i].pressure[0], pi->entry[i].pressure[1],
+ pi->entry[i].sec / 60, pi->entry[i].sec % 60,
+ pi->entry[i].temperature, pi->entry[i].depth, pi->entry[i].smoothed);
+ printf(" }\n");
+}
+
+/*
+ * When showing dive profiles, we scale things to the
+ * current dive. However, we don't scale past less than
+ * 30 minutes or 90 ft, just so that small dives show
+ * up as such.
+ * we also need to add 180 seconds at the end so the min/max
+ * plots correctly
+ */
+static int get_maxtime(struct plot_info *pi)
+{
+ int seconds = pi->maxtime;
+ /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
+ return MAX(30*60, ROUND_UP(seconds+150, 60*5));
+}
+
+static int get_maxdepth(struct plot_info *pi)
+{
+ unsigned mm = pi->maxdepth;
+ /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
+ return MAX(30000, ROUND_UP(mm+3000, 10000));
+}
+
+typedef struct {
+ int size;
+ color_indice_t color;
+ double hpos, vpos;
+} text_render_options_t;
+
+#define RIGHT (-1.0)
+#define CENTER (-0.5)
+#define LEFT (0.0)
+
+#define TOP (1)
+#define MIDDLE (0)
+#define BOTTOM (-1)
+
+static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
+ double x, double y, const char *fmt, ...)
+{
+ cairo_t *cr = gc->cr;
+ cairo_font_extents_t fe;
+ cairo_text_extents_t extents;
+ double dx, dy;
+ char buffer[80];
+ va_list args;
+
+ va_start(args, fmt);
+ vsnprintf(buffer, sizeof(buffer), fmt, args);
+ va_end(args);
+
+ cairo_set_font_size(cr, tro->size);
+ cairo_font_extents(cr, &fe);
+ cairo_text_extents(cr, buffer, &extents);
+ dx = tro->hpos * extents.width + extents.x_bearing;
+ dy = tro->vpos * extents.height + fe.descent;
+
+ move_to(gc, x, y);
+ cairo_rel_move_to(cr, dx, dy);
+
+ cairo_text_path(cr, buffer);
+ set_source_rgba(gc, TEXT_BACKGROUND);
+ cairo_stroke(cr);
+
+ move_to(gc, x, y);
+ cairo_rel_move_to(cr, dx, dy);
+
+ set_source_rgba(gc, tro->color);
+ cairo_show_text(cr, buffer);
+}
+
+struct ev_select {
+ char *ev_name;
+ gboolean plot_ev;
+};
+static struct ev_select *ev_namelist;
+static int evn_allocated;
+static int evn_used;
+
+void evn_foreach(void (*callback)(const char *, int *, void *), void *data)
+{
+ int i;
+
+ for (i = 0; i < evn_used; i++) {
+ callback(ev_namelist[i].ev_name, &ev_namelist[i].plot_ev, data);
+ }
+}
+
+void remember_event(const char *eventname)
+{
+ int i=0, len;
+
+ if (!eventname || (len = strlen(eventname)) == 0)
+ return;
+ while (i < evn_used) {
+ if (!strncmp(eventname,ev_namelist[i].ev_name,len))
+ return;
+ i++;
+ }
+ if (evn_used == evn_allocated) {
+ evn_allocated += 10;
+ ev_namelist = realloc(ev_namelist, evn_allocated * sizeof(struct ev_select));
+ if (! ev_namelist)
+ /* we are screwed, but let's just bail out */
+ return;
+ }
+ ev_namelist[evn_used].ev_name = strdup(eventname);
+ ev_namelist[evn_used].plot_ev = TRUE;
+ evn_used++;
+}
+
+static void plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *event, const text_render_options_t *tro)
+{
+ int i, depth = 0;
+ int x,y;
+
+ /* is plotting this event disabled? */
+ if (event->name) {
+ for (i = 0; i < evn_used; i++) {
+ if (! strcmp(event->name, ev_namelist[i].ev_name)) {
+ if (ev_namelist[i].plot_ev)
+ break;
+ else
+ return;
+ }
+ }
+ }
+ for (i = 0; i < pi->nr; i++) {
+ struct plot_data *data = pi->entry + i;
+ if (event->time.seconds < data->sec)
+ break;
+ depth = data->depth;
+ }
+ /* draw a little tirangular marker and attach tooltip */
+ x = SCALEX(gc, event->time.seconds);
+ y = SCALEY(gc, depth);
+ set_source_rgba(gc, ALERT_BG);
+ cairo_move_to(gc->cr, x-15, y+6);
+ cairo_line_to(gc->cr, x-3 , y+6);
+ cairo_line_to(gc->cr, x-9, y-6);
+ cairo_line_to(gc->cr, x-15, y+6);
+ cairo_stroke_preserve(gc->cr);
+ cairo_fill(gc->cr);
+ set_source_rgba(gc, ALERT_FG);
+ cairo_move_to(gc->cr, x-9, y-3);
+ cairo_line_to(gc->cr, x-9, y+1);
+ cairo_move_to(gc->cr, x-9, y+4);
+ cairo_line_to(gc->cr, x-9, y+4);
+ cairo_stroke(gc->cr);
+ attach_tooltip(x-15, y-6, 12, 12, event->name);
+}
+
+static void plot_events(struct graphics_context *gc, struct plot_info *pi, struct dive *dive)
+{
+ static const text_render_options_t tro = {14, EVENTS, CENTER, TOP};
+ struct event *event = dive->events;
+
+ if (gc->printer)
+ return;
+
+ while (event) {
+ plot_one_event(gc, pi, event, &tro);
+ event = event->next;
+ }
+}
+
+static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
+{
+ int sec = entry->sec, decimals;
+ double d;
+
+ d = get_depth_units(entry->depth, &decimals, NULL);
+
+ plot_text(gc, tro, sec, entry->depth, "%.*f", decimals, d);
+}
+
+static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)