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