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