| 1 | #----------------------------------------------------------------------- | ||
| 2 | # Copyright (C) 2005-2015 by Jörn Reder <joern AT zyn.de>. | ||
| 3 | # All Rights Reserved. See file COPYRIGHT for details. | ||
| 4 | # | ||
| 5 | # This module is part of Event::RPC, which is free software; you can | ||
| 6 | # redistribute it and/or modify it under the same terms as Perl itself. | ||
| 7 | #----------------------------------------------------------------------- | ||
| 8 | |||
| 9 | package Event::RPC::Loop::Glib; | ||
| 10 | |||
| 11 | use base qw( Event::RPC::Loop ); | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use utf8; | ||
| 15 | |||
| 16 | use Glib; | ||
| 17 | |||
| 18 | sub get_glib_main_loop { shift->{glib_main_loop} } | ||
| 19 | sub set_glib_main_loop { shift->{glib_main_loop} = $_[1] } | ||
| 20 | |||
| 21 | sub add_io_watcher { | ||
| 22 | my $self = shift; | ||
| 23 | my %par = @_; | ||
| 24 | my ($fh, $cb, $desc, $poll) = @par{'fh','cb','desc','poll'}; | ||
| 25 | |||
| 26 | my $cond = $poll eq 'r' ? | ||
| 27 | ['G_IO_IN', 'G_IO_HUP']: | ||
| 28 | ['G_IO_OUT','G_IO_HUP']; | ||
| 29 | |||
| 30 | return Glib::IO->add_watch ($fh->fileno, $cond, sub { &$cb(); 1 } ); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub del_io_watcher { | ||
| 34 | my $self = shift; | ||
| 35 | my ($watcher) = @_; | ||
| 36 | |||
| 37 | Glib::Source->remove ($watcher); | ||
| 38 | |||
| 39 | 1; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub add_timer { | ||
| 43 | my $self = shift; | ||
| 44 | my %par = @_; | ||
| 45 | my ($interval, $after, $cb, $desc) = | ||
| 46 | @par{'interval','after','cb','desc'}; | ||
| 47 | |||
| 48 | die "interval and after can't be used together" | ||
| 49 | if $interval && $after; | ||
| 50 | |||
| 51 | if ( $interval ) { | ||
| 52 | return Glib::Timeout->add ( | ||
| 53 | $interval * 1000, | ||
| 54 | sub { &$cb(); 1 } | ||
| 55 | ); | ||
| 56 | } | ||
| 57 | else { | ||
| 58 | return Glib::Timeout->add ( | ||
| 59 | $after * 1000, | ||
| 60 | sub { &$cb(); 0 } | ||
| 61 | ); | ||
| 62 | } | ||
| 63 | |||
| 64 | 1; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub del_timer { | ||
| 68 | my $self = shift; | ||
| 69 | my ($timer) = @_; | ||
| 70 | |||
| 71 | Glib::Source->remove($timer); | ||
| 72 | |||
| 73 | 1; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub enter { | ||
| 77 | my $self = shift; | ||
| 78 | |||
| 79 | Glib->install_exception_handler(sub { | ||
| 80 | print "Event::RPC::Loop::Glib caught an exception: $@\n"; | ||
| 81 | 1; | ||
| 82 | }); | ||
| 83 | |||
| 84 | my $main_loop = Glib::MainLoop->new; | ||
| 85 | $self->set_glib_main_loop($main_loop); | ||
| 86 | |||
| 87 | $main_loop->run; | ||
| 88 | |||
| 89 | 1; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub leave { | ||
| 93 | my $self = shift; | ||
| 94 | |||
| 95 | $self->get_glib_main_loop->quit; | ||
| 96 | |||
| 97 | 1; | ||
| 98 | } | ||
| 99 | |||
| 100 | 1; | ||
| 101 | |||
| 102 | __END__ |