add settings modal dialog
authorAndrew Karpow <andy@ndyk.de>
Tue, 4 Feb 2014 16:58:10 +0000 (17:58 +0100)
committerAndrew Karpow <andy@ndyk.de>
Tue, 4 Feb 2014 16:58:10 +0000 (17:58 +0100)
CMakeLists.txt
htdocs/index.html
htdocs/js/mpd.js
src/config.h.in
src/mpd_client.c
src/mpd_client.h
src/ympd.c

index 5e04246..094c895 100644 (file)
@@ -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)
index 8f40d8f..04e2bd4 100644 (file)
@@ -40,6 +40,7 @@
           <li id="playlist"><a href="#/">Playlist</a></li>
           <li id="browse"><a href="#/browse/">Browse database</a></li>
           <li><a href="#" data-toggle="modal" data-target="#about" onclick="getVersion();">About</a></li>
+          <li><a href="#" data-toggle="modal" data-target="#settings" onclick="getHost();">Settings</a></li>
         </ul>
 
         <div class="btn-toolbar navbar-btn navbar-right" role="toolbar">
     </div><!-- /.modal-dialog -->
   </div><!-- /.modal -->
 
+    <!-- Modal -->
+  <div class="modal fade" id="settings" tabindex="-1" role="dialog" aria-labelledby="settingsLabel" aria-hidden="true">
+    <div class="modal-dialog">
+      <div class="modal-content">
+        <div class="modal-header">
+          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
+          <h2 class="modal-title" id="settingsLabel"><span class="glyphicon glyphicon-wrench"></span> Settings</h2>
+        </div>
+        <div class="modal-body">
+          <form class="form-inline" role="form">
+            <div class="form-group row">
+              <div class="col-xs-9">
+                <label class="control-label" for="mpdhost">MPD Host / IP</label>
+                <input type="text" class="form-control" id="mpdhost">
+               </div>
+              <div class="col-xs-3">
+                <label class="control-label" for="mpdport">MPD Port</label>
+                <input type="text" class="form-control" id="mpdport">
+              </div>
+            </div>
+          </form>
+        </div>
+        <div class="modal-footer">
+          <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
+          <button type="button" class="btn btn-default" data-dismiss="modal" onclick="setHost();">Save</button>
+        </div>
+      </div><!-- /.modal-content -->
+    </div><!-- /.modal-dialog -->
+  </div><!-- /.modal -->
+
+
 
   <!-- Bootstrap core JavaScript
   ================================================== -->
index bc16615..60114da 100644 (file)
@@ -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;
 }
index d4f6010..33681c9 100644 (file)
@@ -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
 
index c613dc0..3c34ac5 100644 (file)
@@ -40,6 +40,7 @@
 #include <mpd/tag.h>
 
 #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;
index 6bece9a..5ca70d6 100644 (file)
 #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
 
index 2633f1e..92a28d8 100644 (file)
@@ -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);