Blog RSS

The thought-stream of Tom Medhurst

Archive

Oct
3rd
Wed
permalink

Check out this awesome LIVE session with Pop Deco band IDLE WORDS

(Source: youtube.com)

Comments (View)
Jun
24th
Wed
permalink
Comments (View)
May
3rd
Sun
permalink

Using dmenu to access your delicious.com account

I really like dmenu. It’s like Quicksilver or GNOME DO, but much smaller, faster, and easier to extend!

The common dmenu command to quickly run a program from your path would be something like:

dmenu_path | dmenu

The dmenu_path command quickly fetches all programs from your $PATH and sends the list (newline-separated) into dmenu for user-selection.

Because of the great de-coupled design of dmenu, we can easily hack it to do something slightly cooler!

So what I thought I would try is to grab a list of all my delicious.com tags and display them in dmenu. When I select a tag and hit the return key; I want Firefox to launch and show me all my links associated with that tag…

First step is to grab the list of tags, this can be done with curl, using the command:

curl -u MY_USERNAME:MY_PASSWORD https://api.del.icio.us/v1/tags/get

This should then return the list of tags, however it returns them in XML format. :S
To grab only the tags, I thought I would pipe the xml into awk and extract the tags this way:

curl -u MY_USERNAME:MY_PASSWORD https://api.del.icio.us/v1/tags/get | \
awk '{t=substr($3, 6, 30); t=substr(0, index(t, "\"")-1); print t}'

This should now give you only the tags in a newline separeted format, just like we need for dmenu!

So the next step would be launch firefox and view the links associated to the selected tag. To do this; save the command above into a file called: delicious-get_tags.run (remember to specify a shebang and chmod +x).
We then need to create a new script file called delicious-get_tags-dmenu.run (again chmod +x and specify a bash shebang) and enter this code:

firefox "http://delicious.com/MY_USERNAME/`/path/to/delicious-get_tags.run | dmenu`"

Now when you run this script you will get dmenu appear, select a tag and firefox should launch! :)
If you would like to launch this command using your standard dmenu instance; create a link to this script in a folder in your path, like so:

ln -s /path/to/delicious-get_tags-dmenu.run /usr/bin/delicious_get_tags

Now you can run your new dmenu from dmenu by selecting delicious_get_tags ! enjoy! ;)

Comments (View)
Apr
30th
Thu
permalink

Formulae for getting the point co-ordinates for a circle

Here’s how to get the co-ordinates of a given point in a circle…
NOTE: this code is written in C#

Point GetPointPos(int numofpoints, int pointNum, int radius, int offset)
{
  double angle = 2*Math.PI * ((double)pointNum / numofpoints);
  double x = radius * Math.Cos(angle) + offset;
  double y = radius * Math.Sin(angle) + offset;
  return new Point((int)x, (int)y);
}

With this code, we can now create a geometric pattern by calling the method above like so..
NOTE: this code has been tested on MS .NET 2.0

Random r = new Random();
e.Graphics.Clear(Color.White);
const int numofpoints = 30;
const int radius = 200; // pixels
const int offset = 0; // pixels

for (int i = 0; i < numofpoints; i++)
{
  Point pos1 = GetPointPos(numofpoints, i);
  Color colour = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));

  for (int y = i; y < numofpoints; y++)
  {
    Pen pen = new Pen(colour, 1);
    Point pos2 = GetPointPos(numofpoints, y, radius, offset);
    e.Graphics.DrawLine(pen, pos1, pos2);
  }
}

Which generates this…

Geometric Pattern
Comments (View)
Apr
17th
Fri
permalink

OpenGL programming on Ubuntu (8.10 Intrepid)

Installing all dependencies

sudo aptitude install mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev libglut3-dev

Mesa is an open-source implementation of the OpenGL spec.

NOTE: freeglut3-dev is only required if you wish to use GLUT for your creating your hosting windows and handling user input.

NOTE: (If you wish to use apt-get instead of aptitude, you will also need to specify freeglut3)

Compiling a simple OpenGL/GLUT test

Mesa uses pkg-config, but if you run pkg-config (pkg-config --cflags --libs gl), you’ll notice that it’s not really worth using as all it outputs is -lGL! So for probably for the first time ever; I have chosen to manually specify the build libraries in our compilation command:

gcc -o bin/program main.c -lGL -lGLU -lglut

Then you can create a file named main.c and use the following test program (obtained from the OpenGL redbook):

#include <GL/gl.h>
#include <GL/glut.h>

void display(void)
{
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 1.0, 1.0);
    glBegin(GL_POLYGON);
        glVertex3f (0.25, 0.25, 0.0);
        glVertex3f (0.75, 0.25, 0.0);
        glVertex3f (0.75, 0.75, 0.0);
        glVertex3f (0.25, 0.75, 0.0);
    glEnd();
    glFlush ();
}

void init (void) 
{
    glClearColor (0.0, 0.0, 0.0, 1.0); // note this line is incorrect in the red-book
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (250, 250); 
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("hello");
    init ();
    glutDisplayFunc(display); 
    glutMainLoop();
    return 0;
}
Comments (View)
Apr
15th
Wed
permalink

My Perfect Desktop (or.. How to create a Frankenstien Linux Distro!) PART #2

So now I have the perfect minimal OS running (as explained in my previous article), we need to add some components and start making a beautiful desktop!

The first thing we are going to need is a UI for handling our X11 Sessions and logging in. The mainstream choices are GDM and KDM.

As I’m more of a GNOME man so I went for GDM. I will install this together with my Window Manager (which we need to manage our windows, obviously!), so I also need to choose one of these…

There are plenty of choices, as you can see for yourself, and through a bit of experience with the majority of Windows Managers; I decided that I would use OpenBox.

I chose OpenBox because it is lightweight, easy to configure, and just generally felt (and can look) better (IMHO) than the others.

To install GDM and OpenBox (and some config tools) I ran the following command:

sudo apt-get install gdm openbox obmenu obconf

When this is done, I restarted the machine.

GDM can be made beautiful using gdmsetup (installed with GDM) and you can grab loads of different themes from: www.gnome-look.org.

OpenBox can be made beautiful too, using themes from www.box-look.org and obconf (we installed it earlier, with openbox).

Here is my GDM login screen:

The next task is to install VirtualBox additions and get my desktop looking like this:

Comments (View)
Apr
5th
Sun
permalink

My Perfect Desktop (or.. How to create a Frankenstien Linux Distro!)

For Linux beginners there is no denying that Ubuntu is the weapon of choice at the moment. I think the main reason I believe that is because of APT and how up-to-date and complete their default repositories are.

I really enjoy using Ubuntu as installing a program is as simple as installing it from Synaptic and then launching it! Sometimes building stuff from source can get you into a version-control nightmare, and updating versions can be risky.. not that I shy away from this; stuff like WebKit should be build from the source as it is constantly moving. I am talking about programs which generally release every 6 months+.

The one dislike about Ubuntu is that it feels a little bloated. Say you want to understand how Linux works; where would you start? There is SOO much going on under the covers in Ubuntu it can all be so confusing/overwhelming?!

So I challenged myself to build my own perfect desktop. The idea was to take the minimal Ubuntu install (installed takes about 1Gb of precious HD space) and then slowly build up from there until I have something I can use daily for development and general email/chat/web social functions. All this would happen from inside a Virtualbox VM, so I would also have to make sure everything was compatible with the Virtualbox kernel modules.

A successful project means I would achieve the following:

  • I would be happy to use it everyday, replacing my beloved Ubuntu Desktop 8.10
  • It looks sexy, without getting in the way of my geek-y ways!
  • Be able to list the parts of the OS and how they work together
  • Be comfortable with configuring/tweaking my desktop
  • Install should take up less than 3.5Gb of disk space
  • Have something which runs significantly faster than Ubuntu

Installing Ubuntu Minimal

Ubuntu Minimal Install is a version of Ubuntu with no GUI whatsoever, and barely enough apps to boot the system!!! You can download it here.

As I was using Virtualbox, all I needed to do was to create a new Ubuntu VM (remembering to check the 3D exceleration checkbox and I know I wanted to get compiz running at some point) and mount the install ISO to get going with a command-line install.

Once the install was out of the way, all I had was a blining cursor and bash to keep me company :S

To be continued…

Comments (View)
Mar
30th
Mon
permalink

Installing VirtualBox Guest Additions 2.1.4 on Fedora 10

This can be a bit of a pain. The first time I attempted to run the installer I got the following message:

Please install the build and header files for your current Linux kernel...

Here are the steps required to get VirtualBox Guest Additions installed on Fedora 10. They begin with a freshly installed version of Fedora.

  1. Run a system update. Make sure you have the lastest version of Fedora.
  2. Reboot (this is an extremely important step!!)
  3. Open a Terminal window and type:
    • su -
    • type the root password
    • yum install -y kernel-headers kernel-devel
    • leave this terminal open and return to your host OS
  4. Now mount the VirtualBox Guest Additions CDROM
  5. Go back to the Terminal in your guest OS and type:
    • cd /media/VBOXADDITIONS_2.1.4_42893
    • (this may differ slightly, use tab to auto-complete the mount-name)
    • ./VBoxLinuxAdditions-{YOUR_ARCHITECTURE}.run
  6. Once this is complete, reboot again and you should get higher resolutions, mouse swapping, and all the other goodies that come with this kernel extension!
Comments (View)
Mar
27th
Fri
permalink

Multithreading in C++ using POSIX Threads Tutorial

As C++ doesn’t have any native multi-threading support, you will need to rely on a system APIs to achieve multithreading. M$ has their own APIs, but I want to concentrate on the POSIX threading library as it is more widely compatible.

The Theory

So what does the POSIX threading APIs look like then? Well a bit of bad news first of all to C++ developers; these are C functions (doh!), the main one in question is:

pthread_create(pthread_t *handle, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg);

Nothing too scary looking, but as it takes a C function as the start_routine you may be wondering how on earth this could be integrated into a C++ member method? Well, that’s exactly what I’m going to show you now?!

The Code

Thread.hpp

#include <pthread.h>

class Thread {
public:
    void start();
    virtual void execute();
    void wait_for_exit();
private:
    pthread_t handle;
};

Here we have defined the Thread class, telling it to hold onto a POSIX thread instance in the field handle and also prototyping three methods:

  1. start, which will launch the new thread
  2. execute, which will contain the code to execute on the new thread
  3. wait_for_exit, which will join the thread to the main thread

Thread.cpp

#include <pthread.h>
#include <iostream>
#include "Thread.hpp"
using std::endl;
using std::clog;

extern "C"
{
    // this C function will be used to receive the thread and pass it back to the Thread instance
    void* thread_catch(void* arg) {
        Thread* t = static_cast<Thread*>(arg);
        t->execute();
        return 0;
    }
}

// method which starts the new thread
void Thread::start() {
    pthread_create(&handle, 0, thread_catch, this);
}

// code which will be run on the new thread
void Thread::execute() {
    clog << "Thread:Hello From a new Thread!" << endl;
}

// wait until this thread has finished executing
void Thread::wait_for_exit() {
    pthread_join(handle, NULL);
}

The first thing you’ll notice is the C function thread_catch, this will be our C function which pthread_create will execute on the new thread, the argument will be the instance of the Thread class, so we can call it’s execute method.

The start function creates the POSIX thread and sends it the `this` pointer, so the C function can call execute on the correct instance.

wait_for_exit simply calls pthread_join which joins the main thread with this spawned thread, so the program doesn’t terminate before the thread has finished.

main.cpp

#include "Thread.hpp"
#include <pthread.h>
#include <iostream>
using std::cout;
using std::clog;
using std::endl;

class MyThread : public Thread {
public:
    void execute();
    int i;
};
void MyThread::execute() {
    sleep(i);
    cout << "Execute:" << i << endl;
}

int main() {
    const int THREAD_COUNT = 3;
    
    // create threads
    MyThread t[THREAD_COUNT];
    for (int i=THREAD_COUNT-1; i>=0; i--) { // start the threads in the opposite order to prove multi-threading
        cout << "Start:" << i << endl;
        t[i].i = i;
        t[i].start();
    }
    
    // wait until all threads have finished
    for (int i=0; i<THREAD_COUNT;i++) {
      t[i].wait_for_exit();
    }
    return 0;
}

So what I’ve done here is to write a program that creates three instances of a derived Thread class which waits for x number of seconds. The program loops through the numbers 0-2 backwards, so they are instantiated in reverse. When the start method is called on the instance it spawns the Thread immediately, however the execute command makes the instance sleep for x seconds. This should all mean that the threads are instantiated in reverse order; but the cout in execute should be execute in the correct order. Here is the output:

Start:2
Start:1
Start:0
Execute:0
Execute:1
Execute:2

Compiling

The first trick is to find out where pthread.h is. On my Mac it’s on /usr/include/pthread.h but you can find it using:

locate pthread.h

So to compile the code use the following command:

g++ -o program Thread.cpp main.cpp /usr/include/pthread.h

If you would like to know more about pthread.h, check out this site.

Comments (View)
Mar
26th
Thu
permalink

Using the new OSD Notification system in Ubuntu Jaunty in C++ (libnotifymm)

Wanna add the new OSD Notification system to your C++ (gtkmm) program, but don’t know where to start? Noticed all the tutorials on wiki.ubuntu.com are in C, Python, and C#?

Have no fear, uncle Tom is here with a quick-start guide to getting libnotifymm running on your system!

Installing libnotifymm and it’s prereqs

Open up Synaptic Package Manager and install the following packages and their dependencies:

  • libnotifymm-1.0-7
  • libnotifymm-doc
  • libnotifymm-dev

Creating a test app

Once these are installed you can create a new file called test-notify.cc and open it in your editor/IDE.

Add in the following code:

#include <libnotifymm.h>
#include <iostream>

int main() {
   Notify::init("Basic");
   Notify::Notification n("Title", "Content goes here", "notification-device-eject");
   if (!n.show()) {
      std::cerr << "Could not show notification" << std::endl;
      return 1;
   }
}

Compiling

This app can be compiled by linking it to the libnotifymm and gtkmm libraries using the following command:

g++ -o test-notify test-notify.cc `pkg-config gtkmm-2.4 --libs --cflags` `pkg-config libnotifymm-1.0 --libs --cflags`

Customization

The last argument in the Notification constructor is the name of the icon to use. You can get the list of slick new notify available icons here.

More Help

You installed the libnotifymm-doc package, so you’ll find help and sample source code at: /usr/share/doc/libnotifymm. Enjoy! ;o)

Comments (View)