]> git.sesse.net Git - vlc/blobdiff - modules/video_output/drawable.c
sftp: change item b_net
[vlc] / modules / video_output / drawable.c
index b041adaf3dda284cc67626bd2a03abba9adaaa59..9703f975105f63176a30b7e0831aac9fe6932544 100644 (file)
@@ -5,20 +5,20 @@
 /*****************************************************************************
  * Copyright © 2009 Rémi Denis-Courmont
  *
- * 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
- * of the License, or (at your option) any later version.
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
+ * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Lesser 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
- ****************************************************************************/
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
-#include <vlc_vout.h>
-#include <vlc_window.h>
+#include <vlc_vout_window.h>
 
-static int  OpenXID (vlc_object_t *);
-static int  OpenHWND (vlc_object_t *);
-static void Close (vlc_object_t *);
+#define HWND_TEXT N_("Window handle (HWND)")
+#define HWND_LONGTEXT N_( \
+    "Video will be embedded in this pre-existing window. " \
+    "If zero, a new window will be created.")
 
-#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. " \
-    "This is the X identifier of that window (0 means none).")
+static int  Open (vout_window_t *, const vout_window_cfg_t *);
+static void Close(vout_window_t *);
 
 /*
  * Module descriptor
  */
 vlc_module_begin ()
     set_shortname (N_("Drawable"))
-    set_description (N_("Embedded window video"))
+    set_description (N_("Embedded window video"))
     set_category (CAT_VIDEO)
     set_subcategory (SUBCAT_VIDEO_VOUT)
-    set_capability ("xwindow", 70)
-    set_callbacks (OpenXID, Close)
-    add_integer ("drawable-xid", 0, NULL, XID_TEXT, XID_LONGTEXT, true)
-        change_unsaveable ()
-        /*change_integer_range (0, 0xffffffff)*/
-
-    add_submodule ()
-        set_description (N_("Embedded Windows video"))
-        set_capability ("hwnd", 70)
-        set_callbacks (OpenHWND, Close)
+    set_capability ("vout window", 70)
+    set_callbacks (Open, Close)
+    add_shortcut ("embed-hwnd")
 
+    add_integer ("drawable-hwnd", 0, HWND_TEXT, HWND_LONGTEXT, true)
+        change_volatile ()
 vlc_module_end ()
 
 static int Control (vout_window_t *, int, va_list);
 
+/* Keep a list of busy drawables, so we don't overlap videos if there are
+ * more than one video track in the stream. */
+static vlc_mutex_t serializer = VLC_STATIC_MUTEX;
+static uintptr_t *used = NULL;
+
 /**
  * Find the drawable set by libvlc application.
  */
-static int Open (vlc_object_t *obj, const char *varname, bool ptr)
+static int Open (vout_window_t *wnd, const vout_window_cfg_t *cfg)
 {
-    static vlc_mutex_t serializer = VLC_STATIC_MUTEX;
-    vout_window_t *wnd = (vout_window_t *)obj;
-    vlc_value_t val, globval;
+    if (cfg->type != VOUT_WINDOW_TYPE_INVALID
+     && cfg->type != VOUT_WINDOW_TYPE_HWND)
+        return VLC_EGENERIC;
+
+    uintptr_t val = var_InheritInteger (wnd, "drawable-hwnd");
+    if (val == 0)
+        return VLC_EGENERIC;
 
-    if (var_Create (obj->p_libvlc, "drawable-busy", VLC_VAR_BOOL)
-     || var_Create (obj, varname, VLC_VAR_DOINHERIT
-                                  | (ptr ? VLC_VAR_ADDRESS : VLC_VAR_INTEGER)))
-        return VLC_ENOMEM;
-    var_Get (obj, varname, &val);
+    uintptr_t *tab;
+    size_t n = 0;
 
     vlc_mutex_lock (&serializer);
-    /* Note: We cannot simply clear the drawable variable.
-     * It would break libvlc_video_get_parent(). */
-    var_Get (obj->p_libvlc, varname, &globval);
-    if (ptr ? (val.p_address == globval.p_address)
-            : (val.i_int == globval.i_int))
+    if (used != NULL)
+        for (/*n = 0*/; used[n]; n++)
+            if (used[n] == val)
+            {
+                msg_Warn (wnd, "HWND 0x%zX is busy", val);
+                val = 0;
+                goto skip;
+            }
+
+    tab = realloc (used, sizeof (*used) * (n + 2));
+    if (likely(tab != NULL))
     {
-        if (var_GetBool (obj->p_libvlc, "drawable-busy"))
-        {   /* LibVLC-wide drawable already in use */
-            if (ptr)
-                val.p_address = NULL;
-            else
-                val.i_int = 0;
-        }
-        else
-            var_SetBool (obj->p_libvlc, "drawable-busy", true);
+        used = tab;
+        used[n] = val;
+        used[n + 1] = 0;
     }
-    /* If we got a drawable _not_ from the root object (from the input?),
-     * We assume it is not busy. This is a bug. */
+    else
+        val = 0;
+skip:
     vlc_mutex_unlock (&serializer);
 
-    var_Destroy (obj, varname);
-
-    if (ptr ? (val.p_address == NULL) : (val.i_int == 0))
-    {
-        var_Destroy (obj->p_libvlc, "drawable-busy");
+    if (val == 0)
         return VLC_EGENERIC;
-    }
-
-    if (ptr)
-        wnd->handle.hwnd = val.p_address;
-    else
-        wnd->handle.xid = val.i_int;
-
-    /* FIXME: check that X server matches --x11-display (if specified) */
-    /* FIXME: get window size (in platform-dependent ways) */
 
+    wnd->type = VOUT_WINDOW_TYPE_HWND;
+    wnd->handle.hwnd = (void *)val;
     wnd->control = Control;
+    wnd->sys = (void *)val;
     return VLC_SUCCESS;
 }
 
-static int  OpenXID (vlc_object_t *obj)
-{
-    return Open (obj, "drawable-xid", false);
-}
-
-static int  OpenHWND (vlc_object_t *obj)
-{
-    return Open (obj, "drawable-hwnd", true);
-}
-
-
 /**
  * Release the drawable.
  */
-static void Close (vlc_object_t *obj)
+static void Close (vout_window_t *wnd)
 {
-    /* This is atomic with regards to var_GetBool() in Open(): */
-    var_SetBool (obj->p_libvlc, "drawable-busy", false);
+    uintptr_t val = (uintptr_t)wnd->sys;
+    size_t n = 0;
+
+    /* Remove this drawable from the list of busy ones */
+    vlc_mutex_lock (&serializer);
+    assert (used != NULL);
+    while (used[n] != val)
+    {
+        assert (used[n]);
+        n++;
+    }
+    do
+        used[n] = used[n + 1];
+    while (used[++n] != 0);
 
-    /* Variables are reference-counted... */
-    var_Destroy (obj->p_libvlc, "drawable-busy");
+    if (n == 0)
+    {
+         free (used);
+         used = NULL;
+    }
+    vlc_mutex_unlock (&serializer);
 }
 
 
 static int Control (vout_window_t *wnd, int query, va_list ap)
 {
+    VLC_UNUSED( ap );
+
     switch (query)
     {
-        case VOUT_SET_SIZE: /* not allowed */
-        case VOUT_SET_STAY_ON_TOP: /* not allowed either, would be ugly */
+        case VOUT_WINDOW_SET_SIZE:   /* not allowed */
+        case VOUT_WINDOW_SET_STATE: /* not allowed either, would be ugly */
+            return VLC_EGENERIC;
+        default:
+            msg_Warn (wnd, "unsupported control query %d", query);
             return VLC_EGENERIC;
     }
-
-    msg_Warn (wnd, "unsupported control query %d", query);
-    return VLC_EGENERIC;
 }
-