summaryrefslogtreecommitdiffstats
path: root/core/event.c
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2020-10-25 09:14:16 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2020-10-25 13:59:52 -0700
commit8212acc9925b28ecd546b01047c6a8fc574326ef (patch)
treef4ce765228f3bb2511a186373eb2b3394b647709 /core/event.c
parentd82a7b8b73c13451f469832d6402303ccf2ee3d3 (diff)
downloadsubsurface-8212acc9925b28ecd546b01047c6a8fc574326ef.tar.gz
cleanup: break out event-related code into event.[c|h]
In an effort to reduce the size of dive.h and dive.c, break out the event related functions. Moreover event-names were handled by the profile-code, collect that also in the new source files. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/event.c')
-rw-r--r--core/event.c136
1 files changed, 136 insertions, 0 deletions
diff --git a/core/event.c b/core/event.c
new file mode 100644
index 000000000..8f73aefd8
--- /dev/null
+++ b/core/event.c
@@ -0,0 +1,136 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "event.h"
+#include "subsurface-string.h"
+
+#include <string.h>
+#include <stdlib.h>
+
+int event_is_gaschange(const struct event *ev)
+{
+ return ev->type == SAMPLE_EVENT_GASCHANGE ||
+ ev->type == SAMPLE_EVENT_GASCHANGE2;
+}
+
+bool event_is_divemodechange(const struct event *ev)
+{
+ return same_string(ev->name, "modechange");
+}
+
+struct event *clone_event(const struct event *src_ev)
+{
+ struct event *ev;
+ if (!src_ev)
+ return NULL;
+
+ size_t size = sizeof(*src_ev) + strlen(src_ev->name) + 1;
+ ev = (struct event*) malloc(size);
+ if (!ev)
+ exit(1);
+ memcpy(ev, src_ev, size);
+ ev->next = NULL;
+
+ return ev;
+}
+
+void free_events(struct event *ev)
+{
+ while (ev) {
+ struct event *next = ev->next;
+ free(ev);
+ ev = next;
+ }
+}
+
+struct event *create_event(unsigned int time, int type, int flags, int value, const char *name)
+{
+ int gas_index = -1;
+ struct event *ev;
+ unsigned int size, len = strlen(name);
+
+ size = sizeof(*ev) + len + 1;
+ ev = malloc(size);
+ if (!ev)
+ return NULL;
+ memset(ev, 0, size);
+ memcpy(ev->name, name, len);
+ ev->time.seconds = time;
+ ev->type = type;
+ ev->flags = flags;
+ ev->value = value;
+
+ /*
+ * Expand the events into a sane format. Currently
+ * just gas switches
+ */
+ switch (type) {
+ case SAMPLE_EVENT_GASCHANGE2:
+ /* High 16 bits are He percentage */
+ ev->gas.mix.he.permille = (value >> 16) * 10;
+
+ /* Extension to the GASCHANGE2 format: cylinder index in 'flags' */
+ /* TODO: verify that gas_index < num_cylinders. */
+ if (flags > 0)
+ gas_index = flags-1;
+ /* Fallthrough */
+ case SAMPLE_EVENT_GASCHANGE:
+ /* Low 16 bits are O2 percentage */
+ ev->gas.mix.o2.permille = (value & 0xffff) * 10;
+ ev->gas.index = gas_index;
+ break;
+ }
+
+ return ev;
+}
+
+struct event *clone_event_rename(const struct event *ev, const char *name)
+{
+ return create_event(ev->time.seconds, ev->type, ev->flags, ev->value, name);
+}
+
+bool same_event(const struct event *a, const struct event *b)
+{
+ if (a->time.seconds != b->time.seconds)
+ return 0;
+ if (a->type != b->type)
+ return 0;
+ if (a->flags != b->flags)
+ return 0;
+ if (a->value != b->value)
+ return 0;
+ return !strcmp(a->name, b->name);
+}
+
+/* collect all event names and whether we display them */
+struct ev_select *ev_namelist = NULL;
+int evn_used = 0;
+static int evn_allocated = 0;
+
+void clear_events(void)
+{
+ for (int i = 0; i < evn_used; i++)
+ free(ev_namelist[i].ev_name);
+ evn_used = 0;
+}
+
+void remember_event(const char *eventname)
+{
+ int i = 0, len;
+
+ if (!eventname || (len = strlen(eventname)) == 0)
+ return;
+ while (i < evn_used) {
+ if (!strncmp(eventname, ev_namelist[i].ev_name, len))
+ return;
+ i++;
+ }
+ if (evn_used == evn_allocated) {
+ evn_allocated += 10;
+ ev_namelist = realloc(ev_namelist, evn_allocated * sizeof(struct ev_select));
+ if (!ev_namelist)
+ /* we are screwed, but let's just bail out */
+ return;
+ }
+ ev_namelist[evn_used].ev_name = strdup(eventname);
+ ev_namelist[evn_used].plot_ev = true;
+ evn_used++;
+}