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