-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpdCommande.lua
185 lines (184 loc) · 4.43 KB
/
mpdCommande.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
-------------------------------------------------
-- author: David Cobac
-- twitter: @david_cobac
-- github: https://github.com/cobacdavid
-- date: 2020
-- copyright: CC-BY-NC-SA
-------------------------------------------------
-------------------------------------------------
-- some parts from awesome wm
-- ditribution
-- copyright ??
-------------------------------------------------
local socket = require("socket")
local naughty = require("naughty")
local os = require("os")
--
local myhome = os.getenv("HOME") .. "/"
--
local function strip(chaine)
while (chaine:sub(1, 1) == " ") do
chaine = chaine:sub(2)
end
while (chaine:sub(-1, -1) == " ") do
chaine = chaine:sub(1, -2)
end
return chaine
end
--
local function notifie(t)
naughty.notify({
title = "♬ mpd",
text = tostring(t)
})
end
--
mpd = {}
mpd.cartist = nil
mpd.ctrack = nil
mpd.statusList = nil
mpd._socket = nil
mpd.socketNotifies = false
--
function mpd.run()
-- mpd est lancé via play
-- pas de démarrage auto avec apponlogin...
commande = "/usr/bin/mpd " .. myhome .. ".config/mpd/mpd.conf"
awful.spawn.easy_async_with_shell(commande,
function(stdout, stderr, reason, exit_code)
notifie("lancement")
end)
end
--
function mpd.connect()
local host = "localhost"
local port = 6600
mpd._socket = socket.connect(host, port)
if not mpd._socket then
mpd.run()
mpd._socket = socket.connect(host, port)
end
local r = mpd._socket:receive()
-- ajouter un test de bon déroulement !!
return mpd._socket
end
--
function mpd.close()
mpd._socket:close()
end
--
function mpd.com(ordre)
mpd.connect()
local t = {"play", "stop", "pause", "next", "previous"}
mpd._socket:send(ordre .. "\n")
local reponse = mpd._socket:receive()
if reponse == "OK" and mpd.socketNotifies then
notifie("Serveur MPD : OK")
elseif reponse:sub(1, 3) == "ACK" and mpd.socketNotifies then
notifie("Erreur serveur MPD : " .. reponse)
elseif reponse ~= "OK" and reponse:sub(1, 3) ~= "ACK" then
local fin = false
while (not fin) and t<1000 do
local r = mpd._socket:receive()
notifie(r)
reponse = reponse .. "\n" .. r
fin = r == "OK"
end
if mpd.socketNotifies then
notifie("Serveur MPD : " .. reponse)
end
t = t + 1
end
mpd.close()
end
--
function mpd.status()
mpd.connect()
local tableauReponse = {}
mpd._socket:send("status\n")
local fin = false
local r = mpd._socket:receive()
while (not fin) do
local indiceSep = r:find(":")
local cle = r:sub(1, indiceSep - 1)
local valeur = r:sub(indiceSep + 1)
tableauReponse[cle] = strip(valeur)
r = mpd._socket:receive()
fin = r == "OK"
end
mpd.close()
return tableauReponse
end
--
function mpd.currentsong()
mpd.connect()
local tableauReponse = {}
mpd._socket:send("currentsong\n")
local fin = false
local r = mpd._socket:receive()
while (not fin) do
local indiceSep = r:find(":")
local cle = r:sub(1, indiceSep - 1)
local valeur = r:sub(indiceSep + 1)
tableauReponse[cle] = strip(valeur)
r = mpd._socket:receive()
fin = r == "OK"
end
mpd.close()
return tableauReponse
end
--
function mpd.artistAndTrack()
local t = mpd.currentsong()
mpd.cartist = t['Artist']
mpd.ctrack = t['Title']
end
--
function mpd.previous()
mpd.statusListe = mpd.status()
if mpd.statusListe['state'] == "play" then
mpd.com("previous")
mpd.artistAndTrack()
notifie(mpd.cartist .. "\n" .. mpd.ctrack)
end
end
--
function mpd.next()
mpd.statusListe = mpd.status()
if mpd.statusListe['state'] == "play" then
mpd.com("next")
mpd.artistAndTrack()
notifie(mpd.cartist .. "\n" .. mpd.ctrack)
end
end
--
function mpd.runorplayorpause()
mpd.statusListe = mpd.status()
local etat = nil
if mpd.statusListe['state'] == "stop" then
mpd.com("play")
etat = "play"
else
if mpd.statusListe['state'] == "pause" then
etat = "play"
else
etat = "pause"
end
mpd.com("pause")
end
--
if mpd.socketNotifies then
notifie("Serveur MPD : " .. etat)
end
--
if etat == "play" then
mpd.artistAndTrack()
notifie(mpd.cartist .. "\n" .. mpd.ctrack)
end
end
--
function mpd.stopmusic()
mpd.com("stop")
end
--
return mpd