Added timeouts
[cl-gtk2.git] / glib / gobject.boxed.lisp
1 (in-package :gobject)
2
3 (define-foreign-type g-boxed-foreign-type ()
4   ((info :initarg :info
5          :accessor g-boxed-foreign-info
6          :initform (error "info must be specified"))
7    (return-p :initarg :return-p
8              :accessor g-boxed-foreign-return-p
9              :initform nil))
10   (:actual-type :pointer))
11
12 (eval-when (:compile-toplevel :load-toplevel :execute)
13   (defstruct g-boxed-info
14     name
15     g-type))
16
17 (eval-when (:compile-toplevel :load-toplevel :execute)
18   (defun get-g-boxed-foreign-info (name)
19     (get name 'g-boxed-foreign-info)))
20
21 (defvar *g-type-name->g-boxed-foreign-info* (make-hash-table :test 'equal))
22
23 (defun get-g-boxed-foreign-info-for-gtype (g-type-designator)
24   (or (gethash (g-type-string g-type-designator) *g-type-name->g-boxed-foreign-info*)
25       (error "Unknown GBoxed type '~A'" (g-type-string g-type-designator))))
26
27 (defgeneric make-foreign-type (info &key return-p))
28
29 (define-parse-method g-boxed-foreign (name &rest options)
30   (let ((info (get-g-boxed-foreign-info name)))
31     (assert info nil "Unknown foreign GBoxed type ~A" name)
32     (make-foreign-type info :return-p (member :return options))))
33
34 (defgeneric boxed-copy-fn (type-info native)
35   (:method (type-info native)
36     (g-boxed-copy (g-boxed-info-g-type type-info) native)))
37
38 #+nil(defmethod boxed-copy-fn :before (type-info native)
39   (format t "(boxed-copy-fn ~A ~A)~%" (g-boxed-info-name type-info) native))
40
41 (defgeneric boxed-free-fn (type-info native)
42   (:method (type-info native)
43     (g-boxed-free (g-boxed-info-g-type type-info) native)))
44
45 #+nil(defmethod boxed-free-fn :before (type-info native)
46   (format t "(boxed-free-fn ~A ~A)~%" (g-boxed-info-name type-info) native))
47
48 (defgeneric has-callback-cleanup (foreign-type))
49 (defgeneric cleanup-translated-object-for-callback (foreign-type converted-object native-object))
50
51 (defmethod has-callback-cleanup ((type g-boxed-foreign-type))
52   t)
53
54 (eval-when (:load-toplevel :compile-toplevel :execute)
55   (defstruct (g-boxed-cstruct-wrapper-info (:include g-boxed-info))
56     cstruct-description))
57
58 (defclass boxed-cstruct-foreign-type (g-boxed-foreign-type) ())
59
60 (defstruct cstruct-slot-description
61   name
62   type
63   count
64   initform
65   inline-p)
66
67 (defstruct (cstruct-inline-slot-description (:include cstruct-slot-description))
68   boxed-type-name)
69
70 (defmethod make-load-form ((object cstruct-slot-description) &optional environment)
71   (make-load-form-saving-slots object :environment environment))
72
73 (defmethod make-load-form ((object cstruct-inline-slot-description) &optional environment)
74   (make-load-form-saving-slots object :environment environment))
75
76 (defstruct cstruct-description
77   name
78   slots)
79
80 (defmethod make-load-form ((object cstruct-description) &optional environment)
81   (make-load-form-saving-slots object :environment environment))
82
83 (defun parse-cstruct-slot (slot)
84   (destructuring-bind (name type &key count initform inline) slot
85     (if inline
86         (make-cstruct-inline-slot-description :name name :type (generated-cunion-name type)
87                                        :count count :initform initform :inline-p inline
88                                        :boxed-type-name type)
89         (make-cstruct-inline-slot-description :name name :type type
90                                               :count count :initform initform :inline-p inline))))
91
92 (defun parse-cstruct-definition (name slots)
93   (make-cstruct-description :name name
94                             :slots (mapcar #'parse-cstruct-slot slots)))
95
96 (defmacro define-g-boxed-cstruct (name g-type-name &body slots)
97   (let ((cstruct-description (parse-cstruct-definition name slots)))
98     `(progn
99        (defstruct ,name
100          ,@(iter (for slot in (cstruct-description-slots cstruct-description))
101                  (for name = (cstruct-slot-description-name slot))
102                  (for initform = (cstruct-slot-description-initform slot))
103                  (collect (list name initform))))
104        (defcstruct ,(generated-cstruct-name name)
105          ,@(iter (for slot in (cstruct-description-slots cstruct-description))
106                  (for name = (cstruct-slot-description-name slot))
107                  (for type = (cstruct-slot-description-type slot))
108                  (for count = (cstruct-slot-description-count slot))
109                  (collect `(,name ,type ,@(when count `(:count ,count))))))
110        (defcunion ,(generated-cunion-name name)
111          (,name ,(generated-cstruct-name name)))
112        (eval-when (:compile-toplevel :load-toplevel :execute)
113          (setf (get ',name 'g-boxed-foreign-info)
114                (make-g-boxed-cstruct-wrapper-info :name ',name
115                                                   :g-type ,g-type-name
116                                                   :cstruct-description ,cstruct-description)
117                (gethash ,g-type-name *g-type-name->g-boxed-foreign-info*)
118                (get ',name 'g-boxed-foreign-info)
119                (get ',name 'structure-constructor)
120                ',(intern (format nil "MAKE-~A" (symbol-name name)) (symbol-package name)))))))
121
122 (defmethod make-foreign-type ((info g-boxed-cstruct-wrapper-info) &key return-p)
123   (make-instance 'boxed-cstruct-foreign-type :info info :return-p return-p))
124
125 (defun memcpy (target source bytes)
126   (iter (for i from 0 below bytes)
127         (setf (mem-aref target :uchar i)
128               (mem-aref source :uchar i))))
129
130 (defmethod boxed-copy-fn ((info g-boxed-cstruct-wrapper-info) native)
131   (if (g-boxed-info-g-type info)
132       (g-boxed-copy (g-boxed-info-g-type info) native)
133       (let ((copy (foreign-alloc (generated-cstruct-name (g-boxed-info-name info)))))
134         (memcpy copy native (foreign-type-size (generated-cstruct-name (g-boxed-info-name info))))
135         copy)))
136
137 (defmethod boxed-free-fn ((info g-boxed-cstruct-wrapper-info) native)
138   (if (g-boxed-info-g-type info)
139       (g-boxed-free (g-boxed-info-g-type info) native)
140       (foreign-free native)))
141
142 (defun copy-slots-to-native (proxy native cstruct-description)
143   (iter (with cstruct-type = (generated-cstruct-name (cstruct-description-name cstruct-description)))
144         (for slot in (cstruct-description-slots cstruct-description))
145         (for slot-name = (cstruct-slot-description-name slot))
146         (cond
147           ((cstruct-slot-description-count slot)
148            (iter (with ptr = (foreign-slot-pointer native cstruct-type slot-name))
149                  (with array = (slot-value proxy slot-name))
150                  (for i from 0 below (cstruct-slot-description-count slot))
151                  (setf (mem-aref ptr (cstruct-slot-description-type slot) i)
152                        (aref array i))))
153           ((cstruct-slot-description-inline-p slot)
154            (let ((info (get-g-boxed-foreign-info (cstruct-inline-slot-description-boxed-type-name slot))))
155              (copy-slots-to-native (slot-value proxy slot-name)
156                                    (foreign-slot-pointer native cstruct-type slot-name)
157                                    (g-boxed-cstruct-wrapper-info-cstruct-description info))))
158           (t
159            (setf (foreign-slot-value native cstruct-type slot-name)
160                  (slot-value proxy slot-name))))))
161
162 (defun create-structure (structure-name)
163   (let ((constructor (get structure-name 'structure-constructor)))
164     (assert constructor nil "Don't know how to create structure of type ~A" structure-name)
165     (funcall constructor)))
166
167 (defun copy-slots-to-proxy (proxy native cstruct-description)
168   (iter (with cstruct-type = (generated-cstruct-name (cstruct-description-name cstruct-description)))
169         (for slot in (cstruct-description-slots cstruct-description))
170         (for slot-name = (cstruct-slot-description-name slot))
171         (cond
172           ((cstruct-slot-description-count slot)
173            (setf (slot-value proxy slot-name) (make-array (list (cstruct-slot-description-count slot))))
174            (iter (with ptr = (foreign-slot-pointer native cstruct-type slot-name))
175                  (with array = (slot-value proxy slot-name))
176                  (for i from 0 below (cstruct-slot-description-count slot))
177                  (setf (aref array i)
178                        (mem-aref ptr (cstruct-slot-description-type slot) i))))
179           ((cstruct-slot-description-inline-p slot)
180            (let ((info (get-g-boxed-foreign-info (cstruct-inline-slot-description-boxed-type-name slot))))
181              (setf (slot-value proxy slot-name) (create-structure (cstruct-inline-slot-description-boxed-type-name slot)))
182              (copy-slots-to-proxy (slot-value proxy slot-name)
183                                   (foreign-slot-pointer native cstruct-type slot-name)
184                                   (g-boxed-cstruct-wrapper-info-cstruct-description info))))
185           (t (setf (slot-value proxy slot-name)
186                    (foreign-slot-value native cstruct-type slot-name))))))
187
188 (defmethod translate-to-foreign (proxy (type boxed-cstruct-foreign-type))
189   (if (null proxy)
190       (null-pointer)
191       (let* ((info (g-boxed-foreign-info type))
192              (native-structure-type (generated-cstruct-name (g-boxed-info-name info))))
193         (with-foreign-object (native-structure native-structure-type)
194           (copy-slots-to-native proxy native-structure (g-boxed-cstruct-wrapper-info-cstruct-description info))
195           (values (boxed-copy-fn info native-structure) proxy)))))
196
197 (defmethod free-translated-object (native-structure (type boxed-cstruct-foreign-type) proxy)
198   (when proxy
199     (let ((info (g-boxed-foreign-info type)))
200       (copy-slots-to-proxy proxy native-structure (g-boxed-cstruct-wrapper-info-cstruct-description info))
201       (boxed-free-fn info native-structure))))
202
203 (defmethod translate-from-foreign (native-structure (type boxed-cstruct-foreign-type))
204   (unless (null-pointer-p native-structure)
205     (let* ((info (g-boxed-foreign-info type))
206            (proxy-structure-type (g-boxed-info-name info))
207            (proxy (create-structure proxy-structure-type)))
208       (copy-slots-to-proxy proxy native-structure (g-boxed-cstruct-wrapper-info-cstruct-description info))
209       (when (g-boxed-foreign-return-p type)
210         (boxed-free-fn info native-structure))
211       proxy)))
212
213 (defmethod cleanup-translated-object-for-callback ((type boxed-cstruct-foreign-type) proxy native-structure)
214   (when proxy
215     (let ((info (g-boxed-foreign-info type)))
216       (copy-slots-to-native proxy native-structure (g-boxed-cstruct-wrapper-info-cstruct-description info)))))
217
218 (eval-when (:compile-toplevel :load-toplevel :execute)
219   (defstruct (g-boxed-opaque-wrapper-info (:include g-boxed-info))
220     alloc free))
221
222 (define-foreign-type boxed-opaque-foreign-type (g-boxed-foreign-type) ())
223
224 (defclass g-boxed-opaque ()
225   ((pointer :initarg :pointer
226             :initform nil
227             :accessor g-boxed-opaque-pointer)))
228
229 (defmethod pointer ((object g-boxed-opaque))
230   (g-boxed-opaque-pointer object))
231
232 (defmethod make-foreign-type ((info g-boxed-opaque-wrapper-info) &key return-p)
233   (make-instance 'boxed-opaque-foreign-type :info info :return-p return-p))
234
235 (defmethod translate-to-foreign (proxy (type boxed-opaque-foreign-type))
236   (prog1 (g-boxed-opaque-pointer proxy)
237     (when (g-boxed-foreign-return-p type)
238       (tg:cancel-finalization proxy)
239       (setf (g-boxed-opaque-pointer proxy) nil))))
240
241 (defmethod free-translated-object (native (type boxed-opaque-foreign-type) param)
242   (declare (ignore native type param)))
243
244 (defun make-boxed-free-finalizer (type pointer)
245   (lambda () (boxed-free-fn type pointer)))
246
247 (defmethod translate-from-foreign (native (foreign-type boxed-opaque-foreign-type))
248   (let* ((type (g-boxed-foreign-info foreign-type))
249          (proxy (make-instance (g-boxed-info-name type) :pointer native)))
250     (tg:finalize proxy (make-boxed-free-finalizer type native))))
251
252 (defmethod cleanup-translated-object-for-callback ((type boxed-opaque-foreign-type) proxy native)
253   (tg:cancel-finalization proxy)
254   (setf (g-boxed-opaque-pointer proxy) nil))
255
256 (defmacro define-g-boxed-opaque (name g-type-name &key
257                                  (alloc (error "Alloc must be specified")))
258   (let ((native-copy (gensym "NATIVE-COPY-"))
259         (instance (gensym "INSTANCE-")))
260     `(progn (defclass ,name (g-boxed-opaque) ())
261             (defmethod initialize-instance :after ((,instance ,name) &key &allow-other-keys)
262               (unless (g-boxed-opaque-pointer ,instance)
263                 (let ((,native-copy ,alloc))
264                   (setf (g-boxed-opaque-pointer ,instance) ,native-copy)
265                   (finalize ,instance (make-boxed-free-finalizer (get ',name 'g-boxed-foreign-info) ,native-copy)))))
266             (eval-when (:compile-toplevel :load-toplevel :execute)
267               (setf (get ',name 'g-boxed-foreign-info)
268                     (make-g-boxed-opaque-wrapper-info :name ',name
269                                                       :g-type ,g-type-name)
270                     (gethash ,g-type-name *g-type-name->g-boxed-foreign-info*)
271                     (get ',name 'g-boxed-foreign-info))))))
272
273 (defstruct var-structure
274   name
275   parent
276   slots
277   discriminator-slot
278   variants
279   resulting-cstruct-description)
280
281 (defstruct var-structure-variant
282   discriminating-values
283   structure)
284
285 (defmethod make-load-form ((object var-structure) &optional env)
286   (make-load-form-saving-slots object :environment env))
287
288 (defmethod make-load-form ((object var-structure-variant) &optional env)
289   (make-load-form-saving-slots object :environment env))
290
291 (defun var-struct-all-slots (struct)
292   (when struct
293     (append (var-struct-all-slots (var-structure-parent struct))
294             (var-structure-slots struct))))
295
296 (defun all-structures (structure)
297   (append (iter (for variant in (var-structure-variants structure))
298                 (appending (all-structures (var-structure-variant-structure variant))))
299           (list structure)))
300
301 (defun parse-variant-structure-definition (name slots &optional parent)
302   (iter (with result = (make-var-structure :name name
303                                            :parent parent
304                                            :slots nil
305                                            :discriminator-slot nil
306                                            :variants nil))
307         (for slot in slots)
308         (if (eq :variant (first slot))
309             (progn
310               (when (var-structure-discriminator-slot result)
311                 (error "Structure has more than one discriminator slot"))
312               (setf (var-structure-discriminator-slot result) (second slot)
313                     (var-structure-variants result) (parse-variants result (nthcdr 2 slot))))
314             (push (parse-cstruct-slot slot) (var-structure-slots result)))
315         (finally (setf (var-structure-slots result)
316                        (reverse (var-structure-slots result)))
317                  (unless parent
318                    (set-variant-result-structure result))
319                  (return result))))
320
321 (defun set-variant-result-structure (var-structure)
322   (setf (var-structure-resulting-cstruct-description var-structure)
323         (make-cstruct-description
324          :name
325          (var-structure-name var-structure)
326          :slots
327          (append
328           (when (var-structure-parent var-structure)
329             (cstruct-description-slots (var-structure-resulting-cstruct-description (var-structure-parent var-structure))))
330           (var-structure-slots var-structure))))
331   (iter (for variant in (var-structure-variants var-structure))
332         (for child-var-structure = (var-structure-variant-structure variant))
333         (set-variant-result-structure child-var-structure)))
334
335 (defun ensure-list (thing)
336   (if (listp thing)
337       thing
338       (list thing)))
339
340 (defun parse-variants (parent variants)
341   (iter (for var-descr in variants)
342         (for (options variant-name . slots) in variants)
343         (for variant =
344              (make-var-structure-variant
345               :discriminating-values (ensure-list options)
346               :structure (parse-variant-structure-definition variant-name slots parent)))
347         (collect variant)))
348
349 (defun generated-cstruct-name (symbol)
350   (intern (format nil "~A-CSTRUCT-GENERATED-BY-GOBJECT-BOXED" (symbol-name symbol)) (symbol-package symbol)))
351
352 (defun generated-cunion-name (symbol)
353   (intern (format nil "~A-CUNION-GENERATED-BY-GOBJECT-BOXED" (symbol-name symbol)) (symbol-package symbol)))
354
355 (defun generate-cstruct-1 (struct)
356   `(defcstruct ,(generated-cstruct-name (cstruct-description-name struct))
357      ,@(iter (for slot in (cstruct-description-slots struct))
358              (collect `(,(cstruct-slot-description-name slot) ,(cstruct-slot-description-type slot)
359                          ,@(when (cstruct-slot-description-count slot)
360                                  `(:count ,(cstruct-slot-description-count slot))))))))
361
362 (defun generate-c-structures (structure)
363   (iter (for str in (all-structures structure))
364         (for cstruct = (var-structure-resulting-cstruct-description str))
365         (collect (generate-cstruct-1 cstruct))))
366
367 (defun generate-variant-union (struct)
368   `(defcunion ,(generated-cunion-name (var-structure-name struct))
369      ,@(iter (for str in (all-structures struct))
370              (collect `(,(var-structure-name str)
371                          ,(generated-cstruct-name (var-structure-name str)))))))
372
373 (defun generate-structure-1 (str)
374   (let ((name (var-structure-name str)))
375     `(progn
376        (defstruct ,(if (var-structure-parent str)
377                        `(,(var-structure-name str) (:include ,(var-structure-name (var-structure-parent str))
378                                                              (,(var-structure-discriminator-slot (var-structure-parent str))
379                                                                ,(first (var-structure-variant-discriminating-values
380                                                                         (find str
381                                                                               (var-structure-variants
382                                                                                (var-structure-parent str))
383                                                                               :key #'var-structure-variant-structure))))))
384                        `,(var-structure-name str))
385          ,@(iter (for slot in (var-structure-slots str))
386                  (collect `(,(cstruct-slot-description-name slot)
387                              ,(cstruct-slot-description-initform slot)))))
388        (setf (get ',name 'structure-constructor)
389              ',(intern (format nil "MAKE-~A" (symbol-name name)) (symbol-package name))))))
390
391 (defun generate-structures (str)
392   (iter (for variant in (reverse (all-structures str)))
393         (collect (generate-structure-1 variant))))
394
395 (defun generate-native-type-decision-procedure-1 (str proxy-var)
396   (if (null (var-structure-discriminator-slot str))
397       `(values ',(var-structure-resulting-cstruct-description str))
398       `(typecase ,proxy-var
399          ,@(iter (for variant in (var-structure-variants str))
400                  (for v-str = (var-structure-variant-structure variant))
401                  (collect `(,(var-structure-name v-str)
402                              ,(generate-native-type-decision-procedure-1 v-str proxy-var))))
403          (,(var-structure-name str)
404           (values ',(var-structure-resulting-cstruct-description str))))))
405
406 (defun generate-proxy-type-decision-procedure-1 (str native-var)
407   (if (null (var-structure-discriminator-slot str))
408       `(values ',(var-structure-name str)
409                ',(var-structure-resulting-cstruct-description str))
410       `(case (foreign-slot-value ,native-var
411                                  ',(generated-cstruct-name (var-structure-name str))
412                                  ',(var-structure-discriminator-slot str))
413          ,@(iter (for variant in (var-structure-variants str))
414                  (for v-str = (var-structure-variant-structure variant))
415                  (collect `(,(var-structure-variant-discriminating-values variant)
416                              ,(generate-proxy-type-decision-procedure-1
417                                v-str
418                                native-var))))
419          (t (values ',(var-structure-name str)
420                     ',(var-structure-resulting-cstruct-description str))))))
421
422 (defun generate-proxy-type-decision-procedure (str)
423   (let ((native (gensym "NATIVE-")))
424     `(lambda (,native)
425        (declare (ignorable ,native))
426        ,(generate-proxy-type-decision-procedure-1 str native))))
427
428 (defun generate-native-type-decision-procedure (str)
429   (let ((proxy (gensym "PROXY-")))
430     `(lambda (,proxy)
431        (declare (ignorable ,proxy))
432        ,(generate-native-type-decision-procedure-1 str proxy))))
433
434 (defun compile-proxy-type-decision-procedure (str)
435   (compile nil (generate-proxy-type-decision-procedure str)))
436
437 (defun compile-native-type-decision-procedure (str)
438   (compile nil (generate-native-type-decision-procedure str)))
439
440 (defstruct (g-boxed-variant-cstruct-info (:include g-boxed-info))
441   root
442   native-type-decision-procedure
443   proxy-type-decision-procedure)
444
445 (defmethod make-load-form ((object g-boxed-variant-cstruct-info) &optional env)
446   (make-load-form-saving-slots object :environment env))
447
448 (define-foreign-type boxed-variant-cstruct-foreign-type (g-boxed-foreign-type) ())
449
450 (defmethod make-foreign-type ((info g-boxed-variant-cstruct-info) &key return-p)
451   (make-instance 'boxed-variant-cstruct-foreign-type :info info :return-p return-p))
452
453 (defmacro define-g-boxed-variant-cstruct (name g-type-name &body slots)
454   (let* ((structure (parse-variant-structure-definition name slots)))
455     `(progn ,@(generate-c-structures structure)
456             ,(generate-variant-union structure)
457             ,@(generate-structures structure)
458             (eval-when (:compile-toplevel :load-toplevel :execute)
459               (setf (get ',name 'g-boxed-foreign-info)
460                     (make-g-boxed-variant-cstruct-info :name ',name
461                                                        :g-type ,g-type-name
462                                                        :root ,structure
463                                                        :native-type-decision-procedure
464                                                        ,(generate-native-type-decision-procedure structure)
465                                                        :proxy-type-decision-procedure
466                                                        ,(generate-proxy-type-decision-procedure structure))
467                     (gethash ,g-type-name *g-type-name->g-boxed-foreign-info*)
468                     (get ',name 'g-boxed-foreign-info))))))
469
470 (defun decide-native-type (info proxy)
471   (funcall (g-boxed-variant-cstruct-info-native-type-decision-procedure info) proxy))
472
473 (defmethod boxed-copy-fn ((info g-boxed-variant-cstruct-info) native)
474   (if (g-boxed-info-g-type info)
475       (g-boxed-copy (g-boxed-info-g-type info) native)
476       (let ((copy (foreign-alloc (generated-cunion-name (g-boxed-info-name info)))))
477         (memcpy copy native (foreign-type-size (generated-cunion-name (g-boxed-info-name info))))
478         copy)))
479
480 (defmethod boxed-free-fn ((info g-boxed-variant-cstruct-info) native)
481   (if (g-boxed-info-g-type info)
482       (g-boxed-free (g-boxed-info-g-type info) native)
483       (foreign-free native)))
484
485 (defmethod translate-to-foreign (proxy (foreign-type boxed-variant-cstruct-foreign-type))
486   (if (null proxy)
487       (null-pointer)
488       (let* ((type (g-boxed-foreign-info foreign-type))
489              (cstruct-description (decide-native-type type proxy)))
490         (with-foreign-object (native-structure (generated-cunion-name
491                                                 (var-structure-name
492                                                  (g-boxed-variant-cstruct-info-root type))))
493           (copy-slots-to-native proxy native-structure cstruct-description)
494           (values (boxed-copy-fn type native-structure) proxy)))))
495
496 (defun decide-proxy-type (info native-structure)
497   (funcall (g-boxed-variant-cstruct-info-proxy-type-decision-procedure info) native-structure))
498
499 (defmethod free-translated-object (native (foreign-type boxed-variant-cstruct-foreign-type) proxy)
500   (when proxy
501     (let ((type (g-boxed-foreign-info foreign-type)))
502       (multiple-value-bind (actual-struct cstruct-description) (decide-proxy-type type native)
503         (unless (eq (type-of proxy) actual-struct)
504           (restart-case
505               (error "Expected type of boxed variant structure ~A and actual type ~A do not match"
506                      (type-of proxy) actual-struct)
507             (skip-parsing-values () (return-from free-translated-object))))
508         (copy-slots-to-proxy proxy native cstruct-description)
509         (boxed-free-fn type native)))))
510
511 (defmethod translate-from-foreign (native (foreign-type boxed-variant-cstruct-foreign-type))
512   (unless (null-pointer-p native)
513     (let ((type (g-boxed-foreign-info foreign-type)))
514       (multiple-value-bind (actual-struct cstruct-description) (decide-proxy-type type native)
515         (let ((proxy (create-structure actual-struct)))
516           (copy-slots-to-proxy proxy native cstruct-description)
517           (when (g-boxed-foreign-return-p foreign-type)
518             (boxed-free-fn type native))
519           proxy)))))
520
521 (defmethod cleanup-translated-object-for-callback ((foreign-type boxed-variant-cstruct-foreign-type) proxy native)
522   (when proxy
523     (let ((type (g-boxed-foreign-info foreign-type)))
524       (let ((cstruct-description (decide-native-type type proxy)))
525         (copy-slots-to-native proxy native cstruct-description)))))
526
527 (defgeneric boxed-parse-g-value (gvalue-ptr info))
528
529 (defgeneric boxed-set-g-value (gvalue-ptr info proxy))
530
531 (defmethod parse-g-value-for-type (gvalue-ptr (type-numeric (eql +g-type-boxed+)) parse-kind)
532   (declare (ignore parse-kind))
533   (if (g-type= (g-value-type gvalue-ptr) (g-strv-get-type))
534       (convert-from-foreign (g-value-get-boxed gvalue-ptr) '(glib:gstrv :free-from-foreign nil))
535       (let ((boxed-type (get-g-boxed-foreign-info-for-gtype (g-value-type gvalue-ptr))))
536         (boxed-parse-g-value gvalue-ptr boxed-type))))
537
538 (defmethod set-gvalue-for-type (gvalue-ptr (type-numeric (eql +g-type-boxed+)) value)
539   (if (g-type= (g-value-type gvalue-ptr) (g-strv-get-type))
540       (g-value-set-boxed gvalue-ptr (convert-to-foreign value '(glib:gstrv :free-from-foreign nil)))
541       (let ((boxed-type (get-g-boxed-foreign-info-for-gtype (g-value-type gvalue-ptr))))
542         (boxed-set-g-value gvalue-ptr boxed-type value))))
543
544 (defmethod boxed-parse-g-value (gvalue-ptr (info g-boxed-cstruct-wrapper-info))
545   (translate-from-foreign (g-value-get-boxed gvalue-ptr) (make-foreign-type info :return-p nil)))
546
547 (defmethod boxed-set-g-value (gvalue-ptr (info g-boxed-cstruct-wrapper-info) proxy)
548   (g-value-take-boxed gvalue-ptr (translate-to-foreign proxy (make-foreign-type info :return-p nil))))
549
550 (defmethod boxed-parse-g-value (gvalue-ptr (info g-boxed-variant-cstruct-info))
551   (translate-from-foreign (g-value-get-boxed gvalue-ptr) (make-foreign-type info :return-p nil)))
552
553 (defmethod boxed-set-g-value (gvalue-ptr (info g-boxed-variant-cstruct-info) proxy)
554   (g-value-take-boxed gvalue-ptr (translate-to-foreign proxy (make-foreign-type info :return-p nil))))
555
556 (defmethod boxed-parse-g-value (gvalue-ptr (info g-boxed-opaque-wrapper-info))
557   (translate-from-foreign (boxed-copy-fn info (g-value-get-boxed gvalue-ptr)) (make-foreign-type info :return-p nil)))
558
559 (defmethod boxed-set-g-value (gvalue-ptr (info g-boxed-opaque-wrapper-info) proxy)
560   (g-value-set-boxed gvalue-ptr (translate-to-foreign proxy (make-foreign-type info :return-p nil))))
561
562 (defun boxed-related-symbols (name)
563   (let ((info (get-g-boxed-foreign-info name)))
564     (etypecase info
565       (g-boxed-cstruct-wrapper-info
566        (append (list name
567                      (intern (format nil "MAKE-~A" (symbol-name name)))
568                      (intern (format nil "COPY-~A" (symbol-name name))))
569                (iter (for slot in (cstruct-description-slots (g-boxed-cstruct-wrapper-info-cstruct-description info)))
570                      (for slot-name = (cstruct-slot-description-name slot))
571                      (collect (intern (format nil "~A-~A" (symbol-name name) (symbol-name slot-name)))))))
572       (g-boxed-opaque-wrapper-info
573        (list name))
574       (g-boxed-variant-cstruct-info
575        (append (list name)
576                (iter (for var-struct in (all-structures (g-boxed-variant-cstruct-info-root info)))
577                      (for s-name = (var-structure-name var-struct))
578                      (for cstruct-description = (var-structure-resulting-cstruct-description var-struct))
579                      (appending (append (list s-name)
580                                         (list (intern (format nil "MAKE-~A" (symbol-name s-name)))
581                                               (intern (format nil "COPY-~A" (symbol-name s-name))))
582                                         (iter (for slot in (cstruct-description-slots cstruct-description))
583                                               (for slot-name = (cstruct-slot-description-name slot))
584                                               (collect (intern (format nil "~A-~A" (symbol-name s-name)
585                                                                        (symbol-name slot-name)))))))))))))
586
587 (defmacro define-boxed-opaque-accessor (boxed-name accessor-name &key type reader writer)
588   (let ((var (gensym))
589         (n-var (gensym)))
590     `(progn ,@(when reader
591                     (list (etypecase reader
592                             (symbol `(defun ,accessor-name (,var)
593                                        (funcall ,reader ,var)))
594                             (string `(defcfun (,accessor-name ,reader) ,type
595                                        (,var (g-boxed-foreign ,boxed-name)))))))
596             ,@(when writer
597                     (list (etypecase reader
598                             (symbol `(defun (setf ,accessor-name) (,n-var ,var)
599                                        (funcall ,reader ,n-var ,var)))
600                             (string `(defun (setf ,accessor-name) (,n-var ,var)
601                                        (foreign-funcall ,writer (g-boxed-foreign ,boxed-name) ,var ,type ,n-var :void)))))))))