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
57 changes: 57 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

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

-- }}}


-- {{{ Network functions

-- Create a recieving buffer
-- NOTE: Not sure whether to export this with helpers or not
-- Probably just gonna leave it local for now
function generate_buffer(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_buffer .. " "
end

return recv_buffer
end
Copy link

Choose a reason for hiding this comment

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

return string.rep(" ", buffer_length + 1) (the +1 is there to match the behaviour of your code, but I don't know if your code really should do that)


-- Wrapper function to send to any sort of address
function helpers.send_to_address(host, port, data, buffer_length, callback)
-- Have a default buffer length of 1000
if not buffer_length then
buffer_length = 1000
end

-- Generate a buffer to store our result in
local recv_buffer = generate_buffer(buffer_length)

-- Check if we should be sending to a socket or an IP
local is_socket = (string.sub(host, 1, 1) == "/")

-- Create a client to listen and send with
local client = Gio.SocketClient()

local addr

if is_socket then
addr = Gio.UnixSocketAddress.new(host)
else
local inet_addr = gio.InetAddress.new_from_string(host)
addr = Gio.InetSocketAddress.new(inet_addr, port)
end

local conn = client:connect(addr)

local input_stream = conn:get_output_stream()
local output_stream = conn:get_input_stream()

input_stream:write(data)
output_stream:read(recv_buffer)
output_stream:read(recv_buffer)

callback(recv_buffer)
Copy link

Choose a reason for hiding this comment

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

Hm... this will actually "break Lua". Lua assumes that strings are immutable. So, e.g., if any other piece of code constructed the same number of blanks before, the received data will also end up in there.

Also, you should conn:close() when done.

(Note that I just looked over this in a drive-by fashion, @copycat-killer gets to decide things and I have no say in anything here)

Copy link
Owner

Choose a reason for hiding this comment

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

@psychon I appreciate any review of yours, for me you are free to come by as you like.

end
-- }}}

-- {{{ Misc

-- check if an element exist on a table
Expand Down
14 changes: 7 additions & 7 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,16 +37,15 @@ 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 echo = string.format("printf \"%sstatus\\ncurrentsong\\nclose\\n\"", password)
local cmd = string.format("%s | curl --connect-timeout 1 -fsm 3 %s", echo, mpdh)

local mpdh = ""
local cmd = ""
local echo = string.format("%sstatus\ncurrentsong\nclose\n", password)
mpd_notification_preset = { title = "Now playing", timeout = 6 }

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

function mpd.update()
helpers.async({ shell, "-c", cmd }, function(f)
helpers.send_to_address(host, port, echo, 10000, function(f)
mpd_now = {
random_mode = false,
single_mode = false,
Expand Down