55212af1 |
1 | ;; Common Lisp bindings for GTK+ v2.x |
08cb5756 |
2 | ;; Copyright 2000-2006 Espen S. Johnsen <espen@users.sf.net> |
0d07716f |
3 | ;; |
55212af1 |
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: |
0d07716f |
11 | ;; |
55212af1 |
12 | ;; The above copyright notice and this permission notice shall be |
13 | ;; included in all copies or substantial portions of the Software. |
0d07716f |
14 | ;; |
55212af1 |
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. |
0d07716f |
22 | |
1c6936ad |
23 | ;; $Id: gobject.lisp,v 1.57 2007/06/01 10:46:15 espen Exp $ |
0d07716f |
24 | |
25 | (in-package "GLIB") |
26 | |
27 | |
0c87a95a |
28 | ;;;; Metaclass used for subclasses of gobject |
29 | |
30 | (eval-when (:compile-toplevel :load-toplevel :execute) |
4c502835 |
31 | ;; (push :debug-ref-counting *features*) |
0c87a95a |
32 | (defclass gobject-class (ginstance-class) |
08cb5756 |
33 | ((instance-slots-p :initform nil :reader instance-slots-p |
67985e36 |
34 | :documentation "Non NIL if the class has slots with instance allocation"))) |
08cb5756 |
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))) |
0c87a95a |
42 | |
3d36c5d6 |
43 | (defmethod validate-superclass ((class gobject-class) (super standard-class)) |
0c87a95a |
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) |
d5dae790 |
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))) |
0c87a95a |
52 | |
53 | (defclass effective-property-slot-definition (effective-virtual-slot-definition) |
54 | ((pname :reader slot-definition-pname :initarg :pname) |
08cb5756 |
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))) |
d4ddcf2c |
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 | |
0c87a95a |
65 | |
66 | (defbinding %object-ref () pointer |
67 | (location pointer)) |
68 | |
69 | (defbinding %object-unref () nil |
70 | (location pointer)) |
71 | |
08cb5756 |
72 | #?(pkg-exists-p "glib-2.0" :atleast-version "2.8.0") |
124eb32c |
73 | (progn |
a92553bd |
74 | (define-callback toggle-ref-callback nil |
75 | ((data pointer) (location pointer) (last-ref-p boolean)) |
4d1fea77 |
76 | (declare (ignore data)) |
7ce0497d |
77 | #+debug-ref-counting |
78 | (if last-ref-p |
08cb5756 |
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))) |
124eb32c |
81 | (if last-ref-p |
82 | (cache-instance (find-cached-instance location) t) |
83 | (cache-instance (find-cached-instance location) nil))) |
84 | |
4660a86a |
85 | (defbinding %object-add-toggle-ref (location) pointer |
124eb32c |
86 | (location pointer) |
a92553bd |
87 | (toggle-ref-callback callback) |
124eb32c |
88 | (nil null)) |
89 | |
4660a86a |
90 | (defbinding %object-remove-toggle-ref (location) pointer |
124eb32c |
91 | (location pointer) |
a92553bd |
92 | (toggle-ref-callback callback) |
124eb32c |
93 | (nil null))) |
67985e36 |
94 | |
124eb32c |
95 | #+debug-ref-counting |
96 | (progn |
a92553bd |
97 | (define-callback weak-ref-callback nil ((data pointer) (location pointer)) |
08cb5756 |
98 | (format t "Object at 0x~8,'0X (~A) being finalized~%" (pointer-address location) (type-from-number (%type-number-of-ginstance location)))) |
124eb32c |
99 | |
4660a86a |
100 | (defbinding %object-weak-ref (location) pointer |
124eb32c |
101 | (location pointer) |
a92553bd |
102 | (weak-ref-callback callback) |
124eb32c |
103 | (nil null))) |
0c87a95a |
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)) |
d4ddcf2c |
122 | (:user-data (find-class 'direct-user-data-slot-definition)) |
0c87a95a |
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)) |
d4ddcf2c |
128 | (:user-data (find-class 'effective-user-data-slot-definition)) |
0c87a95a |
129 | (t (call-next-method)))) |
130 | |
131 | (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds) |
c23cc486 |
132 | (if (eq (slot-definition-allocation (first direct-slotds)) :property) |
08cb5756 |
133 | (nconc |
134 | (compute-most-specific-initargs direct-slotds |
135 | '(pname construct-only readable writable)) |
0c87a95a |
136 | (call-next-method)) |
137 | (call-next-method))) |
138 | |
139 | |
d5dae790 |
140 | (defvar *ignore-setting-construct-only-property* nil) |
141 | (declaim (special *ignore-setting-construct-only-property*)) |
142 | |
6b716036 |
143 | (defmethod compute-slot-reader-function ((slotd effective-property-slot-definition) &optional signal-unbound-p) |
144 | (declare (ignore signal-unbound-p)) |
376809b0 |
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) |
c7507589 |
157 | (declare (ignore value)) |
376809b0 |
158 | (unless *ignore-setting-construct-only-property* |
159 | (error 'unwritable-slot :name (slot-definition-name slotd) :instance object))) |
08cb5756 |
160 | (call-next-method))) |
64bce834 |
161 | |
08cb5756 |
162 | (defmethod compute-slot-writer-function ((slotd effective-property-slot-definition)) |
376809b0 |
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))) |
08cb5756 |
167 | #'(lambda (value object) |
376809b0 |
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) |
08cb5756 |
178 | |
6b716036 |
179 | (defmethod compute-slot-reader-function ((slotd effective-user-data-slot-definition) &optional signal-unbound-p) |
180 | (declare (ignore signal-unbound-p)) |
08cb5756 |
181 | (let ((slot-name (slot-definition-name slotd))) |
182 | #'(lambda (object) |
183 | (user-data object slot-name)))) |
0c87a95a |
184 | |
08cb5756 |
185 | (defmethod compute-slot-boundp-function ((slotd effective-user-data-slot-definition)) |
d4ddcf2c |
186 | (let ((slot-name (slot-definition-name slotd))) |
08cb5756 |
187 | #'(lambda (object) |
188 | (user-data-p object slot-name)))) |
189 | |
376809b0 |
190 | (defmethod slot-writable-p ((slotd effective-user-data-slot-definition)) |
191 | (declare (ignore slotd)) |
192 | t) |
193 | |
08cb5756 |
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)) |
67985e36 |
213 | |
214 | |
0c87a95a |
215 | ;;;; Super class for all classes in the GObject type hierarchy |
216 | |
0d07716f |
217 | (eval-when (:compile-toplevel :load-toplevel :execute) |
c9819f3e |
218 | (defclass gobject (ginstance) |
4c502835 |
219 | (#+debug-ref-counting |
220 | (ref-count :allocation :alien :type int :reader ref-count)) |
0c87a95a |
221 | (:metaclass gobject-class) |
dcb31db6 |
222 | (:gtype "GObject"))) |
6baf860c |
223 | |
4c502835 |
224 | #+debug-ref-counting |
225 | (defmethod print-object ((instance gobject) stream) |
226 | (print-unreadable-object (instance stream :type t :identity nil) |
cf45719a |
227 | (if (proxy-valid-p instance) |
08cb5756 |
228 | (format stream "at 0x~X (~D)" (pointer-address (foreign-location instance)) (ref-count instance)) |
4c502835 |
229 | (write-string "at \"unbound\"" stream)))) |
230 | |
64bce834 |
231 | |
08cb5756 |
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 | |
64bce834 |
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 | |
1d06a422 |
268 | (defmethod make-proxy-instance ((class gobject-class) location &rest initargs) |
269 | (declare (ignore location initargs)) |
270 | (if (slot-value class 'instance-slots-p) |
08cb5756 |
271 | (error "Objects of class ~A has instance slots and should only be created with MAKE-INSTANCE" class) |
1d06a422 |
272 | (call-next-method))) |
273 | |
1c6936ad |
274 | (defparameter +gparameter-gvalue-offset+ |
275 | (max (size-of 'pointer) (type-alignment '(unsigned-byte 64)))) |
276 | (defparameter +gparameter-size+ |
277 | (+ +gparameter-gvalue-offset+ +gvalue-size+)) |
d5dae790 |
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 |
1c6936ad |
303 | (let* ((num-slots (length init-slots))) |
304 | (with-memory (params (* num-slots +gparameter-size+)) |
d5dae790 |
305 | (loop |
306 | with string-writer = (writer-function 'string) |
307 | for (slotd . value) in init-slots |
1c6936ad |
308 | as param = params then (pointer+ param +gparameter-size+) |
d5dae790 |
309 | as type = (slot-definition-type slotd) |
310 | as pname = (slot-definition-pname slotd) |
08cb5756 |
311 | do (funcall string-writer pname param) |
1c6936ad |
312 | (gvalue-init |
313 | (pointer+ param +gparameter-gvalue-offset+) type value)) |
d5dae790 |
314 | |
315 | (unwind-protect |
316 | (%gobject-newv (type-number-of object) num-slots params) |
317 | |
318 | (loop |
319 | with string-destroy = (destroy-function 'string) |
320 | repeat num-slots |
1c6936ad |
321 | as param = params then (pointer+ param +gparameter-size+) |
08cb5756 |
322 | do (funcall string-destroy param) |
1c6936ad |
323 | (gvalue-unset (pointer+ param +gparameter-gvalue-offset+))))))) |
324 | |
d5dae790 |
325 | (t (%gobject-new (type-number-of object)))))) |
326 | |
327 | |
328 | (defmethod shared-initialize ((object gobject) names &rest initargs) |
329 | (declare (ignore names initargs)) |
330 | (let ((*ignore-setting-construct-only-property* t)) |
331 | (call-next-method))) |
332 | |
124eb32c |
333 | (defmethod initialize-instance :around ((object gobject) &rest initargs) |
334 | (declare (ignore initargs)) |
d5dae790 |
335 | (prog1 |
336 | (call-next-method) |
337 | #+debug-ref-counting(%object-weak-ref (foreign-location object)) |
08cb5756 |
338 | #?(pkg-exists-p "glib-2.0" :atleast-version "2.8.0") |
d5dae790 |
339 | (when (slot-value (class-of object) 'instance-slots-p) |
08cb5756 |
340 | (%object-add-toggle-ref (foreign-location object)) |
341 | (%object-unref (foreign-location object))))) |
935a783c |
342 | |
343 | |
29933e83 |
344 | (defmethod instance-finalizer ((instance gobject)) |
7ce0497d |
345 | (let ((location (foreign-location instance))) |
08cb5756 |
346 | #?(pkg-exists-p "glib-2.0" :atleast-version "2.8.0") |
67985e36 |
347 | (if (slot-value (class-of instance) 'instance-slots-p) |
348 | #'(lambda () |
124eb32c |
349 | #+debug-ref-counting |
08cb5756 |
350 | (format t "Finalizing proxy for 0x~8,'0X~%" (pointer-address location)) |
67985e36 |
351 | (%object-remove-toggle-ref location)) |
352 | #'(lambda () |
124eb32c |
353 | #+debug-ref-counting |
08cb5756 |
354 | (format t "Finalizing proxy for 0x~8,'0X~%" (pointer-address location)) |
985e4aa3 |
355 | (%object-unref location))) |
08cb5756 |
356 | #?-(pkg-exists-p "glib-2.0" :atleast-version "2.8.0") |
985e4aa3 |
357 | #'(lambda () |
08cb5756 |
358 | (%object-unref location)))) |
29933e83 |
359 | |
6baf860c |
360 | |
3a935dfa |
361 | (defbinding (%gobject-new "g_object_new") () pointer |
b75a7b77 |
362 | (type type-number) |
363 | (nil null)) |
0d07716f |
364 | |
6baf860c |
365 | (defbinding (%gobject-newv "g_object_newv") () pointer |
935a783c |
366 | (type type-number) |
367 | (n-parameters unsigned-int) |
6baf860c |
368 | (params pointer)) |
9db52165 |
369 | |
370 | |
3a935dfa |
371 | |
383653f5 |
372 | ;;;; Property stuff |
0d07716f |
373 | |
c2cd6b3a |
374 | (defbinding %object-set-property () nil |
c9819f3e |
375 | (object gobject) |
376 | (name string) |
377 | (value gvalue)) |
a905f5d9 |
378 | |
c2cd6b3a |
379 | (defbinding %object-get-property () nil |
c9819f3e |
380 | (object gobject) |
381 | (name string) |
b75a7b77 |
382 | (value gvalue)) |
a905f5d9 |
383 | |
c2cd6b3a |
384 | (defbinding %object-notify () nil |
c9819f3e |
385 | (object gobject) |
386 | (name string)) |
a905f5d9 |
387 | |
c2cd6b3a |
388 | (defbinding object-freeze-notify () nil |
b75a7b77 |
389 | (object gobject)) |
a905f5d9 |
390 | |
c2cd6b3a |
391 | (defbinding object-thaw-notify () nil |
b75a7b77 |
392 | (object gobject)) |
a905f5d9 |
393 | |
850bf007 |
394 | |
395 | ;;;; User data |
396 | |
936d2c20 |
397 | (defgeneric (setf user-data) (data object key)) |
398 | (defgeneric user-data (object key)) |
399 | (defgeneric user-data-p (object key)) |
400 | (defgeneric unset-user-data (object key)) |
401 | |
c2cd6b3a |
402 | (defbinding %object-set-qdata-full () nil |
a905f5d9 |
403 | (object gobject) |
404 | (id quark) |
936d2c20 |
405 | (data pointer-data) |
a92553bd |
406 | (destroy-marshal callback)) |
a905f5d9 |
407 | |
936d2c20 |
408 | (define-callback user-data-destroy-callback nil ((id pointer-data)) |
3d36c5d6 |
409 | (destroy-user-data id)) |
410 | |
936d2c20 |
411 | (defmethod (setf user-data) (data (object gobject) key) |
850bf007 |
412 | (%object-set-qdata-full object (quark-intern key) |
a92553bd |
413 | (register-user-data data) user-data-destroy-callback) |
a905f5d9 |
414 | data) |
415 | |
936d2c20 |
416 | (defbinding %object-get-qdata () pointer-data |
a905f5d9 |
417 | (object gobject) |
418 | (id quark)) |
419 | |
936d2c20 |
420 | (defmethod user-data ((object gobject) key) |
850bf007 |
421 | (find-user-data (%object-get-qdata object (quark-intern key)))) |
d4ddcf2c |
422 | |
936d2c20 |
423 | (defmethod user-data-p ((object gobject) key) |
850bf007 |
424 | (user-data-exists-p (%object-get-qdata object (quark-intern key)))) |
425 | |
936d2c20 |
426 | (defbinding %object-steal-qdata () pointer-data |
850bf007 |
427 | (object gobject) |
428 | (id quark)) |
429 | |
936d2c20 |
430 | (defmethod unset-user-data ((object gobject) key) |
850bf007 |
431 | (destroy-user-data (%object-steal-qdata object (quark-intern key)))) |
a905f5d9 |
432 | |
433 | |
3a935dfa |
434 | ;;;; |
435 | |
383653f5 |
436 | (defbinding %object-class-list-properties () pointer |
3a935dfa |
437 | (class pointer) |
438 | (n-properties unsigned-int :out)) |
439 | |
b7833c4c |
440 | |
441 | (defun %map-params (params length type inherited-p) |
442 | (if inherited-p |
6baf860c |
443 | (map-c-vector 'list #'identity params 'param length) |
b7833c4c |
444 | (let ((properties ())) |
6baf860c |
445 | (map-c-vector 'list |
b7833c4c |
446 | #'(lambda (param) |
447 | (when (eql (param-owner-type param) type) |
448 | (push param properties))) |
449 | params 'param length) |
450 | (nreverse properties)))) |
451 | |
452 | (defun query-object-class-properties (type &optional inherited-p) |
80031aba |
453 | (let* ((type-number (find-type-number type t)) |
b7833c4c |
454 | (class (type-class-ref type-number))) |
455 | (unwind-protect |
456 | (multiple-value-bind (array length) |
457 | (%object-class-list-properties class) |
6556dccd |
458 | (unless (null-pointer-p array) |
459 | (unwind-protect |
460 | (%map-params array length type-number inherited-p) |
461 | (deallocate-memory array)))) |
b7833c4c |
462 | ; (type-class-unref type-number) |
463 | ))) |
3a935dfa |
464 | |
465 | |
466 | (defun default-slot-name (name) |
08cb5756 |
467 | (let ((prefix-len (length (package-prefix)))) |
468 | (intern (substitute #\- #\_ |
469 | (string-upcase |
470 | (if (and |
471 | (string-prefix-p (package-prefix) name) |
472 | (char= #\- (char name prefix-len))) |
473 | (subseq name (1+ prefix-len)) |
474 | name)))))) |
3a935dfa |
475 | |
476 | (defun default-slot-accessor (class-name slot-name type) |
477 | (intern |
478 | (format |
479 | nil "~A-~A~A" class-name slot-name |
935a783c |
480 | (if (eq type 'boolean) "-P" "")))) |
3a935dfa |
481 | |
383653f5 |
482 | |
3330adc9 |
483 | (defun slot-definition-from-property (class property &optional slot-name args) |
b7833c4c |
484 | (with-slots (name flags value-type documentation) property |
3330adc9 |
485 | (let* ((slot-name (or slot-name (default-slot-name name))) |
e9934f39 |
486 | (slot-type (or (getf args :type) (type-from-number value-type) 'pointer)) |
b7833c4c |
487 | (accessor (default-slot-accessor class slot-name slot-type))) |
488 | |
489 | `(,slot-name |
490 | :allocation :property :pname ,name |
64bce834 |
491 | |
80031aba |
492 | ,@(when (find :unbound args) (list :unbound (getf args :unbound))) |
493 | ,@(when (find :getter args) (list :getter (getf args :getter))) |
494 | ,@(when (find :setter args) (list :setter (getf args :setter))) |
b7833c4c |
495 | |
496 | ;; accessors |
497 | ,@(cond |
498 | ((and |
499 | (member :writable flags) (member :readable flags) |
500 | (not (member :construct-only flags))) |
501 | (list :accessor accessor)) |
502 | ((and (member :writable flags) (not (member :construct-only flags))) |
503 | (list :writer `(setf ,accessor))) |
504 | ((member :readable flags) |
505 | (list :reader accessor))) |
506 | |
507 | ;; readable/writable/construct |
508 | ,@(when (or (not (member :writable flags)) |
509 | (member :construct-only flags)) |
510 | '(:writable nil)) |
511 | ,@(when (not (member :readable flags)) |
512 | '(:readable nil)) |
d5dae790 |
513 | ,@(when (member :construct-only flags) |
514 | '(:construct-only t)) |
b7833c4c |
515 | |
516 | ;; initargs |
c7a44ea9 |
517 | ,@(if (find :initarg args) |
518 | (let ((initarg (getf args :initarg))) |
519 | (etypecase initarg |
520 | (null ()) |
521 | (symbol `(:initarg ,initarg)))) |
522 | (when (or (member :construct flags) |
523 | (member :construct-only flags) |
524 | (member :writable flags)) |
525 | (list :initarg (intern (string slot-name) "KEYWORD")))) |
b7833c4c |
526 | |
527 | :type ,slot-type |
528 | :documentation ,documentation)))) |
529 | |
530 | |
531 | (defun slot-definitions (class properties slots) |
532 | (loop |
b7833c4c |
533 | for property in properties |
64bce834 |
534 | as slot = (or |
535 | (find (param-name property) slots |
536 | :key #'(lambda (slot) (getf (rest slot) :pname)) |
537 | :test #'string=) |
538 | (find (param-name property) slots |
539 | :key #'first :test #'string-equal)) |
540 | do (cond |
541 | ((not slot) |
542 | (push (slot-definition-from-property class property) slots)) |
543 | ((getf (rest slot) :merge) |
544 | (setf |
545 | (rest slot) |
3330adc9 |
546 | (rest (slot-definition-from-property class property (first slot) (rest slot))))))) |
b7833c4c |
547 | (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots)) |
548 | |
549 | |
e9934f39 |
550 | (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class)) |
b7833c4c |
551 | (let ((supers (cons (supertype type) (implements type))) |
552 | (class (type-from-number type)) |
08cb5756 |
553 | (slots (getf options :slots))) |
b7833c4c |
554 | `(defclass ,class ,supers |
80031aba |
555 | ,(unless forward-p |
556 | (slot-definitions class (query-object-class-properties type) slots)) |
557 | (:metaclass ,metaclass) |
558 | (:gtype ,(register-type-as type))))) |
383653f5 |
559 | |
08cb5756 |
560 | (defun gobject-dependencies (type options) |
6556dccd |
561 | (delete-duplicates |
562 | (cons |
563 | (supertype type) |
564 | (append |
565 | (type-interfaces type) |
08cb5756 |
566 | (mapcar #'param-value-type (query-object-class-properties type)) |
567 | (getf options :dependencies) |
568 | (loop |
569 | for slot in (getf options :slots) |
570 | as type = (getf (rest slot) :type) |
571 | when (and type (symbolp type) (find-type-number type)) |
572 | collect (find-type-number type)))))) |
3a935dfa |
573 | |
e9934f39 |
574 | |
575 | (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies) |
7875d055 |
576 | |
577 | |
578 | ;;; Pseudo type for gobject instances which have their reference count |
579 | ;;; increased by the returning function |
580 | |
039c2fdb |
581 | (deftype referenced (type) type) |
7875d055 |
582 | |
08cb5756 |
583 | (define-type-method from-alien-form ((type referenced) form &key (ref :free)) |
584 | (cond |
585 | ((not (eq ref :free)) |
586 | (error "Keyword arg :REF to FROM-ALIEN-FORM should be :FREE for type ~A. It was give ~A" type ref)) |
587 | ((subtypep type 'gobject) |
588 | (from-alien-form (second (type-expand-to 'referenced type)) form :ref ref)))) |
589 | |
590 | (define-type-method from-alien-function ((type referenced) &key (ref :free)) |
591 | (cond |
592 | ((not (eq ref :free)) |
593 | (error "Keyword arg :REF to FROM-ALIEN-FUNCTION should be :FREE for type ~A. It was give ~A" type ref)) |
594 | ; ((subtypep type 'gobject) (call-next-method type ref :free)))) |
595 | ((subtypep type 'gobject) |
596 | (from-alien-function (second (type-expand-to 'referenced type)) :ref ref)))) |