examples: make tinyds-dpms files
[platform/core/uifw/libds-tizen.git] / examples / tinyds-tdm.c
index 8ffaec5..cb8f2e5 100644 (file)
@@ -1,50 +1,4 @@
-#include <assert.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <signal.h>
-#include <time.h>
-
-#include <drm_fourcc.h>
-#include <pixman.h>
-#include <wayland-server.h>
-#include <libds/log.h>
-#include <libds/backend.h>
-#include <libds/output.h>
-#include <libds/compositor.h>
-#include <libds/xdg_shell.h>
-#include <libds-tizen/allocator/tbm.h>
-#include <libds-tizen/backend/tdm.h>
-#include <libds/backend/libinput.h>
-#include <libds-tizen/tbm_server.h>
-#include <libds-tizen/dpms.h>
-#include <libds/input_device.h>
-#include <libds/keyboard.h>
-#include <libds/touch.h>
-#include <libds/pointer.h>
-#include <libds/seat.h>
-#include <libds-tizen/input_devicemgr.h>
-#include <xkbcommon/xkbcommon.h>
-#include <libds/interfaces/keyboard.h>
-#include <libds-tizen/launch.h>
-#include <libds-tizen/backend/tdm_output_hwc.h>
-#include <libds-tizen/input_method.h>
-#include <libds-tizen/text_input.h>
-
-#define USE_TDM_BUFFER_QUEUE
-
-#ifdef USE_TDM_BUFFER_QUEUE
-#include "pixman-tbm-helper.h"
-#include "tinyds-tdm-renderer.h"
-#else
-#include <libds/swapchain.h>
-#endif
-
-#include "pixman-helper.h"
-
-#define TINYDS_UNUSED   __attribute__((unused))
-struct tinyds_keyboard;
-struct tinyds_pointer;
+#include "tinyds-tdm.h"
 
 struct tinyds_output
 {
@@ -72,57 +26,11 @@ struct tinyds_output
 
     struct ds_tdm_output_hwc *hwc;
     struct ds_tdm_output_hwc_window *bg_hwc_window;
-};
-
-struct tinyds_dpms
-{
-    struct ds_tizen_dpms *ds_dpms;
-    struct tinyds_server *server;
-
-    struct wl_listener destroy;
-    struct wl_listener set_dpms;
-    struct wl_listener get_dpms;
-};
-
-struct tinyds_server
-{
-    struct ds_tbm_server *tbm_server;
-
-    struct wl_display *display;
-
-    struct ds_backend *backend;
-    struct ds_backend *input_backend;
-    struct ds_compositor *compositor;
-    struct ds_xdg_shell *xdg_shell;
-    struct ds_seat *seat;
-    uint32_t seat_caps;
-    double output_x, output_y;
-    struct ds_tizen_input_devicemgr *devicemgr;
-    struct ds_tizen_launch_effect *effect;
-    struct ds_tizen_launch_splash *splash;
 
-    struct tinyds_output *output;
-    struct tinyds_dpms *dpms;
-    struct wl_event_source *stdin_source;
-
-    struct wl_list views;
-
-    struct wl_listener new_output;
-    struct wl_listener new_input;
-    struct wl_listener new_xdg_surface;
-    struct wl_listener devicemgr_destroy;
-    struct wl_listener pointer_warp;
-    struct wl_listener effect_destroy;
-    struct wl_listener effect_type_set;
-    struct wl_listener effect_type_unset;
-    struct wl_listener new_splash;
-    struct wl_listener splash_owner;
-
-    struct wl_list keyboards;
-    struct wl_list pointers;
-
-    struct tinyds_text_input *text_input;
-    struct tinyds_input_method *input_method;
+#ifdef USE_CURSOR
+    bool cursor_enabled;
+    struct ds_tdm_output_hwc_window *cursor_hwc_window;
+#endif
 };
 
 struct tinyds_view
@@ -191,11 +99,10 @@ struct tinyds_text_input {
 
     struct wl_list input_methods;
 
-    struct wl_listener destroy;
     struct wl_listener mgr_destroy;
-
     struct wl_listener new_text_input;
 
+    struct wl_listener destroy;
     struct wl_listener text_input_activate;
     struct wl_listener text_input_deactivate;
     struct wl_listener text_input_reset;
@@ -267,16 +174,12 @@ static void output_swapchain_init(struct tinyds_output *output,
 static void output_draw_with_swapchain(struct tinyds_output *output);
 static void draw_view(struct tinyds_view *view, pixman_image_t *dst_image);
 #endif
-static void dpms_handle_destroy(struct wl_listener *listener, void *data);
-static void dpms_handle_set_dpms(struct wl_listener *listener, void *data);
-static void dpms_handle_get_dpms(struct wl_listener *listener, void *data);
 static void server_add_keyboard(struct tinyds_server *server,
         struct ds_input_device *dev);
 static void server_add_pointer(struct tinyds_server *server,
         struct ds_input_device *dev);
 static void server_add_touch(struct tinyds_server *server,
         struct ds_input_device *dev);
-
 static struct tinyds_view *
 server_view_at(struct tinyds_server *server, double lx, double ly,
         double *sx, double *sy);
@@ -331,6 +234,9 @@ main(void)
 
     wl_display_run(display);
 
+    protocol_trace_enable(false);
+    protocol_trace_fini();
+
     wl_display_destroy_clients(display);
     wl_display_destroy(display);
 
@@ -430,6 +336,8 @@ view_handle_surface_commit(struct wl_listener *listener,
 static void
 server_new_xdg_surface(struct wl_listener *listener, void *data)
 {
+    static unsigned int seedx = 1;
+    static unsigned int seedy = 43210;
     struct tinyds_server *server;
     struct tinyds_view *view;
     struct ds_xdg_surface *xdg_surface;
@@ -462,8 +370,8 @@ server_new_xdg_surface(struct wl_listener *listener, void *data)
             ds_xdg_surface_get_surface(xdg_surface),
             &view->surface_commit);
 
-    view->x = rand() % 1000;
-    view->y = rand() % 500;
+    view->x = rand_r(&seedx) % 1000;
+    view->y = rand_r(&seedy) % 500;
 
     view->hwc_window = ds_tdm_output_hwc_window_create(server->output->hwc);
     assert(view->hwc_window);
@@ -530,6 +438,10 @@ backend_handle_new_output(struct wl_listener *listener, void *data)
     ds_tdm_output_hwc_window_set_dest_size(output->bg_hwc_window, output->width, output->height);
     ds_tdm_output_hwc_window_set_transform(output->bg_hwc_window, WL_OUTPUT_TRANSFORM_NORMAL);
 
+#ifdef USE_CURSOR
+    output->cursor_enabled = false;
+#endif
+
     output->output_destroy.notify = output_handle_destroy;
     ds_output_add_destroy_listener(ds_output, &output->output_destroy);
 
@@ -539,42 +451,12 @@ backend_handle_new_output(struct wl_listener *listener, void *data)
     ds_tizen_input_devicemgr_set_output_width_height(server->devicemgr, (uint32_t)output->width, (uint32_t)output->height);
 
     server->output = output;
+    server->output_x = (double)(output->width) / 2;
+    server->output_y = (double)(output->height) / 2;
 
     output_schedule_commit(output);
 }
 
-static bool
-add_new_dpms(struct tinyds_server *server)
-{
-    struct tinyds_dpms *dpms;
-
-    dpms = calloc(1, sizeof *dpms);
-    if (!dpms)
-        return false;
-
-    dpms->ds_dpms = ds_tizen_dpms_create(server->display);
-    if (!dpms->ds_dpms) {
-        free(dpms);
-        ds_err("Could not create ds_tizen_dpms");
-        return false;
-    }
-
-    dpms->destroy.notify = dpms_handle_destroy;
-    ds_tizen_dpms_add_destroy_listener(dpms->ds_dpms, &dpms->destroy);
-
-    dpms->set_dpms.notify = dpms_handle_set_dpms;
-    ds_tizen_dpms_add_set_dpms_listener(dpms->ds_dpms, &dpms->set_dpms);
-
-    dpms->get_dpms.notify = dpms_handle_get_dpms;
-    ds_tizen_dpms_add_get_dpms_listener(dpms->ds_dpms, &dpms->get_dpms);
-
-    server->dpms = dpms;
-
-    ds_inf("Dpms (%p) added", dpms);
-
-    return true;
-}
-
 static void
 backend_handle_new_input(struct wl_listener *listener, void *data)
 {
@@ -812,6 +694,22 @@ launch_effect_handle_new_splash(struct wl_listener *listener, void *data)
             &server->splash_owner);
 }
 
+static void
+dpms_free_func(void *data)
+{
+    struct tinyds_server *server = (struct tinyds_server *)data;
+
+    server->dpms = NULL;
+}
+
+static void
+policy_free_func(void *data)
+{
+    struct tinyds_server *server = (struct tinyds_server *)data;
+
+    server->policy = NULL;
+}
+
 static bool
 init_server(struct tinyds_server *server, struct wl_display *display)
 {
@@ -857,7 +755,12 @@ init_server(struct tinyds_server *server, struct wl_display *display)
     ds_xdg_shell_add_new_surface_listener(server->xdg_shell,
             &server->new_xdg_surface);
 
-    if (!add_new_dpms(server))
+    server->dpms = tinyds_dpms_init(server->display, dpms_free_func, (void *)server);
+    if (!server->dpms)
+        goto err;
+
+    server->policy = tinyds_policy_init(server->display, policy_free_func, (void *)server);
+    if (!server->policy)
         goto err;
 
     server->seat = ds_seat_create(display, "seat0" /* arbitrary name */);
@@ -909,6 +812,9 @@ init_server(struct tinyds_server *server, struct wl_display *display)
     if (!add_new_input_method(server))
         goto err;
 
+    if (protocol_trace_init(display))
+        protocol_trace_enable(true);
+
     return true;
 
 err:
@@ -1002,6 +908,25 @@ output_commit(struct tinyds_output *output)
         need_target = true;
     }
 
+#ifdef USE_CURSOR
+    if (output->cursor_hwc_window) {
+        src_box.x = 0;
+        src_box.y = 0;
+        src_box.width = CURSOR_W;
+        src_box.height = CURSOR_H;
+
+        ds_tdm_output_hwc_window_set_src_box(output->cursor_hwc_window, &src_box);
+        ds_tdm_output_hwc_window_set_position(output->cursor_hwc_window, output->server->output_x, output->server->output_y);
+        ds_tdm_output_hwc_window_set_dest_size(output->cursor_hwc_window, CURSOR_W, CURSOR_H);
+        ds_tdm_output_hwc_window_set_transform(output->cursor_hwc_window, WL_OUTPUT_TRANSFORM_NORMAL);
+
+        ds_tdm_output_hwc_window_set_composition(output->cursor_hwc_window,
+                    DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_CLIENT);
+        num_windows++;
+        need_target = true;
+    }
+#endif
+
     if (num_windows) {
         composited_hwc_windows = calloc(num_windows, sizeof *composited_hwc_windows);
         if (!composited_hwc_windows)
@@ -1031,6 +956,10 @@ output_commit(struct tinyds_output *output)
             ds_tdm_output_hwc_window_set_transform(view->hwc_window, WL_OUTPUT_TRANSFORM_NORMAL);
 
             if (view->mapped) {
+#ifdef USE_CURSOR
+                ds_tdm_output_hwc_window_set_composition(view->hwc_window,
+                        DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_CLIENT);
+#endif
                 ds_tdm_output_hwc_window_set_composition(view->hwc_window,
                         DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_DEVICE);
 
@@ -1046,6 +975,13 @@ output_commit(struct tinyds_output *output)
             composited_hwc_windows[current_num_windows] = output->bg_hwc_window;
             current_num_windows++;
         }
+
+#ifdef USE_CURSOR
+        if (output->cursor_hwc_window) {
+            composited_hwc_windows[current_num_windows] = output->cursor_hwc_window;
+            current_num_windows++;
+        }
+#endif
     }
 
     if (!ds_tdm_output_hwc_validate(output->hwc, composited_hwc_windows,
@@ -1372,53 +1308,6 @@ server_dispatch_stdin(int fd, uint32_t mask, void *data)
 }
 
 static void
-dpms_handle_destroy(struct wl_listener *listener, void *data)
-{
-    struct tinyds_dpms *dpms;
-
-    dpms = wl_container_of(listener, dpms, destroy);
-
-    ds_inf("Dpms(%p) destroyed", dpms);
-
-    wl_list_remove(&dpms->destroy.link);
-    wl_list_remove(&dpms->set_dpms.link);
-    wl_list_remove(&dpms->get_dpms.link);
-
-    free(dpms);
-}
-
-static void
-dpms_handle_set_dpms(struct wl_listener *listener, void *data)
-{
-    struct tinyds_dpms *dpms;
-    struct ds_tizen_dpms_event *event = data;
-
-    dpms = wl_container_of(listener, dpms, set_dpms);
-
-    ds_inf("Dpms(%p) set dpms : %d", dpms, event->mode);
-
-    //To do
-    //set dpms mode to output
-    ds_tizen_dpms_send_set_result(dpms->ds_dpms, event->mode,
-        DS_TIZEN_DPMS_ERROR_NONE);
-}
-
-static void
-dpms_handle_get_dpms(struct wl_listener *listener, void *data)
-{
-    struct tinyds_dpms *dpms;
-
-    dpms = wl_container_of(listener, dpms, get_dpms);
-
-    ds_inf("Dpms(%p) get dpms", dpms);
-
-    //To do
-    //get dpms mode from output
-    ds_tizen_dpms_send_get_result(dpms->ds_dpms, DS_TIZEN_DPMS_MODE_ON,
-        DS_TIZEN_DPMS_ERROR_NONE);
-}
-
-static void
 keyboard_handle_device_destroy(struct wl_listener *listener, void *data)
 {
     struct tinyds_keyboard *kbd;
@@ -1601,6 +1490,13 @@ touch_handle_down(struct wl_listener *listener, void *data)
         ds_seat_touch_notify_down(touch->server->seat, ds_xdg_surface_get_surface(view->xdg_surface),
                 event->time_msec, event->id, sx, sy);
     }
+
+#ifdef USE_CURSOR
+    if (server->output && server->output->cursor_enabled) {
+        renderer_cursor_update(&server->output->renderer, server->output_x, server->output_y);
+        draw_server_with_damage(server);
+    }
+#endif
 }
 
 static void
@@ -1641,6 +1537,13 @@ touch_handle_motion(struct wl_listener *listener, void *data)
         ds_seat_touch_notify_motion(server->seat, event->time_msec,
                 event->id, sx, sy);
     }
+
+#ifdef USE_CURSOR
+    if (server->output && server->output->cursor_enabled) {
+        renderer_cursor_update(&server->output->renderer, server->output_x, server->output_y);
+        draw_server_with_damage(server);
+    }
+#endif
 }
 
 static void
@@ -1673,6 +1576,7 @@ static void
 pointer_handle_device_destroy(struct wl_listener *listener, void *data)
 {
     struct tinyds_pointer *pointer;
+    struct tinyds_server *server;
 
     pointer = wl_container_of(listener, pointer, destroy);
 
@@ -1684,6 +1588,22 @@ pointer_handle_device_destroy(struct wl_listener *listener, void *data)
     wl_list_remove(&pointer->frame.link);
     wl_list_remove(&pointer->link);
 
+#ifdef USE_CURSOR
+    server = pointer->server;
+    if (server->output && wl_list_empty(&server->pointers))
+    {
+        server->output->cursor_enabled = false;
+        renderer_cursor_destroy(&server->output->renderer);
+
+        if (server->output->cursor_hwc_window)
+        {
+            ds_tdm_output_hwc_window_destroy(server->output->cursor_hwc_window);
+            server->output->cursor_hwc_window = NULL;
+        }
+        draw_server_with_damage(server);
+    }
+#endif
+
     free(pointer);
 }
 
@@ -1742,6 +1662,13 @@ pointer_handle_motion(struct wl_listener *listener, void *data)
         ds_seat_pointer_notify_motion(pointer->server->seat,
                 event->time_msec, sx, sy);
     }
+
+#ifdef USE_CURSOR
+    if (server->output && server->output->cursor_enabled) {
+        renderer_cursor_update(&server->output->renderer, server->output_x, server->output_y);
+        draw_server_with_damage(server);
+    }
+#endif
 }
 
 static void
@@ -1781,8 +1708,6 @@ server_add_pointer(struct tinyds_server *server, struct ds_input_device *dev)
 
     pointer->dev = dev;
     pointer->server = server;
-    server->output_x = 200;
-    server->output_y = 200;
 
     pointer->destroy.notify = pointer_handle_device_destroy;
     ds_input_device_add_destroy_listener(dev, &pointer->destroy);
@@ -1801,6 +1726,21 @@ server_add_pointer(struct tinyds_server *server, struct ds_input_device *dev)
 
     pointer->focused_view = NULL;
 
+#ifdef USE_CURSOR
+    if (server->output && wl_list_empty(&server->pointers)) {
+        server->output_x = (double)(server->output->width) / 2;
+        server->output_y = (double)(server->output->height) / 2;
+
+        server->output->cursor_enabled = true;
+        renderer_cursor_create(&server->output->renderer, 255, 0, 0, CURSOR_W, CURSOR_H);
+        renderer_cursor_update(&server->output->renderer, server->output_x, server->output_y);
+
+        server->output->cursor_hwc_window = ds_tdm_output_hwc_window_create(server->output->hwc);
+        assert(server->output->cursor_hwc_window);
+        draw_server_with_damage(server);
+    }
+#endif
+
     wl_list_insert(&server->pointers, &pointer->link);
 
     ds_inf("Pointer(%p) added", pointer);
@@ -1836,22 +1776,10 @@ text_input_mgr_handle_destroy(struct wl_listener *listener, void *data)
     text_input = wl_container_of(listener, text_input, mgr_destroy);
 
     wl_list_remove(&text_input->mgr_destroy.link);
-    wl_list_remove(&text_input->destroy.link);
-
     wl_list_remove(&text_input->new_text_input.link);
 
-    wl_list_remove(&text_input->text_input_activate.link);
-    wl_list_remove(&text_input->text_input_deactivate.link);
-    wl_list_remove(&text_input->text_input_reset.link);
-    wl_list_remove(&text_input->text_input_set_content_type.link);
-    wl_list_remove(&text_input->text_input_invoke_action.link);
-    wl_list_remove(&text_input->text_input_commit_state.link);
-    wl_list_remove(&text_input->text_input_set_preferred_language.link);
-
     server = text_input->server;
     server->text_input = NULL;
-
-    free(text_input);
 }
 
 static void
@@ -1864,6 +1792,15 @@ text_input_handle_destroy(struct wl_listener *listener, void *data)
     text_input = wl_container_of(listener, text_input, destroy);
 
     wl_list_remove(&text_input->destroy.link);
+    wl_list_remove(&text_input->text_input_activate.link);
+    wl_list_remove(&text_input->text_input_deactivate.link);
+    wl_list_remove(&text_input->text_input_reset.link);
+    wl_list_remove(&text_input->text_input_set_content_type.link);
+    wl_list_remove(&text_input->text_input_invoke_action.link);
+    wl_list_remove(&text_input->text_input_commit_state.link);
+    wl_list_remove(&text_input->text_input_set_preferred_language.link);
+
+    free(text_input);
 }
 
 static void
@@ -2340,9 +2277,9 @@ add_new_text_input(struct tinyds_server *server)
 
     wl_list_init(&text_input->input_methods);
 
-    text_input->destroy.notify = text_input_mgr_handle_destroy;
+    text_input->mgr_destroy.notify = text_input_mgr_handle_destroy;
     ds_tizen_text_input_manager_add_destroy_listener(text_input->text_input_mgr,
-            &text_input->destroy);
+            &text_input->mgr_destroy);
 
     text_input->new_text_input.notify = text_input_mgr_handle_new_text_input;
     ds_tizen_text_input_manager_add_new_text_input_listener(text_input->text_input_mgr,
@@ -2412,6 +2349,10 @@ add_new_input_method_context(struct tinyds_input_method *input_method,
     context->input = text_input;
 
     context->context = ds_tizen_input_method_create_context(input_method->input_method);
+    if (context->context == NULL) {
+        ds_err("ds_tizen_input_method_create_context() failed.");
+        return false;
+    }
 
     context->destroy.notify = context_handle_destroy;
     ds_tizen_input_method_context_add_destroy_listener(context->context,