Saturday, March 20, 2010

A harmonograph in JavaScript

What's a harmonograph, you ask? Well, it's something like a Spirograph for grown-ups. A harmonograph uses a construction with pendulums to draw pretty patterns.

One particular incarnation uses two pendulums of nearly equal length, each about two metres. To the right pendulum, a slowly revolving disk is attached, which is driven by a small electrical motor. The drawing paper is taped onto the disk. To the left pendulum, an arm is attached with the pen at the end. When the pressure and thus the friction of the pen is sufficiently low, the pendulums can keep swinging for tens of minutes.

For learning and fun, I replicated this harmonograph in JavaScript with the HTML5 canvas element. The pendulum motion is approximated by a sine function, and friction by an exponential, but apart from that it should be pretty physically accurate.

Without further ado, I present to you: Harmonograph in JavaScript. Usage should be self-evident. Although some input validation is done, you will be able to break it with strange values.

With the link at the top, you can save and bookmark your creations or send them to your friends. Enjoy!

Update, 2017-01-22: Hosting is now done here on GitHub Pages. The old location redirects you. I also published the source code (“View Source” still works just fine, but it's now officially MIT licensed as well).

Sunday, March 14, 2010

Capitalizing MP3s from the command line

For my own future reference and your enjoyment, here's a neat little oneliner to transform 01 - some song.mp3 into 01 - Some Song.mp3:

rename "s/([^._'])\b(\w)(\w*)/\$1\U\$2\L\$3/g" *.mp3

It needs the prename utility from Perl, which on Ubuntu is available by default under the name rename.

Friday, February 5, 2010

Ubuntu fails to load nvidia kernel module

As much a note to myself, as a warning to others…

After a kernel upgrade, my Ubuntu Karmic started the X server in low-resolution mode. My Xorg.0.log said:

(II) LoadModule: "nvidia"
(II) Loading /usr/lib/xorg/modules/drivers//nvidia_drv.so
(II) Module nvidia: vendor="NVIDIA Corporation"
        compiled for 4.0.2, module version = 1.0.0
        Module class: X.Org Video Driver
(EE) NVIDIA: Failed to load the NVIDIA kernel module. Please check your
(EE) NVIDIA:     system's kernel log for additional error messages.
(II) UnloadModule: "nvidia"
(II) Unloading /usr/lib/xorg/modules/drivers//nvidia_drv.so
(EE) Failed to load module "nvidia" (module-specific error, 0)
(EE) No drivers available.

I tried modprobe nvidia and such, but it seemed that the module actually did not exist. This module should be installed by the package nvidia-185-kernel-source, which was present on my system. However, it turns out that the kernel module is compiled on-the-fly by a program called jockey which controls DKMS, the Dynamic Kernel Module Support.

It is possible to force a recompile using dpkg-reconfigure:

$ sudo dpkg-reconfigure nvidia-185-kernel-source
Removing all DKMS Modules
Done.
Loading new nvidia-185.18.36 DKMS files...
Building for architecture x86_64
Module build for the currently running kernel was skipped since the
kernel source for this kernel does not seem to be installed.

I need the kernel source, eh? Why the hell is that not a dependency, if the driver package is useless without it? Anyway, let's install the kernel source then:

$ uname -r
2.6.31-19-generic
$ sudo apt-get install linux-source-2.6.31

Installs fine, but makes no difference. Turns out that dpkg-reconfigure was lying: I just need the headers. Here we go:

$ sudo apt-get install linux-headers-2.6.31-19-generic
...
$ sudo dpkg-reconfigure nvidia-185-kernel-source
Removing all DKMS Modules
Done.
Loading new nvidia-185.18.36 DKMS files...
Building for architecture x86_64
Building initial module for 2.6.31-19-generic
Done.

nvidia.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/2.6.31-19-generic/updates/dkms/

depmod......

DKMS: install Completed.
$ modprobe nvidia
$

That's better.

Several bug reports indicate similar problems, but the current way this is handled is terribly inadequate. The driver package should pull in the kernel headers if it needs them. There was no warning about this when the kernel was upgraded. There was no warning when the module failed to compile on boot. A fix for a problem with the same symptoms was released back in December; another one is in the upcoming Lucid release.

Oh yeah, I ended up rebooting my system. Whatever happened to Ctrl+Alt+Backspace? (Answer.)

Update, 2010-08-04: After another kernel upgrade, my display driver was hosed again. After hours of tinkering, I typed sudo dpkg-reconfigure nvidia-current and was greeted with the message gzip: stdout: No space left on device. Apparently, my /boot partition was full (of abandoned kernels). Something to check, for whoever runs into similar problems! Also, the kernel module appears to have been renamed from nvidia to nvidia-current.

Sunday, January 24, 2010

Review of free C++ IDEs for Linux

For my new project, I started looking into IDEs for Linux for the C++ language. And since I'm testing them anyway, I might as well write some quick reviews. ‘Quick’, because I'll mostly be going on first impressions, and some things I say might be quite wrong. Feel free to correct me in the comments!

My previous IDE experience includes Visual Studio for C++ and for C#, Eclipse for Java. I will sometimes compare to these.

Code::Blocks

I am greeted with very nice wizards, and it's easy to get up to speed. There is even a template for GLFW projects, which I select. But when trying to compile (using shortcuts nothing like any other IDE I know) I get a linker error; some XF86 library is missing, and even apt-file cannot tell me where to get it.

Hunting through the build settings, I notice many other problems. The settings are nested three levels deep: a bar with huge icons on the left, scrolling tabs (the horror) on the right, and nested tabs below those. Many options are unclear. When I press Esc to cancel my changes and dismiss the dialog, nothing happens.

Some parts of the Code::Blocks UI are really good, other parts are… not so good. I wouldn't want to work in this for a long time.

KDevelop

It starts up with a blank screen, so I have to ask it to create a new project myself. Not a big deal. The New Project wizard is reminiscent of Visual Studio, so I feel quickly at home. It allows me to create a CMake project; I like CMake. The wizard gives me a Hello World program, but I cannot run it: I have to create a Launch Configuration first. Then I run my program, I think, but where does the output go? The Debug Area (‘Perspective’) is empty. Turns out that building failed, because I moved the project in the mean time, and the project file contains absolute paths. Bad!

Support for external tools seems good. I already mentioned CMake; the Valgrind profiler and Git versioning system can also be used right from the IDE.

The IDE itself actually seems like a blend of Eclipse (borrowing much of its concepts and terminology), Visual Studio (some wizards and the layout of dialogs) and Kate (the general KDE stuff). Not bad at all. Still, I had some trouble getting everything set up, but once you've taken the time to do that, KDevelop might be a decent IDE.

Eclipse

Big, bloated, but feature-rich and quite user-friendly once you've warmed up to it. That's been my impression of Eclipse for Java, which I'm quite familiar with. For Java, it's easily the best IDE around; the refactoring options alone make it worthwhile. Let's see how Eclipse with the CDT plugin performs for C++.

I install Eclipse, grab the CDT plugin through its update site and restart Eclipse. A few clicks later, I have an empty C++ project. I add a main Hello World file, and after some strange trouble with the Debug Configuration, it runs. I add a library to link with, finding the option without any difficulty. Not being unhappy about this, I continue to write some more code, to try the completion and refactoring features.

Completion pleasantly surprises me. This stuff is better than Visual Studio's. It's fast, it's accurate, it gets the contents of a newly included header file right after I saved… nice! Smart insert mode adds closing paretheses, braces and quotes, but if I type them myself as well, they are overwritten.

Error highlighting is equally fancy. Unlike Visual Studio, syntax errors are highlighted in real time, without even having to save the file.

I ask it to create a new class, and get a header and source file with a nice skeleton. It's not exactly the way I would write it, but I'm sure it's customizable.

Refactoring, unfortunately, is not as fancy as it is for Java. It is limited to renaming identifiers, and doesn't rename the source and header file accordingly. It can also extract selected expressions/statements into a separate function, but it does not create this as a member function. There are no “quick fix” options for adding include directives, like with Java. Yet, it's better than what Visual Studio offers on this front.

Eclipse with the CDT plugin is not perfect, but it's good. I could live with it.

Anjuta

I am greeted with a welcome screen, which I ask to create a new project. I am asked for all kinds of needless details, like the license type and my e-mail address. No wonder: it creates an entire autotools project for me. I tried to add a library to the linker command, but couldn't find the option anywhere. Do I really have to edit the Makefile.am?

I've only had a brief look, and Anjuta seems like a nice IDE if you want what it wants, but at first sight doesn't seem flexible enough to cope with everything.

Qt Creator

Would this work for non-Qt applications as well? It wouldn't seem so at a first glance, but I imported an empty directory as a Makefile project, which seemed to work. I had to write a Makefile, of course, which failed because the IDE quietly changed my tab to four spaces. After fixing that, and some directory issues, I could build the program. Running it required similar trickery.

Code completion works very well, and identifiers from a newly included header are available quickly. The entire IDE looks and feels smooth and polished, and is reminiscent of Visual Studio in many details.

The build system remains weird, though. It created an autotools project for me, with about a dozen files, and not offering me any choice in the matter. There is a reference to CMake in the Preferences, but no clue how to create a CMake project.

Qt Creator is a slick, clean, usable IDE. I like it. Still, it is very Qt-centric, and its build system seems inflexible. Too bad.

NetBeans

Yes, another Java IDE that can be used for C++. I have actually never used NetBeans for Java before, so I have no preconceptions about how it should work.

I apt-get the IDE and install the C/C++ plugin; this is very straightforward. I create a new C++ project, and a Makefile gets created automatically. I run the empty main function, and the program pops up in an external terminal.

Then I start editing the code, and I'm pleasantly surprised by the editor. Not just syntax errors, but unknown identifiers get highlighted in real time. Function signatures pop up in a large, but helpful tooltip. Even the filenames of include files are autocompleted! The contents of the include file become available without even having to save the file.

Yet, not all compile errors are highlighted, even though you can click to them from the compiler output window. I could not manage to get the function signature to reappear while I was typing arguments. Refactoring options are all grayed out; I assume those work for Java only.

The project settings are very much like Visual Studio, but allow for little customization. It's just enough to add a -l flag to the linker command line to link in an external library. There is also Git integration (if you install the plugin) but I didn't try it.

I checked out NetBeans as an afterthought, but that seems to have been a mistake. Of all the IDEs I tried, this is actually one of the nicest. It doesn't have many bells and whistles, and its build system might be inflexible, but the code editor makes up for it.

Conclusion

Are IDEs for C++ worth the trouble? I'm not sure. The value of an IDE lies in fast code editing, refactoring options, easy building, and error highlighting, and integrated debugging. None of the IDEs I tried score highly on all these fronts. I can understand: a flexible, usable IDE for C++ is almost impossible to create. Maybe I'll just go back to my trusted old Kate and gnome-terminal…

Tuesday, January 12, 2010

Programmers are not artists

As you may know, it takes a long time to install Visual Studio. Even on a modern system like mine, it took around twenty minutes. No program I know has ever gotten close to that, except Microsoft Office.

This means that the guy below is on your screen for a long time. Seriously, the longer you look at it, the more disturbing his face becomes.

In the style of Photoshop Disasters, I'm supposed to write a witty comment below the photo. How about this:

Management: “Hey, this guy is not smiling! Do something about it!”
Intern: “Whatever, I'll just photograph my girlfriend's mouth from this entirely different angle and slap it on. It's not as if this photo will be visible for a long time, anyway.”

Wednesday, December 16, 2009

Unfortunate cutoff

If you are writing a piece of text that may be cut off, pay some extra attention to your first sentence…

Sunday, December 6, 2009

Reinstalling GRUB with LVM from the Ubuntu cd

This post is mostly a reminder to myself. Every time I reinstall Windows, it overwrites my boot sector, and I need to reinstall it from the Ubuntu live CD to get my dual-boot system back. However, since my Ubuntu install is inside an LVM partition, this is not very straightforward. Here are the instructions. I kept them general so that they may be of use to someone else as well.

  • Start from the Ubuntu CD and open a terminal.
  • Install LVM:
    sudo apt-get install lvm2
  • Find the name of the volume group:
    sudo vgdisplay
  • Make the volume group available:
    sudo vgchange -ay name-of-vg
  • Mount the root and boot file systems, and bind the dev file system:
    sudo mkdir foo
    sudo mount /dev/name-of-vg/name-of-root-lv foo
    sudo mount /dev/name-of-boot-partition foo/boot
    sudo mount -obind /dev foo/dev
  • Change to the root of your system:
    sudo chroot foo
  • Drop into the GRUB prompt:
    sudo grub
  • Install the GRUB MBR (assuming that (hd0) is your boot drive and (hd0,0) your boot partition):
    root (hd0,0)
    setup (hd0)
  • Reboot and have your boot menu back!