Skip to content

Instantly share code, notes, and snippets.

@kriyative
Created February 1, 2019 05:28
Show Gist options
  • Save kriyative/bf5189a07763f79f23605e671a82f7dc to your computer and use it in GitHub Desktop.
Save kriyative/bf5189a07763f79f23605e671a82f7dc to your computer and use it in GitHub Desktop.
An emacs lisp package for enabling/disabling X inputs by symbolic reference
(defun set-inputs-parse-devices ()
(let ((dev-re (concat
"^[^A-Za-z]*\\([A-Za-z][-A-Za-z0-9/:,_ ]+"
"[-A-Za-z0-9/:,_]\\)"
"[ \t]*id=\\([0-9]+\\)"
"[ \t]*\\[\\(\\w+\\)"
"[ \t]*\\(\\w+\\)"))
devices)
(with-temp-buffer
(call-process "xinput" nil t)
(goto-char (point-min))
(while (re-search-forward dev-re nil t)
(push (list :name (match-string 1)
:id (match-string 2)
:class (match-string 3)
:type (match-string 4))
devices))
devices)))
(defun match (item matchers &keys key)
(cl-find-if (lambda (matcher)
(let ((value (funcall (or key 'identity) item)))
(cond
((stringp matcher) (string-match matcher value))
((functionp matcher) (funcall matcher value))
(t (equal matcher value)))))
matchers))
(defun set-inputs-find-devices (device-patterns)
(cl-remove-if-not (lambda (device)
(match device device-patterns
:key (lambda (p) (plist-get p :name))))
(set-inputs-parse-devices)))
(defun set-inputs-enabled (device-patterns bool)
(dolist (device (set-inputs-find-devices device-patterns))
(call-process "xinput"
nil
nil
nil
(if bool "--enable" "--disable")
(plist-get device :id))))
(defvar set-inputs-devices
'("AT Translated Set 2 keyboard"
"ThinkPad Extra Buttons"
"SynPS/2 Synaptics TouchPad"
"TPPS/2 IBM TrackPoint"))
(defun enable-input-devices ()
(interactive)
(set-inputs-enabled set-inputs-devices t))
(defun disable-input-devices ()
(interactive)
(set-inputs-enabled set-inputs-devices nil))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment