From 5b3555fb929ed652924d9c4e5ccbd13cfae1150e Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sat, 30 Aug 2014 21:59:26 -0500 Subject: [PATCH] mpdnotify: Add host/port options To support connecting to MPD instances on other hosts, or if the socket is in a different location, the host and/or port can be specified using either command-line arguments or environment variables. The behavior is similar to that of `mpc` in that the password can be specified in the host value using `password@host` syntax. The command-line arguments override the environment variables. If neither are specified, the default of `localhost:6600` is used. --- src/mpdnotify.vala | 57 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/src/mpdnotify.vala b/src/mpdnotify.vala index 58592d4..0e83859 100644 --- a/src/mpdnotify.vala +++ b/src/mpdnotify.vala @@ -1,14 +1,67 @@ namespace MpdNotify { + public static const string DEFAULT_HOST = "localhost"; + public static const int DEFAULT_PORT = 6600; + + private static string host; + private static int port; + + private const OptionEntry[] options = { + {"host", 'h', 0, OptionArg.STRING, ref host, + "MPD server hostname/address", "HOST" }, + {"port", 'p', 0, OptionArg.INT, ref port, + "MPD server port", "PORT" }, + { null } + }; + + private static void parse_args(string[] args) throws OptionError { + host = Environment.get_variable("MPD_HOST"); + if (host == null) { + host = DEFAULT_HOST; + } + string port_s = Environment.get_variable("MPD_PORT"); + if (port_s == null) { + port = DEFAULT_PORT; + } else { + int p; + port_s.scanf("%d", out p); + port = p; + } + + var ctx = new OptionContext( + "- Send desktop notifications for MPD events"); + ctx.set_help_enabled(true); + ctx.add_main_entries(options, null); + ctx.parse(ref args); + } + + int main(string[] args) { + try { + parse_args(args); + } catch (OptionError e) { + stderr.printf("Error: %s\n", e.message); + return 2; + } - void main() { var loop = new MainLoop(); Unix.signal_add(2, () => { loop.quit(); return false; }); - var notifier = new Notifier("/run/mpd/socket", 0); + + string _host; + string _password; + var split_host = host.split("@", 2); + if (split_host.length == 2) { + _host = split_host[1]; + _password = split_host[0]; + } else { + _host = split_host[0]; + _password = null; + } + var notifier = new Notifier(_host, port, _password); notifier.start(); loop.run(); + return 0; } }