documentation
[wmiirc-lua.git] / plugins / mpd.lua
blobd18a1f1733f67ba7cc0a3733e01634eaef80e74b
1 --[[
2 =pod
4 =head1 NAME
6 mpd.lua - wmiirc-lua plugin for monitoring and controlling mpd (music player daemon)
8 =head1 SYNOPSIS
10 -- in your wmiirc.lua:
11 mpd = wmii.load_plugin("mpd")
14 =head1 DESCRIPTION
16 For binding the mpd controls to the multimedia keys you could add
17 the following keyhandlers to your wmiirc.lua:
19 wmii.add_key_handler('XF86AudioNext', mpd.next_song())
21 wmii.add_key_handler('XF86AudioPrev', mpd.prev_song())
23 wmii.add_key_handler('XF86AudioPlay', mpd.toggle_pause())
25 wmii.add_key_handler('XF86AudioStop', mpd.stop())
27 =head1 SEE ALSO
29 L<wmii(1)>, L<lua(1)>
31 =head1 AUTHOR
33 Jan-David Quesel <jdq@gmx.net>
35 =head1 LICENCE AND COPYRIGHT
37 Copyright (c) 2008, Jan-David Quesel <jdq@gmx.net>
39 This is free software. You may redistribute copies of it under the terms of
40 the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>. There
41 is NO WARRANTY, to the extent permitted by law.
43 =cut
44 --]]
45 local wmii = require("wmii")
46 local io = require("io")
47 local string = require("string")
49 module("mpd")
50 api_version=0.1
52 -- ------------------------------------------------------------
53 -- Configuration Settings
54 wmii.set_conf ("mpd.server", "127.0.0.1")
55 wmii.set_conf ("mpd.port", "6600")
57 -- ------------------------------------------------------------
58 -- Init Plugin
59 local widget = wmii.widget:new ("301_mpd_status")
61 local function _command ( cmd )
63 if (cmd) then
64 wmii.log( "about to run " .. cmd)
65 local file = io.popen( cmd)
66 local status = file:read("*a")
67 file:close()
69 return status:match("[^\n]*")
70 else
71 return ""
72 end
73 end
75 local function update_mpd_status (time_since_update)
76 local printout = _command("export MPD_HOST=" .. wmii.get_conf("mpd.server") .. "&& export MPD_PORT=" .. wmii.get_conf("mpd.port") .. " && mpc")
78 widget:show(printout)
79 return 5
80 end
82 function next_song()
83 _command("export MPD_HOST=" .. wmii.get_conf("mpd.server") .. "&& export MPD_PORT=" .. wmii.get_conf("mpd.port") .. " && mpc next")
84 update_mpd_status(0)
85 end
87 function prev_song()
88 _command("export MPD_HOST=" .. wmii.get_conf("mpd.server") .. "&& export MPD_PORT=" .. wmii.get_conf("mpd.port") .. " && mpc prev")
89 update_mpd_status(0)
90 end
92 function toggle_pause()
93 _command("export MPD_HOST=" .. wmii.get_conf("mpd.server") .. "&& export MPD_PORT=" .. wmii.get_conf("mpd.port") .. " && mpc toggle")
94 update_mpd_status(0)
95 end
97 function stop()
98 _command("export MPD_HOST=" .. wmii.get_conf("mpd.server") .. "&& export MPD_PORT=" .. wmii.get_conf("mpd.port") .. " && mpc stop")
99 update_mpd_status(0)
102 function register_action()
103 wmii.add_action_handler ("mpd",
104 function(act,args)
105 local actions = { 'play', 'pause', 'stop', 'next', 'prev' }
106 local act = wmii.menu(actions, "mpd: ")
107 local fn = function_handlers[act]
108 if fn then
109 local r, err = pcall (fn)
110 end
111 end)
114 local timer = wmii.timer:new (update_mpd_status, 1)