summaryrefslogtreecommitdiff
path: root/src/wasp-overlay.el
blob: b62550e7a529271af97a5050c11898fca388d1b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
;;; wasp-overlay --- Fullscreen overlay -*- lexical-binding: t; -*-
;;; Commentary:
;;; Code:

(require 'wasp-utils)
(require 'wasp-bus-binary)

(defun w/overlay-toggle (effect)
  "Toggle EFFECT in the overlay."
  (w/binary-pub "overlay toggle" effect))
(defun w/overlay-toggle-set (effect)
  "Turn on EFFECT in the overlay."
  (w/binary-pub "overlay toggle set" effect))
(defun w/overlay-toggle-unset (effect)
  "Turn off EFFECT in the overlay."
  (w/binary-pub "overlay toggle unset" effect))

(defun w/overlay-shader (user shader)
  "Set the overlay shader to SHADER by USER."
  (w/binary-pub "overlay shader"
    (s-concat
      (w/bus-binary-build-length-prefixed user)
      (w/bus-binary-build-length-prefixed shader))))

(defun w/overlay-chat (msg)
  "Update the overlay about chat MSG."
  ;; (w/binary-pub "overlay chat"
  ;;   (s-concat
  ;;     (w/bus-binary-build-length-prefixed (w/. user msg))
  ;;     (w/bus-binary-build-length-prefixed (w/. text msg))
  ;;     (w/bus-binary-build-length-prefixed (format "%s" (w/unix-time)))
  ;;     (w/bus-binary-build-length-prefixed (format "%s" (or (w/. biblicality msg) 0.0)))))
  )

(defun w/overlay-muzak (user song)
  "Update the overlay about Muzak SONG played by USER."
  (ignore song)
  (w/binary-pub "overlay info credits music" user))

(defun w/overlay-muzak-clear ()
  "Tell the overlay that there is no Muzak song playing."
  (w/binary-pub "overlay info credits music clear" ""))

(defvar w/overlay-last-cursor nil)
(defun w/overlay-update-cursor ()
  "Inform the overlay about the current cursor position."
  (when (and (process-live-p (get-process w/bus-binary-process)))
    (when-let* ((pos (window-absolute-pixel-position)))
      (when (not (equal pos w/overlay-last-cursor))
        (setf w/overlay-last-cursor pos)
        (w/binary-pub "overlay info emacs cursor"
          (s-concat
            (w/bus-binary-build-length-prefixed (format "%s" (car pos)))
            (w/bus-binary-build-length-prefixed (format "%s" (cdr pos)))))))))
(add-hook 'post-command-hook #'w/overlay-update-cursor)

(defun w/overlay-emacs ()
  "Update the overlay with miscellaneous data from Emacs."
  ;; (w/binary-pub "overlay info emacs" (w/bus-binary-build-int32le (w/heartrate)))
  )

(defun w/overlay-automata (user s &optional color)
  "Send a cellular automata S from USER in RLE format to the overlay.
Optionally, make the cells be COLOR."
  (w/binary-pub "overlay automata spawn"
    (s-concat
      (w/bus-binary-build-length-prefixed s)
      (w/bus-binary-build-length-prefixed user)
      (w/bus-binary-build-int32le (string-to-number (s-chop-prefix "#" (or color (w/random-color))) 16)))))

;;;; Clippy
(defun w/overlay-clippy-animate (anim)
  "Set the Clippy animation to ANIM."
  (w/binary-pub "overlay clippy animate"
    (s-concat
      (w/bus-binary-build-length-prefixed anim))))
(defun w/overlay-clippy-border (border)
  "Set the Clippy border state to BORDER."
  (w/binary-pub
    (if border
      "overlay clippy border on"
      "overlay clippy border off")
    ""))

(cl-defstruct (w/overlay-clippy-event (:constructor w/overlay-clippy-event-new))
  timestamp
  type)
(define-derived-mode w/overlay-clippy-mode special-mode "Karl Klammer"
  "Major mode for displaying Clippy dialogue."
  :group 'wasp
  (setq mode-line-format nil))
(defface w/overlay-clippy-face
  '((t
      :foreground "black"
      :font "Terminus (TTF):antialias=false:hinting=false:pixelsize=24"
      ))
  "Face for Clippy dialogue."
  :group 'wasp)
(defconst w/overlay-clippy-frame-buffer-name " *clippy-dialogue*")
(defvar w/overlay-clippy-frame-background-color "#ffffcc")
(defvar w/overlay-clippy-frame nil)
(defvar w/overlay-clippy-last-event nil)
(defvar w/overlay-clippy-speech-timer nil)
(defun w/overlay-clippy-frame-buffer ()
  "Return the buffer used to display Clippy dialogue."
  (let ((name w/overlay-clippy-frame-buffer-name))
    (unless (get-buffer name)
      (with-current-buffer (get-buffer-create name)
        (w/overlay-clippy-mode)))
    (get-buffer name)))
(defun w/overlay-clippy-frame-create ()
  "Build a frame for displaying completion candidates."
  (when (framep w/overlay-clippy-frame)
    (delete-frame w/overlay-clippy-frame))
  (setf
    w/overlay-clippy-frame
    (make-frame
      (append
        `((name . "clippy")
           (unsplittable . t)
           (undecorated . t)
           (no-accept-focus . t)
           (no-focus-on-map . t)
           (override-redirect . t)
           (user-size . t)
           (width . 25)
           (height . 8)
           (user-position . t)
           (left . 1600)
           (top . 550)
           (default-minibuffer-frame . ,(selected-frame))
           (minibuffer . nil)
           (left-fringe . 0)
           (right-fringe . 0)
           (cursor-type . nil)
           (background-color . ,w/overlay-clippy-frame-background-color)))))
  (make-frame-invisible w/overlay-clippy-frame)
  (let ((window (frame-selected-window w/overlay-clippy-frame)))
    (set-window-buffer window (w/overlay-clippy-frame-buffer))))
(w/overlay-clippy-frame-create)
(defun w/overlay-clippy-clear ()
  "Clear the Clippy buffer."
  (when w/overlay-clippy-frame
    (modify-frame-parameters w/overlay-clippy-frame
      `((background-color . ,w/overlay-clippy-frame-background-color))))
  (with-current-buffer (w/overlay-clippy-frame-buffer)
    (let ((inhibit-read-only t))
      (erase-buffer))))
(defun w/overlay-clippy-write (msg)
  "Write MSG to the Clippy buffer."
  (when w/overlay-clippy-frame
    (modify-frame-parameters w/overlay-clippy-frame
      `((background-color . ,w/overlay-clippy-frame-background-color))))
  (let ((window (frame-selected-window w/overlay-clippy-frame)))
    (set-window-buffer window (w/overlay-clippy-frame-buffer)))
  (with-current-buffer (w/overlay-clippy-frame-buffer)
    (let ((inhibit-read-only t))
      (w/write-line msg 'w/overlay-clippy-face))))
(defun w/overlay-clippy-say (msg)
  "Have Clippy say MSG."
  (w/overlay-clippy-clear)
  (w/overlay-clippy-write msg)
  (w/overlay-clippy-border t)
  (w/show-frame w/overlay-clippy-frame t)
  (raise-frame w/overlay-clippy-frame)
  (w/overlay-clippy-animate "EXPLAIN")
  (setf w/overlay-clippy-speech-timer 10))

(defun w/overlay-clippy-check-events ()
  "Check for events that Clippy should respond to."
  )

(defun w/overlay-clippy-update ()
  "Update Clippy."
  (when w/overlay-clippy-speech-timer
    (cl-decf w/overlay-clippy-speech-timer)
    (when (<= w/overlay-clippy-speech-timer 0)
      (w/overlay-clippy-border nil)
      (w/show-frame w/overlay-clippy-frame nil)
      (setf w/overlay-clippy-speech-timer nil)))
  (w/overlay-clippy-check-events))
(defvar w/overlay-clippy-timer nil)
(defun w/overlay-clippy-timer-run ()
  "Run the gizmo update timer."
  (when w/overlay-clippy-timer
    (cancel-timer w/overlay-clippy-timer))
  (w/overlay-clippy-update)
  (setq
    w/overlay-clippy-timer
    (run-with-timer 1 nil #'w/overlay-clippy-timer-run)))

(provide 'wasp-overlay)
;;; wasp-overlay.el ends here