remove destry resource
[platform/core/uifw/e-mod-tizen-eom.git] / src / e_mod_main.c
1 #define E_COMP_WL
2 #include "e.h"
3 #include "e_mod_main.h"
4 #include "eom-server-protocol.h"
5 #include "Ecore_Drm.h"
6
7 typedef struct _E_Eom E_Eom, *E_EomPtr;
8
9 struct _E_Eom
10 {
11    E_Comp_Data *cdata;
12    struct wl_global *global;
13    struct wl_resource *resource;
14    Eina_List *handlers;
15 };
16
17 E_EomPtr g_eom = NULL;
18
19 EAPI E_Module_Api e_modapi = { E_MODULE_API_VERSION, "EOM Module" };
20
21 static E_Comp_Wl_Output *
22 _e_eom_e_comp_wl_output_get(Eina_List *outputs, const char *id)
23 {
24    Eina_List *l;
25    E_Comp_Wl_Output *output;
26
27    EINA_LIST_FOREACH(outputs, l, output)
28      {
29        if (!strcmp(output->id, id))
30          return output;
31      }
32
33    return NULL;
34 }
35
36 static Eina_Bool
37 _e_eom_e_client_remove_cb(void *data, int type, void *event)
38 {
39    E_Event_Client *ev = event;
40    E_Client *ec = ev->ec;
41
42    (void) type;
43    (void) event;
44    (void) data;
45
46    EOM_DBG("e_client: %p is died\n", ec);
47
48    return ECORE_CALLBACK_PASS_ON;
49 }
50
51
52 static Eina_Bool
53 _e_eom_ecore_drm_output_cb(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
54 {
55    Ecore_Drm_Event_Output *e;
56    E_EomPtr eom = data;
57    E_Comp_Data *cdata;
58    E_Comp_Wl_Output *output;
59    Eina_List *l2;
60    struct wl_resource *output_resource;
61    enum wl_eom_type eom_type = WL_EOM_TYPE_NONE;
62    char buff[PATH_MAX];
63
64    if (!(e = event)) goto end;
65
66    if (!e->plug) goto end;
67
68    EOM_DBG("id:%d (x,y,w,h):(%d,%d,%d,%d) (w_mm,h_mm):(%d,%d) refresh:%d subpixel_order:%d transform:%d make:%s model:%s plug:%d\n",
69       e->id, e->x, e->y, e->w, e->h, e->phys_width, e->phys_height, e->refresh, e->subpixel_order, e->transform, e->make, e->model, e->plug);
70
71    if (!(cdata = e_comp->wl_comp_data)) goto end;
72
73    snprintf(buff, sizeof(buff), "%d", e->id);
74
75    /* get the e_comp_wl_output */
76    output = _e_eom_e_comp_wl_output_get(cdata->outputs, buff);
77    if (!output)
78      {
79         EOM_ERR("no e_comp_wl_outputs.\n");
80         goto end;
81      }
82
83    /* TODO: we need ecore_drm_output_connector_get()/ecore_drm_output_conn_name_get() function to get the connector type */
84
85
86    /* send notify in each outputs associated with e_comp_wl_output */
87    EINA_LIST_FOREACH(output->resources, l2, output_resource)
88      {
89         if (e->plug)
90            wl_eom_send_output_type(eom->resource,  output_resource, eom_type, WL_EOM_STATUS_CONNECTION);
91         else
92             wl_eom_send_output_type(eom->resource,  output_resource, eom_type, WL_EOM_STATUS_DISCONNECTION);
93      }
94
95 #if 0
96    e_comp_wl_output_init(buff, e->make, e->model, e->x, e->y, e->w, e->h,
97                          e->phys_width, e->phys_height, e->refresh,
98                          e->subpixel_order, e->transform);
99 #endif
100
101 end:
102    return ECORE_CALLBACK_PASS_ON;
103 }
104
105 static Eina_Bool
106 _e_eom_ecore_drm_activate_cb(void *data, int type EINA_UNUSED, void *event)
107 {
108    Ecore_Drm_Event_Activate *e = NULL;
109    E_EomPtr eom = NULL;
110
111    if ((!event) || (!data)) goto end;
112    e = event;
113    data = eom;
114
115    if (e->active)
116      {
117         /* TODO: something do */
118      }
119    else
120      {
121         /* TODO: something do */
122      }
123
124 end:
125    return ECORE_CALLBACK_PASS_ON;
126 }
127
128
129 /* wl_eom_set_keygrab request handler */
130 static void
131 _e_eom_wl_request_set_attribute_cb(struct wl_client *client,
132               struct wl_resource *resource,
133               struct wl_resource *output,
134               uint32_t attribute)
135 {
136    (void) client;
137    (void) attribute;
138
139    EOM_DBG("attribute:%d\n", attribute);
140
141    wl_eom_send_output_attribute(resource, output, attribute, WL_EOM_ATTRIBUTE_STATE_ACTIVE, WL_EOM_ERROR_NONE);
142 }
143
144 static const struct wl_eom_interface _e_eom_wl_implementation = {
145    _e_eom_wl_request_set_attribute_cb
146 };
147
148 /* wl_eom global object destroy function */
149 static void
150 _e_eom_wl_resource_destory_cb(struct wl_resource *resource)
151 {
152
153 /* TODO : destroy resources if exist */
154
155 }
156
157 /* wl_eom global object bind function */
158 static void
159 _e_eom_wl_bind_cb(struct wl_client *client, void *data, uint32_t version, uint32_t id)
160 {
161    E_EomPtr eom = data;
162    struct wl_resource *resource;
163
164    resource = wl_resource_create(client, &wl_eom_interface, MIN(version, 1), id);
165    if (!resource)
166      {
167         EOM_ERR("error. resource is null. (version :%d, id:%d)\n", version, id);
168         wl_client_post_no_memory(client);
169         return;
170      }
171
172    wl_resource_set_implementation(resource, &_e_eom_wl_implementation, eom, _e_eom_wl_resource_destory_cb);
173
174    eom->resource = resource;
175
176    EOM_DBG("create wl_eom global resource.\n");
177 }
178
179 static void
180 _e_eom_deinit()
181 {
182     Ecore_Event_Handler *h = NULL;
183
184    if (!g_eom) return;
185
186    /* remove event handlers */
187    if (g_eom->handlers)
188      {
189         EINA_LIST_FREE(g_eom->handlers, h)
190           ecore_event_handler_del(h);
191      }
192
193    if (g_eom->global) wl_global_destroy(g_eom->global);
194
195    E_FREE(g_eom);
196    g_eom = NULL;
197 }
198
199 static Eina_Bool
200 _e_eom_init()
201 {
202    E_Comp_Data *cdata = NULL;
203
204    g_eom = E_NEW(E_Eom, 1);
205    if (!g_eom)
206      {
207         EOM_ERR("error. fail to allocate the memory.\n");
208         return EINA_FALSE;
209      }
210
211    if (!e_comp)
212      {
213         EOM_ERR("error. e_comp is null.\n");
214         goto err;
215      }
216
217    cdata = e_comp->wl_comp_data;
218    if (!cdata)
219      {
220         EOM_ERR("error. e_comp->wl_comp_data is null.\n");
221         goto err;
222      }
223
224    g_eom->cdata = cdata;
225    g_eom->global = wl_global_create(cdata->wl.disp, &wl_eom_interface, 1, g_eom, _e_eom_wl_bind_cb);
226    if (!g_eom->global)
227      {
228         EOM_ERR("error. g_eom->global is null.\n");
229         goto err;
230      }
231
232    /* add event hanlders */
233    E_LIST_HANDLER_APPEND(g_eom->handlers, ECORE_DRM_EVENT_ACTIVATE, _e_eom_ecore_drm_activate_cb, g_eom);
234    E_LIST_HANDLER_APPEND(g_eom->handlers, ECORE_DRM_EVENT_OUTPUT, _e_eom_ecore_drm_output_cb, g_eom);
235    E_LIST_HANDLER_APPEND(g_eom->handlers, E_EVENT_CLIENT_REMOVE, _e_eom_e_client_remove_cb, g_eom);
236
237    return EINA_TRUE;
238
239 err:
240    _e_eom_deinit();
241
242    return EINA_FALSE;
243 }
244
245 EAPI void *
246 e_modapi_init(E_Module *m)
247 {
248    return (_e_eom_init() ? m : NULL);
249 }
250
251 EAPI int
252 e_modapi_shutdown(E_Module *m EINA_UNUSED)
253 {
254    _e_eom_deinit();
255
256    return 1;
257 }
258
259 EAPI int
260 e_modapi_save(E_Module *m EINA_UNUSED)
261 {
262    /* Save something to be kept */
263    return 1;
264 }
265