Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement some actual useful functionality

  • Loading branch information...
commit 637a1ee93394996ff4dc6e874d8c8d233ae83ed7 1 parent aaa7b16
@flussence authored
View
7 README.md
@@ -1,6 +1,9 @@
# perl6-XMMS2
An XMMS2 client module for Perl 6 and a command line tool to use it with, inspired by tadzik++'s
-[perl6-MPD](//github.com/tadzik/perl6-MPD). It's actually based on the xmms2-tutorial C code.
+[perl6-MPD](//github.com/tadzik/perl6-MPD).
-Currently supports connecting, and starting playback. More to be added soon...
+I'm implementing bits of the xmms2-tutorial C code. This is being done mostly for my own amusement,
+but if you find it useful then even better.
+
+Currently supports connecting, play/pause/stop, and can tell you the current track number.
View
13 lib/XMMS2/Client.pm6
@@ -1,5 +1,4 @@
use v6;
-use NativeCall;
use XMMS2::Connection;
#####
@@ -12,19 +11,19 @@ method new(Str $client-name = 'perl6-XMMS2', Str $path = %*ENV<XMMS_PATH>) {
}
method play returns Bool {
- return ?$!connection.play;
+ return ?$!connection.playback_start;
}
method pause returns Bool {
- ???
+ return ?$!connection.playback_pause;
}
method toggle returns Bool {
- ???
+ return ?$!connection.playback_toggle;
}
method stop returns Bool {
- ???
+ return ?$!connection.playback_stop;
}
method prev returns Bool {
@@ -35,6 +34,10 @@ method next returns Bool {
???
}
+method current returns Int {
+ return $!connection.playback_current_id;
+}
+
submethod BUILD(Str $client-name, Str $path) {
$!connection = XMMS2::Connection.new($client-name, $path);
}
View
32 lib/XMMS2/Connection.pm6
@@ -6,10 +6,22 @@ use XMMS2::Result;
class xmmsc_connection_t is OpaquePointer { };
# Native functions
+sub xmmsc_playback_stop(xmmsc_connection_t)
+ returns xmmsc_result_t
+ is native('libxmmsclient') { ... }
+
sub xmmsc_playback_start(xmmsc_connection_t)
returns xmmsc_result_t
is native('libxmmsclient') { ... }
+sub xmmsc_playback_pause(xmmsc_connection_t)
+ returns xmmsc_result_t
+ is native('libxmmsclient') { ... }
+
+sub xmmsc_playback_current_id(xmmsc_connection_t)
+ returns xmmsc_result_t
+ is native('libxmmsclient') { ... }
+
sub xmmsc_init(Str $clientname)
returns xmmsc_connection_t
is native('libxmmsclient') { ... }
@@ -33,10 +45,28 @@ method new(Str $client-name, Str $path = %*ENV<XMMS_PATH>) {
self.bless(*, :$client-name, :$path);
}
-method play returns XMMS2::Result {
+method playback_stop returns XMMS2::Result {
+ return XMMS2::Result.new: result => xmmsc_playback_stop($!connection);
+}
+
+method playback_start returns XMMS2::Result {
return XMMS2::Result.new: result => xmmsc_playback_start($!connection);
}
+method playback_pause returns XMMS2::Result {
+ return XMMS2::Result.new: result => xmmsc_playback_pause($!connection);
+}
+
+method playback_toggle returns XMMS2::Result {
+ return ???
+ ?? XMMS2::Result.new: result => self.playback_pause;
+ !! XMMS2::Result.new: result => self.playback_start;
+}
+
+method playback_current_id returns XMMS2::Result {
+ return XMMS2::Result.new: result => xmmsc_playback_current_id($!connection);
+}
+
submethod BUILD(Str $client-name, Str $path) {
# FIXME: xmmsc_init can return NULL, on out-of-memory.
# That might sound stupid but it's still rude to ignore errors.
View
8 lib/XMMS2/Result.pm6
@@ -16,13 +16,17 @@ sub xmmsc_result_get_value(xmmsc_result_t)
sub xmmsc_result_unref(xmmsc_result_t)
is native('libxmmsclient') { ... }
-# Wrapper around a result object
+# Wrapper around a result object, which in turn is just a wrapper around a value...
class XMMS2::Result;
has xmmsc_result_t $!result;
# Returns false if this result contains an error status
method Bool {
- return ?self.get_value;
+ return self.get_value.Bool;
+}
+
+method Int {
+ return self.get_value.Int;
}
# Get result value
View
19 lib/XMMS2/Value.pm6
@@ -13,6 +13,10 @@ sub xmmsv_get_error(xmmsv_t, Str $error is rw)
returns Int
is native('libxmmsclient') { ... }
+sub xmmsv_get_int(xmmsv_t, Int $value is rw)
+ returns Int
+ is native('libxmmsclient') { ... }
+
# Wrapper around an XMMS2 value struct
class XMMS2::Value;
has xmmsv_t $!value;
@@ -22,10 +26,19 @@ method Bool {
return !xmmsv_is_error($!value);
}
+method Int {
+ if xmmsv_get_int($!value, my Int $i) {
+ return $i;
+ }
+
+ return Int;
+}
+
# Get the error string from a value
method error_string returns Str {
- my Str $error;
- xmmsv_get_error($!value, $error) if !self;
+ if !self and xmmsv_get_error($!value, my Str $error) {
+ return $error;
+ }
- return $error;
+ return Str;
}
View
35 t/actions.t
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl6
+use v6;
+use Test;
+use XMMS2::Client;
+
+plan 6;
+
+unless %*ENV<XMMS_PATH> {
+ warn 'Set XMMS_PATH if you really want to run this, it\'ll mess with your playback';
+ skip_rest("\%*ENV<XMMS_PATH> required for this test");
+ exit 1;
+}
+
+my $xmms2;
+
+unless lives_ok { $xmms2 = XMMS2::Client.new(:client-name<test>) }, 'Connect' {
+ skip_rest("Couldn't connect");
+ exit 1;
+}
+
+ok $xmms2.stop, 'Stopping playback';
+say 'Will now play for 2s, pause 2s, play 2s then stop';
+
+ok $xmms2.play, 'Play';
+sleep 2;
+
+ok $xmms2.pause, 'Pause';
+sleep 2;
+
+ok $xmms2.play, 'Resuming playback';
+sleep 2;
+
+ok $xmms2.stop, 'Stop';
+
+# vim: set ft=perl6 :
Please sign in to comment.
Something went wrong with that request. Please try again.