The writings of Peter Stuifzand

Archive for October 2005

I was so nice to create a package for osdtime.

Source

osdtime-0.1.tar.gz.

Today I liked to know the time. I don't have a clock and on my computer it's to much work to show the time. I use ion3 and it can show the time in a statusbar, but I don't like that, because it takes too much of my screen real estate.

So I created a little program that shows the time for a few seconds in the right bottom corner of my screen. It's called (how original) osdtime.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <locale.h>
#include <xosd.h>
#include <time.h>

/* seconds to display time */
int seconds_to_display = 3;

/* display font */
const char* display_font = "-adobe-helvetica-bold-r-normal-*-*-320-*-*-p-*-*";

/* time format string */
const char* time_fmt = "%H:%M:%S";

/* text-colour */
const char* text_colour = "white";

/* outline-colour */
const char* outline_colour = "black";

int main(int argc, char *argv[])
{
  xosd *osd;
  setlocale(LC_ALL, "");
  osd = xosd_create(2);
  if (osd == NULL) {
    perror("Could not create \"osd\"");
    exit(1);
  }
  /* Set the position of the display. */
  xosd_set_pos(osd, XOSD_bottom);
  xosd_set_align(osd, XOSD_right);

  /* Set the font and the colours. */
  xosd_set_font(osd, display_font);
  xosd_set_colour(osd, text_colour);

  xosd_set_outline_offset(osd, 2);
  xosd_set_outline_colour(osd, outline_colour);

  /* Display the time for some seconds. */
  int i;
  for (i = 0; i < seconds_to_display; i++) {
    time_t tm;
    struct tm *localtm;
    time(&tm);
    localtm = localtime(&tm);

    char buf[9];
    strftime(buf, 9, time_fmt, localtm);

    xosd_display(osd, 0, XOSD_string, buf);
    sleep(1);
  }

  xosd_destroy(osd);
  exit(0);
}

It uses libxosd to show the text. As always, it comes without any warranty. Use at your own risk. It can be compiled with the following command line.

gcc `xosd-config --libs --cflags` osdtime.c -o osdtime

At the beginning of the program it's possible to set the variables to nicer values if you like.

At the ion3 side of this hack I changed the ~/.ion3/cfg_bindings.lua file. The time will be displayed when I press F11. This can be done by putting the following lines in the bindings file after the line that uses F9.

bdoc("Display the time."),
kpress(MOD2.."F11", "ioncore.exec_on(_, 'osdtime')"),

I created a script that allows me to update a page on a media wiki. It uses WWW::Mechanize, IO::All and Config::Std. The last two modules aren't needed probably. But it makes it all a lot simpler.

#!/usr/bin/perl -w

use IO::All;
use WWW::Mechanize;
use Config::Std;

read_config "$ENV{HOME}/.mediawiki" => my %config;

my $username = $config{login}{username};
my $password = $config{login}{password};
my $hostname = $config{login}{hostname};

my $title = $ARGV[0] or die "usage:   mediawiki [page title with under scores]\n\n";
my $url = "http://$hostname/mediawiki/index.php?title=$title&action=edit";

my $ua = WWW::Mechanize->new;

my $login_url = "http://$hostname/mediawiki/index.php?title=Special:Userlogin&returnto=Main_Page";
$ua->get($login_url);

$ua->field('wpName', $username);
$ua->field('wpPassword', $password);
$ua->click('wpLoginattempt');

$ua->get($url);

$ua->form_number(1);

my $form = $ua->current_form();

my $tmpfile = io('/tmp/mediawiki-1');
$form->value('wpTextbox1') > $tmpfile;

system('vi /tmp/mediawiki-1');

$tmpfile->open('r');

my $content = $tmpfile->slurp;
$ua->field('wpTextbox1', $content);
$ua->click('wpSave');

print "Done!\n\n";

The configuration is read from /home/$USER/.mediawiki. It should look something like this.

[login]
username=your name
password=your password
hostname=your wiki hostname

Where hostname should be something like 'localhost' or 'www.somedomain.org'.

Oh, this code comes without any warranty. Use at your own risk.

This post is not about perl plugins (what would that be?). But about programs that use plugins. One of those programs is Qpsmtpd, a perl mail server. They use a special and very nice way to use plugins.

Moveable Type is another perl program, that uses plugins. These plugins are a little bit simpler than the qpsmtpd plugins, but just as powerful.

And now there is another perl program that uses plugins, my webshop. It is really nice to see how easy it is to create a new type of shop on top of the current code. I can't yet show it, but within a few days it will be online.

The day after I posted the previous post, I became aware of a serious flaw in the design of the player. It couldn't play music from on artist or one album. I really liked this functionality, so I wrote it.

MUSICDIR=~/music
cd $MUSICDIR
find . -type f -name "*.mp3" -o -name "*.ogg" | grep "$1" > playlist
mplayer -quiet -shuffle -playlist playlist | grep Playing

Also it tried to play the cover images that are in some of the directories. This was solved by specifying the extensions of the files to play.

Today I created a new music tool quiet like the Apple Shuffle. It will play music files randomly from my 'collection'. Here follows the code.

#!/bin/sh
cd ~/music
find . -type f > playlist
mplayer -quiet -shuffle -playlist playlist | grep Playing

It can be started in it's own terminal and be controlled with all the normal mplayer combinations. PgUp and PgDn will skip songs. Left and Right will skip in a song. + and - will change the volume. Q let's you quit.

It's a little crude at the edges, but it's probably the best tool I've written in a while. No stuff that will keep you from getting your job done.

At the company, where I'm doing my internship, I needed a code generator for some C code. There was some C code needed. So I started of writing it. After some lines, there started to show some patterns, which could be easily handled by some description language.

The first description language was a line based format. First a line with the name of the object. And after that the names of the functions. This format is easy to parse and easy to use. The output was done with print statements.

For code generation I always used this method. But why should the webapplications have all the template fun? So I downloaded Template (which is the Template Toolkit), from the CPAN and I tried some things with it. It was really easy to generate code like this. The code was also much cleaner and shorter.

All nice and easy, but not good enough, because it should be possible to add C code to the functions. I almost started to try and create regular expressions for this task, but then the laziness started to kick in again. I remembered something about a Text::Balanced, which is written by Damian Conway. I can extract delimeted text from strings. Which was exactly what I wanted to do.

The code is again much smaller, and writing the regexen would be the wrong way to go. In this case Text::Balanced is the middle ground between regexen and some parser written with something like Parse::RecDescent. Laziness + CPAN is a good thing.

View archived entries