Blame tools/gst-play-kb.c

Packit 971217
/* GStreamer command line playback testing utility - keyboard handling helpers
Packit 971217
 *
Packit 971217
 * Copyright (C) 2013 Tim-Philipp Müller <tim centricular net>
Packit 971217
 * Copyright (C) 2013 Centricular Ltd
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include "gst-play-kb.h"
Packit 971217
Packit 971217
#include <stdlib.h>
Packit 971217
#include <stdio.h>
Packit 971217
#include <string.h>
Packit 971217
Packit 971217
#ifdef G_OS_UNIX
Packit 971217
#include <unistd.h>
Packit 971217
#include <termios.h>
Packit 971217
#endif
Packit 971217
Packit 971217
#include <gst/gst.h>
Packit 971217
Packit 971217
/* This is all not thread-safe, but doesn't have to be really */
Packit 971217
Packit 971217
#ifdef G_OS_UNIX
Packit 971217
Packit 971217
static struct termios term_settings;
Packit 971217
static gboolean term_settings_saved = FALSE;
Packit 971217
static GstPlayKbFunc kb_callback;
Packit 971217
static gpointer kb_callback_data;
Packit 971217
static gulong io_watch_id;
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_play_kb_io_cb (GIOChannel * ioc, GIOCondition cond, gpointer user_data)
Packit 971217
{
Packit 971217
  GIOStatus status;
Packit 971217
Packit 971217
  if (cond & G_IO_IN) {
Packit 971217
    gchar buf[16] = { 0, };
Packit 971217
    gsize read;
Packit 971217
Packit 971217
    status = g_io_channel_read_chars (ioc, buf, sizeof (buf) - 1, &read, NULL);
Packit 971217
    if (status == G_IO_STATUS_ERROR)
Packit 971217
      return FALSE;
Packit 971217
    if (status == G_IO_STATUS_NORMAL) {
Packit 971217
      if (kb_callback)
Packit 971217
        kb_callback (buf, kb_callback_data);
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  return TRUE;                  /* call us again */
Packit 971217
}
Packit 971217
Packit 971217
gboolean
Packit 971217
gst_play_kb_set_key_handler (GstPlayKbFunc kb_func, gpointer user_data)
Packit 971217
{
Packit 971217
  GIOChannel *ioc;
Packit 971217
Packit 971217
  if (!isatty (STDIN_FILENO)) {
Packit 971217
    GST_INFO ("stdin is not connected to a terminal");
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
Packit 971217
  if (io_watch_id > 0) {
Packit 971217
    g_source_remove (io_watch_id);
Packit 971217
    io_watch_id = 0;
Packit 971217
  }
Packit 971217
Packit 971217
  if (kb_func == NULL && term_settings_saved) {
Packit 971217
    /* restore terminal settings */
Packit 971217
    if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &term_settings) == 0)
Packit 971217
      term_settings_saved = FALSE;
Packit 971217
    else
Packit 971217
      g_warning ("could not restore terminal attributes");
Packit 971217
Packit 971217
    setvbuf (stdin, NULL, _IOLBF, 0);
Packit 971217
  }
Packit 971217
Packit 971217
  if (kb_func != NULL) {
Packit 971217
    struct termios new_settings;
Packit 971217
Packit 971217
    if (!term_settings_saved) {
Packit 971217
      if (tcgetattr (STDIN_FILENO, &term_settings) != 0) {
Packit 971217
        g_warning ("could not save terminal attributes");
Packit 971217
        return FALSE;
Packit 971217
      }
Packit 971217
      term_settings_saved = TRUE;
Packit 971217
Packit 971217
      /* Echo off, canonical mode off, extended input processing off  */
Packit 971217
      new_settings = term_settings;
Packit 971217
      new_settings.c_lflag &= ~(ECHO | ICANON | IEXTEN);
Packit 971217
      new_settings.c_cc[VMIN] = 0;
Packit 971217
      new_settings.c_cc[VTIME] = 0;
Packit 971217
Packit 971217
      if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &new_settings) != 0) {
Packit 971217
        g_warning ("Could not set terminal state");
Packit 971217
        return FALSE;
Packit 971217
      }
Packit 971217
      setvbuf (stdin, NULL, _IONBF, 0);
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  ioc = g_io_channel_unix_new (STDIN_FILENO);
Packit 971217
Packit 971217
  io_watch_id = g_io_add_watch_full (ioc, G_PRIORITY_DEFAULT, G_IO_IN,
Packit 971217
      (GIOFunc) gst_play_kb_io_cb, user_data, NULL);
Packit 971217
  g_io_channel_unref (ioc);
Packit 971217
Packit 971217
  kb_callback = kb_func;
Packit 971217
  kb_callback_data = user_data;
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
#else /* !G_OS_UNIX */
Packit 971217
Packit 971217
gboolean
Packit 971217
gst_play_kb_set_key_handler (GstPlayKbFunc key_func, gpointer user_data)
Packit 971217
{
Packit 971217
  GST_FIXME ("Keyboard handling for this OS needs to be implemented");
Packit 971217
  return FALSE;
Packit 971217
}
Packit 971217
Packit 971217
#endif /* !G_OS_UNIX */