Hello altogether,

Maybe you remember my questions back in the end of August about how I
could invert the terminal (using reversed video) as a reaction on the
DBus event 'BrightnessChanged' (at least as it is emitted by the
'org.gnome.PowerManager.Backlight' interface).

Marc pointed me to the AnyEvent::DBus module which does a good job
dealing with Net::DBus (no more Net::DBus::Reactor which causes the
module to block). Furthermore, since urxvt provides already an AnyEvent
event loop integrating my module was fairly easy.

I stumbled about some really nasty problem though which caused the long
delay and nearly brought me away from this. Let me briefly explain it to
you and show you the solution.

Initially I just tried to get a DBus session bus object with
Net::DBus->session(). This worked but unfortunately the bus object
blocked and kept the process running even after closing the terminal.
The solution to this was quite simple but took me a long time to figure
out because I had to dig through the whole Net::DBus API. A connection
can be closed with the 'disconnect()' method. Having a bus object with a
get_connection() method this is as simple as
'$bus->get_connection()->disconnect()'. Since you are not allowed to
close a shared connection it has to be opened private (session(private
=> 1)).
That's it. Now it is possible to shut down the DBus connection and exit
gracefully.

I hope you found this interesting and if you like this module feel free
to add it to urxvt. Any comments and improvements are welcome.
Some of the ideas I have include

* making the threshold (currently fixed to 80) a configurable X resource
* reacting on any BrightnessChanged signal, not only from gnome pm
* making the behaviour configurable (e.g. set fg/bg color instead of rvid)
* abstract the whole thing so that any DBus event can cause any action

Please tell me if you are interested in this, or have suggestions. Of
course, if there's no interest in it I'll probably keep it in an IWM (it
works for me) state..

I can also upload to a public repository if you want to, so I don't need
to send updates every time to this list.

Have fun,

Jochen
#! perl

use AnyEvent::DBus;
use Net::DBus::Annotation qw(:call);

sub on_start {
  my ($self) = @_;

  $self->{term}->{bus} = Net::DBus->session(private => 1);
  $self->{term}->{upower} = 
$self->{term}->{bus}->get_service('org.gnome.PowerManager');
  $self->{term}->{object} = $self->{term}->{upower}->get_object(
    '/org/gnome/PowerManager/Backlight',
    'org.gnome.PowerManager.Backlight');

  if ($self->{term}->{object}->GetBrightness() < 80) {
    $self->cmd_parse("\033[?5h");
    $self->want_refresh();
  }

  $self->{term}->{object}->connect_to_signal('BrightnessChanged', sub {
      if ((shift) < 80) {
        $self->{term}->cmd_parse("\033[?5h");
        $self->{term}->want_refresh();
      } else {
        $self->{term}->cmd_parse("\033[?5l");
        $self->{term}->want_refresh();
      }
  });

  ()
}

sub on_destroy {
  my ($self) = @_;
  $self->{term}->{bus}->get_connection()->disconnect();
  ()
}

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
rxvt-unicode mailing list
[email protected]
http://lists.schmorp.de/cgi-bin/mailman/listinfo/rxvt-unicode

Reply via email to