From 0df16a81cd49242b7db89abcb34cd1a357db5244 Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Sat, 22 Feb 2020 12:22:36 +0100 Subject: [PATCH] initial commit --- default.nix | 24 +++++++++++++++ src/config-print | 32 ++++++++++++++++++++ src/config-reload | 77 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 default.nix create mode 100644 src/config-print create mode 100644 src/config-reload diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..cefec3f --- /dev/null +++ b/default.nix @@ -0,0 +1,24 @@ +{ stdenv, perlPackages }: + +stdenv.mkDerivation rec { + pname = "urxvt-config-reload"; + version = "1.0"; + + src = ./src; + + passthru.perlPackages = with perlPackages; + [ AnyEvent LinuxFD SubExporter + DataOptList ParamsUtil SubInstall + ]; + + installPhase = '' + install -D -m644 "$src"/* -t "$out/lib/urxvt/perl" + ''; + + meta = with stdenv.lib; { + description = "A urxvt plugin that reloads the configuration on SIGUSR1"; + homepage = "https://maxwell.ydns.eu/git/rnhmjoj/urxvt-config-reload"; + maintainers = with maintainers; [ rnhmjoj ]; + platforms = platforms.unix; + }; +} diff --git a/src/config-print b/src/config-print new file mode 100644 index 0000000..e29d815 --- /dev/null +++ b/src/config-print @@ -0,0 +1,32 @@ +#! perl + +use v5.20; +use strict; +use warnings; +no warnings "experimental"; + +our @resources = ( + qw( + background foreground cursorColor + pointerColor pointerColor2 + highlight borderColor + font geometry + ), + map { "color$_" } 0..15 + ); + +sub on_start { + my %fallbacks = ( + borderColor => "background", + cursorColor => "foreground"); + + for (@resources) { + my $val = $TERM->x_resource($_); + my $default = $TERM->x_resource($fallbacks{$_}); + if (not $val and $default){ + $val = $default + } + say "$_: " . $val; + } + exit; +} diff --git a/src/config-reload b/src/config-reload new file mode 100644 index 0000000..a1a292d --- /dev/null +++ b/src/config-reload @@ -0,0 +1,77 @@ +#! perl + +use v5.20; +use strict; +use warnings; +no warnings "experimental"; + +use AnyEvent; +use Linux::FD qw(signalfd); +use POSIX; +use File::Basename qw(dirname); + +my $sigset = POSIX::SigSet->new(&POSIX::SIGUSR1); +sigprocmask(SIG_BLOCK, $sigset); +my $sigfd = signalfd($sigset); +my $libdir = dirname(__FILE__); + +our @terms = (); + +sub reload_all { + my @lines = `urxvt --perl-lib $libdir -pe config-print 2>&1`; + my %resource; + for (@lines) { + $resource{$1} = $2 if (/(.*?): (.*)/); + } + my $cmd = ""; + my $i = 0; +RESOURCE: + for (keys %resource) { + my $key; + given ($_) { + $key = "4;$1" when (/color(\d+)/); + $key = 10 when "foreground"; + $key = 11 when "background"; + $key = 12 when "cursorColor"; + $key = 13 when "pointerColor2"; + $key = 14 when "pointerColor"; + $key = 17 when "highlight"; + $key = 708 when "borderColor"; + $key = 710 when "font"; + when ("geometry") { + if (my ($col, $row) = $resource{$_} =~ /(\d+)x(\d+)/) { + $cmd .= "\e[8;${row};${col}t"; + } + next RESOURCE; + } + default { next RESOURCE } + } + my $val = $resource{$_}; + $cmd .= "\e]$key;$val\a"; + } + for (@terms) { + $_->cmd_parse($cmd); + } +} + +our $watch = AnyEvent->io ( + fh => $sigfd, # which file handle to check + poll => "r", # which event to wait for ("r"ead data) + cb => sub { # what callback to execute + sysread $sigfd, my $dummy, 4096; + reload_all; + } + ); + +sub on_start { + my ($ext) = @_; + push @terms, $TERM; + (); +} + +sub on_destroy { + my ($ext) = @_; + @terms = grep { $_ != $TERM } @terms; + if (!@terms) { undef $watch; } + (); +}