v4l2: guard use of ENODATA with #ifdef
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2videooverlay.c
1 /* GStreamer
2  * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  *               2006 Edgard Lima <edgard.lima@indt.org.br>
4  *
5  * gstv4l2video_overlay.c: X-based overlay interface implementation for V4L2
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <string.h>
28 #include <gst/gst.h>
29 #include <X11/X.h>
30 #include <X11/Xlib.h>
31 #include <X11/extensions/Xv.h>
32 #include <X11/extensions/Xvlib.h>
33 #include <sys/stat.h>
34 /* for XkbKeycodeToKeysym */
35 #include <X11/XKBlib.h>
36
37 #include <gst/video/navigation.h>
38
39 #include "gstv4l2videooverlay.h"
40 #include "gstv4l2object.h"
41 #include "v4l2_calls.h"
42
43 #include "gst/gst-i18n-plugin.h"
44 #include <gst/glib-compat-private.h>
45
46 struct _GstV4l2Xv
47 {
48   Display *dpy;
49   gint port, idle_id, event_id;
50   GMutex mutex;                 /* to serialize calls to X11 */
51 };
52
53 GST_DEBUG_CATEGORY_STATIC (v4l2xv_debug);
54 #define GST_CAT_DEFAULT v4l2xv_debug
55
56 void
57 gst_v4l2_video_overlay_interface_init (GstVideoOverlayInterface * iface)
58 {
59   GST_DEBUG_CATEGORY_INIT (v4l2xv_debug, "v4l2xv", 0,
60       "V4L2 GstVideoOverlay interface debugging");
61 }
62
63 static void
64 gst_v4l2_video_overlay_open (GstV4l2Object * v4l2object)
65 {
66   struct stat s;
67   GstV4l2Xv *v4l2xv;
68   const gchar *name = g_getenv ("DISPLAY");
69   unsigned int ver, rel, req, ev, err, anum;
70   int i, id = 0, first_id = 0, min;
71   XvAdaptorInfo *ai;
72   Display *dpy;
73
74   /* we need a display, obviously */
75   if (!name || !(dpy = XOpenDisplay (name))) {
76     GST_WARNING_OBJECT (v4l2object->element,
77         "No $DISPLAY set or failed to open - no overlay");
78     return;
79   }
80
81   /* First let's check that XVideo extension is available */
82   if (!XQueryExtension (dpy, "XVideo", &i, &i, &i)) {
83     GST_WARNING_OBJECT (v4l2object->element,
84         "Xv extension not available - no overlay");
85     XCloseDisplay (dpy);
86     return;
87   }
88
89   /* find port that belongs to this device */
90   if (XvQueryExtension (dpy, &ver, &rel, &req, &ev, &err) != Success) {
91     GST_WARNING_OBJECT (v4l2object->element,
92         "Xv extension not supported - no overlay");
93     XCloseDisplay (dpy);
94     return;
95   }
96   if (XvQueryAdaptors (dpy, DefaultRootWindow (dpy), &anum, &ai) != Success) {
97     GST_WARNING_OBJECT (v4l2object->element, "Failed to query Xv adaptors");
98     XCloseDisplay (dpy);
99     return;
100   }
101   if (fstat (v4l2object->video_fd, &s) < 0) {
102     GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
103         (_("Cannot identify device '%s'."), v4l2object->videodev),
104         GST_ERROR_SYSTEM);
105     XCloseDisplay (dpy);
106     return;
107   }
108   min = s.st_rdev & 0xff;
109   for (i = 0; i < anum; i++) {
110     GST_DEBUG_OBJECT (v4l2object->element, "found adapter: %s", ai[i].name);
111     if (!strcmp (ai[i].name, "video4linux2") ||
112         !strcmp (ai[i].name, "video4linux")) {
113       if (first_id == 0)
114         first_id = ai[i].base_id;
115
116       GST_DEBUG_OBJECT (v4l2object->element,
117           "first_id=%d, base_id=%lu, min=%d", first_id, ai[i].base_id, min);
118
119       /* hmm... */
120       if (first_id != 0 && ai[i].base_id == first_id + min)
121         id = ai[i].base_id;
122     }
123   }
124   XvFreeAdaptorInfo (ai);
125
126   if (id == 0) {
127     GST_WARNING_OBJECT (v4l2object->element,
128         "Did not find XvPortID for device - no overlay");
129     XCloseDisplay (dpy);
130     return;
131   }
132
133   v4l2xv = g_new0 (GstV4l2Xv, 1);
134   v4l2xv->dpy = dpy;
135   v4l2xv->port = id;
136   g_mutex_init (&v4l2xv->mutex);
137   v4l2xv->idle_id = 0;
138   v4l2xv->event_id = 0;
139   v4l2object->xv = v4l2xv;
140
141   if (v4l2object->xwindow_id) {
142     gst_v4l2_video_overlay_set_window_handle (v4l2object,
143         v4l2object->xwindow_id);
144   }
145 }
146
147 static void
148 gst_v4l2_video_overlay_close (GstV4l2Object * v4l2object)
149 {
150   GstV4l2Xv *v4l2xv = v4l2object->xv;
151
152   if (!v4l2object->xv)
153     return;
154
155   if (v4l2object->xwindow_id) {
156     gst_v4l2_video_overlay_set_window_handle (v4l2object, 0);
157   }
158
159   XCloseDisplay (v4l2xv->dpy);
160   g_mutex_clear (&v4l2xv->mutex);
161   if (v4l2xv->idle_id)
162     g_source_remove (v4l2xv->idle_id);
163   if (v4l2xv->event_id)
164     g_source_remove (v4l2xv->event_id);
165   g_free (v4l2xv);
166   v4l2object->xv = NULL;
167 }
168
169 void
170 gst_v4l2_video_overlay_start (GstV4l2Object * v4l2object)
171 {
172   if (v4l2object->xwindow_id) {
173     gst_v4l2_video_overlay_open (v4l2object);
174   }
175 }
176
177 void
178 gst_v4l2_video_overlay_stop (GstV4l2Object * v4l2object)
179 {
180   gst_v4l2_video_overlay_close (v4l2object);
181 }
182
183 /* should be called with mutex held */
184 static gboolean
185 get_render_rect (GstV4l2Object * v4l2object, GstVideoRectangle * rect)
186 {
187   GstV4l2Xv *v4l2xv = v4l2object->xv;
188   if (v4l2xv && v4l2xv->dpy && v4l2object->xwindow_id) {
189     XWindowAttributes attr;
190     XGetWindowAttributes (v4l2xv->dpy, v4l2object->xwindow_id, &attr);
191     /* this is where we'd add support to maintain aspect ratio */
192     rect->x = 0;
193     rect->y = 0;
194     rect->w = attr.width;
195     rect->h = attr.height;
196     return TRUE;
197   } else {
198     return FALSE;
199   }
200 }
201
202 gboolean
203 gst_v4l2_video_overlay_get_render_rect (GstV4l2Object * v4l2object,
204     GstVideoRectangle * rect)
205 {
206   GstV4l2Xv *v4l2xv = v4l2object->xv;
207   gboolean ret = FALSE;
208   if (v4l2xv) {
209     g_mutex_lock (&v4l2xv->mutex);
210     ret = get_render_rect (v4l2object, rect);
211     g_mutex_unlock (&v4l2xv->mutex);
212   }
213   return ret;
214 }
215
216 static void
217 update_geometry (GstV4l2Object * v4l2object)
218 {
219   GstV4l2Xv *v4l2xv = v4l2object->xv;
220   GstVideoRectangle rect;
221   if (!get_render_rect (v4l2object, &rect))
222     return;
223   /* note: we don't pass in valid video x/y/w/h.. currently the xserver
224    * doesn't need to know these, as they come from v4l2 by setting the
225    * crop..
226    */
227   XvPutVideo (v4l2xv->dpy, v4l2xv->port, v4l2object->xwindow_id,
228       DefaultGC (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy)),
229       0, 0, rect.w, rect.h, rect.x, rect.y, rect.w, rect.h);
230 }
231
232 static gboolean
233 idle_refresh (gpointer data)
234 {
235   GstV4l2Object *v4l2object = GST_V4L2_OBJECT (data);
236   GstV4l2Xv *v4l2xv = v4l2object->xv;
237
238   GST_LOG_OBJECT (v4l2object->element, "idle refresh");
239
240   if (v4l2xv) {
241     g_mutex_lock (&v4l2xv->mutex);
242
243     update_geometry (v4l2object);
244
245     v4l2xv->idle_id = 0;
246     g_mutex_unlock (&v4l2xv->mutex);
247   }
248
249   /* once */
250   return FALSE;
251 }
252
253
254 static gboolean
255 event_refresh (gpointer data)
256 {
257   GstV4l2Object *v4l2object = GST_V4L2_OBJECT (data);
258   GstV4l2Xv *v4l2xv = v4l2object->xv;
259
260   GST_LOG_OBJECT (v4l2object->element, "event refresh");
261
262   if (v4l2xv) {
263     XEvent e;
264
265     g_mutex_lock (&v4l2xv->mutex);
266
267     /* If the element supports navigation, collect the relavent input
268      * events and push them upstream as navigation events
269      */
270     if (GST_IS_NAVIGATION (v4l2object->element)) {
271       guint pointer_x = 0, pointer_y = 0;
272       gboolean pointer_moved = FALSE;
273
274       /* We get all pointer motion events, only the last position is
275        * interesting.
276        */
277       while (XCheckWindowEvent (v4l2xv->dpy, v4l2object->xwindow_id,
278               PointerMotionMask, &e)) {
279         switch (e.type) {
280           case MotionNotify:
281             pointer_x = e.xmotion.x;
282             pointer_y = e.xmotion.y;
283             pointer_moved = TRUE;
284             break;
285           default:
286             break;
287         }
288       }
289       if (pointer_moved) {
290         GST_DEBUG_OBJECT (v4l2object->element,
291             "pointer moved over window at %d,%d", pointer_x, pointer_y);
292         g_mutex_unlock (&v4l2xv->mutex);
293         gst_navigation_send_mouse_event (GST_NAVIGATION (v4l2object->element),
294             "mouse-move", 0, e.xbutton.x, e.xbutton.y);
295         g_mutex_lock (&v4l2xv->mutex);
296       }
297
298       /* We get all events on our window to throw them upstream
299        */
300       while (XCheckWindowEvent (v4l2xv->dpy, v4l2object->xwindow_id,
301               KeyPressMask | KeyReleaseMask |
302               ButtonPressMask | ButtonReleaseMask, &e)) {
303         KeySym keysym;
304         const char *key_str = NULL;
305
306         g_mutex_unlock (&v4l2xv->mutex);
307
308         switch (e.type) {
309           case ButtonPress:
310             GST_DEBUG_OBJECT (v4l2object->element,
311                 "button %d pressed over window at %d,%d",
312                 e.xbutton.button, e.xbutton.x, e.xbutton.y);
313             gst_navigation_send_mouse_event (GST_NAVIGATION
314                 (v4l2object->element), "mouse-button-press", e.xbutton.button,
315                 e.xbutton.x, e.xbutton.y);
316             break;
317           case ButtonRelease:
318             GST_DEBUG_OBJECT (v4l2object->element,
319                 "button %d released over window at %d,%d",
320                 e.xbutton.button, e.xbutton.x, e.xbutton.y);
321             gst_navigation_send_mouse_event (GST_NAVIGATION
322                 (v4l2object->element), "mouse-button-release", e.xbutton.button,
323                 e.xbutton.x, e.xbutton.y);
324             break;
325           case KeyPress:
326           case KeyRelease:
327             g_mutex_lock (&v4l2xv->mutex);
328             keysym = XkbKeycodeToKeysym (v4l2xv->dpy, e.xkey.keycode, 0, 0);
329             if (keysym != NoSymbol) {
330               key_str = XKeysymToString (keysym);
331             } else {
332               key_str = "unknown";
333             }
334             g_mutex_unlock (&v4l2xv->mutex);
335             GST_DEBUG_OBJECT (v4l2object->element,
336                 "key %d pressed over window at %d,%d (%s)",
337                 e.xkey.keycode, e.xkey.x, e.xkey.y, key_str);
338             gst_navigation_send_key_event (GST_NAVIGATION (v4l2object->element),
339                 e.type == KeyPress ? "key-press" : "key-release", key_str);
340             break;
341           default:
342             GST_DEBUG_OBJECT (v4l2object->element,
343                 "unhandled X event (%d)", e.type);
344         }
345
346         g_mutex_lock (&v4l2xv->mutex);
347       }
348     }
349
350     /* Handle ConfigureNotify */
351     while (XCheckWindowEvent (v4l2xv->dpy, v4l2object->xwindow_id,
352             StructureNotifyMask, &e)) {
353       switch (e.type) {
354         case ConfigureNotify:
355           update_geometry (v4l2object);
356           break;
357         default:
358           break;
359       }
360     }
361     g_mutex_unlock (&v4l2xv->mutex);
362   }
363
364   /* repeat */
365   return TRUE;
366 }
367
368 void
369 gst_v4l2_video_overlay_set_window_handle (GstV4l2Object * v4l2object,
370     guintptr id)
371 {
372   GstV4l2Xv *v4l2xv;
373   XID xwindow_id = id;
374   gboolean change = (v4l2object->xwindow_id != xwindow_id);
375
376   GST_LOG_OBJECT (v4l2object->element, "Setting XID to %lx",
377       (gulong) xwindow_id);
378
379   if (!v4l2object->xv && GST_V4L2_IS_OPEN (v4l2object))
380     gst_v4l2_video_overlay_open (v4l2object);
381
382   v4l2xv = v4l2object->xv;
383
384   if (v4l2xv)
385     g_mutex_lock (&v4l2xv->mutex);
386
387   if (change) {
388     if (v4l2object->xwindow_id && v4l2xv) {
389       GST_DEBUG_OBJECT (v4l2object->element,
390           "Deactivating old port %lx", v4l2object->xwindow_id);
391
392       XvSelectPortNotify (v4l2xv->dpy, v4l2xv->port, 0);
393       XvSelectVideoNotify (v4l2xv->dpy, v4l2object->xwindow_id, 0);
394       XvStopVideo (v4l2xv->dpy, v4l2xv->port, v4l2object->xwindow_id);
395     }
396
397     v4l2object->xwindow_id = xwindow_id;
398   }
399
400   if (!v4l2xv || xwindow_id == 0) {
401     if (v4l2xv)
402       g_mutex_unlock (&v4l2xv->mutex);
403     return;
404   }
405
406   if (change) {
407     GST_DEBUG_OBJECT (v4l2object->element, "Activating new port %lx",
408         xwindow_id);
409
410     /* draw */
411     XvSelectPortNotify (v4l2xv->dpy, v4l2xv->port, 1);
412     XvSelectVideoNotify (v4l2xv->dpy, v4l2object->xwindow_id, 1);
413   }
414
415   update_geometry (v4l2object);
416
417   if (v4l2xv->idle_id)
418     g_source_remove (v4l2xv->idle_id);
419   v4l2xv->idle_id = g_idle_add (idle_refresh, v4l2object);
420   g_mutex_unlock (&v4l2xv->mutex);
421 }
422
423 /**
424  * gst_v4l2_video_overlay_prepare_window_handle:
425  * @v4l2object: the v4l2object
426  * @required: %TRUE if display is required (ie. TRUE for v4l2sink, but
427  *   FALSE for any other element with optional overlay capabilities)
428  *
429  * Helper function to create a windo if none is set from the application.
430  */
431 void
432 gst_v4l2_video_overlay_prepare_window_handle (GstV4l2Object * v4l2object,
433     gboolean required)
434 {
435   GstVideoOverlay *overlay;
436
437   if (!GST_V4L2_IS_OVERLAY (v4l2object))
438     return;
439
440   overlay = GST_VIDEO_OVERLAY (v4l2object->element);
441   gst_video_overlay_prepare_window_handle (overlay);
442
443   if (required && !v4l2object->xwindow_id) {
444     GstV4l2Xv *v4l2xv;
445     Window win;
446     int width, height;
447     long event_mask;
448
449     if (!v4l2object->xv && GST_V4L2_IS_OPEN (v4l2object))
450       gst_v4l2_video_overlay_open (v4l2object);
451
452     v4l2xv = v4l2object->xv;
453
454     /* if video_overlay is not supported, just bail */
455     if (!v4l2xv)
456       return;
457
458     /* video_overlay is supported, but we don't have a window.. so create one */
459     GST_DEBUG_OBJECT (v4l2object->element, "creating window");
460
461     g_mutex_lock (&v4l2xv->mutex);
462
463     width = XDisplayWidth (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy));
464     height = XDisplayHeight (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy));
465     GST_DEBUG_OBJECT (v4l2object->element, "dpy=%p", v4l2xv->dpy);
466
467     win = XCreateSimpleWindow (v4l2xv->dpy,
468         DefaultRootWindow (v4l2xv->dpy),
469         0, 0, width, height, 0, 0,
470         XBlackPixel (v4l2xv->dpy, DefaultScreen (v4l2xv->dpy)));
471
472     GST_DEBUG_OBJECT (v4l2object->element, "win=%lu", win);
473
474     event_mask = ExposureMask | StructureNotifyMask;
475     if (GST_IS_NAVIGATION (v4l2object->element)) {
476       event_mask |= PointerMotionMask |
477           KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;
478     }
479     XSelectInput (v4l2xv->dpy, win, event_mask);
480     v4l2xv->event_id = g_timeout_add (45, event_refresh, v4l2object);
481
482     XMapRaised (v4l2xv->dpy, win);
483
484     XSync (v4l2xv->dpy, FALSE);
485
486     g_mutex_unlock (&v4l2xv->mutex);
487
488     GST_DEBUG_OBJECT (v4l2object->element, "got window");
489
490     gst_v4l2_video_overlay_set_window_handle (v4l2object, win);
491   }
492 }