]> git.sesse.net Git - vlc/blob - modules/video_output/drawable.c
drawable: use static variable rather than LibVLC variable
[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 (vout_window_t *, const vout_window_cfg_t *);
35 static void Close(vout_window_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", 0)
46     set_callbacks (Open, Close)
47     add_shortcut ("embed-hwnd")
48 vlc_module_end ()
49
50 static int Control (vout_window_t *, int, va_list);
51
52 /* Keep a list of busy drawables, so we don't overlap videos if there are
53  * more than one video track in the stream. */
54 static vlc_mutex_t serializer = VLC_STATIC_MUTEX;
55 static void **used = NULL;
56
57 /**
58  * Find the drawable set by libvlc application.
59  */
60 static int Open (vout_window_t *wnd, const vout_window_cfg_t *cfg)
61 {
62     VLC_UNUSED (cfg);
63     void *val = var_InheritAddress (wnd, "drawable-hwnd");
64     if (val == NULL)
65         return VLC_EGENERIC;
66
67     void **tab;
68     size_t n = 0;
69
70     vlc_mutex_lock (&serializer);
71     if (used != NULL)
72         for (/*n = 0*/; used[n] != NULL; n++)
73             if (used[n] == val)
74             {
75                 msg_Warn (wnd, "HWND %p is busy", val);
76                 val = NULL;
77                 goto skip;
78             }
79
80     tab = realloc (used, sizeof (*used) * (n + 2));
81     if (likely(tab != NULL))
82     {
83         used = tab;
84         used[n] = val;
85         used[n + 1] = NULL;
86     }
87     else
88         val = NULL;
89 skip:
90     vlc_mutex_unlock (&serializer);
91
92     if (val == NULL)
93         return VLC_EGENERIC;
94
95     wnd->handle.hwnd = val;
96     wnd->control = Control;
97     wnd->sys = val;
98     return VLC_SUCCESS;
99 }
100
101 /**
102  * Release the drawable.
103  */
104 static void Close (vout_window_t *wnd)
105 {
106     void *val = wnd->sys;
107     size_t n = 0;
108
109     /* Remove this drawable from the list of busy ones */
110     vlc_mutex_lock (&serializer);
111     assert (used != NULL);
112     while (used[n] != val)
113     {
114         assert (used[n] != NULL);
115         n++;
116     }
117     do
118         used[n] = used[n + 1];
119     while (used[++n] != NULL);
120
121     if (n == 0)
122     {
123          free (used);
124          used = NULL;
125     }
126     vlc_mutex_unlock (&serializer);
127 }
128
129
130 static int Control (vout_window_t *wnd, int query, va_list ap)
131 {
132     VLC_UNUSED( ap );
133
134     switch (query)
135     {
136         case VOUT_WINDOW_SET_SIZE:   /* not allowed */
137         case VOUT_WINDOW_SET_STATE: /* not allowed either, would be ugly */
138             return VLC_EGENERIC;
139         default:
140             msg_Warn (wnd, "unsupported control query %d", query);
141             return VLC_EGENERIC;
142     }
143 }
144