Remove unused function `class-slot-definition'.
[cl-v4l2.git] / videodev2.lisp
blob23dd7c118a91c7a396a1d62f829e26dca7f8db70
1 (in-package :cl-v4l2)
3 ;; Generate wrapper classes for all structures.
4 ;; Slot access is overtaken via MOP. This allows to utilize usual `with-slots'
5 ;; macro. This is slow, 'cause it uses hash lookup tables.
6 ;;
7 ;; Better access speed can be achieved using generated accessors
8 ;; named like <class-name>-<slot-name>, e.g. `capability-driver'.
10 (defclass v4l2 (standard-class) ())
12 (defmethod validate-superclass ((obj v4l2) (obj1 standard-class)) t)
14 (defvar *v4l2-slot-readers* (make-hash-table :test 'equal))
15 (defvar *v4l2-slot-writers* (make-hash-table :test 'equal))
17 (defmethod slot-value-using-class ((class v4l2) inst slot)
18 (if (string= (string-upcase (slot-definition-name slot)) "RAW")
19 (call-next-method class inst)
20 (funcall (gethash (cons (class-name class) (slot-definition-name slot))
21 *v4l2-slot-readers*)
22 inst)))
24 (defmethod (setf slot-value-using-class) (new (class v4l2) inst slot)
25 (if (string= (string-upcase (slot-definition-name slot)) "RAW")
26 (call-next-method new class inst)
27 (funcall (gethash (cons (class-name class) (slot-definition-name slot))
28 *v4l2-slot-writers*)
29 new inst)))
31 (defmacro define-wrapper (class-and-type supers &optional slots)
32 (destructuring-bind (class-name &optional (struct-type class-name))
33 (cffi::ensure-list class-and-type)
34 (let ((slots (or slots (cffi::foreign-slot-names struct-type)))
35 (raw-accessor (cffi::format-symbol t "~A-RAW" class-name)))
36 `(progn
37 (defclass ,class-name ,supers
38 (,@(loop for slot in slots collect
39 `(,slot))
40 (raw :accessor ,raw-accessor))
41 (:metaclass v4l2))
43 ,@(loop for slot in slots
44 for slot-name = (cffi::format-symbol t "~A-~A" class-name slot)
45 for slot-type = (cffi::slot-type (cffi::get-slot-info class-name slot))
46 collect
47 `(defun ,slot-name (inst)
48 ,(if (or (eq slot-type :char) (eq slot-type :uchar))
49 `(convert-from-foreign
50 (foreign-slot-value (,raw-accessor inst) ',class-name ',slot) :string)
51 (if (cffi::aggregatep (cffi::parse-type slot-type))
52 `(make-instance ',slot-type
53 :pointer (foreign-slot-value (,raw-accessor inst) ',class-name ',slot))
54 `(foreign-slot-value (,raw-accessor inst) ',class-name ',slot))))
55 collect
56 `(setf (gethash (cons ',class-name ',slot) *v4l2-slot-readers*)
57 (fdefinition ',slot-name))
59 collect
60 `(defun (setf ,slot-name) (new inst)
61 (setf (foreign-slot-value (,raw-accessor inst) ',class-name ',slot)
62 (convert-to-foreign new ',slot-type)))
63 collect
64 `(setf (gethash (cons ',class-name ',slot) *v4l2-slot-writers*)
65 (fdefinition '(setf ,slot-name))))
67 (defmethod initialize-instance :after ((inst ,class-name) &key pointer)
68 (let ((obj (or pointer (foreign-alloc ',class-name))))
69 (setf (,raw-accessor inst) obj)
70 (unless pointer
71 (finalize inst (lambda ()
72 (cl:format t "finalize ~A~%" obj)
73 (foreign-free obj))))))
74 ',class-name))))
76 (defmacro def-c-struct (name &rest args)
77 "Define cffi struct and generate wrapper"
78 `(progn
79 (defcstruct ,name
80 ,@args)
81 (define-wrapper ,name ())))
83 (defmacro def-c-union (name &rest args)
84 "Define cffi union and generate wrapper"
85 `(progn
86 (defcunion ,name
87 ,@args)
88 (define-wrapper ,name ())))
90 (def-c-struct capability
91 (driver :uchar :count 16)
92 (card :uchar :count 32)
93 (bus-info :uchar :count 32)
94 (version :uint32)
95 (capabilities :uint32)
96 (reserved :uint32 :count 4))
98 (def-c-struct fract
99 (numerator :uint32)
100 (denominator :uint32))
102 (def-c-struct captureparm
103 (capability :uint32) ; Supported modes
104 (capturemode :uint32) ; Current mode
105 (timeperframe fract) ; Time per frame in .1us units
106 (extendedmode :uint32) ; Driver-specific extensions
107 (readbuffers :uint32) ; # of buffers for read
108 (reserved :uint32 :count 4))
110 (def-c-struct outputparm
111 (capability :uint32) ; Supported modes
112 (outputmode :uint32) ; Current mode
113 (timeperframe fract) ; Time per frame in .1us units
114 (extendedmode :uint32) ; Driver-specific extensions
115 (writebuffers :uint32) ; # of buffers for write
116 (reserved :uint32 :count 4))
118 (def-c-union streamparm-union
119 (capture captureparm)
120 (output outputparm)
121 (raw-data :uchar :count 200))
123 (defcenum buf-type
124 (:buf-type-video-capture 1)
125 :buf-type-video-output
126 :buf-type-video-overlay
127 :buf-type-vbi-capture
128 :buf-type-vbi-output
129 :buf-type-sliced-vbi-capture
130 :buf-type-sliced-vbi-output
131 :buf-type-video-output-overlay)
133 (def-c-struct streamparm
134 (type buf-type)
135 (parm streamparm-union))
137 (defcenum tuner-type
138 (:tuner-radio 1)
139 :tuner-analog-tv
140 :tuner-digital-tv)
142 (def-c-struct tuner
143 (index :uint32)
144 (name :uchar :count 32)
145 (type tuner-type)
146 (capability :uint32)
147 (rangelow :uint32)
148 (rangehigh :uint32)
149 (rxsubchans :uint32)
150 (audmode :uint32)
151 (signal :int32)
152 (afc :int32)
153 (reserved :uint32 :count 4))
155 (def-c-struct standard
156 (index :uint32)
157 (id :uint64)
158 (name :uchar :count 24)
159 (frameperiod fract)
160 (framelines :uint32)
161 (reserved :uint32 :count 4))
163 (def-c-struct input
164 (index :uint32) ; Which input
165 (name :uchar :count 32) ; Label
166 (type :uint32) ; Type of input
167 (audioset :uint32) ; Associated audios (bitfield)
168 (tuner :uint32) ; Associated tuner
169 (std :uint64)
170 (status :uint32)
171 (reserved :uint32 :count 4))
174 ;; F O R M A T E N U M E R A T I O N
176 (def-c-struct fmtdesc
177 (index :uint32) ; Format number
178 (type buf-type) ; buffer type
179 (flags :uint32)
180 (description :uchar :count 32) ; Description string
181 (pixelformat :uint32) ; Format fourcc
182 (reserved :uint32 :count 4))
184 ;; Values for the 'type' field
185 (defconstant input-type-tuner 1)
187 (defconstant input-type-camera 2)
189 (defcenum field
190 :field-any ; driver can choose from none
191 ; top, bottom, interlaced
192 ; depending on whatever it thinks
193 ; is approximate ...
194 :field-none ; this device has no fields ...
195 :field-top ; top field only
196 :field-bottom ; bottom field only
197 :field-interlaced ; both fields interlaced
198 :field-seq-tb ; both fields sequential into one
199 ; buffer, top-bottom order
200 :field-seq-bt ; same as above + bottom-top order
201 :field-alternate ; both fields alternating into
202 ; separate buffers
203 :field-interlaced-tb ; both fields interlaced, top field
204 ; first and the top field is
205 ; transmitted first
206 :field-interlaced-bt ; both fields interlaced, top field
207 ; first and the bottom field is
208 ; transmitted first
211 (defcenum colorspace
212 (:colorspace-smpte170m 1) ; ITU-R 601 -- broadcast NTSC/PAL
213 :colorspace_smpte240m ; 1125-Line (US) HDTV
214 :colorspace-rec709 ; HD and modern captures.
215 ; broken BT878 extents (601, luma range 16-253 instead of 16-235)
216 :colorspace-bt878
217 ; These should be useful. Assume 601 extents.
218 :colorspace-470-system-m
219 :colorspace-470-system-bg
221 ; I know there will be cameras that send this. So, this is
222 ; unspecified chromaticities and full 0-255 on each of the
223 ; Y'CbCr components
224 :colorspace-jpeg
226 ; For RGB colourspaces, this is probably a good start.
227 :colorspace-srgb)
230 ;; V I D E O I M A G E F O R M A T
232 (def-c-struct pix-format
233 (width :uint32)
234 (height :uint32)
235 (pixelformat :uint32)
236 (field field)
237 (bytesperline :uint32) ; for padding, zero if unused
238 (sizeimage :uint32)
239 (colorspace colorspace)
240 (priv :uint32)) ; private data, depends on pixelformat
242 ;; Stream data format
245 (def-c-struct timecode
246 (type :uint32)
247 (flags :uint32)
248 (frames :uchar)
249 (seconds :uchar)
250 (minutes :uchar)
251 (hours :uchar)
252 (userbits :uchar :count 4))
254 (defcenum memory
255 (:memory-mmap 1)
256 :memory-userptr
257 :memory-overlay)
259 ;; M E M O R Y - M A P P I N G B U F F E R S
261 (def-c-struct requestbuffers
262 (count :uint32)
263 (type buf-type)
264 (memory memory)
265 (reserved :uint32 :count 2))
267 (def-c-union buffer-union
268 (offset :uint32)
269 (userptr :ulong))
271 (def-c-struct buffer
272 (index :uint32)
273 (type buf-type)
274 (bytesused :uint32)
275 (flags :uint32)
276 (field field)
277 (timestamp timeval)
278 (timecode timecode)
279 (sequence :uint32)
280 ; memory location
281 (memory memory)
282 (m buffer-union)
283 (length :uint32)
284 (input :uint32)
285 (reserved :uint32))
287 (defcenum ctrl-type
288 (:ctrl-type-integer 1)
289 :ctrl-type-boolean
290 :ctrl-type-menu
291 :ctrl-type-button
292 :ctrl-type-integer64
293 :ctrl-type-ctrl-class)
295 ;; Used in the VIDIOC_QUERYCTRL ioctl for querying controls
296 (def-c-struct queryctrl
297 (id :uint32)
298 (type ctrl-type)
299 (name :uchar :count 32)
300 (minimum :int32)
301 (maximum :int32)
302 (step :int32)
303 (default-value :int32)
304 (flags :uint32)
305 (reserved :uint32 :count 2))
307 (def-c-struct control
308 (id :uint32)
309 (value :int32))
311 (defcenum power-line-frequency
312 :cid-power-line-frequency-disabled
313 :cid-power-line-frequency-50hz
314 :cid-power-line-frequency-60hz)
316 (defcenum colorfx
317 :colorfx-none
318 :colorfx-bw
319 :colorfx-sepia)
321 (defcenum exposure-auto-type
322 :exposure-auto
323 :exposure-manual
324 :exposure-shutter-priority
325 :exposure-aperture-priority)