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)
75 if (!isatty (STDIN_FILENO)) {
76 GST_INFO ("stdin is not connected to a terminal");
80 if (io_watch_id > 0) {
81 g_source_remove (io_watch_id);
85 if (kb_func == NULL && term_settings_saved) {
86 /* restore terminal settings */
87 if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &term_settings) == 0)
88 term_settings_saved = FALSE;
90 g_warning ("could not restore terminal attributes");
92 setvbuf (stdin, NULL, _IOLBF, 0);
95 if (kb_func != NULL) {
96 struct termios new_settings;
98 if (!term_settings_saved) {
99 if (tcgetattr (STDIN_FILENO, &term_settings) != 0) {
100 g_warning ("could not save terminal attributes");
103 term_settings_saved = TRUE;
105 /* Echo off, canonical mode off, extended input processing off */
106 new_settings = term_settings;
107 new_settings.c_lflag &= ~(ECHO | ICANON | IEXTEN);
108 new_settings.c_cc[VMIN] = 0;
109 new_settings.c_cc[VTIME] = 0;
111 if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &new_settings) != 0) {
112 g_warning ("Could not set terminal state");
115 setvbuf (stdin, NULL, _IONBF, 0);
119 ioc = g_io_channel_unix_new (STDIN_FILENO);
121 io_watch_id = g_io_add_watch_full (ioc, G_PRIORITY_DEFAULT, G_IO_IN,
122 (GIOFunc) gst_play_kb_io_cb, user_data, NULL);
123 g_io_channel_unref (ioc);
125 kb_callback = kb_func;
126 kb_callback_data = user_data;
131 #else /* !G_OS_UNIX */
134 gst_play_kb_set_key_handler (GstPlayKbFunc key_func, gpointer user_data)
136 GST_FIXME ("Keyboard handling for this OS needs to be implemented");
140 #endif /* !G_OS_UNIX */