08 Nov 2009
Sending Keystrokes to Applications in Linux (Programatically)
Lately I've been using the Awesome window manager, a simple dynamic window manager based on dwm. It's great, but it seems to fight with imwheel, which I've long used to map my mouse's thumb buttons to page-up and page-down. I wondered: why is there no reasonable imwheel alternative?
I tried xrebind and xnee, but it wasn't easy to get either to do what I wanted. I realized that Awesome has a built-in mouse binding support, but doesn't have a way to send keystrokes to client applications. So, I ended up figuring out a little C program that sends a keystroke to the focused client, and telling Awesome to run the program with the appropriate argument when the mouse thumb buttons are pressed.
Here's the C code:
#include <X11/extensions/XTest.h>
int main(int argc, char **argv) {
int keycode;
Display *dpy = XOpenDisplay(NULL);
if (!dpy) return 1;
keycode = atoi(argv[1]);
XTestFakeKeyEvent(dpy, keycode, 0, CurrentTime);
XCloseDisplay(dpy);
return 0;
}
And here's the Awesome config:
mouse
{
client
{
button = "8"
command = "spawn"
arg = "exec /home/adam/.local/bin/sendkey 117"
}
client
{
button = "9"
command = "spawn"
arg = "exec /home/adam/.local/bin/sendkey 112"
}
}
Perhaps someone else will find this useful (or integrate it into Awesome).
24 Sep 2009
Typesetting MIPS Assembly Code in LaTeX
I'm TAing an introductory architecture course this term, and for each lab assignment I need to present some material in the lab. Since the course is taught with MIPS (using SPIM), my presentation slides sometimes need to contain MIPS assembly language. I use LaTeX and Beamer for my slides, so naturally I turned to the listings package to typeset my code.
As it turns out, though, listings doesn't include a MIPS assmebly language definition, so while it will happily typeset the code, it won't do any sort of syntax hilighting.
So, I created a language definition for MIPS. This definition treats all the directives and instructions supported by SPIM (including pseudoinstructions) as keywords, so listings will format them as such.
See the listings package documentation for how to use this style file. An example is:
\documentclass{article}
\usepackage{listings}
\usepackage{mips}
\begin{document}
\lstset{language=[mips]Assembler}
\begin{lstlisting}
.text
main:
# I'm going to output a string.
li $v0, 4
la $a0, mystr
syscall
# Bye!
li $v0, 10
syscall
.data
mystr:
.asciiz "This is my string. All mine!\n"
\end{lstlisting}
\end{document}
04 Jan 2009
Volsched Release
A few people have expressed their interest in the VolSched source, so here it is! I haven't actually tried deploying it from this tarball, so let me know if it doesn't work. Hopefully I've remembered to include everything I need to include.
This is released under the MIT License, a copy of which is included in the tarball. This basically means, use it as you will.
23 Nov 2008
Automatically Downloading Album Covers From Amazon Using Perl
I recently started using SqueezeCenter to stream music from my home computer to my computer at school. It has a nice AJAX web interface, and is a pretty slick system. The web interface will display cover art for each album. I've never cared about cover art before. At home, I use mplayer on the commandline to play my music, so I would never see the cover art. I keep my music meticulously organized into directories by artist and album, so I don't need a fancy music player with a library and such; in fact, those programs drive me absolutely insane. But, since SqueezeCenter can display album art, I suddenly wanted album art.
I found a few applications that download album art from Amazon, but none of them did exactly what I wanted. First, they would all try to download album art baesd on the artist and album found in each music file's tag. This doesn't work perfectly, especially since my music is in a multitude of formats (most of them non-MP3), and since I meticulously organize my music it's not necessary anyway. So that was problem one. Problem two is that they all wanted to either embed the cover art into the music file (fine, if excessive, for MP3s, but bad for other formats), or save the cover art somewhere based on the artist and album found in the tags. This was a big problem, since for much of my music the tags don't exactly match the directory names.
Sigh, I guess I'll have to write something. I found a blog post that describes how to get album art from Amazon using Perl. The script was almost what I needed, so I modified to to do exactly what I wanted. It takes as input a directory path in artist/album format. It tries to find the album art on Amazon, then downloads it to the directory as cover.jpg. It's also smart enough to use almost-matches, so if you have Album disc 1 and Album disc 2 in separate directories, it'll download the right album art for both. You can do something like find ~/media/Music -mindepth 2 -maxdepth 2 -type d -exec ./dlcoverart.pl "{}" \; to download album art for all your music. Here's the script:
#!/usr/bin/perl -- # -*- Perl -*-
use strict;
use Net::Amazon;
use LWP::UserAgent;
use HTTP::Cookies;
my $lwpua = LWP::UserAgent->new();
my $amzua = Net::Amazon->new(token => 'YOUR-AMAZON-API-KEY');
my $artist;
my $album;
my $path = $ARGV[0];
if($path =~ /.*\/(.+?)\/(.+?)\/?$/) {
$artist = $1;
$album = $2;
} else {
printf("Usage: dlcoverart.pl \n");
exit;
}
if($album =~ /(.+?) disc.+/i) {
$album = $1;
}
# Get a request object
my $response = $amzua->search(artist => $artist,
mode => 'music',
keywords => $album);
my $result;
my $img;
if ($response->is_success()) {
foreach $result ($response->properties) {
my $fartist = $result->artist();
my $falbum = $result->album();
if($artist =~ /$fartist/i && $album =~ /$falbum/i) {
printf("Found %s by %s\n", $falbum, $fartist);
if(defined $result && exists $result->{'ImageUrlLarge'}) {
$img = $lwpua->get($result->{'ImageUrlLarge'});
} elsif(defined $result && exists $result->{'ImageUrlMedium'}) {
$img = $lwpua->get($result->{'ImageUrlMedium'});
} elsif(defined $result && exists $result->{'ImageUrlSmall'}) {
$img = $lwpua->get($result->{'ImageUrlSmall'});
} else {
print("No images.\n");
next;
}
if($img->is_success()) {
unlink("$path/cover.jpg");
open(F, ">$path/cover.jpg");
print(F $img->content());
close(F);
printf("Downloaded %s/cover.jpg\n", $path);
last;
}
}
}
} else {
print("Error: ", $response->message(), "\n");
}
25 Aug 2008
Xubuntu on the HP 2133 Mini-Note
After getting tired of the default SUSE on my Mini-Note, I decided to install Xubuntu (my Linux of choice). It didn't all go smoothly, following the wiki instructions, so I thought I'd write it up here. Note that these are instructions for installation from a USB drive. Doing it from an external CD drive may be easier, but I don't have one of those.
Step One: Prepare the USB Stick
This is the part that I had the most trouble with. The instructions on the wiki are way more complicated than they need to be. You really don't need a fancy script that "converts" your ISO to a USB stick. You just need to do the following:
- Install syslinux (apt-get install syslinux).
- Plug in the USB stick. Note its device name (/dev/sde1 for me).
- Make sure it's formatted FAT and is bootable - use fdisk or parted if needed.
syslinux /dev/sde1. This installs the bootloader on the stick.- Mount the USB stick somewhere:
mount /dev/sde1 /mnt/usb - Copy the contents of the
isolinuxdirectory on the CD to the root of the USB stick. - Rename
isolinux.cfgtosyslinux.cfgin the root of the USB stuck. - Create a directory called
installin the root of the USB stick and downloadinitrd.gzandvmlinuzfrom here into it. - Copy the ISO for the install CD into the root of the USB stick. The installer will automatically mount and use this
Step Two: Boot from the USB Stick and Start Installing
If the first step worked, you should be able to hit F9 when booting the Mini-Note and boot from the USB stick. You can install as normal from here.
Step Three: Install the Video Drivers
When your Mini-Note boots up into Xubuntu, X probably won't start. Follow the instructions on the wiki to get the video drivers installed. Reboot. X should work now.
Step Four: CPU Scaling
The CPU scaling instructions on the wiki are basically right, but they aren't very clear on where to add the boot flags. The instructions on the MiniNoteUser wiki are better. Note that the option is acpi_osi, not acpi_os. I did this wrong a couple times.
Step Five: Wireless Drivers
Different Mini-Note models have different wireless modules in them, and you need different drivers for them. The wiki doesn't address this very well. Do an lspci to see what wireless module you have. Mine is a BCM4312 (rev 02). Look at this wiki page to see which driver you actually need, and download it. Then you can follow the wiki instructions, using the .exe you downloaded in place of sp38766.exe. When that's done, you should be able to use your wireless.
Step Six: Audio Drivers
The instructions on the wiki work fine for the audio drivers. Remember to turn the volume up ;-).
After all that, everything should work, and you should be happy!
Note (2008-08-26): I used the alternative install CD, because I always do. I think my instructions will work with the live install CD, but I haven't tried it.
23 Aug 2008
I Bought One
So, I finally decided to stop waiting for new processors and just buy an HP Mini-Note. I've had it for about a week now, and I'm very happy with my purchase. It's a bit slow playing flash videos sometimes, but for the most part it seems perfectly adequate in terms of speed.
I bought the $549 version, with the 1.2GHz processor, 120GB hard drive, and 1GB of RAM. I've ordered a solid-state drive for it, but it's back-ordered at the moment. I expect that'll provide a bit of a speed improvement. If it helps the battery life, too, that'd be nice, but I'm not expecting it to. The battery life seems to be about the advertised 2 hours, which is fine with me.
The screen is gorgeous. Everyone who sees it is impressed with how great it looks (even my hard-of-sight father). Text is a bit small, but I'll take that over not being able to see a whole webpage. The keyboard is also as great as expected. I can type on it comfortably, with very little adjustment.
Overall, I highly recommend this notebook to anyone looking for something small. It's more expensive than an Eee or an Aspire One, but for me the screen and keyboard are well worth it. If HP puts the Nano in it, I expect it'll kill all competition.
31 Jul 2008
The Imperfect World of Netbooks
Ever since I saw an Asus Eee for the first time, I've thought I'd like to get a little laptop to carry around at school. I don't want or need a desktop replacement - I have a fully-functional desktop computer at home. I don't even want a full-size laptop, as I find them too large for me to carry around everywhere. However, the Eee is so incrediblly small and light that I can actually see myself carrying it around. And it does everything I need - runs Linux, has a web browser, has ssh, etc. So, it would seem perfect.
Not so fast, though. The original Eee had two problems that kept me from buying one:
- The screen. 7" just isn't very big.
- The keyboard. It's tricky to type on. I found myself hitting quite a few wrong keys.
So, the vast range of recently-released 8.9" mini-notebooks should be perfect for me, right? Still a no go. I have yet to see one that I'm prepared to buy at the moment. Allow me to elaborate.
The HP 2133 Mini-Note is the leader by a long shot in my mind. It looks gorgeous, it has a nearly-full-sized keyboard, and it has a great screen. But it's not quite right. The first problem is the CPU. I haven't seen it in action, but everything I've read says that the VIA C7-M just doesn't quite offer enough horsepower. It may be fine for basic web browsing and email, but I like to watch a Youtube video now and then, and I'd certainly like to be able to watch full-screen movies on my little laptop. All the blogs on the internet seem to agree that the HP is lacking here. I'm hoping to see a version with the new VIA Nano processor soon, which would certainly make this a better option for me. The second, smaller, problem for me is the hard drives. I don't want one. Only the base model is offered with solid-state storage, and I would rather have one with a slightly faster processor and more RAM. See, this is a secondary computer for me. I can install Linux in under 1GB, and all I need to keep in /home is whatever bit of code I'm working on at the moment and maybe the occaisional video. I simply don't need or want the extra moving parts and power consumption of a hard drive - especially a tortoise-like 5400RPM one.
Now, I hear you all shouting "MSI Wind" and "Aspire One" and "Eee 900". These all have a bigger keyboard than the Eee 700, and all offer 8.9" screens. Better yet, many of them have the fast Intel Atom CPU and are available with an array of solid-state storage options. Cosmetic issues aside (the HP's metal casing really is gorgeous), there's one big problem with all of these. Can you guess what it is?
Yep, the screens. Each and every one of these would-be competitors has a 1024x600 screen, even the 10" MSI Wind. What the hell are these companies thinking? Cell phones have better screens than that these days. My dual 19" monitors at home give me 3200x1200, I'm simply not going to do well with 1024x600 on a laptop. Only HP offers a small laptop with a 1280x800 screen.
So, here I sit, Googling HP Mini-Note every day, hoping that one day the news will come that they've released a revision with a better CPU. Now and then I go look at other options; sometimes I even get excited about one. But then I look at the specs and see yet another shitty screen. Boo-urns.
07 May 2008
VolSched - The Online Volunteer Scheduling Solution
A couple of years ago, I wrote a script called Shift Scheduler to solve the problem of scheduling volunteers for events. I used it for a couple of installfests, and I still get lots of emails from people with questions about it. Scheduling volunteers is a pain, and having a way to let volunteers sign up for the jobs they want to do is nice. It's also an underserved need.
I noticed that a lot of people who contacted me about Shift Scheduler were event organizers, not technical people, and that they needed help to set up the script on their servers. Even some technical people had trouble with it, since it requires an assortment of non-standard Perl modules. I decided it would probably be good to provide a hosted service where people could create events, jobs and shifts just as they would with Shift Scheduler, then give potential volunteers the URL to their event and let them sign up. I figured some people might even want the world to be able to see their events, in some sort of listing.
Thus, VolSched was born. It's pretty, it works well, and it provides all the features listed above. It's free to use, and I plan to eventually release the source so others can run it themselves. We'll see what the uptake is like, but hopefully people find it useful.
18 Apr 2008
Getting Pretty Emacs Colors in Screen
Emacs 22 has extremely nice color schemes for syntax hilighting. However, they only work in terminals that support lots of colors. By default, screen doesn't support lots of colors, so you're stuck with the plain old color scheme in emacs when using screen.
I recently found a good guide about how to make the pretty colors work in screen. The original URL of the guide is http://pastie.caboo.se/125089.txt, and I've mirrored it on xvx.ca in case the original disappears.
Happy emacsing.
08 Apr 2008
Getting Things Done With Tracks
I'm a pretty organized and pretty productive person, but I've always had trouble with keeping a to-do list up-to-date, and figuring out how to stay on top of projects. I have a notebook that I write things down in when I have meetings, and a notepad at work that I write things down that I need to do, but I'm not always good at checking these regularly.
I read an article in Wired about David Allen's Getting Things Done method, and I figured it sounded like a decent idea. But I realized that in many ways, I already do this: I try to keep my inbox as empty as possible, I write down things I need to do, etc. What I needed was a tool for Getting Things Done, and my experience with traditional to-do lists and pads of paper told me that they weren't it. I searched the web a bit and found Tracks, a Ruby on Rails application that implements GTD very nicely. I've been giving it a try over the past few days, and I wanted to write down some of my thoughts on it.
One thing GTD encourages that I've found handy is making everything you write down an "actionable item." This has really changed how I use the to-do list-like bits of Tracks. Before my todo list might have said something like "develop content for orientation lab session." Now it will say "email presenter re lab session content," "email labadmin re lab session content" and "meet with presenter and labadmin re lab session content." This means that I don't need to keep as many notes attached to a to-do item, since each one is a speific thing I need to do. This is made feasible by the GTD notion of "deferring" an actionable item. I need to email the presenter right away, but I don't need to email labadmin until next month, and I won't be able to meet with them until after I've heard back from both. So, I create all three to-do items, but only make emailing the presenter show up right away; the other two will show up in a month. Making my broad to-do items into small, concise actionable items has really helped me.
I have noticed a couple of issues: 1) I have to remember to put everything into Tracks. If I think of something while I'm in bed, or when I'm talking to someone, I have to remember it until the next time I'm at a computer (or write it down). 2) I need to figure out a way to integrate Tracks with my email. It'd be nice if I could tell Tracks I'm blocking on someone, and have it automatically unblock that item when I get an email from that person. I might just work on this. 3) A non-GTD issue: Ruby on Rails uses an ass-load of memory. I can't run it on xvx.ca, I have to run it on my home machine because it eats memory like nobody's business.
Anyway, I might post more about GTD/Tracks sometime, but that's it for now. Give it a try if you like, it's working pretty well for me.
RSS Feed