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 "nvcodec.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 GstNvCodecPlayKbFunc kb_callback;
46 static gpointer kb_callback_data;
47 static gulong io_watch_id;
48
49 static gboolean
gst_nvcodec_kb_io_cb(GIOChannel * ioc,GIOCondition cond,gpointer user_data)50 gst_nvcodec_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 G_SOURCE_REMOVE;
61 if (status == G_IO_STATUS_NORMAL) {
62 if (kb_callback)
63 kb_callback (buf, kb_callback_data);
64 }
65 }
66
67 return G_SOURCE_CONTINUE;
68 }
69
70 gboolean
gst_nvcodec_kb_set_key_handler(GstNvCodecPlayKbFunc kb_func,gpointer user_data)71 gst_nvcodec_kb_set_key_handler (GstNvCodecPlayKbFunc kb_func,
72 gpointer user_data)
73 {
74 GIOChannel *ioc;
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 new_settings.c_cc[VMIN] = 0;
110 new_settings.c_cc[VTIME] = 0;
111
112 if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &new_settings) != 0) {
113 g_warning ("Could not set terminal state");
114 return FALSE;
115 }
116 setvbuf (stdin, NULL, _IONBF, 0);
117 }
118 }
119
120 ioc = g_io_channel_unix_new (STDIN_FILENO);
121
122 io_watch_id = g_io_add_watch_full (ioc, G_PRIORITY_DEFAULT, G_IO_IN,
123 (GIOFunc) gst_nvcodec_kb_io_cb, user_data, NULL);
124 g_io_channel_unref (ioc);
125
126 kb_callback = kb_func;
127 kb_callback_data = user_data;
128
129 return TRUE;
130 }
131
132 #else /* !G_OS_UNIX */
133
134 gboolean
gst_nvcodec_kb_set_key_handler(GstNvCodecPlayKbFunc key_func,gpointer user_data)135 gst_nvcodec_kb_set_key_handler (GstNvCodecPlayKbFunc key_func,
136 gpointer user_data)
137 {
138 GST_FIXME ("Keyboard handling for this OS needs to be implemented");
139 return FALSE;
140 }
141
142 #endif /* !G_OS_UNIX */
143