Archive

Author Archive

Linux Credits

April 9th, 2010 subogero Comments off

I happened to activate the “Source” repos in Synaptic today and then downloaded the Linux 2.6.28 source code. Just for fun.

I also happened to have a look into CREDITS. It turned out to be an interesting read. It seems Linux comes from the happier parts of the world. Let’s see:

++ I'm proud to announce four Hungarian kernel hackers
++ But the happiest part of the Eastern Block seems to be the Czech Republic
++ The North rocks, or rather hacks (Finland, Sweden, Denmark, Canada)
++ The bulk comes from the West (USA, UK, Germany, Netherlands, France, Italy)
++ Eric S. Raymond is there (a.k.a. Master Foo)
++ Japan in da house
++ The Republic of China (Taiwan) beats ...
-- ... the People's Republic of China (only one hacker for 1 billion people)
-- India, just one? Come on! (Even he is Malaysian)
-- Same for Israel. Although I always knew Haifa is the best place there
-- Absolutely zero from Islamic countries.

I have a theory about the effects the “Religion of Peace” has on the human brain. It seems to be confirmed.

Perl 3

April 6th, 2010 subogero Comments off

The Camel Book has arrived. It does not reach P. G. Wodehouse’s joke density (3 per word), but it’s close. And it’s 1000 pages. So Larry may have beaten Pelham Grenville in the overall joke count.

Perl 2

April 2nd, 2010 subogero Comments off

I’ve ordered the Camel Book. Can’t wait…

Categories: Uncategorized Tags: , ,

Perl

March 27th, 2010 subogero Comments off

I love Perl. The beautiful friendship started blossoming recently. I remember a few years ago, before I got infected with Unix, I was always scared to death when confronted with Perl. But after half a year of meddling with Bash, GNU coreutils and regular expressions, I happened to look into an occasional Camel-smelling script and, much to my surprise, I was literally shocked by its beauty. Let me list my personal highlights.

Perl is like human languages.

open(FILE, "< $filename") or die "$filename could not be opened for reading.";

In Perl, there are many ways to express yourself to stress different things, like in human languages. Not like in German.

if ($foo) { print "Foo!"; }
print "Foo!" if ($foo);
print "Foo!" unless (!$foo);

Perl is very terse, like good slang.

Ever heard of Perl Golf?

Perl comes with batteries included.

With everything from Bash and coreutils, without using any extra modules. In the little Hungarian calendar below, I open a filehandle for a pipe from another program, I use the command line arguments, I do string substitutions.

#!/usr/bin/perl
open CAL, "cal -m @ARGV |";
while (<CAL>) {
  s/January/ Január/;
  # translations for other months and days ...
  print;
}
close CAL;

I forget, where did I hear this “batteries included” last time?

Default variable $_, especially when it’s not even written. Just like ogc (or vice versa).

Look at the code above. Guess what the s/// substitution is working on? Guess what is printed? Exactly. The hidden $_ variable, the result of the previous operation.

Variable substitution within regular expressions.

In my first ever Perl-script I came across some substitution problem where a part of the pattern to change was stored in a variable. I thought: How nice it would be to be able to put the variable into the regexp. I googled it. Of course it works.

$foo =~ s/(^.* )$variable$/$1$bar/;

And don’t tell me it’s unreadable…

Perl is available for Windows.

At the slightly brain-damaged company I work for, I can write small programs for slightly brain-damaged users. They don’t have to install Cygwin to run them, but Perl gives me the full arsenal of Unix.

The Perl style guide.

Compactness. Readability. No bullshit.

The Camel book.

I keep laughing out loud reading it.

Larry Wall is a Bible guy.

Remember, Perl comes from a linguist who originally wanted to translate the Bible into exotic languages.

Larry Wall is always right.

Even when he is not.

Categories: Uncategorized Tags: , ,

Jaunty Keyboard Layout Settings 2

March 18th, 2010 subogero Comments off

Finally. I’ve picked up my US keyboard for the ASUS UL20A today. Installation was surprisingly easy, with no warranty-void-if-removed stickers anywhere. Time to play again with keyboard settings. Default layout became US International (with dead keys). I kept Hungarian (qwerty) as well for the occasional őŐűŰ. And now to the most interesting part, the Layout Options.

Ctrl Key Position, Make CapsLock an additional Ctrl.
Hurraaaaah!!!! The bloody CapsLock is eliminated at last!

Key(s) to Change Layout, Both Shift keys together.
A combination of keys far away from each other, never normally used together.

Use keyboard LED to show alternative layout. CapsLock.
Of course. As the bastard is dead, I can use its LED to indicate if the Hungarian layout is active.

This incredible abundance of keyboard settings is amazing. A good reason alone to choose Linux over Windows.

Bash – Check if Directory Writable

February 28th, 2010 subogero 2 comments

Back to the basics, a.k.a. the Bourne Again Shell.

How to check in a script whether a directory is writable? Many would suggest the

if [ -w $DIR ]; then echo Do semething; fi

way, but it just checks the writable flag, which may be set on something on a read-only-mounted device. It’s much better to effectively try it.

Do something if $DIR is not writable

touch ${DIR}/foo && rm -f ${DIR}/foo || echo Do something

Do something if $DIR is writable

touch ${DIR}/foo && rm -f ${DIR}/foo && echo Do something

Do something if $DIR is writable, otherwise suppress the error message

touch ${DIR}/foo 2>/dev/null && rm -f ${DIR}/foo && echo Do something

Do something if $DIR is writable, otherwise print a custom diagnostic message

touch ${DIR}/foo 2>foo && rm -f ${DIR}/foo && echo Do something
cat foo | sed 's/.*/Directory not writable!/' && rm -f foo

The above case is a bit special. Let’s see what happens:

  • touch attempts to create file “foo” in $DIR
  • it’s potential error message is redirected to file “foo” in currect dir
  • the commands afters “&&” are only executed if touch has succeeded
  • if so, file “foo” in $DIR is removed and we “Do something”
  • the error message stored in “foo” is replaced by our own, using sed
  • file “foo” with the original error message is removed.

Note: the sed-pipeline cannot be integrated into the first line, because pipelines only fail if their last command does, so we would forget whether “touch” has failed or not.

Categories: Uncategorized Tags: , ,

Apple vs Ubuntu

February 11th, 2010 subogero Comments off

Apple’s products are simply beautiful. Mac mini, iPod Nano, MacBook or iPhone: anything they do, they do it with style.

Except, they don’t always work, apparently. During the weekend’s skiing/snowboarding trip, one of us wanted to transfer some important photos (showing just-about-to-fall snowboarders) from her iPhone (gorgeous pink cover) to her MacBook (gorgeous flat chassis). No way.

So we connected the iPhone to my Ubuntu-driven notebook, the “Apple Inc. iPhone” icon promptly appeared on the desktop, and in a few minutes time the compromising pictures were transferred to the MacBook travelling on board of a carefree pendrive.

Categories: Uncategorized Tags: , , , ,

Dive into Python

January 5th, 2010 subogero Comments off

Literally.

I am very interested in the chronology of Biblical events (anything in the Bible, actually).
Problem: the Bible refers to the time of events always relative to other events. When was the Big Flood relative to Adam’s creation? All we are told is it happened when Noah was 600 years old.

The goal:

  • Create a program that calculates the time of biblical events relative to a single event.
  • The events shall be entered as specified in the Bible, that is, relative to other events.
  • Events should have an optional duration (e.g. the life of Methuselah).
  • Print the result in a nice table-like format
  • Create a timeline diagram

The means: Python

Events are entered into an xml-file, specifying a reference event and a start-time relative to that, and an optional duration. There must be a root event without a reference event, obviously. It looks like this:

<data>
  <event name="Adam" duration="930"/>
  <event name="Seth" ref="Adam" start="130" duration="912"/>
  <event name="Enos" ref="Seth" start="105" duration="905"/>
  <event name="Kenan" ref="Enos" start="90" duration="910"/>
  <event name="Mahalalel" ref="Kenan" start="70" duration="895"/>
  <event name="Jared" ref="Mahalalel" start="65" duration="962"/>
  <event name="Enoch" ref="Jared" start="162" duration="365"/>
  <event name="Methuselah" ref="Enoch" start="65" duration="969"/>
  <event name="Lamech" ref="Methuselah" start="187" duration="777"/>
  <event name="Noah" ref="Lamech" start="182" duration="950"/>
  <event name="Shem" ref="Noah" start="502" duration="600"/>
  <event name="Ham" ref="Noah" start="502"/>
  <event name="Japhet" ref="Noah" start="502"/>
  <event name="Flood" ref="Noah" start="600" duration="1"/>
  ...

The xml file is passed to the Python program as an argument, which parses it using the xml.dom.minidom package. An optional argument specifies the name of the event at zero years, otherwise everything is calculated from the root event (Adam).

$ ./tline.py BibleTime.xml Adam

We get this on the console:

       Adam     0   930
       Seth   130  1042
       Enos   235  1140
      Kenan   325  1235
  Mahalalel   395  1290
      Jared   460  1422
      Enoch   622   987
 Methuselah   687  1656
     Lamech   874  1651
       Noah  1056  2006
       Shem  1558  2158
        Ham  1558
     Japhet  1558
      Flood  1656  1657

If you call tkline.py, you also get a timeline window using the Tk-binding of Python, the Tkinter package:

Timeline

Timeline

Before, I did not know Python, never made GUI programs and never used XML. This program took me less than one day to develop. Any questions?

The source code: bible.7z

Jaunty Keyboard Layout Settings

December 28th, 2009 subogero Comments off

As I’ve mentioned my laptop came with a Hungarian keyboard, which will be changed to a US version as soon as the part arrives (6 weeks). In the meantime I’m trying very hard to live with this one. I’ve learned to use the awkward Alt-Gr key to access trivial characters like backslash, tilde or square brackets.

But there are limits. The “qwertz” stuff. The swapped “z” and “y” keys. It’s an abomination. So I went to System/Preferences/Keyboard and I found an abundance of layout settings Windows users can’t even dream of. Of course there is a Hungarian “qwerty” layout. Many, actually. When you choose a layout you get an illustration of the entire keyboard. So it was not difficult to find variant “Hungary 102/qwerty/comma/Dead keys”.

However wonderful this nice GUI for keyboard settings is, it suffers from a general illness of Linux GUI programs. They are fantastic but, in the absence of enforced corporate QA, they never get finished. This one is not finished either. The “Apply System-wide” button has stopped working. So the login screen has been still using a “qwertz” layout.

Until now. But “Config Wizard” has solved it. All these GUIs are just smart config-file editors. But never as smart as a text editor. I guess that’s the main reason those GUIs are never finished. You develop them and, at 90% completness, you realise any text-editor is better.

Anyway, this is the location of the keyboard layout config files:

User:

~/.gconf/desktop/gnome/peripherals/keyboard/kbd/%gconf.xml

System-wide (login screen):

/etc/default/console-setup

ASUS UL20A Brightness Buttons

December 24th, 2009 subogero 10 comments

As I’ve mentioned before, ubuntu 9.04 Jaunty has been running fine on my ASUS UL20A laptop. Except the screen brightness buttons, Fn-F5 (down) and Fn-F6 (up).

Fn-F5 was setting brightness to the darkest value while Fn-F6 to the second darkest one. I’ve managed to create a crufty workaround, detailed below. It’s all about hacking the configuration of acpid (the ACPI daemon).

Set Brightness Directly

There is a special file called /proc/acpi/video/VGA/LCDD/brightness which lists the available and the actual brightness values. Brightest is shown below.

szg@OG3:/proc/acpi/video/VGA/LCDD$ cat brightness
levels:  10 16 22 28 34 40 46 52 58 64 70 76 82 88
current: 88

Setting brightness is done by writing a number into this file, the example shows darkest possible.

echo 10 > /proc/acpi/video/VGA/LCDD/brightness

Explore What Events the Brightness Keys Generate

Start the program called “acpi_listen”, which will print power-management related events when they happen.

When hitting Fn-F5 (Brightness down):

video LCDD 00000087 00000000
hotkey ATKD 00000020 000000fc

When hitting Fn-F6 (Brightness up):

video LCDD 00000086 00000000
hotkey ATKD 00000011 00000106

So each hotkey generates two events. Now let’s see how these events are handled. (Hard to see, as by now the screen is very dark. Bugger.)

Configure How acpid Handles Events

The configuration of “acpid” is in the “/etc/acpi/events” directory. Each file here handles one event type, line “event=regexp” defines the event, where the regexp shall match something like above, while line “action=command” the callback.

Fn-F5 (down) video LCDD 00000087 00000000 is handled by file “video_brightnessdown”:

event=video.* 00000087
action=/etc/acpi/video_brightnessdown.sh

Fn-F5 (down) hotkey ATKD 00000020 00000000fc is handled by “asus-brightness-down”:

event=hotkey (ATKD|HOTK) 0000002[0123456789abcdef]
action=/etc/acpi/asus-brn-down.sh

Fn-F6 (up) video LCDD 00000086 00000000 is handled by file “video_brightnessup”:

event=video.* 00000086
action=/etc/acpi/video_brightnessup.sh

Fn-F6 (up) hotkey ATKD 00000011 0000000106 is handled by “asus-brightness-up”:

event=hotkey (ATKD|HOTK) 0000001[0123456789abcdef]
action=/etc/acpi/asus-brn-up.sh

As you can see the callback scripts are in “/etc/acpi”.

Test the acpid Callback Scripts

Running all four callback scipts directly from the command line revealed they don’t work at all. Absolutely no effect on brightness. That’s why I now ignore their contents completely.

The test revealed something else as well: the broken function of both hotkeys comes from outside of this acpid-config mechanism. Probably the kernel. Which I won’t try to fix, I’ll just create a workaround.

The Fix

I linked the fixed callbacks to the second event for each hotkey, “hotkey ATKD 00000020″ (down) and “hotkey ATKD 00000011″ (up). Configured by “asus-brightness-down” and “asus-brightness-up”, respectively. I’ve removed the other two config files (video_brightnessdown/up) completely, which were handling the first event for each hotkey.

The new callback script for brightness down reads the actual value from a new config file (/etc/acpi/brightness), decrements it by 6, and stores it to the config file AND the “/proc/acpi/video/VGA/LCDD/brightness” file as well.

#!/bin/bash
if [ ! -f /etc/acpi/brightness ]; then echo 88 > /etc/acpi/brightness; fi
BRIGHTNESS=`cat /etc/acpi/brightness`
if [ $BRIGHTNESS -gt 10 ]; then let BRIGHTNESS-=6; fi
echo $BRIGHTNESS > /etc/acpi/brightness
echo $BRIGHTNESS > /proc/acpi/video/VGA/LCDD/brightness

The new brighness up script is similar, just increments the value by 6.

#!/bin/bash
if [ ! -f /etc/acpi/brightness ]; then echo 88 > /etc/acpi/brightness; fi
BRIGHTNESS=`cat /etc/acpi/brightness`
if [ $BRIGHTNESS -gt 10 ]; then let BRIGHTNESS+=6; fi
echo $BRIGHTNESS > /etc/acpi/brightness
echo $BRIGHTNESS > /proc/acpi/video/VGA/LCDD/brightness

The final touch: the callback script for AC/battery events (power.sh) also writes the config file to guarantee we always start changing the brightness from the actual value.

...
for x in /proc/acpi/ac_adapter/*; do
  grep -q off-line $x/state

  if [ $? = 0 ] && [ x$1 != xstop ]; then    
    for SCRIPT in /etc/acpi/battery.d/*.sh; do
    . $SCRIPT
    done
    echo 52 > /etc/acpi/brightness
    echo 52 > /proc/acpi/video/VGA/LCDD/brightness
  else
    for SCRIPT in /etc/acpi/ac.d/*.sh; do
    . $SCRIPT
    done
    echo 88 > /etc/acpi/brightness
    echo 88 > /proc/acpi/video/VGA/LCDD/brightness
  fi
done

Now, when hitting the brightness hotkeys, the screen switches to the darkest setting for a moment, but then it works. Perfect. Nearly.