1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Tester Core
3 * ----------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief X11 utilities.
22 *//*--------------------------------------------------------------------*/
24 #include "tcuLnxX11.hpp"
25 #include "gluRenderConfig.hpp"
28 #include <X11/Xutil.h>
37 DisplayBase::DisplayBase (EventState& platform)
38 : m_eventState (platform)
42 DisplayBase::~DisplayBase (void)
46 WindowBase::WindowBase ()
51 WindowBase::~WindowBase (void)
55 XlibDisplay::DisplayState XlibDisplay::s_displayState = XlibDisplay::DISPLAY_STATE_UNKNOWN;
57 bool XlibDisplay::hasDisplay (const char* name)
59 if (s_displayState == DISPLAY_STATE_UNKNOWN)
62 Display *display = XOpenDisplay((char*)name);
65 s_displayState = DISPLAY_STATE_AVAILABLE;
66 XCloseDisplay(display);
68 s_displayState = DISPLAY_STATE_UNAVAILABLE;
70 return s_displayState == DISPLAY_STATE_AVAILABLE ? true : false;
73 XlibDisplay::XlibDisplay (EventState& eventState, const char* name)
74 : DisplayBase (eventState)
76 // From man:XinitThreads(3):
78 // The XInitThreads function initializes Xlib support for concurrent
79 // threads. This function must be the first Xlib function
80 // a multi-threaded program calls, and it must complete before any other
82 DE_CHECK_RUNTIME_ERR(XInitThreads() != 0);
83 m_display = XOpenDisplay((char*)name); // Won't modify argument string.
85 throw ResourceError("Failed to open display", name, __FILE__, __LINE__);
87 m_deleteAtom = XInternAtom(m_display, "WM_DELETE_WINDOW", False);
90 XlibDisplay::~XlibDisplay (void)
92 XCloseDisplay(m_display);
95 void XlibDisplay::processEvent (XEvent& event)
100 if ((unsigned)event.xclient.data.l[0] == m_deleteAtom)
101 m_eventState.setQuitFlag(true);
103 // note: ConfigureNotify for window is handled in setDimensions()
109 void XlibDisplay::processEvents (void)
113 while (XPending(m_display))
115 XNextEvent(m_display, &event);
120 bool XlibDisplay::getVisualInfo (VisualID visualID, XVisualInfo& dst)
123 query.visualid = visualID;
125 XVisualInfo* response = XGetVisualInfo(m_display, VisualIDMask, &query, &numVisuals);
128 if (response != DE_NULL)
130 if (numVisuals > 0) // should be 1, but you never know...
141 ::Visual* XlibDisplay::getVisual (VisualID visualID)
145 if (getVisualInfo(visualID, info))
151 XlibWindow::XlibWindow (XlibDisplay& display, int width, int height, ::Visual* visual)
153 , m_display (display)
157 XSetWindowAttributes swa;
158 ::Display* const dpy = m_display.getXDisplay();
159 ::Window root = DefaultRootWindow(dpy);
160 unsigned long mask = CWBorderPixel | CWEventMask;
162 // If redirect is enabled, window size can't be guaranteed and it is up to
163 // the window manager to decide whether to honor sizing requests. However,
164 // overriding that causes window to appear as an overlay, which causes
165 // other issues, so this is disabled by default.
166 const bool overrideRedirect = false;
168 int depth = CopyFromParent;
170 if (overrideRedirect)
172 mask |= CWOverrideRedirect;
173 swa.override_redirect = true;
176 if (visual == DE_NULL)
177 visual = CopyFromParent;
180 XVisualInfo info = XVisualInfo();
181 bool succ = display.getVisualInfo(XVisualIDFromVisual(visual), info);
183 TCU_CHECK_INTERNAL(succ);
185 root = RootWindow(dpy, info.screen);
186 m_colormap = XCreateColormap(dpy, root, visual, AllocNone);
187 swa.colormap = m_colormap;
193 swa.border_pixel = 0;
194 swa.event_mask = ExposureMask|KeyPressMask|KeyReleaseMask|StructureNotifyMask;
196 if (width == glu::RenderConfig::DONT_CARE)
197 width = DEFAULT_WINDOW_WIDTH;
198 if (height == glu::RenderConfig::DONT_CARE)
199 height = DEFAULT_WINDOW_HEIGHT;
201 m_window = XCreateWindow(dpy, root, 0, 0, width, height, 0,
202 depth, InputOutput, visual, mask, &swa);
205 /* Prevent the window from stealing input, since our windows are
208 XWMHints *hints = XAllocWMHints();
209 hints->flags |= InputHint;
210 hints->input = False;
211 XSetWMHints(dpy, m_window, hints);
214 Atom deleteAtom = m_display.getDeleteAtom();
215 XSetWMProtocols(dpy, m_window, &deleteAtom, 1);
219 void XlibWindow::setVisibility (bool visible)
221 ::Display* dpy = m_display.getXDisplay();
222 int eventType = None;
225 if (visible == m_visible)
230 XMapWindow(dpy, m_window);
231 eventType = MapNotify;
235 XUnmapWindow(dpy, m_window);
236 eventType = UnmapNotify;
239 // We are only interested about exposure/structure notify events, not user input
240 XSelectInput(dpy, m_window, ExposureMask | StructureNotifyMask);
244 XWindowEvent(dpy, m_window, ExposureMask | StructureNotifyMask, &event);
245 } while (event.type != eventType);
250 void XlibWindow::getDimensions (int* width, int* height) const
254 unsigned width_, height_, borderWidth, depth;
256 XGetGeometry(m_display.getXDisplay(), m_window, &root, &x, &y, &width_, &height_, &borderWidth, &depth);
257 if (width != DE_NULL)
258 *width = static_cast<int>(width_);
259 if (height != DE_NULL)
260 *height = static_cast<int>(height_);
263 void XlibWindow::setDimensions (int width, int height)
265 const unsigned int mask = CWWidth | CWHeight;
266 XWindowChanges changes;
267 ::Display* dpy = m_display.getXDisplay();
269 changes.width = width;
270 changes.height = height;
271 XConfigureWindow(dpy, m_window, mask, &changes);
276 XNextEvent(dpy, &myevent);
277 if (myevent.type == ConfigureNotify) {
278 XConfigureEvent e = myevent.xconfigure;
279 if (e.width == width && e.height == height)
283 m_display.processEvent(myevent);
287 void XlibWindow::processEvents (void)
289 // A bit of a hack, since we don't really handle all the events.
290 m_display.processEvents();
293 XlibWindow::~XlibWindow (void)
295 XDestroyWindow(m_display.getXDisplay(), m_window);
296 if (m_colormap != None)
297 XFreeColormap(m_display.getXDisplay(), m_colormap);