[vlc-devel] [PATCH v5 21/24] video_output: move the mouse events handling outside of the control pop

Steve Lhomme robux4 at ycbcr.xyz
Mon Aug 24 09:42:02 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 0a22b55eae1..e876b89d7ef 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;
 
@@ -392,34 +393,33 @@ 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 (cmd.mouse.b_double_click)
+    if (m.b_double_click)
         var_ToggleBool(vout, "fullscreen");
 }
 
@@ -1839,8 +1839,6 @@ static void *Thread(void *object)
     bool wait = false;
 
     for (;;) {
-        vout_control_cmd_t cmd;
-
         if (wait)
         {
             if (deadline != INVALID_DEADLINE)
@@ -1852,19 +1850,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);
 
@@ -1999,7 +2019,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);
@@ -2077,7 +2097,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