a9d47359d1679b39287cd14ac2052f5c1713c82a
[clg] / glib / gobject.lisp
1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 2000-2006 Espen S. Johnsen <espen@users.sf.net>
3 ;;
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:
11 ;;
12 ;; The above copyright notice and this permission notice shall be
13 ;; included in all copies or substantial portions of the Software.
14 ;;
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.
22
23 ;; $Id: gobject.lisp,v 1.58 2008-10-09 18:20:52 espen Exp $
24
25 (in-package "GLIB")
26
27
28 ;;;; Metaclass used for subclasses of gobject
29
30 (eval-when (:compile-toplevel :load-toplevel :execute)
31 ;; (push :debug-ref-counting *features*)
32 (defclass gobject-class (ginstance-class)
33 ((instance-slots-p :initform nil :reader instance-slots-p
34 :documentation "Non NIL if the class has slots with instance allocation")))
35
36 (defmethod validate-superclass ((class gobject-class) (super standard-class))
37 ; (subtypep (class-name super) 'gobject)
38 t))
39
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
55 (defclass direct-property-slot-definition (direct-virtual-slot-definition)
56 ((pname :reader slot-definition-pname :initarg :pname)
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)))
60
61 (defclass effective-property-slot-definition (effective-virtual-slot-definition)
62 ((pname :reader slot-definition-pname :initarg :pname)
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)))
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
73
74 (defbinding %object-ref () pointer
75 (location pointer))
76
77 (defbinding %object-unref () nil
78 (location pointer))
79
80 #?(pkg-exists-p "glib-2.0" :atleast-version "2.8.0")
81 (progn
82 (define-callback toggle-ref-callback nil
83 ((data pointer) (location pointer) (last-ref-p boolean))
84 (declare (ignore data))
85 #+debug-ref-counting
86 (if last-ref-p
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)))
89 (if last-ref-p
90 (cache-instance (find-cached-instance location) t)
91 (cache-instance (find-cached-instance location) nil)))
92
93 (defbinding %object-add-toggle-ref (location) pointer
94 (location pointer)
95 (toggle-ref-callback callback)
96 (nil null))
97
98 (defbinding %object-remove-toggle-ref (location) pointer
99 (location pointer)
100 (toggle-ref-callback callback)
101 (nil null)))
102
103 #+debug-ref-counting
104 (progn
105 (define-callback weak-ref-callback nil ((data pointer) (location pointer))
106 (format t "Object at 0x~8,'0X (~A) being finalized~%" (pointer-address location) (type-from-number (%type-number-of-ginstance location))))
107
108 (defbinding %object-weak-ref (location) pointer
109 (location pointer)
110 (weak-ref-callback callback)
111 (nil null)))
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))
130 (:user-data (find-class 'direct-user-data-slot-definition))
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))
136 (:user-data (find-class 'effective-user-data-slot-definition))
137 (t (call-next-method))))
138
139 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
140 (if (eq (slot-definition-allocation (first direct-slotds)) :property)
141 (nconc
142 (compute-most-specific-initargs direct-slotds
143 '(pname construct-only readable writable))
144 (call-next-method))
145 (call-next-method)))
146
147
148 (defvar *ignore-setting-construct-only-property* nil)
149 (declaim (special *ignore-setting-construct-only-property*))
150
151 (defmethod compute-slot-reader-function ((slotd effective-property-slot-definition) &optional signal-unbound-p)
152 (declare (ignore signal-unbound-p))
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)
165 (declare (ignore value))
166 (unless *ignore-setting-construct-only-property*
167 (error 'unwritable-slot :name (slot-definition-name slotd) :instance object)))
168 (call-next-method)))
169
170 (defmethod compute-slot-writer-function ((slotd effective-property-slot-definition))
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)))
175 #'(lambda (value object)
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)
186
187 (defmethod compute-slot-reader-function ((slotd effective-user-data-slot-definition) &optional signal-unbound-p)
188 (declare (ignore signal-unbound-p))
189 (let ((slot-name (slot-definition-name slotd)))
190 #'(lambda (object)
191 (user-data object slot-name))))
192
193 (defmethod compute-slot-boundp-function ((slotd effective-user-data-slot-definition))
194 (let ((slot-name (slot-definition-name slotd)))
195 #'(lambda (object)
196 (user-data-p object slot-name))))
197
198 (defmethod slot-writable-p ((slotd effective-user-data-slot-definition))
199 (declare (ignore slotd))
200 t)
201
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))
221
222
223 ;;;; Super class for all classes in the GObject type hierarchy
224
225 (eval-when (:compile-toplevel :load-toplevel :execute)
226 (defclass gobject (ginstance)
227 (#+debug-ref-counting
228 (ref-count :allocation :alien :type int :reader ref-count))
229 (:metaclass gobject-class)
230 (:gtype "GObject")))
231
232 #+debug-ref-counting
233 (defmethod print-object ((instance gobject) stream)
234 (print-unreadable-object (instance stream :type t :identity nil)
235 (if (proxy-valid-p instance)
236 (format stream "at 0x~X (~D)" (pointer-address (foreign-location instance)) (ref-count instance))
237 (write-string "at \"unbound\"" stream))))
238
239
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
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
276 (defmethod make-proxy-instance ((class gobject-class) location &rest initargs)
277 (declare (ignore location initargs))
278 (if (slot-value class 'instance-slots-p)
279 (error "Objects of class ~A has instance slots and should only be created with MAKE-INSTANCE" class)
280 (call-next-method)))
281
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+))
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
311 (let* ((num-slots (length init-slots)))
312 (with-memory (params (* num-slots +gparameter-size+))
313 (loop
314 with string-writer = (writer-function 'string)
315 for (slotd . value) in init-slots
316 as param = params then (pointer+ param +gparameter-size+)
317 as type = (slot-definition-type slotd)
318 as pname = (slot-definition-pname slotd)
319 do (funcall string-writer pname param)
320 (gvalue-init
321 (pointer+ param +gparameter-gvalue-offset+) type value))
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
329 as param = params then (pointer+ param +gparameter-size+)
330 do (funcall string-destroy param)
331 (gvalue-unset (pointer+ param +gparameter-gvalue-offset+)))))))
332
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
341 (defmethod initialize-instance :around ((object gobject) &rest initargs)
342 (declare (ignore initargs))
343 (prog1
344 (call-next-method)
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)))))
351
352
353 (defmethod instance-finalizer ((instance gobject))
354 (let ((location (foreign-location instance)))
355 #?(pkg-exists-p "glib-2.0" :atleast-version "2.8.0")
356 (if (slot-value (class-of instance) 'instance-slots-p)
357 #'(lambda ()
358 #+debug-ref-counting
359 (format t "Finalizing proxy for 0x~8,'0X (~A)~%"
360 (pointer-address location)
361 (find-foreign-type-name (%type-number-of-ginstance location)))
362 (%object-remove-toggle-ref location))
363 #'(lambda ()
364 #+debug-ref-counting
365 (format t "Finalizing proxy for 0x~8,'0X (~A)~%"
366 (pointer-address location)
367 (find-foreign-type-name (%type-number-of-ginstance location)))
368 (%object-unref location)))
369 #?-(pkg-exists-p "glib-2.0" :atleast-version "2.8.0")
370 #'(lambda ()
371 (%object-unref location))))
372
373
374 (defbinding (%gobject-new "g_object_new") () pointer
375 (type type-number)
376 (nil null))
377
378 (defbinding (%gobject-newv "g_object_newv") () pointer
379 (type type-number)
380 (n-parameters unsigned-int)
381 (params pointer))
382
383
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
403
404 ;;;; Property stuff
405
406 (defbinding %object-set-property () nil
407 (object gobject)
408 (name string)
409 (value gvalue))
410
411 (defbinding %object-get-property () nil
412 (object gobject)
413 (name string)
414 (value gvalue))
415
416 (defbinding %object-notify () nil
417 (object gobject)
418 (name string))
419
420 (defbinding object-freeze-notify () nil
421 (object gobject))
422
423 (defbinding object-thaw-notify () nil
424 (object gobject))
425
426
427 ;;;; User data
428
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
434 (defbinding %object-set-qdata-full () nil
435 (object gobject)
436 (id quark)
437 (data pointer-data)
438 (destroy-marshal callback))
439
440 (define-callback user-data-destroy-callback nil ((id pointer-data))
441 (destroy-user-data id))
442
443 (defmethod (setf user-data) (data (object gobject) key)
444 (%object-set-qdata-full object (quark-intern key)
445 (register-user-data data) user-data-destroy-callback)
446 data)
447
448 (defbinding %object-get-qdata () pointer-data
449 (object gobject)
450 (id quark))
451
452 (defmethod user-data ((object gobject) key)
453 (find-user-data (%object-get-qdata object (quark-intern key))))
454
455 (defmethod user-data-p ((object gobject) key)
456 (user-data-exists-p (%object-get-qdata object (quark-intern key))))
457
458 (defbinding %object-steal-qdata () pointer-data
459 (object gobject)
460 (id quark))
461
462 (defmethod unset-user-data ((object gobject) key)
463 (destroy-user-data (%object-steal-qdata object (quark-intern key))))
464
465
466 ;;;;
467
468 (defbinding %object-class-list-properties () pointer
469 (class pointer)
470 (n-properties unsigned-int :out))
471
472
473 (defun %map-params (params length type inherited-p)
474 (if inherited-p
475 (map-c-vector 'list #'identity params 'param length)
476 (let ((properties ()))
477 (map-c-vector 'list
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)
485 (let* ((type-number (find-type-number type t))
486 (class (type-class-ref type-number)))
487 (unwind-protect
488 (multiple-value-bind (array length)
489 (%object-class-list-properties class)
490 (unless (null-pointer-p array)
491 (unwind-protect
492 (%map-params array length type-number inherited-p)
493 (deallocate-memory array))))
494 ; (type-class-unref type-number)
495 )))
496
497
498 (defun default-slot-name (name)
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))))))
507
508 (defun default-slot-accessor (class-name slot-name type)
509 (intern
510 (format
511 nil "~A-~A~A" class-name slot-name
512 (if (eq type 'boolean) "-P" ""))))
513
514
515 (defun slot-definition-from-property (class property &optional slot-name args)
516 (with-slots (name flags value-type documentation) property
517 (let* ((slot-name (or slot-name (default-slot-name name)))
518 (slot-type (or (getf args :type) (type-from-number value-type) 'pointer))
519 (accessor (default-slot-accessor class slot-name slot-type)))
520
521 `(,slot-name
522 :allocation :property :pname ,name
523
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)))
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))
545 ,@(when (member :construct-only flags)
546 '(:construct-only t))
547
548 ;; initargs
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"))))
558
559 :type ,slot-type
560 :documentation ,documentation))))
561
562
563 (defun slot-definitions (class properties slots)
564 (loop
565 for property in properties
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)
578 (rest (slot-definition-from-property class property (first slot) (rest slot)))))))
579 (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
580
581
582 (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class))
583 (let ((supers (cons (supertype type) (implements type)))
584 (class (type-from-number type))
585 (slots (getf options :slots)))
586 `(defclass ,class ,supers
587 ,(unless forward-p
588 (slot-definitions class (query-object-class-properties type) slots))
589 (:metaclass ,metaclass)
590 (:gtype ,(register-type-as type)))))
591
592 (defun gobject-dependencies (type options)
593 (delete-duplicates
594 (cons
595 (supertype type)
596 (append
597 (type-interfaces type)
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))))))
605
606
607 (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies)
608
609
610 ;;; Pseudo type for gobject instances which have their reference count
611 ;;; increased by the returning function
612
613 (deftype referenced (type) type)
614
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))))