]> git.sesse.net Git - vlc/blob - modules/video_output/drawable.c
b904ff9b9ebd15ecac4cf1f01e5e27c0f691f7b6
[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 /*
40  * Module descriptor
41  */
42 vlc_module_begin ()
43     set_shortname (N_("Drawable"))
44     set_description (N_("Embedded X window video"))
45     set_category (CAT_VIDEO)
46     set_subcategory (SUBCAT_VIDEO_VOUT)
47     set_capability ("xwindow", 70)
48     set_callbacks (OpenXID, Close)
49
50     add_submodule ()
51         set_description (N_("Embedded Windows video"))
52         set_capability ("hwnd", 70)
53         set_callbacks (OpenHWND, Close)
54
55 vlc_module_end ()
56
57 static int Control (vout_window_t *, int, va_list);
58
59 /**
60  * Find the drawable set by libvlc application.
61  */
62 static int Open (vlc_object_t *obj, const char *varname, bool ptr)
63 {
64     static vlc_mutex_t serializer = VLC_STATIC_MUTEX;
65     vout_window_t *wnd = (vout_window_t *)obj;
66     vlc_value_t val, globval;
67
68     if (var_Create (obj->p_libvlc, "drawable-busy", VLC_VAR_BOOL)
69      || var_Create (obj, varname, VLC_VAR_DOINHERIT
70                                   | (ptr ? VLC_VAR_ADDRESS : VLC_VAR_INTEGER)))
71         return VLC_ENOMEM;
72     var_Get (obj, varname, &val);
73
74     vlc_mutex_lock (&serializer);
75     /* Note: We cannot simply clear the drawable variable.
76      * It would break libvlc_video_get_parent(). */
77     var_Get (obj->p_libvlc, varname, &globval);
78     if (ptr ? (val.p_address == globval.p_address)
79             : (val.i_int == globval.i_int))
80     {
81         if (var_GetBool (obj->p_libvlc, "drawable-busy"))
82         {   /* LibVLC-wide drawable already in use */
83             if (ptr)
84                 val.p_address = NULL;
85             else
86                 val.i_int = 0;
87         }
88         else
89             var_SetBool (obj->p_libvlc, "drawable-busy", true);
90     }
91     /* If we got a drawable _not_ from the root object (from the input?),
92      * We assume it is not busy. This is a bug. */
93     vlc_mutex_unlock (&serializer);
94
95     var_Destroy (obj, varname);
96
97     if (ptr ? (val.p_address == NULL) : (val.i_int == 0))
98     {
99         var_Destroy (obj->p_libvlc, "drawable-busy");
100         return VLC_EGENERIC;
101     }
102
103     if (ptr)
104         wnd->handle.hwnd = val.p_address;
105     else
106         wnd->handle.xid = val.i_int;
107
108     /* FIXME: check that X server matches --x11-display (if specified) */
109     /* FIXME: get window size (in platform-dependent ways) */
110
111     wnd->control = Control;
112     return VLC_SUCCESS;
113 }
114
115 static int  OpenXID (vlc_object_t *obj)
116 {
117     return Open (obj, "drawable-xid", false);
118 }
119
120 static int  OpenHWND (vlc_object_t *obj)
121 {
122     return Open (obj, "drawable-hwnd", true);
123 }
124
125
126 /**
127  * Release the drawable.
128  */
129 static void Close (vlc_object_t *obj)
130 {
131     /* This is atomic with regards to var_GetBool() in Open(): */
132     var_SetBool (obj->p_libvlc, "drawable-busy", false);
133
134     /* Variables are reference-counted... */
135     var_Destroy (obj->p_libvlc, "drawable-busy");
136 }
137
138
139 static int Control (vout_window_t *wnd, int query, va_list ap)
140 {
141     switch (query)
142     {
143         case VOUT_GET_SIZE:
144         {
145             unsigned int *pi_width = va_arg (ap, unsigned int *);
146             unsigned int *pi_height = va_arg (ap, unsigned int *);
147             *pi_width = wnd->width;
148             *pi_height = wnd->height;
149             return VLC_SUCCESS;
150         }
151
152         case VOUT_SET_SIZE: /* not allowed */
153         case VOUT_SET_STAY_ON_TOP: /* not allowed either, would be ugly */
154             return VLC_EGENERIC;
155     }
156
157     msg_Warn (wnd, "unsupported control query %d", query);
158     return VLC_EGENERIC;
159 }
160