Blob


1 ;;; sndio.el --- Interact with sndio(8) -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2020 Omar Polo
5 ;; Author: Omar Polo <op@omarpolo.com>
6 ;; Version: 1.0
7 ;; Keywords: multimedia
8 ;; URL: https://git.omarpolo.com/sndio.el
10 ;;; Commentary:
12 ;; This package provides the sndio major mode to interact with
13 ;; OpenBSD' sndio(8).
15 ;;; Code:
17 (eval-when-compile (require 'subr-x))
19 (defvar sndio-sndioctl-cmd "sndioctl"
20 "Path to the sndioctl executable.")
22 (defvar sndio-step 0.02
23 "Step for `sndio-increase' and `sndio-decrease'.")
25 (defvar sndio-mode-map
26 (let ((m (make-sparse-keymap)))
27 (define-key m (kbd "n") #'forward-line)
28 (define-key m (kbd "p") #'previous-line)
29 (define-key m (kbd "i") #'sndio-increase)
30 (define-key m (kbd "d") #'sndio-decrease)
31 (define-key m (kbd "m") #'sndio-mute)
32 (define-key m (kbd "t") #'sndio-toggle)
33 (define-key m (kbd "g") #'sndio-update)
34 m)
35 "Keymap for sndio.")
37 (define-derived-mode sndio-mode special-mode "sndio"
38 "Major mode for sndio interaction."
39 (buffer-disable-undo)
40 (sndio-update))
42 (defun sndio-update ()
43 "Update the current sndio buffer."
44 (interactive)
45 (with-current-buffer "*sndio*"
46 (let ((inhibit-read-only t))
47 (erase-buffer)
48 (process-file sndio-sndioctl-cmd nil (current-buffer) nil))))
50 (defun sndio--run (&rest args)
51 "Run `sndio-sndioctl-cmd' with ARGS yielding its output."
52 (with-temp-buffer
53 (when (zerop (apply #'process-file sndio-sndioctl-cmd nil t nil args))
54 (buffer-string))))
56 (defun sndio--current-io ()
57 "Yield the input/poutput at point as string."
58 (when-let (end (save-excursion
59 (beginning-of-line)
60 (ignore-errors (search-forward "="))))
61 (buffer-substring-no-properties (line-beginning-position)
62 (1- end))))
64 (defun sndio--update-value (x)
65 "Update the value for the input/output at point setting it to X."
66 (save-excursion
67 (beginning-of-line)
68 (search-forward "=")
69 (let ((inhibit-read-only t))
70 (delete-region (point) (line-end-position))
71 (insert (string-trim-right x)))))
73 (defun sndio-increase ()
74 "Increase the volume for the input/output at point."
75 (interactive)
76 (when-let (x (sndio--current-io))
77 (when-let (val (sndio--run "-n" (concat x "=+" (number-to-string sndio-step))))
78 (sndio--update-value val))))
80 (defun sndio-decrease ()
81 "Decrease the volume for the input/output at point."
82 (interactive)
83 (when-let (x (sndio--current-io))
84 (when-let (val (sndio--run "-n" (concat x "=-" (number-to-string sndio-step))))
85 (sndio--update-value val))))
87 (defun sndio-mute ()
88 "Mute the input/output at point."
89 (interactive)
90 (when-let (x (sndio--current-io))
91 (when-let (val (sndio--run "-n" (concat x "=0")))
92 (sndio--update-value val))))
94 (defun sndio-toggle ()
95 "Toggle input/output at point."
96 (interactive)
97 (when-let (x (sndio--current-io))
98 (when-let (val (sndio--run "-n" (concat x "=!")))
99 (sndio--update-value val))))
101 ;;;###autoload
102 (defun sndio ()
103 "Launch sndio."
104 (interactive)
105 (switch-to-buffer "*sndio*")
106 (sndio-mode))
108 (provide 'sndio)
109 ;;; sndio.el ends here