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 | |
584285fb |
23 | ;; $Id: gobject.lisp,v 1.53 2006-08-16 11:02:46 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 | |
584285fb |
152 | (defmethod compute-slot-reader-function ((slotd effective-property-slot-definition) &optional signal-unbound-p) |
153 | (declare (ignore signal-unbound-p)) |
74821f75 |
154 | (if (slot-readable-p slotd) |
155 | (let* ((type (slot-definition-type slotd)) |
156 | (pname (slot-definition-pname slotd)) |
157 | (reader (reader-function type :ref :get))) |
158 | #'(lambda (object) |
159 | (with-memory (gvalue +gvalue-size+) |
160 | (%gvalue-init gvalue (find-type-number type)) |
161 | (%object-get-property object pname gvalue) |
162 | (funcall reader gvalue +gvalue-value-offset+)))) |
163 | (call-next-method))) |
eeda1c2d |
164 | |
74821f75 |
165 | (defmethod compute-slot-writer-function ((slotd effective-property-slot-definition)) |
166 | (cond |
167 | ((slot-writable-p slotd) |
168 | (let* ((type (slot-definition-type slotd)) |
169 | (pname (slot-definition-pname slotd)) |
170 | (writer (writer-function type :temp t)) |
171 | (destroy (destroy-function type :temp t))) |
172 | #'(lambda (value object) |
173 | (with-memory (gvalue +gvalue-size+) |
174 | (%gvalue-init gvalue (find-type-number type)) |
175 | (funcall writer value gvalue +gvalue-value-offset+) |
176 | (%object-set-property object pname gvalue) |
177 | (funcall destroy gvalue +gvalue-value-offset+)) |
178 | value))) |
179 | |
180 | ((construct-only-property-p slotd) |
181 | #'(lambda (value object) |
182 | (declare (ignore value object)) |
183 | (unless *ignore-setting-construct-only-property* |
184 | (error 'unwritable-slot :name (slot-definition-name slotd) :instance object)))) |
185 | ((call-next-method)))) |
186 | |
584285fb |
187 | (defmethod compute-slot-reader-function ((slotd effective-user-data-slot-definition) &optional signal-unbound-p) |
188 | (declare (ignore signal-unbound-p)) |
74821f75 |
189 | (let ((slot-name (slot-definition-name slotd))) |
190 | #'(lambda (object) |
191 | (user-data object slot-name)))) |
2176a9c7 |
192 | |
74821f75 |
193 | (defmethod compute-slot-boundp-function ((slotd effective-user-data-slot-definition)) |
174e8a5c |
194 | (let ((slot-name (slot-definition-name slotd))) |
74821f75 |
195 | #'(lambda (object) |
196 | (user-data-p object slot-name)))) |
197 | |
198 | (defmethod compute-slot-writer-function ((slotd effective-user-data-slot-definition)) |
199 | (let ((slot-name (slot-definition-name slotd))) |
200 | #'(lambda (value object) |
201 | (setf (user-data object slot-name) value)))) |
202 | |
203 | (defmethod compute-slot-makunbound-function ((slotd effective-user-data-slot-definition)) |
204 | (let ((slot-name (slot-definition-name slotd))) |
205 | #'(lambda (object) |
206 | (unset-user-data object slot-name)))) |
207 | |
208 | (defmethod compute-slots :around ((class gobject-class)) |
209 | (let ((slots (call-next-method))) |
210 | (when (some #'(lambda (slotd) |
211 | (and |
212 | (eq (slot-definition-allocation slotd) :instance) |
213 | (not (typep slotd 'effective-special-slot-definition)))) |
214 | slots) |
215 | (setf (slot-value class 'instance-slots-p) t)) |
216 | slots)) |
b036d261 |
217 | |
218 | |
2176a9c7 |
219 | ;;;; Super class for all classes in the GObject type hierarchy |
220 | |
560af5c5 |
221 | (eval-when (:compile-toplevel :load-toplevel :execute) |
c8c48a4c |
222 | (defclass gobject (ginstance) |
a6e339e1 |
223 | (#+debug-ref-counting |
224 | (ref-count :allocation :alien :type int :reader ref-count)) |
2176a9c7 |
225 | (:metaclass gobject-class) |
dfa4f314 |
226 | (:gtype "GObject"))) |
9adccb27 |
227 | |
a6e339e1 |
228 | #+debug-ref-counting |
229 | (defmethod print-object ((instance gobject) stream) |
230 | (print-unreadable-object (instance stream :type t :identity nil) |
c0e19882 |
231 | (if (proxy-valid-p instance) |
74821f75 |
232 | (format stream "at 0x~X (~D)" (pointer-address (foreign-location instance)) (ref-count instance)) |
a6e339e1 |
233 | (write-string "at \"unbound\"" stream)))) |
234 | |
eeda1c2d |
235 | |
74821f75 |
236 | (define-type-method reader-function ((type gobject) &key (ref :read) inlined) |
237 | (assert-not-inlined type inlined) |
238 | (ecase ref |
239 | ((:read :peek) (call-next-method type :ref :read)) |
240 | (:get |
241 | #'(lambda (location &optional (offset 0)) |
242 | (let ((instance (ref-pointer location offset))) |
243 | (unless (null-pointer-p instance) |
244 | (multiple-value-bind (gobject new-p) |
245 | (ensure-proxy-instance 'gobject instance :reference nil) |
246 | (unless new-p |
247 | (%object-unref instance)) |
248 | (setf (ref-pointer location offset) (make-pointer 0)) |
249 | gobject))))))) |
250 | |
251 | (define-type-method callback-wrapper ((type gobject) var arg form) |
252 | (let ((class (type-expand type))) |
253 | `(let ((,var (ensure-proxy-instance ',class ,arg))) |
254 | ,form))) |
255 | |
eeda1c2d |
256 | (defun initial-add (object function initargs key pkey) |
257 | (loop |
258 | as (initarg value . rest) = initargs then rest |
259 | do (cond |
260 | ((eq initarg key) (funcall function object value)) |
261 | ((eq initarg pkey) (mapc #'(lambda (value) |
262 | (funcall function object value)) |
263 | value))) |
264 | while rest)) |
265 | |
266 | (defun initial-apply-add (object function initargs key pkey) |
267 | (initial-add object #'(lambda (object value) |
268 | (apply function object (mklist value))) |
269 | initargs key pkey)) |
270 | |
271 | |
8958fa4a |
272 | (defmethod make-proxy-instance ((class gobject-class) location &rest initargs) |
273 | (declare (ignore location initargs)) |
274 | (if (slot-value class 'instance-slots-p) |
74821f75 |
275 | (error "Objects of class ~A has instance slots and should only be created with MAKE-INSTANCE" class) |
8958fa4a |
276 | (call-next-method))) |
277 | |
e624bc26 |
278 | |
279 | (defmethod allocate-foreign ((object gobject) &rest initargs) |
280 | (let ((init-slots ())) |
281 | (flet ((value-from-initargs (slotd) |
282 | (loop |
283 | with slot-initargs = (slot-definition-initargs slotd) |
284 | for (initarg value) on initargs by #'cddr |
285 | when (find initarg slot-initargs) |
286 | do (return (values value t))))) |
287 | |
288 | (loop |
289 | for slotd in (class-slots (class-of object)) |
290 | when (and |
291 | (eq (slot-definition-allocation slotd) :property) |
292 | (construct-only-property-p slotd)) |
293 | do (multiple-value-bind (value initarg-p) (value-from-initargs slotd) |
294 | (cond |
295 | (initarg-p (push (cons slotd value) init-slots)) |
296 | ((slot-definition-initfunction slotd) |
297 | (push |
298 | (cons slotd (funcall (slot-definition-initfunction slotd))) |
299 | init-slots)))))) |
300 | |
301 | (cond |
302 | (init-slots |
74821f75 |
303 | (let* ((pointer-size (size-of 'pointer)) |
304 | (element-size (+ +gvalue-size+ pointer-size)) |
305 | (num-slots (length init-slots))) |
306 | (with-memory (params (* num-slots element-size)) |
e624bc26 |
307 | (loop |
308 | with string-writer = (writer-function 'string) |
309 | for (slotd . value) in init-slots |
74821f75 |
310 | as param = params then (pointer+ param element-size) |
e624bc26 |
311 | as type = (slot-definition-type slotd) |
312 | as pname = (slot-definition-pname slotd) |
74821f75 |
313 | do (funcall string-writer pname param) |
314 | (gvalue-init (pointer+ param pointer-size) type value)) |
e624bc26 |
315 | |
316 | (unwind-protect |
317 | (%gobject-newv (type-number-of object) num-slots params) |
318 | |
319 | (loop |
320 | with string-destroy = (destroy-function 'string) |
321 | repeat num-slots |
74821f75 |
322 | as param = params then (pointer+ param element-size) |
323 | do (funcall string-destroy param) |
324 | (gvalue-unset (pointer+ param pointer-size))))))) |
e624bc26 |
325 | |
326 | (t (%gobject-new (type-number-of object)))))) |
327 | |
328 | |
329 | (defmethod shared-initialize ((object gobject) names &rest initargs) |
330 | (declare (ignore names initargs)) |
331 | (let ((*ignore-setting-construct-only-property* t)) |
332 | (call-next-method))) |
333 | |
008c81d9 |
334 | (defmethod initialize-instance :around ((object gobject) &rest initargs) |
335 | (declare (ignore initargs)) |
e624bc26 |
336 | (prog1 |
337 | (call-next-method) |
338 | #+debug-ref-counting(%object-weak-ref (foreign-location object)) |
74821f75 |
339 | #?(pkg-exists-p "glib-2.0" :atleast-version "2.8.0") |
e624bc26 |
340 | (when (slot-value (class-of object) 'instance-slots-p) |
74821f75 |
341 | (%object-add-toggle-ref (foreign-location object)) |
342 | (%object-unref (foreign-location object))))) |
4d83a8a6 |
343 | |
344 | |
1dbf4216 |
345 | (defmethod instance-finalizer ((instance gobject)) |
09f6e237 |
346 | (let ((location (foreign-location instance))) |
74821f75 |
347 | #?(pkg-exists-p "glib-2.0" :atleast-version "2.8.0") |
b036d261 |
348 | (if (slot-value (class-of instance) 'instance-slots-p) |
349 | #'(lambda () |
008c81d9 |
350 | #+debug-ref-counting |
74821f75 |
351 | (format t "Finalizing proxy for 0x~8,'0X~%" (pointer-address location)) |
b036d261 |
352 | (%object-remove-toggle-ref location)) |
353 | #'(lambda () |
008c81d9 |
354 | #+debug-ref-counting |
74821f75 |
355 | (format t "Finalizing proxy for 0x~8,'0X~%" (pointer-address location)) |
5eb27832 |
356 | (%object-unref location))) |
74821f75 |
357 | #?-(pkg-exists-p "glib-2.0" :atleast-version "2.8.0") |
5eb27832 |
358 | #'(lambda () |
74821f75 |
359 | (%object-unref location)))) |
1dbf4216 |
360 | |
9adccb27 |
361 | |
d4b21b08 |
362 | (defbinding (%gobject-new "g_object_new") () pointer |
a9044181 |
363 | (type type-number) |
364 | (nil null)) |
560af5c5 |
365 | |
9adccb27 |
366 | (defbinding (%gobject-newv "g_object_newv") () pointer |
4d83a8a6 |
367 | (type type-number) |
368 | (n-parameters unsigned-int) |
9adccb27 |
369 | (params pointer)) |
de8516ca |
370 | |
371 | |
d4b21b08 |
372 | |
323d4265 |
373 | ;;;; Property stuff |
560af5c5 |
374 | |
26b133ed |
375 | (defbinding %object-set-property () nil |
c8c48a4c |
376 | (object gobject) |
377 | (name string) |
378 | (value gvalue)) |
86d9d6ab |
379 | |
26b133ed |
380 | (defbinding %object-get-property () nil |
c8c48a4c |
381 | (object gobject) |
382 | (name string) |
a9044181 |
383 | (value gvalue)) |
86d9d6ab |
384 | |
26b133ed |
385 | (defbinding %object-notify () nil |
c8c48a4c |
386 | (object gobject) |
387 | (name string)) |
86d9d6ab |
388 | |
26b133ed |
389 | (defbinding object-freeze-notify () nil |
a9044181 |
390 | (object gobject)) |
86d9d6ab |
391 | |
26b133ed |
392 | (defbinding object-thaw-notify () nil |
a9044181 |
393 | (object gobject)) |
86d9d6ab |
394 | |
a00ba56a |
395 | |
396 | ;;;; User data |
397 | |
26b133ed |
398 | (defbinding %object-set-qdata-full () nil |
86d9d6ab |
399 | (object gobject) |
400 | (id quark) |
401 | (data unsigned-long) |
56ccd5b7 |
402 | (destroy-marshal callback)) |
86d9d6ab |
403 | |
56ccd5b7 |
404 | (define-callback user-data-destroy-callback nil ((id unsigned-int)) |
73572c12 |
405 | (destroy-user-data id)) |
406 | |
174e8a5c |
407 | (defun (setf user-data) (data object key) |
a00ba56a |
408 | (%object-set-qdata-full object (quark-intern key) |
56ccd5b7 |
409 | (register-user-data data) user-data-destroy-callback) |
86d9d6ab |
410 | data) |
411 | |
26b133ed |
412 | (defbinding %object-get-qdata () unsigned-long |
86d9d6ab |
413 | (object gobject) |
414 | (id quark)) |
415 | |
174e8a5c |
416 | (defun user-data (object key) |
a00ba56a |
417 | (find-user-data (%object-get-qdata object (quark-intern key)))) |
174e8a5c |
418 | |
174e8a5c |
419 | (defun user-data-p (object key) |
a00ba56a |
420 | (user-data-exists-p (%object-get-qdata object (quark-intern key)))) |
421 | |
422 | (defbinding %object-steal-qdata () unsigned-long |
423 | (object gobject) |
424 | (id quark)) |
425 | |
426 | (defun unset-user-data (object key) |
427 | (destroy-user-data (%object-steal-qdata object (quark-intern key)))) |
86d9d6ab |
428 | |
429 | |
d4b21b08 |
430 | ;;;; |
431 | |
323d4265 |
432 | (defbinding %object-class-list-properties () pointer |
d4b21b08 |
433 | (class pointer) |
434 | (n-properties unsigned-int :out)) |
435 | |
9c03a07c |
436 | |
437 | (defun %map-params (params length type inherited-p) |
438 | (if inherited-p |
9adccb27 |
439 | (map-c-vector 'list #'identity params 'param length) |
9c03a07c |
440 | (let ((properties ())) |
9adccb27 |
441 | (map-c-vector 'list |
9c03a07c |
442 | #'(lambda (param) |
443 | (when (eql (param-owner-type param) type) |
444 | (push param properties))) |
445 | params 'param length) |
446 | (nreverse properties)))) |
447 | |
448 | (defun query-object-class-properties (type &optional inherited-p) |
735a29da |
449 | (let* ((type-number (find-type-number type t)) |
9c03a07c |
450 | (class (type-class-ref type-number))) |
451 | (unwind-protect |
452 | (multiple-value-bind (array length) |
453 | (%object-class-list-properties class) |
21299acf |
454 | (unless (null-pointer-p array) |
455 | (unwind-protect |
456 | (%map-params array length type-number inherited-p) |
457 | (deallocate-memory array)))) |
9c03a07c |
458 | ; (type-class-unref type-number) |
459 | ))) |
d4b21b08 |
460 | |
461 | |
462 | (defun default-slot-name (name) |
74821f75 |
463 | (let ((prefix-len (length (package-prefix)))) |
464 | (intern (substitute #\- #\_ |
465 | (string-upcase |
466 | (if (and |
467 | (string-prefix-p (package-prefix) name) |
468 | (char= #\- (char name prefix-len))) |
469 | (subseq name (1+ prefix-len)) |
470 | name)))))) |
d4b21b08 |
471 | |
472 | (defun default-slot-accessor (class-name slot-name type) |
473 | (intern |
474 | (format |
475 | nil "~A-~A~A" class-name slot-name |
4d83a8a6 |
476 | (if (eq type 'boolean) "-P" "")))) |
d4b21b08 |
477 | |
323d4265 |
478 | |
646466b0 |
479 | (defun slot-definition-from-property (class property &optional slot-name args) |
9c03a07c |
480 | (with-slots (name flags value-type documentation) property |
646466b0 |
481 | (let* ((slot-name (or slot-name (default-slot-name name))) |
62f12808 |
482 | (slot-type (or (getf args :type) (type-from-number value-type) 'pointer)) |
9c03a07c |
483 | (accessor (default-slot-accessor class slot-name slot-type))) |
484 | |
485 | `(,slot-name |
486 | :allocation :property :pname ,name |
eeda1c2d |
487 | |
735a29da |
488 | ,@(when (find :unbound args) (list :unbound (getf args :unbound))) |
489 | ,@(when (find :getter args) (list :getter (getf args :getter))) |
490 | ,@(when (find :setter args) (list :setter (getf args :setter))) |
9c03a07c |
491 | |
492 | ;; accessors |
493 | ,@(cond |
494 | ((and |
495 | (member :writable flags) (member :readable flags) |
496 | (not (member :construct-only flags))) |
497 | (list :accessor accessor)) |
498 | ((and (member :writable flags) (not (member :construct-only flags))) |
499 | (list :writer `(setf ,accessor))) |
500 | ((member :readable flags) |
501 | (list :reader accessor))) |
502 | |
503 | ;; readable/writable/construct |
504 | ,@(when (or (not (member :writable flags)) |
505 | (member :construct-only flags)) |
506 | '(:writable nil)) |
507 | ,@(when (not (member :readable flags)) |
508 | '(:readable nil)) |
e624bc26 |
509 | ,@(when (member :construct-only flags) |
510 | '(:construct-only t)) |
9c03a07c |
511 | |
512 | ;; initargs |
e5c7586b |
513 | ,@(if (find :initarg args) |
514 | (let ((initarg (getf args :initarg))) |
515 | (etypecase initarg |
516 | (null ()) |
517 | (symbol `(:initarg ,initarg)))) |
518 | (when (or (member :construct flags) |
519 | (member :construct-only flags) |
520 | (member :writable flags)) |
521 | (list :initarg (intern (string slot-name) "KEYWORD")))) |
9c03a07c |
522 | |
523 | :type ,slot-type |
524 | :documentation ,documentation)))) |
525 | |
526 | |
527 | (defun slot-definitions (class properties slots) |
528 | (loop |
9c03a07c |
529 | for property in properties |
eeda1c2d |
530 | as slot = (or |
531 | (find (param-name property) slots |
532 | :key #'(lambda (slot) (getf (rest slot) :pname)) |
533 | :test #'string=) |
534 | (find (param-name property) slots |
535 | :key #'first :test #'string-equal)) |
536 | do (cond |
537 | ((not slot) |
538 | (push (slot-definition-from-property class property) slots)) |
539 | ((getf (rest slot) :merge) |
540 | (setf |
541 | (rest slot) |
646466b0 |
542 | (rest (slot-definition-from-property class property (first slot) (rest slot))))))) |
9c03a07c |
543 | (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots)) |
544 | |
545 | |
62f12808 |
546 | (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class)) |
9c03a07c |
547 | (let ((supers (cons (supertype type) (implements type))) |
548 | (class (type-from-number type)) |
74821f75 |
549 | (slots (getf options :slots))) |
9c03a07c |
550 | `(defclass ,class ,supers |
735a29da |
551 | ,(unless forward-p |
552 | (slot-definitions class (query-object-class-properties type) slots)) |
553 | (:metaclass ,metaclass) |
554 | (:gtype ,(register-type-as type))))) |
323d4265 |
555 | |
74821f75 |
556 | (defun gobject-dependencies (type options) |
21299acf |
557 | (delete-duplicates |
558 | (cons |
559 | (supertype type) |
560 | (append |
561 | (type-interfaces type) |
74821f75 |
562 | (mapcar #'param-value-type (query-object-class-properties type)) |
563 | (getf options :dependencies) |
564 | (loop |
565 | for slot in (getf options :slots) |
566 | as type = (getf (rest slot) :type) |
567 | when (and type (symbolp type) (find-type-number type)) |
568 | collect (find-type-number type)))))) |
d4b21b08 |
569 | |
62f12808 |
570 | |
571 | (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies) |
ae37d096 |
572 | |
573 | |
574 | ;;; Pseudo type for gobject instances which have their reference count |
575 | ;;; increased by the returning function |
576 | |
4555db90 |
577 | (deftype referenced (type) type) |
ae37d096 |
578 | |
74821f75 |
579 | (define-type-method from-alien-form ((type referenced) form &key (ref :free)) |
580 | (cond |
581 | ((not (eq ref :free)) |
582 | (error "Keyword arg :REF to FROM-ALIEN-FORM should be :FREE for type ~A. It was give ~A" type ref)) |
583 | ((subtypep type 'gobject) |
584 | (from-alien-form (second (type-expand-to 'referenced type)) form :ref ref)))) |
585 | |
586 | (define-type-method from-alien-function ((type referenced) &key (ref :free)) |
587 | (cond |
588 | ((not (eq ref :free)) |
589 | (error "Keyword arg :REF to FROM-ALIEN-FUNCTION should be :FREE for type ~A. It was give ~A" type ref)) |
590 | ; ((subtypep type 'gobject) (call-next-method type ref :free)))) |
591 | ((subtypep type 'gobject) |
592 | (from-alien-function (second (type-expand-to 'referenced type)) :ref ref)))) |