2 "Interact with Squeezebox media servers"
6 (defcustom squeeze-server-address "localhost"
7 "Address for the Squeezebox server"
9 (defcustom squeeze-server-port 9090
10 "Port number for the Squeezebox server"
13 (defvar squeeze-mode-map
14 (let ((map (make-sparse-keymap)))
15 (define-key map (kbd "TAB") 'completion-at-point)
18 (define-derived-mode squeeze-mode comint-mode "Squeeze"
19 "Major mode for interacting with the Squeezebox Server CLI.\\<squeeze-mode-map>"
20 (add-hook 'comint-preoutput-filter-functions 'url-unhex-string nil t)
21 (add-hook 'comint-preoutput-filter-functions 'squeeze-update-state nil t))
23 (defvar squeeze-control-mode-map
24 (let ((map (make-sparse-keymap)))
25 (define-key map (kbd "SPC") 'squeeze-control-toggle-power)
26 (define-key map (kbd "g") 'squeeze-control-refresh)
27 (define-key map (kbd "+") 'squeeze-control-volume-up)
28 (define-key map (kbd "-") 'squeeze-control-volume-down)
29 (define-key map (kbd "t") 'squeeze-control-toggle-syncgroup-display)
32 (define-derived-mode squeeze-control-mode special-mode "SqueezeControl"
33 "Major mode for controlling Squeezebox Servers.\\<squeeze-control-mode-map>")
35 (defvar squeeze-control-inhibit-display nil)
37 (defun squeeze-update-state (string)
39 (dolist (line (split-string string "\n"))
40 (when (squeeze-update-state-from-line line)
41 (setq done-something t)))
43 (unless squeeze-control-inhibit-display
44 (squeeze-control-display-players))))
47 (defconst squeeze-player-line-regexp
48 "^\\(\\(?:[0-9a-f]\\{2\\}%3A\\)\\{5\\}[0-9a-f]\\{2\\}\\) ")
50 (defun squeeze-find-player (id)
51 (dolist (player squeeze-players)
52 (when (string= id (squeeze-player-playerid player))
55 (defun squeeze-update-power (player state)
57 (setf (squeeze-player-power player) state)
58 (let ((current (squeeze-player-power player)))
59 (setf (squeeze-player-power player)
60 (cond ((string= current "0") "1")
61 ((string= current "1") "0"))))))
63 (defun squeeze-update-mixer-volume (player value)
64 (let ((current (squeeze-player-volume player))
65 (number (string-to-number value)))
66 (if (string-match "^[-+]" value)
67 (setf (squeeze-player-volume player)
68 (and current (max 0 (min 100 (+ current number)))))
69 (setf (squeeze-player-volume player) number))))
71 (defun squeeze-update-state-from-line (string)
73 ((string-match "^players 0" string)
74 (setq squeeze-players (squeeze-parse-players-line string))
76 ((string-match "^syncgroups" string)
77 (setq squeeze-syncgroups (squeeze-parse-syncgroups-line string))
79 ((string-match squeeze-player-line-regexp string)
80 (let ((substring (substring string (match-end 0)))
81 (id (url-unhex-string (match-string 1 string))))
83 ((string-match "^power\\(?: \\([01]\\)\\)?" substring)
84 (let ((state (match-string 1 substring))
85 (player (squeeze-find-player id)))
86 (squeeze-update-power player state))
88 ((string-match "^mixer volume \\(\\(?:-\\|%2B\\)?[0-9]*\\)" substring)
89 (let ((value (url-unhex-string (match-string 1 substring)))
90 (player (squeeze-find-player id)))
91 (squeeze-update-mixer-volume player value))
94 (defface squeeze-player-face
96 "Face for displaying players"
98 (defface squeeze-player-on-face
99 '((t :weight bold :inherit squeeze-player-face))
100 "Face for displaying players which are on"
102 (defface squeeze-player-off-face
103 '((t :weight light :inherit squeeze-player-face))
104 "Face for displaying players which are off"
107 (defface squeeze-mixer-face
109 "Face for displaying mixer information"
111 (defface squeeze-mixer-muted-face
112 '((t :weight light :inherit squeeze-mixer-face))
113 "Face for displaying mixer information when muted"
115 (defface squeeze-mixer-quiet-face
116 '((t :foreground "green3" :inherit squeeze-mixer-face))
117 "Face for quiet volume"
119 (defface squeeze-mixer-medium-face
120 '((t :foreground "gold" :inherit squeeze-mixer-face))
121 "Face for medium volume"
123 (defface squeeze-mixer-loud-face
124 '((t :foreground "OrangeRed1" :inherit squeeze-mixer-face))
125 "Face for loud volume"
127 (defface squeeze-mixer-muted-quiet-face
128 '((t :inherit (squeeze-mixer-muted-face squeeze-mixer-quiet-face)))
129 "Face for quiet volume when muted")
130 (defface squeeze-syncgroup-face
132 "Face for syncgroups"
135 (defun squeeze-mixer-compute-bar (vol width)
136 (let* ((exact (* width (/ vol 100.0)))
137 (nfull (floor exact))
138 (frac (- exact nfull))
139 (nblank (floor (- width exact))))
141 (make-string nfull ?█)
142 (cond ((= width (+ nfull nblank)) "")
143 ((< frac 0.0625) " ")
144 ((< frac 0.1875) "▏")
145 ((< frac 0.3125) "▎")
146 ((< frac 0.4375) "▍")
147 ((< frac 0.5625) "▌")
148 ((< frac 0.6875) "▋")
149 ((< frac 0.8125) "▊")
150 ((< frac 0.9375) "▉")
152 (make-string nblank ? ))))
154 (defun squeeze-mixer-make-bar (vol width)
155 (let ((bar (squeeze-mixer-compute-bar vol width))
156 (lo (floor (* 0.65 width)))
157 (hi (floor (* 0.9 width))))
159 (propertize (substring bar 0 lo) 'face 'squeeze-mixer-quiet-face)
160 (propertize (substring bar lo hi) 'face 'squeeze-mixer-medium-face)
161 (propertize (substring bar hi) 'face 'squeeze-mixer-loud-face)
162 (propertize "▏" 'intangible t))))
164 (defvar squeeze-players ())
165 (defvar squeeze-syncgroups ())
167 (defun squeeze-control-query-syncgroups ()
169 (comint-send-string (get-buffer-process "*squeeze*") (format "syncgroups ?\n")))
171 (defun squeeze-control-query-players ()
173 (comint-send-string (get-buffer-process "*squeeze*") (format "players 0\n")))
175 (defun squeeze-control-toggle-power (&optional id)
178 (setq id (get-text-property (point) 'squeeze-playerid)))
179 (comint-send-string (get-buffer-process "*squeeze*") (format "%s power\n" id)))
181 (defun squeeze-control-query-power (&optional id)
184 (setq id (get-text-property (point) 'squeeze-playerid)))
186 (comint-send-string (get-buffer-process "*squeeze*") (format "%s power ?\n" id))))
188 (defun squeeze-control-volume-up (&optional id inc)
190 (unless inc (setq inc 5))
192 (setq id (get-text-property (point) 'squeeze-playerid)))
194 (comint-send-string (get-buffer-process "*squeeze*") (format "%s mixer volume %+d\n" id inc))))
196 (defun squeeze-control-volume-down (&optional id inc)
198 (unless inc (setq inc 5))
200 (setq id (get-text-property (point) 'squeeze-playerid)))
202 (comint-send-string (get-buffer-process "*squeeze*") (format "%s mixer volume %+d\n" id (- inc)))))
204 (defun squeeze-control-volume-set (id val)
206 (comint-send-string (get-buffer-process "*squeeze*") (format "%s mixer volume %d\n" id val)))
208 (defun squeeze-control-query-mixer-volume (&optional id)
211 (setq id (get-text-property (point) 'squeeze-playerid)))
213 (comint-send-string (get-buffer-process "*squeeze*") (format "%s mixer volume ?\n" id))))
215 (defun squeeze-control-player-face (player)
216 (let ((power (squeeze-player-power player)))
217 (cond ((string= power "1") 'squeeze-player-on-face)
218 ((string= power "0") 'squeeze-player-off-face)
219 (t 'squeeze-player-face))))
221 (defun squeeze-control-listen ()
222 (comint-send-string (get-buffer-process "*squeeze*") (format "listen 1\n")))
224 (defun squeeze-control-refresh ()
226 (let ((squeeze-control-inhibit-display t))
227 (squeeze-control-query-players)
228 (accept-process-output (get-buffer-process "*squeeze*"))
229 (dolist (player squeeze-players)
230 (squeeze-control-query-power (squeeze-player-playerid player))
231 (accept-process-output (get-buffer-process "*squeeze*"))
232 (squeeze-control-query-mixer-volume (squeeze-player-playerid player))
233 (accept-process-output (get-buffer-process "*squeeze*"))))
234 (squeeze-control-display-players))
236 (defvar squeeze-control-mixer-map
237 (let ((map (make-sparse-keymap)))
238 (define-key map (kbd "RET") 'squeeze-control-mixer-set-volume)
239 (define-key map [mouse-1] 'squeeze-control-mixer-mouse-1)
242 (defun squeeze-control-compute-volume (pos)
243 (let* ((end (next-single-property-change pos 'keymap))
244 (start (previous-single-property-change end 'keymap)))
245 (/ (* 100 (- (point) start)) (- end start 1))))
247 (defun squeeze-control-mixer-mouse-1 (event)
249 (let* ((pos (cadadr event))
250 (val (squeeze-control-compute-volume pos))
251 (id (get-text-property pos 'squeeze-playerid)))
252 (squeeze-control-volume-set id val)))
254 (defun squeeze-control-mixer-set-volume ()
256 (let* ((val (squeeze-control-compute-volume (point)))
257 (id (get-text-property (point) 'squeeze-playerid)))
258 (squeeze-control-volume-set id val)))
260 (defvar squeeze-control-display-syncgroups nil)
262 (defun squeeze-control-toggle-syncgroup-display ()
264 (setf squeeze-control-display-syncgroups
265 (not squeeze-control-display-syncgroups))
266 (squeeze-control-display-players))
268 (defun squeeze-control-insert-player (player)
269 (insert (propertize (format "%20s" (squeeze-player-name player))
270 'face (squeeze-control-player-face player)
271 'squeeze-playerid (squeeze-player-playerid player)))
272 (when (squeeze-player-volume player)
274 (squeeze-mixer-make-bar (squeeze-player-volume player) 28)
275 'squeeze-playerid (squeeze-player-playerid player)
276 'keymap squeeze-control-mixer-map
278 'rear-nonsticky '(keymap))))
279 (insert (propertize "\n" 'intangible t)))
281 (defun squeeze-control-display-players ()
284 (squeeze-control-display-syncgroups
285 (with-current-buffer (get-buffer-create "*squeeze-control*")
286 (squeeze-control-mode)
289 (let ((syncgroups squeeze-syncgroups)
292 (let ((names (getf syncgroups :names))
293 (members (split-string (getf syncgroups :members) ",")))
294 (insert (propertize names 'face 'squeeze-syncgroup-face) "\n")
295 (dolist (member members)
296 (let ((player (squeeze-find-player member)))
297 (squeeze-control-insert-player player)
298 (push player seen))))
299 (setq syncgroups (cddddr syncgroups)))
300 (insert (propertize "No syncgroup" 'face 'squeeze-syncgroup-face) "\n")
301 (dolist (player squeeze-players)
302 (unless (member player seen)
303 (squeeze-control-insert-player player))))))
305 (with-current-buffer (get-buffer-create "*squeeze-control*")
306 (squeeze-control-mode)
309 (dolist (player squeeze-players)
310 (squeeze-control-insert-player player))
311 (read-only-mode 1)))))
313 (cl-defstruct (squeeze-player (:constructor squeeze-make-player))
314 playerindex playerid uuid ip name model isplayer displaytype canpoweroff connected power volume)
316 (defun squeeze-string-plistify (string start end)
320 (if (string-match "\\([a-z]+\\)%3A\\([^ \n]+\\)" string start)
321 (let ((mend (match-end 0)))
324 (push (intern (format ":%s" (substring string (match-beginning 1) (match-end 1)))) result)
325 (push (url-unhex-string (substring string (match-beginning 2) (match-end 2))) result)
330 (defun squeeze-parse-syncgroups-line (string)
331 (let ((syncgroupspos (string-match "^syncgroups " string))
332 (startpos (match-end 0)))
334 (squeeze-string-plistify string startpos (length string)))))
336 (defun squeeze-parse-players-line (string)
337 (let ((countpos (string-match " count%3A\\([0-9]\\) " string))
338 (startpos (match-end 0)))
340 (message "no count found in players line"))
341 (let ((count (parse-integer string (match-beginning 1) (match-end 1)))
343 (dotimes (i (1- count))
344 (setq endpos (progn (string-match " connected%3A[0-1] " string startpos)
346 (push (apply 'squeeze-make-player (squeeze-string-plistify string startpos endpos)) result)
347 (setq startpos endpos))
348 (push (apply 'squeeze-make-player (squeeze-string-plistify string startpos (length string))) result)
351 (defun squeeze-complete-command-at-point ()
353 (list (progn (backward-word) (point))
354 (progn (forward-word) (point))
355 '(;; General commands and queries
356 "login" "can" "version" "listen" "subscribe" "pref"
357 "logging" "getstring" "setsncredentials" "debug"
360 ;; Player commands and queries
361 "player" "count" "id" "uuid" "name" "ip" "model" "isplayer"
362 "displaytype" "canpoweroff" "?" "signalstrength" "connected"
363 "sleep" "sync" "syncgroups" "power" "mixer" "volume" "muting"
364 "bass" "treble" "pitch" "show" "display" "linesperscreen"
365 "displaynow" "playerpref" "button" "ir" "irenable"
366 "connect" "client" "forget" "disconnect" "players"
368 ;; Database commands and queries
369 "rescan" "rescanprogress" "abortscan" "wipecache" "info"
370 "total" "genres" "artists" "albums" "songs" "years"
371 "musicfolder" "playlists" "tracks" "new" "rename" "delete"
372 "edit" "songinfo" "titles" "search" "pragma"
374 ;; Playlist commands and queries
375 "play" "stop" "pause" "mode" "time" "genre" "artist" "album"
376 "title" "duration" "remote" "current_title" "path" "playlist"
377 "add" "insert" "deleteitem" "move" "delete" "preview" "resume"
378 "save" "loadalbum" "addalbum" "loadtracks" "addtracks"
379 "insertalbum" "deletealbum" "clear" "zap" "name" "url"
380 "modified" "playlistsinfo" "index" "shuffle" "repeat"
384 "serverstatus" "status" "displaystatus" "readdirectory"
388 ;; Alarm commands and queries
391 ;; Plugins commands and queries
397 (let ((buffer (make-comint-in-buffer "squeeze" nil
398 (cons squeeze-server-address
399 squeeze-server-port))))
400 (switch-to-buffer buffer)
403 (defun squeeze-control ()
406 (let ((buffer (get-buffer-create "*squeeze-control*")))
407 (switch-to-buffer buffer)
408 (squeeze-control-listen)
409 (squeeze-control-refresh)
410 (squeeze-control-display-players)))