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 uppercase_dup(const char *src)
137 for (i = 0; u[i]; i++)
138 u[i] = toupper(u[i]);
144 static const char *indent(int n)
146 const char *whitespace[] = {
147 "\t\t\t\t\t\t\t\t\t\t\t\t",
148 "\t\t\t\t\t\t\t\t\t\t\t\t ",
149 "\t\t\t\t\t\t\t\t\t\t\t\t ",
150 "\t\t\t\t\t\t\t\t\t\t\t\t ",
151 "\t\t\t\t\t\t\t\t\t\t\t\t ",
152 "\t\t\t\t\t\t\t\t\t\t\t\t ",
153 "\t\t\t\t\t\t\t\t\t\t\t\t ",
154 "\t\t\t\t\t\t\t\t\t\t\t\t "
157 return whitespace[n % 8] + 12 - n / 8;
161 desc_dump(char *desc, const char *fmt, ...) __attribute__((format(printf,2,3)));
164 desc_dump(char *desc, const char *fmt, ...)
168 int col, i, j, k, startcol, newlines;
171 vsnprintf(buf, sizeof buf, fmt, ap);
174 for (i = 0, col = 0; buf[i] != '*'; i++) {
176 col = (col + 8) & ~7;
189 col += strlen(&buf[i]);
190 if (col - startcol > 2)
195 for (i = 0; desc[i]; ) {
198 while (desc[i] && isspace(desc[i])) {
207 while (desc[i] && !isspace(desc[i]))
211 printf("\n%s*", indent(startcol));
212 if (newlines > 1 || col + i - j > 72) {
213 printf("\n%s*%c", indent(startcol), hang);
217 if (col > startcol && k > 0)
219 col += printf("%.*s", i - j, &desc[j]);
225 fail(struct parse_context *ctx, const char *msg)
227 fprintf(stderr, "%s:%ld: %s\n",
228 ctx->filename, XML_GetCurrentLineNumber(ctx->parser), msg);
233 is_nullable_type(struct arg *arg)
236 /* Strings, objects, and arrays are possibly nullable */
248 start_element(void *data, const char *element_name, const char **atts)
250 struct parse_context *ctx = data;
251 struct interface *interface;
252 struct message *message;
254 struct enumeration *enumeration;
256 struct description *description;
257 const char *name, *type, *interface_name, *value, *summary, *since;
258 const char *allow_null;
265 interface_name = NULL;
271 for (i = 0; atts[i]; i += 2) {
272 if (strcmp(atts[i], "name") == 0)
274 if (strcmp(atts[i], "version") == 0)
275 version = atoi(atts[i + 1]);
276 if (strcmp(atts[i], "type") == 0)
278 if (strcmp(atts[i], "value") == 0)
280 if (strcmp(atts[i], "interface") == 0)
281 interface_name = atts[i + 1];
282 if (strcmp(atts[i], "summary") == 0)
283 summary = atts[i + 1];
284 if (strcmp(atts[i], "since") == 0)
286 if (strcmp(atts[i], "allow-null") == 0)
287 allow_null = atts[i + 1];
290 ctx->character_data_length = 0;
291 if (strcmp(element_name, "protocol") == 0) {
293 fail(ctx, "no protocol name given");
295 ctx->protocol->name = strdup(name);
296 ctx->protocol->uppercase_name = uppercase_dup(name);
297 ctx->protocol->description = NULL;
298 } else if (strcmp(element_name, "copyright") == 0) {
300 } else if (strcmp(element_name, "interface") == 0) {
302 fail(ctx, "no interface name given");
305 fail(ctx, "no interface version given");
307 interface = malloc(sizeof *interface);
308 interface->name = strdup(name);
309 interface->uppercase_name = uppercase_dup(name);
310 interface->version = version;
311 interface->description = NULL;
312 interface->since = 1;
313 wl_list_init(&interface->request_list);
314 wl_list_init(&interface->event_list);
315 wl_list_init(&interface->enumeration_list);
316 wl_list_insert(ctx->protocol->interface_list.prev,
318 ctx->interface = interface;
319 } else if (strcmp(element_name, "request") == 0 ||
320 strcmp(element_name, "event") == 0) {
322 fail(ctx, "no request name given");
324 message = malloc(sizeof *message);
325 message->name = strdup(name);
326 message->uppercase_name = uppercase_dup(name);
327 wl_list_init(&message->arg_list);
328 message->arg_count = 0;
329 message->description = NULL;
331 if (strcmp(element_name, "request") == 0)
332 wl_list_insert(ctx->interface->request_list.prev,
335 wl_list_insert(ctx->interface->event_list.prev,
338 if (type != NULL && strcmp(type, "destructor") == 0)
339 message->destructor = 1;
341 message->destructor = 0;
344 version = strtol(since, &end, 0);
345 if (errno == EINVAL || end == since || *end != '\0')
346 fail(ctx, "invalid integer\n");
347 if (version < ctx->interface->since)
348 fail(ctx, "since version not increasing\n");
349 ctx->interface->since = version;
352 message->since = ctx->interface->since;
354 if (strcmp(name, "destroy") == 0 && !message->destructor)
355 fail(ctx, "destroy request should be destructor type");
357 ctx->message = message;
358 } else if (strcmp(element_name, "arg") == 0) {
360 fail(ctx, "no argument name given");
362 arg = malloc(sizeof *arg);
363 arg->name = strdup(name);
365 if (strcmp(type, "int") == 0)
367 else if (strcmp(type, "uint") == 0)
368 arg->type = UNSIGNED;
369 else if (strcmp(type, "fixed") == 0)
371 else if (strcmp(type, "string") == 0)
373 else if (strcmp(type, "array") == 0)
375 else if (strcmp(type, "fd") == 0)
377 else if (strcmp(type, "new_id") == 0) {
379 } else if (strcmp(type, "object") == 0) {
382 fail(ctx, "unknown type");
389 arg->interface_name = strdup(interface_name);
391 arg->interface_name = NULL;
394 if (interface_name != NULL)
395 fail(ctx, "interface not allowed");
399 if (allow_null == NULL || strcmp(allow_null, "false") == 0)
401 else if (strcmp(allow_null, "true") == 0)
404 fail(ctx, "invalid value for allow-null attribute");
406 if (allow_null != NULL && !is_nullable_type(arg))
407 fail(ctx, "allow-null is only valid for objects, strings, and arrays");
411 arg->summary = strdup(summary);
413 wl_list_insert(ctx->message->arg_list.prev, &arg->link);
414 ctx->message->arg_count++;
415 } else if (strcmp(element_name, "enum") == 0) {
417 fail(ctx, "no enum name given");
419 enumeration = malloc(sizeof *enumeration);
420 enumeration->name = strdup(name);
421 enumeration->uppercase_name = uppercase_dup(name);
422 enumeration->description = NULL;
423 wl_list_init(&enumeration->entry_list);
425 wl_list_insert(ctx->interface->enumeration_list.prev,
428 ctx->enumeration = enumeration;
429 } else if (strcmp(element_name, "entry") == 0) {
431 fail(ctx, "no entry name given");
433 entry = malloc(sizeof *entry);
434 entry->name = strdup(name);
435 entry->uppercase_name = uppercase_dup(name);
436 entry->value = strdup(value);
438 entry->summary = strdup(summary);
440 entry->summary = NULL;
441 wl_list_insert(ctx->enumeration->entry_list.prev,
443 } else if (strcmp(element_name, "description") == 0) {
445 fail(ctx, "description without summary");
447 description = malloc(sizeof *description);
448 description->summary = strdup(summary);
451 ctx->message->description = description;
452 else if (ctx->enumeration)
453 ctx->enumeration->description = description;
454 else if (ctx->interface)
455 ctx->interface->description = description;
457 ctx->protocol->description = description;
458 ctx->description = description;
463 end_element(void *data, const XML_Char *name)
465 struct parse_context *ctx = data;
467 if (strcmp(name, "copyright") == 0) {
468 ctx->protocol->copyright =
469 strndup(ctx->character_data,
470 ctx->character_data_length);
471 } else if (strcmp(name, "description") == 0) {
472 ctx->description->text =
473 strndup(ctx->character_data,
474 ctx->character_data_length);
475 ctx->description = NULL;
476 } else if (strcmp(name, "request") == 0 ||
477 strcmp(name, "event") == 0) {
479 } else if (strcmp(name, "enum") == 0) {
480 ctx->enumeration = NULL;
485 character_data(void *data, const XML_Char *s, int len)
487 struct parse_context *ctx = data;
489 if (ctx->character_data_length + len > sizeof (ctx->character_data)) {
490 fprintf(stderr, "too much character data");
494 memcpy(ctx->character_data + ctx->character_data_length, s, len);
495 ctx->character_data_length += len;
499 emit_opcodes(struct wl_list *message_list, struct interface *interface)
504 if (wl_list_empty(message_list))
508 wl_list_for_each(m, message_list, link)
509 printf("#define %s_%s\t%d\n",
510 interface->uppercase_name, m->uppercase_name, opcode++);
516 emit_type(struct arg *a)
529 printf("wl_fixed_t ");
532 printf("const char *");
535 printf("struct %s *", a->interface_name);
538 printf("struct wl_array *");
544 emit_stubs(struct wl_list *message_list, struct interface *interface)
548 int has_destructor, has_destroy;
550 printf("static inline void\n"
551 "%s_set_user_data(struct %s *%s, void *user_data)\n"
553 "\twl_proxy_set_user_data((struct wl_proxy *) %s, user_data);\n"
555 interface->name, interface->name, interface->name,
558 printf("static inline void *\n"
559 "%s_get_user_data(struct %s *%s)\n"
561 "\treturn wl_proxy_get_user_data((struct wl_proxy *) %s);\n"
563 interface->name, interface->name, interface->name,
568 wl_list_for_each(m, message_list, link) {
571 if (strcmp(m->name, "destroy)") == 0)
575 if (!has_destructor && has_destroy) {
577 "interface %s has method named destroy but"
578 "no destructor", interface->name);
582 if (!has_destructor && strcmp(interface->name, "wl_display") != 0)
583 printf("static inline void\n"
584 "%s_destroy(struct %s *%s)\n"
586 "\twl_proxy_destroy("
587 "(struct wl_proxy *) %s);\n"
589 interface->name, interface->name, interface->name,
592 if (wl_list_empty(message_list))
595 wl_list_for_each(m, message_list, link) {
597 wl_list_for_each(a, &m->arg_list, link) {
598 if (a->type == NEW_ID)
602 if (ret && ret->interface_name == NULL)
603 printf("static inline void *\n");
605 printf("static inline struct %s *\n",
606 ret->interface_name);
608 printf("static inline void\n");
610 printf("%s_%s(struct %s *%s",
611 interface->name, m->name,
612 interface->name, interface->name);
614 wl_list_for_each(a, &m->arg_list, link) {
615 if (a->type == NEW_ID && a->interface_name == NULL) {
616 printf(", const struct wl_interface *interface"
617 ", uint32_t version");
619 } else if (a->type == NEW_ID)
623 printf("%s", a->name);
629 printf("\tstruct wl_proxy *%s;\n\n"
630 "\t%s = wl_proxy_create("
631 "(struct wl_proxy *) %s,\n",
632 ret->name, ret->name, interface->name);
633 if (ret->interface_name == NULL)
634 printf("\t\t\t interface);\n");
636 printf("\t\t\t &%s_interface);\n",
637 ret->interface_name);
639 printf("\tif (!%s)\n"
640 "\t\treturn NULL;\n\n",
644 printf("\twl_proxy_marshal((struct wl_proxy *) %s,\n"
647 interface->uppercase_name,
650 wl_list_for_each(a, &m->arg_list, link) {
651 if (a->type == NEW_ID && a->interface_name == NULL)
652 printf(", interface->name, version");
654 printf("%s", a->name);
659 printf("\n\twl_proxy_destroy("
660 "(struct wl_proxy *) %s);\n",
663 if (ret && ret->interface_name == NULL)
664 printf("\n\treturn (void *) %s;\n", ret->name);
666 printf("\n\treturn (struct %s *) %s;\n",
667 ret->interface_name, ret->name);
674 emit_event_wrappers(struct wl_list *message_list, struct interface *interface)
679 /* We provide hand written functions for the display object */
680 if (strcmp(interface->name, "wl_display") == 0)
683 wl_list_for_each(m, message_list, link) {
684 printf("static inline void\n"
685 "%s_send_%s(struct wl_resource *resource_",
686 interface->name, m->name);
688 wl_list_for_each(a, &m->arg_list, link) {
693 printf("struct wl_resource *");
698 printf("%s", a->name);
703 "\twl_resource_post_event(resource_, %s_%s",
704 interface->uppercase_name, m->uppercase_name);
706 wl_list_for_each(a, &m->arg_list, link)
707 printf(", %s", a->name);
715 emit_enumerations(struct interface *interface)
717 struct enumeration *e;
720 wl_list_for_each(e, &interface->enumeration_list, link) {
721 struct description *desc = e->description;
723 printf("#ifndef %s_%s_ENUM\n",
724 interface->uppercase_name, e->uppercase_name);
725 printf("#define %s_%s_ENUM\n",
726 interface->uppercase_name, e->uppercase_name);
730 desc_dump(desc->summary,
732 interface->name, e->name);
733 wl_list_for_each(entry, &e->entry_list, link) {
734 desc_dump(entry->summary,
736 interface->uppercase_name,
738 entry->uppercase_name);
742 desc_dump(desc->text, " * ");
746 printf("enum %s_%s {\n", interface->name, e->name);
747 wl_list_for_each(entry, &e->entry_list, link)
748 printf("\t%s_%s_%s = %s,\n",
749 interface->uppercase_name,
751 entry->uppercase_name, entry->value);
753 printf("#endif /* %s_%s_ENUM */\n\n",
754 interface->uppercase_name, e->uppercase_name);
759 emit_structs(struct wl_list *message_list, struct interface *interface)
765 if (wl_list_empty(message_list))
768 is_interface = message_list == &interface->request_list;
769 if (interface->description) {
770 struct description *desc = interface->description;
772 desc_dump(desc->summary, " * %s - ", interface->name);
773 wl_list_for_each(m, message_list, link) {
774 struct description *mdesc = m->description;
775 desc_dump(mdesc ? mdesc->summary : "(none)",
780 desc_dump(desc->text, " * ");
783 printf("struct %s_%s {\n", interface->name,
784 is_interface ? "interface" : "listener");
786 wl_list_for_each(m, message_list, link) {
787 struct description *mdesc = m->description;
790 desc_dump(mdesc ? mdesc->summary : "(none)",
791 "\t * %s - ", m->name);
792 wl_list_for_each(a, &m->arg_list, link) {
794 if (is_interface && a->type == NEW_ID &&
795 a->interface_name == NULL)
796 printf("\t * @interface: name of the objects interface\n"
797 "\t * @version: version of the objects interface\n");
800 desc_dump(a->summary ? a->summary : "(none)",
801 "\t * @%s: ", a->name);
805 desc_dump(mdesc->text, "\t * ");
808 printf("\t * @since: %d\n", m->since);
811 printf("\tvoid (*%s)(", m->name);
813 n = strlen(m->name) + 17;
815 printf("struct wl_client *client,\n"
816 "%sstruct wl_resource *resource",
819 printf("void *data,\n"),
820 printf("%sstruct %s *%s",
821 indent(n), interface->name, interface->name);
824 wl_list_for_each(a, &m->arg_list, link) {
825 printf(",\n%s", indent(n));
827 if (is_interface && a->type == OBJECT)
828 printf("struct wl_resource *");
829 else if (is_interface && a->type == NEW_ID && a->interface_name == NULL)
830 printf("const char *interface, uint32_t version, uint32_t ");
831 else if (!is_interface && a->type == OBJECT && a->interface_name == NULL)
832 printf("struct wl_object *");
834 else if (!is_interface && a->type == NEW_ID)
835 printf("struct %s *", a->interface_name);
839 printf("%s", a->name);
848 printf("static inline int\n"
849 "%s_add_listener(struct %s *%s,\n"
850 "%sconst struct %s_listener *listener, void *data)\n"
852 "\treturn wl_proxy_add_listener((struct wl_proxy *) %s,\n"
853 "%s(void (**)(void)) listener, data);\n"
855 interface->name, interface->name, interface->name,
856 indent(14 + strlen(interface->name)),
864 format_copyright(const char *copyright)
866 int bol = 1, start = 0, i;
868 for (i = 0; copyright[i]; i++) {
869 if (bol && (copyright[i] == ' ' || copyright[i] == '\t')) {
876 if (copyright[i] == '\n' || copyright[i] == '\0') {
878 i == 0 ? "/*" : " *",
879 i - start, copyright + start);
887 emit_header(struct protocol *protocol, int server)
890 const char *s = server ? "SERVER" : "CLIENT";
892 if (protocol->copyright)
893 format_copyright(protocol->copyright);
895 printf("#ifndef %s_%s_PROTOCOL_H\n"
896 "#define %s_%s_PROTOCOL_H\n"
898 "#ifdef __cplusplus\n"
902 "#include <stdint.h>\n"
903 "#include <stddef.h>\n"
904 "#include \"%s\"\n\n"
905 "struct wl_client;\n"
906 "struct wl_resource;\n\n",
907 protocol->uppercase_name, s,
908 protocol->uppercase_name, s,
909 server ? "wayland-util.h" : "wayland-client.h");
911 wl_list_for_each(i, &protocol->interface_list, link)
912 printf("struct %s;\n", i->name);
915 wl_list_for_each(i, &protocol->interface_list, link) {
916 printf("extern const struct wl_interface "
922 wl_list_for_each(i, &protocol->interface_list, link) {
924 emit_enumerations(i);
927 emit_structs(&i->request_list, i);
928 emit_opcodes(&i->event_list, i);
929 emit_event_wrappers(&i->event_list, i);
931 emit_structs(&i->event_list, i);
932 emit_opcodes(&i->request_list, i);
933 emit_stubs(&i->request_list, i);
937 printf("#ifdef __cplusplus\n"
945 emit_types_forward_declarations(struct protocol *protocol,
946 struct wl_list *message_list)
952 wl_list_for_each(m, message_list, link) {
955 wl_list_for_each(a, &m->arg_list, link) {
960 if (!a->interface_name)
964 printf("extern const struct wl_interface %s_interface;\n",
972 if (m->all_null && length > protocol->null_run_length)
973 protocol->null_run_length = length;
978 emit_null_run(struct protocol *protocol)
982 for (i = 0; i < protocol->null_run_length; i++)
987 emit_types(struct protocol *protocol, struct wl_list *message_list)
992 wl_list_for_each(m, message_list, link) {
999 protocol->null_run_length + protocol->type_index;
1000 protocol->type_index += m->arg_count;
1002 wl_list_for_each(a, &m->arg_list, link) {
1006 if (a->interface_name)
1007 printf("\t&%s_interface,\n",
1010 printf("\tNULL,\n");
1013 printf("\tNULL,\n");
1021 emit_messages(struct wl_list *message_list,
1022 struct interface *interface, const char *suffix)
1027 if (wl_list_empty(message_list))
1030 printf("static const struct wl_message "
1032 interface->name, suffix);
1034 wl_list_for_each(m, message_list, link) {
1035 printf("\t{ \"%s\", \"", m->name);
1036 wl_list_for_each(a, &m->arg_list, link) {
1037 if (is_nullable_type(a) && a->nullable)
1046 if (a->interface_name == NULL)
1070 printf("\", types + %d },\n", m->type_index);
1077 emit_code(struct protocol *protocol)
1079 struct interface *i;
1081 if (protocol->copyright)
1082 format_copyright(protocol->copyright);
1084 printf("#include <stdlib.h>\n"
1085 "#include <stdint.h>\n"
1086 "#include \"wayland-util.h\"\n\n");
1088 wl_list_for_each(i, &protocol->interface_list, link) {
1089 emit_types_forward_declarations(protocol, &i->request_list);
1090 emit_types_forward_declarations(protocol, &i->event_list);
1094 printf("static const struct wl_interface *types[] = {\n");
1095 emit_null_run(protocol);
1096 wl_list_for_each(i, &protocol->interface_list, link) {
1097 emit_types(protocol, &i->request_list);
1098 emit_types(protocol, &i->event_list);
1102 wl_list_for_each(i, &protocol->interface_list, link) {
1104 emit_messages(&i->request_list, i, "requests");
1105 emit_messages(&i->event_list, i, "events");
1107 printf("WL_EXPORT const struct wl_interface "
1108 "%s_interface = {\n"
1110 i->name, i->name, i->version);
1112 if (!wl_list_empty(&i->request_list))
1113 printf("\t%d, %s_requests,\n",
1114 wl_list_length(&i->request_list), i->name);
1116 printf("\t0, NULL,\n");
1118 if (!wl_list_empty(&i->event_list))
1119 printf("\t%d, %s_events,\n",
1120 wl_list_length(&i->event_list), i->name);
1122 printf("\t0, NULL,\n");
1128 int main(int argc, char *argv[])
1130 struct parse_context ctx;
1131 struct protocol protocol;
1136 usage(EXIT_FAILURE);
1138 wl_list_init(&protocol.interface_list);
1139 protocol.type_index = 0;
1140 protocol.null_run_length = 0;
1141 protocol.copyright = NULL;
1142 memset(&ctx, 0, sizeof ctx);
1143 ctx.protocol = &protocol;
1145 ctx.filename = "<stdin>";
1146 ctx.parser = XML_ParserCreate(NULL);
1147 XML_SetUserData(ctx.parser, &ctx);
1148 if (ctx.parser == NULL) {
1149 fprintf(stderr, "failed to create parser\n");
1153 XML_SetElementHandler(ctx.parser, start_element, end_element);
1154 XML_SetCharacterDataHandler(ctx.parser, character_data);
1157 buf = XML_GetBuffer(ctx.parser, XML_BUFFER_SIZE);
1158 len = fread(buf, 1, XML_BUFFER_SIZE, stdin);
1160 fprintf(stderr, "fread: %m\n");
1163 XML_ParseBuffer(ctx.parser, len, len == 0);
1167 XML_ParserFree(ctx.parser);
1169 if (strcmp(argv[1], "client-header") == 0) {
1170 emit_header(&protocol, 0);
1171 } else if (strcmp(argv[1], "server-header") == 0) {
1172 emit_header(&protocol, 1);
1173 } else if (strcmp(argv[1], "code") == 0) {
1174 emit_code(&protocol);