summaryrefslogtreecommitdiff
path: root/src/examples/eldbus/dbusmodel.c
blob: 10150c40f27c08c93c0c67f3e487fd7fe6f241ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//Compile with:
// gcc -o busmodel busmodel.c `pkg-config --cflags --libs eldbus ecore eina`

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Eldbus.h>
#include <Eldbus_Model.h>
#include <Efl.h>
#include <Ecore.h>
#include <Ecore_Getopt.h>

#define DEFAULT_BUS_NAME  "org.freedesktop.DBus"
#define DEFAULT_PATH "/"

static int retval = EXIT_SUCCESS;
static Eina_Bool quit_on_done = EINA_TRUE;

static void
_on_properties_changed(void *data EINA_UNUSED, const Efl_Event *event)
{
   Efl_Model_Property_Event *ev = event->info;
   Eina_Array_Iterator it;
   const char *property;
   unsigned int i;

   printf("Properties changed:\n");
   EINA_ARRAY_ITER_NEXT(ev->changed_properties, i, property, it)
     {
        Eina_Value *v;
        char *str;

        v = efl_model_property_get(event->object, property);

        if (!v)
          {
             EINA_LOG_CRIT("Property '%s' returned nothing.", property);
             abort();
          }

        str = eina_value_to_string(v);
        printf("\t%s: '%s'\n", property, str);
        free(str);

        eina_value_free(v);
     }

   printf("Properties invalidated:\n");
   EINA_ARRAY_ITER_NEXT(ev->invalidated_properties, i, property, it)
     printf("\t%s\n", property);
}

static void
_on_invalidate(void *data EINA_UNUSED, const Efl_Event *event)
{
   efl_unref(event->object);
}

EFL_CALLBACKS_ARRAY_DEFINE(child_cbs,
                           { EFL_MODEL_EVENT_PROPERTIES_CHANGED, _on_properties_changed },
                           { EFL_EVENT_INVALIDATE, _on_invalidate });

static void
process(Eo *child, unsigned int index)
{
   Eina_Array *properties = efl_model_properties_get(child);
   const char *property;
   Eina_Array_Iterator it;
   Eina_Strbuf *buf;
   unsigned int i;

   buf = eina_strbuf_new();

   const char *name = eldbus_model_proxy_name_get(child);

   EINA_ARRAY_ITER_NEXT(properties, i, property, it)
     {
        Eina_Value *v = efl_model_property_get(child, property);
        char *str;

        if (!v)
          {
             EINA_LOG_CRIT("Property '%s' returned nothing.", property);
             abort();
          }

        str = eina_value_to_string(v);
        eina_strbuf_append_printf(buf, " \t* %s: '%s'\n", property, str);
        free(str);

        eina_value_free(v);
     }

   if (eina_array_count(properties) <= 0)
     eina_strbuf_append_printf(buf,  " %2d: %s (no properties yet)\n", index, name);
   else
     eina_strbuf_prepend_printf(buf, " -> %s\n   Properties:\n", name);

   printf("%s", eina_strbuf_string_get(buf));

   eina_array_free(properties);
   eina_strbuf_free(buf);

   efl_ref(child);

   efl_event_callback_array_add(child, child_cbs(), NULL);

   printf("monitoring events...\n");
}

static Eina_Value
_slice(void *data, const Eina_Value v,
       const Eina_Future *dead_future EINA_UNUSED)
{
   unsigned int offset = (unsigned int)(uintptr_t) data;
   unsigned int i, len;
   Eo *child;

   if (eina_value_type_get(&v) == EINA_VALUE_TYPE_ERROR) return v;

   EINA_VALUE_ARRAY_FOREACH(&v, len, i, child)
     process(child, offset + i);

   return v;
}

static void
_on_child_added(void *data EINA_UNUSED, const Efl_Event *event)
{
   Efl_Model_Children_Event *ev = event->info;

   printf("Children Added: %i\n", ev->index);

   eina_future_then(efl_model_children_slice_get(event->object, ev->index, 1),
                    _slice, (void*)(uintptr_t) ev->index);
}

static void
_on_child_removed(void *data EINA_UNUSED, const Efl_Event *event)
{
   Eo *child = event->info;
   printf("Children Removed: %p\n", child);
}

EFL_CALLBACKS_ARRAY_DEFINE(event_cbs,
                           { EFL_MODEL_EVENT_PROPERTIES_CHANGED, _on_properties_changed },
                           { EFL_MODEL_EVENT_CHILD_ADDED, _on_child_added },
                           { EFL_MODEL_EVENT_CHILD_REMOVED, _on_child_removed });

static const Ecore_Getopt options = {
  "dbusmodel", /* program name */
  NULL, /* usage line */
  "1", /* version */
  "(C) 2016 Enlightenment Project", /* copyright */
  "BSD 2-Clause", /* license */
  /* long description, may be multiline and contain \n */
  "Example of Eldbus.Model.Object to fetch children and properties.\n",
  EINA_FALSE,
  {
    ECORE_GETOPT_STORE_TRUE('s', "system", "connect to the system bus, not user session."),
    ECORE_GETOPT_STORE_FALSE('w', "wait", "after done, wait for events (monitoring)"),

    ECORE_GETOPT_VERSION('V', "version"),
    ECORE_GETOPT_COPYRIGHT('C', "copyright"),
    ECORE_GETOPT_LICENSE('L', "license"),
    ECORE_GETOPT_HELP('h', "help"),

    ECORE_GETOPT_STORE_METAVAR_STR(0, NULL, "The bus name to connect.", "bus_name"),
    ECORE_GETOPT_STORE_METAVAR_STR(0, NULL, "The path to explore.", "path"),

    ECORE_GETOPT_SENTINEL
  }
};

int
main(int argc, char **argv EINA_UNUSED)
{
   Eldbus_Connection_Type conn_type;
   Eina_Bool is_system = EINA_FALSE;
   char *bus_name = DEFAULT_BUS_NAME;
   char *path = DEFAULT_PATH;
   Eina_Bool quit_option = EINA_FALSE;
   Ecore_Getopt_Value values[] = {
     ECORE_GETOPT_VALUE_BOOL(is_system),
     ECORE_GETOPT_VALUE_BOOL(quit_on_done),

     /* standard block to provide version, copyright, license and help */
     ECORE_GETOPT_VALUE_BOOL(quit_option), /* -V/--version quits */
     ECORE_GETOPT_VALUE_BOOL(quit_option), /* -C/--copyright quits */
     ECORE_GETOPT_VALUE_BOOL(quit_option), /* -L/--license quits */
     ECORE_GETOPT_VALUE_BOOL(quit_option), /* -h/--help quits */

     /* positional argument */
     ECORE_GETOPT_VALUE_STR(bus_name),
     ECORE_GETOPT_VALUE_STR(path),

     ECORE_GETOPT_VALUE_NONE /* sentinel */
   };
   int args;
   Eo *root;

   ecore_init();
   eldbus_init();

   args = ecore_getopt_parse(&options, values, argc, argv);
   if (args < 0)
     {
        fputs("ERROR: Could not parse command line options.\n", stderr);
        retval = EXIT_FAILURE;
        goto end;
     }

   if (quit_option) goto end;

   args = ecore_getopt_parse_positional(&options, values, argc, argv, args);
   if (args < 0)
     {
        fputs("ERROR: Could not parse positional arguments.\n", stderr);
        retval = EXIT_FAILURE;
        goto end;
     }

   conn_type = (is_system ?
                ELDBUS_CONNECTION_TYPE_SYSTEM :
                ELDBUS_CONNECTION_TYPE_SESSION);

   root = efl_add_ref(ELDBUS_MODEL_OBJECT_CLASS, efl_main_loop_get(),
                      eldbus_model_connect(efl_added, conn_type, NULL, EINA_FALSE),
                      eldbus_model_object_bus_set(efl_added, bus_name),
                      eldbus_model_object_path_set(efl_added, path),
                      efl_event_callback_array_add(efl_added, event_cbs(), NULL));

   if (efl_model_children_count_get(root))
     eina_future_then(efl_model_children_slice_get(root, 0, efl_model_children_count_get(root)),
                      _slice, (uintptr_t) 0);

   ecore_main_loop_begin();
   efl_del(root);

 end:
   eldbus_shutdown();
   ecore_shutdown();
   return retval;
}