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