]> git.tdb.fi Git - ext/subsurface.git/blob - libdivecomputer.c
Start some very initial libdivecomputer integration
[ext/subsurface.git] / libdivecomputer.c
1 #include <stdio.h>
2 #include <gtk/gtk.h>
3
4 #include "display.h"
5
6 /* libdivecomputer */
7 #include <device.h>
8 #include <suunto.h>
9 #include <reefnet.h>
10 #include <uwatec.h>
11 #include <oceanic.h>
12 #include <mares.h>
13 #include <hw.h>
14 #include <cressi.h>
15 #include <zeagle.h>
16 #include <atomics.h>
17 #include <utils.h>
18
19 static device_status_t device_open(const char *devname,
20         device_type_t type,
21         device_t **device)
22 {
23         switch (type) {
24         case DEVICE_TYPE_SUUNTO_SOLUTION:
25                 return suunto_solution_device_open(device, devname);
26
27         case DEVICE_TYPE_SUUNTO_EON:
28                 return suunto_eon_device_open(device, devname);
29
30         case DEVICE_TYPE_SUUNTO_VYPER:
31                 return suunto_vyper_device_open(device, devname);
32
33         case DEVICE_TYPE_SUUNTO_VYPER2:
34                 return suunto_vyper2_device_open(device, devname);
35
36         case DEVICE_TYPE_SUUNTO_D9:
37                 return suunto_d9_device_open(device, devname);
38
39         case DEVICE_TYPE_UWATEC_ALADIN:
40                 return uwatec_aladin_device_open(device, devname);
41
42         case DEVICE_TYPE_UWATEC_MEMOMOUSE:
43                 return uwatec_memomouse_device_open(device, devname);
44
45         case DEVICE_TYPE_UWATEC_SMART:
46                 return uwatec_smart_device_open(device);
47
48         case DEVICE_TYPE_REEFNET_SENSUS:
49                 return reefnet_sensus_device_open(device, devname);
50
51         case DEVICE_TYPE_REEFNET_SENSUSPRO:
52                 return reefnet_sensuspro_device_open(device, devname);
53
54         case DEVICE_TYPE_REEFNET_SENSUSULTRA:
55                 return reefnet_sensusultra_device_open(device, devname);
56
57         case DEVICE_TYPE_OCEANIC_VTPRO:
58                 return oceanic_vtpro_device_open(device, devname);
59
60         case DEVICE_TYPE_OCEANIC_VEO250:
61                 return oceanic_veo250_device_open(device, devname);
62
63         case DEVICE_TYPE_OCEANIC_ATOM2:
64                 return oceanic_atom2_device_open(device, devname);
65
66         case DEVICE_TYPE_MARES_NEMO:
67                 return mares_nemo_device_open(device, devname);
68
69         case DEVICE_TYPE_MARES_PUCK:
70                 return mares_puck_device_open(device, devname);
71
72         case DEVICE_TYPE_MARES_ICONHD:
73                 return mares_iconhd_device_open(device, devname);
74
75         case DEVICE_TYPE_HW_OSTC:
76                 return hw_ostc_device_open(device, devname);
77
78         case DEVICE_TYPE_CRESSI_EDY:
79                 return cressi_edy_device_open(device, devname);
80
81         case DEVICE_TYPE_ZEAGLE_N2ITION3:
82                 return zeagle_n2ition3_device_open(device, devname);
83
84         case DEVICE_TYPE_ATOMICS_COBALT:
85                 return atomics_cobalt_device_open(device);
86
87         default:
88                 return DEVICE_STATUS_ERROR;
89         }
90 }
91
92 static void do_import(const char *computer, device_type_t type)
93 {
94         /* FIXME! Needs user input! */
95         const char *devname = "/dev/ttyUSB0";
96         device_t *device = NULL;
97         device_status_t rc;
98
99         rc = device_open(devname, type, &device);
100         printf("rc=%d\n", rc);
101         if (rc != DEVICE_STATUS_SUCCESS)
102                 return;
103 }
104
105 /*
106  * Taken from 'example.c' in libdivecomputer.
107  *
108  * I really wish there was some way to just have
109  * libdivecomputer tell us what devices it supports,
110  * rather than have the application have to know..
111  */
112 struct device_list {
113         const char *name;
114         device_type_t type;
115 } device_list[] = {
116         { "Suunto Solution",    DEVICE_TYPE_SUUNTO_SOLUTION },
117         { "Suunto Eon",         DEVICE_TYPE_SUUNTO_EON },
118         { "Suunto Vyper",       DEVICE_TYPE_SUUNTO_VYPER },
119         { "Suunto Vyper Air",   DEVICE_TYPE_SUUNTO_VYPER2 },
120         { "Suunto D9",          DEVICE_TYPE_SUUNTO_D9 },
121         { "Uwatec Aladin",      DEVICE_TYPE_UWATEC_ALADIN },
122         { "Uwatec Memo Mouse",  DEVICE_TYPE_UWATEC_MEMOMOUSE },
123         { "Uwatec Smart",       DEVICE_TYPE_UWATEC_SMART },
124         { "ReefNet Sensus",     DEVICE_TYPE_REEFNET_SENSUS },
125         { "ReefNet Sensus Pro", DEVICE_TYPE_REEFNET_SENSUSPRO },
126         { "ReefNet Sensus Ultra",DEVICE_TYPE_REEFNET_SENSUSULTRA },
127         { "Oceanic VT Pro",     DEVICE_TYPE_OCEANIC_VTPRO },
128         { "Oceanic Veo250",     DEVICE_TYPE_OCEANIC_VEO250 },
129         { "Oceanic Atom 2",     DEVICE_TYPE_OCEANIC_ATOM2 },
130         { "Mares Nemo",         DEVICE_TYPE_MARES_NEMO },
131         { "Mares Puck",         DEVICE_TYPE_MARES_PUCK },
132         { "Mares Icon HD",      DEVICE_TYPE_MARES_ICONHD },
133         { "OSTC",               DEVICE_TYPE_HW_OSTC },
134         { "Cressi Edy",         DEVICE_TYPE_CRESSI_EDY },
135         { "Zeagle N2iTiON 3",   DEVICE_TYPE_ZEAGLE_N2ITION3 },
136         { "Atomics Cobalt",     DEVICE_TYPE_ATOMICS_COBALT },
137         { NULL }
138 };
139
140 static void fill_computer_list(GtkListStore *store)
141 {
142         GtkTreeIter iter;
143         struct device_list *list = device_list;
144
145         for (list = device_list ; list->name ; list++) {
146                 gtk_list_store_append(store, &iter);
147                 gtk_list_store_set(store, &iter,
148                         0, list->name,
149                         1, list->type,
150                         -1);
151         }
152 }
153
154 static GtkComboBox *dive_computer_selector(GtkWidget *dialog)
155 {
156         GtkWidget *hbox, *combo_box;
157         GtkListStore *model;
158         GtkCellRenderer *renderer;
159
160         hbox = gtk_hbox_new(FALSE, 6);
161         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), hbox);
162
163         model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
164         fill_computer_list(model);
165
166         combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
167         gtk_box_pack_start(GTK_BOX(hbox), combo_box, FALSE, TRUE, 3);
168
169         renderer = gtk_cell_renderer_text_new();
170         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
171         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
172
173         return GTK_COMBO_BOX(combo_box);
174 }
175
176 void import_dialog(GtkWidget *w, gpointer data)
177 {
178         int result;
179         GtkWidget *dialog;
180         GtkComboBox *computer;
181
182         dialog = gtk_dialog_new_with_buttons("Import from dive computer",
183                 GTK_WINDOW(main_window),
184                 GTK_DIALOG_DESTROY_WITH_PARENT,
185                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
186                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
187                 NULL);
188
189         computer = dive_computer_selector(dialog);
190
191         gtk_widget_show_all(dialog);
192         result = gtk_dialog_run(GTK_DIALOG(dialog));
193         switch (result) {
194                 int type;
195                 GtkTreeIter iter;
196                 GtkTreeModel *model;
197                 const char *comp;
198         case GTK_RESPONSE_ACCEPT:
199                 if (!gtk_combo_box_get_active_iter(computer, &iter))
200                         break;
201                 model = gtk_combo_box_get_model(computer);
202                 gtk_tree_model_get(model, &iter,
203                         0, &comp,
204                         1, &type,
205                         -1);
206                 do_import(comp, type);
207                 break;
208         default:
209                 break;
210         }
211         gtk_widget_destroy(dialog);
212 }
213