1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Client for a MPD server
22 # Connection to a MPD server
24 var socket
: nullable Socket = null
28 var password
: nullable String
30 var error
: nullable String = null
32 # Connect to the MPD server
35 var p
: nullable Socket = null
37 p
= new Socket.stream_with_host
(host
, port
)
43 assert not rep
.is_empty
44 if not rep
.has_prefix
("OK") then
45 print
"MPD responded {rep}"
51 var password
= password
52 if password
!= null then
53 write
("password {password}\n")
57 # Write a command to the MPD server
58 protected fun write
(msg
: String)
60 if socket
== null then connect
65 assert rep
.has_prefix
("OK") else print
"MPD responded {rep}"
68 # Get MPD server status
69 fun status
: nullable ServerStatus
71 if socket
== null then connect
73 var volume
: nullable Int = null
74 var state
: nullable String = null
77 socket
.write
("status\n")
79 for l
in rep
.split_with
("\n") do
80 var words
= l
.split_with
(" ")
81 if words
.length
> 1 then
82 var key
= words
[0].to_lower
83 var first_space
= l
.index_of
(' ')
84 var rest
= l
.substring_from
(first_space
+1)
85 if key
== "volume:" then
87 if volume
== -1 then volume
= null
88 else if key
== "volume:" then
95 return new ServerStatus(volume
, state
)
101 # Set the volume relatively
102 fun relative_volume
=(diff
: Int)
104 if socket
== null then connect
107 if status
!= null then
108 var vol
= status
.volume
110 var new_vol
= vol
+ diff
111 new_vol
= new_vol
.max
(0).min
(100)
117 error
= "Cannot get volume"
120 fun volume
=(vol
: Int) do write
("setvol {vol}\n")
122 # Pause music playing on the MPD server
123 fun pause
do write
("pause\n")
125 # Stop music playing on the MPD server
126 fun stop
do write
("stop\n")
128 # Play music playing on the MPD server
129 fun play
do write
("play\n")
131 # Get information on the currently playing song on the MPD server
132 fun current_song
: nullable SongInfo
134 if socket
== null then connect
136 var album
: nullable String = null
137 var artist
: nullable String = null
138 var title
: nullable String = null
140 socket
.write
("currentsong\n")
141 var rep
= socket
.read
142 for l
in rep
.split_with
("\n") do
143 var words
= l
.split_with
(" ")
144 if words
.length
> 1 then
145 var key
= words
[0].to_lower
146 var first_space
= l
.index_of
(' ')
147 var rest
= l
.substring_from
(first_space
+1)
148 if key
== "album:" then
150 else if key
== "artist:" then
152 else if key
== "title:" then
158 if album
!= null and artist
!= null and title
!= null then
159 return new SongInfo(album
, artist
, title
)
175 var volume
: nullable Int