1 /* GStreamer command line playback testing utility - keyboard handling helpers
3 * Copyright (C) 2013 Tim-Philipp Müller <tim centricular net>
4 * Copyright (C) 2013 Centricular Ltd
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.
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.
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.
26 #include "gst-play-kb.h"
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;
49 static struct termios term_settings;
50 static gboolean term_settings_saved = FALSE;
51 static gulong io_watch_id;
54 gst_play_kb_io_cb (GIOChannel * ioc, GIOCondition cond, gpointer user_data)
59 gchar buf[16] = { 0, };
62 status = g_io_channel_read_chars (ioc, buf, sizeof (buf) - 1, &read, NULL);
63 if (status == G_IO_STATUS_ERROR)
65 if (status == G_IO_STATUS_NORMAL) {
67 kb_callback (buf, kb_callback_data);
71 return TRUE; /* call us again */
75 gst_play_kb_set_key_handler (GstPlayKbFunc kb_func, gpointer user_data)
79 if (!isatty (STDIN_FILENO)) {
80 GST_INFO ("stdin is not connected to a terminal");
84 if (io_watch_id > 0) {
85 g_source_remove (io_watch_id);
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;
94 g_warning ("could not restore terminal attributes");
96 setvbuf (stdin, NULL, _IOLBF, 0);
99 if (kb_func != NULL) {
100 struct termios new_settings;
102 if (!term_settings_saved) {
103 if (tcgetattr (STDIN_FILENO, &term_settings) != 0) {
104 g_warning ("could not save terminal attributes");
107 term_settings_saved = TRUE;
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;
115 if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &new_settings) != 0) {
116 g_warning ("Could not set terminal state");
119 setvbuf (stdin, NULL, _IONBF, 0);
123 ioc = g_io_channel_unix_new (STDIN_FILENO);
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);
129 kb_callback = kb_func;
130 kb_callback_data = user_data;
135 #elif defined(G_OS_WIN32)
141 HANDLE console_handle;
146 static Win32KeyHandler *win32_handler = NULL;
149 gst_play_kb_source_cb (Win32KeyHandler * handler)
151 HANDLE h_input = handler->console_handle;
155 if (PeekConsoleInput (h_input, &buffer, 1, &n) && n == 1) {
156 ReadConsoleInput (h_input, &buffer, 1, &n);
158 if (buffer.EventType == KEY_EVENT && buffer.Event.KeyEvent.bKeyDown) {
159 gchar key_val[2] = { 0 };
161 switch (buffer.Event.KeyEvent.wVirtualKeyCode) {
163 kb_callback (GST_PLAY_KB_ARROW_RIGHT, kb_callback_data);
166 kb_callback (GST_PLAY_KB_ARROW_LEFT, kb_callback_data);
169 kb_callback (GST_PLAY_KB_ARROW_UP, kb_callback_data);
172 kb_callback (GST_PLAY_KB_ARROW_DOWN, kb_callback_data);
175 key_val[0] = buffer.Event.KeyEvent.uChar.AsciiChar;
176 kb_callback (key_val, kb_callback_data);
182 return G_SOURCE_REMOVE;
186 gst_play_kb_win32_thread (gpointer user_data)
188 Win32KeyHandler *handler = (Win32KeyHandler *) user_data;
191 handles[0] = handler->event_handle;
192 handles[1] = handler->console_handle;
198 DWORD ret = WaitForMultipleObjects (2, handles, FALSE, INFINITE);
200 if (ret == WAIT_FAILED) {
201 GST_WARNING ("WaitForMultipleObject Failed");
205 g_mutex_lock (&handler->lock);
206 if (handler->closing) {
207 g_mutex_unlock (&handler->lock);
211 g_mutex_unlock (&handler->lock);
213 g_idle_add ((GSourceFunc) gst_play_kb_source_cb, handler);
220 gst_play_kb_set_key_handler (GstPlayKbFunc kb_func, gpointer user_data)
222 gint fd = _fileno (stdin);
225 GST_INFO ("stdin is not connected to a terminal");
230 g_mutex_lock (&win32_handler->lock);
231 win32_handler->closing = TRUE;
232 g_mutex_unlock (&win32_handler->lock);
234 SetEvent (win32_handler->event_handle);
235 g_thread_join (win32_handler->thread);
236 CloseHandle (win32_handler->event_handle);
238 g_mutex_clear (&win32_handler->lock);
239 g_free (win32_handler);
240 win32_handler = NULL;
244 SECURITY_ATTRIBUTES sec_attrs;
246 sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
247 sec_attrs.lpSecurityDescriptor = NULL;
248 sec_attrs.bInheritHandle = FALSE;
250 win32_handler = g_new0 (Win32KeyHandler, 1);
252 /* create cancellable event handle */
253 win32_handler->event_handle = CreateEvent (&sec_attrs, TRUE, FALSE, NULL);
255 if (!win32_handler->event_handle) {
256 GST_WARNING ("Couldn't create event handle");
257 g_free (win32_handler);
258 win32_handler = NULL;
263 win32_handler->console_handle = GetStdHandle (STD_INPUT_HANDLE);
264 if (!win32_handler->console_handle) {
265 GST_WARNING ("Couldn't get console handle");
266 CloseHandle (win32_handler->event_handle);
267 g_free (win32_handler);
268 win32_handler = NULL;
273 g_mutex_init (&win32_handler->lock);
274 win32_handler->thread =
275 g_thread_new ("gst-play-kb", gst_play_kb_win32_thread, win32_handler);
278 kb_callback = kb_func;
279 kb_callback_data = user_data;
287 gst_play_kb_set_key_handler (GstPlayKbFunc key_func, gpointer user_data)
289 GST_FIXME ("Keyboard handling for this OS needs to be implemented");
293 #endif /* !G_OS_UNIX */