]> git.sesse.net Git - vlc/blob - modules/video_output/drawable.c
vout_window_t: simplify via anynomous union
[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
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 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 static vlc_mutex_t serializer = VLC_STATIC_MUTEX;
54
55 /**
56  * Find the drawable set by libvlc application.
57  */
58 static int Open (vlc_object_t *obj)
59 {
60     vout_window_t *wnd = (vout_window_t *)obj;
61     void **used, *val;
62     size_t n = 0;
63
64     if (var_Create (obj->p_libvlc, "hwnd-in-use", VLC_VAR_ADDRESS)
65      || var_Create (obj, "drawable-hwnd", VLC_VAR_DOINHERIT | VLC_VAR_ADDRESS))
66         return VLC_ENOMEM;
67
68     val = var_GetAddress (obj, "drawable-hwnd");
69     var_Destroy (obj, "drawable-hwn");
70
71     /* Keep a list of busy drawables, so we don't overlap videos if there are
72      * more than one video track in the stream. */
73     vlc_mutex_lock (&serializer);
74     /* TODO: per-type list of busy drawables */
75     used = var_GetAddress (obj->p_libvlc, "drawables-in-use");
76     if (used != NULL)
77     {
78         while (used[n] != NULL)
79         {
80             if (used[n] == val)
81                 goto skip;
82             n++;
83         }
84     }
85
86     used = realloc (used, sizeof (*used) * (n + 2));
87     if (used != NULL)
88     {
89         used[n] = val;
90         used[n + 1] = NULL;
91         var_SetAddress (obj->p_libvlc, "hwnd-in-use", used);
92     }
93     else
94     {
95 skip:
96         msg_Warn (wnd, "HWND %p is busy", val);
97         val = NULL;
98     }
99     vlc_mutex_unlock (&serializer);
100
101     if (val == NULL)
102         return VLC_EGENERIC;
103
104     wnd->hwnd = val;
105     wnd->control = Control;
106     wnd->sys = val;
107     return VLC_SUCCESS;
108 }
109
110 /**
111  * Release the drawable.
112  */
113 static void Close (vlc_object_t *obj)
114 {
115     vout_window_t *wnd = (vout_window_t *)obj;
116     void **used, *val = wnd->sys;
117     size_t n = 0;
118
119     /* Remove this drawable from the list of busy ones */
120     vlc_mutex_lock (&serializer);
121     used = var_GetAddress (obj->p_libvlc, "hwnd-in-use");
122     assert (used);
123     while (used[n] != val)
124     {
125         assert (used[n]);
126         n++;
127     }
128     do
129         used[n] = used[n + 1];
130     while (used[++n] != NULL);
131
132     if (n == 0)
133          var_SetAddress (obj->p_libvlc, "hwnd-in-use", NULL);
134     vlc_mutex_unlock (&serializer);
135
136     if (n == 0)
137         free (used);
138     /* Variables are reference-counted... */
139     var_Destroy (obj->p_libvlc, "hwnd-in-use");
140 }
141
142
143 static int Control (vout_window_t *wnd, int query, va_list ap)
144 {
145     VLC_UNUSED( ap );
146
147     switch (query)
148     {
149         case VOUT_WINDOW_SET_SIZE:   /* not allowed */
150         case VOUT_WINDOW_SET_ON_TOP: /* not allowed either, would be ugly */
151             return VLC_EGENERIC;
152         default:
153             msg_Warn (wnd, "unsupported control query %d", query);
154             return VLC_EGENERIC;
155     }
156 }
157