-
Notifications
You must be signed in to change notification settings - Fork 2
/
graphic-forms-server.lisp
359 lines (306 loc) · 12.6 KB
/
graphic-forms-server.lisp
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
(in-package :clim-graphic-forms)
(defvar *graphic-forms-server* nil)
(defvar *graphic-forms-server-command-queue*
(lparallel.queue:make-queue))
(defvar *graphic-forms-server-event-queue*
(lparallel.queue:make-queue))
(defvar *graphic-forms-mouse-down-sheet* nil
"Windows' mouse-up event is different with X's mouse-release event that the window mouse-up happens is the window
that user release the mouse, in X it's always the previous window mouse-press (mouse-down) event happens. So we need
to track this information manually.")
(defparameter *graphic-forms-server-debug* t)
;;; Debugging utils
;; This must be set, otherwise because thread bindings, directly print to *standard-output* will not display in SLIME.
(defparameter *graphic-forms-server-debug-output* *standard-output*)
(defparameter *debug-print-lock* (bt:make-recursive-lock "DEBUG-PRINT-LOCK"))
(defun debug-print(&rest args)
(bt:with-recursive-lock-held (*debug-print-lock*)
(when *graphic-forms-server-debug*
(format *graphic-forms-server-debug-output* "~{~a~%~}" args))))
(defun debug-prin1 (&rest args)
(bt:with-recursive-lock-held (*debug-print-lock*)
(when *graphic-forms-server-debug*
(format *graphic-forms-server-debug-output* "~{~a ~}~%" args))))
(defun start-graphic-forms-server ()
(or *graphic-forms-server*
(setf *graphic-forms-server*
(bt:make-thread 'graphic-forms-server-loop :name "GRAPHIC-FORMS-SERVER"))))
(defun empty-queue (queue)
(loop
(if (lparallel.queue:queue-empty-p queue)
(return)
(lparallel.queue:pop-queue queue))))
(defun close-graphic-forms-server ()
(when *graphic-forms-server*
(bt:destroy-thread *graphic-forms-server*)
(setf *graphic-forms-server* nil)
(empty-queue *graphic-forms-server-command-queue*)
(empty-queue *graphic-forms-server-event-queue*)))
(defun restart-graphic-forms-server ()
(close-graphic-forms-server)
(start-graphic-forms-server))
(defun graphic-forms-server-loop ()
(cffi:with-foreign-object (msg-ptr '(:struct gfs::msg))
(loop
(let ((gm (gfs::peek-message msg-ptr (cffi:null-pointer) 0 0 (logior gfs::+pm-noyield+ gfs::+pm-remove+)))
(command-and-promise (graphic-forms-server-listen)))
(when command-and-promise
(graphic-forms-server-read)
(lparallel:fulfill (cdr command-and-promise)
(process-command (car command-and-promise))))
(when (/= gm 0)
(cffi:with-foreign-slots ((gfs::message gfs::wparam) msg-ptr (:struct gfs::msg))
(when (funcall 'gfw:default-message-filter gm msg-ptr)
(return-from graphic-forms-server-loop gfs::wparam))))))))
(defun graphic-forms-server-listen ()
(lparallel.queue:peek-queue *graphic-forms-server-command-queue*))
(defun graphic-forms-server-read ()
(lparallel.queue:pop-queue *graphic-forms-server-command-queue*))
(defun send-to-graphic-forms-server (command)
(assert *graphic-forms-server*)
(let ((p (lparallel:promise)))
(lparallel.queue:push-queue (cons command p) *graphic-forms-server-command-queue*)
p))
(defun send-to-graphic-forms-server/blocked (command)
(lparallel:force (send-to-graphic-forms-server command)))
(defmacro <- (command)
`(send-to-graphic-forms-server ,command))
(defmacro <+ (command)
`(send-to-graphic-forms-server/blocked ,command))
;;; TODO: need to examine command and recover from error
;;; There must be no blocked command send in process-command
(defun process-command (command)
(case (first command)
(gfw:show
(gfw:show (second command) (third command)))
(otherwise (let ((result (eval command)))
result))))
(defmacro with-server-graphics-context ((gc &optional thing) &body body)
`(let ((,gc (<+ `(gfg:make-graphics-context ,,thing))))
(unwind-protect
(progn
,@body)
(<- `(gfs:dispose ,,gc)))))
(defmacro with-server-root-window ((win) &body body)
`(let ((,win (<+ `(make-instance 'gfw:root-window))))
(unwind-protect
(progn
,@body)
(<- `(gfs:dispose ,,win)))))
(start-graphic-forms-server)
(defun server-add-event (event)
(setf (slot-value event 'climi::timestamp) (gfw:obtain-event-time))
(lparallel.queue:push-queue event *graphic-forms-server-event-queue*))
(defun server-get-event ()
(lparallel.queue:pop-queue *graphic-forms-server-event-queue*))
(defclass gf-mirror-mixin ()
((sheet
:accessor sheet
:initarg :sheet
:initform nil))
(:documentation "Server side graphic-forms object"))
(defclass gfw-top-level (gfw:top-level gf-mirror-mixin) ()
(:documentation "Server side top level window"))
(defclass gfw-panel (gfw:panel gf-mirror-mixin) ()
(:documentation "Server side gadgets"))
(defclass gfw-window (gfw:window gf-mirror-mixin) ()
(:documentation "Server side window for unmanaged-top-level-sheet-panes"))
(defclass sheet-event-dispatcher (gfw:event-dispatcher)
((port
:accessor port
:initform nil)
(last-pointer-position
:accessor last-pointer-position
:initform nil))
(:documentation "Server side windows native event handler"))
(defvar *sheet-dispatcher* (<+ `(make-instance 'sheet-event-dispatcher)))
(setf (last-pointer-position *sheet-dispatcher*) (<+ `(gfs:make-point)))
;;;
;;; dispatchers and callbacks
;;;
(defmethod gfw:event-close ((self sheet-event-dispatcher) mirror)
(server-add-event
(make-instance 'window-manager-delete-event :sheet (sheet mirror))))
;; copy&paste from port.lisp|CLX:
(defun sheet-desired-ink (sheet)
(typecase sheet
(sheet-with-medium-mixin
(medium-background sheet))
(basic-pane
;; CHECKME [is this sensible?] seems to be
(let ((background (pane-background sheet)))
(if (typep background 'color)
background
+white+)))
(t
+white+)))
;;; This function should only be called in graphics-forms-server thread
(defun ink-to-color (medium ink)
(cond
((subtypep (class-of ink) (find-class 'climi::opacity))
(setf ink (medium-foreground medium))) ; see discussion of opacity in design.lisp
((eql ink +foreground-ink+)
(setf ink (medium-foreground medium)))
((eql ink +background-ink+)
(setf ink (medium-background medium))))
(%ink-to-color ink))
(defun %ink-to-color (ink)
(multiple-value-bind (red green blue) (clim:color-rgb ink)
(gfg:make-color :red (min (truncate (* red 256)) 255)
:green (min (truncate (* green 256)) 255)
:blue (min (truncate (* blue 256)) 255))))
(defmethod gfw:event-paint ((self sheet-event-dispatcher) mirror gc rect)
(let ((sheet (sheet mirror)))
;; (when (and (typep sheet 'sheet-with-medium-mixin)
;; ; (not (image-of (sheet-medium sheet)))
;; )
;; (let ((c (ink-to-color (sheet-medium sheet)
;; (sheet-desired-ink sheet))))
;; (setf (gfg:background-color gc) c
;; (gfg:foreground-color gc) c))
;; (gfg:draw-filled-rectangle gc rect))
(server-add-event
(make-instance 'window-repaint-event
:sheet sheet
:region (translate-rectangle rect)))
;; (distribute-event (port sheet) (make-instance 'window-repaint-event
;; :sheet sheet
;; :region (translate-rectangle rect)))
))
;;; This function should only be called in graphics-forms-server thread
(defun generate-configuration-event (mirror pnt size)
(make-instance 'window-configuration-event
:sheet (sheet mirror)
:x (gfs:point-x pnt)
:y (gfs:point-y pnt)
:width (gfs:size-width size)
:height (gfs:size-height size)))
(defmethod gfw:event-resize ((self sheet-event-dispatcher) mirror size type)
(declare (ignore type))
(setf size (gfw:client-size mirror))
(let ((sheet (sheet mirror)))
(if (and sheet (subtypep (class-of sheet) 'sheet-with-medium-mixin))
(let ((medium (climi::sheet-medium sheet)))
(when (and medium (image-of medium))
(resize-medium-buffer medium size)))))
(server-add-event
(generate-configuration-event mirror (gfw:location mirror) size)))
(defmethod gfw:event-move ((self sheet-event-dispatcher) mirror pnt)
(server-add-event
(generate-configuration-event mirror pnt (gfw:client-size mirror))))
(defun translate-button-name (name)
(case name
(:left-button +pointer-left-button+)
(:right-button +pointer-right-button+)
(:middle-button +pointer-middle-button+)
((nil) nil)
(t
(warn "unknown button name: ~A" name)
nil)))
(defun mouse-actually-move (last-pos new-pos)
(unless (gfs:equal-point-p last-pos new-pos)
t))
(defmethod gfw:event-mouse-move
((self sheet-event-dispatcher) mirror point button)
(when (mouse-actually-move (last-pointer-position self) point)
(setf (gfs:point-x (last-pointer-position self)) (gfs:point-x point)
(gfs:point-y (last-pointer-position self)) (gfs:point-y point))
(server-add-event
(make-instance 'pointer-motion-event
:pointer 0
:sheet (sheet mirror)
:x (gfs:point-x point)
:y (gfs:point-y point)
:button (translate-button-name button)
;; FIXME:
;;; :graft-x
;;; :graft-y
:modifier-state 0))))
(defmethod gfw:event-mouse-down ((self sheet-event-dispatcher) mirror point button)
(setf *graphic-forms-mouse-down-sheet* (sheet mirror))
(server-add-event
(make-instance 'pointer-button-press-event
:pointer 0
:sheet *graphic-forms-mouse-down-sheet*
:x (gfs:point-x point)
:y (gfs:point-y point)
:button (translate-button-name button)
;; FIXME:
;;; :graft-x
;;; :graft-y
:modifier-state 0)))
(defmethod gfw:event-mouse-up ((self sheet-event-dispatcher) mirror point button)
(server-add-event
(make-instance 'pointer-button-release-event
:pointer 0
:sheet *graphic-forms-mouse-down-sheet*
:x (gfs:point-x point)
:y (gfs:point-y point)
:button (translate-button-name button)
;; FIXME:
;;; :graft-x
;;; :graft-y
:modifier-state 0))
(setf *graphic-forms-mouse-down-sheet* nil))
(defun char-to-sym (char)
(case char
(#\ :| |) (#\! :!) (#\" :|"|) (#\# :|#|) (#\$ :$) (#\% :%) (#\& :&)
(#\' :|'|) (#\( :|(|) (#\) :|)|) (#\* :*) (#\+ :+) (#\, :|,|) (#\- :-)
(#\. :|.|) (#\/ :/) (#\0 :|0|) (#\1 :|1|) (#\2 :|2|) (#\3 :|3|) (#\4 :|4|)
(#\5 :|5|) (#\6 :|6|) (#\7 :|7|) (#\8 :|8|) (#\9 :|9|) (#\: :|:|) (#\; :|;|)
(#\< :<) (#\= :=) (#\> :>) (#\? :?) (#\@ :@) (#\A :A) (#\B :B) (#\C :C)
(#\D :D) (#\E :E) (#\F :F) (#\G :G) (#\H :H) (#\I :I) (#\J :J) (#\K :K)
(#\L :L) (#\M :M) (#\N :N) (#\O :O) (#\P :P) (#\Q :Q) (#\R :R) (#\S :S)
(#\T :T) (#\U :U) (#\V :V) (#\W :W) (#\X :X) (#\Y :Y) (#\Z :Z) (#\[ :[)
(#\\ :|\\|) (#\] :]) (#\_ :_) (#\` :|`|) (#\a :|a|) (#\b :|b|) (#\c :|c|)
(#\d :|d|) (#\e :|e|) (#\f :|f|) (#\g :|g|) (#\h :|h|) (#\i :|i|) (#\j :|j|)
(#\k :|k|) (#\l :|l|) (#\m :|m|) (#\n :|n|) (#\o :|o|) (#\p :|p|) (#\q :|q|)
(#\r :|r|) (#\s :|s|) (#\t :|t|) (#\u :|u|) (#\v :|v|) (#\w :|w|) (#\x :|x|)
(#\y :|y|) (#\z :|z|) (#\{ :{) (#\| :|\||) (#\} :}) (#\Backspace :BACKSPACE)
(#\Tab :TAB) (#\Return :RETURN) (#\Rubout :DELETE)))
(defmethod gfw:event-key-down ((self sheet-event-dispatcher) mirror code char)
(server-add-event
(make-instance 'key-press-event
:key-name (char-to-sym char)
:key-character char
:sheet (sheet mirror)
;; FIXME:
:x 0
:y 0
:modifier-state 0
;;; :graft-x root-x
;;; :graft-y root-y
)))
(defmethod gfw:event-key-up ((self sheet-event-dispatcher) mirror code char)
(server-add-event
(make-instance 'key-release-event
:key-name (char-to-sym char)
:key-character char
:sheet (sheet mirror)
;; FIXME:
:x 0
:y 0
:modifier-state 0
;;; :graft-x root-x
;;; :graft-y root-y
)))
(defmethod gfw:event-mouse-enter ((self sheet-event-dispatcher) mirror point button)
; (debug-prin1 "enter" (translate-button-name button))
(server-add-event
(make-instance 'pointer-enter-event
:pointer 0
:sheet (sheet mirror)
:x (gfs:point-x point)
:y (gfs:point-y point)
:button (translate-button-name button)
:modifier-state 0)))
(defmethod gfw:event-mouse-exit ((self sheet-event-dispatcher) mirror point button)
; (debug-prin1 "exit " (translate-button-name button))
(server-add-event
(make-instance 'pointer-exit-event
:pointer 0
:sheet (sheet mirror)
:x (gfs:point-x point)
:y (gfs:point-y point)
:button (translate-button-name button)
:modifier-state 0)))