Пример #1
0
int
fe_input_add (int sok, int read, int write, int ex, void *func, void *data)
{
    int tag, type = 0;
    GIOChannel *channel;

#ifdef WIN32
    if (read == 3)
        channel = g_io_channel_win32_new_fd (sok);
    else
        channel = g_io_channel_win32_new_socket (sok);
#else
    channel = g_io_channel_unix_new (sok);
#endif

    if (read)
        type |= G_IO_IN | G_IO_HUP | G_IO_ERR;
    if (write)
        type |= G_IO_OUT | G_IO_ERR;
    if (ex)
        type |= G_IO_PRI;

    tag = g_io_add_watch (channel, type, (GIOFunc) func, data);
    g_io_channel_unref (channel);

    return tag;
}
Пример #2
0
static SockLookupData *sock_get_address_info_async(const gchar *hostname,
						   gushort port,
						   SockAddrFunc func,
						   gpointer data)
{
	SockLookupData *lookup_data = NULL;
	
	refresh_resolvers();

        lookup_data = g_new0(SockLookupData, 1);
        lookup_data->hostname = g_strdup(hostname);
        lookup_data->func = func;
        lookup_data->data = data;
        lookup_data->port = port;
        lookup_data->child_pid = (pid_t)(-1);
        lookup_data->pipe_fds[0] = -1;
        lookup_data->pipe_fds[1] = -1;

	if (pipe(lookup_data->pipe_fds) < 0) {
		perror("pipe");
		func(NULL, data);
                g_free (lookup_data->hostname);
                g_free (lookup_data);
		return NULL;
	}

#ifndef G_OS_WIN32
	if ((lookup_data->child_pid = fork()) < 0) {
		perror("fork");
		func(NULL, data);
                g_free (lookup_data->hostname);
                g_free (lookup_data);
		return NULL;
	}

	if (lookup_data->child_pid == 0) {
                /* Child process. */
                address_info_async_child (lookup_data);
                g_assert_not_reached ();
	}
        /* Parent process. */
        close(lookup_data->pipe_fds[1]);
        lookup_data->pipe_fds[1] = -1;
#endif  /*!G_OS_WIN32 */
        
#ifndef G_OS_WIN32
        lookup_data->channel = g_io_channel_unix_new(lookup_data->pipe_fds[0]);
#else
        lookup_data->channel = g_io_channel_win32_new_fd(lookup_data->pipe_fds[0]);
#endif
        lookup_data->io_tag = g_io_add_watch(lookup_data->channel, G_IO_IN,
                                             sock_get_address_info_async_cb,
                                             lookup_data);
#ifdef G_OS_WIN32
	lookup_data->child_pid = _beginthread(
		address_info_async_child, 0, lookup_data);
#endif

	return lookup_data;
}
Пример #3
0
int
fe_input_add (int sok, int flags, void *func, void *data)
{
	int tag, type = 0;
	GIOChannel *channel;

#ifdef G_OS_WIN32
	if (flags & FIA_FD)
		channel = g_io_channel_win32_new_fd (sok);
	else
		channel = g_io_channel_win32_new_socket (sok);
#else
	channel = g_io_channel_unix_new (sok);
#endif

	if (flags & FIA_READ)
		type |= G_IO_IN | G_IO_HUP | G_IO_ERR;
	if (flags & FIA_WRITE)
		type |= G_IO_OUT | G_IO_ERR;
	if (flags & FIA_EX)
		type |= G_IO_PRI;

	tag = g_io_add_watch (channel, type, (GIOFunc) func, data);
	g_io_channel_unref (channel);

	return tag;
}
Пример #4
0
static gboolean obtain_send_channel_cb(GIOChannel *channel, GIOCondition condition,
	G_GNUC_UNUSED gpointer gdata)
{
#if HAS_SPAWN_LEAVE_STDIN_OPEN
	if (condition & G_IO_FAILURE)
		g_io_channel_shutdown(channel, FALSE, NULL);
	else
	{
		g_io_channel_ref(channel);
		send_channel = channel;
		create_send_source();  /* for the initialization commands */
	}
#else
	if (!(condition & G_IO_FAILURE))
	{
		gint stdin_fd = dup(g_io_channel_unix_get_fd(channel));

	#ifdef G_OS_UNIX
		send_channel = g_io_channel_unix_new(stdin_fd);
		g_io_channel_set_flags(send_channel, G_IO_FLAG_NONBLOCK, NULL);
	#else
		send_channel = g_io_channel_win32_new_fd(stdin_fd);
	#endif
		g_io_channel_set_encoding(send_channel, NULL, NULL);
		g_io_channel_set_buffered(send_channel, FALSE);
		create_send_source();  /* for the initialization commands */
	}
#endif

	return FALSE;
}
Пример #5
0
/* Returns the FD or -1 on resource limit.  */
int
new_channel_from_fd (int cfd)
{
  int idx;

  for (idx = 0; idx < MAX_SLAFD; idx++)
    if (! giochannel_table[idx].used)
      break;

  if (idx == MAX_SLAFD)
    {
      errno = EIO;
      return -1;
    }

  giochannel_table[idx].used = 1;
  giochannel_table[idx].chan = g_io_channel_win32_new_fd (cfd);
  giochannel_table[idx].fd = cfd;
  giochannel_table[idx].socket = INVALID_SOCKET;
  giochannel_table[idx].primary = 1;

  g_io_channel_set_encoding (giochannel_table[idx].chan, NULL, NULL);
  g_io_channel_set_buffered (giochannel_table[idx].chan, FALSE);

  return idx;
}
Пример #6
0
Файл: app.c Проект: g2p/libpgm
static
gboolean
on_startup (
	G_GNUC_UNUSED gpointer data
	)
{
	g_message ("startup.");

	g_sessions = g_hash_table_new (g_str_hash, g_str_equal);

/* add stdin to event manager */
#ifndef G_OS_WIN32
	g_stdin_channel = g_io_channel_unix_new (fileno(stdin));
#else
	g_stdin_channel = g_io_channel_win32_new_fd (fileno(stdin));
#endif
	printf ("binding stdin with encoding %s.\n", g_io_channel_get_encoding(g_stdin_channel));

	g_io_add_watch (g_stdin_channel, G_IO_IN | G_IO_PRI, on_stdin_data, NULL);

/* period timer to indicate some form of life */
// TODO: Gnome 2.14: replace with g_timeout_add_seconds()
	g_timeout_add(10 * 1000, (GSourceFunc)on_mark, NULL);

	puts ("READY");
	fflush (stdout);
	return FALSE;
}
Пример #7
0
void
fe_main (void)
{
	GIOChannel *keyboard_input;

	main_loop = g_main_loop_new(NULL, FALSE);

	/* Keyboard Entry Setup */
#ifdef G_OS_WIN32
	keyboard_input = g_io_channel_win32_new_fd(STDIN_FILENO);
#else
	keyboard_input = g_io_channel_unix_new(STDIN_FILENO);
#endif

	while (!done)
	{
		g_io_add_watch(keyboard_input, G_IO_IN, handle_line, NULL);

		g_main_loop_run(main_loop);

		return;
}

void
fe_exit (void)
{
	done = TRUE;
	g_main_loop_quit(main_loop);
}
Пример #8
0
static VALUE
rg_s_open(gint argc, VALUE *argv, G_GNUC_UNUSED VALUE self)
{
    VALUE arg1, arg2;
    VALUE rio;
    GIOChannel* io = NULL;

    rb_scan_args(argc, argv, "11", &arg1, &arg2);

    if (TYPE(arg1) == T_FIXNUM){
#ifdef G_OS_UNIX
        io = g_io_channel_unix_new(NUM2INT(arg1));
#elif defined(G_OS_WIN32)
        io = g_io_channel_win32_new_fd(NUM2INT(arg1));
#else
        rb_raise(rb_eRuntimeError,
                 "GLib::IOChannel.new(fd) is supported on "
                 "UNIX and Windows environment only");
#endif
    } else {
        GError* err = NULL;
        io = g_io_channel_new_file(RVAL2CSTR(arg1), 
                                   NIL_P(arg2) ? "r" : RVAL2CSTR(arg2), &err);

        if (err != NULL) RAISE_GERROR(err);
    }

    rio = BOXED2RVAL(io, G_TYPE_IO_CHANNEL);

    if (rb_block_given_p()) {
        return rb_ensure(rb_yield, rio, ioc_close, rio);
    }
    return rio;
}
Пример #9
0
static VALUE
rg_initialize(gint argc, VALUE *argv, VALUE self)
{
    VALUE arg1, arg2;

    GIOChannel* io = NULL;
    rb_scan_args(argc, argv, "11", &arg1, &arg2);

    if (TYPE(arg1) != T_STRING){
        gint fd;
        if (TYPE(arg1) == T_FIXNUM){
            fd = NUM2INT(arg1);
        } else {
            fd = NUM2INT(rb_funcall(arg1, rb_intern("to_i"), 0));
        }
#ifdef G_OS_UNIX
        io = g_io_channel_unix_new(fd);
#elif defined(G_OS_WIN32)
        io = g_io_channel_win32_new_fd(fd);
#else
        rb_raise(rb_eRuntimeError, "GLib::IOChannel.new(fd) is supported on UNIX environment only");
#endif
    } else {
        GError* err = NULL;
        io = g_io_channel_new_file(RVAL2CSTR(arg1), 
                                   NIL_P(arg2) ? "r" : RVAL2CSTR(arg2), &err);

        if (err != NULL) RAISE_GERROR(err);
    }

    G_INITIALIZE(self, io);

    return Qnil;
}
Пример #10
0
GIOChannel* get_g_io_channel(gint fd)
{
#ifdef __WIN32__
    return g_io_channel_win32_new_fd(fd);
#elif __UNIX__
    return g_io_channel_unix_new(fd);
#endif
}
Пример #11
0
static GIOChannel *
create_io_channel (int fd)
{
#ifdef G_OS_WIN32
  return g_io_channel_win32_new_fd (fd);
#else
  return g_io_channel_unix_new (fd);
#endif
}
Пример #12
0
static void
IoRedirect(signal_user_data_t *ud)
{
    GIOChannel *channel;
    gint pfd[2];
    gchar *config, *path, *str;
    pid_t pid;

    // I'm opening a pipe and attaching the writer end to stderr
    // The reader end will be polled by main event loop and I'll get
    // a callback when there is data available.
    if (pipe( pfd ) < 0)
    {
        g_warning("Failed to redirect IO. Logging impaired\n");
        return;
    }
    clean_old_logs();
    // Open activity log.
    config = ghb_get_user_config_dir(NULL);
    pid = getpid();
    path = g_strdup_printf("%s/Activity.log.%d", config, pid);
    ud->activity_log = g_io_channel_new_file (path, "w", NULL);
    ud->job_activity_log = NULL;
    str = g_strdup_printf("<big><b>%s</b></big>", path);
    ghb_ui_update(ud, "activity_location", ghb_string_value(str));
    g_free(str);
    g_free(path);
    g_free(config);
    // Set encoding to raw.
    g_io_channel_set_encoding(ud->activity_log, NULL, NULL);
    // redirect stderr to the writer end of the pipe

#if defined(_WIN32)
    _dup2(pfd[1], _fileno(stderr));
#else
    dup2(pfd[1], STDERR_FILENO);
#endif
    setvbuf(stderr, NULL, _IONBF, 0);

#if defined(_WIN32)
    channel = g_io_channel_win32_new_fd(pfd[0]);
#else
    channel = g_io_channel_unix_new(pfd[0]);
#endif

    // I was getting an this error:
    // "Invalid byte sequence in conversion input"
    // Set disable encoding on the channel.
    g_io_channel_set_encoding(channel, NULL, NULL);
    g_io_add_watch(channel, G_IO_IN, ghb_log_cb, (gpointer)ud );
}
int main(int argc, char *argv[]) {
  CustomData data;
  GstStateChangeReturn ret;
  GIOChannel *io_stdin;
   
  /* Initialize GStreamer */
  gst_init (&argc, &argv);
  
  /* Initialize our data structure */
  memset (&data, 0, sizeof (data));
  
  /* Print usage map */
  g_print (
    "USAGE: Choose one of the following options, then press enter:\n"
    " 'C' to increase contrast, 'c' to decrease contrast\n"
    " 'B' to increase brightness, 'b' to decrease brightness\n"
    " 'H' to increase hue, 'h' to decrease hue\n"
    " 'S' to increase saturation, 's' to decrease saturation\n"
    " 'Q' to quit\n");
  
  /* Build the pipeline */
  data.pipeline = gst_parse_launch ("playbin2 uri=http://docs.gstreamer.com/media/sintel_trailer-480p.webm", NULL);
  
  /* Add a keyboard watch so we get notified of keystrokes */
#ifdef _WIN32
  io_stdin = g_io_channel_win32_new_fd (fileno (stdin));
#else
  io_stdin = g_io_channel_unix_new (fileno (stdin));
#endif
  g_io_add_watch (io_stdin, G_IO_IN, (GIOFunc)handle_keyboard, &data);
  
  /* Start playing */
  ret = gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
  if (ret == GST_STATE_CHANGE_FAILURE) {
    g_printerr ("Unable to set the pipeline to the playing state.\n");
    gst_object_unref (data.pipeline);
    return -1;
  }
  print_current_values (data.pipeline);
   
  /* Create a GLib Main Loop and set it to run */
  data.loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (data.loop);
  
  /* Free resources */
  g_main_loop_unref (data.loop);
  g_io_channel_unref (io_stdin);
  gst_element_set_state (data.pipeline, GST_STATE_NULL);
  gst_object_unref (data.pipeline);
  return 0;
}
Пример #14
0
GIOChannel *
g_io_channel_unix_new (gint fd)
{
  struct stat st;

  if (fstat (fd, &st) == 0)
    return g_io_channel_win32_new_fd (fd);
  
  if (getsockopt (fd, SOL_SOCKET, SO_TYPE, NULL, NULL) != SO_ERROR)
    return g_io_channel_win32_new_socket(fd);

  g_warning (G_STRLOC ": %d is neither a file descriptor or a socket", fd);
  return NULL;
}
Пример #15
0
/**
 * Wrapper for atk_streamable_content_get_stream().
 */
static GIOChannel*
_streamable_get_stream (AtkStreamableContent *streamable,
                        const gchar *mime_type)
{
    PyObject *result;
    PyObject *obj;
    GIOChannel *retval = NULL;

    debug ("_streamable_get_stream\n");

    obj = g_object_get_data (G_OBJECT (streamable), PAPI_PYOBJECT);
    result = PyObject_CallMethod (obj, "streamable_get_stream", "s",
                                  mime_type);
    if (!result)
        return NULL;

    /* Create a GIOChannel. */
    if (PyInt_Check (result))
    {
        /* File descriptor value, try to create a channel directly from it. */
#ifdef IS_WIN32
        retval = g_io_channel_win32_new_fd (PyInt_AsLong (result));
#else
        /* Try the unix way... */
        retval = g_io_channel_unix_new (PyInt_AsLong (result));
#endif
    }
    else if (PyString_Check (result))
    {
        /* A filename or something like that. */
        const gchar *val = PyString_AsString (result);
        GError *error;
        if (val)
        {
            retval = g_io_channel_new_file (val, "r", &error);
            if (!retval)
            {
                PyErr_SetString (PyExc_IOError, error->message);
                g_error_free (error);
            }
        }
    }
    Py_DECREF (result);
    return retval;
}
Пример #16
0
void event_pipe_init(void)
{
	GIOChannel *channel;
	int ret;

	ret = pipe_cloexec_nonblock(event_pipe);
	if (ret < 0)
		MPD_ERROR("Couldn't open pipe: %s", strerror(errno));

#ifndef G_OS_WIN32
	channel = g_io_channel_unix_new(event_pipe[0]);
#else
	channel = g_io_channel_win32_new_fd(event_pipe[0]);
#endif
	g_io_channel_set_encoding(channel, NULL, NULL);
	g_io_channel_set_buffered(channel, false);

	event_pipe_source_id = g_io_add_watch(channel, G_IO_IN,
					      main_notify_event, NULL);

	event_channel = channel;

	event_pipe_mutex = g_mutex_new();
}
Пример #17
0
void nntp_main_init(gboolean skip_ssl_cert_check)
{
	int fd_thread_manager;
	
	etpan_skip_ssl_cert_check = skip_ssl_cert_check;
	
	nntp_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
	session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
	
	thread_manager = etpan_thread_manager_new();
	
	fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
	
#ifndef G_OS_WIN32
	io_channel = g_io_channel_unix_new(fd_thread_manager);
#else
	io_channel = g_io_channel_win32_new_fd(fd_thread_manager);
#endif
	
	thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
						    thread_manager_event,
						    (gpointer) NULL,
						    NULL);
}
Пример #18
0
GIOChannel *
g_io_channel_new_file (const gchar  *filename,
                       const gchar  *mode,
                       GError      **error)
{
  int fid, flags, pmode;
  GIOChannel *channel;

  enum { /* Cheesy hack */
    MODE_R = 1 << 0,
    MODE_W = 1 << 1,
    MODE_A = 1 << 2,
    MODE_PLUS = 1 << 3,
  } mode_num;

  g_return_val_if_fail (filename != NULL, NULL);
  g_return_val_if_fail (mode != NULL, NULL);
  g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);

  switch (mode[0])
    {
      case 'r':
        mode_num = MODE_R;
        break;
      case 'w':
        mode_num = MODE_W;
        break;
      case 'a':
        mode_num = MODE_A;
        break;
      default:
        g_warning (G_STRLOC ": Invalid GIOFileMode %s.\n", mode);
        return NULL;
    }

  switch (mode[1])
    {
      case '\0':
        break;
      case '+':
        if (mode[2] == '\0')
          {
            mode_num |= MODE_PLUS;
            break;
          }
        /* Fall through */
      default:
        g_warning (G_STRLOC ": Invalid GIOFileMode %s.\n", mode);
        return NULL;
    }

  switch (mode_num)
    {
      case MODE_R:
        flags = O_RDONLY;
        pmode = _S_IREAD;
        break;
      case MODE_W:
        flags = O_WRONLY | O_TRUNC | O_CREAT;
        pmode = _S_IWRITE;
        break;
      case MODE_A:
        flags = O_WRONLY | O_APPEND | O_CREAT;
        pmode = _S_IWRITE;
        break;
      case MODE_R | MODE_PLUS:
        flags = O_RDWR;
        pmode = _S_IREAD | _S_IWRITE;
        break;
      case MODE_W | MODE_PLUS:
        flags = O_RDWR | O_TRUNC | O_CREAT;
        pmode = _S_IREAD | _S_IWRITE;
        break;
      case MODE_A | MODE_PLUS:
        flags = O_RDWR | O_APPEND | O_CREAT;
        pmode = _S_IREAD | _S_IWRITE;
        break;
      default:
        g_assert_not_reached ();
        flags = 0;
        pmode = 0;
    }

  /* always open 'untranslated' */
  fid = open (filename, flags | _O_BINARY, pmode);

  if (g_io_win32_get_debug_flag ())
    {
      g_print ("g_io_channel_win32_new_file: open(\"%s\", ", filename);
      g_win32_print_access_mode (flags|_O_BINARY);
      g_print (",%#o)=%d\n", pmode, fid);
    }

  if (fid < 0)
    {
      g_set_error (error, G_FILE_ERROR,
                   g_file_error_from_errno (errno),
                   g_strerror (errno));
      return (GIOChannel *)NULL;
    }

  channel = g_io_channel_win32_new_fd (fid);

  /* XXX: move this to g_io_channel_win32_new_fd () */
  channel->close_on_unref = TRUE;
  channel->is_seekable = TRUE;

  switch (mode_num)
    {
      case MODE_R:
        channel->is_readable = TRUE;
        channel->is_writeable = FALSE;
        break;
      case MODE_W:
      case MODE_A:
        channel->is_readable = FALSE;
        channel->is_writeable = TRUE;
        break;
      case MODE_R | MODE_PLUS:
      case MODE_W | MODE_PLUS:
      case MODE_A | MODE_PLUS:
        channel->is_readable = TRUE;
        channel->is_writeable = TRUE;
        break;
      default:
        g_assert_not_reached ();
    }

  return channel;
}
Пример #19
0
static void quick_find()
{
	gtk_tree_store_clear(list);
	const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
	if(strcmp(text, "") == 0) {
		return;
	}
	
	get_path();
	row_pos = 1;
	
	gboolean case_sensitive = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check_case));
	
	GError *error = NULL;
	gint std_out, std_err;

	gchar **cmd;
	gchar *command = g_strconcat(executable, " ", case_sensitive ? "" : "-i ", g_shell_quote(text), NULL);
	if(!g_shell_parse_argv(command, NULL, &cmd, &error)) {
		ui_set_statusbar(TRUE, _("quick-find failed: %s (%s)"), error->message, command);
		g_error_free(error);
		g_free(command);
		return;
	}
	g_free(command);

	gchar **env = utils_copy_environment(
		NULL,
		"GEAN", "running from geany",
		NULL
	);

	if(g_spawn_async_with_pipes(
		base_directory,
		cmd,
		env,
		0, NULL, NULL, NULL, NULL,
		&std_out,
		&std_err,
		&error
	))
	{
		#ifdef G_OS_WIN32
			GIOChannel *err_channel = g_io_channel_win32_new_fd(std_err);
			GIOChannel *out_channel = g_io_channel_win32_new_fd(std_out);
		#else
			GIOChannel *err_channel = g_io_channel_unix_new(std_err);
			GIOChannel *out_channel = g_io_channel_unix_new(std_out);
		#endif

    g_io_channel_set_encoding(out_channel, NULL, NULL);
		g_io_add_watch(out_channel, G_IO_IN | G_IO_HUP, (GIOFunc)output_out, GUINT_TO_POINTER(0));

    g_io_channel_set_encoding(err_channel, NULL, NULL);
		g_io_add_watch(err_channel, G_IO_IN | G_IO_HUP, (GIOFunc)output_out, GUINT_TO_POINTER(1));
	}
	else {
		printf("quick-find ERROR %s: %s\n", cmd[0], error->message);
		ui_set_statusbar(TRUE, _("quick-find ERROR %s: %s"), cmd[0], error->message);
		g_error_free(error);
	}
	g_free(cmd);
	g_free(env);
}
Пример #20
0
int
main (int argc, char **argv)
{
  GladePreviewer *app;
  GOptionContext *context;
  GError *error = NULL;
  GObject *toplevel = NULL;

#ifdef ENABLE_NLS
  setlocale (LC_ALL, "");
  bindtextdomain (GETTEXT_PACKAGE, glade_app_get_locale_dir ());
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);
#endif

  context = g_option_context_new (_("- previews a glade UI definition"));
  g_option_context_add_main_entries (context, option_entries, GETTEXT_PACKAGE);
  g_option_context_add_group (context, gtk_get_option_group (TRUE));
  
  if (!g_option_context_parse (context, &argc, &argv, &error))
    {
      g_printerr (_("%s\nRun '%s --help' to see a full list of available command line "
                   "options.\n"), error->message, argv[0]);
      g_error_free (error);
      g_option_context_free (context);
      return 1;
    }

  g_option_context_free (context);

  if (version)
    {
      g_print ("glade-previewer " VERSION "\n");
      return 0;
    }

  if (!listen && !file_name)
    {
      g_printerr (_("Either --listen or --filename must be specified.\n"));
      return 0;
    }

  gtk_init (&argc, &argv);
  glade_app_get ();

  app = glade_previewer_new (file_name, toplevel_name);
  gtk_widget_show (GTK_WIDGET (app->window));
  
  if (css_file_name)
    glade_preview_window_set_css_file (app->window, css_file_name);

  if (listen)
    {
#ifdef WINDOWS
      GIOChannel *input = g_io_channel_win32_new_fd (fileno (stdin));
#else
      GIOChannel *input = g_io_channel_unix_new (fileno (stdin));
#endif

      app->is_template = template;

      g_io_add_watch (input, G_IO_IN | G_IO_HUP, on_data_incoming, app);

      gtk_main ();
    }
  else if (template)
Пример #21
0
static void *
example_thread(void *data)
{
  NiceAgent *agent;
  NiceCandidate *local, *remote;
  GIOChannel* io_stdin;
  guint stream_id;
  gchar *line = NULL;
  int rval;

#ifdef G_OS_WIN32
  io_stdin = g_io_channel_win32_new_fd(_fileno(stdin));
#else
  io_stdin = g_io_channel_unix_new(fileno(stdin));
#endif
  g_io_channel_set_flags (io_stdin, G_IO_FLAG_NONBLOCK, NULL);

  // Create the nice agent
  agent = nice_agent_new(g_main_loop_get_context (gloop),
      NICE_COMPATIBILITY_RFC5245);
  if (agent == NULL)
    g_error("Failed to create agent");

  // Set the STUN settings and controlling mode
  if (stun_addr) {
    g_object_set(agent, "stun-server", stun_addr, NULL);
    g_object_set(agent, "stun-server-port", stun_port, NULL);
  }
  g_object_set(agent, "controlling-mode", controlling, NULL);

  // Connect to the signals
  g_signal_connect(agent, "candidate-gathering-done",
      G_CALLBACK(cb_candidate_gathering_done), NULL);
  g_signal_connect(agent, "new-selected-pair",
      G_CALLBACK(cb_new_selected_pair), NULL);
  g_signal_connect(agent, "component-state-changed",
      G_CALLBACK(cb_component_state_changed), NULL);

  // Create a new stream with one component
  stream_id = nice_agent_add_stream(agent, 1);
  if (stream_id == 0)
    g_error("Failed to add stream");

  // Attach to the component to receive the data
  // Without this call, candidates cannot be gathered
  nice_agent_attach_recv(agent, stream_id, 1,
      g_main_loop_get_context (gloop), cb_nice_recv, NULL);

  // Start gathering local candidates
  if (!nice_agent_gather_candidates(agent, stream_id))
    g_error("Failed to start candidate gathering");

  g_debug("waiting for candidate-gathering-done signal...");

  g_mutex_lock(&gather_mutex);
  while (!exit_thread && !candidate_gathering_done)
    g_cond_wait(&gather_cond, &gather_mutex);
  g_mutex_unlock(&gather_mutex);
  if (exit_thread)
    goto end;

  // Candidate gathering is done. Send our local candidates on stdout
  printf("Copy this line to remote client:\n");
  printf("\n  ");
  print_local_data(agent, stream_id, 1);
  printf("\n");

  // Listen on stdin for the remote candidate list
  printf("Enter remote data (single line, no wrapping):\n");
  printf("> ");
  fflush (stdout);
  while (!exit_thread) {
    GIOStatus s = g_io_channel_read_line (io_stdin, &line, NULL, NULL, NULL);
    if (s == G_IO_STATUS_NORMAL) {
      // Parse remote candidate list and set it on the agent
      rval = parse_remote_data(agent, stream_id, 1, line);
      if (rval == EXIT_SUCCESS) {
        g_free (line);
        break;
      } else {
        fprintf(stderr, "ERROR: failed to parse remote data\n");
        printf("Enter remote data (single line, no wrapping):\n");
        printf("> ");
        fflush (stdout);
      }
      g_free (line);
    } else if (s == G_IO_STATUS_AGAIN) {
      g_usleep (100000);
    }
  }

  g_debug("waiting for state READY or FAILED signal...");
  g_mutex_lock(&negotiate_mutex);
  while (!exit_thread && !negotiation_done)
    g_cond_wait(&negotiate_cond, &negotiate_mutex);
  g_mutex_unlock(&negotiate_mutex);
  if (exit_thread)
    goto end;

  // Get current selected candidate pair and print IP address used
  if (nice_agent_get_selected_pair (agent, stream_id, 1,
          &local, &remote)) {
    gchar ipaddr[INET6_ADDRSTRLEN];

    nice_address_to_string(&local->addr, ipaddr);
    printf("\nNegotiation complete: ([%s]:%d,",
        ipaddr, nice_address_get_port(&local->addr));
    nice_address_to_string(&remote->addr, ipaddr);
    printf(" [%s]:%d)\n", ipaddr, nice_address_get_port(&remote->addr));
  }

  // Listen to stdin and send data written to it
  printf("\nSend lines to remote (Ctrl-D to quit):\n");
  printf("> ");
  fflush (stdout);
  while (!exit_thread) {
    GIOStatus s = g_io_channel_read_line (io_stdin, &line, NULL, NULL, NULL);
    if (s == G_IO_STATUS_NORMAL) {
      nice_agent_send(agent, stream_id, 1, strlen(line), line);
      g_free (line);
      printf("> ");
      fflush (stdout);
    } else if (s == G_IO_STATUS_AGAIN) {
      g_usleep (100000);
    } else {
      // Ctrl-D was pressed.
      nice_agent_send(agent, stream_id, 1, 1, "\0");
      break;
    }
  }

end:
  g_io_channel_unref (io_stdin);
  g_object_unref(agent);
  g_main_loop_quit (gloop);

  return NULL;
}
Пример #22
0
GIOChannel *
g_io_channel_new_file (const gchar  *filename,
                       const gchar  *mode,
                       GError      **error)
{
  int fid, flags, pmode;
  GIOChannel *channel;

  enum { /* Cheesy hack */
    MODE_R = 1 << 0,
    MODE_W = 1 << 1,
    MODE_A = 1 << 2,
    MODE_PLUS = 1 << 3,
  } mode_num;

  g_return_val_if_fail (filename != NULL, NULL);
  g_return_val_if_fail (mode != NULL, NULL);
  g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);

  switch (mode[0])
    {
      case 'r':
        mode_num = MODE_R;
        break;
      case 'w':
        mode_num = MODE_W;
        break;
      case 'a':
        mode_num = MODE_A;
        break;
      default:
        return NULL;
    }

  switch (mode[1])
    {
      case '\0':
        break;
      case '+':
        if (mode[2] == '\0')
          {
            mode_num |= MODE_PLUS;
            break;
          }
        /* Fall through */
      default:
        return NULL;
    }

  switch (mode_num)
    {
      case MODE_R:
        flags = O_RDONLY;
        pmode = _S_IREAD;
        break;
      case MODE_W:
        flags = O_WRONLY | O_TRUNC | O_CREAT;
        pmode = _S_IWRITE;
        break;
      case MODE_A:
        flags = O_WRONLY | O_APPEND | O_CREAT;
        pmode = _S_IWRITE;
        break;
      case MODE_R | MODE_PLUS:
        flags = O_RDWR;
        pmode = _S_IREAD | _S_IWRITE;
        break;
      case MODE_W | MODE_PLUS:
        flags = O_RDWR | O_TRUNC | O_CREAT;
        pmode = _S_IREAD | _S_IWRITE;
        break;
      case MODE_A | MODE_PLUS:
        flags = O_RDWR | O_APPEND | O_CREAT;
        pmode = _S_IREAD | _S_IWRITE;
        break;
      default:
        g_assert_not_reached ();
        flags = 0;
        pmode = 0;
    }

  /* always open 'untranslated' */
  fid = open (filename, flags | _O_BINARY, pmode);

  if (fid < 0)
    {
      g_set_error (error, G_FILE_ERROR,
                   g_file_error_from_errno (errno),
                   g_strerror (errno));
      return (GIOChannel *)NULL;
    }

  channel = g_io_channel_win32_new_fd (fid);

  /* XXX: move this to g_io_channel_win32_new_fd () */
  channel->close_on_unref = TRUE;
  channel->is_seekable = TRUE;

  /* g_io_channel_win32_new_fd sets is_readable and is_writeable to
   * correspond to actual readability/writeability. Set to FALSE those
   * that mode doesn't allow
   */
  switch (mode_num)
    {
      case MODE_R:
        channel->is_writeable = FALSE;
        break;
      case MODE_W:
      case MODE_A:
        channel->is_readable = FALSE;
        break;
      case MODE_R | MODE_PLUS:
      case MODE_W | MODE_PLUS:
      case MODE_A | MODE_PLUS:
        break;
      default:
        g_assert_not_reached ();
    }

  return channel;
}
Пример #23
0
static void *
example_thread(void *data)
{
  NiceAgent *agent;
  GIOChannel* io_stdin;
  guint stream_id;
  gchar *line = NULL;
  gchar *sdp, *sdp64;

#ifdef G_OS_WIN32
  io_stdin = g_io_channel_win32_new_fd(_fileno(stdin));
#else
  io_stdin = g_io_channel_unix_new(fileno(stdin));
#endif
  g_io_channel_set_flags(io_stdin, G_IO_FLAG_NONBLOCK, NULL);

  // Create the nice agent
  agent = nice_agent_new(g_main_loop_get_context (gloop),
      NICE_COMPATIBILITY_RFC5245);
  if (agent == NULL)
    g_error("Failed to create agent");

  // Set the STUN settings and controlling mode
  if (stun_addr) {
    g_object_set(agent, "stun-server", stun_addr, NULL);
    g_object_set(agent, "stun-server-port", stun_port, NULL);
  }
  g_object_set(agent, "controlling-mode", controlling, NULL);

  // Connect to the signals
  g_signal_connect(agent, "candidate-gathering-done",
      G_CALLBACK(cb_candidate_gathering_done), NULL);
  g_signal_connect(agent, "component-state-changed",
      G_CALLBACK(cb_component_state_changed), NULL);

  // Create a new stream with one component
  stream_id = nice_agent_add_stream(agent, 1);
  if (stream_id == 0)
    g_error("Failed to add stream");
  nice_agent_set_stream_name (agent, stream_id, "text");

  // Attach to the component to receive the data
  // Without this call, candidates cannot be gathered
  nice_agent_attach_recv(agent, stream_id, 1,
      g_main_loop_get_context (gloop), cb_nice_recv, NULL);

  // Start gathering local candidates
  if (!nice_agent_gather_candidates(agent, stream_id))
    g_error("Failed to start candidate gathering");

  g_debug("waiting for candidate-gathering-done signal...");

  g_mutex_lock(&gather_mutex);
  while (!exit_thread && !candidate_gathering_done)
    g_cond_wait(&gather_cond, &gather_mutex);
  g_mutex_unlock(&gather_mutex);
  if (exit_thread)
    goto end;

  // Candidate gathering is done. Send our local candidates on stdout
  sdp = nice_agent_generate_local_sdp (agent);
  printf("Generated SDP from agent :\n%s\n\n", sdp);
  printf("Copy the following line to remote client:\n");
  sdp64 = g_base64_encode ((const guchar *)sdp, strlen (sdp));
  printf("\n  %s\n", sdp64);
  g_free (sdp);
  g_free (sdp64);

  // Listen on stdin for the remote candidate list
  printf("Enter remote data (single line, no wrapping):\n");
  printf("> ");
  fflush (stdout);
  while (!exit_thread) {
    GIOStatus s = g_io_channel_read_line (io_stdin, &line, NULL, NULL, NULL);
    if (s == G_IO_STATUS_NORMAL) {
      gsize sdp_len;

      sdp = (gchar *) g_base64_decode (line, &sdp_len);
      // Parse remote candidate list and set it on the agent
      if (sdp && nice_agent_parse_remote_sdp (agent, sdp) > 0) {
        g_free (sdp);
        g_free (line);
        break;
      } else {
        fprintf(stderr, "ERROR: failed to parse remote data\n");
        printf("Enter remote data (single line, no wrapping):\n");
        printf("> ");
        fflush (stdout);
      }
      g_free (sdp);
      g_free (line);
    } else if (s == G_IO_STATUS_AGAIN) {
      g_usleep (100000);
    }
  }

  g_debug("waiting for state READY or FAILED signal...");
  g_mutex_lock(&negotiate_mutex);
  while (!exit_thread && !negotiation_done)
    g_cond_wait(&negotiate_cond, &negotiate_mutex);
  g_mutex_unlock(&negotiate_mutex);
  if (exit_thread)
    goto end;

  // Listen to stdin and send data written to it
  printf("\nSend lines to remote (Ctrl-D to quit):\n");
  printf("> ");
  fflush (stdout);
  while (!exit_thread) {
    GIOStatus s = g_io_channel_read_line (io_stdin, &line, NULL, NULL, NULL);

    if (s == G_IO_STATUS_NORMAL) {
      nice_agent_send(agent, stream_id, 1, strlen(line), line);
      g_free (line);
      printf("> ");
      fflush (stdout);
    } else if (s == G_IO_STATUS_AGAIN) {
      g_usleep (100000);
    } else {
      // Ctrl-D was pressed.
      nice_agent_send(agent, stream_id, 1, 1, "\0");
      break;
    }
  }

end:
  g_object_unref(agent);
  g_io_channel_unref (io_stdin);
  g_main_loop_quit (gloop);

  return NULL;
}
Пример #24
0
void run_client( pcat_client c, pcat_point p )
{
    gchar *argv[ 2 ] = {
        c->program_name,
        NULL
    };
    size_t additional_env_vars = 1;
    size_t total_env_slots = c->space.num_knobs + additional_env_vars + 1;
    gchar **child_env =
        (gchar **)g_malloc( total_env_slots * sizeof( child_env[0] ) );
    for( size_t i = 0; i < c->space.num_knobs; i++ )
    {
        size_t buffer_size = 1000; /* way too big? */
        child_env[i] = g_malloc( buffer_size );
        pcat_knob k = &get_space( c )->knobs[i];
        printf( "%s\n", k->name );
        switch( k->kind )
        {
            case PCAT_KNOBK_DISCRETE:
                g_snprintf( child_env[i], (gulong)buffer_size, "%s%s=%"PRIi64,
                            PCAT_ENV_VAR_VALUE_PREFIX, k->name, p[i].d );
                break;
            case PCAT_KNOBK_CONTINUOUS:
                g_snprintf( child_env[i], (gulong)buffer_size, "%s%s=%lf",
                            PCAT_ENV_VAR_VALUE_PREFIX, k->name, p[i].c );
                break;
            case PCAT_KNOBK_UNORDERED:
                g_message( "---> Don't really know what to do about " );
                break;
            default:
                g_message( "---> Wrong kind of knob" );
                break;
        }
    }
    child_env[ c->space.num_knobs ] = PCAT_SENSOR_READINGS_FILENAME "=blam";
    child_env[ c->space.num_knobs + 1 ] = NULL;
    // playing_env_dump();
    
    int chld_std_out_fd, chld_std_err_fd;
    GError *err = NULL;
    data_bucket_ b;
    b.out_done = FALSE;
    b.err_done = FALSE;
    b.child_done = FALSE;
    gchar *result_regex_str = "PCAT_CLIENT_((?<res>SUCCESS)|FAILURE[[:space:]]*\\=[[:space:]]*(?<fcode>[^[:space:]]*))";
    gchar *val_regex_str = PCAT_CLIENT_REPORT_PREFIX "(?<var_name>[[:alpha:]][[:alnum:]_]*)[[:space:]]*\\:[[:space:]]*(?<type_val>REAL|STRING|INTEGRAL)[[:space:]]*\\=[[:space:]]*(?<value>[^[:space:]]*)";
    b.result_regex = g_regex_new( result_regex_str, 0 /* compile flags */, 0 /* match flags*/, &err );
    if( err != NULL )
    {
        printf( "blah: %s\n", err->message );
        abort();
    }
    b.val_regex    = g_regex_new( val_regex_str, 0 /* compile flags */, 0 /* match flags*/, &err );
    b.main_loop = g_main_loop_new( NULL, FALSE );
    gboolean spawn_worked = g_spawn_async_with_pipes(
        NULL, /* const gchar *working_directory */
        argv,
        child_env,
        G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
        /* GSpawnFlags flags */
        /* G_SPAWN_LEAVE_DESCRIPTORS_OPEN */
        /* G_SPAWN_SEARCH_PATH */
        /* G_SPAWN_STDOUT_TO_DEV_NULL */
        /* G_SPAWN_STDERR_TO_DEV_NULL */
        /* G_SPAWN_CHILD_INHERITS_STDIN */
        /* G_SPAWN_FILE_AND_ARGV_ZERO */
        NULL, /* GSpawnChildSetupFunc child_setup */
        NULL, /* gpointer user_data */
        &b.child_pid,
        NULL, /* gint *standard_input */
        &chld_std_out_fd,
        &chld_std_err_fd,
        &err );

    if( !spawn_worked )
    {
        // error!
    }

    b.child_eid = g_child_watch_add( b.child_pid, handle_client_exit, &b );

    /* Create channels that will be used to read data from pipes. */
#ifdef G_OS_WIN32
    b.out_source = g_io_channel_win32_new_fd( chld_std_out_fd );
    b.err_source = g_io_channel_win32_new_fd( chld_std_err_fd );
#else
    b.out_source = g_io_channel_unix_new( chld_std_out_fd );
    b.err_source = g_io_channel_unix_new( chld_std_err_fd );
#endif

    b.out_eid = g_io_add_watch( b.out_source, G_IO_IN | G_IO_HUP, (GIOFunc)child_out_handler, &b );
    b.err_eid = g_io_add_watch( b.err_source, G_IO_IN | G_IO_HUP, (GIOFunc)child_out_handler, &b );
    // these too
    // G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
    // G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,

    b.timeout_eid = g_timeout_add_seconds( 10, timeout_func, &b );

    g_main_loop_run( b.main_loop );
    g_message( "Done running client\n" );
    g_io_channel_unref( b.out_source );
    g_io_channel_unref( b.err_source );
    g_main_loop_unref( b.main_loop );
    g_regex_unref( b.val_regex );
    g_regex_unref( b.result_regex );
    for( size_t i = 0; i < c->space.num_knobs; i++ )
    {
        g_free( child_env[i] );
    }
    g_free( child_env );
}
Пример #25
0
int
main(int argc, char *argv[])
{
  NiceAgent *agent;
  gchar *stun_addr = NULL;
  guint stun_port = 0;
  gboolean controlling;

  // Parse arguments
  if (argc > 4 || argc < 2 || argv[1][1] != '\0') {
    fprintf(stderr, "Usage: %s 0|1 stun_addr [stun_port]\n", argv[0]);
    return EXIT_FAILURE;
  }
  controlling = argv[1][0] - '0';
  if (controlling != 0 && controlling != 1) {
    fprintf(stderr, "Usage: %s 0|1 stun_addr [stun_port]\n", argv[0]);
    return EXIT_FAILURE;
  }

  if (argc > 2) {
    stun_addr = argv[2];
    if (argc > 3)
      stun_port = atoi(argv[3]);
    else
      stun_port = 3478;

    g_debug("Using stun server '[%s]:%u'\n", stun_addr, stun_port);
  }

  g_networking_init();

  gloop = g_main_loop_new(NULL, FALSE);
#ifdef G_OS_WIN32
  io_stdin = g_io_channel_win32_new_fd(_fileno(stdin));
#else
  io_stdin = g_io_channel_unix_new(fileno(stdin));
#endif

  // Create the nice agent
  agent = nice_agent_new(g_main_loop_get_context (gloop),
      NICE_COMPATIBILITY_RFC5245);
  if (agent == NULL)
    g_error("Failed to create agent");

  // Set the STUN settings and controlling mode
  if (stun_addr) {
    g_object_set(agent, "stun-server", stun_addr, NULL);
    g_object_set(agent, "stun-server-port", stun_port, NULL);
  }
  g_object_set(agent, "controlling-mode", controlling, NULL);

  // Connect to the signals
  g_signal_connect(agent, "candidate-gathering-done",
      G_CALLBACK(cb_candidate_gathering_done), NULL);
  g_signal_connect(agent, "new-selected-pair",
      G_CALLBACK(cb_new_selected_pair), NULL);
  g_signal_connect(agent, "component-state-changed",
      G_CALLBACK(cb_component_state_changed), NULL);

  // Create a new stream with one component
  stream_id = nice_agent_add_stream(agent, 1);
  if (stream_id == 0)
    g_error("Failed to add stream");

  // Attach to the component to receive the data
  // Without this call, candidates cannot be gathered
  nice_agent_attach_recv(agent, stream_id, 1,
      g_main_loop_get_context (gloop), cb_nice_recv, NULL);

  // Start gathering local candidates
  if (!nice_agent_gather_candidates(agent, stream_id))
    g_error("Failed to start candidate gathering");

  g_debug("waiting for candidate-gathering-done signal...");

  // Run the mainloop. Everything else will happen asynchronously
  // when the candidates are done gathering.
  g_main_loop_run (gloop);

  g_main_loop_unref(gloop);
  g_object_unref(agent);
  g_io_channel_unref (io_stdin);

  return EXIT_SUCCESS;
}
Пример #26
0
int main(int argc, char *argv[]) {
    CustomData data;
    GstBus *bus;
    GstStateChangeReturn ret;
    gint flags;
    GIOChannel *io_stdin;

    /* Initialize GStreamer */
    gst_init (&argc, &argv);

    /* Create the elements */
    data.playbin2 = gst_element_factory_make ("playbin2", "playbin2");

    if (!data.playbin2) {
        g_printerr ("Not all elements could be created.\n");
        return -1;
    }

    /* Set the URI to play */
    // g_object_set (data.playbin2, "uri", "http://docs.gstreamer.com/media/sintel_cropped_multilingual.webm"/*"/home/douzy/movie.ogg"*/, NULL);
    g_object_set (data.playbin2, "uri", "file:///home/douzy/movie.ogg", NULL);

    /* Set flags to show Audio and Video but ignore Subtitles */
    g_object_get (data.playbin2, "flags", &flags, NULL);
    flags |= GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_AUDIO;
    flags &= ~GST_PLAY_FLAG_TEXT;
    g_object_set (data.playbin2, "flags", flags, NULL);

    /* Set connection speed. This will affect some internal decisions of playbin2 */
    g_object_set (data.playbin2, "connection-speed", 56, NULL);

    /* Add a bus watch, so we get notified when a message arrives */
    bus = gst_element_get_bus (data.playbin2);
    gst_bus_add_watch (bus, (GstBusFunc)handle_message, &data);

    /* Add a keyboard watch so we get notified of keystrokes */
#ifdef _WIN32
    io_stdin = g_io_channel_win32_new_fd (fileno (stdin));
#else
    io_stdin = g_io_channel_unix_new (fileno (stdin));
#endif
    g_io_add_watch (io_stdin, G_IO_IN, (GIOFunc)handle_keyboard, &data);

    /* Start playing */
    ret = gst_element_set_state (data.playbin2, GST_STATE_PLAYING);
    if (ret == GST_STATE_CHANGE_FAILURE) {
        g_printerr ("Unable to set the pipeline to the playing state.\n");
        gst_object_unref (data.playbin2);
        return -1;
    }

    /* Create a GLib Main Loop and set it to run */
    data.main_loop = g_main_loop_new (NULL, FALSE);
    g_main_loop_run (data.main_loop);

    /* Free resources */
    g_main_loop_unref (data.main_loop);
    g_io_channel_unref (io_stdin);
    gst_object_unref (bus);
    gst_element_set_state (data.playbin2, GST_STATE_NULL);
    gst_object_unref (data.playbin2);
    return 0;
}
int main(int argc, char *argv[]) {
  CustomData data;
  GstBus *bus;
  GstStateChangeReturn ret;
  gint flags;
  GIOChannel *io_stdin;
  
  /* Initialize GStreamer */
  gst_init (&argc, &argv);
   
  /* Create the elements */
  data.playbin2 = gst_element_factory_make ("playbin2", "playbin2");
  
  if (!data.playbin2) {
    g_printerr ("Not all elements could be created.\n");
    return -1;
  }
  
  /* Set the URI to play */
  g_object_set (data.playbin2, "uri", "http://docs.gstreamer.com/media/sintel_trailer-480p.ogv", NULL);
  
  /* Set the subtitle URI to play and some font description */
  g_object_set (data.playbin2, "suburi", "http://docs.gstreamer.com/media/sintel_trailer_gr.srt", NULL);
  g_object_set (data.playbin2, "subtitle-font-desc", "Sans, 18", NULL);
  
  /* Set flags to show Audio, Video and Subtitles */
  g_object_get (data.playbin2, "flags", &flags, NULL);
  flags |= GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_TEXT;
  g_object_set (data.playbin2, "flags", flags, NULL);
  
  /* Add a bus watch, so we get notified when a message arrives */
  bus = gst_element_get_bus (data.playbin2);
  gst_bus_add_watch (bus, (GstBusFunc)handle_message, &data);
  
  /* Add a keyboard watch so we get notified of keystrokes */
#ifdef _WIN32
  io_stdin = g_io_channel_win32_new_fd (fileno (stdin));
#else
  io_stdin = g_io_channel_unix_new (fileno (stdin));
#endif
  g_io_add_watch (io_stdin, G_IO_IN, (GIOFunc)handle_keyboard, &data);
  
  /* Start playing */
  ret = gst_element_set_state (data.playbin2, GST_STATE_PLAYING);
  if (ret == GST_STATE_CHANGE_FAILURE) {
    g_printerr ("Unable to set the pipeline to the playing state.\n");
    gst_object_unref (data.playbin2);
    return -1;
  }
  
  /* Create a GLib Main Loop and set it to run */
  data.main_loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (data.main_loop);
  
  /* Free resources */
  g_main_loop_unref (data.main_loop);
  g_io_channel_unref (io_stdin);
  gst_object_unref (bus);
  gst_element_set_state (data.playbin2, GST_STATE_NULL);
  gst_object_unref (data.playbin2);
  return 0;
}