]> git.sesse.net Git - vlc/blobdiff - modules/video_output/xcb/events.c
XCB: flush when changing the cursor
[vlc] / modules / video_output / xcb / events.c
index 3a9b67dbffa5a71a5093e9cc7d1431aaacfda700..e68a35e7e73b704284ac08c3b3f610f984848b81 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
  ****************************************************************************/
 
 #include "xcb_vlc.h"
 
+/**
+ * Check for an error
+ */
+int CheckError (vout_display_t *vd, xcb_connection_t *conn,
+                const char *str, xcb_void_cookie_t ck)
+{
+    xcb_generic_error_t *err;
+
+    err = xcb_request_check (conn, ck);
+    if (err)
+    {
+        msg_Err (vd, "%s: X11 error %d", str, err->error_code);
+        free (err);
+        return VLC_EGENERIC;
+    }
+    return VLC_SUCCESS;
+}
+
+/**
+ * Gets the size of an X window.
+ */
+int GetWindowSize (struct vout_window_t *wnd, xcb_connection_t *conn,
+                   unsigned *restrict width, unsigned *restrict height)
+{
+    xcb_get_geometry_cookie_t ck = xcb_get_geometry (conn, wnd->handle.xid);
+    xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, ck, NULL);
+
+    if (!geo)
+        return -1;
+
+    *width = geo->width;
+    *height = geo->height;
+    free (geo);
+    return 0;
+}
+
+/**
+ * Create a blank cursor.
+ * Note that the pixmaps are leaked (until the X disconnection). Hence, this
+ * function should be called no more than once per X connection.
+ * @param conn XCB connection
+ * @param scr target XCB screen
+ */
+xcb_cursor_t CreateBlankCursor (xcb_connection_t *conn,
+                                const xcb_screen_t *scr)
+{
+    xcb_cursor_t cur = xcb_generate_id (conn);
+    xcb_pixmap_t pix = xcb_generate_id (conn);
+
+    xcb_create_pixmap (conn, 1, pix, scr->root, 1, 1);
+    xcb_create_cursor (conn, cur, pix, pix, 0, 0, 0, 0, 0, 0, 1, 1);
+    return cur;
+}
+
+/**
+ * (Try to) register to mouse events on a window if needed.
+ */
+void RegisterMouseEvents (vlc_object_t *obj, xcb_connection_t *conn,
+                          xcb_window_t wnd)
+{
+    /* Subscribe to parent window resize events */
+    uint32_t value = XCB_EVENT_MASK_POINTER_MOTION
+                   | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
+    xcb_change_window_attributes (conn, wnd, XCB_CW_EVENT_MASK, &value);
+    /* Try to subscribe to click events */
+    /* (only one X11 client can get them, so might not work) */
+    if (var_InheritBool (obj, "mouse-events"))
+    {
+        value |= XCB_EVENT_MASK_BUTTON_PRESS
+               | XCB_EVENT_MASK_BUTTON_RELEASE;
+        xcb_change_window_attributes (conn, wnd,
+                                      XCB_CW_EVENT_MASK, &value);
+    }
+}
+
 /* NOTE: we assume no other thread will be _setting_ our video output events
  * variables. Afterall, only this plugin is supposed to know when these occur.
   * Otherwise, we'd var_OrInteger() and var_NandInteger() functions...
@@ -52,11 +127,16 @@ static void HandleButtonRelease (vout_display_t *vd,
     vout_display_SendEventMouseReleased (vd, ev->detail - 1);
 }
 
-static void HandleMotionNotify (vout_display_t *vd,
+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 });
+    xcb_flush (conn);
+
     /* TODO it could be saved */
     vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
 
@@ -64,30 +144,32 @@ static void HandleMotionNotify (vout_display_t *vd,
         return;
 
     const int x = vd->source.i_x_offset +
-        (int64_t)(ev->event_x -0*place.x) * vd->source.i_visible_width / place.width;
+        (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 -0*place.y) * vd->source.i_visible_height/ place.height;
+        (int64_t)(ev->event_y - place.y) * vd->source.i_visible_height/ place.height;
 
-    /* TODO show the cursor ? */
-    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);
+    vout_display_SendEventMouseMoved (vd, x, y);
+}
+
+static void HandleVisibilityNotify (vout_display_t *vd, bool *visible,
+                                    const xcb_visibility_notify_event_t *ev)
+{
+    *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);
+    vout_display_SendEventDisplaySize (vd, ev->width, ev->height, vd->cfg->is_fullscreen);
 }
 
 /**
  * Process an X11 event.
  */
-static int ProcessEvent (vout_display_t *vd,
-                         xcb_window_t window, 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)
     {
@@ -100,18 +182,17 @@ static int ProcessEvent (vout_display_t *vd,
             break;
 
         case XCB_MOTION_NOTIFY:
-            HandleMotionNotify (vd, (xcb_motion_notify_event_t *)ev);
+            HandleMotionNotify (vd, conn, (xcb_motion_notify_event_t *)ev);
             break;
 
-        case XCB_CONFIGURE_NOTIFY:
-        {
-            xcb_configure_notify_event_t *cn =
-                (xcb_configure_notify_event_t *)ev;
+        case XCB_VISIBILITY_NOTIFY:
+            HandleVisibilityNotify (vd, visible,
+                                    (xcb_visibility_notify_event_t *)ev);
+            break;
 
-            assert (cn->window != window);
-            HandleParentStructure (vd, cn);
+        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:
@@ -132,12 +213,12 @@ static int ProcessEvent (vout_display_t *vd,
 /**
  * Process incoming X events.
  */
-int ManageEvent (vout_display_t *vd, xcb_connection_t *conn, xcb_window_t window)
+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, window, ev);
+        ProcessEvent (vd, conn, visible, ev);
 
     if (xcb_connection_has_error (conn))
     {
@@ -147,6 +228,3 @@ int ManageEvent (vout_display_t *vd, xcb_connection_t *conn, xcb_window_t window
 
     return VLC_SUCCESS;
 }
-
-
-