2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <Ecore_Wayland.h>
22 #include <tizen-remote-surface-client-protocol.h>
23 #include <tbm_surface.h>
25 #include "aul_app_com.h"
27 #include "aul_rsm_provider.h"
28 #include "aul_rsm_viewer.h"
30 struct __remote_surface_s {
31 struct wl_surface *surface;
32 struct tizen_remote_surface_provider *rsp;
39 void (*update_cb)(struct tizen_remote_surface *trs, struct wl_buffer *buffer, uint32_t time, void *data);
40 void (*missing_cb)(struct tizen_remote_surface *trs, void *data);
42 struct tizen_remote_surface *surface;
46 static struct tizen_remote_surface_manager *__rsm;
47 static struct wayland_tbm_client *__tbm_client;
48 static struct wl_tbm *__tbm;
49 static int __init_count;
50 static GHashTable *__viewer_tbl;
51 static aul_app_com_connection_h __conn;
55 struct wl_registry *registry;
56 Ecore_Wl_Global *global;
59 if (__init_count > 0) {
65 registry = ecore_wl_registry_get();
66 globals = ecore_wl_globals_get();
68 if (!registry || !globals) {
69 _E("could not get registry(%p) or global list(%p)", registry,
74 EINA_INLIST_FOREACH(globals, global) {
75 if (!strcmp(global->interface, "tizen_remote_surface_manager")) {
76 __rsm = wl_registry_bind(registry, global->id,
77 &tizen_remote_surface_manager_interface, 1);
82 _E("could not get remote surface manager");
97 wayland_tbm_client_deinit(__tbm_client);
100 tizen_remote_surface_manager_destroy(__rsm);
109 API int aul_rsm_provider_init()
116 API int aul_rsm_provider_fini()
123 static void __rsp_resource_id_cb(void *data, struct tizen_remote_surface_provider *provider, uint32_t res_id)
125 struct __remote_surface_s *remote = (struct __remote_surface_s *)data;
126 bundle *envelope = bundle_create();
128 if (envelope == NULL) {
129 _E("failed to create envelope");
133 bundle_add_str(envelope, "__AUL_RSP_RESOURCE_ID__", remote->id);
134 bundle_add_byte(envelope, "__AUL_RSP_SURFACE_ID__", &res_id, sizeof(uint32_t));
136 if (aul_app_com_send("tbm_surface_notify", envelope) < 0) {
137 _E("failed to send surface notify");
140 bundle_free(envelope);
143 static void __rsp_visibility_cb(void *data, struct tizen_remote_surface_provider *provider, uint32_t visibility)
145 struct __remote_surface_s *remote = (struct __remote_surface_s *)data;
146 Ecore_Wl_Event_Window_Visibility_Change *ev;
148 ev = calloc(1, sizeof(Ecore_Wl_Event_Window_Visibility_Change));
154 ev->win = ecore_wl_window_id_get(remote->win);
156 if (visibility == TIZEN_REMOTE_SURFACE_VISIBILITY_TYPE_VISIBLE)
157 ev->fully_obscured = 0;
159 ev->fully_obscured = 1;
161 _D("visibility changed:%d", visibility);
163 ecore_event_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE, ev, NULL, NULL);
166 static const struct tizen_remote_surface_provider_listener __rsp_listener = {
167 __rsp_resource_id_cb,
171 API int aul_rsm_provider_remote_enable(const char *id, struct wl_surface *surface)
173 struct __remote_surface_s *remote;
176 _E("__rsm is not ready");
180 if (!surface || !id) {
181 _E("invalid parameter");
185 remote = (struct __remote_surface_s *)malloc(sizeof(struct __remote_surface_s));
191 remote->surface = surface;
192 remote->win = ecore_wl_window_surface_find(surface);
194 _E("failed to find win");
198 remote->rsp = tizen_remote_surface_manager_create_provider(__rsm, surface);
200 _E("failed to create provider");
204 remote->id = strdup(id);
210 tizen_remote_surface_provider_add_listener(remote->rsp, &__rsp_listener, remote);
212 wl_display_roundtrip(ecore_wl_display_get());
213 _D("%d surface remote enable");
218 static void __rs_buffer_update_cb(void *data, struct tizen_remote_surface *trs,
219 struct wl_buffer *buffer, uint32_t time)
221 struct __surface_s *surface = data;
223 surface->update_cb(trs, buffer, time, surface->data);
226 static void __rs_missing_cb(void *data, struct tizen_remote_surface *trs)
228 struct __surface_s *surface = data;
230 surface->missing_cb(trs, surface->data);
233 static const struct tizen_remote_surface_listener __rs_listener = {
234 __rs_buffer_update_cb,
238 static void __redirect_surface(char *id, int res_id)
240 struct __surface_s *surface = NULL;
243 surface = g_hash_table_lookup(__viewer_tbl, id);
245 _E("unknown surface");
250 surface->resource_id = res_id;
251 surface->surface = tizen_remote_surface_manager_create_surface(__rsm,
252 (uint32_t)res_id, __tbm);
253 tizen_remote_surface_add_listener(surface->surface, &__rs_listener,
255 tizen_remote_surface_redirect(surface->surface);
258 static int __tbm_handler(const char *endpoint, aul_app_com_result_e e,
259 bundle *envelope, void *user_data)
262 uint32_t *res_id = NULL;
265 bundle_get_str(envelope, "__AUL_RSP_RESOURCE_ID__", &id);
266 bundle_get_byte(envelope, "__AUL_RSP_SURFACE_ID__", (void **)&res_id,
270 __redirect_surface(id, (int)*res_id);
272 _E("bad tbm message received. missing arguments");
277 API int aul_rsm_viewer_init()
281 __tbm_client = (struct wayland_tbm_client *)wayland_tbm_client_init(ecore_wl_display_get());
283 _E("could not get tbm client");
287 __tbm = (struct wl_tbm *)wayland_tbm_client_get_wl_tbm(__tbm_client);
289 _E("could not get tbm");
293 aul_app_com_create("tbm_surface_notify", NULL, __tbm_handler, NULL, &__conn);
295 _E("failed to create app com");
302 API int aul_rsm_viewer_fini()
309 API int aul_rsm_viewer_set_surface_handler(const char *id, aul_rsm_handler *cbs, void *data)
311 struct __surface_s *surface;
314 _E("invalid parameter");
319 __viewer_tbl = g_hash_table_new(g_str_hash, g_str_equal);
321 _E("unable to create hashtable");
326 surface = (struct __surface_s *)malloc(sizeof(struct __surface_s));
332 surface->id = strdup(id);
339 surface->resource_id = -1;
340 surface->data = data;
341 surface->update_cb = cbs->aul_rsm_surface_update_cb;
342 surface->missing_cb = cbs->aul_rsm_surface_missing_cb;
344 g_hash_table_insert(__viewer_tbl, surface->id, surface);
349 API int aul_rsm_viewer_set_surface_handler_by_rid(int resource_id, aul_rsm_handler *cbs, void *data)
353 snprintf(buf, sizeof(buf), "%d", resource_id);
354 aul_rsm_viewer_set_surface_handler(buf, cbs, data);
355 __redirect_surface(buf, resource_id);
360 API int aul_rsm_viewer_get_surface_rid(const char *id, int *resource_id)
362 struct __surface_s *surface;
364 if (!id || !resource_id) {
365 _E("illegal arguments");
369 surface = g_hash_table_lookup(__viewer_tbl, id);
371 _E("unknown surface");
375 if (surface->resource_id < 0) {
376 _E("resource id not yet initialized");
380 *resource_id = surface->resource_id;