]> git.sesse.net Git - vlc/blobdiff - modules/video_output/xcb/events.c
XCB: unset the cursor when we detect pointer motion
[vlc] / modules / video_output / xcb / events.c
index 89c62732f9a0ec372340905b0c99bd05fdfeb07e..dd66eabe28c247f9f6a9e4d6b3645158d5a732d9 100644 (file)
@@ -7,7 +7,7 @@
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2.0
+ * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
@@ -15,7 +15,7 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU Lesser General Public
+ * You should have received a copy of the GNU General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  ****************************************************************************/
 #endif
 
 #include <inttypes.h>
+#include <assert.h>
 
 #include <xcb/xcb.h>
 
 #include <vlc_common.h>
-#include <vlc_vout.h>
+#include <vlc_vout_display.h>
 
 #include "xcb_vlc.h"
 
   * Otherwise, we'd var_OrInteger() and var_NandInteger() functions...
  */
 
-static void HandleButtonPress (vout_thread_t *vout,
-                               xcb_button_press_event_t *ev)
+/* FIXME we assume direct mapping between XCB and VLC */
+static void HandleButtonPress (vout_display_t *vd,
+                               const xcb_button_press_event_t *ev)
 {
-    unsigned buttons = var_GetInteger (vout, "mouse-button-down");
-    buttons |= (1 << (ev->detail - 1));
-    var_SetInteger (vout, "mouse-button-down", buttons);
+    vout_display_SendEventMousePressed (vd, ev->detail - 1);
 }
 
-static void HandleButtonRelease (vout_thread_t *vout,
-                                 xcb_button_release_event_t *ev)
+static void HandleButtonRelease (vout_display_t *vd,
+                                 const xcb_button_release_event_t *ev)
 {
-    unsigned buttons = var_GetInteger (vout, "mouse-button-down");
-    buttons &= ~(1 << (ev->detail - 1));
-    var_SetInteger (vout, "mouse-button-down", buttons);
+    vout_display_SendEventMouseReleased (vd, ev->detail - 1);
+}
+
+static void HandleMotionNotify (vout_display_t *vd, xcb_connection_t *conn,
+                                const xcb_motion_notify_event_t *ev)
+{
+    vout_display_place_t place;
+
+    /* show the default cursor */
+    xcb_change_window_attributes (conn, ev->event, XCB_CW_CURSOR,
+                                  &(uint32_t) { XCB_CURSOR_NONE });
 
-    if (ev->detail == 1) /* left mouse button */
-        var_SetBool (vout, "mouse-clicked", true);
+    /* TODO it could be saved */
+    vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
+
+    if (place.width <= 0 || place.height <= 0)
+        return;
+
+    const int x = vd->source.i_x_offset +
+        (int64_t)(ev->event_x - place.x) * vd->source.i_visible_width / place.width;
+    const int y = vd->source.i_y_offset +
+        (int64_t)(ev->event_y - place.y) * vd->source.i_visible_height/ place.height;
+
+    if (x >= vd->source.i_x_offset && x < vd->source.i_x_offset + vd->source.i_visible_width &&
+        y >= vd->source.i_y_offset && y < vd->source.i_y_offset + vd->source.i_visible_height)
+        vout_display_SendEventMouseMoved (vd, x, y);
 }
 
-static void HandleMotionNotify (vout_thread_t *vout,
-                                xcb_motion_notify_event_t *ev)
+static void HandleVisibilityNotify (vout_display_t *vd, bool *visible,
+                                    const xcb_visibility_notify_event_t *ev)
 {
-    unsigned x, y, width, height;
-    int v;
-
-    vout_PlacePicture (vout, vout->output.i_width, vout->output.i_height,
-                       &x, &y, &width, &height);
-    v = vout->fmt_in.i_x_offset
-        + ((ev->event_x - x) * vout->fmt_in.i_visible_width / width);
-    if (v < 0)
-        v = 0; /* to the left of the picture */
-    else if ((unsigned)v > vout->fmt_in.i_width)
-        v = vout->fmt_in.i_width; /* to the right of the picture */
-    var_SetInteger (vout, "mouse-x", v);
-
-    v = vout->fmt_in.i_y_offset
-        + ((ev->event_y - y) * vout->fmt_in.i_visible_height / height);
-    if (v < 0)
-        v = 0; /* above the picture */
-    else if ((unsigned)v > vout->fmt_in.i_height)
-        v = vout->fmt_in.i_height; /* below the picture */
-    var_SetInteger (vout, "mouse-y", v);
+    *visible = ev->state != XCB_VISIBILITY_FULLY_OBSCURED;
+    msg_Dbg (vd, "display is %svisible", *visible ? "" : "not ");
+}
+
+static void
+HandleParentStructure (vout_display_t *vd,
+                       const xcb_configure_notify_event_t *ev)
+{
+    if (ev->width  != vd->cfg->display.width ||
+        ev->height != vd->cfg->display.height)
+        vout_display_SendEventDisplaySize (vd, ev->width, ev->height, vd->cfg->is_fullscreen);
 }
 
 /**
  * Process an X11 event.
  */
-int ProcessEvent (vout_thread_t *vout, xcb_generic_event_t *ev)
+static int ProcessEvent (vout_display_t *vd, xcb_connection_t *conn,
+                         bool *visible, xcb_generic_event_t *ev)
 {
     switch (ev->response_type & 0x7f)
     {
         case XCB_BUTTON_PRESS:
-            HandleButtonPress (vout, (xcb_button_press_event_t *)ev);
+            HandleButtonPress (vd, (xcb_button_press_event_t *)ev);
             break;
 
         case XCB_BUTTON_RELEASE:
-            HandleButtonRelease (vout, (xcb_button_release_event_t *)ev);
+            HandleButtonRelease (vd, (xcb_button_release_event_t *)ev);
             break;
 
         case XCB_MOTION_NOTIFY:
-            HandleMotionNotify (vout, (xcb_motion_notify_event_t *)ev);
+            HandleMotionNotify (vd, conn, (xcb_motion_notify_event_t *)ev);
+            break;
+
+        case XCB_VISIBILITY_NOTIFY:
+            HandleVisibilityNotify (vd, visible,
+                                    (xcb_visibility_notify_event_t *)ev);
+            break;
+
+        case XCB_CONFIGURE_NOTIFY:
+            HandleParentStructure (vd, (xcb_configure_notify_event_t *)ev);
+            break;
+
+        /* FIXME I am not sure it is the right one */
+        case XCB_DESTROY_NOTIFY:
+            vout_display_SendEventClose (vd);
+            break;
+
+        case XCB_MAPPING_NOTIFY:
             break;
 
         default:
-            msg_Dbg (vout, "unhandled event %02x", (unsigned)ev->response_type);
+            msg_Dbg (vd, "unhandled event %"PRIu8, ev->response_type);
     }
 
     free (ev);
     return VLC_SUCCESS;
 }
+
+/**
+ * Process incoming X events.
+ */
+int ManageEvent (vout_display_t *vd, xcb_connection_t *conn, bool *visible)
+{
+    xcb_generic_event_t *ev;
+
+    while ((ev = xcb_poll_for_event (conn)) != NULL)
+        ProcessEvent (vd, conn, visible, ev);
+
+    if (xcb_connection_has_error (conn))
+    {
+        msg_Err (vd, "X server failure");
+        return VLC_EGENERIC;
+    }
+
+    return VLC_SUCCESS;
+}