From 8c49b2a877a55bf07e2fddd04d8b81009264ad63 Mon Sep 17 00:00:00 2001 From: Andrew Karpow Date: Tue, 4 Feb 2014 17:58:10 +0100 Subject: [PATCH] add settings modal dialog --- CMakeLists.txt | 1 + htdocs/index.html | 32 ++++++++++++++++++++++++++++++++ htdocs/js/mpd.js | 21 +++++++++++++++++++++ src/config.h.in | 2 +- src/mpd_client.c | 28 +++++++++++++++++++++++++--- src/mpd_client.h | 9 ++++++--- src/ympd.c | 4 ++-- 7 files changed, 88 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e04246..094c895 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ set(CPACK_DEBIAN_PACKAGE_MAINTAINER "andy@ndyk.de") set(CPACK_DEBIAN_PACKAGE_DEPENDS "libssl1.0.0,libmpdclient2") option(WITH_STATIC_WEBSOCKETS "Build with static libwebsockets library" ON) +option(WITH_MPD_HOST_CHANGE "Let users of the web frontend change the MPD Host" ON) find_package(LibMPDClient REQUIRED) find_package(LibWebSockets REQUIRED) diff --git a/htdocs/index.html b/htdocs/index.html index 8f40d8f..04e2bd4 100644 --- a/htdocs/index.html +++ b/htdocs/index.html @@ -40,6 +40,7 @@
  • Playlist
  • Browse database
  • About
  • +
  • Settings
  • + + + + diff --git a/htdocs/js/mpd.js b/htdocs/js/mpd.js index bc16615..60114da 100644 --- a/htdocs/js/mpd.js +++ b/htdocs/js/mpd.js @@ -311,6 +311,10 @@ function webSocketConnect() { type: "info", }).show(); + break; + case "mpdhost": + $('#mpdhost').val(obj.data.host); + $('#mpdport').val(obj.data.port); break; case "error": $('.top-right').notify({ @@ -452,6 +456,23 @@ function getVersion() }); } +function getHost() { + socket.send('MPD_API_GET_MPDHOST'); + + function onEnter(event) { + if ( event.which == 13 ) + setHost(); + $('#settings').modal('hide'); + } + + $('#mpdhost').keypress(onEnter); + $('#mpdport').keypress(onEnter); +} + +function setHost() { + socket.send('MPD_API_SET_MPDHOST,'+$('#mpdport').val()+','+$('#mpdhost').val()); +} + function notificationsSupported() { return "webkitNotifications" in window; } diff --git a/src/config.h.in b/src/config.h.in index d4f6010..33681c9 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -34,6 +34,6 @@ #define YMPD_VERSION_PATCH ${CPACK_PACKAGE_VERSION_PATCH} #define DATADIR "${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}" #define LOCAL_RESOURCE_PATH DATADIR"/htdocs" - +#cmakedefine WITH_MPD_HOST_CHANGE #endif diff --git a/src/mpd_client.c b/src/mpd_client.c index c613dc0..3c34ac5 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -40,6 +40,7 @@ #include #include "mpd_client.h" +#include "config.h" struct mpd_connection *conn = NULL; enum mpd_conn_states mpd_conn_state = MPD_DISCONNECTED; @@ -94,6 +95,12 @@ int callback_ympd(struct libwebsocket_context *context, pss->do_send &= ~DO_SEND_BROWSE; free(pss->browse_path); } + else if(pss->do_send & DO_SEND_MPDHOST) { + n = snprintf(p, MAX_SIZE, "{\"type\":\"mpdhost\", \"data\": " + "{\"host\" : \"%s\", \"port\": \"%d\"}" + "}", mpd_host, mpd_port); + pss->do_send &= ~DO_SEND_MPDHOST; + } else { /* Default Action */ int current_song_id; @@ -209,11 +216,25 @@ int callback_ympd(struct libwebsocket_context *context, free(uri); } } - +#ifdef WITH_MPD_HOST_CHANGE + else if(!strncmp((const char *)in, MPD_API_SET_MPDHOST, sizeof(MPD_API_SET_MPDHOST)-1)) { + char *host; + int port = 0; + if(sscanf(in, "MPD_API_SET_MPDHOST,%d,%m[^\t\n ]", &port, &host) && host != NULL && port > 0) { + strncpy(mpd_host, host, sizeof(mpd_host)); + free(host); + mpd_port = port; + mpd_conn_state = MPD_RECONNECT; + break; + } + } + else if(!strncmp((const char *)in, MPD_API_GET_MPDHOST, sizeof(MPD_API_GET_MPDHOST)-1)) { + pss->do_send |= DO_SEND_MPDHOST; + } +#endif - if(mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) + if(mpd_conn_state == MPD_CONNECTED && mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) pss->do_send |= DO_SEND_ERROR; - break; default: @@ -249,6 +270,7 @@ void mpd_loop() case MPD_FAILURE: lwsl_err("MPD connection failed.\n"); + case MPD_RECONNECT: if(conn != NULL) mpd_connection_free(conn); conn = NULL; diff --git a/src/mpd_client.h b/src/mpd_client.h index 6bece9a..5ca70d6 100644 --- a/src/mpd_client.h +++ b/src/mpd_client.h @@ -38,12 +38,13 @@ #define DO_SEND_TRACK_INFO (1 << 2) #define DO_SEND_BROWSE (1 << 3) #define DO_SEND_ERROR (1 << 4) - +#define DO_SEND_MPDHOST (1 << 5) #define MPD_API_GET_SEEK "MPD_API_GET_SEEK" #define MPD_API_GET_PLAYLIST "MPD_API_GET_PLAYLIST" #define MPD_API_GET_TRACK_INFO "MPD_API_GET_TRACK_INFO" #define MPD_API_GET_BROWSE "MPD_API_GET_BROWSE" +#define MPD_API_GET_MPDHOST "MPD_API_GET_MPDHOST" #define MPD_API_ADD_TRACK "MPD_API_ADD_TRACK" #define MPD_API_ADD_PLAY_TRACK "MPD_API_ADD_PLAY_TRACK" #define MPD_API_PLAY_TRACK "MPD_API_PLAY_TRACK" @@ -56,6 +57,7 @@ #define MPD_API_SET_SEEK "MPD_API_SET_SEEK" #define MPD_API_SET_NEXT "MPD_API_SET_PREV" #define MPD_API_SET_PREV "MPD_API_SET_NEXT" +#define MPD_API_SET_MPDHOST "MPD_API_SET_MPDHOST" #define MPD_API_UPDATE_DB "MPD_API_UPDATE_DB" #define MPD_API_TOGGLE_RANDOM "MPD_API_TOGGLE_RANDOM" #define MPD_API_TOGGLE_CONSUME "MPD_API_TOGGLE_CONSUME" @@ -72,7 +74,8 @@ struct per_session_data__ympd { enum mpd_conn_states { MPD_FAILURE, MPD_DISCONNECTED, - MPD_CONNECTED + MPD_CONNECTED, + MPD_RECONNECT }; void *mpd_idle_connection(void *_data); @@ -87,7 +90,7 @@ int mpd_put_playlist(char *buffer); int mpd_put_browse(char *buffer, char *path); int mpd_port; -const char *mpd_host; +char mpd_host[255]; #endif diff --git a/src/ympd.c b/src/ympd.c index 2633f1e..92a28d8 100644 --- a/src/ympd.c +++ b/src/ympd.c @@ -81,7 +81,7 @@ int main(int argc, char **argv) atexit(bye); memset(&info, 0, sizeof info); info.port = 8080; - mpd_host = "127.0.0.1"; + strcpy(mpd_host, "127.0.0.1"); mpd_port = 6600; lws_set_log_level(LLL_ERR | LLL_WARN, NULL); @@ -105,7 +105,7 @@ int main(int argc, char **argv) long_options, &option_index)) != -1) { switch (n) { case 'h': - mpd_host = optarg; + strncpy(mpd_host, optarg, sizeof(mpd_host)); break; case 'p': mpd_port = atoi(optarg);