Include mod_xrandr instead of using submodules
[notion/jeffpc.git] / contrib / scripts / mpd.lua
blob8a89f0b5eb16cdc2c64e3bc143aa8108448241b8
1 -- Authors: Steve Jothen <sjothen@gmail.com>
2 -- License: Unknown
3 -- Last Changed: Unknown
4 --
5 -- Small interface to MusicPD
6 --
7 -- Author: Steve Jothen <sjothen at gmail dot com>
8 --
9 -- Requires netcat
11 -- Change your path/settings according to your setup
12 --
13 -- defbindings("WScreen", {
14 -- kpress("KP_6", "MusicPD.next()"), -- next song
15 -- kpress("KP_4", "MusicPD.previous()"), -- previous song
16 -- kpress("KP_2", "MusicPD.volume_down()"), -- volume down settings.volume_delta units
17 -- kpress("KP_8", "MusicPD.volume_up()"), -- volume up ....
18 -- kpress("KP_5", "MusicPD.toggle_play()") -- toggle play/pause/stop
19 -- })
21 local netcat = "/bin/netcat"
23 local settings = {
24 hostname = "localhost",
25 password = nil,
26 port = 6600,
27 volume_delta = 10
30 MusicPD = {}
32 function MusicPD.file_exists()
33 if io.open(netcat, "r") then -- check to see if file exists
34 return true
35 else
36 return false
37 end
38 end
40 -- creates the appropriate string to call with io.popen
42 function MusicPD.create_command(command)
43 local arguments = nil
44 if settings.password then
45 arguments = string.format(
46 "echo -n \"password %s\n%s\nclose\n\" | %s %s %d",
47 settings.password, command, netcat, settings.hostname, settings.port)
49 else
50 arguments = string.format(
51 "echo -n \"%s\nclose\n\" | %s %s %d",
52 command, netcat, settings.hostname, settings.port)
53 end
54 return arguments
55 end
57 -- calls the command and returns table of key, value pairs
58 --
59 function MusicPD.call_command(command)
60 local arg_cmd = MusicPD.create_command(command)
61 local values = {}
62 if MusicPD.file_exists() then
63 local handle = io.popen(arg_cmd, "r")
64 local line = handle:read("*l")
65 while line do
66 local _, _, key, value = string.find(line, "(.+):%s(.+)")
67 if key then
68 values[string.lower(key)] = value
69 end
70 line = handle:read("*l")
71 end
72 handle:close()
73 end
74 return values
75 end
77 function MusicPD.next()
78 MusicPD.call_command("next")
79 end
81 function MusicPD.previous()
82 MusicPD.call_command("previous")
83 end
85 function MusicPD.pause()
86 MusicPD.call_command("pause")
87 end
89 function MusicPD.stop()
90 MusicPD.call_command("stop")
91 end
93 function MusicPD.volume_up()
94 local stats = MusicPD.call_command("status")
95 local cur_volume = tonumber(stats.volume)
96 local new_volume
97 if cur_volume == 100 then
98 return nil
99 elseif cur_volume + settings.volume_delta > 100 then
100 new_volume = string.format("setvol %d", 100)
101 else
102 new_volume = string.format("setvol %d", settings.volume_delta + cur_volume)
104 MusicPD.call_command(new_volume)
107 function MusicPD.volume_down()
108 local stats = MusicPD.call_command("status")
109 local cur_volume = tonumber(stats.volume)
110 local new_volume
111 if cur_volume == 0 then
112 return nil
113 elseif cur_volume - settings.volume_delta < 0 then
114 new_volume = string.format("setvol %d", 0)
115 else
116 new_volume = string.format("setvol %d", cur_volume - settings.volume_delta)
118 MusicPD.call_command(new_volume)
121 function MusicPD.toggle_random()
122 local stats = MusicPD.call_command("status")
123 local random = tonumber(stats.random)
124 if random == 0 then
125 MusicPD.call_command("random 1")
126 elseif random == 1 then
127 MusicPD.call_command("random 0")
131 function MusicPD.toggle_repeat()
132 local stats = MusicPD.call_command("status")
133 local rpt = tonumber(stats["repeat"])
134 if rpt == 0 then
135 MusicPD.call_command("repeat 1")
136 else
137 MusicPD.call_command("repeat 0")
141 function MusicPD.toggle_play()
142 local stats = MusicPD.call_command("status")
143 if stats.state == "play" then
144 MusicPD.call_command("pause")
145 elseif stats.state == "pause" then
146 MusicPD.call_command("pause")
147 elseif stats.state == "stop" then
148 MusicPD.call_command("play")
152 defbindings("WScreen", {
153 kpress("KP_6", "MusicPD.next()"),
154 kpress("KP_4", "MusicPD.previous()"),
155 kpress("KP_2", "MusicPD.volume_down()"),
156 kpress("KP_8", "MusicPD.volume_up()"),
157 kpress("KP_5", "MusicPD.toggle_play()"),
158 kpress("KP_7", "MusicPD.toggle_repeat()"),
159 kpress("KP_9", "MusicPD.toggle_random()")