Re: watching a file descriptor in gtk3



On Sun, 22 Jan 2017 11:05:41 -1000 (HST)
rbd <rbd soest hawaii edu> wrote:
Hi all,

I need to monitor a Unix file descriptor for input within my gtk3
program. I believe that I need to somehow be using
g_source_add_unix_fd() and friends but am finding the documentation
confusing. Are there any simple examples which show how to do this
anywhere? (I looked but could not find, apologies for any oversight
on my part.)

I am porting an old Motif app which used

         XtAppAddInput( ... fd ... callbackproc ...)

to register an input handler callbackproc() to be called whenever
there was input pending on fd. Whatever I use to replace this should
NOT block for any user-perceivable amount of time when checking fd
for pending input, which will typically be very small (under 80
bytes) and occurring between a few times per second and a few times
per hour.

Absent a working understanding of how to use g_source_add_unix_fd(),
etc., my inclination is to just use g_idle_add_full() and do my own
non-blocking select() inside my callback. Seems like
g_source_add_unix_fd() is perhaps better suited to this task, but I
can't figure out what I need to do vs. what glib can do without me,
i.e., do I really need to create and attach my own GSource and code
my own entire set of GSourceFuncs? That would seem to be a hugely
complicated effort compared to the simple XtAppAddInput() call, or
even to coding my own select() stuff inside a g_idle_add_full()
callback. Monitoring a few stray file descriptors within a gtk3
program would seem to be a not-unusual need, so I am hoping someone
will tell me that there is an existing GSource somewhere that I can
just add my own fd into, but that is not at all obvious from the
documentation.

I do not fully understand your question (especially I don't understand
your reference to using "g_idle_add_full() and do my own non-blocking
select() inside my callback", which would not work), but you can either
use glib's g_poll()/g_source_add_poll() implementation or (on unix) its
g_source_add_unix_fd() implementation to watch on a file descriptor.
The functionality of both is equivalent, and on unix both use poll()
underneath. This may (or may not) help:

https://sourceforge.net/p/cxx-gtk-utils/git/ci/master/tree/c++-gtk-utils/io_watch.cpp

If you want to use glib's g_source_add_poll() implementation rather
than its g_source_add_unix_fd() implementation, you can avoid some of
the boiler plate with g_io_add_watch() - you can use this to call up a
callback function whenever a file descriptor is ready (you don't need
to use the GIOChannel object for any other purpose, such as reading from
or writing to the descriptor):

https://developer.gnome.org/glib/stable/glib-IO-Channels.html#g-io-add-watch

Chris


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