| 1 |
root |
1.1 |
#! perl |
| 2 |
|
|
|
| 3 |
root |
1.16 |
#:META:X_RESOURCE:%.expr:string:background expression |
| 4 |
|
|
#:META:X_RESOURCE:%.enable:boolean:some boolean |
| 5 |
root |
1.18 |
#:META:X_RESOURCE:%.extra.:value:extra config |
| 6 |
root |
1.12 |
|
| 7 |
root |
1.26 |
our $EXPR = 'move W * 0.1, -H * 0.1, resize W * 0.5, H * 0.5, repeat_none load "MagnoliaAlpha.png"'; |
| 8 |
root |
1.19 |
#$EXPR = ' |
| 9 |
|
|
# rotate W, H, 50, 50, counter 1/59.95, repeat_mirror, |
| 10 |
|
|
# clip X, Y, W, H, repeat_mirror, |
| 11 |
|
|
# load "/root/pix/das_fette_schwein.jpg" |
| 12 |
|
|
#'; |
| 13 |
|
|
#$EXPR = 'solid "red"'; |
| 14 |
root |
1.1 |
#$EXPR = 'blur root, 10, 10' |
| 15 |
|
|
#$EXPR = 'blur move (root, -x, -y), 5, 5' |
| 16 |
|
|
#resize load "/root/pix/das_fette_schwein.jpg", w, h |
| 17 |
|
|
|
| 18 |
|
|
use Safe; |
| 19 |
|
|
|
| 20 |
root |
1.3 |
our ($bgdsl_self, $old, $new); |
| 21 |
|
|
our ($l, $t, $w, $h); |
| 22 |
|
|
|
| 23 |
root |
1.16 |
# enforce at least this interval between updates |
| 24 |
root |
1.10 |
our $MIN_INTERVAL = 1/100; |
| 25 |
root |
1.9 |
|
| 26 |
root |
1.1 |
{ |
| 27 |
|
|
package urxvt::bgdsl; # background language |
| 28 |
|
|
|
| 29 |
root |
1.20 |
# *repeat_empty = \&urxvt::RepeatNone; |
| 30 |
|
|
# *repeat_tile = \&urxvt::RepeatNormal; |
| 31 |
|
|
# *repeat_pad = \&urxvt::RepeatPad; |
| 32 |
|
|
# *repeat_mirror = \&urxvt::RepeatReflect; |
| 33 |
root |
1.1 |
|
| 34 |
root |
1.15 |
=head2 PROVIDERS/GENERATORS |
| 35 |
|
|
|
| 36 |
|
|
=over 4 |
| 37 |
|
|
|
| 38 |
|
|
=item load $path |
| 39 |
|
|
|
| 40 |
|
|
=cut |
| 41 |
|
|
|
| 42 |
root |
1.2 |
sub load($) { |
| 43 |
root |
1.1 |
my ($path) = @_; |
| 44 |
|
|
|
| 45 |
root |
1.3 |
$new->{load}{$path} = $old->{load}{$path} || $bgdsl_self->new_img_from_file ($path); |
| 46 |
root |
1.1 |
} |
| 47 |
|
|
|
| 48 |
root |
1.2 |
sub root() { |
| 49 |
root |
1.9 |
$new->{rootpmap_sensitive} = 1; |
| 50 |
root |
1.1 |
die "root op not supported, exg, we need you"; |
| 51 |
|
|
} |
| 52 |
|
|
|
| 53 |
root |
1.15 |
sub solid($;$$) { |
| 54 |
|
|
my $img = $bgdsl_self->new_img (urxvt::PictStandardARGB32, $_[1] || 1, $_[2] || 1); |
| 55 |
|
|
$img->fill ($_[0]); |
| 56 |
|
|
$img |
| 57 |
|
|
} |
| 58 |
|
|
|
| 59 |
|
|
=back |
| 60 |
|
|
|
| 61 |
root |
1.20 |
=head2 VARIABLES |
| 62 |
|
|
|
| 63 |
|
|
=over 4 |
| 64 |
|
|
|
| 65 |
|
|
=cut |
| 66 |
|
|
|
| 67 |
|
|
sub X() { $new->{position_sensitive} = 1; $l } |
| 68 |
|
|
sub Y() { $new->{position_sensitive} = 1; $t } |
| 69 |
|
|
sub W() { $new->{size_sensitive} = 1; $w } |
| 70 |
|
|
sub H() { $new->{size_sensitive} = 1; $h } |
| 71 |
|
|
|
| 72 |
|
|
sub now() { urxvt::NOW } |
| 73 |
|
|
|
| 74 |
|
|
sub again($) { |
| 75 |
|
|
$new->{again} = $_[0]; |
| 76 |
|
|
} |
| 77 |
|
|
|
| 78 |
|
|
sub counter($) { |
| 79 |
|
|
$new->{again} = $_[0]; |
| 80 |
|
|
$bgdsl_self->{counter} + 0 |
| 81 |
|
|
} |
| 82 |
|
|
|
| 83 |
|
|
=back |
| 84 |
|
|
|
| 85 |
root |
1.15 |
=head2 OPERATORS |
| 86 |
|
|
|
| 87 |
|
|
=over 4 |
| 88 |
|
|
|
| 89 |
|
|
=cut |
| 90 |
|
|
|
| 91 |
root |
1.4 |
# sub clone($) { |
| 92 |
|
|
# $_[0]->clone |
| 93 |
|
|
# } |
| 94 |
|
|
|
| 95 |
root |
1.24 |
sub repeat_none($) { |
| 96 |
|
|
my $img = $_[0]->clone; |
| 97 |
|
|
$img->repeat_mode (urxvt::RepeatNone); |
| 98 |
|
|
$img |
| 99 |
|
|
} |
| 100 |
|
|
|
| 101 |
root |
1.20 |
sub clip($;$$;$$) { |
| 102 |
root |
1.7 |
my $img = pop; |
| 103 |
root |
1.21 |
my $h = pop || H; |
| 104 |
|
|
my $w = pop || W; |
| 105 |
|
|
$img->sub_rect ($_[0], $_[1], $w, $h) |
| 106 |
root |
1.4 |
} |
| 107 |
|
|
|
| 108 |
root |
1.2 |
sub resize($$$) { |
| 109 |
root |
1.7 |
my $img = pop; |
| 110 |
|
|
$img->scale ($_[0], $_[1]) |
| 111 |
root |
1.1 |
} |
| 112 |
|
|
|
| 113 |
root |
1.7 |
# TODO: ugly |
| 114 |
|
|
sub move($$;$) { |
| 115 |
root |
1.20 |
my $img = pop->clone; |
| 116 |
|
|
$img->move ($_[0], $_[1]); |
| 117 |
|
|
$img |
| 118 |
|
|
# my $img = pop; |
| 119 |
|
|
# $img->sub_rect ( |
| 120 |
|
|
# $_[0], $_[1], |
| 121 |
|
|
# $img->w, $img->h, |
| 122 |
|
|
# $_[2], |
| 123 |
|
|
# ) |
| 124 |
root |
1.1 |
} |
| 125 |
|
|
|
| 126 |
root |
1.20 |
sub rotate($$$$$$) { |
| 127 |
root |
1.7 |
my $img = pop; |
| 128 |
|
|
$img->rotate ( |
| 129 |
|
|
$_[0], |
| 130 |
root |
1.4 |
$_[1], |
| 131 |
root |
1.7 |
$_[2] * $img->w * .01, |
| 132 |
|
|
$_[3] * $img->h * .01, |
| 133 |
|
|
$_[4] * (3.14159265 / 180), |
| 134 |
root |
1.4 |
) |
| 135 |
root |
1.1 |
} |
| 136 |
|
|
|
| 137 |
root |
1.2 |
sub blur($$$) { |
| 138 |
root |
1.7 |
my ($rh, $rv, $img) = @_; |
| 139 |
root |
1.1 |
|
| 140 |
root |
1.4 |
$img->blur ($rh, $rv); |
| 141 |
root |
1.1 |
} |
| 142 |
|
|
|
| 143 |
root |
1.2 |
sub contrast($$;$$;$) { |
| 144 |
root |
1.7 |
my $img = pop; |
| 145 |
|
|
my ($r, $g, $b, $a) = @_; |
| 146 |
root |
1.4 |
|
| 147 |
root |
1.1 |
($g, $b) = ($r, $r) if @_ < 4; |
| 148 |
|
|
$a = 1 if @_ < 5; |
| 149 |
root |
1.4 |
|
| 150 |
root |
1.1 |
$img = $img->clone; |
| 151 |
|
|
$img->contrast ($r, $g, $b, $a); |
| 152 |
|
|
$img |
| 153 |
|
|
} |
| 154 |
|
|
|
| 155 |
root |
1.2 |
sub brightness($$;$$;$) { |
| 156 |
root |
1.7 |
my $img = pop; |
| 157 |
|
|
my ($r, $g, $b, $a) = @_; |
| 158 |
root |
1.4 |
|
| 159 |
root |
1.1 |
($g, $b) = ($r, $r) if @_ < 4; |
| 160 |
|
|
$a = 1 if @_ < 5; |
| 161 |
root |
1.4 |
|
| 162 |
root |
1.1 |
$img = $img->clone; |
| 163 |
|
|
$img->brightness ($r, $g, $b, $a); |
| 164 |
|
|
$img |
| 165 |
|
|
} |
| 166 |
|
|
|
| 167 |
root |
1.15 |
=back |
| 168 |
|
|
|
| 169 |
|
|
=cut |
| 170 |
|
|
|
| 171 |
root |
1.1 |
} |
| 172 |
|
|
|
| 173 |
|
|
sub parse_expr { |
| 174 |
|
|
my $expr = eval "sub {\npackage urxvt::bgdsl;\n#line 0 'background expression'\n$_[0]\n}"; |
| 175 |
|
|
die if $@; |
| 176 |
|
|
$expr |
| 177 |
|
|
} |
| 178 |
|
|
|
| 179 |
|
|
# compiles a parsed expression |
| 180 |
|
|
sub set_expr { |
| 181 |
|
|
my ($self, $expr) = @_; |
| 182 |
|
|
|
| 183 |
|
|
$self->{expr} = $expr; |
| 184 |
|
|
$self->recalculate; |
| 185 |
|
|
} |
| 186 |
|
|
|
| 187 |
|
|
# evaluate the current bg expression |
| 188 |
|
|
sub recalculate { |
| 189 |
|
|
my ($self) = @_; |
| 190 |
|
|
|
| 191 |
root |
1.10 |
# rate limit evaluation |
| 192 |
|
|
|
| 193 |
root |
1.9 |
if ($self->{next_refresh} > urxvt::NOW) { |
| 194 |
|
|
$self->{next_refresh_timer} = urxvt::timer->new->after ($self->{next_refresh} - urxvt::NOW)->cb (sub { |
| 195 |
|
|
$self->recalculate; |
| 196 |
|
|
}); |
| 197 |
root |
1.12 |
return; |
| 198 |
root |
1.9 |
} |
| 199 |
|
|
|
| 200 |
|
|
$self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL; |
| 201 |
|
|
|
| 202 |
root |
1.10 |
# set environment to evaluate user expression |
| 203 |
root |
1.6 |
|
| 204 |
root |
1.3 |
local $bgdsl_self = $self; |
| 205 |
root |
1.1 |
|
| 206 |
root |
1.3 |
local $old = $self->{state}; |
| 207 |
|
|
local $new = my $state = $self->{state} = {}; |
| 208 |
root |
1.1 |
|
| 209 |
root |
1.3 |
($l, $t, $w, $h) = |
| 210 |
root |
1.1 |
$self->get_geometry; |
| 211 |
|
|
|
| 212 |
root |
1.22 |
warn "$l,$t,$w,$h\n";#d# |
| 213 |
|
|
|
| 214 |
root |
1.10 |
# evaluate user expression |
| 215 |
|
|
|
| 216 |
root |
1.1 |
my $img = eval { $self->{expr}->() }; |
| 217 |
|
|
warn $@ if $@;#d# |
| 218 |
root |
1.15 |
die if !UNIVERSAL::isa $img, "urxvt::img"; |
| 219 |
root |
1.1 |
|
| 220 |
root |
1.10 |
# if the expression is sensitive to external events, prepare reevaluation then |
| 221 |
|
|
|
| 222 |
root |
1.2 |
my $repeat; |
| 223 |
|
|
|
| 224 |
root |
1.1 |
if (my $again = $state->{again}) { |
| 225 |
root |
1.2 |
$repeat = 1; |
| 226 |
root |
1.6 |
$state->{timer} = $again == $old->{again} |
| 227 |
|
|
? $old->{timer} |
| 228 |
root |
1.7 |
: urxvt::timer->new->after ($again)->interval ($again)->cb (sub { |
| 229 |
|
|
++$self->{counter}; |
| 230 |
|
|
$self->recalculate |
| 231 |
|
|
}); |
| 232 |
root |
1.1 |
} |
| 233 |
|
|
|
| 234 |
root |
1.2 |
if (delete $state->{position_sensitive}) { |
| 235 |
|
|
$repeat = 1; |
| 236 |
|
|
$self->enable (position_change => sub { $_[0]->recalculate }); |
| 237 |
|
|
} else { |
| 238 |
|
|
$self->disable ("position_change"); |
| 239 |
|
|
} |
| 240 |
|
|
|
| 241 |
|
|
if (delete $state->{size_sensitive}) { |
| 242 |
|
|
$repeat = 1; |
| 243 |
|
|
$self->enable (size_change => sub { $_[0]->recalculate }); |
| 244 |
|
|
} else { |
| 245 |
|
|
$self->disable ("size_change"); |
| 246 |
|
|
} |
| 247 |
|
|
|
| 248 |
root |
1.9 |
if (delete $state->{rootpmap_sensitive}) { |
| 249 |
|
|
$repeat = 1; |
| 250 |
|
|
$self->enable (rootpmap_change => sub { $_[0]->recalculate }); |
| 251 |
|
|
} else { |
| 252 |
|
|
$self->disable ("rootpmap_change"); |
| 253 |
|
|
} |
| 254 |
|
|
|
| 255 |
root |
1.10 |
# clear stuff we no longer need |
| 256 |
|
|
|
| 257 |
root |
1.6 |
%$old = (); |
| 258 |
|
|
|
| 259 |
root |
1.5 |
unless ($repeat) { |
| 260 |
|
|
delete $self->{state}; |
| 261 |
|
|
delete $self->{expr}; |
| 262 |
|
|
} |
| 263 |
|
|
|
| 264 |
root |
1.10 |
# prepare and set background pixmap |
| 265 |
|
|
|
| 266 |
root |
1.4 |
$img = $img->sub_rect (0, 0, $w, $h) |
| 267 |
|
|
if $img->w != $w || $img->h != $h; |
| 268 |
root |
1.1 |
|
| 269 |
|
|
$self->set_background ($img); |
| 270 |
|
|
$self->scr_recolour (0); |
| 271 |
|
|
$self->want_refresh; |
| 272 |
|
|
} |
| 273 |
|
|
|
| 274 |
|
|
sub on_start { |
| 275 |
|
|
my ($self) = @_; |
| 276 |
|
|
|
| 277 |
|
|
$self->set_expr (parse_expr $EXPR); |
| 278 |
|
|
|
| 279 |
|
|
() |
| 280 |
|
|
} |
| 281 |
|
|
|