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"
39 /* This is all not thread-safe, but doesn't have to be really */
43 static struct termios term_settings;
44 static gboolean term_settings_saved = FALSE;
45 static GstPlayKbFunc kb_callback;
46 static gpointer kb_callback_data;
47 static gulong io_watch_id;
50 gst_play_kb_io_cb (GIOChannel * ioc, GIOCondition cond, gpointer user_data)
55 gchar buf[16] = { 0, };
58 status = g_io_channel_read_chars (ioc, buf, sizeof (buf) - 1, &read, NULL);
59 if (status == G_IO_STATUS_ERROR)
61 if (status == G_IO_STATUS_NORMAL) {
63 kb_callback (buf, kb_callback_data);
67 return TRUE; /* call us again */
71 gst_play_kb_set_key_handler (GstPlayKbFunc kb_func, gpointer user_data)
76 if (!isatty (STDIN_FILENO)) {
77 GST_INFO ("stdin is not connected to a terminal");
81 if (io_watch_id > 0) {
82 g_source_remove (io_watch_id);
86 if (kb_func == NULL && term_settings_saved) {
87 /* restore terminal settings */
88 if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &term_settings) == 0)
89 term_settings_saved = FALSE;
91 g_warning ("could not restore terminal attributes");
93 setvbuf (stdin, NULL, _IOLBF, 0);
96 if (kb_func != NULL) {
97 struct termios new_settings;
99 if (!term_settings_saved) {
100 if (tcgetattr (STDIN_FILENO, &term_settings) != 0) {
101 g_warning ("could not save terminal attributes");
104 term_settings_saved = TRUE;
106 /* Echo off, canonical mode off, extended input processing off */
107 new_settings = term_settings;
108 new_settings.c_lflag &= ~(ECHO | ICANON | IEXTEN);
110 if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &new_settings) != 0) {
111 g_warning ("Could not set terminal state");
114 setvbuf (stdin, NULL, _IONBF, 0);
118 ioc = g_io_channel_unix_new (STDIN_FILENO);
120 /* make non-blocking */
121 flags = g_io_channel_get_flags (ioc);
122 g_io_channel_set_flags (ioc, flags | G_IO_FLAG_NONBLOCK, NULL);
124 io_watch_id = g_io_add_watch_full (ioc, G_PRIORITY_DEFAULT, G_IO_IN,
125 (GIOFunc) gst_play_kb_io_cb, user_data, NULL);
126 g_io_channel_unref (ioc);
128 kb_callback = kb_func;
129 kb_callback_data = user_data;
134 #else /* !G_OS_UNIX */
137 gst_play_kb_set_key_handler (GstPlayKbFunc key_func, gpointer user_data)
139 GST_FIXME ("Keyboard handling for this OS needs to be implemented");
143 #endif /* !G_OS_UNIX */