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