Vala name clash

A friend pointed out to me lately that Vala can produce C code with clashing symbols. True, and I knew it was possible, but there is a solution. First however, the problem:

public class prefix {
    public static int other_name;
}
public class prefix_other {
    public static int name;
}
int main() {
    prefix.other_name = 1;
    prefix_other.name = 2;
    return 0;
}

And the solution:

public class prefix {
    [CCode(cname = "prefix_other_name2")]
    public static int other_name;
}
public class prefix_other {
    public static int name;
}
int main() {
    prefix.other_name = 1;
    prefix_other.name = 2;
    return 0;
}

Now that may seem like a hack, but that’s OK. Please remember that the stated goal of Vala is to bring the benefits of a modern programming language to GLib, whilst keeping compatibility (paraphrased). GLib is C based, with naming conventions, and coding standards that must be followed. So it follows that Vala must follow them, right down to the mechanisms for choosing variable names.

Posted in Programming | Tagged | Leave a comment

Colchester studio Photosomething

Photosomething is a small Colchester based photographic studio. Ok, there are quite a few of those. In fact, with the explosion of digital SLRs on the market everyone and their dog is claiming to be a “photographer”. What sets this place apart is all that happens after the clients have left the building, the art of processing. Case in point below.

Posted in Photography | Tagged , | Leave a comment

Vala struct parameters by value

In Vala, many things operate as they would in the C world. Even structs appear to do so, with one exception. When passing a struct as a parameter, it is passed by reference. So, the signature some_method(ref MyStruct s) produces identical code to the signature some_method(MyStruct s). Try the following code sample:

struct MyStruct {
    public int a;
    public int b;
}
void other(MyStruct s) {
    s.a = 10;
}
int main() {
    MyStruct s = {2,3};
    other(s);
    stdout.printf("a=%d\n", s.a);
    return 0;
}

This outputs ’10′. Not what you might expect. Well, it turns out that there is a way to get back the good old behaviour, on a per struct basis. The SimpleType attribute:

[SimpleType]
struct MyStruct {
    public int a;
    public int b;
}
void other(MyStruct s) {
    s.a = 10;
}
int main() {
    MyStruct s = {2,3};
    other(s);
    stdout.printf("a=%d\n", s.a);
    return 0;
}

The output this time is ’2′. Yey!

Posted in Programming | Tagged | 2 Comments

Vala and DBus interfaces

Things to note about Vala and DBus:

  • Vala class = DBus interface
  • Vala interface = DBus proxy

I hope that’s clear. So, how do we implement multiple interfaces on a single proxy? Just register multiple instances against the same proxy name. Also, if you are using Vala’s GDBus support, you get introspection and property interfaces for free.

Sample code for multiple interfaces:

[DBus(name = "com.my.object")]
public class MyObject : Object {
    public void hello() {
        stdout.printf("MyObject\n");
    }
    public void hello2() {
        stdout.printf("MyObject2\n");
    }
}
[DBus(name = "com.my.other")]
public class MyOther : Object {
    public string value {get; set; }
    public void hello() {
        stdout.printf("MyOther %s\n", value);
    }
}

void on_bus_aquired (DBusConnection conn) {
    try {
        conn.register_object ("/org/example/demo", new MyObject());
        conn.register_object ("/org/example/demo", new MyOther ());
    } catch (IOError e) {
        stderr.printf ("Could not register service\n");
    }
}
void main () {
    Bus.own_name (BusType.SESSION, "org.example.Demo", BusNameOwnerFlags.NONE,
                  on_bus_aquired,
                  () => {},
                  () => stderr.printf ("Could not aquire name\n"));
    new MainLoop ().run ();
}

And here is a sample python script to prove it:

import dbus
pvrProxy = dbus.SessionBus().get_object("org.example.Demo", "/org/example/demo")
iface1 = dbus.Interface(pvrProxy, dbus_interface="com.my.object")
iface2 = dbus.Interface(pvrProxy, dbus_interface="com.my.other")

iface1.Hello()
iface1.Hello2() 

iface2.Value = 'fred'
iface2.Hello()

By the way, there is a bug in the above Vala code. It doesn’t stop it working. Can you spot it?

Posted in Programming | Tagged , | Leave a comment

Vala, DBus, interfaces and segfaults

Vala, DBus, interfaces and segfaults in the valac compiler. Take note, when you markup an interface with the DBus attribute, that interface must extend Object. If it extends nothing, valac will segfault mysteriously.

Try the following code sample:

[DBus(name = "com.my.interface")]
public interface MyIface {
    public abstract void hello();
}
static int main(string[] args) {
    return 0;
}

And compile with:

valac --pkg gio-2.0 test.vala

Notice the output:

** (valac:31817): CRITICAL **: vala_ccode_identifier_construct: assertion `_name != NULL' failed
** (valac:31817): CRITICAL **: vala_ccode_identifier_construct: assertion `_name != NULL' failed
Segmentation fault

Now modify the interface to extend Object as follows:

[DBus(name = "com.my.interface")]
public interface MyIface : Object {
    public abstract void hello();
}
static int main(string[] args) {
    return 0;
}
Posted in Programming | Tagged | Leave a comment

Install Clutter on OSX

Clutter is a great Widget system that uses OpenGL(ES) for rendering and performance. It is build in C using GLib, works on many systems and has bindings for many languages.

  1. Install MacPorts from here.
  2. Open a Terminal and enter the following:
    sudo port install clutter +quartz
    sudo port install vala
  3. Enter a small test program in the terminal to try. The sample from here should do, but add a line after Clutter.init to make fonts work.
    Clutter.set_font_flags(FontFlags.HINTING);

Until I figure out why MonoDevelop Vala plugin doesn’t see the compiler, I won’t recommend it. VI is my friend today.

Posted in Programming | Tagged , | Leave a comment

Building MonoDevelop Vala plugin on OSX

This is tricky, as there doesn’t seem to be a pre-built binary to download, and I love pre-built binaries. I have Vala installed already thanks to MacPorts, so really this is about MonoDevelop. If you are not comfortable with Terminal or Bash, please skip the do it yourself bit. And finally, buyer beware, these are instructions based on what I did, and may be completely inappropriate for you, so do this at your own risk.

  1. Download and install the latest stable Mono runtime from here.
  2. Download and install the latest beta of MonoDevelop from here.
  3. From the same page, download the Vala language support tarball, and extract it by double clicking.
  4. Open Terminal and cd to the folder you just created.
  5. Enter the following:
    export DYLD_FALLBACK_LIBRARY_PATH=/Library/Frameworks/Mono.framework/\
    Versions/Current/lib:/lib:/usr/lib:/Applications/MonoDevelop.app/\
    Contents/MacOS/lib
    
    export PKG_CONFIG_PATH=/Applications/MonoDevelop.app/Contents/MacOS/\
    lib/pkgconfig
    
    ./configure
    
    make
    
    cp build/MonoDevelop.ValaBinding.dll /Applications/MonoDevelop.app/\
    Contents/MacOS/lib/monodevelop/AddIns/
  6. Next time you start MonoDevelop there should be a Vala option under New file.

Alternatively, you can download the binary I have built here. As usual, you do so at your own risk. Just extract the ZIP and copy the DLL to the folder /Applications/MonoDevelop.app/Contents/MacOS/lib/monodevelop/AddIns/.

Posted in Programming | Tagged , , | 3 Comments