2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
31 #include "wayland-util.h"
36 fprintf(stderr, "usage: ./scanner [client-header|server-header|code]\n");
40 #define XML_BUFFER_SIZE 4096
50 struct wl_list interface_list;
54 struct description *description;
62 struct wl_list request_list;
63 struct wl_list event_list;
64 struct wl_list enumeration_list;
66 struct description *description;
72 struct wl_list arg_list;
79 struct description *description;
104 char *uppercase_name;
105 struct wl_list entry_list;
107 struct description *description;
112 char *uppercase_name;
118 struct parse_context {
119 const char *filename;
121 struct protocol *protocol;
122 struct interface *interface;
123 struct message *message;
124 struct enumeration *enumeration;
125 struct description *description;
126 char character_data[8192];
127 unsigned int character_data_length;
131 list_length(struct wl_list *list)
136 for (i = 0, l = list->next; l != list; i++, l = l->next)
143 uppercase_dup(const char *src)
149 for (i = 0; u[i]; i++)
150 u[i] = toupper(u[i]);
156 static const char *indent(int n)
158 const char *whitespace[] = {
159 "\t\t\t\t\t\t\t\t\t\t\t\t",
160 "\t\t\t\t\t\t\t\t\t\t\t\t ",
161 "\t\t\t\t\t\t\t\t\t\t\t\t ",
162 "\t\t\t\t\t\t\t\t\t\t\t\t ",
163 "\t\t\t\t\t\t\t\t\t\t\t\t ",
164 "\t\t\t\t\t\t\t\t\t\t\t\t ",
165 "\t\t\t\t\t\t\t\t\t\t\t\t ",
166 "\t\t\t\t\t\t\t\t\t\t\t\t "
169 return whitespace[n % 8] + 12 - n / 8;
173 desc_dump(char *desc, const char *fmt, ...) __attribute__((format(printf,2,3)));
176 desc_dump(char *desc, const char *fmt, ...)
180 int col, i, j, k, startcol;
183 vsnprintf(buf, sizeof buf, fmt, ap);
186 for (i = 0, col = 0; buf[i] != '*'; i++) {
188 col = (col + 8) & ~7;
201 col += strlen(&buf[i]);
202 if (col - startcol > 2)
207 for (i = 0; desc[i]; ) {
209 while (desc[i] && isspace(desc[i]))
215 while (desc[i] && !isspace(desc[i]))
218 if (col + i - j > 72) {
219 printf("\n%s*%c", indent(startcol), hang);
223 if (col > startcol && k > 0)
225 col += printf("%.*s", i - j, &desc[j]);
231 fail(struct parse_context *ctx, const char *msg)
233 fprintf(stderr, "%s:%ld: %s\n",
234 ctx->filename, XML_GetCurrentLineNumber(ctx->parser), msg);
239 is_nullable_type(struct arg *arg)
242 /* Strings, objects, and arrays are possibly nullable */
254 start_element(void *data, const char *element_name, const char **atts)
256 struct parse_context *ctx = data;
257 struct interface *interface;
258 struct message *message;
260 struct enumeration *enumeration;
262 struct description *description;
263 const char *name, *type, *interface_name, *value, *summary, *since;
264 const char *allow_null;
271 interface_name = NULL;
277 for (i = 0; atts[i]; i += 2) {
278 if (strcmp(atts[i], "name") == 0)
280 if (strcmp(atts[i], "version") == 0)
281 version = atoi(atts[i + 1]);
282 if (strcmp(atts[i], "type") == 0)
284 if (strcmp(atts[i], "value") == 0)
286 if (strcmp(atts[i], "interface") == 0)
287 interface_name = atts[i + 1];
288 if (strcmp(atts[i], "summary") == 0)
289 summary = atts[i + 1];
290 if (strcmp(atts[i], "since") == 0)
292 if (strcmp(atts[i], "allow-null") == 0)
293 allow_null = atts[i + 1];
296 ctx->character_data_length = 0;
297 if (strcmp(element_name, "protocol") == 0) {
299 fail(ctx, "no protocol name given");
301 ctx->protocol->name = strdup(name);
302 ctx->protocol->uppercase_name = uppercase_dup(name);
303 ctx->protocol->description = NULL;
304 } else if (strcmp(element_name, "copyright") == 0) {
306 } else if (strcmp(element_name, "interface") == 0) {
308 fail(ctx, "no interface name given");
311 fail(ctx, "no interface version given");
313 interface = malloc(sizeof *interface);
314 interface->name = strdup(name);
315 interface->uppercase_name = uppercase_dup(name);
316 interface->version = version;
317 interface->description = NULL;
318 interface->since = 1;
319 wl_list_init(&interface->request_list);
320 wl_list_init(&interface->event_list);
321 wl_list_init(&interface->enumeration_list);
322 wl_list_insert(ctx->protocol->interface_list.prev,
324 ctx->interface = interface;
325 } else if (strcmp(element_name, "request") == 0 ||
326 strcmp(element_name, "event") == 0) {
328 fail(ctx, "no request name given");
330 message = malloc(sizeof *message);
331 message->name = strdup(name);
332 message->uppercase_name = uppercase_dup(name);
333 wl_list_init(&message->arg_list);
334 message->arg_count = 0;
335 message->description = NULL;
337 if (strcmp(element_name, "request") == 0)
338 wl_list_insert(ctx->interface->request_list.prev,
341 wl_list_insert(ctx->interface->event_list.prev,
344 if (type != NULL && strcmp(type, "destructor") == 0)
345 message->destructor = 1;
347 message->destructor = 0;
350 version = strtol(since, &end, 0);
351 if (errno == EINVAL || end == since || *end != '\0')
352 fail(ctx, "invalid integer\n");
353 if (version <= ctx->interface->since)
354 fail(ctx, "since version not increasing\n");
355 ctx->interface->since = version;
358 message->since = ctx->interface->since;
360 if (strcmp(name, "destroy") == 0 && !message->destructor)
361 fail(ctx, "destroy request should be destructor type");
363 ctx->message = message;
364 } else if (strcmp(element_name, "arg") == 0) {
366 fail(ctx, "no argument name given");
368 arg = malloc(sizeof *arg);
369 arg->name = strdup(name);
371 if (strcmp(type, "int") == 0)
373 else if (strcmp(type, "uint") == 0)
374 arg->type = UNSIGNED;
375 else if (strcmp(type, "fixed") == 0)
377 else if (strcmp(type, "string") == 0)
379 else if (strcmp(type, "array") == 0)
381 else if (strcmp(type, "fd") == 0)
383 else if (strcmp(type, "new_id") == 0) {
385 } else if (strcmp(type, "object") == 0) {
388 fail(ctx, "unknown type");
395 arg->interface_name = strdup(interface_name);
398 if (interface_name != NULL)
399 fail(ctx, "interface not allowed");
403 if (allow_null == NULL || strcmp(allow_null, "false") == 0)
405 else if (strcmp(allow_null, "true") == 0)
408 fail(ctx, "invalid value for allow-null attribute");
410 if (allow_null != NULL && !is_nullable_type(arg))
411 fail(ctx, "allow-null is only valid for objects, strings, and arrays");
415 arg->summary = strdup(summary);
417 wl_list_insert(ctx->message->arg_list.prev, &arg->link);
418 ctx->message->arg_count++;
419 } else if (strcmp(element_name, "enum") == 0) {
421 fail(ctx, "no enum name given");
423 enumeration = malloc(sizeof *enumeration);
424 enumeration->name = strdup(name);
425 enumeration->uppercase_name = uppercase_dup(name);
426 enumeration->description = NULL;
427 wl_list_init(&enumeration->entry_list);
429 wl_list_insert(ctx->interface->enumeration_list.prev,
432 ctx->enumeration = enumeration;
433 } else if (strcmp(element_name, "entry") == 0) {
435 fail(ctx, "no entry name given");
437 entry = malloc(sizeof *entry);
438 entry->name = strdup(name);
439 entry->uppercase_name = uppercase_dup(name);
440 entry->value = strdup(value);
442 entry->summary = strdup(summary);
444 entry->summary = NULL;
445 wl_list_insert(ctx->enumeration->entry_list.prev,
447 } else if (strcmp(element_name, "description") == 0) {
449 fail(ctx, "description without summary");
451 description = malloc(sizeof *description);
453 description->summary = strdup(summary);
455 description->summary = NULL;
458 ctx->message->description = description;
459 else if (ctx->enumeration)
460 ctx->enumeration->description = description;
461 else if (ctx->interface)
462 ctx->interface->description = description;
464 ctx->protocol->description = description;
465 ctx->description = description;
470 end_element(void *data, const XML_Char *name)
472 struct parse_context *ctx = data;
474 if (strcmp(name, "copyright") == 0) {
475 ctx->protocol->copyright =
476 strndup(ctx->character_data,
477 ctx->character_data_length);
478 } else if (strcmp(name, "description") == 0) {
479 char *text = strndup(ctx->character_data,
480 ctx->character_data_length);
482 ctx->description->text = text;
483 ctx->description = NULL;
484 } else if (strcmp(name, "request") == 0 ||
485 strcmp(name, "event") == 0) {
487 } else if (strcmp(name, "enum") == 0) {
488 ctx->enumeration = NULL;
493 character_data(void *data, const XML_Char *s, int len)
495 struct parse_context *ctx = data;
497 if (ctx->character_data_length + len > sizeof (ctx->character_data)) {
498 fprintf(stderr, "too much character data");
502 memcpy(ctx->character_data + ctx->character_data_length, s, len);
503 ctx->character_data_length += len;
507 emit_opcodes(struct wl_list *message_list, struct interface *interface)
512 if (wl_list_empty(message_list))
516 wl_list_for_each(m, message_list, link)
517 printf("#define %s_%s\t%d\n",
518 interface->uppercase_name, m->uppercase_name, opcode++);
524 emit_type(struct arg *a)
537 printf("wl_fixed_t ");
540 printf("const char *");
543 printf("struct %s *", a->interface_name);
546 printf("struct wl_array *");
552 emit_stubs(struct wl_list *message_list, struct interface *interface)
556 int has_destructor, has_destroy;
558 printf("static inline void\n"
559 "%s_set_user_data(struct %s *%s, void *user_data)\n"
561 "\twl_proxy_set_user_data((struct wl_proxy *) %s, user_data);\n"
563 interface->name, interface->name, interface->name,
566 printf("static inline void *\n"
567 "%s_get_user_data(struct %s *%s)\n"
569 "\treturn wl_proxy_get_user_data((struct wl_proxy *) %s);\n"
571 interface->name, interface->name, interface->name,
576 wl_list_for_each(m, message_list, link) {
579 if (strcmp(m->name, "destroy)") == 0)
583 if (!has_destructor && has_destroy) {
585 "interface %s has method named destroy but"
586 "no destructor", interface->name);
590 if (!has_destructor && strcmp(interface->name, "wl_display") != 0)
591 printf("static inline void\n"
592 "%s_destroy(struct %s *%s)\n"
594 "\twl_proxy_destroy("
595 "(struct wl_proxy *) %s);\n"
597 interface->name, interface->name, interface->name,
600 if (wl_list_empty(message_list))
603 wl_list_for_each(m, message_list, link) {
605 wl_list_for_each(a, &m->arg_list, link) {
606 if (a->type == NEW_ID)
610 if (ret && ret->interface_name == NULL)
611 printf("static inline void *\n");
613 printf("static inline struct %s *\n",
614 ret->interface_name);
616 printf("static inline void\n");
618 printf("%s_%s(struct %s *%s",
619 interface->name, m->name,
620 interface->name, interface->name);
622 wl_list_for_each(a, &m->arg_list, link) {
623 if (a->type == NEW_ID && a->interface_name == NULL) {
624 printf(", const struct wl_interface *interface"
625 ", uint32_t version");
627 } else if (a->type == NEW_ID)
631 printf("%s", a->name);
637 printf("\tstruct wl_proxy *%s;\n\n"
638 "\t%s = wl_proxy_create("
639 "(struct wl_proxy *) %s,\n",
640 ret->name, ret->name, interface->name);
641 if (ret->interface_name == NULL)
642 printf("\t\t\t interface);\n");
644 printf("\t\t\t &%s_interface);\n",
645 ret->interface_name);
647 printf("\tif (!%s)\n"
648 "\t\treturn NULL;\n\n",
652 printf("\twl_proxy_marshal((struct wl_proxy *) %s,\n"
655 interface->uppercase_name,
658 wl_list_for_each(a, &m->arg_list, link) {
659 if (a->type == NEW_ID && a->interface_name == NULL)
660 printf(", interface->name, version");
662 printf("%s", a->name);
667 printf("\n\twl_proxy_destroy("
668 "(struct wl_proxy *) %s);\n",
671 if (ret && ret->interface_name == NULL)
672 printf("\n\treturn (void *) %s;\n", ret->name);
674 printf("\n\treturn (struct %s *) %s;\n",
675 ret->interface_name, ret->name);
682 emit_event_wrappers(struct wl_list *message_list, struct interface *interface)
687 /* We provide hand written functions for the display object */
688 if (strcmp(interface->name, "wl_display") == 0)
691 wl_list_for_each(m, message_list, link) {
692 printf("static inline void\n"
693 "%s_send_%s(struct wl_resource *resource_",
694 interface->name, m->name);
696 wl_list_for_each(a, &m->arg_list, link) {
701 printf("struct wl_resource *");
706 printf("%s", a->name);
711 "\twl_resource_post_event(resource_, %s_%s",
712 interface->uppercase_name, m->uppercase_name);
714 wl_list_for_each(a, &m->arg_list, link)
715 printf(", %s", a->name);
723 emit_enumerations(struct interface *interface)
725 struct enumeration *e;
728 wl_list_for_each(e, &interface->enumeration_list, link) {
729 struct description *desc = e->description;
731 printf("#ifndef %s_%s_ENUM\n",
732 interface->uppercase_name, e->uppercase_name);
733 printf("#define %s_%s_ENUM\n",
734 interface->uppercase_name, e->uppercase_name);
738 desc_dump(desc->summary,
740 interface->name, e->name);
741 wl_list_for_each(entry, &e->entry_list, link) {
742 desc_dump(entry->summary,
744 interface->uppercase_name,
746 entry->uppercase_name);
750 desc_dump(desc->text, " * ");
754 printf("enum %s_%s {\n", interface->name, e->name);
755 wl_list_for_each(entry, &e->entry_list, link)
756 printf("\t%s_%s_%s = %s,\n",
757 interface->uppercase_name,
759 entry->uppercase_name, entry->value);
761 printf("#endif /* %s_%s_ENUM */\n\n",
762 interface->uppercase_name, e->uppercase_name);
767 emit_structs(struct wl_list *message_list, struct interface *interface)
773 if (wl_list_empty(message_list))
776 is_interface = message_list == &interface->request_list;
777 if (interface->description) {
778 struct description *desc = interface->description;
780 desc_dump(desc->summary, " * %s - ", interface->name);
781 wl_list_for_each(m, message_list, link) {
782 struct description *mdesc = m->description;
783 desc_dump(mdesc ? mdesc->summary : "(none)",
788 desc_dump(desc->text, " * ");
791 printf("struct %s_%s {\n", interface->name,
792 is_interface ? "interface" : "listener");
794 wl_list_for_each(m, message_list, link) {
795 struct description *mdesc = m->description;
798 desc_dump(mdesc ? mdesc->summary : "(none)",
799 "\t * %s - ", m->name);
800 wl_list_for_each(a, &m->arg_list, link) {
802 if (is_interface && a->type == NEW_ID &&
803 a->interface_name == NULL)
804 printf("\t * @interface: name of the objects interface\n"
805 "\t * @version: version of the objects interface\n");
808 desc_dump(a->summary ? a->summary : "(none)",
809 "\t * @%s: ", a->name);
813 desc_dump(mdesc->text, "\t * ");
816 printf("\t * @since: %d\n", m->since);
819 printf("\tvoid (*%s)(", m->name);
821 n = strlen(m->name) + 17;
823 printf("struct wl_client *client,\n"
824 "%sstruct wl_resource *resource",
827 printf("void *data,\n"),
828 printf("%sstruct %s *%s",
829 indent(n), interface->name, interface->name);
832 wl_list_for_each(a, &m->arg_list, link) {
833 printf(",\n%s", indent(n));
835 if (is_interface && a->type == OBJECT)
836 printf("struct wl_resource *");
837 else if (is_interface && a->type == NEW_ID && a->interface_name == NULL)
838 printf("const char *interface, uint32_t version, uint32_t ");
839 else if (!is_interface && a->type == OBJECT && a->interface_name == NULL)
840 printf("struct wl_object *");
842 else if (!is_interface && a->type == NEW_ID)
843 printf("struct %s *", a->interface_name);
847 printf("%s", a->name);
856 printf("static inline int\n"
857 "%s_add_listener(struct %s *%s,\n"
858 "%sconst struct %s_listener *listener, void *data)\n"
860 "\treturn wl_proxy_add_listener((struct wl_proxy *) %s,\n"
861 "%s(void (**)(void)) listener, data);\n"
863 interface->name, interface->name, interface->name,
864 indent(14 + strlen(interface->name)),
872 format_copyright(const char *copyright)
874 int bol = 1, start = 0, i;
876 for (i = 0; copyright[i]; i++) {
877 if (bol && (copyright[i] == ' ' || copyright[i] == '\t')) {
884 if (copyright[i] == '\n' || copyright[i] == '\0') {
886 i == 0 ? "/*" : " *",
887 i - start, copyright + start);
895 emit_header(struct protocol *protocol, int server)
898 const char *s = server ? "SERVER" : "CLIENT";
900 if (protocol->copyright)
901 format_copyright(protocol->copyright);
903 printf("#ifndef %s_%s_PROTOCOL_H\n"
904 "#define %s_%s_PROTOCOL_H\n"
906 "#ifdef __cplusplus\n"
910 "#include <stdint.h>\n"
911 "#include <stddef.h>\n"
912 "#include \"%s\"\n\n"
913 "struct wl_client;\n"
914 "struct wl_resource;\n\n",
915 protocol->uppercase_name, s,
916 protocol->uppercase_name, s,
917 server ? "wayland-util.h" : "wayland-client.h");
919 wl_list_for_each(i, &protocol->interface_list, link)
920 printf("struct %s;\n", i->name);
923 wl_list_for_each(i, &protocol->interface_list, link) {
924 printf("extern const struct wl_interface "
930 wl_list_for_each(i, &protocol->interface_list, link) {
932 emit_enumerations(i);
935 emit_structs(&i->request_list, i);
936 emit_opcodes(&i->event_list, i);
937 emit_event_wrappers(&i->event_list, i);
939 emit_structs(&i->event_list, i);
940 emit_opcodes(&i->request_list, i);
941 emit_stubs(&i->request_list, i);
945 printf("#ifdef __cplusplus\n"
953 emit_types_forward_declarations(struct protocol *protocol,
954 struct wl_list *message_list)
960 wl_list_for_each(m, message_list, link) {
963 wl_list_for_each(a, &m->arg_list, link) {
968 if (!a->interface_name)
972 printf("extern const struct wl_interface %s_interface;\n",
980 if (m->all_null && length > protocol->null_run_length)
981 protocol->null_run_length = length;
986 emit_null_run(struct protocol *protocol)
990 for (i = 0; i < protocol->null_run_length; i++)
995 emit_types(struct protocol *protocol, struct wl_list *message_list)
1000 wl_list_for_each(m, message_list, link) {
1007 protocol->null_run_length + protocol->type_index;
1008 protocol->type_index += m->arg_count;
1010 wl_list_for_each(a, &m->arg_list, link) {
1014 if (a->interface_name)
1015 printf("\t&%s_interface,\n",
1018 printf("\tNULL,\n");
1021 printf("\tNULL,\n");
1029 emit_messages(struct wl_list *message_list,
1030 struct interface *interface, const char *suffix)
1035 if (wl_list_empty(message_list))
1038 printf("static const struct wl_message "
1040 interface->name, suffix);
1042 wl_list_for_each(m, message_list, link) {
1043 printf("\t{ \"%s\", \"", m->name);
1044 wl_list_for_each(a, &m->arg_list, link) {
1045 if (is_nullable_type(a) && a->nullable)
1054 if (a->interface_name == NULL)
1078 printf("\", types + %d },\n", m->type_index);
1085 emit_code(struct protocol *protocol)
1087 struct interface *i;
1089 if (protocol->copyright)
1090 format_copyright(protocol->copyright);
1092 printf("#include <stdlib.h>\n"
1093 "#include <stdint.h>\n"
1094 "#include \"wayland-util.h\"\n\n");
1096 wl_list_for_each(i, &protocol->interface_list, link) {
1097 emit_types_forward_declarations(protocol, &i->request_list);
1098 emit_types_forward_declarations(protocol, &i->event_list);
1102 printf("static const struct wl_interface *types[] = {\n");
1103 emit_null_run(protocol);
1104 wl_list_for_each(i, &protocol->interface_list, link) {
1105 emit_types(protocol, &i->request_list);
1106 emit_types(protocol, &i->event_list);
1110 wl_list_for_each(i, &protocol->interface_list, link) {
1112 emit_messages(&i->request_list, i, "requests");
1113 emit_messages(&i->event_list, i, "events");
1115 printf("WL_EXPORT const struct wl_interface "
1116 "%s_interface = {\n"
1118 i->name, i->name, i->version);
1120 if (!wl_list_empty(&i->request_list))
1121 printf("\t%d, %s_requests,\n",
1122 list_length(&i->request_list), i->name);
1124 printf("\t0, NULL,\n");
1126 if (!wl_list_empty(&i->event_list))
1127 printf("\t%d, %s_events,\n",
1128 list_length(&i->event_list), i->name);
1130 printf("\t0, NULL,\n");
1136 int main(int argc, char *argv[])
1138 struct parse_context ctx;
1139 struct protocol protocol;
1144 usage(EXIT_FAILURE);
1146 wl_list_init(&protocol.interface_list);
1147 protocol.type_index = 0;
1148 protocol.null_run_length = 0;
1149 protocol.copyright = NULL;
1150 ctx.protocol = &protocol;
1152 ctx.filename = "<stdin>";
1153 ctx.parser = XML_ParserCreate(NULL);
1154 XML_SetUserData(ctx.parser, &ctx);
1155 if (ctx.parser == NULL) {
1156 fprintf(stderr, "failed to create parser\n");
1160 XML_SetElementHandler(ctx.parser, start_element, end_element);
1161 XML_SetCharacterDataHandler(ctx.parser, character_data);
1164 buf = XML_GetBuffer(ctx.parser, XML_BUFFER_SIZE);
1165 len = fread(buf, 1, XML_BUFFER_SIZE, stdin);
1167 fprintf(stderr, "fread: %m\n");
1170 XML_ParseBuffer(ctx.parser, len, len == 0);
1174 XML_ParserFree(ctx.parser);
1176 if (strcmp(argv[1], "client-header") == 0) {
1177 emit_header(&protocol, 0);
1178 } else if (strcmp(argv[1], "server-header") == 0) {
1179 emit_header(&protocol, 1);
1180 } else if (strcmp(argv[1], "code") == 0) {
1181 emit_code(&protocol);