Terminal sounds

March 8, 2015, 5:56 p.m.

While looking for a way to play sounds in the terminal for those long running background tasks, I found several useful methods.

The most direct method is printing the bell character:

echo -e '\a'

This will probably work if the process is attached to a terminal, or more specifically, its stdout redirects to a tty or pts that a shell is reading from. This is true of most shell scripts. If stdout is redirected to /dev/null, echoing to stderr may still work.

If you have ALSA installed, the aplay command will take a path to a sound file and play it. NOTE: This only works for .wav files. Your ears will explode if you attempt to play anything else with it.

If you're using PulseAudio, then paplay will also work with .ogg files.

These are all poor-man's methods. The easiest way is probably to install sox, but that's no fun.

Bash internal variables

Feb. 27, 2015, 9:32 p.m.

Bash internal variables are the special variables like $PWD, $!, etc. One cool one is $PIPESTATUS, which contains the exit statuses of every command in a pipe. Useless, but cool.

Name hiding

Feb. 3, 2015, 12:22 a.m.

In C++, when a method is declared in a subclass, all overloaded versions of the function with the same name in the base class becomes "hidden" to the subclass. So if class Base has a method virtual void foo(int), and subclass Derived has a method void foo(string), calling Derived::foo(0) will generate a compile error, because the int version "doesn't exist". This is called name hiding.

There are arguments for why there is such a rule. They sound somewhat reasonable, until I realized that most of the problems are actually caused by the programmer; specifically, overloading a function when you shouldn't. If you have:

struct Base {
    virtual void foo(int) { /*stuff*/ }

In what scenario would you add a void foo(double) to a derived class? Wouldn't such a function usually be doing something different, and thus, be given a different name? Nobody would put a foo(int) and foo(double) in the same class, and doing so in the same hierarchy chain equally makes no sense. As it is, this is just begging for confusion, and name hiding is a futile attempt to treat a symptom, while neglecting the disease.

Perhaps this goes more into the philosophy of why overloaded functions exist at all, but at least for me, overloads should have only one, specific purpose. Consider different ways of loading a resource from a file:

struct Resource {
    Resource(char* data, unsigned size) { /* stuff */ }
    /* class-specific stuff */
struct Base {
    virtual Resource load_resource(char* data, unsigned size) {
        return Resource(data, size);
    virtual Resource load_resource(ifstream& fin) {
        vector<char> data;
        // ... read from ifstream into data ...
        return load_resource(&data[0], data.size());
    virtual Resource load_resource(string path) {
        ifstream fin(path.c_str());
        return load_resource(fin);

A user of this class has the option of instantiating a Resource in three different ways. What if I add a subclass that slightly transforms a Resource before returning it?

struct Derived: public Base {
    virtual Resource load_resource(char* data, unsigned size) {
        Resource r(data, size);
        r.tag = "Derived";
        return r;

This will now error if I call any of the other versions of load_resource. g++ will just say it can't find them. BUT AREN'T THEY RIGHT THERE? No, says g++, I can't see beyond the first version of the method that I find, even if the arguments don't fit at all.

Graphical Shutdown

Jan. 29, 2015, 11:58 p.m.

How do desktop environments poweroff, reboot, and call other ACPI functions that control your laptop/desktop state without requiring the root password? Certainly not through the tradtional shutdown commands, which require root access.

It turns out modern desktop environments make use of managers such as ConsoleKit to track user sessions, and PolicyKit to decide what users can do what privileged actions. In order to register an intent to poweroff, the user can send a dbus message to ConsoleKit, and depending on the policy, ConsoleKit may approve of the request and initate the shutdown.

With the advent of systemd, ConsoleKit is being phased out by a newer and simpler session-manager called logind. However, the basic mechanism behind powering off remains the same.

C++ using

Jan. 11, 2015, 12:03 p.m.

Apparently using <namespace>::<definition> is a thing.

It's like a wimpier version of Python's import ... as ... I guess.

newer << Page 4/6 >> older