| 1 | #! /bin/false | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # Copyright (C) 2021-2026 Guido Flohr <guido.flohr@cantanea.com>, | ||||||||||||||
| 4 | # all rights reserved. | ||||||||||||||
| 5 | |||||||||||||||
| 6 | # This program is free software. It comes without any warranty, to | ||||||||||||||
| 7 | # the extent permitted by applicable law. You can redistribute it | ||||||||||||||
| 8 | # and/or modify it under the terms of the Do What the Fuck You Want | ||||||||||||||
| 9 | # to Public License, Version 2, as published by Sam Hocevar. See | ||||||||||||||
| 10 | # http://www.wtfpl.net/ for more details. | ||||||||||||||
| 11 | |||||||||||||||
| 12 | package Chess::Plisco::Engine::InputWatcher; | ||||||||||||||
| 13 | 1 | $Chess::Plisco::Engine::InputWatcher::VERSION = 'v1.0.3'; | |||||||||||||
| 14 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 1 | ▶ | use IO::Select; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 1 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 19 | 1 | my ($class, $fh) = @_; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 1 | $fh->autoflush(1); | |||||||||||||
| 22 | 1 | my $sel = IO::Select->new($fh); | |||||||||||||
| 23 | |||||||||||||||
| 24 | 1 | bless { | |||||||||||||
| 25 | __handle => $fh, | ||||||||||||||
| 26 | __sel => $sel, | ||||||||||||||
| 27 | __input => '', | ||||||||||||||
| 28 | }, $class; | ||||||||||||||
| 29 | } | ||||||||||||||
| 30 | |||||||||||||||
| 31 | 1 | ▶ | sub setBatchMode { | ||||||||||||
|
Subroutine
setBatchMode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 32 | 1 | my ($self, $mode) = @_; | |||||||||||||
| 33 | |||||||||||||||
| 34 | 1 | $self->{__batch_mode} = $mode; | |||||||||||||
| 35 | |||||||||||||||
| 36 | 1 | return $self; | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | |||||||||||||||
| 39 | 0 | ▶ | sub onInput { | ||||||||||||
|
Subroutine
onInput: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 40 | 0 | my ($self, $cb) = @_; | |||||||||||||
| 41 | |||||||||||||||
| 42 | 0 | $self->{__on_input} = $cb; | |||||||||||||
| 43 | } | ||||||||||||||
| 44 | |||||||||||||||
| 45 | 0 | ▶ | sub onEof { | ||||||||||||
|
Subroutine
onEof: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 46 | 0 | my ($self, $cb) = @_; | |||||||||||||
| 47 | |||||||||||||||
| 48 | 0 | $self->{__on_eof} = $cb; | |||||||||||||
| 49 | |||||||||||||||
| 50 | 0 | return $self; | |||||||||||||
| 51 | } | ||||||||||||||
| 52 | |||||||||||||||
| 53 | 3 | ▶ | sub check { | ||||||||||||
|
Subroutine
check: called CC=9 SCAR=41.8
Pod
undocumented
| |||||||||||||||
| 54 | 3 | my ($self, $tree) = @_; | |||||||||||||
| 55 | |||||||||||||||
| 56 | 3 | ▶ | return if $self->{__batch_mode}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | |||||||||||||||
| 58 | 0 | ▶ | if (delete $self->{__stop_requested}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 0 | $tree->{stop_requested} = 1; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 0 | ▶ | while (my @ready = $self->{__sel}->can_read(0)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | 0 | foreach my $fh (@ready) { | |||||||||||||
| 64 | 0 | my $offset = length $self->{__input}; | |||||||||||||
| 65 | 0 | my $bytes = $fh->sysread($self->{__input}, 1, $offset); | |||||||||||||
| 66 | 0 | ▶ | if (!$bytes) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 67 | 0 | ▶ | $self->{__on_eof}->() if $self->{__on_eof}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | 0 | return; | |||||||||||||
| 69 | } elsif ($self->{__input} =~ s/^(.*?)\n//) { | ||||||||||||||
| 70 | 0 | ▶ | $self->{__on_input}->($1) if $self->{__on_input}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | } | ||||||||||||||
| 72 | } | ||||||||||||||
| 73 | } | ||||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 0 | ▶ | sub requestStop { | ||||||||||||
|
Subroutine
requestStop: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 77 | 0 | my ($self) = @_; | |||||||||||||
| 78 | |||||||||||||||
| 79 | 0 | $self->{__stop_requested} = 1; | |||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 1 | 1; | |||||||||||||