impelement libds-tizen-global-resource 36/278636/1
authorSooChan Lim <sc1.lim@samsung.com>
Thu, 21 Jul 2022 11:54:26 +0000 (20:54 +0900)
committerSooChan Lim <sc1.lim@samsung.com>
Fri, 22 Jul 2022 14:00:42 +0000 (23:00 +0900)
This is the server implementation for tizen_surface protocol.

Change-Id: I40e97d4fedabe68e364ff9c9a95791311646216a

include/libds-tizen/global_resource.h [new file with mode: 0644]
packaging/libds-tizen.spec
src/global_resource/global_resource.c [new file with mode: 0644]
src/global_resource/meson.build [new file with mode: 0644]
src/meson.build

diff --git a/include/libds-tizen/global_resource.h b/include/libds-tizen/global_resource.h
new file mode 100644 (file)
index 0000000..82612cd
--- /dev/null
@@ -0,0 +1,45 @@
+#ifndef LIBDS_TIZEN_GLOBAL_RESOURCE_H
+#define LIBDS_TIZEN_GLOBAL_RESOURCE_H
+
+#include <stdint.h>
+#include <wayland-server.h>
+#include <libds/surface.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct ds_tizen_global_resource;
+struct ds_tizen_global_resource_info;
+
+struct ds_tizen_global_resource *
+ds_tizen_global_resource_create(struct wl_display *display);
+
+void
+ds_tizen_global_resource_add_destroy_listener(
+        struct ds_tizen_global_resource *global_resource,
+        struct wl_listener *listener);
+
+void
+ds_tizen_global_resource_add_get_resource_info_listener(
+        struct ds_tizen_global_resource *global_resource,
+        struct wl_listener *listener);
+
+void
+ds_tizen_global_resource_info_add_destroy_listener(
+        struct ds_tizen_global_resource_info *info,
+        struct wl_listener *listener);
+
+struct ds_surface *
+ds_tizen_global_resource_info_get_surface(
+        struct ds_tizen_global_resource_info *info);
+
+uint32_t
+ds_tizen_global_resource_get_universal_id(
+        struct ds_tizen_global_resource_info *info);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
index 70f06e5..abfe00c 100644 (file)
@@ -231,6 +231,21 @@ Group:   Development/Libraries
 %description screen-rotation-devel
 Development package for tizen screen rotation
 
+## libds-tizen-global-resource
+%package global-resource
+Summary: Library for tizen global resource
+Group:   Development/Libraries
+
+%description global-resource
+Library for tizen global resource
+
+%package global-resource-devel
+Summary: Development package for tizen global resource
+Group:   Development/Libraries
+
+%description global-resource-devel
+Development package for tizen global resource
+
 %prep
 %setup -q
 cp %{SOURCE1001} .
@@ -447,3 +462,17 @@ ninja -C builddir install
 %{_libdir}/pkgconfig/libds-tizen-screen-rotation.pc
 %{_libdir}/libds-tizen-screen-rotation.so
 %{_bindir}/libds-tizen-screen-rotation-tests
+
+%files global-resource
+%manifest %{name}.manifest
+%defattr(-,root,root,-)
+%license LICENSE
+%{_libdir}/libds-tizen-global-resource.so.*
+
+%files global-resource-devel
+%manifest %{name}.manifest
+%defattr(-,root,root,-)
+%license LICENSE
+%{_includedir}/libds-tizen/global_resource.h
+%{_libdir}/pkgconfig/libds-tizen-global-resource.pc
+%{_libdir}/libds-tizen-global-resource.so
diff --git a/src/global_resource/global_resource.c b/src/global_resource/global_resource.c
new file mode 100644 (file)
index 0000000..3c15f23
--- /dev/null
@@ -0,0 +1,345 @@
+#include <assert.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <wayland-server.h>
+#include <tizen-extension-server-protocol.h>
+#include <libds/log.h>
+
+#include "util.h"
+#include "libds-tizen/global_resource.h"
+
+#define TIZEN_SURFACE_VERSION 1
+
+static uint32_t unique_id = 0;
+
+struct ds_tizen_global_resource
+{
+    struct wl_global *global;
+
+    struct wl_list clients;
+
+    struct wl_listener destroy;
+
+    struct {
+        struct wl_signal destroy;
+        struct wl_signal get_resource_info;
+    } events;
+};
+
+struct ds_tizen_global_resource_client
+{
+    struct ds_tizen_global_resource *global_resource;
+
+    struct wl_resource *resource;
+    struct wl_client *wl_client;
+
+    struct wl_list infos;
+
+    struct {
+        struct wl_signal destroy;
+    } events;
+
+    struct wl_list link; // ds_tizen_global_resource::clients
+};
+
+struct ds_tizen_global_resource_info
+{
+    struct ds_tizen_global_resource_client *client;
+
+    struct wl_resource *resource;
+
+    struct ds_surface *surface;
+
+    uint32_t universal_id;
+
+    struct wl_list link; // ds_tizen_global_resource_client::infos
+};
+
+static void resource_handle_display_destroy(struct wl_listener *listener,
+        void *data);
+
+static void resource_bind(struct wl_client *wl_client, void *data,
+        uint32_t version, uint32_t id);
+
+static struct ds_tizen_global_resource_info *tizen_global_resource_client_find_info(
+    struct ds_tizen_global_resource_client *client,
+    struct ds_surface *surface);
+
+static struct ds_tizen_global_resource_info *tizen_global_resource_client_get_info(
+        struct ds_tizen_global_resource_client *client,
+        struct ds_surface *surface);
+
+WL_EXPORT struct ds_tizen_global_resource *
+ds_tizen_global_resource_create(struct wl_display *display)
+{
+    struct ds_tizen_global_resource *global_resource;
+
+    global_resource = calloc(1, sizeof *global_resource);
+    if (!global_resource) {
+        ds_err("calloc() failed.");
+        return NULL;
+    }
+
+    global_resource->global = wl_global_create(display, &tizen_surface_interface,
+            TIZEN_SURFACE_VERSION, global_resource, resource_bind);
+    if (!global_resource->global) {
+        ds_err("wl_global_create() failed. tizen_resource_interface");
+        free(global_resource);
+        return NULL;
+    }
+
+    wl_list_init(&global_resource->clients);
+
+    global_resource->destroy.notify = resource_handle_display_destroy;
+    wl_display_add_destroy_listener(display, &global_resource->destroy);
+
+    wl_signal_init(&global_resource->events.destroy);
+    wl_signal_init(&global_resource->events.get_resource_info);
+
+    ds_inf("Global created: tizen_global_resource(%p)", global_resource);
+
+    return global_resource;
+}
+
+WL_EXPORT void
+ds_tizen_global_resource_add_destroy_listener(
+        struct ds_tizen_global_resource *global_resource,
+        struct wl_listener *listener)
+{
+    wl_signal_add(&global_resource->events.destroy, listener);
+}
+
+WL_EXPORT void
+ds_tizen_global_resource_add_get_resource_info_listener(
+        struct ds_tizen_global_resource *global_resource,
+        struct wl_listener *listener)
+{
+    wl_signal_add(&global_resource->events.get_resource_info, listener);
+}
+
+WL_EXPORT void
+ds_tizen_global_resource_info_add_destroy_listener(
+        struct ds_tizen_global_resource_info *info,
+        struct wl_listener *listener)
+{
+    wl_signal_add(&info->client->events.destroy, listener);
+}
+
+WL_EXPORT struct ds_surface *
+ds_tizen_global_resource_info_get_surface(
+    struct ds_tizen_global_resource_info *info)
+{
+    return info->surface;
+}
+
+WL_EXPORT uint32_t
+ds_tizen_global_resource_get_universal_id(
+    struct ds_tizen_global_resource_info *info)
+{
+    return info->universal_id;
+}
+
+static struct ds_tizen_global_resource_info *
+tizen_global_resource_client_find_info(
+    struct ds_tizen_global_resource_client *client,
+    struct ds_surface *surface)
+{
+    struct ds_tizen_global_resource_info *info;
+
+    wl_list_for_each(info, &client->infos, link) {
+        if (surface == info->surface)
+            return info;
+    }
+
+    return NULL;
+}
+
+static struct ds_tizen_global_resource_info *
+tizen_global_resource_client_get_info(
+    struct ds_tizen_global_resource_client *client,
+    struct ds_surface *surface)
+{
+    struct ds_tizen_global_resource_info *info;
+
+    info = tizen_global_resource_client_find_info(client, surface);
+    if (info)
+        return info;
+
+    info = calloc(1, sizeof *info);
+    if (info == NULL) {
+        ds_err("calloc() failed. tizen_global_resource");
+        return NULL;
+    }
+
+    info->client = client;
+    info->surface = surface;
+
+    wl_list_insert(&client->infos, &info->link);
+
+    return info;
+}
+
+static void
+resource_handle_display_destroy(struct wl_listener *listener, void *data)
+{
+    struct ds_tizen_global_resource *global_resource;
+
+    global_resource = wl_container_of(listener, global_resource, destroy);
+
+    ds_inf("Global destroy: resource(%p)", global_resource);
+
+    wl_signal_emit(&global_resource->events.destroy, global_resource);
+    wl_list_remove(&global_resource->destroy.link);
+    wl_global_destroy(global_resource->global);
+    free(global_resource);
+}
+
+static void
+resource_handle_destroy(struct wl_client *wl_client,
+    struct wl_resource *resource)
+{
+    wl_resource_destroy(resource);
+}
+
+static const struct tizen_resource_interface resource_impl =
+{
+   resource_handle_destroy,
+};
+
+static void
+_tizen_surface_handle_destroy(struct wl_resource *resource)
+{
+    struct ds_tizen_global_resource_info *info;
+
+    info = wl_resource_get_user_data(resource);
+
+    ds_inf("_tizen_surface_handle_destroy (info:%p)", info);
+
+    wl_signal_emit(&info->client->events.destroy, info);
+    wl_list_remove(&info->link);
+    free(info);
+}
+
+static void
+surface_handle_get_tizen_global_resource(struct wl_client *wl_client,
+    struct wl_resource *resource,uint32_t id,
+    struct wl_resource *surface_resource)
+{
+    struct ds_tizen_global_resource_client *client;
+    struct ds_tizen_global_resource_info *info;
+    struct ds_surface *surface;
+
+    ds_inf("tizen_global_resource: surface_handle_get_tizen_global_resource");
+
+    client = wl_resource_get_user_data(resource);
+    surface = ds_surface_from_resource(surface_resource);
+
+    info = tizen_global_resource_client_get_info(client, surface);
+    if (info == NULL) {
+        ds_err("tizen_global_resource_client_get_info() failed.");
+        wl_client_post_no_memory(wl_client);
+        return;
+    }
+
+    info->resource = wl_resource_create(wl_client,
+        &tizen_resource_interface, wl_resource_get_version(resource),
+        id);
+    if (info->resource == NULL) {
+        ds_err("tizen_global_resource : wl_resource_create() failed.");
+        wl_list_remove(&info->link);
+        free(info);
+        wl_client_post_no_memory(wl_client);
+        return;
+    }
+
+    // generate the univeral_id;
+    info->universal_id = unique_id++;
+
+    wl_resource_set_implementation(info->resource, &resource_impl, info,
+        _tizen_surface_handle_destroy);
+
+    wl_signal_emit(&client->global_resource->events.get_resource_info, info);
+
+    tizen_resource_send_resource_id(info->resource, info->universal_id);
+}
+
+static void
+surface_handle_destroy(struct wl_client *wl_client,
+    struct wl_resource *resource)
+{
+    struct ds_tizen_global_resource_client *client;
+
+    client = wl_resource_get_user_data(resource);
+
+    if (!wl_list_empty(&client->infos)) {
+        ds_err("tizen_global_resource was destroyed before children");
+        return;
+    }
+
+    wl_resource_destroy(resource);
+}
+
+static const struct tizen_surface_interface surface_impl =
+{
+   surface_handle_get_tizen_global_resource,
+   surface_handle_destroy,
+};
+
+static void
+_tizen_global_resource_client_handle_destroy(struct wl_resource *resource)
+{
+    struct ds_tizen_global_resource_client *client;
+    struct ds_tizen_global_resource_info *info, *tmp;
+
+    client = wl_resource_get_user_data(resource);
+
+    ds_inf("_tizen_global_resource_client_handle_destroy (client:%p)", client);
+
+    wl_list_for_each_safe(info, tmp, &client->infos, link) {
+        wl_signal_emit(&info->client->events.destroy, info);
+        wl_list_remove(&info->link);
+        free(info);
+    }
+
+    wl_list_remove(&client->link);
+    free(client);
+}
+
+static void
+resource_bind(struct wl_client *wl_client, void *data, uint32_t version,
+        uint32_t id)
+{
+    struct ds_tizen_global_resource *global_resource = data;
+    struct ds_tizen_global_resource_client *client;
+
+    client = calloc(1, sizeof *client);
+    if (client == NULL) {
+        ds_err("calloc() failed. tizen_global_resource");
+        wl_client_post_no_memory(wl_client);
+        return;
+    }
+
+    ds_inf("tizen_global_resource_client binds. (client:%p)", client);
+
+    client->global_resource = global_resource;
+    client->wl_client = wl_client;
+
+    wl_list_init(&client->infos);
+
+    client->resource = wl_resource_create(wl_client, &tizen_surface_interface,
+            MIN(version, TIZEN_SURFACE_VERSION), id);
+
+    if (client->resource == NULL) {
+        ds_err("tizen_global_resource : wl_resource_create() failed.");
+        free(client);
+        wl_client_post_no_memory(wl_client);
+        return;
+    }
+
+    wl_resource_set_implementation(client->resource, &surface_impl, client,
+            _tizen_global_resource_client_handle_destroy);
+
+    wl_signal_init(&client->events.destroy);
+
+    wl_list_insert(&global_resource->clients, &client->link);
+}
diff --git a/src/global_resource/meson.build b/src/global_resource/meson.build
new file mode 100644 (file)
index 0000000..13d5d7a
--- /dev/null
@@ -0,0 +1,30 @@
+libds_tizen_global_resource_files = [
+  'global_resource.c',
+]
+
+libds_tizen_global_resource_deps = [
+  deps_libds_tizen,
+  dependency('tizen-extension-server', required: true),
+]
+
+lib_libds_tizen_global_resource = shared_library('ds-tizen-global-resource',
+  libds_tizen_global_resource_files,
+  dependencies: libds_tizen_global_resource_deps,
+  include_directories: [ common_inc, include_directories('.'), include_directories('..') ],
+  version: meson.project_version(),
+  install: true
+)
+
+deps_libds_tizen_global_resource = declare_dependency(
+  link_with: lib_libds_tizen_global_resource,
+  dependencies: libds_tizen_global_resource_deps,
+  include_directories: [ common_inc, include_directories('.') ],
+)
+
+pkgconfig = import('pkgconfig')
+pkgconfig.generate(lib_libds_tizen_global_resource,
+  version: meson.project_version(),
+  filebase: 'libds-tizen-global-resource',
+  name: 'libds-tizen-global-resource',
+  description: 'tizen resource extension of libds-tizen for tizen platform',
+)
index 4fee86e..66f10c6 100644 (file)
@@ -40,3 +40,4 @@ subdir('display_policy')
 subdir('memory_flusher')
 subdir('renderer')
 subdir('screen_rotation')
+subdir('global_resource')