[vlc-devel] [PATCH] caca: use vlc_list instead of block_fifo_t

Thomas Guillem thomas at gllm.fr
Wed Sep 18 17:36:00 CEST 2019


I don't really know how to test it. The caca vout works only from a tty and key
events are not propagated in that case.
---
 modules/video_output/caca.c | 83 ++++++++++++++++++++++++++-----------
 1 file changed, 58 insertions(+), 25 deletions(-)

diff --git a/modules/video_output/caca.c b/modules/video_output/caca.c
index f95d985034..1b521240fe 100644
--- a/modules/video_output/caca.c
+++ b/modules/video_output/caca.c
@@ -33,7 +33,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <vlc_common.h>
-#include <vlc_block.h>
+#include <vlc_list.h>
 #include <vlc_plugin.h>
 #include <vlc_vout_display.h>
 #if !defined(_WIN32) && !defined(__APPLE__)
@@ -45,13 +45,22 @@
 
 #include <caca.h>
 
+struct vlc_caca_event
+{
+    int key;
+    struct vlc_list node;
+};
+
 /* */
 struct vout_display_sys_t {
     cucul_canvas_t *cv;
     caca_display_t *dp;
     cucul_dither_t *dither;
 
-    block_fifo_t *fifo;
+    struct vlc_list event_head;
+    vlc_mutex_t event_lock;
+    vlc_cond_t event_cond;
+
     vlc_thread_t thread;
     vout_window_t *window;
     vout_display_place_t place;
@@ -64,16 +73,29 @@ noreturn static void *VoutDisplayEventKeyDispatch(void *data)
 {
     vout_display_t *vd = data;
     vout_display_sys_t *sys = vd->sys;
-    block_fifo_t *fifo = sys->fifo;
 
     for (;;) {
-        block_t *event = block_FifoGet(fifo);
+        int key;
+        struct vlc_caca_event *event;
+
+        vlc_mutex_lock(&sys->event_lock);
+
+        mutex_cleanup_push(&sys->event_lock);
+
+        while ((event = vlc_list_first_entry_or_null(&sys->event_head,
+                                                     struct vlc_caca_event,
+                                                     node)) == NULL)
+            vlc_cond_wait(&sys->event_cond, &sys->event_lock);
+
+        key = event->key;
+        vlc_list_remove(&event->node);
+        free(event);
+
+        vlc_cleanup_pop();
+        vlc_mutex_unlock(&sys->event_lock);
 
         int cancel = vlc_savecancel();
-        int key;
 
-        memcpy(&key, event->p_buffer, sizeof (key));
-        block_Release(event);
         vout_window_ReportKeyPress(sys->window, key);
         vlc_restorecancel(cancel);
     }
@@ -81,11 +103,17 @@ noreturn static void *VoutDisplayEventKeyDispatch(void *data)
 
 static void VoutDisplayEventKey(vout_display_sys_t *sys, int key)
 {
-    block_t *event = block_Alloc(sizeof (key));
-    if (likely(event != NULL)) {
-        memcpy(event->p_buffer, &key, sizeof (key));
-        block_FifoPut(sys->fifo, event);
-    }
+    struct vlc_caca_event *event = malloc(sizeof(*event));
+    if (!event)
+        return;
+    event->key = key;
+
+    vlc_mutex_lock(&sys->event_lock);
+
+    vlc_list_append(&event->node, &sys->event_head);
+    vlc_cond_signal(&sys->event_cond);
+
+    vlc_mutex_unlock(&sys->event_lock);
 }
 
 /**
@@ -356,11 +384,16 @@ static void Close(vout_display_t *vd)
 {
     vout_display_sys_t *sys = vd->sys;
 
-    if (sys->fifo != NULL) {
-        vlc_cancel(sys->thread);
-        vlc_join(sys->thread, NULL);
-        block_FifoRelease(sys->fifo);
-    }
+    vlc_cancel(sys->thread);
+    vlc_join(sys->thread, NULL);
+
+    struct vlc_caca_event *event;
+    vlc_list_foreach(event, &sys->event_head, node)
+        free(event);
+
+    vlc_mutex_destroy(&sys->event_lock);
+    vlc_cond_destroy(&sys->event_cond);
+
     if (sys->dither)
         cucul_free_dither(sys->dither);
     caca_free_display(sys->dp);
@@ -470,15 +503,15 @@ static int Open(vout_display_t *vd, const vout_display_cfg_t *cfg,
         (title != NULL) ? title : VOUT_TITLE "(Colour AsCii Art)");
     free(title);
 
-    block_fifo_t *fifo = block_FifoNew();
-    if (likely(fifo != NULL)) {
-        sys->fifo = fifo;
+    vlc_list_init(&sys->event_head);
+    vlc_mutex_init(&sys->event_lock);
+    vlc_cond_init(&sys->event_cond);
 
-        if (vlc_clone(&sys->thread, VoutDisplayEventKeyDispatch, vd,
-                      VLC_THREAD_PRIORITY_LOW)) {
-            block_FifoRelease(fifo);
-            sys->fifo = NULL;
-        }
+    if (vlc_clone(&sys->thread, VoutDisplayEventKeyDispatch, vd,
+                  VLC_THREAD_PRIORITY_LOW)) {
+        vlc_mutex_destroy(&sys->event_lock);
+        vlc_cond_destroy(&sys->event_cond);
+        goto error;
     }
 
     sys->cursor_timeout = VLC_TICK_FROM_MS( var_InheritInteger(vd, "mouse-hide-timeout") );
-- 
2.20.1



More information about the vlc-devel mailing list