2008-04-15

Goodbye xorg.conf!

After reading this article on xrandr, I wanted to see how total autoconfiguration works on X Windows.

As a start, I tried removing xorg.conf file completely and restart X. The sky didn't fall down! In fact, I didn't notice any change. Everything from USB hotplug to OpenGL continued to work as before.

The only tweak needed was to the old font system. Unlike fontconfig, the old X font system seems to depend on the "font path" set in xorg.conf. This was a problem for using my custom SUN22x12 font in xterm. After adding the following lines to ~/.fvwm/preferences/Startup, this problem was gone, too.

AddToFunc InitFunction
+ I Exec exec /usr/bin/xset +fp /usr/local/share/fonts

Yes, I use fvwm-crystal, a "polished" version of FVWM. Old school, so what?

Read more...

2008-04-14

Simplifying Digital Camera Access on GNU/Linux

Digital camera access is simple enough on GNU/Linux, but with a couple of tweaks here and there, it can be made even simpler.

Summary: I keep my photos in directories named "yyyy-mm-dd" by the date taken. When I plug in the camera, photos are automatically downloaded and sent to correct directories. If you like to know how I did it, please read on!

Accessing digital cameras has always been simple on GNU/Linux. A large number of digital cameras are supported out of the box. When using the shell, arguably gphoto is the most convenient. Running gphoto with the "P" option autodetects the camera and downloads all the photos in it.

% gphoto2 -P

I keep all my photos in a "photos" directory with subdirectories in the "yyyy-dd-mm" format indicating the date taken.

First step of simplification is to automatically put each image into the correct location. Digital cameras put a lot of Exif information into each image, so extracting the date taken is quite straightforward. I use a simple tool called exif to do this.

Both gphoto2 and exif are available on Debian.

# apt-get install gphoto2 exif

After some trial and error, I figured that the images taken with my Canon PowerShot S3 IS have a tag 0x132 indicating the date each photo was taken.

% exif -t 0x132 IMG_0416.JPG 
EXIF entry 'Date and Time' (0x132, 'Date and Time')...
Tag: 0x132 ('DateTime')
  Format: 2 ('Ascii')
  Components: 20
  Size: 20
  Value: 2007:10:22 06:05:49

What we want is in the "Value:" line. After filtering that line with grep, and using sed a couple of times, we can get the date in yyyy-dd-mm format.

% exif -t 0x132 IMG_0416.JPG | \
    grep 'Value: ' | \                           # Filter the line with "Value:"
    sed 's/.*Value: \(....:..:..\) .*/\1/' | \   # Get the yyyy:mm:dd part of the value line
    sed 's/:/-/g'                                # convert ":" to "-"
2007-10-22

If you want to understand exactly what each step is doing, try the above pipeline by adding one filter at a time.

Then I put together a small script to move each image in the current directory to ~/pictures/yyyy-mm-dd/ subdirectories where I want them.

gphoto2 -P

for i in *.JPG
do
    date=$(exif -t 0x132 $i | \
        grep 'Value: ' | \
        sed 's/.*Value: \(....:..:..\) .*/\1/' | \
        sed 's/:/-/g')
    dir="/home/anuradha/pictures/test/$date"
    mkdir -p "$dir"
    mv -f "$i" "$dir"
done

Notice that I use a test directory. I saved this in ~/bin/, and made it executable.

Now comes the fun part. After connecting the camera to the computer, I used "lsusb" to find out its vendor ID and product ID are 04a9:311a. The following udev rule in /etc/udev/rules.d/010_local.rules invokes the above script whenever this camera is plugged in.

ACTION=="add", BUS=="usb", \
    SYSFS{idVendor}=="04a9", SYSFS{idProduct}=="311a", \
    RUN+="/home/anuradha/bin/pictures.sh"

Well, matters are a little more complicated. Udev seems to invoke the script multiple times. So I added two extra "features" to stop that.

  • Adding a lock file to prevent multiple simultaneous running of the script.
  • Use a "timestamp" file at the end of the script, and not run again "too soon" (60 seconds turned out to be ok).

These made sure that the script is run only once when the camera is plugged in.

I used the number of seconds since the Unix Epoch given by the stat and date commands. If the timestamp file was created less than 60 seconds ago, the script aborts.

So here is the complete script:

#!/bin/bash

set -e

user=anuradha
group=users
pictures="/home/$user/Pictures"
download="$pictures/.download"
logdir="$pictures/log"
log=$(date +"$logdir/%Y-%m-%d");
cooldown=60

lock=/tmp/.pictures.download
lasttime=$lock.time

# Avoid multiple simultaneous runs
ln -s $lock $lock || exit 0

# Abort if we had run less than $cooldown seconds ago
if [ -f "$lasttime" ]
then
    t1=$(stat -c '%Z' $lasttime)
    t2=$(date +'%s');
    dt=$((t2 - t1))
    if [ $dt -lt $cooldown ]
    then
        rm -f $lock
        exit 0
    fi
fi

# Take it slowly ;-)
sleep 3

mkdir -p $download
mkdir -p $logdir
rm -f $download/*

# Get the photos, all of them
cd $download
gphoto2 -P

for i in *.JPG
do
    date=$(exif -t 0x132 $i | \
        grep 'Value: ' | \
        sed 's/.*Value: \(....:..:..\) .*/\1/' | \
        sed 's/:/-/g')
    dir="$pictures/$date"
    if [ ! -f "$dir/$i" ]
    then
        [ -d $dir ] || mkdir -p $dir
        chown $user:$group $i
        chmod 644 $i
        chown $user:$group $dir
        mv -f $i $dir
        echo "$date/$i" >> $log
    fi
done

cd
rmdir $download

# Add a timestamp
touch $lasttime

rm -f $lock
exit 0

Read more...

2008-04-09

Network Traffic Accounting

After totally automating my Mobitel 3G connection, the next natural step was to setup some kind of a traffic accounting system. I wanted to avoid tools that monitor individual packets, because that was an unnecessary overhead. vnStat turned out to be a perfect match.

Here are the steps in setting up vnstat on Debian. Good news is that vnstat in Debian comes with proper crontab entries and network up/down hooks already in place.

  • First step, obviously is to install vnstat:
    # apt-get install vnstat
    
  • Create a new configuration:
    # vnstat --showconfig > /etc/vnstat.conf
    
  • Edit /etc/vnstat.conf and set the default interface to "ppp0".
  • Create an empty database for ppp0:
    # vnstat -u -i ppp0
    

Now vnstat starts counting network traffic. The default crontab seems to run "vnstat -u" every 5 minutes.

Then I installed this simple web based frontend called vnStat PHP frontend. Installation is just a matter of unpacking:

# cd /usr/local/src/
# wget http://www.sqweek.com/sqweek/files/vnstat_php_frontend-1.3.tar.gz
# cd /var/www
# tar -xzvf /usr/local/src/vnstat_php_frontend-1.3.tar.gz
# mv vnstat_php_frontend-1.3 vnstat

Then I had to edit /var/www/vnstat/config.php and set the following values.

$iface_list          = array('ppp0');
$iface_title['ppp0'] = 'Mobitel 3G';
$vnstat_bin          = '/usr/bin/vnstat';

Pointing a browser to http://localhost/vmstat/ showed that everything is working fine.

I also have the following .htaccess file in vnstat directory to avoid access from remote hosts:

Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128

Read more...

Mobitel 3G with Huawei E220 on Debian

Finally I decided to shift my mobile Internet connectivity from GPRS to HSDPA by getting a Mobitel 3G broadband connection.

The package includes a Huawei E220 HSDPA USB modem (Wikipedia article), SIM card and connection, connection fee and first month's bill waived, a Rs 5k deposit which shall be refunded after an year.

Huawai E220 is known to work out of the box on Linux after 2.6.20. However, some of the most recent kernels seems to have a conflict with the USB mass storage driver. It means, the disk drive in the modem with Windows drivers get detected, but not the modem. I am presently running Linux 2.6.24.4, which also exhibits this behavior.

Bud suggested a quick workaround: to start the computer while the USB dongle is plugged in. This worked, and the modem was autodetected as /dev/ttyUSB0.

The sales person at Excel World Mobitel outlet told me that the APN has to be statically set to "mobitel3g" and the number is "*99***1#". It was easy to find the AT commands to do this. I created the following /etc/wvdial.conf file and running "wvdial" afterwards took me to the Internet. Username and password was just to stop wvdial complaining.

[Dialer Defaults]
Modem = /dev/ttyUSB0
Baud = 1843200
Modem Type = Analog Modem
Init2 = ATZ
Init3 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init4 = AT+CGDCONT=1,"IP","mobitel3g"
Dial Command = ATDT
Phone = *99***1#
Username = foo
Password = bar
Stupid Mode = yes

Going with the "get it working, then make it better" philosophy, then I looked at the conflict with the USB disk. There are couple of suggestions to the kernel usb-storage driver discussed on the above thread - which didn't work for me unfortunately, but just running this huaweiAktBbo utility did the trick. So I copied the binary to a standard location.

# apt-get install build-essential libusb-dev   # just to be sure
# cc -o huaweiAktBbo -lusb huaweiAktBbo.c
# cp huaweiAktBbo /usr/local/sbin/

Then I created a small script to initiate the connection:

#!/bin/sh

lock=/tmp/.mobitel.lock
if ln -s $lock $lock
then
    [ -c /dev/ttyUSB0 ] || /usr/local/sbin/huaweiAktBbo
    sleep 3
    if [ -c /dev/ttyUSB0 ]
    then
        cp -f /etc/wvdial.conf.mobitel /etc/wvdial
        /usr/bin/wvdial
    fi
    rm -f $lock
fi

exit 0

Lock can also be created in /var/run, and notice that I have not done a "set -e", because if wvdial stops with an error, we still need to remove the lock. The lock is there to avoid multiple invocations of the script.

Then I created a simple udev rule to automatically connect whenever the E220 is plugged in. You can use "lsusb", among others, to find the vendor ID and product ID. I created a new /etc/udev/rules.d/010_local.rules file with the following line.

ACTION=="add", BUS=="usb", SYSFS{idVendor}=="12d1", \
    SYSFS{idProduct}=="1003", RUN+="/usr/local/sbin/mobitel.sh"

That's it! Now I am on the Internet automatically whenever the device is connected to the computer!

The only remaining "problem" was hal which was still trying to automount the USB disk. As suggested in ArchLinux wiki, creating a file /usr/share/hal/fdi/preprobe/20thirdparty/10-huawei-e220.fdi with the following fixes this:

<?xml version="1.0" encoding="UTF-8"?>
<deviceinfo version="0.2">
 <device>
   <match key="usb.vendor_id" int="0x12d1">
     <match key="usb.product_id" int="0x1003">
       <merge key="info.ignore" type="bool">true</merge>
     </match>
   </match>
 </device>
</deviceinfo>

The connection is fast and way cheaper than GPRS when it comes to volume. HSDPA costs one rupee per MB, while GPRS costs 20!

Update: I noticed that udev runs the script multiple times, so as a quick fix I have added a test for /dev/ttyUSB0 in a couple of places in the script to make matters better.

Read more...

2008-03-30

HDR Imaging on FOSS

High dynamic range (HDR) imaging is an interesting aspect of modern photography.

Today I demonstrated HDR imaging and panoramas at AG School of Business and Computer Studies, Negambo. Here are some resources I listed during this session.

Read more...

2008-03-07

More Sinhala Web Sites Using Unicode

Use of of Sinhala Unicode on the Web is slowly, but steadily, growing.

Two years ago, our good friend Niranjan Meegammana started the Sinhala Unicode group to bring together all those who are involved in the development of Sinhala Unicode to communicate, and to use Sinhala itself to do so. Today the group has become one of the very active forums on the topic, and provides a living example for the success of the standard.

Although some newspapers are still reluctant to move from legacy font tricks to Sinhala Unicode, Dinamina and e-thalaya online newspapers made a bold move by using only Unicode in their web sites, providing fine examples that the implementations are mature.

This Sinhala blog sindicator aggregates dozens of blogs written in Sinhala.

My previous posts on this topic are here, here and here.

Read more...

2008-02-06

The Old Order Changeth

"The old order changeth, yielding place to new" [said King Arthur] -- From Morte D'Arthur by Lord Tennyson

And so again the time has come for a change. I decided to spend more time in the wilderness where I came from, and move away from this city life, to which I will never get used to. ;-)

I started working as a freelance consultant starting 1st of February, 2008. Initially I am focusing on software/IT, but hoping to expand to CNC sooner or later.

I will also continue to work for Virtusa as an external consultant.

You can reach me at anuradha at taprobane dot org. If you forget it or loose it, searching Google for "anuradha" or "anuradha ratnaweera" should find my home page with contact details and links.

Read more...

2008-01-11

FBI on LTTE

The FBI site has this article on how to stop helping terrorism while living in the US. It is also the top story on the site as of today.

Read more...

2007-10-16

My Photo Blog

I'm featuring selected photos in my photo gallery on a photo blog.

Read more...

2007-07-22

FVWM-Crystal Windows Manager

I always liked to have FVWM as my window manager. However, it lacks three important features I need on my desktop:

  • Taskbar
  • Notification area (AKA system tray)
  • Application launcher

The FVWM-Crystal window manager adds exactly these features to vanilla FVWM, along with other subtle goodies all over.

Lack of desktop icons has never been a problem to me. Launchers are best left in a corner, so there is no need to "show desktop" or "hide windows" in order to use icons.

Another nice thing about FVWM-Crystal is the ability to start a terminal just by right-clicking anywhere on the desktop. Even blank areas between launcher, workarea switcher and notification area would do, so there is no need to resize a maximized window to do this. Another nice thing is the ability to start a small terminal by pressing Alt + grave(`).

I know several users who have switched to FWVM-Crystal and are happy. :-)

Read more...