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
)
31 api_name
+= "%s %s,\n" % (c_type(argentry
), c_var(argname
))
32 api_name
+= "".ljust(l
)
34 api_name
+= "Error **errp)"
38 # Following are the core functions that generate C APIs to emit event.
40 def generate_event_declaration(api_name
):
47 def generate_event_implement(api_name
, event_name
, params
):
48 # step 1: declare any variables
54 Error *local_err = NULL;
55 QMPEventFuncEmit emit;
61 QmpOutputVisitor *qov;
67 # step 2: check emit function, create a dict
69 emit = qmp_event_get_func_emit();
74 qmp = qmp_event_build_dict("%(event_name)s");
77 event_name
= event_name
)
79 # step 3: visit the params if params != None
82 qov = qmp_output_visitor_new();
85 v = qmp_output_get_visitor(qov);
88 /* Fake visit, as if all members are under a structure */
89 visit_start_struct(v, NULL, "", "%(event_name)s", 0, &local_err);
95 event_name
= event_name
)
97 for argname
, argentry
, optional
, structured
in parse_args(params
):
102 var
= c_var(argname
))
105 if argentry
== "str":
106 var_type
= "(char **)"
111 visit_type_%(type)s(v, %(var_type)s&%(var)s, "%(name)s", &local_err);
117 var
= c_var(argname
),
118 type = type_name(argentry
),
129 visit_end_struct(v, &local_err);
134 obj = qmp_output_get_qobject(qov);
135 g_assert(obj != NULL);
137 qdict_put_obj(qmp, "data", obj);
140 # step 4: call qmp event api
142 emit(%(event_enum_value)s, qmp, &local_err);
145 event_enum_value
= event_enum_value
)
151 qmp_output_visitor_cleanup(qov);
154 error_propagate(errp, local_err);
162 # Following are the functions that generate an enum type for all defined
163 # events, similar to qapi-types.py. Here we already have enum name and
164 # values which were generated before and recorded in event_enum_*. It also
165 # works around the issue that "import qapi-types" can't work.
167 def generate_event_enum_decl(event_enum_name
, event_enum_values
):
168 lookup_decl
= mcgen('''
170 extern const char *%(event_enum_name)s_lookup[];
172 event_enum_name
= event_enum_name
)
174 enum_decl
= mcgen('''
175 typedef enum %(event_enum_name)s
178 event_enum_name
= event_enum_name
)
180 # append automatically generated _MAX value
181 enum_max_value
= generate_enum_full_value(event_enum_name
, "MAX")
182 enum_values
= event_enum_values
+ [ enum_max_value
]
185 for value
in enum_values
:
186 enum_decl
+= mcgen('''
193 enum_decl
+= mcgen('''
194 } %(event_enum_name)s;
196 event_enum_name
= event_enum_name
)
198 return lookup_decl
+ enum_decl
200 def generate_event_enum_lookup(event_enum_name
, event_enum_strings
):
203 const char *%(event_enum_name)s_lookup[] = {
205 event_enum_name
= event_enum_name
)
208 for string
in event_enum_strings
:
224 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chbp:i:o:",
225 ["source", "header", "builtins", "prefix=",
226 "input-file=", "output-dir="])
227 except getopt
.GetoptError
, err
:
234 c_file
= 'qapi-event.c'
235 h_file
= 'qapi-event.h'
242 if o
in ("-p", "--prefix"):
244 elif o
in ("-i", "--input-file"):
246 elif o
in ("-o", "--output-dir"):
248 elif o
in ("-c", "--source"):
250 elif o
in ("-h", "--header"):
252 elif o
in ("-b", "--builtins"):
255 if not do_c
and not do_h
:
259 c_file
= output_dir
+ prefix
+ c_file
260 h_file
= output_dir
+ prefix
+ h_file
263 os
.makedirs(output_dir
)
265 if e
.errno
!= errno
.EEXIST
:
268 def maybe_open(really
, name
, opt
):
270 return open(name
, opt
)
273 return StringIO
.StringIO()
275 fdef
= maybe_open(do_c
, c_file
, 'w')
276 fdecl
= maybe_open(do_h
, h_file
, 'w')
279 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
282 * schema-defined QAPI event functions
284 * Copyright (c) 2014 Wenchao Xia
287 * Wenchao Xia <wenchaoqemu@gmail.com>
289 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
290 * See the COPYING.LIB file in the top-level directory.
294 #include "qemu-common.h"
295 #include "%(header)s"
296 #include "%(prefix)sqapi-visit.h"
297 #include "qapi/qmp-output-visitor.h"
298 #include "qapi/qmp-event.h"
301 prefix
=prefix
, header
=basename(h_file
)))
303 fdecl
.write(mcgen('''
304 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
307 * schema-defined QAPI event functions
309 * Copyright (c) 2014 Wenchao Xia
312 * Wenchao Xia <wenchaoqemu@gmail.com>
314 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
315 * See the COPYING.LIB file in the top-level directory.
322 #include "qapi/error.h"
323 #include "qapi/qmp/qdict.h"
324 #include "%(prefix)sqapi-types.h"
327 prefix
=prefix
, guard
=guardname(h_file
)))
329 exprs
= parse_schema(input_file
)
331 event_enum_name
= prefix
.upper().replace('-', '_') + "QAPIEvent"
332 event_enum_values
= []
333 event_enum_strings
= []
336 if expr
.has_key('event'):
337 event_name
= expr
['event']
338 params
= expr
.get('data')
339 if params
and len(params
) == 0:
342 api_name
= _generate_event_api_name(event_name
, params
)
343 ret
= generate_event_declaration(api_name
)
346 # We need an enum value per event
347 event_enum_value
= generate_enum_full_value(event_enum_name
,
349 ret
= generate_event_implement(api_name
, event_name
, params
)
352 # Record it, and generate enum later
353 event_enum_values
.append(event_enum_value
)
354 event_enum_strings
.append(event_name
)
356 ret
= generate_event_enum_decl(event_enum_name
, event_enum_values
)
358 ret
= generate_event_enum_lookup(event_enum_name
, event_enum_strings
)