Blame tools/gst-play-kb.c

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