diff --git a/gmusicbrowser.pl b/gmusicbrowser.pl index ab0be029..bdaf8068 100755 --- a/gmusicbrowser.pl +++ b/gmusicbrowser.pl @@ -410,13 +410,11 @@ BEGIN if (!$running && !$CmdLine{noDBus}) { eval {require 'gmusicbrowser_dbus.pm'} || warn "Error loading gmusicbrowser_dbus.pm :\n$@ => controlling gmusicbrowser through DBus won't be possible.\n\n"; - eval - { my $bus= $GMB::DBus::bus || die; - my $service = $bus->get_service($DBus_id) || die; - my $object = $service->get_object('/org/gmusicbrowser', 'org.gmusicbrowser') || die; - $object->RunCommand($_) for @cmd; - }; - $running="using DBus id=$DBus_id" unless $@; + my $object= GMB::DBus::simple_call("$DBus_id org.gmusicbrowser/org/gmusicbrowser"); + if ($object) + { $object->RunCommand($_) for @cmd; + $running="using DBus id=$DBus_id"; + } } if ($running) { warn "Found a running instance ($running)\n"; diff --git a/gmusicbrowser_dbus.pm b/gmusicbrowser_dbus.pm index a540d992..198413a3 100644 --- a/gmusicbrowser_dbus.pm +++ b/gmusicbrowser_dbus.pm @@ -151,6 +151,27 @@ sub init 0; #called in an idle, return 0 to run only once } +use Net::DBus::Annotation qw(:call); +sub simple_call #$service_path can be service_and_path separated by space, or the object +{ my ($service_path,$method,$args,$reply)=@_; + my $return= eval + { my $object; + if (ref $service_path) { $object=$service_path } + else + { my ($name,$path)= split / +/,$service_path,2; + my $service= $bus->get_service($name); + return $service unless $path; + my $interface= $path=~s#^([^/]+)## ? $1 : undef; + $object = $service->get_object($path,$interface); + } + return $object unless $method; + $reply&&= $reply eq 'async' ? dbus_call_async : + $reply eq 'noreply' ? dbus_call_noreply : undef; + return $object->$method(($reply||()),@$args); + }; + return $@ ? undef : $return; +} + sub DBus_mainloop_hack { # use Net::DBus internals to connect it to the Glib mainloop, though unlikely, it may break with future version of Net::DBus use Net::DBus::Reactor; diff --git a/plugins/gnome_mmkeys.pm b/plugins/gnome_mmkeys.pm index 40faa1ae..1a86314a 100644 --- a/plugins/gnome_mmkeys.pm +++ b/plugins/gnome_mmkeys.pm @@ -24,16 +24,12 @@ my %Names= mate => 'org.mate.SettingsDaemon /org/mate/SettingsDaemon/MediaKeys', ); -my $bus= Net::DBus->find; -my ($service,$object); +my $object; for my $desktop (qw/gnome mate ognome/) -{ my ($name,$path)= split /\s+/, $Names{$desktop}; - eval - { $service= $bus->get_service($name); - $object = $service->get_object($path); - $object->connect_to_signal(MediaPlayerKeyPressed => \&callback); - }; - last if $object && !$@; +{ if ($object= GMB::DBus::simple_call($Names{$desktop})) + { $object->connect_to_signal(MediaPlayerKeyPressed => \&callback); + last + } } die "Can't find the dbus Settings Daemon for gnome or MATE\n" if $@;