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
17 def _generate_event_api_name(event_name
, params
):
18 api_name
= "void qapi_event_send_%s(" % c_name(event_name
).lower();
22 for argname
, argentry
, optional
in parse_args(params
):
24 api_name
+= "bool has_%s,\n" % c_name(argname
)
25 api_name
+= "".ljust(l
)
27 api_name
+= "%s %s,\n" % (c_type(argentry
, is_param
=True),
29 api_name
+= "".ljust(l
)
31 api_name
+= "Error **errp)"
35 # Following are the core functions that generate C APIs to emit event.
37 def generate_event_declaration(api_name
):
44 def generate_event_implement(api_name
, event_name
, params
):
45 # step 1: declare any variables
51 Error *local_err = NULL;
52 QMPEventFuncEmit emit;
58 QmpOutputVisitor *qov;
64 # step 2: check emit function, create a dict
66 emit = qmp_event_get_func_emit();
71 qmp = qmp_event_build_dict("%(event_name)s");
74 event_name
= event_name
)
76 # step 3: visit the params if params != None
79 qov = qmp_output_visitor_new();
82 v = qmp_output_get_visitor(qov);
85 /* Fake visit, as if all members are under a structure */
86 visit_start_struct(v, NULL, "", "%(event_name)s", 0, &local_err);
92 event_name
= event_name
)
94 for argname
, argentry
, optional
in parse_args(params
):
99 var
= c_name(argname
))
102 if argentry
== "str":
103 var_type
= "(char **)"
108 visit_type_%(type)s(v, %(var_type)s&%(var)s, "%(name)s", &local_err);
114 var
= c_name(argname
),
115 type = type_name(argentry
),
126 visit_end_struct(v, &local_err);
131 obj = qmp_output_get_qobject(qov);
132 g_assert(obj != NULL);
134 qdict_put_obj(qmp, "data", obj);
137 # step 4: call qmp event api
139 emit(%(event_enum_value)s, qmp, &local_err);
142 event_enum_value
= event_enum_value
)
148 qmp_output_visitor_cleanup(qov);
151 error_propagate(errp, local_err);
159 # Following are the functions that generate an enum type for all defined
160 # events, similar to qapi-types.py. Here we already have enum name and
161 # values which were generated before and recorded in event_enum_*. It also
162 # works around the issue that "import qapi-types" can't work.
164 def generate_event_enum_decl(event_enum_name
, event_enum_values
):
165 lookup_decl
= mcgen('''
167 extern const char *%(event_enum_name)s_lookup[];
169 event_enum_name
= event_enum_name
)
171 enum_decl
= mcgen('''
172 typedef enum %(event_enum_name)s
175 event_enum_name
= event_enum_name
)
177 # append automatically generated _MAX value
178 enum_max_value
= c_enum_const(event_enum_name
, "MAX")
179 enum_values
= event_enum_values
+ [ enum_max_value
]
182 for value
in enum_values
:
183 enum_decl
+= mcgen('''
190 enum_decl
+= mcgen('''
191 } %(event_enum_name)s;
193 event_enum_name
= event_enum_name
)
195 return lookup_decl
+ enum_decl
197 def generate_event_enum_lookup(event_enum_name
, event_enum_strings
):
200 const char *%(event_enum_name)s_lookup[] = {
202 event_enum_name
= event_enum_name
)
205 for string
in event_enum_strings
:
220 c_file
= 'qapi-event.c'
221 h_file
= 'qapi-event.h'
223 (input_file
, output_dir
, do_c
, do_h
, prefix
, dummy
) = parse_command_line()
225 c_file
= output_dir
+ prefix
+ c_file
226 h_file
= output_dir
+ prefix
+ h_file
229 os
.makedirs(output_dir
)
231 if e
.errno
!= errno
.EEXIST
:
234 def maybe_open(really
, name
, opt
):
236 return open(name
, opt
)
239 return StringIO
.StringIO()
241 fdef
= maybe_open(do_c
, c_file
, 'w')
242 fdecl
= maybe_open(do_h
, h_file
, 'w')
245 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
248 * schema-defined QAPI event functions
250 * Copyright (c) 2014 Wenchao Xia
253 * Wenchao Xia <wenchaoqemu@gmail.com>
255 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
256 * See the COPYING.LIB file in the top-level directory.
260 #include "qemu-common.h"
261 #include "%(header)s"
262 #include "%(prefix)sqapi-visit.h"
263 #include "qapi/qmp-output-visitor.h"
264 #include "qapi/qmp-event.h"
267 prefix
=prefix
, header
=basename(h_file
)))
269 fdecl
.write(mcgen('''
270 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
273 * schema-defined QAPI event functions
275 * Copyright (c) 2014 Wenchao Xia
278 * Wenchao Xia <wenchaoqemu@gmail.com>
280 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
281 * See the COPYING.LIB file in the top-level directory.
288 #include "qapi/error.h"
289 #include "qapi/qmp/qdict.h"
290 #include "%(prefix)sqapi-types.h"
293 prefix
=prefix
, guard
=guardname(h_file
)))
295 exprs
= parse_schema(input_file
)
297 event_enum_name
= prefix
.upper().replace('-', '_') + "QAPIEvent"
298 event_enum_values
= []
299 event_enum_strings
= []
302 if expr
.has_key('event'):
303 event_name
= expr
['event']
304 params
= expr
.get('data')
305 if params
and len(params
) == 0:
308 api_name
= _generate_event_api_name(event_name
, params
)
309 ret
= generate_event_declaration(api_name
)
312 # We need an enum value per event
313 event_enum_value
= c_enum_const(event_enum_name
, event_name
)
314 ret
= generate_event_implement(api_name
, event_name
, params
)
317 # Record it, and generate enum later
318 event_enum_values
.append(event_enum_value
)
319 event_enum_strings
.append(event_name
)
321 ret
= generate_event_enum_decl(event_enum_name
, event_enum_values
)
323 ret
= generate_event_enum_lookup(event_enum_name
, event_enum_strings
)