summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 30327ee)
raw | patch | inline | side by side (parent: 30327ee)
author | Kalle Wallin <kaw@linux.se> | |
Mon, 29 Mar 2004 18:09:38 +0000 (18:09 +0000) | ||
committer | Kalle Wallin <kaw@linux.se> | |
Mon, 29 Mar 2004 18:09:38 +0000 (18:09 +0000) |
main.c | patch | blob | history | |
mpc.c | patch | blob | history | |
mpc.h | patch | blob | history | |
options.c | patch | blob | history | |
options.h | patch | blob | history |
index 92e40b6dda539f92833a144d3594bb3ec3a28a6e..8538636097e1bb8965e1c3e10c6019853b11d857 100644 (file)
--- a/main.c
+++ b/main.c
#include "screen.h"
#include "conf.h"
+/* time in seconds between mpd updates (double) */
+#define MPD_UPDATE_TIME 1.0
-#define MPD_UPDATE_TIME 1.0
+/* timout in seconds before trying to reconnect (int) */
+#define MPD_RECONNECT_TIMEOUT 3
static mpd_client_t *mpc = NULL;
mpc_close(mpc);
}
g_free(options.host);
+ g_free(options.password);
if( timer )
g_timer_destroy(timer);
}
{
options_t *options;
struct sigaction act;
- int connected;
+ gboolean connected;
/* initialize options */
options = options_init();
}
/* set xterm title */
- if( getenv("DISPLAY") )
- printf("%c]0;%s%c", '\033', PACKAGE " v" VERSION, '\007');
+ if( g_getenv("DISPLAY") )
+ printf("%c]0;%s%c", '\033', PACKAGE " version " VERSION, '\007');
/* install exit function */
atexit(exit_and_cleanup);
/* connect to our music player daemon */
- mpc = mpc_connect(options->host, options->port);
+ mpc = mpc_connect(options->host, options->port, options->password);
if( mpc_error(mpc) )
exit(EXIT_FAILURE);
/* initialize timer */
timer = g_timer_new();
- connected=1;
+ connected = TRUE;
while( connected || options->reconnect )
{
static gdouble t = G_MAXDOUBLE;
if( connected && t>=MPD_UPDATE_TIME )
{
mpc_update(mpc);
- if( mpc_error(mpc) )
+ if( mpc_error(mpc) == MPD_ERROR_ACK )
+ {
+ screen_status_printf("%s", mpc_error_str(mpc));
+ mpd_clearError(mpc->connection);
+ mpd_finishCommand(mpc->connection);
+ }
+ else if( mpc_error(mpc) )
{
- connected=0;
screen_status_printf("Lost connection to %s", options->host);
+ connected = FALSE;
doupdate();
+ mpd_clearError(mpc->connection);
mpd_closeConnection(mpc->connection);
mpc->connection = NULL;
}
- else
+ else
mpd_finishCommand(mpc->connection);
g_timer_start(timer);
}
-
if( connected )
{
command_t cmd;
}
else if( options->reconnect )
{
- sleep(3);
+ sleep(MPD_RECONNECT_TIMEOUT);
screen_status_printf("Connecting to %s... [Press Ctrl-C to abort]",
options->host);
- if( mpc_reconnect(mpc, options->host, options->port) == 0 )
+ if( mpc_reconnect(mpc,
+ options->host,
+ options->port,
+ options->password) == 0 )
{
screen_status_printf("Connected to %s!", options->host);
- connected=1;
+ connected = TRUE;
}
doupdate();
}
t = g_timer_elapsed(timer, NULL);
-
}
+
exit(EXIT_FAILURE);
}
index 785117fc9ad58c8b0ddd420d0a6696e2bc174a27..00a044462d3d0b0cd9568759373154c284c35252 100644 (file)
--- a/mpc.c
+++ b/mpc.c
}
mpd_client_t *
-mpc_connect(char *host, int port)
+mpc_connect(char *host, int port, char *password)
{
mpd_Connection *connection;
mpd_client_t *c;
c->connection = connection;
c->cwd = g_strdup("");
+ if( password )
+ {
+ mpd_sendPasswordCommand(connection, password);
+ mpd_finishCommand(connection);
+ }
+
return c;
}
int
-mpc_reconnect(mpd_client_t *c, char *host, int port)
+mpc_reconnect(mpd_client_t *c, char *host, int port, char *password)
{
mpd_Connection *connection;
c->connection = connection;
+ if( password )
+ {
+ mpd_sendPasswordCommand(connection, password);
+ mpd_finishCommand(connection);
+ }
+
return 0;
}
if( c == NULL || c->connection == NULL )
return 1;
if( c->connection->error )
- return 1;
+ return c->connection->error;
return 0;
}
index 608d22c5c0806543910e2113a6733ad851c1d589..b18974b61aed20930d2bf91cf2b0983808bd0049 100644 (file)
--- a/mpc.h
+++ b/mpc.h
int mpc_close(mpd_client_t *c);
-mpd_client_t *mpc_connect(char *host, int port);
-int mpc_reconnect(mpd_client_t *c, char *host, int port);
+mpd_client_t *mpc_connect(char *host, int port, char *passwd);
+int mpc_reconnect(mpd_client_t *c, char *host, int port, char *passwd);
int mpc_update(mpd_client_t *c);
int mpc_update_playlist(mpd_client_t *c);
diff --git a/options.c b/options.c
index cf3bbca27142e1c76ff4dd2d1c1bd42ddbe97f2a..8556b0f742ee55b6c303f5a3e090345c5d5e98ca 100644 (file)
--- a/options.c
+++ b/options.c
#include "config.h"
#include "options.h"
#include "command.h"
+#include "support.h"
options_t options;
+static char *mpd_host = NULL;
+static char *mpd_password = NULL;
+
static struct poptOption optionsTable[] = {
#ifdef DEBUG
{ "debug", 'D', 0, 0, 'D', "Enable debug output." },
{ "exit", 'e', 0, 0, 'e', "Exit on connection errors." },
{ "port", 'p', POPT_ARG_INT, &options.port, 0,
"Connect to server on port [" DEFAULT_PORT_STR "].", "PORT" },
- { "host", 'h', POPT_ARG_STRING, &options.host, 0,
+ { "host", 'h', POPT_ARG_STRING, &mpd_host, 0,
"Connect to server [" DEFAULT_HOST "].", "HOSTNAME" },
+ { "passwd", 'P', POPT_ARG_STRING, &mpd_password, 0,
+ "Connect with password.", "PASSWORD" },
POPT_AUTOHELP
{ NULL, 0, 0, NULL, 0 }
};
int c;
poptContext optCon; /* context for parsing command-line options */
+ mpd_host = NULL;
+ mpd_password = NULL;
optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
while ((c = poptGetNextOpt(optCon)) >= 0)
{
exit(EXIT_FAILURE);
}
+ if( mpd_host )
+ {
+ g_free(options.host);
+ options.host = mpd_host;
+ }
+ if( mpd_password )
+ {
+ g_free(options.password);
+ options.password = mpd_password;
+ }
poptFreeContext(optCon);
return &options;
}
options_t *
options_init( void )
{
- char *value;
+ const char *value;
+ char *tmp;
memset(&options, 0, sizeof(options_t));
- if( (value=getenv(MPD_HOST_ENV)) )
+
+ if( (value=g_getenv(MPD_HOST_ENV)) )
options.host = g_strdup(value);
else
options.host = g_strdup(DEFAULT_HOST);
- if( (value=getenv(MPD_PORT_ENV)) )
+ if( (tmp=g_strstr_len(options.host, strlen(options.host), "@")) )
+ {
+ char *oldhost = options.host;
+ *tmp = '\0';
+ options.password = locale_to_utf8(oldhost);
+ options.host = g_strdup(tmp+1);
+ g_free(oldhost);
+ }
+
+ if( (value=g_getenv(MPD_PORT_ENV)) )
options.port = atoi(value);
else
options.port = DEFAULT_PORT;
options.find_wrap = 1;
options.bg_color = COLOR_BLACK;
- options.title_color = COLOR_BLUE;
- options.line_color = COLOR_GREEN;
- options.list_color = COLOR_YELLOW;
- options.progress_color = COLOR_GREEN;
- options.status_color = COLOR_RED;
- options.alert_color = COLOR_MAGENTA;;
+ options.title_color = COLOR_YELLOW;
+ options.line_color = COLOR_WHITE;
+ options.list_color = COLOR_GREEN;
+ options.progress_color = COLOR_WHITE;
+ options.status_color = COLOR_YELLOW;
+ options.alert_color = COLOR_RED;
return &options;
}
diff --git a/options.h b/options.h
index cb346374dffff402736bb239a645e2198f23bec7..fb17485e3c3653764ce61ff0446a9badce932cdf 100644 (file)
--- a/options.h
+++ b/options.h
typedef struct
{
char *host;
+ char *username;
+ char *password;
int port;
int reconnect;
int debug;