urxvt-background(1) | RXVT-UNICODE | urxvt-background(1) |
background - manage terminal background
urxvt --background-expr 'background expression' --background-border --background-interval seconds
Just load a random jpeg image and tile the background with it without scaling or anything else:
load "/path/to/img.jpg"
The same, but use mirroring/reflection instead of tiling:
mirror load "/path/to/img.jpg"
Load an image and scale it to exactly fill the terminal window:
scale keep { load "/path/to/img.jpg" }
Implement pseudo-transparency by using a suitably-aligned root pixmap as window background:
rootalign root
Likewise, but keep a blurred copy:
rootalign keep { blur 10, root }
This extension manages the terminal background by creating a picture that is behind the text, replacing the normal background colour.
It does so by evaluating a Perl expression that calculates the image on the fly, for example, by grabbing the root background or loading a file.
While the full power of Perl is available, the operators have been design to be as simple as possible.
For example, to load an image and scale it to the window size, you would use:
urxvt --background-expr 'scale keep { load "/path/to/mybg.png" }'
Or specified as a X resource:
URxvt.background.expr: scale keep { load "/path/to/mybg.png" }
At startup, just before the window is mapped for the first time, the expression is evaluated and must yield an image. The image is then extended as necessary to cover the whole terminal window, and is set as a background pixmap.
If the image contains an alpha channel, then it will be used as-is in visuals that support alpha channels (for example, for a compositing manager). In other visuals, the terminal background colour will be used to replace any transparency.
When the expression relies, directly or indirectly, on the window size, position, the root pixmap, or a timer, then it will be remembered. If not, then it will be removed.
If any of the parameters that the expression relies on changes (when the window is moved or resized, its position or size changes; when the root pixmap is replaced by another one the root background changes; or when the timer elapses), then the expression will be evaluated again.
For example, an expression such as "scale keep { load "$HOME/mybg.png" }" scales the image to the window size, so it relies on the window size and will be reevaluated each time it is changed, but not when it moves for example. That ensures that the picture always fills the terminal, even after its size changes.
Expressions are normal Perl expressions, in fact, they are Perl blocks - which means you could use multiple lines and statements:
scale keep { again 3600; if (localtime now)[6]) { return load "$HOME/weekday.png"; } else { return load "$HOME/sunday.png"; } }
This inner expression is evaluated once per hour (and whenever the terminal window is resized). It sets sunday.png as background on Sundays, and weekday.png on all other days.
Fortunately, we expect that most expressions will be much simpler, with little Perl knowledge needed.
Basically, you always start with a function that "generates" an image object, such as "load", which loads an image from disk, or "root", which returns the root window background image:
load "$HOME/mypic.png"
The path is usually specified as a quoted string (the exact rules can be found in the perlop manpage). The $HOME at the beginning of the string is expanded to the home directory.
Then you prepend one or more modifiers or filtering expressions, such as "scale":
scale load "$HOME/mypic.png"
Just like a mathematical expression with functions, you should read these expressions from right to left, as the "load" is evaluated first, and its result becomes the argument to the "scale" function.
Many operators also allow some parameters preceding the input image that modify its behaviour. For example, "scale" without any additional arguments scales the image to size of the terminal window. If you specify an additional argument, it uses it as a scale factor (multiply by 100 to get a percentage):
scale 2, load "$HOME/mypic.png"
This enlarges the image by a factor of 2 (200%). As you can see, "scale" has now two arguments, the 200 and the "load" expression, while "load" only has one argument. Arguments are separated from each other by commas.
Scale also accepts two arguments, which are then separate factors for both horizontal and vertical dimensions. For example, this halves the image width and doubles the image height:
scale 0.5, 2, load "$HOME/mypic.png"
IF you try out these expressions, you might suffer from some sluggishness, because each time the terminal is resized, it loads the PNG image again and scales it. Scaling is usually fast (and unavoidable), but loading the image can be quite time consuming. This is where "keep" comes in handy:
scale 0.5, 2, keep { load "$HOME/mypic.png" }
The "keep" operator executes all the statements inside the braces only once, or when it thinks the outcome might change. In other cases it returns the last value computed by the brace block.
This means that the "load" is only executed once, which makes it much faster, but also means that more memory is being used, because the loaded image must be kept in memory at all times. In this expression, the trade-off is likely worth it.
But back to effects: Other effects than scaling are also readily available, for example, you can tile the image to fill the whole window, instead of resizing it:
tile keep { load "$HOME/mypic.png" }
In fact, images returned by "load" are in "tile" mode by default, so the "tile" operator is kind of superfluous.
Another common effect is to mirror the image, so that the same edges touch:
mirror keep { load "$HOME/mypic.png" }
Another common background expression is:
rootalign root
This one first takes a snapshot of the screen background image, and then moves it to the upper left corner of the screen (as opposed to the upper left corner of the terminal window)- the result is pseudo-transparency: the image seems to be static while the window is moved around.
Whenever an operator expects a "colour", then this can be specified in one of two ways: Either as string with an X11 colour specification, such as:
"red" # named colour "#f00" # simple rgb "[50]red" # red with 50% alpha "TekHVC:300/50/50" # anything goes
OR as an array reference with one, three or four components:
[0.5] # 50% gray, 100% alpha [0.5, 0, 0] # dark red, no green or blur, 100% alpha [0.5, 0, 0, 0.7] # same with explicit 70% alpha
Since some operations (such as "load" and "blur") can take a long time, caching results can be very important for a smooth operation. Caching can also be useful to reduce memory usage, though, for example, when an image is cached by "load", it could be shared by multiple terminal windows running inside urxvtd.
"keep { ... }" caching
The most important way to cache expensive operations is to use "keep { ... }". The "keep" operator takes a block of multiple statements enclosed by "{}" and keeps the return value in memory.
An expression can be "sensitive" to various external events, such as scaling or moving the window, root background changes and timers. Simply using an expression (such as "scale" without parameters) that depends on certain changing values (called "variables"), or using those variables directly, will make an expression sensitive to these events - for example, using "scale" or "TW" will make the expression sensitive to the terminal size, and thus to resizing events.
When such an event happens, "keep" will automatically trigger a reevaluation of the whole expression with the new value of the expression.
"keep" is most useful for expensive operations, such as "blur":
rootalign keep { blur 20, root }
This makes a blurred copy of the root background once, and on subsequent calls, just root-aligns it. Since "blur" is usually quite slow and "rootalign" is quite fast, this trades extra memory (for the cached blurred pixmap) with speed (blur only needs to be redone when root changes).
"load" caching
The "load" operator itself does not keep images in memory, but as long as the image is still in memory, "load" will use the in-memory image instead of loading it freshly from disk.
That means that this expression:
keep { load "$HOME/path..." }
Not only caches the image in memory, other terminal instances that try to "load" it can reuse that in-memory copy.
Specifying this flag changes the behaviour, so that the image only replaces the background of the character area.
If you want to do updates more often, you can decrease this safety interval with this switch.
These functions provide an image, by loading it from disk, grabbing it from the root screen or by simply generating it. They are used as starting points to get an image you can play with.
If the image is already in memory (e.g. because another terminal instance uses it), then the in-memory copy is returned instead.
This function makes your expression root sensitive, that means it will be reevaluated when the bg image changes.
If $width and $height are omitted, it creates a 1x1 image, which is useful for solid backgrounds or for use in filtering effects.
This function is called automatically when an expression returns multiple images.
The following operators modify the tiling mode of an image, that is, the way that pixels outside the image area are painted when the image is used.
Example: load an image and tile it over the background, without resizing. The "tile" call is superfluous because "load" already defaults to tiling mode.
tile load "mybg.png"
Example: load an image and mirror it over the background, avoiding sharp edges at the image borders at the expense of mirroring the image itself
mirror load "mybg.png"
Example: load an image and display it in the upper left corner. The rest of the space is left "empty" (transparent or whatever your compositor does in alpha mode, else background colour).
pad load "mybg.png"
Example: just for curiosity, how does this pixel extension stuff work?
extend move 50, 50, load "mybg.png"
The following functions provide variable data such as the terminal window dimensions. They are not (Perl-) variables, they just return stuff that varies. Most of them make your expression sensitive to some events, for example using "TW" (terminal width) means your expression is evaluated again when the terminal is resized.
Using these functions makes your expression sensitive to window moves.
These functions are mainly useful to align images to the root window.
Example: load an image and align it so it looks as if anchored to the background (that's exactly what "rootalign" does btw.):
move -TX, -TY, keep { load "mybg.png" }
Using these functions makes your expression sensitive to window resizes.
These functions are mainly useful to scale images, or to clip images to the window size to conserve memory.
Example: take the screen background, clip it to the window size, blur it a bit, align it to the window position and use it as background.
clip move -TX, -TY, keep { blur 5, root }
Using this function makes your expression sensitive to focus changes.
A common use case is to fade the background image when the terminal loses focus, often together with the "-fade" command line option. In fact, there is a special function for just that use case: "focus_fade".
Example: use two entirely different background images, depending on whether the window has focus.
FOCUS ? keep { load "has_focus.jpg" } : keep { load "no_focus.jpg" }
Using this expression does not make your expression sensitive to time, but the next two functions do.
Example: load some image and rotate it according to the time of day (as if it were the hour pointer of a clock). Update this image every minute.
again 60; rotate 50, 50, (now % 86400) * -72 / 8640, scale keep { load "myclock.png" }
The following operators modify the shape, size or position of the image.
If $x and $y are missing, then 0 is assumed for both.
If $width and $height are missing, then the window size will be assumed.
Example: load an image, blur it, and clip it to the window size to save memory.
clip keep { blur 10, load "mybg.png" }
If only one factor is given, it is used for both directions.
If no factors are given, scales the image to the window size without keeping aspect.
Example: move the image right by 20 pixels and down by 30.
move 20, 30, ...
Example: remove any visible border around an image, center it vertically but move it to the right hand side.
align 1, 0.5, pad $img
Example: load an image and center it.
center keep { pad load "mybg.png" }
Example: load a background image, put it in mirror mode and root align it.
rootalign keep { mirror load "mybg.png" }
Example: take the screen background and align it, giving the illusion of transparency as long as the window isn't in front of other windows.
rootalign root
Example: rotate the image by 90 degrees around its center.
rotate 0.5, 0.5, 90, keep { load "$HOME/mybg.png" }
The following operators change the pixels of the image.
Example: tint the image red.
tint "red", load "rgb.png"
Example: the same, but specify the colour by component.
tint [1, 0, 0], load "rgb.png"
The first form applies a single $factor to red, green and blue, the second form applies separate factors to each colour channel, and the last form includes the alpha channel.
Values from 0 to 1 lower the contrast, values higher than 1 increase the contrast.
Due to limitations in the underlying XRender extension, lowering contrast also reduces brightness, while increasing contrast currently also increases brightness.
The first form applies a single $bias to red, green and blue, the second form applies separate biases to each colour channel, and the last form includes the alpha channel.
Values less than 0 reduce brightness, while values larger than 0 increase it. Useful range is from -1 to 1 - the former results in a black, the latter in a white picture.
Due to idiosyncrasies in the underlying XRender extension, biases less than zero can be very slow.
You can also try the experimental(!) "muladd" operator.
Due to numerous bugs in XRender implementations, it can also introduce a number of visual artifacts.
Example: increase contrast by a factor of $c without changing image brightness too much.
muladd $c, (1 - $c) * 0.5, $img
Blurring is often very slow, at least compared or other operators. Larger blur radii are slower than smaller ones, too, so if you don't want to freeze your screen for long times, start experimenting with low values for radius (<5).
Example: do the right thing when focus fading is requested.
focus_fade load "mybg.jpg";
Anything that didn't fit any of the other categories, even after applying force and closing our eyes.
The trick is that this code block is only evaluated when the outcome changes - on other calls the "keep" simply returns the image it computed previously (yes, it should only be used with images). Or in other words, "keep" caches the result of the code block so it doesn't need to be computed again.
This can be extremely useful to avoid redoing slow operations - for example, if your background expression takes the root background, blurs it and then root-aligns it it would have to blur the root background on every window move or resize.
Another example is "load", which can be quite slow.
In fact, urxvt itself encloses the whole expression in some kind of "keep" block so it only is reevaluated as required.
Putting the blur into a "keep" block will make sure the blur is only done once, while the "rootalign" is still done each time the window moves.
rootalign keep { blur 10, root }
This leaves the question of how to force reevaluation of the block, in case the root background changes: If expression inside the block is sensitive to some event (root background changes, window geometry changes), then it will be reevaluated automatically as needed.
2021-05-21 | 9.22 |