-
Notifications
You must be signed in to change notification settings - Fork 5
/
xcb-renderutil.el
296 lines (269 loc) · 13.7 KB
/
xcb-renderutil.el
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
;;; xcb-renderutil.el --- Utility functions for -*- lexical-binding: t -*-
;;; the Render extension
;; Copyright (C) 2016-2024 Free Software Foundation, Inc.
;; Author: Chris Feng <[email protected]>
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This library provides utility functions for the Render extension.
;; Todo
;; + Glyph-related functions are not implemented.
;; References:
;; + xcb/util-renderutil (git://anongit.freedesktop.org/xcb/util-renderutil)
;;; Code:
(require 'xcb)
(require 'xcb-render)
;; Protocol version.
(defconst xcb:renderutil:-MAJOR_VERSION 0)
(defconst xcb:renderutil:-MINOR_VERSION 11)
;; PICTFORMINFO masks.
(defconst xcb:renderutil:PICT_FORMAT:ID 1)
(defconst xcb:renderutil:PICT_FORMAT:TYPE 2)
(defconst xcb:renderutil:PICT_FORMAT:DEPTH 4)
(defconst xcb:renderutil:PICT_FORMAT:RED 8)
(defconst xcb:renderutil:PICT_FORMAT:RED_MASK 16)
(defconst xcb:renderutil:PICT_FORMAT:GREEN 32)
(defconst xcb:renderutil:PICT_FORMAT:GREEN_MASK 64)
(defconst xcb:renderutil:PICT_FORMAT:BLUE 128)
(defconst xcb:renderutil:PICT_FORMAT:BLUE_MASK 256)
(defconst xcb:renderutil:PICT_FORMAT:ALPHA 512)
(defconst xcb:renderutil:PICT_FORMAT:ALPHA_MASK 1024)
(defconst xcb:renderutil:PICT_FORMAT:COLORMAP 2048)
;; Indices of standard PictFormats.
(defconst xcb:renderutil:PICT_STANDARD:ARGB_32 0)
(defconst xcb:renderutil:PICT_STANDARD:RGB_24 1)
(defconst xcb:renderutil:PICT_STANDARD:A_8 2)
(defconst xcb:renderutil:PICT_STANDARD:A_4 3)
(defconst xcb:renderutil:PICT_STANDARD:A_1 4)
(defconst xcb:renderutil:STANDARD-TEMPLATES
(list
;; xcb:renderutil:PICT_STANDARD:ARGB_32
(vector (make-instance 'xcb:render:PICTFORMINFO
:id 0
:type xcb:render:PictType:Direct
:depth 32
:direct (make-instance 'xcb:render:DIRECTFORMAT
:red-shift 16
:red-mask #xFF
:green-shift 8
:green-mask #xFF
:blue-shift 0
:blue-mask #xFF
:alpha-shift 24
:alpha-mask #xFF)
:colormap 0)
(logior xcb:renderutil:PICT_FORMAT:TYPE
xcb:renderutil:PICT_FORMAT:DEPTH
xcb:renderutil:PICT_FORMAT:RED
xcb:renderutil:PICT_FORMAT:RED_MASK
xcb:renderutil:PICT_FORMAT:GREEN
xcb:renderutil:PICT_FORMAT:GREEN_MASK
xcb:renderutil:PICT_FORMAT:BLUE
xcb:renderutil:PICT_FORMAT:BLUE_MASK
xcb:renderutil:PICT_FORMAT:ALPHA
xcb:renderutil:PICT_FORMAT:ALPHA_MASK))
;; xcb:renderutil:PICT_STANDARD:RGB_24
(vector (make-instance 'xcb:render:PICTFORMINFO
:id 0
:type xcb:render:PictType:Direct
:depth 24
:direct (make-instance 'xcb:render:DIRECTFORMAT
:red-shift 16
:red-mask #xFF
:green-shift 8
:green-mask #xFF
:blue-shift 0
:blue-mask #xFF
:alpha-shift 0
:alpha-mask #x00)
:colormap 0)
(logior xcb:renderutil:PICT_FORMAT:TYPE
xcb:renderutil:PICT_FORMAT:DEPTH
xcb:renderutil:PICT_FORMAT:RED
xcb:renderutil:PICT_FORMAT:RED_MASK
xcb:renderutil:PICT_FORMAT:GREEN
xcb:renderutil:PICT_FORMAT:GREEN_MASK
xcb:renderutil:PICT_FORMAT:BLUE
xcb:renderutil:PICT_FORMAT:BLUE_MASK
xcb:renderutil:PICT_FORMAT:ALPHA_MASK))
;; xcb:renderutil:PICT_STANDARD:A_8
(vector (make-instance 'xcb:render:PICTFORMINFO
:id 0
:type xcb:render:PictType:Direct
:depth 8
:direct (make-instance 'xcb:render:DIRECTFORMAT
:red-shift 0
:red-mask #x00
:green-shift 0
:green-mask #x00
:blue-shift 0
:blue-mask #x00
:alpha-shift 0
:alpha-mask #xFF)
:colormap 0)
(logior xcb:renderutil:PICT_FORMAT:TYPE
xcb:renderutil:PICT_FORMAT:DEPTH
xcb:renderutil:PICT_FORMAT:RED_MASK
xcb:renderutil:PICT_FORMAT:GREEN_MASK
xcb:renderutil:PICT_FORMAT:BLUE_MASK
xcb:renderutil:PICT_FORMAT:ALPHA
xcb:renderutil:PICT_FORMAT:ALPHA_MASK))
;; xcb:renderutil:PICT_STANDARD:A_4
(vector (make-instance 'xcb:render:PICTFORMINFO
:id 0
:type xcb:render:PictType:Direct
:depth 4
:direct (make-instance 'xcb:render:DIRECTFORMAT
:red-shift 0
:red-mask #x00
:green-shift 0
:green-mask #x00
:blue-shift 0
:blue-mask #x00
:alpha-shift 0
:alpha-mask #x0F)
:colormap 0)
(logior xcb:renderutil:PICT_FORMAT:TYPE
xcb:renderutil:PICT_FORMAT:DEPTH
xcb:renderutil:PICT_FORMAT:RED_MASK
xcb:renderutil:PICT_FORMAT:GREEN_MASK
xcb:renderutil:PICT_FORMAT:BLUE_MASK
xcb:renderutil:PICT_FORMAT:ALPHA
xcb:renderutil:PICT_FORMAT:ALPHA_MASK))
;; xcb:renderutil:PICT_STANDARD:A_1
(vector (make-instance 'xcb:render:PICTFORMINFO
:id 0
:type xcb:render:PictType:Direct
:depth 1
:direct (make-instance 'xcb:render:DIRECTFORMAT
:red-shift 0
:red-mask #x00
:green-shift 0
:green-mask #x00
:blue-shift 0
:blue-mask #x00
:alpha-shift 0
:alpha-mask #x01)
:colormap 0)
(logior xcb:renderutil:PICT_FORMAT:TYPE
xcb:renderutil:PICT_FORMAT:DEPTH
xcb:renderutil:PICT_FORMAT:RED_MASK
xcb:renderutil:PICT_FORMAT:GREEN_MASK
xcb:renderutil:PICT_FORMAT:BLUE_MASK
xcb:renderutil:PICT_FORMAT:ALPHA
xcb:renderutil:PICT_FORMAT:ALPHA_MASK)))
"Standard PictFormats.")
(cl-defmethod xcb:renderutil:-get-cache ((obj xcb:connection))
"Return the cache and initialize the extension when first called."
(let ((result (plist-get (slot-value obj 'extra-plist) 'renderutil))
required-depths)
(unless (or result
(= 0 (slot-value
(xcb:get-extension-data obj 'xcb:render)
'present)))
(setq result
(vector (xcb:+request-unchecked+reply obj
(make-instance 'xcb:render:QueryVersion
:client-major-version
xcb:renderutil:-MAJOR_VERSION
:client-minor-version
xcb:renderutil:-MINOR_VERSION))
(xcb:+request-unchecked+reply obj
(make-instance 'xcb:render:QueryPictFormats))))
(setq required-depths '(1 4 8 24 32))
(catch 'break
(dolist (s (slot-value (aref result 1) 'screens))
(dolist (d (slot-value s 'depths))
(setq required-depths
(delq (slot-value d 'depth) required-depths))
(unless required-depths
(throw 'break nil)))))
(if required-depths
(setq result nil)
(setf (slot-value obj 'extra-plist)
(plist-put (slot-value obj 'extra-plist) 'renderutil result))))
result))
(defun xcb:renderutil:find-visual-format (formats visual)
"Search FORMATS for a format matching visual VISUAL."
(catch 'return
(dolist (s (slot-value formats 'screens))
(dolist (d (slot-value s 'depths))
(dolist (v (slot-value d 'visuals))
(when (= (slot-value v 'visual) visual)
(throw 'return (slot-value v 'format))))))))
(defun xcb:renderutil:find-format (formats mask template count)
"Search FORMATS for a format matching mask MASK and template TEMPLATE.
Return COUNT-th match."
(catch 'return
(unless formats
(throw 'return nil))
(dolist (f (slot-value formats 'formats))
(when (and (if (/= 0 (logand mask xcb:renderutil:PICT_FORMAT:ID))
(eq (slot-value template 'id) (slot-value f 'id))
t)
(if (/= 0 (logand mask xcb:renderutil:PICT_FORMAT:TYPE))
(eq (slot-value template 'type) (slot-value f 'type))
t)
(if (/= 0 (logand mask xcb:renderutil:PICT_FORMAT:DEPTH))
(eq (slot-value template 'depth) (slot-value f 'depth))
t)
(if (/= 0 (logand mask xcb:renderutil:PICT_FORMAT:RED))
(eq (slot-value (slot-value template 'direct) 'red-shift)
(slot-value (slot-value f 'direct) 'red-shift))
t)
(if (/= 0 (logand mask xcb:renderutil:PICT_FORMAT:RED_MASK))
(eq (slot-value (slot-value template 'direct) 'red-mask)
(slot-value (slot-value f 'direct) 'red-mask))
t)
(if (/= 0 (logand mask xcb:renderutil:PICT_FORMAT:GREEN))
(eq (slot-value (slot-value template 'direct) 'red-shift)
(slot-value (slot-value f 'direct) 'red-shift))
t)
(if (/= 0 (logand mask xcb:renderutil:PICT_FORMAT:GREEN_MASK))
(eq (slot-value (slot-value template 'direct) 'red-mask)
(slot-value (slot-value f 'direct) 'red-mask))
t)
(if (/= 0 (logand mask xcb:renderutil:PICT_FORMAT:BLUE))
(eq (slot-value (slot-value template 'direct) 'red-shift)
(slot-value (slot-value f 'direct) 'red-shift))
t)
(if (/= 0 (logand mask xcb:renderutil:PICT_FORMAT:BLUE_MASK))
(eq (slot-value (slot-value template 'direct) 'red-mask)
(slot-value (slot-value f 'direct) 'red-mask))
t)
(if (/= 0 (logand mask xcb:renderutil:PICT_FORMAT:COLORMAP))
(eq (slot-value template 'colormap)
(slot-value f 'colormap))
t))
(when (= count 0)
(throw 'return (slot-value f 'id))
(cl-decf count))))))
(defun xcb:renderutil:find-standard (formats format)
"Search FORMATS for a standard format matching format ID FORMAT."
(when (and (<= 0 format (1- (length xcb:renderutil:STANDARD-TEMPLATES))))
(let ((standard-format (elt xcb:renderutil:STANDARD-TEMPLATES format)))
(xcb:renderutil:find-format formats
(aref standard-format 1)
(aref standard-format 0)
0))))
(cl-defmethod xcb:renderutil:query-version ((obj xcb:connection))
"Return the version of Render extension."
(let ((cache (xcb:renderutil:-get-cache obj)))
(when cache
(aref cache 0))))
(cl-defmethod xcb:renderutil:query-formats ((obj xcb:connection))
"Return supported formats of this X server."
(let ((cache (xcb:renderutil:-get-cache obj)))
(when cache
(aref cache 1))))
(provide 'xcb-renderutil)
;;; xcb-renderutil.el ends here