gstaudioconvert: doc: Fix mix-matrix example
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-base / tools / gst-play-kb.c
1 /* GStreamer command line playback testing utility - keyboard handling helpers
2  *
3  * Copyright (C) 2013 Tim-Philipp Müller <tim centricular net>
4  * Copyright (C) 2013 Centricular Ltd
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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "gst-play-kb.h"
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #ifdef G_OS_UNIX
33 #include <unistd.h>
34 #include <termios.h>
35 #endif
36
37 #ifdef G_OS_WIN32
38 #include <windows.h>
39 #include <io.h>
40 #endif
41
42 #include <gst/gst.h>
43
44 /* This is all not thread-safe, but doesn't have to be really */
45 static GstPlayKbFunc kb_callback;
46 static gpointer kb_callback_data;
47
48 #ifdef G_OS_UNIX
49 static struct termios term_settings;
50 static gboolean term_settings_saved = FALSE;
51 static gulong io_watch_id;
52
53 static gboolean
54 gst_play_kb_io_cb (GIOChannel * ioc, GIOCondition cond, gpointer user_data)
55 {
56   GIOStatus status;
57
58   if (cond & G_IO_IN) {
59     gchar buf[16] = { 0, };
60     gsize read;
61
62     status = g_io_channel_read_chars (ioc, buf, sizeof (buf) - 1, &read, NULL);
63     if (status == G_IO_STATUS_ERROR)
64       return FALSE;
65     if (status == G_IO_STATUS_NORMAL) {
66       if (kb_callback)
67         kb_callback (buf, kb_callback_data);
68     }
69   }
70
71   return TRUE;                  /* call us again */
72 }
73
74 gboolean
75 gst_play_kb_set_key_handler (GstPlayKbFunc kb_func, gpointer user_data)
76 {
77   GIOChannel *ioc;
78
79   if (!isatty (STDIN_FILENO)) {
80     GST_INFO ("stdin is not connected to a terminal");
81     return FALSE;
82   }
83
84   if (io_watch_id > 0) {
85     g_source_remove (io_watch_id);
86     io_watch_id = 0;
87   }
88
89   if (kb_func == NULL && term_settings_saved) {
90     /* restore terminal settings */
91     if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &term_settings) == 0)
92       term_settings_saved = FALSE;
93     else
94       g_warning ("could not restore terminal attributes");
95
96     setvbuf (stdin, NULL, _IOLBF, 0);
97   }
98
99   if (kb_func != NULL) {
100     struct termios new_settings;
101
102     if (!term_settings_saved) {
103       if (tcgetattr (STDIN_FILENO, &term_settings) != 0) {
104         g_warning ("could not save terminal attributes");
105         return FALSE;
106       }
107       term_settings_saved = TRUE;
108
109       /* Echo off, canonical mode off, extended input processing off  */
110       new_settings = term_settings;
111       new_settings.c_lflag &= ~(ECHO | ICANON | IEXTEN);
112       new_settings.c_cc[VMIN] = 0;
113       new_settings.c_cc[VTIME] = 0;
114
115       if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &new_settings) != 0) {
116         g_warning ("Could not set terminal state");
117         return FALSE;
118       }
119       setvbuf (stdin, NULL, _IONBF, 0);
120     }
121   }
122
123   ioc = g_io_channel_unix_new (STDIN_FILENO);
124
125   io_watch_id = g_io_add_watch_full (ioc, G_PRIORITY_DEFAULT, G_IO_IN,
126       (GIOFunc) gst_play_kb_io_cb, user_data, NULL);
127   g_io_channel_unref (ioc);
128
129   kb_callback = kb_func;
130   kb_callback_data = user_data;
131
132   return TRUE;
133 }
134
135 #elif defined(G_OS_WIN32)
136
137 typedef struct
138 {
139   GThread *thread;
140   HANDLE event_handle;
141   HANDLE console_handle;
142   gboolean closing;
143   GMutex lock;
144 } Win32KeyHandler;
145
146 static Win32KeyHandler *win32_handler = NULL;
147
148 typedef struct
149 {
150   gboolean is_ascii;
151   WORD vkcode;
152   gchar key_val[2];
153 } KbSourceData;
154
155 static gboolean
156 gst_play_kb_source_cb (KbSourceData * data)
157 {
158   if (!kb_callback)
159     return G_SOURCE_REMOVE;
160
161   if (data->is_ascii) {
162     kb_callback (data->key_val, kb_callback_data);
163   } else {
164     switch (data->vkcode) {
165       case VK_RIGHT:
166         kb_callback (GST_PLAY_KB_ARROW_RIGHT, kb_callback_data);
167         break;
168       case VK_LEFT:
169         kb_callback (GST_PLAY_KB_ARROW_LEFT, kb_callback_data);
170         break;
171       case VK_UP:
172         kb_callback (GST_PLAY_KB_ARROW_UP, kb_callback_data);
173         break;
174       case VK_DOWN:
175         kb_callback (GST_PLAY_KB_ARROW_DOWN, kb_callback_data);
176         break;
177       default:
178         break;
179     }
180   }
181
182   return G_SOURCE_REMOVE;
183 }
184
185 static gpointer
186 gst_play_kb_win32_thread (gpointer user_data)
187 {
188   Win32KeyHandler *handler = (Win32KeyHandler *) user_data;
189   HANDLE handles[2];
190   INPUT_RECORD buffer;
191   DWORD n;
192
193   handles[0] = handler->event_handle;
194   handles[1] = handler->console_handle;
195
196   if (!kb_callback)
197     return NULL;
198
199   while (TRUE) {
200     DWORD ret = WaitForMultipleObjects (2, handles, FALSE, INFINITE);
201
202     if (ret == WAIT_FAILED) {
203       GST_WARNING ("WaitForMultipleObject Failed");
204       return NULL;
205     }
206
207     g_mutex_lock (&handler->lock);
208     if (handler->closing) {
209       g_mutex_unlock (&handler->lock);
210
211       return NULL;
212     }
213     g_mutex_unlock (&handler->lock);
214
215     if (PeekConsoleInput (handler->console_handle, &buffer, 1, &n) && n == 1) {
216       if (ReadConsoleInput (handler->console_handle, &buffer, 1, &n) &&
217           buffer.EventType == KEY_EVENT && buffer.Event.KeyEvent.bKeyDown) {
218         KbSourceData *data = g_new0 (KbSourceData, 1);
219
220         switch (buffer.Event.KeyEvent.wVirtualKeyCode) {
221           case VK_RIGHT:
222           case VK_LEFT:
223           case VK_UP:
224           case VK_DOWN:
225             data->is_ascii = FALSE;
226             data->vkcode = buffer.Event.KeyEvent.wVirtualKeyCode;
227             break;
228           default:
229             data->is_ascii = TRUE;
230             data->key_val[0] = buffer.Event.KeyEvent.uChar.AsciiChar;
231             data->key_val[1] = '\0';
232             break;
233         }
234
235         g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
236             (GSourceFunc) gst_play_kb_source_cb, data, g_free);
237       }
238     }
239   }
240
241   return NULL;
242 }
243
244 gboolean
245 gst_play_kb_set_key_handler (GstPlayKbFunc kb_func, gpointer user_data)
246 {
247   gint fd = _fileno (stdin);
248
249   if (!_isatty (fd)) {
250     GST_INFO ("stdin is not connected to a terminal");
251     return FALSE;
252   }
253
254   if (win32_handler) {
255     g_mutex_lock (&win32_handler->lock);
256     win32_handler->closing = TRUE;
257     g_mutex_unlock (&win32_handler->lock);
258
259     SetEvent (win32_handler->event_handle);
260     g_thread_join (win32_handler->thread);
261     CloseHandle (win32_handler->event_handle);
262
263     g_mutex_clear (&win32_handler->lock);
264     g_free (win32_handler);
265     win32_handler = NULL;
266   }
267
268   if (kb_func) {
269     SECURITY_ATTRIBUTES sec_attrs;
270
271     sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
272     sec_attrs.lpSecurityDescriptor = NULL;
273     sec_attrs.bInheritHandle = FALSE;
274
275     win32_handler = g_new0 (Win32KeyHandler, 1);
276
277     /* create cancellable event handle */
278     win32_handler->event_handle = CreateEvent (&sec_attrs, TRUE, FALSE, NULL);
279
280     if (!win32_handler->event_handle) {
281       GST_WARNING ("Couldn't create event handle");
282       g_free (win32_handler);
283       win32_handler = NULL;
284
285       return FALSE;
286     }
287
288     win32_handler->console_handle = GetStdHandle (STD_INPUT_HANDLE);
289     if (!win32_handler->console_handle) {
290       GST_WARNING ("Couldn't get console handle");
291       CloseHandle (win32_handler->event_handle);
292       g_free (win32_handler);
293       win32_handler = NULL;
294
295       return FALSE;
296     }
297
298     g_mutex_init (&win32_handler->lock);
299     win32_handler->thread =
300         g_thread_new ("gst-play-kb", gst_play_kb_win32_thread, win32_handler);
301   }
302
303   kb_callback = kb_func;
304   kb_callback_data = user_data;
305
306   return TRUE;
307 }
308
309 #else
310
311 gboolean
312 gst_play_kb_set_key_handler (GstPlayKbFunc key_func, gpointer user_data)
313 {
314   GST_FIXME ("Keyboard handling for this OS needs to be implemented");
315   return FALSE;
316 }
317
318 #endif /* !G_OS_UNIX */