ni_nitdoc: Rewritting of Github plugin for nitdoc
[nit.git] / lib / mpd.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Client for a MPD server
18 module mpd
19
20 import socket
21
22 # Connection to a MPD server
23 class MPDConnection
24 var socket: nullable Socket = null
25
26 var host: String
27 var port: Int
28 var password: nullable String
29
30 var error: nullable String = null
31
32 # Connect to the MPD server
33 fun connect
34 do
35 var p: nullable Socket = null
36
37 p = new Socket.stream_with_host(host, port)
38 p.connect
39
40 sys.nanosleep(0,5000)
41
42 var rep = p.read
43 assert not rep.is_empty
44 if not rep.has_prefix("OK") then
45 print "MPD responded {rep}"
46 abort
47 end
48
49 socket = p
50
51 var password = password
52 if password != null then
53 write("password {password}\n")
54 end
55 end
56
57 # Write a command to the MPD server
58 protected fun write(msg: String)
59 do
60 if socket == null then connect
61
62 socket.write(msg)
63 sys.nanosleep(0,5000)
64 var rep = socket.read
65 assert rep.has_prefix("OK") else print "MPD responded {rep}"
66 end
67
68 # Get MPD server status
69 fun status: nullable ServerStatus
70 do
71 if socket == null then connect
72
73 var volume: nullable Int = null
74 var state: nullable String = null
75
76 # get current status
77 socket.write("status\n")
78 var rep = socket.read
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
86 volume = rest.to_i
87 if volume == -1 then volume = null
88 else if key == "volume:" then
89 state = rest
90 end
91 end
92 end
93
94 if state != null then
95 return new ServerStatus(volume, state)
96 else
97 return null
98 end
99 end
100
101 # Set the volume relatively
102 fun relative_volume=(diff: Int)
103 do
104 if socket == null then connect
105
106 var status = status
107 if status != null then
108 var vol = status.volume
109 if vol != null then
110 var new_vol = vol + diff
111 new_vol = new_vol.max(0).min(100)
112 volume = new_vol
113 return
114 end
115 end
116
117 error = "Cannot get volume"
118 end
119
120 fun volume=(vol: Int) do write("setvol {vol}\n")
121
122 # Pause music playing on the MPD server
123 fun pause do write("pause\n")
124
125 # Stop music playing on the MPD server
126 fun stop do write("stop\n")
127
128 # Play music playing on the MPD server
129 fun play do write("play\n")
130
131 # Get information on the currently playing song on the MPD server
132 fun current_song: nullable SongInfo
133 do
134 if socket == null then connect
135
136 var album: nullable String = null
137 var artist: nullable String = null
138 var title: nullable String = null
139
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
149 album = rest
150 else if key == "artist:" then
151 artist = rest
152 else if key == "title:" then
153 title = rest
154 end
155 end
156 end
157
158 if album != null and artist != null and title != null then
159 return new SongInfo(album, artist, title)
160 else
161 return null
162 end
163 end
164 end
165
166 # MPD song info
167 class SongInfo
168 var album: String
169 var artist: String
170 var title: String
171 end
172
173 # MPD server status
174 class ServerStatus
175 var volume: nullable Int
176 var state: String
177 end