Blob


1 ;;; sndio.el --- Interact with sndio(8) -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2020, 2021 Omar Polo
5 ;; Author: Omar Polo <op@omarpolo.com>
6 ;; Version: 1.0
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/>.
24 ;;; Commentary:
26 ;; This package provides the sndio major mode to interact with
27 ;; OpenBSD' sndio(8).
29 ;;; Code:
31 (eval-when-compile
32 (require 'subr-x))
34 (defgroup sndio nil
35 "Sndio."
36 :group 'sndio)
38 (defcustom sndio-sndioctl-cmd "sndioctl"
39 "Path to the sndioctl executable."
40 :type 'string)
42 (defcustom sndio-step 0.02
43 "Step for `sndio-increase' and `sndio-decrease'."
44 :type 'integer)
46 (defvar sndio--window nil
47 "The sndio window.")
49 (defvar sndio-mode-map
50 (let ((m (make-sparse-keymap)))
51 (define-key m (kbd "n") #'forward-line)
52 (define-key m (kbd "p") #'previous-line)
53 (define-key m (kbd "i") #'sndio-increase)
54 (define-key m (kbd "d") #'sndio-decrease)
55 (define-key m (kbd "m") #'sndio-mute)
56 (define-key m (kbd "t") #'sndio-toggle)
57 (define-key m (kbd "g") #'sndio-update)
58 (define-key m (kbd "q") #'sndio-quit)
59 m)
60 "Keymap for sndio.")
62 (define-derived-mode sndio-mode special-mode "sndio"
63 "Major mode for sndio interaction."
64 (buffer-disable-undo)
65 (sndio-update))
67 (defun sndio-update ()
68 "Update the current sndio buffer."
69 (interactive)
70 (when (derived-mode-p 'sndio-mode)
71 (let ((inhibit-read-only t))
72 (erase-buffer)
73 (process-file sndio-sndioctl-cmd nil (current-buffer) nil)
74 (goto-char (point-min)))))
76 (defun sndio--run (&rest args)
77 "Run `sndio-sndioctl-cmd' with ARGS yielding its output."
78 (with-temp-buffer
79 (when (zerop (apply #'process-file sndio-sndioctl-cmd nil t nil args))
80 (buffer-string))))
82 (defun sndio--current-io ()
83 "Yield the input/poutput at point as string."
84 (when-let (end (save-excursion
85 (beginning-of-line)
86 (ignore-errors (search-forward "="))))
87 (buffer-substring-no-properties (line-beginning-position)
88 (1- end))))
90 (defun sndio--update-value (x)
91 "Update the value for the input/output at point setting it to X."
92 (save-excursion
93 (beginning-of-line)
94 (search-forward "=")
95 (let ((inhibit-read-only t))
96 (delete-region (point) (line-end-position))
97 (insert (string-trim-right x)))))
99 (defun sndio-increase ()
100 "Increase the volume for the input/output at point."
101 (interactive)
102 (when-let (x (sndio--current-io))
103 (when-let (val (sndio--run "-n" (concat x "=+" (number-to-string sndio-step))))
104 (sndio--update-value val))))
106 (defun sndio-decrease ()
107 "Decrease the volume for the input/output at point."
108 (interactive)
109 (when-let (x (sndio--current-io))
110 (when-let (val (sndio--run "-n" (concat x "=-" (number-to-string sndio-step))))
111 (sndio--update-value val))))
113 (defun sndio-mute ()
114 "Mute the input/output at point."
115 (interactive)
116 (when-let (x (sndio--current-io))
117 (when-let (val (sndio--run "-n" (concat x "=0")))
118 (sndio--update-value val))))
120 (defun sndio-toggle ()
121 "Toggle input/output at point."
122 (interactive)
123 (when-let (x (sndio--current-io))
124 (when-let (val (sndio--run "-n" (concat x "=!")))
125 (sndio--update-value val))))
127 (defun sndio-quit ()
128 "Quits sndio.
129 Call `delete-window' when the sndio popup window is open or
130 `quit-window' otherwise."
131 (interactive)
132 (if (window-live-p sndio--window)
133 (delete-window)
134 (quit-window)))
136 ;;;###autoload
137 (defun sndio ()
138 "Launch sndio."
139 (interactive)
140 (switch-to-buffer "*sndio*")
141 (sndio-mode))
143 ;;;###autoload
144 (defun sndio-win-open ()
145 "Open an sndio window at the bottom of the frame for quick editing."
146 (interactive)
147 (unless (window-live-p sndio--window)
148 (setq sndio--window
149 (select-window
150 (let ((ignore-window-parameters t))
151 (split-window (frame-root-window)
152 -1
153 'below))
154 'norecord))
155 (switch-to-buffer (get-buffer-create "*sndio-quick*")
156 'norecord)
157 (if (derived-mode-p 'sndio-mode)
158 (sndio-update)
159 (sndio-mode))
160 (hl-line-mode +1)
161 (setq mode-line-format nil
162 header-line-format nil
163 tab-line-format nil)
164 (set-window-hscroll sndio--window 0)
165 (set-window-dedicated-p sndio--window t)
166 (select-window sndio--window 'norecord)
167 (let ((window-resize-pixelwise t)
168 (window-size-fixed))
169 (fit-window-to-buffer sndio--window nil nil 1))))
171 (provide 'sndio)
172 ;;; sndio.el ends here