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/sndioctl.el
10 ;;; Commentary:
12 ;; This package provides the sndio major mode to interact with
13 ;; OpenBSD' sndio(8).
15 ;;; Code:
17 (require 'cl-lib)
19 (defvar sndioctl-cmd "sndioctl"
20 "Path to the sndioctl executable.")
22 (defvar sndio-step 0.02
23 "Step for `sndio-increase' and `sndio-decrease'.")
25 (define-derived-mode sndio-mode special-mode "sndio"
26 "Major mode for sndio interaction."
28 (define-key sndio-mode-map (kbd "n") #'forward-line)
29 (define-key sndio-mode-map (kbd "p") #'previous-line)
30 (define-key sndio-mode-map (kbd "i") #'sndio-increase)
31 (define-key sndio-mode-map (kbd "d") #'sndio-decrease)
32 (define-key sndio-mode-map (kbd "m") #'sndio-mute)
33 (define-key sndio-mode-map (kbd "t") #'sndio-toggle)
34 (define-key sndio-mode-map (kbd "g") #'sndio-update)
36 (buffer-disable-undo)
38 (sndio-update))
40 (defun sndio-update ()
41 "Update the current sndio buffer."
42 (interactive)
43 (with-current-buffer "*sndio*"
44 (save-excursion
45 (let ((inhibit-read-only t))
46 (erase-buffer)
47 (process-file sndioctl-cmd nil (current-buffer) nil)))))
49 (defun sndio--run (&rest args)
50 "Run `sndioctl-cmd' with ARGS yielding its output."
51 (with-temp-buffer
52 (when (zerop (apply #'process-file sndioctl-cmd nil t nil args))
53 (buffer-string))))
55 (defun sndio--current-io ()
56 "Yield the input/poutput at point as string."
57 (when-let (end (save-excursion
58 (beginning-of-line)
59 (ignore-errors (search-forward "="))))
60 (buffer-substring-no-properties (line-beginning-position)
61 (1- end))))
63 (defun sndio--update-value (x)
64 "Update the value for the input/output at point setting it to X."
65 (save-excursion
66 (search-forward "=")
67 (let ((inhibit-read-only t))
68 (delete-region (point) (line-end-position))
69 (insert (string-trim-right x)))))
71 (defun sndio-increase ()
72 "Increase the volume for the input/output at point."
73 (interactive)
74 (when-let (x (sndio--current-io))
75 (when-let (val (sndio--run "-n" (concat x "=+" (number-to-string sndio-step))))
76 (sndio--update-value val))))
78 (defun sndio-decrease ()
79 "Decrease the volume for the input/output at point."
80 (interactive)
81 (when-let (x (sndio--current-io))
82 (when-let (val (sndio--run "-n" (concat x "=-" (number-to-string sndio-step))))
83 (sndio--update-value val))))
85 (defun sndio-mute ()
86 "Mute the input/output at point."
87 (interactive)
88 (when-let (x (sndio--current-io))
89 (when-let (val (sndio--run "-n" (concat x "=0")))
90 (sndio--update-value val))))
92 (defun sndio-toggle ()
93 "Toggle input/output at point."
94 (interactive)
95 (when-let (x (sndio--current-io))
96 (when-let (val (sndio--run "-n" (concat x "=!")))
97 (sndio--update-value val))))
99 ;;;###autoload
100 (defun sndio ()
101 "Launch sndio."
102 (interactive)
103 (switch-to-buffer "*sndio*")
104 (sndio-mode))
106 (provide 'sndio)
107 ;;; sndio.el ends here.