Blame


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