1 |
#! perl |
2 |
|
3 |
our $EXPR = 'move load "/root/pix/das_fette_schwein.jpg", repeat_wrap, X, Y'; |
4 |
$EXPR = ' |
5 |
rotate W, H, 50, 50, counter 1/60, repeat_mirror, |
6 |
clip X, Y, W, H, repeat_mirror, |
7 |
load "/root/pix/das_fette_schwein.jpg" |
8 |
'; |
9 |
#$EXPR = 'blur root, 10, 10' |
10 |
#$EXPR = 'blur move (root, -x, -y), 5, 5' |
11 |
#resize load "/root/pix/das_fette_schwein.jpg", w, h |
12 |
|
13 |
use Safe; |
14 |
|
15 |
our ($bgdsl_self, $old, $new); |
16 |
our ($l, $t, $w, $h); |
17 |
|
18 |
{ |
19 |
package urxvt::bgdsl; # background language |
20 |
|
21 |
*repeat_black = \&urxvt::RepeatNone; #TODO wtf |
22 |
*repeat_wrap = \&urxvt::RepeatNormal; |
23 |
*repeat_pad = \&urxvt::RepeatPad; |
24 |
*repeat_mirror = \&urxvt::RepeatReflect; |
25 |
|
26 |
sub load($) { |
27 |
my ($path) = @_; |
28 |
|
29 |
$new->{load}{$path} = $old->{load}{$path} || $bgdsl_self->new_img_from_file ($path); |
30 |
} |
31 |
|
32 |
sub root() { |
33 |
die "root op not supported, exg, we need you"; |
34 |
} |
35 |
|
36 |
# sub clone($) { |
37 |
# $_[0]->clone |
38 |
# } |
39 |
|
40 |
sub clip($$$$$;$) { |
41 |
my $img = pop; |
42 |
$img->sub_rect ($_[0], $_[1], $_[2], $_[3], $_[4]) |
43 |
} |
44 |
|
45 |
sub resize($$$) { |
46 |
my $img = pop; |
47 |
$img->scale ($_[0], $_[1]) |
48 |
} |
49 |
|
50 |
# TODO: ugly |
51 |
sub move($$;$) { |
52 |
my $img = pop; |
53 |
$img->sub_rect ( |
54 |
$_[0], $_[1], |
55 |
$img->w, $img->h, |
56 |
$_[2], |
57 |
) |
58 |
} |
59 |
|
60 |
sub rotate($$$$$$;$) { |
61 |
my $img = pop; |
62 |
$img->rotate ( |
63 |
$_[0], |
64 |
$_[1], |
65 |
$_[2] * $img->w * .01, |
66 |
$_[3] * $img->h * .01, |
67 |
$_[4] * (3.14159265 / 180), |
68 |
$_[5], |
69 |
) |
70 |
} |
71 |
|
72 |
sub blur($$$) { |
73 |
my ($rh, $rv, $img) = @_; |
74 |
|
75 |
$img = $img->clone; |
76 |
$img->blur ($rh, $rv); |
77 |
$img |
78 |
} |
79 |
|
80 |
sub contrast($$;$$;$) { |
81 |
my $img = pop; |
82 |
my ($r, $g, $b, $a) = @_; |
83 |
|
84 |
($g, $b) = ($r, $r) if @_ < 4; |
85 |
$a = 1 if @_ < 5; |
86 |
|
87 |
$img = $img->clone; |
88 |
$img->contrast ($r, $g, $b, $a); |
89 |
$img |
90 |
} |
91 |
|
92 |
sub brightness($$;$$;$) { |
93 |
my $img = pop; |
94 |
my ($r, $g, $b, $a) = @_; |
95 |
|
96 |
($g, $b) = ($r, $r) if @_ < 4; |
97 |
$a = 1 if @_ < 5; |
98 |
|
99 |
$img = $img->clone; |
100 |
$img->brightness ($r, $g, $b, $a); |
101 |
$img |
102 |
} |
103 |
|
104 |
sub X() { $new->{position_sensitive} = 1; $l } |
105 |
sub Y() { $new->{position_sensitive} = 1; $t } |
106 |
sub W() { $new->{size_sensitive} = 1; $w } |
107 |
sub H() { $new->{size_sensitive} = 1; $h } |
108 |
|
109 |
sub now() { urxvt::NOW } |
110 |
|
111 |
sub again($) { |
112 |
$new->{again} = $_[0]; |
113 |
} |
114 |
|
115 |
sub counter($) { |
116 |
$new->{again} = $_[0]; |
117 |
$bgdsl_self->{counter} + 0 |
118 |
} |
119 |
} |
120 |
|
121 |
sub parse_expr { |
122 |
my $expr = eval "sub {\npackage urxvt::bgdsl;\n#line 0 'background expression'\n$_[0]\n}"; |
123 |
die if $@; |
124 |
$expr |
125 |
} |
126 |
|
127 |
# compiles a parsed expression |
128 |
sub set_expr { |
129 |
my ($self, $expr) = @_; |
130 |
|
131 |
$self->{expr} = $expr; |
132 |
$self->recalculate; |
133 |
} |
134 |
|
135 |
# evaluate the current bg expression |
136 |
sub recalculate { |
137 |
my ($self) = @_; |
138 |
|
139 |
#TODO: rate limit calls |
140 |
|
141 |
local $bgdsl_self = $self; |
142 |
|
143 |
local $old = $self->{state}; |
144 |
local $new = my $state = $self->{state} = {}; |
145 |
|
146 |
($l, $t, $w, $h) = |
147 |
$self->get_geometry; |
148 |
|
149 |
my $img = eval { $self->{expr}->() }; |
150 |
warn $@ if $@;#d# |
151 |
|
152 |
my $repeat; |
153 |
|
154 |
if (my $again = $state->{again}) { |
155 |
$repeat = 1; |
156 |
$state->{timer} = $again == $old->{again} |
157 |
? $old->{timer} |
158 |
: urxvt::timer->new->after ($again)->interval ($again)->cb (sub { |
159 |
++$self->{counter}; |
160 |
$self->recalculate |
161 |
}); |
162 |
} |
163 |
|
164 |
if (delete $state->{position_sensitive}) { |
165 |
$repeat = 1; |
166 |
$self->enable (position_change => sub { $_[0]->recalculate }); |
167 |
} else { |
168 |
$self->disable ("position_change"); |
169 |
} |
170 |
|
171 |
if (delete $state->{size_sensitive}) { |
172 |
$repeat = 1; |
173 |
$self->enable (size_change => sub { $_[0]->recalculate }); |
174 |
} else { |
175 |
$self->disable ("size_change"); |
176 |
} |
177 |
|
178 |
%$old = (); |
179 |
|
180 |
unless ($repeat) { |
181 |
delete $self->{state}; |
182 |
delete $self->{expr}; |
183 |
} |
184 |
|
185 |
$img = $img->sub_rect (0, 0, $w, $h) |
186 |
if $img->w != $w || $img->h != $h; |
187 |
|
188 |
$self->set_background ($img); |
189 |
$self->scr_recolour (0); |
190 |
$self->want_refresh; |
191 |
} |
192 |
|
193 |
sub on_start { |
194 |
my ($self) = @_; |
195 |
|
196 |
$self->set_expr (parse_expr $EXPR); |
197 |
|
198 |
() |
199 |
} |
200 |
|