]> git.tdb.fi Git - ext/subsurface.git/blob - dive.h
65b812b06aaa615c02fa26e97c76317fde152cfe
[ext/subsurface.git] / dive.h
1 #ifndef DIVE_H
2 #define DIVE_H
3
4 #include <stdlib.h>
5 #include <time.h>
6
7 #include <glib.h>
8 #include <libxml/tree.h>
9
10 /*
11  * Some silly typedefs to make our units very explicit.
12  *
13  * Also, the units are chosen so that values can be expressible as
14  * integers, so that we never have FP rounding issues. And they
15  * are small enough that converting to/from imperial units doesn't
16  * really matter.
17  *
18  * We also strive to make '0' a meaningless number saying "not
19  * initialized", since many values are things that may not have
20  * been reported (eg cylinder pressure or temperature from dive
21  * computers that don't support them). But sometimes -1 is an even
22  * more explicit way of saying "not there".
23  *
24  * Thus "millibar" for pressure, for example, or "millikelvin" for
25  * temperatures. Doing temperatures in celsius or fahrenheit would
26  * make for loss of precision when converting from one to the other,
27  * and using millikelvin is SI-like but also means that a temperature
28  * of '0' is clearly just a missing temperature or cylinder pressure.
29  *
30  * Also strive to use units that can not possibly be mistaken for a
31  * valid value in a "normal" system without conversion. If the max
32  * depth of a dive is '20000', you probably didn't convert from mm on
33  * output, or if the max depth gets reported as "0.2ft" it was either
34  * a really boring dive, or there was some missing input conversion,
35  * and a 60-ft dive got recorded as 60mm.
36  *
37  * Doing these as "structs containing value" means that we always
38  * have to explicitly write out those units in order to get at the
39  * actual value. So there is hopefully little fear of using a value
40  * in millikelvin as Fahrenheit by mistake.
41  *
42  * We don't actually use these all yet, so maybe they'll change, but
43  * I made a number of types as guidelines.
44  */
45 typedef struct {
46         int seconds;
47 } duration_t;
48
49 typedef struct {
50         int mm;
51 } depth_t;
52
53 typedef struct {
54         int mbar;
55 } pressure_t;
56
57 typedef struct {
58         int mkelvin;
59 } temperature_t;
60
61 typedef struct {
62         int mliter;
63 } volume_t;
64
65 typedef struct {
66         int permille;
67 } fraction_t;
68
69 typedef struct {
70         int grams;
71 } weight_t;
72
73 struct gasmix {
74         fraction_t o2;
75         fraction_t he;
76 };
77
78 typedef struct {
79         volume_t size;
80         pressure_t workingpressure;
81         const char *description;        /* "LP85", "AL72", "AL80", "HP100+" or whatever */
82 } cylinder_type_t;
83
84 typedef struct {
85         cylinder_type_t type;
86         struct gasmix gasmix;
87         pressure_t start, end, sample_start, sample_end;
88 } cylinder_t;
89
90 typedef struct {
91         weight_t weight;
92         const char *description;        /* "integrated", "belt", "ankle" */
93 } weightsystem_t;
94
95 extern gboolean cylinder_none(void *_data);
96 extern gboolean no_cylinders(cylinder_t *cyl);
97 extern gboolean cylinders_equal(cylinder_t *cyl1, cylinder_t *cyl2);
98 extern gboolean no_weightsystems(weightsystem_t *ws);
99 extern gboolean weightsystems_equal(weightsystem_t *ws1, weightsystem_t *ws2);
100
101 extern int get_pressure_units(unsigned int mb, const char **units);
102 extern double get_depth_units(unsigned int mm, int *frac, const char **units);
103 extern double get_volume_units(unsigned int ml, int *frac, const char **units);
104 extern double get_temp_units(unsigned int mk, const char **units);
105 extern double get_weight_units(unsigned int grams, int *frac, const char **units);
106
107 static inline double grams_to_lbs(int grams)
108 {
109         return grams / 453.6;
110 }
111
112 static inline int lbs_to_grams(double lbs)
113 {
114         return lbs * 453.6 + 0.5;
115 }
116
117 static inline double ml_to_cuft(int ml)
118 {
119         return ml / 28316.8466;
120 }
121
122 static inline double cuft_to_l(double cuft)
123 {
124         return cuft * 28.3168466;
125 }
126
127 static inline double mm_to_feet(int mm)
128 {
129         return mm * 0.00328084;
130 }
131
132 static inline unsigned long feet_to_mm(double feet)
133 {
134         return feet * 304.8 + 0.5;
135 }
136
137 static inline int to_feet(depth_t depth)
138 {
139         return mm_to_feet(depth.mm) + 0.5;
140 }
141
142 static inline double mkelvin_to_C(int mkelvin)
143 {
144         return (mkelvin - 273150) / 1000.0;
145 }
146
147 static inline double mkelvin_to_F(int mkelvin)
148 {
149         return mkelvin * 9 / 5000.0 - 459.670;
150 }
151
152 static inline unsigned long F_to_mkelvin(double f)
153 {
154         return (f-32) * 1000 / 1.8 + 273150.5;
155 }
156
157 static inline int to_C(temperature_t temp)
158 {
159         if (!temp.mkelvin)
160                 return 0;
161         return mkelvin_to_C(temp.mkelvin) + 0.5;
162 }
163
164 static inline int to_F(temperature_t temp)
165 {
166         if (!temp.mkelvin)
167                 return 0;
168         return mkelvin_to_F(temp.mkelvin) + 0.5;
169 }
170
171 static inline int to_K(temperature_t temp)
172 {
173         if (!temp.mkelvin)
174                 return 0;
175         return (temp.mkelvin + 499)/1000;
176 }
177
178 static inline double psi_to_bar(double psi)
179 {
180         return psi / 14.5037738;
181 }
182
183 static inline unsigned long psi_to_mbar(double psi)
184 {
185         return psi_to_bar(psi)*1000 + 0.5;
186 }
187
188 static inline int to_PSI(pressure_t pressure)
189 {
190         return pressure.mbar * 0.0145037738 + 0.5;
191 }
192
193 static inline double bar_to_atm(double bar)
194 {
195         return bar / 1.01325;
196 }
197
198 static inline double to_ATM(pressure_t pressure)
199 {
200         return pressure.mbar / 1013.25;
201 }
202
203 static inline int mbar_to_PSI(int mbar)
204 {
205         pressure_t p = {mbar};
206         return to_PSI(p);
207 }
208
209 struct sample {
210         duration_t time;
211         depth_t depth;
212         temperature_t temperature;
213         pressure_t cylinderpressure;
214         int cylinderindex;
215 };
216
217 /*
218  * Events are currently pretty meaningless. This is
219  * just based on the random data that libdivecomputer
220  * gives us. I'm not sure what a real "architected"
221  * event model would actually look like, but right
222  * now you can associate a list of events with a dive,
223  * and we'll do something about it.
224  */
225 struct event {
226         struct event *next;
227         duration_t time;
228         int type, flags, value;
229         char name[];
230 };
231
232 #define MAX_CYLINDERS (8)
233 #define MAX_WEIGHTSYSTEMS (4)
234 #define W_IDX_PRIMARY 0
235 #define W_IDX_SECONDARY 1
236
237 typedef enum { TF_NONE, NO_TRIP, IN_TRIP, NUM_TRIPFLAGS } tripflag_t;
238 extern const char *tripflag_names[NUM_TRIPFLAGS];
239
240 struct dive {
241         int number;
242         tripflag_t tripflag;
243         int selected;
244         time_t when;
245         char *location;
246         char *notes;
247         char *divemaster, *buddy;
248         int rating;
249         double latitude, longitude;
250         depth_t maxdepth, meandepth;
251         duration_t duration, surfacetime;
252         depth_t visibility;
253         temperature_t airtemp, watertemp;
254         cylinder_t cylinder[MAX_CYLINDERS];
255         weightsystem_t weightsystem[MAX_WEIGHTSYSTEMS];
256         char *suit;
257         int sac, otu;
258         struct event *events;
259         int samples, alloc_samples;
260         struct sample sample[];
261 };
262
263 extern GList *dive_trip_list;
264 extern gboolean autogroup;
265 /* random threashold: three days without diving -> new trip
266  * this works very well for people who usually dive as part of a trip and don't
267  * regularly dive at a local facility; this is why trips are an optional feature */
268 #define TRIP_THRESHOLD 3600*24*3
269
270 #define UNGROUPED_DIVE(_dive) ((_dive)->tripflag == NO_TRIP)
271 #define DIVE_IN_TRIP(_dive) ((_dive)->tripflag == IN_TRIP)
272 #define NEXT_TRIP(_entry, _list) ((_entry) ? g_list_next(_entry) : (_list))
273 #define PREV_TRIP(_entry, _list) ((_entry) ? g_list_previous(_entry) : g_list_last(_list))
274 #define DIVE_TRIP(_trip) ((struct dive *)(_trip)->data)
275 #define DIVE_FITS_TRIP(_dive, _dive_trip) ((_dive_trip)->when - TRIP_THRESHOLD <= (_dive)->when)
276
277 static inline int dive_date_cmp(gconstpointer _a, gconstpointer _b) {
278         return ((struct dive *)(_a))->when - ((struct dive *)(_b))->when;
279 }
280
281 #define INSERT_TRIP(_trip, _list) g_list_insert_sorted((_list), (_trip), dive_date_cmp)
282 #define FIND_TRIP(_trip, _list) g_list_find_custom((_list), (_trip), dive_date_cmp)
283
284 /*
285  * We keep our internal data in well-specified units, but
286  * the input and output may come in some random format. This
287  * keeps track of those units.
288  */
289 /* turns out in Win32 PASCAL is defined as a calling convention */
290 #ifdef WIN32
291 #undef PASCAL
292 #endif
293 struct units {
294         enum { METERS, FEET } length;
295         enum { LITER, CUFT } volume;
296         enum { BAR, PSI, PASCAL } pressure;
297         enum { CELSIUS, FAHRENHEIT, KELVIN } temperature;
298         enum { KG, LBS } weight;
299 };
300
301 extern const struct units SI_units, IMPERIAL_units;
302 extern struct units input_units, output_units;
303
304 extern int verbose;
305
306 struct dive_table {
307         int nr, allocated, preexisting;
308         struct dive **dives;
309 };
310
311 extern struct dive_table dive_table;
312
313 extern int selected_dive;
314 #define current_dive (get_dive(selected_dive))
315
316 static inline struct dive *get_dive(unsigned int nr)
317 {
318         if (nr >= dive_table.nr || nr < 0)
319                 return NULL;
320         return dive_table.dives[nr];
321 }
322
323 extern void parse_xml_init(void);
324 extern void parse_xml_buffer(const char *url, const char *buf, int size, GError **error);
325 extern void set_filename(const char *filename);
326
327 extern void parse_file(const char *filename, GError **error);
328
329 #ifdef XSLT
330 extern xmlDoc *test_xslt_transforms(xmlDoc *doc);
331 #endif
332
333 extern void show_dive_info(struct dive *);
334
335 extern void show_dive_equipment(struct dive *, int w_idx);
336
337 extern void show_dive_stats(struct dive *);
338
339 extern void update_dive(struct dive *new_dive);
340 extern void save_dives(const char *filename);
341
342 static inline unsigned int dive_size(int samples)
343 {
344         return sizeof(struct dive) + samples*sizeof(struct sample);
345 }
346
347 extern time_t utc_mktime(struct tm *tm);
348
349 extern struct dive *alloc_dive(void);
350 extern void record_dive(struct dive *dive);
351 extern void delete_dive(struct dive *dive);
352
353 extern struct sample *prepare_sample(struct dive **divep);
354 extern void finish_sample(struct dive *dive);
355
356 extern void report_dives(gboolean imported);
357 extern struct dive *fixup_dive(struct dive *dive);
358 extern struct dive *try_to_merge(struct dive *a, struct dive *b);
359
360 extern void renumber_dives(int nr);
361
362 extern void add_event(struct dive *dive, int time, int type, int flags, int value, const char *name);
363
364 /* UI related protopypes */
365
366 extern void init_ui(int *argcp, char ***argvp);
367
368 extern void run_ui(void);
369 extern void exit_ui(void);
370
371 extern void report_error(GError* error);
372
373 extern void add_cylinder_description(cylinder_type_t *);
374 extern void add_weightsystem_description(weightsystem_t *);
375 extern void add_people(const char *string);
376 extern void add_location(const char *string);
377 extern void add_suit(const char *string);
378 extern void remember_event(const char *eventname);
379 extern void evn_foreach(void (*callback)(const char *, int *, void *), void *data);
380
381 extern int add_new_dive(struct dive *dive);
382 extern int edit_dive_info(struct dive *dive);
383 extern int edit_multi_dive_info(int idx);
384 extern void dive_list_update_dives(void);
385 extern void flush_divelist(struct dive *dive);
386
387 #define DIVE_ERROR_PARSE 1
388
389 const char *weekday(int wday);
390 const char *monthname(int mon);
391
392 #define UTF8_DEGREE "\xc2\xb0"
393 #define UTF8_SUBSCRIPT_2 "\xe2\x82\x82"
394 #define UTF8_WHITESTAR "\xe2\x98\x86"
395 #define UTF8_BLACKSTAR "\xe2\x98\x85"
396 #define ZERO_STARS      UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR
397 #define ONE_STARS       UTF8_BLACKSTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR
398 #define TWO_STARS       UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR
399 #define THREE_STARS     UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_WHITESTAR UTF8_WHITESTAR
400 #define FOUR_STARS      UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_WHITESTAR
401 #define FIVE_STARS      UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR
402 extern const char *star_strings[];
403
404 #define AIR_PERMILLE 209
405
406 #endif /* DIVE_H */