Added abstraction layer for C callback functions
[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.18 2004-11-07 01:23:38 espen Exp $
19
20 (in-package "GLIB")
21
22
23 (eval-when (:compile-toplevel :load-toplevel :execute)
24 (defclass gobject (ginstance)
25 ()
26 (:metaclass ginstance-class)
27 (:alien-name "GObject")))
28
29 (defmethod print-object ((instance gobject) stream)
30 (print-unreadable-object (instance stream :type t :identity nil)
31 (if (slot-boundp instance 'location)
32 (format stream "at 0x~X" (sap-int (proxy-location instance)))
33 (write-string "(destroyed)" stream))))
34
35
36 (defmethod initialize-instance ((object gobject) &rest initargs)
37 ;; Extract initargs which we should pass directly to the GObeject
38 ;; constructor
39 (let* ((slotds (class-slots (class-of object)))
40 (args (loop
41 as tmp = initargs then (cddr tmp) while tmp
42 as key = (first tmp)
43 as value = (second tmp)
44 as slotd = (find-if
45 #'(lambda (slotd)
46 (member key (slot-definition-initargs slotd)))
47 slotds)
48 when (and (typep slotd 'effective-property-slot-definition)
49 (slot-value slotd 'construct))
50 collect (progn
51 (remf initargs key)
52 (list
53 (slot-definition-pname slotd)
54 (slot-definition-type slotd)
55 value)))))
56 (if args
57 (let* ((string-size (size-of 'string))
58 (string-writer (writer-function 'string))
59 (string-destroy (destroy-function 'string))
60 (params (allocate-memory
61 (* (length args) (+ string-size +gvalue-size+)))))
62 (loop
63 for (pname type value) in args
64 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
65 do (funcall string-writer pname tmp)
66 (gvalue-init (sap+ tmp string-size) type value))
67 (unwind-protect
68 (setf
69 (slot-value object 'location)
70 (%gobject-newv (type-number-of object) (length args) params))
71 (loop
72 repeat (length args)
73 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
74 do (funcall string-destroy tmp)
75 (gvalue-unset (sap+ tmp string-size)))
76 (deallocate-memory params)))
77 (setf
78 (slot-value object 'location)
79 (%gobject-new (type-number-of object)))))
80
81 (%object-weak-ref object)
82 (apply #'call-next-method object initargs))
83
84
85 (defmethod initialize-instance :around ((object gobject) &rest initargs)
86 (declare (ignore initargs))
87 (call-next-method)
88 (%object-weak-ref object))
89
90
91 (defcallback weak-notify (nil (data int) (location pointer))
92 (let ((object (find-cached-instance location)))
93 (when object
94 ;; (warn "~A being finalized by the GObject system while still in existence in lisp" object)
95 (slot-makunbound object 'location)
96 (remove-cached-instance location))))
97
98 (defbinding %object-weak-ref (object) nil
99 (object gobject)
100 ((callback weak-notify) pointer)
101 (0 unsigned-int))
102
103 (defbinding (%gobject-new "g_object_new") () pointer
104 (type type-number)
105 (nil null))
106
107 (defbinding (%gobject-newv "g_object_newv") () pointer
108 (type type-number)
109 (n-parameters unsigned-int)
110 (params pointer))
111
112
113
114 ;;;; Property stuff
115
116 (defbinding %object-set-property () nil
117 (object gobject)
118 (name string)
119 (value gvalue))
120
121 (defbinding %object-get-property () nil
122 (object gobject)
123 (name string)
124 (value gvalue))
125
126 (defbinding %object-notify () nil
127 (object gobject)
128 (name string))
129
130 (defbinding object-freeze-notify () nil
131 (object gobject))
132
133 (defbinding object-thaw-notify () nil
134 (object gobject))
135
136 (defbinding %object-set-qdata-full () nil
137 (object gobject)
138 (id quark)
139 (data unsigned-long)
140 (destroy-marshal pointer))
141
142
143 ;;;; User data
144
145 (defun (setf object-data) (data object key &key (test #'eq))
146 (%object-set-qdata-full
147 object (quark-from-object key :test test)
148 (register-user-data data) (callback %destroy-user-data))
149 data)
150
151 (defbinding %object-get-qdata () unsigned-long
152 (object gobject)
153 (id quark))
154
155 (defun object-data (object key &key (test #'eq))
156 (find-user-data
157 (%object-get-qdata object (quark-from-object key :test test))))
158
159
160
161 ;;;; Metaclass used for subclasses of gobject
162
163 ;(eval-when (:compile-toplevel :load-toplevel :execute)
164 (defclass gobject-class (ginstance-class)
165 ())
166
167 (defclass direct-property-slot-definition (direct-virtual-slot-definition)
168 ((pname :reader slot-definition-pname :initarg :pname)
169 (readable :initform t :reader slot-readable-p :initarg :readable)
170 (writable :initform t :reader slot-writable-p :initarg :writable)
171 (construct :initform nil :initarg :construct)))
172
173 (defclass effective-property-slot-definition (effective-virtual-slot-definition)
174 ((pname :reader slot-definition-pname :initarg :pname)
175 (readable :reader slot-readable-p :initarg :readable)
176 (writable :reader slot-writable-p :initarg :writable)
177 (construct :initarg :construct)));)
178
179 (defbinding %object-ref () pointer
180 (location pointer))
181
182 (defbinding %object-unref () nil
183 (location pointer))
184
185 (defmethod reference-foreign ((class gobject-class) location)
186 (declare (ignore class))
187 (%object-ref location))
188
189 (defmethod unreference-foreign ((class gobject-class) location)
190 (declare (ignore class))
191 (%object-unref location))
192
193
194 ; (defbinding object-class-install-param () nil
195 ; (class pointer)
196 ; (id unsigned-int)
197 ; (parameter parameter))
198
199 ; (defbinding object-class-find-param-spec () parameter
200 ; (class pointer)
201 ; (name string))
202
203 (defun signal-name-to-string (name)
204 (substitute #\_ #\- (string-downcase (string name))))
205
206
207 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
208 (case (getf initargs :allocation)
209 (:property (find-class 'direct-property-slot-definition))
210 (t (call-next-method))))
211
212 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
213 (case (getf initargs :allocation)
214 (:property (find-class 'effective-property-slot-definition))
215 (t (call-next-method))))
216
217 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
218 (if (eq (most-specific-slot-value direct-slotds 'allocation) :property)
219 (nconc
220 (list :pname (signal-name-to-string
221 (most-specific-slot-value direct-slotds 'pname))
222 :readable (most-specific-slot-value direct-slotds 'readable)
223 :writable (most-specific-slot-value direct-slotds 'writable)
224 :construct (most-specific-slot-value direct-slotds 'construct))
225 (call-next-method))
226 (call-next-method)))
227
228
229 (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
230 (let* ((type (slot-definition-type slotd))
231 (pname (slot-definition-pname slotd))
232 (type-number (find-type-number type)))
233 (unless (slot-boundp slotd 'reader-function)
234 (setf
235 (slot-value slotd 'reader-function)
236 (if (slot-readable-p slotd)
237 (let () ;(reader (reader-function (type-from-number type-number))))
238 #'(lambda (object)
239 (let ((gvalue (gvalue-new type-number)))
240 (%object-get-property object pname gvalue)
241 (unwind-protect
242 (funcall #|reader|# (reader-function (type-from-number type-number)) gvalue +gvalue-value-offset+)
243 (gvalue-free gvalue t)))))
244 #'(lambda (value object)
245 (error "Slot is not readable: ~A" (slot-definition-name slotd))))))
246
247 (unless (slot-boundp slotd 'writer-function)
248 (setf
249 (slot-value slotd 'writer-function)
250 (if (slot-writable-p slotd)
251 (let ();; (writer (writer-function (type-from-number type-number)))
252 ;; (destroy (destroy-function (type-from-number type-number))))
253 #'(lambda (value object)
254 (let ((gvalue (gvalue-new type-number)))
255 (funcall #|writer|# (writer-function (type-from-number type-number)) value gvalue +gvalue-value-offset+)
256 (%object-set-property object pname gvalue)
257 ; (funcall #|destroy|#(destroy-function (type-from-number type-number)) gvalue +gvalue-value-offset+)
258 (gvalue-free gvalue t)
259 value)))
260 #'(lambda (value object)
261 (error "Slot is not writable: ~A" (slot-definition-name slotd))))))
262
263 (unless (slot-boundp slotd 'boundp-function)
264 (setf
265 (slot-value slotd 'boundp-function)
266 #'(lambda (object)
267 (declare (ignore object))
268 t))))
269 (call-next-method))
270
271
272 (defmethod validate-superclass ((class gobject-class)
273 (super pcl::standard-class))
274 ; (subtypep (class-name super) 'gobject)
275 t)
276
277
278
279 ;;;;
280
281 (defbinding %object-class-list-properties () pointer
282 (class pointer)
283 (n-properties unsigned-int :out))
284
285
286 (defun %map-params (params length type inherited-p)
287 (if inherited-p
288 (map-c-vector 'list #'identity params 'param length)
289 (let ((properties ()))
290 (map-c-vector 'list
291 #'(lambda (param)
292 (when (eql (param-owner-type param) type)
293 (push param properties)))
294 params 'param length)
295 (nreverse properties))))
296
297 (defun query-object-class-properties (type &optional inherited-p)
298 (let* ((type-number (find-type-number type))
299 (class (type-class-ref type-number)))
300 (unwind-protect
301 (multiple-value-bind (array length)
302 (%object-class-list-properties class)
303 (unwind-protect
304 (%map-params array length type-number inherited-p)
305 (deallocate-memory array)))
306 ; (type-class-unref type-number)
307 )))
308
309
310 (defun default-slot-name (name)
311 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
312
313 (defun default-slot-accessor (class-name slot-name type)
314 (intern
315 (format
316 nil "~A-~A~A" class-name slot-name
317 (if (eq type 'boolean) "-P" ""))))
318
319
320 (defun slot-definition-from-property (class property)
321 (with-slots (name flags value-type documentation) property
322 (let* ((slot-name (default-slot-name name))
323 (slot-type (or (type-from-number value-type) value-type))
324 (accessor (default-slot-accessor class slot-name slot-type)))
325
326 `(,slot-name
327 :allocation :property :pname ,name
328
329 ;; accessors
330 ,@(cond
331 ((and
332 (member :writable flags) (member :readable flags)
333 (not (member :construct-only flags)))
334 (list :accessor accessor))
335 ((and (member :writable flags) (not (member :construct-only flags)))
336 (list :writer `(setf ,accessor)))
337 ((member :readable flags)
338 (list :reader accessor)))
339
340 ;; readable/writable/construct
341 ,@(when (or (not (member :writable flags))
342 (member :construct-only flags))
343 '(:writable nil))
344 ,@(when (not (member :readable flags))
345 '(:readable nil))
346 ,@(when (or (member :construct flags)
347 (member :construct-only flags))
348 '(:construct t))
349
350 ;; initargs
351 ,@(when (or (member :construct flags)
352 (member :construct-only flags)
353 (member :writable flags))
354 (list :initarg (intern (string slot-name) "KEYWORD")))
355
356 :type ,slot-type
357 :documentation ,documentation))))
358
359
360 (defun slot-definitions (class properties slots)
361 (loop
362 with manual-slots = slots
363 for property in properties
364 unless (find-if
365 #'(lambda (slot)
366 (destructuring-bind (name &rest args) slot
367 (or
368 (equal (param-name property) (getf args :pname))
369 (eq (default-slot-name (param-name property)) name))))
370 manual-slots)
371 do (push (slot-definition-from-property class property) slots))
372 (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
373
374
375 (defun expand-gobject-type (type &optional options (metaclass 'gobject-class))
376 (let ((supers (cons (supertype type) (implements type)))
377 (class (type-from-number type))
378 (slots (getf options :slots)))
379 `(defclass ,class ,supers
380 ,(slot-definitions class (query-object-class-properties type) slots)
381 (:metaclass ,metaclass)
382 (:alien-name ,(find-type-name type)))))
383
384
385 (register-derivable-type 'gobject "GObject" 'expand-gobject-type)