1e5c31af7Sopenharmony_ci/*-------------------------------------------------------------------------
2e5c31af7Sopenharmony_ci * drawElements Quality Program Tester Core
3e5c31af7Sopenharmony_ci * ----------------------------------------
4e5c31af7Sopenharmony_ci *
5e5c31af7Sopenharmony_ci * Copyright (c) 2016 The Khronos Group Inc.
6e5c31af7Sopenharmony_ci *
7e5c31af7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
8e5c31af7Sopenharmony_ci * you may not use this file except in compliance with the License.
9e5c31af7Sopenharmony_ci * You may obtain a copy of the License at
10e5c31af7Sopenharmony_ci *
11e5c31af7Sopenharmony_ci *      http://www.apache.org/licenses/LICENSE-2.0
12e5c31af7Sopenharmony_ci *
13e5c31af7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
14e5c31af7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
15e5c31af7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16e5c31af7Sopenharmony_ci * See the License for the specific language governing permissions and
17e5c31af7Sopenharmony_ci * limitations under the License.
18e5c31af7Sopenharmony_ci *
19e5c31af7Sopenharmony_ci *//*!
20e5c31af7Sopenharmony_ci * \file
21e5c31af7Sopenharmony_ci * \brief X11 using XCB utilities.
22e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
23e5c31af7Sopenharmony_ci
24e5c31af7Sopenharmony_ci#include "tcuLnxX11Xcb.hpp"
25e5c31af7Sopenharmony_ci#include "deMemory.h"
26e5c31af7Sopenharmony_ci
27e5c31af7Sopenharmony_cinamespace tcu
28e5c31af7Sopenharmony_ci{
29e5c31af7Sopenharmony_cinamespace lnx
30e5c31af7Sopenharmony_ci{
31e5c31af7Sopenharmony_cinamespace x11
32e5c31af7Sopenharmony_ci{
33e5c31af7Sopenharmony_ci
34e5c31af7Sopenharmony_ciXcbDisplay::DisplayState XcbDisplay::s_displayState = XcbDisplay::DISPLAY_STATE_UNKNOWN;
35e5c31af7Sopenharmony_ci
36e5c31af7Sopenharmony_cibool XcbDisplay::hasDisplay (const char* name)
37e5c31af7Sopenharmony_ci{
38e5c31af7Sopenharmony_ci	if (s_displayState == DISPLAY_STATE_UNKNOWN)
39e5c31af7Sopenharmony_ci	{
40e5c31af7Sopenharmony_ci		xcb_connection_t *connection = xcb_connect(name, NULL);
41e5c31af7Sopenharmony_ci		if (connection && !xcb_connection_has_error(connection) )
42e5c31af7Sopenharmony_ci		{
43e5c31af7Sopenharmony_ci			s_displayState = DISPLAY_STATE_AVAILABLE;
44e5c31af7Sopenharmony_ci			xcb_disconnect(connection);
45e5c31af7Sopenharmony_ci		}
46e5c31af7Sopenharmony_ci		else
47e5c31af7Sopenharmony_ci		{
48e5c31af7Sopenharmony_ci			s_displayState = DISPLAY_STATE_UNAVAILABLE;
49e5c31af7Sopenharmony_ci		}
50e5c31af7Sopenharmony_ci	}
51e5c31af7Sopenharmony_ci	return s_displayState == DISPLAY_STATE_AVAILABLE ? true : false;
52e5c31af7Sopenharmony_ci}
53e5c31af7Sopenharmony_ci
54e5c31af7Sopenharmony_ciXcbDisplay::XcbDisplay (EventState& platform, const char* name)
55e5c31af7Sopenharmony_ci	: DisplayBase	(platform)
56e5c31af7Sopenharmony_ci{
57e5c31af7Sopenharmony_ci	m_connection						= xcb_connect(name, NULL);
58e5c31af7Sopenharmony_ci	const xcb_setup_t		*setup		= xcb_get_setup(m_connection);
59e5c31af7Sopenharmony_ci	xcb_screen_iterator_t	iterator	= xcb_setup_roots_iterator(setup);
60e5c31af7Sopenharmony_ci	m_screen							= iterator.data;
61e5c31af7Sopenharmony_ci}
62e5c31af7Sopenharmony_ci
63e5c31af7Sopenharmony_ciXcbDisplay::~XcbDisplay (void)
64e5c31af7Sopenharmony_ci{
65e5c31af7Sopenharmony_ci	xcb_disconnect (m_connection);
66e5c31af7Sopenharmony_ci}
67e5c31af7Sopenharmony_ci
68e5c31af7Sopenharmony_civoid XcbDisplay::processEvents (void)
69e5c31af7Sopenharmony_ci{
70e5c31af7Sopenharmony_ci	xcb_generic_event_t *ev;
71e5c31af7Sopenharmony_ci	while ((ev = xcb_poll_for_event(m_connection)))
72e5c31af7Sopenharmony_ci	{
73e5c31af7Sopenharmony_ci		deFree(ev);
74e5c31af7Sopenharmony_ci		/* Manage your event */
75e5c31af7Sopenharmony_ci	}
76e5c31af7Sopenharmony_ci}
77e5c31af7Sopenharmony_ci
78e5c31af7Sopenharmony_ciXcbWindow::XcbWindow (XcbDisplay& display, int width, int height, xcb_visualid_t* visual)
79e5c31af7Sopenharmony_ci	: WindowBase	()
80e5c31af7Sopenharmony_ci	, m_display		(display)
81e5c31af7Sopenharmony_ci{
82e5c31af7Sopenharmony_ci	xcb_connection_t*	connection = m_display.getConnection();
83e5c31af7Sopenharmony_ci	uint32_t			values[2];
84e5c31af7Sopenharmony_ci	m_window	= xcb_generate_id(connection);
85e5c31af7Sopenharmony_ci	m_colormap	= xcb_generate_id(connection);
86e5c31af7Sopenharmony_ci
87e5c31af7Sopenharmony_ci	if (visual == DE_NULL)
88e5c31af7Sopenharmony_ci		visual = &m_display.getScreen()->root_visual;
89e5c31af7Sopenharmony_ci
90e5c31af7Sopenharmony_ci	values[0] = m_display.getScreen()->white_pixel;
91e5c31af7Sopenharmony_ci	values[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_PROPERTY_CHANGE;
92e5c31af7Sopenharmony_ci
93e5c31af7Sopenharmony_ci	xcb_create_window	(
94e5c31af7Sopenharmony_ci							connection,								// Connection
95e5c31af7Sopenharmony_ci							XCB_COPY_FROM_PARENT,					// depth (same as root)
96e5c31af7Sopenharmony_ci							m_window,								// window Id
97e5c31af7Sopenharmony_ci							display.getScreen()->root,				// parent window
98e5c31af7Sopenharmony_ci							0, 0,									// x, y
99e5c31af7Sopenharmony_ci							static_cast<uint16_t >(width),			// width
100e5c31af7Sopenharmony_ci							static_cast<uint16_t >(height),			// height
101e5c31af7Sopenharmony_ci							10,										// border_width
102e5c31af7Sopenharmony_ci							XCB_WINDOW_CLASS_INPUT_OUTPUT,			// class
103e5c31af7Sopenharmony_ci							*visual,								// visual
104e5c31af7Sopenharmony_ci							XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,	// masks
105e5c31af7Sopenharmony_ci							values									//not used yet
106e5c31af7Sopenharmony_ci						);
107e5c31af7Sopenharmony_ci
108e5c31af7Sopenharmony_ci	xcb_create_colormap	(
109e5c31af7Sopenharmony_ci							connection,
110e5c31af7Sopenharmony_ci							XCB_COLORMAP_ALLOC_NONE,
111e5c31af7Sopenharmony_ci							m_colormap,
112e5c31af7Sopenharmony_ci							m_window,
113e5c31af7Sopenharmony_ci							*visual
114e5c31af7Sopenharmony_ci						);
115e5c31af7Sopenharmony_ci
116e5c31af7Sopenharmony_ci	xcb_alloc_color_reply_t* rep = xcb_alloc_color_reply(connection, xcb_alloc_color(connection, m_colormap, 65535, 0, 0), NULL);
117e5c31af7Sopenharmony_ci	deFree(rep);
118e5c31af7Sopenharmony_ci	xcb_flush (connection);
119e5c31af7Sopenharmony_ci}
120e5c31af7Sopenharmony_ci
121e5c31af7Sopenharmony_ciXcbWindow::~XcbWindow (void)
122e5c31af7Sopenharmony_ci{
123e5c31af7Sopenharmony_ci	xcb_flush (m_display.getConnection());
124e5c31af7Sopenharmony_ci	xcb_free_colormap(m_display.getConnection(), m_colormap);
125e5c31af7Sopenharmony_ci	xcb_destroy_window(m_display.getConnection(), m_window);
126e5c31af7Sopenharmony_ci}
127e5c31af7Sopenharmony_ci
128e5c31af7Sopenharmony_civoid XcbWindow::setVisibility (bool visible)
129e5c31af7Sopenharmony_ci{
130e5c31af7Sopenharmony_ci	if (visible == m_visible)
131e5c31af7Sopenharmony_ci		return;
132e5c31af7Sopenharmony_ci
133e5c31af7Sopenharmony_ci	if (visible)
134e5c31af7Sopenharmony_ci		 xcb_map_window(m_display.getConnection(), m_window);
135e5c31af7Sopenharmony_ci	else
136e5c31af7Sopenharmony_ci		xcb_unmap_window(m_display.getConnection(), m_window);
137e5c31af7Sopenharmony_ci
138e5c31af7Sopenharmony_ci	m_visible = visible;
139e5c31af7Sopenharmony_ci	xcb_flush (m_display.getConnection());
140e5c31af7Sopenharmony_ci
141e5c31af7Sopenharmony_ci}
142e5c31af7Sopenharmony_ci
143e5c31af7Sopenharmony_civoid XcbWindow::processEvents (void)
144e5c31af7Sopenharmony_ci{
145e5c31af7Sopenharmony_ci	// A bit of a hack, since we don't really handle all the events.
146e5c31af7Sopenharmony_ci	m_display.processEvents();
147e5c31af7Sopenharmony_ci}
148e5c31af7Sopenharmony_ci
149e5c31af7Sopenharmony_civoid XcbWindow::getDimensions (int* width, int* height) const
150e5c31af7Sopenharmony_ci{
151e5c31af7Sopenharmony_ci	xcb_get_geometry_reply_t *geom;
152e5c31af7Sopenharmony_ci	geom = xcb_get_geometry_reply(m_display.getConnection(), xcb_get_geometry(m_display.getConnection(), m_window), NULL);
153e5c31af7Sopenharmony_ci	*height = static_cast<int>(geom->height);
154e5c31af7Sopenharmony_ci	*width = static_cast<int>(geom->width);
155e5c31af7Sopenharmony_ci	deFree(geom);
156e5c31af7Sopenharmony_ci}
157e5c31af7Sopenharmony_ci
158e5c31af7Sopenharmony_civoid XcbWindow::setDimensions (int width, int height)
159e5c31af7Sopenharmony_ci{
160e5c31af7Sopenharmony_ci	const uint32_t		values[]	= {static_cast<uint32_t >(width), static_cast<uint32_t >(height)};
161e5c31af7Sopenharmony_ci	xcb_void_cookie_t	result;
162e5c31af7Sopenharmony_ci	xcb_connection_t*	display		= m_display.getConnection();
163e5c31af7Sopenharmony_ci	result = xcb_configure_window(display, m_window, XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, values);
164e5c31af7Sopenharmony_ci	DE_ASSERT(DE_NULL == xcb_request_check(display,result));
165e5c31af7Sopenharmony_ci	xcb_flush (display);
166e5c31af7Sopenharmony_ci
167e5c31af7Sopenharmony_ci	for(;;)
168e5c31af7Sopenharmony_ci	{
169e5c31af7Sopenharmony_ci		xcb_generic_event_t*	event = xcb_poll_for_event(display);
170e5c31af7Sopenharmony_ci		int						w, h;
171e5c31af7Sopenharmony_ci		if(event != DE_NULL)
172e5c31af7Sopenharmony_ci		{
173e5c31af7Sopenharmony_ci			if (XCB_PROPERTY_NOTIFY == (event->response_type & ~0x80))
174e5c31af7Sopenharmony_ci			{
175e5c31af7Sopenharmony_ci				const xcb_property_notify_event_t* pnEvent = (xcb_property_notify_event_t*)event;
176e5c31af7Sopenharmony_ci				if (pnEvent->atom == XCB_ATOM_RESOLUTION)
177e5c31af7Sopenharmony_ci				{
178e5c31af7Sopenharmony_ci					deFree(event);
179e5c31af7Sopenharmony_ci					break;
180e5c31af7Sopenharmony_ci				}
181e5c31af7Sopenharmony_ci			}
182e5c31af7Sopenharmony_ci			deFree(event);
183e5c31af7Sopenharmony_ci		}
184e5c31af7Sopenharmony_ci		getDimensions (&w,&h);
185e5c31af7Sopenharmony_ci		if (h==height || w==width)
186e5c31af7Sopenharmony_ci			break;
187e5c31af7Sopenharmony_ci	}
188e5c31af7Sopenharmony_ci}
189e5c31af7Sopenharmony_ci
190e5c31af7Sopenharmony_ci} // xcb
191e5c31af7Sopenharmony_ci} // lnx
192e5c31af7Sopenharmony_ci} // tcu
193