From 515982cdbbcb42320909f63054cd5c4555ace3bc Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Wed, 23 Oct 2024 18:47:46 +0200 Subject: [PATCH] Adding autoclose to the printer configure. --- lib/Exd/Gui/PrinterConfigure.pm | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/Exd/Gui/PrinterConfigure.pm b/lib/Exd/Gui/PrinterConfigure.pm index ee0c995..d31131e 100644 --- a/lib/Exd/Gui/PrinterConfigure.pm +++ b/lib/Exd/Gui/PrinterConfigure.pm @@ -28,6 +28,7 @@ has _window => ( is => 'rw' ); has _bluetooth_printers => ( is => 'rw', default => sub { [] } ); has _usb_printers => ( is => 'rw', default => sub { [] }); has _cat_printers => ( is => 'rw', default => sub { [] }); +has _stop_timeout => ( is => 'rw' ); sub _build__select($self) { return IO::Select->new; @@ -37,11 +38,11 @@ sub _read_bluetooth_printers($self) { my @fhs = $self->_select->can_read(0); for my $fh (@fhs) { $fh->blocking(0); - while (my $line = <$fh>) { - my @return = map {$self->instance->device_hash_to_object($_)} @{JSON::from_json($line)}; + while (defined(my $line = <$fh>)) { + my @return = map {$self->app->device_hash_to_object($_)} @{JSON::from_json($line)}; $self->_bluetooth_printers(\@return); + Exd::DeviceToBluetooth->cache_printers($self->_bluetooth_printers); } - Exd::DeviceToBluetooth->cache_printers($self->_bluetooth_printers); } } @@ -115,6 +116,7 @@ sub start($self) { $self->_select->add($self->_read_from_daemon); $self->_bluetooth_printers(Exd::DeviceToBluetooth->devices_from_cache); Glib::Timeout->add(1000, sub { + return 0 if $self->_stop_timeout; eval { $self->_read_usb_printers; $self->_read_cat_printers; @@ -122,7 +124,7 @@ sub start($self) { $self->_update_box; }; if ($@) { - warn $@; + die $@; } return 1; }); @@ -133,7 +135,8 @@ sub start($self) { sub _update_box($self) { my $window = $self->_window; my $box = Gtk4::Box->new( 'vertical', 10 ); - for my $device ($self->_bluetooth_printers->@*) { + my $bluetooth_printers = $self->_bluetooth_printers; + for my $device ($bluetooth_printers->@*) { $self->_bluetoth_printer_print_to_box($device, $box); } for my $device ($self->_usb_printers->@*) { @@ -163,7 +166,7 @@ sub _start_daemon_bluetooth_search($self) { } sub _daemon_bluetooth_search_iteration($self) { - say 'Listing bluetooth devices'; +# say 'Listing bluetooth devices'; if (!kill 0, $self->app->parent_pid) { say 'Parent died at bluetooth search iteration, exiting...'; exit 1; @@ -178,13 +181,15 @@ sub _daemon_bluetooth_search_iteration($self) { push @return, Exd::DeviceToBluetooth->new(name => $devices->{$address}, address => $address, port => $search->{RFCOMM}); } } +# say 'Finishing listing bluetooth devices'; @return = map { $_->serialize } sort { $a->name cmp $b->name } @return; - $self->_write_to_app->print(JSON::to_json(\@return)); + $self->_write_to_app->say(JSON::to_json(\@return)); $self->_write_to_app->flush; } sub _on_close($self) { + $self->_stop_timeout(1); kill 'KILL', $self->_pid_daemon; waitpid $self->_pid_daemon, 0; $self->on_close->();