fix client_focused()
[wmiirc-lua.git] / plugins / mpd.lua
blob3e096b9d3309b8466e7819627582a645df259111
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)
80 -- if we dont get a response from the server we test every 2 minutes
81 if printout == nil or printout == "" then
82 return 120
83 end
84 return 5
85 end
87 function next_song()
88 _command("export MPD_HOST=" .. wmii.get_conf("mpd.server") .. "&& export MPD_PORT=" .. wmii.get_conf("mpd.port") .. " && mpc next")
89 update_mpd_status(0)
90 end
92 function prev_song()
93 _command("export MPD_HOST=" .. wmii.get_conf("mpd.server") .. "&& export MPD_PORT=" .. wmii.get_conf("mpd.port") .. " && mpc prev")
94 update_mpd_status(0)
95 end
97 function toggle_pause()
98 _command("export MPD_HOST=" .. wmii.get_conf("mpd.server") .. "&& export MPD_PORT=" .. wmii.get_conf("mpd.port") .. " && mpc toggle")
99 update_mpd_status(0)
102 function stop()
103 _command("export MPD_HOST=" .. wmii.get_conf("mpd.server") .. "&& export MPD_PORT=" .. wmii.get_conf("mpd.port") .. " && mpc stop")
104 update_mpd_status(0)
107 function register_action()
108 wmii.add_action_handler ("mpd",
109 function(act,args)
110 local actions = { 'play', 'pause', 'stop', 'next', 'prev' }
111 local act = wmii.menu(actions, "mpd: ")
112 local fn = function_handlers[act]
113 if fn then
114 local r, err = pcall (fn)
115 end
116 end)
119 local timer = wmii.timer:new (update_mpd_status, 1)