Signal handling code added
[clg] / glib / gobject.lisp
1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
3 ;;
4 ;; This library is free software; you can redistribute it and/or
5 ;; modify it under the terms of the GNU Lesser General Public
6 ;; License as published by the Free Software Foundation; either
7 ;; version 2 of the License, or (at your option) any later version.
8 ;;
9 ;; This library is distributed in the hope that it will be useful,
10 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ;; Lesser General Public License for more details.
13 ;;
14 ;; You should have received a copy of the GNU Lesser General Public
15 ;; License along with this library; if not, write to the Free Software
16 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18 ;; $Id: gobject.lisp,v 1.26 2004-12-29 21:07:46 espen Exp $
19
20 (in-package "GLIB")
21
22
23 ;;;; Metaclass used for subclasses of gobject
24
25 (eval-when (:compile-toplevel :load-toplevel :execute)
26 (defclass gobject-class (ginstance-class)
27 ())
28
29 (defmethod validate-superclass ((class gobject-class)
30 (super pcl::standard-class))
31 ; (subtypep (class-name super) 'gobject)
32 t))
33
34 (defclass direct-property-slot-definition (direct-virtual-slot-definition)
35 ((pname :reader slot-definition-pname :initarg :pname)
36 (readable :initform t :reader slot-readable-p :initarg :readable)
37 (writable :initform t :reader slot-writable-p :initarg :writable)
38 (construct :initform nil :initarg :construct)))
39
40 (defclass effective-property-slot-definition (effective-virtual-slot-definition)
41 ((pname :reader slot-definition-pname :initarg :pname)
42 (readable :reader slot-readable-p :initarg :readable)
43 (writable :reader slot-writable-p :initarg :writable)
44 (construct :initarg :construct)))
45
46 (defclass direct-user-data-slot-definition (direct-virtual-slot-definition)
47 ())
48
49 (defclass effective-user-data-slot-definition (effective-virtual-slot-definition)
50 ())
51
52
53 (defbinding %object-ref () pointer
54 (location pointer))
55
56 (defbinding %object-unref () nil
57 (location pointer))
58
59 (defmethod reference-foreign ((class gobject-class) location)
60 (declare (ignore class))
61 (%object-ref location))
62
63 (defmethod unreference-foreign ((class gobject-class) location)
64 (declare (ignore class))
65 (%object-unref location))
66
67
68 ; (defbinding object-class-install-param () nil
69 ; (class pointer)
70 ; (id unsigned-int)
71 ; (parameter parameter))
72
73 ; (defbinding object-class-find-param-spec () parameter
74 ; (class pointer)
75 ; (name string))
76
77 (defun signal-name-to-string (name)
78 (substitute #\_ #\- (string-downcase (string name))))
79
80
81 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
82 (case (getf initargs :allocation)
83 (:property (find-class 'direct-property-slot-definition))
84 (:user-data (find-class 'direct-user-data-slot-definition))
85 (t (call-next-method))))
86
87 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
88 (case (getf initargs :allocation)
89 (:property (find-class 'effective-property-slot-definition))
90 (:user-data (find-class 'effective-user-data-slot-definition))
91 (t (call-next-method))))
92
93 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
94 (if (typep (first direct-slotds) 'direct-property-slot-definition)
95 (nconc
96 (list :pname (signal-name-to-string
97 (most-specific-slot-value direct-slotds 'pname))
98 :readable (most-specific-slot-value direct-slotds 'readable)
99 :writable (most-specific-slot-value direct-slotds 'writable)
100 :construct (most-specific-slot-value direct-slotds 'construct))
101 (call-next-method))
102 (call-next-method)))
103
104
105 (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
106 (let* ((type (slot-definition-type slotd))
107 (pname (slot-definition-pname slotd))
108 (type-number (find-type-number type)))
109 (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
110 (setf
111 (slot-value slotd 'getter)
112 (let ((reader nil))
113 #'(lambda (object)
114 (unless reader
115 (setq reader (reader-function type))) ;(type-from-number type-number))))
116 (let ((gvalue (gvalue-new type-number)))
117 (%object-get-property object pname gvalue)
118 (unwind-protect
119 (funcall reader gvalue +gvalue-value-offset+)
120 (gvalue-free gvalue t)))))))
121
122 (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd))
123 (setf
124 (slot-value slotd 'setter)
125 (let ((writer nil))
126 #'(lambda (value object)
127 (unless writer
128 (setq writer (writer-function type))) ;(type-from-number type-number))))
129 (let ((gvalue (gvalue-new type-number)))
130 (funcall writer value gvalue +gvalue-value-offset+)
131 (%object-set-property object pname gvalue)
132 (gvalue-free gvalue t)
133 value))))))
134
135 (call-next-method))
136
137 (defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
138 (let ((slot-name (slot-definition-name slotd)))
139 (setf
140 (slot-value slotd 'getter)
141 #'(lambda (object)
142 (prog1 (user-data object slot-name))))
143 (setf
144 (slot-value slotd 'setter)
145 #'(lambda (value object)
146 (setf (user-data object slot-name) value)))
147 (setf
148 (slot-value slotd 'boundp)
149 #'(lambda (object)
150 (user-data-p object slot-name))))
151 (call-next-method))
152
153
154 ;;;; Super class for all classes in the GObject type hierarchy
155
156 (eval-when (:compile-toplevel :load-toplevel :execute)
157 (defclass gobject (ginstance)
158 ()
159 (:metaclass gobject-class)
160 (:alien-name "GObject")))
161
162
163 (defun initial-add (object function initargs key pkey)
164 (loop
165 as (initarg value . rest) = initargs then rest
166 do (cond
167 ((eq initarg key) (funcall function object value))
168 ((eq initarg pkey) (mapc #'(lambda (value)
169 (funcall function object value))
170 value)))
171 while rest))
172
173 (defun initial-apply-add (object function initargs key pkey)
174 (initial-add object #'(lambda (object value)
175 (apply function object (mklist value)))
176 initargs key pkey))
177
178
179 (defmethod initialize-instance ((object gobject) &rest initargs)
180 (unless (slot-boundp object 'location)
181 ;; Extract initargs which we should pass directly to the GObeject
182 ;; constructor
183 (let* ((slotds (class-slots (class-of object)))
184 (args (when initargs
185 (loop
186 as (key value . rest) = initargs then rest
187 as slotd = (find-if
188 #'(lambda (slotd)
189 (member key (slot-definition-initargs slotd)))
190 slotds)
191 when (and (typep slotd 'effective-property-slot-definition)
192 (slot-value slotd 'construct))
193 collect (progn
194 (remf initargs key)
195 (list
196 (slot-definition-pname slotd)
197 (slot-definition-type slotd)
198 value))
199 while rest))))
200 (if args
201 (let* ((string-size (size-of 'string))
202 (string-writer (writer-function 'string))
203 (string-destroy (destroy-function 'string))
204 (params (allocate-memory
205 (* (length args) (+ string-size +gvalue-size+)))))
206 (loop
207 for (pname type value) in args
208 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
209 do (funcall string-writer pname tmp)
210 (gvalue-init (sap+ tmp string-size) type value))
211 (unwind-protect
212 (setf
213 (slot-value object 'location)
214 (%gobject-newv (type-number-of object) (length args) params))
215 (loop
216 repeat (length args)
217 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
218 do (funcall string-destroy tmp)
219 (gvalue-unset (sap+ tmp string-size)))
220 (deallocate-memory params)))
221 (setf
222 (slot-value object 'location)
223 (%gobject-new (type-number-of object))))))
224
225 (apply #'call-next-method object initargs))
226
227
228 (defmethod instance-finalizer ((instance gobject))
229 (let ((location (proxy-location instance)))
230 #'(lambda ()
231 (remove-cached-instance location)
232 (%object-unref location))))
233
234
235 (defbinding (%gobject-new "g_object_new") () pointer
236 (type type-number)
237 (nil null))
238
239 (defbinding (%gobject-newv "g_object_newv") () pointer
240 (type type-number)
241 (n-parameters unsigned-int)
242 (params pointer))
243
244
245
246 ;;;; Property stuff
247
248 (defbinding %object-set-property () nil
249 (object gobject)
250 (name string)
251 (value gvalue))
252
253 (defbinding %object-get-property () nil
254 (object gobject)
255 (name string)
256 (value gvalue))
257
258 (defbinding %object-notify () nil
259 (object gobject)
260 (name string))
261
262 (defbinding object-freeze-notify () nil
263 (object gobject))
264
265 (defbinding object-thaw-notify () nil
266 (object gobject))
267
268 (defbinding %object-set-qdata-full () nil
269 (object gobject)
270 (id quark)
271 (data unsigned-long)
272 (destroy-marshal pointer))
273
274
275 ;;;; User data
276
277 (defun (setf user-data) (data object key)
278 (%object-set-qdata-full
279 object (quark-from-object key)
280 (register-user-data data) (callback %destroy-user-data))
281 data)
282
283 ;; depecated
284 (defun (setf object-data) (data object key &key (test #'eq))
285 (assert (eq test #'eq))
286 (setf (user-data object key) data))
287
288 (defbinding %object-get-qdata () unsigned-long
289 (object gobject)
290 (id quark))
291
292 (defun user-data (object key)
293 (find-user-data (%object-get-qdata object (quark-from-object key))))
294
295 ;; depecated
296 (defun object-data (object key &key (test #'eq))
297 (assert (eq test #'eq))
298 (user-data object key))
299
300 (defun user-data-p (object key)
301 (nth-value 1 (find-user-data (%object-get-qdata object (quark-from-object key)))))
302
303
304 ;;;;
305
306 (defbinding %object-class-list-properties () pointer
307 (class pointer)
308 (n-properties unsigned-int :out))
309
310
311 (defun %map-params (params length type inherited-p)
312 (if inherited-p
313 (map-c-vector 'list #'identity params 'param length)
314 (let ((properties ()))
315 (map-c-vector 'list
316 #'(lambda (param)
317 (when (eql (param-owner-type param) type)
318 (push param properties)))
319 params 'param length)
320 (nreverse properties))))
321
322 (defun query-object-class-properties (type &optional inherited-p)
323 (let* ((type-number (find-type-number type))
324 (class (type-class-ref type-number)))
325 (unwind-protect
326 (multiple-value-bind (array length)
327 (%object-class-list-properties class)
328 (unwind-protect
329 (%map-params array length type-number inherited-p)
330 (deallocate-memory array)))
331 ; (type-class-unref type-number)
332 )))
333
334
335 (defun default-slot-name (name)
336 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
337
338 (defun default-slot-accessor (class-name slot-name type)
339 (intern
340 (format
341 nil "~A-~A~A" class-name slot-name
342 (if (eq type 'boolean) "-P" ""))))
343
344
345 (defun slot-definition-from-property (class property &optional args)
346 (with-slots (name flags value-type documentation) property
347 (let* ((slot-name (default-slot-name name))
348 (slot-type (or (getf args :type) (type-from-number value-type) value-type))
349 (accessor (default-slot-accessor class slot-name slot-type)))
350
351 `(,slot-name
352 :allocation :property :pname ,name
353
354 ,@(cond
355 ((find :unbound args) (list :unbound (getf args :unbound))))
356
357 ;; accessors
358 ,@(cond
359 ((and
360 (member :writable flags) (member :readable flags)
361 (not (member :construct-only flags)))
362 (list :accessor accessor))
363 ((and (member :writable flags) (not (member :construct-only flags)))
364 (list :writer `(setf ,accessor)))
365 ((member :readable flags)
366 (list :reader accessor)))
367
368 ;; readable/writable/construct
369 ,@(when (or (not (member :writable flags))
370 (member :construct-only flags))
371 '(:writable nil))
372 ,@(when (not (member :readable flags))
373 '(:readable nil))
374 ,@(when (or (member :construct flags)
375 (member :construct-only flags))
376 '(:construct t))
377
378 ;; initargs
379 ,@(when (or (member :construct flags)
380 (member :construct-only flags)
381 (member :writable flags))
382 (list :initarg (intern (string slot-name) "KEYWORD")))
383
384 :type ,slot-type
385 :documentation ,documentation))))
386
387
388 (defun slot-definitions (class properties slots)
389 (loop
390 for property in properties
391 as slot = (or
392 (find (param-name property) slots
393 :key #'(lambda (slot) (getf (rest slot) :pname))
394 :test #'string=)
395 (find (param-name property) slots
396 :key #'first :test #'string-equal))
397 do (cond
398 ((not slot)
399 (push (slot-definition-from-property class property) slots))
400 ((getf (rest slot) :merge)
401 (setf
402 (rest slot)
403 (rest (slot-definition-from-property class property (rest slot)))))))
404 (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
405
406
407 (defun expand-gobject-type (type &optional options (metaclass 'gobject-class))
408 (let ((supers (cons (supertype type) (implements type)))
409 (class (type-from-number type))
410 (slots (getf options :slots)))
411 `(defclass ,class ,supers
412 ,(slot-definitions class (query-object-class-properties type) slots)
413 (:metaclass ,metaclass)
414 (:alien-name ,(find-type-name type)))))
415
416
417 (register-derivable-type 'gobject "GObject" 'expand-gobject-type)
418
419
420 ;;; Pseudo type for gobject instances which have their reference count
421 ;;; increased by the returning function
422
423 (defmethod alien-type ((type (eql 'referenced)) &rest args)
424 (declare (ignore type args))
425 (alien-type 'gobject))
426
427 (defmethod from-alien-form (form (type (eql 'referenced)) &rest args)
428 (declare (ignore type))
429 (destructuring-bind (type) args
430 (if (subtypep type 'gobject)
431 (let ((instance (make-symbol "INSTANCE")))
432 `(let ((,instance ,(from-alien-form form type)))
433 (when ,instance
434 (%object-unref (proxy-location ,instance)))
435 ,instance))
436 (error "~A is not a subclass of GOBJECT" type))))
437
438 (export 'referenced)