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_and_check
("password {password}\n")
57 # Write a command to the MPD server
58 protected fun write_and_check
(msg
: String)
60 if socket
== null then connect
65 if not rep
.has_prefix
("OK") then
66 print
"Error: MPD responded {rep}"
72 # Get MPD server status
73 fun status
: nullable ServerStatus
75 if socket
== null then connect
77 var volume
: nullable Int = null
78 var state
: nullable String = null
81 socket
.write
("status\n")
83 for l
in rep
.split_with
("\n") do
84 var words
= l
.split_with
(" ")
85 if words
.length
> 1 then
86 var key
= words
[0].to_lower
87 var first_space
= l
.index_of
(' ')
88 var rest
= l
.substring_from
(first_space
+1)
89 if key
== "volume:" then
91 if volume
== -1 then volume
= null
92 else if key
== "volume:" then
99 return new ServerStatus(volume
, state
)
105 # Set the volume relatively
106 fun relative_volume
=(diff
: Int)
108 if socket
== null then connect
111 if status
!= null then
112 var vol
= status
.volume
114 var new_vol
= vol
+ diff
115 new_vol
= new_vol
.max
(0).min
(100)
121 error
= "Cannot get volume"
124 fun volume
=(vol
: Int) do write_and_check
("setvol {vol}\n")
126 # Pause music playing on the MPD server
127 fun pause
do write_and_check
("pause\n")
129 # Stop music playing on the MPD server
130 fun stop
do write_and_check
("stop\n")
132 # Play music playing on the MPD server
133 fun play
do write_and_check
("play\n")
135 # Get information on the currently playing song on the MPD server
136 fun current_song
: nullable SongInfo
138 if socket
== null then connect
140 var album
: nullable String = null
141 var artist
: nullable String = null
142 var title
: nullable String = null
144 socket
.write
("currentsong\n")
145 var rep
= socket
.read
146 for l
in rep
.split_with
("\n") do
147 var words
= l
.split_with
(" ")
148 if words
.length
> 1 then
149 var key
= words
[0].to_lower
150 var first_space
= l
.index_of
(' ')
151 var rest
= l
.substring_from
(first_space
+1)
152 if key
== "album:" then
154 else if key
== "artist:" then
156 else if key
== "title:" then
162 if album
!= null and artist
!= null and title
!= null then
163 return new SongInfo(album
, artist
, title
)
179 var volume
: nullable Int