Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mpd: Allow mpd connection via UNIX socket #349

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
38 changes: 38 additions & 0 deletions helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ local io = { lines = io.lines,
local rawget = rawget
local table = { sort = table.sort }

local gio = require("lgi").Gio
Copy link
Owner

@lcpz lcpz Jul 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, rename the variable to Gio, for consistency.


-- Lain helper functions for internal use
-- lain.helpers
local helpers = {}
Expand Down Expand Up @@ -134,6 +136,42 @@ end

-- }}}


-- {{{ Network functions

-- Send some data to a UNIX socket
function helpers.send_to_unix_socket(unix_path, data, buffer_length)

-- First, create an output buffer to recieve from
local recv_buffer = ""
-- We'll need to allocate `buffer_length` bytes to it
for i=1,buffer_length do
recv_buffer = recv_bufer .. " "
end

-- Create a socket to send some data from
local sock = gio.Socket.new(gio.SocketFamily.UNIX,
gio.SocketType.STREAM,
gio.SocketProtocol.DEFAULT)

-- Create a socket address to connect to
local addr = gio.UnixSocketAddress.new(path)

-- Connect across
sock:connect(addr)

-- Send our message
sock:send(data)

-- Pull back anything it sends back
sock:receive(recv_buffer)

return recv_buffer
end


-- }}}

-- {{{ Misc

-- check if an element exist on a table
Expand Down
20 changes: 16 additions & 4 deletions widget/mpd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ local wibox = require("wibox")
local os = { getenv = os.getenv }
local string = { format = string.format,
gmatch = string.gmatch,
match = string.match }
match = string.match,
sub = string.sub }

-- MPD infos
-- lain.widget.mpd
Expand All @@ -36,10 +37,21 @@ local function factory(args)
local followtag = args.followtag or false
local settings = args.settings or function() end

local mpdh = string.format("telnet://%s:%s", host, port)
local mpdh = ""
local cmd = ""
local echo = string.format("printf \"%sstatus\\ncurrentsong\\nclose\\n\"", password)
local cmd = string.format("%s | curl --connect-timeout 1 -fsm 3 %s", echo, mpdh)


-- If host begins with "/", we can assume that it is a socket
if string.sub(host, 1, 1) == "/" then
-- It's a socket, use socat to talk directly to it
mpdh = string.format("UNIX-CONNECT:%s", host)
cmd = string.format("%s | socat - %s", echo, mpdh)
else
-- It's not a socket, assume IP or other host
mpdh = string.format("telnet://%s:%s", host, port)
cmd = string.format("%s | curl --connect-timeout 1 -fsm 3 %s", echo, mpdh)
end

mpd_notification_preset = { title = "Now playing", timeout = 6 }

helpers.set_map("current mpd track", nil)
Expand Down