2007/02/11

Mew の拡張ツールを作る(1)

Mew の拡張ツールを Perl/Tk で作ってみた。

会社のワークステーションでは随分前から Mew を使ってメールを読んでいる。Mew を使ってメールを読む上で困るのはメールが来ているか来ていないかが視覚的にわかりづらいこと。Mew にも biff の機能はあるのだけどステータスバーにそれがちょいと表示されていても気づかないことがある。

以前、この不満の解消のために biff ツールを GTK ベースで自分でつくってみたことがある。普段は適当なアニメーションをしていて、POP サーバーに新たなメールが来るとアラートのアニメーション表示をするツールだった。随分と活躍していたのだけれど社内で SSL が導入されてから役にたたなくなってしまった。SSL の実装を調べるのも面倒でずっとなにもしていなかったのだけれど、ワークステーションでメールは SSL に対応している Mew を使って読んでいるのだから Mew の機能を使わせてもらえばいいじゃないかと発想の転換をしてみた。

つまり Mew の biff 機能を横取りして新着メールがある時と新着メールが取り込まれたことをイベントのトリガーとしてアニメーションできるツールがあればよい。

アニメーション、及びイベント処理が簡単に出来そうなプログラミング言語として Perl/Tk を選択。イベントの受け渡しは単純に UNIX のシグナルを使うことにした(INT と HUP)。

そのソース ptkbiff.pl とコンフィギュレーションファイル ptkbiff.conf を以下に貼付けておく。アニメーション用の画像ファイルは適宜用意して欲しい。
実行には Perl/Tk が必要なのは言うまでもない。逆に Perl/Tk が動く環境ならほぼこのソースコードは実行できる。ま、需要は殆どないと思われるが。

あとで気づいたのだけど GIF 対応なら GIF アニメーションを使えばツール側でアニメーション機能はいらないなぁと w


--- ptkbiff.pl ----------------------------------------------

#!/bin/perl
#-----------------------------------------------------------------------------
# Title : ptkbiff
# Project : Biff
#-----------------------------------------------------------------------------
# File : ptkbiff.pl
# Author : Tatsuro Satoh
# Created : 10.02.2007
# Last modified : 10.02.2007
#-----------------------------------------------------------------------------
# Description :
#
# Animation activated by signal
#
# HUP(kill -1) activates normal animation(default)
# INT(kill -2) activates alart animation
#
# You have to put configuration file ptkbiff.conf into the same
# directory of this script.
#
# This scripts PID is saved at /var/tmp/ptkbiff.${USER}
#
#-----------------------------------------------------------------------------
# License :
# This script file is distributed under BSD licese.
# http://www.opensource.org/licenses/bsd-license.php
#-----------------------------------------------------------------------------
# Copyright (c) 2007 by Tatsuro Satoh
#-----------------------------------------------------------------------------
# Modification history :
# 10.02.2007 : add clear
#-----------------------------------------------------------------------------

use Tk;

# Check Script Path
@ptkbiff_path = split('/', $0);
pop @ptkbiff_path;
$ptkbiff_path = ($#ptkbiff_path == -1) ? '.' : join('/', @ptkbiff_path);

# Grobal Values
$iniflg = 0;
$state = 0;
%imgs = ();
@nanim = ();
@aanim = ();
$mw = MainWindow->new;
@seq = (); # sequence
$seqp = 0; # sequence pointer
$timer = 0;

&loadConfig();

# Bind Interrupt Signals
$SIG{HUP} = \&catch_hup;
$SIG{INT} = \&catch_int;

# Check PID
$pid_file = "/var/tmp/ptkbiff.$ENV{USER}";
open PFILE, ">$pid_file" or die "Cannot open $pid_file\n";
print PFILE "$$\n";
close PFILE;

# Init and Start
&InitDisp;
MainLoop;


# SUB routines
sub InitDisp {
$mw->title ("");
$l0 = $mw->Label(-image => $img)->pack;
@seq = @nanim;
$seqp = -1;
&tick;
}

sub catch_hup {
@seq = @nanim;
$seqp = -1;
&tick;
}

sub catch_int {
@seq = @aanim;
$seqp = -1;
&tick;
}

sub tick {
if ($iniflg) {
$timer->cancel();
} else {
$iniflg = 1;
}
$seqp = ($#seq == $seqp) ? 0 : $seqp + 1;

sub tick {
if ($iniflg) {
$timer->cancel();
} else {
$iniflg = 1;
}
$seqp = ($#seq == $seqp) ? 0 : $seqp + 1;
# print "$seqp\n";
$l0->configure(-image => $imgs{$seq[$seqp]->{img}});
if ($seq[$seqp]->{time}) {
$timer = $mw->after($seq[$seqp]->{time}, \&tick);
}
}

sub loadConfig {
my $cfile = "$ptkbiff_path/ptkbiff.conf";
open CFILE, "<$cfile" or die "Cannot open config file($cfile).\n";

my $section = ""; # current section

while () {
chomp;
my @s = split('#', $_);
$s = shift @s;
if ($s !~ /^\s*$/) {
if ($s =~ /\s*EndSection/) {
$section = ""; # clear section
} elsif ($s =~ /\s*Section\s+(\S+)/) {
$section = $1; # set section
} else {
@s = split(/\s+/, $s);
if ($section eq "ImageFiles") {
$imgs{$s[0]} = $mw->Photo(-file => "${ptkbiff_path}/img/$s[1]");
} elsif ($section eq "NormAnimation") {
my %animitem = (img => @s[0],
time => @s[1]);
push(@nanim, \%animitem);
} elsif ($section eq "AlertAnimation") {
my %animitem = (img => @s[0],
time => @s[1]);
push(@aanim, \%animitem);
}
}
}
}
close CFILE;
}


--- ptkbiff.conf ----------------------------------------------

# Confiuration file of ptkbiff (C) 2007 by Tatsuro Satoh

# pktbiff.pl 用のコンフィギュレーションファイルです。
# このファイルは ptkbiff.pl と同じディレクトリに置いて下さい.
#
# コメントは '#' でこの記号以降は無視されます。
#
# このファイルは ptkbiff.pl でアニメーションに使うファイルの登録と
# アニメーションの設定を行ないます。
#
# Section

# Endsection
#
# で囲まれた部分に Section TAG で示す Section の設定を書きます。


# Section TAG: ImageFiles
# 使用するイメージファイルの登録をします。
# GIF と JPEG(多分) が使えます。サイズや色に制限はありませんが、
# アニメーションの都合上それぞれの画像のサイズは合わせておいた
# 方がよいです。また表示環境が貧弱な場合も考えて色数は極力少なく
# しておいた方が無難です。
#
# 記述の仕方は
# のようにエイリアスとそれが指すイメージファイルをスペースで区切った
# ものを一行に一組合せを書き、それを列挙します。登録数に制限はありません。
# Image File は ptkbiff.pl と同じディレクトリにある img ディレクトリ
# にあるファイルを指定します。
# Alias の文字に特に制限はありませんが、正規表現のコントロール記号は
# 避けましょう。

Section ImageFiles
img0 img0.gif # normal
img1 img1.gif # close eyes
img2 img2.gif # drinking
img3 img3.gif # interesting..
img4 img4.gif # receive
EndSection


# Section TAG: NormAnimation
# 通常のアニメーション表示の設定です。
# の組合せを列挙します。
# Alias は ImageFiles で登録したイメージファイルの Alias です。
# Time は Alias のイメージを表示する時間(単位 [ms])の指定です。
# 表示は上から下の順に行なわれ、最後のイメージが表示された次には
# 再び最初のイメージを表示する自動ループ制御が行なわれます。
# Time を 0 に指定したらアニメーションを止める。。ようにしたかった
# のですがデバッグしてる Mac OS X で思い通りにならなかったので
# ほっといてます。
# また Time はあまり短すぎても無駄に CPU を使うだけなので節度ある
# 設定にしましょう。
# 根性次第で複雑なアニメーションも可能です。
# ちなみにアニメーションの時間待ち中の割り込み受付は OS に依存する
# ような感じがします。OS によっては Time 設定を長くしすぎるとその
# 期間割り込みの処理が待たされて反応が鈍くなる場合があります。

Section NormAnimation
img0 7000
img1 300
img0 7200
img1 500
img0 7000
img1 300
img0 6600
img2 1000
EndSection


# Section TAG: AlertAnimation
# アラート状態のアニメーション表示の設定です。
# 設定は NormalAnimation と同じです。

Section AlertAnimation
img3 1500
img4 1500
EndSection

0 件のコメント: