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 (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
41 "The sndio window.")
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)
53 m)
54 "Keymap for sndio.")
56 (define-derived-mode sndio-mode special-mode "sndio"
57 "Major mode for sndio interaction."
58 (buffer-disable-undo)
59 (sndio-update))
61 (defun sndio-update ()
62 "Update the current sndio buffer."
63 (interactive)
64 (when (derived-mode-p 'sndio-mode)
65 (let ((inhibit-read-only t))
66 (erase-buffer)
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."
72 (with-temp-buffer
73 (when (zerop (apply #'process-file sndio-sndioctl-cmd nil t nil args))
74 (buffer-string))))
76 (defun sndio--current-io ()
77 "Yield the input/poutput at point as string."
78 (when-let (end (save-excursion
79 (beginning-of-line)
80 (ignore-errors (search-forward "="))))
81 (buffer-substring-no-properties (line-beginning-position)
82 (1- end))))
84 (defun sndio--update-value (x)
85 "Update the value for the input/output at point setting it to X."
86 (save-excursion
87 (beginning-of-line)
88 (search-forward "=")
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."
95 (interactive)
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."
102 (interactive)
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))))
107 (defun sndio-mute ()
108 "Mute the input/output at point."
109 (interactive)
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."
116 (interactive)
117 (when-let (x (sndio--current-io))
118 (when-let (val (sndio--run "-n" (concat x "=!")))
119 (sndio--update-value val))))
121 (defun sndio-quit ()
122 "Quits sndio.
123 Call `delete-window' when the sndio popup window is open or
124 `quit-window' otherwise."
125 (interactive)
126 (if (window-live-p sndio--window)
127 (delete-window)
128 (quit-window)))
130 ;;;###autoload
131 (defun sndio ()
132 "Launch sndio."
133 (interactive)
134 (switch-to-buffer "*sndio*")
135 (sndio-mode))
137 ;;;###autoload
138 (defun sndio-win-open ()
139 "Open an sndio window at the bottom of the frame for quick editing."
140 (interactive)
141 (unless (window-live-p sndio--window)
142 (setq sndio--window
143 (select-window
144 (let ((ignore-window-parameters t))
145 (split-window (frame-root-window)
146 -1
147 'below))
148 'norecord))
149 (switch-to-buffer (get-buffer-create "*sndio-quick*")
150 'norecord)
151 (if (derived-mode-p 'sndio-mode)
152 (sndio-update)
153 (sndio-mode))
154 (hl-line-mode +1)
155 (setq mode-line-format nil
156 header-line-format nil
157 tab-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)
162 (window-size-fixed))
163 (fit-window-to-buffer sndio--window nil nil 1))))
165 (provide 'sndio)
166 ;;; sndio.el ends here