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 b24cd761 2020-12-28 op (process-file sndio-sndioctl-cmd nil (current-buffer) nil))))
49 c708ecad 2020-12-19 op
50 c708ecad 2020-12-19 op (defun sndio--run (&rest args)
51 b24cd761 2020-12-28 op "Run `sndio-sndioctl-cmd' with ARGS yielding its output."
52 c708ecad 2020-12-19 op (with-temp-buffer
53 b24cd761 2020-12-28 op (when (zerop (apply #'process-file sndio-sndioctl-cmd nil t nil args))
54 c708ecad 2020-12-19 op (buffer-string))))
55 c708ecad 2020-12-19 op
56 c708ecad 2020-12-19 op (defun sndio--current-io ()
57 c708ecad 2020-12-19 op "Yield the input/poutput at point as string."
58 c708ecad 2020-12-19 op (when-let (end (save-excursion
59 c708ecad 2020-12-19 op (beginning-of-line)
60 c708ecad 2020-12-19 op (ignore-errors (search-forward "="))))
61 c708ecad 2020-12-19 op (buffer-substring-no-properties (line-beginning-position)
62 c708ecad 2020-12-19 op (1- end))))
63 c708ecad 2020-12-19 op
64 c708ecad 2020-12-19 op (defun sndio--update-value (x)
65 c708ecad 2020-12-19 op "Update the value for the input/output at point setting it to X."
66 c708ecad 2020-12-19 op (save-excursion
67 f05c8ffa 2020-12-20 op (beginning-of-line)
68 c708ecad 2020-12-19 op (search-forward "=")
69 c708ecad 2020-12-19 op (let ((inhibit-read-only t))
70 c708ecad 2020-12-19 op (delete-region (point) (line-end-position))
71 c708ecad 2020-12-19 op (insert (string-trim-right x)))))
72 c708ecad 2020-12-19 op
73 c708ecad 2020-12-19 op (defun sndio-increase ()
74 c708ecad 2020-12-19 op "Increase the volume for the input/output at point."
75 c708ecad 2020-12-19 op (interactive)
76 c708ecad 2020-12-19 op (when-let (x (sndio--current-io))
77 c708ecad 2020-12-19 op (when-let (val (sndio--run "-n" (concat x "=+" (number-to-string sndio-step))))
78 c708ecad 2020-12-19 op (sndio--update-value val))))
79 c708ecad 2020-12-19 op
80 c708ecad 2020-12-19 op (defun sndio-decrease ()
81 c708ecad 2020-12-19 op "Decrease the volume for the input/output at point."
82 c708ecad 2020-12-19 op (interactive)
83 c708ecad 2020-12-19 op (when-let (x (sndio--current-io))
84 c708ecad 2020-12-19 op (when-let (val (sndio--run "-n" (concat x "=-" (number-to-string sndio-step))))
85 c708ecad 2020-12-19 op (sndio--update-value val))))
86 c708ecad 2020-12-19 op
87 c708ecad 2020-12-19 op (defun sndio-mute ()
88 c708ecad 2020-12-19 op "Mute the input/output at point."
89 c708ecad 2020-12-19 op (interactive)
90 c708ecad 2020-12-19 op (when-let (x (sndio--current-io))
91 c708ecad 2020-12-19 op (when-let (val (sndio--run "-n" (concat x "=0")))
92 c708ecad 2020-12-19 op (sndio--update-value val))))
93 c708ecad 2020-12-19 op
94 c708ecad 2020-12-19 op (defun sndio-toggle ()
95 c708ecad 2020-12-19 op "Toggle input/output at point."
96 c708ecad 2020-12-19 op (interactive)
97 c708ecad 2020-12-19 op (when-let (x (sndio--current-io))
98 c708ecad 2020-12-19 op (when-let (val (sndio--run "-n" (concat x "=!")))
99 c708ecad 2020-12-19 op (sndio--update-value val))))
100 c708ecad 2020-12-19 op
101 c708ecad 2020-12-19 op ;;;###autoload
102 c708ecad 2020-12-19 op (defun sndio ()
103 c708ecad 2020-12-19 op "Launch sndio."
104 c708ecad 2020-12-19 op (interactive)
105 c708ecad 2020-12-19 op (switch-to-buffer "*sndio*")
106 c708ecad 2020-12-19 op (sndio-mode))
107 c708ecad 2020-12-19 op
108 c708ecad 2020-12-19 op (provide 'sndio)
109 b24cd761 2020-12-28 op ;;; sndio.el ends here