1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | #:META:RESOURCE:%.expr:string:background expression |
3 | #:META:RESOURCE:%.expr:string:background expression |
4 | #:META:RESOURCE:%.border:boolean:respect the terminal border |
4 | #:META:RESOURCE:%.border:boolean:respect the terminal border |
5 | #:META:RESOURCE:%.interval:seconds:minimum time between updates |
5 | #:META:RESOURCE:%.interval:seconds:minimum time between updates |
|
|
6 | #:META:RESOURCE:pixmap:file[;geom]:set image as background |
|
|
7 | #:META:RESOURCE:backgroundPixmap:file[;geom]:set image as background |
|
|
8 | #:META:RESOURCE:tr:boolean:set root pixmap as background |
|
|
9 | #:META:RESOURCE:transparent:boolean:set root pixmap as background |
|
|
10 | #:META:RESOURCE:tint:color:tint background with color |
|
|
11 | #:META:RESOURCE:tintColor:color:tint background with color |
|
|
12 | #:META:RESOURCE:sh:number:shade background by number % |
|
|
13 | #:META:RESOURCE:shading:number:shade background by number % |
|
|
14 | #:META:RESOURCE:blr:HxV:gaussian-blur background with radii |
|
|
15 | #:META:RESOURCE:blurRadius:HxV:gaussian-blur background with radii |
6 | |
16 | |
7 | =head1 NAME |
17 | =head1 NAME |
8 | |
18 | |
9 | background - manage terminal background |
19 | background - manage terminal background |
10 | |
20 | |
… | |
… | |
54 | |
64 | |
55 | urxvt --background-expr 'scale keep { load "/path/to/mybg.png" }' |
65 | urxvt --background-expr 'scale keep { load "/path/to/mybg.png" }' |
56 | |
66 | |
57 | Or specified as a X resource: |
67 | Or specified as a X resource: |
58 | |
68 | |
59 | URxvt.background-expr: scale keep { load "/path/to/mybg.png" } |
69 | URxvt.background.expr: scale keep { load "/path/to/mybg.png" } |
60 | |
70 | |
61 | =head1 THEORY OF OPERATION |
71 | =head1 THEORY OF OPERATION |
62 | |
72 | |
63 | At startup, just before the window is mapped for the first time, the |
73 | At startup, just before the window is mapped for the first time, the |
64 | expression is evaluated and must yield an image. The image is then |
74 | expression is evaluated and must yield an image. The image is then |
… | |
… | |
132 | get a percentage): |
142 | get a percentage): |
133 | |
143 | |
134 | scale 2, load "$HOME/mypic.png" |
144 | scale 2, load "$HOME/mypic.png" |
135 | |
145 | |
136 | This enlarges the image by a factor of 2 (200%). As you can see, C<scale> |
146 | This enlarges the image by a factor of 2 (200%). As you can see, C<scale> |
137 | has now two arguments, the C<200> and the C<load> expression, while |
147 | has now two arguments, the C<2> and the C<load> expression, while |
138 | C<load> only has one argument. Arguments are separated from each other by |
148 | C<load> only has one argument. Arguments are separated from each other by |
139 | commas. |
149 | commas. |
140 | |
150 | |
141 | Scale also accepts two arguments, which are then separate factors for both |
151 | Scale also accepts two arguments, which are then separate factors for both |
142 | horizontal and vertical dimensions. For example, this halves the image |
152 | horizontal and vertical dimensions. For example, this halves the image |
… | |
… | |
1021 | |
1031 | |
1022 | This leaves the question of how to force reevaluation of the block, |
1032 | This leaves the question of how to force reevaluation of the block, |
1023 | in case the root background changes: If expression inside the block |
1033 | in case the root background changes: If expression inside the block |
1024 | is sensitive to some event (root background changes, window geometry |
1034 | is sensitive to some event (root background changes, window geometry |
1025 | changes), then it will be reevaluated automatically as needed. |
1035 | changes), then it will be reevaluated automatically as needed. |
|
|
1036 | |
|
|
1037 | =back |
|
|
1038 | |
|
|
1039 | =head1 OLD BACKGROUND IMAGE SETTINGS |
|
|
1040 | |
|
|
1041 | This extension also provides support for the old options/resources and |
|
|
1042 | OSC sequences for setting a background image. These settings are |
|
|
1043 | B<deprecated> and will be removed in future versions. |
|
|
1044 | |
|
|
1045 | =head2 OPTIONS AND RESOURCES |
|
|
1046 | |
|
|
1047 | =over 4 |
|
|
1048 | |
|
|
1049 | =item B<-pixmap> I<file[;oplist]> |
|
|
1050 | |
|
|
1051 | =item B<backgroundPixmap:> I<file[;oplist]> |
|
|
1052 | |
|
|
1053 | Use the specified image file as the window's background and also |
|
|
1054 | optionally specify a colon separated list of operations to modify it. |
|
|
1055 | Note that you may need to quote the C<;> character when using the |
|
|
1056 | command line option, as C<;> is usually a metacharacter in shells. |
|
|
1057 | Supported operations are: |
|
|
1058 | |
|
|
1059 | =over 4 |
|
|
1060 | |
|
|
1061 | =item B<WxH+X+Y> |
|
|
1062 | |
|
|
1063 | sets scale and position. B<"W" / "H"> specify the horizontal/vertical |
|
|
1064 | scale (percent), and B<"X" / "Y"> locate the image centre (percent). A |
|
|
1065 | scale of 0 disables scaling. |
|
|
1066 | |
|
|
1067 | =item B<op=tile> |
|
|
1068 | |
|
|
1069 | enables tiling |
|
|
1070 | |
|
|
1071 | =item B<op=keep-aspect> |
|
|
1072 | |
|
|
1073 | maintain the image aspect ratio when scaling |
|
|
1074 | |
|
|
1075 | =item B<op=root-align> |
|
|
1076 | |
|
|
1077 | use the position of the terminal window relative to the root window as |
|
|
1078 | the image offset, simulating a root window background |
|
|
1079 | |
|
|
1080 | =back |
|
|
1081 | |
|
|
1082 | The default scale and position setting is C<100x100+50+50>. |
|
|
1083 | Alternatively, a predefined set of templates can be used to achieve |
|
|
1084 | the most common setups: |
|
|
1085 | |
|
|
1086 | =over 4 |
|
|
1087 | |
|
|
1088 | =item B<style=tiled> |
|
|
1089 | |
|
|
1090 | the image is tiled with no scaling. Equivalent to 0x0+0+0:op=tile |
|
|
1091 | |
|
|
1092 | =item B<style=aspect-stretched> |
|
|
1093 | |
|
|
1094 | the image is scaled to fill the whole window maintaining the aspect |
|
|
1095 | ratio and centered. Equivalent to 100x100+50+50:op=keep-aspect |
|
|
1096 | |
|
|
1097 | =item B<style=stretched> |
|
|
1098 | |
|
|
1099 | the image is scaled to fill the whole window. Equivalent to 100x100 |
|
|
1100 | |
|
|
1101 | =item B<style=centered> |
|
|
1102 | |
|
|
1103 | the image is centered with no scaling. Equivalent to 0x0+50+50 |
|
|
1104 | |
|
|
1105 | =item B<style=root-tiled> |
|
|
1106 | |
|
|
1107 | the image is tiled with no scaling and using 'root' positioning. |
|
|
1108 | Equivalent to 0x0:op=tile:op=root-align |
|
|
1109 | |
|
|
1110 | =back |
|
|
1111 | |
|
|
1112 | If multiple templates are specified the last one wins. Note that a |
|
|
1113 | template overrides all the scale, position and operations settings. |
|
|
1114 | |
|
|
1115 | If used in conjunction with pseudo-transparency, the specified image |
|
|
1116 | will be blended over the transparent background using alpha-blending. |
|
|
1117 | |
|
|
1118 | =item B<-tr>|B<+tr> |
|
|
1119 | |
|
|
1120 | =item B<transparent:> I<boolean> |
|
|
1121 | |
|
|
1122 | Turn on/off pseudo-transparency by using the root pixmap as background. |
|
|
1123 | |
|
|
1124 | =item B<-tint> I<colour> |
|
|
1125 | |
|
|
1126 | =item B<tintColor:> I<colour> |
|
|
1127 | |
|
|
1128 | Tint the transparent background with the given colour. Note that a |
|
|
1129 | black tint yields a completely black image while a white tint yields |
|
|
1130 | the image unchanged. |
|
|
1131 | |
|
|
1132 | =item B<-sh> I<number> |
|
|
1133 | |
|
|
1134 | =item B<shading:> I<number> |
|
|
1135 | |
|
|
1136 | Darken (0 .. 99) or lighten (101 .. 200) the transparent background. |
|
|
1137 | A value of 100 means no shading. |
|
|
1138 | |
|
|
1139 | =item B<-blr> I<HxV> |
|
|
1140 | |
|
|
1141 | =item B<blurRadius:> I<HxV> |
|
|
1142 | |
|
|
1143 | Apply gaussian blur with the specified radius to the transparent |
|
|
1144 | background. If a single number is specified, the vertical and |
|
|
1145 | horizontal radii are considered to be the same. Setting one of the |
|
|
1146 | radii to 1 and the other to a large number creates interesting effects |
|
|
1147 | on some backgrounds. The maximum radius value is 128. An horizontal or |
|
|
1148 | vertical radius of 0 disables blurring. |
|
|
1149 | |
|
|
1150 | =back |
|
|
1151 | |
|
|
1152 | =head2 OSC sequences |
|
|
1153 | |
|
|
1154 | =over 4 |
|
|
1155 | |
|
|
1156 | =item B<< C<ESC ] 705 ; Pt ST> >> Change transparent background tint colour to B<< C<Pt> >>. |
|
|
1157 | |
|
|
1158 | =item B<< C<ESC ] 20 ; Pt ST> >> Change/Query background image |
|
|
1159 | parameters: the value of B<< C<Pt> >> can be one of the following |
|
|
1160 | commands: |
|
|
1161 | |
|
|
1162 | =over 4 |
|
|
1163 | |
|
|
1164 | =item B<< C<?> >> |
|
|
1165 | |
|
|
1166 | display scale and position in the title |
|
|
1167 | |
|
|
1168 | =item B<< C<;WxH+X+Y> >> |
|
|
1169 | |
|
|
1170 | change scale and/or position |
|
|
1171 | |
|
|
1172 | =item B<< C<FILE;WxH+X+Y> >> |
|
|
1173 | |
|
|
1174 | change background image |
|
|
1175 | |
|
|
1176 | =back |
1026 | |
1177 | |
1027 | =cut |
1178 | =cut |
1028 | |
1179 | |
1029 | sub keep(&) { |
1180 | sub keep(&) { |
1030 | my $id = $_[0]+0; |
1181 | my $id = $_[0]+0; |
… | |
… | |
1190 | $self->set_background ($img, $self->{border}); |
1341 | $self->set_background ($img, $self->{border}); |
1191 | $self->scr_recolor (0); |
1342 | $self->scr_recolor (0); |
1192 | $self->want_refresh; |
1343 | $self->want_refresh; |
1193 | } |
1344 | } |
1194 | |
1345 | |
|
|
1346 | sub old_bg_opts { |
|
|
1347 | my ($self, $arg) = @_; |
|
|
1348 | |
|
|
1349 | $arg or return; |
|
|
1350 | |
|
|
1351 | my @str = split /;/, $arg; |
|
|
1352 | |
|
|
1353 | return unless $str[0] or $self->{bg_opts}->{path}; |
|
|
1354 | |
|
|
1355 | my $bg_opts = $self->{bg_opts}; |
|
|
1356 | |
|
|
1357 | if ($str[0]) { |
|
|
1358 | $bg_opts->{tile} = 0; |
|
|
1359 | $bg_opts->{keep_aspect} = 0; |
|
|
1360 | $bg_opts->{root_align} = 0; |
|
|
1361 | $bg_opts->{h_scale} = $bg_opts->{v_scale} = 100; |
|
|
1362 | $bg_opts->{h_align} = $bg_opts->{v_align} = 50; |
|
|
1363 | $bg_opts->{path} = unpack "H*", $str[0]; |
|
|
1364 | } |
|
|
1365 | |
|
|
1366 | my @oplist = split /:/, $str[1]; |
|
|
1367 | |
|
|
1368 | for (@oplist) { |
|
|
1369 | if (/style=tiled/i) { |
|
|
1370 | $bg_opts->{tile} = 1; |
|
|
1371 | $bg_opts->{keep_aspect} = 0; |
|
|
1372 | $bg_opts->{root_align} = 0; |
|
|
1373 | $bg_opts->{h_scale} = $bg_opts->{v_scale} = 0; |
|
|
1374 | $bg_opts->{h_align} = $bg_opts->{v_align} = 0; |
|
|
1375 | } elsif (/style=aspect-stretched/i) { |
|
|
1376 | $bg_opts->{tile} = 0; |
|
|
1377 | $bg_opts->{keep_aspect} = 1; |
|
|
1378 | $bg_opts->{root_align} = 0; |
|
|
1379 | $bg_opts->{h_scale} = $bg_opts->{v_scale} = 100; |
|
|
1380 | $bg_opts->{h_align} = $bg_opts->{v_align} = 50; |
|
|
1381 | } elsif (/style=stretched/i) { |
|
|
1382 | $bg_opts->{tile} = 0; |
|
|
1383 | $bg_opts->{keep_aspect} = 0; |
|
|
1384 | $bg_opts->{root_align} = 0; |
|
|
1385 | $bg_opts->{h_scale} = $bg_opts->{v_scale} = 100; |
|
|
1386 | $bg_opts->{h_align} = $bg_opts->{v_align} = 50; |
|
|
1387 | } elsif (/style=centered/i) { |
|
|
1388 | $bg_opts->{tile} = 0; |
|
|
1389 | $bg_opts->{keep_aspect} = 0; |
|
|
1390 | $bg_opts->{root_align} = 0; |
|
|
1391 | $bg_opts->{h_scale} = $bg_opts->{v_scale} = 0; |
|
|
1392 | $bg_opts->{h_align} = $bg_opts->{v_align} = 50; |
|
|
1393 | } elsif (/style=root-tiled/i) { |
|
|
1394 | $bg_opts->{tile} = 1; |
|
|
1395 | $bg_opts->{keep_aspect} = 0; |
|
|
1396 | $bg_opts->{root_align} = 1; |
|
|
1397 | $bg_opts->{h_scale} = $bg_opts->{v_scale} = 0; |
|
|
1398 | $bg_opts->{h_align} = $bg_opts->{v_align} = 0; |
|
|
1399 | } elsif (/op=tile/i) { |
|
|
1400 | $bg_opts->{tile} = 1; |
|
|
1401 | } elsif (/op=keep_aspect/i) { |
|
|
1402 | $bg_opts->{keep_aspect} = 1; |
|
|
1403 | } elsif (/op=root_align/i) { |
|
|
1404 | $bg_opts->{root_align} = 1; |
|
|
1405 | } elsif (/^ =? ([0-9]+)? (?:[xX] ([0-9]+))? ([+-][0-9]+)? ([+-][0-9]+)? $/x) { |
|
|
1406 | my ($w, $h, $x, $y) = ($1, $2, $3, $4); |
|
|
1407 | |
|
|
1408 | if ($str[0]) { |
|
|
1409 | $w = $h unless defined $w; |
|
|
1410 | $h = $w unless defined $h; |
|
|
1411 | $y = $x unless defined $y; |
|
|
1412 | } |
|
|
1413 | |
|
|
1414 | $bg_opts->{h_scale} = $w if defined $w; |
|
|
1415 | $bg_opts->{v_scale} = $h if defined $h; |
|
|
1416 | $bg_opts->{h_align} = $x if defined $x; |
|
|
1417 | $bg_opts->{v_align} = $y if defined $y; |
|
|
1418 | } |
|
|
1419 | } |
|
|
1420 | } |
|
|
1421 | |
|
|
1422 | sub old_bg_expr { |
|
|
1423 | my ($self) = @_; |
|
|
1424 | |
|
|
1425 | my $expr; |
|
|
1426 | |
|
|
1427 | my $bg_opts = $self->{bg_opts}; |
|
|
1428 | |
|
|
1429 | if ($bg_opts->{root}) { |
|
|
1430 | $expr .= "tile ("; |
|
|
1431 | |
|
|
1432 | my $shade = $bg_opts->{shade}; |
|
|
1433 | |
|
|
1434 | if ($shade) { |
|
|
1435 | $shade = List::Util::min $shade, 200; |
|
|
1436 | $shade = List::Util::max $shade, -100; |
|
|
1437 | $shade = 200 - (100 + $shade) if $shade < 0; |
|
|
1438 | |
|
|
1439 | $shade = $shade * 0.01 - 1; |
|
|
1440 | $expr .= "shade $shade, "; |
|
|
1441 | } |
|
|
1442 | |
|
|
1443 | my $tint = $bg_opts->{tint}; |
|
|
1444 | |
|
|
1445 | if ($tint) { |
|
|
1446 | $expr .= "tint $tint, "; |
|
|
1447 | } |
|
|
1448 | |
|
|
1449 | my $blur = $bg_opts->{blur}; |
|
|
1450 | |
|
|
1451 | if ($blur and $blur =~ /^ =? ([0-9]+)? (?:[xX] ([0-9]+))? $/x) { |
|
|
1452 | my $hr = defined $1 ? $1 : 1; |
|
|
1453 | my $vr = defined $2 ? $2 : $hr; |
|
|
1454 | |
|
|
1455 | if ($hr != 0 and $vr != 0) { |
|
|
1456 | $expr .= "blur $hr, $vr, "; |
|
|
1457 | } |
|
|
1458 | } |
|
|
1459 | |
|
|
1460 | $expr .= "rootalign root)"; |
|
|
1461 | } |
|
|
1462 | |
|
|
1463 | if ($bg_opts->{path}) { |
|
|
1464 | my $file_expr; |
|
|
1465 | my $h_scale = $bg_opts->{h_scale} * 0.01; |
|
|
1466 | my $v_scale = $bg_opts->{v_scale} * 0.01; |
|
|
1467 | my $h_align = $bg_opts->{h_align} * 0.01; |
|
|
1468 | my $v_align = $bg_opts->{v_align} * 0.01; |
|
|
1469 | |
|
|
1470 | if (!$bg_opts->{tile}) { |
|
|
1471 | $file_expr .= "pad ("; |
|
|
1472 | } else { |
|
|
1473 | $file_expr .= "tile ("; |
|
|
1474 | } |
|
|
1475 | |
|
|
1476 | if ($bg_opts->{root_align}) { |
|
|
1477 | $file_expr .= "rootalign "; |
|
|
1478 | } else { |
|
|
1479 | $file_expr .= "align $h_align, $v_align, "; |
|
|
1480 | } |
|
|
1481 | |
|
|
1482 | if ($h_scale != 0 and $v_scale != 0) { |
|
|
1483 | my $op = $bg_opts->{keep_aspect} ? "fit" : "resize"; |
|
|
1484 | $file_expr .= "$op TW * $h_scale, TH * $v_scale, "; |
|
|
1485 | } |
|
|
1486 | |
|
|
1487 | $file_expr .= "keep { load pack \"H*\", \"$bg_opts->{path}\" })"; |
|
|
1488 | |
|
|
1489 | if ($expr) { |
|
|
1490 | $expr .= ", tint (\"[50]white\", $file_expr)"; |
|
|
1491 | } else { |
|
|
1492 | $expr = $file_expr; |
|
|
1493 | } |
|
|
1494 | } |
|
|
1495 | |
|
|
1496 | $expr |
|
|
1497 | } |
|
|
1498 | |
|
|
1499 | sub on_osc_seq { |
|
|
1500 | my ($self, $op, $arg) = @_; |
|
|
1501 | |
|
|
1502 | $self->{bg_opts} or return; |
|
|
1503 | |
|
|
1504 | $op =~ /^(20|705)$/ or return; |
|
|
1505 | |
|
|
1506 | if ($op eq "20") { |
|
|
1507 | if ($arg eq "?") { |
|
|
1508 | my $h_scale = $self->{bg_opts}->{h_scale}; |
|
|
1509 | my $v_scale = $self->{bg_opts}->{v_scale}; |
|
|
1510 | my $h_align = $self->{bg_opts}->{h_align}; |
|
|
1511 | my $v_align = $self->{bg_opts}->{v_align}; |
|
|
1512 | $self->cmd_parse ("\033]2;[${h_scale}x${v_scale}+${h_align}+${v_align}]\007"); |
|
|
1513 | } else { |
|
|
1514 | $self->old_bg_opts ($arg); |
|
|
1515 | my $expr = $self->old_bg_expr; |
|
|
1516 | $self->set_expr (parse_expr $expr) if $expr; |
|
|
1517 | } |
|
|
1518 | } elsif ($op eq "705") { |
|
|
1519 | $self->{bg_opts}->{tint} = $arg; |
|
|
1520 | my $expr = $self->old_bg_expr; |
|
|
1521 | $self->set_expr (parse_expr $expr) if $expr; |
|
|
1522 | } |
|
|
1523 | |
|
|
1524 | 1 |
|
|
1525 | } |
|
|
1526 | |
|
|
1527 | sub find_resource { |
|
|
1528 | my ($self, $a, $b) = @_; |
|
|
1529 | |
|
|
1530 | my $v = $self->x_resource ($a); |
|
|
1531 | $v = $self->x_resource ($b) unless defined $v; |
|
|
1532 | |
|
|
1533 | $v |
|
|
1534 | } |
|
|
1535 | |
1195 | sub on_start { |
1536 | sub on_start { |
1196 | my ($self) = @_; |
1537 | my ($self) = @_; |
1197 | |
1538 | |
1198 | my $expr = $self->x_resource ("%.expr") |
1539 | my $expr = $self->x_resource ("%.expr"); |
|
|
1540 | |
|
|
1541 | if (!$expr) { |
|
|
1542 | $self->{bg_opts} = { h_scale => 100, v_scale => 100, |
|
|
1543 | h_align => 50, v_align => 50 }; |
|
|
1544 | |
|
|
1545 | $self->{bg_opts}->{shade} = $self->find_resource ("shading", "sh"); |
|
|
1546 | $self->{bg_opts}->{tint} = $self->find_resource ("tintColor", "tint"); |
|
|
1547 | $self->{bg_opts}->{blur} = $self->find_resource ("blurRadius", "blr"); |
|
|
1548 | if ($self->x_resource_boolean ("transparent") |
|
|
1549 | or $self->x_resource_boolean ("tr")) { |
|
|
1550 | $self->{bg_opts}->{root} = 1; |
|
|
1551 | } |
|
|
1552 | |
|
|
1553 | $self->old_bg_opts ($self->find_resource ("backgroundPixmap", "pixmap")); |
|
|
1554 | $expr = $self->old_bg_expr; |
|
|
1555 | } |
|
|
1556 | |
1199 | or return; |
1557 | $expr or return; |
1200 | |
1558 | |
1201 | $self->has_render |
1559 | $self->has_render |
1202 | or die "background extension needs RENDER extension 0.10 or higher, ignoring background-expr.\n"; |
1560 | or die "background extension needs RENDER extension 0.10 or higher, ignoring background-expr.\n"; |
1203 | |
1561 | |
1204 | $self->set_expr (parse_expr $expr); |
1562 | $self->set_expr (parse_expr $expr); |