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.6 by root, Tue Jun 5 13:56:42 2012 UTC vs.
Revision 1.23 by root, Thu Jun 7 10:22:55 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines