[vlc-devel] [PATCH v4 19/23] video_output: move the mouse events handling outside of the control pop

Steve Lhomme robux4 at ycbcr.xyz
Fri Aug 21 11:59:37 CEST 2020


We manage a local array of vlc_mouse_t.

We exited the loop if no command was found or we were asked to terminate.
---
 src/video_output/video_output.c | 62 ++++++++++++++++++++++-----------
 1 file changed, 41 insertions(+), 21 deletions(-)

diff --git a/src/video_output/video_output.c b/src/video_output/video_output.c
index 571e86f6248..2e2ec1e5642 100644
--- a/src/video_output/video_output.c
+++ b/src/video_output/video_output.c
@@ -123,6 +123,7 @@ typedef struct vout_thread_sys_t
     vlc_mutex_t     control_lock;
     vlc_cond_t      has_pictures;
     bool            terminate;
+    DECL_ARRAY(vlc_mouse_t) mouse_events;
     vout_control_t  control;
     vlc_thread_t    thread;
 
@@ -391,32 +392,31 @@ void vout_MouseState(vout_thread_t *vout, const vlc_mouse_t *mouse)
     vout_thread_sys_t *sys = VOUT_THREAD_TO_SYS(vout);
     assert(!sys->dummy);
     assert(mouse);
-    vout_control_cmd_t cmd;
-    vout_control_cmd_Init(&cmd, VOUT_CONTROL_MOUSE_STATE);
 
     /* Translate window coordinates to video coordinates */
+    vlc_mouse_t m;
     vlc_mutex_lock(&sys->display_lock);
     if (sys->display)
-        vout_display_TranslateMouseState(sys->display, &cmd.mouse, mouse);
+        vout_display_TranslateMouseState(sys->display, &m, mouse);
     else
-        cmd.mouse = *mouse;
+        m = *mouse;
 
-    bool moved = vlc_mouse_HasMoved(&sys->mouse, &cmd.mouse);
-    bool button = vlc_mouse_HasButton(&sys->mouse, &cmd.mouse);
-    bool left_pressed = button && vlc_mouse_HasPressed(&sys->mouse, &cmd.mouse, MOUSE_BUTTON_LEFT);
-    sys->mouse = cmd.mouse;
+    bool moved = vlc_mouse_HasMoved(&sys->mouse, &m);
+    bool button = vlc_mouse_HasButton(&sys->mouse, &m);
+    bool left_pressed = button && vlc_mouse_HasPressed(&sys->mouse, &m, MOUSE_BUTTON_LEFT);
+    sys->mouse = m;
     vlc_mutex_unlock(&sys->display_lock);
 
     vlc_mutex_lock(&sys->control_lock);
-    vout_control_Push(&sys->control, &cmd);
+    ARRAY_APPEND(sys->mouse_events, m);
     vlc_cond_signal(&sys->has_pictures);
     vlc_mutex_unlock(&sys->control_lock);
 
     if (moved)
-        var_SetCoords(vout, "mouse-moved", cmd.mouse.i_x, cmd.mouse.i_y);
+        var_SetCoords(vout, "mouse-moved", m.i_x, m.i_y);
 
     if (button) {
-        var_SetInteger(vout, "mouse-button-down", cmd.mouse.i_pressed);
+        var_SetInteger(vout, "mouse-button-down", m.i_pressed);
 
         if (left_pressed) {
             /* FIXME? */
@@ -427,7 +427,7 @@ void vout_MouseState(vout_thread_t *vout, const vlc_mouse_t *mouse)
         }
     }
 
-    if (cmd.mouse.b_double_click)
+    if (m.b_double_click)
         var_ToggleBool(vout, "fullscreen");
 }
 
@@ -1840,8 +1840,6 @@ static void *Thread(void *object)
     bool wait = false;
 
     for (;;) {
-        vout_control_cmd_t cmd;
-
         if (wait)
         {
             if (deadline != INVALID_DEADLINE)
@@ -1853,19 +1851,41 @@ static void *Thread(void *object)
         }
 
         vlc_mutex_lock(&sys->control_lock);
-        while (!vout_control_Pop(&sys->control, &cmd, deadline,
-                                 &sys->has_pictures, &sys->control_lock)) {
+        // loop to process pending mouse events
+        for (;;)
+        {
             if (sys->terminate)
             {
                 vlc_mutex_unlock(&sys->control_lock);
                 return NULL;
             }
 
-            switch(cmd.type) {
-                case VOUT_CONTROL_MOUSE_STATE:
-                    ThreadProcessMouseState(vout, &cmd.mouse);
+            if (sys->mouse_events.i_size <= 0)
+            {
+                // no more pending mouse events, we may wait for more
+                if (!wait)
                     break;
+                vlc_cond_timedwait(&sys->has_pictures, &sys->control_lock, deadline);
+
+                if (sys->mouse_events.i_size <= 0)
+                    // we timed out waiting or received a new picture and there's
+                    // no mouse event to process
+                    break;
+            }
+
+            if (sys->terminate)
+            {
+                vlc_mutex_unlock(&sys->control_lock);
+                return NULL;
             }
+
+            vlc_mouse_t event = ARRAY_VAL(sys->mouse_events, 0);
+            ARRAY_REMOVE(sys->mouse_events, 0);
+            vlc_mutex_unlock(&sys->control_lock);
+
+            ThreadProcessMouseState(vout, &event);
+
+            vlc_mutex_lock(&sys->control_lock);
         }
         vlc_mutex_unlock(&sys->control_lock);
 
@@ -2000,7 +2020,7 @@ void vout_Release(vout_thread_t *vout)
     assert(!sys->window_enabled);
     vout_display_window_Delete(sys->display_cfg.window);
 
-    vout_control_Clean(&sys->control);
+    ARRAY_RESET(sys->mouse_events);
 
     /* */
     vout_statistic_Clean(&sys->statistic);
@@ -2078,7 +2098,7 @@ vout_thread_t *vout_Create(vlc_object_t *object)
 
     vlc_mutex_init(&sys->control_lock);
     vlc_cond_init(&sys->has_pictures);
-    vout_control_Init(&sys->control);
+    ARRAY_INIT(sys->mouse_events);
     sys->terminate = false;
 
     sys->title.show     = var_InheritBool(vout, "video-title-show");
-- 
2.26.2



More information about the vlc-devel mailing list