Wordpress to blogger

I wanted to move this blog to blogger for more flexible css and java script manipulation, so I wrote this py-w2b project to do that. So far I’m able to transfer blog posts, but not comments, since every comment posted by an authenticated session will become my own comments instead of the original authors. It’s trivial to use so I’ll skip the introduction here.

How Dalvik launches Android app

This is just a guess, but I think the way Dalvik, the JVM of Android, launches an app is similar to the processing launcher I wrote for Openmoko. It starts a service on dbus, then load and run the processing apps by request. This can make the start up time much shorter because basically it runs in the same jvm instance. Something like this:

1
2
3
4
5
6
7
fork()
if (child) {
load the app java class
run it
}
/* I'm parent */
wait and check if it's okay

So, during system start up, the first instance of Dalvik is created, and all the common java classes are loaded because you need to start the ‘home’ application. Now whenever you fork a new process, the linux kernel will try to be smart and only copy memory pages if the child process modified something in it. (copy on write)

Anyway, we will see after the release of Android source code.

2008/09/30: So, it turns out I’m right. According to Anatomy & Physiology of an Android, that first instance is called ‘zygote’. Quite self-explaining.

udev and allow-hotplug on ubuntu and debian

I finally got bothered enough to fix the /etc/network/interfaces problem on my Ubuntu notebook. The problem is that when my neo got connected, I want to do some automatic setup. On Debian lenny, it’s quite easy:

1
2
3
4
5
6
7
8
allow-hotplug usb0
iface usb0 inet static
address 192.168.0.200
netmask 255.255.255.0
post-up iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j MASQUERADE
post-up echo 1 > /proc/sys/net/ipv4/ip_forward
pre-down echo 0 > /proc/sys/net/ipv4/ip_forward
pre-down iptables -t nat -D POSTROUTING -s 192.168.0.0/24 -j MASQUERADE

But on Ubuntu gutsy it doesn’t work. I have to replace “allow-hotplug” by “auto”. Even if I changed that, it will only work for the first time you plugged the device. That’s because of a bug listed here. The solution is to remove the DRIVER="?*" in /etc/udev/rules.d/85-ifupdown.rules.

Now, finally, if works even after multiple plug/unplug. However, there is still one problem left: “auto usb0” basically means the system will try to bring up the device during booting. It works for “lo”, the local loopback network interface because it’s always there. But the usb0 won’t be there everytime we boot. That causes some error messages. My solution is to modify /etc/udev/rules.d/85-ifupdown.rules again, replace “auto” by “hotplug”, and use “allow-hotplug usb0” in my /etc/network/interfaces . The final 85-ifupdown.rules looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# This file causes network devices to be brought up or down as a result
# of hardware being added or removed, including that which isn't ordinarily
# removable.
# See udev(7) for syntax.

SUBSYSTEM=="net", GOTO="net_start"
GOTO="net_end"

LABEL="net_start"

# Bring devices up and down only if they're marked auto.
# Use start-stop-daemon so we don't wait on dhcp
ACTION=="add", RUN+="/sbin/start-stop-daemon --start --background --pidfile /var/run/network/bogus --startas /sbin/ifup -- --allow hotplug $env{INTERFACE}"

ACTION=="remove", RUN+="/sbin/start-stop-daemon --start --background --pidfile /var/run/network/bogus --startas /sbin/ifdown -- --allow hotplug $env{INTERFACE}"

LABEL="net_end"

Some further digging shows the difference between debian lenny and ubuntu gutsy in the handling of the “interfaces” file. On Debian:

1
2
john@buddha:/etc/udev/rules.d$ grep net.agent *
80-drivers.rules:SUBSYSTEM=="net", RUN+="net.agent"

so it runs net.agent, a script under /lib/udev/ to bring up the interfaces. On Ubuntu, this is simply the job of the 85-ifupdown.rules above.

Using Neo Free Runner as Daily Phone

NOTE: The illume info is outdated now. Just opkg install illume-theme-illume and modify /etc/enlightenment/default-profile to get the wrench and qwerty buttons.

I used the Om2008.8-update as the base image, then modify it for daily usage.

First of all I would like to use the illume default keyboard instead of the qtopia one because it gets a full-qwerty layout and it’s more suitable for terminal application. It also gives me access to illume configuration besides of the default ‘Settings’ application, which is not enough for advance usage. To do this, you need to switch to the testing repository.

  • backup the original /etc/opkg directory. cp -r /etc/opkg /etc/opkg.orig
  • replace all urls under /etc/opkg to the testing repository Then remove all the illume related packages and install the new ones.

  • opkg list_installed | grep illume | awk ‘{print $1}’ | xargs opkg -force-depends remove

  • opkg update; opkg install illume-theme-asu; opkg install illume
  • opkg list_installed | grep libe | grep cvs | awk ‘{print $1}’ | xargs opkg install Now delete the old configs and switch to the illume default profile.

  • rm -rf ~/.e

  • replace ‘asu’ by ‘illume’ in /etc/enlightenment/default_profile Prevent the qtopia keyboard from showing up:

  • Add “export QTOPIA_NO_VIRTUAL_KEYBOARD=1” into /etc/X11/Xsession.d/89qtopia The second thing I did is to lower the volume of gsm handset. The original setting is so loud that everyone around me can hear the caller talking.

  • (call someone)

  • run alsamixer
  • scroll to the right end, lower the “Speaker” volume. You can do this while talking to test the value.
  • alsactl store -f /usr/share/openmoko/scenarios/gsmhandset.state
  • (hang up) I also need Chinese font so I can read the sms messages.

  • (on host) scp <chinese_font.ttf> root@192.168.0.202:

  • mv ~/<chinese_font.ttf> /usr/share/fonts/truetype
  • vi /opt/Qtopia/etc/default/Trolltech/qpe.conf , find [Font], change FontFamily[] to the name of the ttf. (not the filename) Change back to the default om2008.8 repository.

  • mv /etc/opkg /etc/opkg.testing

  • mv /etc/opkg.orig /etc/opkg Reboot.

git clone –mirror

It seems there will be a new option for git-clone, ‘–mirror’. The command line

1
$ git clone --mirror $URL

is now a short-hand for

1
2
$ git clone --bare $URL
$ (cd $(basename $URL) && git remote add --mirror origin $URL)

Refer to http://kerneltrap.org/mailarchive/git/2008/8/2/2793244 about this.

This is extremely useful if you need to check the contents of different branches in different directories at the same time. Each directory will be a local git clone -l -s, and git push will use --mirror as well by default.

bug in python-opengl + mesa

Since like forever, I have never succeed in executing python-opengl demos on debian lenny. It always segfault during glutInitDisplayMode. After some debugging, here is why.

One should be able to call glGetError at any time. If there’s nothing wrong, it should just return something like GLNOERROR. Any raw (native) function call in python-opengl, no matter it belongs to GL, GLU or GLUT, will always call glGetError right after each call to check for error. I think this is wrong since functions like glutInitDisplayMode have NOTHING to do with glGetError. With mesa 7.0.3-5 in my system, the call to glGetError without glInit will cause segfault. I check the same thing on Ubuntu, which has an older version of mesa and python-opengl, and it does not happen.

python-opengl enabled error checking by default with OpenGL.ERROR_CHECKING, which is set to True in OpenGL/__init__.py. This code snippet can disable it:

1
2
3
import OpenGL
OpenGL.ERROR_CHECKING = False
# import other stuffs such as OpenGL.GL, etc.

or you can just modify the __init__.py to disable it by default.

Given the current status of python-opengl and low level x protocol support in python, I think the best language to do 3D in FOSS world will still be pure simple C.

oprofile

First you have to enable oprofile in your kernel, i.e. you must have

1
2
CONFIG_PROFILING=y
CONFIG_OPROFILE=y

in your kernel config. Then you need userspace utilities to use it. I used oprofile 0.9.4. First of all you need to setup it with something like

1
2
3
opcontrol --init
opcontrol --no-vmlinux
opcontrol --callgraph=5

After that, here is the script I used to do profiling against a program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
john@buddha:~$ cat bin/profile
#!/bin/sh

which sudo && SUDO=sudo || SUDO=
which sudo && SUDO=sudo || SUDO=

${SUDO} opcontrol --shutdown
${SUDO} rm -rf /var/lib/oprofile/*
${SUDO} opcontrol --status
${SUDO} opcontrol --start-daemon
${SUDO} opcontrol --start
$@
${SUDO} opcontrol --stop
${SUDO} opcontrol --dump
${SUDO} opcontrol --shutdown

Use profile <the name of your executable> to get the profile. Use opreport to read the result. opreport -l might be more meaningful. You can use opreport <the name of your executable> to only get the result related to your program, but personally I prefer to check the whole system.

generate object methods at runtime

I have been working on a dialer button class since yesterday. It makes sense to use the command design pattern here, and I want to separate the commands and the buttons so I can change the functionality of every button at runtime.

So we are talking about something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
class DialerButtons:
def __init__(self):
self.command_table = [
self.numpad_1, self.numpad_2, self.numpad_3,
self.numpad_4, self.numpad_5, self.numpad_6,
self.numpad_7, self.numpad_8, self.numpad_9,
self.cancel, self.numpad_0, self.dial]

def numpad_0(self):
self.text.append('0')

def execute(self, n):
self.command_table[n]()

and you can use this class like this:

1
2
3
dialer = DialerButtons()
dialer.execute(0)
dialer.execute(8)
Now obviously define numpad_0 to numpad_9 is a boring task. What happens if you need to define numpad 0 to 99? So, I came out with this code piece:
1
2
3
4
5
6
    @classmethod
def _numpad_commands_factory(cls):
for n in xrange(0, 10):
setattr(cls, 'numpad_%d' % n, lambda self: self.text.append(str(n)))

DialerButtons._numpad_commands_factory()
This way you initialize DialerButtons AFTER you start the program and make methods numpad_0 to 9 on the fly. At least that’s what I was trying to do. However, it didn’t come out as I expected. Every numpad method will just add ‘9’ to self.text, instead of the respective ‘0’ to ‘9’. Why?

The reason is that the context of numpad_0, for example, is actually “f(self): self.text.append(str(n)))” instead of “f(self): self.text.append(‘0’)”. so, what it does here is that it refers to the variable n inside _numpad_commands_factory, and the value of n is 9 after you executed it.

The correct code piece is:

1
2
3
4
5
6
@classmethod
def _numpad_commands_factory(cls):
def f(chr):
return lambda self: self.text.append(chr)
for n in xrange(0, 10):
setattr(cls, 'numpad_%d' % n, f(str(n)))
This way we can evaluate the value of str(n) first, then generate the appropriate function and assign it to numpad_n.

python-efl on ubuntu gutsy

hey if you have a extremely slow laptop (Pentium III) like me yet you still want to use python-efl on it, here is how.

first, compiling the whole e17 from scratch is not an option. it will take forever. there’s a ubuntu package repository for e17:

1
2
3
4
5
$ cat /etc/apt/sources.list.d/e17.list
deb http://e17.dunnewind.net/ubuntu gutsy e17
deb-src http://e17.dunnewind.net/ubuntu gutsy e17
$ sudo apt-get update
$ sudo apt-get install efl-dev libecore-imf-evas-dev

this can save a lot of time.

the other problem is that the latest cython release (0.9.8) does NOT work with python-efl. you have to install 0.9.6.14 instead. don’t forget to install python-pyrex as well.

now you’re all set, get python-efl.

1
2
cvs -d :pserver:anonymous@anoncvs.enlightenment.org:/var/cvs/e login
cvs -z3 -d :pserver:anonymous@anoncvs.enlightenment.org:/var/cvs/e co e17

it’s under e17/proto/python-efl. another funny gotcha is that the binary packages I installed were built on 20080309. so the latest python-efl will not build successfully.

1
cvs -z3 update -dP -D 20080309

fixs this.

the default build-all.sh under python-efl directory builds evas ecore edje emotion e_dbus epsilon. that’s too many. for me I just need evas ecore edje e_dbus.