anyplayer 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,65 +1,63 @@
1
- module Anyplayer
2
- class Mpd < Player
3
- def playpause
4
- mpc 'toggle'
5
- end
1
+ class Anyplayer::Mpd < Anyplayer::Player
2
+ def playpause
3
+ mpc 'toggle'
4
+ end
6
5
 
7
- def play
8
- mpc 'play'
9
- end
6
+ def play
7
+ mpc 'play'
8
+ end
10
9
 
11
- def pause
12
- mpc 'pause'
13
- end
10
+ def pause
11
+ mpc 'pause'
12
+ end
14
13
 
15
- def prev
16
- mpc 'prev'
17
- super
18
- end
14
+ def prev
15
+ mpc 'prev'
16
+ super
17
+ end
19
18
 
20
- def next
21
- mpc 'next'
22
- super
23
- end
19
+ def next
20
+ mpc 'next'
21
+ super
22
+ end
24
23
 
25
- def voldown
26
- mpc 'volume -10'
27
- end
24
+ def voldown
25
+ mpc 'volume -10'
26
+ end
28
27
 
29
- def volup
30
- mpc 'volume +10'
31
- end
28
+ def volup
29
+ mpc 'volume +10'
30
+ end
32
31
 
33
- def volume
34
- mpc('volume').grep(/([0-9]+)/)
35
- $1
36
- end
32
+ def volume
33
+ mpc('volume').grep(/([0-9]+)/)
34
+ $1
35
+ end
37
36
 
38
- def track
39
- mpc '-f "%title%" current'
40
- end
37
+ def track
38
+ mpc '-f "%title%" current'
39
+ end
41
40
 
42
- def artist
43
- mpc '-f "%artist%" current'
44
- end
41
+ def artist
42
+ mpc '-f "%artist%" current'
43
+ end
45
44
 
46
- def album
47
- mpc '-f "%album%" current'
48
- end
45
+ def album
46
+ mpc '-f "%album%" current'
47
+ end
49
48
 
50
- def launched?
51
- %x(mpc 2> /dev/null)
52
- $? == 0
53
- end
49
+ def launched?
50
+ %x(mpc 2> /dev/null)
51
+ $? == 0
52
+ end
54
53
 
55
- def host
56
- ENV['MPD_HOST'] || super
57
- end
54
+ def host
55
+ ENV['MPD_HOST'] || super
56
+ end
58
57
 
59
- private
58
+ private
60
59
  def mpc(command)
61
60
  %x(mpc #{command}).split("\n").first
62
61
  end
63
- end
64
62
  end
65
63
 
@@ -0,0 +1,45 @@
1
+ class Anyplayer::Noplayer < Anyplayer::Player
2
+ def playpause
3
+ end
4
+
5
+ def play
6
+ end
7
+
8
+ def pause
9
+ end
10
+
11
+ def prev
12
+ super
13
+ end
14
+
15
+ def next
16
+ super
17
+ end
18
+
19
+ def voldown
20
+ end
21
+
22
+ def volup
23
+ end
24
+
25
+ def volume
26
+ 100
27
+ end
28
+
29
+ def track
30
+ "Money"
31
+ end
32
+
33
+ def artist
34
+ "The Drums"
35
+ end
36
+
37
+ def album
38
+ "Money"
39
+ end
40
+
41
+ def launched?
42
+ true
43
+ end
44
+ end
45
+
@@ -1,59 +1,57 @@
1
- module Anyplayer
2
- class Rhythmbox < Player
3
- def playpause
4
- tell_to 'play-pause'
5
- end
1
+ class Anyplayer::Rhythmbox < Anyplayer::Player
2
+ def playpause
3
+ rhythmbox 'play-pause'
4
+ end
6
5
 
7
- def play
8
- tell_to 'play'
9
- end
6
+ def play
7
+ rhythmbox 'play'
8
+ end
10
9
 
11
- def pause
12
- tell_to 'pause'
13
- end
10
+ def pause
11
+ rhythmbox 'pause'
12
+ end
14
13
 
15
- def prev
16
- tell_to 'previous'
17
- super
18
- end
14
+ def prev
15
+ rhythmbox 'previous'
16
+ super
17
+ end
19
18
 
20
- def next
21
- tell_to 'next'
22
- super
23
- end
19
+ def next
20
+ rhythmbox 'next'
21
+ super
22
+ end
24
23
 
25
- def voldown
26
- tell_to 'volume-down'
27
- end
24
+ def voldown
25
+ rhythmbox 'volume-down'
26
+ end
28
27
 
29
- def volup
30
- tell_to 'volume-up'
31
- end
28
+ def volup
29
+ rhythmbox 'volume-up'
30
+ end
32
31
 
33
- def volume
34
- tell_to 'print-volume'
35
- end
32
+ def volume
33
+ rhythmbox 'print-volume'
34
+ end
36
35
 
37
- def track
38
- tell_to 'print-playing-format=%tt'
39
- end
36
+ def track
37
+ rhythmbox 'print-playing-format=%tt'
38
+ end
40
39
 
41
- def artist
42
- tell_to 'print-playing-format=%ta'
43
- end
40
+ def artist
41
+ rhythmbox 'print-playing-format=%ta'
42
+ end
44
43
 
45
- def album
46
- tell_to 'print-playing-format=%at'
47
- end
44
+ def album
45
+ rhythmbox 'print-playing-format=%at'
46
+ end
48
47
 
49
- def launched?
50
- %x(rhythmbox-client --no-start --print-playing 2>/dev/null).rstrip != ""
51
- end
48
+ def launched?
49
+ %x(rhythmbox-client --no-start --print-playing 2>/dev/null).rstrip != ""
50
+ end
52
51
 
53
- private
54
- def tell_to(command)
52
+ private
53
+ def rhythmbox(command)
55
54
  %x(rhythmbox-client --no-start --#{command}).rstrip
56
55
  end
57
- end
58
56
  end
59
57
 
@@ -1,79 +1,77 @@
1
- module Anyplayer
2
- class Xmms2 < Player
3
- def playpause
4
- xmms2 'toggle'
5
- end
1
+ class Anyplayer::Xmms2 < Anyplayer::Player
2
+ def playpause
3
+ xmms2 'toggle'
4
+ end
6
5
 
7
- def play
8
- xmms2 'play'
9
- end
6
+ def play
7
+ xmms2 'play'
8
+ end
10
9
 
11
- def pause
12
- xmms2 'pause'
13
- end
10
+ def pause
11
+ xmms2 'pause'
12
+ end
14
13
 
15
- def prev
16
- xmms2 'prev'
17
- super
18
- end
14
+ def prev
15
+ xmms2 'prev'
16
+ super
17
+ end
19
18
 
20
- def next
21
- xmms2 'next'
22
- super
23
- end
19
+ def next
20
+ xmms2 'next'
21
+ super
22
+ end
24
23
 
25
- def voldown
26
- current = volume.to_i
27
- new_volume = (current < 11 ? 0 : current - 10)
28
- xmms2 "server volume #{new_volume}"
29
- end
24
+ def voldown
25
+ current = volume.to_i
26
+ new_volume = (current < 11 ? 0 : current - 10)
27
+ xmms2 "server volume #{new_volume}"
28
+ end
30
29
 
31
- def volup
32
- current = volume.to_i
33
- new_volume = (current > 89 ? 100 : current + 10)
34
- xmms2 "server volume #{new_volume}"
35
- end
30
+ def volup
31
+ current = volume.to_i
32
+ new_volume = (current > 89 ? 100 : current + 10)
33
+ xmms2 "server volume #{new_volume}"
34
+ end
36
35
 
37
- def volume
38
- # currently just the first (left?) channel
39
- xmms2('server volume').split("\n").first.sub(/([^0-9]*)/, '')
40
- $1
41
- end
36
+ def volume
37
+ # currently just the first (left?) channel
38
+ xmms2('server volume').split("\n").first.sub(/([^0-9]*)/, '')
39
+ $1
40
+ end
42
41
 
43
- def track
44
- xmms2 "status -f '${title}'"
45
- end
42
+ def track
43
+ xmms2 "status -f '${title}'"
44
+ end
46
45
 
47
- def artist
48
- xmms2 "status -f '${artist}'"
49
- end
46
+ def artist
47
+ xmms2 "status -f '${artist}'"
48
+ end
50
49
 
51
- def album
52
- xmms2 "status -f '${album}'"
53
- end
50
+ def album
51
+ xmms2 "status -f '${album}'"
52
+ end
54
53
 
55
- def playing?
56
- xmms2("status -f '${playback_status}'") == "Playing"
57
- end
54
+ def playing?
55
+ xmms2("status -f '${playback_status}'") == "Playing"
56
+ end
58
57
 
59
- def paused?
60
- xmms2("status -f '${playback_status}'") == "Paused"
61
- end
58
+ def paused?
59
+ xmms2("status -f '${playback_status}'") == "Paused"
60
+ end
62
61
 
63
- def launched?
64
- # xmms2 autolaunches the daemon, so this should always be true
65
- %x(xmms2 status 2> /dev/null)
66
- $? == 0
67
- end
62
+ def launched?
63
+ # xmms2 autolaunches the daemon, so this should always be true
64
+ %x(xmms2 status 2> /dev/null)
65
+ $? == 0
66
+ end
68
67
 
69
- def host
70
- ENV['XMMS_PATH'] || super
71
- end
68
+ def host
69
+ ENV['XMMS_PATH'] || super
70
+ end
72
71
 
73
- private
72
+ private
74
73
  def xmms2(command)
75
74
  %x(xmms2 #{command}).split("\n").first.strip
76
75
  end
77
- end
78
-
79
76
  end
77
+
@@ -0,0 +1,66 @@
1
+ # The Selector is the tool that will find you the currently running player on your platform
2
+ #
3
+ # Example:
4
+ #
5
+ # selector = Anyplayer::Selector.new
6
+ # player = selector.player
7
+ #
8
+ # Needs the PLAYERS constant to contain a list of players.
9
+ class Anyplayer::Selector
10
+ attr_accessor :verbose
11
+ attr_reader :errors
12
+
13
+ def initialize
14
+ @verbose = false
15
+ @errors = []
16
+ end
17
+
18
+ # Returns an instance of the first music player that's launched
19
+ def player
20
+ players_for_this_platform.each { |player|
21
+ player_load(player) or next
22
+ instance = player_class(player).new
23
+ return instance if player_launched(instance)
24
+ }
25
+ nil
26
+ end
27
+
28
+
29
+ private
30
+
31
+ def players_for_this_platform
32
+ players = Anyplayer::PLAYERS
33
+ players.reject! { |player| player =~ /_mac$/ } if RUBY_PLATFORM !~ /darwin/
34
+ players.reject! { |player| player =~ /_windows$/ } if RUBY_PLATFORM !~ /cygwin|mswin|mingw|bccwin|wince|emx/
35
+ players
36
+ end
37
+
38
+ def player_load(player)
39
+ begin
40
+ require "anyplayer/players/#{player}"
41
+ $stderr.puts "Loaded #{player}" if verbose
42
+ true
43
+ rescue LoadError => e
44
+ $stderr.puts "Could not load #{player}" if verbose
45
+ @errors << "Error loading #{player}: #{e.message}"
46
+ false
47
+ end
48
+ end
49
+
50
+ def player_class(player)
51
+ camelized = player.to_s.split(/_/).map{ |word| word.capitalize }.join
52
+ Anyplayer::const_get(camelized)
53
+ end
54
+
55
+ def player_launched(player)
56
+ $stderr.puts "#{player.name} launched?" if verbose
57
+
58
+ seconds = 5
59
+ begin
60
+ Timeout::timeout(seconds) { player.launched? }
61
+ rescue Timeout::Error
62
+ $stderr.puts "Timed out after #{seconds} seconds" if verbose
63
+ false
64
+ end
65
+ end
66
+ end