791269d957df83dd54cc8132e4bda245a8f55ed8
[platform/upstream/gst-plugins-base.git] / 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 #include <gst/gst.h>
38
39 /* This is all not thread-safe, but doesn't have to be really */
40
41 #ifdef G_OS_UNIX
42
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;
48
49 static gboolean
50 gst_play_kb_io_cb (GIOChannel * ioc, GIOCondition cond, gpointer user_data)
51 {
52   GIOStatus status;
53
54   if (cond & G_IO_IN) {
55     gchar buf[16] = { 0, };
56     gsize read;
57
58     status = g_io_channel_read_chars (ioc, buf, sizeof (buf) - 1, &read, NULL);
59     if (status == G_IO_STATUS_ERROR)
60       return FALSE;
61     if (status == G_IO_STATUS_NORMAL) {
62       if (kb_callback)
63         kb_callback (buf, kb_callback_data);
64     }
65   }
66
67   return TRUE;                  /* call us again */
68 }
69
70 gboolean
71 gst_play_kb_set_key_handler (GstPlayKbFunc kb_func, gpointer user_data)
72 {
73   GIOChannel *ioc;
74   int flags;
75
76   if (!isatty (STDIN_FILENO)) {
77     GST_INFO ("stdin is not connected to a terminal");
78     return FALSE;
79   }
80
81   if (io_watch_id > 0) {
82     g_source_remove (io_watch_id);
83     io_watch_id = 0;
84   }
85
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;
90     else
91       g_warning ("could not restore terminal attributes");
92
93     setvbuf (stdin, NULL, _IOLBF, 0);
94   }
95
96   if (kb_func != NULL) {
97     struct termios new_settings;
98
99     if (!term_settings_saved) {
100       if (tcgetattr (STDIN_FILENO, &term_settings) != 0) {
101         g_warning ("could not save terminal attributes");
102         return FALSE;
103       }
104       term_settings_saved = TRUE;
105
106       /* Echo off, canonical mode off, extended input processing off  */
107       new_settings = term_settings;
108       new_settings.c_lflag &= ~(ECHO | ICANON | IEXTEN);
109
110       if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &new_settings) != 0) {
111         g_warning ("Could not set terminal state");
112         return FALSE;
113       }
114       setvbuf (stdin, NULL, _IONBF, 0);
115     }
116   }
117
118   ioc = g_io_channel_unix_new (STDIN_FILENO);
119
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);
123
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);
127
128   kb_callback = kb_func;
129   kb_callback_data = user_data;
130
131   return TRUE;
132 }
133
134 #else /* !G_OS_UNIX */
135
136 gboolean
137 gst_play_kb_set_key_handler (GstPlayKbFunc key_func, gpointer user_data)
138 {
139   GST_FIXME ("Keyboard handling for this OS needs to be implemented");
140   return FALSE;
141 }
142
143 #endif /* !G_OS_UNIX */