ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/background
Revision: 1.7
Committed: Tue Jun 5 14:04:58 2012 UTC (11 years, 11 months ago) by root
Branch: MAIN
Changes since 1.6: +34 -22 lines
Log Message:
hmmhmm

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
3 root 1.4 our $EXPR = 'move load "/root/pix/das_fette_schwein.jpg", repeat_wrap, X, Y';
4 root 1.7 $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 root 1.1 #$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 root 1.3 our ($bgdsl_self, $old, $new);
16     our ($l, $t, $w, $h);
17    
18 root 1.1 {
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 root 1.2 sub load($) {
27 root 1.1 my ($path) = @_;
28    
29 root 1.3 $new->{load}{$path} = $old->{load}{$path} || $bgdsl_self->new_img_from_file ($path);
30 root 1.1 }
31    
32 root 1.2 sub root() {
33 root 1.1 die "root op not supported, exg, we need you";
34     }
35    
36 root 1.4 # sub clone($) {
37     # $_[0]->clone
38     # }
39    
40 root 1.7 sub clip($$$$$;$) {
41     my $img = pop;
42     $img->sub_rect ($_[0], $_[1], $_[2], $_[3], $_[4])
43 root 1.4 }
44    
45 root 1.2 sub resize($$$) {
46 root 1.7 my $img = pop;
47     $img->scale ($_[0], $_[1])
48 root 1.1 }
49    
50 root 1.7 # TODO: ugly
51     sub move($$;$) {
52     my $img = pop;
53     $img->sub_rect (
54     $_[0], $_[1],
55     $img->w, $img->h,
56     $_[2],
57 root 1.1 )
58     }
59    
60 root 1.4 sub rotate($$$$$$;$) {
61 root 1.7 my $img = pop;
62     $img->rotate (
63     $_[0],
64 root 1.4 $_[1],
65 root 1.7 $_[2] * $img->w * .01,
66     $_[3] * $img->h * .01,
67     $_[4] * (3.14159265 / 180),
68     $_[5],
69 root 1.4 )
70 root 1.1 }
71    
72 root 1.2 sub blur($$$) {
73 root 1.7 my ($rh, $rv, $img) = @_;
74 root 1.1
75     $img = $img->clone;
76 root 1.4 $img->blur ($rh, $rv);
77 root 1.1 $img
78     }
79    
80 root 1.2 sub contrast($$;$$;$) {
81 root 1.7 my $img = pop;
82     my ($r, $g, $b, $a) = @_;
83 root 1.4
84 root 1.1 ($g, $b) = ($r, $r) if @_ < 4;
85     $a = 1 if @_ < 5;
86 root 1.4
87 root 1.1 $img = $img->clone;
88     $img->contrast ($r, $g, $b, $a);
89     $img
90     }
91    
92 root 1.2 sub brightness($$;$$;$) {
93 root 1.7 my $img = pop;
94     my ($r, $g, $b, $a) = @_;
95 root 1.4
96 root 1.1 ($g, $b) = ($r, $r) if @_ < 4;
97     $a = 1 if @_ < 5;
98 root 1.4
99 root 1.1 $img = $img->clone;
100     $img->brightness ($r, $g, $b, $a);
101     $img
102     }
103    
104 root 1.4 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 root 1.3
109 root 1.2 sub now() { urxvt::NOW }
110 root 1.1
111 root 1.2 sub again($) {
112 root 1.1 $new->{again} = $_[0];
113     }
114    
115 root 1.2 sub counter($) {
116 root 1.1 $new->{again} = $_[0];
117 root 1.7 $bgdsl_self->{counter} + 0
118 root 1.1 }
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 root 1.6 #TODO: rate limit calls
140    
141 root 1.3 local $bgdsl_self = $self;
142 root 1.1
143 root 1.3 local $old = $self->{state};
144     local $new = my $state = $self->{state} = {};
145 root 1.1
146 root 1.3 ($l, $t, $w, $h) =
147 root 1.1 $self->get_geometry;
148    
149     my $img = eval { $self->{expr}->() };
150     warn $@ if $@;#d#
151    
152 root 1.2 my $repeat;
153    
154 root 1.1 if (my $again = $state->{again}) {
155 root 1.2 $repeat = 1;
156 root 1.6 $state->{timer} = $again == $old->{again}
157     ? $old->{timer}
158 root 1.7 : urxvt::timer->new->after ($again)->interval ($again)->cb (sub {
159     ++$self->{counter};
160     $self->recalculate
161     });
162 root 1.1 }
163    
164 root 1.2 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 root 1.6 %$old = ();
179    
180 root 1.5 unless ($repeat) {
181     delete $self->{state};
182     delete $self->{expr};
183     }
184    
185 root 1.4 $img = $img->sub_rect (0, 0, $w, $h)
186     if $img->w != $w || $img->h != $h;
187 root 1.1
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