Index: trunk/src/host/pye17/codegen/codegen.py
===================================================================
--- trunk/src/host/pye17/codegen/codegen.py	(revision 4694)
+++ trunk/src/host/pye17/codegen/codegen.py	(revision 4879)
@@ -205,4 +205,14 @@
         )
 
+    cb_function_tmpl = (
+        'static PyObject *%(cb_name)s_obj = NULL;\n\n'
+        'void %(c_name)s_cb(void)\n'
+        '{\n'
+        '    PyObject *arglist = NULL;\n\n'
+        '    if (%(cb_name)s_obj != NULL)\n'
+	'        PyEval_CallObject(%(cb_name)s_obj, arglist);\n'
+        '}\n\n'
+        )
+
     virtual_accessor_tmpl = (
         'static PyObject *\n'
@@ -278,7 +288,8 @@
         # Maybe this could be done in a nicer way, but I'll leave it as it is
         # for now: -- Johan
-        if not self.overrides.slot_is_overriden('%s.tp_init' %
-                                                self.objinfo.c_name):
-            substdict['tp_init'] = self.write_constructor()
+        if not self.overrides.slot_is_overriden('%s.tp_init' % self.objinfo.c_name):
+           #import sys
+           #sys.stderr.write("tp_init: " + str(self.objinfo.c_name) + "\n")
+           substdict['tp_init'] = self.write_constructor()
         substdict['tp_methods'] = self.write_methods()
         substdict['tp_getset'] = self.write_getsets()
@@ -303,5 +314,5 @@
         self.write_virtuals()
 
-    def write_function_wrapper(self, function_obj, template,
+    def write_function_wrapper(self, function_obj, template, cb_template,
                                handle_return=0, is_method=0, kwargs_needed=0,
                                substdict=None):
@@ -311,4 +322,6 @@
 
         info = argtypes.WrapperInfo()
+	info.function_c_name = function_obj.c_name
+	info.piscb = 0
 
         substdict.setdefault('errorreturn', 'NULL')
@@ -329,7 +342,9 @@
                 info.add_parselist('|', [], [])
             handler = argtypes.matcher.get(param.ptype)
+	    if param.piscb:
+	      info.piscb += 1
 	    #fd.write("pname: " + param.pname + "; ptype: " + str(param.ptype) + "; pdflt: " + str(param.pdflt) + "; pnull: " + str(param.pnull) + "; handler: " + str(handler) + "\n")
             handler.write_param(param.ptype, param.pname, param.pdflt,
-                                param.pnull, info)
+                                param.pnull, info, param.piscb)
 
 	#fd.write("arglist 2: " + str(info.get_arglist()) + "\n")
@@ -368,5 +383,17 @@
         substdict['typecodes'] = info.parsestr
         substdict['parselist'] = info.get_parselist()
+
         substdict['arglist'] = info.get_arglist()
+
+	substdict['cb_code'] = ""
+
+	# TODO: deal with more arguments
+	if info.piscb > 0:
+	  substdict['arglist'] = ", &" + function_obj.c_name + "_cb"
+	  substdict['cb_code'] = cb_template % {'c_name': function_obj.c_name,
+	                                        'cb_name': function_obj.c_name}
+	  template = substdict['cb_code'] + template
+          sys.stderr.write("cb_code: %s\n" % substdict['cb_code'])
+
         substdict['codebefore'] = deprecated + (
             string.replace(info.get_codebefore(),
@@ -390,17 +417,24 @@
             flags = 'METH_NOARGS'
 
-	#fd.write("arglist: " + str(info.get_arglist()) + "\n")
 	#for key,item in substdict.iteritems():
-	#	fd.write("key: " + str(key) + ", item: " + str(item) + "\n")
-        #fd.close()
-
+		#sys.stderr.write("key: " + str(key) + ", item: " + str(item) + "\n")
+
+        if info.piscb > 0:
+           sys.stderr.write("template: %s\n" % (template % substdict))
+	   #sys.stderr.write("cb_code (again): %s\n" % substdict['cb_code'])
+	   #substdict['callback_code'] = 'balbla'
+	   #for key,item in substdict.iteritems():
+		#sys.stderr.write("key: " + str(key) + ", item: " + str(item) + "\n")
         return template % substdict, flags
 
     def write_constructor(self):
         initfunc = '0'
+	#import sys
+	#sys.stderr.write("searching constructor ...\n")
         constructor = self.parser.find_constructor(self.objinfo,self.overrides)
         if not constructor:
             return self.write_default_constructor()
 
+	#sys.stderr.write("constructor: " + str(constructor.c_name) + "\n")
         funcname = constructor.c_name
         try:
@@ -431,4 +465,5 @@
 
                 # write constructor from template ...
+		#sys.stderr.write("constructor: " + str(funcname) + ", tmpl:" + str(self.constructor_tmpl) + "\n")
                 code = self.write_function_wrapper(constructor,
                     self.constructor_tmpl,
@@ -506,6 +541,6 @@
                     # write constructor from template ...
                     code, methflags = self.write_function_wrapper(meth,
-                        self.method_tmpl, handle_return=1, is_method=1,
-                        substdict=self.get_initial_method_substdict(meth))
+                        self.method_tmpl, self.cb_function_tmpl, handle_return=1,
+			is_method=1, substdict=self.get_initial_method_substdict(meth))
                     self.fp.write(code)
                 methods.append(self.methdef_tmpl %
@@ -857,5 +892,5 @@
                     # write constructor from template ...
                     code, methflags = self.write_function_wrapper(func,
-                        self.function_tmpl, handle_return=1, is_method=0)
+                        self.function_tmpl, self.cb_function_tmpl, handle_return=1, is_method=0)
                     self.fp.write(code)
                 functions.append((func.name, '_wrap_' + funcname,
