Advertisement
6clk4mW8KYjKb5Af

Roku controlled by Emacs-lisp

Jul 11th, 2021
2,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 3.86 KB | None | 0 0
  1. (defun roku-key-to-command (key)
  2.   (cond ((eq key 'home) "Home")
  3.         ((eq key 'reverse) "Rev")
  4.         ((eq key 'forward) "Fwd")
  5.         ((eq key 'play) "Play")
  6.         ((eq key 'select) "Select")
  7.         ((eq key 'left) "Left")
  8.         ((eq key 'right) "Right")
  9.         ((eq key 'down) "Down")
  10.         ((eq key 'up) "Up")
  11.         ((eq key 'back) "Back")
  12.         ((eq key 'instant-replay) "InstantReplay")
  13.         ((eq key 'info) "Info")
  14.         ((eq key 'backspace) "Backspace")
  15.         ((eq key 'search) "Search")
  16.         ((eq key 'enter) "Enter")
  17.         ((eq key 'power-on) "PowerOn")
  18.         ((eq key 'power-off) "PowerOff")
  19.         ((eq key 'volume-up) "VolumeUp")
  20.         ((eq key 'volume-down) "VolumeDown")
  21.         ((eq key 'volume-mute) "VolumeMute")
  22.         (t (url-hexify-string (format "Lit_%c" key)))))
  23.  
  24. (defun roku-make-keypress-url (host key)
  25.   (concat "http://" host ":8060/keypress/" (roku-key-to-command key)))
  26.  
  27. (defun roku-callback (x)
  28.   (when x
  29.     (message "%s" x)))
  30.  
  31. (defun roku-send-keypress (host key)
  32.   (let ((url-request-method "POST")
  33.         (url-request-data ""))
  34.     (url-retrieve (roku-make-keypress-url host key) #'roku-callback)))
  35.  
  36. ;;; Tied to host
  37.  
  38. (defvar roku-current-host nil)
  39.  
  40. (defun roku-run (host)
  41.   "Initiate transient state."
  42.   (setq roku-current-host host)
  43.   (hydra-roku/body))
  44.  
  45. (defmacro roku-make-command (command)
  46.   `(defun ,(intern (concat "roku-current-send-" (symbol-name command))) ()
  47.      (interactive)
  48.      (roku-send-keypress roku-current-host ',command)))
  49.  
  50. (roku-make-command back)
  51. (roku-make-command home)
  52. (roku-make-command info)
  53. (roku-make-command instant-replay)
  54.  
  55. (roku-make-command play)
  56. (roku-make-command search)
  57. (roku-make-command select)
  58.  
  59. (roku-make-command backspace)
  60. (roku-make-command enter)
  61.  
  62. (roku-make-command forward)
  63. (roku-make-command reverse)
  64.  
  65. (roku-make-command up)
  66. (roku-make-command down)
  67. (roku-make-command left)
  68. (roku-make-command right)
  69.  
  70. (roku-make-command power-on)
  71. (roku-make-command power-off)
  72.  
  73. (roku-make-command volume-up)
  74. (roku-make-command volume-down)
  75. (roku-make-command volume-mute)
  76.  
  77. (defun roku-send-text-sender (str-to-send pos)
  78.   (let ((url-request-method "POST")
  79.         (url-request-data ""))
  80.     (cond ((< pos (length str-to-send))
  81.            (let ((url (roku-make-keypress-url roku-current-host (elt str-to-send pos))))
  82.              (url-retrieve url
  83.                            (lambda (_status)
  84.                              (run-at-time 1 nil (lambda ()
  85.                                                   (roku-send-text-sender str-to-send (1+ pos))))))))
  86.           (t
  87.            (roku-current-send-enter)))))
  88.  
  89. (defun roku-send-text ()
  90.   (interactive)
  91.   (let ((s (read-string "Send text: ")))
  92.     (when (stringp s)
  93.       (message "%s" s)
  94.       (roku-send-text-sender s 0))
  95.     t))
  96.  
  97. (defhydra hydra-roku (:hint nil)
  98.   "
  99. [_h_] left [_j_] down [_k_] up [_l_] right
  100. [_<return>_] ok [_SPC_] play [_gh_] home [_<backspace>_] backspace
  101. [_H_] back [_i_] info [_m_] instant replay [_s_] search
  102. [_,_] reverse [_._] forward [_t_] enter text [_q_] quit
  103. [_p_] power off [_o_] power on [_0_] mute [_-_] vol down [_=_] vol up"
  104.   ("q" nil)
  105.  
  106.   ("0" roku-current-send-volume-mute)
  107.   ("-" roku-current-send-volume-down)
  108.   ("=" roku-current-send-volume-up)
  109.  
  110.   ("p" roku-current-send-power-off)
  111.   ("o" roku-current-send-power-on)
  112.  
  113.   ("h" roku-current-send-left)
  114.   ("l" roku-current-send-right)
  115.   ("j" roku-current-send-down)
  116.   ("k" roku-current-send-up)
  117.  
  118.   ("<backspace>" roku-current-send-backspace)
  119.  
  120.   ("H" roku-current-send-back)
  121.  
  122.   ("<return>" roku-current-send-select)
  123.   ("SPC" roku-current-send-play)
  124.  
  125.   ("gh" roku-current-send-home)
  126.   ("i" roku-current-send-info)
  127.   ("m" roku-current-send-instant-replay)
  128.   ("s" roku-current-send-search)
  129.  
  130.   ("," roku-current-send-reverse)
  131.   ("." roku-current-send-forward)
  132.   ("t" roku-send-text))
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement