struct mpd_settings * mpd_settings_new(const char *host, unsigned port, unsigned timeout_ms, const char *reserved, const char *password) { (void)reserved; struct mpd_settings *settings = malloc(sizeof(*settings)); if (settings == NULL) return settings; settings->password = NULL; port = mpd_check_port(port); host = mpd_check_host(host, &settings->password); if (settings->password == NULL && password != NULL) settings->password = strdup(password); if (host == NULL) { #ifdef DEFAULT_SOCKET if (port == 0) /* default to local socket only if no port was explicitly configured */ host = DEFAULT_SOCKET; else #endif host = DEFAULT_HOST; } settings->host = strdup(host); settings->timeout_ms = timeout_ms != 0 ? timeout_ms : mpd_default_timeout_ms(); settings->port = host[0] == '/' ? 0 /* no port for local socket */ : (port != 0 ? port : DEFAULT_PORT); return settings; }
int main(int argc, char *argv[]) { char *password = NULL; struct mpd_connection *conn = mpd_connection_new(mpd_check_host(NULL, &password), 0, 0); if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { fprintf(stderr,"%s: %s\n", argv[0], mpd_connection_get_error_message(conn)); return 1; } if (password != NULL) { if (mpd_run_password(conn, password) == false) { fprintf(stderr, "%s: mpd_run_password %s\n", argv[0], mpd_connection_get_error_message(conn)); free(password); return 2; } } if (notify_init("MPD_Notification") == 0) { fprintf(stderr, "%s: Cannot initialize libnotify.\n", argv[0]); return 3; } infinite_loop(conn); mpd_connection_free(conn); free(password); notify_uninit(); return 0; }
struct mpd_connection * mpd_connection_new(const char *host, unsigned port, unsigned timeout_ms) { struct mpd_connection *connection = malloc(sizeof(*connection)); bool success; int fd; const char *line; char *password = NULL; if (connection == NULL) return NULL; mpd_error_init(&connection->error); connection->async = NULL; connection->parser = NULL; connection->receiving = false; connection->sending_command_list = false; connection->pair_state = PAIR_STATE_NONE; connection->request = NULL; if (!mpd_socket_global_init(&connection->error)) return connection; if (timeout_ms == 0) /* 30s is the default */ timeout_ms = 30000; mpd_connection_set_timeout(connection, timeout_ms); host = mpd_check_host(host, &password); port = mpd_check_port(port); fd = mpd_connect(host, port, &connection->timeout, &connection->error); if (fd < 0) { free(password); return connection; } connection->async = mpd_async_new(fd); if (connection->async == NULL) { free(password); mpd_socket_close(fd); mpd_error_code(&connection->error, MPD_ERROR_OOM); return connection; } connection->parser = mpd_parser_new(); if (connection->parser == NULL) { free(password); mpd_error_code(&connection->error, MPD_ERROR_OOM); return connection; } line = mpd_sync_recv_line(connection->async, &connection->timeout); if (line == NULL) { free(password); mpd_connection_sync_error(connection); return connection; } success = mpd_parse_welcome(connection, line); if (password != NULL) { if (success) mpd_run_password(connection, password); free(password); } return connection; }