10c5f77bc66043aa9d3a022708e19d51e205e885
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2xoverlay.c
1 /* GStreamer X-based overlay interface implementation
2  * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  *
4  * gstv4l2xoverlay.c: X-based overlay interface implementation for V4L2
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <gst/gst.h>
27 #include <gst/xoverlay/xoverlay.h>
28 #include <gst/xwindowlistener/xwindowlistener.h>
29
30 #include "gstv4l2xoverlay.h"
31 #include "gstv4l2element.h"
32 #include "v4l2_calls.h"
33
34 static void     gst_v4l2_xoverlay_set_xwindow_id (GstXOverlay *overlay,
35                                                   XID          xwindow_id);
36
37 void
38 gst_v4l2_xoverlay_interface_init (GstXOverlayClass *klass)
39 {
40   /* default virtual functions */
41   klass->set_xwindow_id = gst_v4l2_xoverlay_set_xwindow_id;
42 }
43
44 GstXWindowListener *
45 gst_v4l2_xoverlay_new (GstV4l2Element *v4l2element)
46 {
47   GstXWindowListener *xwin =
48         gst_x_window_listener_new (NULL,
49                                    (MapWindowFunc) gst_v4l2_enable_overlay,
50                                    (SetWindowFunc) gst_v4l2_set_window,
51                                    (gpointer) v4l2element);
52
53   v4l2element->overlay = xwin;
54   v4l2element->xwindow_id = 0;
55
56   return xwin;
57 }
58
59 void
60 gst_v4l2_xoverlay_free (GstV4l2Element *v4l2element)
61 {
62   gst_v4l2_xoverlay_close (v4l2element);
63   g_object_unref (G_OBJECT (v4l2element->overlay));
64   v4l2element->overlay = NULL;
65 }
66
67 void
68 gst_v4l2_xoverlay_open (GstV4l2Element *v4l2element)
69 {
70   GstXWindowListener *xwin = v4l2element->overlay;
71
72   if (xwin) {
73     xwin->display_name = g_strdup (v4l2element->display);
74
75     if (v4l2element->xwindow_id != 0 &&
76         xwin->display_name &&
77         xwin->display_name[0] == ':') {
78       gst_x_window_listener_set_xid (xwin, v4l2element->xwindow_id);
79     }
80   }
81 }
82
83 void
84 gst_v4l2_xoverlay_close (GstV4l2Element *v4l2element)
85 {
86   GstXWindowListener *xwin = v4l2element->overlay;
87
88   if (xwin != NULL) {
89     if (v4l2element->xwindow_id != 0 &&
90         xwin->display_name &&
91         xwin->display_name[0] == ':') {
92       gst_x_window_listener_set_xid (xwin, 0);
93     }
94
95     g_free (xwin->display_name);
96     xwin->display_name = NULL;
97   }
98 }
99
100 static void
101 gst_v4l2_xoverlay_set_xwindow_id (GstXOverlay *overlay,
102                                   XID          xwindow_id)
103 {
104   GstV4l2Element *v4l2element = GST_V4L2ELEMENT (overlay);
105   GstXWindowListener *xwin = v4l2element->overlay;
106
107   if (v4l2element->xwindow_id == xwindow_id) {
108     return;
109   }
110
111   if (gst_element_get_state (GST_ELEMENT (v4l2element)) != GST_STATE_NULL &&
112       v4l2element->xwindow_id != 0 &&
113       xwin != NULL &&
114       xwin->display_name &&
115       xwin->display_name[0] == ':') {
116     gst_x_window_listener_set_xid (xwin, 0);
117   }
118
119   v4l2element->xwindow_id = xwindow_id;
120
121   if (gst_element_get_state (GST_ELEMENT (v4l2element)) != GST_STATE_NULL &&
122       v4l2element->xwindow_id != 0 &&
123       xwin != NULL &&
124       xwin->display_name &&
125       xwin->display_name[0] == ':') {
126     gst_x_window_listener_set_xid (xwin, v4l2element->xwindow_id);
127   }
128 }