]> git.tdb.fi Git - ext/subsurface.git/blob - profile.c
Revert "Correctly plot the tank end pressure if it was set manually"
[ext/subsurface.git] / profile.c
1 /* profile.c */
2 /* creates all the necessary data for drawing the dive profile 
3  * uses cairo to draw it
4  */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdarg.h>
8 #include <string.h>
9 #include <time.h>
10
11 #include "dive.h"
12 #include "display.h"
13 #include "divelist.h"
14
15 int selected_dive = 0;
16
17 typedef enum { STABLE, SLOW, MODERATE, FAST, CRAZY } velocity_t;
18 /* Plot info with smoothing, velocity indication
19  * and one-, two- and three-minute minimums and maximums */
20 struct plot_info {
21         int nr;
22         int maxtime;
23         int meandepth, maxdepth;
24         int maxpressure;
25         int mintemp, maxtemp;
26         struct plot_data {
27                 unsigned int same_cylinder:1;
28                 unsigned int cylinderindex;
29                 int sec;
30                 /* pressure[0] is sensor pressure
31                  * pressure[1] is interpolated pressure */
32                 int pressure[2];
33                 int temperature;
34                 /* Depth info */
35                 int depth;
36                 int smoothed;
37                 velocity_t velocity;
38                 struct plot_data *min[3];
39                 struct plot_data *max[3];
40                 int avg[3];
41         } entry[];
42 };
43 #define SENSOR_PR 0
44 #define INTERPOLATED_PR 1
45 #define SENSOR_PRESSURE(_entry) (_entry)->pressure[SENSOR_PR]
46 #define INTERPOLATED_PRESSURE(_entry) (_entry)->pressure[INTERPOLATED_PR]
47 #define GET_PRESSURE(_entry) (SENSOR_PRESSURE(_entry) ? : INTERPOLATED_PRESSURE(_entry))
48
49 /* convert velocity to colors */
50 typedef struct { double r, g, b; } rgb_t;
51 static const rgb_t velocity_color[] = {
52         [STABLE]   = {0.0, 0.4, 0.0},
53         [SLOW]     = {0.4, 0.8, 0.0},
54         [MODERATE] = {0.8, 0.8, 0.0},
55         [FAST]     = {0.8, 0.5, 0.0},
56         [CRAZY]    = {1.0, 0.0, 0.0},
57 };
58
59 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
60
61 /* Scale to 0,0 -> maxx,maxy */
62 #define SCALEX(gc,x)  (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
63 #define SCALEY(gc,y)  (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
64 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
65
66 static void move_to(struct graphics_context *gc, double x, double y)
67 {
68         cairo_move_to(gc->cr, SCALE(gc, x, y));
69 }
70
71 static void line_to(struct graphics_context *gc, double x, double y)
72 {
73         cairo_line_to(gc->cr, SCALE(gc, x, y));
74 }
75
76 static void set_source_rgba(struct graphics_context *gc, double r, double g, double b, double a)
77 {
78         /*
79          * For printers, we still honor 'a', but ignore colors
80          * for now. Black is white and white is black
81          */
82         if (gc->printer) {
83                 double sum = r+g+b;
84                 if (sum > 0.8)
85                         r = g = b = 0;
86                 else
87                         r = g = b = 1;
88         }
89         cairo_set_source_rgba(gc->cr, r, g, b, a);
90 }
91
92 static void set_source_rgb_struct(struct graphics_context *gc, const rgb_t *rgb)
93 {
94         set_source_rgba(gc, rgb->r, rgb->g, rgb->b, 1);
95 }
96
97 void set_source_rgb(struct graphics_context *gc, double r, double g, double b)
98 {
99         set_source_rgba(gc, r, g, b, 1);
100 }
101
102 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
103
104 /* debugging tool - not normally used */
105 static void dump_pi (struct plot_info *pi)
106 {
107         int i;
108
109         printf("pi:{nr:%d maxtime:%d meandepth:%d maxdepth:%d \n"
110                 "    maxpressure:%d mintemp:%d maxtemp:%d\n",
111                 pi->nr, pi->maxtime, pi->meandepth, pi->maxdepth,
112                 pi->maxpressure, pi->mintemp, pi->maxtemp);
113         for (i = 0; i < pi->nr; i++)
114                 printf("    entry[%d]:{same_cylinder:%d cylinderindex:%d sec:%d pressure:{%d,%d}\n"
115                         "                temperature:%d depth:%d smoothed:%d}\n",
116                         i, pi->entry[i].same_cylinder, pi->entry[i].cylinderindex, pi->entry[i].sec,
117                         pi->entry[i].pressure[0], pi->entry[i].pressure[1],
118                         pi->entry[i].temperature, pi->entry[i].depth, pi->entry[i].smoothed);
119         printf("   }\n");
120 }
121
122 /*
123  * When showing dive profiles, we scale things to the
124  * current dive. However, we don't scale past less than
125  * 30 minutes or 90 ft, just so that small dives show
126  * up as such.
127  * we also need to add 180 seconds at the end so the min/max
128  * plots correctly
129  */
130 static int get_maxtime(struct plot_info *pi)
131 {
132         int seconds = pi->maxtime;
133         /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
134         return MAX(30*60, ROUND_UP(seconds+150, 60*5));
135 }
136
137 static int get_maxdepth(struct plot_info *pi)
138 {
139         unsigned mm = pi->maxdepth;
140         /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
141         return MAX(30000, ROUND_UP(mm+3000, 10000));
142 }
143
144 typedef struct {
145         int size;
146         double r,g,b;
147         double hpos, vpos;
148 } text_render_options_t;
149
150 #define RIGHT (-1.0)
151 #define CENTER (-0.5)
152 #define LEFT (0.0)
153
154 #define TOP (1)
155 #define MIDDLE (0)
156 #define BOTTOM (-1)
157
158 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
159                       double x, double y, const char *fmt, ...)
160 {
161         cairo_t *cr = gc->cr;
162         cairo_font_extents_t fe;
163         cairo_text_extents_t extents;
164         double dx, dy;
165         char buffer[80];
166         va_list args;
167
168         va_start(args, fmt);
169         vsnprintf(buffer, sizeof(buffer), fmt, args);
170         va_end(args);
171
172         cairo_set_font_size(cr, tro->size);
173         cairo_font_extents(cr, &fe);
174         cairo_text_extents(cr, buffer, &extents);
175         dx = tro->hpos * extents.width + extents.x_bearing;
176         dy = tro->vpos * extents.height + fe.descent;
177
178         move_to(gc, x, y);
179         cairo_rel_move_to(cr, dx, dy);
180
181         cairo_text_path(cr, buffer);
182         set_source_rgb(gc, 0, 0, 0);
183         cairo_stroke(cr);
184
185         move_to(gc, x, y);
186         cairo_rel_move_to(cr, dx, dy);
187
188         set_source_rgb(gc, tro->r, tro->g, tro->b);
189         cairo_show_text(cr, buffer);
190 }
191
192 struct ev_select {
193         char *ev_name;
194         gboolean plot_ev;
195 };
196 static struct ev_select *ev_namelist;
197 static int evn_allocated;
198 static int evn_used;
199
200 void evn_foreach(void (*callback)(const char *, int *, void *), void *data)
201 {
202         int i;
203
204         for (i = 0; i < evn_used; i++) {
205                 callback(ev_namelist[i].ev_name, &ev_namelist[i].plot_ev, data);
206         }
207 }
208
209 void remember_event(const char *eventname)
210 {
211         int i=0, len;
212
213         if (!eventname || (len = strlen(eventname)) == 0)
214                 return;
215         while (i < evn_used) {
216                 if (!strncmp(eventname,ev_namelist[i].ev_name,len))
217                         return;
218                 i++;
219         }
220         if (evn_used == evn_allocated) {
221                 evn_allocated += 10;
222                 ev_namelist = realloc(ev_namelist, evn_allocated * sizeof(struct ev_select));
223                 if (! ev_namelist)
224                         /* we are screwed, but let's just bail out */
225                         return;
226         }
227         ev_namelist[evn_used].ev_name = strdup(eventname);
228         ev_namelist[evn_used].plot_ev = TRUE;
229         evn_used++;
230 }
231
232 static void plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *event, const text_render_options_t *tro)
233 {
234         int i, depth = 0;
235         int x,y;
236
237         /* is plotting this event disabled? */
238         if (event->name) {
239                 for (i = 0; i < evn_used; i++) {
240                         if (! strcmp(event->name, ev_namelist[i].ev_name)) {
241                                 if (ev_namelist[i].plot_ev)
242                                         break;
243                                 else
244                                         return;
245                         }
246                 }
247         }
248         for (i = 0; i < pi->nr; i++) {
249                 struct plot_data *data = pi->entry + i;
250                 if (event->time.seconds < data->sec)
251                         break;
252                 depth = data->depth;
253         }
254         /* draw a little tirangular marker and attach tooltip */
255         x = SCALEX(gc, event->time.seconds);
256         y = SCALEY(gc, depth);
257         set_source_rgba(gc, 1.0, 1.0, 0.1, 0.8);
258         cairo_move_to(gc->cr, x-15, y+6);
259         cairo_line_to(gc->cr, x-3  , y+6);
260         cairo_line_to(gc->cr, x-9, y-6);
261         cairo_line_to(gc->cr, x-15, y+6);
262         cairo_stroke_preserve(gc->cr);
263         cairo_fill(gc->cr);
264         set_source_rgba(gc, 0.0, 0.0, 0.0, 0.8);
265         cairo_move_to(gc->cr, x-9, y-3);
266         cairo_line_to(gc->cr, x-9, y+1);
267         cairo_move_to(gc->cr, x-9, y+4);
268         cairo_line_to(gc->cr, x-9, y+4);
269         cairo_stroke(gc->cr);
270         attach_tooltip(x-15, y-6, 12, 12, event->name);
271 }
272
273 static void plot_events(struct graphics_context *gc, struct plot_info *pi, struct dive *dive)
274 {
275         static const text_render_options_t tro = {14, 1.0, 0.2, 0.2, CENTER, TOP};
276         struct event *event = dive->events;
277
278         if (gc->printer)
279                 return;
280
281         while (event) {
282                 plot_one_event(gc, pi, event, &tro);
283                 event = event->next;
284         }
285 }
286
287 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
288 {
289         int sec = entry->sec, decimals;
290         double d;
291
292         d = get_depth_units(entry->depth, &decimals, NULL);
293
294         plot_text(gc, tro, sec, entry->depth, "%.*f", decimals, d);
295 }
296
297 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
298 {
299         static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
300         static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
301         int i;
302         int last = -1;
303
304         for (i = 0; i < pi->nr; i++) {
305                 struct plot_data *entry = pi->entry + i;
306
307                 if (entry->depth < 2000)
308                         continue;
309
310                 if ((entry == entry->max[2]) && entry->depth != last) {
311                         render_depth_sample(gc, entry, &deep);
312                         last = entry->depth;
313                 }
314
315                 if ((entry == entry->min[2]) && entry->depth != last) {
316                         render_depth_sample(gc, entry, &shallow);
317                         last = entry->depth;
318                 }
319
320                 if (entry->depth != last)
321                         last = -1;
322         }
323 }
324
325 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
326 {
327         int maxtime, maxdepth;
328
329         /* Get plot scaling limits */
330         maxtime = get_maxtime(pi);
331         maxdepth = get_maxdepth(pi);
332
333         gc->leftx = 0; gc->rightx = maxtime;
334         gc->topy = 0; gc->bottomy = maxdepth;
335
336         plot_text_samples(gc, pi);
337 }
338
339 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
340 {
341         int i;
342         struct plot_data *entry = pi->entry;
343
344         set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
345         move_to(gc, entry->sec, entry->smoothed);
346         for (i = 1; i < pi->nr; i++) {
347                 entry++;
348                 line_to(gc, entry->sec, entry->smoothed);
349         }
350         cairo_stroke(gc->cr);
351 }
352
353 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
354                                 int index, double a)
355 {
356         int i;
357         struct plot_data *entry = pi->entry;
358
359         set_source_rgba(gc, 1, 0.2, 1, a);
360         move_to(gc, entry->sec, entry->min[index]->depth);
361         for (i = 1; i < pi->nr; i++) {
362                 entry++;
363                 line_to(gc, entry->sec, entry->min[index]->depth);
364         }
365         for (i = 1; i < pi->nr; i++) {
366                 line_to(gc, entry->sec, entry->max[index]->depth);
367                 entry--;
368         }
369         cairo_close_path(gc->cr);
370         cairo_fill(gc->cr);
371 }
372
373 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
374 {
375         if (gc->printer)
376                 return;
377         plot_minmax_profile_minute(gc, pi, 2, 0.1);
378         plot_minmax_profile_minute(gc, pi, 1, 0.1);
379         plot_minmax_profile_minute(gc, pi, 0, 0.1);
380 }
381
382 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
383 {
384         int i, incr;
385         cairo_t *cr = gc->cr;
386         int sec, depth;
387         struct plot_data *entry;
388         int maxtime, maxdepth, marker;
389         int increments[4] = { 5*60, 10*60, 15*60, 30*60 };
390
391         /* Get plot scaling limits */
392         maxtime = get_maxtime(pi);
393         maxdepth = get_maxdepth(pi);
394
395         /* Time markers: at most every 5 min, but no more than 12 markers
396          * and for convenience we do 5, 10, 15 or 30 min intervals.
397          * This allows for 6h dives - enough (I hope) for even the craziest
398          * divers - but just in case, for those 8h depth-record-breaking dives,
399          * we double the interval if this still doesn't get us to 12 or fewer
400          * time markers */
401         i = 0;
402         while (maxtime / increments[i] > 12 && i < 4)
403                 i++;
404         incr = increments[i];
405         while (maxtime / incr > 12)
406                 incr *= 2;
407
408         gc->leftx = 0; gc->rightx = maxtime;
409         gc->topy = 0; gc->bottomy = 1.0;
410         set_source_rgba(gc, 1, 1, 1, 0.5);
411         for (i = incr; i < maxtime; i += incr) {
412                 move_to(gc, i, 0);
413                 line_to(gc, i, 1);
414         }
415         cairo_stroke(cr);
416
417         /* now the text on every second time marker */
418         text_render_options_t tro = {10, 0.2, 1.0, 0.2, CENTER, TOP};
419         for (i = incr; i < maxtime; i += 2 * incr)
420                 plot_text(gc, &tro, i, 1, "%d", i/60);
421
422         /* Depth markers: every 30 ft or 10 m*/
423         gc->leftx = 0; gc->rightx = 1.0;
424         gc->topy = 0; gc->bottomy = maxdepth;
425         switch (output_units.length) {
426         case METERS: marker = 10000; break;
427         case FEET: marker = 9144; break;        /* 30 ft */
428         }
429
430         set_source_rgba(gc, 1, 1, 1, 0.5);
431         for (i = marker; i < maxdepth; i += marker) {
432                 move_to(gc, 0, i);
433                 line_to(gc, 1, i);
434         }
435         cairo_stroke(cr);
436
437         /* Show mean depth */
438         if (! gc->printer) {
439                 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
440                 move_to(gc, 0, pi->meandepth);
441                 line_to(gc, 1, pi->meandepth);
442                 cairo_stroke(cr);
443         }
444
445         gc->leftx = 0; gc->rightx = maxtime;
446
447         /*
448          * These are good for debugging text placement etc,
449          * but not for actual display..
450          */
451         if (0) {
452                 plot_smoothed_profile(gc, pi);
453                 plot_minmax_profile(gc, pi);
454         }
455
456         set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
457
458         /* Do the depth profile for the neat fill */
459         gc->topy = 0; gc->bottomy = maxdepth;
460         set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
461
462         entry = pi->entry;
463         move_to(gc, 0, 0);
464         for (i = 0; i < pi->nr; i++, entry++)
465                 line_to(gc, entry->sec, entry->depth);
466         cairo_close_path(gc->cr);
467         if (gc->printer) {
468                 set_source_rgba(gc, 1, 1, 1, 0.2);
469                 cairo_fill_preserve(cr);
470                 set_source_rgb(gc, 1, 1, 1);
471                 cairo_stroke(cr);
472                 return;
473         }
474         cairo_fill(gc->cr);
475
476         /* Now do it again for the velocity colors */
477         entry = pi->entry;
478         for (i = 1; i < pi->nr; i++) {
479                 entry++;
480                 sec = entry->sec;
481                 /* we want to draw the segments in different colors
482                  * representing the vertical velocity, so we need to
483                  * chop this into short segments */
484                 depth = entry->depth;
485                 set_source_rgb_struct(gc, &velocity_color[entry->velocity]);
486                 move_to(gc, entry[-1].sec, entry[-1].depth);
487                 line_to(gc, sec, depth);
488                 cairo_stroke(cr);
489         }
490 }
491
492 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
493 {
494         int maxtime, mintemp, maxtemp, delta;
495
496         /* Get plot scaling limits */
497         maxtime = get_maxtime(pi);
498         mintemp = pi->mintemp;
499         maxtemp = pi->maxtemp;
500
501         gc->leftx = 0; gc->rightx = maxtime;
502         /* Show temperatures in roughly the lower third, but make sure the scale
503            is at least somewhat reasonable */
504         delta = maxtemp - mintemp;
505         if (delta > 3000) { /* more than 3K in fluctuation */
506                 gc->topy = maxtemp + delta*2;
507                 gc->bottomy = mintemp - delta/2;
508         } else {
509                 gc->topy = maxtemp + 1500 + delta*2;
510                 gc->bottomy = mintemp - delta/2;
511         }
512
513         return maxtemp > mintemp;
514 }
515
516 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
517 {
518         double deg;
519         const char *unit;
520         static const text_render_options_t tro = {12, 0.6, 0.6, 1.0, LEFT, TOP};
521
522         deg = get_temp_units(mkelvin, &unit);
523
524         plot_text(gc, &tro, sec, mkelvin, "%d%s", (int)(deg + 0.5), unit);
525 }
526
527 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
528 {
529         int i;
530         int last = -300, sec = 0;
531         int last_temperature = 0, last_printed_temp = 0;
532
533         if (!setup_temperature_limits(gc, pi))
534                 return;
535
536         for (i = 0; i < pi->nr; i++) {
537                 struct plot_data *entry = pi->entry+i;
538                 int mkelvin = entry->temperature;
539
540                 if (!mkelvin)
541                         continue;
542                 last_temperature = mkelvin;
543                 sec = entry->sec;
544                 /* don't print a temperature
545                  * if it's been less than 5min and less than a 2K change OR
546                  * if it's been less than 2min OR if the change from the
547                  * last print is less than .4K (and therefore less than 1F */
548                 if (((sec < last + 300) && (abs(mkelvin - last_printed_temp) < 2000)) ||
549                         (sec < last + 120) ||
550                         (abs(mkelvin - last_printed_temp) < 400))
551                         continue;
552                 last = sec;
553                 plot_single_temp_text(gc,sec,mkelvin);
554                 last_printed_temp = mkelvin;
555         }
556         /* it would be nice to print the end temperature, if it's
557          * different or if the last temperature print has been more
558          * than a quarter of the dive back */
559         if ((abs(last_temperature - last_printed_temp) > 500) ||
560                 ((double)last / (double)sec < 0.75))
561                 plot_single_temp_text(gc, sec, last_temperature);
562 }
563
564 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
565 {
566         int i;
567         cairo_t *cr = gc->cr;
568         int last = 0;
569
570         if (!setup_temperature_limits(gc, pi))
571                 return;
572
573         set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
574         for (i = 0; i < pi->nr; i++) {
575                 struct plot_data *entry = pi->entry + i;
576                 int mkelvin = entry->temperature;
577                 int sec = entry->sec;
578                 if (!mkelvin) {
579                         if (!last)
580                                 continue;
581                         mkelvin = last;
582                 }
583                 if (last)
584                         line_to(gc, sec, mkelvin);
585                 else
586                         move_to(gc, sec, mkelvin);
587                 last = mkelvin;
588         }
589         cairo_stroke(cr);
590 }
591
592 /* gets both the actual start and end pressure as well as the scaling factors */
593 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
594 {
595         gc->leftx = 0;
596         gc->rightx = get_maxtime(pi);
597
598         gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
599         return pi->maxpressure != 0;
600 }
601
602 #define SAC_COLORS 9
603 static const rgb_t sac_color[SAC_COLORS] = {
604         { 0.0, 0.4, 0.2},
605         { 0.2, 0.6, 0.2},
606         { 0.4, 0.8, 0.2},
607         { 0.6, 0.8, 0.2},
608         { 0.8, 0.8, 0.2},
609         { 0.8, 0.6, 0.2},
610         { 0.8, 0.4, 0.2},
611         { 0.9, 0.3, 0.2},
612         { 1.0, 0.2, 0.2},
613 };
614
615 /* set the color for the pressure plot according to temporary sac rate
616  * as compared to avg_sac; the calculation simply maps the delta between
617  * sac and avg_sac to indexes 0 .. (SAC_COLORS - 1) with everything
618  * more than 6000 ml/min below avg_sac mapped to 0 */
619
620 static void set_sac_color(struct graphics_context *gc, int sac, int avg_sac)
621 {
622         int sac_index = 0;
623         int delta = sac - avg_sac + 7000;
624
625         sac_index = delta / 2000;
626         if (sac_index < 0)
627                 sac_index = 0;
628         if (sac_index > SAC_COLORS - 1)
629                 sac_index = SAC_COLORS - 1;
630         set_source_rgb_struct(gc, &sac_color[sac_index]);
631 }
632
633 /* calculate the current SAC in ml/min and convert to int */
634 #define GET_LOCAL_SAC(_entry1, _entry2, _dive)  (int)                           \
635         ((GET_PRESSURE((_entry1)) - GET_PRESSURE((_entry2))) *                  \
636                 (_dive)->cylinder[(_entry1)->cylinderindex].type.size.mliter /  \
637                 (((_entry2)->sec - (_entry1)->sec) / 60.0) /                    \
638                 (1 + ((_entry1)->depth + (_entry2)->depth) / 20000.0) /         \
639                 1000.0)
640
641 #define SAC_WINDOW 45   /* sliding window in seconds for current SAC calculation */
642
643 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi,
644                                 struct dive *dive)
645 {
646         int i;
647         int last = -1;
648         int lift_pen = FALSE;
649         int first_plot = TRUE;
650         int sac = 0;
651         struct plot_data *last_entry = NULL;
652
653         if (!get_cylinder_pressure_range(gc, pi))
654                 return;
655
656         for (i = 0; i < pi->nr; i++) {
657                 int mbar;
658                 struct plot_data *entry = pi->entry + i;
659
660                 mbar = GET_PRESSURE(entry);
661                 if (!entry->same_cylinder) {
662                         lift_pen = TRUE;
663                         last_entry = NULL;
664                 }
665                 if (!mbar) {
666                         lift_pen = TRUE;
667                         continue;
668                 }
669                 if (!last_entry) {
670                         last = i;
671                         last_entry = entry;
672                         sac = GET_LOCAL_SAC(entry, pi->entry + i + 1, dive);
673                 } else {
674                         int j;
675                         sac = 0;
676                         for (j = last; j < i; j++)
677                                 sac += GET_LOCAL_SAC(pi->entry + j, pi->entry + j + 1, dive);
678                         sac /= (i - last);
679                         if (entry->sec - last_entry->sec >= SAC_WINDOW) {
680                                 last++;
681                                 last_entry = pi->entry + last;
682                         }
683                 }
684                 set_sac_color(gc, sac, dive->sac);
685                 if (lift_pen) {
686                         if (!first_plot && entry->same_cylinder) {
687                                 /* if we have a previous event from the same tank,
688                                  * draw at least a short line */
689                                 int prev_pr;
690                                 prev_pr = GET_PRESSURE(entry - 1);
691                                 move_to(gc, (entry-1)->sec, prev_pr);
692                                 line_to(gc, entry->sec, mbar);
693                         } else {
694                                 first_plot = FALSE;
695                                 move_to(gc, entry->sec, mbar);
696                         }
697                         lift_pen = FALSE;
698                 } else {
699                         line_to(gc, entry->sec, mbar);
700                 }
701                 cairo_stroke(gc->cr);
702                 move_to(gc, entry->sec, mbar);
703         }
704 }
705
706 static void plot_pressure_value(struct graphics_context *gc, int mbar, int sec,
707                                 int xalign, int yalign)
708 {
709         int pressure;
710         const char *unit;
711
712         pressure = get_pressure_units(mbar, &unit);
713         text_render_options_t tro = {10, 0.2, 1.0, 0.2, xalign, yalign};
714         plot_text(gc, &tro, sec, mbar, "%d %s", pressure, unit);
715 }
716
717 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
718 {
719         int i;
720         int mbar, cyl;
721         int seen_cyl[MAX_CYLINDERS] = { FALSE, };
722         int last_pressure[MAX_CYLINDERS] = { 0, };
723         int last_time[MAX_CYLINDERS] = { 0, };
724         struct plot_data *entry;
725
726         if (!get_cylinder_pressure_range(gc, pi))
727                 return;
728
729         /* only loop over the actual events from the dive computer
730          * plus the second synthetic event at the start (to make sure
731          * we get "time=0" right)
732          * sadly with a recent change that first entry may no longer
733          * have any pressure reading - in that case just grab the
734          * pressure from the second entry */
735         if (GET_PRESSURE(pi->entry + 1) == 0 && GET_PRESSURE(pi->entry + 2) !=0)
736                 INTERPOLATED_PRESSURE(pi->entry + 1) = GET_PRESSURE(pi->entry + 2);
737         for (i = 1; i < pi->nr; i++) {
738                 entry = pi->entry + i;
739
740                 if (!entry->same_cylinder) {
741                         cyl = entry->cylinderindex;
742                         if (!seen_cyl[cyl]) {
743                                 mbar = GET_PRESSURE(entry);
744                                 plot_pressure_value(gc, mbar, entry->sec, LEFT, BOTTOM);
745                                 seen_cyl[cyl] = TRUE;
746                         }
747                         if (i > 2) {
748                                 /* remember the last pressure and time of
749                                  * the previous cylinder */
750                                 cyl = (entry - 1)->cylinderindex;
751                                 last_pressure[cyl] = GET_PRESSURE(entry - 1);
752                                 last_time[cyl] = (entry - 1)->sec;
753                         }
754                 }
755         }
756         cyl = entry->cylinderindex;
757         if (GET_PRESSURE(entry))
758                 last_pressure[cyl] = GET_PRESSURE(entry);
759         last_time[cyl] = entry->sec;
760
761         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
762                 if (last_time[cyl]) {
763                         plot_pressure_value(gc, last_pressure[cyl], last_time[cyl], CENTER, TOP);
764                 }
765         }
766 }
767
768 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
769 {
770         struct plot_data *p = entry;
771         int time = entry->sec;
772         int seconds = 90*(index+1);
773         struct plot_data *min, *max;
774         int avg, nr;
775
776         /* Go back 'seconds' in time */
777         while (p > first) {
778                 if (p[-1].sec < time - seconds)
779                         break;
780                 p--;
781         }
782
783         /* Then go forward until we hit an entry past the time */
784         min = max = p;
785         avg = p->depth;
786         nr = 1;
787         while (++p < last) {
788                 int depth = p->depth;
789                 if (p->sec > time + seconds)
790                         break;
791                 avg += depth;
792                 nr ++;
793                 if (depth < min->depth)
794                         min = p;
795                 if (depth > max->depth)
796                         max = p;
797         }
798         entry->min[index] = min;
799         entry->max[index] = max;
800         entry->avg[index] = (avg + nr/2) / nr;
801 }
802
803 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
804 {
805         analyze_plot_info_minmax_minute(entry, first, last, 0);
806         analyze_plot_info_minmax_minute(entry, first, last, 1);
807         analyze_plot_info_minmax_minute(entry, first, last, 2);
808 }
809
810 static velocity_t velocity(int speed)
811 {
812         velocity_t v;
813
814         if (speed < -304) /* ascent faster than -60ft/min */
815                 v = CRAZY;
816         else if (speed < -152) /* above -30ft/min */
817                 v = FAST;
818         else if (speed < -76) /* -15ft/min */
819                 v = MODERATE;
820         else if (speed < -25) /* -5ft/min */
821                 v = SLOW;
822         else if (speed < 25) /* very hard to find data, but it appears that the recommendations
823                                 for descent are usually about 2x ascent rate; still, we want 
824                                 stable to mean stable */
825                 v = STABLE;
826         else if (speed < 152) /* between 5 and 30ft/min is considered slow */
827                 v = SLOW;
828         else if (speed < 304) /* up to 60ft/min is moderate */
829                 v = MODERATE;
830         else if (speed < 507) /* up to 100ft/min is fast */
831                 v = FAST;
832         else /* more than that is just crazy - you'll blow your ears out */
833                 v = CRAZY;
834
835         return v;
836 }
837 static struct plot_info *analyze_plot_info(struct plot_info *pi)
838 {
839         int i;
840         int nr = pi->nr;
841
842         /* Do pressure min/max based on the non-surface data */
843         for (i = 0; i < nr; i++) {
844                 struct plot_data *entry = pi->entry+i;
845                 int pressure = GET_PRESSURE(entry);
846                 int temperature = entry->temperature;
847
848                 if (pressure) {
849                         if (pressure > pi->maxpressure)
850                                 pi->maxpressure = pressure;
851                 }
852
853                 if (temperature) {
854                         if (!pi->mintemp || temperature < pi->mintemp)
855                                 pi->mintemp = temperature;
856                         if (temperature > pi->maxtemp)
857                                 pi->maxtemp = temperature;
858                 }
859         }
860
861         /* Smoothing function: 5-point triangular smooth */
862         for (i = 2; i < nr; i++) {
863                 struct plot_data *entry = pi->entry+i;
864                 int depth;
865
866                 if (i < nr-2) {
867                         depth = entry[-2].depth + 2*entry[-1].depth + 3*entry[0].depth + 2*entry[1].depth + entry[2].depth;
868                         entry->smoothed = (depth+4) / 9;
869                 }
870                 /* vertical velocity in mm/sec */
871                 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
872                 if (entry[0].sec - entry[-1].sec) {
873                         entry->velocity = velocity((entry[0].depth - entry[-1].depth) / (entry[0].sec - entry[-1].sec));
874                         /* if our samples are short and we aren't too FAST*/
875                         if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
876                                 int past = -2;
877                                 while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
878                                         past--;
879                                 entry->velocity = velocity((entry[0].depth - entry[past].depth) / 
880                                                         (entry[0].sec - entry[past].sec));
881                         }
882                 } else
883                         entry->velocity = STABLE;
884         }
885
886         /* One-, two- and three-minute minmax data */
887         for (i = 0; i < nr; i++) {
888                 struct plot_data *entry = pi->entry +i;
889                 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
890         }
891         
892         return pi;
893 }
894
895 /*
896  * simple structure to track the beginning and end tank pressure as
897  * well as the integral of depth over time spent while we have no
898  * pressure reading from the tank */
899 typedef struct pr_track_struct pr_track_t;
900 struct pr_track_struct {
901         int start;
902         int end;
903         int t_start;
904         int t_end;
905         double pressure_time;
906         pr_track_t *next;
907 };
908
909 static pr_track_t *pr_track_alloc(int start, int t_start) {
910         pr_track_t *pt = malloc(sizeof(pr_track_t));
911         pt->start = start;
912         pt->t_start = t_start;
913         pt->end = 0;
914         pt->t_end = 0;
915         pt->pressure_time = 0.0;
916         pt->next = NULL;
917         return pt;
918 }
919
920 /* poor man's linked list */
921 static pr_track_t *list_last(pr_track_t *list)
922 {
923         pr_track_t *tail = list;
924         if (!tail)
925                 return NULL;
926         while (tail->next) {
927                 tail = tail->next;
928         }
929         return tail;
930 }
931
932 static pr_track_t *list_add(pr_track_t *list, pr_track_t *element)
933 {
934         pr_track_t *tail = list_last(list);
935         if (!tail)
936                 return element;
937         tail->next = element;
938         return list;
939 }
940
941 static void list_free(pr_track_t *list)
942 {
943         if (!list)
944                 return;
945         list_free(list->next);
946         free(list);
947 }
948
949 static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi,
950                                         pr_track_t **track_pr)
951 {
952         pr_track_t *list = NULL;
953         pr_track_t *nlist = NULL;
954         double pt, magic;
955         int cyl, i;
956         struct plot_data *entry;
957         int cur_pr[MAX_CYLINDERS];
958
959         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
960                 cur_pr[cyl] = track_pr[cyl]->start;
961         }
962
963         /* The first two are "fillers", but in case we don't have a sample
964          * at time 0 we need to process the second of them here */
965         for (i = 1; i < pi->nr; i++) {
966                 entry = pi->entry + i;
967                 if (SENSOR_PRESSURE(entry)) {
968                         cur_pr[entry->cylinderindex] = SENSOR_PRESSURE(entry);
969                 } else {
970                         if(!list || list->t_end < entry->sec) {
971                                 nlist = track_pr[entry->cylinderindex];
972                                 list = NULL;
973                                 while (nlist && nlist->t_start <= entry->sec) {
974                                         list = nlist;
975                                         nlist = list->next;
976                                 }
977                                 /* there may be multiple segments - so
978                                  * let's assemble the length */
979                                 nlist = list;
980                                 pt = list->pressure_time;
981                                 while (!nlist->end) {
982                                         nlist = nlist->next;
983                                         if (!nlist) {
984                                                 /* oops - we have no end pressure,
985                                                  * so this means this is a tank without
986                                                  * gas consumption information */
987                                                 break;
988                                         }
989                                         pt += nlist->pressure_time;
990                                 }
991                                 if (!nlist) {
992                                         /* just continue without calculating
993                                          * interpolated values */
994                                         INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
995                                         list = NULL;
996                                         continue;
997                                 }
998                                 magic = (nlist->end - cur_pr[entry->cylinderindex]) / pt;
999                         }
1000                         if (pt != 0.0) {
1001                                 double cur_pt = (entry->sec - (entry-1)->sec) *
1002                                         (1 + (entry->depth + (entry-1)->depth) / 20000.0);
1003                                 INTERPOLATED_PRESSURE(entry) =
1004                                         cur_pr[entry->cylinderindex] + cur_pt * magic;
1005                                 cur_pr[entry->cylinderindex] = INTERPOLATED_PRESSURE(entry);
1006                         } else
1007                                 INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
1008                 }
1009         }
1010 }
1011
1012 static int get_cylinder_index(struct dive *dive, struct event *ev)
1013 {
1014         int i;
1015
1016         /*
1017          * Try to find a cylinder that matches the O2 percentage
1018          * in the gas change event 'value' field.
1019          *
1020          * Crazy suunto gas change events. We really should do
1021          * this in libdivecomputer or something.
1022          */
1023         for (i = 0; i < MAX_CYLINDERS; i++) {
1024                 cylinder_t *cyl = dive->cylinder+i;
1025                 int o2 = (cyl->gasmix.o2.permille + 5) / 10;
1026                 if (o2 == ev->value)
1027                         return i;
1028         }
1029
1030         return 0;
1031 }
1032
1033 static struct event *get_next_gaschange(struct event *event)
1034 {
1035         while (event) {
1036                 if (!strcmp(event->name, "gaschange"))
1037                         return event;
1038                 event = event->next;
1039         }
1040         return event;
1041 }
1042
1043 static int set_cylinder_index(struct plot_info *pi, int i, int cylinderindex, unsigned int end)
1044 {
1045         while (i < pi->nr) {
1046                 struct plot_data *entry = pi->entry+i;
1047                 if (entry->sec > end)
1048                         break;
1049                 if (entry->cylinderindex != cylinderindex) {
1050                         entry->cylinderindex = cylinderindex;
1051                         entry->pressure[0] = 0;
1052                 }
1053                 i++;
1054         }
1055         return i;
1056 }
1057
1058 static void check_gas_change_events(struct dive *dive, struct plot_info *pi)
1059 {
1060         int i = 0, cylinderindex = 0;
1061         struct event *ev = get_next_gaschange(dive->events);
1062
1063         if (!ev)
1064                 return;
1065
1066         do {
1067                 i = set_cylinder_index(pi, i, cylinderindex, ev->time.seconds);
1068                 cylinderindex = get_cylinder_index(dive, ev);
1069                 ev = get_next_gaschange(ev->next);
1070         } while (ev);
1071         set_cylinder_index(pi, i, cylinderindex, ~0u);
1072 }
1073
1074 /* for computers that track gas changes through events */
1075 static int count_gas_change_events(struct dive *dive)
1076 {
1077         int count = 0;
1078         struct event *ev = get_next_gaschange(dive->events);
1079
1080         while (ev) {
1081                 count++;
1082                 ev = get_next_gaschange(ev->next);
1083         }
1084         return count;
1085 }
1086
1087 /*
1088  * Create a plot-info with smoothing and ranged min/max
1089  *
1090  * This also makes sure that we have extra empty events on both
1091  * sides, so that you can do end-points without having to worry
1092  * about it.
1093  */
1094 static struct plot_info *create_plot_info(struct dive *dive, int nr_samples, struct sample *dive_sample)
1095 {
1096         int cylinderindex = -1;
1097         int lastdepth, lastindex;
1098         int i, pi_idx, nr, sec, cyl;
1099         size_t alloc_size;
1100         struct plot_info *pi;
1101         pr_track_t *track_pr[MAX_CYLINDERS] = {NULL, };
1102         pr_track_t *pr_track, *current;
1103         gboolean missing_pr = FALSE;
1104         struct plot_data *entry = NULL;
1105         struct event *ev;
1106
1107         /* we want to potentially add synthetic plot_info elements for the gas changes */
1108         nr = nr_samples + 4 + 2 * count_gas_change_events(dive);
1109         alloc_size = plot_info_size(nr);
1110         pi = malloc(alloc_size);
1111         if (!pi)
1112                 return pi;
1113         memset(pi, 0, alloc_size);
1114         pi->nr = nr;
1115         pi_idx = 2; /* the two extra events at the start */
1116         /* check for gas changes before the samples start */
1117         ev = get_next_gaschange(dive->events);
1118         while (ev && ev->time.seconds < dive_sample->time.seconds) {
1119                 entry = pi->entry + pi_idx;
1120                 entry->sec = ev->time.seconds;
1121                 entry->depth = 0; /* is that always correct ? */
1122                 pi_idx++;
1123                 ev = get_next_gaschange(ev->next);
1124         }
1125         if (ev && ev->time.seconds == dive_sample->time.seconds) {
1126                 /* we already have a sample at the time of the event */
1127                 ev = get_next_gaschange(ev->next);
1128         }
1129         sec = 0;
1130         lastindex = 0;
1131         lastdepth = -1;
1132         for (i = 0; i < nr_samples; i++) {
1133                 int depth;
1134                 int delay = 0;
1135                 struct sample *sample = dive_sample+i;
1136
1137                 entry = pi->entry + i + pi_idx;
1138                 while (ev && ev->time.seconds < sample->time.seconds) {
1139                         /* insert two fake plot info structures for the end of
1140                          * the old tank and the start of the new tank */
1141                         if (ev->time.seconds == sample->time.seconds - 1) {
1142                                 entry->sec = ev->time.seconds - 1;
1143                                 (entry+1)->sec = ev->time.seconds;
1144                         } else {
1145                                 entry->sec = ev->time.seconds;
1146                                 (entry+1)->sec = ev->time.seconds + 1;
1147                         }
1148                         /* we need a fake depth - let's interpolate */
1149                         if (i) {
1150                                 entry->depth = sample->depth.mm -
1151                                         (sample->depth.mm - (sample-1)->depth.mm) / 2;
1152                         } else
1153                                 entry->depth = sample->depth.mm;
1154                         (entry+1)->depth = entry->depth;
1155                         pi_idx += 2;
1156                         entry = pi->entry + i + pi_idx;
1157                         ev = get_next_gaschange(ev->next);
1158                 }
1159                 if (ev && ev->time.seconds == sample->time.seconds) {
1160                         /* we already have a sample at the time of the event
1161                          * just add a new one for the old tank and delay the
1162                          * real even by one second (to keep time monotonous) */
1163                         entry->sec = ev->time.seconds;
1164                         entry->depth = sample->depth.mm;
1165                         pi_idx++;
1166                         entry = pi->entry + i + pi_idx;
1167                         ev = get_next_gaschange(ev->next);
1168                         delay = 1;
1169                 }
1170                 sec = entry->sec = sample->time.seconds + delay;
1171                 depth = entry->depth = sample->depth.mm;
1172                 entry->cylinderindex = sample->cylinderindex;
1173                 SENSOR_PRESSURE(entry) = sample->cylinderpressure.mbar;
1174                 entry->temperature = sample->temperature.mkelvin;
1175
1176                 if (depth || lastdepth)
1177                         lastindex = i + pi_idx;
1178
1179                 lastdepth = depth;
1180                 if (depth > pi->maxdepth)
1181                         pi->maxdepth = depth;
1182         }
1183         entry = pi->entry + i + pi_idx;
1184         /* are there still unprocessed gas changes? that would be very strange */
1185         while (ev) {
1186                 entry->sec = ev->time.seconds;
1187                 entry->depth = 0; /* why are there gas changes after the dive is over? */
1188                 pi_idx++;
1189                 entry = pi->entry + i + pi_idx;
1190                 ev = get_next_gaschange(ev->next);
1191         }
1192         nr = nr_samples + pi_idx - 2;
1193         check_gas_change_events(dive, pi);
1194
1195         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) /* initialize the start pressures */
1196                 track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1);
1197         current = track_pr[pi->entry[2].cylinderindex];
1198         for (i = 0; i < nr + 1; i++) {
1199                 entry = pi->entry + i + 1;
1200
1201                 entry->same_cylinder = entry->cylinderindex == cylinderindex;
1202                 cylinderindex = entry->cylinderindex;
1203
1204                 /* track the segments per cylinder and their pressure/time integral */
1205                 if (!entry->same_cylinder) {
1206                         current->end = SENSOR_PRESSURE(entry-1);
1207                         current->t_end = (entry-1)->sec;
1208                         current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1209                         track_pr[cylinderindex] = list_add(track_pr[cylinderindex], current);
1210                 } else { /* same cylinder */
1211                         if ((!SENSOR_PRESSURE(entry) && SENSOR_PRESSURE(entry-1)) ||
1212                                 (SENSOR_PRESSURE(entry) && !SENSOR_PRESSURE(entry-1))) {
1213                                 /* transmitter changed its working status */
1214                                 current->end = SENSOR_PRESSURE(entry-1);
1215                                 current->t_end = (entry-1)->sec;
1216                                 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1217                                 track_pr[cylinderindex] =
1218                                         list_add(track_pr[cylinderindex], current);
1219                         }
1220                 }
1221                 /* finally, do the discrete integration to get the SAC rate equivalent */
1222                 current->pressure_time += (entry->sec - (entry-1)->sec) *
1223                         (1 + (entry->depth + (entry-1)->depth) / 20000.0);
1224                 missing_pr |= !SENSOR_PRESSURE(entry);
1225         }
1226
1227         if (entry)
1228                 current->t_end = entry->sec;
1229
1230         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) { /* initialize the end pressures */
1231                 int pr = dive->cylinder[cyl].end.mbar;
1232                 if (pr && track_pr[cyl]) {
1233                         pr_track = list_last(track_pr[cyl]);
1234                         pr_track->end = pr;
1235                 }
1236         }
1237         /* Fill in the last two entries with empty values but valid times
1238          * without creating a false cylinder change event */
1239         i = nr + 2;
1240         pi->entry[i].sec = sec + 20;
1241         pi->entry[i].same_cylinder = 1;
1242         pi->entry[i].cylinderindex = pi->entry[i-1].cylinderindex;
1243         INTERPOLATED_PRESSURE(pi->entry + i) = GET_PRESSURE(pi->entry + i - 1);
1244         pi->entry[i+1].sec = sec + 40;
1245         pi->entry[i+1].same_cylinder = 1;
1246         pi->entry[i+1].cylinderindex = pi->entry[i-1].cylinderindex;
1247         INTERPOLATED_PRESSURE(pi->entry + i + 1) = GET_PRESSURE(pi->entry + i - 1);
1248         /* the number of actual entries - some computers have lots of
1249          * depth 0 samples at the end of a dive, we want to make sure
1250          * we have exactly one of them at the end */
1251         pi->nr = lastindex+1;
1252         while (pi->nr <= i+2 && pi->entry[pi->nr-1].depth > 0)
1253                 pi->nr++;
1254         pi->maxtime = pi->entry[lastindex].sec;
1255
1256         /* Analyze_plot_info() will do the sample max pressures,
1257          * this handles the manual pressures
1258          */
1259         pi->maxpressure = 0;
1260         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
1261                 unsigned int mbar = dive->cylinder[cyl].start.mbar;
1262                 if (mbar > pi->maxpressure)
1263                         pi->maxpressure = mbar;
1264         }
1265
1266         pi->meandepth = dive->meandepth.mm;
1267
1268         if (missing_pr) {
1269                 fill_missing_tank_pressures(dive, pi, track_pr);
1270         }
1271         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++)
1272                 list_free(track_pr[cyl]);
1273         if (0) /* awesome for debugging - not useful otherwise */
1274                 dump_pi(pi);
1275         return analyze_plot_info(pi);
1276 }
1277
1278 void plot(struct graphics_context *gc, cairo_rectangle_int_t *drawing_area, struct dive *dive)
1279 {
1280         struct plot_info *pi;
1281         static struct sample fake[4];
1282         struct sample *sample = dive->sample;
1283         int nr = dive->samples;
1284
1285         if (!nr) {
1286                 int duration = dive->duration.seconds;
1287                 int maxdepth = dive->maxdepth.mm;
1288                 sample = fake;
1289                 fake[1].time.seconds = duration * 0.05;
1290                 fake[1].depth.mm = maxdepth;
1291                 fake[2].time.seconds = duration * 0.95;
1292                 fake[2].depth.mm = maxdepth;
1293                 fake[3].time.seconds = duration * 1.00;
1294                 nr = 4;
1295         }
1296
1297         pi = create_plot_info(dive, nr, sample);
1298
1299         cairo_translate(gc->cr, drawing_area->x, drawing_area->y);
1300         cairo_set_line_width(gc->cr, 2);
1301         cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
1302         cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
1303
1304         /*
1305          * We can use "cairo_translate()" because that doesn't
1306          * scale line width etc. But the actual scaling we need
1307          * do set up ourselves..
1308          *
1309          * Snif. What a pity.
1310          */
1311         gc->maxx = (drawing_area->width - 2*drawing_area->x);
1312         gc->maxy = (drawing_area->height - 2*drawing_area->y);
1313
1314         /* Temperature profile */
1315         plot_temperature_profile(gc, pi);
1316
1317         /* Depth profile */
1318         plot_depth_profile(gc, pi);
1319         plot_events(gc, pi, dive);
1320
1321         /* Cylinder pressure plot */
1322         plot_cylinder_pressure(gc, pi, dive);
1323
1324         /* Text on top of all graphs.. */
1325         plot_temperature_text(gc, pi);
1326         plot_depth_text(gc, pi);
1327         plot_cylinder_pressure_text(gc, pi);
1328
1329         /* Bounding box last */
1330         gc->leftx = 0; gc->rightx = 1.0;
1331         gc->topy = 0; gc->bottomy = 1.0;
1332
1333         set_source_rgb(gc, 1, 1, 1);
1334         move_to(gc, 0, 0);
1335         line_to(gc, 0, 1);
1336         line_to(gc, 1, 1);
1337         line_to(gc, 1, 0);
1338         cairo_close_path(gc->cr);
1339         cairo_stroke(gc->cr);
1340
1341         free(pi);
1342 }