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.
master
Dustin 2014-08-30 21:59:26 -05:00
parent c493d67a93
commit 5b3555fb92
1 changed files with 55 additions and 2 deletions

View File

@ -1,14 +1,67 @@
namespace MpdNotify { 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(); var loop = new MainLoop();
Unix.signal_add(2, () => { Unix.signal_add(2, () => {
loop.quit(); loop.quit();
return false; 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(); notifier.start();
loop.run(); loop.run();
return 0;
} }
} }