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