<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl

use strict;

use X11::Protocol;
use IO::Select;
use Time::HiRes 'gettimeofday';

sub min { $_[0] &lt;= $_[1] ? $_[0] : $_[1] }
sub max { $_[0] &gt;= $_[1] ? $_[0] : $_[1] }

my $X = new X11::Protocol;
$X-&gt;init_extension("RENDER") or die "The Render extension is required";

my($rgba32, $screen_fmt);

my($formats, $screens,) = $X-&gt;RenderQueryPictFormats();
for my $f (@$formats) {
    $rgba32 = $f-&gt;[0] if $f-&gt;[2] == 32 and $f-&gt;[3] == 16 and $f-&gt;[5] == 8
      and $f-&gt;[7] == 0 and $f-&gt;[9] == 24;
}
for my $s (@$screens) {
    my @s = @$s;
    shift @s;
    for my $d (@s) {
	my @d = @$d;
	next unless shift(@d) == $X-&gt;root_depth;
	for my $v (@d) {
	    if ($v-&gt;[0] == $X-&gt;root_visual) {
		$screen_fmt = $v-&gt;[1];
	    }
	}
    }
}

my $size = 70;
my($width_fact, $radius, $tick_size, $depth);
use constant PI =&gt; 4*atan2(1,1);

sub tri_to_traps {
    my($x1, $y1, $x2, $y2, $x3, $y3) = @_;
    my @points = ([$x1, $y1], [$x2, $y2], [$x3, $y3]);
    @points = sort {$a-&gt;[1] &lt;=&gt; $b-&gt;[1]} @points;
    ($x1, $y1, $x2, $y2, $x3, $y3) =
      (@{$points[0]}, @{$points[1]}, @{$points[2]});
    my($trap1, $trap2);
    if (($x2-$x1)*($y3-$y1) &lt; ($x3-$x1)*($y2-$y1)) {
        $trap1 = [$y1, $y2, ($x1, $y1), ($x2, $y2), ($x1, $y1), ($x3, $y3)];
        $trap2 = [$y2, $y3, ($x2, $y2), ($x3, $y3), ($x1, $y1), ($x3, $y3)];
    } else {
        $trap1 = [$y1, $y2, ($x1, $y1), ($x3, $y3), ($x1, $y1), ($x2, $y2)],
        $trap2 = [$y2, $y3, ($x1, $y1), ($x3, $y3), ($x2, $y2), ($x3, $y3)];
    }
    return ($trap1, $trap2);
}

sub render_tri {
    my($op, $src_pict, $src_x, $src_y, $dst_pict, $mask, $tri) = @_;
    my($trap1, $trap2) = tri_to_traps(@$tri);
    $X-&gt;RenderTrapezoids($op, $src_pict, $src_x, $src_y, $dst_pict,
			 $mask, $trap1, $trap2);
#    $X-&gt;RenderTriangles($op, $src_pict, $src_x, $src_y, $dst_pict, $mask,
#			$tri);
}

sub render_quad {
    my($op, $src_pict, $src_x, $src_y, $dst_pict, $mask, @points) = @_;
    render_tri($op, $src_pict, $src_x, $src_y, $dst_pict, $mask,
	       [@points[0,1, 2,3, 4,5]]);
    render_tri($op, $src_pict, $src_x, $src_y, $dst_pict, $mask,
	       [@points[0,1, 4,5, 6,7]]);
}

sub polar2rect {
    my($r, $theta) = @_;
    my $x = $size/2 + $r * sin($theta);
    my $y = $size/2 - $r * cos($theta);
    return ($x, $y);
}

my $win = $X-&gt;new_rsrc;
$X-&gt;CreateWindow($win, $X-&gt;root, 'InputOutput', $X-&gt;root_depth,
                 'CopyFromParent', (0, 0), $size, $size, 0,
                 'background_pixel' =&gt; $X-&gt;white_pixel,
                 'event_mask' =&gt;
                   $X-&gt;pack_event_mask('Exposure', 'KeyPress', 'ButtonRelease',
                                       'StructureNotify'));

$X-&gt;ChangeProperty($win, $X-&gt;atom('WM_ICON_NAME'),
                   $X-&gt;atom('STRING'), 8, 'Replace', "render-clock");
$X-&gt;ChangeProperty($win, $X-&gt;atom('WM_NAME'), $X-&gt;atom('STRING'),
                   8, 'Replace', "Rendered Clock");
$X-&gt;ChangeProperty($win, $X-&gt;atom('WM_NORMAL_HINTS'),
                   $X-&gt;atom('WM_SIZE_HINTS'), 32, 'Replace',
                   pack("Lx40llllx12", 128, 1, 1, 1, 1));
$X-&gt;ChangeProperty($win, $X-&gt;atom('WM_HINTS'), $X-&gt;atom('WM_HINTS'),
                   32, 'Replace', pack("IIIx24", 1|2, 1, 1));

my $delete_atom = $X-&gt;atom('WM_DELETE_WINDOW');
$X-&gt;ChangeProperty($win, $X-&gt;atom('WM_PROTOCOLS'), $X-&gt;atom('ATOM'),
                   32, 'Replace', pack("L", $delete_atom));

my $progname = $0;
$progname =~ s[^.*/][];
$progname = $ENV{'RESOURCE_NAME'} || $progname;

$X-&gt;ChangeProperty($win, $X-&gt;atom('WM_CLASS'), $X-&gt;atom('STRING'),
                   8, 'Replace', "$progname\0Render-clock");

my($tick_color, $minute_color, $hour_color, $second_color);

#                Red    Green  Blue   Opacity
# $tick_color   = [0,     0,     0,     0xffff];
# $minute_color = [0xffff,0,     0,     0x8000];
# $hour_color   = [0,     0xffff,0,     0x8000];
# $second_color = [0,     0,     0xffff,0x8000];

# #                Red    Green  Blue   Opacity
# $tick_color   = [0,     0,     0,     0xffff];
# $minute_color = [0,     0,     0,     0x8000];
# $hour_color   = [0,     0,     0,     0x8000];
# $second_color = [0,     0,     0,     0x8000];

# #                Red    Green  Blue   Opacity
# $tick_color   = [0,     0,     0,     0xffff];
# $minute_color = [0,     0,     0x4fff,0x8000];
# $hour_color   = [0,     0,     0x4fff,0x8000];
# $second_color = [0,     0,     0x4fff,0x8000];

#                Red    Green  Blue   Opacity
$tick_color   = [0,     0,     0,     0xffff];
$minute_color = [0xffff,0,     0,     0x8000];
$hour_color   = [0,     0x4fff,0,     0x8000];
$second_color = [0,     0,     0x4fff,0x8000];


my($face_pixmap, $face_pict);

my $black_pixmap = $X-&gt;new_rsrc;
$X-&gt;CreatePixmap($black_pixmap, $win, 32, 1, 1);
my $black_pict = $X-&gt;new_rsrc;
$X-&gt;RenderCreatePicture($black_pict, $black_pixmap, $rgba32, 'repeat' =&gt; 1);
$X-&gt;RenderFillRectangles('Src', $black_pict, $tick_color, [0, 0, 1, 1]);

my $red_pixmap = $X-&gt;new_rsrc;
$X-&gt;CreatePixmap($red_pixmap, $win, 32, 1, 1);
my $red_pict = $X-&gt;new_rsrc;
$X-&gt;RenderCreatePicture($red_pict, $red_pixmap, $rgba32, 'repeat' =&gt; 1);
$X-&gt;RenderFillRectangles('Src', $red_pict, $minute_color, [0, 0, 1, 1]);

my $green_pixmap = $X-&gt;new_rsrc;
$X-&gt;CreatePixmap($green_pixmap, $win, 32, 1, 1);
my $green_pict = $X-&gt;new_rsrc;
$X-&gt;RenderCreatePicture($green_pict, $green_pixmap, $rgba32, 'repeat' =&gt; 1);
$X-&gt;RenderFillRectangles('Src', $green_pict, $hour_color, [0, 0, 1, 1]);

my $blue_pixmap = $X-&gt;new_rsrc;
$X-&gt;CreatePixmap($blue_pixmap, $win, 32, 1, 1);
my $blue_pict = $X-&gt;new_rsrc;
$X-&gt;RenderCreatePicture($blue_pict, $blue_pixmap, $rgba32, 'repeat' =&gt; 1);
$X-&gt;RenderFillRectangles('Src', $blue_pict, $second_color, [0, 0, 1, 1]);

my $hilite_pixmap = $X-&gt;new_rsrc;
$X-&gt;CreatePixmap($hilite_pixmap, $win, 32, 1, 1);
my $hilite_pict = $X-&gt;new_rsrc;
$X-&gt;RenderCreatePicture($hilite_pict, $hilite_pixmap, $rgba32, 'repeat' =&gt; 1);

my($buffer_pixmap, $buffer_pict);

sub setup_face {
    $width_fact = 2;
    $radius = 0.475 * $size;
    $tick_size = $size / 10;
    $depth = $size / 150;

    if ($face_pixmap) {
	$X-&gt;FreePixmap($face_pixmap);
	$X-&gt;RenderFreePicture($face_pict);
    } else {
	$face_pixmap = $X-&gt;new_rsrc;
	$face_pict = $X-&gt;new_rsrc;
    }
    $X-&gt;CreatePixmap($face_pixmap, $win, 32, $size, $size);
    $X-&gt;RenderCreatePicture($face_pict, $face_pixmap, $rgba32,
			    'poly_edge' =&gt; 'Smooth', 'poly_mode' =&gt; 'Precise');
    $X-&gt;RenderFillRectangles('Src', $face_pict, [0xefff,0xefff,0xefff,0xffff],
			     [0, 0, $size, $size]);

    for my $tick (0 .. 59) {
	my $theta = $tick/30 * PI;
	my $size_outer = 0.01;
	my $inner_rad;
	if ($tick % 5) {
	    $inner_rad = $radius - $tick_size/2;
	} else {
	    $inner_rad = $radius - $tick_size;
	}
	my $size_inner = $size_outer * ($radius/$inner_rad);
	my($x1, $y1) = polar2rect($radius, $theta - $size_outer);
	my($x2, $y2) = polar2rect($radius, $theta + $size_outer);
	my($x3, $y3) = polar2rect($inner_rad, $theta + $size_inner);
	my($x4, $y4) = polar2rect($inner_rad, $theta - $size_inner);
	render_quad('Over', $black_pict, $size, $size, $face_pict, 'None',
		    ($x1, $y1), ($x2, $y2), ($x3, $y3), ($x4, $y4));
    }
    #$X-&gt;RenderFillRectangles('Over', $face_pict, [0,0,0,0xffff],
    #			 [$size/2-5, $size/2-5, 10, 10]);

    if ($buffer_pixmap) {
	$X-&gt;FreePixmap($buffer_pixmap);
	$X-&gt;RenderFreePicture($buffer_pict);
    } else {
	$buffer_pixmap = $X-&gt;new_rsrc;
	$buffer_pict = $X-&gt;new_rsrc;
    }
    $X-&gt;CreatePixmap($buffer_pixmap, $win, $X-&gt;root_depth, $size, $size);
    $X-&gt;RenderCreatePicture($buffer_pict, $buffer_pixmap, $screen_fmt,
			    'poly_edge' =&gt; 'Smooth', 'poly_mode' =&gt; 'Precise');
}

setup_face();

my $copy_gc = $X-&gt;new_rsrc;
$X-&gt;CreateGC($copy_gc, $win);

$X-&gt;MapWindow($win);

sub draw_hand {
    my($pict, $x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4) = @_;
    my @p = ([$x1, $y1], [$x2, $y2], [$x3, $y3], [$x4, $y4]);
    my @ip;
    $#ip = $#p;
    for my $j (-2 .. $#p - 2) {
	my($ix, $iy) = ($p[$j+1][0] - $p[$j][0], $p[$j+1][1] - $p[$j][1]);
        my($ox, $oy) = ($p[$j+2][0] - $p[$j+1][0], $p[$j+2][1] - $p[$j+1][1]);
        if ($ix*$oy &gt; $iy*$ox) {
            $ix = -$ix; $iy = -$iy;
        } else {
            $ox = -$ox; $oy = -$oy;
        }
        my($in) = sqrt($ix*$ix + $iy*$iy); $ix /= $in; $iy /= $in;
        my($on) = sqrt($ox*$ox + $oy*$oy); $ox /= $on; $oy /= $on;
        my($mx, $my) = (($ix + $ox)/2, ($iy + $oy)/2);
        my($mn) = max(abs($mx), abs($my)); $mx /= $mn; $my /= $mn;
        $ip[$j+1][0] = $p[$j+1][0] + $depth * $mx;
        $ip[$j+1][1] = $p[$j+1][1] + $depth * $my;

    }
    render_quad('Over', $pict, $size, $size, $buffer_pict, 'None',
		($x1, $y1), ($x2, $y2), ($x3, $y3), ($x4, $y4));
    for my $j (-1 .. $#p - 1) {
	my $angle = atan2($p[$j+1][1]-$p[$j][1], $p[$j+1][0]-$p[$j][0]);
	my $gray = 0x8000 + 0x4000 * sin($angle + 3*PI / 4);
	my $alpha = 0.5;
	$X-&gt;RenderFillRectangles('Src', $hilite_pict,
				 [$gray, $gray, $gray,
				  $alpha*0xffff],
				 [0, 0, 1, 1]);
	render_quad('Over', $hilite_pict, $size, $size, $buffer_pict, 'None',
		    @{$p[$j]}, @{$ip[$j]}, @{$ip[$j + 1]}, @{$p[$j + 1]});
    }
}

sub draw {
    $X-&gt;RenderFillRectangles('Src', $buffer_pict,
			     [0xffff, 0xffff, 0xffff, 0xffff],
			     [0, 0, $size, $size]);
    $X-&gt;RenderComposite('Over', $face_pict, 'None', $buffer_pict, 0, 0,
			0, 0, 0, 0, $size, $size);

    my($unix_time, $microsec) = gettimeofday();
    my($sec, $min, $hour) = localtime($unix_time);
    $sec += $microsec / 1_000_000;

    {
	my $hour_theta = ($hour % 12 + $min/60 + $sec/3600)/6 * PI;
	my $hour_size_outer = 0.04 * $width_fact;
	my $hour_size_inner = $hour_size_outer * (.6/.3) * 1.4;
	my($x1, $y1) = polar2rect(.6*$radius, $hour_theta - $hour_size_outer);
	my($x2, $y2) = polar2rect(.6*$radius, $hour_theta + $hour_size_outer);
	my($x3, $y3) = polar2rect(-.3*$radius, $hour_theta - $hour_size_inner);
	my($x4, $y4) = polar2rect(-.3*$radius, $hour_theta + $hour_size_inner);
	draw_hand($green_pict,
		  ($x1, $y1), ($x2, $y2), ($x3, $y3), ($x4, $y4));
    }

    {
	my $min_theta = ($min + $sec/60)/30 * PI;
	my $min_size_outer = 0.02 * $width_fact;
	my $min_size_inner = $min_size_outer * (.8/.2) * 1.3;
	my($x1, $y1) = polar2rect(.8*$radius, $min_theta - $min_size_outer);
	my($x2, $y2) = polar2rect(.8*$radius, $min_theta + $min_size_outer);
	my($x3, $y3) = polar2rect(-.2*$radius, $min_theta - $min_size_inner);
	my($x4, $y4) = polar2rect(-.2*$radius, $min_theta + $min_size_inner);
	draw_hand($red_pict,
		  ($x1, $y1), ($x2, $y2), ($x3, $y3), ($x4, $y4));
    }

    {
	my $sec_theta = $sec/30 * PI;
	my $sec_size_outer = 0.01 * $width_fact;
	my $sec_size_inner = $sec_size_outer * (.95/.15) * 1.3;
	my($x1, $y1) = polar2rect(.95*$radius, $sec_theta - $sec_size_outer);
	my($x2, $y2) = polar2rect(.95*$radius, $sec_theta + $sec_size_outer);
	my($x3, $y3) = polar2rect(-.15*$radius, $sec_theta - $sec_size_inner);
	my($x4, $y4) = polar2rect(-.15*$radius, $sec_theta + $sec_size_inner);
	draw_hand($blue_pict,
		  ($x1, $y1), ($x2, $y2), ($x3, $y3), ($x4, $y4));
    }

    $X-&gt;CopyArea($buffer_pixmap, $win, $copy_gc,
		 0, 0, $size, $size, 0, 0);
}

$X-&gt;event_handler('queue');
my $fds = IO::Select-&gt;new($X-&gt;connection-&gt;fh);

my $start_time = time;
my $sample_time = Time::HiRes::time;
my $frames = 0;
my $delay = 0.00001;

for (;;) {
    $X-&gt;flush();
    $X-&gt;GetScreenSaver(); # AKA XSync()
    #$X-&gt;handle_input if $fds-&gt;can_read(0);
    Time::HiRes::sleep(0.01 + $delay);
    my %e;
    while (%e = $X-&gt;dequeue_event) {
	if ($e{'name'} eq "Expose") {
	    draw();
	} elsif ($e{'name'} eq "ButtonRelease"
		 or $e{'name'} eq "KeyPress") {
	    exit;
	} elsif ($e{'name'} eq "ConfigureNotify") {
	    my($w, $h) = @e{'width', 'height'};
	    $size = min($w, $h);
	    setup_face();
	    $frames = 0;
	    $start_time = time;
	    $sample_time = Time::HiRes::time;
	} elsif ($e{'name'} eq "ClientMessage"
		 and unpack("L", $e{'data'}) == $delete_atom) {
	    exit;
	}
    }
    draw();
    $frames++;
    if (!($frames % 20)) {
	my $fps = $frames/(Time::HiRes::time-$sample_time);
	#print "$fps FPS delay $delay\n";
	if ($fps &gt; 30) {
	    $delay = 0.75 * $delay + 0.25 * ($delay + 1/30 - 1/$fps);
	} elsif ($fps &lt; 30) {
	    $delay = 0.75 * $delay;
	}
    }
}
</pre></body></html>