X11OpenGLWindow.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. #include "X11OpenGLWindow.h"
  2. #include "OpenGLInclude.h"
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #ifdef GLEW_STATIC
  6. #include "CustomGL/glew.h"
  7. #else
  8. #include <GL/glew.h>
  9. #endif//GLEW_STATIC
  10. #ifdef GLEW_DYNAMIC_LOAD_ALL_GLX_FUNCTIONS
  11. #include "CustomGL/glxew.h"
  12. #else
  13. #include<GL/glx.h>
  14. #endif // GLEW_DYNAMIC_LOAD_ALL_GLX_FUNCTIONS
  15. #include <assert.h>
  16. //#define DYNAMIC_LOAD_X11_FUNCTIONS
  17. #ifdef DYNAMIC_LOAD_X11_FUNCTIONS
  18. #include <dlfcn.h>
  19. #endif //DYNAMIC_LOAD_X11_FUNCTIONS
  20. //#include<X11/X.h>
  21. //#include<X11/Xlib.h>
  22. //#include<GL/gl.h>
  23. //defined in GL/glxew.h
  24. //#include<GL/glu.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <pthread.h>
  30. GLint att[] = { GLX_RGBA,
  31. GLX_DEPTH_SIZE, 24,
  32. GLX_RED_SIZE , 8,
  33. GLX_GREEN_SIZE , 8,
  34. GLX_BLUE_SIZE , 8,
  35. GLX_ALPHA_SIZE , 8,
  36. GLX_STENCIL_SIZE , 8,
  37. GLX_DOUBLEBUFFER,
  38. None };
  39. /*
  40. static int att[] =
  41. {
  42. GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None
  43. GLX_X_RENDERABLE , True,
  44. GLX_DRAWABLE_TYPE , GLX_WINDOW_BIT,
  45. GLX_RENDER_TYPE , GLX_RGBA_BIT,
  46. GLX_X_VISUAL_TYPE , GLX_TRUE_COLOR,
  47. GLX_RED_SIZE , 8,
  48. GLX_GREEN_SIZE , 8,
  49. GLX_BLUE_SIZE , 8,
  50. GLX_ALPHA_SIZE , 8,
  51. GLX_DEPTH_SIZE , 24,
  52. GLX_STENCIL_SIZE , 8,
  53. GLX_DOUBLEBUFFER , True,
  54. None
  55. };
  56. */
  57. static bool forceOpenGL3 = true;
  58. #ifdef DYNAMIC_LOAD_X11_FUNCTIONS
  59. ///our X11 function typedefs
  60. typedef int (*PFNXFREE)(void*);
  61. typedef XErrorHandler (* PFNXSETERRORHANDLER) (XErrorHandler);
  62. typedef int (* PFNXSYNC) (Display* a,Bool b);
  63. typedef Display* (* PFNXOPENDISPLAY) (_Xconst char* a);
  64. typedef Colormap (*PFNXCREATECOLORMAP) (Display* a,Window b,Visual* c,int d);
  65. typedef Window (*PFNXCREATEWINDOW) (Display* a,Window b,int c,int d,unsigned int e,unsigned int f,unsigned int g,int h,unsigned int i,Visual* j,unsigned long k,XSetWindowAttributes* l);
  66. typedef int (*PFNXMAPWINDOW) (Display*, Window);
  67. typedef int (*PFNXSTORENAME) (Display* a,Window b,_Xconst char* c);
  68. typedef int (*PFNXCLOSEDISPLAY) (Display* a);
  69. typedef int (*PFNXDESTROYWINDOW) (Display* a,Window b);
  70. typedef int (*PFNXRAISEWINDOW) (Display* a, Window b);
  71. #if NeedWidePrototypes
  72. typedef KeySym* (*PFNXGETKEYBOARDMAPPING) (Display*,unsigned int,int,int*);
  73. typedef KeySym (*PFNXKEYCODETOKEYSYM) (Display* a,unsigned int b,int c);
  74. #else
  75. typedef KeySym* (*PFNXGETKEYBOARDMAPPING) (Display*,KeyCode,int,int*);
  76. typedef KeySym (*PFNXKEYCODETOKEYSYM) (Display* a,KeyCode b,int c);
  77. #endif
  78. typedef void (*PFNXCONVERTCASE) (KeySym /* sym */,KeySym * /* lower */,KeySym * /* upper */);
  79. typedef int (*PFNXPENDING) (Display* a);
  80. typedef int (*PFNXNEXTEVENT) (Display* a,XEvent* b);
  81. typedef int (*PFNXEVENTSQUEUED) (Display* a,int b);
  82. typedef int (*PFNXPEEKEVENT) (Display* a,XEvent* b);
  83. typedef KeySym (*PFNXLOOKUPKEYSYM) (XKeyEvent* a,int b);
  84. typedef Status (*PFNXGETWINDOWATTRIBUTES) (Display* a,Window b,XWindowAttributes* c);
  85. #define X11_LIBRARY "libX11.so.6"
  86. #define MyXSync m_data->m_x11_XSync
  87. #define MyXGetKeyboardMapping m_data->m_x11_XGetKeyboardMapping
  88. #define MyXSetErrorHandler m_data->m_x11_XSetErrorHandler
  89. #define MyXOpenDisplay m_data->m_x11_XOpenDisplay
  90. #define MyXCreateColormap m_data->m_x11_XCreateColormap
  91. #define MyXCreateWindow m_data->m_x11_XCreateWindow
  92. #define MyXMapWindow m_data->m_x11_XMapWindow
  93. #define MyXStoreName m_data->m_x11_XStoreName
  94. #define MyXDestroyWindow m_data->m_x11_XDestroyWindow
  95. #define MyXRaiseWindow m_data->m_x11_XRaiseWindow
  96. #define MyXCloseDisplay m_data->m_x11_XCloseDisplay
  97. #define MyXKeycodeToKeysym m_data->m_x11_XKeycodeToKeysym
  98. #define MyXConvertCase m_data->m_x11_XConvertCase
  99. #define MyXPending m_data->m_x11_XPending
  100. #define MyXNextEvent m_data->m_x11_XNextEvent
  101. #define MyXEventsQueued m_data->m_x11_XEventsQueued
  102. #define MyXPeekEvent m_data->m_x11_XPeekEvent
  103. #define MyXNextEvent m_data->m_x11_XNextEvent
  104. #define MyXGetWindowAttributes m_data->m_x11_XGetWindowAttributes
  105. #define MyXStoreName m_data->m_x11_XStoreName
  106. #define MyXFree m_data->m_x11_XFree
  107. #define MyXMapWindow m_data->m_x11_XMapWindow
  108. #define MyXStoreName m_data->m_x11_XStoreName
  109. #define MyXLookupKeysym m_data->m_x11_XLookupKeysym
  110. #else
  111. #define MyXSync XSync
  112. #define MyXGetKeyboardMapping XGetKeyboardMapping
  113. #define MyXSetErrorHandler XSetErrorHandler
  114. #define MyXOpenDisplay XOpenDisplay
  115. #define MyXCreateColormap XCreateColormap
  116. #define MyXCreateWindow XCreateWindow
  117. #define MyXMapWindow XMapWindow
  118. #define MyXStoreName XStoreName
  119. #define MyXDestroyWindow XDestroyWindow
  120. #define MyXRaiseWindow XRaiseWindow
  121. #define MyXCloseDisplay XCloseDisplay
  122. #define MyXKeycodeToKeysym XKeycodeToKeysym
  123. #define MyXConvertCase XConvertCase
  124. #define MyXPending XPending
  125. #define MyXNextEvent XNextEvent
  126. #define MyXEventsQueued XEventsQueued
  127. #define MyXPeekEvent XPeekEvent
  128. #define MyXNextEvent XNextEvent
  129. #define MyXGetWindowAttributes XGetWindowAttributes
  130. #define MyXStoreName XStoreName
  131. #define MyXFree XFree
  132. #define MyXMapWindow XMapWindow
  133. #define MyXStoreName XStoreName
  134. #define MyXLookupKeysym XLookupKeysym
  135. #endif//DYNAMIC_LOAD_X11_FUNCTIONS
  136. enum
  137. {
  138. MY_X11_ALT_KEY = 1,
  139. MY_X11_SHIFT_KEY = 2,
  140. MY_X11_CONTROL_KEY = 4
  141. };
  142. struct InternalData2
  143. {
  144. Display* m_dpy;
  145. Window m_root;
  146. XVisualInfo* m_vi;
  147. Colormap m_cmap;
  148. XSetWindowAttributes m_swa;
  149. Window m_win;
  150. GLXContext m_glc;
  151. XWindowAttributes m_gwa;
  152. XEvent m_xev;
  153. GLXFBConfig m_bestFbc;
  154. int m_modifierFlags;
  155. int m_glWidth;
  156. int m_glHeight;
  157. #ifdef DYNAMIC_LOAD_X11_FUNCTIONS
  158. //dynamically load stuff
  159. void* m_x11_library;
  160. PFNXFREE m_x11_XFree;
  161. PFNXSETERRORHANDLER m_x11_XSetErrorHandler;
  162. PFNXSYNC m_x11_XSync;
  163. PFNXOPENDISPLAY m_x11_XOpenDisplay;
  164. PFNXCREATECOLORMAP m_x11_XCreateColormap;
  165. PFNXCREATEWINDOW m_x11_XCreateWindow;
  166. PFNXMAPWINDOW m_x11_XMapWindow;
  167. PFNXSTORENAME m_x11_XStoreName;
  168. PFNXCLOSEDISPLAY m_x11_XCloseDisplay;
  169. PFNXDESTROYWINDOW m_x11_XDestroyWindow;
  170. PFNXRAISEWINDOW m_x11_XRaiseWindow;
  171. PFNXKEYCODETOKEYSYM m_x11_XKeycodeToKeysym;
  172. PFNXGETKEYBOARDMAPPING m_x11_XGetKeyboardMapping;
  173. PFNXCONVERTCASE m_x11_XConvertCase;
  174. PFNXPENDING m_x11_XPending;
  175. PFNXNEXTEVENT m_x11_XNextEvent;
  176. PFNXEVENTSQUEUED m_x11_XEventsQueued;
  177. PFNXPEEKEVENT m_x11_XPeekEvent;
  178. PFNXLOOKUPKEYSYM m_x11_XLookupKeysym;
  179. PFNXGETWINDOWATTRIBUTES m_x11_XGetWindowAttributes;
  180. #endif //DYNAMIC_LOAD_X11_FUNCTIONS
  181. b3WheelCallback m_wheelCallback;
  182. b3MouseMoveCallback m_mouseMoveCallback;
  183. b3MouseButtonCallback m_mouseButtonCallback;
  184. b3ResizeCallback m_resizeCallback;
  185. b3KeyboardCallback m_keyboardCallback;
  186. InternalData2()
  187. :m_dpy(0),
  188. m_vi(0),
  189. m_modifierFlags(0),
  190. m_glWidth(-1),
  191. m_glHeight(-1),
  192. m_wheelCallback(0),
  193. m_mouseMoveCallback(0),
  194. m_mouseButtonCallback(0),
  195. m_resizeCallback(0),
  196. m_keyboardCallback(0)
  197. {
  198. #ifdef DYNAMIC_LOAD_X11_FUNCTIONS
  199. m_x11_library = dlopen(X11_LIBRARY, RTLD_LOCAL | RTLD_NOW);
  200. if (!m_x11_library)
  201. {
  202. printf("Error opening X11 library %s\n", X11_LIBRARY);
  203. exit(0);
  204. }
  205. bool missingFunc = false;
  206. missingFunc = ((m_x11_XFree = (PFNXFREE) dlsym(m_x11_library, "XFree"))==NULL) | missingFunc;
  207. assert(!missingFunc);
  208. if (missingFunc) { printf("Error: missing func XFree in %s, exiting!\n", X11_LIBRARY); exit(0);}
  209. missingFunc = ((m_x11_XSetErrorHandler = (PFNXSETERRORHANDLER) dlsym(m_x11_library,"XSetErrorHandler"))==NULL) | missingFunc;
  210. if (missingFunc) { printf("Error: missing func XSetErrorHandler in %s, exiting!\n", X11_LIBRARY); exit(0);}
  211. missingFunc = ((m_x11_XSetErrorHandler = (PFNXSETERRORHANDLER) dlsym(m_x11_library,"XSetErrorHandler"))==NULL) | missingFunc;
  212. if (missingFunc) { printf("Error: missing func XSetErrorHandler in %s, exiting!\n", X11_LIBRARY); exit(0);}
  213. missingFunc = ((m_x11_XSync = (PFNXSYNC) dlsym(m_x11_library,"XSync"))==NULL) | missingFunc;
  214. if (missingFunc) { printf("Error: missing func XSync in %s, exiting!\n", X11_LIBRARY); exit(0);}
  215. missingFunc = ((m_x11_XOpenDisplay = (PFNXOPENDISPLAY) dlsym(m_x11_library,"XOpenDisplay"))==NULL) | missingFunc;
  216. if (missingFunc) { printf("Error: missing func XOpenDisplay in %s, exiting!\n", X11_LIBRARY); exit(0);}
  217. missingFunc = ((m_x11_XCreateColormap = (PFNXCREATECOLORMAP) dlsym(m_x11_library,"XCreateColormap"))==NULL) | missingFunc;
  218. if (missingFunc) { printf("Error: missing func XCreateColormap in %s, exiting!\n", X11_LIBRARY); exit(0);}
  219. missingFunc = ((m_x11_XCreateWindow = (PFNXCREATEWINDOW) dlsym(m_x11_library,"XCreateWindow"))==NULL) | missingFunc;
  220. if (missingFunc) { printf("Error: missing func XCreateWindow in %s, exiting!\n", X11_LIBRARY); exit(0);}
  221. missingFunc = ((m_x11_XMapWindow = (PFNXMAPWINDOW) dlsym(m_x11_library,"XMapWindow"))==NULL) | missingFunc;
  222. if (missingFunc) { printf("Error: missing func XMapWindow in %s, exiting!\n", X11_LIBRARY); exit(0);}
  223. missingFunc = ((m_x11_XStoreName = (PFNXSTORENAME) dlsym(m_x11_library,"XStoreName"))==NULL) | missingFunc;
  224. if (missingFunc) { printf("Error: missing func XStoreName in %s, exiting!\n", X11_LIBRARY); exit(0);}
  225. missingFunc = ((m_x11_XCloseDisplay = (PFNXCLOSEDISPLAY) dlsym(m_x11_library,"XCloseDisplay"))==NULL) | missingFunc;
  226. if (missingFunc) { printf("Error: missing func XCloseDisplay in %s, exiting!\n", X11_LIBRARY); exit(0);}
  227. missingFunc = ((m_x11_XDestroyWindow = (PFNXDESTROYWINDOW) dlsym(m_x11_library,"XDestroyWindow"))==NULL) | missingFunc;
  228. if (missingFunc) { printf("Error: missing func XDestroyWindow in %s, exiting!\n", X11_LIBRARY); exit(0);}
  229. missingFunc = ((m_x11_XRaiseWindow = (PFNXRAISEWINDOW) dlsym(m_x11_library,"XRaiseWindow"))==NULL) | missingFunc;
  230. if (missingFunc) { printf("Error: missing func XRaiseWindow in %s, exiting!\n", X11_LIBRARY); exit(0);}
  231. missingFunc = ((m_x11_XGetKeyboardMapping = (PFNXGETKEYBOARDMAPPING) dlsym(m_x11_library,"XGetKeyboardMapping"))==NULL) | missingFunc;
  232. if (missingFunc) { printf("Error: missing func XGetKeyboardMapping in %s, exiting!\n", X11_LIBRARY); exit(0);}
  233. missingFunc = ((m_x11_XKeycodeToKeysym = (PFNXKEYCODETOKEYSYM) dlsym(m_x11_library,"XKeycodeToKeysym"))==NULL) | missingFunc;
  234. if (missingFunc) { printf("Error: missing func XKeycodeToKeysym in %s, exiting!\n", X11_LIBRARY); exit(0);}
  235. missingFunc = ((m_x11_XConvertCase = (PFNXCONVERTCASE) dlsym(m_x11_library,"XConvertCase"))==NULL) | missingFunc;
  236. if (missingFunc) { printf("Error: missing func XConvertCase in %s, exiting!\n", X11_LIBRARY); exit(0);}
  237. missingFunc = ((m_x11_XPending = (PFNXPENDING) dlsym(m_x11_library,"XPending"))==NULL) | missingFunc;
  238. if (missingFunc) { printf("Error: missing func XPending in %s, exiting!\n", X11_LIBRARY); exit(0);}
  239. missingFunc = ((m_x11_XNextEvent = (PFNXNEXTEVENT) dlsym(m_x11_library,"XNextEvent"))==NULL) | missingFunc;
  240. if (missingFunc) { printf("Error: missing func XNextEvent in %s, exiting!\n", X11_LIBRARY); exit(0);}
  241. missingFunc = ((m_x11_XEventsQueued = (PFNXEVENTSQUEUED) dlsym(m_x11_library,"XEventsQueued"))==NULL) | missingFunc;
  242. if (missingFunc) { printf("Error: missing func XEventsQueued in %s, exiting!\n", X11_LIBRARY); exit(0);}
  243. missingFunc = ((m_x11_XPeekEvent = (PFNXPEEKEVENT) dlsym(m_x11_library,"XPeekEvent"))==NULL) | missingFunc;
  244. if (missingFunc) { printf("Error: missing func XPeekEvent in %s, exiting!\n", X11_LIBRARY); exit(0);}
  245. missingFunc = ((m_x11_XLookupKeysym = (PFNXLOOKUPKEYSYM) dlsym(m_x11_library,"XLookupKeysym"))==NULL) | missingFunc;
  246. if (missingFunc) { printf("Error: missing func XLookupKeysym in %s, exiting!\n", X11_LIBRARY); exit(0);}
  247. missingFunc = ((m_x11_XGetWindowAttributes = (PFNXGETWINDOWATTRIBUTES) dlsym(m_x11_library,"XGetWindowAttributes"))==NULL) | missingFunc;
  248. if (missingFunc) { printf("Error: missing func XGetWindowAttributes in %s, exiting!\n", X11_LIBRARY); exit(0);}
  249. if (missingFunc)
  250. {
  251. printf("Error: a missing func in %s, exiting!\n", X11_LIBRARY);
  252. exit(0);
  253. } else
  254. {
  255. printf("X11 functions dynamically loaded using dlopen/dlsym OK!\n");
  256. }
  257. #endif //DYNAMIC_LOAD_X11_FUNCTIONS
  258. }
  259. };
  260. #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
  261. #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
  262. typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
  263. // Helper to check for extension string presence. Adapted from:
  264. // http://www.opengl.org/resources/features/OGLextensions/
  265. static bool isExtensionSupported(const char *extList, const char *extension)
  266. {
  267. const char *start;
  268. const char *where, *terminator;
  269. /* Extension names should not have spaces. */
  270. where = strchr(extension, ' ');
  271. if (where || *extension == '\0')
  272. return false;
  273. /* It takes a bit of care to be fool-proof about parsing the
  274. OpenGL extensions string. Don't be fooled by sub-strings,
  275. etc. */
  276. for (start=extList;;) {
  277. where = strstr(start, extension);
  278. if (!where)
  279. break;
  280. terminator = where + strlen(extension);
  281. if ( where == start || *(where - 1) == ' ' )
  282. if ( *terminator == ' ' || *terminator == '\0' )
  283. return true;
  284. start = terminator;
  285. }
  286. return false;
  287. }
  288. static bool ctxErrorOccurred = false;
  289. static int ctxErrorHandler( Display *dpy, XErrorEvent *ev )
  290. {
  291. ctxErrorOccurred = true;
  292. return 0;
  293. }
  294. X11OpenGLWindow::X11OpenGLWindow()
  295. :m_OpenGLInitialized(false),
  296. m_requestedExit(false)
  297. {
  298. m_data = new InternalData2;
  299. }
  300. X11OpenGLWindow::~X11OpenGLWindow()
  301. {
  302. if (m_OpenGLInitialized)
  303. {
  304. disableOpenGL();
  305. }
  306. delete m_data;
  307. }
  308. void X11OpenGLWindow::enableOpenGL()
  309. {
  310. if (forceOpenGL3)
  311. {
  312. // Get the default screen's GLX extension list
  313. const char *glxExts = glXQueryExtensionsString( m_data->m_dpy,
  314. DefaultScreen( m_data->m_dpy ) );
  315. // NOTE: It is not necessary to create or make current to a context before
  316. // calling glXGetProcAddressARB, unless we dynamically load OpenGL/GLX/X11
  317. glXCreateContextAttribsARBProc glXCreateContextAttribsARB = 0;
  318. glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)
  319. glXGetProcAddressARB( (const GLubyte *) "glXCreateContextAttribsARB" );
  320. GLXContext ctx = 0;
  321. // Install an X error handler so the application won't exit if GL 3.0
  322. // context allocation fails.
  323. //
  324. // Note this error handler is global. All display connections in all threads
  325. // of a process use the same error handler, so be sure to guard against other
  326. // threads issuing X commands while this code is running.
  327. ctxErrorOccurred = false;
  328. int (*oldHandler)(Display*, XErrorEvent*) =
  329. MyXSetErrorHandler(&ctxErrorHandler);
  330. // Check for the GLX_ARB_create_context extension string and the function.
  331. // If either is not present, use GLX 1.3 context creation method.
  332. if ( !isExtensionSupported( glxExts, "GLX_ARB_create_context" ) ||
  333. !glXCreateContextAttribsARB )
  334. {
  335. printf( "glXCreateContextAttribsARB() not found"
  336. " ... using old-style GLX context\n" );
  337. ctx = glXCreateNewContext( m_data->m_dpy, m_data->m_bestFbc, GLX_RGBA_TYPE, 0, True );
  338. }
  339. // If it does, try to get a GL 3.0 context!
  340. else
  341. {
  342. int context_attribs[] = {
  343. GLX_CONTEXT_MAJOR_VERSION_ARB ,3,
  344. GLX_CONTEXT_MINOR_VERSION_ARB, 2,
  345. GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB,
  346. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,None
  347. };
  348. /*
  349. int context_attribs[] =
  350. {
  351. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  352. GLX_CONTEXT_MINOR_VERSION_ARB, 2,
  353. //GLX_CONTEXT_FLAGS_ARB , GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
  354. None
  355. };
  356. */
  357. printf( "Creating context\n" );
  358. ctx = glXCreateContextAttribsARB( m_data->m_dpy, m_data->m_bestFbc, 0,
  359. True, context_attribs );
  360. // Sync to ensure any errors generated are processed.
  361. MyXSync( m_data->m_dpy, False );
  362. if ( !ctxErrorOccurred && ctx )
  363. printf( "Created GL 3.0 context\n" );
  364. else
  365. {
  366. // Couldn't create GL 3.0 context. Fall back to old-style 2.x context.
  367. // When a context version below 3.0 is requested, implementations will
  368. // return the newest context version compatible with OpenGL versions less
  369. // than version 3.0.
  370. // GLX_CONTEXT_MAJOR_VERSION_ARB = 1
  371. context_attribs[1] = 1;
  372. // GLX_CONTEXT_MINOR_VERSION_ARB = 0
  373. context_attribs[3] = 0;
  374. ctxErrorOccurred = false;
  375. printf( "Failed to create GL 3.0 context"
  376. " ... using old-style GLX context\n" );
  377. ctx = glXCreateContextAttribsARB( m_data->m_dpy, m_data->m_bestFbc, 0,
  378. True, context_attribs );
  379. }
  380. }
  381. // Sync to ensure any errors generated are processed.
  382. MyXSync( m_data->m_dpy, False );
  383. // Restore the original error handler
  384. MyXSetErrorHandler( oldHandler );
  385. if ( ctxErrorOccurred || !ctx )
  386. {
  387. printf( "Failed to create an OpenGL context\n" );
  388. exit(1);
  389. }
  390. // Verifying that context is a direct context
  391. if ( ! glXIsDirect ( m_data->m_dpy, ctx ) )
  392. {
  393. printf( "Indirect GLX rendering context obtained\n" );
  394. }
  395. else
  396. {
  397. printf( "Direct GLX rendering context obtained\n" );
  398. }
  399. printf( "Making context current\n" );
  400. glXMakeCurrent( m_data->m_dpy, m_data->m_win, ctx );
  401. m_data->m_glc = ctx;
  402. } else
  403. {
  404. m_data->m_glc = glXCreateContext(m_data->m_dpy, m_data->m_vi, NULL, GL_TRUE);
  405. glXMakeCurrent(m_data->m_dpy, m_data->m_win, m_data->m_glc);
  406. }
  407. #ifdef GLEW_INIT_OPENGL11_FUNCTIONS
  408. {
  409. GLboolean res = glewOpenGL11Init();
  410. if (res==0)
  411. {
  412. printf("glewOpenGL11Init OK!\n");
  413. } else
  414. {
  415. printf("ERROR: glewOpenGL11Init failed, exiting!\n");
  416. exit(0);
  417. }
  418. }
  419. #endif //GLEW_INIT_OPENGL11_FUNCTIONS
  420. const GLubyte* ven = glGetString(GL_VENDOR);
  421. printf("GL_VENDOR=%s\n", ven);
  422. const GLubyte* ren = glGetString(GL_RENDERER);
  423. printf("GL_RENDERER=%s\n",ren);
  424. const GLubyte* ver = glGetString(GL_VERSION);
  425. printf("GL_VERSION=%s\n", ver);
  426. const GLubyte* sl = glGetString(GL_SHADING_LANGUAGE_VERSION);
  427. printf("GL_SHADING_LANGUAGE_VERSION=%s\n", sl);
  428. //Access pthreads as a workaround for a bug in Linux/Ubuntu
  429. //See https://bugs.launchpad.net/ubuntu/+source/nvidia-graphics-drivers-319/+bug/1248642
  430. int i=pthread_getconcurrency();
  431. printf("pthread_getconcurrency()=%d\n",i);
  432. // const GLubyte* ext = glGetString(GL_EXTENSIONS);
  433. // printf("GL_EXTENSIONS=%s\n", ext);
  434. }
  435. void X11OpenGLWindow::disableOpenGL()
  436. {
  437. glXMakeCurrent(m_data->m_dpy, None, NULL);
  438. glXDestroyContext(m_data->m_dpy, m_data->m_glc);
  439. }
  440. void X11OpenGLWindow::createWindow(const b3gWindowConstructionInfo& ci)
  441. {
  442. m_data->m_dpy = MyXOpenDisplay(NULL);
  443. m_data->m_glWidth = ci.m_width;
  444. m_data->m_glHeight = ci.m_height;
  445. if(m_data->m_dpy == NULL) {
  446. printf("\n\tcannot connect to X server\n\n");
  447. exit(0);
  448. }
  449. m_data->m_root = DefaultRootWindow(m_data->m_dpy);
  450. #ifdef GLEW_DYNAMIC_LOAD_ALL_GLX_FUNCTIONS
  451. GLboolean res = glewXInit();
  452. if (res==0)
  453. {
  454. printf("glewXInit OK\n");
  455. } else
  456. {
  457. printf("glewXInit failed, exit\n");
  458. exit(0);
  459. }
  460. #endif //GLEW_DYNAMIC_LOAD_ALL_GLX_FUNCTIONS
  461. if (ci.m_openglVersion < 3)
  462. {
  463. forceOpenGL3 = false;
  464. }
  465. if (forceOpenGL3)
  466. {
  467. int glxMinor, glxMajor;
  468. if (!glXQueryVersion(m_data->m_dpy,&glxMajor,&glxMinor) || (((glxMajor==1)&&(glxMinor<3)) || (glxMajor<1)))
  469. {
  470. printf("Invalid GLX version: major %d, minor %d\n",glxMajor,glxMinor);
  471. exit(0);
  472. }
  473. static int visual_attribs[] =
  474. {
  475. GLX_X_RENDERABLE , True,
  476. GLX_DRAWABLE_TYPE , GLX_WINDOW_BIT,
  477. GLX_RENDER_TYPE , GLX_RGBA_BIT,
  478. GLX_X_VISUAL_TYPE , GLX_TRUE_COLOR,
  479. GLX_RED_SIZE , 8,
  480. GLX_GREEN_SIZE , 8,
  481. GLX_BLUE_SIZE , 8,
  482. GLX_ALPHA_SIZE , 8,
  483. GLX_DEPTH_SIZE , 24,
  484. GLX_STENCIL_SIZE , 8,
  485. GLX_DOUBLEBUFFER , True,
  486. None
  487. };
  488. int fbcount;
  489. GLXFBConfig* fbc = glXChooseFBConfig(m_data->m_dpy, DefaultScreen(m_data->m_dpy), visual_attribs, &fbcount);
  490. if (!fbc)
  491. {
  492. printf( "Failed to retrieve a framebuffer config\n" );
  493. exit(1);
  494. }
  495. ///don't use highest samples, it is really slow on some NVIDIA Quadro cards
  496. #ifdef USE_HIGHEST_SAMPLES
  497. int best_fbc = -1, worst_fbc = -1, best_num_samp = -1, worst_num_samp = 999;
  498. int i;
  499. for (i=0; i<fbcount; ++i)
  500. {
  501. XVisualInfo *vi = glXGetVisualFromFBConfig( m_data->m_dpy, fbc[i] );
  502. if ( vi )
  503. {
  504. int samp_buf, samples;
  505. glXGetFBConfigAttrib( m_data->m_dpy, fbc[i], GLX_SAMPLE_BUFFERS, &samp_buf );
  506. glXGetFBConfigAttrib( m_data->m_dpy, fbc[i], GLX_SAMPLES , &samples );
  507. //printf( " Matching fbconfig %d, visual ID 0x%2x: SAMPLE_BUFFERS = %d,"
  508. // " SAMPLES = %d\n",
  509. // i, vi -> visualid, samp_buf, samples );
  510. if ( best_fbc < 0 || (samp_buf && (samples > best_num_samp)) )
  511. best_fbc = i, best_num_samp = samples;
  512. if ( worst_fbc < 0 || (!samp_buf || (samples < worst_num_samp)) )
  513. worst_fbc = i, worst_num_samp = samples;
  514. }
  515. MyXFree( vi );
  516. }
  517. m_data->m_bestFbc = fbc[ best_fbc ];
  518. #else
  519. m_data->m_bestFbc = *fbc;
  520. #endif
  521. // Be sure to free the FBConfig list allocated by glXChooseFBConfig()
  522. MyXFree( fbc );
  523. m_data->m_vi = glXGetVisualFromFBConfig( m_data->m_dpy, m_data->m_bestFbc );
  524. m_data->m_swa.colormap = m_data->m_cmap = MyXCreateColormap( m_data->m_dpy,
  525. RootWindow( m_data->m_dpy, m_data->m_vi->screen ),
  526. m_data->m_vi->visual, AllocNone );
  527. m_data->m_swa.background_pixmap = None ;
  528. m_data->m_swa.border_pixel = 0;
  529. m_data->m_swa.event_mask = ExposureMask | KeyReleaseMask | KeyPressMask |ButtonPressMask | ButtonReleaseMask |PointerMotionMask|StructureNotifyMask;
  530. ;
  531. m_data->m_root = RootWindow( m_data->m_dpy, m_data->m_vi->screen );
  532. m_data->m_win = MyXCreateWindow( m_data->m_dpy, m_data->m_root,
  533. 0, 0, ci.m_width, ci.m_height, 0, m_data->m_vi->depth, InputOutput,
  534. m_data->m_vi->visual,
  535. CWBorderPixel|CWColormap|CWEventMask, &m_data->m_swa );
  536. //m_data->m_win = m_data->m_x11_XCreateWindow(m_data->m_dpy, m_data->m_root, 0, 0, ci.m_width, ci.m_height, 0, m_data->m_vi->depth, InputOutput, m_data->m_vi->visual, CWColormap | CWEventMask, &m_data->m_swa);
  537. if (!m_data->m_win)
  538. {
  539. printf("Cannot create window\n");
  540. exit(0);
  541. }
  542. MyXMapWindow(m_data->m_dpy, m_data->m_win);
  543. MyXStoreName(m_data->m_dpy, m_data->m_win, "OpenGL3 Window");
  544. } else
  545. {
  546. m_data->m_vi = glXChooseVisual(m_data->m_dpy, 0, att);
  547. printf("4\n");
  548. if(m_data->m_vi == NULL) {
  549. printf("\n\tno appropriate visual found\n\n");
  550. exit(0);
  551. }
  552. else {
  553. printf("\n\tvisual %p selected\n", (void *)m_data->m_vi->visualid); /* %p creates hexadecimal output like in glxinfo */
  554. }
  555. m_data->m_cmap = MyXCreateColormap(m_data->m_dpy, m_data->m_root, m_data->m_vi->visual, AllocNone);
  556. m_data->m_swa.colormap = m_data->m_cmap;
  557. m_data->m_swa.event_mask = ExposureMask | KeyReleaseMask | KeyPressMask |ButtonPressMask | ButtonReleaseMask |PointerMotionMask|StructureNotifyMask;
  558. m_data->m_win = MyXCreateWindow(m_data->m_dpy, m_data->m_root, 0, 0, ci.m_width, ci.m_height, 0, m_data->m_vi->depth, InputOutput, m_data->m_vi->visual, CWColormap | CWEventMask, &m_data->m_swa);
  559. MyXMapWindow(m_data->m_dpy, m_data->m_win);
  560. MyXStoreName(m_data->m_dpy, m_data->m_win, "OpenGL2 Window");
  561. }
  562. enableOpenGL();
  563. }
  564. void X11OpenGLWindow::closeWindow()
  565. {
  566. disableOpenGL();
  567. MyXDestroyWindow(m_data->m_dpy, m_data->m_win);
  568. MyXCloseDisplay(m_data->m_dpy);
  569. }
  570. int X11OpenGLWindow::getAsciiCodeFromVirtualKeycode(int keycode)
  571. {
  572. int result = 0;
  573. KeySym key, key_lc, key_uc;
  574. int keysyms_per_keycode_return;
  575. KeySym *keysym = MyXGetKeyboardMapping(m_data->m_dpy,
  576. keycode,
  577. 1,
  578. &keysyms_per_keycode_return);
  579. key = keysym[0];
  580. //key = MyXKeycodeToKeysym( m_data->m_dpy, keycode, 0 );
  581. switch( key )
  582. {
  583. case XK_Escape: return B3G_ESCAPE;
  584. case XK_Return: return B3G_RETURN;
  585. case XK_Control_L:
  586. case XK_Control_R: {
  587. return B3G_CONTROL;
  588. }
  589. case XK_Left: return B3G_LEFT_ARROW;
  590. case XK_Right: return B3G_RIGHT_ARROW;
  591. case XK_Up: return B3G_UP_ARROW;
  592. case XK_Down: return B3G_DOWN_ARROW;
  593. case XK_Alt_L:
  594. case XK_Alt_R:
  595. {
  596. return B3G_ALT;
  597. }
  598. case XK_Shift_L:
  599. case XK_Shift_R: return B3G_SHIFT;
  600. case XK_F1: return B3G_F1;
  601. case XK_F2: return B3G_F2;
  602. case XK_F3: return B3G_F3;
  603. case XK_F4: return B3G_F4;
  604. case XK_F5: return B3G_F5;
  605. case XK_F6: return B3G_F6;
  606. case XK_F7: return B3G_F7;
  607. case XK_F8: return B3G_F8;
  608. case XK_F9: return B3G_F9;
  609. case XK_F10: return B3G_F10;
  610. case XK_F11: return B3G_F11;
  611. case XK_F12: return B3G_F12;
  612. case XK_F13: return B3G_F13;
  613. case XK_F14: return B3G_F14;
  614. case XK_F15: return B3G_F15;
  615. default:
  616. // Make lowercase
  617. MyXConvertCase( key, &key_lc, &key_uc );
  618. key = key_lc;
  619. // Valid ISO 8859-1 character?
  620. if( (key >= 32 && key <= 126) ||(key >= 160 && key <= 255) )
  621. {
  622. return (int) key;
  623. }
  624. result = -1;
  625. }
  626. MyXFree(keysym);
  627. return result;
  628. }
  629. bool X11OpenGLWindow::isModifierKeyPressed(int key)
  630. {
  631. bool isPressed = false;
  632. switch (key)
  633. {
  634. case B3G_ALT:
  635. {
  636. isPressed = ((m_data->m_modifierFlags & MY_X11_ALT_KEY)!=0);
  637. break;
  638. };
  639. case B3G_SHIFT:
  640. {
  641. isPressed = ((m_data->m_modifierFlags & MY_X11_SHIFT_KEY)!=0);
  642. break;
  643. };
  644. case B3G_CONTROL:
  645. {
  646. isPressed = ((m_data->m_modifierFlags & MY_X11_CONTROL_KEY )!=0);
  647. break;
  648. };
  649. default:
  650. {
  651. }
  652. };
  653. return isPressed;
  654. }
  655. void X11OpenGLWindow::pumpMessage()
  656. {
  657. int buttonState = 1;
  658. // Process all pending events
  659. while( MyXPending( m_data->m_dpy ) )
  660. {
  661. MyXNextEvent(m_data->m_dpy, &m_data->m_xev);
  662. // printf("#");
  663. // fflush(stdout);
  664. switch( m_data->m_xev.type )
  665. {
  666. case KeyPress:
  667. {
  668. int keycode = getAsciiCodeFromVirtualKeycode(m_data->m_xev.xkey.keycode);
  669. switch (keycode)
  670. {
  671. case B3G_ALT:
  672. m_data->m_modifierFlags |= MY_X11_ALT_KEY;
  673. break;
  674. case B3G_SHIFT:
  675. m_data->m_modifierFlags |= MY_X11_SHIFT_KEY;
  676. break;
  677. case B3G_CONTROL:
  678. m_data->m_modifierFlags |= MY_X11_CONTROL_KEY;
  679. break;
  680. default:
  681. {}
  682. };
  683. if (m_data->m_keyboardCallback)
  684. {
  685. int state = 1;
  686. (*m_data->m_keyboardCallback)(keycode,state);
  687. // printf("keycode %d",keycode);
  688. // fflush(stdout);
  689. }
  690. break;
  691. }
  692. case KeyRelease:
  693. {
  694. // fflush(stdout);
  695. int keycode = getAsciiCodeFromVirtualKeycode( m_data->m_xev.xkey.keycode);
  696. switch (keycode)
  697. {
  698. case B3G_ALT:
  699. m_data->m_modifierFlags &= ~MY_X11_ALT_KEY;
  700. break;
  701. case B3G_SHIFT:
  702. m_data->m_modifierFlags &= ~MY_X11_SHIFT_KEY;
  703. break;
  704. case B3G_CONTROL:
  705. m_data->m_modifierFlags &= ~MY_X11_CONTROL_KEY;
  706. break;
  707. default:
  708. {}
  709. };
  710. if (m_data->m_keyboardCallback)
  711. {
  712. #if 0
  713. unsigned short is_retriggered = 0;
  714. ///filter out keyboard repeat
  715. //see http://stackoverflow.com/questions/2100654/ignore-auto-repeat-in-x11-applications
  716. if (MyXEventsQueued(m_data->m_dpy, QueuedAfterReading))
  717. {
  718. XEvent nev;
  719. MyXPeekEvent(m_data->m_dpy, &nev);
  720. if (nev.type == KeyPress && nev.xkey.time == m_data->m_xev.xkey.time &&
  721. nev.xkey.keycode == m_data->m_xev.xkey.keycode)
  722. {
  723. fprintf (stdout, "key #%ld was retriggered.\n",
  724. (long) MyXLookupKeysym(&nev.xkey, 0));
  725. // delete retriggered KeyPress event
  726. MyXNextEvent(m_data->m_dpy, & m_data->m_xev);
  727. is_retriggered = 1;
  728. }
  729. }
  730. #endif
  731. int state = 0;
  732. (*m_data->m_keyboardCallback)(keycode,state);
  733. }
  734. break;
  735. }
  736. case ButtonRelease:
  737. buttonState = 0;
  738. //continue with ButtonPress code
  739. case ButtonPress:
  740. {
  741. // printf("!");
  742. // fflush(stdout);
  743. int button=-1;
  744. switch (m_data->m_xev.xbutton.button)
  745. {
  746. case Button1:
  747. {
  748. button=0;
  749. break;
  750. }
  751. case Button2:
  752. {
  753. button=1;
  754. break;
  755. }
  756. case Button3:
  757. {
  758. button=2;
  759. break;
  760. }
  761. case Button4:
  762. {
  763. if (m_data->m_wheelCallback)
  764. {
  765. (*m_data->m_wheelCallback)(0,10);
  766. }
  767. break;
  768. }
  769. case Button5:
  770. {
  771. if (m_data->m_wheelCallback)
  772. {
  773. (*m_data->m_wheelCallback)(0,-10);
  774. }
  775. break;
  776. }
  777. }
  778. int xpos = m_data->m_xev.xmotion.x;
  779. int ypos = m_data->m_xev.xmotion.y;
  780. if (button>=0 && m_data->m_mouseButtonCallback)
  781. {
  782. // printf("xpos = %d, ypos = %d\n",xpos,ypos);
  783. (*m_data->m_mouseButtonCallback)(button,buttonState,xpos,ypos);
  784. }
  785. break;
  786. }
  787. case MotionNotify:
  788. {
  789. // printf("!");
  790. // fflush(0);
  791. if (m_data->m_mouseMoveCallback)
  792. {
  793. int xpos = m_data->m_xev.xmotion.x;
  794. int ypos = m_data->m_xev.xmotion.y;
  795. (*m_data->m_mouseMoveCallback)(xpos,ypos);
  796. }
  797. break;
  798. }
  799. case ConfigureNotify:
  800. {
  801. // printf("@");
  802. // fflush(0);
  803. m_data->m_glWidth = m_data->m_xev.xconfigure.width;
  804. m_data->m_glHeight = m_data->m_xev.xconfigure.height;
  805. if (m_data->m_resizeCallback)
  806. {
  807. (*m_data->m_resizeCallback)(m_data->m_xev.xconfigure.width,m_data->m_xev.xconfigure.height);
  808. }
  809. break;
  810. }
  811. case ClientMessage:
  812. {
  813. // printf("?");
  814. // fflush(stdout);
  815. break;
  816. }
  817. case Expose:
  818. {
  819. break;
  820. }
  821. case DestroyNotify:
  822. {
  823. break;
  824. }
  825. default:
  826. {
  827. //XRRUpdateConfiguration( &event );
  828. }
  829. };
  830. }
  831. }
  832. void X11OpenGLWindow::startRendering()
  833. {
  834. pumpMessage();
  835. MyXGetWindowAttributes(m_data->m_dpy, m_data->m_win, &m_data->m_gwa);
  836. glViewport(0, 0, m_data->m_gwa.width, m_data->m_gwa.height);
  837. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); //clear buffers
  838. //glCullFace(GL_BACK);
  839. //glFrontFace(GL_CCW);
  840. glEnable(GL_DEPTH_TEST);
  841. }
  842. void X11OpenGLWindow::renderAllObjects()
  843. {
  844. }
  845. void X11OpenGLWindow::endRendering()
  846. {
  847. glXSwapBuffers(m_data->m_dpy, m_data->m_win);
  848. }
  849. void X11OpenGLWindow::runMainLoop()
  850. {
  851. }
  852. float X11OpenGLWindow::getTimeInSeconds()
  853. {
  854. return 0.f;
  855. }
  856. bool X11OpenGLWindow::requestedExit() const
  857. {
  858. return m_requestedExit;
  859. }
  860. void X11OpenGLWindow::setRequestExit()
  861. {
  862. m_requestedExit=true;
  863. }
  864. void X11OpenGLWindow::setRenderCallback( b3RenderCallback renderCallback)
  865. {
  866. }
  867. void X11OpenGLWindow::setWindowTitle(const char* title)
  868. {
  869. MyXStoreName(m_data->m_dpy, m_data->m_win, title);
  870. }
  871. void X11OpenGLWindow::setWheelCallback(b3WheelCallback wheelCallback)
  872. {
  873. m_data->m_wheelCallback = wheelCallback;
  874. }
  875. void X11OpenGLWindow::setMouseMoveCallback(b3MouseMoveCallback mouseCallback)
  876. {
  877. m_data->m_mouseMoveCallback = mouseCallback;
  878. }
  879. void X11OpenGLWindow::setMouseButtonCallback(b3MouseButtonCallback mouseCallback)
  880. {
  881. m_data->m_mouseButtonCallback = mouseCallback;
  882. }
  883. void X11OpenGLWindow::setResizeCallback(b3ResizeCallback resizeCallback)
  884. {
  885. if (resizeCallback && m_data->m_glWidth>0 && m_data->m_glHeight > 0)
  886. {
  887. resizeCallback(m_data->m_glWidth, m_data->m_glHeight);
  888. }
  889. m_data->m_resizeCallback = resizeCallback;
  890. }
  891. void X11OpenGLWindow::setKeyboardCallback( b3KeyboardCallback keyboardCallback)
  892. {
  893. m_data->m_keyboardCallback = keyboardCallback;
  894. }
  895. b3MouseMoveCallback X11OpenGLWindow::getMouseMoveCallback()
  896. {
  897. return m_data->m_mouseMoveCallback;
  898. }
  899. b3MouseButtonCallback X11OpenGLWindow::getMouseButtonCallback()
  900. {
  901. return m_data->m_mouseButtonCallback;
  902. }
  903. b3ResizeCallback X11OpenGLWindow::getResizeCallback()
  904. {
  905. return m_data->m_resizeCallback;
  906. }
  907. b3WheelCallback X11OpenGLWindow::getWheelCallback()
  908. {
  909. return m_data->m_wheelCallback;
  910. }
  911. b3KeyboardCallback X11OpenGLWindow::getKeyboardCallback()
  912. {
  913. return m_data->m_keyboardCallback;
  914. }
  915. int X11OpenGLWindow::getWidth() const
  916. {
  917. if (m_data)
  918. return m_data->m_glWidth;
  919. return 0;
  920. }
  921. int X11OpenGLWindow::getHeight() const
  922. {
  923. if (m_data)
  924. return m_data->m_glHeight;
  925. return 0;
  926. }
  927. #include <stdio.h>
  928. int X11OpenGLWindow::fileOpenDialog(char* filename, int maxNameLength)
  929. {
  930. int len = 0;
  931. FILE * output = popen("zenity --file-selection --file-filter=\"*.urdf\" --file-filter=\"*.sdf\" --file-filter=\"*.obj\" --file-filter=\"*.*\"","r");
  932. if (output)
  933. {
  934. while( fgets(filename, maxNameLength-1, output) != NULL )
  935. {
  936. len=strlen(filename);
  937. if (len>0)
  938. {
  939. filename[len-1]=0;
  940. printf("file open (length=%d) = %s\n", len,filename);
  941. }
  942. }
  943. pclose(output);
  944. } else
  945. {
  946. printf("Error: fileOpenDialog no popen output, perhaps install zenity?\n");
  947. }
  948. MyXRaiseWindow(m_data->m_dpy, m_data->m_win);
  949. return len;
  950. }