Author Archives: tehwalrus

About tehwalrus

Lib Dem, Software Developer, General Geek.

tcl – choirs of singing angels

seriously, I have not had this much of a eureka moment since I ran my first rails application.

TCL is an ancient, Lisp-inspired, unix based scripting language – what’s so exciting about that?! Let me tell you…

TCLKIT is a redistributable binary version of TCL/TK, prepackaged for Windows, Mac, Linux and Solaris on multiple architectures (and, if you can find a platform they haven’t built it for yet, you can always download the code and build it yourself :)). A quick test of mine has shown a (very basic) GUI prompt is possible on Windows 7 64-bit, Windows XP 32-bit, Mac OS X (10.6), openSUSE 64-bit (gnome) and ubuntu 32-bit (kde), all using ONLY the appropriate TCLKIT binary and the following three lines of code:

package require Tk
label .hello -text "Hello World"
pack .hello

More importantly, it also has a kickass interface for catching stdout from preinstalled command line utilities, namely:

set output [exec ls -la /]

will do exactly what it looks like – run the shell command and store the stdout in a variable called output. (which you have to call $output for the rest of the script.)

That is all for now, more posts if I find anything else awesome (or terrible) about this way of writing short cross platform scripts to wrap command line calls in string parsing complex logic, and occasionally firing simple dialogs.


.NET command line compiler – embedding .ico files as resources

Clearly C# / .NET is missing all the purity of a proper programming language – when has any developer sat down with an editor and a terminal and made something with it?

Well, I am currently trying (it’s an open source app and I want it to build from a batch file without needing a proprietory IDE) and it’s hard.

The particular thing that irritated me today was embedding an ICO file into an assembly as a resource. I tried plain old
csc /resource:subdir/file.ico
I tried
resgen subdir/file.ico subdir/file.ico.resource

BUT NO, you can NOT has cheeseburger (said the compiler to the lolcat). I finally solved the issue by realising the following:

  • you need to embed the item at compile time
  • you need to extract it at runtime using an identifier
  • you need to specify the identifier when you embed at compile time.

Great Scott!! so, in build.bat:

csc [snip] /res:subdir/file.ico,app.file.ico [snip]

This embeds the resource with identifier "app.file.ico". In your application when you want to create an Icon instance from the file:

Icon i = new Icon(this.GetType().Assembly
  .GetManifestResourceStream("app.file.ico"));

There didn’t seem to be a clear answer on the subject on Google, so here you go internets! 🙂

(by the way this is using .NET 2.0, the standard SDK.)


How to make ubuntu SO fast

So, I am running Ubuntu netbook remix, lucid lynx, on my Eee PC 1005HA (Seashell, black). It runs pretty fast, but I’m always looking for that extra bit of juice, especially given the “mere” single core processor and 1GB RAM it makes do with, compared to my Desktop which runs win 7 64-bit on an AMD64x2 with 8GB.

I originally went down this route to get my webcam working better – at the moment it can only do stills but not video – but the result was so much faster that I would recommend this to anyone with a sluggish netbook and a 24 hour period where they don’t need it.

Building a Kernel is EASY on Ubuntu

Your first reference is Ubuntu’s own docs on building a kernel. This link is the dependency installation, and this one will take you straight to the relevant paragraphs – but don’t go there yet! Ubuntu makes this process much easier than it is traditionally (anyone who’s ever tried Gentoo will know what I mean). You proceed as follows:

1: Install dependencies

You’ll be needing a few things before you can build C code, like GCC, Make, and various other developer utilities. Copy and paste the apt-get commands for doing this from the docs above before you start (you can paste in a terminal using Ctrl+Shift+V). Make sure you also install the ncurses libraries from the second set of instructions too.

2: Download the Kernel source code

This need not be scary, and while the link above recommends grabbing the latest source from their git server, this is almost certainly a rubbish idea (unless you are interested in writing some low low low level C code as a developer…) the way to do it is to install a package using aptitude, which will give you a zip of the source (specifically a “BZIP2”) so that you don’t need to install versioning tools (like git-core) just to grab a file.

3: Extract the source

Now DON’T do this in the place where it has been dropped for you. You need to be root (sudo) and there is NO point building a kernel as root; you can build as an ordinary user (and you’re much less likely to break everything in the process too.) Make yourself a directory in your home folder (cd ~) called src (mkdir ~/src), and extract the zip into there, just like it says to on the link above.

4: Configuration

DON’T grab the current configuration, the script may or may not pick up everything you need, just cd straight in and “make menuconfig”. If you want to go hunting for a particular driver, feel free (although basically everything is in there by default.) The thing to do to get a massive speed increase is to head into

Processor type and features —>
  Processor family (something) —>

and find the Intel Atom option (or whatever your processor is). This will optimise all your OSs most central and low level libraries for your processor specifically, which will make the whole thing MASSIVELY faster to run.

5: Build into a package (the step that takes about 24 hours on a wee netbook)

Normally, you’d build Linux kernels into a bzImage and place it in your /boot folder manually, but on Ubuntu there is a better way with many fewer pitfalls. What you are doing is building an installable .deb package that you can install in a single command later, when you are ready.

Run the command starting “fakeroot make-kpkg” from the docs, making sure you put a nice recognisable string in for the image name, and go to sleep. when you get up, check for errors and go to work (restart if you’ve hit an error). When you get home, you should have a pair of debs (linux-image-something and linux-headers-something) sitting in that ~/src folder you made. Install as the docs say, using dpkg -i.

5a: They had to include one tricky step…

You will now hit a horrid error if you try and boot your shiny new kernel – you haven’t put a RAMFS image (with the separate modules) in the same folder. (apparently this “feature” is new for lucid – YAY!) so run the two cp commands on the docs to put an appropriate script in /etc/default/kernel/ .

this is apparently the bit they didn’t want you to know — to run the install script manually, you need to supply two arguments. The first is the bit after the vmlinuz- in your kernel name (“ls /boot” will let you find it, Ctrl+Shift+C to copy from terminal,) and the second is the path to that kernel file. The script doesn’t print any error messages if you don’t specify these arguments, it just fails silently, so take care!

6: Configuring your bootloader

Run sudo update-grub, and note the position in the ordering of your vmlinuz. (you should also see an initramfs file with a similar suffix, if you don’t, try running the undocumented script again, properly). use sudo gedit /etc/default/grub to change the GRUB_DEFAULT=0 line to a different number to make your new kernel the default. The images will appear in the order update-grub spat them out, and each of the Ubuntu ones at the top will have 2 entries, one for it and one for the failsafe mode version. And, of course, you are counting from 0 not 1.

run sudo update-grub one more time to commit that change, and then restart and see what you get! If you get an error (it will almost certainly be some form of Kernel Panic, which means the one you tried to boot is broken,) you can always use the old one to see what went wrong – just select it from the boot menu before the count down hits 0.

That’s all folks!

When you get kernel-updates you’ll have to recompile again, alas, but this is what we geeks put up with to have laptops that outperform everyone else’s by a mile.

Let me know if you have a problem with this SPECIFIC method (which worked for me) and I’ll see if I can help, but as Ubuntu say this is an advanced option and help is pretty sparse, so make sure you understand enough of what I’ve said to undo it! or you’ll need to reinstall from scratch….


LaTeX and fonts

I recently came across the Liberation fonts (serif, sans, mono) which I rather like. I wanted to use the serif version in my LaTeX documents, but this seemed complicated (just google “latex fonts” and you’ll see what I mean.) The solution I found was on this web site, but I am always suspicious of this stuff whenever I see it as it is never as simple as it looks.

In the event, however, it seems to have been relatively painless to deal with. If you are on Windows and using MiKTeX (the standard 1-click installer for LaTeX on that platform) you already have both XeLaTeX and the fontspec package installed, allowing you to use any truetype font in your document.

\usepackage[cm-default]{fontspec}

Activates the appropriate module, and

\defaultfontfeatures{Scale=MatchLowercase}
\setmainfont{Liberation Serif}

firstly compensates for a sizing error on truetype fonts and secondly sets the main font. Compile and see your new shiny wonderfulness!

You can also mess with the other default fonts like so:

\setsansfont{Liberation Sans}
\setmonofont[SmallCapsFont={Liberation Mono}]{Liberation Mono}

or substitute in your own font names as required.


Getting an HTML webpage to centre in all browsers

This is something I’ve got cross about faaaar too many times, so here is a simple explanation, for my reference and yours.

1) IE (being completely pants) requires that you text-align: center; the body element. You can do this in CSS:

body {
margin:0px;
text-align: center; /* align for IE */
}

…or in the tag itself:
<body style="margin:0px;text-align:center;">

2) Next, you need to undo that so that your text is left-aligned and actually centre your main wrapper div (using auto margins), as follows (again in a stylesheet or in the HTML itself):

div#maindiv {
margin:0 auto; /* align for good browsers */
text-align: left; /* counter the body center */
width:1000px; /* or some other value */
border:0;
padding:0;
}

I like making my main div as thin as possible for the content, specified in pixels, so that the page works seamlessly on any size screen above that value. At the moment I tend to use a value around 800; wide enough for a sidebar and content, keeping up the good old LaTeX tradition of only having a thin column of text for readability, but that’s just a personal preference.


The choice of netbook OS…

So, having just ordered my Eee PC 1005HA (black), I am wondering what I should load onto it’s tidy 160GB hard disk. I will be using it for blogging, web browsing, writing (in LaTeX) and email, in particular while on the move (train carriages, the bus, etc,) so it need to be an OS with established credentials in these areas.

It comes with windows XP home, which I will shrink down to 20GB and leave there for emergency “this needs to be done in windows” situations (sad, but unfortunately required…) but XP simply fails the LaTeX test without any further questions.

So, where from there? Well, Chrome OS isn’t quite ready yet (and there is no way my primary OS is going to be beta – this thing has to be stable), so I had a look at Ubuntu Netbook Remix. I like the idea of window switching being relegated to a corner of the screen (given that you are only ever going to have 3 or 4 things open at once, tops,) and it comes with special Eee PC hacks (woo!), and of course, being Ubuntu underneath, it has access to all of Donald Knuth’s finest typesetting packages.

…and it looks shiny…

We will see when it arrives whether or not it is speedy like the wind or it sucks. If the worst comes to the worst I can always fall back on debian or gentoo with XFCE, and have my ultra optimised little beast ready with a few hours of painful command line wrangling. I think that openSUSE would be overkill (installed size of like 4 GB usually) for such a petite laptop of awesome. I will of course install with seperate / and home partitions, just so I can switch OS without the need for backups and other nonsense (loyalty to a particular flavour of Linux?! Never!)

After it’s arrival (being cheap that could be any time between now and this time next week, as I refuse to pay postage where possible out of principal…) and my successful installation I will blog again form the device itself with instructions for the intrepid netbook explorers among you, who wish to follow in my jungle-like path.


XML Serialisation in ActionScript 3

XML Serialisation – a nest of rubbishness no more.

In my experience, code to do this takes the form of massive source files devoted solely to mechanically copying properties out of one type of entity into another, which needed to be updated every time you tweaked the data model. blergh.

As such, I have written an ActionScript 3 helper library (h2j9k.XMLSerialiser) which dynamically serialises any in-memory object, and can de-serialise it back again into the same state from that XML (as long as all the important data is held in public fields or read-write properties, obviously). It preserves types, and includes child objects inline, and requires no configuration whatsoever.

I’m hosting the project at my site for open source code, so why not go take a look? It is MIT licensed so you can use it in your code at work…although it would be nice if you credited it back to h2j9k.org.

Enjoy!


C++ dev environment that works.

I have spent an awful lot of my life in the last couple of years trying to code C++. Not learning how to do it, not struggling with compiler errors or mistakes anywhere, just with the nonsense task of getting an IDE with a “compile and run” button working. Lets be clear about my requirements here. I need the standard c++ libraries, I need a compiler that is unix friendly, and I don’t need to use any win32 calls (i.e. there’s no way I’m using Visual Studio, sledgehammer to crack a nut, etc etc).

First, Kdevelop broke. The automake library it used suddenly ceased to work, and so the ubuntu and openSUSE versions just bit the dust. You could write code, but you couldn’t automatically compile it, rendering my Masters Degree source code project unusable. Useless, eh? good job I kept working TGZs of the various builds.

Next, Netbeans broke. Now to get Netbeans C++ working on windows (I abandoned Linux for development after my KDE experience) you have to install cygwin, prat around configuring netbeans (adding in new header file locations, pointing it to the appropriate bin directory, etc), but hey, give them some slack and just enter the right values. And then it throws a Java null pointer exception when you try and run. And when you try and report the bug, your report is marked as a duplicate that has been fixed in the latest version, except I am using the latest version. A similar problem appears in the v6.8 beta. AARGH. (etc).

Then, finally, I Googled “c++ dev environment windows” and read some reviews. I tried various things (Code::blocks was rubbish and threw about 400 exceptions every time I tried to compile or run, eclipse…well, that’s basically just Netbeans and I didn’t want to go down that route again.) And then I found dev c++.

It is written in delphi (yuck) it is open source (hmm, interesting) and it works out of the box. I don’t know if it was because I already had cygwin installers, but the v5 beta (4.9.9.2), specifically the version that claims to have a bundled mingw GCC, just works. You can dictate your own directory structure, it will “handle” it. You can use GCC header files, and they just work. And most importantly, you hit compile and run and you get a command window with “Hello world!” popping up so you can see your code running.

This is every headache I have ever had with this reversed and compensated for. Thank you…Bloodshed software? who write stuff in delphi? Well, thank you anyway, for this marvellous solution to what seemed to be an eternal problem.


Setting up your own flash project for free.

OK, so I am not the best person in the world in terms of finishing things off. I start a project, get somewhere (so it is doing something vaguely useful) and then forget entirely about it and leave it to rot.

But one idea that I keep coming back to is a particular project in flash (no details until I release a beta, when you will get a link and be able to see all the code), and I have installed the CS4 demo (30 day trial) that one is allowed to use to test out CS4 before purchase, and I am now in a race against the clock to finish it off before my trial expires!

I am familiar with CS4, as I use it at work, so I have a much shorter ramp-up curve than anyone else just joining in the fun (I will make available some documentation on ActionScript 3 soon, when I get around to finishing off my KB website for developers), but I hope that someone else might be up for “trying to make something in flash in 30 days”, so here are rough steps for setting up the dev environment (I’m assuming windows here, but they should work similarly on Mac OS X):

  • Go to the Adobe website and find the page to purchase Flash CS4.
  • Click on the “Try” link next to buy and upgrade, and sign up for an adobe account. Don’t worry about the fact that they have your email address, or use your “spare” account if you have one; I have never had any spam from Adobe after signing up for this.
  • Install the silly little download manager and obtain the installer. before you run it, make sure you check the following:
    1. Remove any external HDDs, USB sticks or truecrypt or linux volumes you have mounted by some magic or other. The system optimiser that runs at the start of the install process tanks if it finds any weirdness on any FS it can see.
    2. Make sure you have the latest graphics card drivers installed before you run the install; CS4 is pretty hefty as UIs go (entirely custom and native to Windows) so that CS4 optimises itself properly for your computer and isn’t painfully slow.
  • Finally, when you get to the License part of the installer, choose “I want to try for 30 days”.

Now enjoy the CS4 goodness! Make sure you use ActionScript 3 not 2 when making your applications (AS3 is much more stable, better structured and generally a bit more of a proper programming language), even if you are just going to do most stuff by drag and dropping images and components into place.

Happy flashing!


Windows 7 installation – what to watch out for!

The thing I liked about the Vista/7 activation system was that it let you “try before you buy”, much like many of the laptops that come with 60 day Office 2007 trials. You can load all your old files, install all your favourite programs and see how well they work, and so on. Unfortunately, with Windows 7, I have found that this is not the whole story with delayed activation; let me tell you how I broke it!

If you are doing the above for testing purposes, but are fully intending to activate the machine assuming there are no problems, then you won’t enter your key when you install (just in case windows sneakily activates before you are happy, and then there’s a problem).

However, the standard install disk for Windows 7 has all the different editions in there, Home Premium, Professional, Ultimate, and so on. It actually chooses which version to install based on the key you enter; if, like me when testing, you don’t enter one, it installs ultimate, which means that the home premium/professional license you have will fail to activate with a “you must buy a Windows 7 Ultimate license” warning.

Gah! So how do you downgrade your new installation (which you just spent ages configuring, installing all your games, building cygwin, and generally massaging into a usable windows install)? Oh bad luck, you can’t! You have to reinstall from scratch!

Now on that thread it says the disk will let you choose – not so my friend! There are no warnings (or if they are then I missed them in 2 RC installs and 2 final version ones)!

So just in case you are installing in a testing environment that might become a proper environment in the future – make sure you can activate before you invest too much of your life setting up the machine!