]> git.tdb.fi Git - ext/subsurface.git/blob - profile.c
Don't colorize the pressure plot when printing
[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         if (!gc->printer) {
626                 sac_index = delta / 2000;
627                 if (sac_index < 0)
628                         sac_index = 0;
629                 if (sac_index > SAC_COLORS - 1)
630                         sac_index = SAC_COLORS - 1;
631                 set_source_rgb_struct(gc, &sac_color[sac_index]);
632         } else {
633                 set_source_rgb(gc, 1.0, 1.0, 1.0);
634         }
635 }
636
637 /* calculate the current SAC in ml/min and convert to int */
638 #define GET_LOCAL_SAC(_entry1, _entry2, _dive)  (int)                           \
639         ((GET_PRESSURE((_entry1)) - GET_PRESSURE((_entry2))) *                  \
640                 (_dive)->cylinder[(_entry1)->cylinderindex].type.size.mliter /  \
641                 (((_entry2)->sec - (_entry1)->sec) / 60.0) /                    \
642                 (1 + ((_entry1)->depth + (_entry2)->depth) / 20000.0) /         \
643                 1000.0)
644
645 #define SAC_WINDOW 45   /* sliding window in seconds for current SAC calculation */
646
647 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi,
648                                 struct dive *dive)
649 {
650         int i;
651         int last = -1;
652         int lift_pen = FALSE;
653         int first_plot = TRUE;
654         int sac = 0;
655         struct plot_data *last_entry = NULL;
656
657         if (!get_cylinder_pressure_range(gc, pi))
658                 return;
659
660         for (i = 0; i < pi->nr; i++) {
661                 int mbar;
662                 struct plot_data *entry = pi->entry + i;
663
664                 mbar = GET_PRESSURE(entry);
665                 if (!entry->same_cylinder) {
666                         lift_pen = TRUE;
667                         last_entry = NULL;
668                 }
669                 if (!mbar) {
670                         lift_pen = TRUE;
671                         continue;
672                 }
673                 if (!last_entry) {
674                         last = i;
675                         last_entry = entry;
676                         sac = GET_LOCAL_SAC(entry, pi->entry + i + 1, dive);
677                 } else {
678                         int j;
679                         sac = 0;
680                         for (j = last; j < i; j++)
681                                 sac += GET_LOCAL_SAC(pi->entry + j, pi->entry + j + 1, dive);
682                         sac /= (i - last);
683                         if (entry->sec - last_entry->sec >= SAC_WINDOW) {
684                                 last++;
685                                 last_entry = pi->entry + last;
686                         }
687                 }
688                 set_sac_color(gc, sac, dive->sac);
689                 if (lift_pen) {
690                         if (!first_plot && entry->same_cylinder) {
691                                 /* if we have a previous event from the same tank,
692                                  * draw at least a short line */
693                                 int prev_pr;
694                                 prev_pr = GET_PRESSURE(entry - 1);
695                                 move_to(gc, (entry-1)->sec, prev_pr);
696                                 line_to(gc, entry->sec, mbar);
697                         } else {
698                                 first_plot = FALSE;
699                                 move_to(gc, entry->sec, mbar);
700                         }
701                         lift_pen = FALSE;
702                 } else {
703                         line_to(gc, entry->sec, mbar);
704                 }
705                 cairo_stroke(gc->cr);
706                 move_to(gc, entry->sec, mbar);
707         }
708 }
709
710 static void plot_pressure_value(struct graphics_context *gc, int mbar, int sec,
711                                 int xalign, int yalign)
712 {
713         int pressure;
714         const char *unit;
715
716         pressure = get_pressure_units(mbar, &unit);
717         text_render_options_t tro = {10, 0.2, 1.0, 0.2, xalign, yalign};
718         plot_text(gc, &tro, sec, mbar, "%d %s", pressure, unit);
719 }
720
721 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
722 {
723         int i;
724         int mbar, cyl;
725         int seen_cyl[MAX_CYLINDERS] = { FALSE, };
726         int last_pressure[MAX_CYLINDERS] = { 0, };
727         int last_time[MAX_CYLINDERS] = { 0, };
728         struct plot_data *entry;
729
730         if (!get_cylinder_pressure_range(gc, pi))
731                 return;
732
733         /* only loop over the actual events from the dive computer
734          * plus the second synthetic event at the start (to make sure
735          * we get "time=0" right)
736          * sadly with a recent change that first entry may no longer
737          * have any pressure reading - in that case just grab the
738          * pressure from the second entry */
739         if (GET_PRESSURE(pi->entry + 1) == 0 && GET_PRESSURE(pi->entry + 2) !=0)
740                 INTERPOLATED_PRESSURE(pi->entry + 1) = GET_PRESSURE(pi->entry + 2);
741         for (i = 1; i < pi->nr; i++) {
742                 entry = pi->entry + i;
743
744                 if (!entry->same_cylinder) {
745                         cyl = entry->cylinderindex;
746                         if (!seen_cyl[cyl]) {
747                                 mbar = GET_PRESSURE(entry);
748                                 plot_pressure_value(gc, mbar, entry->sec, LEFT, BOTTOM);
749                                 seen_cyl[cyl] = TRUE;
750                         }
751                         if (i > 2) {
752                                 /* remember the last pressure and time of
753                                  * the previous cylinder */
754                                 cyl = (entry - 1)->cylinderindex;
755                                 last_pressure[cyl] = GET_PRESSURE(entry - 1);
756                                 last_time[cyl] = (entry - 1)->sec;
757                         }
758                 }
759         }
760         cyl = entry->cylinderindex;
761         if (GET_PRESSURE(entry))
762                 last_pressure[cyl] = GET_PRESSURE(entry);
763         last_time[cyl] = entry->sec;
764
765         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
766                 if (last_time[cyl]) {
767                         plot_pressure_value(gc, last_pressure[cyl], last_time[cyl], CENTER, TOP);
768                 }
769         }
770 }
771
772 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
773 {
774         struct plot_data *p = entry;
775         int time = entry->sec;
776         int seconds = 90*(index+1);
777         struct plot_data *min, *max;
778         int avg, nr;
779
780         /* Go back 'seconds' in time */
781         while (p > first) {
782                 if (p[-1].sec < time - seconds)
783                         break;
784                 p--;
785         }
786
787         /* Then go forward until we hit an entry past the time */
788         min = max = p;
789         avg = p->depth;
790         nr = 1;
791         while (++p < last) {
792                 int depth = p->depth;
793                 if (p->sec > time + seconds)
794                         break;
795                 avg += depth;
796                 nr ++;
797                 if (depth < min->depth)
798                         min = p;
799                 if (depth > max->depth)
800                         max = p;
801         }
802         entry->min[index] = min;
803         entry->max[index] = max;
804         entry->avg[index] = (avg + nr/2) / nr;
805 }
806
807 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
808 {
809         analyze_plot_info_minmax_minute(entry, first, last, 0);
810         analyze_plot_info_minmax_minute(entry, first, last, 1);
811         analyze_plot_info_minmax_minute(entry, first, last, 2);
812 }
813
814 static velocity_t velocity(int speed)
815 {
816         velocity_t v;
817
818         if (speed < -304) /* ascent faster than -60ft/min */
819                 v = CRAZY;
820         else if (speed < -152) /* above -30ft/min */
821                 v = FAST;
822         else if (speed < -76) /* -15ft/min */
823                 v = MODERATE;
824         else if (speed < -25) /* -5ft/min */
825                 v = SLOW;
826         else if (speed < 25) /* very hard to find data, but it appears that the recommendations
827                                 for descent are usually about 2x ascent rate; still, we want 
828                                 stable to mean stable */
829                 v = STABLE;
830         else if (speed < 152) /* between 5 and 30ft/min is considered slow */
831                 v = SLOW;
832         else if (speed < 304) /* up to 60ft/min is moderate */
833                 v = MODERATE;
834         else if (speed < 507) /* up to 100ft/min is fast */
835                 v = FAST;
836         else /* more than that is just crazy - you'll blow your ears out */
837                 v = CRAZY;
838
839         return v;
840 }
841 static struct plot_info *analyze_plot_info(struct plot_info *pi)
842 {
843         int i;
844         int nr = pi->nr;
845
846         /* Do pressure min/max based on the non-surface data */
847         for (i = 0; i < nr; i++) {
848                 struct plot_data *entry = pi->entry+i;
849                 int pressure = GET_PRESSURE(entry);
850                 int temperature = entry->temperature;
851
852                 if (pressure) {
853                         if (pressure > pi->maxpressure)
854                                 pi->maxpressure = pressure;
855                 }
856
857                 if (temperature) {
858                         if (!pi->mintemp || temperature < pi->mintemp)
859                                 pi->mintemp = temperature;
860                         if (temperature > pi->maxtemp)
861                                 pi->maxtemp = temperature;
862                 }
863         }
864
865         /* Smoothing function: 5-point triangular smooth */
866         for (i = 2; i < nr; i++) {
867                 struct plot_data *entry = pi->entry+i;
868                 int depth;
869
870                 if (i < nr-2) {
871                         depth = entry[-2].depth + 2*entry[-1].depth + 3*entry[0].depth + 2*entry[1].depth + entry[2].depth;
872                         entry->smoothed = (depth+4) / 9;
873                 }
874                 /* vertical velocity in mm/sec */
875                 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
876                 if (entry[0].sec - entry[-1].sec) {
877                         entry->velocity = velocity((entry[0].depth - entry[-1].depth) / (entry[0].sec - entry[-1].sec));
878                         /* if our samples are short and we aren't too FAST*/
879                         if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
880                                 int past = -2;
881                                 while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
882                                         past--;
883                                 entry->velocity = velocity((entry[0].depth - entry[past].depth) / 
884                                                         (entry[0].sec - entry[past].sec));
885                         }
886                 } else
887                         entry->velocity = STABLE;
888         }
889
890         /* One-, two- and three-minute minmax data */
891         for (i = 0; i < nr; i++) {
892                 struct plot_data *entry = pi->entry +i;
893                 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
894         }
895         
896         return pi;
897 }
898
899 /*
900  * simple structure to track the beginning and end tank pressure as
901  * well as the integral of depth over time spent while we have no
902  * pressure reading from the tank */
903 typedef struct pr_track_struct pr_track_t;
904 struct pr_track_struct {
905         int start;
906         int end;
907         int t_start;
908         int t_end;
909         double pressure_time;
910         pr_track_t *next;
911 };
912
913 static pr_track_t *pr_track_alloc(int start, int t_start) {
914         pr_track_t *pt = malloc(sizeof(pr_track_t));
915         pt->start = start;
916         pt->t_start = t_start;
917         pt->end = 0;
918         pt->t_end = 0;
919         pt->pressure_time = 0.0;
920         pt->next = NULL;
921         return pt;
922 }
923
924 /* poor man's linked list */
925 static pr_track_t *list_last(pr_track_t *list)
926 {
927         pr_track_t *tail = list;
928         if (!tail)
929                 return NULL;
930         while (tail->next) {
931                 tail = tail->next;
932         }
933         return tail;
934 }
935
936 static pr_track_t *list_add(pr_track_t *list, pr_track_t *element)
937 {
938         pr_track_t *tail = list_last(list);
939         if (!tail)
940                 return element;
941         tail->next = element;
942         return list;
943 }
944
945 static void list_free(pr_track_t *list)
946 {
947         if (!list)
948                 return;
949         list_free(list->next);
950         free(list);
951 }
952
953 static void dump_pr_track(pr_track_t **track_pr)
954 {
955         int cyl;
956         pr_track_t *list;
957
958         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
959                 list = track_pr[cyl];
960                 while (list) {
961                         printf("cyl%d: start %d end %d t_start %d t_end %d pt %6.3f\n", cyl,
962                                 list->start, list->end, list->t_start, list->t_end, list->pressure_time);
963                         list = list->next;
964                 }
965         }
966 }
967
968 static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi,
969                                         pr_track_t **track_pr)
970 {
971         pr_track_t *list = NULL;
972         pr_track_t *nlist = NULL;
973         double pt, magic;
974         int cyl, i;
975         struct plot_data *entry;
976         int cur_pr[MAX_CYLINDERS];
977
978         if (0) {
979                 /* another great debugging tool */
980                 dump_pr_track(track_pr);
981         }
982         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
983                 cur_pr[cyl] = track_pr[cyl]->start;
984         }
985
986         /* The first two are "fillers", but in case we don't have a sample
987          * at time 0 we need to process the second of them here */
988         for (i = 1; i < pi->nr; i++) {
989                 entry = pi->entry + i;
990                 if (SENSOR_PRESSURE(entry)) {
991                         cur_pr[entry->cylinderindex] = SENSOR_PRESSURE(entry);
992                 } else {
993                         if(!list || list->t_end < entry->sec) {
994                                 nlist = track_pr[entry->cylinderindex];
995                                 list = NULL;
996                                 while (nlist && nlist->t_start <= entry->sec) {
997                                         list = nlist;
998                                         nlist = list->next;
999                                 }
1000                                 /* there may be multiple segments - so
1001                                  * let's assemble the length */
1002                                 nlist = list;
1003                                 pt = list->pressure_time;
1004                                 while (!nlist->end) {
1005                                         nlist = nlist->next;
1006                                         if (!nlist) {
1007                                                 /* oops - we have no end pressure,
1008                                                  * so this means this is a tank without
1009                                                  * gas consumption information */
1010                                                 break;
1011                                         }
1012                                         pt += nlist->pressure_time;
1013                                 }
1014                                 if (!nlist) {
1015                                         /* just continue without calculating
1016                                          * interpolated values */
1017                                         INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
1018                                         list = NULL;
1019                                         continue;
1020                                 }
1021                                 magic = (nlist->end - cur_pr[entry->cylinderindex]) / pt;
1022                         }
1023                         if (pt != 0.0) {
1024                                 double cur_pt = (entry->sec - (entry-1)->sec) *
1025                                         (1 + (entry->depth + (entry-1)->depth) / 20000.0);
1026                                 INTERPOLATED_PRESSURE(entry) =
1027                                         cur_pr[entry->cylinderindex] + cur_pt * magic + 0.5;
1028                                 cur_pr[entry->cylinderindex] = INTERPOLATED_PRESSURE(entry);
1029                         } else
1030                                 INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
1031                 }
1032         }
1033 }
1034
1035 static int get_cylinder_index(struct dive *dive, struct event *ev)
1036 {
1037         int i;
1038
1039         /*
1040          * Try to find a cylinder that matches the O2 percentage
1041          * in the gas change event 'value' field.
1042          *
1043          * Crazy suunto gas change events. We really should do
1044          * this in libdivecomputer or something.
1045          */
1046         for (i = 0; i < MAX_CYLINDERS; i++) {
1047                 cylinder_t *cyl = dive->cylinder+i;
1048                 int o2 = (cyl->gasmix.o2.permille + 5) / 10;
1049                 if (o2 == ev->value)
1050                         return i;
1051         }
1052
1053         return 0;
1054 }
1055
1056 static struct event *get_next_gaschange(struct event *event)
1057 {
1058         while (event) {
1059                 if (!strcmp(event->name, "gaschange"))
1060                         return event;
1061                 event = event->next;
1062         }
1063         return event;
1064 }
1065
1066 static int set_cylinder_index(struct plot_info *pi, int i, int cylinderindex, unsigned int end)
1067 {
1068         while (i < pi->nr) {
1069                 struct plot_data *entry = pi->entry+i;
1070                 if (entry->sec > end)
1071                         break;
1072                 if (entry->cylinderindex != cylinderindex) {
1073                         entry->cylinderindex = cylinderindex;
1074                         entry->pressure[0] = 0;
1075                 }
1076                 i++;
1077         }
1078         return i;
1079 }
1080
1081 static void check_gas_change_events(struct dive *dive, struct plot_info *pi)
1082 {
1083         int i = 0, cylinderindex = 0;
1084         struct event *ev = get_next_gaschange(dive->events);
1085
1086         if (!ev)
1087                 return;
1088
1089         do {
1090                 i = set_cylinder_index(pi, i, cylinderindex, ev->time.seconds);
1091                 cylinderindex = get_cylinder_index(dive, ev);
1092                 ev = get_next_gaschange(ev->next);
1093         } while (ev);
1094         set_cylinder_index(pi, i, cylinderindex, ~0u);
1095 }
1096
1097 /* for computers that track gas changes through events */
1098 static int count_gas_change_events(struct dive *dive)
1099 {
1100         int count = 0;
1101         struct event *ev = get_next_gaschange(dive->events);
1102
1103         while (ev) {
1104                 count++;
1105                 ev = get_next_gaschange(ev->next);
1106         }
1107         return count;
1108 }
1109
1110 /*
1111  * Create a plot-info with smoothing and ranged min/max
1112  *
1113  * This also makes sure that we have extra empty events on both
1114  * sides, so that you can do end-points without having to worry
1115  * about it.
1116  */
1117 static struct plot_info *create_plot_info(struct dive *dive, int nr_samples, struct sample *dive_sample)
1118 {
1119         int cylinderindex = -1;
1120         int lastdepth, lastindex;
1121         int i, pi_idx, nr, sec, cyl;
1122         size_t alloc_size;
1123         struct plot_info *pi;
1124         pr_track_t *track_pr[MAX_CYLINDERS] = {NULL, };
1125         pr_track_t *pr_track, *current;
1126         gboolean missing_pr = FALSE;
1127         struct plot_data *entry = NULL;
1128         struct event *ev;
1129
1130         /* we want to potentially add synthetic plot_info elements for the gas changes */
1131         nr = nr_samples + 4 + 2 * count_gas_change_events(dive);
1132         alloc_size = plot_info_size(nr);
1133         pi = malloc(alloc_size);
1134         if (!pi)
1135                 return pi;
1136         memset(pi, 0, alloc_size);
1137         pi->nr = nr;
1138         pi_idx = 2; /* the two extra events at the start */
1139         /* check for gas changes before the samples start */
1140         ev = get_next_gaschange(dive->events);
1141         while (ev && ev->time.seconds < dive_sample->time.seconds) {
1142                 entry = pi->entry + pi_idx;
1143                 entry->sec = ev->time.seconds;
1144                 entry->depth = 0; /* is that always correct ? */
1145                 pi_idx++;
1146                 ev = get_next_gaschange(ev->next);
1147         }
1148         if (ev && ev->time.seconds == dive_sample->time.seconds) {
1149                 /* we already have a sample at the time of the event */
1150                 ev = get_next_gaschange(ev->next);
1151         }
1152         sec = 0;
1153         lastindex = 0;
1154         lastdepth = -1;
1155         for (i = 0; i < nr_samples; i++) {
1156                 int depth;
1157                 int delay = 0;
1158                 struct sample *sample = dive_sample+i;
1159
1160                 entry = pi->entry + i + pi_idx;
1161                 while (ev && ev->time.seconds < sample->time.seconds) {
1162                         /* insert two fake plot info structures for the end of
1163                          * the old tank and the start of the new tank */
1164                         if (ev->time.seconds == sample->time.seconds - 1) {
1165                                 entry->sec = ev->time.seconds - 1;
1166                                 (entry+1)->sec = ev->time.seconds;
1167                         } else {
1168                                 entry->sec = ev->time.seconds;
1169                                 (entry+1)->sec = ev->time.seconds + 1;
1170                         }
1171                         /* we need a fake depth - let's interpolate */
1172                         if (i) {
1173                                 entry->depth = sample->depth.mm -
1174                                         (sample->depth.mm - (sample-1)->depth.mm) / 2;
1175                         } else
1176                                 entry->depth = sample->depth.mm;
1177                         (entry+1)->depth = entry->depth;
1178                         pi_idx += 2;
1179                         entry = pi->entry + i + pi_idx;
1180                         ev = get_next_gaschange(ev->next);
1181                 }
1182                 if (ev && ev->time.seconds == sample->time.seconds) {
1183                         /* we already have a sample at the time of the event
1184                          * just add a new one for the old tank and delay the
1185                          * real even by one second (to keep time monotonous) */
1186                         entry->sec = ev->time.seconds;
1187                         entry->depth = sample->depth.mm;
1188                         pi_idx++;
1189                         entry = pi->entry + i + pi_idx;
1190                         ev = get_next_gaschange(ev->next);
1191                         delay = 1;
1192                 }
1193                 sec = entry->sec = sample->time.seconds + delay;
1194                 depth = entry->depth = sample->depth.mm;
1195                 entry->cylinderindex = sample->cylinderindex;
1196                 SENSOR_PRESSURE(entry) = sample->cylinderpressure.mbar;
1197                 entry->temperature = sample->temperature.mkelvin;
1198
1199                 if (depth || lastdepth)
1200                         lastindex = i + pi_idx;
1201
1202                 lastdepth = depth;
1203                 if (depth > pi->maxdepth)
1204                         pi->maxdepth = depth;
1205         }
1206         entry = pi->entry + i + pi_idx;
1207         /* are there still unprocessed gas changes? that would be very strange */
1208         while (ev) {
1209                 entry->sec = ev->time.seconds;
1210                 entry->depth = 0; /* why are there gas changes after the dive is over? */
1211                 pi_idx++;
1212                 entry = pi->entry + i + pi_idx;
1213                 ev = get_next_gaschange(ev->next);
1214         }
1215         nr = nr_samples + pi_idx - 2;
1216         check_gas_change_events(dive, pi);
1217
1218         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) /* initialize the start pressures */
1219                 track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1);
1220         current = track_pr[pi->entry[2].cylinderindex];
1221         for (i = 0; i < nr + 1; i++) {
1222                 entry = pi->entry + i + 1;
1223
1224                 entry->same_cylinder = entry->cylinderindex == cylinderindex;
1225                 cylinderindex = entry->cylinderindex;
1226
1227                 /* track the segments per cylinder and their pressure/time integral */
1228                 if (!entry->same_cylinder) {
1229                         current->end = SENSOR_PRESSURE(entry-1);
1230                         current->t_end = (entry-1)->sec;
1231                         current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1232                         track_pr[cylinderindex] = list_add(track_pr[cylinderindex], current);
1233                 } else { /* same cylinder */
1234                         if ((!SENSOR_PRESSURE(entry) && SENSOR_PRESSURE(entry-1)) ||
1235                                 (SENSOR_PRESSURE(entry) && !SENSOR_PRESSURE(entry-1))) {
1236                                 /* transmitter changed its working status */
1237                                 current->end = SENSOR_PRESSURE(entry-1);
1238                                 current->t_end = (entry-1)->sec;
1239                                 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1240                                 track_pr[cylinderindex] =
1241                                         list_add(track_pr[cylinderindex], current);
1242                         }
1243                 }
1244                 /* finally, do the discrete integration to get the SAC rate equivalent */
1245                 current->pressure_time += (entry->sec - (entry-1)->sec) *
1246                         (1 + (entry->depth + (entry-1)->depth) / 20000.0);
1247                 missing_pr |= !SENSOR_PRESSURE(entry);
1248         }
1249
1250         if (entry)
1251                 current->t_end = entry->sec;
1252
1253         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) { /* initialize the end pressures */
1254                 int pr = dive->cylinder[cyl].end.mbar;
1255                 if (pr && track_pr[cyl]) {
1256                         pr_track = list_last(track_pr[cyl]);
1257                         pr_track->end = pr;
1258                 }
1259         }
1260         /* Fill in the last two entries with empty values but valid times
1261          * without creating a false cylinder change event */
1262         i = nr + 2;
1263         pi->entry[i].sec = sec + 20;
1264         pi->entry[i].same_cylinder = 1;
1265         pi->entry[i].cylinderindex = pi->entry[i-1].cylinderindex;
1266         INTERPOLATED_PRESSURE(pi->entry + i) = GET_PRESSURE(pi->entry + i - 1);
1267         pi->entry[i+1].sec = sec + 40;
1268         pi->entry[i+1].same_cylinder = 1;
1269         pi->entry[i+1].cylinderindex = pi->entry[i-1].cylinderindex;
1270         INTERPOLATED_PRESSURE(pi->entry + i + 1) = GET_PRESSURE(pi->entry + i - 1);
1271         /* the number of actual entries - some computers have lots of
1272          * depth 0 samples at the end of a dive, we want to make sure
1273          * we have exactly one of them at the end */
1274         pi->nr = lastindex+1;
1275         while (pi->nr <= i+2 && pi->entry[pi->nr-1].depth > 0)
1276                 pi->nr++;
1277         pi->maxtime = pi->entry[lastindex].sec;
1278
1279         /* Analyze_plot_info() will do the sample max pressures,
1280          * this handles the manual pressures
1281          */
1282         pi->maxpressure = 0;
1283         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
1284                 unsigned int mbar = dive->cylinder[cyl].start.mbar;
1285                 if (mbar > pi->maxpressure)
1286                         pi->maxpressure = mbar;
1287         }
1288
1289         pi->meandepth = dive->meandepth.mm;
1290
1291         if (missing_pr) {
1292                 fill_missing_tank_pressures(dive, pi, track_pr);
1293         }
1294         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++)
1295                 list_free(track_pr[cyl]);
1296         if (0) /* awesome for debugging - not useful otherwise */
1297                 dump_pi(pi);
1298         return analyze_plot_info(pi);
1299 }
1300
1301 void plot(struct graphics_context *gc, cairo_rectangle_int_t *drawing_area, struct dive *dive)
1302 {
1303         struct plot_info *pi;
1304         static struct sample fake[4];
1305         struct sample *sample = dive->sample;
1306         int nr = dive->samples;
1307
1308         if (!nr) {
1309                 int duration = dive->duration.seconds;
1310                 int maxdepth = dive->maxdepth.mm;
1311                 sample = fake;
1312                 fake[1].time.seconds = duration * 0.05;
1313                 fake[1].depth.mm = maxdepth;
1314                 fake[2].time.seconds = duration * 0.95;
1315                 fake[2].depth.mm = maxdepth;
1316                 fake[3].time.seconds = duration * 1.00;
1317                 nr = 4;
1318         }
1319
1320         pi = create_plot_info(dive, nr, sample);
1321
1322         cairo_translate(gc->cr, drawing_area->x, drawing_area->y);
1323         cairo_set_line_width(gc->cr, 2);
1324         cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
1325         cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
1326
1327         /*
1328          * We can use "cairo_translate()" because that doesn't
1329          * scale line width etc. But the actual scaling we need
1330          * do set up ourselves..
1331          *
1332          * Snif. What a pity.
1333          */
1334         gc->maxx = (drawing_area->width - 2*drawing_area->x);
1335         gc->maxy = (drawing_area->height - 2*drawing_area->y);
1336
1337         /* Temperature profile */
1338         plot_temperature_profile(gc, pi);
1339
1340         /* Depth profile */
1341         plot_depth_profile(gc, pi);
1342         plot_events(gc, pi, dive);
1343
1344         /* Cylinder pressure plot */
1345         plot_cylinder_pressure(gc, pi, dive);
1346
1347         /* Text on top of all graphs.. */
1348         plot_temperature_text(gc, pi);
1349         plot_depth_text(gc, pi);
1350         plot_cylinder_pressure_text(gc, pi);
1351
1352         /* Bounding box last */
1353         gc->leftx = 0; gc->rightx = 1.0;
1354         gc->topy = 0; gc->bottomy = 1.0;
1355
1356         set_source_rgb(gc, 1, 1, 1);
1357         move_to(gc, 0, 0);
1358         line_to(gc, 0, 1);
1359         line_to(gc, 1, 1);
1360         line_to(gc, 1, 0);
1361         cairo_close_path(gc->cr);
1362         cairo_stroke(gc->cr);
1363
1364         free(pi);
1365 }