From 81926df0f7c9c6459e7c7052b589bc5a27283a20 Mon Sep 17 00:00:00 2001 From: zheng_wenlong Date: Mon, 23 Oct 2017 14:38:56 +0900 Subject: Move sample from agl-service-homescreen-2017 This sample is using libhomescren with homescreen sevice, So better move inside of libhomescreen. Relative Commit: https://gerrit.automotivelinux.org/gerrit/#/c/11429/ Change-Id: Id9a1a2401a9a0a42442ce84025213036a4fccd78 Signed-off-by: zheng_wenlong --- sample/simple-egl/CMakeLists.txt | 69 ++ sample/simple-egl/README.md | 24 + .../include/ivi-application-client-protocol.h | 245 +++++++ sample/simple-egl/include/platform.h | 115 ++++ sample/simple-egl/package/hvac/config.xml | 16 + sample/simple-egl/package/hvac/icon.svg | 279 ++++++++ sample/simple-egl/package/navi/config.xml | 16 + sample/simple-egl/package/navi/icon.svg | 279 ++++++++ sample/simple-egl/package/root/config.xml | 16 + sample/simple-egl/package/root/icon.svg | 279 ++++++++ sample/simple-egl/src/simple-egl.cpp | 744 +++++++++++++++++++++ sample/template/README | 28 + sample/template/main.cpp | 207 ++++++ sample/template/smhandler.cpp | 157 +++++ sample/template/smhandler.h | 58 ++ sample/template/wmhandler.cpp | 36 + sample/template/wmhandler.h | 47 ++ 17 files changed, 2615 insertions(+) create mode 100644 sample/simple-egl/CMakeLists.txt create mode 100644 sample/simple-egl/README.md create mode 100644 sample/simple-egl/include/ivi-application-client-protocol.h create mode 100644 sample/simple-egl/include/platform.h create mode 100644 sample/simple-egl/package/hvac/config.xml create mode 100644 sample/simple-egl/package/hvac/icon.svg create mode 100644 sample/simple-egl/package/navi/config.xml create mode 100644 sample/simple-egl/package/navi/icon.svg create mode 100644 sample/simple-egl/package/root/config.xml create mode 100644 sample/simple-egl/package/root/icon.svg create mode 100644 sample/simple-egl/src/simple-egl.cpp create mode 100644 sample/template/README create mode 100644 sample/template/main.cpp create mode 100644 sample/template/smhandler.cpp create mode 100644 sample/template/smhandler.h create mode 100644 sample/template/wmhandler.cpp create mode 100644 sample/template/wmhandler.h diff --git a/sample/simple-egl/CMakeLists.txt b/sample/simple-egl/CMakeLists.txt new file mode 100644 index 0000000..b0d72f2 --- /dev/null +++ b/sample/simple-egl/CMakeLists.txt @@ -0,0 +1,69 @@ +# +# Copyright (c) 2017 TOYOTA MOTOR CORPORATION +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +project(simple-egl C CXX) + +cmake_minimum_required(VERSION 3.0) + +set(PROJECT_NAME "simple-egl") +set(PROJECT_VERSION "1.0") +set(PROJECT_PRETTY_NAME "SE") +set(PROJECT_DESCRIPTION "Sample application for homescreen and windowmanager") +set(PROJECT_URL "") +set(CMAKE_CXX_FLAGS "-Wall -fpermissive") + +#source directory +aux_source_directory(src DIR_SRCS) + +#head file path +include_directories(include) + +#set extern libraries +SET(LIBRARIES + libwindowmanager.so + libEGL.so + libGLESv2.so + libm.so + libwayland-egl.so + libwayland-client.so + libjson-c.so + libhomescreen.so + librt.so + libpthread.so +) + +#add executable file +add_executable(simple-egl ${DIR_SRCS}) + +#add link library +TARGET_LINK_LIBRARIES(simple-egl ${LIBRARIES}) + +add_custom_command(TARGET simple-egl POST_BUILD + COMMAND cp -rf ${CMAKE_CURRENT_SOURCE_DIR}/package ${PROJECT_BINARY_DIR} + COMMAND cp -rf ${PROJECT_BINARY_DIR}/simple-egl ${PROJECT_BINARY_DIR}/package/root/bin) + +add_custom_target(widget DEPENDS ${PROJECT_BINARY_DIR}/package/root + COMMAND wgtpkg-pack -f -o ${PROJECT_BINARY_DIR}/package/${PROJECT_NAME}.wgt ${PROJECT_BINARY_DIR}/package/root) + +add_custom_target(hvac DEPENDS ${PROJECT_BINARY_DIR}/package/root + COMMAND mkdir -p ${PROJECT_BINARY_DIR}/package/hvac/bin + COMMAND cp -rf ${PROJECT_BINARY_DIR}/simple-egl ${PROJECT_BINARY_DIR}/package/hvac/bin/hvac + COMMAND wgtpkg-pack -f -o ${PROJECT_BINARY_DIR}/package/hvac.wgt ${PROJECT_BINARY_DIR}/package/hvac) + +add_custom_target(navi DEPENDS ${PROJECT_BINARY_DIR}/package/root + COMMAND mkdir -p ${PROJECT_BINARY_DIR}/package/navi/bin + COMMAND cp -rf ${PROJECT_BINARY_DIR}/simple-egl ${PROJECT_BINARY_DIR}/package/navi/bin/navi + COMMAND wgtpkg-pack -f -o ${PROJECT_BINARY_DIR}/package/navi.wgt ${PROJECT_BINARY_DIR}/package/navi) diff --git a/sample/simple-egl/README.md b/sample/simple-egl/README.md new file mode 100644 index 0000000..126f00c --- /dev/null +++ b/sample/simple-egl/README.md @@ -0,0 +1,24 @@ +Information +==== +
This is a sample application for homescreen-2017 and windowmanager-2017. + +How to compile and install +==== +
$ mkdir build +
$ cd build +
$ cmake .. +
$ make +
$ make widget + +
Copy package/simple-egl.wgt to rootfs. + +How to use +==== +
afm-util install simple-egl.wgt +
afm-util start simple-egl@0.1 + +Depends +==== +
homescreen-2017 +
agl-service-homescreen-2017 +
agl-service-windowmanger-2017 diff --git a/sample/simple-egl/include/ivi-application-client-protocol.h b/sample/simple-egl/include/ivi-application-client-protocol.h new file mode 100644 index 0000000..ca31bf7 --- /dev/null +++ b/sample/simple-egl/include/ivi-application-client-protocol.h @@ -0,0 +1,245 @@ +/* Generated by wayland-scanner 1.11.0 */ + +#ifndef IVI_APPLICATION_CLIENT_PROTOCOL_H +#define IVI_APPLICATION_CLIENT_PROTOCOL_H + +#include +#include +#include "wayland-client.h" + +//#ifdef __cplusplus +//extern "C" { +//#endif + +/** + * @page page_ivi_application The ivi_application protocol + * @section page_ifaces_ivi_application Interfaces + * - @subpage page_iface_ivi_surface - application interface to surface in ivi compositor + * - @subpage page_iface_ivi_application - create ivi-style surfaces + * @section page_copyright_ivi_application Copyright + *
+ *
+ * Copyright (C) 2013 DENSO CORPORATION
+ * Copyright (c) 2013 BMW Car IT GmbH
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ * 
+ */ +struct ivi_application; +struct ivi_surface; +struct wl_surface; + +/** + * @page page_iface_ivi_surface ivi_surface + * @section page_iface_ivi_surface_desc Description + * @section page_iface_ivi_surface_api API + * See @ref iface_ivi_surface. + */ +/** + * @defgroup iface_ivi_surface The ivi_surface interface + */ +extern const struct wl_interface ivi_surface_interface; +/** + * @page page_iface_ivi_application ivi_application + * @section page_iface_ivi_application_desc Description + * + * This interface is exposed as a global singleton. + * This interface is implemented by servers that provide IVI-style user interfaces. + * It allows clients to associate a ivi_surface with wl_surface. + * @section page_iface_ivi_application_api API + * See @ref iface_ivi_application. + */ +/** + * @defgroup iface_ivi_application The ivi_application interface + * + * This interface is exposed as a global singleton. + * This interface is implemented by servers that provide IVI-style user interfaces. + * It allows clients to associate a ivi_surface with wl_surface. + */ +extern const struct wl_interface ivi_application_interface; + +/** + * @ingroup iface_ivi_surface + * @struct ivi_surface_listener + */ +struct ivi_surface_listener { + /** + * suggest resize + * + * The configure event asks the client to resize its surface. + * + * The size is a hint, in the sense that the client is free to + * ignore it if it doesn't resize, pick a smaller size (to satisfy + * aspect ratio or resize in steps of NxM pixels). + * + * The client is free to dismiss all but the last configure event + * it received. + * + * The width and height arguments specify the size of the window in + * surface-local coordinates. + */ + void (*configure)(void *data, + struct ivi_surface *ivi_surface, + int32_t width, + int32_t height); +}; + +/** + * @ingroup ivi_surface_iface + */ +static inline int +ivi_surface_add_listener(struct ivi_surface *ivi_surface, + const struct ivi_surface_listener *listener, void *data) +{ + return wl_proxy_add_listener((struct wl_proxy *) ivi_surface, + (void (**)(void)) listener, data); +} + +#define IVI_SURFACE_DESTROY 0 + +/** + * @ingroup iface_ivi_surface + */ +#define IVI_SURFACE_DESTROY_SINCE_VERSION 1 + +/** @ingroup iface_ivi_surface */ +static inline void +ivi_surface_set_user_data(struct ivi_surface *ivi_surface, void *user_data) +{ + wl_proxy_set_user_data((struct wl_proxy *) ivi_surface, user_data); +} + +/** @ingroup iface_ivi_surface */ +static inline void * +ivi_surface_get_user_data(struct ivi_surface *ivi_surface) +{ + return wl_proxy_get_user_data((struct wl_proxy *) ivi_surface); +} + +static inline uint32_t +ivi_surface_get_version(struct ivi_surface *ivi_surface) +{ + return wl_proxy_get_version((struct wl_proxy *) ivi_surface); +} + +/** + * @ingroup iface_ivi_surface + * + * This removes link from ivi_id to wl_surface and destroys ivi_surface. + * The ID, ivi_id, is free and can be used for surface_create again. + */ +static inline void +ivi_surface_destroy(struct ivi_surface *ivi_surface) +{ + wl_proxy_marshal((struct wl_proxy *) ivi_surface, + IVI_SURFACE_DESTROY); + + wl_proxy_destroy((struct wl_proxy *) ivi_surface); +} + +#ifndef IVI_APPLICATION_ERROR_ENUM +#define IVI_APPLICATION_ERROR_ENUM +enum ivi_application_error { + /** + * given wl_surface has another role + */ + IVI_APPLICATION_ERROR_ROLE = 0, + /** + * given ivi_id is assigned to another wl_surface + */ + IVI_APPLICATION_ERROR_IVI_ID = 1, +}; +#endif /* IVI_APPLICATION_ERROR_ENUM */ + +#define IVI_APPLICATION_SURFACE_CREATE 0 + +/** + * @ingroup iface_ivi_application + */ +#define IVI_APPLICATION_SURFACE_CREATE_SINCE_VERSION 1 + +/** @ingroup iface_ivi_application */ +static inline void +ivi_application_set_user_data(struct ivi_application *ivi_application, void *user_data) +{ + wl_proxy_set_user_data((struct wl_proxy *) ivi_application, user_data); +} + +/** @ingroup iface_ivi_application */ +static inline void * +ivi_application_get_user_data(struct ivi_application *ivi_application) +{ + return wl_proxy_get_user_data((struct wl_proxy *) ivi_application); +} + +static inline uint32_t +ivi_application_get_version(struct ivi_application *ivi_application) +{ + return wl_proxy_get_version((struct wl_proxy *) ivi_application); +} + +/** @ingroup iface_ivi_application */ +static inline void +ivi_application_destroy(struct ivi_application *ivi_application) +{ + wl_proxy_destroy((struct wl_proxy *) ivi_application); +} + +/** + * @ingroup iface_ivi_application + * + * This request gives the wl_surface the role of an IVI Surface. Creating more than + * one ivi_surface for a wl_surface is not allowed. Note, that this still allows the + * following example: + * + * 1. create a wl_surface + * 2. create ivi_surface for the wl_surface + * 3. destroy the ivi_surface + * 4. create ivi_surface for the wl_surface (with the same or another ivi_id as before) + * + * surface_create will create a interface:ivi_surface with numeric ID; ivi_id in + * ivi compositor. These ivi_ids are defined as unique in the system to identify + * it inside of ivi compositor. The ivi compositor implements business logic how to + * set properties of the surface with ivi_id according to status of the system. + * E.g. a unique ID for Car Navigation application is used for implementing special + * logic of the application about where it shall be located. + * The server regards following cases as protocol errors and disconnects the client. + * - wl_surface already has an nother role. + * - ivi_id is already assigned to an another wl_surface. + * + * If client destroys ivi_surface or wl_surface which is assigne to the ivi_surface, + * ivi_id which is assigned to the ivi_surface is free for reuse. + */ +static inline struct ivi_surface * +ivi_application_surface_create(struct ivi_application *ivi_application, uint32_t ivi_id, struct wl_surface *surface) +{ + struct wl_proxy *id; + + id = wl_proxy_marshal_constructor((struct wl_proxy *) ivi_application, + IVI_APPLICATION_SURFACE_CREATE, &ivi_surface_interface, ivi_id, surface, NULL); + + return (struct ivi_surface *) id; +} + +//#ifdef __cplusplus +//} +//#endif + +#endif diff --git a/sample/simple-egl/include/platform.h b/sample/simple-egl/include/platform.h new file mode 100644 index 0000000..8fd57b0 --- /dev/null +++ b/sample/simple-egl/include/platform.h @@ -0,0 +1,115 @@ +/* + * Copyright © 2015 Collabora, Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial + * portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef WESTON_PLATFORM_H +#define WESTON_PLATFORM_H + +#include + +#include +#include +#include + +#ifndef EGL_PLATFORM_WAYLAND_KHR +#define EGL_PLATFORM_WAYLAND_KHR 0x31D8 +#endif + +#ifdef __cplusplus +extern "C" { +#endif + + +#ifndef EGL_EXT_platform_base +typedef EGLDisplay (*PFNEGLGETPLATFORMDISPLAYEXTPROC) (EGLenum platform, + void *native_display, + const EGLint *attrib_list); +typedef EGLSurface (*PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC) (EGLDisplay dpy, + EGLConfig config, + void *native_window, + const EGLint *attrib_list); +#endif + +static inline void * +weston_platform_get_egl_proc_address(const char *address) +{ + const char *extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS); + + if (extensions + && (strstr(extensions, "EGL_EXT_platform_wayland") + || strstr(extensions, "EGL_KHR_platform_wayland"))) { + return (void *) eglGetProcAddress(address); + } + + return NULL; +} + +static inline EGLDisplay +weston_platform_get_egl_display(EGLenum platform, void *native_display, + const EGLint *attrib_list) +{ + static PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL; + + if (!get_platform_display) { + get_platform_display = (PFNEGLGETPLATFORMDISPLAYEXTPROC) + weston_platform_get_egl_proc_address( + "eglGetPlatformDisplayEXT"); + } + + if (get_platform_display) + return get_platform_display(platform, + native_display, attrib_list); + + return eglGetDisplay((EGLNativeDisplayType) native_display); +} + +static inline EGLSurface +weston_platform_create_egl_surface(EGLDisplay dpy, EGLConfig config, + void *native_window, + const EGLint *attrib_list) +{ + static PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC + create_platform_window = NULL; + + if (!create_platform_window) { + create_platform_window = (PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC) + weston_platform_get_egl_proc_address( + "eglCreatePlatformWindowSurfaceEXT"); + } + + if (create_platform_window) + return create_platform_window(dpy, config, + native_window, + attrib_list); + + return eglCreateWindowSurface(dpy, config, + (EGLNativeWindowType) native_window, + attrib_list); +} + +#ifdef __cplusplus +} +#endif + +#endif /* WESTON_PLATFORM_H */ diff --git a/sample/simple-egl/package/hvac/config.xml b/sample/simple-egl/package/hvac/config.xml new file mode 100644 index 0000000..9af2114 --- /dev/null +++ b/sample/simple-egl/package/hvac/config.xml @@ -0,0 +1,16 @@ + + + HVAC + + + This is a demo application for simple-egl + TOYOA + APL 2.0 + + + + + + + + diff --git a/sample/simple-egl/package/hvac/icon.svg b/sample/simple-egl/package/hvac/icon.svg new file mode 100644 index 0000000..91661a7 --- /dev/null +++ b/sample/simple-egl/package/hvac/icon.svg @@ -0,0 +1,279 @@ + + + +image/svg+xmlMULTIMEDIA + \ No newline at end of file diff --git a/sample/simple-egl/package/navi/config.xml b/sample/simple-egl/package/navi/config.xml new file mode 100644 index 0000000..6f6e649 --- /dev/null +++ b/sample/simple-egl/package/navi/config.xml @@ -0,0 +1,16 @@ + + + Navigation + + + This is a demo application for simple-egl + TOYOA + APL 2.0 + + + + + + + + diff --git a/sample/simple-egl/package/navi/icon.svg b/sample/simple-egl/package/navi/icon.svg new file mode 100644 index 0000000..91661a7 --- /dev/null +++ b/sample/simple-egl/package/navi/icon.svg @@ -0,0 +1,279 @@ + + + +image/svg+xmlMULTIMEDIA + \ No newline at end of file diff --git a/sample/simple-egl/package/root/config.xml b/sample/simple-egl/package/root/config.xml new file mode 100644 index 0000000..17abd41 --- /dev/null +++ b/sample/simple-egl/package/root/config.xml @@ -0,0 +1,16 @@ + + + SIMPLE-EGL + + + This is a demo application for simple-egl + TOYOA + APL 2.0 + + + + + + + + diff --git a/sample/simple-egl/package/root/icon.svg b/sample/simple-egl/package/root/icon.svg new file mode 100644 index 0000000..91661a7 --- /dev/null +++ b/sample/simple-egl/package/root/icon.svg @@ -0,0 +1,279 @@ + + + +image/svg+xmlMULTIMEDIA + \ No newline at end of file diff --git a/sample/simple-egl/src/simple-egl.cpp b/sample/simple-egl/src/simple-egl.cpp new file mode 100644 index 0000000..173d728 --- /dev/null +++ b/sample/simple-egl/src/simple-egl.cpp @@ -0,0 +1,744 @@ +/* + * Copyright (c) 2017 TOYOTA MOTOR CORPORATION + * Copyright © 2011 Benjamin Franzke + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include +#include +#include + +#include "libwindowmanager.h" +#include + +#include "ivi-application-client-protocol.h" +using namespace std; + + +uint32_t g_id_ivisurf = 9009; +long port = 1700; +string token = string("wm"); +bool enable_debug = false; + +string app_name = string("Navigation"); + +LibHomeScreen* hs; +LibWindowmanager *wm; + +static const struct wl_interface *types[] = { + NULL, + NULL, + NULL, + &wl_surface_interface, + &ivi_surface_interface, +}; + +static const struct wl_message ivi_surface_requests[] = { + { "destroy", "", types + 0 }, +}; + +static const struct wl_message ivi_surface_events[] = { + { "configure", "ii", types + 0 }, +}; + +const struct wl_interface ivi_surface_interface = { + "ivi_surface", 1, + 1, ivi_surface_requests, + 1, ivi_surface_events, +}; + +static const struct wl_message ivi_application_requests[] = { + { "surface_create", "uon", types + 2 }, +}; + +const struct wl_interface ivi_application_interface = { + "ivi_application", 1, + 1, ivi_application_requests, + 0, NULL, +}; + +#include "platform.h" + +#ifndef EGL_EXT_swap_buffers_with_damage +#define EGL_EXT_swap_buffers_with_damage 1 +typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects); +#endif + +#ifndef EGL_EXT_buffer_age +#define EGL_EXT_buffer_age 1 +#define EGL_BUFFER_AGE_EXT 0x313D +#endif + +struct window; +struct seat; + +struct display { + struct wl_display *display; + struct wl_registry *registry; + struct wl_compositor *compositor; + struct wl_seat *seat; + struct { + EGLDisplay dpy; + EGLContext ctx; + EGLConfig conf; + } egl; + struct window *window; + struct ivi_application *ivi_application; + + PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage; +}; + +struct geometry { + int width, height; +}; + +struct window { + struct display *display; + struct geometry geometry, window_size; + struct { + GLuint rotation_uniform; + GLuint pos; + GLuint col; + } gl; + + uint32_t benchmark_time, frames; + struct wl_egl_window *native; + struct wl_surface *surface; + struct ivi_surface *ivi_surface; + EGLSurface egl_surface; + struct wl_callback *callback; + int fullscreen, opaque, buffer_size, frame_sync; +}; + +static const char *vert_shader_text = + "uniform mat4 rotation;\n" + "attribute vec4 pos;\n" + "attribute vec4 color;\n" + "varying vec4 v_color;\n" + "void main() {\n" + " gl_Position = rotation * pos;\n" + " v_color = color;\n" + "}\n"; + +static const char *frag_shader_text = + "precision mediump float;\n" + "varying vec4 v_color;\n" + "void main() {\n" + " gl_FragColor = v_color;\n" + "}\n"; + +static int running = 1; + +static void debug_out(const char* str, ...) +{ + if(!enable_debug) + return; + char *out; + va_list arg_ptr; + va_start(arg_ptr, str); + vasprintf(&out, str, arg_ptr); + cout << out; + va_end(arg_ptr); + // cout << endl; +} + +static void +init_egl(struct display *display, struct window *window) +{ + static const EGLint context_attribs[] = { + EGL_CONTEXT_CLIENT_VERSION, 2, + EGL_NONE + }; + const char *extensions; + + EGLint config_attribs[] = { + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_ALPHA_SIZE, 1, + EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, + EGL_NONE + }; + + EGLint major, minor, n, count, i, size; + EGLConfig *configs; + EGLBoolean ret; + + if (window->opaque || window->buffer_size == 16) + config_attribs[9] = 0; + + display->egl.dpy = weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR, display->display, NULL); + assert(display->egl.dpy); + + ret = eglInitialize(display->egl.dpy, &major, &minor); + assert(ret == EGL_TRUE); + ret = eglBindAPI(EGL_OPENGL_ES_API); + assert(ret == EGL_TRUE); + + if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1) + assert(0); + + configs = calloc(count, sizeof *configs); + assert(configs); + + ret = eglChooseConfig(display->egl.dpy, config_attribs, + configs, count, &n); + assert(ret && n >= 1); + + for (i = 0; i < n; i++) { + eglGetConfigAttrib(display->egl.dpy, + configs[i], EGL_BUFFER_SIZE, &size); + if (window->buffer_size == size) { + display->egl.conf = configs[i]; + break; + } + } + free(configs); + if (display->egl.conf == NULL) { + debug_out("did not find config with buffer size %d\n", + window->buffer_size); + exit(EXIT_FAILURE); + } + + display->egl.ctx = eglCreateContext(display->egl.dpy, + display->egl.conf, + EGL_NO_CONTEXT, context_attribs); + assert(display->egl.ctx); + + display->swap_buffers_with_damage = NULL; + extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS); + if (extensions && + strstr(extensions, "EGL_EXT_swap_buffers_with_damage") && + strstr(extensions, "EGL_EXT_buffer_age")) + display->swap_buffers_with_damage = + (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC) + eglGetProcAddress("eglSwapBuffersWithDamageEXT"); + + if (display->swap_buffers_with_damage) + debug_out("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n"); + +} + +static void +fini_egl(struct display *display) +{ + eglTerminate(display->egl.dpy); + eglReleaseThread(); +} + +static GLuint +create_shader(struct window *window, const char *source, GLenum shader_type) +{ + GLuint shader; + GLint status; + + shader = glCreateShader(shader_type); + assert(shader != 0); + + glShaderSource(shader, 1, (const char **) &source, NULL); + glCompileShader(shader); + + glGetShaderiv(shader, GL_COMPILE_STATUS, &status); + if (!status) { + char log[1000]; + GLsizei len; + glGetShaderInfoLog(shader, 1000, &len, log); + debug_out("Error: compiling %s: %*s\n", + shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment", + len, log); + exit(1); + } + + return shader; +} + +static void +init_gl(struct window *window) +{ + GLuint frag, vert; + GLuint program; + GLint status; + + frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER); + vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER); + + program = glCreateProgram(); + glAttachShader(program, frag); + glAttachShader(program, vert); + glLinkProgram(program); + + glGetProgramiv(program, GL_LINK_STATUS, &status); + if (!status) { + char log[1000]; + GLsizei len; + glGetProgramInfoLog(program, 1000, &len, log); + debug_out("Error: linking:\n%*s\n", len, log); + exit(1); + } + + glUseProgram(program); + + window->gl.pos = 0; + window->gl.col = 1; + + glBindAttribLocation(program, window->gl.pos, "pos"); + glBindAttribLocation(program, window->gl.col, "color"); + glLinkProgram(program); + + window->gl.rotation_uniform = + glGetUniformLocation(program, "rotation"); +} + +static void +handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface, + int32_t width, int32_t height) +{ + struct window *window = data; + + wl_egl_window_resize(window->native, width, height, 0, 0); + + window->geometry.width = width; + window->geometry.height = height; + + if (!window->fullscreen) + window->window_size = window->geometry; +} + +static const struct ivi_surface_listener ivi_surface_listener = { + handle_ivi_surface_configure, +}; + +static void +create_ivi_surface(struct window *window, struct display *display) +{ + uint32_t id_ivisurf = g_id_ivisurf; + window->ivi_surface = + ivi_application_surface_create(display->ivi_application, + id_ivisurf, window->surface); + + if (window->ivi_surface == NULL) { + debug_out("Failed to create ivi_client_surface\n"); + abort(); + } + + ivi_surface_add_listener(window->ivi_surface, + &ivi_surface_listener, window); +} + +static void +create_surface(struct window *window) +{ + struct display *display = window->display; + EGLBoolean ret; + + window->surface = wl_compositor_create_surface(display->compositor); + + window->native = + wl_egl_window_create(window->surface, + window->geometry.width, + window->geometry.height); + window->egl_surface = + weston_platform_create_egl_surface(display->egl.dpy, + display->egl.conf, + window->native, NULL); + + + if (display->ivi_application ) { + create_ivi_surface(window, display); + } else { + assert(0); + } + + ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface, + window->egl_surface, window->display->egl.ctx); + assert(ret == EGL_TRUE); + + if (!window->frame_sync) + eglSwapInterval(display->egl.dpy, 0); + +} + +static void +destroy_surface(struct window *window) +{ + /* Required, otherwise segfault in egl_dri2.c: dri2_make_current() + * on eglReleaseThread(). */ + eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, + EGL_NO_CONTEXT); + + eglDestroySurface(window->display->egl.dpy, window->egl_surface); + wl_egl_window_destroy(window->native); + + if (window->display->ivi_application) + ivi_surface_destroy(window->ivi_surface); + wl_surface_destroy(window->surface); + + if (window->callback) + wl_callback_destroy(window->callback); +} + +static void +redraw(void *data, struct wl_callback *callback, uint32_t time) +{ + struct window *window = data; + struct display *display = window->display; + static const GLfloat verts[3][2] = { + { -0.5, -0.5 }, + { 0.5, -0.5 }, + { 0, 0.5 } + }; + + static const GLfloat colors_H[3][3] = { + { 1, 1, 1 }, + { 1, 1, 1 }, + { 1, 1, 1 } + }; + + static const GLfloat colors_N[3][3] = { + { 1, 0, 0 }, + { 0, 1, 0 }, + { 0, 0, 1 } + }; + + GLfloat angle; + GLfloat rotation[4][4] = { + { 1, 0, 0, 0 }, + { 0, 1, 0, 0 }, + { 0, 0, 1, 0 }, + { 0, 0, 0, 1 } + }; + static const uint32_t speed_div = 5, benchmark_interval = 5; + struct wl_region *region; + EGLint rect[4]; + EGLint buffer_age = 0; + struct timeval tv; + + assert(window->callback == callback); + window->callback = NULL; + + if (callback) + wl_callback_destroy(callback); + + gettimeofday(&tv, NULL); + time = tv.tv_sec * 1000 + tv.tv_usec / 1000; + if (window->frames == 0) + window->benchmark_time = time; + if (time - window->benchmark_time > (benchmark_interval * 1000)) { + debug_out("%d frames in %d seconds: %f fps\n", + window->frames, + benchmark_interval, + (float) window->frames / benchmark_interval); + window->benchmark_time = time; + window->frames = 0; + } + + angle = (time / speed_div) % 360 * M_PI / 180.0; + rotation[0][0] = cos(angle); + rotation[0][2] = sin(angle); + rotation[2][0] = -sin(angle); + rotation[2][2] = cos(angle); + + if (display->swap_buffers_with_damage) + eglQuerySurface(display->egl.dpy, window->egl_surface, + EGL_BUFFER_AGE_EXT, &buffer_age); + + glViewport(0, 0, window->geometry.width, window->geometry.height); + + glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE, + (GLfloat *) rotation); + + glClearColor(0.0, 0.0, 0.0, 0.5); + glClear(GL_COLOR_BUFFER_BIT); + + glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts); + if(app_name == string("HVAC")){ + glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors_H); + } + else{ + glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors_N); + } + glEnableVertexAttribArray(window->gl.pos); + glEnableVertexAttribArray(window->gl.col); + + glDrawArrays(GL_TRIANGLES, 0, 3); + + glDisableVertexAttribArray(window->gl.pos); + glDisableVertexAttribArray(window->gl.col); + + if (window->opaque || window->fullscreen) { + region = wl_compositor_create_region(window->display->compositor); + wl_region_add(region, 0, 0, + window->geometry.width, + window->geometry.height); + wl_surface_set_opaque_region(window->surface, region); + wl_region_destroy(region); + } else { + wl_surface_set_opaque_region(window->surface, NULL); + } + + if (display->swap_buffers_with_damage && buffer_age > 0) { + rect[0] = window->geometry.width / 4 - 1; + rect[1] = window->geometry.height / 4 - 1; + rect[2] = window->geometry.width / 2 + 2; + rect[3] = window->geometry.height / 2 + 2; + display->swap_buffers_with_damage(display->egl.dpy, + window->egl_surface, + rect, 1); + } else { + eglSwapBuffers(display->egl.dpy, window->egl_surface); + } + + window->frames++; +} + +static void +registry_handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version) +{ + struct display *d = data; + + if (strcmp(interface, "wl_compositor") == 0) { + d->compositor = + wl_registry_bind(registry, name, + &wl_compositor_interface, 1); + } else if (strcmp(interface, "ivi_application") == 0) { + d->ivi_application = + wl_registry_bind(registry, name, + &ivi_application_interface, 1); + } +} + +static void +registry_handle_global_remove(void *data, struct wl_registry *registry, + uint32_t name) +{ +} + +static const struct wl_registry_listener registry_listener = { + registry_handle_global, + registry_handle_global_remove +}; + +static void +signal_int(int signum) +{ + running = 0; +} + +int +init_wm(LibWindowmanager *wm) +{ + char* surfaceIdStr; + + if (wm->init(port, token.c_str()) != 0) { + debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] wm init failed. \n"); + return -1; + } + + if (wm->requestSurface(app_name.c_str()) != 0) { + debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] wm request surface failed \n"); + return -1; + } + + wm->set_event_handler(LibWindowmanager::Event_Active, [](char const *label) { + debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] Surface %s got activated! \n", label); + }); + + wm->set_event_handler(LibWindowmanager::Event_Inactive, [](char const *label) { + debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] Surface %s got inactivated!\n", label); + }); + + wm->set_event_handler(LibWindowmanager::Event_Visible, [wm](char const *label) { + debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] Surface %s got visibled!\n", label); + }); + + wm->set_event_handler(LibWindowmanager::Event_Invisible, [](char const *label) { + debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] Surface %s got invisibled!\n", label); + }); + + wm->set_event_handler(LibWindowmanager::Event_SyncDraw, [wm](char const *label) { + debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] Surface %s got syncdraw!\n", label); + debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] try to endDraw %s \n", app_name.c_str()); + wm->endDraw(app_name.c_str()); + }); + + wm->set_event_handler(LibWindowmanager::Event_FlushDraw, [](char const *label) { + debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] Surface %s got flushdraw! \n", label); + }); + + do + { + surfaceIdStr = getenv("QT_IVI_SURFACE_ID"); + } while (surfaceIdStr == NULL); + + g_id_ivisurf = atoi(surfaceIdStr); + debug_out("************** [SIMPLE EGL] [WM SIMPLE >>>>] IVI_SURFACE_ID: %d \n", g_id_ivisurf); + + return 0; +} + +int +init_hs(LibHomeScreen* hs){ + if(hs->init(port, token)!=0) + { + debug_out("************** [SIMPLE EGL] [HS SIMPLE >>>>] homescreen init failed. \n"); + return -1; + } + + hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [](const char* application_name){ + debug_out("************** [SIMPLE EGL] [HS SIMPLE >>>>] Event_TapShortcut application_name = %s \n", application_name); + if(strcmp(application_name, app_name.c_str()) == 0) + { + debug_out("************** [SIMPLE EGL] [HS SIMPLE] try to activesurface %s \n", app_name.c_str()); + wm->activateSurface(app_name.c_str()); + } + }); + + hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [](const char* display_message){ + debug_out("************** [SIMPLE EGL] [HS SIMPLE >>>>] Event_OnScreenMessage display_message = %s \n", display_message); + }); + + return 0; +} + +int +main(int argc, char **argv) +{ + struct sigaction sigint; + struct window window = { 0 }; + struct display display = { 0 }; + + if(getenv("ENABLE_DEMO_DEBUG")) + { + enable_debug = true; + } + + window.display = &display; + display.window = &window; + window.geometry.width = 1080; + window.geometry.height = 1488; + window.window_size = window.geometry; + window.buffer_size = 32; + window.frame_sync = 1; + + if(argc > 2) + { + if(string(argv[0]).find("hvac") != string::npos) + app_name = string("HVAC"); + port = strtol(argv[1], NULL, 10); + token = argv[2]; + } + + debug_out("************** [SIMPLE EGL] [MAIN] app_name: %s, port: %d, token: %s. \n", app_name.c_str(), port, token.c_str()); + + display.display = wl_display_connect(NULL); + assert(display.display); + + display.registry = wl_display_get_registry(display.display); + wl_registry_add_listener(display.registry, + ®istry_listener, &display); + + wl_display_roundtrip(display.display); + + init_egl(&display, &window); + + wm = new LibWindowmanager(); + hs = new LibHomeScreen(); + + if(init_wm(wm)!=0){ + fini_egl(&display); + if (display.ivi_application) + ivi_application_destroy(display.ivi_application); + if (display.compositor) + wl_compositor_destroy(display.compositor); + wl_registry_destroy(display.registry); + wl_display_flush(display.display); + return -1; + } + + if(init_hs(hs)!=0){ + fini_egl(&display); + if (display.ivi_application) + ivi_application_destroy(display.ivi_application); + if (display.compositor) + wl_compositor_destroy(display.compositor); + wl_registry_destroy(display.registry); + wl_display_flush(display.display); + return -1; + } + + create_surface(&window); + init_gl(&window); + + //Ctrl+C + sigint.sa_handler = signal_int; + sigemptyset(&sigint.sa_mask); + sigint.sa_flags = SA_RESETHAND; + sigaction(SIGINT, &sigint, NULL); + + eglSwapBuffers(display.egl.dpy, window.egl_surface); + wm->activateSurface(app_name.c_str()); + + /* The mainloop here is a little subtle. Redrawing will cause + * EGL to read events so we can just call + * wl_display_dispatch_pending() to handle any events that got + * queued up as a side effect. */ + while (running) { + wl_display_dispatch_pending(display.display); + redraw(&window, NULL, 0); + } + + debug_out("************** [SIMPLE EGL] [MAIN] simple-egl exiting! \n"); + + destroy_surface(&window); + fini_egl(&display); + + if (display.ivi_application) + ivi_application_destroy(display.ivi_application); + + if (display.compositor) + wl_compositor_destroy(display.compositor); + + wl_registry_destroy(display.registry); + wl_display_flush(display.display); + wl_display_disconnect(display.display); + + return 0; +} diff --git a/sample/template/README b/sample/template/README new file mode 100644 index 0000000..3012b79 --- /dev/null +++ b/sample/template/README @@ -0,0 +1,28 @@ +========== + Overview +========== + This is the templete that is useful for implementation of Qt application + by using HomeScreen, WindowManager and SoundManager. + + +=========== + Structure +=========== + - main.cpp + The templete for implementation main function for Qt application. + + - wmhandler.cpp + - wmhandler.h + These file is implemented "slot" + that is needed by Qt application that uses WindowManager. + These file should be modified according to Qt application. + + - smhandler.cpp + - smhandler.h + These file is implemented "signal" and "slot" + that is needed by Qt application that uses SoundManager. + These file should be modified according to Qt application. + + - README + This document. + diff --git a/sample/template/main.cpp b/sample/template/main.cpp new file mode 100644 index 0000000..ccbe935 --- /dev/null +++ b/sample/template/main.cpp @@ -0,0 +1,207 @@ +/* + * Copyright (c) 2017 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "wmhandler.h" +#include "smhandler.h" + + +static LibHomeScreen* hs; +static LibWindowmanager* wm; +static LibSMWrapper* smw; +static WmHandler* wmh; + +static std::string myname = std::string("Templete"); + + +static void onRep(struct json_object* reply_contents); +static void onEv(const std::string& event, struct json_object* event_contents); + + +int main(int argc, char *argv[]) +{ + QGuiApplication app(argc, argv); + QQmlApplicationEngine engine; + QQmlContext* context = engine.rootContext(); + QObject* root; + QQuickWindow* window; + + QQuickStyle::setStyle("AGL"); + + /* + * Set argument and option + */ + QCommandLineParser parser; + parser.addPositionalArgument("port", app.translate("main", "port for binding")); + parser.addPositionalArgument("secret", app.translate("main", "secret for binding")); + parser.addHelpOption(); + parser.addVersionOption(); + parser.process(app); + QStringList positionalArguments = parser.positionalArguments(); + + if (positionalArguments.length() == 2) { + /* + * Get argument + */ + int port = positionalArguments.takeFirst().toInt(); + QString secret = positionalArguments.takeFirst(); + std::string token = secret.toStdString(); + + + /* + * Get instance + */ + hs = new LibHomeScreen(); + wm = new LibWindowmanager(); + smw = new LibSMWrapper(port, secret); + wmh = new WmHandler(); + + + /* + * Set WindowManager + */ + // Initialize + if(wm->init(port, token.c_str()) != 0){ + exit(EXIT_FAILURE); + } + + // Application should call requestSurface at first + if (wm->requestSurface(myname.c_str()) != 0) { + exit(EXIT_FAILURE); + } + + // Set event handlers for each event + wm->set_event_handler(LibWindowmanager::Event_Active, [](char const *label) { + fprintf(stderr, "Surface %s got activated!\n", label); + }); + wm->set_event_handler(LibWindowmanager::Event_Inactive, [](char const *label) { + fprintf(stderr, "Surface %s got deactivated!\n", label); + }); + wm->set_event_handler(LibWindowmanager::Event_Visible, [](char const *label) { + fprintf(stderr, "Surface %s got visible!\n", label); + }); + wm->set_event_handler(LibWindowmanager::Event_Invisible, [](char const *label) { + fprintf(stderr, "Surface %s got invisible!\n", label); + }); + wm->set_event_handler(LibWindowmanager::Event_SyncDraw, [wm](char const *label) { + fprintf(stderr, "Surface %s got syncDraw!\n", label); + // Application should call LibWindowmanager::endDraw() in SyncDraw handler + wm->endDraw(label); + }); + wm->set_event_handler(LibWindowmanager::Event_FlushDraw, [](char const *label) { + fprintf(stderr, "Surface %s got flushDraw!\n", label); + }); + + // Initialize WmHandler + wmh->init(wm, myname.c_str()); + + + /* + * Set HomeScreen + */ + // Initialize + hs->init(port, token.c_str()); + + // Set event handler + hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [wm](const char* appname) { + if(myname == appname) { + qDebug("Surface %s got tapShortcut\n", appname); + // Application should call LibWindowmanager::endDraw() in TapShortcut handler + wm->activateSurface(myname.c_str()); + } + }); + + // Run event loop for HomeScreen + hs->runEventloop(); + + + /* + * Set SoundManager + */ + smw->wrapper_registerCallback(onEv, onRep); + smw->subscribe(QString("newMainConnection")); + smw->subscribe(QString("mainConnectionStateChanged")); + smw->subscribe(QString("removedMainConnection")); + smw->subscribe(QString("asyncSetSourceState")); + smw->subscribe(QString("asyncConnect")); + smw->run_eventloop(); + + // Set context property for SoundManager + context->setContextProperty("smw", smw); + + + /* + * Load qml + */ + engine.load(QUrl(QStringLiteral("qrc:/QmlForThisApp.qml"))); + + + /* + * Set slot for WindowManager and SoundManager + */ + root = engine.rootObjects().first(); + window = qobject_cast(root); + + // Set slot for calling LibWindowmanager::activateSurface() when loading qml have completed + QObject::connect(window, SIGNAL(frameSwapped()), + wmh, SLOT(slotActivateSurface())); + + // Set slot for SoundManager + QObject::connect(smw, SIGNAL(smEvent(QVariant, QVariant)), + root, SLOT(slotEvent(QVariant, QVariant))); + QObject::connect(smw, SIGNAL(smReply(QVariant)), + root, SLOT(slotReply(QVariant))); + } + + return app.exec(); +} + +static void onRep(struct json_object* reply_contents) +{ + qDebug("%s is called", __FUNCTION__); + QString str = QString(json_object_get_string(reply_contents)); + QJsonParseError error; + QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8(), &error); + QJsonObject jobj = jdoc.object(); + + smw->emit_reply(jobj); + json_object_put(reply_contents); +} + +static void onEv(const std::string& event, struct json_object* event_contents) +{ + qDebug("%s is called", __FUNCTION__); + const QString event_name = QString(event.c_str()); + QString str = QString(json_object_get_string(event_contents)); + QJsonParseError error; + QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8(), &error); + const QJsonObject jobj = jdoc.object(); + smw->emit_event(event_name, jobj); + + json_object_put(event_contents); +} + diff --git a/sample/template/smhandler.cpp b/sample/template/smhandler.cpp new file mode 100644 index 0000000..54b94d8 --- /dev/null +++ b/sample/template/smhandler.cpp @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2017 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "libsmwrapper.h" +using namespace std; + +static int create_json_object(const QJsonObject& obj, struct json_object* jobj); +static bool put_val_to_jobj(const char* key, const QJsonValue& val, struct json_object* jobj); +static bool put_array_to_jobj(const char* key, const QJsonArray& qarray, struct json_object* jobj); + +LibSMWrapper::LibSMWrapper(QObject *parent) : + QObject(parent) +{ + /* This is not enabled */ + //libsm = new LibSoundmanager(); +} + +LibSMWrapper::LibSMWrapper(const int port, const QString& token, QObject *parent) : + QObject(parent) +{ + libsm = new LibSoundmanager(port, token.toStdString()); +} + +LibSMWrapper::~LibSMWrapper() +{ + delete libsm; +} + +int LibSMWrapper::call(const QString &verb, const QString &arg) +{ + // translate QJsonObject to struct json_object + struct json_object* jobj = json_object_new_object(); + QJsonDocument jsonDoc = QJsonDocument::fromJson(arg.toUtf8()); + const QJsonObject jsonObj = jsonDoc.object(); + int ret = create_json_object(jsonObj, jobj); + if(ret < 0) + { + return -1; + } + return libsm->call(verb.toStdString().c_str(), jobj); +} + +static int create_json_object(const QJsonObject& obj, struct json_object* jobj) +{ + try{ + for(auto itr = obj.begin(); itr != obj.end();++itr) + { + string key = itr.key().toStdString(); + //const char* key = itr.key().toStdString().c_str(); // Do not code like this. string is removed if size is over 16!! + + bool ret = put_val_to_jobj(key.c_str(), itr.value(),jobj); + if(!ret){ + /*This is not implemented*/ + qDebug("JsonArray can't parse for now"); + return -1; + // ToDo + // For now, array may not be inputted for soundmanager + // But use case absolutely exists + /*QJsonArray qarray = itr.value().toArray(); + ret = put_array_to_jobj(key, qarray, jobj);*/ + } + } + } + catch(...){ + qDebug("Json parse error occured"); + return -1; + } + return 0; +} + +static bool put_val_to_jobj(const char* key, const QJsonValue& val, struct json_object* jobj) +{ + if(val.isArray()){ + return false; // Array can't input + } + if(val.isString()){ + string value = val.toString().toStdString(); + json_object_object_add(jobj, key, json_object_new_string(value.c_str())); + } + else{ + const int value = val.toInt(); + json_object_object_add(jobj, key, json_object_new_int(value)); + } + return true; +} + +static bool put_array_to_jobj(const char* key, const QJsonArray& qarray, struct json_object* jobj) +{ + // ToDo Fix this !! +/* struct json_object* jarray = json_object_new_array(); + + bool ret; + for(auto jitr = qarray.begin(); jitr != qarray.end(); ++jitr){ + struct json_object* tmp = json_object_new_object(); + ret = put_val_to_jobj(key,jitr,tmp); + if(!ret) + { + put_array_to_jobj(key,jitr,tmp); + } + json_object_array_add(jarray, tmp); + } + json_object_object_add(jobj, key, jarray); + return true;*/ +} + +void LibSMWrapper::wrapper_registerCallback( + void (*event_func)(const string& event, struct json_object* event_contents), + void (*reply_func)(struct json_object* reply_contents)) +{ + libsm->register_callback(event_func, reply_func); +} + +void LibSMWrapper::subscribe(const QString event_name) +{ + std::string str = event_name.toStdString(); + libsm->subscribe(str); +} + +void LibSMWrapper::unsubscribe(const QString event_name) +{ + std::string str = event_name.toStdString(); + libsm->unsubscribe(str); +} + +void LibSMWrapper::run_eventloop() +{ + libsm->run_eventloop(); +} + +void LibSMWrapper::print(const QString &str) +{ + qDebug("%s is called", str.toStdString().c_str()); +} + +void LibSMWrapper::emit_event(const QString &event, const QJsonObject &msg) +{ + qDebug("emit smEvent signal @%s", __FUNCTION__); + emit smEvent(event, msg); +} +void LibSMWrapper::emit_reply(const QJsonObject &msg) +{ + qDebug("emit smReply signal @%s", __FUNCTION__); + emit smReply(msg); +} \ No newline at end of file diff --git a/sample/template/smhandler.h b/sample/template/smhandler.h new file mode 100644 index 0000000..3954a29 --- /dev/null +++ b/sample/template/smhandler.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2017 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SIGNALER_H +#define SIGNALER_H + + #include + #include + #include + #include + #include + #include + #include + + +class LibSMWrapper : public QObject +{ + Q_OBJECT +public: // method + explicit LibSMWrapper(QObject *parent = nullptr); + LibSMWrapper(const int port, const QString& token, QObject *parent = nullptr); + ~LibSMWrapper(); + + void wrapper_registerCallback( + void (*event_func)(const std::string& event, struct json_object* event_contents), + void (*reply_func)(struct json_object* reply_contents) + ); + void subscribe(const QString event_name); + void unsubscribe(const QString event_name); + void run_eventloop(); + + void emit_event(const QString &event, const QJsonObject &msg); + void emit_reply(const QJsonObject &msg); +public slots: + int call(const QString &verb, const QString &arg); + void print(const QString &str); +signals: + void smEvent(const QVariant &event, const QVariant &msg); + void smReply(const QVariant &msg); + +private: + LibSoundmanager* libsm; +}; + + +#endif /*SIGNALER_H*/ \ No newline at end of file diff --git a/sample/template/wmhandler.cpp b/sample/template/wmhandler.cpp new file mode 100644 index 0000000..761915f --- /dev/null +++ b/sample/template/wmhandler.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2017 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "wmhandler.h" +#include + + +void WmHandler::init(LibWindowmanager *p_wm, std::string applabel) +{ + mp_wm = p_wm; + m_applabel = applabel; +} + +void WmHandler::slotActivateSurface() { + mp_wm->activateSurface(m_applabel.c_str()); +} + +WmHandler::WmHandler(QObject *parent) + :QObject(parent) +{ +} + +WmHandler::~WmHandler() { } diff --git a/sample/template/wmhandler.h b/sample/template/wmhandler.h new file mode 100644 index 0000000..5442d8e --- /dev/null +++ b/sample/template/wmhandler.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2017 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WMHANDLER_H +#define WMHANDLER_H +#include +#include +#include +#include +#include +#include +#include + +class WmHandler : public QObject { +Q_OBJECT +public: + explicit WmHandler(QObject *parent = nullptr); + ~WmHandler(); + + WmHandler(const WmHandler &) = delete; + WmHandler &operator=(const WmHandler &) = delete; + +public: + static WmHandler &instance(); + void init(LibWindowmanager *p_wm, std::string applabel); + +public slots: + void slotActivateSurface(); + +private: + LibWindowmanager* mp_wm; + std::string m_applabel; +}; +#endif // WMHANDLER_H -- cgit 1.2.3-korg