Commit 9919afa8a1c83ac050bfa9eff3539ba5c8932019

tokyovania 2022-08-09T16:35:35

Merge remote-tracking branch 'kmx/master'

diff --git a/cli/rtbuf_cli.c b/cli/rtbuf_cli.c
index 9efbaac..c05c32b 100644
--- a/cli/rtbuf_cli.c
+++ b/cli/rtbuf_cli.c
@@ -220,14 +220,17 @@ void * rtbuf_cli_run_thread_proc (void *arg)
 {
   (void) arg;
   printf("rtbuf thread: start\n");
-  if (!rtbuf_start())
+  if (!rtbuf_start()) {
     g_rtbuf_run = 1;
+    g_rtbuf_running = 1;
+  }
   while (g_rtbuf_run) {
     if (rtbuf_run())
       g_rtbuf_run = 0;
   }
   printf("rtbuf thread: stop\n");
   rtbuf_stop();
+  g_rtbuf_running = 0;
   return 0;
 }
 
@@ -302,6 +305,7 @@ int rtbuf_cli_exit (int argc, const char *argv[])
   (void) argc;
   (void) argv;
   rtbuf_cli_stop();
+  librtbuf_shutdown();
   close(0);
   exit(0);
   return 0;
diff --git a/cli/rtbuf_cli_main.c b/cli/rtbuf_cli_main.c
index 4b52d71..6145072 100644
--- a/cli/rtbuf_cli_main.c
+++ b/cli/rtbuf_cli_main.c
@@ -27,6 +27,7 @@ int main (int argc, char *argv[])
   repl_init();
   rtbuf_cli_args(argc, argv);
   res = repl();
+  rtbuf_cli_stop();
   librtbuf_shutdown();
   return res;
 }
diff --git a/lib/glfw3/keyboard.c b/lib/glfw3/keyboard.c
index b33bcc6..2ebe151 100644
--- a/lib/glfw3/keyboard.c
+++ b/lib/glfw3/keyboard.c
@@ -53,6 +53,12 @@ int rtbuf_lib_init (s_rtbuf_lib *lib)
   return 0;
 }
 
+void rtbuf_lib_unload (s_rtbuf_lib *lib)
+{
+  (void) lib;
+  glfwTerminate();
+}
+
 double scancode_frequency (int scancode, unsigned int octave)
 {
   int note = -1;
diff --git a/librtbuf/defs.h b/librtbuf/defs.h
index 0830b88..d71a196 100644
--- a/librtbuf/defs.h
+++ b/librtbuf/defs.h
@@ -36,5 +36,16 @@ typedef enum rtbuf_var_type {
 
 typedef int f_rtbuf_proc (s_rtbuf *rtbuf);
 typedef int f_rtbuf_lib_init (s_rtbuf_lib *lib);
+typedef void f_rtbuf_lib_unload (s_rtbuf_lib *lib);
+
+typedef union rtbuf_lib_init {
+  void *ptr;
+  f_rtbuf_lib_init *fun;
+} u_rtbuf_lib_init;
+
+typedef union rtbuf_lib_unload {
+  void *ptr;
+  f_rtbuf_lib_unload *fun;
+} u_rtbuf_lib_unload;
 
 #endif
diff --git a/librtbuf/lib.h b/librtbuf/lib.h
index b5bc91c..619ed25 100644
--- a/librtbuf/lib.h
+++ b/librtbuf/lib.h
@@ -81,6 +81,7 @@ struct rtbuf_lib {
   s_rtbuf_proc *proc;
   void *lib;
   const char *path;
+  f_rtbuf_lib_unload *unload;
 };
 
 extern char *g_rtbuf_lib_path[];
diff --git a/librtbuf/rtbuf.c b/librtbuf/rtbuf.c
index 37ad446..f37e42c 100644
--- a/librtbuf/rtbuf.c
+++ b/librtbuf/rtbuf.c
@@ -32,9 +32,10 @@ s_data_type   g_rtbuf_type = {
   sizeof(s_rtbuf) * 8,
   DATA_TYPE_BITS
 };
-s_data_alloc  g_rtbuf_alloc;
+s_data_alloc  g_rtbuf_alloc = {0};
 s_rtbuf      *g_rtbuf = 0;
 int           g_rtbuf_run = 0;
+int           g_rtbuf_running = 0;
 unsigned int  g_rtbuf_sort = 0;
 unsigned int  g_rtbuf_sorted[RTBUF_MAX];
 unsigned int  g_rtbuf_sorted_n = 0;
diff --git a/librtbuf/rtbuf.h b/librtbuf/rtbuf.h
index b41246b..9491355 100644
--- a/librtbuf/rtbuf.h
+++ b/librtbuf/rtbuf.h
@@ -107,6 +107,11 @@ extern s_rtbuf     *g_rtbuf;
 extern int          g_rtbuf_run;
 
 /**
+ * @brief Is the main loop still active ?
+ */
+extern int          g_rtbuf_running;
+
+/**
  * @brief Callback function for rtbuf_new.
  */
 typedef void (*f_rtbuf_new_cb) (s_rtbuf *rtb);
@@ -128,13 +133,28 @@ typedef void (*f_rtbuf_bind_cb) (s_rtbuf *src, unsigned int out,
 typedef void (*f_rtbuf_unbind_cb) (s_rtbuf *src, unsigned int out,
                                    s_rtbuf *dest, unsigned int in);
 
+/**
+ * @brief Callback function for rtbuf_new.
+ */
 extern f_rtbuf_new_cb    g_rtbuf_new_cb;
+
+/**
+ * @brief Callback function for rtbuf_delete.
+ */
 extern f_rtbuf_delete_cb g_rtbuf_delete_cb;
+
+/**
+ * @brief Callback function for rtbuf_bind.
+ */
 extern f_rtbuf_bind_cb   g_rtbuf_bind_cb;
+
+/**
+ * @brief Callback function for rtbuf_unbind.
+ */
 extern f_rtbuf_unbind_cb g_rtbuf_unbind_cb;
 
 /**
- * Initialize librtbuf.
+ * @brief Initialize librtbuf.
  *
  * Should be called before any other rtbuf function.
  *
@@ -143,7 +163,7 @@ extern f_rtbuf_unbind_cb g_rtbuf_unbind_cb;
 int librtbuf_init ();
 
 /**
- * Shutdown librtbuf.
+ * @brief Shutdown librtbuf.
  *
  * Should be called only after any other rtbuf function, when the
  * program shuts down.
@@ -155,9 +175,9 @@ void librtbuf_shutdown ();
 int rtbuf_err (const char *msg);
 
 /**
- * Create a new real time buffer.
+ * @brief Create a new real time buffer.
  *
- * The buffer is allocated in real time and ready to be bound.
+ * The buffer is allocated in real time and is ready to be bound.
  * This is equivalent to calling rtbuf_new_ptr with user_ptr
  * set to NULL.
  *
@@ -166,7 +186,18 @@ int rtbuf_err (const char *msg);
  */
 int rtbuf_new (s_rtbuf_proc *rp);
 
+/**
+ * @brief Create a new real time buffer.
+ *
+ * The buffer is allocated in real time and is ready to be bound.
+ * Its user pointer is set to user_ptr.
+ *
+ * @param rp The procedure for the new rtbuf.
+ * @param ptr The user pointer for the new rtbuf.
+ * @return The index of the new rtbuf in g_rtbuf.
+ */
 int   rtbuf_new_ptr (s_rtbuf_proc *rp, void *user_ptr);
+
 void  rtbuf_in_unbind (s_rtbuf *rtb, unsigned int in);
 void  rtbuf_out_unbind (s_rtbuf *rtb, unsigned int out);
 void  rtbuf_unbind_all (s_rtbuf *rtb);
diff --git a/librtbuf/rtbuf_lib.c b/librtbuf/rtbuf_lib.c
index 8161020..b8f7858 100644
--- a/librtbuf/rtbuf_lib.c
+++ b/librtbuf/rtbuf_lib.c
@@ -106,6 +106,8 @@ void rtbuf_lib_delete (s_rtbuf_lib *rl)
   if (rl->proc) {
     rtbuf_proc_delete(rl->proc);
     rl->proc = 0;
+    if (rl->unload)
+      rl->unload(rl);
   }
   data_delete(&g_rtbuf_lib_alloc, rl);
 }
@@ -170,10 +172,8 @@ s_rtbuf_lib * rtbuf_lib_load (const char *name)
   s_rtbuf_lib *lib = rtbuf_lib_new();
   s_rtbuf_lib_proc *proc;
   unsigned long *ver;
-  union {
-    void *ptr;
-    f_rtbuf_lib_init *init;
-  } init_ptr;
+  u_rtbuf_lib_init init;
+  u_rtbuf_lib_unload unload;
   if (!lib)
     return 0;
   rtbuf_lib_load_path(lib, name);
@@ -189,11 +189,13 @@ s_rtbuf_lib * rtbuf_lib_load (const char *name)
   }
   lib->name = symbol_intern(name);
   /* printf("lib_load name %s\n", lib->name); */
-  if ((init_ptr.ptr = dlsym(lib->lib, "rtbuf_lib_init")))
-    if (init_ptr.init(lib) < 0) {
+  if ((init.ptr = dlsym(lib->lib, "rtbuf_lib_init")))
+    if (init.fun(lib) < 0) {
       rtbuf_lib_delete(lib);
       return 0;
     }
+  unload.ptr = dlsym(lib->lib, "rtbuf_lib_init");
+  lib->unload = unload.fun;
   proc = dlsym(lib->lib, "rtbuf_lib_proc");
   lib->proc = rtbuf_proc_new();
   assert(lib->proc);