Imported Upstream version 2.50.2
[platform/upstream/glib.git] / gio / gdbus-2.0 / codegen / codegen.py
1 # -*- Mode: Python -*-
2
3 # GDBus - GLib D-Bus Library
4 #
5 # Copyright (C) 2008-2011 Red Hat, Inc.
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General
18 # Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #
20 # Author: David Zeuthen <davidz@redhat.com>
21
22 import sys
23
24 from . import config
25 from . import utils
26 from . import dbustypes
27
28 # ----------------------------------------------------------------------------------------------------
29
30 class CodeGenerator:
31     def __init__(self, ifaces, namespace, interface_prefix, generate_objmanager, generate_autocleanup, docbook_gen, h, c):
32         self.docbook_gen = docbook_gen
33         self.generate_objmanager = generate_objmanager
34         self.generate_autocleanup = generate_autocleanup
35         self.ifaces = ifaces
36         self.h = h
37         self.c = c
38         self.namespace = namespace
39         if len(namespace) > 0:
40             if utils.is_ugly_case(namespace):
41                 self.namespace = namespace.replace('_', '')
42                 self.ns_upper = namespace.upper() + '_'
43                 self.ns_lower = namespace.lower() + '_'
44             else:
45                 self.ns_upper = utils.camel_case_to_uscore(namespace).upper() + '_'
46                 self.ns_lower = utils.camel_case_to_uscore(namespace).lower() + '_'
47         else:
48             self.ns_upper = ''
49             self.ns_lower = ''
50         self.interface_prefix = interface_prefix
51         self.header_guard = self.h.name.upper().replace('.', '_').replace('-', '_').replace('/', '_')
52
53     # ----------------------------------------------------------------------------------------------------
54
55     def generate_intro(self):
56         self.c.write('/*\n'
57                      ' * Generated by gdbus-codegen %s. DO NOT EDIT.\n'
58                      ' *\n'
59                      ' * The license of this code is the same as for the source it was derived from.\n'
60                      ' */\n'
61                      '\n'
62                      %(config.VERSION))
63         self.c.write('#ifdef HAVE_CONFIG_H\n'
64                      '#  include "config.h"\n'
65                      '#endif\n'
66                      '\n'
67                      '#include "%s"\n'
68                      '\n'
69                      '#include <string.h>\n'
70                      %(self.h.name))
71
72         self.c.write('#ifdef G_OS_UNIX\n'
73                      '#  include <gio/gunixfdlist.h>\n'
74                      '#endif\n'
75                      '\n')
76
77         self.c.write('typedef struct\n'
78                      '{\n'
79                      '  GDBusArgInfo parent_struct;\n'
80                      '  gboolean use_gvariant;\n'
81                      '} _ExtendedGDBusArgInfo;\n'
82                      '\n')
83
84         self.c.write('typedef struct\n'
85                      '{\n'
86                      '  GDBusMethodInfo parent_struct;\n'
87                      '  const gchar *signal_name;\n'
88                      '  gboolean pass_fdlist;\n'
89                      '} _ExtendedGDBusMethodInfo;\n'
90                      '\n')
91
92         self.c.write('typedef struct\n'
93                      '{\n'
94                      '  GDBusSignalInfo parent_struct;\n'
95                      '  const gchar *signal_name;\n'
96                      '} _ExtendedGDBusSignalInfo;\n'
97                      '\n')
98
99         self.c.write('typedef struct\n'
100                      '{\n'
101                      '  GDBusPropertyInfo parent_struct;\n'
102                      '  const gchar *hyphen_name;\n'
103                      '  gboolean use_gvariant;\n'
104                      '} _ExtendedGDBusPropertyInfo;\n'
105                      '\n')
106
107         self.c.write('typedef struct\n'
108                      '{\n'
109                      '  GDBusInterfaceInfo parent_struct;\n'
110                      '  const gchar *hyphen_name;\n'
111                      '} _ExtendedGDBusInterfaceInfo;\n'
112                      '\n')
113
114         self.c.write('typedef struct\n'
115                      '{\n'
116                      '  const _ExtendedGDBusPropertyInfo *info;\n'
117                      '  guint prop_id;\n'
118                      '  GValue orig_value; /* the value before the change */\n'
119                      '} ChangedProperty;\n'
120                      '\n'
121                      'static void\n'
122                      '_changed_property_free (ChangedProperty *data)\n'
123                      '{\n'
124                      '  g_value_unset (&data->orig_value);\n'
125                      '  g_free (data);\n'
126                      '}\n'
127                      '\n')
128
129         self.c.write('static gboolean\n'
130                      '_g_strv_equal0 (gchar **a, gchar **b)\n'
131                      '{\n'
132                      '  gboolean ret = FALSE;\n'
133                      '  guint n;\n'
134                      '  if (a == NULL && b == NULL)\n'
135                      '    {\n'
136                      '      ret = TRUE;\n'
137                      '      goto out;\n'
138                      '    }\n'
139                      '  if (a == NULL || b == NULL)\n'
140                      '    goto out;\n'
141                      '  if (g_strv_length (a) != g_strv_length (b))\n'
142                      '    goto out;\n'
143                      '  for (n = 0; a[n] != NULL; n++)\n'
144                      '    if (g_strcmp0 (a[n], b[n]) != 0)\n'
145                      '      goto out;\n'
146                      '  ret = TRUE;\n'
147                      'out:\n'
148                      '  return ret;\n'
149                      '}\n'
150                      '\n')
151
152         self.c.write('static gboolean\n'
153                      '_g_variant_equal0 (GVariant *a, GVariant *b)\n'
154                      '{\n'
155                      '  gboolean ret = FALSE;\n'
156                      '  if (a == NULL && b == NULL)\n'
157                      '    {\n'
158                      '      ret = TRUE;\n'
159                      '      goto out;\n'
160                      '    }\n'
161                      '  if (a == NULL || b == NULL)\n'
162                      '    goto out;\n'
163                      '  ret = g_variant_equal (a, b);\n'
164                      'out:\n'
165                      '  return ret;\n'
166                      '}\n'
167                      '\n')
168
169         # simplified - only supports the types we use
170         self.c.write('G_GNUC_UNUSED static gboolean\n'
171                      '_g_value_equal (const GValue *a, const GValue *b)\n'
172                      '{\n'
173                      '  gboolean ret = FALSE;\n'
174                      '  g_assert (G_VALUE_TYPE (a) == G_VALUE_TYPE (b));\n'
175                      '  switch (G_VALUE_TYPE (a))\n'
176                      '    {\n'
177                      '      case G_TYPE_BOOLEAN:\n'
178                      '        ret = (g_value_get_boolean (a) == g_value_get_boolean (b));\n'
179                      '        break;\n'
180                      '      case G_TYPE_UCHAR:\n'
181                      '        ret = (g_value_get_uchar (a) == g_value_get_uchar (b));\n'
182                      '        break;\n'
183                      '      case G_TYPE_INT:\n'
184                      '        ret = (g_value_get_int (a) == g_value_get_int (b));\n'
185                      '        break;\n'
186                      '      case G_TYPE_UINT:\n'
187                      '        ret = (g_value_get_uint (a) == g_value_get_uint (b));\n'
188                      '        break;\n'
189                      '      case G_TYPE_INT64:\n'
190                      '        ret = (g_value_get_int64 (a) == g_value_get_int64 (b));\n'
191                      '        break;\n'
192                      '      case G_TYPE_UINT64:\n'
193                      '        ret = (g_value_get_uint64 (a) == g_value_get_uint64 (b));\n'
194                      '        break;\n'
195                      '      case G_TYPE_DOUBLE:\n'
196                      '        {\n'
197                      '          /* Avoid -Wfloat-equal warnings by doing a direct bit compare */\n'
198                      '          gdouble da = g_value_get_double (a);\n'
199                      '          gdouble db = g_value_get_double (b);\n'
200                      '          ret = memcmp (&da, &db, sizeof (gdouble)) == 0;\n'
201                      '        }\n'
202                      '        break;\n'
203                      '      case G_TYPE_STRING:\n'
204                      '        ret = (g_strcmp0 (g_value_get_string (a), g_value_get_string (b)) == 0);\n'
205                      '        break;\n'
206                      '      case G_TYPE_VARIANT:\n'
207                      '        ret = _g_variant_equal0 (g_value_get_variant (a), g_value_get_variant (b));\n'
208                      '        break;\n'
209                      '      default:\n'
210                      '        if (G_VALUE_TYPE (a) == G_TYPE_STRV)\n'
211                      '          ret = _g_strv_equal0 (g_value_get_boxed (a), g_value_get_boxed (b));\n'
212                      '        else\n'
213                      '          g_critical ("_g_value_equal() does not handle type %s", g_type_name (G_VALUE_TYPE (a)));\n'
214                      '        break;\n'
215                      '    }\n'
216                      '  return ret;\n'
217                      '}\n'
218                      '\n')
219
220         self.h.write('/*\n'
221                      ' * Generated by gdbus-codegen %s. DO NOT EDIT.\n'
222                      ' *\n'
223                      ' * The license of this code is the same as for the source it was derived from.\n'
224                      ' */\n'
225                      '\n'
226                      '#ifndef __%s__\n'
227                      '#define __%s__\n'
228                      '\n'%(config.VERSION, self.header_guard, self.header_guard))
229         self.h.write('#include <gio/gio.h>\n'
230                      '\n'
231                      'G_BEGIN_DECLS\n'
232                      '\n')
233
234     # ----------------------------------------------------------------------------------------------------
235
236     def declare_types(self):
237         for i in self.ifaces:
238             self.h.write('\n')
239             self.h.write('/* ------------------------------------------------------------------------ */\n')
240             self.h.write('/* Declarations for %s */\n'%i.name)
241             self.h.write('\n')
242
243             # First the GInterface
244             self.h.write('#define %sTYPE_%s (%s_get_type ())\n'%(i.ns_upper, i.name_upper, i.name_lower))
245             self.h.write('#define %s%s(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), %sTYPE_%s, %s))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper, i.camel_name))
246             self.h.write('#define %sIS_%s(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), %sTYPE_%s))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper))
247             self.h.write('#define %s%s_GET_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), %sTYPE_%s, %sIface))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper, i.camel_name))
248             self.h.write('\n')
249             self.h.write('struct _%s;\n'%(i.camel_name))
250             self.h.write('typedef struct _%s %s;\n'%(i.camel_name, i.camel_name))
251             self.h.write('typedef struct _%sIface %sIface;\n'%(i.camel_name, i.camel_name))
252             self.h.write('\n')
253             self.h.write('struct _%sIface\n'%(i.camel_name))
254             self.h.write('{\n')
255             self.h.write('  GTypeInterface parent_iface;\n')
256
257             function_pointers = {}
258
259             # vfuncs for methods
260             if len(i.methods) > 0:
261                 self.h.write('\n')
262                 for m in i.methods:
263                     unix_fd = False
264                     if utils.lookup_annotation(m.annotations, 'org.gtk.GDBus.C.UnixFD'):
265                         unix_fd = True
266                     key = (m.since, '_method_%s'%m.name_lower)
267                     value  = '  gboolean (*handle_%s) (\n'%(m.name_lower)
268                     value += '    %s *object,\n'%(i.camel_name)
269                     value += '    GDBusMethodInvocation *invocation'%()
270                     if unix_fd:
271                         value += ',\n    GUnixFDList *fd_list'
272                     for a in m.in_args:
273                         value += ',\n    %sarg_%s'%(a.ctype_in, a.name)
274                     value += ');\n\n'
275                     function_pointers[key] = value
276
277             # vfuncs for signals
278             if len(i.signals) > 0:
279                 self.h.write('\n')
280                 for s in i.signals:
281                     key = (s.since, '_signal_%s'%s.name_lower)
282                     value  = '  void (*%s) (\n'%(s.name_lower)
283                     value += '    %s *object'%(i.camel_name)
284                     for a in s.args:
285                         value += ',\n    %sarg_%s'%(a.ctype_in, a.name)
286                     value += ');\n\n'
287                     function_pointers[key] = value
288
289             # vfuncs for properties
290             if len(i.properties) > 0:
291                 self.h.write('\n')
292                 for p in i.properties:
293                     key = (p.since, '_prop_get_%s'%p.name_lower)
294                     value = '  %s (*get_%s) (%s *object);\n\n'%(p.arg.ctype_in, p.name_lower, i.camel_name)
295                     function_pointers[key] = value
296
297             # Sort according to @since tag, then name.. this ensures
298             # that the function pointers don't change order assuming
299             # judicious use of @since
300             #
301             # Also use a proper version comparison function so e.g.
302             # 10.0 comes after 2.0.
303             #
304             # See https://bugzilla.gnome.org/show_bug.cgi?id=647577#c5
305             # for discussion
306             for key in sorted(function_pointers.keys(), key=utils.version_cmp_key):
307                 self.h.write('%s'%function_pointers[key])
308
309             self.h.write('};\n')
310             self.h.write('\n')
311             if self.generate_autocleanup == 'all':
312                 self.h.write('#if GLIB_CHECK_VERSION(2, 44, 0)\n')
313                 self.h.write('G_DEFINE_AUTOPTR_CLEANUP_FUNC (%s, g_object_unref)\n' % (i.camel_name))
314                 self.h.write('#endif\n')
315                 self.h.write('\n')
316             self.h.write('GType %s_get_type (void) G_GNUC_CONST;\n'%(i.name_lower))
317             self.h.write('\n')
318             self.h.write('GDBusInterfaceInfo *%s_interface_info (void);\n'%(i.name_lower))
319             self.h.write('guint %s_override_properties (GObjectClass *klass, guint property_id_begin);\n'%(i.name_lower))
320             self.h.write('\n')
321
322             # Then method call completion functions
323             if len(i.methods) > 0:
324                 self.h.write('\n')
325                 self.h.write('/* D-Bus method call completion functions: */\n')
326                 for m in i.methods:
327                     unix_fd = False
328                     if utils.lookup_annotation(m.annotations, 'org.gtk.GDBus.C.UnixFD'):
329                         unix_fd = True
330                     if m.deprecated:
331                         self.h.write('G_GNUC_DEPRECATED ')
332                     self.h.write('void %s_complete_%s (\n'
333                                  '    %s *object,\n'
334                                  '    GDBusMethodInvocation *invocation'%(i.name_lower, m.name_lower, i.camel_name))
335                     if unix_fd:
336                         self.h.write(',\n    GUnixFDList *fd_list')
337                     for a in m.out_args:
338                         self.h.write(',\n    %s%s'%(a.ctype_in, a.name))
339                     self.h.write(');\n')
340                     self.h.write('\n')
341                 self.h.write('\n')
342
343             # Then signal emission functions
344             if len(i.signals) > 0:
345                 self.h.write('\n')
346                 self.h.write('/* D-Bus signal emissions functions: */\n')
347                 for s in i.signals:
348                     if s.deprecated:
349                         self.h.write('G_GNUC_DEPRECATED ')
350                     self.h.write('void %s_emit_%s (\n'
351                                  '    %s *object'%(i.name_lower, s.name_lower, i.camel_name))
352                     for a in s.args:
353                         self.h.write(',\n    %sarg_%s'%(a.ctype_in, a.name))
354                     self.h.write(');\n')
355                     self.h.write('\n')
356                 self.h.write('\n')
357
358             # Then method call declarations
359             if len(i.methods) > 0:
360                 self.h.write('\n')
361                 self.h.write('/* D-Bus method calls: */\n')
362                 for m in i.methods:
363                     unix_fd = False
364                     if utils.lookup_annotation(m.annotations, 'org.gtk.GDBus.C.UnixFD'):
365                         unix_fd = True
366                     # async begin
367                     if m.deprecated:
368                         self.h.write('G_GNUC_DEPRECATED ')
369                     self.h.write('void %s_call_%s (\n'
370                                  '    %s *proxy'%(i.name_lower, m.name_lower, i.camel_name))
371                     for a in m.in_args:
372                         self.h.write(',\n    %sarg_%s'%(a.ctype_in, a.name))
373                     if unix_fd:
374                         self.h.write(',\n    GUnixFDList *fd_list')
375                     self.h.write(',\n'
376                                  '    GCancellable *cancellable,\n'
377                                  '    GAsyncReadyCallback callback,\n'
378                                  '    gpointer user_data);\n')
379                     self.h.write('\n')
380                     # async finish
381                     if m.deprecated:
382                         self.h.write('G_GNUC_DEPRECATED ')
383                     self.h.write('gboolean %s_call_%s_finish (\n'
384                                  '    %s *proxy'%(i.name_lower, m.name_lower, i.camel_name))
385                     for a in m.out_args:
386                         self.h.write(',\n    %sout_%s'%(a.ctype_out, a.name))
387                     if unix_fd:
388                         self.h.write(',\n    GUnixFDList **out_fd_list')
389                     self.h.write(',\n'
390                                  '    GAsyncResult *res,\n'
391                                  '    GError **error);\n')
392                     self.h.write('\n')
393                     # sync
394                     if m.deprecated:
395                         self.h.write('G_GNUC_DEPRECATED ')
396                     self.h.write('gboolean %s_call_%s_sync (\n'
397                                  '    %s *proxy'%(i.name_lower, m.name_lower, i.camel_name))
398                     for a in m.in_args:
399                         self.h.write(',\n    %sarg_%s'%(a.ctype_in, a.name))
400                     if unix_fd:
401                         self.h.write(',\n    GUnixFDList  *fd_list')
402                     for a in m.out_args:
403                         self.h.write(',\n    %sout_%s'%(a.ctype_out, a.name))
404                     if unix_fd:
405                         self.h.write(',\n    GUnixFDList **out_fd_list')
406                     self.h.write(',\n'
407                                  '    GCancellable *cancellable,\n'
408                                  '    GError **error);\n')
409                     self.h.write('\n')
410                 self.h.write('\n')
411
412             # Then the property accessor declarations
413             if len(i.properties) > 0:
414                 self.h.write('\n')
415                 self.h.write('/* D-Bus property accessors: */\n')
416                 for p in i.properties:
417                     # getter
418                     if p.deprecated:
419                         self.h.write('G_GNUC_DEPRECATED ')
420                     self.h.write('%s%s_get_%s (%s *object);\n'%(p.arg.ctype_in, i.name_lower, p.name_lower, i.camel_name))
421                     if p.arg.free_func != None:
422                         if p.deprecated:
423                             self.h.write('G_GNUC_DEPRECATED ')
424                         self.h.write('%s%s_dup_%s (%s *object);\n'%(p.arg.ctype_in_dup, i.name_lower, p.name_lower, i.camel_name))
425                     # setter
426                     if p.deprecated:
427                         self.h.write('G_GNUC_DEPRECATED ')
428                     self.h.write('void %s_set_%s (%s *object, %svalue);\n'%(i.name_lower, p.name_lower, i.camel_name, p.arg.ctype_in, ))
429                     self.h.write('\n')
430
431             # Then the proxy
432             self.h.write('\n')
433             self.h.write('/* ---- */\n')
434             self.h.write('\n')
435             self.h.write('#define %sTYPE_%s_PROXY (%s_proxy_get_type ())\n'%(i.ns_upper, i.name_upper, i.name_lower))
436             self.h.write('#define %s%s_PROXY(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), %sTYPE_%s_PROXY, %sProxy))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper, i.camel_name))
437             self.h.write('#define %s%s_PROXY_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), %sTYPE_%s_PROXY, %sProxyClass))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper, i.camel_name))
438             self.h.write('#define %s%s_PROXY_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), %sTYPE_%s_PROXY, %sProxyClass))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper, i.camel_name))
439             self.h.write('#define %sIS_%s_PROXY(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), %sTYPE_%s_PROXY))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper))
440             self.h.write('#define %sIS_%s_PROXY_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), %sTYPE_%s_PROXY))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper))
441             self.h.write('\n')
442             self.h.write('typedef struct _%sProxy %sProxy;\n'%(i.camel_name, i.camel_name))
443             self.h.write('typedef struct _%sProxyClass %sProxyClass;\n'%(i.camel_name, i.camel_name))
444             self.h.write('typedef struct _%sProxyPrivate %sProxyPrivate;\n'%(i.camel_name, i.camel_name))
445             self.h.write('\n')
446             self.h.write('struct _%sProxy\n'%(i.camel_name))
447             self.h.write('{\n')
448             self.h.write('  /*< private >*/\n')
449             self.h.write('  GDBusProxy parent_instance;\n')
450             self.h.write('  %sProxyPrivate *priv;\n'%(i.camel_name))
451             self.h.write('};\n')
452             self.h.write('\n')
453             self.h.write('struct _%sProxyClass\n'%(i.camel_name))
454             self.h.write('{\n')
455             self.h.write('  GDBusProxyClass parent_class;\n')
456             self.h.write('};\n')
457             self.h.write('\n')
458             self.h.write('GType %s_proxy_get_type (void) G_GNUC_CONST;\n'%(i.name_lower))
459             self.h.write('\n')
460             if self.generate_autocleanup in ('objects', 'all'):
461                 self.h.write('#if GLIB_CHECK_VERSION(2, 44, 0)\n')
462                 self.h.write('G_DEFINE_AUTOPTR_CLEANUP_FUNC (%sProxy, g_object_unref)\n' % (i.camel_name))
463                 self.h.write('#endif\n')
464                 self.h.write('\n')
465             if i.deprecated:
466                 self.h.write('G_GNUC_DEPRECATED ')
467             self.h.write('void %s_proxy_new (\n'
468                          '    GDBusConnection     *connection,\n'
469                          '    GDBusProxyFlags      flags,\n'
470                          '    const gchar         *name,\n'
471                          '    const gchar         *object_path,\n'
472                          '    GCancellable        *cancellable,\n'
473                          '    GAsyncReadyCallback  callback,\n'
474                          '    gpointer             user_data);\n'
475                          %(i.name_lower))
476             if i.deprecated:
477                 self.h.write('G_GNUC_DEPRECATED ')
478             self.h.write('%s *%s_proxy_new_finish (\n'
479                          '    GAsyncResult        *res,\n'
480                          '    GError             **error);\n'
481                          %(i.camel_name, i.name_lower))
482             if i.deprecated:
483                 self.h.write('G_GNUC_DEPRECATED ')
484             self.h.write('%s *%s_proxy_new_sync (\n'
485                          '    GDBusConnection     *connection,\n'
486                          '    GDBusProxyFlags      flags,\n'
487                          '    const gchar         *name,\n'
488                          '    const gchar         *object_path,\n'
489                          '    GCancellable        *cancellable,\n'
490                          '    GError             **error);\n'
491                          %(i.camel_name, i.name_lower))
492             self.h.write('\n')
493             if i.deprecated:
494                 self.h.write('G_GNUC_DEPRECATED ')
495             self.h.write('void %s_proxy_new_for_bus (\n'
496                          '    GBusType             bus_type,\n'
497                          '    GDBusProxyFlags      flags,\n'
498                          '    const gchar         *name,\n'
499                          '    const gchar         *object_path,\n'
500                          '    GCancellable        *cancellable,\n'
501                          '    GAsyncReadyCallback  callback,\n'
502                          '    gpointer             user_data);\n'
503                          %(i.name_lower))
504             if i.deprecated:
505                 self.h.write('G_GNUC_DEPRECATED ')
506             self.h.write('%s *%s_proxy_new_for_bus_finish (\n'
507                          '    GAsyncResult        *res,\n'
508                          '    GError             **error);\n'
509                          %(i.camel_name, i.name_lower))
510             if i.deprecated:
511                 self.h.write('G_GNUC_DEPRECATED ')
512             self.h.write('%s *%s_proxy_new_for_bus_sync (\n'
513                          '    GBusType             bus_type,\n'
514                          '    GDBusProxyFlags      flags,\n'
515                          '    const gchar         *name,\n'
516                          '    const gchar         *object_path,\n'
517                          '    GCancellable        *cancellable,\n'
518                          '    GError             **error);\n'
519                          %(i.camel_name, i.name_lower))
520             self.h.write('\n')
521
522             # Then the skeleton
523             self.h.write('\n')
524             self.h.write('/* ---- */\n')
525             self.h.write('\n')
526             self.h.write('#define %sTYPE_%s_SKELETON (%s_skeleton_get_type ())\n'%(i.ns_upper, i.name_upper, i.name_lower))
527             self.h.write('#define %s%s_SKELETON(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), %sTYPE_%s_SKELETON, %sSkeleton))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper, i.camel_name))
528             self.h.write('#define %s%s_SKELETON_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), %sTYPE_%s_SKELETON, %sSkeletonClass))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper, i.camel_name))
529             self.h.write('#define %s%s_SKELETON_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), %sTYPE_%s_SKELETON, %sSkeletonClass))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper, i.camel_name))
530             self.h.write('#define %sIS_%s_SKELETON(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), %sTYPE_%s_SKELETON))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper))
531             self.h.write('#define %sIS_%s_SKELETON_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), %sTYPE_%s_SKELETON))\n'%(i.ns_upper, i.name_upper, i.ns_upper, i.name_upper))
532             self.h.write('\n')
533             self.h.write('typedef struct _%sSkeleton %sSkeleton;\n'%(i.camel_name, i.camel_name))
534             self.h.write('typedef struct _%sSkeletonClass %sSkeletonClass;\n'%(i.camel_name, i.camel_name))
535             self.h.write('typedef struct _%sSkeletonPrivate %sSkeletonPrivate;\n'%(i.camel_name, i.camel_name))
536             self.h.write('\n')
537             self.h.write('struct _%sSkeleton\n'%(i.camel_name))
538             self.h.write('{\n')
539             self.h.write('  /*< private >*/\n')
540             self.h.write('  GDBusInterfaceSkeleton parent_instance;\n')
541             self.h.write('  %sSkeletonPrivate *priv;\n'%(i.camel_name))
542             self.h.write('};\n')
543             self.h.write('\n')
544             self.h.write('struct _%sSkeletonClass\n'%(i.camel_name))
545             self.h.write('{\n')
546             self.h.write('  GDBusInterfaceSkeletonClass parent_class;\n')
547             self.h.write('};\n')
548             self.h.write('\n')
549             self.h.write('GType %s_skeleton_get_type (void) G_GNUC_CONST;\n'%(i.name_lower))
550             self.h.write('\n')
551             if self.generate_autocleanup in ('objects', 'all'):
552                 self.h.write('#if GLIB_CHECK_VERSION(2, 44, 0)\n')
553                 self.h.write('G_DEFINE_AUTOPTR_CLEANUP_FUNC (%sSkeleton, g_object_unref)\n' % (i.camel_name))
554                 self.h.write('#endif\n')
555                 self.h.write('\n')
556             if i.deprecated:
557                 self.h.write('G_GNUC_DEPRECATED ')
558             self.h.write('%s *%s_skeleton_new (void);\n'%(i.camel_name, i.name_lower))
559
560             self.h.write('\n')
561
562         # Finally, the Object, ObjectProxy, ObjectSkeleton and ObjectManagerClient
563         if self.generate_objmanager:
564             self.h.write('\n')
565             self.h.write('/* ---- */\n')
566             self.h.write('\n')
567             self.h.write('#define %sTYPE_OBJECT (%sobject_get_type ())\n'%(self.ns_upper, self.ns_lower))
568             self.h.write('#define %sOBJECT(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), %sTYPE_OBJECT, %sObject))\n'%(self.ns_upper, self.ns_upper, self.namespace))
569             self.h.write('#define %sIS_OBJECT(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), %sTYPE_OBJECT))\n'%(self.ns_upper, self.ns_upper))
570             self.h.write('#define %sOBJECT_GET_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), %sTYPE_OBJECT, %sObject))\n'%(self.ns_upper, self.ns_upper, self.namespace))
571             self.h.write('\n')
572             self.h.write('struct _%sObject;\n'%(self.namespace))
573             self.h.write('typedef struct _%sObject %sObject;\n'%(self.namespace, self.namespace))
574             self.h.write('typedef struct _%sObjectIface %sObjectIface;\n'%(self.namespace, self.namespace))
575             self.h.write('\n')
576             self.h.write('struct _%sObjectIface\n'%(self.namespace))
577             self.h.write('{\n'
578                          '  GTypeInterface parent_iface;\n'
579                          '};\n'
580                          '\n')
581             self.h.write('GType %sobject_get_type (void) G_GNUC_CONST;\n'
582                          '\n'
583                          %(self.ns_lower))
584             for i in self.ifaces:
585                 if i.deprecated:
586                     self.h.write('G_GNUC_DEPRECATED ')
587                 self.h.write ('%s *%sobject_get_%s (%sObject *object);\n'
588                               %(i.camel_name, self.ns_lower, i.name_upper.lower(), self.namespace))
589             for i in self.ifaces:
590                 if i.deprecated:
591                     self.h.write('G_GNUC_DEPRECATED ')
592                 self.h.write ('%s *%sobject_peek_%s (%sObject *object);\n'
593                               %(i.camel_name, self.ns_lower, i.name_upper.lower(), self.namespace))
594             self.h.write('\n')
595             self.h.write('#define %sTYPE_OBJECT_PROXY (%sobject_proxy_get_type ())\n'%(self.ns_upper, self.ns_lower))
596             self.h.write('#define %sOBJECT_PROXY(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), %sTYPE_OBJECT_PROXY, %sObjectProxy))\n'%(self.ns_upper, self.ns_upper, self.namespace))
597             self.h.write('#define %sOBJECT_PROXY_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), %sTYPE_OBJECT_PROXY, %sObjectProxyClass))\n'%(self.ns_upper, self.ns_upper, self.namespace))
598             self.h.write('#define %sOBJECT_PROXY_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), %sTYPE_OBJECT_PROXY, %sObjectProxyClass))\n'%(self.ns_upper, self.ns_upper, self.namespace))
599             self.h.write('#define %sIS_OBJECT_PROXY(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), %sTYPE_OBJECT_PROXY))\n'%(self.ns_upper, self.ns_upper))
600             self.h.write('#define %sIS_OBJECT_PROXY_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), %sTYPE_OBJECT_PROXY))\n'%(self.ns_upper, self.ns_upper))
601             self.h.write('\n')
602             self.h.write('typedef struct _%sObjectProxy %sObjectProxy;\n'%(self.namespace, self.namespace))
603             self.h.write('typedef struct _%sObjectProxyClass %sObjectProxyClass;\n'%(self.namespace, self.namespace))
604             self.h.write('typedef struct _%sObjectProxyPrivate %sObjectProxyPrivate;\n'%(self.namespace, self.namespace))
605             self.h.write('\n')
606             self.h.write('struct _%sObjectProxy\n'%(self.namespace))
607             self.h.write('{\n')
608             self.h.write('  /*< private >*/\n')
609             self.h.write('  GDBusObjectProxy parent_instance;\n')
610             self.h.write('  %sObjectProxyPrivate *priv;\n'%(self.namespace))
611             self.h.write('};\n')
612             self.h.write('\n')
613             self.h.write('struct _%sObjectProxyClass\n'%(self.namespace))
614             self.h.write('{\n')
615             self.h.write('  GDBusObjectProxyClass parent_class;\n')
616             self.h.write('};\n')
617             self.h.write('\n')
618             self.h.write('GType %sobject_proxy_get_type (void) G_GNUC_CONST;\n'%(self.ns_lower))
619             self.h.write('\n')
620             if self.generate_autocleanup in ('objects', 'all'):
621                 self.h.write('#if GLIB_CHECK_VERSION(2, 44, 0)\n')
622                 self.h.write('G_DEFINE_AUTOPTR_CLEANUP_FUNC (%sObjectProxy, g_object_unref)\n' % (self.namespace))
623                 self.h.write('#endif\n')
624                 self.h.write('\n')
625             self.h.write('%sObjectProxy *%sobject_proxy_new (GDBusConnection *connection, const gchar *object_path);\n'%(self.namespace, self.ns_lower))
626             self.h.write('\n')
627             self.h.write('#define %sTYPE_OBJECT_SKELETON (%sobject_skeleton_get_type ())\n'%(self.ns_upper, self.ns_lower))
628             self.h.write('#define %sOBJECT_SKELETON(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), %sTYPE_OBJECT_SKELETON, %sObjectSkeleton))\n'%(self.ns_upper, self.ns_upper, self.namespace))
629             self.h.write('#define %sOBJECT_SKELETON_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), %sTYPE_OBJECT_SKELETON, %sObjectSkeletonClass))\n'%(self.ns_upper, self.ns_upper, self.namespace))
630             self.h.write('#define %sOBJECT_SKELETON_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), %sTYPE_OBJECT_SKELETON, %sObjectSkeletonClass))\n'%(self.ns_upper, self.ns_upper, self.namespace))
631             self.h.write('#define %sIS_OBJECT_SKELETON(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), %sTYPE_OBJECT_SKELETON))\n'%(self.ns_upper, self.ns_upper))
632             self.h.write('#define %sIS_OBJECT_SKELETON_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), %sTYPE_OBJECT_SKELETON))\n'%(self.ns_upper, self.ns_upper))
633             self.h.write('\n')
634             self.h.write('typedef struct _%sObjectSkeleton %sObjectSkeleton;\n'%(self.namespace, self.namespace))
635             self.h.write('typedef struct _%sObjectSkeletonClass %sObjectSkeletonClass;\n'%(self.namespace, self.namespace))
636             self.h.write('typedef struct _%sObjectSkeletonPrivate %sObjectSkeletonPrivate;\n'%(self.namespace, self.namespace))
637             self.h.write('\n')
638             self.h.write('struct _%sObjectSkeleton\n'%(self.namespace))
639             self.h.write('{\n')
640             self.h.write('  /*< private >*/\n')
641             self.h.write('  GDBusObjectSkeleton parent_instance;\n')
642             self.h.write('  %sObjectSkeletonPrivate *priv;\n'%(self.namespace))
643             self.h.write('};\n')
644             self.h.write('\n')
645             self.h.write('struct _%sObjectSkeletonClass\n'%(self.namespace))
646             self.h.write('{\n')
647             self.h.write('  GDBusObjectSkeletonClass parent_class;\n')
648             self.h.write('};\n')
649             self.h.write('\n')
650             self.h.write('GType %sobject_skeleton_get_type (void) G_GNUC_CONST;\n'%(self.ns_lower))
651             self.h.write('\n')
652             if self.generate_autocleanup in ('objects', 'all'):
653                 self.h.write('#if GLIB_CHECK_VERSION(2, 44, 0)\n')
654                 self.h.write('G_DEFINE_AUTOPTR_CLEANUP_FUNC (%sObjectSkeleton, g_object_unref)\n' % (self.namespace))
655                 self.h.write('#endif\n')
656                 self.h.write('\n')
657             self.h.write('%sObjectSkeleton *%sobject_skeleton_new (const gchar *object_path);\n'
658                          %(self.namespace, self.ns_lower))
659             for i in self.ifaces:
660                 if i.deprecated:
661                     self.h.write('G_GNUC_DEPRECATED ')
662                 self.h.write ('void %sobject_skeleton_set_%s (%sObjectSkeleton *object, %s *interface_);\n'
663                               %(self.ns_lower, i.name_upper.lower(), self.namespace, i.camel_name))
664             self.h.write('\n')
665
666             self.h.write('/* ---- */\n')
667             self.h.write('\n')
668             self.h.write('#define %sTYPE_OBJECT_MANAGER_CLIENT (%sobject_manager_client_get_type ())\n'%(self.ns_upper, self.ns_lower))
669             self.h.write('#define %sOBJECT_MANAGER_CLIENT(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), %sTYPE_OBJECT_MANAGER_CLIENT, %sObjectManagerClient))\n'%(self.ns_upper, self.ns_upper, self.namespace))
670             self.h.write('#define %sOBJECT_MANAGER_CLIENT_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), %sTYPE_OBJECT_MANAGER_CLIENT, %sObjectManagerClientClass))\n'%(self.ns_upper, self.ns_upper, self.namespace))
671             self.h.write('#define %sOBJECT_MANAGER_CLIENT_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), %sTYPE_OBJECT_MANAGER_CLIENT, %sObjectManagerClientClass))\n'%(self.ns_upper, self.ns_upper, self.namespace))
672             self.h.write('#define %sIS_OBJECT_MANAGER_CLIENT(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), %sTYPE_OBJECT_MANAGER_CLIENT))\n'%(self.ns_upper, self.ns_upper))
673             self.h.write('#define %sIS_OBJECT_MANAGER_CLIENT_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), %sTYPE_OBJECT_MANAGER_CLIENT))\n'%(self.ns_upper, self.ns_upper))
674             self.h.write('\n')
675             self.h.write('typedef struct _%sObjectManagerClient %sObjectManagerClient;\n'%(self.namespace, self.namespace))
676             self.h.write('typedef struct _%sObjectManagerClientClass %sObjectManagerClientClass;\n'%(self.namespace, self.namespace))
677             self.h.write('typedef struct _%sObjectManagerClientPrivate %sObjectManagerClientPrivate;\n'%(self.namespace, self.namespace))
678             self.h.write('\n')
679             self.h.write('struct _%sObjectManagerClient\n'%(self.namespace))
680             self.h.write('{\n')
681             self.h.write('  /*< private >*/\n')
682             self.h.write('  GDBusObjectManagerClient parent_instance;\n')
683             self.h.write('  %sObjectManagerClientPrivate *priv;\n'%(self.namespace))
684             self.h.write('};\n')
685             self.h.write('\n')
686             self.h.write('struct _%sObjectManagerClientClass\n'%(self.namespace))
687             self.h.write('{\n')
688             self.h.write('  GDBusObjectManagerClientClass parent_class;\n')
689             self.h.write('};\n')
690             self.h.write('\n')
691             if self.generate_autocleanup in ('objects', 'all'):
692                 self.h.write('#if GLIB_CHECK_VERSION(2, 44, 0)\n')
693                 self.h.write('G_DEFINE_AUTOPTR_CLEANUP_FUNC (%sObjectManagerClient, g_object_unref)\n' % (self.namespace))
694                 self.h.write('#endif\n')
695                 self.h.write('\n')
696             self.h.write('GType %sobject_manager_client_get_type (void) G_GNUC_CONST;\n'%(self.ns_lower))
697             self.h.write('\n')
698             self.h.write('GType %sobject_manager_client_get_proxy_type (GDBusObjectManagerClient *manager, const gchar *object_path, const gchar *interface_name, gpointer user_data);\n'%(self.ns_lower))
699             self.h.write('\n')
700             self.h.write('void %sobject_manager_client_new (\n'
701                          '    GDBusConnection        *connection,\n'
702                          '    GDBusObjectManagerClientFlags  flags,\n'
703                          '    const gchar            *name,\n'
704                          '    const gchar            *object_path,\n'
705                          '    GCancellable           *cancellable,\n'
706                          '    GAsyncReadyCallback     callback,\n'
707                          '    gpointer                user_data);\n'
708                          %(self.ns_lower))
709             self.h.write('GDBusObjectManager *%sobject_manager_client_new_finish (\n'
710                          '    GAsyncResult        *res,\n'
711                          '    GError             **error);\n'
712                          %(self.ns_lower))
713             self.h.write('GDBusObjectManager *%sobject_manager_client_new_sync (\n'
714                          '    GDBusConnection        *connection,\n'
715                          '    GDBusObjectManagerClientFlags  flags,\n'
716                          '    const gchar            *name,\n'
717                          '    const gchar            *object_path,\n'
718                          '    GCancellable           *cancellable,\n'
719                          '    GError                **error);\n'
720                          %(self.ns_lower))
721             self.h.write('\n')
722             self.h.write('void %sobject_manager_client_new_for_bus (\n'
723                          '    GBusType                bus_type,\n'
724                          '    GDBusObjectManagerClientFlags  flags,\n'
725                          '    const gchar            *name,\n'
726                          '    const gchar            *object_path,\n'
727                          '    GCancellable           *cancellable,\n'
728                          '    GAsyncReadyCallback     callback,\n'
729                          '    gpointer                user_data);\n'
730                          %(self.ns_lower))
731             self.h.write('GDBusObjectManager *%sobject_manager_client_new_for_bus_finish (\n'
732                          '    GAsyncResult        *res,\n'
733                          '    GError             **error);\n'
734                          %(self.ns_lower))
735             self.h.write('GDBusObjectManager *%sobject_manager_client_new_for_bus_sync (\n'
736                          '    GBusType                bus_type,\n'
737                          '    GDBusObjectManagerClientFlags  flags,\n'
738                          '    const gchar            *name,\n'
739                          '    const gchar            *object_path,\n'
740                          '    GCancellable           *cancellable,\n'
741                          '    GError                **error);\n'
742                          %(self.ns_lower))
743             self.h.write('\n')
744
745     # ----------------------------------------------------------------------------------------------------
746
747     def generate_outro(self):
748         self.h.write('\n'
749                      'G_END_DECLS\n'
750                      '\n'
751                      '#endif /* __%s__ */\n'%(self.header_guard))
752
753     # ----------------------------------------------------------------------------------------------------
754
755     def generate_annotations(self, prefix, annotations):
756         if annotations == None:
757             return
758
759         n = 0
760         for a in annotations:
761             #self.generate_annotations('%s_%d'%(prefix, n), a.get_annotations())
762
763             # skip internal annotations
764             if a.key.startswith('org.gtk.GDBus'):
765                 continue
766
767             self.c.write('static const GDBusAnnotationInfo %s_%d =\n'
768                          '{\n'
769                          '  -1,\n'
770                          '  (gchar *) "%s",\n'
771                          '  (gchar *) "%s",\n'%(prefix, n, a.key, a.value))
772             if len(a.annotations) == 0:
773                 self.c.write('  NULL\n')
774             else:
775                 self.c.write('  (GDBusAnnotationInfo **) &%s_%d_pointers\n'%(prefix, n))
776             self.c.write('};\n'
777                          '\n')
778             n += 1
779
780         if n > 0:
781             self.c.write('static const GDBusAnnotationInfo * const %s_pointers[] =\n'
782                              '{\n'%(prefix))
783             m = 0;
784             for a in annotations:
785                 if a.key.startswith('org.gtk.GDBus'):
786                     continue
787                 self.c.write('  &%s_%d,\n'%(prefix, m))
788                 m += 1
789             self.c.write('  NULL\n'
790                          '};\n'
791                          '\n')
792         return n
793
794     def generate_args(self, prefix, args):
795         for a in args:
796             num_anno = self.generate_annotations('%s_arg_%s_annotation_info'%(prefix, a.name), a.annotations)
797
798             self.c.write('static const _ExtendedGDBusArgInfo %s_%s =\n'
799                          '{\n'
800                          '  {\n'
801                          '    -1,\n'
802                          '    (gchar *) "%s",\n'
803                          '    (gchar *) "%s",\n'%(prefix, a.name, a.name, a.signature))
804             if num_anno == 0:
805                 self.c.write('    NULL\n')
806             else:
807                 self.c.write('    (GDBusAnnotationInfo **) &%s_arg_%s_annotation_info_pointers\n'%(prefix, a.name))
808             self.c.write('  },\n')
809             if not utils.lookup_annotation(a.annotations, 'org.gtk.GDBus.C.ForceGVariant'):
810                 self.c.write('  FALSE\n')
811             else:
812                 self.c.write('  TRUE\n')
813             self.c.write('};\n'
814                          '\n')
815
816         if len(args) > 0:
817             self.c.write('static const _ExtendedGDBusArgInfo * const %s_pointers[] =\n'
818                              '{\n'%(prefix))
819             for a in args:
820                 self.c.write('  &%s_%s,\n'%(prefix, a.name))
821             self.c.write('  NULL\n'
822                          '};\n'
823                          '\n')
824
825     def generate_introspection_for_interface(self, i):
826             self.c.write('/* ---- Introspection data for %s ---- */\n'
827                          '\n'%(i.name))
828
829             if len(i.methods) > 0:
830                 for m in i.methods:
831                     unix_fd = False
832                     if utils.lookup_annotation(m.annotations, 'org.gtk.GDBus.C.UnixFD'):
833                         unix_fd = True
834                     self.generate_args('_%s_method_info_%s_IN_ARG'%(i.name_lower, m.name_lower), m.in_args)
835                     self.generate_args('_%s_method_info_%s_OUT_ARG'%(i.name_lower, m.name_lower), m.out_args)
836
837                     num_anno = self.generate_annotations('_%s_method_%s_annotation_info'%(i.name_lower, m.name_lower), m.annotations)
838
839                     self.c.write('static const _ExtendedGDBusMethodInfo _%s_method_info_%s =\n'
840                                  '{\n'
841                                  '  {\n'
842                                  '    -1,\n'
843                                  '    (gchar *) "%s",\n'%(i.name_lower, m.name_lower, m.name))
844                     if len(m.in_args) == 0:
845                         self.c.write('    NULL,\n')
846                     else:
847                         self.c.write('    (GDBusArgInfo **) &_%s_method_info_%s_IN_ARG_pointers,\n'%(i.name_lower, m.name_lower))
848                     if len(m.out_args) == 0:
849                         self.c.write('    NULL,\n')
850                     else:
851                         self.c.write('    (GDBusArgInfo **) &_%s_method_info_%s_OUT_ARG_pointers,\n'%(i.name_lower, m.name_lower))
852                     if num_anno == 0:
853                         self.c.write('    NULL\n')
854                     else:
855                         self.c.write('    (GDBusAnnotationInfo **) &_%s_method_%s_annotation_info_pointers\n'%(i.name_lower, m.name_lower))
856                     self.c.write('  },\n'
857                                  '  "handle-%s",\n'
858                                  '  %s\n'
859                                  %(m.name_hyphen, 'TRUE' if unix_fd else 'FALSE'))
860                     self.c.write('};\n'
861                                  '\n')
862
863                 self.c.write('static const _ExtendedGDBusMethodInfo * const _%s_method_info_pointers[] =\n'
864                              '{\n'%(i.name_lower))
865                 for m in i.methods:
866                     self.c.write('  &_%s_method_info_%s,\n'%(i.name_lower, m.name_lower))
867                 self.c.write('  NULL\n'
868                              '};\n'
869                              '\n')
870
871             # ---
872
873             if len(i.signals) > 0:
874                 for s in i.signals:
875                     self.generate_args('_%s_signal_info_%s_ARG'%(i.name_lower, s.name_lower), s.args)
876
877                     num_anno = self.generate_annotations('_%s_signal_%s_annotation_info'%(i.name_lower, s.name_lower), s.annotations)
878                     self.c.write('static const _ExtendedGDBusSignalInfo _%s_signal_info_%s =\n'
879                                  '{\n'
880                                  '  {\n'
881                                  '    -1,\n'
882                                  '    (gchar *) "%s",\n'%(i.name_lower, s.name_lower, s.name))
883                     if len(s.args) == 0:
884                         self.c.write('    NULL,\n')
885                     else:
886                         self.c.write('    (GDBusArgInfo **) &_%s_signal_info_%s_ARG_pointers,\n'%(i.name_lower, s.name_lower))
887                     if num_anno == 0:
888                         self.c.write('    NULL\n')
889                     else:
890                         self.c.write('    (GDBusAnnotationInfo **) &_%s_signal_%s_annotation_info_pointers\n'%(i.name_lower, s.name_lower))
891                     self.c.write('  },\n'
892                                  '  "%s"\n'
893                                  %(s.name_hyphen))
894                     self.c.write('};\n'
895                                  '\n')
896
897                 self.c.write('static const _ExtendedGDBusSignalInfo * const _%s_signal_info_pointers[] =\n'
898                              '{\n'%(i.name_lower))
899                 for s in i.signals:
900                     self.c.write('  &_%s_signal_info_%s,\n'%(i.name_lower, s.name_lower))
901                 self.c.write('  NULL\n'
902                              '};\n'
903                              '\n')
904
905             # ---
906
907             if len(i.properties) > 0:
908                 for p in i.properties:
909                     if p.readable and p.writable:
910                         access = 'G_DBUS_PROPERTY_INFO_FLAGS_READABLE | G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE'
911                     elif p.readable:
912                         access = 'G_DBUS_PROPERTY_INFO_FLAGS_READABLE'
913                     elif p.writable:
914                         access = 'G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE'
915                     else:
916                         access = 'G_DBUS_PROPERTY_INFO_FLAGS_NONE'
917                     num_anno = self.generate_annotations('_%s_property_%s_annotation_info'%(i.name_lower, p.name_lower), p.annotations)
918                     self.c.write('static const _ExtendedGDBusPropertyInfo _%s_property_info_%s =\n'
919                                  '{\n'
920                                  '  {\n'
921                                  '    -1,\n'
922                                  '    (gchar *) "%s",\n'
923                                  '    (gchar *) "%s",\n'
924                                  '    %s,\n'%(i.name_lower, p.name_lower, p.name, p.arg.signature, access))
925                     if num_anno == 0:
926                         self.c.write('    NULL\n')
927                     else:
928                         self.c.write('    (GDBusAnnotationInfo **) &_%s_property_%s_annotation_info_pointers\n'%(i.name_lower, p.name_lower))
929                     self.c.write('  },\n'
930                                  '  "%s",\n'
931                                  %(p.name_hyphen))
932                     if not utils.lookup_annotation(p.annotations, 'org.gtk.GDBus.C.ForceGVariant'):
933                         self.c.write('  FALSE\n')
934                     else:
935                         self.c.write('  TRUE\n')
936                     self.c.write('};\n'
937                                  '\n')
938
939                 self.c.write('static const _ExtendedGDBusPropertyInfo * const _%s_property_info_pointers[] =\n'
940                              '{\n'%(i.name_lower))
941                 for p in i.properties:
942                     self.c.write('  &_%s_property_info_%s,\n'%(i.name_lower, p.name_lower))
943                 self.c.write('  NULL\n'
944                              '};\n'
945                              '\n')
946
947             num_anno = self.generate_annotations('_%s_annotation_info'%(i.name_lower), i.annotations)
948             self.c.write('static const _ExtendedGDBusInterfaceInfo _%s_interface_info =\n'
949                          '{\n'
950                          '  {\n'
951                          '    -1,\n'
952                          '    (gchar *) "%s",\n'%(i.name_lower, i.name))
953             if len(i.methods) == 0:
954                 self.c.write('    NULL,\n')
955             else:
956                 self.c.write('    (GDBusMethodInfo **) &_%s_method_info_pointers,\n'%(i.name_lower))
957             if len(i.signals) == 0:
958                 self.c.write('    NULL,\n')
959             else:
960                 self.c.write('    (GDBusSignalInfo **) &_%s_signal_info_pointers,\n'%(i.name_lower))
961             if len(i.properties) == 0:
962                 self.c.write('    NULL,\n')
963             else:
964                 self.c.write('    (GDBusPropertyInfo **) &_%s_property_info_pointers,\n'%(i.name_lower))
965             if num_anno == 0:
966                 self.c.write('    NULL\n')
967             else:
968                 self.c.write('    (GDBusAnnotationInfo **) &_%s_annotation_info_pointers\n'%(i.name_lower))
969             self.c.write('  },\n'
970                          '  "%s",\n'
971                          '};\n'
972                          '\n'
973                          %(i.name_hyphen))
974             self.c.write('\n')
975             self.c.write(self.docbook_gen.expand(
976                     '/**\n'
977                     ' * %s_interface_info:\n'
978                     ' *\n'
979                     ' * Gets a machine-readable description of the #%s D-Bus interface.\n'
980                     ' *\n'
981                     ' * Returns: (transfer none): A #GDBusInterfaceInfo. Do not free.\n'
982                     %(i.name_lower, i.name), False))
983             self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
984             self.c.write('GDBusInterfaceInfo *\n'
985                          '%s_interface_info (void)\n'
986                          '{\n'
987                          '  return (GDBusInterfaceInfo *) &_%s_interface_info.parent_struct;\n'
988                          '}\n'
989                          '\n'%(i.name_lower, i.name_lower))
990
991             self.c.write(self.docbook_gen.expand(
992                     '/**\n'
993                     ' * %s_override_properties:\n'
994                     ' * @klass: The class structure for a #GObject<!-- -->-derived class.\n'
995                     ' * @property_id_begin: The property id to assign to the first overridden property.\n'
996                     ' *\n'
997                     ' * Overrides all #GObject properties in the #%s interface for a concrete class.\n'
998                     ' * The properties are overridden in the order they are defined.\n'
999                     ' *\n'
1000                     ' * Returns: The last property id.\n'
1001                     %(i.name_lower, i.camel_name), False))
1002             self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
1003             self.c.write('guint\n'
1004                          '%s_override_properties (GObjectClass *klass, guint property_id_begin)\n'
1005                          '{\n'%(i.name_lower))
1006             for p in i.properties:
1007                 self.c.write ('  g_object_class_override_property (klass, property_id_begin++, "%s");\n'%(p.name_hyphen))
1008             self.c.write('  return property_id_begin - 1;\n'
1009                          '}\n'
1010                          '\n')
1011             self.c.write('\n')
1012
1013     # ----------------------------------------------------------------------------------------------------
1014
1015     def generate_interface(self, i):
1016         self.c.write('\n')
1017
1018         self.c.write(self.docbook_gen.expand(
1019                 '/**\n'
1020                 ' * %s:\n'
1021                 ' *\n'
1022                 ' * Abstract interface type for the D-Bus interface #%s.\n'
1023                 %(i.camel_name, i.name), False))
1024         self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
1025         self.c.write('\n')
1026
1027         self.c.write(self.docbook_gen.expand(
1028                 '/**\n'
1029                 ' * %sIface:\n'
1030                 ' * @parent_iface: The parent interface.\n'
1031                 %(i.camel_name), False))
1032
1033         doc_bits = {}
1034         if len(i.methods) > 0:
1035             for m in i.methods:
1036                 key = (m.since, '_method_%s'%m.name_lower)
1037                 value  = '@handle_%s: '%(m.name_lower)
1038                 value += 'Handler for the #%s::handle-%s signal.'%(i.camel_name, m.name_hyphen)
1039                 doc_bits[key] = value
1040         if len(i.signals) > 0:
1041             for s in i.signals:
1042                 key = (s.since, '_signal_%s'%s.name_lower)
1043                 value  = '@%s: '%(s.name_lower)
1044                 value += 'Handler for the #%s::%s signal.'%(i.camel_name, s.name_hyphen)
1045                 doc_bits[key] = value
1046         if len(i.properties) > 0:
1047             for p in i.properties:
1048                 key = (p.since, '_prop_get_%s'%p.name_lower)
1049                 value  = '@get_%s: '%(p.name_lower)
1050                 value += 'Getter for the #%s:%s property.'%(i.camel_name, p.name_hyphen)
1051                 doc_bits[key] = value
1052         for key in sorted(doc_bits.keys(), key=utils.version_cmp_key):
1053             self.c.write(' * %s\n'%doc_bits[key])
1054
1055         self.c.write(self.docbook_gen.expand(
1056                 ' *\n'
1057                 ' * Virtual table for the D-Bus interface #%s.\n'
1058                 %(i.name), False))
1059         self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
1060         self.c.write('\n')
1061
1062         self.c.write('typedef %sIface %sInterface;\n'%(i.camel_name, i.camel_name))
1063         self.c.write('G_DEFINE_INTERFACE (%s, %s, G_TYPE_OBJECT);\n'%(i.camel_name, i.name_lower))
1064         self.c.write('\n')
1065
1066         self.c.write('static void\n'
1067                      '%s_default_init (%sIface *iface)\n'
1068                      '{\n'%(i.name_lower, i.camel_name));
1069
1070         if len(i.methods) > 0:
1071             self.c.write('  /* GObject signals for incoming D-Bus method calls: */\n')
1072             for m in i.methods:
1073                 unix_fd = False
1074                 if utils.lookup_annotation(m.annotations, 'org.gtk.GDBus.C.UnixFD'):
1075                     unix_fd = True
1076                 self.c.write(self.docbook_gen.expand(
1077                         '  /**\n'
1078                         '   * %s::handle-%s:\n'
1079                         '   * @object: A #%s.\n'
1080                         '   * @invocation: A #GDBusMethodInvocation.\n'
1081                         %(i.camel_name, m.name_hyphen, i.camel_name), False))
1082                 if unix_fd:
1083                     self.c.write ('   * @fd_list: (allow-none): A #GUnixFDList or %NULL.\n')
1084                 for a in m.in_args:
1085                     self.c.write ('   * @arg_%s: Argument passed by remote caller.\n'%(a.name))
1086                 self.c.write(self.docbook_gen.expand(
1087                         '   *\n'
1088                         '   * Signal emitted when a remote caller is invoking the %s.%s() D-Bus method.\n'
1089                         '   *\n'
1090                         '   * If a signal handler returns %%TRUE, it means the signal handler will handle the invocation (e.g. take a reference to @invocation and eventually call %s_complete_%s() or e.g. g_dbus_method_invocation_return_error() on it) and no order signal handlers will run. If no signal handler handles the invocation, the %%G_DBUS_ERROR_UNKNOWN_METHOD error is returned.\n'
1091                         '   *\n'
1092                         '   * Returns: %%TRUE if the invocation was handled, %%FALSE to let other signal handlers run.\n'
1093                         %(i.name, m.name, i.name_lower, m.name_lower), False))
1094                 self.write_gtkdoc_deprecated_and_since_and_close(m, self.c, 2)
1095                 if unix_fd:
1096                     extra_args = 2
1097                 else:
1098                     extra_args = 1
1099                 self.c.write('  g_signal_new ("handle-%s",\n'
1100                              '    G_TYPE_FROM_INTERFACE (iface),\n'
1101                              '    G_SIGNAL_RUN_LAST,\n'
1102                              '    G_STRUCT_OFFSET (%sIface, handle_%s),\n'
1103                              '    g_signal_accumulator_true_handled,\n'
1104                              '    NULL,\n' # accu_data
1105                              '    g_cclosure_marshal_generic,\n'
1106                              '    G_TYPE_BOOLEAN,\n'
1107                              '    %d,\n'
1108                              '    G_TYPE_DBUS_METHOD_INVOCATION'
1109                              %(m.name_hyphen, i.camel_name, m.name_lower, len(m.in_args) + extra_args))
1110                 if unix_fd:
1111                     self.c.write(', G_TYPE_UNIX_FD_LIST')
1112                 for a in m.in_args:
1113                     self.c.write (', %s'%(a.gtype))
1114                 self.c.write(');\n')
1115                 self.c.write('\n')
1116
1117         if len(i.signals) > 0:
1118             self.c.write('  /* GObject signals for received D-Bus signals: */\n')
1119             for s in i.signals:
1120                 self.c.write(self.docbook_gen.expand(
1121                         '  /**\n'
1122                         '   * %s::%s:\n'
1123                         '   * @object: A #%s.\n'
1124                         %(i.camel_name, s.name_hyphen, i.camel_name), False))
1125                 for a in s.args:
1126                     self.c.write ('   * @arg_%s: Argument.\n'%(a.name))
1127                 self.c.write(self.docbook_gen.expand(
1128                         '   *\n'
1129                         '   * On the client-side, this signal is emitted whenever the D-Bus signal #%s::%s is received.\n'
1130                         '   *\n'
1131                         '   * On the service-side, this signal can be used with e.g. g_signal_emit_by_name() to make the object emit the D-Bus signal.\n'
1132                         %(i.name, s.name), False))
1133                 self.write_gtkdoc_deprecated_and_since_and_close(s, self.c, 2)
1134                 self.c.write('  g_signal_new ("%s",\n'
1135                              '    G_TYPE_FROM_INTERFACE (iface),\n'
1136                              '    G_SIGNAL_RUN_LAST,\n'
1137                              '    G_STRUCT_OFFSET (%sIface, %s),\n'
1138                              '    NULL,\n' # accumulator
1139                              '    NULL,\n' # accu_data
1140                              '    g_cclosure_marshal_generic,\n'
1141                              '    G_TYPE_NONE,\n'
1142                              '    %d'
1143                              %(s.name_hyphen, i.camel_name, s.name_lower, len(s.args)))
1144                 for a in s.args:
1145                     self.c.write (', %s'%(a.gtype))
1146                 self.c.write(');\n')
1147                 self.c.write('\n')
1148
1149         if len(i.properties) > 0:
1150             self.c.write('  /* GObject properties for D-Bus properties: */\n')
1151             for p in i.properties:
1152                 if p.readable and p.writable:
1153                     hint = 'Since the D-Bus property for this #GObject property is both readable and writable, it is meaningful to both read from it and write to it on both the service- and client-side.'
1154                 elif p.readable:
1155                     hint = 'Since the D-Bus property for this #GObject property is readable but not writable, it is meaningful to read from it on both the client- and service-side. It is only meaningful, however, to write to it on the service-side.'
1156                 elif p.writable:
1157                     hint = 'Since the D-Bus property for this #GObject property is writable but not readable, it is meaningful to write to it on both the client- and service-side. It is only meaningful, however, to read from it on the service-side.'
1158                 else:
1159                     raise RuntimeError('Cannot handle property %s that neither readable nor writable'%(p.name))
1160                 self.c.write(self.docbook_gen.expand(
1161                         '  /**\n'
1162                         '   * %s:%s:\n'
1163                         '   *\n'
1164                         '   * Represents the D-Bus property #%s:%s.\n'
1165                         '   *\n'
1166                         '   * %s\n'
1167                         %(i.camel_name, p.name_hyphen, i.name, p.name, hint), False))
1168                 self.write_gtkdoc_deprecated_and_since_and_close(p, self.c, 2)
1169                 self.c.write('  g_object_interface_install_property (iface,\n')
1170                 if p.arg.gtype == 'G_TYPE_VARIANT':
1171                     s = 'g_param_spec_variant ("%s", "%s", "%s", G_VARIANT_TYPE ("%s"), NULL'%(p.name_hyphen, p.name, p.name, p.arg.signature)
1172                 elif p.arg.signature == 'b':
1173                     s = 'g_param_spec_boolean ("%s", "%s", "%s", FALSE'%(p.name_hyphen, p.name, p.name)
1174                 elif p.arg.signature == 'y':
1175                     s = 'g_param_spec_uchar ("%s", "%s", "%s", 0, 255, 0'%(p.name_hyphen, p.name, p.name)
1176                 elif p.arg.signature == 'n':
1177                     s = 'g_param_spec_int ("%s", "%s", "%s", G_MININT16, G_MAXINT16, 0'%(p.name_hyphen, p.name, p.name)
1178                 elif p.arg.signature == 'q':
1179                     s = 'g_param_spec_uint ("%s", "%s", "%s", 0, G_MAXUINT16, 0'%(p.name_hyphen, p.name, p.name)
1180                 elif p.arg.signature == 'i':
1181                     s = 'g_param_spec_int ("%s", "%s", "%s", G_MININT32, G_MAXINT32, 0'%(p.name_hyphen, p.name, p.name)
1182                 elif p.arg.signature == 'u':
1183                     s = 'g_param_spec_uint ("%s", "%s", "%s", 0, G_MAXUINT32, 0'%(p.name_hyphen, p.name, p.name)
1184                 elif p.arg.signature == 'x':
1185                     s = 'g_param_spec_int64 ("%s", "%s", "%s", G_MININT64, G_MAXINT64, 0'%(p.name_hyphen, p.name, p.name)
1186                 elif p.arg.signature == 't':
1187                     s = 'g_param_spec_uint64 ("%s", "%s", "%s", 0, G_MAXUINT64, 0'%(p.name_hyphen, p.name, p.name)
1188                 elif p.arg.signature == 'd':
1189                     s = 'g_param_spec_double ("%s", "%s", "%s", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0'%(p.name_hyphen, p.name, p.name)
1190                 elif p.arg.signature == 's':
1191                     s = 'g_param_spec_string ("%s", "%s", "%s", NULL'%(p.name_hyphen, p.name, p.name)
1192                 elif p.arg.signature == 'o':
1193                     s = 'g_param_spec_string ("%s", "%s", "%s", NULL'%(p.name_hyphen, p.name, p.name)
1194                 elif p.arg.signature == 'g':
1195                     s = 'g_param_spec_string ("%s", "%s", "%s", NULL'%(p.name_hyphen, p.name, p.name)
1196                 elif p.arg.signature == 'ay':
1197                     s = 'g_param_spec_string ("%s", "%s", "%s", NULL'%(p.name_hyphen, p.name, p.name)
1198                 elif p.arg.signature == 'as':
1199                     s = 'g_param_spec_boxed ("%s", "%s", "%s", G_TYPE_STRV'%(p.name_hyphen, p.name, p.name)
1200                 elif p.arg.signature == 'ao':
1201                     s = 'g_param_spec_boxed ("%s", "%s", "%s", G_TYPE_STRV'%(p.name_hyphen, p.name, p.name)
1202                 elif p.arg.signature == 'aay':
1203                     s = 'g_param_spec_boxed ("%s", "%s", "%s", G_TYPE_STRV'%(p.name_hyphen, p.name, p.name)
1204                 else:
1205                     raise RuntimeError('Unsupported gtype %s for GParamSpec'%(p.arg.gtype))
1206                 self.c.write('    %s, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));'%s);
1207                 self.c.write('\n')
1208
1209         self.c.write('}\n'
1210                      '\n')
1211
1212     # ----------------------------------------------------------------------------------------------------
1213
1214     def generate_property_accessors(self, i):
1215         for p in i.properties:
1216             # getter
1217             if p.readable and p.writable:
1218                 hint = 'Since this D-Bus property is both readable and writable, it is meaningful to use this function on both the client- and service-side.'
1219             elif p.readable:
1220                 hint = 'Since this D-Bus property is readable, it is meaningful to use this function on both the client- and service-side.'
1221             elif p.writable:
1222                 hint = 'Since this D-Bus property is not readable, it is only meaningful to use this function on the service-side.'
1223             else:
1224                 raise RuntimeError('Cannot handle property %s that neither readable nor writable'%(p.name))
1225             self.c.write(self.docbook_gen.expand(
1226                     '/**\n'
1227                     ' * %s_get_%s: (skip)\n'
1228                     ' * @object: A #%s.\n'
1229                     ' *\n'
1230                     ' * Gets the value of the #%s:%s D-Bus property.\n'
1231                     ' *\n'
1232                     ' * %s\n'
1233                     ' *\n'
1234                     %(i.name_lower, p.name_lower, i.camel_name, i.name, p.name, hint), False))
1235             if p.arg.free_func != None:
1236                 self.c.write(' * <warning>The returned value is only valid until the property changes so on the client-side it is only safe to use this function on the thread where @object was constructed. Use %s_dup_%s() if on another thread.</warning>\n'
1237                              ' *\n'
1238                              ' * Returns: (transfer none): The property value or %%NULL if the property is not set. Do not free the returned value, it belongs to @object.\n'
1239                              %(i.name_lower, p.name_lower))
1240             else:
1241                 self.c.write(' * Returns: The property value.\n')
1242             self.write_gtkdoc_deprecated_and_since_and_close(p, self.c, 0)
1243             self.c.write('%s\n'
1244                          '%s_get_%s (%s *object)\n'
1245                          '{\n'%(p.arg.ctype_in, i.name_lower, p.name_lower, i.camel_name))
1246             self.c.write('  return %s%s_GET_IFACE (object)->get_%s (object);\n'%(i.ns_upper, i.name_upper, p.name_lower))
1247             self.c.write('}\n')
1248             self.c.write('\n')
1249             if p.arg.free_func != None:
1250
1251                 self.c.write(self.docbook_gen.expand(
1252                         '/**\n'
1253                         ' * %s_dup_%s: (skip)\n'
1254                         ' * @object: A #%s.\n'
1255                         ' *\n'
1256                         ' * Gets a copy of the #%s:%s D-Bus property.\n'
1257                         ' *\n'
1258                         ' * %s\n'
1259                         ' *\n'
1260                         ' * Returns: (transfer full): The property value or %%NULL if the property is not set. The returned value should be freed with %s().\n'
1261                         %(i.name_lower, p.name_lower, i.camel_name, i.name, p.name, hint, p.arg.free_func), False))
1262                 self.write_gtkdoc_deprecated_and_since_and_close(p, self.c, 0)
1263                 self.c.write('%s\n'
1264                              '%s_dup_%s (%s *object)\n'
1265                              '{\n'
1266                              '  %svalue;\n'%(p.arg.ctype_in_dup, i.name_lower, p.name_lower, i.camel_name, p.arg.ctype_in_dup))
1267                 self.c.write('  g_object_get (G_OBJECT (object), "%s", &value, NULL);\n'%(p.name_hyphen))
1268                 self.c.write('  return value;\n')
1269                 self.c.write('}\n')
1270                 self.c.write('\n')
1271
1272             # setter
1273             if p.readable and p.writable:
1274                 hint = 'Since this D-Bus property is both readable and writable, it is meaningful to use this function on both the client- and service-side.'
1275             elif p.readable:
1276                 hint = 'Since this D-Bus property is not writable, it is only meaningful to use this function on the service-side.'
1277             elif p.writable:
1278                 hint = 'Since this D-Bus property is writable, it is meaningful to use this function on both the client- and service-side.'
1279             else:
1280                 raise RuntimeError('Cannot handle property %s that neither readable nor writable'%(p.name))
1281             self.c.write(self.docbook_gen.expand(
1282                     '/**\n'
1283                     ' * %s_set_%s: (skip)\n'
1284                     ' * @object: A #%s.\n'
1285                     ' * @value: The value to set.\n'
1286                     ' *\n'
1287                     ' * Sets the #%s:%s D-Bus property to @value.\n'
1288                     ' *\n'
1289                     ' * %s\n'
1290                     %(i.name_lower, p.name_lower, i.camel_name, i.name, p.name, hint), False))
1291             self.write_gtkdoc_deprecated_and_since_and_close(p, self.c, 0)
1292             self.c.write('void\n'
1293                          '%s_set_%s (%s *object, %svalue)\n'
1294                          '{\n'%(i.name_lower, p.name_lower, i.camel_name, p.arg.ctype_in, ))
1295             self.c.write('  g_object_set (G_OBJECT (object), "%s", value, NULL);\n'%(p.name_hyphen))
1296             self.c.write('}\n')
1297             self.c.write('\n')
1298
1299     # ---------------------------------------------------------------------------------------------------
1300
1301     def generate_signal_emitters(self, i):
1302         for s in i.signals:
1303             self.c.write(self.docbook_gen.expand(
1304                     '/**\n'
1305                     ' * %s_emit_%s:\n'
1306                     ' * @object: A #%s.\n'
1307                     %(i.name_lower, s.name_lower, i.camel_name), False))
1308             for a in s.args:
1309                 self.c.write(' * @arg_%s: Argument to pass with the signal.\n'%(a.name))
1310             self.c.write(self.docbook_gen.expand(
1311                     ' *\n'
1312                     ' * Emits the #%s::%s D-Bus signal.\n'
1313                     %(i.name, s.name), False))
1314             self.write_gtkdoc_deprecated_and_since_and_close(s, self.c, 0)
1315             self.c.write('void\n'
1316                          '%s_emit_%s (\n'
1317                          '    %s *object'%(i.name_lower, s.name_lower, i.camel_name))
1318             for a in s.args:
1319                 self.c.write(',\n    %sarg_%s'%(a.ctype_in, a.name))
1320             self.c.write(')\n'
1321                          '{\n'
1322                          '  g_signal_emit_by_name (object, "%s"'%(s.name_hyphen))
1323             for a in s.args:
1324                 self.c.write(', arg_%s'%a.name)
1325             self.c.write(');\n')
1326             self.c.write('}\n'
1327                          '\n')
1328
1329     # ---------------------------------------------------------------------------------------------------
1330
1331     def generate_method_calls(self, i):
1332         for m in i.methods:
1333             unix_fd = False
1334             if utils.lookup_annotation(m.annotations, 'org.gtk.GDBus.C.UnixFD'):
1335                 unix_fd = True
1336             # async begin
1337             self.c.write('/**\n'
1338                          ' * %s_call_%s:\n'
1339                          ' * @proxy: A #%sProxy.\n'
1340                          %(i.name_lower, m.name_lower, i.camel_name))
1341             for a in m.in_args:
1342                 self.c.write(' * @arg_%s: Argument to pass with the method invocation.\n'%(a.name))
1343             if unix_fd:
1344                 self.c.write(' * @fd_list: (allow-none): A #GUnixFDList or %NULL.\n')
1345             self.c.write(self.docbook_gen.expand(
1346                     ' * @cancellable: (allow-none): A #GCancellable or %%NULL.\n'
1347                     ' * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %%NULL.\n'
1348                     ' * @user_data: User data to pass to @callback.\n'
1349                     ' *\n'
1350                     ' * Asynchronously invokes the %s.%s() D-Bus method on @proxy.\n'
1351                     ' * When the operation is finished, @callback will be invoked in the <link linkend="g-main-context-push-thread-default">thread-default main loop</link> of the thread you are calling this method from.\n'
1352                     ' * You can then call %s_call_%s_finish() to get the result of the operation.\n'
1353                     ' *\n'
1354                     ' * See %s_call_%s_sync() for the synchronous, blocking version of this method.\n'
1355                     %(i.name, m.name, i.name_lower, m.name_lower, i.name_lower, m.name_lower), False))
1356             self.write_gtkdoc_deprecated_and_since_and_close(m, self.c, 0)
1357             self.c.write('void\n'
1358                          '%s_call_%s (\n'
1359                          '    %s *proxy'%(i.name_lower, m.name_lower, i.camel_name))
1360             for a in m.in_args:
1361                 self.c.write(',\n    %sarg_%s'%(a.ctype_in, a.name))
1362             if unix_fd:
1363                 self.c.write(',\n    GUnixFDList *fd_list')
1364             self.c.write(',\n'
1365                          '    GCancellable *cancellable,\n'
1366                          '    GAsyncReadyCallback callback,\n'
1367                          '    gpointer user_data)\n'
1368                          '{\n')
1369             if unix_fd:
1370                 self.c.write('  g_dbus_proxy_call_with_unix_fd_list (G_DBUS_PROXY (proxy),\n')
1371             else:
1372                 self.c.write('  g_dbus_proxy_call (G_DBUS_PROXY (proxy),\n')
1373             self.c.write('    "%s",\n'
1374                          '    g_variant_new ("('%(m.name))
1375             for a in m.in_args:
1376                 self.c.write('%s'%(a.format_in))
1377             self.c.write(')"')
1378             for a in m.in_args:
1379                 self.c.write(',\n                   arg_%s'%(a.name))
1380             self.c.write('),\n'
1381                          '    G_DBUS_CALL_FLAGS_NONE,\n'
1382                          '    -1,\n')
1383             if unix_fd:
1384                 self.c.write('    fd_list,\n')
1385             self.c.write('    cancellable,\n'
1386                          '    callback,\n'
1387                          '    user_data);\n')
1388             self.c.write('}\n'
1389                          '\n')
1390             # async finish
1391             self.c.write('/**\n'
1392                          ' * %s_call_%s_finish:\n'
1393                          ' * @proxy: A #%sProxy.\n'
1394                          %(i.name_lower, m.name_lower, i.camel_name))
1395             for a in m.out_args:
1396                 self.c.write(' * @out_%s: (out): Return location for return parameter or %%NULL to ignore.\n'%(a.name))
1397             if unix_fd:
1398                 self.c.write(' * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.\n')
1399             self.c.write(self.docbook_gen.expand(
1400                     ' * @res: The #GAsyncResult obtained from the #GAsyncReadyCallback passed to %s_call_%s().\n'
1401                     ' * @error: Return location for error or %%NULL.\n'
1402                     ' *\n'
1403                     ' * Finishes an operation started with %s_call_%s().\n'
1404                     ' *\n'
1405                     ' * Returns: (skip): %%TRUE if the call succeded, %%FALSE if @error is set.\n'
1406                     %(i.name_lower, m.name_lower, i.name_lower, m.name_lower), False))
1407             self.write_gtkdoc_deprecated_and_since_and_close(m, self.c, 0)
1408             self.c.write('gboolean\n'
1409                          '%s_call_%s_finish (\n'
1410                          '    %s *proxy'%(i.name_lower, m.name_lower, i.camel_name))
1411             for a in m.out_args:
1412                 self.c.write(',\n    %sout_%s'%(a.ctype_out, a.name))
1413             if unix_fd:
1414                 self.c.write(',\n    GUnixFDList **out_fd_list')
1415             self.c.write(',\n'
1416                          '    GAsyncResult *res,\n'
1417                          '    GError **error)\n'
1418                          '{\n'
1419                          '  GVariant *_ret;\n')
1420             if unix_fd:
1421                 self.c.write('  _ret = g_dbus_proxy_call_with_unix_fd_list_finish (G_DBUS_PROXY (proxy), out_fd_list, res, error);\n')
1422             else:
1423                 self.c.write('  _ret = g_dbus_proxy_call_finish (G_DBUS_PROXY (proxy), res, error);\n')
1424             self.c.write('  if (_ret == NULL)\n'
1425                          '    goto _out;\n')
1426             self.c.write('  g_variant_get (_ret,\n'
1427                          '                 \"(')
1428             for a in m.out_args:
1429                 self.c.write('%s'%(a.format_out))
1430             self.c.write(')"')
1431             for a in m.out_args:
1432                 self.c.write(',\n                 out_%s'%(a.name))
1433             self.c.write(');\n'
1434                          '  g_variant_unref (_ret);\n')
1435             self.c.write('_out:\n'
1436                          '  return _ret != NULL;\n'
1437                          '}\n'
1438                          '\n')
1439
1440
1441             # sync
1442             self.c.write('/**\n'
1443                          ' * %s_call_%s_sync:\n'
1444                          ' * @proxy: A #%sProxy.\n'
1445                          %(i.name_lower, m.name_lower, i.camel_name))
1446             for a in m.in_args:
1447                 self.c.write(' * @arg_%s: Argument to pass with the method invocation.\n'%(a.name))
1448             if unix_fd:
1449                 self.c.write(' * @fd_list: (allow-none): A #GUnixFDList or %NULL.\n')
1450             for a in m.out_args:
1451                 self.c.write(' * @out_%s: (out): Return location for return parameter or %%NULL to ignore.\n'%(a.name))
1452             if unix_fd:
1453                 self.c.write(' * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.\n')
1454             self.c.write(self.docbook_gen.expand(
1455                     ' * @cancellable: (allow-none): A #GCancellable or %%NULL.\n'
1456                     ' * @error: Return location for error or %%NULL.\n'
1457                     ' *\n'
1458                     ' * Synchronously invokes the %s.%s() D-Bus method on @proxy. The calling thread is blocked until a reply is received.\n'
1459                     ' *\n'
1460                     ' * See %s_call_%s() for the asynchronous version of this method.\n'
1461                     ' *\n'
1462                     ' * Returns: (skip): %%TRUE if the call succeded, %%FALSE if @error is set.\n'
1463                     %(i.name, m.name, i.name_lower, m.name_lower), False))
1464             self.write_gtkdoc_deprecated_and_since_and_close(m, self.c, 0)
1465             self.c.write('gboolean\n'
1466                          '%s_call_%s_sync (\n'
1467                          '    %s *proxy'%(i.name_lower, m.name_lower, i.camel_name))
1468             for a in m.in_args:
1469                 self.c.write(',\n    %sarg_%s'%(a.ctype_in, a.name))
1470             if unix_fd:
1471                 self.c.write(',\n    GUnixFDList  *fd_list')
1472             for a in m.out_args:
1473                 self.c.write(',\n    %sout_%s'%(a.ctype_out, a.name))
1474             if unix_fd:
1475                 self.c.write(',\n    GUnixFDList **out_fd_list')
1476             self.c.write(',\n'
1477                          '    GCancellable *cancellable,\n'
1478                          '    GError **error)\n'
1479                          '{\n'
1480                          '  GVariant *_ret;\n')
1481             if unix_fd:
1482                 self.c.write('  _ret = g_dbus_proxy_call_with_unix_fd_list_sync (G_DBUS_PROXY (proxy),\n')
1483             else:
1484                 self.c.write('  _ret = g_dbus_proxy_call_sync (G_DBUS_PROXY (proxy),\n')
1485             self.c.write('    "%s",\n'
1486                          '    g_variant_new ("('%(m.name))
1487             for a in m.in_args:
1488                 self.c.write('%s'%(a.format_in))
1489             self.c.write(')"')
1490             for a in m.in_args:
1491                 self.c.write(',\n                   arg_%s'%(a.name))
1492             self.c.write('),\n'
1493                          '    G_DBUS_CALL_FLAGS_NONE,\n'
1494                          '    -1,\n')
1495             if unix_fd:
1496                 self.c.write('    fd_list,\n'
1497                              '    out_fd_list,\n')
1498             self.c.write('    cancellable,\n'
1499                          '    error);\n'
1500                          '  if (_ret == NULL)\n'
1501                          '    goto _out;\n')
1502             self.c.write('  g_variant_get (_ret,\n'
1503                          '                 \"(')
1504             for a in m.out_args:
1505                 self.c.write('%s'%(a.format_out))
1506             self.c.write(')"')
1507             for a in m.out_args:
1508                 self.c.write(',\n                 out_%s'%(a.name))
1509             self.c.write(');\n'
1510                          '  g_variant_unref (_ret);\n')
1511             self.c.write('_out:\n'
1512                          '  return _ret != NULL;\n'
1513                          '}\n'
1514                          '\n')
1515
1516     # ---------------------------------------------------------------------------------------------------
1517
1518     def generate_method_completers(self, i):
1519         for m in i.methods:
1520             unix_fd = False
1521             if utils.lookup_annotation(m.annotations, 'org.gtk.GDBus.C.UnixFD'):
1522                 unix_fd = True
1523             self.c.write('/**\n'
1524                          ' * %s_complete_%s:\n'
1525                          ' * @object: A #%s.\n'
1526                          ' * @invocation: (transfer full): A #GDBusMethodInvocation.\n'
1527                          %(i.name_lower, m.name_lower, i.camel_name))
1528             if unix_fd:
1529                 self.c.write (' * @fd_list: (allow-none): A #GUnixFDList or %NULL.\n')
1530             for a in m.out_args:
1531                 self.c.write(' * @%s: Parameter to return.\n'%(a.name))
1532             self.c.write(self.docbook_gen.expand(
1533                     ' *\n'
1534                     ' * Helper function used in service implementations to finish handling invocations of the %s.%s() D-Bus method. If you instead want to finish handling an invocation by returning an error, use g_dbus_method_invocation_return_error() or similar.\n'
1535                     ' *\n'
1536                     ' * This method will free @invocation, you cannot use it afterwards.\n'
1537                     %(i.name, m.name), False))
1538             self.write_gtkdoc_deprecated_and_since_and_close(m, self.c, 0)
1539             self.c.write('void\n'
1540                          '%s_complete_%s (\n'
1541                          '    %s *object,\n'
1542                          '    GDBusMethodInvocation *invocation'%(i.name_lower, m.name_lower, i.camel_name))
1543             if unix_fd:
1544                 self.c.write(',\n    GUnixFDList *fd_list')
1545             for a in m.out_args:
1546                 self.c.write(',\n    %s%s'%(a.ctype_in, a.name))
1547             self.c.write(')\n'
1548                          '{\n')
1549
1550             if unix_fd:
1551                 self.c.write('  g_dbus_method_invocation_return_value_with_unix_fd_list (invocation,\n'
1552                              '    g_variant_new ("(')
1553             else:
1554                 self.c.write('  g_dbus_method_invocation_return_value (invocation,\n'
1555                              '    g_variant_new ("(')
1556             for a in m.out_args:
1557                 self.c.write('%s'%(a.format_in))
1558             self.c.write(')"')
1559             for a in m.out_args:
1560                 self.c.write(',\n                   %s'%(a.name))
1561             if unix_fd:
1562                 self.c.write('),\n    fd_list);\n')
1563             else:
1564                 self.c.write('));\n')
1565             self.c.write('}\n'
1566                          '\n')
1567
1568     # ---------------------------------------------------------------------------------------------------
1569
1570     def generate_proxy(self, i):
1571         # class boilerplate
1572         self.c.write('/* ------------------------------------------------------------------------ */\n'
1573                      '\n')
1574
1575         self.c.write(self.docbook_gen.expand(
1576                 '/**\n'
1577                 ' * %sProxy:\n'
1578                 ' *\n'
1579                 ' * The #%sProxy structure contains only private data and should only be accessed using the provided API.\n'
1580                 %(i.camel_name, i.camel_name), False))
1581         self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
1582         self.c.write('\n')
1583
1584         self.c.write(self.docbook_gen.expand(
1585                 '/**\n'
1586                 ' * %sProxyClass:\n'
1587                 ' * @parent_class: The parent class.\n'
1588                 ' *\n'
1589                 ' * Class structure for #%sProxy.\n'
1590                 %(i.camel_name, i.camel_name), False))
1591         self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
1592         self.c.write('\n')
1593
1594         self.c.write('struct _%sProxyPrivate\n'
1595                      '{\n'
1596                      '  GData *qdata;\n'
1597                      '};\n'
1598                      '\n'%i.camel_name)
1599
1600         self.c.write('static void %s_proxy_iface_init (%sIface *iface);\n'
1601                      '\n'%(i.name_lower, i.camel_name))
1602         self.c.write('#if GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_38\n')
1603         self.c.write('G_DEFINE_TYPE_WITH_CODE (%sProxy, %s_proxy, G_TYPE_DBUS_PROXY,\n'%(i.camel_name, i.name_lower))
1604         self.c.write('                         G_ADD_PRIVATE (%sProxy)\n'%(i.camel_name))
1605         self.c.write('                         G_IMPLEMENT_INTERFACE (%sTYPE_%s, %s_proxy_iface_init));\n\n'%(i.ns_upper, i.name_upper, i.name_lower))
1606         self.c.write('#else\n')
1607         self.c.write('G_DEFINE_TYPE_WITH_CODE (%sProxy, %s_proxy, G_TYPE_DBUS_PROXY,\n'%(i.camel_name, i.name_lower))
1608         self.c.write('                         G_IMPLEMENT_INTERFACE (%sTYPE_%s, %s_proxy_iface_init));\n\n'%(i.ns_upper, i.name_upper, i.name_lower))
1609         self.c.write('#endif\n')
1610
1611         # finalize
1612         self.c.write('static void\n'
1613                      '%s_proxy_finalize (GObject *object)\n'
1614                      '{\n'%(i.name_lower))
1615         self.c.write('  %sProxy *proxy = %s%s_PROXY (object);\n'%(i.camel_name, i.ns_upper, i.name_upper))
1616         self.c.write('  g_datalist_clear (&proxy->priv->qdata);\n')
1617         self.c.write('  G_OBJECT_CLASS (%s_proxy_parent_class)->finalize (object);\n'
1618                      '}\n'
1619                      '\n'%(i.name_lower))
1620
1621         # property accessors
1622         #
1623         # Note that we are guaranteed that prop_id starts at 1 and is
1624         # laid out in the same order as introspection data pointers
1625         #
1626         self.c.write('static void\n'
1627                      '%s_proxy_get_property (GObject      *object,\n'
1628                      '  guint         prop_id,\n'
1629                      '  GValue       *value,\n'
1630                      '  GParamSpec   *pspec G_GNUC_UNUSED)\n'
1631                      '{\n'%(i.name_lower))
1632         if len(i.properties) > 0:
1633             self.c.write('  const _ExtendedGDBusPropertyInfo *info;\n'
1634                          '  GVariant *variant;\n'
1635                          '  g_assert (prop_id != 0 && prop_id - 1 < %d);\n'
1636                          '  info = _%s_property_info_pointers[prop_id - 1];\n'
1637                          '  variant = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (object), info->parent_struct.name);\n'
1638                          '  if (info->use_gvariant)\n'
1639                          '    {\n'
1640                          '      g_value_set_variant (value, variant);\n'
1641                          '    }\n'
1642                          '  else\n'
1643                          '    {\n'
1644                          # could be that we don't have the value in cache - in that case, we do
1645                          # nothing and the user gets the default value for the GType
1646                          '      if (variant != NULL)\n'
1647                          '        g_dbus_gvariant_to_gvalue (variant, value);\n'
1648                          '    }\n'
1649                          '  if (variant != NULL)\n'
1650                          '    g_variant_unref (variant);\n'
1651                          %(len(i.properties), i.name_lower))
1652         self.c.write('}\n'
1653                      '\n')
1654         if len(i.properties) > 0:
1655             self.c.write('static void\n'
1656                          '%s_proxy_set_property_cb (GDBusProxy *proxy,\n'
1657                          '  GAsyncResult *res,\n'
1658                          '  gpointer      user_data)\n'
1659                          '{\n'%(i.name_lower))
1660             self.c.write('  const _ExtendedGDBusPropertyInfo *info = user_data;\n'
1661                          '  GError *error;\n'
1662                          '  GVariant *_ret;\n'
1663                          '  error = NULL;\n'
1664                          '  _ret = g_dbus_proxy_call_finish (proxy, res, &error);\n'
1665                          '  if (!_ret)\n'
1666                          '    {\n'
1667                          '      g_warning ("Error setting property \'%%s\' on interface %s: %%s (%%s, %%d)",\n'
1668                          '                 info->parent_struct.name, \n'
1669                          '                 error->message, g_quark_to_string (error->domain), error->code);\n'
1670                          '      g_error_free (error);\n'
1671                          '    }\n'
1672                          '  else\n'
1673                          '    {\n'
1674                          '      g_variant_unref (_ret);\n'
1675                          '    }\n'
1676                          %(i.name))
1677             self.c.write('}\n'
1678                          '\n')
1679         self.c.write('static void\n'
1680                      '%s_proxy_set_property (GObject      *object,\n'
1681                      '  guint         prop_id,\n'
1682                      '  const GValue *value,\n'
1683                      '  GParamSpec   *pspec G_GNUC_UNUSED)\n'
1684                      '{\n'%(i.name_lower))
1685         if len(i.properties) > 0:
1686             self.c.write('  const _ExtendedGDBusPropertyInfo *info;\n'
1687                          '  GVariant *variant;\n'
1688                          '  g_assert (prop_id != 0 && prop_id - 1 < %d);\n'
1689                          '  info = _%s_property_info_pointers[prop_id - 1];\n'
1690                          '  variant = g_dbus_gvalue_to_gvariant (value, G_VARIANT_TYPE (info->parent_struct.signature));\n'
1691                          '  g_dbus_proxy_call (G_DBUS_PROXY (object),\n'
1692                          '    "org.freedesktop.DBus.Properties.Set",\n'
1693                          '    g_variant_new ("(ssv)", "%s", info->parent_struct.name, variant),\n'
1694                          '    G_DBUS_CALL_FLAGS_NONE,\n'
1695                          '    -1,\n'
1696                          '    NULL, (GAsyncReadyCallback) %s_proxy_set_property_cb, (GDBusPropertyInfo *) &info->parent_struct);\n'
1697                          '  g_variant_unref (variant);\n'
1698                          %(len(i.properties), i.name_lower, i.name, i.name_lower))
1699         self.c.write('}\n'
1700                      '\n')
1701
1702         # signal received
1703         self.c.write('static void\n'
1704                      '%s_proxy_g_signal (GDBusProxy *proxy,\n'
1705                      '  const gchar *sender_name G_GNUC_UNUSED,\n'
1706                      '  const gchar *signal_name,\n'
1707                      '  GVariant *parameters)\n'
1708                      '{\n'%(i.name_lower))
1709         self.c.write('  _ExtendedGDBusSignalInfo *info;\n'
1710                      '  GVariantIter iter;\n'
1711                      '  GVariant *child;\n'
1712                      '  GValue *paramv;\n'
1713                      '  guint num_params;\n'
1714                      '  guint n;\n'
1715                      '  guint signal_id;\n');
1716         # Note: info could be NULL if we are talking to a newer version of the interface
1717         self.c.write('  info = (_ExtendedGDBusSignalInfo *) g_dbus_interface_info_lookup_signal ((GDBusInterfaceInfo *) &_%s_interface_info.parent_struct, signal_name);\n'
1718                      '  if (info == NULL)\n'
1719                      '    return;\n'
1720                      %(i.name_lower))
1721         self.c.write ('  num_params = g_variant_n_children (parameters);\n'
1722                       '  paramv = g_new0 (GValue, num_params + 1);\n'
1723                       '  g_value_init (&paramv[0], %sTYPE_%s);\n'
1724                       '  g_value_set_object (&paramv[0], proxy);\n'
1725                       %(i.ns_upper, i.name_upper))
1726         self.c.write('  g_variant_iter_init (&iter, parameters);\n'
1727                      '  n = 1;\n'
1728                      '  while ((child = g_variant_iter_next_value (&iter)) != NULL)\n'
1729                      '    {\n'
1730                      '      _ExtendedGDBusArgInfo *arg_info = (_ExtendedGDBusArgInfo *) info->parent_struct.args[n - 1];\n'
1731                      '      if (arg_info->use_gvariant)\n'
1732                      '        {\n'
1733                      '          g_value_init (&paramv[n], G_TYPE_VARIANT);\n'
1734                      '          g_value_set_variant (&paramv[n], child);\n'
1735                      '          n++;\n'
1736                      '        }\n'
1737                      '      else\n'
1738                      '        g_dbus_gvariant_to_gvalue (child, &paramv[n++]);\n'
1739                      '      g_variant_unref (child);\n'
1740                      '    }\n'
1741                      )
1742         self.c.write('  signal_id = g_signal_lookup (info->signal_name, %sTYPE_%s);\n'
1743                      %(i.ns_upper, i.name_upper))
1744         self.c.write('  g_signal_emitv (paramv, signal_id, 0, NULL);\n')
1745         self.c.write('  for (n = 0; n < num_params + 1; n++)\n'
1746                      '    g_value_unset (&paramv[n]);\n'
1747                      '  g_free (paramv);\n')
1748         self.c.write('}\n'
1749                      '\n')
1750
1751         # property changed
1752         self.c.write('static void\n'
1753                      '%s_proxy_g_properties_changed (GDBusProxy *_proxy,\n'
1754                      '  GVariant *changed_properties,\n'
1755                      '  const gchar *const *invalidated_properties)\n'
1756                      '{\n'%(i.name_lower))
1757         # Note: info could be NULL if we are talking to a newer version of the interface
1758         self.c.write('  %sProxy *proxy = %s%s_PROXY (_proxy);\n'
1759                      '  guint n;\n'
1760                      '  const gchar *key;\n'
1761                      '  GVariantIter *iter;\n'
1762                      '  _ExtendedGDBusPropertyInfo *info;\n'
1763                      '  g_variant_get (changed_properties, "a{sv}", &iter);\n'
1764                      '  while (g_variant_iter_next (iter, "{&sv}", &key, NULL))\n'
1765                      '    {\n'
1766                      '      info = (_ExtendedGDBusPropertyInfo *) g_dbus_interface_info_lookup_property ((GDBusInterfaceInfo *) &_%s_interface_info.parent_struct, key);\n'
1767                      '      g_datalist_remove_data (&proxy->priv->qdata, key);\n'
1768                      '      if (info != NULL)\n'
1769                      '        g_object_notify (G_OBJECT (proxy), info->hyphen_name);\n'
1770                      '    }\n'
1771                      '  g_variant_iter_free (iter);\n'
1772                      '  for (n = 0; invalidated_properties[n] != NULL; n++)\n'
1773                      '    {\n'
1774                      '      info = (_ExtendedGDBusPropertyInfo *) g_dbus_interface_info_lookup_property ((GDBusInterfaceInfo *) &_%s_interface_info.parent_struct, invalidated_properties[n]);\n'
1775                      '      g_datalist_remove_data (&proxy->priv->qdata, invalidated_properties[n]);\n'
1776                      '      if (info != NULL)\n'
1777                      '        g_object_notify (G_OBJECT (proxy), info->hyphen_name);\n'
1778                      '    }\n'
1779                      '}\n'
1780                      '\n'
1781                      %(i.camel_name, i.ns_upper, i.name_upper,
1782                        i.name_lower, i.name_lower))
1783
1784         # property vfuncs
1785         for p in i.properties:
1786             nul_value = '0'
1787             if p.arg.free_func != None:
1788                 nul_value = 'NULL'
1789             self.c.write('static %s\n'
1790                          '%s_proxy_get_%s (%s *object)\n'
1791                          '{\n'
1792                          '  %sProxy *proxy = %s%s_PROXY (object);\n'
1793                          '  GVariant *variant;\n'
1794                          '  %svalue = %s;\n'%(p.arg.ctype_in, i.name_lower, p.name_lower, i.camel_name,
1795                                               i.camel_name, i.ns_upper, i.name_upper,
1796                                               p.arg.ctype_in, nul_value))
1797             # For some property types, we have to free the returned
1798             # value (or part of it, e.g. the container) because of how
1799             # GVariant works.. see https://bugzilla.gnome.org/show_bug.cgi?id=657100
1800             # for details
1801             #
1802             free_container = False;
1803             if p.arg.gvariant_get == 'g_variant_get_strv' or p.arg.gvariant_get == 'g_variant_get_objpathv' or p.arg.gvariant_get == 'g_variant_get_bytestring_array':
1804                 free_container = True;
1805             # If already using an old value for strv, objpathv, bytestring_array (see below),
1806             # then just return that... that way the result from multiple consecutive calls
1807             # to the getter are valid as long as they're freed
1808             #
1809             if free_container:
1810                 self.c.write('  value = g_datalist_get_data (&proxy->priv->qdata, \"%s\");\n'
1811                              '  if (value != NULL)\n'
1812                              '    return value;\n'
1813                              %(p.name))
1814             self.c.write('  variant = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), \"%s\");\n'%(p.name))
1815             if p.arg.gtype == 'G_TYPE_VARIANT':
1816                 self.c.write('  value = variant;\n')
1817                 self.c.write('  if (variant != NULL)\n')
1818                 self.c.write('    g_variant_unref (variant);\n')
1819             else:
1820                 self.c.write('  if (variant != NULL)\n'
1821                              '    {\n')
1822                 extra_len = ''
1823                 if p.arg.gvariant_get == 'g_variant_get_string' or p.arg.gvariant_get == 'g_variant_get_strv' or p.arg.gvariant_get == 'g_variant_get_objv' or p.arg.gvariant_get == 'g_variant_get_bytestring_array':
1824                     extra_len = ', NULL'
1825                 self.c.write('      value = %s (variant%s);\n'%(p.arg.gvariant_get, extra_len))
1826                 if free_container:
1827                     self.c.write('      g_datalist_set_data_full (&proxy->priv->qdata, \"%s\", (gpointer) value, g_free);\n'
1828                                  %(p.name))
1829                 self.c.write('      g_variant_unref (variant);\n')
1830                 self.c.write('    }\n')
1831             self.c.write('  return value;\n')
1832             self.c.write('}\n')
1833             self.c.write('\n')
1834
1835         # class boilerplate
1836         self.c.write('static void\n'
1837                      '%s_proxy_init (%sProxy *proxy)\n'
1838                      '{\n'
1839                      '#if GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_38\n'
1840                      '  proxy->priv = %s_proxy_get_instance_private (proxy);\n'
1841                      '#else\n'
1842                      '  proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, %sTYPE_%s_PROXY, %sProxyPrivate);\n'
1843                      '#endif\n\n'
1844                      '  g_dbus_proxy_set_interface_info (G_DBUS_PROXY (proxy), %s_interface_info ());\n'
1845                      '}\n'
1846                      '\n'
1847                      %(i.name_lower, i.camel_name,
1848                        i.name_lower,
1849                        i.ns_upper, i.name_upper, i.camel_name,
1850                        i.name_lower))
1851         self.c.write('static void\n'
1852                      '%s_proxy_class_init (%sProxyClass *klass)\n'
1853                      '{\n'
1854                      '  GObjectClass *gobject_class;\n'
1855                      '  GDBusProxyClass *proxy_class;\n'
1856                      '\n'
1857                      '  gobject_class = G_OBJECT_CLASS (klass);\n'
1858                      '  gobject_class->finalize     = %s_proxy_finalize;\n'
1859                      '  gobject_class->get_property = %s_proxy_get_property;\n'
1860                      '  gobject_class->set_property = %s_proxy_set_property;\n'
1861                      '\n'
1862                      '  proxy_class = G_DBUS_PROXY_CLASS (klass);\n'
1863                      '  proxy_class->g_signal = %s_proxy_g_signal;\n'
1864                      '  proxy_class->g_properties_changed = %s_proxy_g_properties_changed;\n'
1865                      '\n'%(i.name_lower, i.camel_name,
1866                            i.name_lower, i.name_lower, i.name_lower, i.name_lower, i.name_lower))
1867         if len(i.properties) > 0:
1868             self.c.write('  %s_override_properties (gobject_class, 1);\n\n'%(i.name_lower))
1869         self.c.write('#if GLIB_VERSION_MAX_ALLOWED < GLIB_VERSION_2_38\n'
1870                      '  g_type_class_add_private (klass, sizeof (%sProxyPrivate));\n'
1871                      '#endif\n'%(i.camel_name))
1872         self.c.write('}\n'
1873                      '\n')
1874
1875         self.c.write('static void\n'
1876                      '%s_proxy_iface_init (%sIface *iface)\n'
1877                      '{\n'%(i.name_lower, i.camel_name))
1878         for p in i.properties:
1879             self.c.write('  iface->get_%s = %s_proxy_get_%s;\n'%(p.name_lower, i.name_lower, p.name_lower))
1880         self.c.write('}\n'
1881                      '\n')
1882
1883         # constructors
1884         self.c.write(self.docbook_gen.expand(
1885                 '/**\n'
1886                 ' * %s_proxy_new:\n'
1887                 ' * @connection: A #GDBusConnection.\n'
1888                 ' * @flags: Flags from the #GDBusProxyFlags enumeration.\n'
1889                 ' * @name: (allow-none): A bus name (well-known or unique) or %%NULL if @connection is not a message bus connection.\n'
1890                 ' * @object_path: An object path.\n'
1891                 ' * @cancellable: (allow-none): A #GCancellable or %%NULL.\n'
1892                 ' * @callback: A #GAsyncReadyCallback to call when the request is satisfied.\n'
1893                 ' * @user_data: User data to pass to @callback.\n'
1894                 ' *\n'
1895                 ' * Asynchronously creates a proxy for the D-Bus interface #%s. See g_dbus_proxy_new() for more details.\n'
1896                 ' *\n'
1897                 ' * When the operation is finished, @callback will be invoked in the <link linkend="g-main-context-push-thread-default">thread-default main loop</link> of the thread you are calling this method from.\n'
1898                 ' * You can then call %s_proxy_new_finish() to get the result of the operation.\n'
1899                 ' *\n'
1900                 ' * See %s_proxy_new_sync() for the synchronous, blocking version of this constructor.\n'
1901                 %(i.name_lower, i.name, i.name_lower, i.name_lower), False))
1902         self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
1903         self.c.write('void\n'
1904                      '%s_proxy_new (\n'
1905                      '    GDBusConnection     *connection,\n'
1906                      '    GDBusProxyFlags      flags,\n'
1907                      '    const gchar         *name,\n'
1908                      '    const gchar         *object_path,\n'
1909                      '    GCancellable        *cancellable,\n'
1910                      '    GAsyncReadyCallback  callback,\n'
1911                      '    gpointer             user_data)\n'
1912                      '{\n'
1913                      '  g_async_initable_new_async (%sTYPE_%s_PROXY, G_PRIORITY_DEFAULT, cancellable, callback, user_data, "g-flags", flags, "g-name", name, "g-connection", connection, "g-object-path", object_path, "g-interface-name", "%s", NULL);\n'
1914                      '}\n'
1915                      '\n'
1916                      %(i.name_lower, i.ns_upper, i.name_upper, i.name))
1917         self.c.write('/**\n'
1918                      ' * %s_proxy_new_finish:\n'
1919                      ' * @res: The #GAsyncResult obtained from the #GAsyncReadyCallback passed to %s_proxy_new().\n'
1920                      ' * @error: Return location for error or %%NULL\n'
1921                      ' *\n'
1922                      ' * Finishes an operation started with %s_proxy_new().\n'
1923                      ' *\n'
1924                      ' * Returns: (transfer full) (type %sProxy): The constructed proxy object or %%NULL if @error is set.\n'
1925                      %(i.name_lower, i.name_lower, i.name_lower, i.camel_name))
1926         self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
1927         self.c.write('%s *\n'
1928                      '%s_proxy_new_finish (\n'
1929                      '    GAsyncResult        *res,\n'
1930                      '    GError             **error)\n'
1931                      '{\n'
1932                      '  GObject *ret;\n'
1933                      '  GObject *source_object;\n'
1934                      '  source_object = g_async_result_get_source_object (res);\n'
1935                      '  ret = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object), res, error);\n'
1936                      '  g_object_unref (source_object);\n'
1937                      '  if (ret != NULL)\n'
1938                      '    return %s%s (ret);\n'
1939                      '  else\n'
1940                      '    return NULL;\n'
1941                      '}\n'
1942                      '\n'
1943                      %(i.camel_name, i.name_lower, i.ns_upper, i.name_upper))
1944         self.c.write(self.docbook_gen.expand(
1945                 '/**\n'
1946                 ' * %s_proxy_new_sync:\n'
1947                 ' * @connection: A #GDBusConnection.\n'
1948                 ' * @flags: Flags from the #GDBusProxyFlags enumeration.\n'
1949                 ' * @name: (allow-none): A bus name (well-known or unique) or %%NULL if @connection is not a message bus connection.\n'
1950                 ' * @object_path: An object path.\n'
1951                 ' * @cancellable: (allow-none): A #GCancellable or %%NULL.\n'
1952                 ' * @error: Return location for error or %%NULL\n'
1953                 ' *\n'
1954                 ' * Synchronously creates a proxy for the D-Bus interface #%s. See g_dbus_proxy_new_sync() for more details.\n'
1955                 ' *\n'
1956                 ' * The calling thread is blocked until a reply is received.\n'
1957                 ' *\n'
1958                 ' * See %s_proxy_new() for the asynchronous version of this constructor.\n'
1959                 ' *\n'
1960                 ' * Returns: (transfer full) (type %sProxy): The constructed proxy object or %%NULL if @error is set.\n'
1961                 %(i.name_lower, i.name, i.name_lower, i.camel_name), False))
1962         self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
1963         self.c.write('%s *\n'
1964                      '%s_proxy_new_sync (\n'
1965                      '    GDBusConnection     *connection,\n'
1966                      '    GDBusProxyFlags      flags,\n'
1967                      '    const gchar         *name,\n'
1968                      '    const gchar         *object_path,\n'
1969                      '    GCancellable        *cancellable,\n'
1970                      '    GError             **error)\n'
1971                      '{\n'
1972                      '  GInitable *ret;\n'
1973                      '  ret = g_initable_new (%sTYPE_%s_PROXY, cancellable, error, "g-flags", flags, "g-name", name, "g-connection", connection, "g-object-path", object_path, "g-interface-name", "%s", NULL);\n'
1974                      '  if (ret != NULL)\n'
1975                      '    return %s%s (ret);\n'
1976                      '  else\n'
1977                      '    return NULL;\n'
1978                      '}\n'
1979                      '\n'
1980                      %(i.camel_name, i.name_lower, i.ns_upper, i.name_upper, i.name, i.ns_upper, i.name_upper))
1981         self.c.write('\n')
1982         self.c.write(self.docbook_gen.expand(
1983                 '/**\n'
1984                 ' * %s_proxy_new_for_bus:\n'
1985                 ' * @bus_type: A #GBusType.\n'
1986                 ' * @flags: Flags from the #GDBusProxyFlags enumeration.\n'
1987                 ' * @name: A bus name (well-known or unique).\n'
1988                 ' * @object_path: An object path.\n'
1989                 ' * @cancellable: (allow-none): A #GCancellable or %%NULL.\n'
1990                 ' * @callback: A #GAsyncReadyCallback to call when the request is satisfied.\n'
1991                 ' * @user_data: User data to pass to @callback.\n'
1992                 ' *\n'
1993                 ' * Like %s_proxy_new() but takes a #GBusType instead of a #GDBusConnection.\n'
1994                 ' *\n'
1995                 ' * When the operation is finished, @callback will be invoked in the <link linkend="g-main-context-push-thread-default">thread-default main loop</link> of the thread you are calling this method from.\n'
1996                 ' * You can then call %s_proxy_new_for_bus_finish() to get the result of the operation.\n'
1997                 ' *\n'
1998                 ' * See %s_proxy_new_for_bus_sync() for the synchronous, blocking version of this constructor.\n'
1999                 %(i.name_lower, i.name_lower, i.name_lower, i.name_lower), False))
2000         self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
2001         self.c.write('void\n'
2002                      '%s_proxy_new_for_bus (\n'
2003                      '    GBusType             bus_type,\n'
2004                      '    GDBusProxyFlags      flags,\n'
2005                      '    const gchar         *name,\n'
2006                      '    const gchar         *object_path,\n'
2007                      '    GCancellable        *cancellable,\n'
2008                      '    GAsyncReadyCallback  callback,\n'
2009                      '    gpointer             user_data)\n'
2010                      '{\n'
2011                      '  g_async_initable_new_async (%sTYPE_%s_PROXY, G_PRIORITY_DEFAULT, cancellable, callback, user_data, "g-flags", flags, "g-name", name, "g-bus-type", bus_type, "g-object-path", object_path, "g-interface-name", "%s", NULL);\n'
2012                      '}\n'
2013                      '\n'
2014                      %(i.name_lower, i.ns_upper, i.name_upper, i.name))
2015         self.c.write('/**\n'
2016                      ' * %s_proxy_new_for_bus_finish:\n'
2017                      ' * @res: The #GAsyncResult obtained from the #GAsyncReadyCallback passed to %s_proxy_new_for_bus().\n'
2018                      ' * @error: Return location for error or %%NULL\n'
2019                      ' *\n'
2020                      ' * Finishes an operation started with %s_proxy_new_for_bus().\n'
2021                      ' *\n'
2022                      ' * Returns: (transfer full) (type %sProxy): The constructed proxy object or %%NULL if @error is set.\n'
2023                      %(i.name_lower, i.name_lower, i.name_lower, i.camel_name))
2024         self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
2025         self.c.write('%s *\n'
2026                      '%s_proxy_new_for_bus_finish (\n'
2027                      '    GAsyncResult        *res,\n'
2028                      '    GError             **error)\n'
2029                      '{\n'
2030                      '  GObject *ret;\n'
2031                      '  GObject *source_object;\n'
2032                      '  source_object = g_async_result_get_source_object (res);\n'
2033                      '  ret = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object), res, error);\n'
2034                      '  g_object_unref (source_object);\n'
2035                      '  if (ret != NULL)\n'
2036                      '    return %s%s (ret);\n'
2037                      '  else\n'
2038                      '    return NULL;\n'
2039                      '}\n'
2040                      '\n'
2041                      %(i.camel_name, i.name_lower, i.ns_upper, i.name_upper))
2042         self.c.write(self.docbook_gen.expand(
2043                 '/**\n'
2044                 ' * %s_proxy_new_for_bus_sync:\n'
2045                 ' * @bus_type: A #GBusType.\n'
2046                 ' * @flags: Flags from the #GDBusProxyFlags enumeration.\n'
2047                 ' * @name: A bus name (well-known or unique).\n'
2048                 ' * @object_path: An object path.\n'
2049                 ' * @cancellable: (allow-none): A #GCancellable or %%NULL.\n'
2050                 ' * @error: Return location for error or %%NULL\n'
2051                 ' *\n'
2052                 ' * Like %s_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.\n'
2053                 ' *\n'
2054                 ' * The calling thread is blocked until a reply is received.\n'
2055                 ' *\n'
2056                 ' * See %s_proxy_new_for_bus() for the asynchronous version of this constructor.\n'
2057                 ' *\n'
2058                 ' * Returns: (transfer full) (type %sProxy): The constructed proxy object or %%NULL if @error is set.\n'
2059                 %(i.name_lower, i.name_lower, i.name_lower, i.camel_name), False))
2060         self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
2061         self.c.write('%s *\n'
2062                      '%s_proxy_new_for_bus_sync (\n'
2063                      '    GBusType             bus_type,\n'
2064                      '    GDBusProxyFlags      flags,\n'
2065                      '    const gchar         *name,\n'
2066                      '    const gchar         *object_path,\n'
2067                      '    GCancellable        *cancellable,\n'
2068                      '    GError             **error)\n'
2069                      '{\n'
2070                      '  GInitable *ret;\n'
2071                      '  ret = g_initable_new (%sTYPE_%s_PROXY, cancellable, error, "g-flags", flags, "g-name", name, "g-bus-type", bus_type, "g-object-path", object_path, "g-interface-name", "%s", NULL);\n'
2072                      '  if (ret != NULL)\n'
2073                      '    return %s%s (ret);\n'
2074                      '  else\n'
2075                      '    return NULL;\n'
2076                      '}\n'
2077                      '\n'
2078                      %(i.camel_name, i.name_lower, i.ns_upper, i.name_upper, i.name, i.ns_upper, i.name_upper))
2079         self.c.write('\n')
2080
2081     # ---------------------------------------------------------------------------------------------------
2082
2083     def generate_skeleton(self, i):
2084         # class boilerplate
2085         self.c.write('/* ------------------------------------------------------------------------ */\n'
2086                      '\n')
2087
2088         self.c.write(self.docbook_gen.expand(
2089                 '/**\n'
2090                 ' * %sSkeleton:\n'
2091                 ' *\n'
2092                 ' * The #%sSkeleton structure contains only private data and should only be accessed using the provided API.\n'
2093                 %(i.camel_name, i.camel_name), False))
2094         self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
2095         self.c.write('\n')
2096
2097         self.c.write(self.docbook_gen.expand(
2098                 '/**\n'
2099                 ' * %sSkeletonClass:\n'
2100                 ' * @parent_class: The parent class.\n'
2101                 ' *\n'
2102                 ' * Class structure for #%sSkeleton.\n'
2103                 %(i.camel_name, i.camel_name), False))
2104         self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
2105         self.c.write('\n')
2106
2107         self.c.write('struct _%sSkeletonPrivate\n'
2108                      '{\n'
2109                      '  GValue *properties;\n'
2110                      '  GList *changed_properties;\n'
2111                      '  GSource *changed_properties_idle_source;\n'
2112                      '  GMainContext *context;\n'
2113                      '  GMutex lock;\n'
2114                      '};\n'
2115                      '\n'%i.camel_name)
2116
2117         self.c.write('static void\n'
2118                      '_%s_skeleton_handle_method_call (\n'
2119                      '  GDBusConnection *connection G_GNUC_UNUSED,\n'
2120                      '  const gchar *sender G_GNUC_UNUSED,\n'
2121                      '  const gchar *object_path G_GNUC_UNUSED,\n'
2122                      '  const gchar *interface_name,\n'
2123                      '  const gchar *method_name,\n'
2124                      '  GVariant *parameters,\n'
2125                      '  GDBusMethodInvocation *invocation,\n'
2126                      '  gpointer user_data)\n'
2127                      '{\n'
2128                      '  %sSkeleton *skeleton = %s%s_SKELETON (user_data);\n'
2129                      '  _ExtendedGDBusMethodInfo *info;\n'
2130                      '  GVariantIter iter;\n'
2131                      '  GVariant *child;\n'
2132                      '  GValue *paramv;\n'
2133                      '  guint num_params;\n'
2134                      '  guint num_extra;\n'
2135                      '  guint n;\n'
2136                      '  guint signal_id;\n'
2137                      '  GValue return_value = G_VALUE_INIT;\n'
2138                      %(i.name_lower, i.camel_name, i.ns_upper, i.name_upper))
2139         self.c.write('  info = (_ExtendedGDBusMethodInfo *) g_dbus_method_invocation_get_method_info (invocation);\n'
2140                      '  g_assert (info != NULL);\n'
2141                      %())
2142         self.c.write ('  num_params = g_variant_n_children (parameters);\n'
2143                       '  num_extra = info->pass_fdlist ? 3 : 2;'
2144                       '  paramv = g_new0 (GValue, num_params + num_extra);\n'
2145                       '  n = 0;\n'
2146                       '  g_value_init (&paramv[n], %sTYPE_%s);\n'
2147                       '  g_value_set_object (&paramv[n++], skeleton);\n'
2148                       '  g_value_init (&paramv[n], G_TYPE_DBUS_METHOD_INVOCATION);\n'
2149                       '  g_value_set_object (&paramv[n++], invocation);\n'
2150                       '  if (info->pass_fdlist)\n'
2151                       '    {\n'
2152                       '#ifdef G_OS_UNIX\n'
2153                       '      g_value_init (&paramv[n], G_TYPE_UNIX_FD_LIST);\n'
2154                       '      g_value_set_object (&paramv[n++], g_dbus_message_get_unix_fd_list (g_dbus_method_invocation_get_message (invocation)));\n'
2155                       '#else\n'
2156                       '      g_assert_not_reached ();\n'
2157                       '#endif\n'
2158                       '    }\n'
2159                       %(i.ns_upper, i.name_upper))
2160         self.c.write('  g_variant_iter_init (&iter, parameters);\n'
2161                      '  while ((child = g_variant_iter_next_value (&iter)) != NULL)\n'
2162                      '    {\n'
2163                      '      _ExtendedGDBusArgInfo *arg_info = (_ExtendedGDBusArgInfo *) info->parent_struct.in_args[n - num_extra];\n'
2164                      '      if (arg_info->use_gvariant)\n'
2165                      '        {\n'
2166                      '          g_value_init (&paramv[n], G_TYPE_VARIANT);\n'
2167                      '          g_value_set_variant (&paramv[n], child);\n'
2168                      '          n++;\n'
2169                      '        }\n'
2170                      '      else\n'
2171                      '        g_dbus_gvariant_to_gvalue (child, &paramv[n++]);\n'
2172                      '      g_variant_unref (child);\n'
2173                      '    }\n'
2174                      )
2175         self.c.write('  signal_id = g_signal_lookup (info->signal_name, %sTYPE_%s);\n'
2176                      %(i.ns_upper, i.name_upper))
2177         self.c.write('  g_value_init (&return_value, G_TYPE_BOOLEAN);\n'
2178                      '  g_signal_emitv (paramv, signal_id, 0, &return_value);\n'
2179                      '  if (!g_value_get_boolean (&return_value))\n'
2180                      '    g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD, "Method %s is not implemented on interface %s", method_name, interface_name);\n'
2181                      '  g_value_unset (&return_value);\n'
2182                      )
2183         self.c.write('  for (n = 0; n < num_params + num_extra; n++)\n'
2184                      '    g_value_unset (&paramv[n]);\n'
2185                      '  g_free (paramv);\n')
2186         self.c.write('}\n'
2187                      '\n')
2188
2189         self.c.write('static GVariant *\n'
2190                      '_%s_skeleton_handle_get_property (\n'
2191                      '  GDBusConnection *connection G_GNUC_UNUSED,\n'
2192                      '  const gchar *sender G_GNUC_UNUSED,\n'
2193                      '  const gchar *object_path G_GNUC_UNUSED,\n'
2194                      '  const gchar *interface_name G_GNUC_UNUSED,\n'
2195                      '  const gchar *property_name,\n'
2196                      '  GError **error,\n'
2197                      '  gpointer user_data)\n'
2198                      '{\n'
2199                      '  %sSkeleton *skeleton = %s%s_SKELETON (user_data);\n'
2200                      '  GValue value = G_VALUE_INIT;\n'
2201                      '  GParamSpec *pspec;\n'
2202                      '  _ExtendedGDBusPropertyInfo *info;\n'
2203                      '  GVariant *ret;\n'
2204                      %(i.name_lower, i.camel_name, i.ns_upper, i.name_upper))
2205         self.c.write('  ret = NULL;\n'
2206                      '  info = (_ExtendedGDBusPropertyInfo *) g_dbus_interface_info_lookup_property ((GDBusInterfaceInfo *) &_%s_interface_info.parent_struct, property_name);\n'
2207                      '  g_assert (info != NULL);\n'
2208                      '  pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (skeleton), info->hyphen_name);\n'
2209                      '  if (pspec == NULL)\n'
2210                      '    {\n'
2211                      '      g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "No property with name %%s", property_name);\n'
2212                      '    }\n'
2213                      '  else\n'
2214                      '    {\n'
2215                      '      g_value_init (&value, pspec->value_type);\n'
2216                      '      g_object_get_property (G_OBJECT (skeleton), info->hyphen_name, &value);\n'
2217                      '      ret = g_dbus_gvalue_to_gvariant (&value, G_VARIANT_TYPE (info->parent_struct.signature));\n'
2218                      '      g_value_unset (&value);\n'
2219                      '    }\n'
2220                      '  return ret;\n'
2221                      '}\n'
2222                      '\n'
2223                      %(i.name_lower))
2224
2225         self.c.write('static gboolean\n'
2226                      '_%s_skeleton_handle_set_property (\n'
2227                      '  GDBusConnection *connection G_GNUC_UNUSED,\n'
2228                      '  const gchar *sender G_GNUC_UNUSED,\n'
2229                      '  const gchar *object_path G_GNUC_UNUSED,\n'
2230                      '  const gchar *interface_name G_GNUC_UNUSED,\n'
2231                      '  const gchar *property_name,\n'
2232                      '  GVariant *variant,\n'
2233                      '  GError **error,\n'
2234                      '  gpointer user_data)\n'
2235                      '{\n'
2236                      '  %sSkeleton *skeleton = %s%s_SKELETON (user_data);\n'
2237                      '  GValue value = G_VALUE_INIT;\n'
2238                      '  GParamSpec *pspec;\n'
2239                      '  _ExtendedGDBusPropertyInfo *info;\n'
2240                      '  gboolean ret;\n'
2241                      %(i.name_lower, i.camel_name, i.ns_upper, i.name_upper))
2242         self.c.write('  ret = FALSE;\n'
2243                      '  info = (_ExtendedGDBusPropertyInfo *) g_dbus_interface_info_lookup_property ((GDBusInterfaceInfo *) &_%s_interface_info.parent_struct, property_name);\n'
2244                      '  g_assert (info != NULL);\n'
2245                      '  pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (skeleton), info->hyphen_name);\n'
2246                      '  if (pspec == NULL)\n'
2247                      '    {\n'
2248                      '      g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "No property with name %%s", property_name);\n'
2249                      '    }\n'
2250                      '  else\n'
2251                      '    {\n'
2252                      '      if (info->use_gvariant)\n'
2253                      '        g_value_set_variant (&value, variant);\n'
2254                      '      else\n'
2255                      '        g_dbus_gvariant_to_gvalue (variant, &value);\n'
2256                      '      g_object_set_property (G_OBJECT (skeleton), info->hyphen_name, &value);\n'
2257                      '      g_value_unset (&value);\n'
2258                      '      ret = TRUE;\n'
2259                      '    }\n'
2260                      '  return ret;\n'
2261                      '}\n'
2262                      '\n'
2263                      %(i.name_lower))
2264
2265
2266         self.c.write('static const GDBusInterfaceVTable _%s_skeleton_vtable =\n'
2267                      '{\n'
2268                      '  _%s_skeleton_handle_method_call,\n'
2269                      '  _%s_skeleton_handle_get_property,\n'
2270                      '  _%s_skeleton_handle_set_property,\n'
2271                      '  {NULL}\n'
2272                      '};\n'
2273                      '\n'%(i.name_lower, i.name_lower, i.name_lower, i.name_lower))
2274
2275         self.c.write('static GDBusInterfaceInfo *\n'
2276                      '%s_skeleton_dbus_interface_get_info (GDBusInterfaceSkeleton *skeleton G_GNUC_UNUSED)\n'
2277                      '{\n'
2278                      '  return %s_interface_info ();\n'
2279                      %(i.name_lower, i.name_lower))
2280         self.c.write('}\n'
2281                      '\n')
2282
2283         self.c.write('static GDBusInterfaceVTable *\n'
2284                      '%s_skeleton_dbus_interface_get_vtable (GDBusInterfaceSkeleton *skeleton G_GNUC_UNUSED)\n'
2285                      '{\n'
2286                      '  return (GDBusInterfaceVTable *) &_%s_skeleton_vtable;\n'
2287                      %(i.name_lower, i.name_lower))
2288         self.c.write('}\n'
2289                      '\n')
2290
2291         self.c.write('static GVariant *\n'
2292                      '%s_skeleton_dbus_interface_get_properties (GDBusInterfaceSkeleton *_skeleton)\n'
2293                      '{\n'
2294                      '  %sSkeleton *skeleton = %s%s_SKELETON (_skeleton);\n'
2295                      %(i.name_lower, i.camel_name, i.ns_upper, i.name_upper))
2296         self.c.write('\n'
2297                      '  GVariantBuilder builder;\n'
2298                      '  guint n;\n'
2299                      '  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));\n'
2300                      '  if (_%s_interface_info.parent_struct.properties == NULL)\n'
2301                      '    goto out;\n'
2302                      '  for (n = 0; _%s_interface_info.parent_struct.properties[n] != NULL; n++)\n'
2303                      '    {\n'
2304                      '      GDBusPropertyInfo *info = _%s_interface_info.parent_struct.properties[n];\n'
2305                      '      if (info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE)\n'
2306                      '        {\n'
2307                      '          GVariant *value;\n'
2308                      '          value = _%s_skeleton_handle_get_property (g_dbus_interface_skeleton_get_connection (G_DBUS_INTERFACE_SKELETON (skeleton)), NULL, g_dbus_interface_skeleton_get_object_path (G_DBUS_INTERFACE_SKELETON (skeleton)), "%s", info->name, NULL, skeleton);\n'
2309                      '          if (value != NULL)\n'
2310                      '            {\n'
2311                      '              g_variant_take_ref (value);\n'
2312                      '              g_variant_builder_add (&builder, "{sv}", info->name, value);\n'
2313                      '              g_variant_unref (value);\n'
2314                      '            }\n'
2315                      '        }\n'
2316                      '    }\n'
2317                      'out:\n'
2318                      '  return g_variant_builder_end (&builder);\n'
2319                      '}\n'
2320                      '\n'
2321                      %(i.name_lower, i.name_lower, i.name_lower, i.name_lower, i.name))
2322
2323         if len(i.properties) > 0:
2324             self.c.write('static gboolean _%s_emit_changed (gpointer user_data);\n'
2325                          '\n'
2326                          %(i.name_lower))
2327
2328         self.c.write('static void\n'
2329                      '%s_skeleton_dbus_interface_flush (GDBusInterfaceSkeleton *_skeleton)\n'
2330                      '{\n'
2331                      %(i.name_lower))
2332         if len(i.properties) > 0:
2333             self.c.write('  %sSkeleton *skeleton = %s%s_SKELETON (_skeleton);\n'
2334                          '  gboolean emit_changed = FALSE;\n'
2335                          '\n'
2336                          '  g_mutex_lock (&skeleton->priv->lock);\n'
2337                          '  if (skeleton->priv->changed_properties_idle_source != NULL)\n'
2338                          '    {\n'
2339                          '      g_source_destroy (skeleton->priv->changed_properties_idle_source);\n'
2340                          '      skeleton->priv->changed_properties_idle_source = NULL;\n'
2341                          '      emit_changed = TRUE;\n'
2342                          '    }\n'
2343                          '  g_mutex_unlock (&skeleton->priv->lock);\n'
2344                          '\n'
2345                          '  if (emit_changed)\n'
2346                          '    _%s_emit_changed (skeleton);\n'
2347                          %(i.camel_name, i.ns_upper, i.name_upper, i.name_lower))
2348         self.c.write('}\n'
2349                      '\n')
2350
2351         for s in i.signals:
2352             self.c.write('static void\n'
2353                          '_%s_on_signal_%s (\n'
2354                          '    %s *object'%(i.name_lower, s.name_lower, i.camel_name))
2355             for a in s.args:
2356                 self.c.write(',\n    %sarg_%s'%(a.ctype_in, a.name))
2357             self.c.write(')\n'
2358                          '{\n'
2359                          '  %sSkeleton *skeleton = %s%s_SKELETON (object);\n\n'
2360                          '  GList      *connections, *l;\n'
2361                          '  GVariant   *signal_variant;\n'
2362                          '  connections = g_dbus_interface_skeleton_get_connections (G_DBUS_INTERFACE_SKELETON (skeleton));\n'
2363                          %(i.camel_name, i.ns_upper, i.name_upper))
2364             self.c.write('\n'
2365                          '  signal_variant = g_variant_ref_sink (g_variant_new ("(')
2366             for a in s.args:
2367                 self.c.write('%s'%(a.format_in))
2368             self.c.write(')"')
2369             for a in s.args:
2370                 self.c.write(',\n                   arg_%s'%(a.name))
2371             self.c.write('));\n')
2372
2373             self.c.write('  for (l = connections; l != NULL; l = l->next)\n'
2374                          '    {\n'
2375                          '      GDBusConnection *connection = l->data;\n'
2376                          '      g_dbus_connection_emit_signal (connection,\n'
2377                          '        NULL, g_dbus_interface_skeleton_get_object_path (G_DBUS_INTERFACE_SKELETON (skeleton)), "%s", "%s",\n'
2378                          '        signal_variant, NULL);\n'
2379                          '    }\n'
2380                          %(i.name, s.name))
2381             self.c.write('  g_variant_unref (signal_variant);\n')
2382             self.c.write('  g_list_free_full (connections, g_object_unref);\n')
2383             self.c.write('}\n'
2384                          '\n')
2385
2386         self.c.write('static void %s_skeleton_iface_init (%sIface *iface);\n'
2387                      %(i.name_lower, i.camel_name))
2388
2389         self.c.write('#if GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_38\n')
2390         self.c.write('G_DEFINE_TYPE_WITH_CODE (%sSkeleton, %s_skeleton, G_TYPE_DBUS_INTERFACE_SKELETON,\n'%(i.camel_name, i.name_lower))
2391         self.c.write('                         G_ADD_PRIVATE (%sSkeleton)\n'%(i.camel_name))
2392         self.c.write('                         G_IMPLEMENT_INTERFACE (%sTYPE_%s, %s_skeleton_iface_init));\n\n'%(i.ns_upper, i.name_upper, i.name_lower))
2393         self.c.write('#else\n')
2394         self.c.write('G_DEFINE_TYPE_WITH_CODE (%sSkeleton, %s_skeleton, G_TYPE_DBUS_INTERFACE_SKELETON,\n'%(i.camel_name, i.name_lower))
2395         self.c.write('                         G_IMPLEMENT_INTERFACE (%sTYPE_%s, %s_skeleton_iface_init));\n\n'%(i.ns_upper, i.name_upper, i.name_lower))
2396         self.c.write('#endif\n')
2397
2398         # finalize
2399         self.c.write('static void\n'
2400                      '%s_skeleton_finalize (GObject *object)\n'
2401                      '{\n'%(i.name_lower))
2402         self.c.write('  %sSkeleton *skeleton = %s%s_SKELETON (object);\n'%(i.camel_name, i.ns_upper, i.name_upper))
2403         if len(i.properties) > 0:
2404             self.c.write('  guint n;\n'
2405                          '  for (n = 0; n < %d; n++)\n'
2406                          '    g_value_unset (&skeleton->priv->properties[n]);\n'%(len(i.properties)))
2407             self.c.write('  g_free (skeleton->priv->properties);\n')
2408         self.c.write('  g_list_free_full (skeleton->priv->changed_properties, (GDestroyNotify) _changed_property_free);\n')
2409         self.c.write('  if (skeleton->priv->changed_properties_idle_source != NULL)\n')
2410         self.c.write('    g_source_destroy (skeleton->priv->changed_properties_idle_source);\n')
2411         self.c.write('  g_main_context_unref (skeleton->priv->context);\n')
2412         self.c.write('  g_mutex_clear (&skeleton->priv->lock);\n')
2413         self.c.write('  G_OBJECT_CLASS (%s_skeleton_parent_class)->finalize (object);\n'
2414                      '}\n'
2415                      '\n'%(i.name_lower))
2416
2417         # property accessors (TODO: generate PropertiesChanged signals in setter)
2418         if len(i.properties) > 0:
2419             self.c.write('static void\n'
2420                          '%s_skeleton_get_property (GObject      *object,\n'
2421                          '  guint         prop_id,\n'
2422                          '  GValue       *value,\n'
2423                          '  GParamSpec   *pspec G_GNUC_UNUSED)\n'
2424                          '{\n'%(i.name_lower))
2425             self.c.write('  %sSkeleton *skeleton = %s%s_SKELETON (object);\n'
2426                          '  g_assert (prop_id != 0 && prop_id - 1 < %d);\n'
2427                          '  g_mutex_lock (&skeleton->priv->lock);\n'
2428                          '  g_value_copy (&skeleton->priv->properties[prop_id - 1], value);\n'
2429                          '  g_mutex_unlock (&skeleton->priv->lock);\n'
2430                          %(i.camel_name, i.ns_upper, i.name_upper, len(i.properties)))
2431             self.c.write('}\n'
2432                          '\n')
2433
2434             # if property is already scheduled then re-use entry.. though it could be
2435             # that the user did
2436             #
2437             #  foo_set_prop_bar (object, "");
2438             #  foo_set_prop_bar (object, "blah");
2439             #
2440             # say, every update... In this case, where nothing happens, we obviously
2441             # don't want a PropertiesChanged() event. We can easily check for this
2442             # by comparing against the _original value_ recorded before the first
2443             # change event. If the latest value is not different from the original
2444             # one, we can simply ignore the ChangedProperty
2445             #
2446             self.c.write('static gboolean\n'
2447                          '_%s_emit_changed (gpointer user_data)\n'
2448                          '{\n'
2449                          '  %sSkeleton *skeleton = %s%s_SKELETON (user_data);\n'
2450                          %(i.name_lower, i.camel_name, i.ns_upper, i.name_upper))
2451             self.c.write('  GList *l;\n'
2452                          '  GVariantBuilder builder;\n'
2453                          '  GVariantBuilder invalidated_builder;\n'
2454                          '  guint num_changes;\n'
2455                          '\n'
2456                          '  g_mutex_lock (&skeleton->priv->lock);\n'
2457                          '  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));\n'
2458                          '  g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));\n'
2459                          '  for (l = skeleton->priv->changed_properties, num_changes = 0; l != NULL; l = l->next)\n'
2460                          '    {\n'
2461                          '      ChangedProperty *cp = l->data;\n'
2462                          '      GVariant *variant;\n'
2463                          '      const GValue *cur_value;\n'
2464                          '\n'
2465                          '      cur_value = &skeleton->priv->properties[cp->prop_id - 1];\n'
2466                          '      if (!_g_value_equal (cur_value, &cp->orig_value))\n'
2467                          '        {\n'
2468                          '          variant = g_dbus_gvalue_to_gvariant (cur_value, G_VARIANT_TYPE (cp->info->parent_struct.signature));\n'
2469                          '          g_variant_builder_add (&builder, "{sv}", cp->info->parent_struct.name, variant);\n'
2470                          '          g_variant_unref (variant);\n'
2471                          '          num_changes++;\n'
2472                          '        }\n'
2473                          '    }\n'
2474                          '  if (num_changes > 0)\n'
2475                          '    {\n'
2476                          '      GList *connections, *ll;\n'
2477                          '      GVariant *signal_variant;'
2478                          '\n'
2479                          '      signal_variant = g_variant_ref_sink (g_variant_new ("(sa{sv}as)", "%s",\n'
2480                          '                                           &builder, &invalidated_builder));\n'
2481                          '      connections = g_dbus_interface_skeleton_get_connections (G_DBUS_INTERFACE_SKELETON (skeleton));\n'
2482                          '      for (ll = connections; ll != NULL; ll = ll->next)\n'
2483                          '        {\n'
2484                          '          GDBusConnection *connection = ll->data;\n'
2485                          '\n'
2486                          '          g_dbus_connection_emit_signal (connection,\n'
2487                          '                                         NULL, g_dbus_interface_skeleton_get_object_path (G_DBUS_INTERFACE_SKELETON (skeleton)),\n'
2488                          '                                         "org.freedesktop.DBus.Properties",\n'
2489                          '                                         "PropertiesChanged",\n'
2490                          '                                         signal_variant,\n'
2491                          '                                         NULL);\n'
2492                          '        }\n'
2493                          '      g_variant_unref (signal_variant);\n'
2494                          '      g_list_free_full (connections, g_object_unref);\n'
2495                          '    }\n'
2496                          '  else\n'
2497                          '    {\n'
2498                          '      g_variant_builder_clear (&builder);\n'
2499                          '      g_variant_builder_clear (&invalidated_builder);\n'
2500                          '    }\n'
2501                          %(i.name))
2502             self.c.write('  g_list_free_full (skeleton->priv->changed_properties, (GDestroyNotify) _changed_property_free);\n')
2503             self.c.write('  skeleton->priv->changed_properties = NULL;\n')
2504             self.c.write('  skeleton->priv->changed_properties_idle_source = NULL;\n')
2505             self.c.write('  g_mutex_unlock (&skeleton->priv->lock);\n')
2506             self.c.write('  return FALSE;\n'
2507                          '}\n'
2508                          '\n')
2509             # holding lock while being called
2510             self.c.write('static void\n'
2511                          '_%s_schedule_emit_changed (%sSkeleton *skeleton, const _ExtendedGDBusPropertyInfo *info, guint prop_id, const GValue *orig_value)\n'
2512                          '{\n'
2513                          '  ChangedProperty *cp;\n'
2514                          '  GList *l;\n'
2515                          '  cp = NULL;\n'
2516                          '  for (l = skeleton->priv->changed_properties; l != NULL; l = l->next)\n'
2517                          '    {\n'
2518                          '      ChangedProperty *i_cp = l->data;\n'
2519                          '      if (i_cp->info == info)\n'
2520                          '        {\n'
2521                          '          cp = i_cp;\n'
2522                          '          break;\n'
2523                          '        }\n'
2524                          '    }\n'
2525                          %(i.name_lower, i.camel_name))
2526             self.c.write('  if (cp == NULL)\n'
2527                          '    {\n'
2528                          '      cp = g_new0 (ChangedProperty, 1);\n'
2529                          '      cp->prop_id = prop_id;\n'
2530                          '      cp->info = info;\n'
2531                          '      skeleton->priv->changed_properties = g_list_prepend (skeleton->priv->changed_properties, cp);\n'
2532                          '      g_value_init (&cp->orig_value, G_VALUE_TYPE (orig_value));\n'
2533                          '      g_value_copy (orig_value, &cp->orig_value);\n'
2534                          '    }\n'
2535                          '}\n'
2536                          '\n'
2537                          %())
2538
2539             # Postpone setting up the refresh source until the ::notify signal is emitted as
2540             # this allows use of g_object_freeze_notify()/g_object_thaw_notify() ...
2541             # This is useful when updating several properties from another thread than
2542             # where the idle will be emitted from
2543             self.c.write('static void\n'
2544                          '%s_skeleton_notify (GObject      *object,\n'
2545                          '  GParamSpec *pspec G_GNUC_UNUSED)\n'
2546                          '{\n'
2547                          '  %sSkeleton *skeleton = %s%s_SKELETON (object);\n'
2548                          '  g_mutex_lock (&skeleton->priv->lock);\n'
2549                          '  if (skeleton->priv->changed_properties != NULL &&\n'
2550                          '      skeleton->priv->changed_properties_idle_source == NULL)\n'
2551                          '    {\n'
2552                          '      skeleton->priv->changed_properties_idle_source = g_idle_source_new ();\n'
2553                          '      g_source_set_priority (skeleton->priv->changed_properties_idle_source, G_PRIORITY_DEFAULT);\n'
2554                          '      g_source_set_callback (skeleton->priv->changed_properties_idle_source, _%s_emit_changed, g_object_ref (skeleton), (GDestroyNotify) g_object_unref);\n'
2555                          '      g_source_set_name (skeleton->priv->changed_properties_idle_source, "[generated] _%s_emit_changed");\n'
2556                          '      g_source_attach (skeleton->priv->changed_properties_idle_source, skeleton->priv->context);\n'
2557                          '      g_source_unref (skeleton->priv->changed_properties_idle_source);\n'
2558                          '    }\n'
2559                          '  g_mutex_unlock (&skeleton->priv->lock);\n'
2560                          '}\n'
2561                          '\n'
2562                          %(i.name_lower, i.camel_name, i.ns_upper, i.name_upper, i.name_lower, i.name_lower))
2563
2564             self.c.write('static void\n'
2565                          '%s_skeleton_set_property (GObject      *object,\n'
2566                          '  guint         prop_id,\n'
2567                          '  const GValue *value,\n'
2568                          '  GParamSpec   *pspec)\n'
2569                          '{\n'%(i.name_lower))
2570             self.c.write('  %sSkeleton *skeleton = %s%s_SKELETON (object);\n'
2571                          '  g_assert (prop_id != 0 && prop_id - 1 < %d);\n'
2572                          '  g_mutex_lock (&skeleton->priv->lock);\n'
2573                          '  g_object_freeze_notify (object);\n'
2574                          '  if (!_g_value_equal (value, &skeleton->priv->properties[prop_id - 1]))\n'
2575                          '    {\n'
2576                          '      if (g_dbus_interface_skeleton_get_connection (G_DBUS_INTERFACE_SKELETON (skeleton)) != NULL)\n'
2577                          '        _%s_schedule_emit_changed (skeleton, _%s_property_info_pointers[prop_id - 1], prop_id, &skeleton->priv->properties[prop_id - 1]);\n'
2578                          '      g_value_copy (value, &skeleton->priv->properties[prop_id - 1]);\n'
2579                          '      g_object_notify_by_pspec (object, pspec);\n'
2580                          '    }\n'
2581                          '  g_mutex_unlock (&skeleton->priv->lock);\n'
2582                          '  g_object_thaw_notify (object);\n'
2583                          %(i.camel_name, i.ns_upper, i.name_upper, len(i.properties), i.name_lower, i.name_lower))
2584             self.c.write('}\n'
2585                          '\n')
2586
2587         self.c.write('static void\n'
2588                      '%s_skeleton_init (%sSkeleton *skeleton)\n'
2589                      '{\n'
2590                      '#if GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_38\n'
2591                      '  skeleton->priv = %s_skeleton_get_instance_private (skeleton);\n'
2592                      '#else\n'
2593                      '  skeleton->priv = G_TYPE_INSTANCE_GET_PRIVATE (skeleton, %sTYPE_%s_SKELETON, %sSkeletonPrivate);\n'
2594                      '#endif\n\n'
2595                      %(i.name_lower, i.camel_name,
2596                        i.name_lower,
2597                        i.ns_upper, i.name_upper, i.camel_name))
2598         self.c.write('  g_mutex_init (&skeleton->priv->lock);\n')
2599         self.c.write('  skeleton->priv->context = g_main_context_ref_thread_default ();\n')
2600         if len(i.properties) > 0:
2601             self.c.write('  skeleton->priv->properties = g_new0 (GValue, %d);\n'%(len(i.properties)))
2602             n = 0
2603             for p in i.properties:
2604                 self.c.write('  g_value_init (&skeleton->priv->properties[%d], %s);\n'%(n, p.arg.gtype))
2605                 n += 1
2606         self.c.write('}\n'
2607                      '\n')
2608
2609         # property vfuncs
2610         n = 0
2611         for p in i.properties:
2612             self.c.write('static %s\n'
2613                          '%s_skeleton_get_%s (%s *object)\n'
2614                          '{\n'
2615                          %(p.arg.ctype_in, i.name_lower, p.name_lower, i.camel_name))
2616             self.c.write('  %sSkeleton *skeleton = %s%s_SKELETON (object);\n'%(i.camel_name, i.ns_upper, i.name_upper))
2617             self.c.write('  %svalue;\n'
2618                          '  g_mutex_lock (&skeleton->priv->lock);\n'
2619                          '  value = %s (&(skeleton->priv->properties[%d]));\n'
2620                          '  g_mutex_unlock (&skeleton->priv->lock);\n'
2621                          %(p.arg.ctype_in_g, p.arg.gvalue_get, n))
2622             self.c.write('  return value;\n')
2623             self.c.write('}\n')
2624             self.c.write('\n')
2625             n += 1
2626
2627         self.c.write('static void\n'
2628                      '%s_skeleton_class_init (%sSkeletonClass *klass)\n'
2629                      '{\n'
2630                      '  GObjectClass *gobject_class;\n'
2631                      '  GDBusInterfaceSkeletonClass *skeleton_class;\n'
2632                      '\n'
2633                      '  gobject_class = G_OBJECT_CLASS (klass);\n'
2634                      '  gobject_class->finalize = %s_skeleton_finalize;\n'
2635                      %(i.name_lower, i.camel_name, i.name_lower))
2636         if len(i.properties) > 0:
2637             self.c.write('  gobject_class->get_property = %s_skeleton_get_property;\n'
2638                          '  gobject_class->set_property = %s_skeleton_set_property;\n'
2639                          '  gobject_class->notify       = %s_skeleton_notify;\n'
2640                          '\n'%(i.name_lower, i.name_lower, i.name_lower))
2641             self.c.write('\n'
2642                          '  %s_override_properties (gobject_class, 1);\n'%(i.name_lower))
2643         self.c.write('\n'
2644                      '  skeleton_class = G_DBUS_INTERFACE_SKELETON_CLASS (klass);\n');
2645         self.c.write('  skeleton_class->get_info = %s_skeleton_dbus_interface_get_info;\n'%(i.name_lower))
2646         self.c.write('  skeleton_class->get_properties = %s_skeleton_dbus_interface_get_properties;\n'%(i.name_lower))
2647         self.c.write('  skeleton_class->flush = %s_skeleton_dbus_interface_flush;\n'%(i.name_lower))
2648         self.c.write('  skeleton_class->get_vtable = %s_skeleton_dbus_interface_get_vtable;\n'%(i.name_lower))
2649
2650         self.c.write('\n'
2651                      '#if GLIB_VERSION_MAX_ALLOWED < GLIB_VERSION_2_38\n'
2652                      '  g_type_class_add_private (klass, sizeof (%sSkeletonPrivate));\n'
2653                      '#endif\n'%(i.camel_name))
2654
2655         self.c.write('}\n'
2656                      '\n')
2657
2658         self.c.write('static void\n'
2659                      '%s_skeleton_iface_init (%sIface *iface)\n'
2660                      '{\n'
2661                      %(i.name_lower, i.camel_name))
2662         for s in i.signals:
2663             self.c.write('  iface->%s = _%s_on_signal_%s;\n'
2664                          %(s.name_lower, i.name_lower, s.name_lower))
2665         for p in i.properties:
2666             self.c.write('  iface->get_%s = %s_skeleton_get_%s;\n'%(p.name_lower, i.name_lower, p.name_lower))
2667         self.c.write('}\n'
2668                      '\n')
2669
2670         # constructors
2671         self.c.write(self.docbook_gen.expand(
2672                 '/**\n'
2673                 ' * %s_skeleton_new:\n'
2674                 ' *\n'
2675                 ' * Creates a skeleton object for the D-Bus interface #%s.\n'
2676                 ' *\n'
2677                 ' * Returns: (transfer full) (type %sSkeleton): The skeleton object.\n'
2678                 %(i.name_lower, i.name, i.camel_name), False))
2679         self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
2680         self.c.write('%s *\n'
2681                      '%s_skeleton_new (void)\n'
2682                      '{\n'
2683                      '  return %s%s (g_object_new (%sTYPE_%s_SKELETON, NULL));\n'
2684                      '}\n'
2685                      '\n'%(i.camel_name, i.name_lower, i.ns_upper, i.name_upper, i.ns_upper, i.name_upper))
2686
2687     # ---------------------------------------------------------------------------------------------------
2688
2689     def generate_object(self):
2690         self.c.write('/* ------------------------------------------------------------------------\n'
2691                      ' * Code for Object, ObjectProxy and ObjectSkeleton\n'
2692                      ' * ------------------------------------------------------------------------\n'
2693                      ' */\n'
2694                      '\n')
2695
2696         self.c.write(self.docbook_gen.expand(
2697                 '/**\n'
2698                 ' * SECTION:%sObject\n'
2699                 ' * @title: %sObject\n'
2700                 ' * @short_description: Specialized GDBusObject types\n'
2701                 ' *\n'
2702                 ' * This section contains the #%sObject, #%sObjectProxy, and #%sObjectSkeleton types which make it easier to work with objects implementing generated types for D-Bus interfaces.\n'
2703                 ' */\n'
2704                 %(self.namespace, self.namespace, self.namespace, self.namespace, self.namespace), False))
2705         self.c.write('\n')
2706
2707         self.c.write(self.docbook_gen.expand(
2708                 '/**\n'
2709                 ' * %sObject:\n'
2710                 ' *\n'
2711                 ' * The #%sObject type is a specialized container of interfaces.\n'
2712                 ' */\n'
2713                 %(self.namespace, self.namespace), False))
2714         self.c.write('\n')
2715
2716         self.c.write(self.docbook_gen.expand(
2717                 '/**\n'
2718                 ' * %sObjectIface:\n'
2719                 ' * @parent_iface: The parent interface.\n'
2720                 ' *\n'
2721                 ' * Virtual table for the #%sObject interface.\n'
2722                 ' */\n'
2723                 %(self.namespace, self.namespace), False))
2724         self.c.write('\n')
2725
2726         self.c.write('typedef %sObjectIface %sObjectInterface;\n'%(self.namespace, self.namespace))
2727         self.c.write('G_DEFINE_INTERFACE_WITH_CODE (%sObject, %sobject, G_TYPE_OBJECT, g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_DBUS_OBJECT));\n'%(self.namespace, self.ns_lower))
2728         self.c.write('\n')
2729         self.c.write('static void\n'
2730                      '%sobject_default_init (%sObjectIface *iface)\n'
2731                      '{\n'
2732                      %(self.ns_lower, self.namespace));
2733         for i in self.ifaces:
2734             self.c.write(self.docbook_gen.expand(
2735                     '  /**\n'
2736                     '   * %sObject:%s:\n'
2737                     '   *\n'
2738                     '   * The #%s instance corresponding to the D-Bus interface #%s, if any.\n'
2739                     '   *\n'
2740                     '   * Connect to the #GObject::notify signal to get informed of property changes.\n'
2741                     %(self.namespace, i.name_hyphen, i.camel_name, i.name), False))
2742             self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 2)
2743             self.c.write('  g_object_interface_install_property (iface, g_param_spec_object ("%s", "%s", "%s", %sTYPE_%s, G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS));\n'
2744                          '\n'
2745                          %(i.name_hyphen, i.name_hyphen, i.name_hyphen, self.ns_upper, i.name_upper))
2746         self.c.write('}\n'
2747                      '\n')
2748
2749         for i in self.ifaces:
2750             self.c.write(self.docbook_gen.expand(
2751                     '/**\n'
2752                     ' * %sobject_get_%s:\n'
2753                     ' * @object: A #%sObject.\n'
2754                     ' *\n'
2755                     ' * Gets the #%s instance for the D-Bus interface #%s on @object, if any.\n'
2756                     ' *\n'
2757                     ' * Returns: (transfer full): A #%s that must be freed with g_object_unref() or %%NULL if @object does not implement the interface.\n'
2758                     %(self.ns_lower, i.name_upper.lower(), self.namespace, i.camel_name, i.name, i.camel_name), False))
2759             self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
2760             self.c.write ('%s *%sobject_get_%s (%sObject *object)\n'
2761                           %(i.camel_name, self.ns_lower, i.name_upper.lower(), self.namespace))
2762             self.c.write('{\n'
2763                          '  GDBusInterface *ret;\n'
2764                          '  ret = g_dbus_object_get_interface (G_DBUS_OBJECT (object), "%s");\n'
2765                          '  if (ret == NULL)\n'
2766                          '    return NULL;\n'
2767                          '  return %s%s (ret);\n'
2768                          '}\n'
2769                          '\n'
2770                          %(i.name, self.ns_upper, i.name_upper))
2771         self.c.write('\n')
2772         for i in self.ifaces:
2773             self.c.write(self.docbook_gen.expand(
2774                     '/**\n'
2775                     ' * %sobject_peek_%s: (skip)\n'
2776                     ' * @object: A #%sObject.\n'
2777                     ' *\n'
2778                     ' * Like %sobject_get_%s() but doesn\'t increase the reference count on the returned object.\n'
2779                     ' *\n'
2780                     ' * <warning>It is not safe to use the returned object if you are on another thread than the one where the #GDBusObjectManagerClient or #GDBusObjectManagerServer for @object is running.</warning>\n'
2781                     ' *\n'
2782                     ' * Returns: (transfer none): A #%s or %%NULL if @object does not implement the interface. Do not free the returned object, it is owned by @object.\n'
2783                     %(self.ns_lower, i.name_upper.lower(), self.namespace, self.ns_lower, i.name_upper.lower(), i.camel_name), False))
2784             self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
2785             self.c.write ('%s *%sobject_peek_%s (%sObject *object)\n'
2786                           %(i.camel_name, self.ns_lower, i.name_upper.lower(), self.namespace))
2787             self.c.write('{\n'
2788                          '  GDBusInterface *ret;\n'
2789                          '  ret = g_dbus_object_get_interface (G_DBUS_OBJECT (object), "%s");\n'
2790                          '  if (ret == NULL)\n'
2791                          '    return NULL;\n'
2792                          '  g_object_unref (ret);\n'
2793                          '  return %s%s (ret);\n'
2794                          '}\n'
2795                          '\n'
2796                          %(i.name, self.ns_upper, i.name_upper))
2797         self.c.write('\n')
2798         # shared by ObjectProxy and ObjectSkeleton classes
2799         self.c.write('static void\n'
2800                      '%sobject_notify (GDBusObject *object, GDBusInterface *interface)\n'
2801                      '{\n'
2802                      '  _ExtendedGDBusInterfaceInfo *info = (_ExtendedGDBusInterfaceInfo *) g_dbus_interface_get_info (interface);\n'
2803                      '  /* info can be NULL if the other end is using a D-Bus interface we don\'t know\n'
2804                      '   * anything about, for example old generated code in this process talking to\n'
2805                      '   * newer generated code in the other process. */\n'
2806                      '  if (info != NULL)\n'
2807                      '    g_object_notify (G_OBJECT (object), info->hyphen_name);\n'
2808                      '}\n'
2809                      '\n'
2810                      %(self.ns_lower))
2811
2812         self.c.write(self.docbook_gen.expand(
2813                 '/**\n'
2814                 ' * %sObjectProxy:\n'
2815                 ' *\n'
2816                 ' * The #%sObjectProxy structure contains only private data and should only be accessed using the provided API.\n'
2817                 %(self.namespace, self.namespace), False))
2818         self.c.write(' */\n')
2819         self.c.write('\n')
2820         self.c.write(self.docbook_gen.expand(
2821                 '/**\n'
2822                 ' * %sObjectProxyClass:\n'
2823                 ' * @parent_class: The parent class.\n'
2824                 ' *\n'
2825                 ' * Class structure for #%sObjectProxy.\n'
2826                 %(self.namespace, self.namespace), False))
2827         self.c.write(' */\n')
2828         self.c.write('\n')
2829         # class boilerplate
2830         self.c.write('static void\n'
2831                      '%sobject_proxy__%sobject_iface_init (%sObjectIface *iface G_GNUC_UNUSED)\n'
2832                      '{\n'
2833                      '}\n'
2834                      '\n'
2835                      %(self.ns_lower, self.ns_lower, self.namespace))
2836         self.c.write('static void\n'
2837                      '%sobject_proxy__g_dbus_object_iface_init (GDBusObjectIface *iface)\n'
2838                      '{\n'
2839                      '  iface->interface_added = %sobject_notify;\n'
2840                      '  iface->interface_removed = %sobject_notify;\n'
2841                      '}\n'
2842                      '\n'
2843                      %(self.ns_lower, self.ns_lower, self.ns_lower))
2844         self.c.write('\n')
2845         self.c.write('G_DEFINE_TYPE_WITH_CODE (%sObjectProxy, %sobject_proxy, G_TYPE_DBUS_OBJECT_PROXY,\n'
2846                      '                         G_IMPLEMENT_INTERFACE (%sTYPE_OBJECT, %sobject_proxy__%sobject_iface_init)\n'
2847                      '                         G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_OBJECT, %sobject_proxy__g_dbus_object_iface_init));\n'
2848                      '\n'
2849                      %(self.namespace, self.ns_lower, self.ns_upper, self.ns_lower, self.ns_lower, self.ns_lower))
2850         # class boilerplate
2851         self.c.write('static void\n'
2852                      '%sobject_proxy_init (%sObjectProxy *object G_GNUC_UNUSED)\n'
2853                      '{\n'
2854                      '}\n'
2855                      '\n'%(self.ns_lower, self.namespace))
2856         self.c.write('static void\n'
2857                      '%sobject_proxy_set_property (GObject      *gobject,\n'
2858                      '  guint         prop_id,\n'
2859                      '  const GValue *value G_GNUC_UNUSED,\n'
2860                      '  GParamSpec   *pspec)\n'
2861                      '{\n'
2862                      '  G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);\n'
2863                      %(self.ns_lower))
2864         self.c.write('}\n'
2865                      '\n'%())
2866         self.c.write('static void\n'
2867                      '%sobject_proxy_get_property (GObject      *gobject,\n'
2868                      '  guint         prop_id,\n'
2869                      '  GValue       *value,\n'
2870                      '  GParamSpec   *pspec)\n'
2871                      '{\n'
2872                      '  %sObjectProxy *object = %sOBJECT_PROXY (gobject);\n'
2873                      '  GDBusInterface *interface;\n'
2874                      '\n'
2875                      '  switch (prop_id)\n'
2876                      '    {\n'
2877                      %(self.ns_lower, self.namespace, self.ns_upper))
2878         n = 1
2879         for i in self.ifaces:
2880             self.c.write('    case %d:\n'
2881                          '      interface = g_dbus_object_get_interface (G_DBUS_OBJECT (object), "%s");\n'
2882                          '      g_value_take_object (value, interface);\n'
2883                          '      break;\n'
2884                          '\n'
2885                          %(n, i.name))
2886             n += 1
2887         self.c.write('    default:\n'
2888                      '      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);\n'
2889                      '      break;\n'
2890                      '  }\n'
2891                      '}\n'
2892                      '\n'%())
2893         self.c.write('static void\n'
2894                      '%sobject_proxy_class_init (%sObjectProxyClass *klass)\n'
2895                      '{\n'
2896                      '  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);\n'
2897                      '\n'
2898                      '  gobject_class->set_property = %sobject_proxy_set_property;\n'
2899                      '  gobject_class->get_property = %sobject_proxy_get_property;\n'
2900                      '\n'
2901                      %(self.ns_lower, self.namespace, self.ns_lower, self.ns_lower))
2902         n = 1
2903         for i in self.ifaces:
2904             self.c.write('  g_object_class_override_property (gobject_class, %d, "%s");'
2905                          '\n'
2906                          %(n, i.name_hyphen))
2907             n += 1
2908         self.c.write('}\n'
2909                      '\n')
2910
2911         self.c.write(self.docbook_gen.expand(
2912                 '/**\n'
2913                 ' * %sobject_proxy_new:\n'
2914                 ' * @connection: A #GDBusConnection.\n'
2915                 ' * @object_path: An object path.\n'
2916                 ' *\n'
2917                 ' * Creates a new proxy object.\n'
2918                 ' *\n'
2919                 ' * Returns: (transfer full): The proxy object.\n'
2920                 ' */\n'
2921                 %(self.ns_lower), False))
2922         self.c.write('%sObjectProxy *\n'
2923                      '%sobject_proxy_new (GDBusConnection *connection,\n'
2924                      '  const gchar *object_path)\n'
2925                      '{\n'
2926                      '  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);\n'
2927                      '  g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);\n'
2928                      '  return %sOBJECT_PROXY (g_object_new (%sTYPE_OBJECT_PROXY, "g-connection", connection, "g-object-path", object_path, NULL));\n'
2929                      '}\n'
2930                      '\n'%(self.namespace, self.ns_lower, self.ns_upper, self.ns_upper))
2931
2932         self.c.write(self.docbook_gen.expand(
2933                 '/**\n'
2934                 ' * %sObjectSkeleton:\n'
2935                 ' *\n'
2936                 ' * The #%sObjectSkeleton structure contains only private data and should only be accessed using the provided API.\n'
2937                 %(self.namespace, self.namespace), False))
2938         self.c.write(' */\n')
2939         self.c.write('\n')
2940         self.c.write(self.docbook_gen.expand(
2941                 '/**\n'
2942                 ' * %sObjectSkeletonClass:\n'
2943                 ' * @parent_class: The parent class.\n'
2944                 ' *\n'
2945                 ' * Class structure for #%sObjectSkeleton.\n'
2946                 %(self.namespace, self.namespace), False))
2947         self.c.write(' */\n')
2948         self.c.write('\n')
2949         # class boilerplate
2950         self.c.write('static void\n'
2951                      '%sobject_skeleton__%sobject_iface_init (%sObjectIface *iface G_GNUC_UNUSED)\n'
2952                      '{\n'
2953                      '}\n'
2954                      '\n'
2955                      %(self.ns_lower, self.ns_lower, self.namespace))
2956         self.c.write('\n')
2957         self.c.write('static void\n'
2958                      '%sobject_skeleton__g_dbus_object_iface_init (GDBusObjectIface *iface)\n'
2959                      '{\n'
2960                      '  iface->interface_added = %sobject_notify;\n'
2961                      '  iface->interface_removed = %sobject_notify;\n'
2962                      '}\n'
2963                      '\n'
2964                      %(self.ns_lower, self.ns_lower, self.ns_lower))
2965         self.c.write('G_DEFINE_TYPE_WITH_CODE (%sObjectSkeleton, %sobject_skeleton, G_TYPE_DBUS_OBJECT_SKELETON,\n'
2966                      '                         G_IMPLEMENT_INTERFACE (%sTYPE_OBJECT, %sobject_skeleton__%sobject_iface_init)\n'
2967                      '                         G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_OBJECT, %sobject_skeleton__g_dbus_object_iface_init));\n'
2968                      '\n'
2969                      %(self.namespace, self.ns_lower, self.ns_upper, self.ns_lower, self.ns_lower, self.ns_lower))
2970         # class boilerplate
2971         self.c.write('static void\n'
2972                      '%sobject_skeleton_init (%sObjectSkeleton *object G_GNUC_UNUSED)\n'
2973                      '{\n'
2974                      '}\n'
2975                      '\n'%(self.ns_lower, self.namespace))
2976         self.c.write('static void\n'
2977                      '%sobject_skeleton_set_property (GObject      *gobject,\n'
2978                      '  guint         prop_id,\n'
2979                      '  const GValue *value,\n'
2980                      '  GParamSpec   *pspec)\n'
2981                      '{\n'
2982                      '  %sObjectSkeleton *object = %sOBJECT_SKELETON (gobject);\n'
2983                      '  GDBusInterfaceSkeleton *interface;\n'
2984                      '\n'
2985                      '  switch (prop_id)\n'
2986                      '    {\n'
2987                      %(self.ns_lower, self.namespace, self.ns_upper))
2988         n = 1
2989         for i in self.ifaces:
2990             self.c.write('    case %d:\n'
2991                          '      interface = g_value_get_object (value);\n'
2992                          '      if (interface != NULL)\n'
2993                          '        {\n'
2994                          '          g_warn_if_fail (%sIS_%s (interface));\n'
2995                          '          g_dbus_object_skeleton_add_interface (G_DBUS_OBJECT_SKELETON (object), interface);\n'
2996                          '        }\n'
2997                          '      else\n'
2998                          '        {\n'
2999                          '          g_dbus_object_skeleton_remove_interface_by_name (G_DBUS_OBJECT_SKELETON (object), "%s");\n'
3000                          '        }\n'
3001                          '      break;\n'
3002                          '\n'
3003                          %(n, self.ns_upper, i.name_upper, i.name))
3004             n += 1
3005         self.c.write('    default:\n'
3006                      '      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);\n'
3007                      '      break;\n'
3008                      '  }\n'
3009                      '}\n'
3010                      '\n'%())
3011         self.c.write('static void\n'
3012                      '%sobject_skeleton_get_property (GObject      *gobject,\n'
3013                      '  guint         prop_id,\n'
3014                      '  GValue       *value,\n'
3015                      '  GParamSpec   *pspec)\n'
3016                      '{\n'
3017                      '  %sObjectSkeleton *object = %sOBJECT_SKELETON (gobject);\n'
3018                      '  GDBusInterface *interface;\n'
3019                      '\n'
3020                      '  switch (prop_id)\n'
3021                      '    {\n'
3022                      %(self.ns_lower, self.namespace, self.ns_upper))
3023         n = 1
3024         for i in self.ifaces:
3025             self.c.write('    case %d:\n'
3026                          '      interface = g_dbus_object_get_interface (G_DBUS_OBJECT (object), "%s");\n'
3027                          '      g_value_take_object (value, interface);\n'
3028                          '      break;\n'
3029                          '\n'
3030                          %(n, i.name))
3031             n += 1
3032         self.c.write('    default:\n'
3033                      '      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);\n'
3034                      '      break;\n'
3035                      '  }\n'
3036                      '}\n'
3037                      '\n'%())
3038         self.c.write('static void\n'
3039                      '%sobject_skeleton_class_init (%sObjectSkeletonClass *klass)\n'
3040                      '{\n'
3041                      '  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);\n'
3042                      '\n'
3043                      '  gobject_class->set_property = %sobject_skeleton_set_property;\n'
3044                      '  gobject_class->get_property = %sobject_skeleton_get_property;\n'
3045                      '\n'
3046                      %(self.ns_lower, self.namespace, self.ns_lower, self.ns_lower))
3047         n = 1
3048         for i in self.ifaces:
3049             self.c.write('  g_object_class_override_property (gobject_class, %d, "%s");'
3050                          '\n'
3051                          %(n, i.name_hyphen))
3052             n += 1
3053         self.c.write('}\n'
3054                      '\n')
3055         self.c.write(self.docbook_gen.expand(
3056                 '/**\n'
3057                 ' * %sobject_skeleton_new:\n'
3058                 ' * @object_path: An object path.\n'
3059                 ' *\n'
3060                 ' * Creates a new skeleton object.\n'
3061                 ' *\n'
3062                 ' * Returns: (transfer full): The skeleton object.\n'
3063                 ' */\n'
3064                 %(self.ns_lower), False))
3065         self.c.write('%sObjectSkeleton *\n'
3066                      '%sobject_skeleton_new (const gchar *object_path)\n'
3067                      '{\n'
3068                      '  g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);\n'
3069                      '  return %sOBJECT_SKELETON (g_object_new (%sTYPE_OBJECT_SKELETON, "g-object-path", object_path, NULL));\n'
3070                      '}\n'
3071                      '\n'%(self.namespace, self.ns_lower, self.ns_upper, self.ns_upper))
3072         for i in self.ifaces:
3073             self.c.write(self.docbook_gen.expand(
3074                     '/**\n'
3075                     ' * %sobject_skeleton_set_%s:\n'
3076                     ' * @object: A #%sObjectSkeleton.\n'
3077                     ' * @interface_: (allow-none): A #%s or %%NULL to clear the interface.\n'
3078                     ' *\n'
3079                     ' * Sets the #%s instance for the D-Bus interface #%s on @object.\n'
3080                     %(self.ns_lower, i.name_upper.lower(), self.namespace, i.camel_name, i.camel_name, i.name), False))
3081             self.write_gtkdoc_deprecated_and_since_and_close(i, self.c, 0)
3082             self.c.write ('void %sobject_skeleton_set_%s (%sObjectSkeleton *object, %s *interface_)\n'
3083                           %(self.ns_lower, i.name_upper.lower(), self.namespace, i.camel_name))
3084             self.c.write('{\n'
3085                          '  g_object_set (G_OBJECT (object), "%s", interface_, NULL);\n'
3086                          '}\n'
3087                          '\n'
3088                          %(i.name_hyphen))
3089         self.c.write('\n')
3090
3091
3092     def generate_object_manager_client(self):
3093         self.c.write('/* ------------------------------------------------------------------------\n'
3094                      ' * Code for ObjectManager client\n'
3095                      ' * ------------------------------------------------------------------------\n'
3096                      ' */\n'
3097                      '\n')
3098
3099         self.c.write(self.docbook_gen.expand(
3100                 '/**\n'
3101                 ' * SECTION:%sObjectManagerClient\n'
3102                 ' * @title: %sObjectManagerClient\n'
3103                 ' * @short_description: Generated GDBusObjectManagerClient type\n'
3104                 ' *\n'
3105                 ' * This section contains a #GDBusObjectManagerClient that uses %sobject_manager_client_get_proxy_type() as the #GDBusProxyTypeFunc.\n'
3106                 ' */\n'
3107                 %(self.namespace, self.namespace, self.ns_lower), False))
3108         self.c.write('\n')
3109
3110         self.c.write(self.docbook_gen.expand(
3111                 '/**\n'
3112                 ' * %sObjectManagerClient:\n'
3113                 ' *\n'
3114                 ' * The #%sObjectManagerClient structure contains only private data and should only be accessed using the provided API.\n'
3115                 %(self.namespace, self.namespace), False))
3116         self.c.write(' */\n')
3117         self.c.write('\n')
3118
3119         self.c.write(self.docbook_gen.expand(
3120                 '/**\n'
3121                 ' * %sObjectManagerClientClass:\n'
3122                 ' * @parent_class: The parent class.\n'
3123                 ' *\n'
3124                 ' * Class structure for #%sObjectManagerClient.\n'
3125                 %(self.namespace, self.namespace), False))
3126         self.c.write(' */\n')
3127         self.c.write('\n')
3128
3129         # class boilerplate
3130         self.c.write('G_DEFINE_TYPE (%sObjectManagerClient, %sobject_manager_client, G_TYPE_DBUS_OBJECT_MANAGER_CLIENT);\n'
3131                      '\n'
3132                      %(self.namespace, self.ns_lower))
3133
3134         # class boilerplate
3135         self.c.write('static void\n'
3136                      '%sobject_manager_client_init (%sObjectManagerClient *manager G_GNUC_UNUSED)\n'
3137                      '{\n'
3138                      '}\n'
3139                      '\n'%(self.ns_lower, self.namespace))
3140         self.c.write('static void\n'
3141                      '%sobject_manager_client_class_init (%sObjectManagerClientClass *klass G_GNUC_UNUSED)\n'
3142                      '{\n'
3143                      '}\n'
3144                      '\n'%(self.ns_lower, self.namespace))
3145
3146         self.c.write(self.docbook_gen.expand(
3147                 '/**\n'
3148                 ' * %sobject_manager_client_get_proxy_type:\n'
3149                 ' * @manager: A #GDBusObjectManagerClient.\n'
3150                 ' * @object_path: The object path of the remote object (unused).\n'
3151                 ' * @interface_name: (allow-none): Interface name of the remote object or %%NULL to get the object proxy #GType.\n'
3152                 ' * @user_data: User data (unused).\n'
3153                 ' *\n'
3154                 ' * A #GDBusProxyTypeFunc that maps @interface_name to the generated #GDBusObjectProxy<!-- -->- and #GDBusProxy<!-- -->-derived types.\n'
3155                 ' *\n'
3156                 ' * Returns: A #GDBusProxy<!-- -->-derived #GType if @interface_name is not %%NULL, otherwise the #GType for #%sObjectProxy.\n'
3157                 %(self.ns_lower, self.namespace), False))
3158         self.c.write(' */\n')
3159         self.c.write('GType\n'
3160                      '%sobject_manager_client_get_proxy_type (GDBusObjectManagerClient *manager G_GNUC_UNUSED, const gchar *object_path G_GNUC_UNUSED, const gchar *interface_name, gpointer user_data G_GNUC_UNUSED)\n'
3161                      '{\n'
3162                      %(self.ns_lower))
3163         self.c.write('  static gsize once_init_value = 0;\n'
3164                      '  static GHashTable *lookup_hash;\n'
3165                      '  GType ret;\n'
3166                      '\n'
3167                      '  if (interface_name == NULL)\n'
3168                      '    return %sTYPE_OBJECT_PROXY;\n'
3169                      '  if (g_once_init_enter (&once_init_value))\n'
3170                      '    {\n'
3171                      '      lookup_hash = g_hash_table_new (g_str_hash, g_str_equal);\n'
3172                      %(self.ns_upper))
3173         for i in self.ifaces:
3174             self.c.write('      g_hash_table_insert (lookup_hash, (gpointer) "%s", GSIZE_TO_POINTER (%sTYPE_%s_PROXY));\n'
3175                          %(i.name, i.ns_upper, i.name_upper))
3176         self.c.write('      g_once_init_leave (&once_init_value, 1);\n'
3177                      '    }\n')
3178         self.c.write('  ret = (GType) GPOINTER_TO_SIZE (g_hash_table_lookup (lookup_hash, interface_name));\n'
3179                      '  if (ret == (GType) 0)\n'
3180                      '    ret = G_TYPE_DBUS_PROXY;\n')
3181         self.c.write('  return ret;\n'
3182                      '}\n'
3183                      '\n')
3184
3185         # constructors
3186         self.c.write(self.docbook_gen.expand(
3187                 '/**\n'
3188                 ' * %sobject_manager_client_new:\n'
3189                 ' * @connection: A #GDBusConnection.\n'
3190                 ' * @flags: Flags from the #GDBusObjectManagerClientFlags enumeration.\n'
3191                 ' * @name: (allow-none): A bus name (well-known or unique) or %%NULL if @connection is not a message bus connection.\n'
3192                 ' * @object_path: An object path.\n'
3193                 ' * @cancellable: (allow-none): A #GCancellable or %%NULL.\n'
3194                 ' * @callback: A #GAsyncReadyCallback to call when the request is satisfied.\n'
3195                 ' * @user_data: User data to pass to @callback.\n'
3196                 ' *\n'
3197                 ' * Asynchronously creates #GDBusObjectManagerClient using %sobject_manager_client_get_proxy_type() as the #GDBusProxyTypeFunc. See g_dbus_object_manager_client_new() for more details.\n'
3198                 ' *\n'
3199                 ' * When the operation is finished, @callback will be invoked in the <link linkend="g-main-context-push-thread-default">thread-default main loop</link> of the thread you are calling this method from.\n'
3200                 ' * You can then call %sobject_manager_client_new_finish() to get the result of the operation.\n'
3201                 ' *\n'
3202                 ' * See %sobject_manager_client_new_sync() for the synchronous, blocking version of this constructor.\n'
3203                 %(self.ns_lower, self.ns_lower, self.ns_lower, self.ns_lower), False))
3204         self.c.write(' */\n')
3205         self.c.write('void\n'
3206                      '%sobject_manager_client_new (\n'
3207                      '    GDBusConnection        *connection,\n'
3208                      '    GDBusObjectManagerClientFlags  flags,\n'
3209                      '    const gchar            *name,\n'
3210                      '    const gchar            *object_path,\n'
3211                      '    GCancellable           *cancellable,\n'
3212                      '    GAsyncReadyCallback     callback,\n'
3213                      '    gpointer                user_data)\n'
3214                      '{\n'
3215                      '  g_async_initable_new_async (%sTYPE_OBJECT_MANAGER_CLIENT, G_PRIORITY_DEFAULT, cancellable, callback, user_data, "flags", flags, "name", name, "connection", connection, "object-path", object_path, "get-proxy-type-func", %sobject_manager_client_get_proxy_type, NULL);\n'
3216                      '}\n'
3217                      '\n'
3218                      %(self.ns_lower, self.ns_upper, self.ns_lower))
3219         self.c.write('/**\n'
3220                      ' * %sobject_manager_client_new_finish:\n'
3221                      ' * @res: The #GAsyncResult obtained from the #GAsyncReadyCallback passed to %sobject_manager_client_new().\n'
3222                      ' * @error: Return location for error or %%NULL\n'
3223                      ' *\n'
3224                      ' * Finishes an operation started with %sobject_manager_client_new().\n'
3225                      ' *\n'
3226                      ' * Returns: (transfer full) (type %sObjectManagerClient): The constructed object manager client or %%NULL if @error is set.\n'
3227                      %(self.ns_lower, self.ns_lower, self.ns_lower, self.namespace))
3228         self.c.write(' */\n')
3229         self.c.write('GDBusObjectManager *\n'
3230                      '%sobject_manager_client_new_finish (\n'
3231                      '    GAsyncResult        *res,\n'
3232                      '    GError             **error)\n'
3233                      '{\n'
3234                      '  GObject *ret;\n'
3235                      '  GObject *source_object;\n'
3236                      '  source_object = g_async_result_get_source_object (res);\n'
3237                      '  ret = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object), res, error);\n'
3238                      '  g_object_unref (source_object);\n'
3239                      '  if (ret != NULL)\n'
3240                      '    return G_DBUS_OBJECT_MANAGER (ret);\n'
3241                      '  else\n'
3242                      '    return NULL;\n'
3243                      '}\n'
3244                      '\n'
3245                      %(self.ns_lower))
3246         self.c.write(self.docbook_gen.expand(
3247                 '/**\n'
3248                 ' * %sobject_manager_client_new_sync:\n'
3249                 ' * @connection: A #GDBusConnection.\n'
3250                 ' * @flags: Flags from the #GDBusObjectManagerClientFlags enumeration.\n'
3251                 ' * @name: (allow-none): A bus name (well-known or unique) or %%NULL if @connection is not a message bus connection.\n'
3252                 ' * @object_path: An object path.\n'
3253                 ' * @cancellable: (allow-none): A #GCancellable or %%NULL.\n'
3254                 ' * @error: Return location for error or %%NULL\n'
3255                 ' *\n'
3256                 ' * Synchronously creates #GDBusObjectManagerClient using %sobject_manager_client_get_proxy_type() as the #GDBusProxyTypeFunc. See g_dbus_object_manager_client_new_sync() for more details.\n'
3257                 ' *\n'
3258                 ' * The calling thread is blocked until a reply is received.\n'
3259                 ' *\n'
3260                 ' * See %sobject_manager_client_new() for the asynchronous version of this constructor.\n'
3261                 ' *\n'
3262                 ' * Returns: (transfer full) (type %sObjectManagerClient): The constructed object manager client or %%NULL if @error is set.\n'
3263                 %(self.ns_lower, self.ns_lower, self.ns_lower, self.namespace), False))
3264         self.c.write(' */\n')
3265         self.c.write('GDBusObjectManager *\n'
3266                      '%sobject_manager_client_new_sync (\n'
3267                      '    GDBusConnection        *connection,\n'
3268                      '    GDBusObjectManagerClientFlags  flags,\n'
3269                      '    const gchar            *name,\n'
3270                      '    const gchar            *object_path,\n'
3271                      '    GCancellable           *cancellable,\n'
3272                      '    GError                **error)\n'
3273                      '{\n'
3274                      '  GInitable *ret;\n'
3275                      '  ret = g_initable_new (%sTYPE_OBJECT_MANAGER_CLIENT, cancellable, error, "flags", flags, "name", name, "connection", connection, "object-path", object_path, "get-proxy-type-func", %sobject_manager_client_get_proxy_type, NULL);\n'
3276                      '  if (ret != NULL)\n'
3277                      '    return G_DBUS_OBJECT_MANAGER (ret);\n'
3278                      '  else\n'
3279                      '    return NULL;\n'
3280                      '}\n'
3281                      '\n'
3282                      %(self.ns_lower, self.ns_upper, self.ns_lower))
3283         self.c.write('\n')
3284         self.c.write(self.docbook_gen.expand(
3285                 '/**\n'
3286                 ' * %sobject_manager_client_new_for_bus:\n'
3287                 ' * @bus_type: A #GBusType.\n'
3288                 ' * @flags: Flags from the #GDBusObjectManagerClientFlags enumeration.\n'
3289                 ' * @name: A bus name (well-known or unique).\n'
3290                 ' * @object_path: An object path.\n'
3291                 ' * @cancellable: (allow-none): A #GCancellable or %%NULL.\n'
3292                 ' * @callback: A #GAsyncReadyCallback to call when the request is satisfied.\n'
3293                 ' * @user_data: User data to pass to @callback.\n'
3294                 ' *\n'
3295                 ' * Like %sobject_manager_client_new() but takes a #GBusType instead of a #GDBusConnection.\n'
3296                 ' *\n'
3297                 ' * When the operation is finished, @callback will be invoked in the <link linkend="g-main-context-push-thread-default">thread-default main loop</link> of the thread you are calling this method from.\n'
3298                 ' * You can then call %sobject_manager_client_new_for_bus_finish() to get the result of the operation.\n'
3299                 ' *\n'
3300                 ' * See %sobject_manager_client_new_for_bus_sync() for the synchronous, blocking version of this constructor.\n'
3301                 %(self.ns_lower, self.ns_lower, self.ns_lower, self.ns_lower), False))
3302         self.c.write(' */\n')
3303         self.c.write('void\n'
3304                      '%sobject_manager_client_new_for_bus (\n'
3305                      '    GBusType                bus_type,\n'
3306                      '    GDBusObjectManagerClientFlags  flags,\n'
3307                      '    const gchar            *name,\n'
3308                      '    const gchar            *object_path,\n'
3309                      '    GCancellable           *cancellable,\n'
3310                      '    GAsyncReadyCallback     callback,\n'
3311                      '    gpointer                user_data)\n'
3312                      '{\n'
3313                      '  g_async_initable_new_async (%sTYPE_OBJECT_MANAGER_CLIENT, G_PRIORITY_DEFAULT, cancellable, callback, user_data, "flags", flags, "name", name, "bus-type", bus_type, "object-path", object_path, "get-proxy-type-func", %sobject_manager_client_get_proxy_type, NULL);\n'
3314                      '}\n'
3315                      '\n'
3316                      %(self.ns_lower, self.ns_upper, self.ns_lower))
3317         self.c.write('/**\n'
3318                      ' * %sobject_manager_client_new_for_bus_finish:\n'
3319                      ' * @res: The #GAsyncResult obtained from the #GAsyncReadyCallback passed to %sobject_manager_client_new_for_bus().\n'
3320                      ' * @error: Return location for error or %%NULL\n'
3321                      ' *\n'
3322                      ' * Finishes an operation started with %sobject_manager_client_new_for_bus().\n'
3323                      ' *\n'
3324                      ' * Returns: (transfer full) (type %sObjectManagerClient): The constructed object manager client or %%NULL if @error is set.\n'
3325                      %(self.ns_lower, self.ns_lower, self.ns_lower, self.namespace))
3326         self.c.write(' */\n')
3327         self.c.write('GDBusObjectManager *\n'
3328                      '%sobject_manager_client_new_for_bus_finish (\n'
3329                      '    GAsyncResult        *res,\n'
3330                      '    GError             **error)\n'
3331                      '{\n'
3332                      '  GObject *ret;\n'
3333                      '  GObject *source_object;\n'
3334                      '  source_object = g_async_result_get_source_object (res);\n'
3335                      '  ret = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object), res, error);\n'
3336                      '  g_object_unref (source_object);\n'
3337                      '  if (ret != NULL)\n'
3338                      '    return G_DBUS_OBJECT_MANAGER (ret);\n'
3339                      '  else\n'
3340                      '    return NULL;\n'
3341                      '}\n'
3342                      '\n'
3343                      %(self.ns_lower))
3344         self.c.write(self.docbook_gen.expand(
3345                 '/**\n'
3346                 ' * %sobject_manager_client_new_for_bus_sync:\n'
3347                 ' * @bus_type: A #GBusType.\n'
3348                 ' * @flags: Flags from the #GDBusObjectManagerClientFlags enumeration.\n'
3349                 ' * @name: A bus name (well-known or unique).\n'
3350                 ' * @object_path: An object path.\n'
3351                 ' * @cancellable: (allow-none): A #GCancellable or %%NULL.\n'
3352                 ' * @error: Return location for error or %%NULL\n'
3353                 ' *\n'
3354                 ' * Like %sobject_manager_client_new_sync() but takes a #GBusType instead of a #GDBusConnection.\n'
3355                 ' *\n'
3356                 ' * The calling thread is blocked until a reply is received.\n'
3357                 ' *\n'
3358                 ' * See %sobject_manager_client_new_for_bus() for the asynchronous version of this constructor.\n'
3359                 ' *\n'
3360                 ' * Returns: (transfer full) (type %sObjectManagerClient): The constructed object manager client or %%NULL if @error is set.\n'
3361                 %(self.ns_lower, self.ns_lower, self.ns_lower, self.namespace), False))
3362         self.c.write(' */\n')
3363         self.c.write('GDBusObjectManager *\n'
3364                      '%sobject_manager_client_new_for_bus_sync (\n'
3365                      '    GBusType                bus_type,\n'
3366                      '    GDBusObjectManagerClientFlags  flags,\n'
3367                      '    const gchar            *name,\n'
3368                      '    const gchar            *object_path,\n'
3369                      '    GCancellable           *cancellable,\n'
3370                      '    GError                **error)\n'
3371                      '{\n'
3372                      '  GInitable *ret;\n'
3373                      '  ret = g_initable_new (%sTYPE_OBJECT_MANAGER_CLIENT, cancellable, error, "flags", flags, "name", name, "bus-type", bus_type, "object-path", object_path, "get-proxy-type-func", %sobject_manager_client_get_proxy_type, NULL);\n'
3374                      '  if (ret != NULL)\n'
3375                      '    return G_DBUS_OBJECT_MANAGER (ret);\n'
3376                      '  else\n'
3377                      '    return NULL;\n'
3378                      '}\n'
3379                      '\n'
3380                      %(self.ns_lower, self.ns_upper, self.ns_lower))
3381         self.c.write('\n')
3382
3383     # ---------------------------------------------------------------------------------------------------
3384
3385     def write_gtkdoc_deprecated_and_since_and_close(self, obj, f, indent):
3386         if len(obj.since) > 0:
3387             f.write('%*s *\n'
3388                     '%*s * Since: %s\n'
3389                     %(indent, '', indent, '', obj.since))
3390         if obj.deprecated:
3391             if isinstance(obj, dbustypes.Interface):
3392                 thing = 'The D-Bus interface'
3393             elif isinstance(obj, dbustypes.Method):
3394                 thing = 'The D-Bus method'
3395             elif isinstance(obj, dbustypes.Signal):
3396                 thing = 'The D-Bus signal'
3397             elif isinstance(obj, dbustypes.Property):
3398                 thing = 'The D-Bus property'
3399             else:
3400                 raise RuntimeError('Cannot handle object ', obj)
3401             f.write(self.docbook_gen.expand(
3402                     '%*s *\n'
3403                     '%*s * Deprecated: %s has been deprecated.\n'
3404                     %(indent, '', indent, '', thing), False))
3405         f.write('%*s */\n'%(indent, ''))
3406
3407     # ---------------------------------------------------------------------------------------------------
3408
3409     def generate_interface_intro(self, i):
3410         self.c.write('/* ------------------------------------------------------------------------\n'
3411                      ' * Code for interface %s\n'
3412                      ' * ------------------------------------------------------------------------\n'
3413                      ' */\n'
3414                      '\n'%(i.name))
3415
3416         self.c.write(self.docbook_gen.expand(
3417                 '/**\n'
3418                 ' * SECTION:%s\n'
3419                 ' * @title: %s\n'
3420                 ' * @short_description: Generated C code for the %s D-Bus interface\n'
3421                 ' *\n'
3422                 ' * This section contains code for working with the #%s D-Bus interface in C.\n'
3423                 ' */\n'
3424                 %(i.camel_name, i.camel_name, i.name, i.name), False))
3425         self.c.write('\n')
3426
3427     def generate(self):
3428         self.generate_intro()
3429         self.declare_types()
3430         for i in self.ifaces:
3431             self.generate_interface_intro(i)
3432             self.generate_introspection_for_interface(i)
3433             self.generate_interface(i)
3434             self.generate_property_accessors(i)
3435             self.generate_signal_emitters(i)
3436             self.generate_method_calls(i)
3437             self.generate_method_completers(i)
3438             self.generate_proxy(i)
3439             self.generate_skeleton(i)
3440         if self.generate_objmanager:
3441             self.generate_object()
3442             self.generate_object_manager_client()
3443
3444         self.generate_outro()