On Thu, 15 Sep 2016 19:38:35 +0200, Mike Wey wrote:

On 09/15/2016 07:47 AM, dan hitt wrote:

Does anybody have any code out there in a project somewhere that uses IOChannel?

Specifically, i'm looking for an example where you read stdin and attach a function to watch it (where the watching takes place subsequent to Main.run). The attaching call would presumably be through ioAddWatch, but i'm not so clear on all the details of the arguments. I would be grateful to see an example in a working piece of code.

So, thanks in advance if this is possible and you have a piece of code you'd like to point to.

dan

I don't have any concrete examples, but i think you would create an
IOChannel for stdin, and than add an callback for the desired IO condition.

auto input = new IOChannel(STDIN_FILENO);
input.addWatch(IOCondition.IN, cast(IOFunc)&callb, null);

static extern(C) bool callb(GIOChannel* src, IOCondition cond, void* data)
{
	auto input = new IOChannel(src);

	...
}

Hi Mike,

Thanks!

That's very helpful.

I did seem to have to make a mod or two in order to get it to compile and run (detailed below).

Here's the version of code i ended up with:

import std.stdio;
private import glib.IOChannel;

class Base_Stdin_Handler {
  this( ) {
    channel = new IOChannel( 0 );
    event_source_id =
      channel.ioAddWatch( channel, IOCondition.IN,
                          cast(GIOFunc)&handle_stdin, cast(void*)this );
  }

  protected void handle( string input ) {
    writeln( "Got this: ", input );
  }

  protected IOChannel channel;
  protected uint event_source_id;
}

extern (C) int handle_stdin( GIOChannel* src, IOCondition cond,
                             Base_Stdin_Handler handler ) {
  auto input = new IOChannel( src );
  string s;
  size_t terminator_pos;
  auto status = input.readLine( s, terminator_pos );
  handler.handle( s );
  return 1;
}

I had to use 0 instead of STDIN, which doesn't seem to exist in stand-alone form in /usr/local/include/d/gtkd-3 (but if it did exist, it would no doubt be zero).

And i haven't included any error control, or even used everything available (such as terminator_pos).

Also, i'm a little dubious about the signature of the callback. I declared it that way to make it more nearly conformant with the alias GIOFunc defined in glibtypes.d, but i was kind of torn as to how to type the data (void) part (third arg). (So if i left it at void i'd need one less cast in object initialization, but one more cast in the call back.)

So i'll need to tune it up, but just putting it up in case somebody is doing a search and needs a starting point. And also, of course, there may be weaknesses, maybe lots of them, besides those i've pointed out, which of course i'd like to hear about from anybody inclined to report them.

And thanks again for your work on gtkd, which is helping me a great deal.

dan