Re: Implementing Gtk2->main in select?



Artūras ?lajus said:
   Hello everybody.

   I'm new to this list and Gtk2-perl. I've got one question. How should
I add Gtk2->main that it doesn't stop all program. I must listen on UDP
and in same time UI must be responsive. When something comes to UDP
socket i call sub and echo something on GTK UI. I have this code:

in theory, there are two ways to do this: integrate the Gtk main loop into
your own event loop, or integrate your event sources into Gtk's main loop.

in practice, it's much easier to use Gtk's main loop and integrate your IO
streams as event sources with Glib::IO->add_watch or Gtk2::Helper->add_watch.

Glib's main loop (Gtk's main loop is actually an abstraction of Glib's) allows
you to attach event sources to a "main context"; a mainloop iteration simply
polls all the event sources in the context.  Glib includes built-in support
for adding file handles to that list of event sources.


basically, you want to add a "watch" on a GIOChannel; since GIOChannels
overlap almost completely with Perl filehandles, we don't have them available
in Gtk2-Perl, except for the function which allow you to create an event
source on a file descriptor.


so, instead of creating an IO::Select and adding the file handles to it and
polling it for data to read, call Glib::IO->add_watch on each file handle's
fileno() and supply a callback to happen on the important events (input,
error, hangup, etc).  then use Gtk2->main normally.

     # use Glib's main loop to watch on the sockets.
     Glib::IO->add_watch (fileno $listen, # must be a file *descriptor*
                          [qw/in hup pri nval/],
                          \&io_handler);


a warning -- writing the io watch handlers for the first time can be a little
hairy.  you may want to look at Gtk2::Helper, which has an abstraction for
this.


recommended reading:

http://developer.gnome.org/doc/API/2.0/glib/glib-The-Main-Event-Loop.html
http://developer.gnome.org/doc/API/2.0/glib/glib-IO-Channels.html#g-io-add-watch
http://gtk2-perl.sourceforge.net/doc/Glib/MainLoop.html
http://gtk2-perl.sourceforge.net/doc/Gtk2/Helper.html


-- 
muppet <scott at asofyet dot org>



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