summaryrefslogtreecommitdiffstats
path: root/app/surface.cpp
blob: 8f6c49ffd419eec7359b4bd1a02fc86f6bf2491a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright (c) 2017 Panasonic Corporation
 * Copyright (c) 2018 Konsulko Group
 *
 * 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 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 <cstdio>
#include <cstdarg>
#include "surface.hpp"
#include "hmi-debug.h"

#define AREA_NORMAL_FULL "normal.full"

SurfaceHandler::SurfaceHandler(const int port, const std::string &token, const std::string &role)
{
	m_port = port;
	m_token = token;
	m_role = role;

	m_rid = getpid();

	// Setup HomeScreen/WindowManager API
	if (init_wm()) {
		HMI_ERROR("receiver:sh", "cannot setup windowmanager API");
		exit(1);
	}

	// Setup ilmController API
	m_ic = new ILMControl(notify_ivi_control_cb_static, this);
	if (!m_ic) {
		HMI_ERROR("receiver:sh", "cannot setup IVI layer management API");
		exit(1);
	}
}

int SurfaceHandler::init_wm(void)
{
	m_wm = new LibWindowmanager();
	if (m_wm->init(m_port, m_token.c_str())) {
		HMI_ERROR("receiver:sh", "cannot initialize windowmanager");
		return -1;
	}

	std::function< void(json_object*) > h_active =
		[](json_object* object) {
			HMI_DEBUG("receiver:sh", "Got Event_Active");
		};

	std::function< void(json_object*) > h_inactive =
		[](json_object* object) {
			HMI_DEBUG("receiver:sh", "Got Event_Inactive");
		};

	std::function< void(json_object*) > h_visible =
		[](json_object* object) {
			HMI_DEBUG("receiver:sh", "Got Event_Visible");
		};

	std::function< void(json_object*) > h_invisible =
		[](json_object* object) {
			HMI_DEBUG("receiver:sh", "Got Event_Invisible");
		};

	std::function< void(json_object*) > h_syncdraw =
		[this](json_object* object) {
			HMI_DEBUG("receiver:sh", "Got Event_SyncDraw");
			this->m_wm->endDraw(this->m_role.c_str());
		};

	std::function< void(json_object*) > h_flushdraw =
		[](json_object* object) {
			HMI_DEBUG("receiver:sh", "Got Event_FlushDraw");
		};

	m_wm->set_event_handler(LibWindowmanager::Event_Active, h_active);
	m_wm->set_event_handler(LibWindowmanager::Event_Inactive, h_inactive);
	m_wm->set_event_handler(LibWindowmanager::Event_Visible, h_visible);
	m_wm->set_event_handler(LibWindowmanager::Event_Invisible, h_invisible);
	m_wm->set_event_handler(LibWindowmanager::Event_SyncDraw, h_syncdraw);
	m_wm->set_event_handler(LibWindowmanager::Event_FlushDraw, h_flushdraw);

	return 0;
}

void SurfaceHandler::notify_ivi_control_cb(ilmObjectType object,
					   t_ilm_uint id,
					   t_ilm_bool created)
{
	if (object == ILM_SURFACE) {
		struct ilmSurfaceProperties surf_props;

		ilm_getPropertiesOfSurface(id, &surf_props);
		pid_t surf_pid = surf_props.creatorPid;

		if (!created) {
			HMI_DEBUG("receiver:sh", "ivi surface (id=%d, pid=%d) destroyed.", id, surf_pid);
			unregister_surfpid(surf_pid);
			m_surfaces.erase(surf_pid);
			return;
		}

		HMI_DEBUG("receiver:sh", "ivi surface (id=%d, pid=%d) is created.", id, surf_pid);

		register_surfpid(surf_pid);
		if (m_rid && surf_pid == find_surfpid_by_rid(m_rid)) {
			setup_surface(id);
		}
		m_surfaces[surf_pid] = id;
	} else if (object == ILM_LAYER) {
		if (created)
			HMI_DEBUG("receiver:sh", "ivi layer: %d created.", id);
		else
			HMI_DEBUG("receiver:sh", "ivi layer: %d destroyed.", id);
	}

}

void SurfaceHandler::notify_ivi_control_cb_static (ilmObjectType object,
						   t_ilm_uint id,
						   t_ilm_bool created,
						   void *user_data)
{
	SurfaceHandler *handler = static_cast<SurfaceHandler*>(user_data);
	handler->notify_ivi_control_cb(object, id, created);
}

void SurfaceHandler::register_surfpid (pid_t surf_pid)
{
	if (surf_pid == m_rid) {
		if (!std::count(m_pid_v.begin(), m_pid_v.end(), surf_pid)) {
			HMI_DEBUG("receiver:sh", "surface creator(pid=%d) registered", surf_pid);
			m_pid_v.push_back(surf_pid);
			HMI_DEBUG("receiver:sh", "m_pid_v.count(%d) = %d", surf_pid,
				  std::count(m_pid_v.begin(), m_pid_v.end(), surf_pid));
		}
	}
}

void SurfaceHandler::unregister_surfpid (pid_t surf_pid)
{
	auto itr = m_pid_v.begin();
	while (itr != m_pid_v.end()) {
		if (*itr == surf_pid) {
			m_pid_v.erase(itr++);
		} else {
			++itr;
		}
	}
}

pid_t SurfaceHandler::find_surfpid_by_rid (pid_t rid)
{
	HMI_DEBUG("receiver:sh", "find surfpid by rid(%d)", rid);
	if (std::count(m_pid_v.begin(), m_pid_v.end(), rid)) {
		HMI_DEBUG("receiver:sh", "found return(%d)", rid);
		return rid;
	}

	return -1;
}

void SurfaceHandler::setup_surface (int id)
{
	std::string sid = std::to_string(id);

	// This surface is mine, register pair app_name and ivi id.
	HMI_DEBUG("receiver:sh", "requestSurfaceXDG(%s,%d)", m_role.c_str(), id);
	m_wm->requestSurfaceXDG(m_role.c_str(), id);

	if (m_pending_create) {
		// Activate window if it has not been yet
		HMI_DEBUG("receiver:sh", "calling activateWindow on (%s,%d)", m_role.c_str(), id);
		m_pending_create = false;
		m_wm->activateWindow(m_role.c_str(), AREA_NORMAL_FULL);
	}
}