Destroy opaque GBoxed object thread-safely (not tested)
[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 (defvar *gboxed-gc-hooks-lock* (make-recursive-lock "gboxed-gc-hooks-lock"))
245 (defvar *gboxed-gc-hooks* nil);;pointers to objects to be freed
246
247 (defun activate-gboxed-gc-hooks ()
248   (with-recursive-lock-held (*gboxed-gc-hooks-lock*)
249     (when *gboxed-gc-hooks*
250       (log-for :gc "activating gc hooks for boxeds: ~A~%" *gboxed-gc-hooks*)
251       (loop
252          for (pointer type) in *gboxed-gc-hooks*
253          do (boxed-free-fn type pointer))
254       (setf *gboxed-gc-hooks* nil))))
255
256 (defcallback gboxed-idle-gc-hook :boolean ((data :pointer))
257   (declare (ignore data))
258   (activate-gboxed-gc-hooks)
259   nil)
260
261 (defun register-gboxed-for-gc (type pointer)
262   (with-recursive-lock-held (*gboxed-gc-hooks-lock*)
263     (let ((locks-were-present (not (null *gboxed-gc-hooks*))))
264       (push (list pointer type) *gboxed-gc-hooks*)
265       (unless locks-were-present
266         (log-for :gc "adding gboxed idle-gc-hook to main loop~%")
267         (g-idle-add (callback gboxed-idle-gc-hook) (null-pointer))))))
268
269 (defun make-boxed-free-finalizer (type pointer)
270   (lambda () (register-gboxed-for-gc type pointer)))
271
272 (defmethod translate-from-foreign (native (foreign-type boxed-opaque-foreign-type))
273   (let* ((type (g-boxed-foreign-info foreign-type))
274          (proxy (make-instance (g-boxed-info-name type) :pointer native)))
275     proxy))
276
277 (defmethod cleanup-translated-object-for-callback ((type boxed-opaque-foreign-type) proxy native)
278   (declare (ignore native))
279   (tg:cancel-finalization proxy)
280   (setf (g-boxed-opaque-pointer proxy) nil))
281
282 (defmacro define-g-boxed-opaque (name g-type-name &key
283                                  (alloc (error "Alloc must be specified")))
284   (let ((native-copy (gensym "NATIVE-COPY-"))
285         (instance (gensym "INSTANCE-")))
286     `(progn (defclass ,name (g-boxed-opaque) ())
287             (defmethod initialize-instance :after ((,instance ,name) &key &allow-other-keys)
288               (unless (g-boxed-opaque-pointer ,instance)
289                 (let ((,native-copy ,alloc))
290                   (setf (g-boxed-opaque-pointer ,instance) ,native-copy)
291                   (finalize ,instance (make-boxed-free-finalizer (get ',name 'g-boxed-foreign-info) ,native-copy)))))
292             (eval-when (:compile-toplevel :load-toplevel :execute)
293               (setf (get ',name 'g-boxed-foreign-info)
294                     (make-g-boxed-opaque-wrapper-info :name ',name
295                                                       :g-type ,g-type-name)
296                     (gethash ,g-type-name *g-type-name->g-boxed-foreign-info*)
297                     (get ',name 'g-boxed-foreign-info))))))
298
299 (defstruct var-structure
300   name
301   parent
302   slots
303   discriminator-slot
304   variants
305   resulting-cstruct-description)
306
307 (defstruct var-structure-variant
308   discriminating-values
309   structure)
310
311 (defmethod make-load-form ((object var-structure) &optional env)
312   (make-load-form-saving-slots object :environment env))
313
314 (defmethod make-load-form ((object var-structure-variant) &optional env)
315   (make-load-form-saving-slots object :environment env))
316
317 (defun var-struct-all-slots (struct)
318   (when struct
319     (append (var-struct-all-slots (var-structure-parent struct))
320             (var-structure-slots struct))))
321
322 (defun all-structures (structure)
323   (append (iter (for variant in (var-structure-variants structure))
324                 (appending (all-structures (var-structure-variant-structure variant))))
325           (list structure)))
326
327 (defun parse-variant-structure-definition (name slots &optional parent)
328   (iter (with result = (make-var-structure :name name
329                                            :parent parent
330                                            :slots nil
331                                            :discriminator-slot nil
332                                            :variants nil))
333         (for slot in slots)
334         (if (eq :variant (first slot))
335             (progn
336               (when (var-structure-discriminator-slot result)
337                 (error "Structure has more than one discriminator slot"))
338               (setf (var-structure-discriminator-slot result) (second slot)
339                     (var-structure-variants result) (parse-variants result (nthcdr 2 slot))))
340             (push (parse-cstruct-slot slot) (var-structure-slots result)))
341         (finally (setf (var-structure-slots result)
342                        (reverse (var-structure-slots result)))
343                  (unless parent
344                    (set-variant-result-structure result))
345                  (return result))))
346
347 (defun set-variant-result-structure (var-structure)
348   (setf (var-structure-resulting-cstruct-description var-structure)
349         (make-cstruct-description
350          :name
351          (var-structure-name var-structure)
352          :slots
353          (append
354           (when (var-structure-parent var-structure)
355             (cstruct-description-slots (var-structure-resulting-cstruct-description (var-structure-parent var-structure))))
356           (var-structure-slots var-structure))))
357   (iter (for variant in (var-structure-variants var-structure))
358         (for child-var-structure = (var-structure-variant-structure variant))
359         (set-variant-result-structure child-var-structure)))
360
361 (defun ensure-list (thing)
362   (if (listp thing)
363       thing
364       (list thing)))
365
366 (defun parse-variants (parent variants)
367   (iter (for (options variant-name . slots) in variants)
368         (for variant =
369              (make-var-structure-variant
370               :discriminating-values (ensure-list options)
371               :structure (parse-variant-structure-definition variant-name slots parent)))
372         (collect variant)))
373
374 (defun generated-cstruct-name (symbol)
375   (intern (format nil "~A-CSTRUCT" (symbol-name symbol)) (symbol-package symbol)))
376
377 (defun generated-cunion-name (symbol)
378   (intern (format nil "~A-CUNION" (symbol-name symbol)) (symbol-package symbol)))
379
380 (defun generate-cstruct-1 (struct)
381   `(defcstruct ,(generated-cstruct-name (cstruct-description-name struct))
382      ,@(iter (for slot in (cstruct-description-slots struct))
383              (collect `(,(cstruct-slot-description-name slot) ,(cstruct-slot-description-type slot)
384                          ,@(when (cstruct-slot-description-count slot)
385                                  `(:count ,(cstruct-slot-description-count slot))))))))
386
387 (defun generate-c-structures (structure)
388   (iter (for str in (all-structures structure))
389         (for cstruct = (var-structure-resulting-cstruct-description str))
390         (collect (generate-cstruct-1 cstruct))))
391
392 (defun generate-variant-union (struct)
393   `(defcunion ,(generated-cunion-name (var-structure-name struct))
394      ,@(iter (for str in (all-structures struct))
395              (collect `(,(var-structure-name str)
396                          ,(generated-cstruct-name (var-structure-name str)))))))
397
398 (defun generate-structure-1 (str)
399   (let ((name (var-structure-name str)))
400     `(progn
401        (defstruct ,(if (var-structure-parent str)
402                        `(,(var-structure-name str) (:include ,(var-structure-name (var-structure-parent str))
403                                                              (,(var-structure-discriminator-slot (var-structure-parent str))
404                                                                ,(first (var-structure-variant-discriminating-values
405                                                                         (find str
406                                                                               (var-structure-variants
407                                                                                (var-structure-parent str))
408                                                                               :key #'var-structure-variant-structure))))))
409                        `,(var-structure-name str))
410          ,@(iter (for slot in (var-structure-slots str))
411                  (collect `(,(cstruct-slot-description-name slot)
412                              ,(cstruct-slot-description-initform slot)))))
413        (setf (get ',name 'structure-constructor)
414              ',(intern (format nil "MAKE-~A" (symbol-name name)) (symbol-package name))))))
415
416 (defun generate-structures (str)
417   (iter (for variant in (reverse (all-structures str)))
418         (collect (generate-structure-1 variant))))
419
420 (defun generate-native-type-decision-procedure-1 (str proxy-var)
421   (if (null (var-structure-discriminator-slot str))
422       `(values ',(var-structure-resulting-cstruct-description str))
423       `(typecase ,proxy-var
424          ,@(iter (for variant in (var-structure-variants str))
425                  (for v-str = (var-structure-variant-structure variant))
426                  (collect `(,(var-structure-name v-str)
427                              ,(generate-native-type-decision-procedure-1 v-str proxy-var))))
428          (,(var-structure-name str)
429           (values ',(var-structure-resulting-cstruct-description str))))))
430
431 (defun generate-proxy-type-decision-procedure-1 (str native-var)
432   (if (null (var-structure-discriminator-slot str))
433       `(values ',(var-structure-name str)
434                ',(var-structure-resulting-cstruct-description str))
435       `(case (foreign-slot-value ,native-var
436                                  ',(generated-cstruct-name (var-structure-name str))
437                                  ',(var-structure-discriminator-slot str))
438          ,@(iter (for variant in (var-structure-variants str))
439                  (for v-str = (var-structure-variant-structure variant))
440                  (collect `(,(var-structure-variant-discriminating-values variant)
441                              ,(generate-proxy-type-decision-procedure-1
442                                v-str
443                                native-var))))
444          (t (values ',(var-structure-name str)
445                     ',(var-structure-resulting-cstruct-description str))))))
446
447 (defun generate-proxy-type-decision-procedure (str)
448   (let ((native (gensym "NATIVE-")))
449     `(lambda (,native)
450        (declare (ignorable ,native))
451        ,(generate-proxy-type-decision-procedure-1 str native))))
452
453 (defun generate-native-type-decision-procedure (str)
454   (let ((proxy (gensym "PROXY-")))
455     `(lambda (,proxy)
456        (declare (ignorable ,proxy))
457        ,(generate-native-type-decision-procedure-1 str proxy))))
458
459 (defun compile-proxy-type-decision-procedure (str)
460   (compile nil (generate-proxy-type-decision-procedure str)))
461
462 (defun compile-native-type-decision-procedure (str)
463   (compile nil (generate-native-type-decision-procedure str)))
464
465 (defstruct (g-boxed-variant-cstruct-info (:include g-boxed-info))
466   root
467   native-type-decision-procedure
468   proxy-type-decision-procedure)
469
470 (defmethod make-load-form ((object g-boxed-variant-cstruct-info) &optional env)
471   (make-load-form-saving-slots object :environment env))
472
473 (define-foreign-type boxed-variant-cstruct-foreign-type (g-boxed-foreign-type) ())
474
475 (defmethod make-foreign-type ((info g-boxed-variant-cstruct-info) &key return-p)
476   (make-instance 'boxed-variant-cstruct-foreign-type :info info :return-p return-p))
477
478 (defmacro define-g-boxed-variant-cstruct (name g-type-name &body slots)
479   (let* ((structure (parse-variant-structure-definition name slots)))
480     `(progn ,@(generate-c-structures structure)
481             ,(generate-variant-union structure)
482             ,@(generate-structures structure)
483             (eval-when (:compile-toplevel :load-toplevel :execute)
484               (setf (get ',name 'g-boxed-foreign-info)
485                     (make-g-boxed-variant-cstruct-info :name ',name
486                                                        :g-type ,g-type-name
487                                                        :root ,structure
488                                                        :native-type-decision-procedure
489                                                        ,(generate-native-type-decision-procedure structure)
490                                                        :proxy-type-decision-procedure
491                                                        ,(generate-proxy-type-decision-procedure structure))
492                     (gethash ,g-type-name *g-type-name->g-boxed-foreign-info*)
493                     (get ',name 'g-boxed-foreign-info))))))
494
495 (defun decide-native-type (info proxy)
496   (funcall (g-boxed-variant-cstruct-info-native-type-decision-procedure info) proxy))
497
498 (defmethod boxed-copy-fn ((info g-boxed-variant-cstruct-info) native)
499   (if (g-boxed-info-g-type info)
500       (g-boxed-copy (g-boxed-info-g-type info) native)
501       (let ((copy (foreign-alloc (generated-cunion-name (g-boxed-info-name info)))))
502         (memcpy copy native (foreign-type-size (generated-cunion-name (g-boxed-info-name info))))
503         copy)))
504
505 (defmethod boxed-free-fn ((info g-boxed-variant-cstruct-info) native)
506   (if (g-boxed-info-g-type info)
507       (g-boxed-free (g-boxed-info-g-type info) native)
508       (foreign-free native)))
509
510 (defmethod translate-to-foreign (proxy (foreign-type boxed-variant-cstruct-foreign-type))
511   (if (null proxy)
512       (null-pointer)
513       (let* ((type (g-boxed-foreign-info foreign-type))
514              (cstruct-description (decide-native-type type proxy)))
515         (with-foreign-object (native-structure (generated-cunion-name
516                                                 (var-structure-name
517                                                  (g-boxed-variant-cstruct-info-root type))))
518           (copy-slots-to-native proxy native-structure cstruct-description)
519           (values (boxed-copy-fn type native-structure) proxy)))))
520
521 (defun decide-proxy-type (info native-structure)
522   (funcall (g-boxed-variant-cstruct-info-proxy-type-decision-procedure info) native-structure))
523
524 (defmethod free-translated-object (native (foreign-type boxed-variant-cstruct-foreign-type) proxy)
525   (when proxy
526     (let ((type (g-boxed-foreign-info foreign-type)))
527       (multiple-value-bind (actual-struct cstruct-description) (decide-proxy-type type native)
528         (unless (eq (type-of proxy) actual-struct)
529           (restart-case
530               (error "Expected type of boxed variant structure ~A and actual type ~A do not match"
531                      (type-of proxy) actual-struct)
532             (skip-parsing-values () (return-from free-translated-object))))
533         (copy-slots-to-proxy proxy native cstruct-description)
534         (boxed-free-fn type native)))))
535
536 (defmethod translate-from-foreign (native (foreign-type boxed-variant-cstruct-foreign-type))
537   (unless (null-pointer-p native)
538     (let ((type (g-boxed-foreign-info foreign-type)))
539       (multiple-value-bind (actual-struct cstruct-description) (decide-proxy-type type native)
540         (let ((proxy (create-structure actual-struct)))
541           (copy-slots-to-proxy proxy native cstruct-description)
542           (when (g-boxed-foreign-return-p foreign-type)
543             (boxed-free-fn type native))
544           proxy)))))
545
546 (defmethod cleanup-translated-object-for-callback ((foreign-type boxed-variant-cstruct-foreign-type) proxy native)
547   (when proxy
548     (let ((type (g-boxed-foreign-info foreign-type)))
549       (let ((cstruct-description (decide-native-type type proxy)))
550         (copy-slots-to-native proxy native cstruct-description)))))
551
552 (defgeneric boxed-parse-g-value (gvalue-ptr info))
553
554 (defgeneric boxed-set-g-value (gvalue-ptr info proxy))
555
556 (defmethod parse-g-value-for-type (gvalue-ptr (type-numeric (eql +g-type-boxed+)) parse-kind)
557   (declare (ignore parse-kind))
558   (if (g-type= (g-value-type gvalue-ptr) (g-strv-get-type))
559       (convert-from-foreign (g-value-get-boxed gvalue-ptr) '(glib:gstrv :free-from-foreign nil))
560       (let ((boxed-type (get-g-boxed-foreign-info-for-gtype (g-value-type gvalue-ptr))))
561         (boxed-parse-g-value gvalue-ptr boxed-type))))
562
563 (defmethod set-gvalue-for-type (gvalue-ptr (type-numeric (eql +g-type-boxed+)) value)
564   (if (g-type= (g-value-type gvalue-ptr) (g-strv-get-type))
565       (g-value-set-boxed gvalue-ptr (convert-to-foreign value '(glib:gstrv :free-from-foreign nil)))
566       (let ((boxed-type (get-g-boxed-foreign-info-for-gtype (g-value-type gvalue-ptr))))
567         (boxed-set-g-value gvalue-ptr boxed-type value))))
568
569 (defmethod boxed-parse-g-value (gvalue-ptr (info g-boxed-cstruct-wrapper-info))
570   (translate-from-foreign (g-value-get-boxed gvalue-ptr) (make-foreign-type info :return-p nil)))
571
572 (defmethod boxed-set-g-value (gvalue-ptr (info g-boxed-cstruct-wrapper-info) proxy)
573   (g-value-take-boxed gvalue-ptr (translate-to-foreign proxy (make-foreign-type info :return-p nil))))
574
575 (defmethod boxed-parse-g-value (gvalue-ptr (info g-boxed-variant-cstruct-info))
576   (translate-from-foreign (g-value-get-boxed gvalue-ptr) (make-foreign-type info :return-p nil)))
577
578 (defmethod boxed-set-g-value (gvalue-ptr (info g-boxed-variant-cstruct-info) proxy)
579   (g-value-take-boxed gvalue-ptr (translate-to-foreign proxy (make-foreign-type info :return-p nil))))
580
581 (defmethod boxed-parse-g-value (gvalue-ptr (info g-boxed-opaque-wrapper-info))
582   (translate-from-foreign (boxed-copy-fn info (g-value-get-boxed gvalue-ptr)) (make-foreign-type info :return-p nil)))
583
584 (defmethod boxed-set-g-value (gvalue-ptr (info g-boxed-opaque-wrapper-info) proxy)
585   (g-value-set-boxed gvalue-ptr (translate-to-foreign proxy (make-foreign-type info :return-p nil))))
586
587 (defun boxed-related-symbols (name)
588   (let ((info (get-g-boxed-foreign-info name)))
589     (etypecase info
590       (g-boxed-cstruct-wrapper-info
591        (append (list name
592                      (intern (format nil "MAKE-~A" (symbol-name name)))
593                      (intern (format nil "COPY-~A" (symbol-name name))))
594                (iter (for slot in (cstruct-description-slots (g-boxed-cstruct-wrapper-info-cstruct-description info)))
595                      (for slot-name = (cstruct-slot-description-name slot))
596                      (collect (intern (format nil "~A-~A" (symbol-name name) (symbol-name slot-name)))))))
597       (g-boxed-opaque-wrapper-info
598        (list name))
599       (g-boxed-variant-cstruct-info
600        (append (list name)
601                (iter (for var-struct in (all-structures (g-boxed-variant-cstruct-info-root info)))
602                      (for s-name = (var-structure-name var-struct))
603                      (for cstruct-description = (var-structure-resulting-cstruct-description var-struct))
604                      (appending (append (list s-name)
605                                         (list (intern (format nil "MAKE-~A" (symbol-name s-name)))
606                                               (intern (format nil "COPY-~A" (symbol-name s-name))))
607                                         (iter (for slot in (cstruct-description-slots cstruct-description))
608                                               (for slot-name = (cstruct-slot-description-name slot))
609                                               (collect (intern (format nil "~A-~A" (symbol-name s-name)
610                                                                        (symbol-name slot-name)))))))))))))
611
612 (defmacro define-boxed-opaque-accessor (boxed-name accessor-name &key type reader writer)
613   (let ((var (gensym))
614         (n-var (gensym)))
615     `(progn ,@(when reader
616                     (list (etypecase reader
617                             (symbol `(defun ,accessor-name (,var)
618                                        (funcall ,reader ,var)))
619                             (string `(defcfun (,accessor-name ,reader) ,type
620                                        (,var (g-boxed-foreign ,boxed-name)))))))
621             ,@(when writer
622                     (list (etypecase reader
623                             (symbol `(defun (setf ,accessor-name) (,n-var ,var)
624                                        (funcall ,reader ,n-var ,var)))
625                             (string `(defun (setf ,accessor-name) (,n-var ,var)
626                                        (foreign-funcall ,writer (g-boxed-foreign ,boxed-name) ,var ,type ,n-var :void)))))))))
627
628 (defun copy-boxed-slots-to-foreign (structure native-ptr &optional (type (and structure (type-of structure))))
629   (when structure
630     (copy-slots-to-native
631      structure
632      native-ptr
633      (g-boxed-cstruct-wrapper-info-cstruct-description (get-g-boxed-foreign-info type)))))
634
635 (define-compiler-macro copy-boxed-slots-to-foreign (&whole whole structure native-ptr &optional type)
636   (if (and type
637            (constantp type))
638       (let* ((type-r (eval type))
639              (f-i (get-g-boxed-foreign-info type-r)))
640         (unless f-i
641           (warn "Unknown foreign GBoxed type ~S" type-r)
642           (return-from copy-boxed-slots-to-foreign whole))
643         (unless (typep f-i 'g-boxed-cstruct-wrapper-info)
644           (warn "Foreign GBoxed type ~S is not a C structure wrapper" type-r)
645           (return-from copy-boxed-slots-to-foreign whole))
646         `(when ,structure
647            (copy-slots-to-native
648             ,structure
649             ,native-ptr
650             (load-time-value (g-boxed-cstruct-wrapper-info-cstruct-description (get-g-boxed-foreign-info ',type-r))))))
651       whole))
652
653 (defmacro with-foreign-boxed-array ((n-var array-var type values-seq) &body body)
654   (let ((values-seq-1 (gensym "VALUES-SEQ-"))
655         (cstruct (generated-cstruct-name type))
656         (x (gensym "X-"))
657         (i (gensym "I-")))
658     `(let* ((,values-seq-1 ,values-seq)
659             (,n-var (length ,values-seq-1)))
660        (with-foreign-object (,array-var ',cstruct ,n-var)
661          (let ((,i 0))
662            (map nil (lambda (,x)
663                       (copy-boxed-slots-to-foreign
664                        ,x
665                        (inc-pointer ,array-var (* ,i (foreign-type-size ',cstruct)))
666                        ',type)
667                       (incf ,i))
668                 ,values-seq-1))
669          ,@body))))