Created
June 13, 2011 12:01
-
-
Save lomereiter/1022664 to your computer and use it in GitHub Desktop.
getting info from clementine for conky via dbus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* to compile: | |
valac --pkg gio-2.0 clem_info.vala | |
example of usage: | |
clem_info '${artist} - ${title}; elapsed: ${elapsed}/${time}' | |
*/ | |
[DBus (name = "org.freedesktop.MediaPlayer")] | |
interface Player : Object { | |
public abstract HashTable<string, Variant> GetMetadata() throws IOError; | |
public abstract int32 PositionGet() throws IOError; | |
} | |
string seconds_to_str(int32 seconds) { | |
int32 minutes = seconds / 60; | |
seconds -= minutes * 60; | |
StringBuilder sb = new StringBuilder(); | |
sb.printf("%d:%02d", minutes, seconds); | |
return sb.str; | |
} | |
void main(string[] args) { | |
if (args.length < 2) { | |
return; | |
} | |
Player player = null; | |
try { | |
player = Bus.get_proxy_sync(BusType.SESSION, "org.mpris.clementine", | |
"/Player"); | |
HashTable<string, Variant> track_info = player.GetMetadata(); | |
Regex regex = new Regex("\\$\\{(.*?)\\}"); | |
stdout.printf("%s", regex.replace_eval(args[1], -1, 0, 0, (match_info, result) => { | |
string param = match_info.fetch(1); | |
switch (param) { | |
case "time": | |
result.append_printf( | |
seconds_to_str(track_info.lookup(param).get_int32()) | |
); | |
break; | |
case "elapsed": | |
try { | |
result.append_printf( | |
seconds_to_str(player.PositionGet() / 1000) | |
); | |
} catch (IOError e) { | |
stderr.printf(e.message); | |
} | |
break; | |
default: | |
Variant v = track_info.lookup(param); | |
if (v.get_type().equal(VariantType.STRING)) { | |
result.append(v.get_string()); | |
} else { | |
result.append(v.print(false)); | |
} | |
break; | |
} | |
return false; | |
})); | |
} catch (Error e) { | |
stderr.printf(e.message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment