/* Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto 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 #include #include #include #include "Types.h" #include "Log.h" #include "OS.h" namespace crown { namespace os { Display* display = NULL; Window window = None; GLXContext glx_context = NULL; GLXDrawable glx_window = None; //----------------------------------------------------------------------------- bool create_render_window(uint x, uint y, uint width, uint height, bool fullscreen) { assert(width != 0 && height != 0); display = XOpenDisplay(NULL); if (display == NULL) { Log::E("Unable to open a display"); return false; } Window defRoot = DefaultRootWindow(display); // Color index buffer not supported - deprecated int fbAttribs[] = { GLX_DOUBLEBUFFER, True, // Only double-buffered GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8, GLX_ALPHA_SIZE, 8, GLX_DEPTH_SIZE, 24, // Depth buffer size GLX_STENCIL_SIZE, 0, // Stencil buffer size GLX_ACCUM_RED_SIZE, 0, GLX_ACCUM_GREEN_SIZE, 0, GLX_ACCUM_BLUE_SIZE, 0, GLX_ACCUM_ALPHA_SIZE, 0, GLX_RENDER_TYPE, GLX_RGBA_BIT, // The default framebuffer is always RGBA GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, GLX_X_RENDERABLE, True, GLX_CONFIG_CAVEAT, GLX_DONT_CARE, GLX_TRANSPARENT_TYPE, GLX_NONE, None }; int fbCount; GLXFBConfig* fbConfig = glXChooseFBConfig(display, XDefaultScreen(display), fbAttribs, &fbCount); if (!fbConfig) { Log::E("Unable to find a matching FrameBuffer configuration."); return false; } XVisualInfo* visualInfo = glXGetVisualFromFBConfig(display, fbConfig[0]); if (!visualInfo) { Log::E("Unable to find a matching Visual for the FrameBuffer configuration."); XFree(fbConfig); return false; } Colormap cmap; cmap = XCreateColormap(display, defRoot, visualInfo->visual, AllocNone); XSetWindowAttributes winAttribs; winAttribs.colormap = cmap; winAttribs.event_mask = FocusChangeMask | StructureNotifyMask; window = XCreateWindow( display, defRoot, x, y, width, height, 0, visualInfo->depth, InputOutput, visualInfo->visual, CWColormap | CWEventMask, &winAttribs ); if (!window) { Log::E("Unable to create the X Window."); return false; } XMapRaised(display, window); glx_window = glXCreateWindow(display, fbConfig[0], window, 0); glx_context = glXCreateNewContext(display, fbConfig[0], GLX_RGBA_TYPE, NULL, True); glXMakeContextCurrent(display, glx_window, glx_window, glx_context); XFreeColormap(display, cmap); XFree(visualInfo); XFree(fbConfig); XFlush(display); return true; } //----------------------------------------------------------------------------- bool destroy_render_window() { if (display) { if (glx_window) { glXDestroyWindow(display, glx_window); } if (glx_context) { glXMakeContextCurrent(display, None, None, NULL); glXDestroyContext(display, glx_context); } if (window) { XDestroyWindow(display, window); } XCloseDisplay(display); } } //----------------------------------------------------------------------------- void swap_buffers() { glXSwapBuffers(display, glx_window); } //----------------------------------------------------------------------------- void get_render_window_metrics(uint& width, uint& height) { XWindowAttributes attribs; XGetWindowAttributes(display, window, &attribs); XFlush(display); width = attribs.width; height = attribs.height; } ////----------------------------------------------------------------------------- //void GLXRenderWindow::Move(uint x, uint y) //{ // if (x == mX && y == mY) // { // return; // } // XMoveWindow(display, window, x, y); //} ////----------------------------------------------------------------------------- //void GLXRenderWindow::Resize(uint width, uint height) //{ // if (!width || !height) // { // return; // } // if (width == mWidth && height == mHeight) // { // return; // } // XResizeWindow(display, window, width, height); //} ////----------------------------------------------------------------------------- //void GLXRenderWindow::SetFullscreen(bool full) //{ // mFull = full; // XEvent xEvent; // Atom wmState = XInternAtom(display, "_NET_WM_STATE", False); // Atom fullscreen = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", False); // xEvent.type = ClientMessage; // xEvent.xclient.window = window; // xEvent.xclient.message_type = wmState; // xEvent.xclient.format = 32; // xEvent.xclient.data.l[0] = (mFull ? 1 : 0); // xEvent.xclient.data.l[1] = fullscreen; // xEvent.xclient.data.l[2] = 0; // XSendEvent(display, DefaultRootWindow(display), False, SubstructureNotifyMask, &xEvent); //} } // namespace os } // namespace crown