summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarsten Haitzler (Rasterman) <raster@rasterman.com>2019-02-16 22:03:08 +0000
committerCarsten Haitzler (Rasterman) <raster@rasterman.com>2019-02-16 22:03:08 +0000
commitb59d2e049c05073e3ab0087e3cce0b1ae6f92d12 (patch)
treec23bdcb8e5d975dbebca3002f8abe83db9d6d114
parenta81a85f9dd2fe725fb9559eeaffab7d0d6d38947 (diff)
examples - add examples for efl exe/thread
-rw-r--r--src/examples/ecore/efl_exe.c70
-rwxr-xr-xsrc/examples/ecore/efl_exe_test.sh9
-rw-r--r--src/examples/ecore/efl_thread.c193
3 files changed, 272 insertions, 0 deletions
diff --git a/src/examples/ecore/efl_exe.c b/src/examples/ecore/efl_exe.c
new file mode 100644
index 0000000000..e50d49588c
--- /dev/null
+++ b/src/examples/ecore/efl_exe.c
@@ -0,0 +1,70 @@
1#define EFL_BETA_API_SUPPORT
2#define EFL_EO_API_SUPPORT
3
4#include <stdio.h>
5#include <string.h>
6
7#include <Eina.h>
8#include <Eo.h>
9#include <Efl_Core.h>
10
11static void _read_change(void *data EINA_UNUSED, const Efl_Event *ev);
12static Eina_Value _task_exit(void *data, Eina_Value v, const Eina_Future *dead EINA_UNUSED);
13
14static void
15_read_change(void *data EINA_UNUSED, const Efl_Event *ev)
16{
17 // read output from exe status changed - read what we can
18 Eo *obj = ev->object;
19 char buf[4096];
20 Eina_Rw_Slice rw_slice = EINA_SLICE_ARRAY(buf);
21
22 while (efl_io_reader_can_read_get(obj))
23 {
24 Eina_Error err = efl_io_reader_read(obj, &rw_slice);
25 if (!err)
26 {
27 buf[rw_slice.len] = 0;
28 printf("--- READ [%p] [%s] ok %i bytes '%s'\n", obj, efl_core_command_line_command_get(obj), (int)rw_slice.len, buf);
29 }
30 }
31}
32
33static Eina_Value
34_task_exit(void *data, Eina_Value v, const Eina_Future *dead EINA_UNUSED)
35{
36 // called when the task says it has completed and exited.
37 // all output to read has stopped
38 Eo *obj = data;
39 printf("--- [%p] EXITED exit_code=%i\n", obj, efl_task_exit_code_get(obj));
40 efl_loop_quit(efl_provider_find(obj, EFL_LOOP_CLASS), eina_value_int_init(99));
41 efl_del(obj);
42 return v;
43}
44
45EAPI_MAIN void
46efl_main(void *data EINA_UNUSED, const Efl_Event *ev)
47{
48 Eo *app = ev->object;
49
50 const Efl_Version *v = efl_app_build_efl_version_get(app);
51 printf("--- EFL %i.%i.%i\n", v->major, v->minor, v->micro);
52 Eina_Array *args = eina_array_new(1);
53 eina_array_push(args, eina_stringshare_add("./efl_exe_test.sh"));
54 Eo *env = efl_duplicate(efl_env_self());
55 efl_core_env_set(env, "BLAH", "blahvalue");
56 Eo *obj = efl_add(EFL_EXE_CLASS, app,
57 efl_core_command_line_command_array_set(efl_added, args),
58 efl_exe_env_set(efl_added, env),
59 efl_task_flags_set(efl_added, EFL_TASK_FLAGS_USE_STDOUT | EFL_TASK_FLAGS_USE_STDIN),
60 efl_event_callback_add(efl_added, EFL_IO_READER_EVENT_CAN_READ_CHANGED, _read_change, NULL),
61 eina_future_then(efl_task_run(efl_added), _task_exit, efl_added)
62 );
63 efl_unref(env);
64
65 char *buf2 = "sample-input\n";
66 Eina_Slice slice = { strlen(buf2), buf2 };
67 Eina_Error err = efl_io_writer_write(obj, &slice, NULL);
68 if (!err) printf("--- WRITE [%p] [%s] ok %i bytes\n", obj, efl_core_command_line_command_get(obj), (int)slice.len);
69}
70EFL_MAIN()
diff --git a/src/examples/ecore/efl_exe_test.sh b/src/examples/ecore/efl_exe_test.sh
new file mode 100755
index 0000000000..a2f4db1e13
--- /dev/null
+++ b/src/examples/ecore/efl_exe_test.sh
@@ -0,0 +1,9 @@
1#!/bin/sh
2
3echo "BLAH is $BLAH"
4sleep 1
5read IN
6sleep 1
7echo "INPUT is $IN"
8sleep 1
9exit 7
diff --git a/src/examples/ecore/efl_thread.c b/src/examples/ecore/efl_thread.c
new file mode 100644
index 0000000000..48a20e9ccc
--- /dev/null
+++ b/src/examples/ecore/efl_thread.c
@@ -0,0 +1,193 @@
1#define EFL_BETA_API_SUPPORT
2#define EFL_EO_API_SUPPORT
3
4#include <stdio.h>
5#include <string.h>
6
7#include <Eina.h>
8#include <Eo.h>
9#include <Efl_Core.h>
10
11static void _th_read_change(void *data EINA_UNUSED, const Efl_Event *ev);
12static void _th_main(void *data EINA_UNUSED, const Efl_Event *ev);
13static void _read_change(void *data EINA_UNUSED, const Efl_Event *ev);
14static Eina_Value _task_exit(void *data, Eina_Value v, const Eina_Future *dead EINA_UNUSED);
15
16////////////////////////////////////////////////////////////////////////////
17//// thread side of code
18static void
19_th_timeout(void *data EINA_UNUSED, const Efl_Event *ev)
20{
21 Eo *obj = data;
22
23 printf("--- START EXIT [%p]\n", obj);
24 efl_threadio_outdata_set(obj, (void *)0x9876);
25 efl_loop_quit(obj, eina_value_int_init(99));
26}
27
28static void
29_th_read_change(void *data EINA_UNUSED, const Efl_Event *ev)
30{
31 // read input from parent thread/loop status chnaged - read what we can
32 Eo *obj = ev->object;
33 char buf[4096];
34 Eina_Rw_Slice rw_slice = EINA_SLICE_ARRAY(buf);
35
36 while (efl_io_reader_can_read_get(obj))
37 {
38 Eina_Error err = efl_io_reader_read(obj, &rw_slice);
39 if (!err)
40 {
41 buf[rw_slice.len] = 0;
42 printf("--- TH READ [%p] [%s] ok %i bytes '%s'\n", obj, efl_core_command_line_command_get(obj), (int)rw_slice.len, buf);
43
44 char *buf2 = "yes-im-here ";
45 Eina_Slice slice = { strlen(buf2), buf2 };
46 Eina_Error err = efl_io_writer_write(obj, &slice, NULL);
47 if (!err)
48 {
49 Eina_Accessor *args_access = efl_core_command_line_command_access(obj);
50 printf("--- TH WRITE [%p] [%s] ok %i bytes\n", obj, efl_core_command_line_command_get(obj), (int)slice.len);
51 void *s = "";
52 eina_accessor_data_get(args_access, 1, &s);
53 if (!strcmp(s, "one"))
54 efl_add(EFL_LOOP_TIMER_CLASS, obj,
55 efl_loop_timer_interval_set(efl_added, 2.0),
56 efl_event_callback_add(efl_added, EFL_LOOP_TIMER_EVENT_TICK, _th_timeout, obj));
57 else
58 efl_add(EFL_LOOP_TIMER_CLASS, obj,
59 efl_loop_timer_interval_set(efl_added, 1.0),
60 efl_event_callback_add(efl_added, EFL_LOOP_TIMER_EVENT_TICK, _th_timeout, obj));
61 eina_accessor_free(args_access);
62 }
63 }
64 }
65}
66
67static void
68_th_main(void *data EINA_UNUSED, const Efl_Event *ev)
69{
70 // the "main function" of the thread thayt gets called with arguments
71 // just like eflm_main for the main loop
72 Eo *obj = ev->object;
73 Eina_Accessor *args_access = efl_core_command_line_command_access(obj);
74 void *s = "", *ss = "";
75 eina_accessor_data_get(args_access, 0, &s);
76 eina_accessor_data_get(args_access, 1, &ss);
77 printf("--- TH main %p, '%s' '%s' indata=%p\n", obj, s, ss, efl_threadio_indata_get(obj));
78 efl_event_callback_add
79 (obj, EFL_IO_READER_EVENT_CAN_READ_CHANGED, _th_read_change, NULL);
80 if (!strcmp(s, "one"))
81 {
82 Eina_Array *args = eina_array_new(1);
83 eina_array_push(args, eina_stringshare_add("number"));
84 eina_array_push(args, eina_stringshare_add("one"));
85 Eo *obj2 = efl_add(EFL_THREAD_CLASS, obj,
86 efl_threadio_indata_set(efl_added, (void *)0x1234),
87 efl_core_command_line_command_array_set(efl_added, args),
88 efl_task_flags_set(efl_added, EFL_TASK_FLAGS_USE_STDOUT | EFL_TASK_FLAGS_USE_STDIN),
89 efl_event_callback_add(efl_added, EFL_LOOP_EVENT_ARGUMENTS, _th_main, NULL),
90 efl_event_callback_add(efl_added, EFL_IO_READER_EVENT_CAN_READ_CHANGED, _read_change, NULL),
91 eina_future_then(efl_task_run(efl_added), _task_exit, efl_added)
92 );
93
94 char *buf2 = "hello-out-there2 ";
95 Eina_Slice slice = { strlen(buf2), buf2 };
96 Eina_Error err = efl_io_writer_write(obj2, &slice, NULL);
97 if (!err) printf("--- WRITE [%p] [%s] ok %i bytes\n", obj2, efl_core_command_line_command_get(obj), (int)slice.len);
98 }
99 eina_accessor_free(args_access);
100}
101
102////////////////////////////////////////////////////////////////////////////
103//// main loop side of code
104static void
105_read_change(void *data EINA_UNUSED, const Efl_Event *ev)
106{
107 // read output from thread status chnaged - read what we can
108 Eo *obj = ev->object;
109 char buf[4096];
110 Eina_Rw_Slice rw_slice = EINA_SLICE_ARRAY(buf);
111
112 while (efl_io_reader_can_read_get(obj))
113 {
114 Eina_Error err = efl_io_reader_read(obj, &rw_slice);
115 if (!err)
116 {
117 buf[rw_slice.len] = 0;
118 printf("--- READ [%p] [%s] ok %i bytes '%s'\n", obj, efl_core_command_line_command_get(obj), (int)rw_slice.len, buf);
119 }
120 }
121}
122
123static Eina_Value
124_task_exit(void *data, Eina_Value v, const Eina_Future *dead EINA_UNUSED)
125{
126 // called when the task says it has completed and exited.
127 // all output to read has stopped
128 Eo *obj = data;
129 printf("--- [%p] EXITED exit_code=%i outdata=%p\n", obj, efl_task_exit_code_get(obj), efl_threadio_outdata_get(obj));
130 efl_del(obj);
131 return v;
132}
133
134////////////////////////////////////////////////////////////////////////////
135// just main loop input handling
136static void
137_stdin_read_change(void *data EINA_UNUSED, const Efl_Event *ev)
138{
139 // read output from thread status chnaged - read what we can
140 Eo *obj = ev->object;
141 char buf[4096];
142 Eina_Rw_Slice rw_slice = EINA_SLICE_ARRAY(buf);
143
144 while (efl_io_reader_can_read_get(obj))
145 {
146 Eina_Error err = efl_io_reader_read(obj, &rw_slice);
147 if (!err)
148 {
149 buf[rw_slice.len] = 0;
150 printf("--- STDIN READ [%p] [%s] ok %i bytes '%s'\n", obj, efl_core_command_line_command_get(obj), (int)rw_slice.len, buf);
151 }
152 }
153}
154
155EAPI_MAIN void
156efl_main(void *data EINA_UNUSED, const Efl_Event *ev)
157{
158 Eo *app = ev->object;
159 int threads = 2, i;
160 Eina_Accessor *args_access = efl_core_command_line_command_access(app);
161 void *s;
162
163 const Efl_Version *v = efl_app_build_efl_version_get(app);
164 printf("--- EFL %i.%i.%i\n", v->major, v->minor, v->micro);
165 s = NULL;
166 eina_accessor_data_get(args_access, 2, &s);
167 if (s && (!strcmp(s, "-stdinwatch")))
168 efl_event_callback_add(app, EFL_IO_READER_EVENT_CAN_READ_CHANGED,
169 _stdin_read_change, NULL);
170 s = NULL;
171 eina_accessor_data_get(args_access, 1, &s);
172 if (s) threads = atoi(s);
173 for (i = 0; i < threads; i++)
174 {
175 Eina_Array *args = eina_array_new(1);
176 eina_array_push(args, eina_stringshare_add("number"));
177 eina_array_push(args, eina_stringshare_add("one"));
178 Eo *obj = efl_add(EFL_THREAD_CLASS, app,
179 efl_threadio_indata_set(efl_added, (void *)0x5678),
180 efl_core_command_line_command_array_set(efl_added, args),
181 efl_task_flags_set(efl_added, EFL_TASK_FLAGS_USE_STDOUT | EFL_TASK_FLAGS_USE_STDIN),
182 efl_event_callback_add(efl_added, EFL_LOOP_EVENT_ARGUMENTS, _th_main, NULL),
183 efl_event_callback_add(efl_added, EFL_IO_READER_EVENT_CAN_READ_CHANGED, _read_change, NULL),
184 eina_future_then(efl_task_run(efl_added), _task_exit, efl_added)
185 );
186
187 char *buf2 = "hello-out-there ";
188 Eina_Slice slice = { strlen(buf2), buf2 };
189 Eina_Error err = efl_io_writer_write(obj, &slice, NULL);
190 if (!err) printf("--- WRITE [%p] [%s] ok %i bytes\n", obj, efl_core_command_line_command_get(obj), (int)slice.len);
191 }
192}
193EFL_MAIN()