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