]> git.sesse.net Git - vlc/blob - modules/video_output/drawable.c
Drawable: check properly for busy drawable
[vlc] / modules / video_output / drawable.c
1 /**
2  * @file drawable.c
3  * @brief Legacy monolithic LibVLC video window provider
4  */
5 /*****************************************************************************
6  * Copyright © 2009 Rémi Denis-Courmont
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2.0
11  * of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdarg.h>
28 #include <assert.h>
29
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_vout.h>
33 #include <vlc_window.h>
34
35 static int  OpenXID (vlc_object_t *);
36 static int  OpenHWND (vlc_object_t *);
37 static void Close (vlc_object_t *);
38
39 #define XID_TEXT N_("ID of the video output X window")
40 #define XID_LONGTEXT N_( \
41     "VLC can embed its video output in an existing X11 window. " \
42     "This is the X identifier of that window (0 means none).")
43
44 /*
45  * Module descriptor
46  */
47 vlc_module_begin ()
48     set_shortname (N_("Drawable"))
49     set_description (N_("Embedded X window video"))
50     set_category (CAT_VIDEO)
51     set_subcategory (SUBCAT_VIDEO_VOUT)
52     set_capability ("xwindow", 70)
53     set_callbacks (OpenXID, Close)
54     add_integer ("drawable-xid", 0, NULL, XID_TEXT, XID_LONGTEXT, true)
55         change_unsaveable ()
56         /*change_integer_range (0, 0xffffffff)*/
57
58     add_submodule ()
59         set_description (N_("Embedded Windows video"))
60         set_capability ("hwnd", 70)
61         set_callbacks (OpenHWND, Close)
62
63 vlc_module_end ()
64
65 static int Control (vout_window_t *, int, va_list);
66
67 /* TODO: move to vlc_variables.h */
68 static inline void *var_GetAddress (vlc_object_t *o, const char *name)
69 {
70     vlc_value_t val;
71     return var_Get (o, name, &val) ? NULL : val.p_address;
72 }
73
74 static vlc_mutex_t serializer = VLC_STATIC_MUTEX;
75
76 /**
77  * Find the drawable set by libvlc application.
78  */
79 static int Open (vlc_object_t *obj, const char *varname, bool ptr)
80 {
81     vout_window_t *wnd = (vout_window_t *)obj;
82     void **used, *val;
83     size_t n = 0;
84
85     if (var_Create (obj->p_libvlc, "drawables-in-use", VLC_VAR_ADDRESS)
86      || var_Create (obj, varname, VLC_VAR_DOINHERIT
87                                   | (ptr ? VLC_VAR_ADDRESS : VLC_VAR_INTEGER)))
88         return VLC_ENOMEM;
89
90     if (ptr)
91         val = var_GetAddress (obj, varname);
92     else
93         val = (void *)(uintptr_t)var_GetInteger (obj, varname);
94     var_Destroy (obj, varname);
95     msg_Err (wnd, "%zu, %p", n, val);
96
97     /* Keep a list of busy drawables, so we don't overlap videos if there are
98      * more than one video track in the stream. */
99     vlc_mutex_lock (&serializer);
100     /* TODO: per-type list of busy drawables */
101     used = var_GetAddress (VLC_OBJECT (obj->p_libvlc), "drawables-in-use");
102     if (used != NULL)
103     {
104         while (used[n] != NULL)
105         {
106             if (used[n] == val)
107                 goto skip;
108             n++;
109         }
110     }
111
112     used = realloc (used, sizeof (*used) * (n + 2));
113     if (used != NULL)
114     {
115         used[n] = val;
116         used[n + 1] = NULL;
117         var_SetAddress (obj->p_libvlc, "drawables-in-use", used);
118     }
119     else
120     {
121 skip:
122         msg_Warn (wnd, "drawable %p is busy", val);
123         val = NULL;
124     }
125     vlc_mutex_unlock (&serializer);
126     msg_Err (wnd, "%zu, %p", n, val);
127
128     if (val == NULL)
129         return VLC_EGENERIC;
130
131     if (ptr)
132         wnd->handle.hwnd = val;
133     else
134         wnd->handle.xid = (uintptr_t)val;
135
136     /* FIXME: check that X server matches --x11-display (if specified) */
137     /* FIXME: get window size (in platform-dependent ways) */
138
139     wnd->control = Control;
140     wnd->p_sys = val;
141     return VLC_SUCCESS;
142 }
143
144 static int  OpenXID (vlc_object_t *obj)
145 {
146     return Open (obj, "drawable-xid", false);
147 }
148
149 static int  OpenHWND (vlc_object_t *obj)
150 {
151     return Open (obj, "drawable-hwnd", true);
152 }
153
154
155 /**
156  * Release the drawable.
157  */
158 static void Close (vlc_object_t *obj)
159 {
160     vout_window_t *wnd = (vout_window_t *)obj;
161     void **used, *val = wnd->p_sys;
162     size_t n = 0;
163
164     /* Remove this drawable from the list of busy ones */
165     vlc_mutex_lock (&serializer);
166     used = var_GetAddress (VLC_OBJECT (obj->p_libvlc), "drawables-in-use");
167     assert (used);
168     while (used[n] != val)
169     {
170         assert (used[n]);
171         n++;
172     }
173     do
174         used[n] = used[n + 1];
175     while (used[n + 1] != NULL);
176
177     if (n == 0)
178       /* should not be needed (var_Destroy...) but better safe than sorry: */
179          var_SetAddress (obj->p_libvlc, "drawables-in-use", NULL);
180     vlc_mutex_unlock (&serializer);
181
182     if (n == 0)
183         free (used);
184     /* Variables are reference-counted... */
185     var_Destroy (obj->p_libvlc, "drawables-in-use");
186 }
187
188
189 static int Control (vout_window_t *wnd, int query, va_list ap)
190 {
191     switch (query)
192     {
193         case VOUT_SET_SIZE: /* not allowed */
194         case VOUT_SET_STAY_ON_TOP: /* not allowed either, would be ugly */
195             return VLC_EGENERIC;
196     }
197
198     msg_Warn (wnd, "unsupported control query %d", query);
199     return VLC_EGENERIC;
200 }
201