Friday, December 22, 2006

.Xmodmap hex digits

I's a couple of day I'm hacking with xmodmap and keycodes to make my keyboard behave as I expect.

Recently it sprang to my mind that with SuSE 7.1 I used a utility called xkeycaps.

Unfortunately enough xkeycaps generates an .Xmodmap with keycodes expressed as hex numbers, while xev shows code as decimal numbers

This simple ruby oneliner converts an .Xmodmap file with hex codes to one with decimal codes

cat .Xmodmap.hex |
ruby -n -e "if $_ =~ /keycode\s*(0x[A-F0-9][A-F0-9])\s*=\s*(.+?)\n/; puts \"keycode #{$1.hex} = #{$2} \" else puts $_ end">
.Xmodmap.dec
provided you called the hex .Xmodmap '.Xmodmap.hex'.

Tuesday, December 19, 2006

A Prolog XML printer in less than 100 lines.

Suppose we have an XML parser that converts an XML file into a Prolog term.
We may (quite) easily build one with DCG. I will be doing one soon, for values of 'soon' that depend on the status of my thesis.
We describe the way the prolog term is built:
Text is simply a prolog term. In a less naive implementation we may want to use the string datatype. However, to show the power of a declarative approach, things would not change a lot. Moreover, if we are using swi-prolog (a widespread open source implementation) internally itis able to use UCS (that is a superset of 16 bit Unicode). We only have to make the parser smart enough to convert entities in the right Unicode character and the whole thing will behave correctly
Attributes are represented as key:value terms, where : is a binary infix functor. Again, this is quite a natural representation.
A tag is represented like an tag(Attributes, Children) term, where term is the name of the tag, andl Attributes and Children are (possibly empty) lists of attributes and 'children'. This representation is not very clever. In fact you can have more optimized programs representing tags like element(tag, Attributes, Children). That is the way things are done in SWI-Prolog official SGML/XML parser library, but this is a simple example.
A 'child' is either a tag or a text section.
This (informal) description can be translated in a straightforward manner in a prolog program. The last thing before presenting the source is an example term representing a very simple XHTML file.

html([], [head([], ['']), body([], [p([], ['ciao ciao', img([src:'ciao.jpg'], [])]) ])])

And now the (81 lines long) prolog source code. You can call it with pp_tag(Term), where Term is the term representing the XML file.

pp_tag(Tag) :-
        !,
        pp_tag(Tag, 0).
pp_tag(Tag, N) :-
        Tag =.. [Tag_Name, Attributes, []],
        !,
        s(N),
        write('<'),
        write(Tag_Name),
        pp_attributes(Attributes),
        write(' />').
pp_tag(Tag, N) :-
        Tag =.. [Tag_Name, Attributes, Children],
        !,
        s(N),
        write('<'),
        write(Tag_Name),
        pp_attributes(Attributes),
        write('>'),
        N1 is N+1,
        pp_children(Children, N1),
        nl,
        tab(N),
        write('
        write(Tag_Name),
        write('>').
pp_text(Text, N) :-
        name(Text, Chars),
        pp_lines(Chars, N).
pp_line(Line, N) :-
        name(Atom_Line, Line),
        s(N),
        write(Atom_Line).
pp_lines(Lines, N) :-
        ((append(Line, [10|Rest], Lines), !),
            pp_line(Line, N),
            pp_lines(Rest, N)
        ;
            Line = Lines,
            pp_line(Line, N)
        ).
pp_children([], _N) :-
        !.
pp_children([X|Xs], N) :-
        !,
        pp_child(X, N),
        pp_children(Xs, N).
pp_child(Child, N) :-
        pp_tag(Child, N)
        ;
        pp_text(Child, N).
pp_attribute(Name:Value) :-
        !,
        write(Name),
        write('='),
        pp_quoted(Value).
pp_attributes([]) :-
        !.
pp_attributes([X|Xs]) :-
        write(' '),
        pp_attribute(X),
        pp_attributes(Xs).
pp_quoted(Term) :-
        !,
        write('"'),
        write(Term),
        write('"').
s(0) :-
        !.
s(N) :-
        nl,
        tab(N).

Of course this is a 'toy' implementation. You find the real thing here.

Monday, December 18, 2006

Windowmaker

How much I love windowmaker...

Now it is much less used than once... Most people do use KDE or GNOME (or XFCE) and the good old Window Managers are kind of forgotten (or maybe simply unknown to the masses). However, when I started KDE was slow and took every single byte of RAM from my machine. GNOME was somewhat lighter. However, the desktop environment was not polished and practical (it wasn't even the 2.0).

In fact some window managers/desktop environment had higher usability (at least in my opinion) even though you had to renounce to a good file-manager (and back than gmc/nautilus was not as good as today).One of those is WindowMaker.

Simple and powerful. As it is today. Well... I restored it on my Ubuntu. Without the debian menu you have to build the application menu yourself (and that is a pain in the ass). However, since most of the times I'm only using firefox, emacs and a terminal, I managed to do it in a very short time.

Saturday, December 16, 2006

Compilers: Principles, Techniques & Tool (Dragonbook)

A couple of days ago it arrived my own copy of "Compilers: Principles, Techniques & Tool" by Aho, Lam, Sethi and Ullman, better known as the "Dragonbook" (look at the cover to understand why). This weekend I am starting reading the 992 page long book. Actually I find it well written and very clear, even though I'm at the beginning of the book.

I suspect things will get more complicated while proceeding further. However, I find the subject dramatically interesting. I hope to find enough time to read it quickly and eventually start implementing something. Some ideas contained in the book I suppose will be of great help for my thesis (in fact I'm implementing something that is closely related to a compiler).

Sunday, November 19, 2006

Adding scrolling to Emacs...

Easy...
;; mouse-wheel: scroll
(global-set-key [mouse-4] 'scroll-down)
(global-set-key [mouse-5] 'scroll-up)
I found a lot of posts about this, but none worked for me. I just took some pieces of information here and there and I came up with this (that works for me).
Emacs 21.4 on Ubuntu PPC.

Wednesday, November 15, 2006

Servoy Hu? 2

Today, while checking my gmail account I read an ad about Servoy being an alternative to Ruby and Rails

Actually, I wanted to check out what was that. I thought about some crappy PHP framework that pretended to do what Rails just cloning its structure, but without Ruby power

In the homepage the first thing you learn about Servoy is that Ruby syntax is complex and that Rails has limited capabilities. Further analisys makes me think it's a Java based environment scripted in Javascript. Basically they are asserting that Javascript syntax is clearer than Ruby one (which is debatable) and that a RAD like thing built on Java is less limited than Rails. Note that Servoy is not Java: it is something built on Java (so I don't expect it to be as powerful as Java, or it would be as complex as well).I'm not against RAD or 'easy' environments. I just find ridiculous when they say that Rails is limited (that is false) and that a closed component system is more complete.

Moreover, I don't know anyone using this Servoy. I haven't read about it on tech magazines, my fellow developers haven't heard about it either. My opinion is that it's a small environment tool that uses Rails popularity (as a comparison) to convince people to pay them for something they could have done easier in Rails.

The same conclusions have been made by this blogger

Difference Lists for visiting trees (Prolog)

This is are some (simple) exercises in Prolog using difference lists.They should be correct, but I have to say I'm a beginner Prolog programmer... so...

inorder_dl(tree(X, L, R), Xs-Ys) :- 
        inorder_dl(L, Xs-[X|Zs]), 
        inorder_dl(R, Zs-Ys). 
inorder_dl(void, Xs-Xs). 
preorder_dl(tree(X, L, R), [X|Xs]-Ys) :- 
        preorder_dl(L, Xs-Zs), 
        preorder_dl(R, Zs-Ys). 
preorder_dl(void, Xs-Xs). 
postorder_dl(tree(X, L, R), Xs-Zs) :- 
        postorder_dl(R, Ys-[X|Zs]), 
        postorder_dl(L, Xs-Ys). 
postorder_dl(void, Xs-Xs). 

Sunday, November 12, 2006

Difference lists

Well.... I just discovered this new wonderful world. I really like them a lot.

I suspect I'm getting a little bit more into Prolog programming. However, I still find more exciting Haskell lazy programming. But I have to admit that my scarce love for Prolog it's more a problem of (my) ignorance.

A couple of days ago my Programming Language teacher built under my eyes a small compiler and and interpreter for arithmetic expressions in less than 10 minutes (while explaining Prolog to my fellow students). In fact he used extensively Prolog unification mechanism (since the arithmetic expressions he was compiling were also Prolog terms).

However, thanks to DCG grammars building a parser is also quite easy.

In fact I'm falling in love with declarative programming. I definitely start liking declarative languages like Haskell (functional) or Prolog (logic) a lot more than classical programming languages.

In fact I still love Python and Ruby. I like C (and  we *need* C). I also like compiled languages like ObjectiveC (waiting for Objective C 2.0...) and D. After all, I like C++ too. But what is the point of Java? I definitely don't understand. It's dramatically uninteresting.

Ubuntu on Powerbook G4 (Trackpad Synaptics Two finger scroll multibutton emulation)

After such a long title (mainly for tagging reasons) I suppose I have to write a long article.
One of the things I didn't really like about Ubuntu standard configuration, was the track-pad.
In fact, although correctly recognized (using the synaptics Xorg driver) default was really poor: the mouse moved so slowly it was unusable. For the first hours you can use an external three button mouse and live happily. However, I wanted to fix my track-pad. I found some articles on the net.
There are plenty of blog posts on this subject and if you use synaptics as a key you find even more. This is a good starting point. However, I strongly advise to read synaptics manual (man synaptics). It is synaptics with a trailing s, not synaptic, the package manager.
If you read the manual, you get a lot of informations about how to configure your trackpad. You also have some GUI configurators (gsynaptics, ksynaptics, qsynaptics). They offer less
Options (but are easier to use).
While I was able to set tracking speed and acceleration correctly (mainly using the link I posted) I'm still somewhat unsatisfied with scrolling facilities. Two finger scroll does not seem to work, and edge scrolling (using the edges of the trackpad to scroll) is far from perfect.
The best thing is that I can use tapping to emulate buttons. Tapping in the right upper corner is a middle click and right lower a right click. Now I can use the powerbook without an external mouse.
I'm to post my xorg.conf. And remember... using 'radeon' codecs instead of 'ati' should be a good thing (I've not investigated, I only read it somewhere)
My XOrg:
# /etc/X11/xorg.conf (xorg X Window System server configuration file) 
# 
# This file was generated by dexconf, the Debian X Configuration tool, using 
# values from the debconf database. 
# 
# Edit this file with caution, and see the /etc/X11/xorg.conf manual page. 
# (Type "man /etc/X11/xorg.conf" at the shell prompt.) 
# 
# This file is automatically updated on xserver-xorg package upgrades *only* 
# if it has not been modified since the last upgrade of the xserver-xorg 
# package. 
# 
# If you have edited this file but would like it to be automatically updated 
# again, run the following command: 
#   sudo dpkg-reconfigure -phigh xserver-xorg  
Section "Files"         
FontPath        "/usr/share/X11/fonts/misc"         
FontPath        "/usr/share/X11/fonts/cyrillic"         
FontPath        "/usr/share/X11/fonts/100dpi/:unscaled"         
FontPath        "/usr/share/X11/fonts/75dpi/:unscaled"         
FontPath        "/usr/share/X11/fonts/Type1"         
FontPath        "/usr/share/X11/fonts/100dpi"         
FontPath        "/usr/share/X11/fonts/75dpi"         
FontPath        "/usr/share/fonts/X11/misc"         # path to defoma fonts         
FontPath        "/var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType" 
End
Section "Module"         
        Load        "i2c"         
        Load        "bitmap"         
        Load        "ddc"         
        Load        "dri"         #
        Load        "dbe"         
        Load        "extmod"         
        Load        "freetype"         
        Load        "glx"         
        Load        "int10"         #
        Load        "record"         #
        Load        "v4l"         
        Load        "type1"         
        Load        "vbe" 
End
Section "InputDevice"         
Identifier        "Generic Keyboard"         
Driver                "kbd"         
        Option                "CoreKeyboard"         
        Option                "XkbRules""xorg"         
        Option                "XkbModel""pc105"         
        Option                "XkbLayout""it"         
        Option                "Xkb
        Options""lv3:ralt_switch"         
        Option                "XkbVariant""nodeadkeys" 
End
Section "InputDevice"         
Identifier        "Configured Mouse"         
Driver                "mouse"         
        Option                "Device""/dev/input/mice"         
        Option                "Protocol""ExplorerPS/2"         
        Option                "ZAxisMapping""4 5"         
        Option                "Emulate3Buttons""true" 
End
Section "InputDevice"          
Identifier         "Synaptics Touchpad"          
Driver                 "synaptics"          
        Option                "CorePointer"         
        Option                 "SendCoreEvents""true"          
        Option                 "Device""/dev/psaux"          
        Option                 "Protocol""auto-dev"          
        Option                 "SHMConfig""true"          
        Option                 "MinSpeed""0.30"          
        Option                 "MaxSpeed""1.10"          
        Option                 "EdgeMotionMinSpeed""200"          
        Option                 "EdgeMotionMaxSpeed""200"          
        Option                 "FastTaps""1"          
        Option                 "MaxTapTime""100"          
        Option                 "AccelFactor""0.030"          
        Option                 "HorizScrollDelta""0"          
        Option                "VertTwoFingerScroll""1"         
        Option                "HorizTwoFingerScroll""1"         
        Option                "CircularScrolling""1"         
        Option                "CircScrollTrigger""0"         
        Option                 "FingerLow""1"          
        Option                 "FingerHigh""3"          
        Option                 "LeftEdge""80"          
        Option                 "TopEdge""80"          
        Option                 "RightEdge""850"                  
        Option                 "BottomEdge""560"          
        Option                 "TapButton1""1"  
End
Section "Device"         
Identifier        "ATI Technologies, Inc. RV350 NP [Mobility Radeon 9600/9700 M10/M11]"         
Driver                "radeon"         
BusID                "PCI:0:16:0"         
        Option                "UseFBDev""true"         
End
Section "Monitor"         Identifier        "Generic Monitor"         
        Option                "DPMS" 
End
Section "Screen"         
Identifier        "Default Screen"         
Device                "ATI Technologies, Inc. RV350 NP [Mobility Radeon 9600/9700 M10/M11]"         
Monitor                "Generic Monitor"         
Default
        Depth        24         Sub
Section "Display"                 
        Depth                1                 
        Modes                "1280x786"         
End
Subsection "Display"                 
        Depth                4                 
        Modes                "1280x786"         
End
Subsection "Display"                 
        Depth                8                 
        Modes                "1280x786"         
End
Subsection "Display"                 
        Depth                15                 
        Modes                "1280x786"         
End
Subsection "Display"                 
        Depth                16                 
        Modes                "1280x786"         
End
Subsection "Display"                 
        Depth                24                 
        Modes                "1280x786"         
End 
End
Section "ServerLayout"         
Identifier        "Default Layout"         
Screen                "Default Screen"         
InputDevice        "Generic Keyboard"         
InputDevice        "Synaptics Touchpad"         
InputDevice        "Configured Mouse" 
End
Section "DRI"         
Mode        0666 
End