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