Posts tagged ‘acpi’

Playing arround with X11::Aosd

So in my desire to map some ACPI buttons on my thinkpad, I noticed this ‘battery’ button (Fn+F2). I thought it would be nice if I get this nice graph showing me my battery state.

Sadly osd_cat only does bars and text, and I wanted a nice pie or something. So I looked around for perl modules which could do more. In comes X11::Aosd.

X11::Aosd is a Cairo powered OSD interface which works quite nicely. As it’s an XS module it works fast and efficient, but as usual lacks in -proper- usage documenation.

The script I ended up writing (which called by another script that actually read the battery info) became as followed:

#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use X11::Aosd ':all';

my $per   = 100;
my $mx    = 200;
my $my    = 200;
my $text;

my $res = GetOptions("percentage=i" => \$per,
                     "xoffset=i"    => \$mx,
                     "yoffset=i"    => \$my,
                     "text=s"       => \$text,
);

my $pi = 3.14159265358979;
my $rad = ((2*$pi) / 100) * $per;

my $aosd = X11::Aosd->new;

$aosd->set_transparency(TRANSPARENCY_COMPOSITE);

$aosd->set_position_with_offset(
    COORDINATE_CENTER,
    COORDINATE_CENTER,
    200, 200, 0, 0
);
$aosd->set_renderer(sub {
    my ($cr) = @_;
    $cr->set_source_rgba (1, 0.6, 0, 1) if($per < 30);
    $cr->set_source_rgba (1, 0, 0, 1)   if($per < 10);
    $cr->set_source_rgba (0, 1, 0, 1)   if($per >= 30);

    $cr->set_line_width(10);
    $cr->set_line_cap('round');
    $cr->arc(100, 75, 70, 0, $rad);

    $cr->line_to(100,75);
    $cr->line_to(170,75) if($per < 100);
    $cr->stroke;
    $cr->fill;

    if($text) {
        $cr->move_to(0, 175);
        $cr->select_font_face("Sans", 'normal', 'bold');
        $cr->set_font_size (23);
        $cr->show_text($text);
        $cr->fill;
    }
});

$aosd->show;
$aosd->loop_once;

It’s a simple piece but it works. I kinda like X11:Aosd even though it’s less then perfect docs, toghether with the original cairo docs you get a nice idea. Maybe more examples later.

btw, Thinkpad F<n> acpi buttons(Fn+Function) are quite simple in their acpi mapping: 00000080 00001003 is F3, 00000080 00001002 is F2, etc

Simple but useful ACPI + Thinkpad script

I always got anoyed when X crashed, or hanged and I couldn’t do anything anymore, while I knew the rest of the system was still functional and I should be able to get my screen session back.

The ‘easy’ way for this would be to SSH into your workstation, but I like to keep sshd turned off on those machines. preferably no external services on workstations at all actually.

Now I have a Thinkpad, and Thinkpads have this ugly Thinkpad/Thinkvantage button, especially ugly in it’s inner workings, and because of that it’s useful here.

The Thinkvantage button is not a keyboard event (which at this point is controlled by a broken Xorg) but it’s polled by the ACPI daemon, which runs as root. So when you press the button, an ACPI event is generated, which can have scripts linked to them.

A quick fix would be to put ‘chvt 2′ in the /etc/acpi/thinkpad-thinkpad.sh script on debian. But I wanted a little more and  changed it to the following:

/etc/acpi/thinkpad-thinkpad.sh

#!/bin/sh
CURVT=`/bin/fgconsole`
NEXTVT=`expr $CURVT + 1`

if [ $NEXTVT -gt 8 ]; then
    NEXTVT=1
    echo "looped back to: $NEXTVT" >> /tmp/nextvt.log;
fi

echo "Current VT: $CURVT, Next VT: $NEXTVT" >> /tmp/nextvt.log
if [ "X$NEXTVT" eq "X" ]; then
    /bin/chvt 2;
else
    /bin/chvt $NEXTVT;
fi

So this actually loops through terminal 1 t/m 8, without needing the keyboard-driver.

(don’t forget to reload acpid after chaning any of the scripts).