Bug fix TIVI-839 ,change the design and permit operation of the music player while...
[profile/ivi/ico-uxf-homescreen.git] / ico-app-framework / ico_apf_ecore.c
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9 /**
10  * @brief   client library for Ecore(EFL) application
11  *
12  * @date    Feb-28-2013
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <getopt.h>
19 #include <string.h>
20 #include <sys/time.h>
21 #include <Ecore.h>
22
23 #include <libwebsockets.h>
24 #include "ico_apf_private.h"
25 #include "ico_apf_ecore.h"
26
27 /* define static function prototype         */
28 static Eina_Bool ico_apf_ecore_fdevent(void *data, Ecore_Fd_Handler *handler);
29 static void ico_apf_ecore_fdcontrol(ico_apf_com_poll_t *fd_ctl[], const int num_fds);
30
31 /*--------------------------------------------------------------------------*/
32 /**
33  *  @brief  ico_apf_ecore_init
34  *          This function connects to AppsController for Ecore application.
35  *          If you use AppsController's function, you must call this function.
36  *
37  *  @param[in]  uri             server URI
38  *  @return     result status
39  *  @retval     ICO_APF_E_NONE      success
40  *  @retval     ICO_APF_E_IO_ERROR  error(failed)
41  */
42 /*--------------------------------------------------------------------------*/
43 ICO_APF_API int
44 ico_apf_ecore_init(const char *uri)
45 {
46     apfw_trace("ico_apf_ecore_init: Enter(uri=%s)", uri ? uri : "(NULL)");
47
48     /* initialize resource controller           */
49     if (ico_apf_resource_init_client(uri) !=  ICO_APF_RESOURCE_E_NONE)  {
50         apfw_error("ico_apf_ecore_init: Leave(Resource initialize Error)");
51         return ICO_APF_E_IO_ERROR;
52     }
53
54     /* set file descriptor to Ecore main loop   */
55     ico_apf_com_poll_fd_control(ico_apf_ecore_fdcontrol);
56
57     apfw_trace("ico_apf_ecore_init: Leave(OK)");
58     return ICO_APF_E_NONE;
59 }
60
61 /*--------------------------------------------------------------------------*/
62 /**
63  *  @brief  ico_apf_ecore_term
64  *          This function connects to AppsController for Ecore application.
65  *          If you use AppsController's function, you must call this function.
66  *
67  *  @param      none
68  *  @return     none
69  */
70 /*--------------------------------------------------------------------------*/
71 ICO_APF_API void
72 ico_apf_ecore_term(void)
73 {
74     apfw_trace("ico_apf_ecore_term: Enter");
75
76     /* terminate resource controller            */
77     ico_apf_resource_term_client();
78
79     apfw_trace("ico_apf_ecore_term: Leave");
80 }
81
82 /*--------------------------------------------------------------------------*/
83 /**
84  *  @brief  ico_apf_ecore_init_server
85  *          This function connects to AppsController for Ecore application.
86  *          If you use AppsController's function, you must call this function.
87  *
88  *  @param[in]  appid           id of application
89  *  @return     result status
90  *  @retval     ICO_APF_E_NONE      success
91  *  @retval     ICO_APF_E_IO_ERROR  error(failed)
92  */
93 /*--------------------------------------------------------------------------*/
94 ICO_APF_API int
95 ico_apf_ecore_init_server(const char *uri)
96 {
97     apfw_trace("ico_apf_ecore_init_server: Enter(uri=%s)", uri ? uri : "(NULL)");
98
99     /* initialize resource controller           */
100     if (ico_apf_resource_init_server(uri) !=  ICO_APF_RESOURCE_E_NONE)  {
101         apfw_error("ico_apf_ecore_init_server: Leave(Resource initialize Error)");
102         return ICO_APF_E_IO_ERROR;
103     }
104
105     /* set file descriptor to Ecore main loop   */
106     ico_apf_com_poll_fd_control(ico_apf_ecore_fdcontrol);
107
108     apfw_trace("ico_apf_ecore_init_server: Leave(OK)");
109     return ICO_APF_E_NONE;
110 }
111
112 /*--------------------------------------------------------------------------*/
113 /**
114  *  @brief  ico_apf_ecore_term_server
115  *          This function connects to AppsController for Ecore application.
116  *          If you use AppsController's function, you must call this function.
117  *
118  *  @param      none
119  *  @return     none
120  */
121 /*--------------------------------------------------------------------------*/
122 ICO_APF_API void
123 ico_apf_ecore_term_server(void)
124 {
125     apfw_trace("ico_apf_ecore_term_server: Enter");
126
127     /* terminate resource controller            */
128     ico_apf_resource_term_server();
129
130     apfw_trace("ico_apf_ecore_term_server: Leave");
131 }
132
133 /*--------------------------------------------------------------------------*/
134 /**
135  * @brief   ico_apf_comm_connect
136  *          callback function called by Ecore when a file descriptor had a change
137  *
138  * @param[in]   data            user data(ico_apf_com_poll_t)
139  * @param[in]   handler         Ecore file descriptor handler
140  * @return      call back setting
141  * @retval      ECORE_CALLBACK_RENEW    set callback
142  */
143 /*--------------------------------------------------------------------------*/
144 static Eina_Bool
145 ico_apf_ecore_fdevent(void *data, Ecore_Fd_Handler *handler)
146 {
147     int     flags;
148
149     flags = (ecore_main_fd_handler_active_get(handler, ECORE_FD_READ))
150                     ? ICO_APF_COM_POLL_READ : 0;
151     if (ecore_main_fd_handler_active_get(handler, ECORE_FD_WRITE))
152         flags |= ICO_APF_COM_POLL_WRITE;
153     if (ecore_main_fd_handler_active_get(handler, ECORE_FD_ERROR))
154         flags |= ICO_APF_COM_POLL_ERROR;
155
156     ico_apf_com_poll_fd_event((ico_apf_com_poll_t *)data, flags);
157
158     return ECORE_CALLBACK_RENEW;
159 }
160
161 /*--------------------------------------------------------------------------*/
162 /**
163  * @brief   ico_apf_ecore_fdcontrol
164  *          This function connects to AppsController.
165  *
166  * @param[in]   fd_ctl      file descriptors
167  * @param[in]   num_fds     number of file descriptors
168  * @return      none
169  */
170 /*--------------------------------------------------------------------------*/
171 static void
172 ico_apf_ecore_fdcontrol(ico_apf_com_poll_t *fd_ctl[], const int num_fds)
173 {
174     Ecore_Fd_Handler_Flags flags;
175     int     i;
176
177     for (i = 0; i < num_fds; i++) {
178         if (fd_ctl[i]->flags) {
179             flags = (fd_ctl[i]->flags & ICO_APF_COM_POLL_READ) ? ECORE_FD_READ : 0;
180
181             if (fd_ctl[i]->flags & ICO_APF_COM_POLL_WRITE)   flags |= ECORE_FD_WRITE;
182             if (fd_ctl[i]->flags & ICO_APF_COM_POLL_ERROR)   flags |= ECORE_FD_ERROR;
183             if (! fd_ctl[i]->user_data) {
184                 /* add file descriptor  */
185                 fd_ctl[i]->user_data = (void *)
186                     ecore_main_fd_handler_add(fd_ctl[i]->fd, flags,
187                                               ico_apf_ecore_fdevent,
188                                               (void *)fd_ctl[i], NULL, NULL);
189             }
190             else {
191                 /* change event         */
192                 ecore_main_fd_handler_active_set((Ecore_Fd_Handler *)fd_ctl[i]->user_data,
193                                                  flags);
194             }
195         }
196         else {
197             /* remove file descriptor   */
198             ecore_main_fd_handler_del((Ecore_Fd_Handler *)fd_ctl[i]->user_data);
199         }
200     }
201 }
202