]> git.sesse.net Git - vlc/blobdiff - modules/video_output/xcb/window.c
vout_window_t: simplify via anynomous union
[vlc] / modules / video_output / xcb / window.c
index 405cefc5599e8bd9ba72110f6e34c5de1bb94b3b..f22db1d353f81f65db403c8147fa1be1107dfc49 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
  ****************************************************************************/
@@ -40,11 +40,6 @@ typedef xcb_atom_t Atom;
 
 #include "xcb_vlc.h"
 
-#define DISPLAY_TEXT N_("X11 display")
-#define DISPLAY_LONGTEXT N_( \
-    "X11 hardware display to use. By default VLC will " \
-    "use the value of the DISPLAY environment variable.")
-
 #define XID_TEXT N_("ID of the video output X window")
 #define XID_LONGTEXT N_( \
     "VLC can embed its video output in an existing X11 window. " \
@@ -66,8 +61,6 @@ vlc_module_begin ()
     set_capability ("vout window xid", 10)
     set_callbacks (Open, Close)
 
-    add_string ("x11-display", NULL, NULL,
-                DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
     /* Obsolete since 1.1.0: */
     add_obsolete_bool ("x11-altfullscreen")
     add_obsolete_bool ("xvideo-altfullscreen")
@@ -90,6 +83,8 @@ vlc_module_end ()
 static int Control (vout_window_t *, int, va_list ap);
 static void *Thread (void *);
 
+#define MATCHBOX_HACK 1 /* Matchbox focus hack */
+
 struct vout_window_sys_t
 {
     xcb_connection_t *conn;
@@ -100,6 +95,9 @@ struct vout_window_sys_t
     xcb_atom_t wm_state;
     xcb_atom_t wm_state_above;
     xcb_atom_t wm_state_fullscreen;
+#ifdef MATCHBOX_HACK
+    xcb_atom_t mb_current_app_window;
+#endif
 };
 
 /** Set an X window property from a nul-terminated string */
@@ -116,7 +114,20 @@ static inline
 void set_ascii_prop (xcb_connection_t *conn, xcb_window_t window,
                      xcb_atom_t atom, const char *value)
 {
-    set_string (conn, window, atom, XA_STRING, value);
+    set_string (conn, window, XA_STRING, atom, value);
+}
+
+static inline
+void set_wm_hints (xcb_connection_t *conn, xcb_window_t window)
+{
+    static const uint32_t wm_hints[8] = {
+        3, /* flags: Input, Initial state */
+        1, /* input: True */
+        1, /* initial state: Normal */
+        0, 0, 0, 0, 0, /* Icon */
+    };
+    xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, XA_WM_HINTS,
+                         XA_WM_HINTS, 32, 8, wm_hints);
 }
 
 /** Set the Window ICCCM client machine property */
@@ -172,10 +183,19 @@ static void CacheAtoms (vout_window_sys_t *p_sys)
     wm_state_ck = intern_string (conn, "_NET_WM_STATE");
     wm_state_above_ck = intern_string (conn, "_NET_WM_STATE_ABOVE");
     wm_state_fs_ck = intern_string (conn, "_NET_WM_STATE_FULLSCREEN");
+#ifdef MATCHBOX_HACK
+    xcb_intern_atom_cookie_t mb_current_app_window;
+    mb_current_app_window = xcb_intern_atom (conn, true,
+                                             strlen ("_MB_CURRENT_APP_WINDOW"),
+                                             "_MB_CURRENT_APP_WINDOW");
+#endif
 
     p_sys->wm_state = get_atom (conn, wm_state_ck);
     p_sys->wm_state_above = get_atom (conn, wm_state_above_ck);
     p_sys->wm_state_fullscreen = get_atom (conn, wm_state_fs_ck);
+#ifdef MATCHBOX_HACK
+    p_sys->mb_current_app_window = get_atom (conn, mb_current_app_window);
+#endif
 }
 
 /**
@@ -241,12 +261,15 @@ static int Open (vlc_object_t *obj)
         goto error;
     }
 
-    wnd->handle.xid = window;
+    wnd->xid = window;
     wnd->control = Control;
     wnd->sys = p_sys;
 
     p_sys->conn = conn;
-    p_sys->keys = CreateKeyHandler (obj, conn);
+    if (var_CreateGetBool (obj, "keyboard-events"))
+        p_sys->keys = CreateKeyHandler (obj, conn);
+    else
+        p_sys->keys = NULL;
     p_sys->root = scr->root;
 
     /* ICCCM
@@ -256,6 +279,7 @@ static int Open (vlc_object_t *obj)
                   vlc_pgettext ("ASCII", "VLC media player"));
     set_ascii_prop (conn, window, XA_WM_ICON_NAME,
                     vlc_pgettext ("ASCII", "VLC"));
+    set_wm_hints (conn, window);
     xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, XA_WM_CLASS,
                          XA_STRING, 8, 8, "vlc\0Vlc");
     set_hostname_prop (conn, window);
@@ -267,6 +291,8 @@ static int Open (vlc_object_t *obj)
         = intern_string (conn, "_NET_WM_NAME");
     xcb_intern_atom_cookie_t net_wm_icon_name_ck
         = intern_string (conn, "_NET_WM_ICON_NAME");
+    xcb_intern_atom_cookie_t wm_window_role_ck
+        = intern_string (conn, "WM_WINDOW_ROLE");
 
     xcb_atom_t utf8 = get_atom (conn, utf8_string_ck);
 
@@ -283,11 +309,22 @@ static int Open (vlc_object_t *obj)
     xcb_atom_t net_wm_icon_name = get_atom (conn, net_wm_icon_name_ck);
     set_string (conn, window, utf8, net_wm_icon_name, _("VLC"));
 
-    /* Make the window visible */
-    xcb_map_window (conn, window);
+    xcb_atom_t wm_window_role = get_atom (conn, wm_window_role_ck);
+    set_ascii_prop (conn, window, wm_window_role, "vlc-video");
 
     /* Cache any EWMH atom we may need later */
     CacheAtoms (p_sys);
+#ifdef MATCHBOX_HACK
+    if (p_sys->mb_current_app_window)
+    {
+        uint32_t value = XCB_EVENT_MASK_PROPERTY_CHANGE;
+        xcb_change_window_attributes (conn, scr->root,
+                                      XCB_CW_EVENT_MASK, &value);
+    }
+#endif
+
+    /* Make the window visible */
+    xcb_map_window (conn, window);
 
     /* Create the event thread. It will dequeue all events, so any checked
      * request from this thread must be completed at this point. */
@@ -295,8 +332,12 @@ static int Open (vlc_object_t *obj)
      && vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
         DestroyKeyHandler (p_sys->keys);
 
+#ifdef MATCHBOX_HACK
+    if (p_sys->mb_current_app_window)
+        xcb_set_input_focus (p_sys->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
+                             wnd->xid, XCB_CURRENT_TIME);
+#endif
     xcb_flush (conn); /* Make sure map_window is sent (should be useless) */
-
     return VLC_SUCCESS;
 
 error:
@@ -349,7 +390,35 @@ static void *Thread (void *data)
         {
             if (ProcessKeyEvent (p_sys->keys, ev) == 0)
                 continue;
-            msg_Dbg (wnd, "unhandled event: %"PRIu8, ev->response_type);
+#ifdef MATCHBOX_HACK
+            if (p_sys->mb_current_app_window
+             && (ev->response_type & 0x7f) == XCB_PROPERTY_NOTIFY)
+            {
+                const xcb_property_notify_event_t *pne =
+                    (xcb_property_notify_event_t *)ev;
+                if (pne->atom == p_sys->mb_current_app_window
+                 && pne->state == XCB_PROPERTY_NEW_VALUE)
+                {
+                    xcb_get_property_reply_t *r =
+                        xcb_get_property_reply (conn,
+                            xcb_get_property (conn, 0, pne->window, pne->atom,
+                                              XA_WINDOW, 0, 4), NULL);
+                    if (r != NULL
+                     && !memcmp (xcb_get_property_value (r), &wnd->xid,
+                                 4))
+                    {
+                        msg_Dbg (wnd, "asking Matchbox for input focus");
+                        xcb_set_input_focus (conn,
+                                             XCB_INPUT_FOCUS_POINTER_ROOT,
+                                             wnd->xid, pne->time);
+                        xcb_flush (conn);
+                    }
+                    free (r);
+                }
+            }
+            else
+#endif
+                msg_Dbg (wnd, "unhandled event: %"PRIu8, ev->response_type);
             free (ev);
         }
         vlc_restorecancel (canc);
@@ -371,7 +440,7 @@ static void set_wm_state (vout_window_t *wnd, bool on, xcb_atom_t state)
     xcb_client_message_event_t ev = {
          .response_type = XCB_CLIENT_MESSAGE,
          .format = 32,
-         .window = wnd->handle.xid,
+         .window = wnd->xid,
          .type = sys->wm_state,
     };
 
@@ -401,7 +470,7 @@ static int Control (vout_window_t *wnd, int cmd, va_list ap)
             unsigned height = va_arg (ap, unsigned);
             const uint32_t values[] = { width, height, };
 
-            xcb_configure_window (conn, wnd->handle.xid,
+            xcb_configure_window (conn, wnd->xid,
                                   XCB_CONFIG_WINDOW_WIDTH |
                                   XCB_CONFIG_WINDOW_HEIGHT, values);
             break;
@@ -430,7 +499,6 @@ static vlc_mutex_t serializer = VLC_STATIC_MUTEX;
 /** Acquire a drawable */
 static int AcquireDrawable (vlc_object_t *obj, xcb_window_t window)
 {
-    vlc_value_t val;
     xcb_window_t *used;
     size_t n = 0;
 
@@ -440,8 +508,7 @@ static int AcquireDrawable (vlc_object_t *obj, xcb_window_t window)
     /* Keep a list of busy drawables, so we don't overlap videos if there are
      * more than one video track in the stream. */
     vlc_mutex_lock (&serializer);
-    var_Get (VLC_OBJECT (obj->p_libvlc), "xid-in-use", &val);
-    used = val.p_address;
+    used = var_GetAddress (obj->p_libvlc, "xid-in-use");
     if (used != NULL)
     {
         while (used[n])
@@ -457,8 +524,7 @@ static int AcquireDrawable (vlc_object_t *obj, xcb_window_t window)
     {
         used[n] = window;
         used[n + 1] = 0;
-        val.p_address = used;
-        var_Set (obj->p_libvlc, "xid-in-use", val);
+        var_SetAddress (obj->p_libvlc, "xid-in-use", used);
     }
     else
     {
@@ -474,13 +540,11 @@ skip:
 /** Remove this drawable from the list of busy ones */
 static void ReleaseDrawable (vlc_object_t *obj, xcb_window_t window)
 {
-    vlc_value_t val;
     xcb_window_t *used;
     size_t n = 0;
 
     vlc_mutex_lock (&serializer);
-    var_Get (VLC_OBJECT (obj->p_libvlc), "xid-in-use", &val);
-    used = val.p_address;
+    used = var_GetAddress (obj->p_libvlc, "xid-in-use");
     assert (used);
     while (used[n] != window)
     {
@@ -521,7 +585,7 @@ static int EmOpen (vlc_object_t *obj)
     if (p_sys == NULL || xcb_connection_has_error (conn))
         goto error;
 
-    wnd->handle.xid = window;
+    wnd->xid = window;
     wnd->control = Control;
     wnd->sys = p_sys;
 
@@ -537,7 +601,7 @@ static int EmOpen (vlc_object_t *obj)
     p_sys->root = geo->root;
     free (geo);
 
-    if (var_CreateGetInteger (obj, "vout-event") != 3) /* FIXME: <- cleanup */
+    if (var_CreateGetBool (obj, "keyboard-events"))
     {
         p_sys->keys = CreateKeyHandler (obj, conn);
         if (p_sys->keys != NULL)
@@ -569,7 +633,7 @@ error:
 static void EmClose (vlc_object_t *obj)
 {
     vout_window_t *wnd = (vout_window_t *)obj;
-    xcb_window_t window = wnd->handle.xid;
+    xcb_window_t window = wnd->xid;
 
     Close (obj);
     ReleaseDrawable (obj, window);