Commit 3f001fc34209c272d9e0640d4645e50e2cfd3a42

Thomas de Grivel 2020-05-14T11:56:03

link rtbuf-gtk to cli start stop and run thread

diff --git a/cli/include/rtbuf/cli.h b/cli/include/rtbuf/cli.h
index 0624aad..0cc8b28 100644
--- a/cli/include/rtbuf/cli.h
+++ b/cli/include/rtbuf/cli.h
@@ -23,4 +23,7 @@ int load (const char *path);
 int repl ();
 void rtbuf_cli_args (int argc, char *argv[]);
 
+int rtbuf_cli_start ();
+int rtbuf_cli_stop ();
+
 #endif /* RTBUF_CLI_H */
diff --git a/cli/rtbuf_cli.c b/cli/rtbuf_cli.c
index 5e25329..6558d75 100644
--- a/cli/rtbuf_cli.c
+++ b/cli/rtbuf_cli.c
@@ -220,11 +220,8 @@ void * rtbuf_cli_thread_proc (void *arg)
   return 0;
 }
 
-int rtbuf_cli_start (int argc, const char *argv[])
+int rtbuf_cli_start ()
 {
-  (void) argv;
-  if (argc != 0)
-    return rtbuf_err("usage: start");
   if (!g_rtbuf_run && g_rtbuf_cli_thread) {
     if (pthread_join(g_rtbuf_cli_thread, 0))
       return rtbuf_err("pthread_join failed");
@@ -238,11 +235,16 @@ int rtbuf_cli_start (int argc, const char *argv[])
   return 0;
 }
 
-int rtbuf_cli_stop (int argc, const char *argv[])
+int rtbuf_cli_start_ (int argc, const char *argv[])
 {
   (void) argv;
   if (argc != 0)
-    return rtbuf_err("usage: stop");
+    return rtbuf_err("usage: start");
+  return rtbuf_cli_start();
+}
+
+int rtbuf_cli_stop ()
+{
   if (g_rtbuf_run)
     g_rtbuf_run = 0;
   if (g_rtbuf_cli_thread) {
@@ -253,6 +255,14 @@ int rtbuf_cli_stop (int argc, const char *argv[])
   return 0;
 }
 
+int rtbuf_cli_stop_ (int argc, const char *argv[])
+{
+  (void) argv;
+  if (argc != 0)
+    return rtbuf_err("usage: stop");
+  return rtbuf_cli_stop();
+}
+
 int rtbuf_cli_help (int argc, const char *argv[])
 {
   (void) argc;
@@ -278,7 +288,7 @@ int rtbuf_cli_exit (int argc, const char *argv[])
 {
   (void) argc;
   (void) argv;
-  rtbuf_cli_stop(0, 0);
+  rtbuf_cli_stop();
   close(0);
   exit(0);
   return 0;
@@ -296,8 +306,8 @@ s_cli_function rtbuf_cli_functions[] = {
   { "unbind",  1, rtbuf_cli_unbind },
   { "unbind",  2, rtbuf_cli_unbind },
   { "let",    -1, rtbuf_cli_let },
-  { "start",   0, rtbuf_cli_start },
-  { "stop",    0, rtbuf_cli_stop },
+  { "start",   0, rtbuf_cli_start_ },
+  { "stop",    0, rtbuf_cli_stop_ },
   { "h",       0, rtbuf_cli_help },
   { "help",    0, rtbuf_cli_help },
   { "exit",    0, rtbuf_cli_exit },
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index 347ceee..455456e 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -1,6 +1,6 @@
 bin_PROGRAMS = rtbuf-gtk
 rtbuf_gtk_CFLAGS = ${GTK3_CFLAGS}
-rtbuf_gtk_LDADD = ${GTK3_LIBS} ../librtbuf/librtbuf.la
+rtbuf_gtk_LDADD = ${GTK3_LIBS} ../librtbuf/librtbuf.la ../cli/librtbuf_cli.la
 rtbuf_gtk_SOURCES = \
 	list.c \
 	queue.c \
diff --git a/gtk/rtbuf_gtk.c b/gtk/rtbuf_gtk.c
index 89c7ead..a208c45 100644
--- a/gtk/rtbuf_gtk.c
+++ b/gtk/rtbuf_gtk.c
@@ -21,6 +21,7 @@
 #include <rtbuf/rtbuf.h>
 #include <rtbuf/lib.h>
 #include <rtbuf/var.h>
+#include <rtbuf/cli.h>
 #include "rtbuf_gtk.h"
 #include "rtbuf_gtk_library.h"
 #include "rtbuf_input_widget.h"
@@ -378,54 +379,12 @@ gboolean rtbuf_gtk_modular_motion (GtkWidget       *widget,
   return FALSE;
 }
 
-void * rtbuf_gtk_thread_proc (void *arg)
-{
-  (void) arg;
-  printf("rtbuf thread: start\n");
-  if (!rtbuf_start())
-    g_rtbuf_run = 1;
-  while (g_rtbuf_run) {
-    if (rtbuf_run())
-      g_rtbuf_run = 0;
-  }
-  printf("rtbuf thread: stop\n");
-  rtbuf_stop();
-  return 0;
-}
-
-int rtbuf_gtk_start ()
-{
-  if (!g_rtbuf_run && g_rtbuf_gtk_thread) {
-    if (pthread_join(g_rtbuf_gtk_thread, 0))
-      return rtbuf_err("pthread_join failed");
-    g_rtbuf_gtk_thread = 0;
-  }
-  if (!g_rtbuf_gtk_thread) {
-    if (pthread_create(&g_rtbuf_gtk_thread, 0, &rtbuf_gtk_thread_proc,
-                       0))
-      return rtbuf_err("pthread_create failed");
-  }
-  return 0;
-}
-
-int rtbuf_gtk_stop ()
-{
-  if (g_rtbuf_run)
-    g_rtbuf_run = 0;
-  if (g_rtbuf_gtk_thread) {
-    if (pthread_join(g_rtbuf_gtk_thread, 0))
-      return rtbuf_err("pthread_join failed");
-    g_rtbuf_gtk_thread = 0;
-  }
-  return 0;
-}
-
 void rtbuf_gtk_modular_start (GtkWidget *widget, gpointer data)
 {
   (void) widget;
   (void) data;
   printf("rtbuf> start\n");
-  rtbuf_gtk_start();
+  rtbuf_cli_start();
 }
 
 void rtbuf_gtk_modular_stop (GtkWidget *widget, gpointer data)
@@ -433,7 +392,7 @@ void rtbuf_gtk_modular_stop (GtkWidget *widget, gpointer data)
   (void) widget;
   (void) data;
   printf("rtbuf> stop\n");
-  rtbuf_gtk_stop();
+  rtbuf_cli_stop();
 }
 
 void rtbuf_gtk_modular_toolbar ()