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
15 def _generate_event_api_name(event_name
, params
):
16 api_name
= "void qapi_event_send_%s(" % c_name(event_name
).lower();
20 for argname
, argentry
, optional
in parse_args(params
):
22 api_name
+= "bool has_%s,\n" % c_name(argname
)
23 api_name
+= "".ljust(l
)
25 api_name
+= "%s %s,\n" % (c_type(argentry
, is_param
=True),
27 api_name
+= "".ljust(l
)
29 api_name
+= "Error **errp)"
33 # Following are the core functions that generate C APIs to emit event.
35 def generate_event_declaration(api_name
):
42 def generate_event_implement(api_name
, event_name
, params
):
43 # step 1: declare any variables
49 Error *local_err = NULL;
50 QMPEventFuncEmit emit;
56 QmpOutputVisitor *qov;
62 # step 2: check emit function, create a dict
64 emit = qmp_event_get_func_emit();
69 qmp = qmp_event_build_dict("%(event_name)s");
72 event_name
= event_name
)
74 # step 3: visit the params if params != None
77 qov = qmp_output_visitor_new();
80 v = qmp_output_get_visitor(qov);
83 /* Fake visit, as if all members are under a structure */
84 visit_start_struct(v, NULL, "", "%(event_name)s", 0, &local_err);
90 event_name
= event_name
)
92 for argname
, argentry
, optional
in parse_args(params
):
97 var
= c_name(argname
))
100 if argentry
== "str":
101 var_type
= "(char **)"
106 visit_type_%(type)s(v, %(var_type)s&%(var)s, "%(name)s", &local_err);
112 var
= c_name(argname
),
113 type = type_name(argentry
),
124 visit_end_struct(v, &local_err);
129 obj = qmp_output_get_qobject(qov);
130 g_assert(obj != NULL);
132 qdict_put_obj(qmp, "data", obj);
135 # step 4: call qmp event api
137 emit(%(event_enum_value)s, qmp, &local_err);
140 event_enum_value
= event_enum_value
)
146 qmp_output_visitor_cleanup(qov);
149 error_propagate(errp, local_err);
157 # Following are the functions that generate an enum type for all defined
158 # events, similar to qapi-types.py. Here we already have enum name and
159 # values which were generated before and recorded in event_enum_*. It also
160 # works around the issue that "import qapi-types" can't work.
162 def generate_event_enum_decl(event_enum_name
, event_enum_values
):
163 lookup_decl
= mcgen('''
165 extern const char *%(event_enum_name)s_lookup[];
167 event_enum_name
= event_enum_name
)
169 enum_decl
= mcgen('''
170 typedef enum %(event_enum_name)s
173 event_enum_name
= event_enum_name
)
175 # append automatically generated _MAX value
176 enum_max_value
= c_enum_const(event_enum_name
, "MAX")
177 enum_values
= event_enum_values
+ [ enum_max_value
]
180 for value
in enum_values
:
181 enum_decl
+= mcgen('''
188 enum_decl
+= mcgen('''
189 } %(event_enum_name)s;
191 event_enum_name
= event_enum_name
)
193 return lookup_decl
+ enum_decl
195 def generate_event_enum_lookup(event_enum_name
, event_enum_strings
):
198 const char *%(event_enum_name)s_lookup[] = {
200 event_enum_name
= event_enum_name
)
203 for string
in event_enum_strings
:
215 (input_file
, output_dir
, do_c
, do_h
, prefix
, dummy
) = parse_command_line()
219 * schema-defined QAPI event functions
221 * Copyright (c) 2014 Wenchao Xia
224 * Wenchao Xia <wenchaoqemu@gmail.com>
226 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
227 * See the COPYING.LIB file in the top-level directory.
233 * schema-defined QAPI event functions
235 * Copyright (c) 2014 Wenchao Xia
238 * Wenchao Xia <wenchaoqemu@gmail.com>
240 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
241 * See the COPYING.LIB file in the top-level directory.
246 (fdef
, fdecl
) = open_output(output_dir
, do_c
, do_h
, prefix
,
247 'qapi-event.c', 'qapi-event.h',
248 c_comment
, h_comment
)
251 #include "qemu-common.h"
252 #include "%(prefix)sqapi-event.h"
253 #include "%(prefix)sqapi-visit.h"
254 #include "qapi/qmp-output-visitor.h"
255 #include "qapi/qmp-event.h"
260 fdecl
.write(mcgen('''
261 #include "qapi/error.h"
262 #include "qapi/qmp/qdict.h"
263 #include "%(prefix)sqapi-types.h"
268 exprs
= parse_schema(input_file
)
270 event_enum_name
= prefix
.upper().replace('-', '_') + "QAPIEvent"
271 event_enum_values
= []
272 event_enum_strings
= []
275 if expr
.has_key('event'):
276 event_name
= expr
['event']
277 params
= expr
.get('data')
278 if params
and len(params
) == 0:
281 api_name
= _generate_event_api_name(event_name
, params
)
282 ret
= generate_event_declaration(api_name
)
285 # We need an enum value per event
286 event_enum_value
= c_enum_const(event_enum_name
, event_name
)
287 ret
= generate_event_implement(api_name
, event_name
, params
)
290 # Record it, and generate enum later
291 event_enum_values
.append(event_enum_value
)
292 event_enum_strings
.append(event_name
)
294 ret
= generate_event_enum_decl(event_enum_name
, event_enum_values
)
296 ret
= generate_event_enum_lookup(event_enum_name
, event_enum_strings
)
299 close_output(fdef
, fdecl
)