4 # Copyright (c) 2014 Wenchao Xia
7 # Wenchao Xia <wenchaoqemu@gmail.com>
9 # This work is licensed under the terms of the GNU GPL, version 2.
10 # See the COPYING file in the top-level directory.
12 from ordereddict
import OrderedDict
19 def _generate_event_api_name(event_name
, params
):
20 api_name
= "void qapi_event_send_%s(" % c_fun(event_name
).lower();
24 for argname
, argentry
, optional
, structured
in parse_args(params
):
26 api_name
+= "bool has_%s,\n" % c_var(argname
)
27 api_name
+= "".ljust(l
)
29 api_name
+= "%s %s,\n" % (c_type(argentry
, is_param
=True),
31 api_name
+= "".ljust(l
)
33 api_name
+= "Error **errp)"
37 # Following are the core functions that generate C APIs to emit event.
39 def generate_event_declaration(api_name
):
46 def generate_event_implement(api_name
, event_name
, params
):
47 # step 1: declare any variables
53 Error *local_err = NULL;
54 QMPEventFuncEmit emit;
60 QmpOutputVisitor *qov;
66 # step 2: check emit function, create a dict
68 emit = qmp_event_get_func_emit();
73 qmp = qmp_event_build_dict("%(event_name)s");
76 event_name
= event_name
)
78 # step 3: visit the params if params != None
81 qov = qmp_output_visitor_new();
84 v = qmp_output_get_visitor(qov);
87 /* Fake visit, as if all members are under a structure */
88 visit_start_struct(v, NULL, "", "%(event_name)s", 0, &local_err);
94 event_name
= event_name
)
96 for argname
, argentry
, optional
, structured
in parse_args(params
):
101 var
= c_var(argname
))
104 if argentry
== "str":
105 var_type
= "(char **)"
110 visit_type_%(type)s(v, %(var_type)s&%(var)s, "%(name)s", &local_err);
116 var
= c_var(argname
),
117 type = type_name(argentry
),
128 visit_end_struct(v, &local_err);
133 obj = qmp_output_get_qobject(qov);
134 g_assert(obj != NULL);
136 qdict_put_obj(qmp, "data", obj);
139 # step 4: call qmp event api
141 emit(%(event_enum_value)s, qmp, &local_err);
144 event_enum_value
= event_enum_value
)
150 qmp_output_visitor_cleanup(qov);
153 error_propagate(errp, local_err);
161 # Following are the functions that generate an enum type for all defined
162 # events, similar to qapi-types.py. Here we already have enum name and
163 # values which were generated before and recorded in event_enum_*. It also
164 # works around the issue that "import qapi-types" can't work.
166 def generate_event_enum_decl(event_enum_name
, event_enum_values
):
167 lookup_decl
= mcgen('''
169 extern const char *%(event_enum_name)s_lookup[];
171 event_enum_name
= event_enum_name
)
173 enum_decl
= mcgen('''
174 typedef enum %(event_enum_name)s
177 event_enum_name
= event_enum_name
)
179 # append automatically generated _MAX value
180 enum_max_value
= generate_enum_full_value(event_enum_name
, "MAX")
181 enum_values
= event_enum_values
+ [ enum_max_value
]
184 for value
in enum_values
:
185 enum_decl
+= mcgen('''
192 enum_decl
+= mcgen('''
193 } %(event_enum_name)s;
195 event_enum_name
= event_enum_name
)
197 return lookup_decl
+ enum_decl
199 def generate_event_enum_lookup(event_enum_name
, event_enum_strings
):
202 const char *%(event_enum_name)s_lookup[] = {
204 event_enum_name
= event_enum_name
)
207 for string
in event_enum_strings
:
223 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chbp:i:o:",
224 ["source", "header", "builtins", "prefix=",
225 "input-file=", "output-dir="])
226 except getopt
.GetoptError
, err
:
233 c_file
= 'qapi-event.c'
234 h_file
= 'qapi-event.h'
241 if o
in ("-p", "--prefix"):
243 elif o
in ("-i", "--input-file"):
245 elif o
in ("-o", "--output-dir"):
247 elif o
in ("-c", "--source"):
249 elif o
in ("-h", "--header"):
251 elif o
in ("-b", "--builtins"):
254 if not do_c
and not do_h
:
258 c_file
= output_dir
+ prefix
+ c_file
259 h_file
= output_dir
+ prefix
+ h_file
262 os
.makedirs(output_dir
)
264 if e
.errno
!= errno
.EEXIST
:
267 def maybe_open(really
, name
, opt
):
269 return open(name
, opt
)
272 return StringIO
.StringIO()
274 fdef
= maybe_open(do_c
, c_file
, 'w')
275 fdecl
= maybe_open(do_h
, h_file
, 'w')
278 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
281 * schema-defined QAPI event functions
283 * Copyright (c) 2014 Wenchao Xia
286 * Wenchao Xia <wenchaoqemu@gmail.com>
288 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
289 * See the COPYING.LIB file in the top-level directory.
293 #include "qemu-common.h"
294 #include "%(header)s"
295 #include "%(prefix)sqapi-visit.h"
296 #include "qapi/qmp-output-visitor.h"
297 #include "qapi/qmp-event.h"
300 prefix
=prefix
, header
=basename(h_file
)))
302 fdecl
.write(mcgen('''
303 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
306 * schema-defined QAPI event functions
308 * Copyright (c) 2014 Wenchao Xia
311 * Wenchao Xia <wenchaoqemu@gmail.com>
313 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
314 * See the COPYING.LIB file in the top-level directory.
321 #include "qapi/error.h"
322 #include "qapi/qmp/qdict.h"
323 #include "%(prefix)sqapi-types.h"
326 prefix
=prefix
, guard
=guardname(h_file
)))
328 exprs
= parse_schema(input_file
)
330 event_enum_name
= prefix
.upper().replace('-', '_') + "QAPIEvent"
331 event_enum_values
= []
332 event_enum_strings
= []
335 if expr
.has_key('event'):
336 event_name
= expr
['event']
337 params
= expr
.get('data')
338 if params
and len(params
) == 0:
341 api_name
= _generate_event_api_name(event_name
, params
)
342 ret
= generate_event_declaration(api_name
)
345 # We need an enum value per event
346 event_enum_value
= generate_enum_full_value(event_enum_name
,
348 ret
= generate_event_implement(api_name
, event_name
, params
)
351 # Record it, and generate enum later
352 event_enum_values
.append(event_enum_value
)
353 event_enum_strings
.append(event_name
)
355 ret
= generate_event_enum_decl(event_enum_name
, event_enum_values
)
357 ret
= generate_event_enum_lookup(event_enum_name
, event_enum_strings
)