gst-indent
[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 = gst_x_window_listener_new (NULL,
48       (MapWindowFunc) gst_v4l2_enable_overlay,
49       (SetWindowFunc) gst_v4l2_set_window,
50       (gpointer) v4l2element);
51
52   v4l2element->overlay = xwin;
53   v4l2element->xwindow_id = 0;
54
55   return xwin;
56 }
57
58 void
59 gst_v4l2_xoverlay_free (GstV4l2Element * v4l2element)
60 {
61   gst_v4l2_xoverlay_close (v4l2element);
62   g_object_unref (G_OBJECT (v4l2element->overlay));
63   v4l2element->overlay = NULL;
64 }
65
66 void
67 gst_v4l2_xoverlay_open (GstV4l2Element * v4l2element)
68 {
69   GstXWindowListener *xwin = v4l2element->overlay;
70
71   if (xwin) {
72     xwin->display_name = g_strdup (v4l2element->display);
73
74     if (v4l2element->xwindow_id != 0 &&
75         xwin->display_name && xwin->display_name[0] == ':') {
76       gst_x_window_listener_set_xid (xwin, v4l2element->xwindow_id);
77     }
78   }
79 }
80
81 void
82 gst_v4l2_xoverlay_close (GstV4l2Element * v4l2element)
83 {
84   GstXWindowListener *xwin = v4l2element->overlay;
85
86   if (xwin != NULL) {
87     if (v4l2element->xwindow_id != 0 &&
88         xwin->display_name && xwin->display_name[0] == ':') {
89       gst_x_window_listener_set_xid (xwin, 0);
90     }
91
92     g_free (xwin->display_name);
93     xwin->display_name = NULL;
94   }
95 }
96
97 static void
98 gst_v4l2_xoverlay_set_xwindow_id (GstXOverlay * overlay, XID xwindow_id)
99 {
100   GstV4l2Element *v4l2element = GST_V4L2ELEMENT (overlay);
101   GstXWindowListener *xwin = v4l2element->overlay;
102
103   if (v4l2element->xwindow_id == xwindow_id) {
104     return;
105   }
106
107   if (gst_element_get_state (GST_ELEMENT (v4l2element)) != GST_STATE_NULL &&
108       v4l2element->xwindow_id != 0 &&
109       xwin != NULL && xwin->display_name && xwin->display_name[0] == ':') {
110     gst_x_window_listener_set_xid (xwin, 0);
111   }
112
113   v4l2element->xwindow_id = xwindow_id;
114
115   if (gst_element_get_state (GST_ELEMENT (v4l2element)) != GST_STATE_NULL &&
116       v4l2element->xwindow_id != 0 &&
117       xwin != NULL && xwin->display_name && xwin->display_name[0] == ':') {
118     gst_x_window_listener_set_xid (xwin, v4l2element->xwindow_id);
119   }
120 }