ecore_exe_example.c

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");
return ECORE_CALLBACK_DONE;
}
strncpy(msg, dataFromProcess->data, dataFromProcess->size);
msg[dataFromProcess->size] = 0;
if (strcmp(msg, "quit") == 0)
{
printf("My child said to me, QUIT!\n");
ecore_main_loop_quit();
}
else
printf("I received a message from my child: %s\n", msg);
return ECORE_CALLBACK_DONE;
}
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);
return ECORE_CALLBACK_RENEW;
}
int
main(void)
{
pid_t childPid;
Ecore_Exe *childHandle;
if (!ecore_init())
goto exit;
childHandle = ecore_exe_pipe_run("./ecore_exe_example_child",
ECORE_EXE_PIPE_WRITE |
ECORE_EXE_PIPE_READ_LINE_BUFFERED |
ECORE_EXE_PIPE_READ, NULL);
if (childHandle == NULL)
{
fprintf(stderr, "Could not create a child process!\n");
goto ecore_shutdown;
}
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_main_loop_begin();
ecore_exe_free(childHandle); //This will not affect the child process
ecore_shutdown();
return EXIT_SUCCESS;
ecore_shutdown:
ecore_shutdown();
exit:
return EXIT_FAILURE;
}
ecore_event_handler_add
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
ecore_exe_pid_get
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_EVENT_DATA
int ECORE_EXE_EVENT_DATA
Data from a child process.
Definition: ecore_exe.c:38
ecore_exe_free
void * ecore_exe_free(Ecore_Exe *exe)
Frees the given process handle.
Definition: ecore_exe.c:188
ecore_exe_send
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
ecore_exe_pipe_run
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
ecore_shutdown
EAPI int ecore_shutdown(void)
Shuts down connections, signal handlers sockets etc.
Definition: ecore.c:371
ecore_init
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition: ecore.c:230
ECORE_CALLBACK_RENEW
#define ECORE_CALLBACK_RENEW
Return value to keep a callback.
Definition: Ecore_Common.h:153
ECORE_CALLBACK_DONE
#define ECORE_CALLBACK_DONE
Return value to stop event handling.
Definition: Ecore_Common.h:156
ecore_main_loop_quit
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
ecore_main_loop_begin
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1311
ecore_timer_add
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
@ ECORE_EXE_PIPE_READ
Exe Pipe Read mask.
Definition: ecore_exe_eo.h:50
ECORE_EXE_PIPE_WRITE
@ ECORE_EXE_PIPE_WRITE
Exe Pipe Write mask.
Definition: ecore_exe_eo.h:51
ECORE_EXE_PIPE_READ_LINE_BUFFERED
@ 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
EINA_TRUE
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539
Eina_Bool
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
EINA_UNUSED
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
Ecore_Exe
Opaque handle to manage Ecore Exe objects.
_Ecore_Exe_Event_Data
Ecore exe event data structure.
Definition: ecore_exe_eo.h:33
_Ecore_Exe_Event_Data::data
void * data
The raw binary data from the child process received.
Definition: ecore_exe_eo.h:36
_Ecore_Exe_Event_Data::size
int size
The size of this data in bytes.
Definition: ecore_exe_eo.h:37