change wl_signal_emit_mutable into wl_signal_emit
[platform/core/uifw/libds-tizen.git] / tests / mockcompositor.cpp
1 /**************************************************************************
2  *
3  * Copyright 2020 Samsung Electronics co., Ltd. All Rights Reserved.
4  *
5  * Contact: SooChan Lim <sc1.lim@samsung.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
23  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27 **************************************************************************/
28
29 #include "mockcompositor.h"
30 #include <iostream>
31
32 static void
33 logger_func(void *user_data, enum wl_protocol_logger_type type,
34         const struct wl_protocol_logger_message *message)
35 {
36     //Compositor *c = static_cast<Compositor *>(user_data);
37 #if 0
38     std::cout << "res:" << wl_resource_get_class(message->resource) << " "
39               << "op:" << message->message_opcode << " "
40               << "name:" << message->message->name << "\n";
41 #endif
42 }
43
44 Compositor::Compositor()
45     : display(wl_display_create()),
46       loop(wl_display_get_event_loop(display)),
47       logger(wl_display_add_protocol_logger(display, logger_func, this))
48 {
49     int ret;
50
51     ret = wl_display_add_socket(display, "test-ds-tizen");
52     if (ret != 0)
53         ds_err("wl_display_add_socket() filed.");
54
55     ret = wl_display_init_shm(display);
56     if (ret != 0)
57         ds_err("wl_display_init_shm() filed.");
58
59     compositor = ds_compositor_create(display);
60     if (compositor == nullptr)
61         ds_err("ds_compositor_create() filed.");
62 }
63
64 Compositor::~Compositor ()
65 {
66     wl_list *clients = wl_display_get_client_list(display);
67     wl_client *client = nullptr;
68
69     wl_client_for_each(client, clients) {
70         wl_client_destroy(client);
71     }
72
73     wl_protocol_logger_destroy(logger);
74     wl_display_destroy(display);
75 }
76
77 void Compositor::DispatchEvents()
78 {
79     /* flush any pending client events */
80     wl_display_flush_clients(display);
81     /* dispatch any pending main loop events */
82     wl_event_loop_dispatch(loop, 0);
83 }
84
85 MockCompositor::MockCompositor()
86     : compositor(nullptr)
87 {
88     th = std::thread([this](){
89         Compositor c;
90         compositor = &c;
91
92         // mockcompositor is ready
93         ready = true;
94         cv.notify_one();
95
96         while (alive) {
97             // mockcompositor process the events in every 40 milliseconds
98             std::this_thread::sleep_for(std::chrono::milliseconds(40));
99             Process();
100         }
101     });
102
103     // wait until the child thread(mockcompositor) is ready
104     {
105         std::unique_lock<std::mutex> lock(m);
106         cv.wait(lock, [this]{return ready;});
107     }
108 }
109
110 MockCompositor::MockCompositor(ds_test_setup_func_t setup_func, void *data)
111     : compositor(nullptr)
112 {
113     th = std::thread([this, setup_func, data](){
114         Compositor c;
115         compositor = &c;
116
117         // call test setup_function
118         setup_func(data);
119
120         // mockcompositor is ready
121         ready = true;
122         cv.notify_one();
123
124         while (alive) {
125             // mockcompositor process the events in every 40 milliseconds
126             std::this_thread::sleep_for(std::chrono::milliseconds(40));
127
128             Process();
129         }
130     });
131
132     // wait until the child thread(mockcompositor) is ready
133     {
134         std::unique_lock<std::mutex> lock(m);
135         cv.wait(lock, [this]{return ready;});
136     }
137 }
138
139 MockCompositor::~MockCompositor() {
140     // finish the child thread(mockcompositor).
141     alive = false;
142     th.join();
143 }
144
145 void MockCompositor::Process()
146 {
147     std::lock_guard<std::mutex> lock(m);
148     compositor->DispatchEvents();
149 }
150
151 struct wl_display *MockCompositor::GetWlDisplay()
152 {
153     return this->compositor->display;
154 }