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