Skip to content

Instantly share code, notes, and snippets.

@tadzik
Created December 21, 2011 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tadzik/1505856 to your computer and use it in GitHub Desktop.
Save tadzik/1505856 to your computer and use it in GitHub Desktop.
use NativeCall;
class Status is repr('CPointer') {
sub mpd_status_free(Status)
is native('libmpdclient.so') {}
sub mpd_status_get_state(Status) returns Int
is native('libmpdclient.so') {}
method state {
<unknown stopped playing paused>[mpd_status_get_state(self)]
}
method free { mpd_status_free(self) }
}
class Song is repr('CPointer') {
sub mpd_song_get_id(Song) returns Int
is native('libmpdclient.so') {}
sub mpd_song_get_uri(Song) returns Str
is native('libmpdclient.so') {}
sub mpd_song_free(Song)
is native('libmpdclient.so') {}
method id { return mpd_song_get_id(self) }
method uri { return mpd_song_get_uri(self) }
method free { return mpd_song_free(self) }
}
class Connection is repr('CPointer') {
sub mpd_connection_new(Str $host, Int $port) returns Connection
is native('libmpdclient.so') {}
sub mpd_connection_get_error(Connection) returns Int
is native('libmpdclient.so') {}
sub mpd_connection_get_error_message(Connection) returns Str
is native('libmpdclient.so') {}
sub mpd_connection_free(Connection)
is native('libmpdclient.so') {}
sub mpd_run_current_song(Connection) returns Song
is native('libmpdclient.so') {}
sub mpd_run_status(Connection) returns Status
is native('libmpdclient.so') {}
method new(Str $host, Int $port) {
my $self = self.bless(mpd_connection_new($host, $port));
if $self.get_error {
die $self.get_error_message
}
return $self
}
method get_error { mpd_connection_get_error(self) }
method get_error_message { mpd_connection_get_error_message(self) }
method current_song { mpd_run_current_song(self) }
method free { mpd_connection_free(self) }
method state {
my $status = mpd_run_status(self);
my $ret = $status.state;
$status.free;
return $ret;
}
}
my Connection $c = Connection.new('localhost', 6600);
say 'State: ', $c.state;
my Song $s = $c.current_song;
say $s.id => $s.uri;
$s.free;
$c.free;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment