1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-gidl.c data structure describing an interface, to be generated from IDL
3  *             or something
4  *
5  * Copyright (C) 2003, 2005  Red Hat, Inc.
6  *
7  * SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
8  *
9  * Licensed under the Academic Free License version 2.1
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
24  *
25  */
26 
27 #include <config.h>
28 
29 #include "dbus-gidl.h"
30 
31 #include <gio/gio.h>
32 
33 struct BaseInfo
34 {
35   unsigned int refcount : 28;
36   unsigned int type     : 4;
37   char *name;
38 };
39 
40 struct NodeInfo
41 {
42   BaseInfo base;
43   GSList *interfaces;
44   GSList *nodes;
45 };
46 
47 struct InterfaceInfo
48 {
49   BaseInfo base;
50   GHashTable *annotations;
51   /* Since we have BaseInfo now these could be one list */
52   GSList *methods;
53   GSList *signals;
54   GSList *properties;
55 };
56 
57 struct MethodInfo
58 {
59   BaseInfo base;
60   GHashTable *annotations;
61   GSList *args;
62 };
63 
64 struct SignalInfo
65 {
66   BaseInfo base;
67   GSList *args;
68 };
69 
70 struct PropertyInfo
71 {
72   BaseInfo base;
73   char *type;
74   PropertyAccessFlags access;
75 };
76 
77 struct ArgInfo
78 {
79   BaseInfo base;
80   char *type;
81   ArgDirection direction;
82   GHashTable *annotations;
83 };
84 
85 static void
get_hash_key(gpointer key,gpointer value,gpointer data)86 get_hash_key (gpointer key, gpointer value, gpointer data)
87 {
88   GSList **list = data;
89   *list = g_slist_prepend (*list, key);
90 }
91 
92 static GSList *
get_hash_keys(GHashTable * table)93 get_hash_keys (GHashTable *table)
94 {
95   GSList *ret = NULL;
96 
97   g_hash_table_foreach (table, get_hash_key, &ret);
98 
99   return ret;
100 }
101 
102 BaseInfo *
base_info_ref(BaseInfo * info)103 base_info_ref (BaseInfo *info)
104 {
105   g_return_val_if_fail (info != NULL, NULL);
106   g_return_val_if_fail (info->refcount > 0, NULL);
107 
108   info->refcount += 1;
109 
110   return info;
111 }
112 
113 static void
base_info_free(void * ptr)114 base_info_free (void *ptr)
115 {
116   BaseInfo *info;
117 
118   info = ptr;
119 
120   g_free (info->name);
121   g_free (info);
122 }
123 
124 void
base_info_unref(BaseInfo * info)125 base_info_unref (BaseInfo *info)
126 {
127   g_return_if_fail (info != NULL);
128   g_return_if_fail (info->refcount > 0);
129 
130   /* This is sort of bizarre, BaseInfo was tacked on later */
131 
132   switch (info->type)
133     {
134     case INFO_TYPE_NODE:
135       node_info_unref ((NodeInfo*) info);
136       break;
137     case INFO_TYPE_INTERFACE:
138       interface_info_unref ((InterfaceInfo*) info);
139       break;
140     case INFO_TYPE_SIGNAL:
141       signal_info_unref ((SignalInfo*) info);
142       break;
143     case INFO_TYPE_METHOD:
144       method_info_unref ((MethodInfo*) info);
145       break;
146     case INFO_TYPE_PROPERTY:
147       property_info_unref ((PropertyInfo*) info);
148       break;
149     case INFO_TYPE_ARG:
150       arg_info_unref ((ArgInfo*) info);
151       break;
152     }
153 }
154 
155 InfoType
base_info_get_type(BaseInfo * info)156 base_info_get_type (BaseInfo      *info)
157 {
158   return info->type;
159 }
160 
161 const char*
base_info_get_name(BaseInfo * info)162 base_info_get_name (BaseInfo *info)
163 {
164   return info->name;
165 }
166 
167 void
base_info_set_name(BaseInfo * info,const char * name)168 base_info_set_name (BaseInfo      *info,
169                     const char    *name)
170 {
171   char *old;
172 
173   old = info->name;
174   info->name = g_strdup (name);
175   g_free (old);
176 }
177 
178 GType
base_info_get_gtype(void)179 base_info_get_gtype (void)
180 {
181   static GType our_type = 0;
182 
183   if (our_type == 0)
184     our_type = g_boxed_type_register_static ("BaseInfo",
185                                              (GBoxedCopyFunc) base_info_ref,
186                                              (GBoxedFreeFunc) base_info_unref);
187 
188   return our_type;
189 }
190 
191 static void
free_interface_list(GSList ** interfaces_p)192 free_interface_list (GSList **interfaces_p)
193 {
194   GSList *tmp;
195   tmp = *interfaces_p;
196   while (tmp != NULL)
197     {
198       interface_info_unref (tmp->data);
199       tmp = tmp->next;
200     }
201   g_slist_free (*interfaces_p);
202   *interfaces_p = NULL;
203 }
204 
205 static void
free_node_list(GSList ** nodes_p)206 free_node_list (GSList **nodes_p)
207 {
208   GSList *tmp;
209   tmp = *nodes_p;
210   while (tmp != NULL)
211     {
212       node_info_unref (tmp->data);
213       tmp = tmp->next;
214     }
215   g_slist_free (*nodes_p);
216   *nodes_p = NULL;
217 }
218 
219 static void
free_method_list(GSList ** methods_p)220 free_method_list (GSList **methods_p)
221 {
222   GSList *tmp;
223   tmp = *methods_p;
224   while (tmp != NULL)
225     {
226       method_info_unref (tmp->data);
227       tmp = tmp->next;
228     }
229   g_slist_free (*methods_p);
230   *methods_p = NULL;
231 }
232 
233 static void
free_signal_list(GSList ** signals_p)234 free_signal_list (GSList **signals_p)
235 {
236   GSList *tmp;
237   tmp = *signals_p;
238   while (tmp != NULL)
239     {
240       signal_info_unref (tmp->data);
241       tmp = tmp->next;
242     }
243   g_slist_free (*signals_p);
244   *signals_p = NULL;
245 }
246 
247 static void
free_property_list(GSList ** props_p)248 free_property_list (GSList **props_p)
249 {
250   GSList *tmp;
251   tmp = *props_p;
252   while (tmp != NULL)
253     {
254       property_info_unref (tmp->data);
255       tmp = tmp->next;
256     }
257   g_slist_free (*props_p);
258   *props_p = NULL;
259 }
260 
261 NodeInfo*
node_info_new(const char * name)262 node_info_new (const char *name)
263 {
264   NodeInfo *info;
265 
266   /* name can be NULL */
267 
268   info = g_new0 (NodeInfo, 1);
269   info->base.refcount = 1;
270   info->base.name = g_strdup (name);
271   info->base.type = INFO_TYPE_NODE;
272 
273   return info;
274 }
275 
276 NodeInfo *
node_info_ref(NodeInfo * info)277 node_info_ref (NodeInfo *info)
278 {
279   info->base.refcount += 1;
280 
281   return info;
282 }
283 
284 void
node_info_unref(NodeInfo * info)285 node_info_unref (NodeInfo *info)
286 {
287   info->base.refcount -= 1;
288   if (info->base.refcount == 0)
289     {
290       free_interface_list (&info->interfaces);
291       free_node_list (&info->nodes);
292       base_info_free (info);
293     }
294 }
295 
296 const char*
node_info_get_name(NodeInfo * info)297 node_info_get_name (NodeInfo *info)
298 {
299   return info->base.name;
300 }
301 
302 GSList*
node_info_get_interfaces(NodeInfo * info)303 node_info_get_interfaces (NodeInfo *info)
304 {
305   return info->interfaces;
306 }
307 
308 void
node_info_add_interface(NodeInfo * info,InterfaceInfo * interface)309 node_info_add_interface (NodeInfo *info,
310                          InterfaceInfo    *interface)
311 {
312   interface_info_ref (interface);
313   info->interfaces = g_slist_append (info->interfaces, interface);
314 }
315 
316 GSList*
node_info_get_nodes(NodeInfo * info)317 node_info_get_nodes (NodeInfo *info)
318 {
319   return info->nodes;
320 }
321 
322 void
node_info_add_node(NodeInfo * info,NodeInfo * node)323 node_info_add_node (NodeInfo *info,
324                     NodeInfo *node)
325 {
326   node_info_ref (node);
327   info->nodes = g_slist_append (info->nodes, node);
328 }
329 
330 void
node_info_replace_node(NodeInfo * info,NodeInfo * old_child,NodeInfo * new_child)331 node_info_replace_node (NodeInfo            *info,
332                         NodeInfo            *old_child,
333                         NodeInfo            *new_child)
334 {
335   GSList *link;
336 
337   node_info_ref (new_child); /* before unref old_child in case they are the same */
338   link = g_slist_find (info->nodes, old_child);
339   g_assert (link != NULL);
340   node_info_unref (old_child);
341   link->data = new_child;
342 }
343 
344 InterfaceInfo*
interface_info_new(const char * name)345 interface_info_new (const char *name)
346 {
347   InterfaceInfo *info;
348 
349   g_return_val_if_fail (g_dbus_is_interface_name (name), NULL);
350 
351   info = g_new0 (InterfaceInfo, 1);
352   info->base.refcount = 1;
353   info->base.name = g_strdup (name);
354   info->base.type = INFO_TYPE_INTERFACE;
355   info->annotations = g_hash_table_new_full (g_str_hash, g_str_equal,
356 					     (GDestroyNotify) g_free,
357 					     (GDestroyNotify) g_free);
358 
359   return info;
360 }
361 
362 InterfaceInfo *
interface_info_ref(InterfaceInfo * info)363 interface_info_ref (InterfaceInfo *info)
364 {
365   info->base.refcount += 1;
366 
367   return info;
368 }
369 
370 void
interface_info_unref(InterfaceInfo * info)371 interface_info_unref (InterfaceInfo *info)
372 {
373   info->base.refcount -= 1;
374   if (info->base.refcount == 0)
375     {
376       g_hash_table_destroy (info->annotations);
377       free_method_list (&info->methods);
378       free_signal_list (&info->signals);
379       free_property_list (&info->properties);
380       base_info_free (info);
381     }
382 }
383 
384 const char*
interface_info_get_name(InterfaceInfo * info)385 interface_info_get_name (InterfaceInfo *info)
386 {
387   return info->base.name;
388 }
389 
390 GSList *
interface_info_get_annotations(InterfaceInfo * info)391 interface_info_get_annotations (InterfaceInfo *info)
392 {
393   return get_hash_keys (info->annotations);
394 }
395 
396 const char*
interface_info_get_annotation(InterfaceInfo * info,const char * name)397 interface_info_get_annotation (InterfaceInfo *info,
398 			       const char    *name)
399 {
400   return g_hash_table_lookup (info->annotations, name);
401 }
402 
403 GSList*
interface_info_get_methods(InterfaceInfo * info)404 interface_info_get_methods (InterfaceInfo *info)
405 {
406   return info->methods;
407 }
408 
409 GSList*
interface_info_get_signals(InterfaceInfo * info)410 interface_info_get_signals (InterfaceInfo *info)
411 {
412   return info->signals;
413 }
414 
415 GSList*
interface_info_get_properties(InterfaceInfo * info)416 interface_info_get_properties (InterfaceInfo *info)
417 {
418   return info->properties;
419 }
420 
421 void
interface_info_add_annotation(InterfaceInfo * info,const char * name,const char * value)422 interface_info_add_annotation (InterfaceInfo *info,
423 			       const char    *name,
424 			       const char    *value)
425 {
426   g_hash_table_insert (info->annotations,
427 		       g_strdup (name),
428 		       g_strdup (value));
429 }
430 
431 void
interface_info_add_method(InterfaceInfo * info,MethodInfo * method)432 interface_info_add_method (InterfaceInfo *info,
433                            MethodInfo    *method)
434 {
435   method_info_ref (method);
436   info->methods = g_slist_append (info->methods, method);
437 }
438 
439 void
interface_info_add_signal(InterfaceInfo * info,SignalInfo * signal)440 interface_info_add_signal (InterfaceInfo *info,
441                            SignalInfo    *signal)
442 {
443   signal_info_ref (signal);
444   info->signals = g_slist_append (info->signals, signal);
445 }
446 
447 void
interface_info_add_property(InterfaceInfo * info,PropertyInfo * property)448 interface_info_add_property (InterfaceInfo *info,
449                              PropertyInfo  *property)
450 {
451   property_info_ref (property);
452   info->properties = g_slist_append (info->properties, property);
453 }
454 
455 static void
free_arg_list(GSList ** args_p)456 free_arg_list (GSList **args_p)
457 {
458   GSList *tmp;
459   tmp = *args_p;
460   while (tmp != NULL)
461     {
462       ArgInfo *ai = tmp->data;
463       g_assert (ai->base.type == INFO_TYPE_ARG);
464       arg_info_unref (tmp->data);
465       tmp = tmp->next;
466     }
467   g_slist_free (*args_p);
468   *args_p = NULL;
469 }
470 
471 MethodInfo*
method_info_new(const char * name)472 method_info_new (const char *name)
473 {
474   MethodInfo *info;
475 
476   g_return_val_if_fail (g_dbus_is_member_name (name), NULL);
477 
478   info = g_new0 (MethodInfo, 1);
479   info->base.refcount = 1;
480   info->base.name = g_strdup (name);
481   info->base.type = INFO_TYPE_METHOD;
482   info->annotations = g_hash_table_new_full (g_str_hash, g_str_equal,
483 					  (GDestroyNotify) g_free,
484 					  (GDestroyNotify) g_free);
485 
486   return info;
487 }
488 
489 MethodInfo *
method_info_ref(MethodInfo * info)490 method_info_ref (MethodInfo *info)
491 {
492   info->base.refcount += 1;
493 
494   return info;
495 }
496 
497 void
method_info_unref(MethodInfo * info)498 method_info_unref (MethodInfo *info)
499 {
500   info->base.refcount -= 1;
501   if (info->base.refcount == 0)
502     {
503       g_hash_table_destroy (info->annotations);
504       free_arg_list (&info->args);
505       base_info_free (info);
506     }
507 }
508 
509 const char*
method_info_get_name(MethodInfo * info)510 method_info_get_name (MethodInfo *info)
511 {
512   return info->base.name;
513 }
514 
515 GSList *
method_info_get_annotations(MethodInfo * info)516 method_info_get_annotations (MethodInfo *info)
517 {
518   return get_hash_keys (info->annotations);
519 }
520 
521 const char*
method_info_get_annotation(MethodInfo * info,const char * name)522 method_info_get_annotation (MethodInfo *info,
523 			    const char *name)
524 {
525   return g_hash_table_lookup (info->annotations, name);
526 }
527 
528 GSList*
method_info_get_args(MethodInfo * info)529 method_info_get_args (MethodInfo *info)
530 {
531   return info->args;
532 }
533 
534 int
method_info_get_n_args(MethodInfo * info)535 method_info_get_n_args (MethodInfo *info)
536 {
537   return g_slist_length (info->args);
538 }
539 
540 static int
args_sort_by_direction(const void * a,const void * b)541 args_sort_by_direction (const void *a,
542                         const void *b)
543 {
544   const ArgInfo *arg_a = a;
545   const ArgInfo *arg_b = b;
546 
547   if (arg_a->direction == arg_b->direction)
548     return 0;
549   else if (arg_a->direction == ARG_IN)
550     return -1; /* in is less than out */
551   else
552     return 1;
553 }
554 
555 void
method_info_add_annotation(MethodInfo * info,const char * name,const char * value)556 method_info_add_annotation (MethodInfo  *info,
557 			    const char  *name,
558 			    const char  *value)
559 {
560   g_hash_table_insert (info->annotations,
561 		       g_strdup (name),
562 		       g_strdup (value));
563 }
564 
565 void
method_info_add_arg(MethodInfo * info,ArgInfo * arg)566 method_info_add_arg (MethodInfo    *info,
567                      ArgInfo       *arg)
568 {
569   arg_info_ref (arg);
570   info->args = g_slist_append (info->args, arg);
571 
572   /* Keep "in" args sorted before "out" and otherwise maintain
573    * stable order (g_slist_sort is stable, at least in sufficiently
574    * new glib)
575    */
576   info->args = g_slist_sort (info->args, args_sort_by_direction);
577 }
578 
579 SignalInfo*
signal_info_new(const char * name)580 signal_info_new (const char *name)
581 {
582   SignalInfo *info;
583 
584   g_return_val_if_fail (g_dbus_is_member_name (name), NULL);
585 
586   info = g_new0 (SignalInfo, 1);
587   info->base.refcount = 1;
588   info->base.name = g_strdup (name);
589   info->base.type = INFO_TYPE_SIGNAL;
590 
591   return info;
592 }
593 
594 SignalInfo *
signal_info_ref(SignalInfo * info)595 signal_info_ref (SignalInfo *info)
596 {
597   info->base.refcount += 1;
598 
599   return info;
600 }
601 
602 void
signal_info_unref(SignalInfo * info)603 signal_info_unref (SignalInfo *info)
604 {
605   info->base.refcount -= 1;
606   if (info->base.refcount == 0)
607     {
608       free_arg_list (&info->args);
609       base_info_free (info);
610     }
611 }
612 
613 const char*
signal_info_get_name(SignalInfo * info)614 signal_info_get_name (SignalInfo *info)
615 {
616   return info->base.name;
617 }
618 
619 GSList*
signal_info_get_args(SignalInfo * info)620 signal_info_get_args (SignalInfo *info)
621 {
622   return info->args;
623 }
624 
625 int
signal_info_get_n_args(SignalInfo * info)626 signal_info_get_n_args (SignalInfo *info)
627 {
628   return g_slist_length (info->args);
629 }
630 
631 void
signal_info_add_arg(SignalInfo * info,ArgInfo * arg)632 signal_info_add_arg (SignalInfo    *info,
633                      ArgInfo       *arg)
634 {
635   g_assert (arg->direction == ARG_OUT);
636 
637   arg_info_ref (arg);
638   info->args = g_slist_append (info->args, arg);
639 
640   /* signal args don't need sorting since only "out" is allowed */
641 }
642 
643 PropertyInfo*
property_info_new(const char * name,const char * type,PropertyAccessFlags access)644 property_info_new (const char          *name,
645                    const char          *type,
646                    PropertyAccessFlags  access)
647 {
648   PropertyInfo *info;
649 
650   g_return_val_if_fail (g_utf8_validate (name, -1, NULL), NULL);
651   g_return_val_if_fail (g_variant_is_signature (type), NULL);
652 
653   info = g_new0 (PropertyInfo, 1);
654   info->base.refcount = 1;
655   info->base.name = g_strdup (name);
656   info->base.type = INFO_TYPE_PROPERTY;
657 
658   info->type = g_strdup (type);
659   info->access = access;
660 
661   return info;
662 }
663 
664 PropertyInfo*
property_info_ref(PropertyInfo * info)665 property_info_ref (PropertyInfo *info)
666 {
667   info->base.refcount += 1;
668 
669   return info;
670 }
671 
672 void
property_info_unref(PropertyInfo * info)673 property_info_unref (PropertyInfo *info)
674 {
675   info->base.refcount -= 1;
676   if (info->base.refcount == 0)
677     {
678       g_free (info->type);
679       base_info_free (info);
680     }
681 }
682 
683 const char*
property_info_get_name(PropertyInfo * info)684 property_info_get_name (PropertyInfo *info)
685 {
686   return info->base.name;
687 }
688 
689 const char *
property_info_get_type(PropertyInfo * info)690 property_info_get_type (PropertyInfo *info)
691 {
692   return info->type;
693 }
694 
695 PropertyAccessFlags
property_info_get_access(PropertyInfo * info)696 property_info_get_access (PropertyInfo *info)
697 {
698   return info->access;
699 }
700 
701 ArgInfo*
arg_info_new(const char * name,ArgDirection direction,const char * type)702 arg_info_new (const char  *name,
703               ArgDirection direction,
704               const char  *type)
705 {
706   ArgInfo *info;
707 
708   info = g_new0 (ArgInfo, 1);
709   info->base.refcount = 1;
710   info->base.type = INFO_TYPE_ARG;
711 
712   /* name can be NULL */
713   info->base.name = g_strdup (name);
714   info->direction = direction;
715   info->type = g_strdup (type);
716   info->annotations = g_hash_table_new_full (g_str_hash, g_str_equal,
717 					     (GDestroyNotify) g_free,
718 					     (GDestroyNotify) g_free);
719 
720   return info;
721 }
722 
723 ArgInfo *
arg_info_ref(ArgInfo * info)724 arg_info_ref (ArgInfo *info)
725 {
726   info->base.refcount += 1;
727 
728   return info;
729 }
730 
731 void
arg_info_unref(ArgInfo * info)732 arg_info_unref (ArgInfo *info)
733 {
734   info->base.refcount -= 1;
735   if (info->base.refcount == 0)
736     {
737       g_hash_table_destroy (info->annotations);
738       g_free (info->type);
739       base_info_free (info);
740     }
741 }
742 
743 const char*
arg_info_get_name(ArgInfo * info)744 arg_info_get_name (ArgInfo *info)
745 {
746   return info->base.name;
747 }
748 
749 const char *
arg_info_get_type(ArgInfo * info)750 arg_info_get_type (ArgInfo *info)
751 {
752   return info->type;
753 }
754 
755 ArgDirection
arg_info_get_direction(ArgInfo * info)756 arg_info_get_direction (ArgInfo *info)
757 {
758   return info->direction;
759 }
760 
761 GSList*
arg_info_get_annotations(ArgInfo * info)762 arg_info_get_annotations (ArgInfo *info)
763 {
764   return get_hash_keys (info->annotations);
765 }
766 
767 const char*
arg_info_get_annotation(ArgInfo * info,const char * annotation)768 arg_info_get_annotation (ArgInfo    *info,
769 			 const char *annotation)
770 {
771   return g_hash_table_lookup (info->annotations, annotation);
772 }
773 
774 void
arg_info_add_annotation(ArgInfo * info,const char * name,const char * value)775 arg_info_add_annotation (ArgInfo             *info,
776 			 const char          *name,
777 			 const char          *value)
778 {
779   g_hash_table_insert (info->annotations,
780 		       g_strdup (name),
781 		       g_strdup (value));
782 }
783