ecore_exe_example.c

This is a process that will send messages to a child and it will stop when it receives "quit".

This is a process that will send messages to a child and it will stop when it receives "quit".Check the Full tutorial

#include <stdio.h>
#include <string.h>
#include <Ecore.h>
#define BUFFER_SIZE 1024
static Eina_Bool
_msg_from_child_handler(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
{
Ecore_Exe_Event_Data *dataFromProcess = (Ecore_Exe_Event_Data *)event;
char msg[BUFFER_SIZE];
if (dataFromProcess->size >= (BUFFER_SIZE - 1))
{
printf("Data too big for bugger. error\n");
}
strncpy(msg, dataFromProcess->data, dataFromProcess->size);
msg[dataFromProcess->size] = 0;
if (strcmp(msg, "quit") == 0)
{
printf("My child said to me, QUIT!\n");
}
else
printf("I received a message from my child: %s\n", msg);
}
static Eina_Bool
_sendMessage(void *data)
{
static int numberOfMessages = 0;
Ecore_Exe *childHandle = (Ecore_Exe *)data;
char msg[BUFFER_SIZE];
sprintf(msg, " Message: %d\n", numberOfMessages);
numberOfMessages++;
if (ecore_exe_send(childHandle, msg, strlen(msg)) != EINA_TRUE)
fprintf(stderr, "Could not send my name to the child\n");
else
printf(
"I'm the father and I sent this message to the child: %s\n", msg);
}
int
main(void)
{
pid_t childPid;
Ecore_Exe *childHandle;
if (!ecore_init())
goto exit;
childHandle = ecore_exe_pipe_run("./ecore_exe_example_child",
if (childHandle == NULL)
{
fprintf(stderr, "Could not create a child process!\n");
}
childPid = ecore_exe_pid_get(childHandle);
if (childPid == -1)
fprintf(stderr, "Could not retrieve the PID!\n");
else
printf("The child process has PID:%u\n", (unsigned int)childPid);
ecore_event_handler_add(ECORE_EXE_EVENT_DATA, _msg_from_child_handler, NULL);
ecore_timer_add(1, _sendMessage, childHandle);
ecore_exe_free(childHandle); //This will not affect the child process
return EXIT_SUCCESS;
exit:
return EXIT_FAILURE;
}
Ecore_Event_Handler * ecore_event_handler_add(int type, Ecore_Event_Handler_Cb func, const void *data)
Adds an event handler.
Definition: ecore_events.c:13
pid_t ecore_exe_pid_get(const Ecore_Exe *exe)
Retrieves the process ID of the given spawned process.
Definition: ecore_exe.c:215
Ecore_Exe * ecore_exe_pipe_run(const char *exe_cmd, Ecore_Exe_Flags flags, const void *data)
Spawns a child process with its stdin/out available for communication.
Definition: ecore_exe.c:64
int ECORE_EXE_EVENT_DATA
Data from a child process.
Definition: ecore_exe.c:38
void * ecore_exe_free(Ecore_Exe *exe)
Frees the given process handle.
Definition: ecore_exe.c:188
Eina_Bool ecore_exe_send(Ecore_Exe *exe, const void *data, int size)
Sends data to the given child process which it receives on stdin.
Definition: ecore_exe.c:115
EAPI int ecore_shutdown(void)
Shuts down connections, signal handlers sockets etc.
Definition: ecore.c:371
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition: ecore.c:230
#define ECORE_CALLBACK_RENEW
Return value to keep a callback.
Definition: Ecore_Common.h:153
#define ECORE_CALLBACK_DONE
Return value to stop event handling.
Definition: Ecore_Common.h:156
void ecore_main_loop_quit(void)
Quits the main loop once all the events currently on the queue have been processed.
Definition: ecore_main.c:1321
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1311
Ecore_Timer * ecore_timer_add(double in, Ecore_Task_Cb func, const void *data)
Creates a timer to call the given function in the given period of time.
Definition: ecore_timer.c:189
@ ECORE_EXE_PIPE_READ
Exe Pipe Read mask.
Definition: ecore_exe_eo.h:50
@ ECORE_EXE_PIPE_WRITE
Exe Pipe Write mask.
Definition: ecore_exe_eo.h:51
@ ECORE_EXE_PIPE_READ_LINE_BUFFERED
Reads are buffered until a newline and split 1 line per Ecore_Exe_Event_Data_Line.
Definition: ecore_exe_eo.h:53
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
Opaque handle to manage Ecore Exe objects.
Ecore exe event data structure.
Definition: ecore_exe_eo.h:33
void * data
The raw binary data from the child process received.
Definition: ecore_exe_eo.h:36
int size
The size of this data in bytes.
Definition: ecore_exe_eo.h:37