GLXRenderWindow.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <X11/Xutil.h>
  23. #include <X11/Xatom.h>
  24. #include <X11/Xlib.h>
  25. #include <GL/glx.h>
  26. #include "Types.h"
  27. #include "Log.h"
  28. #include "OS.h"
  29. namespace crown
  30. {
  31. namespace os
  32. {
  33. Display* display = NULL;
  34. Window window = None;
  35. GLXContext glx_context = NULL;
  36. GLXDrawable glx_window = None;
  37. //-----------------------------------------------------------------------------
  38. bool create_render_window(uint x, uint y, uint width, uint height, bool fullscreen)
  39. {
  40. assert(width != 0 && height != 0);
  41. display = XOpenDisplay(NULL);
  42. if (display == NULL)
  43. {
  44. Log::E("Unable to open a display");
  45. return false;
  46. }
  47. Window defRoot = DefaultRootWindow(display);
  48. // Color index buffer not supported - deprecated
  49. int fbAttribs[] =
  50. {
  51. GLX_DOUBLEBUFFER, True, // Only double-buffered
  52. GLX_RED_SIZE, 8,
  53. GLX_GREEN_SIZE, 8,
  54. GLX_BLUE_SIZE, 8,
  55. GLX_ALPHA_SIZE, 8,
  56. GLX_DEPTH_SIZE, 24, // Depth buffer size
  57. GLX_STENCIL_SIZE, 0, // Stencil buffer size
  58. GLX_ACCUM_RED_SIZE, 0,
  59. GLX_ACCUM_GREEN_SIZE, 0,
  60. GLX_ACCUM_BLUE_SIZE, 0,
  61. GLX_ACCUM_ALPHA_SIZE, 0,
  62. GLX_RENDER_TYPE, GLX_RGBA_BIT, // The default framebuffer is always RGBA
  63. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  64. GLX_X_RENDERABLE, True,
  65. GLX_CONFIG_CAVEAT, GLX_DONT_CARE,
  66. GLX_TRANSPARENT_TYPE, GLX_NONE,
  67. None
  68. };
  69. int fbCount;
  70. GLXFBConfig* fbConfig = glXChooseFBConfig(display, XDefaultScreen(display), fbAttribs, &fbCount);
  71. if (!fbConfig)
  72. {
  73. Log::E("Unable to find a matching FrameBuffer configuration.");
  74. return false;
  75. }
  76. XVisualInfo* visualInfo = glXGetVisualFromFBConfig(display, fbConfig[0]);
  77. if (!visualInfo)
  78. {
  79. Log::E("Unable to find a matching Visual for the FrameBuffer configuration.");
  80. XFree(fbConfig);
  81. return false;
  82. }
  83. Colormap cmap;
  84. cmap = XCreateColormap(display, defRoot, visualInfo->visual, AllocNone);
  85. XSetWindowAttributes winAttribs;
  86. winAttribs.colormap = cmap;
  87. winAttribs.event_mask = FocusChangeMask | StructureNotifyMask;
  88. window = XCreateWindow(
  89. display,
  90. defRoot,
  91. x, y,
  92. width, height,
  93. 0,
  94. visualInfo->depth,
  95. InputOutput,
  96. visualInfo->visual,
  97. CWColormap | CWEventMask,
  98. &winAttribs
  99. );
  100. if (!window)
  101. {
  102. Log::E("Unable to create the X Window.");
  103. return false;
  104. }
  105. XMapRaised(display, window);
  106. glx_window = glXCreateWindow(display, fbConfig[0], window, 0);
  107. glx_context = glXCreateNewContext(display, fbConfig[0], GLX_RGBA_TYPE, NULL, True);
  108. glXMakeContextCurrent(display, glx_window, glx_window, glx_context);
  109. XFreeColormap(display, cmap);
  110. XFree(visualInfo);
  111. XFree(fbConfig);
  112. XFlush(display);
  113. return true;
  114. }
  115. //-----------------------------------------------------------------------------
  116. bool destroy_render_window()
  117. {
  118. if (display)
  119. {
  120. if (glx_window)
  121. {
  122. glXDestroyWindow(display, glx_window);
  123. }
  124. if (glx_context)
  125. {
  126. glXMakeContextCurrent(display, None, None, NULL);
  127. glXDestroyContext(display, glx_context);
  128. }
  129. if (window)
  130. {
  131. XDestroyWindow(display, window);
  132. }
  133. XCloseDisplay(display);
  134. }
  135. }
  136. //-----------------------------------------------------------------------------
  137. void swap_buffers()
  138. {
  139. glXSwapBuffers(display, glx_window);
  140. }
  141. //-----------------------------------------------------------------------------
  142. void get_render_window_metrics(uint& width, uint& height)
  143. {
  144. XWindowAttributes attribs;
  145. XGetWindowAttributes(display, window, &attribs);
  146. XFlush(display);
  147. width = attribs.width;
  148. height = attribs.height;
  149. }
  150. ////-----------------------------------------------------------------------------
  151. //void GLXRenderWindow::Move(uint x, uint y)
  152. //{
  153. // if (x == mX && y == mY)
  154. // {
  155. // return;
  156. // }
  157. // XMoveWindow(display, window, x, y);
  158. //}
  159. ////-----------------------------------------------------------------------------
  160. //void GLXRenderWindow::Resize(uint width, uint height)
  161. //{
  162. // if (!width || !height)
  163. // {
  164. // return;
  165. // }
  166. // if (width == mWidth && height == mHeight)
  167. // {
  168. // return;
  169. // }
  170. // XResizeWindow(display, window, width, height);
  171. //}
  172. ////-----------------------------------------------------------------------------
  173. //void GLXRenderWindow::SetFullscreen(bool full)
  174. //{
  175. // mFull = full;
  176. // XEvent xEvent;
  177. // Atom wmState = XInternAtom(display, "_NET_WM_STATE", False);
  178. // Atom fullscreen = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", False);
  179. // xEvent.type = ClientMessage;
  180. // xEvent.xclient.window = window;
  181. // xEvent.xclient.message_type = wmState;
  182. // xEvent.xclient.format = 32;
  183. // xEvent.xclient.data.l[0] = (mFull ? 1 : 0);
  184. // xEvent.xclient.data.l[1] = fullscreen;
  185. // xEvent.xclient.data.l[2] = 0;
  186. // XSendEvent(display, DefaultRootWindow(display), False, SubstructureNotifyMask, &xEvent);
  187. //}
  188. } // namespace os
  189. } // namespace crown