112ac1d3 |
1 | ;; Common Lisp bindings for GTK+ v2.x |
74821f75 |
2 | ;; Copyright 2000-2006 Espen S. Johnsen <espen@users.sf.net> |
560af5c5 |
3 | ;; |
112ac1d3 |
4 | ;; Permission is hereby granted, free of charge, to any person obtaining |
5 | ;; a copy of this software and associated documentation files (the |
6 | ;; "Software"), to deal in the Software without restriction, including |
7 | ;; without limitation the rights to use, copy, modify, merge, publish, |
8 | ;; distribute, sublicense, and/or sell copies of the Software, and to |
9 | ;; permit persons to whom the Software is furnished to do so, subject to |
10 | ;; the following conditions: |
560af5c5 |
11 | ;; |
112ac1d3 |
12 | ;; The above copyright notice and this permission notice shall be |
13 | ;; included in all copies or substantial portions of the Software. |
560af5c5 |
14 | ;; |
112ac1d3 |
15 | ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
16 | ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
17 | ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
18 | ;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
19 | ;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
20 | ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
21 | ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
560af5c5 |
22 | |
74821f75 |
23 | ;; $Id: gobject.lisp,v 1.52 2006-04-25 22:10:36 espen Exp $ |
560af5c5 |
24 | |
25 | (in-package "GLIB") |
26 | |
27 | |
2176a9c7 |
28 | ;;;; Metaclass used for subclasses of gobject |
29 | |
30 | (eval-when (:compile-toplevel :load-toplevel :execute) |
a6e339e1 |
31 | ;; (push :debug-ref-counting *features*) |
2176a9c7 |
32 | (defclass gobject-class (ginstance-class) |
74821f75 |
33 | ((instance-slots-p :initform nil :reader instance-slots-p |
b036d261 |
34 | :documentation "Non NIL if the class has slots with instance allocation"))) |
74821f75 |
35 | (defmethod shared-initialize ((class gobject-class) names &rest initargs) |
36 | (declare (ignore names initargs)) |
37 | (call-next-method) |
38 | (unless (slot-boundp class 'ref) |
39 | (setf (slot-value class 'ref) '%object-ref)) |
40 | (unless (slot-boundp class 'unref) |
41 | (setf (slot-value class 'unref) '%object-unref))) |
2176a9c7 |
42 | |
73572c12 |
43 | (defmethod validate-superclass ((class gobject-class) (super standard-class)) |
2176a9c7 |
44 | ; (subtypep (class-name super) 'gobject) |
45 | t)) |
46 | |
47 | (defclass direct-property-slot-definition (direct-virtual-slot-definition) |
48 | ((pname :reader slot-definition-pname :initarg :pname) |
e624bc26 |
49 | (readable :reader slot-readable-p :initarg :readable) |
50 | (writable :reader slot-writable-p :initarg :writable) |
51 | (construct-only :initarg :construct-only :reader construct-only-property-p))) |
2176a9c7 |
52 | |
53 | (defclass effective-property-slot-definition (effective-virtual-slot-definition) |
54 | ((pname :reader slot-definition-pname :initarg :pname) |
74821f75 |
55 | (readable :initform t :reader slot-readable-p :initarg :readable) |
56 | (writable :initform t :reader slot-writable-p :initarg :writable) |
57 | (construct-only :initform nil :initarg :construct-only :reader construct-only-property-p))) |
174e8a5c |
58 | |
59 | (defclass direct-user-data-slot-definition (direct-virtual-slot-definition) |
60 | ()) |
61 | |
62 | (defclass effective-user-data-slot-definition (effective-virtual-slot-definition) |
63 | ()) |
64 | |
2176a9c7 |
65 | |
74821f75 |
66 | (defmethod slot-readable-p ((slotd standard-effective-slot-definition)) |
67 | (declare (ignore slotd)) |
68 | t) |
69 | |
70 | (defmethod slot-writable-p ((slotd standard-effective-slot-definition)) |
71 | (declare (ignore slotd)) |
72 | t) |
73 | |
74 | |
2176a9c7 |
75 | (defbinding %object-ref () pointer |
76 | (location pointer)) |
77 | |
78 | (defbinding %object-unref () nil |
79 | (location pointer)) |
80 | |
74821f75 |
81 | #?(pkg-exists-p "glib-2.0" :atleast-version "2.8.0") |
008c81d9 |
82 | (progn |
56ccd5b7 |
83 | (define-callback toggle-ref-callback nil |
84 | ((data pointer) (location pointer) (last-ref-p boolean)) |
75689fea |
85 | (declare (ignore data)) |
09f6e237 |
86 | #+debug-ref-counting |
87 | (if last-ref-p |
74821f75 |
88 | (format t "Object at 0x~8,'0X has no foreign references~%" (pointer-address location)) |
89 | (format t "Foreign reference added to object at 0x~8,'0X~%" (pointer-address location))) |
008c81d9 |
90 | (if last-ref-p |
91 | (cache-instance (find-cached-instance location) t) |
92 | (cache-instance (find-cached-instance location) nil))) |
93 | |
f40eddb4 |
94 | (defbinding %object-add-toggle-ref (location) pointer |
008c81d9 |
95 | (location pointer) |
56ccd5b7 |
96 | (toggle-ref-callback callback) |
008c81d9 |
97 | (nil null)) |
98 | |
f40eddb4 |
99 | (defbinding %object-remove-toggle-ref (location) pointer |
008c81d9 |
100 | (location pointer) |
56ccd5b7 |
101 | (toggle-ref-callback callback) |
008c81d9 |
102 | (nil null))) |
b036d261 |
103 | |
008c81d9 |
104 | #+debug-ref-counting |
105 | (progn |
56ccd5b7 |
106 | (define-callback weak-ref-callback nil ((data pointer) (location pointer)) |
74821f75 |
107 | (format t "Object at 0x~8,'0X (~A) being finalized~%" (pointer-address location) (type-from-number (%type-number-of-ginstance location)))) |
008c81d9 |
108 | |
f40eddb4 |
109 | (defbinding %object-weak-ref (location) pointer |
008c81d9 |
110 | (location pointer) |
56ccd5b7 |
111 | (weak-ref-callback callback) |
008c81d9 |
112 | (nil null))) |
2176a9c7 |
113 | |
114 | |
115 | ; (defbinding object-class-install-param () nil |
116 | ; (class pointer) |
117 | ; (id unsigned-int) |
118 | ; (parameter parameter)) |
119 | |
120 | ; (defbinding object-class-find-param-spec () parameter |
121 | ; (class pointer) |
122 | ; (name string)) |
123 | |
124 | (defun signal-name-to-string (name) |
125 | (substitute #\_ #\- (string-downcase (string name)))) |
126 | |
127 | |
128 | (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs) |
129 | (case (getf initargs :allocation) |
130 | (:property (find-class 'direct-property-slot-definition)) |
174e8a5c |
131 | (:user-data (find-class 'direct-user-data-slot-definition)) |
2176a9c7 |
132 | (t (call-next-method)))) |
133 | |
134 | (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs) |
135 | (case (getf initargs :allocation) |
136 | (:property (find-class 'effective-property-slot-definition)) |
174e8a5c |
137 | (:user-data (find-class 'effective-user-data-slot-definition)) |
2176a9c7 |
138 | (t (call-next-method)))) |
139 | |
140 | (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds) |
b19bbc94 |
141 | (if (eq (slot-definition-allocation (first direct-slotds)) :property) |
74821f75 |
142 | (nconc |
143 | (compute-most-specific-initargs direct-slotds |
144 | '(pname construct-only readable writable)) |
2176a9c7 |
145 | (call-next-method)) |
146 | (call-next-method))) |
147 | |
148 | |
e624bc26 |
149 | (defvar *ignore-setting-construct-only-property* nil) |
150 | (declaim (special *ignore-setting-construct-only-property*)) |
151 | |
74821f75 |
152 | (defmethod compute-slot-reader-function ((slotd effective-property-slot-definition)) |
153 | (if (slot-readable-p slotd) |
154 | (let* ((type (slot-definition-type slotd)) |
155 | (pname (slot-definition-pname slotd)) |
156 | (reader (reader-function type :ref :get))) |
157 | #'(lambda (object) |
158 | (with-memory (gvalue +gvalue-size+) |
159 | (%gvalue-init gvalue (find-type-number type)) |
160 | (%object-get-property object pname gvalue) |
161 | (funcall reader gvalue +gvalue-value-offset+)))) |
162 | (call-next-method))) |
eeda1c2d |
163 | |
74821f75 |
164 | (defmethod compute-slot-writer-function ((slotd effective-property-slot-definition)) |
165 | (cond |
166 | ((slot-writable-p slotd) |
167 | (let* ((type (slot-definition-type slotd)) |
168 | (pname (slot-definition-pname slotd)) |
169 | (writer (writer-function type :temp t)) |
170 | (destroy (destroy-function type :temp t))) |
171 | #'(lambda (value object) |
172 | (with-memory (gvalue +gvalue-size+) |
173 | (%gvalue-init gvalue (find-type-number type)) |
174 | (funcall writer value gvalue +gvalue-value-offset+) |
175 | (%object-set-property object pname gvalue) |
176 | (funcall destroy gvalue +gvalue-value-offset+)) |
177 | value))) |
178 | |
179 | ((construct-only-property-p slotd) |
180 | #'(lambda (value object) |
181 | (declare (ignore value object)) |
182 | (unless *ignore-setting-construct-only-property* |
183 | (error 'unwritable-slot :name (slot-definition-name slotd) :instance object)))) |
184 | ((call-next-method)))) |
185 | |
186 | (defmethod compute-slot-reader-function ((slotd effective-user-data-slot-definition)) |
187 | (let ((slot-name (slot-definition-name slotd))) |
188 | #'(lambda (object) |
189 | (user-data object slot-name)))) |
2176a9c7 |
190 | |
74821f75 |
191 | (defmethod compute-slot-boundp-function ((slotd effective-user-data-slot-definition)) |
174e8a5c |
192 | (let ((slot-name (slot-definition-name slotd))) |
74821f75 |
193 | #'(lambda (object) |
194 | (user-data-p object slot-name)))) |
195 | |
196 | (defmethod compute-slot-writer-function ((slotd effective-user-data-slot-definition)) |
197 | (let ((slot-name (slot-definition-name slotd))) |
198 | #'(lambda (value object) |
199 | (setf (user-data object slot-name) value)))) |
200 | |
201 | (defmethod compute-slot-makunbound-function ((slotd effective-user-data-slot-definition)) |
202 | (let ((slot-name (slot-definition-name slotd))) |
203 | #'(lambda (object) |
204 | (unset-user-data object slot-name)))) |
205 | |
206 | (defmethod compute-slots :around ((class gobject-class)) |
207 | (let ((slots (call-next-method))) |
208 | (when (some #'(lambda (slotd) |
209 | (and |
210 | (eq (slot-definition-allocation slotd) :instance) |
211 | (not (typep slotd 'effective-special-slot-definition)))) |
212 | slots) |
213 | (setf (slot-value class 'instance-slots-p) t)) |
214 | slots)) |
b036d261 |
215 | |
216 | |
2176a9c7 |
217 | ;;;; Super class for all classes in the GObject type hierarchy |
218 | |
560af5c5 |
219 | (eval-when (:compile-toplevel :load-toplevel :execute) |
c8c48a4c |
220 | (defclass gobject (ginstance) |
a6e339e1 |
221 | (#+debug-ref-counting |
222 | (ref-count :allocation :alien :type int :reader ref-count)) |
2176a9c7 |
223 | (:metaclass gobject-class) |
dfa4f314 |
224 | (:gtype "GObject"))) |
9adccb27 |
225 | |
a6e339e1 |
226 | #+debug-ref-counting |
227 | (defmethod print-object ((instance gobject) stream) |
228 | (print-unreadable-object (instance stream :type t :identity nil) |
c0e19882 |
229 | (if (proxy-valid-p instance) |
74821f75 |
230 | (format stream "at 0x~X (~D)" (pointer-address (foreign-location instance)) (ref-count instance)) |
a6e339e1 |
231 | (write-string "at \"unbound\"" stream)))) |
232 | |
eeda1c2d |
233 | |
74821f75 |
234 | (define-type-method reader-function ((type gobject) &key (ref :read) inlined) |
235 | (assert-not-inlined type inlined) |
236 | (ecase ref |
237 | ((:read :peek) (call-next-method type :ref :read)) |
238 | (:get |
239 | #'(lambda (location &optional (offset 0)) |
240 | (let ((instance (ref-pointer location offset))) |
241 | (unless (null-pointer-p instance) |
242 | (multiple-value-bind (gobject new-p) |
243 | (ensure-proxy-instance 'gobject instance :reference nil) |
244 | (unless new-p |
245 | (%object-unref instance)) |
246 | (setf (ref-pointer location offset) (make-pointer 0)) |
247 | gobject))))))) |
248 | |
249 | (define-type-method callback-wrapper ((type gobject) var arg form) |
250 | (let ((class (type-expand type))) |
251 | `(let ((,var (ensure-proxy-instance ',class ,arg))) |
252 | ,form))) |
253 | |
eeda1c2d |
254 | (defun initial-add (object function initargs key pkey) |
255 | (loop |
256 | as (initarg value . rest) = initargs then rest |
257 | do (cond |
258 | ((eq initarg key) (funcall function object value)) |
259 | ((eq initarg pkey) (mapc #'(lambda (value) |
260 | (funcall function object value)) |
261 | value))) |
262 | while rest)) |
263 | |
264 | (defun initial-apply-add (object function initargs key pkey) |
265 | (initial-add object #'(lambda (object value) |
266 | (apply function object (mklist value))) |
267 | initargs key pkey)) |
268 | |
269 | |
8958fa4a |
270 | (defmethod make-proxy-instance ((class gobject-class) location &rest initargs) |
271 | (declare (ignore location initargs)) |
272 | (if (slot-value class 'instance-slots-p) |
74821f75 |
273 | (error "Objects of class ~A has instance slots and should only be created with MAKE-INSTANCE" class) |
8958fa4a |
274 | (call-next-method))) |
275 | |
e624bc26 |
276 | |
277 | (defmethod allocate-foreign ((object gobject) &rest initargs) |
278 | (let ((init-slots ())) |
279 | (flet ((value-from-initargs (slotd) |
280 | (loop |
281 | with slot-initargs = (slot-definition-initargs slotd) |
282 | for (initarg value) on initargs by #'cddr |
283 | when (find initarg slot-initargs) |
284 | do (return (values value t))))) |
285 | |
286 | (loop |
287 | for slotd in (class-slots (class-of object)) |
288 | when (and |
289 | (eq (slot-definition-allocation slotd) :property) |
290 | (construct-only-property-p slotd)) |
291 | do (multiple-value-bind (value initarg-p) (value-from-initargs slotd) |
292 | (cond |
293 | (initarg-p (push (cons slotd value) init-slots)) |
294 | ((slot-definition-initfunction slotd) |
295 | (push |
296 | (cons slotd (funcall (slot-definition-initfunction slotd))) |
297 | init-slots)))))) |
298 | |
299 | (cond |
300 | (init-slots |
74821f75 |
301 | (let* ((pointer-size (size-of 'pointer)) |
302 | (element-size (+ +gvalue-size+ pointer-size)) |
303 | (num-slots (length init-slots))) |
304 | (with-memory (params (* num-slots element-size)) |
e624bc26 |
305 | (loop |
306 | with string-writer = (writer-function 'string) |
307 | for (slotd . value) in init-slots |
74821f75 |
308 | as param = params then (pointer+ param element-size) |
e624bc26 |
309 | as type = (slot-definition-type slotd) |
310 | as pname = (slot-definition-pname slotd) |
74821f75 |
311 | do (funcall string-writer pname param) |
312 | (gvalue-init (pointer+ param pointer-size) type value)) |
e624bc26 |
313 | |
314 | (unwind-protect |
315 | (%gobject-newv (type-number-of object) num-slots params) |
316 | |
317 | (loop |
318 | with string-destroy = (destroy-function 'string) |
319 | repeat num-slots |
74821f75 |
320 | as param = params then (pointer+ param element-size) |
321 | do (funcall string-destroy param) |
322 | (gvalue-unset (pointer+ param pointer-size))))))) |
e624bc26 |
323 | |
324 | (t (%gobject-new (type-number-of object)))))) |
325 | |
326 | |
327 | (defmethod shared-initialize ((object gobject) names &rest initargs) |
328 | (declare (ignore names initargs)) |
329 | (let ((*ignore-setting-construct-only-property* t)) |
330 | (call-next-method))) |
331 | |
008c81d9 |
332 | (defmethod initialize-instance :around ((object gobject) &rest initargs) |
333 | (declare (ignore initargs)) |
e624bc26 |
334 | (prog1 |
335 | (call-next-method) |
336 | #+debug-ref-counting(%object-weak-ref (foreign-location object)) |
74821f75 |
337 | #?(pkg-exists-p "glib-2.0" :atleast-version "2.8.0") |
e624bc26 |
338 | (when (slot-value (class-of object) 'instance-slots-p) |
74821f75 |
339 | (%object-add-toggle-ref (foreign-location object)) |
340 | (%object-unref (foreign-location object))))) |
4d83a8a6 |
341 | |
342 | |
1dbf4216 |
343 | (defmethod instance-finalizer ((instance gobject)) |
09f6e237 |
344 | (let ((location (foreign-location instance))) |
74821f75 |
345 | #?(pkg-exists-p "glib-2.0" :atleast-version "2.8.0") |
b036d261 |
346 | (if (slot-value (class-of instance) 'instance-slots-p) |
347 | #'(lambda () |
008c81d9 |
348 | #+debug-ref-counting |
74821f75 |
349 | (format t "Finalizing proxy for 0x~8,'0X~%" (pointer-address location)) |
b036d261 |
350 | (%object-remove-toggle-ref location)) |
351 | #'(lambda () |
008c81d9 |
352 | #+debug-ref-counting |
74821f75 |
353 | (format t "Finalizing proxy for 0x~8,'0X~%" (pointer-address location)) |
5eb27832 |
354 | (%object-unref location))) |
74821f75 |
355 | #?-(pkg-exists-p "glib-2.0" :atleast-version "2.8.0") |
5eb27832 |
356 | #'(lambda () |
74821f75 |
357 | (%object-unref location)))) |
1dbf4216 |
358 | |
9adccb27 |
359 | |
d4b21b08 |
360 | (defbinding (%gobject-new "g_object_new") () pointer |
a9044181 |
361 | (type type-number) |
362 | (nil null)) |
560af5c5 |
363 | |
9adccb27 |
364 | (defbinding (%gobject-newv "g_object_newv") () pointer |
4d83a8a6 |
365 | (type type-number) |
366 | (n-parameters unsigned-int) |
9adccb27 |
367 | (params pointer)) |
de8516ca |
368 | |
369 | |
d4b21b08 |
370 | |
323d4265 |
371 | ;;;; Property stuff |
560af5c5 |
372 | |
26b133ed |
373 | (defbinding %object-set-property () nil |
c8c48a4c |
374 | (object gobject) |
375 | (name string) |
376 | (value gvalue)) |
86d9d6ab |
377 | |
26b133ed |
378 | (defbinding %object-get-property () nil |
c8c48a4c |
379 | (object gobject) |
380 | (name string) |
a9044181 |
381 | (value gvalue)) |
86d9d6ab |
382 | |
26b133ed |
383 | (defbinding %object-notify () nil |
c8c48a4c |
384 | (object gobject) |
385 | (name string)) |
86d9d6ab |
386 | |
26b133ed |
387 | (defbinding object-freeze-notify () nil |
a9044181 |
388 | (object gobject)) |
86d9d6ab |
389 | |
26b133ed |
390 | (defbinding object-thaw-notify () nil |
a9044181 |
391 | (object gobject)) |
86d9d6ab |
392 | |
a00ba56a |
393 | |
394 | ;;;; User data |
395 | |
26b133ed |
396 | (defbinding %object-set-qdata-full () nil |
86d9d6ab |
397 | (object gobject) |
398 | (id quark) |
399 | (data unsigned-long) |
56ccd5b7 |
400 | (destroy-marshal callback)) |
86d9d6ab |
401 | |
56ccd5b7 |
402 | (define-callback user-data-destroy-callback nil ((id unsigned-int)) |
73572c12 |
403 | (destroy-user-data id)) |
404 | |
174e8a5c |
405 | (defun (setf user-data) (data object key) |
a00ba56a |
406 | (%object-set-qdata-full object (quark-intern key) |
56ccd5b7 |
407 | (register-user-data data) user-data-destroy-callback) |
86d9d6ab |
408 | data) |
409 | |
26b133ed |
410 | (defbinding %object-get-qdata () unsigned-long |
86d9d6ab |
411 | (object gobject) |
412 | (id quark)) |
413 | |
174e8a5c |
414 | (defun user-data (object key) |
a00ba56a |
415 | (find-user-data (%object-get-qdata object (quark-intern key)))) |
174e8a5c |
416 | |
174e8a5c |
417 | (defun user-data-p (object key) |
a00ba56a |
418 | (user-data-exists-p (%object-get-qdata object (quark-intern key)))) |
419 | |
420 | (defbinding %object-steal-qdata () unsigned-long |
421 | (object gobject) |
422 | (id quark)) |
423 | |
424 | (defun unset-user-data (object key) |
425 | (destroy-user-data (%object-steal-qdata object (quark-intern key)))) |
86d9d6ab |
426 | |
427 | |
d4b21b08 |
428 | ;;;; |
429 | |
323d4265 |
430 | (defbinding %object-class-list-properties () pointer |
d4b21b08 |
431 | (class pointer) |
432 | (n-properties unsigned-int :out)) |
433 | |
9c03a07c |
434 | |
435 | (defun %map-params (params length type inherited-p) |
436 | (if inherited-p |
9adccb27 |
437 | (map-c-vector 'list #'identity params 'param length) |
9c03a07c |
438 | (let ((properties ())) |
9adccb27 |
439 | (map-c-vector 'list |
9c03a07c |
440 | #'(lambda (param) |
441 | (when (eql (param-owner-type param) type) |
442 | (push param properties))) |
443 | params 'param length) |
444 | (nreverse properties)))) |
445 | |
446 | (defun query-object-class-properties (type &optional inherited-p) |
735a29da |
447 | (let* ((type-number (find-type-number type t)) |
9c03a07c |
448 | (class (type-class-ref type-number))) |
449 | (unwind-protect |
450 | (multiple-value-bind (array length) |
451 | (%object-class-list-properties class) |
21299acf |
452 | (unless (null-pointer-p array) |
453 | (unwind-protect |
454 | (%map-params array length type-number inherited-p) |
455 | (deallocate-memory array)))) |
9c03a07c |
456 | ; (type-class-unref type-number) |
457 | ))) |
d4b21b08 |
458 | |
459 | |
460 | (defun default-slot-name (name) |
74821f75 |
461 | (let ((prefix-len (length (package-prefix)))) |
462 | (intern (substitute #\- #\_ |
463 | (string-upcase |
464 | (if (and |
465 | (string-prefix-p (package-prefix) name) |
466 | (char= #\- (char name prefix-len))) |
467 | (subseq name (1+ prefix-len)) |
468 | name)))))) |
d4b21b08 |
469 | |
470 | (defun default-slot-accessor (class-name slot-name type) |
471 | (intern |
472 | (format |
473 | nil "~A-~A~A" class-name slot-name |
4d83a8a6 |
474 | (if (eq type 'boolean) "-P" "")))) |
d4b21b08 |
475 | |
323d4265 |
476 | |
646466b0 |
477 | (defun slot-definition-from-property (class property &optional slot-name args) |
9c03a07c |
478 | (with-slots (name flags value-type documentation) property |
646466b0 |
479 | (let* ((slot-name (or slot-name (default-slot-name name))) |
62f12808 |
480 | (slot-type (or (getf args :type) (type-from-number value-type) 'pointer)) |
9c03a07c |
481 | (accessor (default-slot-accessor class slot-name slot-type))) |
482 | |
483 | `(,slot-name |
484 | :allocation :property :pname ,name |
eeda1c2d |
485 | |
735a29da |
486 | ,@(when (find :unbound args) (list :unbound (getf args :unbound))) |
487 | ,@(when (find :getter args) (list :getter (getf args :getter))) |
488 | ,@(when (find :setter args) (list :setter (getf args :setter))) |
9c03a07c |
489 | |
490 | ;; accessors |
491 | ,@(cond |
492 | ((and |
493 | (member :writable flags) (member :readable flags) |
494 | (not (member :construct-only flags))) |
495 | (list :accessor accessor)) |
496 | ((and (member :writable flags) (not (member :construct-only flags))) |
497 | (list :writer `(setf ,accessor))) |
498 | ((member :readable flags) |
499 | (list :reader accessor))) |
500 | |
501 | ;; readable/writable/construct |
502 | ,@(when (or (not (member :writable flags)) |
503 | (member :construct-only flags)) |
504 | '(:writable nil)) |
505 | ,@(when (not (member :readable flags)) |
506 | '(:readable nil)) |
e624bc26 |
507 | ,@(when (member :construct-only flags) |
508 | '(:construct-only t)) |
9c03a07c |
509 | |
510 | ;; initargs |
e5c7586b |
511 | ,@(if (find :initarg args) |
512 | (let ((initarg (getf args :initarg))) |
513 | (etypecase initarg |
514 | (null ()) |
515 | (symbol `(:initarg ,initarg)))) |
516 | (when (or (member :construct flags) |
517 | (member :construct-only flags) |
518 | (member :writable flags)) |
519 | (list :initarg (intern (string slot-name) "KEYWORD")))) |
9c03a07c |
520 | |
521 | :type ,slot-type |
522 | :documentation ,documentation)))) |
523 | |
524 | |
525 | (defun slot-definitions (class properties slots) |
526 | (loop |
9c03a07c |
527 | for property in properties |
eeda1c2d |
528 | as slot = (or |
529 | (find (param-name property) slots |
530 | :key #'(lambda (slot) (getf (rest slot) :pname)) |
531 | :test #'string=) |
532 | (find (param-name property) slots |
533 | :key #'first :test #'string-equal)) |
534 | do (cond |
535 | ((not slot) |
536 | (push (slot-definition-from-property class property) slots)) |
537 | ((getf (rest slot) :merge) |
538 | (setf |
539 | (rest slot) |
646466b0 |
540 | (rest (slot-definition-from-property class property (first slot) (rest slot))))))) |
9c03a07c |
541 | (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots)) |
542 | |
543 | |
62f12808 |
544 | (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class)) |
9c03a07c |
545 | (let ((supers (cons (supertype type) (implements type))) |
546 | (class (type-from-number type)) |
74821f75 |
547 | (slots (getf options :slots))) |
9c03a07c |
548 | `(defclass ,class ,supers |
735a29da |
549 | ,(unless forward-p |
550 | (slot-definitions class (query-object-class-properties type) slots)) |
551 | (:metaclass ,metaclass) |
552 | (:gtype ,(register-type-as type))))) |
323d4265 |
553 | |
74821f75 |
554 | (defun gobject-dependencies (type options) |
21299acf |
555 | (delete-duplicates |
556 | (cons |
557 | (supertype type) |
558 | (append |
559 | (type-interfaces type) |
74821f75 |
560 | (mapcar #'param-value-type (query-object-class-properties type)) |
561 | (getf options :dependencies) |
562 | (loop |
563 | for slot in (getf options :slots) |
564 | as type = (getf (rest slot) :type) |
565 | when (and type (symbolp type) (find-type-number type)) |
566 | collect (find-type-number type)))))) |
d4b21b08 |
567 | |
62f12808 |
568 | |
569 | (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies) |
ae37d096 |
570 | |
571 | |
572 | ;;; Pseudo type for gobject instances which have their reference count |
573 | ;;; increased by the returning function |
574 | |
4555db90 |
575 | (deftype referenced (type) type) |
ae37d096 |
576 | |
74821f75 |
577 | (define-type-method from-alien-form ((type referenced) form &key (ref :free)) |
578 | (cond |
579 | ((not (eq ref :free)) |
580 | (error "Keyword arg :REF to FROM-ALIEN-FORM should be :FREE for type ~A. It was give ~A" type ref)) |
581 | ((subtypep type 'gobject) |
582 | (from-alien-form (second (type-expand-to 'referenced type)) form :ref ref)))) |
583 | |
584 | (define-type-method from-alien-function ((type referenced) &key (ref :free)) |
585 | (cond |
586 | ((not (eq ref :free)) |
587 | (error "Keyword arg :REF to FROM-ALIEN-FUNCTION should be :FREE for type ~A. It was give ~A" type ref)) |
588 | ; ((subtypep type 'gobject) (call-next-method type ref :free)))) |
589 | ((subtypep type 'gobject) |
590 | (from-alien-function (second (type-expand-to 'referenced type)) :ref ref)))) |