chiark / gitweb /
Redesigned proxy initialization protocol
[clg] / glib / gobject.lisp
... / ...
CommitLineData
1;; Common Lisp bindings for GTK+ v2.x
2;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net>
3;;
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:
11;;
12;; The above copyright notice and this permission notice shall be
13;; included in all copies or substantial portions of the Software.
14;;
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.
22
23;; $Id: gobject.lisp,v 1.43 2006-02-05 15:38:57 espen Exp $
24
25(in-package "GLIB")
26
27
28;;;; Metaclass used for subclasses of gobject
29
30(eval-when (:compile-toplevel :load-toplevel :execute)
31 (defclass gobject-class (ginstance-class)
32 ((instance-slots-p :initform nil
33 :documentation "Non NIL if the class has slots with instance allocation")))
34
35 (defmethod validate-superclass ((class gobject-class) (super standard-class))
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)
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
57
58(defbinding %object-ref () pointer
59 (location pointer))
60
61(defbinding %object-unref () nil
62 (location pointer))
63
64#+glib2.8
65(progn
66 (defcallback toggle-ref-callback (nil (data pointer) (location pointer) (last-ref-p boolean))
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)))
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)))
84
85(defmethod reference-foreign ((class gobject-class) location)
86 (declare (ignore class))
87 (%object-ref location))
88
89(defmethod unreference-foreign ((class gobject-class) location)
90 (declare (ignore class))
91 (%object-unref location))
92
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)))
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))
120 (:user-data (find-class 'direct-user-data-slot-definition))
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))
126 (:user-data (find-class 'effective-user-data-slot-definition))
127 (t (call-next-method))))
128
129(defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
130 (if (typep (first direct-slotds) 'direct-property-slot-definition)
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))
142 (let ((type (slot-definition-type slotd))
143 (pname (slot-definition-pname slotd)))
144 (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
145 (setf
146 (slot-value slotd 'getter)
147 (let ((reader nil))
148 #'(lambda (object)
149 (unless reader
150 (setq reader (reader-function type)))
151 (let ((gvalue (gvalue-new type)))
152 (%object-get-property object pname gvalue)
153 (unwind-protect
154 (funcall reader gvalue +gvalue-value-offset+)
155 (gvalue-free gvalue t)))))))
156
157 (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd))
158 (setf
159 (slot-value slotd 'setter)
160 (let ((writer nil))
161 #'(lambda (value object)
162 (unless writer
163 (setq writer (writer-function type)))
164 (let ((gvalue (gvalue-new type)))
165 (funcall writer value gvalue +gvalue-value-offset+)
166 (%object-set-property object pname gvalue)
167 (gvalue-free gvalue t)
168 value))))))
169
170 (call-next-method))
171
172(defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
173 (let ((slot-name (slot-definition-name slotd)))
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)))))
189 (call-next-method))
190
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)
196 (not (typep slotd 'effective-special-slot-definition))))
197 (class-slots class))
198 (setf (slot-value class 'instance-slots-p) t)))
199
200
201
202;;;; Super class for all classes in the GObject type hierarchy
203
204(eval-when (:compile-toplevel :load-toplevel :execute)
205 (defclass gobject (ginstance)
206 ()
207 (:metaclass gobject-class)
208 (:gtype "GObject")))
209
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
227(defmethod make-proxy-instance ((class gobject-class) location &rest initargs)
228 (declare (ignore location initargs))
229 (if (slot-value class 'instance-slots-p)
230 (error "An object of class ~A has instance slots and should only be created with MAKE-INSTANCE" class)
231 (call-next-method)))
232
233(defmethod initialize-instance :around ((object gobject) &rest initargs)
234 (declare (ignore initargs))
235 (call-next-method)
236 #+debug-ref-counting(%object-weak-ref (foreign-location object))
237 #+glib2.8
238 (when (slot-value (class-of object) 'instance-slots-p)
239 (with-slots (location) object
240 (%object-add-toggle-ref location)
241 (%object-unref location))))
242
243
244(defmethod initialize-instance ((object gobject) &rest initargs)
245 (unless (slot-boundp object 'location)
246 ;; Extract initargs which we should pass directly to the GObject
247 ;; constructor
248 (let* ((slotds (class-slots (class-of object)))
249 (args (when initargs
250 (loop
251 as (key value . rest) = initargs then rest
252 as slotd = (find-if
253 #'(lambda (slotd)
254 (member key (slot-definition-initargs slotd)))
255 slotds)
256 when (and (typep slotd 'effective-property-slot-definition)
257 (slot-value slotd 'construct))
258 collect (progn
259 (remf initargs key)
260 (list
261 (slot-definition-pname slotd)
262 (slot-definition-type slotd)
263 value))
264 while rest))))
265 (if args
266 (let* ((string-size (size-of 'string))
267 (string-writer (writer-function 'string))
268 (string-destroy (destroy-function 'string))
269 (params (allocate-memory
270 (* (length args) (+ string-size +gvalue-size+)))))
271 (loop
272 for (pname type value) in args
273 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
274 do (funcall string-writer pname tmp)
275 (gvalue-init (sap+ tmp string-size) type value))
276 (unwind-protect
277 (setf
278 (slot-value object 'location)
279 (%gobject-newv (type-number-of object) (length args) params))
280 (loop
281 repeat (length args)
282 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
283 do (funcall string-destroy tmp)
284 (gvalue-unset (sap+ tmp string-size)))
285 (deallocate-memory params)))
286 (setf
287 (slot-value object 'location)
288 (%gobject-new (type-number-of object))))))
289
290 (apply #'call-next-method object initargs))
291
292
293(defmethod instance-finalizer ((instance gobject))
294 (let ((location (foreign-location instance)))
295 #+glib2.8
296 (if (slot-value (class-of instance) 'instance-slots-p)
297 #'(lambda ()
298 #+debug-ref-counting
299 (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
300 (remove-cached-instance location)
301 (%object-remove-toggle-ref location))
302 #'(lambda ()
303 #+debug-ref-counting
304 (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
305 (remove-cached-instance location)
306 (%object-unref location)))
307 #-glib2.8
308 #'(lambda ()
309 (remove-cached-instance location)
310 (%object-unref location))))
311
312
313(defbinding (%gobject-new "g_object_new") () pointer
314 (type type-number)
315 (nil null))
316
317(defbinding (%gobject-newv "g_object_newv") () pointer
318 (type type-number)
319 (n-parameters unsigned-int)
320 (params pointer))
321
322
323
324;;;; Property stuff
325
326(defbinding %object-set-property () nil
327 (object gobject)
328 (name string)
329 (value gvalue))
330
331(defbinding %object-get-property () nil
332 (object gobject)
333 (name string)
334 (value gvalue))
335
336(defbinding %object-notify () nil
337 (object gobject)
338 (name string))
339
340(defbinding object-freeze-notify () nil
341 (object gobject))
342
343(defbinding object-thaw-notify () nil
344 (object gobject))
345
346
347;;;; User data
348
349(defbinding %object-set-qdata-full () nil
350 (object gobject)
351 (id quark)
352 (data unsigned-long)
353 (destroy-marshal pointer))
354
355(defcallback user-data-destroy-func (nil (id unsigned-int))
356 (destroy-user-data id))
357
358(export 'user-data-destroy-func)
359
360(defun (setf user-data) (data object key)
361 (%object-set-qdata-full object (quark-intern key)
362 (register-user-data data) (callback user-data-destroy-func))
363 data)
364
365;; deprecated
366(defun (setf object-data) (data object key &key (test #'eq))
367 (assert (eq test #'eq))
368 (setf (user-data object key) data))
369
370(defbinding %object-get-qdata () unsigned-long
371 (object gobject)
372 (id quark))
373
374(defun user-data (object key)
375 (find-user-data (%object-get-qdata object (quark-intern key))))
376
377;; deprecated
378(defun object-data (object key &key (test #'eq))
379 (assert (eq test #'eq))
380 (user-data object key))
381
382(defun user-data-p (object key)
383 (user-data-exists-p (%object-get-qdata object (quark-intern key))))
384
385(defbinding %object-steal-qdata () unsigned-long
386 (object gobject)
387 (id quark))
388
389(defun unset-user-data (object key)
390 (destroy-user-data (%object-steal-qdata object (quark-intern key))))
391
392
393;;;;
394
395(defbinding %object-class-list-properties () pointer
396 (class pointer)
397 (n-properties unsigned-int :out))
398
399
400(defun %map-params (params length type inherited-p)
401 (if inherited-p
402 (map-c-vector 'list #'identity params 'param length)
403 (let ((properties ()))
404 (map-c-vector 'list
405 #'(lambda (param)
406 (when (eql (param-owner-type param) type)
407 (push param properties)))
408 params 'param length)
409 (nreverse properties))))
410
411(defun query-object-class-properties (type &optional inherited-p)
412 (let* ((type-number (find-type-number type t))
413 (class (type-class-ref type-number)))
414 (unwind-protect
415 (multiple-value-bind (array length)
416 (%object-class-list-properties class)
417 (unless (null-pointer-p array)
418 (unwind-protect
419 (%map-params array length type-number inherited-p)
420 (deallocate-memory array))))
421; (type-class-unref type-number)
422 )))
423
424
425(defun default-slot-name (name)
426 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
427
428(defun default-slot-accessor (class-name slot-name type)
429 (intern
430 (format
431 nil "~A-~A~A" class-name slot-name
432 (if (eq type 'boolean) "-P" ""))))
433
434
435(defun slot-definition-from-property (class property &optional slot-name args)
436 (with-slots (name flags value-type documentation) property
437 (let* ((slot-name (or slot-name (default-slot-name name)))
438 (slot-type (or (getf args :type) (type-from-number value-type) 'pointer))
439 (accessor (default-slot-accessor class slot-name slot-type)))
440
441 `(,slot-name
442 :allocation :property :pname ,name
443
444 ,@(when (find :unbound args) (list :unbound (getf args :unbound)))
445 ,@(when (find :getter args) (list :getter (getf args :getter)))
446 ,@(when (find :setter args) (list :setter (getf args :setter)))
447
448 ;; accessors
449 ,@(cond
450 ((and
451 (member :writable flags) (member :readable flags)
452 (not (member :construct-only flags)))
453 (list :accessor accessor))
454 ((and (member :writable flags) (not (member :construct-only flags)))
455 (list :writer `(setf ,accessor)))
456 ((member :readable flags)
457 (list :reader accessor)))
458
459 ;; readable/writable/construct
460 ,@(when (or (not (member :writable flags))
461 (member :construct-only flags))
462 '(:writable nil))
463 ,@(when (not (member :readable flags))
464 '(:readable nil))
465 ,@(when (or (member :construct flags)
466 (member :construct-only flags))
467 '(:construct t))
468
469 ;; initargs
470 ,@(if (find :initarg args)
471 (let ((initarg (getf args :initarg)))
472 (etypecase initarg
473 (null ())
474 (symbol `(:initarg ,initarg))))
475 (when (or (member :construct flags)
476 (member :construct-only flags)
477 (member :writable flags))
478 (list :initarg (intern (string slot-name) "KEYWORD"))))
479
480 :type ,slot-type
481 :documentation ,documentation))))
482
483
484(defun slot-definitions (class properties slots)
485 (loop
486 for property in properties
487 as slot = (or
488 (find (param-name property) slots
489 :key #'(lambda (slot) (getf (rest slot) :pname))
490 :test #'string=)
491 (find (param-name property) slots
492 :key #'first :test #'string-equal))
493 do (cond
494 ((not slot)
495 (push (slot-definition-from-property class property) slots))
496 ((getf (rest slot) :merge)
497 (setf
498 (rest slot)
499 (rest (slot-definition-from-property class property (first slot) (rest slot)))))))
500 (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
501
502
503(defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class))
504 (let ((supers (cons (supertype type) (implements type)))
505 (class (type-from-number type))
506 (slots (getf options :slots)))
507 `(defclass ,class ,supers
508 ,(unless forward-p
509 (slot-definitions class (query-object-class-properties type) slots))
510 (:metaclass ,metaclass)
511 (:gtype ,(register-type-as type)))))
512
513(defun gobject-dependencies (type)
514 (delete-duplicates
515 (cons
516 (supertype type)
517 (append
518 (type-interfaces type)
519 (mapcar #'param-value-type (query-object-class-properties type))))))
520
521
522(register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies)
523
524
525;;; Pseudo type for gobject instances which have their reference count
526;;; increased by the returning function
527
528(defmethod alien-type ((type (eql 'referenced)) &rest args)
529 (declare (ignore type args))
530 (alien-type 'gobject))
531
532(defmethod from-alien-form (form (type (eql 'referenced)) &rest args)
533 (declare (ignore type))
534 (destructuring-bind (type) args
535 (if (subtypep type 'gobject)
536 (let ((instance (make-symbol "INSTANCE")))
537 `(let ((,instance ,(from-alien-form form type)))
538 (when ,instance
539 (%object-unref (foreign-location ,instance)))
540 ,instance))
541 (error "~A is not a subclass of GOBJECT" type))))
542
543(export 'referenced)