Added ALLOCATE-FOREIGN method for gobject. Construct slot renamed construct-only
[clg] / glib / gobject.lisp
CommitLineData
55212af1 1;; Common Lisp bindings for GTK+ v2.x
2;; Copyright 2000-2005 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
d5dae790 23;; $Id: gobject.lisp,v 1.46 2006/02/09 22:29:01 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)
67985e36 33 ((instance-slots-p :initform nil
34 :documentation "Non NIL if the class has slots with instance allocation")))
0c87a95a 35
3d36c5d6 36 (defmethod validate-superclass ((class gobject-class) (super standard-class))
0c87a95a 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)
d5dae790 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)))
0c87a95a 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)
d5dae790 50 (construct-only :initarg :construct-only :reader construct-only-property-p)))
d4ddcf2c 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
0c87a95a 58
59(defbinding %object-ref () pointer
60 (location pointer))
61
62(defbinding %object-unref () nil
63 (location pointer))
64
d4665dd8 65#+glib2.8
124eb32c 66(progn
67 (defcallback toggle-ref-callback (nil (data pointer) (location pointer) (last-ref-p boolean))
7ce0497d 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)))
124eb32c 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)))
67985e36 85
0c87a95a 86(defmethod reference-foreign ((class gobject-class) location)
87 (declare (ignore class))
985e4aa3 88 (%object-ref location))
0c87a95a 89
90(defmethod unreference-foreign ((class gobject-class) location)
91 (declare (ignore class))
124eb32c 92 (%object-unref location))
67985e36 93
124eb32c 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)))
0c87a95a 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))
d4ddcf2c 121 (:user-data (find-class 'direct-user-data-slot-definition))
0c87a95a 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))
d4ddcf2c 127 (:user-data (find-class 'effective-user-data-slot-definition))
0c87a95a 128 (t (call-next-method))))
129
130(defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
64bce834 131 (if (typep (first direct-slotds) 'direct-property-slot-definition)
0c87a95a 132 (nconc
133 (list :pname (signal-name-to-string
d5dae790 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))
0c87a95a 140 (call-next-method))
141 (call-next-method)))
142
143
d5dae790 144(defvar *ignore-setting-construct-only-property* nil)
145(declaim (special *ignore-setting-construct-only-property*))
146
0c87a95a 147(defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
dcb31db6 148 (let ((type (slot-definition-type slotd))
149 (pname (slot-definition-pname slotd)))
64bce834 150 (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
0c87a95a 151 (setf
64bce834 152 (slot-value slotd 'getter)
dcb31db6 153 (let ((reader nil))
64bce834 154 #'(lambda (object)
6556dccd 155 (unless reader
156 (setq reader (reader-function type)))
dcb31db6 157 (let ((gvalue (gvalue-new type)))
64bce834 158 (%object-get-property object pname gvalue)
159 (unwind-protect
160 (funcall reader gvalue +gvalue-value-offset+)
161 (gvalue-free gvalue t)))))))
0c87a95a 162
d5dae790 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)
64bce834 181 #'(lambda (value object)
d5dae790 182 (declare (ignore value object))
183 (unless *ignore-setting-construct-only-property*
184 (error "Slot is not writable: ~A" (slot-definition-name slotd)))))))))
64bce834 185
0c87a95a 186 (call-next-method))
187
d4ddcf2c 188(defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
189 (let ((slot-name (slot-definition-name slotd)))
74bad010 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)))))
d4ddcf2c 205 (call-next-method))
206
67985e36 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)
82defe4d 212 (not (typep slotd 'effective-special-slot-definition))))
67985e36 213 (class-slots class))
214 (setf (slot-value class 'instance-slots-p) t)))
215
216
0c87a95a 217
218;;;; Super class for all classes in the GObject type hierarchy
219
0d07716f 220(eval-when (:compile-toplevel :load-toplevel :execute)
c9819f3e 221 (defclass gobject (ginstance)
4c502835 222 (#+debug-ref-counting
223 (ref-count :allocation :alien :type int :reader ref-count))
0c87a95a 224 (:metaclass gobject-class)
dcb31db6 225 (:gtype "GObject")))
6baf860c 226
4c502835 227#+debug-ref-counting
228(defmethod print-object ((instance gobject) stream)
229 (print-unreadable-object (instance stream :type t :identity nil)
cf45719a 230 (if (proxy-valid-p instance)
4c502835 231 (format stream "at 0x~X (~D)" (sap-int (foreign-location instance)) (ref-count instance))
232 (write-string "at \"unbound\"" stream))))
233
64bce834 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
1d06a422 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
d5dae790 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
124eb32c 312(defmethod initialize-instance :around ((object gobject) &rest initargs)
313 (declare (ignore initargs))
d5dae790 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)))))
935a783c 322
323
29933e83 324(defmethod instance-finalizer ((instance gobject))
7ce0497d 325 (let ((location (foreign-location instance)))
d4665dd8 326 #+glib2.8
67985e36 327 (if (slot-value (class-of instance) 'instance-slots-p)
328 #'(lambda ()
124eb32c 329 #+debug-ref-counting
330 (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
67985e36 331 (remove-cached-instance location)
332 (%object-remove-toggle-ref location))
333 #'(lambda ()
124eb32c 334 #+debug-ref-counting
335 (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
67985e36 336 (remove-cached-instance location)
985e4aa3 337 (%object-unref location)))
d4665dd8 338 #-glib2.8
985e4aa3 339 #'(lambda ()
340 (remove-cached-instance location)
341 (%object-unref location))))
29933e83 342
6baf860c 343
3a935dfa 344(defbinding (%gobject-new "g_object_new") () pointer
b75a7b77 345 (type type-number)
346 (nil null))
0d07716f 347
6baf860c 348(defbinding (%gobject-newv "g_object_newv") () pointer
935a783c 349 (type type-number)
350 (n-parameters unsigned-int)
6baf860c 351 (params pointer))
9db52165 352
353
3a935dfa 354
383653f5 355;;;; Property stuff
0d07716f 356
c2cd6b3a 357(defbinding %object-set-property () nil
c9819f3e 358 (object gobject)
359 (name string)
360 (value gvalue))
a905f5d9 361
c2cd6b3a 362(defbinding %object-get-property () nil
c9819f3e 363 (object gobject)
364 (name string)
b75a7b77 365 (value gvalue))
a905f5d9 366
c2cd6b3a 367(defbinding %object-notify () nil
c9819f3e 368 (object gobject)
369 (name string))
a905f5d9 370
c2cd6b3a 371(defbinding object-freeze-notify () nil
b75a7b77 372 (object gobject))
a905f5d9 373
c2cd6b3a 374(defbinding object-thaw-notify () nil
b75a7b77 375 (object gobject))
a905f5d9 376
850bf007 377
378;;;; User data
379
c2cd6b3a 380(defbinding %object-set-qdata-full () nil
a905f5d9 381 (object gobject)
382 (id quark)
383 (data unsigned-long)
384 (destroy-marshal pointer))
385
3d36c5d6 386(defcallback user-data-destroy-func (nil (id unsigned-int))
387 (destroy-user-data id))
388
389(export 'user-data-destroy-func)
390
d4ddcf2c 391(defun (setf user-data) (data object key)
850bf007 392 (%object-set-qdata-full object (quark-intern key)
3d36c5d6 393 (register-user-data data) (callback user-data-destroy-func))
a905f5d9 394 data)
395
850bf007 396;; deprecated
d4ddcf2c 397(defun (setf object-data) (data object key &key (test #'eq))
398 (assert (eq test #'eq))
399 (setf (user-data object key) data))
400
c2cd6b3a 401(defbinding %object-get-qdata () unsigned-long
a905f5d9 402 (object gobject)
403 (id quark))
404
d4ddcf2c 405(defun user-data (object key)
850bf007 406 (find-user-data (%object-get-qdata object (quark-intern key))))
d4ddcf2c 407
850bf007 408;; deprecated
a905f5d9 409(defun object-data (object key &key (test #'eq))
d4ddcf2c 410 (assert (eq test #'eq))
411 (user-data object key))
412
413(defun user-data-p (object key)
850bf007 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))))
a905f5d9 422
423
3a935dfa 424;;;;
425
383653f5 426(defbinding %object-class-list-properties () pointer
3a935dfa 427 (class pointer)
428 (n-properties unsigned-int :out))
429
b7833c4c 430
431(defun %map-params (params length type inherited-p)
432 (if inherited-p
6baf860c 433 (map-c-vector 'list #'identity params 'param length)
b7833c4c 434 (let ((properties ()))
6baf860c 435 (map-c-vector 'list
b7833c4c 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)
80031aba 443 (let* ((type-number (find-type-number type t))
b7833c4c 444 (class (type-class-ref type-number)))
445 (unwind-protect
446 (multiple-value-bind (array length)
447 (%object-class-list-properties class)
6556dccd 448 (unless (null-pointer-p array)
449 (unwind-protect
450 (%map-params array length type-number inherited-p)
451 (deallocate-memory array))))
b7833c4c 452; (type-class-unref type-number)
453 )))
3a935dfa 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
935a783c 463 (if (eq type 'boolean) "-P" ""))))
3a935dfa 464
383653f5 465
3330adc9 466(defun slot-definition-from-property (class property &optional slot-name args)
b7833c4c 467 (with-slots (name flags value-type documentation) property
3330adc9 468 (let* ((slot-name (or slot-name (default-slot-name name)))
e9934f39 469 (slot-type (or (getf args :type) (type-from-number value-type) 'pointer))
b7833c4c 470 (accessor (default-slot-accessor class slot-name slot-type)))
471
472 `(,slot-name
473 :allocation :property :pname ,name
64bce834 474
80031aba 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)))
b7833c4c 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))
d5dae790 496 ,@(when (member :construct-only flags)
497 '(:construct-only t))
b7833c4c 498
499 ;; initargs
c7a44ea9 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"))))
b7833c4c 509
510 :type ,slot-type
511 :documentation ,documentation))))
512
513
514(defun slot-definitions (class properties slots)
515 (loop
b7833c4c 516 for property in properties
64bce834 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)
3330adc9 529 (rest (slot-definition-from-property class property (first slot) (rest slot)))))))
b7833c4c 530 (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
531
532
e9934f39 533(defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class))
b7833c4c 534 (let ((supers (cons (supertype type) (implements type)))
535 (class (type-from-number type))
536 (slots (getf options :slots)))
537 `(defclass ,class ,supers
80031aba 538 ,(unless forward-p
539 (slot-definitions class (query-object-class-properties type) slots))
540 (:metaclass ,metaclass)
541 (:gtype ,(register-type-as type)))))
383653f5 542
e9934f39 543(defun gobject-dependencies (type)
6556dccd 544 (delete-duplicates
545 (cons
546 (supertype type)
547 (append
548 (type-interfaces type)
549 (mapcar #'param-value-type (query-object-class-properties type))))))
3a935dfa 550
e9934f39 551
552(register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies)
7875d055 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
7ce0497d 569 (%object-unref (foreign-location ,instance)))
7875d055 570 ,instance))
571 (error "~A is not a subclass of GOBJECT" type))))
572
573(export 'referenced)