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 *//*--------------------------------------------------------------------*/
25 #include "gluRenderConfig.hpp"
28 #include <X11/Xutil.h>
35 EventState::EventState (void)
40 EventState::~EventState (void)
44 void EventState::setQuitFlag (bool quit)
46 de::ScopedLock lock(m_mutex);
50 bool EventState::getQuitFlag (void)
52 de::ScopedLock lock(m_mutex);
56 DisplayBase::DisplayBase (EventState& platform)
57 : m_eventState (platform)
61 DisplayBase::~DisplayBase (void)
65 WindowBase::WindowBase ()
70 WindowBase::~WindowBase (void)
74 XlibDisplay::XlibDisplay (EventState& eventState, const char* name)
75 : DisplayBase (eventState)
77 m_display = XOpenDisplay((char*)name); // Won't modify argument string.
79 throw ResourceError("Failed to open display", name, __FILE__, __LINE__);
81 m_deleteAtom = XInternAtom(m_display, "WM_DELETE_WINDOW", False);
84 XlibDisplay::~XlibDisplay (void)
86 XCloseDisplay(m_display);
89 void XlibDisplay::processEvent (XEvent& event)
94 if ((unsigned)event.xclient.data.l[0] == m_deleteAtom)
95 m_eventState.setQuitFlag(true);
97 // note: ConfigureNotify for window is handled in setDimensions()
103 void XlibDisplay::processEvents (void)
107 while (XPending(m_display))
109 XNextEvent(m_display, &event);
114 bool XlibDisplay::getVisualInfo (VisualID visualID, XVisualInfo& dst)
117 query.visualid = visualID;
119 XVisualInfo* response = XGetVisualInfo(m_display, VisualIDMask, &query, &numVisuals);
122 if (response != DE_NULL)
124 if (numVisuals > 0) // should be 1, but you never know...
135 ::Visual* XlibDisplay::getVisual (VisualID visualID)
139 if (getVisualInfo(visualID, info))
145 XlibWindow::XlibWindow (XlibDisplay& display, int width, int height, ::Visual* visual)
147 , m_display (display)
151 XSetWindowAttributes swa;
152 ::Display* const dpy = m_display.getXDisplay();
153 ::Window root = DefaultRootWindow(dpy);
154 unsigned long mask = CWBorderPixel | CWEventMask;
156 // If redirect is enabled, window size can't be guaranteed and it is up to
157 // the window manager to decide whether to honor sizing requests. However,
158 // overriding that causes window to appear as an overlay, which causes
159 // other issues, so this is disabled by default.
160 const bool overrideRedirect = false;
162 if (overrideRedirect)
164 mask |= CWOverrideRedirect;
165 swa.override_redirect = true;
168 if (visual == DE_NULL)
169 visual = CopyFromParent;
172 XVisualInfo info = XVisualInfo();
173 bool succ = display.getVisualInfo(XVisualIDFromVisual(visual), info);
175 TCU_CHECK_INTERNAL(succ);
177 root = RootWindow(dpy, info.screen);
178 m_colormap = XCreateColormap(dpy, root, visual, AllocNone);
179 swa.colormap = m_colormap;
183 swa.border_pixel = 0;
184 swa.event_mask = ExposureMask|KeyPressMask|KeyReleaseMask|StructureNotifyMask;
186 if (width == glu::RenderConfig::DONT_CARE)
187 width = DEFAULT_WINDOW_WIDTH;
188 if (height == glu::RenderConfig::DONT_CARE)
189 height = DEFAULT_WINDOW_HEIGHT;
191 m_window = XCreateWindow(dpy, root, 0, 0, width, height, 0,
192 CopyFromParent, InputOutput, visual, mask, &swa);
195 Atom deleteAtom = m_display.getDeleteAtom();
196 XSetWMProtocols(dpy, m_window, &deleteAtom, 1);
200 void XlibWindow::setVisibility (bool visible)
202 ::Display* dpy = m_display.getXDisplay();
203 int eventType = None;
206 if (visible == m_visible)
211 XMapWindow(dpy, m_window);
212 eventType = MapNotify;
216 XUnmapWindow(dpy, m_window);
217 eventType = UnmapNotify;
220 // We are only interested about exposure/structure notify events, not user input
221 XSelectInput(dpy, m_window, ExposureMask | StructureNotifyMask);
225 XWindowEvent(dpy, m_window, ExposureMask | StructureNotifyMask, &event);
226 } while (event.type != eventType);
231 void XlibWindow::getDimensions (int* width, int* height) const
235 unsigned width_, height_, borderWidth, depth;
237 XGetGeometry(m_display.getXDisplay(), m_window, &root, &x, &y, &width_, &height_, &borderWidth, &depth);
238 if (width != DE_NULL)
239 *width = static_cast<int>(width_);
240 if (height != DE_NULL)
241 *height = static_cast<int>(height_);
244 void XlibWindow::setDimensions (int width, int height)
246 const unsigned int mask = CWWidth | CWHeight;
247 XWindowChanges changes;
248 ::Display* dpy = m_display.getXDisplay();
250 changes.width = width;
251 changes.height = height;
252 XConfigureWindow(dpy, m_window, mask, &changes);
257 XNextEvent(dpy, &myevent);
258 if (myevent.type == ConfigureNotify) {
259 XConfigureEvent e = myevent.xconfigure;
260 if (e.width == width && e.height == height)
264 m_display.processEvent(myevent);
268 void XlibWindow::processEvents (void)
270 // A bit of a hack, since we don't really handle all the events.
271 m_display.processEvents();
274 XlibWindow::~XlibWindow (void)
276 XDestroyWindow(m_display.getXDisplay(), m_window);
277 if (m_colormap != None)
278 XFreeColormap(m_display.getXDisplay(), m_colormap);