1 # Copyright (C) 2008 Dag Odenhall <dag.odenhall@gmail.com>
2 # Licensed under the Academic Free License version 3.0
4 require 'amazing/widget'
5 require 'amazing/proc_file'
10 description "MPD Information"
11 dependency "socket", "Ruby standard library"
12 option :hostname, "MPD server hostname", "localhost"
13 option :port, "MPD server port number", 6600
14 option :password, "Authentication password"
15 field :state, "Play state, :playing, :paused or :stopped"
16 field :artist, "Song artist"
17 field :title, "Song title"
18 field :length, "Song length"
19 field :position, "Song position"
20 field :date, "Song date from ID3 tag"
21 field :id, "Song ID in playlist"
22 field :genre, "Song genre"
23 field :album, "Song album"
24 field :file, "Filename of current song"
25 field :shortfile, "Filename without directory path and extension"
26 field :percentage, "Finished percentage of current song", 0.0
27 field :random, "True if playlist is on random"
28 field :repeat, "True if playlist is on repeat"
29 field :volume, "Volume of MPD mixer", 0
32 track = "#{@artist && "#@artist - "}#@title"
33 track = @shortfile if track.empty?
46 status = send_command(:status)
47 @state = {:play => :playing, :pause => :paused, :stop => :stopped}[status["state"].to_sym]
48 @random = {0 => false, 1 => true}[status["random"].to_i]
49 @repeat = {0 => false, 1 => true}[status["repeat"].to_i]
50 @volume = status["volume"].to_i
51 if song = send_command(:currentsong)
52 @artist = song["Artist"]
53 @title = song["Title"]
54 len = song["Time"].to_i
55 minutes = (len / 60) % 60
57 @length = "%d:%02d" % [minutes, seconds]
58 pos = status["time"].to_i
59 minutes = (pos / 60) % 60
61 @position = "%d:%02d" % [minutes, seconds]
64 @genre = song["Genre"]
65 @album = song["Album"]
67 @shortfile = ::File.basename(@file)
68 @shortfile = @shortfile[0..-::File.extname(@shortfile).length-1]
69 @percentage = pos == 0 ? 0.0 : pos / len.to_f * 100
78 unless @@connections["#@identifier"]
79 @@connections["#@identifier"] = TCPSocket.new(@hostname, @port)
80 mpd = @@connections["#@identifier"]
84 mpd = @@connections["#@identifier"]
87 @@connections["#@identifier"] = TCPSocket.new(@hostname, @port)
88 mpd = @@connections["#@identifier"]
97 mpd = @@connections["#@identifier"]
99 mpd.puts("password #@password")
100 if mpd.gets.split[0] == "ACK"
101 raise WidgetError, "incorrect password"
106 def send_command(command)
107 mpd = @@connections["#@identifier"]
108 mpd.puts(command.to_s)
112 if line.split[0] == "ACK"
113 raise WidgetError, line.scan(/\{#{command}\} (.+)/).to_s
114 elsif line.split[0] == "OK"
115 return ProcFile.new(lines.join)[0]