1 ;;; sndio.el --- Interact with sndio(8) -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2020, 2021 Omar Polo
5 ;; Author: Omar Polo <op@omarpolo.com>
7 ;; Keywords: multimedia
8 ;; URL: https://git.omarpolo.com/sndio.el
9 ;; Package-Requires: ((emacs "25.1"))
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
26 ;; This package provides the sndio major mode to interact with
34 (defvar sndio-sndioctl-cmd "sndioctl"
35 "Path to the sndioctl executable.")
37 (defvar sndio-step 0.02
38 "Step for `sndio-increase' and `sndio-decrease'.")
40 (defvar sndio--window nil
43 (defvar sndio-mode-map
44 (let ((m (make-sparse-keymap)))
45 (define-key m (kbd "n") #'forward-line)
46 (define-key m (kbd "p") #'previous-line)
47 (define-key m (kbd "i") #'sndio-increase)
48 (define-key m (kbd "d") #'sndio-decrease)
49 (define-key m (kbd "m") #'sndio-mute)
50 (define-key m (kbd "t") #'sndio-toggle)
51 (define-key m (kbd "g") #'sndio-update)
52 (define-key m (kbd "q") #'sndio-quit)
56 (define-derived-mode sndio-mode special-mode "sndio"
57 "Major mode for sndio interaction."
61 (defun sndio-update ()
62 "Update the current sndio buffer."
64 (when (derived-mode-p 'sndio-mode)
65 (let ((inhibit-read-only t))
67 (process-file sndio-sndioctl-cmd nil (current-buffer) nil)
68 (goto-char (point-min)))))
70 (defun sndio--run (&rest args)
71 "Run `sndio-sndioctl-cmd' with ARGS yielding its output."
73 (when (zerop (apply #'process-file sndio-sndioctl-cmd nil t nil args))
76 (defun sndio--current-io ()
77 "Yield the input/poutput at point as string."
78 (when-let (end (save-excursion
80 (ignore-errors (search-forward "="))))
81 (buffer-substring-no-properties (line-beginning-position)
84 (defun sndio--update-value (x)
85 "Update the value for the input/output at point setting it to X."
89 (let ((inhibit-read-only t))
90 (delete-region (point) (line-end-position))
91 (insert (string-trim-right x)))))
93 (defun sndio-increase ()
94 "Increase the volume for the input/output at point."
96 (when-let (x (sndio--current-io))
97 (when-let (val (sndio--run "-n" (concat x "=+" (number-to-string sndio-step))))
98 (sndio--update-value val))))
100 (defun sndio-decrease ()
101 "Decrease the volume for the input/output at point."
103 (when-let (x (sndio--current-io))
104 (when-let (val (sndio--run "-n" (concat x "=-" (number-to-string sndio-step))))
105 (sndio--update-value val))))
108 "Mute the input/output at point."
110 (when-let (x (sndio--current-io))
111 (when-let (val (sndio--run "-n" (concat x "=0")))
112 (sndio--update-value val))))
114 (defun sndio-toggle ()
115 "Toggle input/output at point."
117 (when-let (x (sndio--current-io))
118 (when-let (val (sndio--run "-n" (concat x "=!")))
119 (sndio--update-value val))))
123 Call `delete-window' when the sndio popup window is open or
124 `quit-window' otherwise."
126 (if (window-live-p sndio--window)
134 (switch-to-buffer "*sndio*")
138 (defun sndio-win-open ()
139 "Open an sndio window at the bottom of the frame for quick editing."
141 (unless (window-live-p sndio--window)
144 (let ((ignore-window-parameters t))
145 (split-window (frame-root-window)
149 (switch-to-buffer (get-buffer-create "*sndio-quick*")
151 (if (derived-mode-p 'sndio-mode)
155 (setq mode-line-format nil
156 header-line-format nil
158 (set-window-hscroll sndio--window 0)
159 (set-window-dedicated-p sndio--window t)
160 (select-window sndio--window 'norecord)
161 (let ((window-resize-pixelwise t)
163 (fit-window-to-buffer sndio--window nil nil 1))))
166 ;;; sndio.el ends here