chiark / gitweb /
Major cleanup of ffi abstraction layer
[clg] / glib / gobject.lisp
CommitLineData
560af5c5 1;; Common Lisp bindings for GTK+ v2.0
82747bbd 2;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
560af5c5 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
9adccb27 18;; $Id: gobject.lisp,v 1.17 2004-11-06 21:39:58 espen Exp $
560af5c5 19
20(in-package "GLIB")
21
22
23(eval-when (:compile-toplevel :load-toplevel :execute)
c8c48a4c 24 (defclass gobject (ginstance)
560af5c5 25 ()
c8c48a4c 26 (:metaclass ginstance-class)
9adccb27 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))))
560af5c5 34
4d83a8a6 35
82747bbd 36(defmethod initialize-instance ((object gobject) &rest initargs)
9adccb27 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)))))
a4b3ec58 80
0f134a29 81 (%object-weak-ref object)
4d83a8a6 82 (apply #'call-next-method object initargs))
83
84
9adccb27 85(defmethod initialize-instance :around ((object gobject) &rest initargs)
0f134a29 86 (declare (ignore initargs))
87 (call-next-method)
9adccb27 88 (%object-weak-ref object))
0f134a29 89
9adccb27 90
91(def-callback weak-notify (c-call:void (data c-call:int) (location system-area-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))))
0f134a29 97
98(defbinding %object-weak-ref (object) nil
99 (object gobject)
100 ((callback weak-notify) pointer)
101 (0 unsigned-int))
102
d4b21b08 103(defbinding (%gobject-new "g_object_new") () pointer
a9044181 104 (type type-number)
105 (nil null))
560af5c5 106
9adccb27 107(defbinding (%gobject-newv "g_object_newv") () pointer
4d83a8a6 108 (type type-number)
109 (n-parameters unsigned-int)
9adccb27 110 (params pointer))
de8516ca 111
112
d4b21b08 113
323d4265 114;;;; Property stuff
560af5c5 115
26b133ed 116(defbinding %object-set-property () nil
c8c48a4c 117 (object gobject)
118 (name string)
119 (value gvalue))
86d9d6ab 120
26b133ed 121(defbinding %object-get-property () nil
c8c48a4c 122 (object gobject)
123 (name string)
a9044181 124 (value gvalue))
86d9d6ab 125
26b133ed 126(defbinding %object-notify () nil
c8c48a4c 127 (object gobject)
128 (name string))
86d9d6ab 129
26b133ed 130(defbinding object-freeze-notify () nil
a9044181 131 (object gobject))
86d9d6ab 132
26b133ed 133(defbinding object-thaw-notify () nil
a9044181 134 (object gobject))
86d9d6ab 135
26b133ed 136(defbinding %object-set-qdata-full () nil
86d9d6ab 137 (object gobject)
138 (id quark)
139 (data unsigned-long)
140 (destroy-marshal pointer))
141
a9044181 142
143;;;; User data
144
86d9d6ab 145(defun (setf object-data) (data object key &key (test #'eq))
146 (%object-set-qdata-full
147 object (quark-from-object key :test test)
0f134a29 148 (register-user-data data) (callback %destroy-user-data))
86d9d6ab 149 data)
150
26b133ed 151(defbinding %object-get-qdata () unsigned-long
86d9d6ab 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
560af5c5 160
a9044181 161;;;; Metaclass used for subclasses of gobject
162
9adccb27 163;(eval-when (:compile-toplevel :load-toplevel :execute)
4d83a8a6 164 (defclass gobject-class (ginstance-class)
165 ())
c8c48a4c 166
9c03a07c 167 (defclass direct-property-slot-definition (direct-virtual-slot-definition)
4d83a8a6 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
9c03a07c 173 (defclass effective-property-slot-definition (effective-virtual-slot-definition)
4d83a8a6 174 ((pname :reader slot-definition-pname :initarg :pname)
175 (readable :reader slot-readable-p :initarg :readable)
176 (writable :reader slot-writable-p :initarg :writable)
9adccb27 177 (construct :initarg :construct)));)
c8c48a4c 178
9adccb27 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))
c8c48a4c 192
560af5c5 193
26b133ed 194; (defbinding object-class-install-param () nil
560af5c5 195; (class pointer)
196; (id unsigned-int)
197; (parameter parameter))
198
26b133ed 199; (defbinding object-class-find-param-spec () parameter
560af5c5 200; (class pointer)
201; (name string))
202
de8516ca 203(defun signal-name-to-string (name)
204 (substitute #\_ #\- (string-downcase (string name))))
a9044181 205
a9044181 206
4d83a8a6 207(defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
a9044181 208 (case (getf initargs :allocation)
9c03a07c 209 (:property (find-class 'direct-property-slot-definition))
a9044181 210 (t (call-next-method))))
211
4d83a8a6 212(defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
a9044181 213 (case (getf initargs :allocation)
9c03a07c 214 (:property (find-class 'effective-property-slot-definition))
a9044181 215 (t (call-next-method))))
216
4d83a8a6 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
9c03a07c 229(defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
4d83a8a6 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)
9adccb27 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)))))
4d83a8a6 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)
9adccb27 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)))
4d83a8a6 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)
a9044181 266 #'(lambda (object)
4d83a8a6 267 (declare (ignore object))
268 t))))
269 (call-next-method))
270
a9044181 271
a9044181 272(defmethod validate-superclass ((class gobject-class)
273 (super pcl::standard-class))
4de90d10 274; (subtypep (class-name super) 'gobject)
275 t)
d4b21b08 276
277
278
279;;;;
280
323d4265 281(defbinding %object-class-list-properties () pointer
d4b21b08 282 (class pointer)
283 (n-properties unsigned-int :out))
284
9c03a07c 285
286(defun %map-params (params length type inherited-p)
287 (if inherited-p
9adccb27 288 (map-c-vector 'list #'identity params 'param length)
9c03a07c 289 (let ((properties ()))
9adccb27 290 (map-c-vector 'list
9c03a07c 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 )))
d4b21b08 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
4d83a8a6 317 (if (eq type 'boolean) "-P" ""))))
d4b21b08 318
323d4265 319
9c03a07c 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)))))
323d4265 383
d4b21b08 384
9c03a07c 385(register-derivable-type 'gobject "GObject" 'expand-gobject-type)