aboutsummaryrefslogtreecommitdiffstats
path: root/reference
diff options
context:
space:
mode:
authorAndy Williams <andy@andywilliams.me>2017-12-05 15:07:32 +0000
committerAndy Williams <andy@andywilliams.me>2017-12-05 15:07:32 +0000
commit9ef1398bfc7a681575e9c42835a4ca4f7631b39e (patch)
tree77ce6dd8e4048ef4afeef350dddd96e062e9e30b /reference
parentDo not use efl as a prefix for variables (diff)
downloadexamples-9ef1398bfc7a681575e9c42835a4ca4f7631b39e.tar.gz
core: Add an events reference example
Diffstat (limited to 'reference')
-rw-r--r--reference/c/core/src/core_event.c156
-rw-r--r--reference/c/core/src/meson.build7
2 files changed, 163 insertions, 0 deletions
diff --git a/reference/c/core/src/core_event.c b/reference/c/core/src/core_event.c
new file mode 100644
index 00000000..3ddebbc8
--- /dev/null
+++ b/reference/c/core/src/core_event.c
@@ -0,0 +1,156 @@
+#define EFL_EO_API_SUPPORT 1
+#define EFL_BETA_API_SUPPORT 1
+
+#include <stdio.h>
+
+#include <Eina.h>
+#include <Efl_Core.h>
+
+/*
+ * Efl Core Event examples.
+ *
+ * This example shows the various ways of adding callbacks for standard events.
+ * It also demonstrates the creation of a custom event and how to freeze and
+ * thaw events on an object.
+ */
+
+Efl_Event_Description *_CUSTOM_EVENT;
+
+static void
+_add_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
+{
+ printf(" Add\n");
+}
+
+static void
+_del_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
+{
+ printf(" Delete Callback\n");
+}
+
+static void
+_del_obj_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
+{
+ printf(" Delete Object\n");
+}
+
+static void
+_custom_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
+{
+ printf(" Custom\n");
+}
+
+static void
+_poll_cb(void *data, const Efl_Event *event EINA_UNUSED)
+{
+ Efl_Loop *mainloop;
+
+ mainloop = (Efl_Loop *)data;
+ printf(" Poll\n");
+
+ efl_event_callback_del(mainloop, _CUSTOM_EVENT, _custom_cb, NULL);
+}
+
+static void
+_fast_cb(void *data, const Efl_Event *event EINA_UNUSED)
+{
+ Efl_Loop *mainloop;
+
+ mainloop = (Efl_Loop *)data;
+ printf(" Running\n");
+}
+
+static void
+_freezethaw_cb(void *data, const Efl_Event *event EINA_UNUSED)
+{
+ Efl_Loop *polled;
+ static int called = 0;
+
+ polled = data;
+ switch (called)
+ {
+ case 0:
+ printf(" Freeze\n");
+ efl_event_freeze(polled);
+ break;
+ case 1:
+ printf(" Thaw\n");
+ efl_event_thaw(polled);
+ break;
+ default:
+ efl_exit(0);
+ }
+
+ called++;
+}
+
+static void
+_events_freeze(Efl_Loop *mainloop)
+{
+ Efl_Loop *polled;
+
+ printf("Test 2:\n");
+
+ polled = efl_add(EFL_LOOP_CLASS, mainloop);
+ efl_event_callback_add(polled, EFL_LOOP_EVENT_POLL_HIGH, _fast_cb, NULL);
+
+ efl_add(EFL_LOOP_TIMER_CLASS, mainloop,
+ efl_loop_timer_interval_set(efl_added, .1),
+ efl_event_callback_add(efl_added, EFL_LOOP_TIMER_EVENT_TICK, _freezethaw_cb, polled));
+}
+
+static void
+_timer_cb(void *data EINA_UNUSED, const Efl_Event *event)
+{
+ Efl_Loop *mainloop;
+
+ mainloop = (Efl_Loop *)data;
+ _events_freeze(mainloop);
+
+ // cancel this timer
+ efl_del(event->object);
+}
+
+EFL_CALLBACKS_ARRAY_DEFINE(_callback_array,
+{ EFL_LOOP_EVENT_POLL_MEDIUM, _poll_cb },
+{ EFL_EVENT_CALLBACK_DEL, _del_cb },
+{ EFL_EVENT_DEL, _del_obj_cb })
+
+static void
+_events_demo(Efl_Loop *mainloop)
+{
+ // this is a definition of a custom event, for our app
+ Efl_Event_Description desc = EFL_EVENT_DESCRIPTION("custom-event");
+
+ _CUSTOM_EVENT = &desc;
+ printf("Test 1:\n");
+
+ // add a singe callback that monitors for new callbacks added
+ efl_event_callback_add(mainloop, EFL_EVENT_CALLBACK_ADD, _add_cb, NULL);
+
+ // add an array of callbacks (defined above)
+ // this is more efficient if you have multiple callbacks to add
+ efl_event_callback_array_add(mainloop, _callback_array(), mainloop);
+
+ // here we add a custom callback and call it immediately
+ efl_event_callback_add(mainloop, _CUSTOM_EVENT, _custom_cb, NULL);
+ efl_event_callback_call(mainloop, _CUSTOM_EVENT, NULL);
+
+ // we will exit from this timer
+ efl_add(EFL_LOOP_TIMER_CLASS, mainloop,
+ efl_loop_timer_interval_set(efl_added, 10),
+ efl_event_callback_add(efl_added, EFL_LOOP_TIMER_EVENT_TICK, _timer_cb, mainloop));
+
+ printf(" Waiting for timer to call back (10 seconds)...\n");
+}
+
+EAPI_MAIN void
+efl_main(void *data EINA_UNUSED, const Efl_Event *ev)
+{
+ Efl_Loop *mainloop;
+
+ mainloop = ev->object;
+ _events_demo(mainloop);
+}
+EFL_MAIN()
+
diff --git a/reference/c/core/src/meson.build b/reference/c/core/src/meson.build
index 6906e537..26085f6d 100644
--- a/reference/c/core/src/meson.build
+++ b/reference/c/core/src/meson.build
@@ -7,6 +7,13 @@ executable('efl_reference_core_loop',
install : true
)
+executable('efl_reference_core_event',
+ files(['core_event.c']),
+ dependencies : deps,
+ include_directories : inc,
+ install : true
+)
+
executable('efl_reference_core_io',
files(['core_io.c']),
dependencies : deps,