From 0282d515db875cee0139e121db13084be0caac82 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Sun, 11 Sep 2011 13:16:23 -0700 Subject: [PATCH 01/16] Work around more Diving Log bugs.. The Diving Log temperature reading is in Fahrenheit for the samples (for the per-dive water/air temperature it's in Celsius). But it seems to have a bug where a lack of a sample has been turned into 32 Fahrenheit (which is 0 celsius). This is despite the dive itself having a water temperature of 8 degF. Just throw away those bogus freezing temperatures. Sure, they can happen, and ice divers are crazy - but in this case I know it's just an error in the log, and it looks very much like a Diving Log bug. Signed-off-by: Linus Torvalds --- parse-xml.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/parse-xml.c b/parse-xml.c index 8e64b1e..e14d9db 100644 --- a/parse-xml.c +++ b/parse-xml.c @@ -488,6 +488,16 @@ static int uemis_fill_sample(struct sample *sample, const char *name, int len, c * Divinglog is crazy. The temperatures are in celsius. EXCEPT * for the sample temperatures, that are in Fahrenheit. * WTF? + * + * Oh, and I think Diving Log *internally* probably kept them + * in celsius, because I'm seeing entries like + * + * 32.0 + * + * in there. Which is freezing, aka 0 degC. I bet the "0" is + * what Diving Log uses for "no temperature". + * + * So throw away crap like that. */ static void fahrenheit(char *buffer, void *_temperature) { @@ -496,6 +506,9 @@ static void fahrenheit(char *buffer, void *_temperature) switch (integer_or_float(buffer, &val)) { case FLOAT: + /* Floating point equality is evil, but works for small integers */ + if (val.fp == 32.0) + break; temperature->mkelvin = (val.fp + 459.67) * 5000/9; break; default: -- 2.43.0 From add58148bc30060e99e9c896fa4d390460c55694 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Sun, 11 Sep 2011 13:43:37 -0700 Subject: [PATCH 02/16] Oops. Fix 'buffered dive' information When fixing the unit changes, I broke the dive buffering logic entirely for switching between dives. Duh. Signed-off-by: Linus Torvalds --- main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.c b/main.c index 138de29..360af84 100644 --- a/main.c +++ b/main.c @@ -98,12 +98,12 @@ void update_dive(struct dive *new_dive) if (old_dive) { flush_dive_info_changes(old_dive); flush_dive_equipment_changes(old_dive); - buffered_dive = new_dive; } if (new_dive) { show_dive_info(new_dive); show_dive_equipment(new_dive); } + buffered_dive = new_dive; } void repaint_dive(void) -- 2.43.0 From c96e2d1767f5c6379b2d7a780ccabc6039cf60bf Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Sun, 11 Sep 2011 15:37:56 -0700 Subject: [PATCH 03/16] Pack all the equipment widgets into boxes We really do want to "pack" them, rather than use up the whole size. I think. I may end up playing around more with this. Signed-off-by: Linus Torvalds --- equipment.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/equipment.c b/equipment.c index bab0d53..2c25e03 100644 --- a/equipment.c +++ b/equipment.c @@ -136,13 +136,16 @@ void show_dive_equipment(struct dive *dive) static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr) { - GtkWidget *frame, *button; + GtkWidget *frame, *hbox, *button; frame = gtk_frame_new(name); - gtk_container_add(GTK_CONTAINER(vbox), frame); + gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, TRUE, 0); + + hbox = gtk_hbox_new(FALSE, 3); + gtk_container_add(GTK_CONTAINER(frame), hbox); button = gtk_spin_button_new_with_range(min, max, incr); - gtk_container_add(GTK_CONTAINER(frame), button); + gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0); gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID); @@ -273,15 +276,18 @@ static void cylinder_widget(GtkWidget *box, int nr, GtkListStore *model) GtkWidget *widget; char buffer[80]; + hbox = gtk_hbox_new(FALSE, 3); + gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, FALSE, 0); + snprintf(buffer, sizeof(buffer), "Cylinder %d", nr); frame = gtk_frame_new(buffer); - gtk_box_pack_start(GTK_BOX(box), frame, FALSE, FALSE, 0); + gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0); - hbox = gtk_hbox_new(TRUE, 3); + hbox = gtk_hbox_new(FALSE, 3); gtk_container_add(GTK_CONTAINER(frame), hbox); frame = gtk_frame_new("Description"); - gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, FALSE, 0); + gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0); widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0); gtk_container_add(GTK_CONTAINER(frame), widget); @@ -292,7 +298,7 @@ static void cylinder_widget(GtkWidget *box, int nr, GtkListStore *model) widget = create_spinbutton(hbox, "Size", 0, 200, 0.1); cylinder_size = GTK_SPIN_BUTTON(widget); - widget = create_spinbutton(hbox, "Working Pressure", 0, 5000, 1); + widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1); cylinder_pressure = GTK_SPIN_BUTTON(widget); widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1); -- 2.43.0 From 9d960c1845ef069a76e58520dfe6f9042b4eb42e Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Sun, 11 Sep 2011 15:39:46 -0700 Subject: [PATCH 04/16] Add a 'Renumber' menu choice If you want to re-number your dives - either because they didn't have any numbering at all, or because you forgot about other dives - you now can. Signed-off-by: Linus Torvalds --- main.c | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 360af84..367d3ad 100644 --- a/main.c +++ b/main.c @@ -325,12 +325,51 @@ static void unit_dialog(GtkWidget *w, gpointer data) gtk_widget_destroy(dialog); } +static void renumber_dives(int nr) +{ + int i; + + for (i = 0; i < dive_table.nr; i++) { + struct dive *dive = dive_table.dives[i]; + dive->number = nr + i; + } +} + +static void renumber_dialog(GtkWidget *w, gpointer data) +{ + int result; + GtkWidget *dialog, *frame, *button; + + dialog = gtk_dialog_new_with_buttons("Renumber", + GTK_WINDOW(main_window), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, + GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, + NULL); + + frame = gtk_frame_new("New starting number"); + gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), frame); + + button = gtk_spin_button_new_with_range(1, 50000, 1); + gtk_container_add(GTK_CONTAINER(frame), button); + + gtk_widget_show_all(dialog); + result = gtk_dialog_run(GTK_DIALOG(dialog)); + if (result == GTK_RESPONSE_ACCEPT) { + int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button)); + renumber_dives(nr); + repaint_dive(); + } + gtk_widget_destroy(dialog); +} + static GtkActionEntry menu_items[] = { { "FileMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL}, { "OpenFile", GTK_STOCK_OPEN, NULL, "O", NULL, G_CALLBACK(file_open) }, { "SaveFile", GTK_STOCK_SAVE, NULL, "S", NULL, G_CALLBACK(file_save) }, + { "Units", NULL, "Units", NULL, NULL, G_CALLBACK(unit_dialog) }, + { "Renumber", NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) }, { "Quit", GTK_STOCK_QUIT, NULL, "Q", NULL, G_CALLBACK(quit) }, - { "Units", NULL, "Units", NULL, NULL, G_CALLBACK(unit_dialog) }, }; static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]); @@ -342,6 +381,7 @@ static const gchar* ui_string = " \ \ \ \ + \ \ \ \ -- 2.43.0 From 15474135b1629c5615c90898df266079fb637354 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Sun, 11 Sep 2011 16:21:21 -0700 Subject: [PATCH 05/16] Accept a smaller profile window I'm trying to make sure that we can shrink the main window and still get a useful experience. Sometimes you have small bad netbooks when diving.. Signed-off-by: Linus Torvalds --- profile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/profile.c b/profile.c index 270a041..1912690 100644 --- a/profile.c +++ b/profile.c @@ -627,7 +627,7 @@ GtkWidget *dive_profile_widget(void) GtkWidget *da; da = gtk_drawing_area_new(); - gtk_widget_set_size_request(da, 450, 350); + gtk_widget_set_size_request(da, 350, 250); g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL); return da; -- 2.43.0 From 04dc4cdf9d0feab5cd3e8c3aa0ea08addfb3323d Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Sun, 11 Sep 2011 16:34:01 -0700 Subject: [PATCH 06/16] Clean up dive info box too Make it denser by putting the dive number/location in the frame label, and make it size up and down more naturally. Signed-off-by: Linus Torvalds --- info.c | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/info.c b/info.c index b65a820..2cc98f4 100644 --- a/info.c +++ b/info.c @@ -7,7 +7,8 @@ #include "display.h" #include "divelist.h" -static GtkWidget *divedate, *divetime, *depth, *duration, *temperature, *locationnote; +static GtkWidget *info_frame; +static GtkWidget *divedate, *divetime, *depth, *duration, *temperature; static GtkEntry *location; static GtkTextBuffer *notes; static int location_changed = 1, notes_changed = 1; @@ -117,11 +118,14 @@ void show_dive_info(struct dive *dive) text = dive->location ? : ""; gtk_entry_set_text(location, text); + text = "Dive Info"; + if (dive->location && *dive->location) + text = dive->location; len = 0; if (dive->number) len = snprintf(buffer, sizeof(buffer), "%d. ", dive->number); snprintf(buffer+len, sizeof(buffer)-len, "%s", text); - gtk_label_set_text(GTK_LABEL(locationnote), buffer); + gtk_frame_set_label(GTK_FRAME(info_frame), buffer); text = dive->notes ? : ""; gtk_text_buffer_set_text(notes, text, -1); @@ -138,23 +142,20 @@ static GtkWidget *info_label(GtkWidget *box, const char *str, GtkJustification j GtkWidget *dive_info_frame(void) { GtkWidget *frame; - GtkWidget *hbox, *hbox2; + GtkWidget *hbox; GtkWidget *vbox; frame = gtk_frame_new("Dive info"); + info_frame = frame; gtk_widget_show(frame); - vbox = gtk_vbox_new(TRUE, 6); + vbox = gtk_vbox_new(FALSE, 6); gtk_container_set_border_width(GTK_CONTAINER(vbox), 3); gtk_container_add(GTK_CONTAINER(frame), vbox); - hbox = gtk_hbox_new(TRUE, 6); + hbox = gtk_hbox_new(FALSE, 16); gtk_container_set_border_width(GTK_CONTAINER(hbox), 3); - gtk_container_add(GTK_CONTAINER(vbox), hbox); - - hbox2 = gtk_hbox_new(FALSE, 6); - gtk_container_set_border_width(GTK_CONTAINER(hbox2), 3); - gtk_container_add(GTK_CONTAINER(vbox), hbox2); + gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0); divedate = info_label(hbox, "date", GTK_JUSTIFY_RIGHT); divetime = info_label(hbox, "time", GTK_JUSTIFY_RIGHT); @@ -162,59 +163,57 @@ GtkWidget *dive_info_frame(void) duration = info_label(hbox, "duration", GTK_JUSTIFY_RIGHT); temperature = info_label(hbox, "temperature", GTK_JUSTIFY_RIGHT); - locationnote = info_label(hbox2, "location", GTK_JUSTIFY_LEFT); - gtk_label_set_width_chars(GTK_LABEL(locationnote), 80); - return frame; } static GtkEntry *text_entry(GtkWidget *box, const char *label) { GtkWidget *entry; - GtkWidget *frame = gtk_frame_new(label); gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0); - entry = gtk_entry_new (); + entry = gtk_entry_new(); gtk_container_add(GTK_CONTAINER(frame), entry); return GTK_ENTRY(entry); } -static GtkTextBuffer *text_view(GtkWidget *box, const char *label, gboolean expand) +static GtkTextBuffer *text_view(GtkWidget *box, const char *label) { - GtkWidget *view; + GtkWidget *view, *vbox; GtkTextBuffer *buffer; - GtkWidget *frame = gtk_frame_new(label); - gtk_box_pack_start(GTK_BOX(box), frame, expand, expand, 0); + gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0); + box = gtk_hbox_new(FALSE, 3); + gtk_container_add(GTK_CONTAINER(frame), box); + vbox = gtk_vbox_new(FALSE, 3); + gtk_container_add(GTK_CONTAINER(box), vbox); - GtkWidget* scrolled_window = gtk_scrolled_window_new (0, 0); + GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN); gtk_widget_show(scrolled_window); - view = gtk_text_view_new (); + view = gtk_text_view_new(); gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD); gtk_container_add(GTK_CONTAINER(scrolled_window), view); buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)); - gtk_container_add(GTK_CONTAINER(frame), scrolled_window); + gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0); return buffer; } GtkWidget *extended_dive_info_widget(void) { GtkWidget *vbox; - vbox = gtk_vbox_new(FALSE, 6); location = text_entry(vbox, "Location"); gtk_container_set_border_width(GTK_CONTAINER(vbox), 6); - notes = text_view(vbox, "Notes", TRUE); + notes = text_view(vbox, "Notes"); /* Add extended info here: name, description, yadda yadda */ show_dive_info(current_dive); -- 2.43.0 From 9cb60c910681b0fb3c04d22c77bcf9c2754bfa7f Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 12 Sep 2011 09:19:21 -0700 Subject: [PATCH 07/16] Start some very initial libdivecomputer integration Ok, so this is quite broken right now: it doesn't actually really *do* anything, and it now requires that you have libdivecomputer all set up and installed. That is fairly easy: mkdir ../src cd ../src git clone git://libdivecomputer.git.sourceforge.net/gitroot/libdivecomputer/libdivecomputer cd libdivecomputer autoreconf --install ./configure make sudo make install but you may feel that this is not exactly useful considering that nothing actually *works* yet. Some day. Signed-off-by: Linus Torvalds --- Makefile | 15 +++- display.h | 2 + libdivecomputer.c | 213 ++++++++++++++++++++++++++++++++++++++++++++++ main.c | 5 +- 4 files changed, 232 insertions(+), 3 deletions(-) create mode 100644 libdivecomputer.c diff --git a/Makefile b/Makefile index 787a74f..7e04f8f 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,18 @@ CC=gcc CFLAGS=-Wall -Wno-pointer-sign -g -OBJS=main.o dive.o profile.o info.o equipment.o divelist.o parse-xml.o save-xml.o +LIBDIVECOMPUTERDIR = /usr/local +LIBDIVECOMPUTERINCLUDES = $(LIBDIVECOMPUTERDIR)/include/libdivecomputer +LIBDIVECOMPUTERARCHIVE = $(LIBDIVECOMPUTERDIR)/lib/libdivecomputer.a + +OBJS = main.o dive.o profile.o info.o equipment.o divelist.o \ + parse-xml.o save-xml.o libdivecomputer.o divelog: $(OBJS) $(CC) $(LDFLAGS) -o divelog $(OBJS) \ `xml2-config --libs` \ - `pkg-config --libs gtk+-2.0 glib-2.0 gconf-2.0` + `pkg-config --libs gtk+-2.0 glib-2.0 gconf-2.0` \ + $(LIBDIVECOMPUTERARCHIVE) parse-xml.o: parse-xml.c dive.h $(CC) $(CFLAGS) `pkg-config --cflags glib-2.0` -c `xml2-config --cflags` parse-xml.c @@ -32,3 +38,8 @@ equipment.o: equipment.c dive.h display.h divelist.h divelist.o: divelist.c dive.h display.h divelist.h $(CC) $(CFLAGS) `pkg-config --cflags gtk+-2.0 glib-2.0` -c divelist.c + +libdivecomputer.o: libdivecomputer.c dive.h display.h + $(CC) $(CFLAGS) `pkg-config --cflags gtk+-2.0 glib-2.0` \ + -I$(LIBDIVECOMPUTERINCLUDES) \ + -c libdivecomputer.c diff --git a/display.h b/display.h index 5e6a390..4b2e83b 100644 --- a/display.h +++ b/display.h @@ -7,6 +7,8 @@ extern GtkWidget *main_window; +extern void import_dialog(GtkWidget *, gpointer); + extern GtkWidget *dive_profile_widget(void); extern GtkWidget *dive_info_frame(void); extern GtkWidget *extended_dive_info_widget(void); diff --git a/libdivecomputer.c b/libdivecomputer.c new file mode 100644 index 0000000..b9850b3 --- /dev/null +++ b/libdivecomputer.c @@ -0,0 +1,213 @@ +#include +#include + +#include "display.h" + +/* libdivecomputer */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static device_status_t device_open(const char *devname, + device_type_t type, + device_t **device) +{ + switch (type) { + case DEVICE_TYPE_SUUNTO_SOLUTION: + return suunto_solution_device_open(device, devname); + + case DEVICE_TYPE_SUUNTO_EON: + return suunto_eon_device_open(device, devname); + + case DEVICE_TYPE_SUUNTO_VYPER: + return suunto_vyper_device_open(device, devname); + + case DEVICE_TYPE_SUUNTO_VYPER2: + return suunto_vyper2_device_open(device, devname); + + case DEVICE_TYPE_SUUNTO_D9: + return suunto_d9_device_open(device, devname); + + case DEVICE_TYPE_UWATEC_ALADIN: + return uwatec_aladin_device_open(device, devname); + + case DEVICE_TYPE_UWATEC_MEMOMOUSE: + return uwatec_memomouse_device_open(device, devname); + + case DEVICE_TYPE_UWATEC_SMART: + return uwatec_smart_device_open(device); + + case DEVICE_TYPE_REEFNET_SENSUS: + return reefnet_sensus_device_open(device, devname); + + case DEVICE_TYPE_REEFNET_SENSUSPRO: + return reefnet_sensuspro_device_open(device, devname); + + case DEVICE_TYPE_REEFNET_SENSUSULTRA: + return reefnet_sensusultra_device_open(device, devname); + + case DEVICE_TYPE_OCEANIC_VTPRO: + return oceanic_vtpro_device_open(device, devname); + + case DEVICE_TYPE_OCEANIC_VEO250: + return oceanic_veo250_device_open(device, devname); + + case DEVICE_TYPE_OCEANIC_ATOM2: + return oceanic_atom2_device_open(device, devname); + + case DEVICE_TYPE_MARES_NEMO: + return mares_nemo_device_open(device, devname); + + case DEVICE_TYPE_MARES_PUCK: + return mares_puck_device_open(device, devname); + + case DEVICE_TYPE_MARES_ICONHD: + return mares_iconhd_device_open(device, devname); + + case DEVICE_TYPE_HW_OSTC: + return hw_ostc_device_open(device, devname); + + case DEVICE_TYPE_CRESSI_EDY: + return cressi_edy_device_open(device, devname); + + case DEVICE_TYPE_ZEAGLE_N2ITION3: + return zeagle_n2ition3_device_open(device, devname); + + case DEVICE_TYPE_ATOMICS_COBALT: + return atomics_cobalt_device_open(device); + + default: + return DEVICE_STATUS_ERROR; + } +} + +static void do_import(const char *computer, device_type_t type) +{ + /* FIXME! Needs user input! */ + const char *devname = "/dev/ttyUSB0"; + device_t *device = NULL; + device_status_t rc; + + rc = device_open(devname, type, &device); + printf("rc=%d\n", rc); + if (rc != DEVICE_STATUS_SUCCESS) + return; +} + +/* + * Taken from 'example.c' in libdivecomputer. + * + * I really wish there was some way to just have + * libdivecomputer tell us what devices it supports, + * rather than have the application have to know.. + */ +struct device_list { + const char *name; + device_type_t type; +} device_list[] = { + { "Suunto Solution", DEVICE_TYPE_SUUNTO_SOLUTION }, + { "Suunto Eon", DEVICE_TYPE_SUUNTO_EON }, + { "Suunto Vyper", DEVICE_TYPE_SUUNTO_VYPER }, + { "Suunto Vyper Air", DEVICE_TYPE_SUUNTO_VYPER2 }, + { "Suunto D9", DEVICE_TYPE_SUUNTO_D9 }, + { "Uwatec Aladin", DEVICE_TYPE_UWATEC_ALADIN }, + { "Uwatec Memo Mouse", DEVICE_TYPE_UWATEC_MEMOMOUSE }, + { "Uwatec Smart", DEVICE_TYPE_UWATEC_SMART }, + { "ReefNet Sensus", DEVICE_TYPE_REEFNET_SENSUS }, + { "ReefNet Sensus Pro", DEVICE_TYPE_REEFNET_SENSUSPRO }, + { "ReefNet Sensus Ultra",DEVICE_TYPE_REEFNET_SENSUSULTRA }, + { "Oceanic VT Pro", DEVICE_TYPE_OCEANIC_VTPRO }, + { "Oceanic Veo250", DEVICE_TYPE_OCEANIC_VEO250 }, + { "Oceanic Atom 2", DEVICE_TYPE_OCEANIC_ATOM2 }, + { "Mares Nemo", DEVICE_TYPE_MARES_NEMO }, + { "Mares Puck", DEVICE_TYPE_MARES_PUCK }, + { "Mares Icon HD", DEVICE_TYPE_MARES_ICONHD }, + { "OSTC", DEVICE_TYPE_HW_OSTC }, + { "Cressi Edy", DEVICE_TYPE_CRESSI_EDY }, + { "Zeagle N2iTiON 3", DEVICE_TYPE_ZEAGLE_N2ITION3 }, + { "Atomics Cobalt", DEVICE_TYPE_ATOMICS_COBALT }, + { NULL } +}; + +static void fill_computer_list(GtkListStore *store) +{ + GtkTreeIter iter; + struct device_list *list = device_list; + + for (list = device_list ; list->name ; list++) { + gtk_list_store_append(store, &iter); + gtk_list_store_set(store, &iter, + 0, list->name, + 1, list->type, + -1); + } +} + +static GtkComboBox *dive_computer_selector(GtkWidget *dialog) +{ + GtkWidget *hbox, *combo_box; + GtkListStore *model; + GtkCellRenderer *renderer; + + hbox = gtk_hbox_new(FALSE, 6); + gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), hbox); + + model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT); + fill_computer_list(model); + + combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model)); + gtk_box_pack_start(GTK_BOX(hbox), combo_box, FALSE, TRUE, 3); + + renderer = gtk_cell_renderer_text_new(); + gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE); + gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL); + + return GTK_COMBO_BOX(combo_box); +} + +void import_dialog(GtkWidget *w, gpointer data) +{ + int result; + GtkWidget *dialog; + GtkComboBox *computer; + + dialog = gtk_dialog_new_with_buttons("Import from dive computer", + GTK_WINDOW(main_window), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, + GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, + NULL); + + computer = dive_computer_selector(dialog); + + gtk_widget_show_all(dialog); + result = gtk_dialog_run(GTK_DIALOG(dialog)); + switch (result) { + int type; + GtkTreeIter iter; + GtkTreeModel *model; + const char *comp; + case GTK_RESPONSE_ACCEPT: + if (!gtk_combo_box_get_active_iter(computer, &iter)) + break; + model = gtk_combo_box_get_model(computer); + gtk_tree_model_get(model, &iter, + 0, &comp, + 1, &type, + -1); + do_import(comp, type); + break; + default: + break; + } + gtk_widget_destroy(dialog); +} + diff --git a/main.c b/main.c index 367d3ad..ab2948e 100644 --- a/main.c +++ b/main.c @@ -367,6 +367,7 @@ static GtkActionEntry menu_items[] = { { "FileMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL}, { "OpenFile", GTK_STOCK_OPEN, NULL, "O", NULL, G_CALLBACK(file_open) }, { "SaveFile", GTK_STOCK_SAVE, NULL, "S", NULL, G_CALLBACK(file_save) }, + { "Import", NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) }, { "Units", NULL, "Units", NULL, NULL, G_CALLBACK(unit_dialog) }, { "Renumber", NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) }, { "Quit", GTK_STOCK_QUIT, NULL, "Q", NULL, G_CALLBACK(quit) }, @@ -380,9 +381,11 @@ static const gchar* ui_string = " \ \ \ \ + \ + \ \ \ - \ + \ \ \ \ -- 2.43.0 From afffcdbc0d6402e3dfee421f86938b98e12fb550 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 12 Sep 2011 09:47:55 -0700 Subject: [PATCH 08/16] Avoid using type 'gasmix_t': use 'struct gasmix' instead libdivecomputer already uses 'gasmix_t' for its own gasmix thing. I don't like th eway we step on each others name spaces, but hey, might as well just use 'struct gasmix' and avoid the typedef. Signed-off-by: Linus Torvalds --- dive.c | 2 +- dive.h | 6 +++--- parse-xml.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/dive.c b/dive.c index ba5200f..e899c6c 100644 --- a/dive.c +++ b/dive.c @@ -246,7 +246,7 @@ static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylind *res = *b; } -static void merge_cylinder_mix(gasmix_t *res, gasmix_t *a, gasmix_t *b) +static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b) { if (a->o2.permille) b = a; diff --git a/dive.h b/dive.h index 9e5d2c4..d96e010 100644 --- a/dive.h +++ b/dive.h @@ -69,10 +69,10 @@ typedef struct { int grams; } weight_t; -typedef struct { +struct gasmix { fraction_t o2; fraction_t he; -} gasmix_t; +}; typedef struct { volume_t size; @@ -82,7 +82,7 @@ typedef struct { typedef struct { cylinder_type_t type; - gasmix_t gasmix; + struct gasmix gasmix; pressure_t start, end; } cylinder_t; diff --git a/parse-xml.c b/parse-xml.c index e14d9db..d17f7a8 100644 --- a/parse-xml.c +++ b/parse-xml.c @@ -998,7 +998,7 @@ static void dive_start(void) memset(&tm, 0, sizeof(tm)); } -static void sanitize_gasmix(gasmix_t *mix) +static void sanitize_gasmix(struct gasmix *mix) { unsigned int o2, he; -- 2.43.0 From 425649a27816dbef9a6684b1da53e2ca6d8b7bd9 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 12 Sep 2011 09:49:54 -0700 Subject: [PATCH 09/16] Make 'report_error()' usable from outside of main.c The dive computer import code will want to show errors too.. Signed-off-by: Linus Torvalds --- display.h | 1 + main.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/display.h b/display.h index 4b2e83b..df701a3 100644 --- a/display.h +++ b/display.h @@ -8,6 +8,7 @@ extern GtkWidget *main_window; extern void import_dialog(GtkWidget *, gpointer); +extern void report_error(GError* error); extern GtkWidget *dive_profile_widget(void); extern GtkWidget *dive_info_frame(void); diff --git a/main.c b/main.c index ab2948e..2357082 100644 --- a/main.c +++ b/main.c @@ -124,7 +124,7 @@ static void on_info_bar_response(GtkWidget *widget, gint response, } } -static void report_error(GError* error) +void report_error(GError* error) { if (error == NULL) { -- 2.43.0 From a9f74044ae228f912515bebc983f872aa7f37695 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 12 Sep 2011 09:50:40 -0700 Subject: [PATCH 10/16] Flesh out the libdivecomputer interfaces some more .. start some error reporting, and register some early (empty) callbacks. This still doesn't actually do anything. But commit early, commit often: when I start seriously breaking things, I want to have a "hey, this still at least compiled" state. Signed-off-by: Linus Torvalds --- libdivecomputer.c | 50 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/libdivecomputer.c b/libdivecomputer.c index b9850b3..c9255e0 100644 --- a/libdivecomputer.c +++ b/libdivecomputer.c @@ -1,6 +1,7 @@ #include #include +#include "dive.h" #include "display.h" /* libdivecomputer */ @@ -89,6 +90,31 @@ static device_status_t device_open(const char *devname, } } +static void error(const char *fmt, ...) +{ + va_list args; + GError *error; + + va_start(args, fmt); + error = g_error_new_valist( + g_quark_from_string("divelog"), + DIVE_ERROR_PARSE, fmt, args); + va_end(args); + report_error(error); + g_error_free(error); +} + +static void +event_cb (device_t *device, device_event_t event, const void *data, void *userdata) +{ +} + +static int +cancel_cb (void *userdata) +{ + return 0; +} + static void do_import(const char *computer, device_type_t type) { /* FIXME! Needs user input! */ @@ -97,9 +123,29 @@ static void do_import(const char *computer, device_type_t type) device_status_t rc; rc = device_open(devname, type, &device); - printf("rc=%d\n", rc); - if (rc != DEVICE_STATUS_SUCCESS) + if (rc != DEVICE_STATUS_SUCCESS) { + error("Unable to open %s (%s)", computer, devname); return; + } + + // Register the event handler. + int events = DEVICE_EVENT_WAITING | DEVICE_EVENT_PROGRESS | DEVICE_EVENT_DEVINFO | DEVICE_EVENT_CLOCK; + rc = device_set_events(device, events, event_cb, NULL); + if (rc != DEVICE_STATUS_SUCCESS) { + error("Error registering the event handler."); + device_close(device); + return; + } + + // Register the cancellation handler. + rc = device_set_cancel(device, cancel_cb, NULL); + if (rc != DEVICE_STATUS_SUCCESS) { + error("Error registering the cancellation handler."); + device_close(device); + return; + } + + error("No actual code yet for importing (%s: %s)", computer, devname); } /* -- 2.43.0 From 6b0bb87f5b1f6495f0d4853f5f71c5774682cc32 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 12 Sep 2011 10:27:35 -0700 Subject: [PATCH 11/16] Further work on libdivecomputer integration .. this now registers the dive parsing callback, and starts to parse the data. So I can see the last divetime on my Suunto Vyper Air now. Still a lot more boilerplate stuff to go, though. The libdivecomputer interfaces really are pretty insane: why should the caller set up the dive parsing for each computer type, when libdivecomputer knows what types it has? IOW, much of that boilerplate should be hidden inside of libdivecomputer, rather than exposed to the user. But whatever. I'm taking pieces from "examples/universal.c" as I go along (it's under LGPL 2.1). I want to do it in small chunks just to feel that I understand what's going on, rather than just blindly copying it all. Signed-off-by: Linus Torvalds --- libdivecomputer.c | 160 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 143 insertions(+), 17 deletions(-) diff --git a/libdivecomputer.c b/libdivecomputer.c index c9255e0..c95e02a 100644 --- a/libdivecomputer.c +++ b/libdivecomputer.c @@ -17,6 +17,135 @@ #include #include +static void error(const char *fmt, ...) +{ + va_list args; + GError *error; + + va_start(args, fmt); + error = g_error_new_valist( + g_quark_from_string("divelog"), + DIVE_ERROR_PARSE, fmt, args); + va_end(args); + report_error(error); + g_error_free(error); +} + +typedef struct device_data_t { + device_type_t type; + const char *name; + device_devinfo_t devinfo; + device_clock_t clock; +} device_data_t; + +static parser_status_t create_parser(device_data_t *devdata, parser_t **parser) +{ + switch (devdata->type) { + case DEVICE_TYPE_SUUNTO_SOLUTION: + return suunto_solution_parser_create(parser); + + case DEVICE_TYPE_SUUNTO_EON: + return suunto_eon_parser_create(parser, 0); + + case DEVICE_TYPE_SUUNTO_VYPER: + if (devdata->devinfo.model == 0x01) + return suunto_eon_parser_create(parser, 1); + return suunto_vyper_parser_create(parser); + + case DEVICE_TYPE_SUUNTO_VYPER2: + case DEVICE_TYPE_SUUNTO_D9: + return suunto_d9_parser_create(parser, devdata->devinfo.model); + + case DEVICE_TYPE_UWATEC_ALADIN: + case DEVICE_TYPE_UWATEC_MEMOMOUSE: + return uwatec_memomouse_parser_create(parser, devdata->clock.devtime, devdata->clock.systime); + + case DEVICE_TYPE_UWATEC_SMART: + return uwatec_smart_parser_create(parser, devdata->devinfo.model, devdata->clock.devtime, devdata->clock.systime); + + case DEVICE_TYPE_REEFNET_SENSUS: + return reefnet_sensus_parser_create(parser, devdata->clock.devtime, devdata->clock.systime); + + case DEVICE_TYPE_REEFNET_SENSUSPRO: + return reefnet_sensuspro_parser_create(parser, devdata->clock.devtime, devdata->clock.systime); + + case DEVICE_TYPE_REEFNET_SENSUSULTRA: + return reefnet_sensusultra_parser_create(parser, devdata->clock.devtime, devdata->clock.systime); + + case DEVICE_TYPE_OCEANIC_VTPRO: + return oceanic_vtpro_parser_create(parser); + + case DEVICE_TYPE_OCEANIC_VEO250: + return oceanic_veo250_parser_create(parser, devdata->devinfo.model); + + case DEVICE_TYPE_OCEANIC_ATOM2: + return oceanic_atom2_parser_create(parser, devdata->devinfo.model); + + case DEVICE_TYPE_MARES_NEMO: + case DEVICE_TYPE_MARES_PUCK: + return mares_nemo_parser_create(parser, devdata->devinfo.model); + + case DEVICE_TYPE_MARES_ICONHD: + return mares_iconhd_parser_create(parser); + + case DEVICE_TYPE_HW_OSTC: + return hw_ostc_parser_create(parser); + + case DEVICE_TYPE_CRESSI_EDY: + case DEVICE_TYPE_ZEAGLE_N2ITION3: + return cressi_edy_parser_create(parser, devdata->devinfo.model); + + case DEVICE_TYPE_ATOMICS_COBALT: + return atomics_cobalt_parser_create(parser); + + default: + return PARSER_STATUS_ERROR; + } +} + +static int dive_cb(const unsigned char *data, unsigned int size, + const unsigned char *fingerprint, unsigned int fsize, + void *userdata) +{ + int rc; + parser_t *parser = NULL; + device_data_t *devdata = userdata; + dc_datetime_t dt = {0}; + + rc = create_parser(devdata, &parser); + if (rc != PARSER_STATUS_SUCCESS) { + error("Unable to create parser for %s", devdata->name); + return rc; + } + + rc = parser_set_data(parser, data, size); + if (rc != PARSER_STATUS_SUCCESS) { + error("Error registering the data."); + parser_destroy(parser); + return rc; + } + + rc = parser_get_datetime(parser, &dt); + if (rc != PARSER_STATUS_SUCCESS && rc != PARSER_STATUS_UNSUPPORTED) { + error("Error parsing the datetime."); + parser_destroy (parser); + return rc; + } + + printf("%04i-%02i-%02i %02i:%02i:%02i\n", + dt.year, dt.month, dt.day, + dt.hour, dt.minute, dt.second); + + parser_destroy(parser); + return PARSER_STATUS_SUCCESS; +} + + +static device_status_t import_device_data(device_t *device, device_data_t *devicedata) +{ + return device_foreach(device, dive_cb, devicedata); +} + static device_status_t device_open(const char *devname, device_type_t type, device_t **device) @@ -90,20 +219,6 @@ static device_status_t device_open(const char *devname, } } -static void error(const char *fmt, ...) -{ - va_list args; - GError *error; - - va_start(args, fmt); - error = g_error_new_valist( - g_quark_from_string("divelog"), - DIVE_ERROR_PARSE, fmt, args); - va_end(args); - report_error(error); - g_error_free(error); -} - static void event_cb (device_t *device, device_event_t event, const void *data, void *userdata) { @@ -121,6 +236,10 @@ static void do_import(const char *computer, device_type_t type) const char *devname = "/dev/ttyUSB0"; device_t *device = NULL; device_status_t rc; + device_data_t devicedata = { + .type = type, + .name = computer, + }; rc = device_open(devname, type, &device); if (rc != DEVICE_STATUS_SUCCESS) { @@ -130,7 +249,7 @@ static void do_import(const char *computer, device_type_t type) // Register the event handler. int events = DEVICE_EVENT_WAITING | DEVICE_EVENT_PROGRESS | DEVICE_EVENT_DEVINFO | DEVICE_EVENT_CLOCK; - rc = device_set_events(device, events, event_cb, NULL); + rc = device_set_events(device, events, event_cb, &devicedata); if (rc != DEVICE_STATUS_SUCCESS) { error("Error registering the event handler."); device_close(device); @@ -138,14 +257,21 @@ static void do_import(const char *computer, device_type_t type) } // Register the cancellation handler. - rc = device_set_cancel(device, cancel_cb, NULL); + rc = device_set_cancel(device, cancel_cb, &devicedata); if (rc != DEVICE_STATUS_SUCCESS) { error("Error registering the cancellation handler."); device_close(device); return; } - error("No actual code yet for importing (%s: %s)", computer, devname); + rc = import_device_data(device, &devicedata); + if (rc != DEVICE_STATUS_SUCCESS) { + error("Dive data import error"); + device_close(device); + return; + } + + device_close(device); } /* -- 2.43.0 From 7d8fed4eee77ed8e011762bde6f5691f0fa81f39 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 12 Sep 2011 10:37:54 -0700 Subject: [PATCH 12/16] More libdivecomputer boilerplate stuff .. fill in the event parsing. This doesn't generate the fingerprint like the example does, I just don't care about that yet. Signed-off-by: Linus Torvalds --- libdivecomputer.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/libdivecomputer.c b/libdivecomputer.c index c95e02a..c08b117 100644 --- a/libdivecomputer.c +++ b/libdivecomputer.c @@ -222,6 +222,35 @@ static device_status_t device_open(const char *devname, static void event_cb (device_t *device, device_event_t event, const void *data, void *userdata) { + const device_progress_t *progress = (device_progress_t *) data; + const device_devinfo_t *devinfo = (device_devinfo_t *) data; + const device_clock_t *clock = (device_clock_t *) data; + device_data_t *devdata = (device_data_t *) userdata; + + switch (event) { + case DEVICE_EVENT_WAITING: + printf("Event: waiting for user action\n"); + break; + case DEVICE_EVENT_PROGRESS: + printf("Event: progress %3.2f%% (%u/%u)\n", + 100.0 * (double) progress->current / (double) progress->maximum, + progress->current, progress->maximum); + break; + case DEVICE_EVENT_DEVINFO: + devdata->devinfo = *devinfo; + printf("Event: model=%u (0x%08x), firmware=%u (0x%08x), serial=%u (0x%08x)\n", + devinfo->model, devinfo->model, + devinfo->firmware, devinfo->firmware, + devinfo->serial, devinfo->serial); + break; + case DEVICE_EVENT_CLOCK: + devdata->clock = *clock; + printf("Event: systime=%lld, devtime=%u\n", + clock->systime, clock->devtime); + break; + default: + break; + } } static int -- 2.43.0 From d5b7f7dc06827cab8dd57d6d373a08d6be111c79 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 12 Sep 2011 11:05:32 -0700 Subject: [PATCH 13/16] Libdivecomputer integration, part n+1 This actually gets me far enough that it prints out all the dives on my dive computer. It doesn't actually turn them into real dives yet, though - only a series of ugly 'printf's so far. And it hangs after printing the last dive. So I'm doing something wrong. Signed-off-by: Linus Torvalds --- libdivecomputer.c | 133 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 132 insertions(+), 1 deletion(-) diff --git a/libdivecomputer.c b/libdivecomputer.c index c08b117..ab75a89 100644 --- a/libdivecomputer.c +++ b/libdivecomputer.c @@ -103,6 +103,87 @@ static parser_status_t create_parser(device_data_t *devdata, parser_t **parser) } } +static int parse_gasmixes(parser_t *parser, int ngases) +{ + int i; + + for (i = 0; i < ngases; i++) { + int rc; + gasmix_t gasmix = {0}; + + rc = parser_get_field(parser, FIELD_TYPE_GASMIX, i, &gasmix); + if (rc != PARSER_STATUS_SUCCESS && rc != PARSER_STATUS_UNSUPPORTED) + return rc; + + printf("\n" + " %.1f\n" + " %.1f\n" + " %.1f\n" + "\n", + gasmix.helium * 100.0, + gasmix.oxygen * 100.0, + gasmix.nitrogen * 100.0); + } + return PARSER_STATUS_SUCCESS; +} + +void +sample_cb (parser_sample_type_t type, parser_sample_value_t value, void *userdata) +{ + int i; + static const char *events[] = { + "none", "deco", "rbt", "ascent", "ceiling", "workload", "transmitter", + "violation", "bookmark", "surface", "safety stop", "gaschange", + "safety stop (voluntary)", "safety stop (mandatory)", "deepstop", + "ceiling (safety stop)", "unknown", "divetime", "maxdepth", + "OLF", "PO2", "airtime", "rgbm", "heading", "tissue level warning"}; + + switch (type) { + case SAMPLE_TYPE_TIME: + printf("\n"); + printf(" \n", value.time / 60, value.time % 60); + break; + case SAMPLE_TYPE_DEPTH: + printf(" %.2f\n", value.depth); + break; + case SAMPLE_TYPE_PRESSURE: + printf(" %.2f\n", value.pressure.tank, value.pressure.value); + break; + case SAMPLE_TYPE_TEMPERATURE: + printf(" %.2f\n", value.temperature); + break; + case SAMPLE_TYPE_EVENT: + printf(" %s\n", + value.event.type, value.event.time, value.event.flags, value.event.value, events[value.event.type]); + break; + case SAMPLE_TYPE_RBT: + printf(" %u\n", value.rbt); + break; + case SAMPLE_TYPE_HEARTBEAT: + printf(" %u\n", value.heartbeat); + break; + case SAMPLE_TYPE_BEARING: + printf(" %u\n", value.bearing); + break; + case SAMPLE_TYPE_VENDOR: + printf(" ", value.vendor.type, value.vendor.size); + for (i = 0; i < value.vendor.size; ++i) + printf("%02X", ((unsigned char *) value.vendor.data)[i]); + printf("\n"); + break; + default: + break; + } +} + + +static int parse_samples(parser_t *parser) +{ + // Parse the sample data. + printf("Parsing the sample data.\n"); + return parser_samples_foreach(parser, sample_cb, NULL); +} + static int dive_cb(const unsigned char *data, unsigned int size, const unsigned char *fingerprint, unsigned int fsize, void *userdata) @@ -136,8 +217,58 @@ static int dive_cb(const unsigned char *data, unsigned int size, dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second); + // Parse the divetime. + printf("Parsing the divetime.\n"); + unsigned int divetime = 0; + rc = parser_get_field (parser, FIELD_TYPE_DIVETIME, 0, &divetime); + if (rc != PARSER_STATUS_SUCCESS && rc != PARSER_STATUS_UNSUPPORTED) { + error("Error parsing the divetime."); + parser_destroy(parser); + return rc; + } + + printf("%02u:%02u\n", + divetime / 60, divetime % 60); + + // Parse the maxdepth. + printf("Parsing the maxdepth.\n"); + double maxdepth = 0.0; + rc = parser_get_field(parser, FIELD_TYPE_MAXDEPTH, 0, &maxdepth); + if (rc != PARSER_STATUS_SUCCESS && rc != PARSER_STATUS_UNSUPPORTED) { + error("Error parsing the maxdepth."); + parser_destroy(parser); + return rc; + } + + printf("%.2f\n", maxdepth); + + // Parse the gas mixes. + printf("Parsing the gas mixes.\n"); + unsigned int ngases = 0; + rc = parser_get_field(parser, FIELD_TYPE_GASMIX_COUNT, 0, &ngases); + if (rc != PARSER_STATUS_SUCCESS && rc != PARSER_STATUS_UNSUPPORTED) { + error("Error parsing the gas mix count."); + parser_destroy(parser); + return rc; + } + + rc = parse_gasmixes(parser, ngases); + if (rc != PARSER_STATUS_SUCCESS) { + error("Error parsing the gas mix."); + parser_destroy(parser); + return rc; + } + + // Initialize the sample data. + rc = parse_samples(parser); + if (rc != PARSER_STATUS_SUCCESS) { + error("Error parsing the samples."); + parser_destroy(parser); + return rc; + } + parser_destroy(parser); - return PARSER_STATUS_SUCCESS; + return 1; } -- 2.43.0 From d45db9fac7b3d2ea0426a96158432f54d938f5c7 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 12 Sep 2011 11:41:26 -0700 Subject: [PATCH 14/16] libdivecomputer integration: add a progress bar Instead of writing out the progress events, use them to update a real progress bar. Also, we need to handle gtk events while busy with the dive computer reading. That should *probably* be done with a threading model, because libdivecomputer does seem to have some timing sensitivity - I'm getting "failure to read memory block" if I make that loop do the standard while (gtk_events_pending()) gtk_main_iteration(); thing. Besides, even if we did do that loop, it would still cause problems when the libdivecomputer code is stuck reading a serial line that doesn't respond or whatever. But for now this ugly hack is "good enough" to get further. Signed-off-by: Linus Torvalds --- libdivecomputer.c | 65 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/libdivecomputer.c b/libdivecomputer.c index ab75a89..7bea8e3 100644 --- a/libdivecomputer.c +++ b/libdivecomputer.c @@ -17,6 +17,18 @@ #include #include +/* + * I'd love to do a while-loop here for pending events, but + * that seems to screw up with the dive computer reading timing. + * + * I may need to spawn a new thread to do the computer + * reading stuff.. + */ +static int run_gtk_mainloop(void) +{ + return gtk_main_iteration_do(0); +} + static void error(const char *fmt, ...) { va_list args; @@ -33,7 +45,8 @@ static void error(const char *fmt, ...) typedef struct device_data_t { device_type_t type; - const char *name; + const char *name, *devname; + GtkWidget *progressbar; device_devinfo_t devinfo; device_clock_t clock; } device_data_t; @@ -193,6 +206,9 @@ static int dive_cb(const unsigned char *data, unsigned int size, device_data_t *devdata = userdata; dc_datetime_t dt = {0}; + /* Christ, this is hacky */ + run_gtk_mainloop(); + rc = create_parser(devdata, &parser); if (rc != PARSER_STATUS_SUCCESS) { error("Unable to create parser for %s", devdata->name); @@ -351,21 +367,23 @@ static device_status_t device_open(const char *devname, } static void -event_cb (device_t *device, device_event_t event, const void *data, void *userdata) +event_cb(device_t *device, device_event_t event, const void *data, void *userdata) { const device_progress_t *progress = (device_progress_t *) data; const device_devinfo_t *devinfo = (device_devinfo_t *) data; const device_clock_t *clock = (device_clock_t *) data; device_data_t *devdata = (device_data_t *) userdata; + /* Christ, this is hacky */ + run_gtk_mainloop(); + switch (event) { case DEVICE_EVENT_WAITING: printf("Event: waiting for user action\n"); break; case DEVICE_EVENT_PROGRESS: - printf("Event: progress %3.2f%% (%u/%u)\n", - 100.0 * (double) progress->current / (double) progress->maximum, - progress->current, progress->maximum); + gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(devdata->progressbar), + (double) progress->current / (double) progress->maximum); break; case DEVICE_EVENT_DEVINFO: devdata->devinfo = *devinfo; @@ -385,31 +403,27 @@ event_cb (device_t *device, device_event_t event, const void *data, void *userda } static int -cancel_cb (void *userdata) +cancel_cb(void *userdata) { - return 0; + return run_gtk_mainloop(); } -static void do_import(const char *computer, device_type_t type) +static void do_import(device_data_t *data) { /* FIXME! Needs user input! */ const char *devname = "/dev/ttyUSB0"; device_t *device = NULL; device_status_t rc; - device_data_t devicedata = { - .type = type, - .name = computer, - }; - rc = device_open(devname, type, &device); + rc = device_open(devname, data->type, &device); if (rc != DEVICE_STATUS_SUCCESS) { - error("Unable to open %s (%s)", computer, devname); + error("Unable to open %s (%s)", data->name, data->devname); return; } // Register the event handler. int events = DEVICE_EVENT_WAITING | DEVICE_EVENT_PROGRESS | DEVICE_EVENT_DEVINFO | DEVICE_EVENT_CLOCK; - rc = device_set_events(device, events, event_cb, &devicedata); + rc = device_set_events(device, events, event_cb, data); if (rc != DEVICE_STATUS_SUCCESS) { error("Error registering the event handler."); device_close(device); @@ -417,14 +431,14 @@ static void do_import(const char *computer, device_type_t type) } // Register the cancellation handler. - rc = device_set_cancel(device, cancel_cb, &devicedata); + rc = device_set_cancel(device, cancel_cb, data); if (rc != DEVICE_STATUS_SUCCESS) { error("Error registering the cancellation handler."); device_close(device); return; } - rc = import_device_data(device, &devicedata); + rc = import_device_data(device, data); if (rc != DEVICE_STATUS_SUCCESS) { error("Dive data import error"); device_close(device); @@ -490,7 +504,7 @@ static GtkComboBox *dive_computer_selector(GtkWidget *dialog) GtkCellRenderer *renderer; hbox = gtk_hbox_new(FALSE, 6); - gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), hbox); + gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, FALSE, FALSE, 3); model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT); fill_computer_list(model); @@ -508,8 +522,11 @@ static GtkComboBox *dive_computer_selector(GtkWidget *dialog) void import_dialog(GtkWidget *w, gpointer data) { int result; - GtkWidget *dialog; + GtkWidget *dialog, *hbox; GtkComboBox *computer; + device_data_t devicedata = { + .devname = "/dev/ttyUSB0", + }; dialog = gtk_dialog_new_with_buttons("Import from dive computer", GTK_WINDOW(main_window), @@ -520,6 +537,11 @@ void import_dialog(GtkWidget *w, gpointer data) computer = dive_computer_selector(dialog); + hbox = gtk_hbox_new(FALSE, 6); + gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, FALSE, TRUE, 3); + devicedata.progressbar = gtk_progress_bar_new(); + gtk_container_add(GTK_CONTAINER(hbox), devicedata.progressbar); + gtk_widget_show_all(dialog); result = gtk_dialog_run(GTK_DIALOG(dialog)); switch (result) { @@ -535,11 +557,12 @@ void import_dialog(GtkWidget *w, gpointer data) 0, &comp, 1, &type, -1); - do_import(comp, type); + devicedata.type = type; + devicedata.name = comp; + do_import(&devicedata); break; default: break; } gtk_widget_destroy(dialog); } - -- 2.43.0 From aa416e3c96dfa53db5ae277e72f6a03821c45cac Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 12 Sep 2011 12:56:34 -0700 Subject: [PATCH 15/16] Abstract out dive/sample allocation a bit We're going to start to want to allocate dives and samples for the libdivecomputer import too, so let's clean things up a bit for that. Signed-off-by: Linus Torvalds --- dive.c | 73 ++++++++++++++++++++++++++++++++++++++--------------- dive.h | 8 +++++- parse-xml.c | 32 +++-------------------- 3 files changed, 64 insertions(+), 49 deletions(-) diff --git a/dive.c b/dive.c index e899c6c..32ea2ff 100644 --- a/dive.c +++ b/dive.c @@ -3,6 +3,51 @@ #include "dive.h" +struct dive *alloc_dive(void) +{ + const int initial_samples = 5; + unsigned int size; + struct dive *dive; + + size = dive_size(initial_samples); + dive = malloc(size); + if (!dive) + exit(1); + memset(dive, 0, size); + dive->alloc_samples = initial_samples; + return dive; +} + +struct sample *prepare_sample(struct dive **divep) +{ + struct dive *dive = *divep; + if (dive) { + int nr = dive->samples; + int alloc_samples = dive->alloc_samples; + struct sample *sample; + if (nr >= alloc_samples) { + unsigned int size; + + alloc_samples = (alloc_samples * 3)/2 + 10; + size = dive_size(alloc_samples); + dive = realloc(dive, size); + if (!dive) + return NULL; + dive->alloc_samples = alloc_samples; + *divep = dive; + } + sample = dive->sample + nr; + memset(sample, 0, sizeof(*sample)); + return sample; + } + return NULL; +} + +void finish_sample(struct dive *dive, struct sample *sample) +{ + dive->samples++; +} + /* * So when we re-calculate maxdepth and meandepth, we will * not override the old numbers if they are close to the @@ -133,24 +178,15 @@ struct dive *fixup_dive(struct dive *dive) #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n) #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n) -static int alloc_samples; - static struct dive *add_sample(struct sample *sample, int time, struct dive *dive) { - int nr = dive->samples; - struct sample *d; + struct sample *p = prepare_sample(&dive); - if (nr >= alloc_samples) { - alloc_samples = (alloc_samples + 64) * 3 / 2; - dive = realloc(dive, dive_size(alloc_samples)); - if (!dive) - return NULL; - } - dive->samples = nr+1; - d = dive->sample + nr; - - *d = *sample; - d->time.seconds = time; + if (!p) + return NULL; + *p = *sample; + p->time.seconds = time; + finish_sample(dive, p); return dive; } @@ -274,15 +310,12 @@ struct dive *try_to_merge(struct dive *a, struct dive *b) if (a->when != b->when) return NULL; - alloc_samples = 5; - res = malloc(dive_size(alloc_samples)); - if (!res) - return NULL; - memset(res, 0, dive_size(alloc_samples)); + res = alloc_dive(); res->when = a->when; res->location = merge_text(a->location, b->location); res->notes = merge_text(a->notes, b->notes); + MERGE_MAX(res, a, b, number); MERGE_MAX(res, a, b, maxdepth.mm); res->meandepth.mm = 0; MERGE_MAX(res, a, b, duration.seconds); diff --git a/dive.h b/dive.h index d96e010..ee57cf8 100644 --- a/dive.h +++ b/dive.h @@ -137,7 +137,7 @@ struct dive { depth_t visibility; temperature_t airtemp, watertemp; cylinder_t cylinder[MAX_CYLINDERS]; - int samples; + int samples, alloc_samples; struct sample sample[]; }; @@ -193,6 +193,12 @@ static inline unsigned int dive_size(int samples) return sizeof(struct dive) + samples*sizeof(struct sample); } +extern struct dive *alloc_dive(void); +extern void record_dive(struct dive *dive); + +extern struct sample *prepare_sample(struct dive **divep); +extern void finish_sample(struct dive *dive, struct sample *sample); + extern struct dive *fixup_dive(struct dive *dive); extern struct dive *try_to_merge(struct dive *a, struct dive *b); diff --git a/parse-xml.c b/parse-xml.c index d17f7a8..4d6912e 100644 --- a/parse-xml.c +++ b/parse-xml.c @@ -16,7 +16,7 @@ struct dive_table dive_table; /* * Add a dive into the dive_table array */ -static void record_dive(struct dive *dive) +void record_dive(struct dive *dive) { int nr = dive_table.nr, allocated = dive_table.allocated; struct dive **dives = dive_table.dives; @@ -90,7 +90,6 @@ const struct units IMPERIAL_units = { /* * Dive info as it is being built up.. */ -static int alloc_samples; static struct dive *dive; static struct sample *sample; static struct tm tm; @@ -984,17 +983,9 @@ static void try_to_fill_dive(struct dive *dive, const char *name, char *buf) */ static void dive_start(void) { - unsigned int size; - if (dive) return; - - alloc_samples = 5; - size = dive_size(alloc_samples); - dive = malloc(size); - if (!dive) - exit(1); - memset(dive, 0, size); + dive = alloc_dive(); memset(&tm, 0, sizeof(tm)); } @@ -1143,22 +1134,7 @@ static void cylinder_end(void) static void sample_start(void) { - int nr; - - if (!dive) - return; - nr = dive->samples; - if (nr >= alloc_samples) { - unsigned int size; - - alloc_samples = (alloc_samples * 3)/2 + 10; - size = dive_size(alloc_samples); - dive = realloc(dive, size); - if (!dive) - return; - } - sample = dive->sample + nr; - memset(sample, 0, sizeof(*sample)); + sample = prepare_sample(&dive); event_index = 0; } @@ -1167,8 +1143,8 @@ static void sample_end(void) if (!dive) return; + finish_sample(dive, sample); sample = NULL; - dive->samples++; } static void entry(const char *name, int size, const char *raw) -- 2.43.0 From 42f627b8b1cf7e929a30c0a07a5fb44a4cec9e1f Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 12 Sep 2011 13:25:05 -0700 Subject: [PATCH 16/16] Libdivecomputer: start actually importing the dive data So this actually reports the dive data that libdivecomputer generates. It doesn't import special events etc, but neither do we for the xml importer. It is also slow as heck, since it doesn't try to do the "hey, I already have this dive" logic and always imports everything, but the basics are definitely there. Signed-off-by: Linus Torvalds --- dive.h | 3 ++ divelist.h | 1 + libdivecomputer.c | 71 +++++++++++++++++++++++++++++------------------ main.c | 2 +- parse-xml.c | 2 +- 5 files changed, 50 insertions(+), 29 deletions(-) diff --git a/dive.h b/dive.h index ee57cf8..cc6e85d 100644 --- a/dive.h +++ b/dive.h @@ -193,12 +193,15 @@ static inline unsigned int dive_size(int samples) return sizeof(struct dive) + samples*sizeof(struct sample); } +extern time_t utc_mktime(struct tm *tm); + extern struct dive *alloc_dive(void); extern void record_dive(struct dive *dive); extern struct sample *prepare_sample(struct dive **divep); extern void finish_sample(struct dive *dive, struct sample *sample); +extern void report_dives(void); extern struct dive *fixup_dive(struct dive *dive); extern struct dive *try_to_merge(struct dive *a, struct dive *b); diff --git a/divelist.h b/divelist.h index 797a896..c046a87 100644 --- a/divelist.h +++ b/divelist.h @@ -10,6 +10,7 @@ struct DiveList { GtkTreeViewColumn *date, *depth, *duration; }; +extern struct DiveList dive_list; extern struct DiveList dive_list_create(void); extern void dive_list_update_dives(struct DiveList); extern void update_dive_list_units(struct DiveList *); diff --git a/libdivecomputer.c b/libdivecomputer.c index 7bea8e3..da9b35b 100644 --- a/libdivecomputer.c +++ b/libdivecomputer.c @@ -2,6 +2,7 @@ #include #include "dive.h" +#include "divelist.h" #include "display.h" /* libdivecomputer */ @@ -116,7 +117,7 @@ static parser_status_t create_parser(device_data_t *devdata, parser_t **parser) } } -static int parse_gasmixes(parser_t *parser, int ngases) +static int parse_gasmixes(struct dive *dive, parser_t *parser, int ngases) { int i; @@ -128,20 +129,17 @@ static int parse_gasmixes(parser_t *parser, int ngases) if (rc != PARSER_STATUS_SUCCESS && rc != PARSER_STATUS_UNSUPPORTED) return rc; - printf("\n" - " %.1f\n" - " %.1f\n" - " %.1f\n" - "\n", - gasmix.helium * 100.0, - gasmix.oxygen * 100.0, - gasmix.nitrogen * 100.0); + if (i >= MAX_CYLINDERS) + continue; + + dive->cylinder[i].gasmix.o2.permille = gasmix.oxygen * 1000 + 0.5; + dive->cylinder[i].gasmix.he.permille = gasmix.helium * 1000 + 0.5; } return PARSER_STATUS_SUCCESS; } void -sample_cb (parser_sample_type_t type, parser_sample_value_t value, void *userdata) +sample_cb(parser_sample_type_t type, parser_sample_value_t value, void *userdata) { int i; static const char *events[] = { @@ -150,20 +148,31 @@ sample_cb (parser_sample_type_t type, parser_sample_value_t value, void *userdat "safety stop (voluntary)", "safety stop (mandatory)", "deepstop", "ceiling (safety stop)", "unknown", "divetime", "maxdepth", "OLF", "PO2", "airtime", "rgbm", "heading", "tissue level warning"}; + struct dive **divep = userdata; + struct dive *dive = *divep; + struct sample *sample; + + /* + * We fill in the "previous" sample - except for SAMPLE_TYPE_TIME, + * which creates a new one. + */ + sample = dive->samples ? dive->sample+dive->samples-1 : NULL; switch (type) { case SAMPLE_TYPE_TIME: - printf("\n"); - printf(" \n", value.time / 60, value.time % 60); + sample = prepare_sample(divep); + sample->time.seconds = value.time; + finish_sample(*divep, sample); break; case SAMPLE_TYPE_DEPTH: - printf(" %.2f\n", value.depth); + sample->depth.mm = value.depth * 1000 + 0.5; break; case SAMPLE_TYPE_PRESSURE: - printf(" %.2f\n", value.pressure.tank, value.pressure.value); + sample->cylinderindex = value.pressure.tank; + sample->cylinderpressure.mbar = value.pressure.value * 1000 + 0.5; break; case SAMPLE_TYPE_TEMPERATURE: - printf(" %.2f\n", value.temperature); + sample->temperature.mkelvin = (value.temperature + 273.15) * 1000 + 0.5; break; case SAMPLE_TYPE_EVENT: printf(" %s\n", @@ -190,11 +199,11 @@ sample_cb (parser_sample_type_t type, parser_sample_value_t value, void *userdat } -static int parse_samples(parser_t *parser) +static int parse_samples(struct dive **divep, parser_t *parser) { // Parse the sample data. printf("Parsing the sample data.\n"); - return parser_samples_foreach(parser, sample_cb, NULL); + return parser_samples_foreach(parser, sample_cb, divep); } static int dive_cb(const unsigned char *data, unsigned int size, @@ -205,6 +214,8 @@ static int dive_cb(const unsigned char *data, unsigned int size, parser_t *parser = NULL; device_data_t *devdata = userdata; dc_datetime_t dt = {0}; + struct tm tm; + struct dive *dive; /* Christ, this is hacky */ run_gtk_mainloop(); @@ -222,6 +233,7 @@ static int dive_cb(const unsigned char *data, unsigned int size, return rc; } + dive = alloc_dive(); rc = parser_get_datetime(parser, &dt); if (rc != PARSER_STATUS_SUCCESS && rc != PARSER_STATUS_UNSUPPORTED) { error("Error parsing the datetime."); @@ -229,9 +241,13 @@ static int dive_cb(const unsigned char *data, unsigned int size, return rc; } - printf("%04i-%02i-%02i %02i:%02i:%02i\n", - dt.year, dt.month, dt.day, - dt.hour, dt.minute, dt.second); + tm.tm_year = dt.year; + tm.tm_mon = dt.month-1; + tm.tm_mday = dt.day; + tm.tm_hour = dt.hour; + tm.tm_min = dt.minute; + tm.tm_sec = dt.second; + dive->when = utc_mktime(&tm); // Parse the divetime. printf("Parsing the divetime.\n"); @@ -242,9 +258,7 @@ static int dive_cb(const unsigned char *data, unsigned int size, parser_destroy(parser); return rc; } - - printf("%02u:%02u\n", - divetime / 60, divetime % 60); + dive->duration.seconds = divetime; // Parse the maxdepth. printf("Parsing the maxdepth.\n"); @@ -255,8 +269,7 @@ static int dive_cb(const unsigned char *data, unsigned int size, parser_destroy(parser); return rc; } - - printf("%.2f\n", maxdepth); + dive->maxdepth.mm = maxdepth * 1000 + 0.5; // Parse the gas mixes. printf("Parsing the gas mixes.\n"); @@ -268,7 +281,7 @@ static int dive_cb(const unsigned char *data, unsigned int size, return rc; } - rc = parse_gasmixes(parser, ngases); + rc = parse_gasmixes(dive, parser, ngases); if (rc != PARSER_STATUS_SUCCESS) { error("Error parsing the gas mix."); parser_destroy(parser); @@ -276,12 +289,13 @@ static int dive_cb(const unsigned char *data, unsigned int size, } // Initialize the sample data. - rc = parse_samples(parser); + rc = parse_samples(&dive, parser); if (rc != PARSER_STATUS_SUCCESS) { error("Error parsing the samples."); parser_destroy(parser); return rc; } + record_dive(dive); parser_destroy(parser); return 1; @@ -565,4 +579,7 @@ void import_dialog(GtkWidget *w, gpointer data) break; } gtk_widget_destroy(dialog); + + report_dives(); + dive_list_update_dives(dive_list); } diff --git a/main.c b/main.c index 2357082..5157c54 100644 --- a/main.c +++ b/main.c @@ -37,7 +37,7 @@ static int sortfn(const void *_a, const void *_b) * This doesn't really report anything at all. We just sort the * dives, the GUI does the reporting */ -static void report_dives(void) +void report_dives(void) { int i; diff --git a/parse-xml.c b/parse-xml.c index 4d6912e..24acb1c 100644 --- a/parse-xml.c +++ b/parse-xml.c @@ -104,7 +104,7 @@ static enum import_source { UDDF, } import_source; -static time_t utc_mktime(struct tm *tm) +time_t utc_mktime(struct tm *tm) { static const int mdays[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 -- 2.43.0