summaryrefslogtreecommitdiff
path: root/tools/genginterface.py
blob: c0ae7b9e6748bf4c8ef6f5ffa237768f0d9309c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
#!/usr/bin/python

import sys
import os.path
import xml.dom.minidom

def cmdline_error():
    print """\
usage:
    gen-ginterface [OPTIONS] xmlfile classname
options:
    --include='<header.h>' (may be repeated)
    --include='"header.h"' (ditto)
        Include extra headers in the generated .c file
    --signal-marshal-prefix='prefix'
        Use the given prefix on generated signal marshallers (default is
        derived from class name). If this is given, classname-signals-marshal.h
        is not automatically included.
    --filename='BASENAME'
        Set the basename for the output files (default is derived from class
        name)
    --not-implemented-func='symbol'
        Set action when methods not implemented in the interface vtable are
        called. symbol must have signature
            void symbol (DBusGMethodInvocation *context)
        and return some sort of "not implemented" error via
            dbus_g_method_return_error (context, ...)
"""
    sys.exit(1)

def dbus_gutils_wincaps_to_uscore(s):
    """Bug-for-bug compatible Python port of _dbus_gutils_wincaps_to_uscore
    which gets sequences of capital letters wrong in the same way.
    (e.g. in Telepathy, SendDTMF -> send_dt_mf)
    """
    ret = ''
    for c in s:
        if c >= 'A' and c <= 'Z':
            length = len(ret)
            if length > 0 and (length < 2 or ret[length-2] != '_'):
                ret += '_'
            ret += c.lower()
        else:
            ret += c
    return ret

def camelcase_to_lower(s):
    out ="";
    out += s[0].lower()
    last_upper=False
    if s[0].isupper():
        last_upper=True
    for i in range(1,len(s)):
        if s[i].isupper():
            if last_upper:
                if (i+1) < len(s) and  s[i+1].islower():
                    out += "_" + s[i].lower()
                else:
                    out += s[i].lower()
            else:
                out += "_" + s[i].lower()
            last_upper=True
        else:
            out += s[i]
            last_upper=False
    return out

def camelcase_to_upper(s):
    return camelcase_to_lower(s).upper()

class SignatureIter:
    """Iterator over a D-Bus signature. Copied from dbus-python 0.71 so we
    can run genginterface in a limited environment with only Python
    (like Scratchbox).
    """
    def __init__(self, string):
        self.remaining = string

    def next(self):
        if self.remaining == '':
            raise StopIteration

        signature = self.remaining
        block_depth = 0
        block_type = None
        end = len(signature)

        for marker in range(0, end):
            cur_sig = signature[marker]

            if cur_sig == 'a':
                pass
            elif cur_sig == '{' or cur_sig == '(':
                if block_type == None:
                    block_type = cur_sig

                if block_type == cur_sig:
                    block_depth = block_depth + 1

            elif cur_sig == '}':
                if block_type == '{':
                    block_depth = block_depth - 1

                if block_depth == 0:
                    end = marker
                    break

            elif cur_sig == ')':
                if block_type == '(':
                    block_depth = block_depth - 1

                if block_depth == 0:
                    end = marker
                    break

            else:
                if block_depth == 0:
                    end = marker
                    break

        end = end + 1
        self.remaining = signature[end:]
        return Signature(signature[0:end])


class Signature(str):
    def __iter__(self):
        return SignatureIter(self)


def type_to_gtype(s):
    if s == 'y': #byte
        return ("guchar ", "G_TYPE_UCHAR","UCHAR", False)
    elif s == 'b': #boolean
        return ("gboolean ", "G_TYPE_BOOLEAN","BOOLEAN", False)
    elif s == 'n': #int16
        return ("gint ", "G_TYPE_INT","INT", False)
    elif s == 'q': #uint16
        return ("guint ", "G_TYPE_UINT","UINT", False)
    elif s == 'i': #int32
        return ("gint ", "G_TYPE_INT","INT", False)
    elif s == 'u': #uint32
        return ("guint ", "G_TYPE_UINT","UINT", False)
    elif s == 'x': #int64
        return ("gint64 ", "G_TYPE_INT64","INT64", False)
    elif s == 't': #uint64
        return ("guint64 ", "G_TYPE_UINT64","UINT64", False)
    elif s == 'd': #double
        return ("gdouble ", "G_TYPE_DOUBLE","DOUBLE", False)
    elif s == 's': #string
        return ("gchar *", "G_TYPE_STRING", "STRING", True)
    elif s == 'g': #signature - FIXME
        return ("gchar *", "DBUS_TYPE_G_SIGNATURE", "STRING", True)
    elif s == 'o': #object path
        return ("gchar *", "DBUS_TYPE_G_OBJECT_PATH", "STRING", True)
    elif s == 'v':  #variant
        return ("GValue *", "G_TYPE_VALUE", "BOXED", True)
    elif s == 'as':  #array of strings
        return ("gchar **", "G_TYPE_STRV", "BOXED", True)
    elif s == 'ay': #byte array
        return ("GArray *",
            "dbus_g_type_get_collection (\"GArray\", G_TYPE_UCHAR)", "BOXED",
            True)
    elif s == 'au': #uint array
        return ("GArray *", "DBUS_TYPE_G_UINT_ARRAY", "BOXED", True)
    elif s == 'ai': #int array
        return ("GArray *", "DBUS_TYPE_G_INT_ARRAY", "BOXED", True)
    elif s == 'ax': #int64 array
        return ("GArray *", "DBUS_TYPE_G_INT64_ARRAY", "BOXED", True)
    elif s == 'at': #uint64 array
        return ("GArray *", "DBUS_TYPE_G_UINT64_ARRAY", "BOXED", True)
    elif s == 'ad': #double array
        return ("GArray *", "DBUS_TYPE_G_DOUBLE_ARRAY", "BOXED", True)
    elif s == 'ab': #boolean array
        return ("GArray *", "DBUS_TYPE_G_BOOLEAN_ARRAY", "BOXED", True)
    elif s[:2] == 'a(': #array of structs, recurse
        gtype = type_to_gtype(s[1:])[1]
        return ("GPtrArray *", "(dbus_g_type_get_collection (\"GPtrArray\", "+gtype+"))", "BOXED", True)
    elif s[:2] == 'aa': #array of arrays, recurse
        gtype = type_to_gtype(s[1:])[1]
        return ("GPtrArray *", "(dbus_g_type_get_collection (\"GPtrArray\", "+gtype+"))", "BOXED", True)
    elif s == 'a{ss}': #hash table of string to string
        return ("GHashTable *", "DBUS_TYPE_G_STRING_STRING_HASHTABLE", "BOXED", False)
    elif s[:2] == 'a{':  #some arbitrary hash tables
        if s[2] not in ('y', 'b', 'n', 'q', 'i', 'u', 's', 'o', 'g'):
            raise Exception, "can't index a hashtable off non-basic type " + s
        first = type_to_gtype(s[2])
        second = type_to_gtype(s[3:-1])
        return ("GHashTable *", "(dbus_g_type_get_map (\"GHashTable\", " + first[1] + ", " + second[1] + "))", "BOXED", False)
    elif s[:1] == '(': #struct
        gtype = "(dbus_g_type_get_struct (\"GValueArray\", "
        for subsig in Signature(s[1:-1]):
            gtype = gtype + type_to_gtype(subsig)[1] + ", "
        gtype = gtype + "G_TYPE_INVALID))"
        return ("GValueArray *", gtype, "BOXED", True)

    # we just don't know ..
    raise Exception, "don't know the GType for " + s


def signal_to_marshal_type(signal):
    """
    return a list of strings indicating the marshalling type for this signal.
    """

    mtype=[]
    for i in signal.getElementsByTagName("arg"):
        name =i.getAttribute("name")
        type = i.getAttribute("type")
        mtype.append(type_to_gtype(type)[2])

    return mtype

def signal_to_marshal_name(signal, prefix):
    glib_marshallers = ['VOID', 'BOOLEAN', 'CHAR', 'UCHAR', 'INT',
            'STRING', 'UINT', 'LONG', 'ULONG', 'ENUM', 'FLAGS', 'FLOAT',
            'DOUBLE', 'STRING', 'PARAM', 'BOXED', 'POINTER', 'OBJECT',
            'UINT_POINTER']

    mtype = signal_to_marshal_type(signal)
    if len(mtype):
        name = '_'.join(mtype)
    else:
        name = 'VOID'

    if name in glib_marshallers:
        return 'g_cclosure_marshal_VOID__' + name
    else:
        return prefix + '_marshal_VOID__' + name

def signal_to_gtype_list(signal):
    gtype=[]
    for i in signal.getElementsByTagName("arg"):
        name =i.getAttribute("name")
        type = i.getAttribute("type")
        gtype.append(type_to_gtype(type)[1])

    return gtype


def print_license(stream, filename, description, dom):
    stream.write(
"""/*
 * %s - %s
 *
""" % (filename, description))

    for c in dom.getElementsByTagName('tp:copyright'):
        # assume all child nodes are text
        stream.write(' * %s\n' % ''.join([n.data for n in c.childNodes
                                            if n.nodeType in (n.TEXT_NODE,
                                                n.CDATA_SECTION_NODE)]))

    stream.write("""\
 *
 * This file may be distributed under the same terms as the specification
 * from which it is generated.
 */

""")

def print_header_begin(stream, prefix):
    guardname = '__'+prefix.upper()+'_H__'
    stream.write ("#ifndef "+guardname+"\n")
    stream.write ("#define "+guardname+"\n\n")

    stream.write ("#include <glib-object.h>\n#include <dbus/dbus-glib.h>\n\n")
    stream.write ("G_BEGIN_DECLS\n\n")

def print_header_end(stream, prefix):
    guardname = '__'+prefix.upper()+'_H__'
    stream.write ("\nG_END_DECLS\n\n")
    stream.write ("#endif /* #ifndef "+guardname+"*/\n")

def print_class_declaration(stream, prefix, classname, methods):
    stream.write ("""\
/**
 * %(classname)s:
 *
 * Dummy typedef representing any implementation of this interface.
 */
typedef struct _%(classname)s %(classname)s;

/**
 * %(classname)sClass:
 *
 * The class of %(classname)s.
 */
typedef struct _%(classname)sClass %(classname)sClass;

""" % locals())

    stream.write(
"""
GType %(prefix)s_get_type (void);

""" % {'prefix':prefix,'uprefix':prefix.upper()})

    macro_prefix = prefix.upper().split('_',1)
    gtype = '_TYPE_'.join(macro_prefix)

    stream.write(
"""/* TYPE MACROS */
#define %(type)s \\
  (%(prefix)s_get_type ())
#define %(main)s_%(sub)s(obj) \\
  (G_TYPE_CHECK_INSTANCE_CAST((obj), %(type)s, %(name)s))
#define %(main)s_IS_%(sub)s(obj) \\
  (G_TYPE_CHECK_INSTANCE_TYPE((obj), %(type)s))
#define %(main)s_%(sub)s_GET_CLASS(obj) \\
  (G_TYPE_INSTANCE_GET_INTERFACE ((obj), %(type)s, %(name)sClass))

""" % {"main":macro_prefix[0], "sub":macro_prefix[1], "type":gtype, "name":classname, "prefix":prefix})
 

def signal_emit_stub(signal):
    # for signal: org.freedesktop.Telepathy.Thing::StuffHappened (s, u)
    # emit: void tp_svc_thing_emit_stuff_happened (gpointer instance,
    #           const char *arg, guint arg2)
    dbus_name = signal.getAttributeNode("name").nodeValue
    c_emitter_name = prefix + '_emit_' + camelcase_to_lower(dbus_name)
    c_signal_const_name = 'SIGNAL_' + dbus_name

    macro_prefix = prefix.upper().split('_',1)

    decl = 'void ' + c_emitter_name + ' (gpointer instance'
    args = ''
    argdoc = ''

    for i in signal.getElementsByTagName("arg"):
        name = i.getAttribute("name")
        type = i.getAttribute("type")
        info = type_to_gtype(type)
        gtype = info[0]
        if gtype[3]:
            gtype = 'const ' + gtype
        decl += ',\n    ' + gtype + ' ' + name
        args += ', ' + name
        argdoc += ' * @' + name + ': FIXME: document args in genginterface\n'
    decl += ')'

    doc = ("""\
/**
 * %s:
 * @instance: An object implementing this interface
%s *
 * Emit the %s D-Bus signal from @instance with the given arguments.
 */
""" % (c_emitter_name, argdoc, dbus_name))

    header = decl + ';\n\n'
    body = doc + decl + ('\n{\n'
                   '  g_assert (%s_IS_%s (instance));\n'
                   '  g_signal_emit (instance, signals[%s], 0%s);\n'
                   '}\n\n'
                   % (macro_prefix[0], macro_prefix[1], c_signal_const_name,
                      args))

    return header, body


def print_class_definition(stream, prefix, classname, methods):
    stream.write ("struct _%sClass {\n" % classname)
    stream.write ("    GObjectClass parent_class;\n")

    for method in methods:
        dbus_method_name = method.getAttributeNode("name").nodeValue
        lc_method_name = camelcase_to_lower(dbus_method_name)
        c_impl_name = prefix + '_' + lc_method_name + '_impl'
        stream.write('    %s %s;\n' % (c_impl_name, lc_method_name))

    stream.write ("};\n\n")


def cmp_by_name(node1, node2):
    return cmp(node1.getAttributeNode("name").nodeValue,
               node2.getAttributeNode("name").nodeValue)


def do_method(method):
    # DoStuff (s -> u)
    dbus_method_name = method.getAttributeNode("name").nodeValue
    lc_method_name = camelcase_to_lower(dbus_method_name)
    # void tp_svc_thing_do_stuff (TpSvcThing *, const char *,
    #                             DBusGMethodInvocation *);
    c_method_name = prefix + '_' + lc_method_name
    # typedef void (*tp_svc_thing_do_stuff_impl) (TpSvcThing *, const char *,
    #                                             DBusGMethodInvocation *);
    c_impl_name = prefix + '_' + lc_method_name + '_impl'
    # void tp_svc_thing_return_from_do_stuff (DBusGMethodInvocation *, guint);
    ret_method_name = prefix + '_return_from_' + lc_method_name

    ret_count=0

    header = ''
    body = ''

    c_decl = "static void\n"
    method_decl = "typedef void (*" + c_impl_name + ') ('
    ret_decl = 'void\n'
    ret_body = '{\n  dbus_g_method_return (dbus_context'
    arg_doc = ''
    ret_arg_doc = ''

    tmp = c_method_name+' ('
    pad = ' ' * len(tmp)
    c_decl += tmp+classname+' *self'

    method_pad = ' ' * len(method_decl)
    method_decl += classname + ' *self'
    args = 'self'

    tmp = ret_method_name+' ('
    ret_pad = ' ' * len(tmp)
    ret_decl += tmp+'DBusGMethodInvocation *dbus_context'

    for i in method.getElementsByTagName("arg"):
        name =i.getAttribute("name")
        direction = i.getAttribute("direction")
        type = i.getAttribute("type")

        if not name and direction == "out":
            if ret_count==0:
                name = "ret"
            else:
                name = "ret"+str(ret_count)
            ret_count += 1

        gtype = type_to_gtype(type)[0]
        if type_to_gtype(type)[3]:
            gtype="const "+gtype
        if direction != "out":
            c_decl +=",\n"+pad+gtype+name
            method_decl +=",\n"+method_pad+gtype+name
            args += ', '+name
            arg_doc += (' * @' + name
                    + ': FIXME: document args in genginterface\n')
        else:
            ret_decl += ",\n"+ret_pad+gtype+name
            ret_body += ', '+name
            ret_arg_doc += (' * @' + name
                    + ': FIXME: document args in genginterface\n')

    c_decl += ",\n"+pad+"DBusGMethodInvocation *context)"
    method_decl += ",\n"+method_pad+"DBusGMethodInvocation *context);\n"
    args += ', context'

    ret_doc = ("""\
/**
 * %s:
 * @dbus_context: The D-Bus method invocation context
%s *
 * Return successfully by calling dbus_g_method_return (@dbus_context,
 * ...). This inline function is just a type-safe wrapper for
 * dbus_g_method_return.
 */
""" % (ret_method_name, ret_arg_doc))

    interface = method.parentNode.getAttribute("name");
    ret_decl += ')\n'
    ret_body += ');\n}\n'
    header += (ret_doc + 'static inline\n/**/\n' + ret_decl + ';\n'
            + 'static inline ' + ret_decl + ret_body)
    body += (
"""
/**
 * %(c_impl_name)s
 * @self: The object implementing this interface
%(arg_doc)s * @context: The D-Bus invocation context to use to return values
 *           or throw an error.
 *
 * Signature of an implementation of D-Bus method %(dbus_method_name)s
 * on interface %(interface)s
 */
""" % locals())

    body += c_decl+"\n{\n"
    body += "  %s impl = (%s_GET_CLASS (self)->%s);\n" % (
            c_impl_name, prefix.upper(), lc_method_name)
    body += "  if (impl)\n"
    body += "    (impl) (%s);\n" % args
    body += "  else\n"
    if not_implemented_func:
        body += "    %s (context);\n" % not_implemented_func
    else:
        # this seems as appropriate an error as any
        body += """\
    {
      GError e = { DBUS_GERROR, DBUS_GERROR_UNKNOWN_METHOD,
          "Method not implemented" };

      dbus_g_method_return_error (context, &e);
    }
"""
    body += "}\n\n"

    dg_method_name = prefix + '_' + dbus_gutils_wincaps_to_uscore(dbus_method_name)
    if dg_method_name != c_method_name:
        body += ("""\
#define %(dg_method_name)s %(c_method_name)s

""" % {'dg_method_name': dg_method_name, 'c_method_name': c_method_name })

    method_decl += 'void %s_implement_%s (%sClass *klass, %s impl);\n\n' \
                   % (prefix, lc_method_name, classname, c_impl_name)

    body += ("""\
/**
 * %s_implement_%s:
 * @klass: A class whose instances implement this interface
 * @impl: A callback used to implement the %s method
 *
 * Register an implementation for the %s method in the vtable of an
 * implementation of this interface. To be called from the interface
 * init function.
 */
""" % (prefix, lc_method_name, dbus_method_name, dbus_method_name))
    body += 'void\n%s_implement_%s (%sClass *klass, %s impl)\n{\n'\
            % (prefix, lc_method_name, classname, c_impl_name)
    body += '  klass->%s = impl;\n' % lc_method_name
    body += '}\n\n'

    return (method_decl, header, body)

if __name__ == '__main__':
    from getopt import gnu_getopt

    options, argv = gnu_getopt(sys.argv[1:], '',
                               ['filename=', 'signal-marshal-prefix=',
                                'include=',
                                'not-implemented-func='])

    try:
        classname = argv[1]
    except IndexError:
        cmdline_error()

    prefix = camelcase_to_lower(classname)

    basename = prefix.replace('_', '-')
    signal_marshal_prefix = prefix
    headers = []
    not_implemented_func = ''

    for option, value in options:
        if option == '--filename':
            basename = value
        elif option == '--signal-marshal-prefix':
            signal_marshal_prefix = value
        elif option == '--include':
            if value[0] not in '<"':
                value = '"%s"' % value
            headers.append(value)
        elif option == '--not-implemented-func':
            not_implemented_func = value

    outname_header = basename + ".h"
    outname_body = basename + ".c"
    outname_signal_marshal = basename + "-signals-marshal.list"

    header=open(outname_header,'w')
    body=open(outname_body, 'w')

    signal_marshal=open(outname_signal_marshal, 'w')

    try:
        dom = xml.dom.minidom.parse(argv[0])
    except IndexError:
        cmdline_error()

    signals = dom.getElementsByTagName("signal")
    signals.sort(cmp_by_name)
    methods = dom.getElementsByTagName("method")
    methods.sort(cmp_by_name)

    print_license(header, outname_header, "Header for " + classname, dom)
    print_license(body, outname_body, "Source for " + classname, dom)
    print_header_begin(header,prefix)

    print_class_declaration(header, prefix, classname, methods)

    # include my own header first, to ensure self-contained
    body.write(
"""#include "%s"

""" % outname_header)

    # required headers
    body.write(
"""#include <stdio.h>
#include <stdlib.h>

""")

    for h in headers:
        body.write('#include %s\n' % h)
    body.write('\n')

    if signal_marshal_prefix == prefix:
        body.write('#include "%s-signals-marshal.h"\n' % basename)
        # else assume the signal marshallers are declared in one of the headers

    body.write('const DBusGObjectInfo dbus_glib_%s_object_info;\n'
            % prefix)

    print_class_definition(body, prefix, classname, methods)

    if signals:
        body.write('enum {\n')
        for signal in signals:
            dbus_name = signal.getAttributeNode("name").nodeValue
            body.write('    SIGNAL_%s,\n' % (dbus_name))
        body.write('    N_SIGNALS\n};\nstatic guint signals[N_SIGNALS] = {0};\n\n')

    gtypename = '_TYPE_'.join(prefix.upper().split('_',1))

    body.write(
"""
static void
%(prefix)s_base_init (gpointer klass)
{
  static gboolean initialized = FALSE;

  if (!initialized)
    {
      initialized = TRUE;
""" % {'classname':classname, 'gtypename':gtypename, 'prefix':prefix, 'uprefix':prefix.upper()})

    header.write("\n")

    marshallers = {}
    for signal in signals:
        dbus_name = signal.getAttributeNode("name").nodeValue
        gtypelist = signal_to_gtype_list(signal)
        marshal_name = signal_to_marshal_name(signal, signal_marshal_prefix)

        body.write(
"""
      signals[SIGNAL_%s] =
      g_signal_new ("%s",
                    G_OBJECT_CLASS_TYPE (klass),
                    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
                    0,
                    NULL, NULL,
                    %s,
                    G_TYPE_NONE, %s);
""" % (dbus_name,
       (dbus_gutils_wincaps_to_uscore(dbus_name)).replace('_','-'),
       marshal_name,
       ', '.join([str(len(gtypelist))] + gtypelist)))

        if not marshal_name.startswith('g_cclosure_marshal_VOID__'):
            mtype = signal_to_marshal_type(signal)
            assert(len(mtype))
            marshallers[','.join(mtype)] = True

    for marshaller in marshallers:
        signal_marshal.write("VOID:"+marshaller+"\n")

    body.write(
"""
      dbus_g_object_type_install_info (%(prefix)s_get_type (), &dbus_glib_%(prefix)s_object_info);
    }
}

GType
%(prefix)s_get_type (void)
{
  static GType type = 0;

  if (G_UNLIKELY (type == 0)) {
    static const GTypeInfo info = {
      sizeof (%(classname)sClass),
      %(prefix)s_base_init, /* base_init */
      NULL, /* base_finalize */
      NULL, /* class_init */
      NULL, /* class_finalize */
      NULL, /* class_data */
      0,
      0, /* n_preallocs */
      NULL /* instance_init */
    };

    type = g_type_register_static (G_TYPE_INTERFACE, "%(classname)s", &info, 0);
  }

  return type;
}

""" % {'classname':classname,'prefix':prefix, 'uprefix':prefix.upper()})

    for method in methods:
        m, h, b = do_method(method)
        header.write(m + '\n')
        header.write(h)
        body.write(b)

    for signal in signals:
        h, b = signal_emit_stub(signal)
        header.write(h)
        body.write(b)

    header.write('\n')

    body.write("""\
#include "%s-glue.h"

""" % (basename))

    print_header_end(header,prefix)
    header.close()
    body.close()