ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/background
(Generate patch)

Comparing rxvt-unicode/src/perl/background (file contents):
Revision 1.3 by root, Tue Jun 5 12:08:23 2012 UTC vs.
Revision 1.26 by root, Thu Jun 7 11:34:09 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines