Initialize Tizen 2.3
[framework/uifw/ecore.git] / wearable / src / examples / ecore_exe_example.c
1 /**
2    Compile with gcc -o ecore_exe_example ecore_exe_example.c `pkg-config --cflags --libs ecore`
3  */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <Ecore.h>
8
9 #define BUFFER_SIZE 1024
10
11 static Eina_Bool
12 _msg_from_child_handler(void *data, int type, void *event)
13 {
14    Ecore_Exe_Event_Data *dataFromProcess = (Ecore_Exe_Event_Data *)event;
15    char msg[BUFFER_SIZE];
16
17    if (dataFromProcess->size >= (BUFFER_SIZE - 1))
18      {
19         fprintf(stdout, "Data too big for bugger. error\n");
20         return ECORE_CALLBACK_DONE;
21      }
22    
23    strncpy(msg, dataFromProcess->data, dataFromProcess->size);
24    msg[dataFromProcess->size] = 0;
25        
26    if (strcmp(msg, "quit") == 0)
27      {
28         fprintf(stdout, "My child said to me, QUIT!\n");
29         ecore_main_loop_quit();
30      }
31    else
32      fprintf(stdout, "I received a message from my child: %s\n", msg);
33
34    return ECORE_CALLBACK_DONE;
35 }
36
37 static Eina_Bool
38 _sendMessage(void *data)
39 {
40    static int numberOfMessages = 0;
41    Ecore_Exe *childHandle = (Ecore_Exe *)data;
42    char msg[BUFFER_SIZE];
43
44    sprintf(msg, " Message: %d\n", numberOfMessages);
45    numberOfMessages++;
46
47    if (ecore_exe_send(childHandle, msg, strlen(msg)) != EINA_TRUE)
48      fprintf(stderr, "Could not send my name to the child\n");
49    else
50      fprintf(stdout,
51              "I'm the father and I sent this message to the child: %s\n", msg);
52
53    return ECORE_CALLBACK_RENEW;
54 }
55
56 int
57 main(int argc, char **argv)
58 {
59    pid_t childPid;
60    Ecore_Exe *childHandle;
61
62    if (!ecore_init())
63      goto exit;
64
65    childHandle = ecore_exe_pipe_run("./ecore_exe_example_child",
66                                     ECORE_EXE_PIPE_WRITE |
67                                     ECORE_EXE_PIPE_READ_LINE_BUFFERED |
68                                     ECORE_EXE_PIPE_READ, NULL);
69
70    if (childHandle == NULL)
71      {
72         fprintf(stderr, "Could not create a child process!\n");
73         goto ecore_shutdown;
74      }
75
76    childPid = ecore_exe_pid_get(childHandle);
77
78    if (childPid == -1)
79      fprintf(stderr, "Could not retrive the PID!\n");
80    else
81      fprintf(stdout, "The child process has PID:%d\n", childPid);
82
83    ecore_event_handler_add(ECORE_EXE_EVENT_DATA, _msg_from_child_handler, NULL);
84    ecore_timer_add(1, _sendMessage, childHandle);
85
86    ecore_main_loop_begin();
87
88    ecore_exe_free(childHandle); //This will not affect the child process
89
90    ecore_shutdown();
91
92    return EXIT_SUCCESS;
93
94 ecore_shutdown:
95    ecore_shutdown();
96
97 exit:
98    return EXIT_FAILURE;
99 }
100