chiark / gitweb /
Added library component class
[clg] / glib / gcallback.lisp
CommitLineData
c9819f3e 1;; Common Lisp bindings for GTK+ v2.0
2;; Copyright (C) 2000 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
935a783c 18;; $Id: gcallback.lisp,v 1.9 2004/10/27 14:58:59 espen Exp $
c9819f3e 19
20(in-package "GLIB")
21
22(use-prefix "g")
23
24
25;;;; Closures
26
27(deftype gclosure () 'pointer)
28
3f4249c7 29(defbinding lisp-callback-closure-new () gclosure
c9819f3e 30 (callback-id unsigned-int))
31
60cfb912 32(defun register-callback-function (function)
33 (check-type function (or null symbol function))
34 (register-user-data function))
c9819f3e 35
60cfb912 36(defun make-callback-closure (function)
37 (lisp-callback-closure-new (register-callback-function function)))
c9819f3e 38
c9819f3e 39
60cfb912 40;;;; Callback mechanism
c9819f3e 41
42(defun callback-trampoline (callback-id params return-value)
43 (let* ((return-type (unless (null-pointer-p return-value)
60cfb912 44 (gvalue-type return-value)))
c9819f3e 45 (args nil)
46 (callback-function (find-user-data callback-id)))
47
48 (destructuring-bind (nparams . param-values) params
49 (dotimes (n nparams)
7eec806d 50 (push (gvalue-get (sap+ param-values (* n +gvalue-size+))) args)))
c9819f3e 51
52 (labels ((invoke-callback ()
53 (restart-case
54 (unwind-protect
d70890e5 55 (let ((result (apply callback-function (reverse args))))
c9819f3e 56 (when return-type
935a783c 57 (gvalue-set return-value result))))
c9819f3e 58
59 (continue nil :report "Return from callback function"
60 (when return-type
61 (format
62 *query-io*
63 "Enter return value of type ~S: "
64 return-type)
65 (force-output *query-io*)
7eec806d 66 (gvalue-set return-value (eval (read *query-io*)))))
c9819f3e 67 (re-invoke nil :report "Re-invoke callback function"
68 (invoke-callback)))))
69 (invoke-callback))))
70
71(defun after-gc-hook ()
72 (setf
73 (extern-alien "callback_trampoline" system-area-pointer)
74 (make-pointer (kernel:get-lisp-obj-address #'callback-trampoline))
75 (extern-alien "destroy_user_data" system-area-pointer)
76 (make-pointer (kernel:get-lisp-obj-address #'destroy-user-data))))
77
78(pushnew 'after-gc-hook ext:*after-gc-hooks*)
79(after-gc-hook)
80
81
60cfb912 82;;;; Timeouts and idle functions
83
84(defvar *source-callback-marshal*
85 (system:foreign-symbol-address "source_callback_marshal"))
86(defvar *destroy-notify*
87 (system:foreign-symbol-address "destroy_notify"))
88
89(defbinding (timeout-add "g_timeout_add_full")
90 (function interval &optional (priority 0)) unsigned-int
91 (priority int)
92 (interval unsigned-int)
93 (*source-callback-marshal* pointer)
94 ((register-callback-function function) unsigned-long)
95 (*destroy-notify* pointer))
96
97(defbinding (idle-add "g_idle_add_full")
98 (function &optional (priority 0)) unsigned-int
99 (priority int)
100 (*source-callback-marshal* pointer)
101 ((register-callback-function function) unsigned-long)
102 (*destroy-notify* pointer))
103
104
c9819f3e 105
106;;;; Signals
107
3f4249c7 108(defbinding signal-lookup (name itype) unsigned-int
c9819f3e 109 ((signal-name-to-string name) string)
110 (itype type-number))
111
3f4249c7 112(defbinding signal-name () string
c9819f3e 113 (signal-id unsigned-int))
114
7eec806d 115(defun ensure-signal-id (signal-id instance)
c9819f3e 116 (etypecase signal-id
117 (integer signal-id)
118 (string (signal-lookup signal-id (type-number-of instance)))
119 (symbol (signal-lookup signal-id (type-number-of instance)))))
120
3f4249c7 121(defbinding signal-stop-emission (instance signal-id) nil
c9819f3e 122 (instance ginstance)
7eec806d 123 ((ensure-signal-id signal-id instance) unsigned-int))
c9819f3e 124
3f4249c7 125; (defbinding (signal-add-emisson-hook "g_signal_add_emission_hook_full")
c9819f3e 126; () unsigned-int
127; (signal-id unsigned-int)
128; (closure gclosure))
129
3f4249c7 130; (defbinding signal-remove-emisson-hook () nil
c9819f3e 131; (signal-id unsigned-int)
132; (hook-id unsigned-int))
133
3f4249c7 134(defbinding (signal-has-handler-pending-p "g_signal_has_handler_pending")
c9819f3e 135 (instance signal-id &key detail blocked) boolean
136 (instance ginstance)
7eec806d 137 ((ensure-signal-id signal-id instance) unsigned-int)
c9819f3e 138 ((or detail 0) quark)
139 (blocked boolean))
140
3f4249c7 141(defbinding (signal-connect-closure "g_signal_connect_closure_by_id")
c9819f3e 142 (instance signal-id closure &key detail after) unsigned-int
143 (instance ginstance)
7eec806d 144 ((ensure-signal-id signal-id instance) unsigned-int)
c9819f3e 145 ((or detail 0) quark)
146 (closure gclosure)
147 (after boolean))
148
3f4249c7 149(defbinding signal-handler-block () nil
c9819f3e 150 (instance ginstance)
151 (handler unsigned-int))
152
3f4249c7 153(defbinding signal-handler-unblock () nil
c9819f3e 154 (instance ginstance)
155 (handler unsigned-int))
156
3f4249c7 157(defbinding signal-handler-disconnect () nil
c9819f3e 158 (instance ginstance)
159 (handler unsigned-int))
160
161
6d00d707 162(defmethod signal-connect ((gobject gobject) signal function &key after object)
935a783c 163"Connects a callback function to a signal for a particular object. If :OBJECT
164 is T, the object connected to is passed as the first argument to the callback
165 function, or if :OBJECT is any other non NIL value, it is passed as the first
166 argument instead. If :AFTER is non NIL, the handler will be called after the
167 default handler of the signal."
6d00d707 168 (let ((callback-id
169 (make-callback-closure
170 (cond
171 ((or (eq object t) (eq object gobject)) function)
172 ((not object)
173 #'(lambda (&rest args) (apply function (cdr args))))
174 (t
175 #'(lambda (&rest args) (apply function object (rest args))))))))
d70890e5 176 (signal-connect-closure gobject signal callback-id :after after)))