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)
35 (define-key sndio-mode-map (kbd "d") #'kill-buffer)
37 (buffer-disable-undo)
39 (sndio-update))
41 (defun sndio-update ()
42 "Update the current sndio buffer."
43 (interactive)
44 (with-current-buffer "*sndio*"
45 (save-excursion
46 (let ((inhibit-read-only t))
47 (erase-buffer)
48 (process-file sndioctl-cmd nil (current-buffer) nil)))))
50 (defun sndio--run (&rest args)
51 "Run `sndioctl-cmd' with ARGS yielding its output."
52 (with-temp-buffer
53 (when (zerop (apply #'process-file 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 (search-forward "=")
68 (let ((inhibit-read-only t))
69 (delete-region (point) (line-end-position))
70 (insert (string-trim-right x)))))
72 (defun sndio-increase ()
73 "Increase the volume for the input/output at point."
74 (interactive)
75 (when-let (x (sndio--current-io))
76 (when-let (val (sndio--run "-n" (concat x "=+" (number-to-string sndio-step))))
77 (sndio--update-value val))))
79 (defun sndio-decrease ()
80 "Decrease the volume for the input/output at point."
81 (interactive)
82 (when-let (x (sndio--current-io))
83 (when-let (val (sndio--run "-n" (concat x "=-" (number-to-string sndio-step))))
84 (sndio--update-value val))))
86 (defun sndio-mute ()
87 "Mute the input/output at point."
88 (interactive)
89 (when-let (x (sndio--current-io))
90 (when-let (val (sndio--run "-n" (concat x "=0")))
91 (sndio--update-value val))))
93 (defun sndio-toggle ()
94 "Toggle input/output at point."
95 (interactive)
96 (when-let (x (sndio--current-io))
97 (when-let (val (sndio--run "-n" (concat x "=!")))
98 (sndio--update-value val))))
100 ;;;###autoload
101 (defun sndio ()
102 "Launch sndio."
103 (interactive)
104 (switch-to-buffer "*sndio*")
105 (sndio-mode))
107 (provide 'sndio)
108 ;;; sndio.el ends here.