blob: 9493f1464b33a7a727381dfa1e33dae1daf99dfb [file] [log] [blame]
Kristian Høgsberga1f3f602010-08-03 09:26:44 -04001/*
2 * Copyright © 2010 Intel Corporation
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#include <stdio.h>
20#include <string.h>
21#include <errno.h>
22#include <ctype.h>
23#include <expat.h>
24
25#include "wayland-util.h"
26
27static const char copyright[] =
28 "/*\n"
29 " * Copyright © 2010 Kristian Høgsberg\n"
30 " *\n"
31 " * Permission to use, copy, modify, distribute, and sell this software and its\n"
32 " * documentation for any purpose is hereby granted without fee, provided that\n"
33 " * the above copyright notice appear in all copies and that both that copyright\n"
34 " * notice and this permission notice appear in supporting documentation, and\n"
35 " * that the name of the copyright holders not be used in advertising or\n"
36 " * publicity pertaining to distribution of the software without specific,\n"
37 " * written prior permission. The copyright holders make no representations\n"
38 " * about the suitability of this software for any purpose. It is provided \"as\n"
39 " * is\" without express or implied warranty.\n"
40 " *\n"
41 " * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,\n"
42 " * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO\n"
43 " * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR\n"
44 " * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,\n"
45 " * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER\n"
46 " * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE\n"
47 " * OF THIS SOFTWARE.\n"
48 " */\n";
49
50static int
51usage(int ret)
52{
53 fprintf(stderr, "usage: ./scanner [header|code]\n");
54 exit(ret);
55}
56
57#define XML_BUFFER_SIZE 4096
58
59struct protocol {
60 struct wl_list interface_list;
61};
62
63struct interface {
64 char *name;
65 char *uppercase_name;
66 int version;
67 struct wl_list request_list;
68 struct wl_list event_list;
69 struct wl_list link;
70};
71
72struct message {
73 char *name;
74 char *uppercase_name;
75 struct wl_list arg_list;
76 struct wl_list link;
77};
78
79enum arg_type {
80 NEW_ID,
81 INT,
82 UNSIGNED,
83 STRING,
84 OBJECT,
85 ARRAY
86};
87
88struct arg {
89 char *name;
90 enum arg_type type;
Kristian Høgsberg3862e432010-08-09 21:25:50 -040091 char *interface_name;
Kristian Høgsberga1f3f602010-08-03 09:26:44 -040092 struct wl_list link;
93};
94
95struct parse_context {
96 struct protocol *protocol;
97 struct interface *interface;
98 struct message *message;
99};
100
101static char *
102uppercase_dup(const char *src)
103{
104 char *u;
105 int i;
106
107 u = strdup(src);
108 for (i = 0; u[i]; i++)
109 u[i] = toupper(u[i]);
110 u[i] = '\0';
111
112 return u;
113}
114
115static void
116start_element(void *data, const char *element_name, const char **atts)
117{
118 struct parse_context *ctx = data;
119 struct interface *interface;
120 struct message *message;
121 struct arg *arg;
Kristian Høgsberg3862e432010-08-09 21:25:50 -0400122 const char *name, *type, *interface_name;
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400123 int i, version;
124
Kristian Høgsberg3862e432010-08-09 21:25:50 -0400125 name = NULL;
126 type = NULL;
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400127 version = 0;
Kristian Høgsberg3862e432010-08-09 21:25:50 -0400128 interface_name = NULL;
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400129 for (i = 0; atts[i]; i += 2) {
130 if (strcmp(atts[i], "name") == 0)
131 name = atts[i + 1];
132 if (strcmp(atts[i], "version") == 0)
133 version = atoi(atts[i + 1]);
134 if (strcmp(atts[i], "type") == 0)
135 type = atts[i + 1];
Kristian Høgsberg3862e432010-08-09 21:25:50 -0400136 if (strcmp(atts[i], "interface") == 0)
137 interface_name = atts[i + 1];
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400138 }
139
140 if (strcmp(element_name, "interface") == 0) {
141 if (name == NULL) {
142 fprintf(stderr, "no interface name given\n");
143 exit(EXIT_FAILURE);
144 }
145
146 if (version == 0) {
147 fprintf(stderr, "no interface version given\n");
148 exit(EXIT_FAILURE);
149 }
150
151 interface = malloc(sizeof *interface);
152 interface->name = strdup(name);
153 interface->uppercase_name = uppercase_dup(name);
154 interface->version = version;
155 wl_list_init(&interface->request_list);
156 wl_list_init(&interface->event_list);
157 wl_list_insert(ctx->protocol->interface_list.prev,
158 &interface->link);
159 ctx->interface = interface;
160 } else if (strcmp(element_name, "request") == 0 ||
161 strcmp(element_name, "event") == 0) {
162 if (name == NULL) {
163 fprintf(stderr, "no request name given\n");
164 exit(EXIT_FAILURE);
165 }
166
167 message = malloc(sizeof *message);
168 message->name = strdup(name);
169 message->uppercase_name = uppercase_dup(name);
170 wl_list_init(&message->arg_list);
171
172 if (strcmp(element_name, "request") == 0)
173 wl_list_insert(ctx->interface->request_list.prev,
174 &message->link);
175 else
176 wl_list_insert(ctx->interface->event_list.prev,
177 &message->link);
178
179 ctx->message = message;
180 } else if (strcmp(element_name, "arg") == 0) {
181 arg = malloc(sizeof *arg);
182 arg->name = strdup(name);
183
Kristian Høgsberg3862e432010-08-09 21:25:50 -0400184 if (strcmp(type, "int") == 0)
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400185 arg->type = INT;
186 else if (strcmp(type, "uint") == 0)
187 arg->type = UNSIGNED;
188 else if (strcmp(type, "string") == 0)
189 arg->type = STRING;
190 else if (strcmp(type, "array") == 0)
191 arg->type = ARRAY;
Kristian Høgsberg3862e432010-08-09 21:25:50 -0400192 else if (strcmp(type, "new_id") == 0) {
193 if (interface_name == NULL) {
194 fprintf(stderr, "no interface name given\n");
195 exit(EXIT_FAILURE);
196 }
197 arg->type = NEW_ID;
198 arg->interface_name = strdup(interface_name);
199 } else if (strcmp(type, "object") == 0) {
200 if (interface_name == NULL) {
201 fprintf(stderr, "no interface name given\n");
202 exit(EXIT_FAILURE);
203 }
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400204 arg->type = OBJECT;
Kristian Høgsberg3862e432010-08-09 21:25:50 -0400205 arg->interface_name = strdup(interface_name);
206 } else {
207 fprintf(stderr, "unknown type: %s\n", type);
208 exit(EXIT_FAILURE);
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400209 }
210
211 wl_list_insert(ctx->message->arg_list.prev,
212 &arg->link);
213 }
214}
215
216static void
217emit_opcodes(struct wl_list *message_list, struct interface *interface)
218{
219 struct message *m;
220 int opcode;
221
222 if (wl_list_empty(message_list))
223 return;
224
225 opcode = 0;
226 wl_list_for_each(m, message_list, link)
227 printf("#define WL_%s_%s\t%d\n",
228 interface->uppercase_name, m->uppercase_name, opcode++);
229
230 printf("\n");
231}
232
233static void
Kristian Høgsberg3862e432010-08-09 21:25:50 -0400234emit_type(struct arg *a)
235{
236 switch (a->type) {
237 default:
238 case INT:
239 printf("int32_t ");
240 break;
241 case NEW_ID:
242 case UNSIGNED:
243 printf("uint32_t ");
244 break;
245 case STRING:
246 printf("const char *");
247 break;
248 case OBJECT:
249 printf("struct wl_%s *", a->interface_name);
250 break;
251 case ARRAY:
252 printf("struct wl_array *");
253 break;
254 }
255}
256
257static void
258emit_stubs(struct wl_list *message_list, struct interface *interface)
259{
260 struct message *m;
261 struct arg *a, *ret;
262
263 if (wl_list_empty(message_list))
264 return;
265
266 wl_list_for_each(m, message_list, link) {
267 ret = NULL;
268 wl_list_for_each(a, &m->arg_list, link) {
269 if (a->type == NEW_ID)
270 ret = a;
271 }
272
273 if (ret)
274 printf("static inline struct wl_%s *\n",
275 ret->interface_name);
276 else
277 printf("static inline void\n");
278
279 printf("wl_%s_%s(struct wl_%s *%s",
280 interface->name, m->name,
281 interface->name, interface->name);
282
283 wl_list_for_each(a, &m->arg_list, link) {
284 if (a->type == NEW_ID)
285 continue;
286 printf(", ");
287 emit_type(a);
288 printf("%s", a->name);
289 }
290
291 printf(")\n"
292 "{\n");
293 if (ret)
294 printf("\tstruct wl_proxy *%s;\n\n"
295 "\t%s = wl_proxy_create("
296 "(struct wl_proxy *) %s, &wl_%s_interface);\n"
297 "\tif (!%s)\n"
298 "\t\treturn NULL;\n\n",
299 ret->name,
300 ret->name,
301 interface->name, ret->interface_name,
302 ret->name);
303
304 printf("\twl_proxy_marshal((struct wl_proxy *) %s, WL_%s_%s",
305 interface->name,
306 interface->uppercase_name,
307 m->uppercase_name);
308
309 wl_list_for_each(a, &m->arg_list, link) {
310 printf(", ");
311 printf("%s", a->name);
312 }
313 printf(");\n");
314
315 if (ret)
316 printf("\n\treturn (struct wl_%s *) %s;\n",
317 ret->interface_name, ret->name);
318
319 printf("}\n\n");
320 }
321}
322
323static void
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400324emit_structs(struct wl_list *message_list, struct interface *interface)
325{
326 struct message *m;
327 struct arg *a;
328 int is_interface;
329
Kristian Høgsberg3862e432010-08-09 21:25:50 -0400330 if (wl_list_empty(message_list))
331 return;
332
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400333 is_interface = message_list == &interface->request_list;
334 printf("struct wl_%s_%s {\n", interface->name,
335 is_interface ? "interface" : "listener");
336
337 wl_list_for_each(m, message_list, link) {
338 printf("\tvoid (*%s)(", m->name);
339
340 if (is_interface) {
341 printf("struct wl_client *client, struct wl_%s *%s",
342 interface->name, interface->name);
343 } else {
344 printf("void *data, struct wl_%s *%s",
345 interface->name, interface->name);
346 }
347
348 if (!wl_list_empty(&m->arg_list))
349 printf(", ");
350
351 wl_list_for_each(a, &m->arg_list, link) {
Kristian Høgsberg3862e432010-08-09 21:25:50 -0400352 emit_type(a);
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400353 printf("%s%s",
354 a->name,
355 a->link.next == &m->arg_list ? "" : ", ");
356 }
357
358 printf(");\n");
359 }
360
361 printf("};\n\n");
362}
363
364static void
365emit_header(struct protocol *protocol, int server)
366{
367 struct interface *i;
368
369 printf("%s\n\n"
370 "#ifndef WAYLAND_PROTOCOL_H\n"
371 "#define WAYLAND_PROTOCOL_H\n"
372 "\n"
373 "#ifdef __cplusplus\n"
374 "extern \"C\" {\n"
375 "#endif\n"
376 "\n"
377 "#include <stdint.h>\n"
378 "#include \"wayland-util.h\"\n\n"
379 "struct wl_client;\n\n", copyright);
380
381 wl_list_for_each(i, &protocol->interface_list, link)
382 printf("struct wl_%s;\n", i->name);
383 printf("\n");
384
Kristian Høgsberg3862e432010-08-09 21:25:50 -0400385 if (!server)
386 printf("struct wl_proxy;\n\n"
387 "extern void\n"
388 "wl_proxy_marshal(struct wl_proxy *p, "
389 "uint32_t opcode, ...);\n"
390 "extern struct wl_proxy *\n"
391 "wl_proxy_create(struct wl_proxy *factory, "
392 "const struct wl_interface *interface);\n\n");
393
394 wl_list_for_each(i, &protocol->interface_list, link) {
395 printf("extern const struct wl_interface "
396 "wl_%s_interface;\n",
397 i->name);
398 }
399 printf("\n");
400
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400401 wl_list_for_each(i, &protocol->interface_list, link) {
402
403 if (server) {
404 emit_structs(&i->request_list, i);
405 emit_opcodes(&i->event_list, i);
406 } else {
407 emit_structs(&i->event_list, i);
408 emit_opcodes(&i->request_list, i);
Kristian Høgsberg3862e432010-08-09 21:25:50 -0400409 emit_stubs(&i->request_list, i);
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400410 }
Kristian Høgsberga1f3f602010-08-03 09:26:44 -0400411 }
412
413 printf("#ifdef __cplusplus\n"
414 "}\n"
415 "#endif\n"
416 "\n"
417 "#endif\n");
418}
419
420static void
421emit_messages(struct wl_list *message_list,
422 struct interface *interface, const char *suffix)
423{
424 struct message *m;
425 struct arg *a;
426
427 if (wl_list_empty(message_list))
428 return;
429
430 printf("static const struct wl_message "
431 "%s_%s[] = {\n",
432 interface->name, suffix);
433
434 wl_list_for_each(m, message_list, link) {
435 printf("\t{ \"%s\", \"", m->name);
436 wl_list_for_each(a, &m->arg_list, link) {
437 switch (a->type) {
438 default:
439 case INT:
440 printf("i");
441 break;
442 case NEW_ID:
443 printf("n");
444 break;
445 case UNSIGNED:
446 printf("u");
447 break;
448 case STRING:
449 printf("s");
450 break;
451 case OBJECT:
452 printf("o");
453 break;
454 case ARRAY:
455 printf("a");
456 break;
457 }
458 }
459 printf("\" },\n");
460 }
461
462 printf("};\n\n");
463}
464
465static void
466emit_code(struct protocol *protocol)
467{
468 struct interface *i;
469
470 printf("%s\n\n"
471 "#include <stdlib.h>\n"
472 "#include <stdint.h>\n"
473 "#include \"wayland-util.h\"\n\n",
474 copyright);
475
476 wl_list_for_each(i, &protocol->interface_list, link) {
477
478 emit_messages(&i->request_list, i, "requests");
479 emit_messages(&i->event_list, i, "events");
480
481 printf("WL_EXPORT const struct wl_interface "
482 "wl_%s_interface = {\n"
483 "\t\"%s\", %d,\n",
484 i->name, i->name, i->version);
485
486 if (!wl_list_empty(&i->request_list))
487 printf("\tARRAY_LENGTH(%s_requests), %s_requests,\n",
488 i->name, i->name);
489 else
490 printf("\t0, NULL,\n");
491
492 if (!wl_list_empty(&i->event_list))
493 printf("\tARRAY_LENGTH(%s_events), %s_events,\n",
494 i->name, i->name);
495 else
496 printf("\t0, NULL,\n");
497
498 printf("};\n\n");
499 }
500}
501
502int main(int argc, char *argv[])
503{
504 struct parse_context ctx;
505 struct protocol protocol;
506 XML_Parser parser;
507 int len;
508 void *buf;
509
510 if (argc != 2)
511 usage(EXIT_FAILURE);
512
513 wl_list_init(&protocol.interface_list);
514 ctx.protocol = &protocol;
515
516 parser = XML_ParserCreate(NULL);
517 XML_SetUserData(parser, &ctx);
518 if (parser == NULL) {
519 fprintf(stderr, "failed to create parser\n");
520 exit(EXIT_FAILURE);
521 }
522
523 XML_SetElementHandler(parser, start_element, NULL);
524 do {
525 buf = XML_GetBuffer(parser, XML_BUFFER_SIZE);
526 len = fread(buf, 1, XML_BUFFER_SIZE, stdin);
527 if (len < 0) {
528 fprintf(stderr, "fread: %s\n", strerror(errno));
529 exit(EXIT_FAILURE);
530 }
531 XML_ParseBuffer(parser, len, len == 0);
532
533 } while (len > 0);
534
535 XML_ParserFree(parser);
536
537 if (strcmp(argv[1], "client-header") == 0) {
538 emit_header(&protocol, 0);
539 } else if (strcmp(argv[1], "server-header") == 0) {
540 emit_header(&protocol, 1);
541 } else if (strcmp(argv[1], "code") == 0) {
542 emit_code(&protocol);
543 }
544
545 return 0;
546}