Re: watching a file descriptor in gtk3



On Mon, 23 Jan 2017 22:18:19 +0000
Emmanuele Bassi <ebassi gmail com> wrote:
Hi;

On 23 January 2017 at 22:09, rbd <rbd soest hawaii edu> wrote:

It is not quite that bad.  The documentation for
g_source_add_unix_fd() is inadequate, but although not immediately
obvious the prepare, check and finalize handlers can in fact
usually just be set to NULL (provided you have set the 'events'
argument for g_source_add_unix_fd() correctly) so you only need to
provide the dispatch handler, which executes your callback when an
event occurs.

The poor quality of the documentation is the real problem here.  I
would give it a try and see how you get on with it.  


Nothing about the g_source_* functions is obvious from the official
docs, either immediately or otherwise! ;-> Seriously, one of the
least illuminated points there actually has to do with timeouts,
exactly where in the check->prepare->dispatch sequence the poll()
gets done, etc.  

Of course, patches and bugs for the documentation are welcome.

A good set of articles on GSource and the main context API in GLib is
on Philip Withnall's blog; Philip actually contributed back that
documentation in the API reference, but this kind of discussions on
how the implementation works is ill-suited for an API documentation.

See:

  - https://tecnocode.co.uk/2014/03/27/what-is-gmaincontext/
  - https://tecnocode.co.uk/2015/05/05/a-detailed-look-at-gsource/
  -
https://tecnocode.co.uk/2014/04/19/ensuring-functions-are-called-in-the-right-context/

The second article also covers your use case pretty accurately.

I don't want to appear argumentative, but the second article is not of
great assistance with respect to the OP's usage of a wrapper for poll()
which executes a callback in the glib main loop when a file descriptor
becomes available for reading - as the OP put it, a replacement for
XtAppAddInput().  For that (if not using GIOChannel) he needs
g_source_add_unix_fd(), NULL for the prepare, check and finalize
handlers and a dispatch handler which executes the callback and a type
derived from GSource which interfaces with that.  As far as I can see
there is little in the second article which explains that. (And I have
not seen any other article which does that either.)

The second article is concerned with a more complex implementation of a
non-file descriptor based event source.  For a file descriptor based
event source these days you just use g_source_add_unix_fd(), which does
most of the boiler plate for you, were one to know it.

Let me stress I am not being negative here.  I very much appreciate the
efforts of the glib developers.  But I have stubbed my toe on exactly
the same issue myself: having said that, setting up a few test cases
and reading the source code made it apparent fairly quickly how
g_source_add_unix_fd() actually works, and from there it was pretty
straightforward.

Chris


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]