4a4e605bc8053f55112d60a065c4f2d8d750ec4e
[profile/ivi/wayland.git] / src / scanner.c
1 /*
2  * Copyright © 2008-2011 Kristian Høgsberg
3  * Copyright © 2011 Intel Corporation
4  *
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.
14  *
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
21  * OF THIS SOFTWARE.
22  */
23
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <expat.h>
30
31 #include "wayland-util.h"
32
33 static int
34 usage(int ret)
35 {
36         fprintf(stderr, "usage: ./scanner [client-header|server-header|code]\n");
37         exit(ret);
38 }
39
40 #define XML_BUFFER_SIZE 4096
41
42 struct description {
43         char *summary;
44         char *text;
45 };
46
47 struct protocol {
48         char *name;
49         char *uppercase_name;
50         struct wl_list interface_list;
51         int type_index;
52         int null_run_length;
53         char *copyright;
54         struct description *description;
55 };
56
57 struct interface {
58         char *name;
59         char *uppercase_name;
60         int version;
61         int since;
62         struct wl_list request_list;
63         struct wl_list event_list;
64         struct wl_list enumeration_list;
65         struct wl_list link;
66         struct description *description;
67 };
68
69 struct message {
70         char *name;
71         char *uppercase_name;
72         struct wl_list arg_list;
73         struct wl_list link;
74         int arg_count;
75         int type_index;
76         int all_null;
77         int destructor;
78         int since;
79         struct description *description;
80 };
81
82 enum arg_type {
83         NEW_ID,
84         INT,
85         UNSIGNED,
86         FIXED,
87         STRING,
88         OBJECT,
89         ARRAY,
90         FD
91 };
92
93 struct arg {
94         char *name;
95         enum arg_type type;
96         int nullable;
97         char *interface_name;
98         struct wl_list link;
99         char *summary;
100 };
101
102 struct enumeration {
103         char *name;
104         char *uppercase_name;
105         struct wl_list entry_list;
106         struct wl_list link;
107         struct description *description;
108 };
109
110 struct entry {
111         char *name;
112         char *uppercase_name;
113         char *value;
114         char *summary;
115         struct wl_list link;
116 };
117
118 struct parse_context {
119         const char *filename;
120         XML_Parser parser;
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;
128 };
129
130 static int
131 list_length(struct wl_list *list)
132 {
133         struct wl_list *l;
134         int i;
135
136         for (i = 0, l = list->next; l != list; i++, l = l->next)
137                 ;
138
139         return i;
140 }
141
142 static char *
143 uppercase_dup(const char *src)
144 {
145         char *u;
146         int i;
147
148         u = strdup(src);
149         for (i = 0; u[i]; i++)
150                 u[i] = toupper(u[i]);
151         u[i] = '\0';
152
153         return u;
154 }
155
156 static const char *indent(int n)
157 {
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       "
167         };
168
169         return whitespace[n % 8] + 12 - n / 8;
170 }
171
172 static void
173 desc_dump(char *desc, const char *fmt, ...) __attribute__((format(printf,2,3)));
174
175 static void
176 desc_dump(char *desc, const char *fmt, ...)
177 {
178         va_list ap;
179         char buf[128], hang;
180         int col, i, j, k, startcol;
181
182         va_start(ap, fmt);
183         vsnprintf(buf, sizeof buf, fmt, ap);
184         va_end(ap);
185
186         for (i = 0, col = 0; buf[i] != '*'; i++) {
187                 if (buf[i] == '\t')
188                         col = (col + 8) & ~7;
189                 else
190                         col++;
191         }
192
193         printf("%s", buf);
194
195         if (!desc) {
196                 printf("(none)\n");
197                 return;
198         }
199
200         startcol = col;
201         col += strlen(&buf[i]);
202         if (col - startcol > 2)
203                 hang = '\t';
204         else
205                 hang = ' ';
206
207         for (i = 0; desc[i]; ) {
208                 k = i;
209                 while (desc[i] && isspace(desc[i]))
210                         i++;
211                 if (!desc[i])
212                         break;
213
214                 j = i;
215                 while (desc[i] && !isspace(desc[i]))
216                         i++;
217
218                 if (col + i - j > 72) {
219                         printf("\n%s*%c", indent(startcol), hang);
220                         col = startcol;
221                 }
222
223                 if (col > startcol && k > 0)
224                         col += printf(" ");
225                 col += printf("%.*s", i - j, &desc[j]);
226         }
227         putchar('\n');
228 }
229
230 static void
231 fail(struct parse_context *ctx, const char *msg)
232 {
233         fprintf(stderr, "%s:%ld: %s\n",
234                 ctx->filename, XML_GetCurrentLineNumber(ctx->parser), msg);
235         exit(EXIT_FAILURE);
236 }
237
238 static int
239 is_nullable_type(struct arg *arg)
240 {
241         switch (arg->type) {
242         /* Strings, objects, and arrays are possibly nullable */
243         case STRING:
244         case OBJECT:
245         case NEW_ID:
246         case ARRAY:
247                 return 1;
248         default:
249                 return 0;
250         }
251 }
252
253 static void
254 start_element(void *data, const char *element_name, const char **atts)
255 {
256         struct parse_context *ctx = data;
257         struct interface *interface;
258         struct message *message;
259         struct arg *arg;
260         struct enumeration *enumeration;
261         struct entry *entry;
262         struct description *description;
263         const char *name, *type, *interface_name, *value, *summary, *since;
264         const char *allow_null;
265         char *end;
266         int i, version;
267
268         name = NULL;
269         type = NULL;
270         version = 0;
271         interface_name = NULL;
272         value = NULL;
273         summary = NULL;
274         description = NULL;
275         since = NULL;
276         allow_null = NULL;
277         for (i = 0; atts[i]; i += 2) {
278                 if (strcmp(atts[i], "name") == 0)
279                         name = atts[i + 1];
280                 if (strcmp(atts[i], "version") == 0)
281                         version = atoi(atts[i + 1]);
282                 if (strcmp(atts[i], "type") == 0)
283                         type = atts[i + 1];
284                 if (strcmp(atts[i], "value") == 0)
285                         value = atts[i + 1];
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)
291                         since = atts[i + 1];
292                 if (strcmp(atts[i], "allow-null") == 0)
293                         allow_null = atts[i + 1];
294         }
295
296         ctx->character_data_length = 0;
297         if (strcmp(element_name, "protocol") == 0) {
298                 if (name == NULL)
299                         fail(ctx, "no protocol name given");
300
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) {
305                 
306         } else if (strcmp(element_name, "interface") == 0) {
307                 if (name == NULL)
308                         fail(ctx, "no interface name given");
309
310                 if (version == 0)
311                         fail(ctx, "no interface version given");
312
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,
323                                &interface->link);
324                 ctx->interface = interface;
325         } else if (strcmp(element_name, "request") == 0 ||
326                    strcmp(element_name, "event") == 0) {
327                 if (name == NULL)
328                         fail(ctx, "no request name given");
329
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;
336
337                 if (strcmp(element_name, "request") == 0)
338                         wl_list_insert(ctx->interface->request_list.prev,
339                                        &message->link);
340                 else
341                         wl_list_insert(ctx->interface->event_list.prev,
342                                        &message->link);
343
344                 if (type != NULL && strcmp(type, "destructor") == 0)
345                         message->destructor = 1;
346                 else
347                         message->destructor = 0;
348
349                 if (since != NULL) {
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;
356                 }
357
358                 message->since = ctx->interface->since;
359
360                 if (strcmp(name, "destroy") == 0 && !message->destructor)
361                         fail(ctx, "destroy request should be destructor type");
362
363                 ctx->message = message;
364         } else if (strcmp(element_name, "arg") == 0) {
365                 if (name == NULL)
366                         fail(ctx, "no argument name given");
367
368                 arg = malloc(sizeof *arg);
369                 arg->name = strdup(name);
370
371                 if (strcmp(type, "int") == 0)
372                         arg->type = INT;
373                 else if (strcmp(type, "uint") == 0)
374                         arg->type = UNSIGNED;
375                 else if (strcmp(type, "fixed") == 0)
376                         arg->type = FIXED;
377                 else if (strcmp(type, "string") == 0)
378                         arg->type = STRING;
379                 else if (strcmp(type, "array") == 0)
380                         arg->type = ARRAY;
381                 else if (strcmp(type, "fd") == 0)
382                         arg->type = FD;
383                 else if (strcmp(type, "new_id") == 0) {
384                         arg->type = NEW_ID;
385                 } else if (strcmp(type, "object") == 0) {
386                         arg->type = OBJECT;
387                 } else {
388                         fail(ctx, "unknown type");
389                 }
390
391                 switch (arg->type) {
392                 case NEW_ID:
393                 case OBJECT:
394                         if (interface_name)
395                                 arg->interface_name = strdup(interface_name);
396                         else
397                                 arg->interface_name = NULL;
398                         break;
399                 default:
400                         if (interface_name != NULL)
401                                 fail(ctx, "interface not allowed");
402                         break;
403                 }
404
405                 if (allow_null == NULL || strcmp(allow_null, "false") == 0)
406                         arg->nullable = 0;
407                 else if (strcmp(allow_null, "true") == 0)
408                         arg->nullable = 1;
409                 else
410                         fail(ctx, "invalid value for allow-null attribute");
411
412                 if (allow_null != NULL && !is_nullable_type(arg))
413                         fail(ctx, "allow-null is only valid for objects, strings, and arrays");
414
415                 arg->summary = NULL;
416                 if (summary)
417                         arg->summary = strdup(summary);
418
419                 wl_list_insert(ctx->message->arg_list.prev, &arg->link);
420                 ctx->message->arg_count++;
421         } else if (strcmp(element_name, "enum") == 0) {
422                 if (name == NULL)
423                         fail(ctx, "no enum name given");
424
425                 enumeration = malloc(sizeof *enumeration);
426                 enumeration->name = strdup(name);
427                 enumeration->uppercase_name = uppercase_dup(name);
428                 enumeration->description = NULL;
429                 wl_list_init(&enumeration->entry_list);
430
431                 wl_list_insert(ctx->interface->enumeration_list.prev,
432                                &enumeration->link);
433
434                 ctx->enumeration = enumeration;
435         } else if (strcmp(element_name, "entry") == 0) {
436                 if (name == NULL)
437                         fail(ctx, "no entry name given");
438
439                 entry = malloc(sizeof *entry);
440                 entry->name = strdup(name);
441                 entry->uppercase_name = uppercase_dup(name);
442                 entry->value = strdup(value);
443                 if (summary)
444                         entry->summary = strdup(summary);
445                 else
446                         entry->summary = NULL;
447                 wl_list_insert(ctx->enumeration->entry_list.prev,
448                                &entry->link);
449         } else if (strcmp(element_name, "description") == 0) {
450                 if (summary == NULL)
451                         fail(ctx, "description without summary");
452
453                 description = malloc(sizeof *description);
454                 description->summary = strdup(summary);
455
456                 if (ctx->message)
457                         ctx->message->description = description;
458                 else if (ctx->enumeration)
459                         ctx->enumeration->description = description;
460                 else if (ctx->interface)
461                         ctx->interface->description = description;
462                 else
463                         ctx->protocol->description = description;
464                 ctx->description = description;
465         }
466 }
467
468 static void
469 end_element(void *data, const XML_Char *name)
470 {
471         struct parse_context *ctx = data;
472
473         if (strcmp(name, "copyright") == 0) {
474                 ctx->protocol->copyright =
475                         strndup(ctx->character_data,
476                                 ctx->character_data_length);
477         } else if (strcmp(name, "description") == 0) {
478                 ctx->description->text =
479                         strndup(ctx->character_data,
480                                 ctx->character_data_length);
481                 ctx->description = NULL;
482         } else if (strcmp(name, "request") == 0 ||
483                    strcmp(name, "event") == 0) {
484                 ctx->message = NULL;
485         } else if (strcmp(name, "enum") == 0) {
486                 ctx->enumeration = NULL;
487         }
488 }
489
490 static void
491 character_data(void *data, const XML_Char *s, int len)
492 {
493         struct parse_context *ctx = data;
494
495         if (ctx->character_data_length + len > sizeof (ctx->character_data)) {
496                 fprintf(stderr, "too much character data");
497                 exit(EXIT_FAILURE);
498             }
499
500         memcpy(ctx->character_data + ctx->character_data_length, s, len);
501         ctx->character_data_length += len;
502 }
503
504 static void
505 emit_opcodes(struct wl_list *message_list, struct interface *interface)
506 {
507         struct message *m;
508         int opcode;
509
510         if (wl_list_empty(message_list))
511                 return;
512
513         opcode = 0;
514         wl_list_for_each(m, message_list, link)
515                 printf("#define %s_%s\t%d\n",
516                        interface->uppercase_name, m->uppercase_name, opcode++);
517
518         printf("\n");
519 }
520
521 static void
522 emit_type(struct arg *a)
523 {
524         switch (a->type) {
525         default:
526         case INT:
527         case FD:
528                 printf("int32_t ");
529                 break;
530         case NEW_ID:
531         case UNSIGNED:
532                 printf("uint32_t ");
533                 break;
534         case FIXED:
535                 printf("wl_fixed_t ");
536                 break;
537         case STRING:
538                 printf("const char *");
539                 break;
540         case OBJECT:
541                 printf("struct %s *", a->interface_name);
542                 break;
543         case ARRAY:
544                 printf("struct wl_array *");
545                 break;
546         }
547 }
548
549 static void
550 emit_stubs(struct wl_list *message_list, struct interface *interface)
551 {
552         struct message *m;
553         struct arg *a, *ret;
554         int has_destructor, has_destroy;
555
556         printf("static inline void\n"
557                "%s_set_user_data(struct %s *%s, void *user_data)\n"
558                "{\n"
559                "\twl_proxy_set_user_data((struct wl_proxy *) %s, user_data);\n"
560                "}\n\n",
561                interface->name, interface->name, interface->name,
562                interface->name);
563
564         printf("static inline void *\n"
565                "%s_get_user_data(struct %s *%s)\n"
566                "{\n"
567                "\treturn wl_proxy_get_user_data((struct wl_proxy *) %s);\n"
568                "}\n\n",
569                interface->name, interface->name, interface->name,
570                interface->name);
571
572         has_destructor = 0;
573         has_destroy = 0;
574         wl_list_for_each(m, message_list, link) {
575                 if (m->destructor)
576                         has_destructor = 1;
577                 if (strcmp(m->name, "destroy)") == 0)
578                         has_destroy = 1;
579         }
580
581         if (!has_destructor && has_destroy) {
582                 fprintf(stderr,
583                         "interface %s has method named destroy but"
584                         "no destructor", interface->name);
585                 exit(EXIT_FAILURE);
586         }
587
588         if (!has_destructor && strcmp(interface->name, "wl_display") != 0)
589                 printf("static inline void\n"
590                        "%s_destroy(struct %s *%s)\n"
591                        "{\n"
592                        "\twl_proxy_destroy("
593                        "(struct wl_proxy *) %s);\n"
594                        "}\n\n",
595                        interface->name, interface->name, interface->name,
596                        interface->name);
597
598         if (wl_list_empty(message_list))
599                 return;
600
601         wl_list_for_each(m, message_list, link) {
602                 ret = NULL;
603                 wl_list_for_each(a, &m->arg_list, link) {
604                         if (a->type == NEW_ID)
605                                 ret = a;
606                 }
607
608                 if (ret && ret->interface_name == NULL)
609                         printf("static inline void *\n");
610                 else if (ret)
611                         printf("static inline struct %s *\n",
612                                ret->interface_name);
613                 else
614                         printf("static inline void\n");
615
616                 printf("%s_%s(struct %s *%s",
617                        interface->name, m->name,
618                        interface->name, interface->name);
619
620                 wl_list_for_each(a, &m->arg_list, link) {
621                         if (a->type == NEW_ID && a->interface_name == NULL) {
622                                 printf(", const struct wl_interface *interface"
623                                        ", uint32_t version");
624                                 continue;
625                         } else if (a->type == NEW_ID)
626                                 continue;
627                         printf(", ");
628                         emit_type(a);
629                         printf("%s", a->name);
630                 }
631
632                 printf(")\n"
633                        "{\n");
634                 if (ret) {
635                         printf("\tstruct wl_proxy *%s;\n\n"
636                                "\t%s = wl_proxy_create("
637                                "(struct wl_proxy *) %s,\n",
638                                ret->name, ret->name, interface->name);
639                         if (ret->interface_name == NULL)
640                                 printf("\t\t\t     interface);\n");
641                         else
642                                 printf("\t\t\t     &%s_interface);\n",
643                                        ret->interface_name);
644
645                         printf("\tif (!%s)\n"
646                                "\t\treturn NULL;\n\n",
647                                ret->name);
648                 }
649
650                 printf("\twl_proxy_marshal((struct wl_proxy *) %s,\n"
651                        "\t\t\t %s_%s",
652                        interface->name,
653                        interface->uppercase_name,
654                        m->uppercase_name);
655
656                 wl_list_for_each(a, &m->arg_list, link) {
657                         if (a->type == NEW_ID && a->interface_name == NULL)
658                                 printf(", interface->name, version");
659                         printf(", ");
660                         printf("%s", a->name);
661                 }
662                 printf(");\n");
663
664                 if (m->destructor)
665                         printf("\n\twl_proxy_destroy("
666                                "(struct wl_proxy *) %s);\n",
667                                interface->name);
668
669                 if (ret && ret->interface_name == NULL)
670                         printf("\n\treturn (void *) %s;\n", ret->name);
671                 else if (ret)
672                         printf("\n\treturn (struct %s *) %s;\n",
673                                ret->interface_name, ret->name);
674
675                 printf("}\n\n");
676         }
677 }
678
679 static void
680 emit_event_wrappers(struct wl_list *message_list, struct interface *interface)
681 {
682         struct message *m;
683         struct arg *a;
684
685         /* We provide hand written functions for the display object */
686         if (strcmp(interface->name, "wl_display") == 0)
687                 return;
688
689         wl_list_for_each(m, message_list, link) {
690                 printf("static inline void\n"
691                        "%s_send_%s(struct wl_resource *resource_",
692                        interface->name, m->name);
693
694                 wl_list_for_each(a, &m->arg_list, link) {
695                         printf(", ");
696                         switch (a->type) {
697                         case NEW_ID:
698                         case OBJECT:
699                                 printf("struct wl_resource *");
700                                 break;
701                         default:
702                                 emit_type(a);
703                         }
704                         printf("%s", a->name);
705                 }
706
707                 printf(")\n"
708                        "{\n"
709                        "\twl_resource_post_event(resource_, %s_%s",
710                        interface->uppercase_name, m->uppercase_name);
711
712                 wl_list_for_each(a, &m->arg_list, link)
713                         printf(", %s", a->name);
714
715                 printf(");\n");
716                 printf("}\n\n");
717         }
718 }
719
720 static void
721 emit_enumerations(struct interface *interface)
722 {
723         struct enumeration *e;
724         struct entry *entry;
725
726         wl_list_for_each(e, &interface->enumeration_list, link) {
727                 struct description *desc = e->description;
728
729                 printf("#ifndef %s_%s_ENUM\n",
730                        interface->uppercase_name, e->uppercase_name);
731                 printf("#define %s_%s_ENUM\n",
732                        interface->uppercase_name, e->uppercase_name);
733
734                 if (desc) {
735                         printf("/**\n");
736                         desc_dump(desc->summary,
737                                   " * %s_%s - ",
738                                   interface->name, e->name);
739                         wl_list_for_each(entry, &e->entry_list, link) {
740                                 desc_dump(entry->summary,
741                                           " * @%s_%s_%s: ",
742                                           interface->uppercase_name,
743                                           e->uppercase_name,
744                                           entry->uppercase_name);
745                         }
746                         if (desc->text) {
747                                 printf(" *\n");
748                                 desc_dump(desc->text, " * ");
749                         }
750                         printf(" */\n");
751                 }
752                 printf("enum %s_%s {\n", interface->name, e->name);
753                 wl_list_for_each(entry, &e->entry_list, link)
754                         printf("\t%s_%s_%s = %s,\n",
755                                interface->uppercase_name,
756                                e->uppercase_name,
757                                entry->uppercase_name, entry->value);
758                 printf("};\n");
759                 printf("#endif /* %s_%s_ENUM */\n\n",
760                        interface->uppercase_name, e->uppercase_name);
761         }
762 }
763
764 static void
765 emit_structs(struct wl_list *message_list, struct interface *interface)
766 {
767         struct message *m;
768         struct arg *a;
769         int is_interface, n;
770
771         if (wl_list_empty(message_list))
772                 return;
773
774         is_interface = message_list == &interface->request_list;
775         if (interface->description) {
776                 struct description *desc = interface->description;
777                 printf("/**\n");
778                 desc_dump(desc->summary, " * %s - ", interface->name);
779                 wl_list_for_each(m, message_list, link) {
780                         struct description *mdesc = m->description;
781                         desc_dump(mdesc ? mdesc->summary : "(none)",
782                                   " * @%s: ",
783                                   m->name);
784                 }
785                 printf(" *\n");
786                 desc_dump(desc->text, " * ");
787                 printf(" */\n");
788         }
789         printf("struct %s_%s {\n", interface->name,
790                is_interface ? "interface" : "listener");
791
792         wl_list_for_each(m, message_list, link) {
793                 struct description *mdesc = m->description;
794
795                 printf("\t/**\n");
796                 desc_dump(mdesc ? mdesc->summary : "(none)",
797                           "\t * %s - ", m->name);
798                 wl_list_for_each(a, &m->arg_list, link) {
799
800                         if (is_interface && a->type == NEW_ID &&
801                             a->interface_name == NULL)
802                                 printf("\t * @interface: name of the objects interface\n"
803                                        "\t * @version: version of the objects interface\n");
804
805
806                         desc_dump(a->summary ? a->summary : "(none)",
807                                   "\t * @%s: ", a->name);
808                 }
809                 if (mdesc) {
810                         printf("\t *\n");
811                         desc_dump(mdesc->text, "\t * ");
812                 }
813                 if (m->since > 1) {
814                         printf("\t * @since: %d\n", m->since);
815                 }
816                 printf("\t */\n");
817                 printf("\tvoid (*%s)(", m->name);
818
819                 n = strlen(m->name) + 17;
820                 if (is_interface) {
821                         printf("struct wl_client *client,\n"
822                                "%sstruct wl_resource *resource",
823                                indent(n));
824                 } else {
825                         printf("void *data,\n"),
826                         printf("%sstruct %s *%s",
827                                indent(n), interface->name, interface->name);
828                 }
829
830                 wl_list_for_each(a, &m->arg_list, link) {
831                         printf(",\n%s", indent(n));
832
833                         if (is_interface && a->type == OBJECT)
834                                 printf("struct wl_resource *");
835                         else if (is_interface && a->type == NEW_ID && a->interface_name == NULL)
836                                 printf("const char *interface, uint32_t version, uint32_t ");
837                         else if (!is_interface && a->type == OBJECT && a->interface_name == NULL)
838                                 printf("struct wl_object *");
839
840                         else if (!is_interface && a->type == NEW_ID)
841                                 printf("struct %s *", a->interface_name);
842                         else
843                                 emit_type(a);
844
845                         printf("%s", a->name);
846                 }
847
848                 printf(");\n");
849         }
850
851         printf("};\n\n");
852
853         if (!is_interface) {
854             printf("static inline int\n"
855                    "%s_add_listener(struct %s *%s,\n"
856                    "%sconst struct %s_listener *listener, void *data)\n"
857                    "{\n"
858                    "\treturn wl_proxy_add_listener((struct wl_proxy *) %s,\n"
859                    "%s(void (**)(void)) listener, data);\n"
860                    "}\n\n",
861                    interface->name, interface->name, interface->name,
862                    indent(14 + strlen(interface->name)),
863                    interface->name,
864                    interface->name,
865                    indent(37));
866         }
867 }
868
869 static void
870 format_copyright(const char *copyright)
871 {
872         int bol = 1, start = 0, i;
873
874         for (i = 0; copyright[i]; i++) {
875                 if (bol && (copyright[i] == ' ' || copyright[i] == '\t')) {
876                         continue;
877                 } else if (bol) {
878                         bol = 0;
879                         start = i;
880                 }
881
882                 if (copyright[i] == '\n' || copyright[i] == '\0') {
883                         printf("%s %.*s\n",
884                                i == 0 ? "/*" : " *",
885                                i - start, copyright + start);
886                         bol = 1;
887                 }
888         }
889         printf(" */\n\n");
890 }
891
892 static void
893 emit_header(struct protocol *protocol, int server)
894 {
895         struct interface *i;
896         const char *s = server ? "SERVER" : "CLIENT";
897
898         if (protocol->copyright)
899                 format_copyright(protocol->copyright);
900
901         printf("#ifndef %s_%s_PROTOCOL_H\n"
902                "#define %s_%s_PROTOCOL_H\n"
903                "\n"
904                "#ifdef  __cplusplus\n"
905                "extern \"C\" {\n"
906                "#endif\n"
907                "\n"
908                "#include <stdint.h>\n"
909                "#include <stddef.h>\n"
910                "#include \"%s\"\n\n"
911                "struct wl_client;\n"
912                "struct wl_resource;\n\n",
913                protocol->uppercase_name, s,
914                protocol->uppercase_name, s,
915                server ? "wayland-util.h" : "wayland-client.h");
916
917         wl_list_for_each(i, &protocol->interface_list, link)
918                 printf("struct %s;\n", i->name);
919         printf("\n");
920
921         wl_list_for_each(i, &protocol->interface_list, link) {
922                 printf("extern const struct wl_interface "
923                        "%s_interface;\n",
924                        i->name);
925         }
926         printf("\n");
927
928         wl_list_for_each(i, &protocol->interface_list, link) {
929
930                 emit_enumerations(i);
931
932                 if (server) {
933                         emit_structs(&i->request_list, i);
934                         emit_opcodes(&i->event_list, i);
935                         emit_event_wrappers(&i->event_list, i);
936                 } else {
937                         emit_structs(&i->event_list, i);
938                         emit_opcodes(&i->request_list, i);
939                         emit_stubs(&i->request_list, i);
940                 }
941         }
942
943         printf("#ifdef  __cplusplus\n"
944                "}\n"
945                "#endif\n"
946                "\n"
947                "#endif\n");
948 }
949
950 static void
951 emit_types_forward_declarations(struct protocol *protocol,
952                                 struct wl_list *message_list)
953 {
954         struct message *m;
955         struct arg *a;
956         int length;
957
958         wl_list_for_each(m, message_list, link) {
959                 length = 0;
960                 m->all_null = 1;
961                 wl_list_for_each(a, &m->arg_list, link) {
962                         length++;
963                         switch (a->type) {
964                         case NEW_ID:
965                         case OBJECT:
966                                 if (!a->interface_name)
967                                         continue;
968
969                                 m->all_null = 0;
970                                 printf("extern const struct wl_interface %s_interface;\n",
971                                        a->interface_name);
972                                 break;
973                         default:
974                                 break;
975                         }
976                 }
977
978                 if (m->all_null && length > protocol->null_run_length)
979                         protocol->null_run_length = length;
980         }
981 }
982
983 static void
984 emit_null_run(struct protocol *protocol)
985 {
986         int i;
987
988         for (i = 0; i < protocol->null_run_length; i++)
989                 printf("\tNULL,\n");
990 }
991
992 static void
993 emit_types(struct protocol *protocol, struct wl_list *message_list)
994 {
995         struct message *m;
996         struct arg *a;
997
998         wl_list_for_each(m, message_list, link) {
999                 if (m->all_null) {
1000                         m->type_index = 0;
1001                         continue;
1002                 }
1003
1004                 m->type_index =
1005                         protocol->null_run_length + protocol->type_index;
1006                 protocol->type_index += m->arg_count;
1007
1008                 wl_list_for_each(a, &m->arg_list, link) {
1009                         switch (a->type) {
1010                         case NEW_ID:
1011                         case OBJECT:
1012                                 if (a->interface_name)
1013                                         printf("\t&%s_interface,\n",
1014                                                a->interface_name);
1015                                 else
1016                                         printf("\tNULL,\n");
1017                                 break;
1018                         default:
1019                                 printf("\tNULL,\n");
1020                                 break;
1021                         }
1022                 }
1023         }
1024 }
1025
1026 static void
1027 emit_messages(struct wl_list *message_list,
1028               struct interface *interface, const char *suffix)
1029 {
1030         struct message *m;
1031         struct arg *a;
1032
1033         if (wl_list_empty(message_list))
1034                 return;
1035
1036         printf("static const struct wl_message "
1037                "%s_%s[] = {\n",
1038                interface->name, suffix);
1039
1040         wl_list_for_each(m, message_list, link) {
1041                 printf("\t{ \"%s\", \"", m->name);
1042                 wl_list_for_each(a, &m->arg_list, link) {
1043                         if (is_nullable_type(a) && a->nullable)
1044                                 printf("?");
1045
1046                         switch (a->type) {
1047                         default:
1048                         case INT:
1049                                 printf("i");
1050                                 break;
1051                         case NEW_ID:
1052                                 if (a->interface_name == NULL)
1053                                         printf("su");
1054                                 printf("n");
1055                                 break;
1056                         case UNSIGNED:
1057                                 printf("u");
1058                                 break;
1059                         case FIXED:
1060                                 printf("f");
1061                                 break;
1062                         case STRING:
1063                                 printf("s");
1064                                 break;
1065                         case OBJECT:
1066                                 printf("o");
1067                                 break;
1068                         case ARRAY:
1069                                 printf("a");
1070                                 break;
1071                         case FD:
1072                                 printf("h");
1073                                 break;
1074                         }
1075                 }
1076                 printf("\", types + %d },\n", m->type_index);
1077         }
1078
1079         printf("};\n\n");
1080 }
1081
1082 static void
1083 emit_code(struct protocol *protocol)
1084 {
1085         struct interface *i;
1086
1087         if (protocol->copyright)
1088                 format_copyright(protocol->copyright);
1089
1090         printf("#include <stdlib.h>\n"
1091                "#include <stdint.h>\n"
1092                "#include \"wayland-util.h\"\n\n");
1093
1094         wl_list_for_each(i, &protocol->interface_list, link) {
1095                 emit_types_forward_declarations(protocol, &i->request_list);
1096                 emit_types_forward_declarations(protocol, &i->event_list);
1097         }
1098         printf("\n");
1099
1100         printf("static const struct wl_interface *types[] = {\n");
1101         emit_null_run(protocol);
1102         wl_list_for_each(i, &protocol->interface_list, link) {
1103                 emit_types(protocol, &i->request_list);
1104                 emit_types(protocol, &i->event_list);
1105         }
1106         printf("};\n\n");
1107
1108         wl_list_for_each(i, &protocol->interface_list, link) {
1109
1110                 emit_messages(&i->request_list, i, "requests");
1111                 emit_messages(&i->event_list, i, "events");
1112
1113                 printf("WL_EXPORT const struct wl_interface "
1114                        "%s_interface = {\n"
1115                        "\t\"%s\", %d,\n",
1116                        i->name, i->name, i->version);
1117
1118                 if (!wl_list_empty(&i->request_list))
1119                         printf("\t%d, %s_requests,\n",
1120                                list_length(&i->request_list), i->name);
1121                 else
1122                         printf("\t0, NULL,\n");
1123
1124                 if (!wl_list_empty(&i->event_list))
1125                         printf("\t%d, %s_events,\n",
1126                                list_length(&i->event_list), i->name);
1127                 else
1128                         printf("\t0, NULL,\n");
1129
1130                 printf("};\n\n");
1131         }
1132 }
1133
1134 int main(int argc, char *argv[])
1135 {
1136         struct parse_context ctx;
1137         struct protocol protocol;
1138         int len;
1139         void *buf;
1140
1141         if (argc != 2)
1142                 usage(EXIT_FAILURE);
1143
1144         wl_list_init(&protocol.interface_list);
1145         protocol.type_index = 0;
1146         protocol.null_run_length = 0;
1147         protocol.copyright = NULL;
1148         memset(&ctx, 0, sizeof ctx);
1149         ctx.protocol = &protocol;
1150
1151         ctx.filename = "<stdin>";
1152         ctx.parser = XML_ParserCreate(NULL);
1153         XML_SetUserData(ctx.parser, &ctx);
1154         if (ctx.parser == NULL) {
1155                 fprintf(stderr, "failed to create parser\n");
1156                 exit(EXIT_FAILURE);
1157         }
1158
1159         XML_SetElementHandler(ctx.parser, start_element, end_element);
1160         XML_SetCharacterDataHandler(ctx.parser, character_data);
1161
1162         do {
1163                 buf = XML_GetBuffer(ctx.parser, XML_BUFFER_SIZE);
1164                 len = fread(buf, 1, XML_BUFFER_SIZE, stdin);
1165                 if (len < 0) {
1166                         fprintf(stderr, "fread: %m\n");
1167                         exit(EXIT_FAILURE);
1168                 }
1169                 XML_ParseBuffer(ctx.parser, len, len == 0);
1170
1171         } while (len > 0);
1172
1173         XML_ParserFree(ctx.parser);
1174
1175         if (strcmp(argv[1], "client-header") == 0) {
1176                 emit_header(&protocol, 0);
1177         } else if (strcmp(argv[1], "server-header") == 0) {
1178                 emit_header(&protocol, 1);
1179         } else if (strcmp(argv[1], "code") == 0) {
1180                 emit_code(&protocol);
1181         }
1182
1183         return 0;
1184 }