diff options
author | dakkar <dakkar@thenautilus.net> | 2021-11-28 15:21:34 +0000 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2021-11-28 15:21:34 +0000 |
commit | 10f2744658eeb4ccbf0342d707c08ecfb9623b91 (patch) | |
tree | c4933340cc82e6536bccf5622bc6873855a20d23 /lib/Lirc/Client.rakumod | |
parent | very rought start (diff) | |
download | media-control-10f2744658eeb4ccbf0342d707c08ecfb9623b91.tar.gz media-control-10f2744658eeb4ccbf0342d707c08ecfb9623b91.tar.bz2 media-control-10f2744658eeb4ccbf0342d707c08ecfb9623b91.zip |
move stuff around, add lirc client
Diffstat (limited to 'lib/Lirc/Client.rakumod')
-rw-r--r-- | lib/Lirc/Client.rakumod | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/Lirc/Client.rakumod b/lib/Lirc/Client.rakumod new file mode 100644 index 0000000..0065335 --- /dev/null +++ b/lib/Lirc/Client.rakumod @@ -0,0 +1,37 @@ +use v6.d; +use NativeCall; + +class Lirc::Client { + our class X::Init is Exception { + has Int $.rc; + method message { "Failed to init LIRC client: $!rc" } + } + + our class X::Send is Exception { + method message { "Failed to send command to LIRC client" } + } + + my sub lirc_get_local_socket(Str $socket is encoded('utf8'), int32 $verbose --> int32) is native('lirc_client') {*} + my sub lirc_send_one(int32 $fd, Str $remote is encoded('utf8'), Str $keysym is encoded('utf8') --> int32) is native('lirc_client') {*} + + has int $!fd; + + submethod BUILD(Str :$socket=Str, Bool :$verbose=False) { + $!fd = lirc_get_local_socket($socket, $verbose ?? 1 !! 0); + X::Init.new(rc => -$!fd).throw() if $!fd < 0; + } + + method !send-sync(Str :$remote, Str :$keysym) { + my $rc = lirc_send_one($!fd, $remote, $keysym); + X::Send.new().throw() if $rc != 0; + } + + # copied from OO::Actors + has Lock::Async $!orderer .= new; + method send(Str :$remote, Str :$keysym) { + $!orderer.lock.then({ + LEAVE $!orderer.unlock; + self!send-sync(:$remote, :$keysym); + }) + } +} |