]> git.sesse.net Git - vlc/blob - modules/video_output/drawable.c
drawable: fix capability
[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_window.h>
33
34 static int  Open (vlc_object_t *);
35 static void Close(vlc_object_t *);
36
37 /*
38  * Module descriptor
39  */
40 vlc_module_begin ()
41     set_shortname (N_("Drawable"))
42     set_description (N_("Embedded window video"))
43     set_category (CAT_VIDEO)
44     set_subcategory (SUBCAT_VIDEO_VOUT)
45     set_capability ("vout window hwnd", 70)
46     set_callbacks (Open, Close)
47     //add_integer ("drawable-hwnd", 0, NULL, HWN_TEXT, HWND_LONGTEXT, true) /* How to ? */
48     //    change_unsaveable ()
49 vlc_module_end ()
50
51 static int Control (vout_window_t *, int, va_list);
52
53 /* TODO: move to vlc_variables.h */
54 static inline void *var_GetAddress (vlc_object_t *o, const char *name)
55 {
56     vlc_value_t val;
57     return var_Get (o, name, &val) ? NULL : val.p_address;
58 }
59
60 static vlc_mutex_t serializer = VLC_STATIC_MUTEX;
61
62 /**
63  * Find the drawable set by libvlc application.
64  */
65 static int Open (vlc_object_t *obj)
66 {
67     vout_window_t *wnd = (vout_window_t *)obj;
68     void **used, *val;
69     size_t n = 0;
70
71     if (var_Create (obj->p_libvlc, "hwnd-in-use", VLC_VAR_ADDRESS)
72      || var_Create (obj, "drawable-hwnd", VLC_VAR_DOINHERIT | VLC_VAR_ADDRESS))
73         return VLC_ENOMEM;
74
75     val = var_GetAddress (obj, "drawable-hwnd");
76     var_Destroy (obj, "drawable-hwn");
77
78     /* Keep a list of busy drawables, so we don't overlap videos if there are
79      * more than one video track in the stream. */
80     vlc_mutex_lock (&serializer);
81     /* TODO: per-type list of busy drawables */
82     used = var_GetAddress (VLC_OBJECT (obj->p_libvlc), "drawables-in-use");
83     if (used != NULL)
84     {
85         while (used[n] != NULL)
86         {
87             if (used[n] == val)
88                 goto skip;
89             n++;
90         }
91     }
92
93     used = realloc (used, sizeof (*used) * (n + 2));
94     if (used != NULL)
95     {
96         used[n] = val;
97         used[n + 1] = NULL;
98         var_SetAddress (obj->p_libvlc, "hwnd-in-use", used);
99     }
100     else
101     {
102 skip:
103         msg_Warn (wnd, "HWND %p is busy", val);
104         val = NULL;
105     }
106     vlc_mutex_unlock (&serializer);
107
108     if (val == NULL)
109         return VLC_EGENERIC;
110
111     wnd->handle.hwnd = val;
112     wnd->control = Control;
113     wnd->sys = val;
114     return VLC_SUCCESS;
115 }
116
117 /**
118  * Release the drawable.
119  */
120 static void Close (vlc_object_t *obj)
121 {
122     vout_window_t *wnd = (vout_window_t *)obj;
123     void **used, *val = wnd->sys;
124     size_t n = 0;
125
126     /* Remove this drawable from the list of busy ones */
127     vlc_mutex_lock (&serializer);
128     used = var_GetAddress (VLC_OBJECT (obj->p_libvlc), "hwnd-in-use");
129     assert (used);
130     while (used[n] != val)
131     {
132         assert (used[n]);
133         n++;
134     }
135     do
136         used[n] = used[n + 1];
137     while (used[++n] != NULL);
138
139     if (n == 0)
140          var_SetAddress (obj->p_libvlc, "hwnd-in-use", NULL);
141     vlc_mutex_unlock (&serializer);
142
143     if (n == 0)
144         free (used);
145     /* Variables are reference-counted... */
146     var_Destroy (obj->p_libvlc, "hwnd-in-use");
147 }
148
149
150 static int Control (vout_window_t *wnd, int query, va_list ap)
151 {
152     switch (query)
153     {
154         case VOUT_WINDOW_SET_SIZE:   /* not allowed */
155         case VOUT_WINDOW_SET_ON_TOP: /* not allowed either, would be ugly */
156             return VLC_EGENERIC;
157         default:
158             msg_Warn (wnd, "unsupported control query %d", query);
159             return VLC_EGENERIC;
160     }
161 }
162