Changeset 4879


Ignore:
Timestamp:
12/23/08 10:24:23 (4 years ago)
Author:
marek
Message:

add primitive callback support

Location:
trunk/src/host/pye17
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/host/pye17/Makefile

    r4720 r4879  
    33ifeq ($(wildcard $(E_PATH)),) 
    44 
    5 E_PATH_TMP=~/openmoko/e17 
     5E_PATH_TMP=~/openmoko/e 
    66ifneq ($(wildcard $(E_PATH_TMP)),) 
    77E_PATH = $(E_PATH_TMP) 
  • trunk/src/host/pye17/codegen/argtypes.py

    r4694 r4879  
    7777 
    7878class ArgType: 
    79     def write_param(self, ptype, pname, pdflt, pnull, info): 
     79    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    8080        """Add code to the WrapperInfo instance to handle 
    8181        parameter.""" 
     
    9595 
    9696class StringArg(ArgType): 
    97     def write_param(self, ptype, pname, pdflt, pnull, info): 
     97    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    9898        if pdflt != None: 
    9999            if pdflt != 'NULL': pdflt = '"' + pdflt + '"' 
     
    125125 
    126126class StringPtrArg(ArgType): 
    127     def write_param(self, ptype, pname, pdflt, pnull, info): 
     127    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    128128        if pdflt != None: 
    129129            if pdflt != 'NULL': pdflt = '"' + pdflt + '"' 
     
    156156class UCharArg(ArgType): 
    157157    # allows strings with embedded NULLs. 
    158     def write_param(self, ptype, pname, pdflt, pnull, info): 
     158    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    159159        if pdflt: 
    160160            info.varlist.add('guchar', '*' + pname + ' = "' + pdflt + '"') 
     
    174174 
    175175class CharArg(ArgType): 
    176     def write_param(self, ptype, pname, pdflt, pnull, info): 
     176    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    177177        if pdflt: 
    178178            info.varlist.add('char', pname + " = '" + pdflt + "'") 
     
    193193                '    py_ret = (Py_UNICODE)ret;\n' 
    194194                '    return PyUnicode_FromUnicode(&py_ret, 1);\n') 
    195     def write_param(self, ptype, pname, pdflt, pnull, info): 
     195    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    196196        if pdflt: 
    197197            info.varlist.add('gunichar', pname + " = '" + pdflt + "'") 
     
    207207 
    208208class IntArg(ArgType): 
    209     def write_param(self, ptype, pname, pdflt, pnull, info): 
     209    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    210210        if pdflt: 
    211211            info.varlist.add('int', pname + ' = ' + pdflt) 
     
    237237              '    if (PyErr_Occurred())\n' 
    238238              '        return NULL;\n') 
    239     def write_param(self, ptype, pname, pdflt, pnull, info): 
     239    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    240240        if not pdflt: 
    241241            pdflt = '0'; 
     
    260260        llp64 = False 
    261261 
    262     def write_param(self, ptype, pname, pdflt, pnull, info): 
     262    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    263263        if pdflt: 
    264264            info.varlist.add(ptype, pname + ' = ' + pdflt) 
     
    284284        llp64 = False 
    285285 
    286     def write_param(self, ptype, pname, pdflt, pnull, info): 
     286    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    287287        if pdflt: 
    288288            info.varlist.add(ptype, pname + ' = ' + pdflt) 
     
    302302 
    303303class LongArg(ArgType): 
    304     def write_param(self, ptype, pname, pdflt, pnull, info): 
     304    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    305305        if pdflt: 
    306306            info.varlist.add(ptype, pname + ' = ' + pdflt) 
     
    319319 
    320320class TimeTArg(ArgType): 
    321     def write_param(self, ptype, pname, pdflt, pnull, info): 
     321    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    322322        if pdflt: 
    323323            info.varlist.add('time_t', pname + ' = ' + pdflt) 
     
    331331 
    332332class ULongArg(ArgType): 
    333     def write_param(self, ptype, pname, pdflt, pnull, info): 
     333    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    334334        if pdflt: 
    335335            info.varlist.add('unsigned long', pname + ' = ' + pdflt) 
     
    343343 
    344344class UInt32Arg(ULongArg): 
    345     def write_param(self, ptype, pname, pdflt, pnull, info): 
     345    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    346346        ULongArg.write_param(self, ptype, pname, pdflt, pnull, info) 
    347347        ## if sizeof(unsigned long) > sizeof(unsigned int), we need to 
     
    357357 
    358358class Int64Arg(ArgType): 
    359     def write_param(self, ptype, pname, pdflt, pnull, info): 
     359    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    360360        if pdflt: 
    361361            info.varlist.add('gint64', pname + ' = ' + pdflt) 
     
    372372           '        %(name)s = PyLong_AsUnsignedLongLong(py_%(name)s);\n' 
    373373    before = '    %(name)s = PyLong_AsUnsignedLongLong(py_%(name)s);\n' 
    374     def write_param(self, ptype, pname, pdflt, pnull, info): 
     374    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    375375        if pdflt: 
    376376            info.varlist.add('guint64', pname + ' = ' + pdflt) 
     
    388388 
    389389class DoubleArg(ArgType): 
    390     def write_param(self, ptype, pname, pdflt, pnull, info): 
     390    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    391391        if pdflt: 
    392392            info.varlist.add('double', pname + ' = ' + pdflt) 
     
    416416    dflt = ('    if (py_%(name)s)\n' 
    417417            '        %(name)s = PyFile_AsFile(py_%(name)s);\n') 
    418     def write_param(self, ptype, pname, pdflt, pnull, info): 
     418    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    419419        if pnull: 
    420420            if pdflt: 
     
    451451        self.enumname = enumname 
    452452        self.typecode = typecode 
    453     def write_param(self, ptype, pname, pdflt, pnull, info): 
     453    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    454454        if pdflt: 
    455455            info.varlist.add(self.enumname, pname + ' = ' + pdflt) 
     
    471471        self.flagname = flagname 
    472472        self.typecode = typecode 
    473     def write_param(self, ptype, pname, pdflt, pnull, info): 
     473    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    474474        if pdflt: 
    475475            info.varlist.add(self.flagname, pname + ' = ' + pdflt) 
     
    511511        self.cast = string.replace(typecode, '_TYPE_', '_', 1) 
    512512        self.parent = parent 
    513     def write_param(self, ptype, pname, pdflt, pnull, info): 
     513    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    514514        if pnull: 
    515515            if pdflt: 
     
    591591        self.typename = ptype 
    592592        self.typecode = typecode 
    593     def write_param(self, ptype, pname, pdflt, pnull, info): 
     593    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    594594        if pnull: 
    595595            info.varlist.add(self.typename, '*' + pname + ' = NULL') 
     
    646646        self.checker = 'Py' + ptype + '_Check' 
    647647        self.new = new 
    648     def write_param(self, ptype, pname, pdflt, pnull, info): 
     648    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    649649        if pnull: 
    650650            info.varlist.add(ptype[:-1], '*' + pname + ' = NULL') 
     
    681681            '        return NULL;\n' 
    682682            '    }\n') 
     683    cb = ('    if (!PyCallable_Check(py_%(name)s)) {\n' 
     684          '        PyErr_SetString(PyExc_TypeError, "parameter must be callable");\n' 
     685          '        return NULL;\n' 
     686          '     }\n\n' 
     687          '     /* Py_XINCREF & Py_XDECREF are NULL safe */\n' 
     688          '     Py_XINCREF(py_%(name)s);   /* Add a reference to new callback */\n' 
     689          '     Py_XDECREF(%(cb_name)s_obj);   /* Dispose of previous callback */\n' 
     690          '     %(cb_name)s_obj = py_%(name)s;   /* Remember new callback */\n') 
    683691    def __init__(self, ptype, typecode): 
    684692        self.typename = ptype 
    685693        self.typecode = typecode 
    686     def write_param(self, ptype, pname, pdflt, pnull, info): 
     694    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    687695        if self.typename == "unsigned-int": 
    688696                local_typename = "unsigned int" 
     
    690698                local_typename = self.typename 
    691699 
    692         if pnull: 
     700        if piscb: 
     701            info.varlist.add(local_typename, '*' + pname + ' = NULL') 
     702            info.varlist.add('PyObject', '*py_' + pname + ' = Py_None') 
     703            info.codebefore.append(self.cb % {'name':  pname, 
     704                                              'cb_name':  info.function_c_name}) 
     705 
     706        elif pnull: 
    693707            info.varlist.add(local_typename, '*' + pname + ' = NULL') 
    694708            info.varlist.add('PyObject', '*py_' + pname + ' = Py_None') 
     
    723737            '    if (PyErr_Occurred())\n' 
    724738            '        return NULL;\n') 
    725     def write_param(self, ptype, pname, pdflt, pnull, info): 
     739    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    726740        if pdflt: 
    727741            info.varlist.add('GdkAtom', pname + ' = ' + pdflt) 
     
    746760    gtype = ('    if ((%(name)s = pyg_type_from_object(py_%(name)s)) == 0)\n' 
    747761             '        return NULL;\n') 
    748     def write_param(self, ptype, pname, pdflt, pnull, info): 
     762    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    749763        info.varlist.add('GType', pname) 
    750764        info.varlist.add('PyObject', '*py_' + pname + ' = NULL') 
     
    760774    handleerror = ('    if (pyg_error_check(&%(name)s))\n' 
    761775                   '        return NULL;\n') 
    762     def write_param(self, ptype, pname, pdflt, pnull, info): 
     776    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    763777        info.varlist.add('GError', '*' + pname + ' = NULL') 
    764778        info.arglist.append('&' + pname) 
     
    783797    def __init__(self): 
    784798        pass 
    785     def write_param(self, ptype, pname, pdflt, pnull, info): 
     799    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    786800        if pnull: 
    787801            info.varlist.add('GtkTreePath', '*' + pname + ' = NULL') 
     
    824838              '    else\n' 
    825839              '            return NULL;\n') 
    826     def write_param(self, ptype, pname, pdflt, pnull, info): 
     840    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    827841        if pnull: 
    828842            info.varlist.add('GdkRectangle', pname + '_rect = { 0, 0, 0, 0 }') 
     
    845859 
    846860class PyObjectArg(ArgType): 
    847     def write_param(self, ptype, pname, pdflt, pnull, info): 
     861    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    848862        info.varlist.add('PyObject', '*' + pname) 
    849863        info.add_parselist('O', ['&' + pname], [pname]) 
     
    863877 
    864878class CairoArg(ArgType): 
    865     def write_param(self, ptype, pname, pdflt, pnull, info): 
     879    def write_param(self, ptype, pname, pdflt, pnull, info, piscb): 
    866880        info.varlist.add('PycairoContext', '*' + pname) 
    867881        info.add_parselist('O!', ['&PycairoContext_Type', '&' + pname], [pname]) 
     
    902916            self.register(ptype, FlagsArg(ptype, typecode)) 
    903917    def register_object(self, ptype, parent, typecode): 
    904         #fd = open("/tmp/debug.log","a") 
    905         #fd.write("ptype: " + str(ptype) + ", parent:" + str(parent) + ", typecode: " + str(typecode) + "\n") 
    906         #fd.close() 
     918        #import sys 
     919        #sys.stderr.write("ptype: " + str(ptype) + ", parent:" + str(parent) + ", typecode: " + str(typecode) + "\n") 
    907920        oa = ObjectArg(ptype, parent, typecode) 
    908921        self.register(ptype, oa)  # in case I forget the * in the .defs 
  • trunk/src/host/pye17/codegen/codegen.py

    r4694 r4879  
    205205        ) 
    206206 
     207    cb_function_tmpl = ( 
     208        'static PyObject *%(cb_name)s_obj = NULL;\n\n' 
     209        'void %(c_name)s_cb(void)\n' 
     210        '{\n' 
     211        '    PyObject *arglist = NULL;\n\n' 
     212        '    if (%(cb_name)s_obj != NULL)\n' 
     213        '        PyEval_CallObject(%(cb_name)s_obj, arglist);\n' 
     214        '}\n\n' 
     215        ) 
     216 
    207217    virtual_accessor_tmpl = ( 
    208218        'static PyObject *\n' 
     
    278288        # Maybe this could be done in a nicer way, but I'll leave it as it is 
    279289        # for now: -- Johan 
    280         if not self.overrides.slot_is_overriden('%s.tp_init' % 
    281                                                 self.objinfo.c_name): 
    282             substdict['tp_init'] = self.write_constructor() 
     290        if not self.overrides.slot_is_overriden('%s.tp_init' % self.objinfo.c_name): 
     291           #import sys 
     292           #sys.stderr.write("tp_init: " + str(self.objinfo.c_name) + "\n") 
     293           substdict['tp_init'] = self.write_constructor() 
    283294        substdict['tp_methods'] = self.write_methods() 
    284295        substdict['tp_getset'] = self.write_getsets() 
     
    303314        self.write_virtuals() 
    304315 
    305     def write_function_wrapper(self, function_obj, template, 
     316    def write_function_wrapper(self, function_obj, template, cb_template, 
    306317                               handle_return=0, is_method=0, kwargs_needed=0, 
    307318                               substdict=None): 
     
    311322 
    312323        info = argtypes.WrapperInfo() 
     324        info.function_c_name = function_obj.c_name 
     325        info.piscb = 0 
    313326 
    314327        substdict.setdefault('errorreturn', 'NULL') 
     
    329342                info.add_parselist('|', [], []) 
    330343            handler = argtypes.matcher.get(param.ptype) 
     344            if param.piscb: 
     345              info.piscb += 1 
    331346            #fd.write("pname: " + param.pname + "; ptype: " + str(param.ptype) + "; pdflt: " + str(param.pdflt) + "; pnull: " + str(param.pnull) + "; handler: " + str(handler) + "\n") 
    332347            handler.write_param(param.ptype, param.pname, param.pdflt, 
    333                                 param.pnull, info) 
     348                                param.pnull, info, param.piscb) 
    334349 
    335350        #fd.write("arglist 2: " + str(info.get_arglist()) + "\n") 
     
    368383        substdict['typecodes'] = info.parsestr 
    369384        substdict['parselist'] = info.get_parselist() 
     385 
    370386        substdict['arglist'] = info.get_arglist() 
     387 
     388        substdict['cb_code'] = "" 
     389 
     390        # TODO: deal with more arguments 
     391        if info.piscb > 0: 
     392          substdict['arglist'] = ", &" + function_obj.c_name + "_cb" 
     393          substdict['cb_code'] = cb_template % {'c_name': function_obj.c_name, 
     394                                                'cb_name': function_obj.c_name} 
     395          template = substdict['cb_code'] + template 
     396          sys.stderr.write("cb_code: %s\n" % substdict['cb_code']) 
     397 
    371398        substdict['codebefore'] = deprecated + ( 
    372399            string.replace(info.get_codebefore(), 
     
    390417            flags = 'METH_NOARGS' 
    391418 
    392         #fd.write("arglist: " + str(info.get_arglist()) + "\n") 
    393419        #for key,item in substdict.iteritems(): 
    394         #       fd.write("key: " + str(key) + ", item: " + str(item) + "\n") 
    395         #fd.close() 
    396  
     420                #sys.stderr.write("key: " + str(key) + ", item: " + str(item) + "\n") 
     421 
     422        if info.piscb > 0: 
     423           sys.stderr.write("template: %s\n" % (template % substdict)) 
     424           #sys.stderr.write("cb_code (again): %s\n" % substdict['cb_code']) 
     425           #substdict['callback_code'] = 'balbla' 
     426           #for key,item in substdict.iteritems(): 
     427                #sys.stderr.write("key: " + str(key) + ", item: " + str(item) + "\n") 
    397428        return template % substdict, flags 
    398429 
    399430    def write_constructor(self): 
    400431        initfunc = '0' 
     432        #import sys 
     433        #sys.stderr.write("searching constructor ...\n") 
    401434        constructor = self.parser.find_constructor(self.objinfo,self.overrides) 
    402435        if not constructor: 
    403436            return self.write_default_constructor() 
    404437 
     438        #sys.stderr.write("constructor: " + str(constructor.c_name) + "\n") 
    405439        funcname = constructor.c_name 
    406440        try: 
     
    431465 
    432466                # write constructor from template ... 
     467                #sys.stderr.write("constructor: " + str(funcname) + ", tmpl:" + str(self.constructor_tmpl) + "\n") 
    433468                code = self.write_function_wrapper(constructor, 
    434469                    self.constructor_tmpl, 
     
    506541                    # write constructor from template ... 
    507542                    code, methflags = self.write_function_wrapper(meth, 
    508                         self.method_tmpl, handle_return=1, is_method=1, 
    509                         substdict=self.get_initial_method_substdict(meth)) 
     543                        self.method_tmpl, self.cb_function_tmpl, handle_return=1, 
     544                        is_method=1, substdict=self.get_initial_method_substdict(meth)) 
    510545                    self.fp.write(code) 
    511546                methods.append(self.methdef_tmpl % 
     
    857892                    # write constructor from template ... 
    858893                    code, methflags = self.write_function_wrapper(func, 
    859                         self.function_tmpl, handle_return=1, is_method=0) 
     894                        self.function_tmpl, self.cb_function_tmpl, handle_return=1, is_method=0) 
    860895                    self.fp.write(code) 
    861896                functions.append((func.name, '_wrap_' + funcname, 
  • trunk/src/host/pye17/codegen/definitions.py

    r4658 r4879  
    1515# New Parameter class, wich emulates a tuple for compatibility reasons 
    1616class Parameter(object): 
    17     def __init__(self, ptype, pname, pdflt, pnull, pdir=None): 
     17    def __init__(self, ptype, pname, pdflt, pnull, pdir=None, piscb=0): 
    1818        self.ptype = ptype 
    1919        self.pname = pname 
     
    2121        self.pnull = pnull 
    2222        self.pdir = pdir 
     23        self.piscb = piscb 
    2324 
    2425    def __len__(self): return 4 
     
    310311                    pnull = 0 
    311312                    pdir = None 
     313                    piscb = 0 
    312314                    for farg in parg[2:]: 
    313                         assert isinstance(farg, tuple) 
    314                         if farg[0] == 'default': 
    315                             pdflt = farg[1] 
    316                         elif farg[0] == 'null-ok': 
    317                             pnull = 1 
    318                         elif farg[0] == 'direction': 
    319                             pdir = farg[1] 
    320                     self.params.append(Parameter(ptype, pname, pdflt, pnull, pdir)) 
     315                        if farg == 'is_callback': 
     316                            piscb = 1 
     317                            continue 
     318                        assert isinstance(farg, tuple) 
     319                        if farg[0] == 'default': 
     320                            pdflt = farg[1] 
     321                        elif farg[0] == 'null-ok': 
     322                            pnull = 1 
     323                        elif farg[0] == 'direction': 
     324                            pdir = farg[1] 
     325                    self.params.append(Parameter(ptype, pname, pdflt, pnull, pdir, piscb)) 
    321326            elif arg[0] == 'varargs': 
    322327                self.varargs = arg[1] in ('t', '#t') 
  • trunk/src/host/pye17/codegen/h2def.py

    r4691 r4879  
    483483                                if callback > -1: 
    484484                                        func_name_end = argument.find(')') 
    485                                         argument = 'void* callback_' + argument[callback+1:func_name_end].lstrip("* ") 
    486                                 spaces = string.count(argument, ' ') 
    487                                 if spaces > 1: 
    488                                         argument = string.replace(argument, ' ', '-', spaces - 1) 
    489                                 #print "argument: " + argument + "; num spaces: " + str(spaces) 
     485                                        argument = 'void* cb_' + argument[callback+1:func_name_end].lstrip("* ") + ' *is_callback*' 
     486                                else: 
     487                                        spaces = string.count(argument, ' ') 
     488                                        if spaces > 1: 
     489                                                argument = string.replace(argument, ' ', '-', spaces - 1) 
     490                                        #print "argument: " + argument + "; num spaces: " + str(spaces) 
     491 
     492                                #sys.stderr.write('append argument: ' + argument + '\n') 
    490493                                arguments.append(argument) 
    491494 
     
    553556            if mname[:l] == self.prefix and mname[l+1] == '_': 
    554557                mname = mname[l+1:] 
     558 
     559        if mname.find("callback") > -1: 
     560          mname = mname.replace("callback", "cb") 
     561 
    555562        self.fp.write('(define-method ' + mname + '\n') 
    556563        self.fp.write('  (of-object "' + obj + '")\n') 
     
    577584            for arg in args: 
    578585                if arg != '...': 
    579                     tupleArg = tuple(string.split(arg)) 
    580                     if len(tupleArg) == 2: 
     586                    tupleArg = tuple(string.split(arg)) 
     587                    is_callback = 0 
     588                    if len(tupleArg) == 3 and tupleArg[2] == "*is_callback*": 
     589                      is_callback = 1 
     590                    if len(tupleArg) == 2 or is_callback: 
    581591                        var_type = tupleArg[0] 
    582592                        # some variable types are not supported 
     
    584594                                #var_type = "guint" # see gtypes.h 
    585595                        #print "var type: " + var_type 
    586                         self.fp.write('    \'("%s" "%s")\n' % (var_type, tupleArg[1])) 
     596                        self.fp.write('    \'("%s" "%s"' % (var_type, tupleArg[1])) 
     597                        if is_callback: 
     598                          self.fp.write(' "is_callback"') 
     599                        self.fp.write(')\n') 
    587600            self.fp.write('  )\n') 
    588601        if is_varargs: 
  • trunk/src/host/pye17/ecore_evas/ecore_evas.override

    r4720 r4879  
    8787    return (PyObject *)new_self; 
    8888} 
     89%% 
  • trunk/src/host/pye17/examples/ecore_evas/simple_window.py

    r4719 r4879  
    33import ecore 
    44import ecore_evas 
     5 
     6 
     7def on_resize(): 
     8  print "resizing window" 
    59 
    610try: 
     
    1014 
    1115window.title_set("TestWindow") 
     16window.cb_resize_set(on_resize) 
    1217window.show() 
    1318 
Note: See TracChangeset for help on using the changeset viewer.