summaryrefslogtreecommitdiff
path: root/tools/libglibcodegen.py
blob: 5cd0058532066ae1c0ab4a5e5cddb4902886d8ce (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
"""Library code for GLib/D-Bus-related code generation.

The master copy of this library is in the telepathy-glib repository -
please make any changes there.
"""

# Copyright (C) 2006-2008 Collabora Limited
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


from libtpcodegen import NS_TP, \
                         Signature, \
                         cmp_by_name, \
                         escape_as_identifier, \
                         get_by_path, \
                         get_descendant_text, \
                         get_docstring, \
                         xml_escape, \
                         get_deprecated

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 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", "BOXED", 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 == 'ao': #object path array
        return ("GPtrArray *",
                'dbus_g_type_get_collection ("GPtrArray",'
                ' DBUS_TYPE_G_OBJECT_PATH)',
                "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[:2] in ('a(', 'aa'): # array of structs or arrays, recurse
        gtype = type_to_gtype(s[1:])[1]
        return ("GPtrArray *", "(dbus_g_type_get_collection (\"GPtrArray\", "+gtype+"))", "BOXED", True)
    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 value_getter(gtype, marshaller):
    if marshaller == 'BOXED':
        return 'g_value_get_boxed'
    elif gtype == 'G_TYPE_STRING':
        return 'g_value_get_string'
    elif gtype == 'G_TYPE_UCHAR':
        return 'g_value_get_uchar'
    elif gtype == 'G_TYPE_BOOLEAN':
        return 'g_value_get_boolean'
    elif gtype == 'G_TYPE_UINT':
        return 'g_value_get_uint'
    elif gtype == 'G_TYPE_INT':
        return 'g_value_get_int'
    elif gtype == 'G_TYPE_UINT64':
        return 'g_value_get_uint64'
    elif gtype == 'G_TYPE_INT64':
        return 'g_value_get_int64'
    elif gtype == 'G_TYPE_DOUBLE':
        return 'g_value_get_double'
    else:
        raise AssertionError("Don't know how to get %s from a GValue" % marshaller)

class GDBusInterfaceInfo(object):
    def __init__(self, ugly_name, iface_element, c_name):
        self.ugly_name = ugly_name
        self.mixed_name = ugly_name.replace('_', '')
        self.lc_name = ugly_name.lower()
        self.uc_name = ugly_name.upper()
        self.c_name = c_name
        self.iface_element = iface_element

        self.method_elements = iface_element.getElementsByTagName('method')
        self.signal_elements = iface_element.getElementsByTagName('signal')
        self.property_elements = iface_element.getElementsByTagName('property')

    def do_methods(self):
        method_args = [
        ]
        method_in_arg_pointers = [
        ]
        method_out_arg_pointers = [
        ]
        methods = [
        ]
        method_pointers = [
            'static const GDBusMethodInfo *const method_pointers_%s[] = {'
                % self.c_name,
        ]

        for meth in self.method_elements:
            lc_name = meth.getAttribute('tp:name-for-bindings')
            if meth.getAttribute('name') != lc_name.replace('_', ''):
                raise AssertionError('Method %s tp:name-for-bindings (%s) '
                        'does not match' %
                        (meth.getAttribute('name'), lc_name))
            lc_name = lc_name.lower()

            c_name = 'method_%s_%s' % (self.c_name, lc_name)

            method_in_arg_pointers.append('static const GDBusArgInfo *const '
                    'method_in_arg_pointers_%s_%s[] = {' %
                    (self.c_name, lc_name))
            method_out_arg_pointers.append('static const GDBusArgInfo *const '
                    'method_out_arg_pointers_%s_%s[] = {'
                    % (self.c_name, lc_name))

            for i, arg in enumerate(meth.getElementsByTagName('arg')):
                name = arg.getAttribute('name')
                if not name:
                    name = 'arg%d' % i

                method_args.append('static const GDBusArgInfo '
                        'method_arg_%s_%s_%d = {' % (self.c_name, lc_name, i))
                method_args.append('  -1, /* refcount */')
                method_args.append('  "%s",' % name)
                method_args.append('  "%s",' % arg.getAttribute('type'))
                method_args.append('  NULL /* annotations */')
                method_args.append('};')

                if arg.getAttribute('direction') == 'out':
                    method_out_arg_pointers.append('  &method_arg_%s_%s_%d,' %
                            (self.c_name, lc_name, i))
                else:
                    method_in_arg_pointers.append('  &method_arg_%s_%s_%d,' %
                            (self.c_name, lc_name, i))

            method_in_arg_pointers.append('  NULL')
            method_in_arg_pointers.append('};')
            method_out_arg_pointers.append('  NULL')
            method_out_arg_pointers.append('};')

            methods.append('static const GDBusMethodInfo %s = {' % c_name)
            methods.append('  -1, /* refcount */')
            methods.append('  "%s",' % meth.getAttribute("name"))
            methods.append('  (GDBusArgInfo **) method_in_arg_pointers_%s_%s,'
                    % (self.c_name, lc_name))
            methods.append('  (GDBusArgInfo **) method_out_arg_pointers_%s_%s,'
                    % (self.c_name, lc_name))
            methods.append('  NULL /* annotations */')
            methods.append('};')

            method_pointers.append('  &%s,' % c_name)

        method_pointers.append('  NULL')
        method_pointers.append('};')

        return (method_args + method_in_arg_pointers +
                method_out_arg_pointers + methods + method_pointers)

    def do_signals(self):
        signal_args = [
        ]
        signal_arg_pointers = [
        ]
        signals = [
        ]
        signal_pointers = [
            'static const GDBusSignalInfo *const signal_pointers_%s[] = {'
                % self.c_name,
        ]

        for sig in self.signal_elements:
            lc_name = sig.getAttribute('tp:name-for-bindings')
            if sig.getAttribute('name') != lc_name.replace('_', ''):
                raise AssertionError('Signal %s tp:name-for-bindings (%s) '
                        'does not match' %
                        (sig.getAttribute('name'), lc_name))
            lc_name = lc_name.lower()

            c_name = 'signal_%s_%s' % (self.c_name, lc_name)

            signal_arg_pointers.append('static const GDBusArgInfo *const '
                    'signal_arg_pointers_%s_%s[] = {' % (self.c_name, lc_name))

            for i, arg in enumerate(sig.getElementsByTagName('arg')):
                name = arg.getAttribute('name')
                if not name:
                    name = 'arg%d' % i

                signal_args.append('static const GDBusArgInfo '
                        'signal_arg_%s_%s_%d = {' % (self.c_name, lc_name, i))
                signal_args.append('  -1, /* refcount */')
                signal_args.append('  "%s",' % name)
                signal_args.append('  "%s",' % arg.getAttribute('type'))
                signal_args.append('  NULL /* annotations */')
                signal_args.append('};')

                signal_arg_pointers.append('  &signal_arg_%s_%s_%d,' %
                        (self.c_name, lc_name, i))

            signal_arg_pointers.append('  NULL')
            signal_arg_pointers.append('};')

            signals.append('static const GDBusSignalInfo %s = {' % c_name)
            signals.append('  -1, /* refcount */')
            signals.append('  "%s",' % sig.getAttribute("name"))
            signals.append('  (GDBusArgInfo **) signal_arg_pointers_%s_%s,'
                    % (self.c_name, lc_name))
            signals.append('  NULL /* annotations */')
            signals.append('};')

            signal_pointers.append('  &%s,' % c_name)

        signal_pointers.append('  NULL')
        signal_pointers.append('};')

        return signal_args + signal_arg_pointers + signals + signal_pointers

    def do_properties(self):
        properties = [
        ]
        property_pointers = [
            'static const GDBusPropertyInfo *const property_pointers_%s[] = {'
                % self.c_name,
        ]

        for prop in self.property_elements:
            access = prop.getAttribute('access')
            flags = {
                'read': 'G_DBUS_PROPERTY_INFO_FLAGS_READABLE',
                'write': 'G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE',
                'readwrite':
                    'G_DBUS_PROPERTY_INFO_FLAGS_READABLE | '
                    'G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE',
            }[access]

            lc_name = prop.getAttribute('tp:name-for-bindings')
            if prop.getAttribute('name') != lc_name.replace('_', ''):
                raise AssertionError('Property %s tp:name-for-bindings (%s) '
                        'does not match' %
                        (prop.getAttribute('name'), lc_name))
            lc_name = lc_name.lower()

            c_name = 'property_%s_%s' % (self.c_name, lc_name)

            properties.append('static const GDBusPropertyInfo %s = {' % c_name)
            properties.append('  -1, /* refcount */')
            properties.append('  "%s",' % prop.getAttribute("name"))
            properties.append('  "%s",' % prop.getAttribute("type"))
            properties.append('  %s,' % flags)
            # FIXME: add annotations?
            properties.append('  NULL /* annotations */')
            properties.append('};')

            property_pointers.append('  &%s,' % c_name)

        property_pointers.append('  NULL')
        property_pointers.append('};')

        return properties + property_pointers

    def to_lines(self, linkage='static'):
        return (self.do_methods() +
                self.do_signals() +
                self.do_properties() + [
            '%s const GDBusInterfaceInfo %s = {' % (linkage, self.c_name),
            '  -1, /* refcount */',
            '  "%s",' % self.iface_element.getAttribute('name'),
            '  (GDBusMethodInfo **) method_pointers_%s,' % self.c_name,
            '  (GDBusSignalInfo **) signal_pointers_%s,' % self.c_name,
            '  (GDBusPropertyInfo **) property_pointers_%s,' % self.c_name,
            '  NULL /* annotations */',
            '};'
        ])