From: Christophe Rhodes Date: Sun, 23 Mar 2014 11:10:36 +0000 (+0000) Subject: separate out display and state management X-Git-Url: http://christophe.rhodes.io/gitweb/?a=commitdiff_plain;h=b64ea2b2167fe6f6ef5a5102564e6ff792280476;p=squeeze-el.git separate out display and state management attempt to keep track of power states, and display them on change. We don't handle all power change messages yet (I think) so this isn't complete but at least we handle our own power toggles some other scattered fixes --- diff --git a/squeeze.el b/squeeze.el index e669b0c..7ac1841 100644 --- a/squeeze.el +++ b/squeeze.el @@ -23,44 +23,62 @@ (defvar squeeze-control-mode-map (let ((map (make-sparse-keymap))) (define-key map (kbd "SPC") 'squeeze-control-toggle-power) - (define-key map (kbd "g") 'squeeze-control-display-players) + (define-key map (kbd "g") 'squeeze-control-refresh) map)) (define-derived-mode squeeze-control-mode special-mode "SqueezeControl" "Major mode for controlling Squeezebox Servers.\\") (defun squeeze-update-state (string) - (dolist (line (split-string string "\n")) - (squeeze-update-state-from-line line)) + (let (done-something) + (dolist (line (split-string string "\n")) + (when (squeeze-update-state-from-line line) + (setq done-something t)))) + (squeeze-control-display-players) string) (defun squeeze-update-state-from-line (string) (cond ((string-match "^players 0" string) - (setq squeeze-players (squeeze-parse-players-line string))) - ((string-match "^\\([0-9a-f][0-9a-f]%3A[0-9a-f][0-9a-f]%3A[0-9a-f][0-9a-f]%3A[0-9a-f][0-9a-f]%3A[0-9a-f][0-9a-f]%3A[0-9a-f][0-9a-f]\\) power \\([01]\\)" string) - (let ((state (match-string 2 string)) + (setq squeeze-players (squeeze-parse-players-line string)) + t) + ((string-match "^\\([0-9a-f][0-9a-f]%3A[0-9a-f][0-9a-f]%3A[0-9a-f][0-9a-f]%3A[0-9a-f][0-9a-f]%3A[0-9a-f][0-9a-f]%3A[0-9a-f][0-9a-f]\\) power\\( \\([01]\\)\\)?" string) + (let ((state (match-string 3 string)) (id (url-unhex-string (match-string 1 string))) player) (dolist (p squeeze-players) (when (string= id (squeeze-player-playerid p)) (setq player p) (return))) - (setf (squeeze-player-power player) state))))) + (if state + (setf (squeeze-player-power player) state) + (let ((current (squeeze-player-power player))) + (setf (squeeze-player-power player) + (cond ((string= current "0") "1") + ((string= current "1") "0")))))) + t))) (defface squeeze-player-face '((t)) - "Face for displaying players") - + "Face for displaying players" + :group 'squeeze) (defface squeeze-player-on-face '((t :weight bold)) - "Face for displaying players which are on") + "Face for displaying players which are on" + :inherit 'squeeze-player-face + :group 'squeeze) (defface squeeze-player-off-face '((t :weight light)) - "Face for displaying players which are on") + "Face for displaying players which are off" + :inherit 'squeeze-player-face + :group 'squeeze) (defvar squeeze-players ()) +(defun squeeze-control-query-players () + (interactive) + (comint-send-string (get-buffer-process "*squeeze*") (format "players ?\n"))) + (defun squeeze-control-toggle-power (&optional id) (interactive) (unless id @@ -71,8 +89,7 @@ (interactive) (unless id (setq id (get-text-property (point) 'squeeze-playerid))) - (comint-send-string (get-buffer-process "*squeeze*") (format "%s power ?\n" id)) - (accept-process-output (get-buffer-process "*squeeze*"))) + (comint-send-string (get-buffer-process "*squeeze*") (format "%s power ?\n" id))) (defun squeeze-control-player-face (player) (let ((power (squeeze-player-power player))) @@ -80,10 +97,14 @@ ((string= power "0") 'squeeze-player-off-face) (t 'squeeze-player-face)))) -(defun squeeze-control-display-players () +(defun squeeze-control-refresh () (interactive) + (squeeze-control-query-players) (dolist (player squeeze-players) - (squeeze-control-query-power (squeeze-player-playerid player))) + (squeeze-control-query-power (squeeze-player-playerid player)))) + +(defun squeeze-control-display-players () + (interactive) (let ((players squeeze-players)) (with-current-buffer (get-buffer-create "*squeeze-control*") (squeeze-control-mode)