tools: play: fix leaving STDIN in non-blocking mode after exit
[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
75   if (!isatty (STDIN_FILENO)) {
76     GST_INFO ("stdin is not connected to a terminal");
77     return FALSE;
78   }
79
80   if (io_watch_id > 0) {
81     g_source_remove (io_watch_id);
82     io_watch_id = 0;
83   }
84
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;
89     else
90       g_warning ("could not restore terminal attributes");
91
92     setvbuf (stdin, NULL, _IOLBF, 0);
93   }
94
95   if (kb_func != NULL) {
96     struct termios new_settings;
97
98     if (!term_settings_saved) {
99       if (tcgetattr (STDIN_FILENO, &term_settings) != 0) {
100         g_warning ("could not save terminal attributes");
101         return FALSE;
102       }
103       term_settings_saved = TRUE;
104
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;
110
111       if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &new_settings) != 0) {
112         g_warning ("Could not set terminal state");
113         return FALSE;
114       }
115       setvbuf (stdin, NULL, _IONBF, 0);
116     }
117   }
118
119   ioc = g_io_channel_unix_new (STDIN_FILENO);
120
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);
124
125   kb_callback = kb_func;
126   kb_callback_data = user_data;
127
128   return TRUE;
129 }
130
131 #else /* !G_OS_UNIX */
132
133 gboolean
134 gst_play_kb_set_key_handler (GstPlayKbFunc key_func, gpointer user_data)
135 {
136   GST_FIXME ("Keyboard handling for this OS needs to be implemented");
137   return FALSE;
138 }
139
140 #endif /* !G_OS_UNIX */