33 #ifdef G4VIS_BUILD_OPENGLX_DRIVER
50 #include <X11/Xatom.h>
51 #include <X11/Xutil.h>
52 #include <X11/Xmu/StdCmap.h>
59 int G4OpenGLXViewer::snglBuf_RGBA[12] =
68 int G4OpenGLXViewer::dblBuf_RGBA[13] =
78 #define NewString(str) \
79 ((str) != 0 ? (strncpy((char*)malloc((unsigned)strlen(str) + 1), str, (unsigned)strlen(str) + 1)) : (char*)0)
81 #define USE_DEFAULT_COLORMAP 1
82 #define USE_STANDARD_COLORMAP 0
84 XVisualInfo* G4OpenGLXViewer::vi_single_buffer = 0;
85 XVisualInfo* G4OpenGLXViewer::vi_double_buffer = 0;
88 static Bool G4OpenGLXViewerWaitForNotify (Display*, XEvent*
e,
char* arg) {
89 return (e->type == MapNotify) && (e->xmap.window == (
Window) arg);
93 void G4OpenGLXViewer::SetView () {
94 #ifdef G4MULTITHREADED
96 glXMakeCurrent (dpy, win, cxMaster);
98 glXMakeCurrent (dpy, win, cxVisSubThread);
101 glXMakeCurrent (dpy, win, cxMaster);
103 G4OpenGLViewer::SetView ();
106 void G4OpenGLXViewer::ShowView () {
107 #ifdef G4MULTITHREADED
115 if (fVP.IsPicking()) {
117 "Window activated for picking (left-mouse), exit (middle-mouse)."
121 XNextEvent(dpy, &event);
122 if (event.type == ButtonPress && event.xbutton.button == 1) {
123 G4cout << Pick(event.xbutton.x, event.xbutton.y) <<
G4endl;
125 else if (event.type == ButtonPress && event.xbutton.button == 2)
break;
127 std::this_thread::sleep_for(std::chrono::milliseconds(100));
132 #ifdef G4MULTITHREADED
134 void G4OpenGLXViewer::SwitchToVisSubThread()
137 cxVisSubThread = glXCreateContext (dpy, vi, cxMaster,
true);
138 glXMakeCurrent (dpy, win, cxVisSubThread);
141 void G4OpenGLXViewer::SwitchToMasterThread()
144 glXMakeCurrent (dpy, win, cxMaster);
146 glXDestroyContext (dpy, cxVisSubThread);
151 void G4OpenGLXViewer::GetXConnection () {
153 dpy = XOpenDisplay (0);
156 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer couldn't open display." <<
G4endl;
161 if (!glXQueryExtension (dpy, &errorBase, &eventBase)) {
163 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer X Server has no GLX extension."
170 void G4OpenGLXViewer::CreateGLXContext (XVisualInfo*
v) {
174 if (!XGetWindowAttributes(dpy, XRootWindow (dpy, vi -> screen), &xwa)) {
176 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer couldn't return window attributes"
182 cxMaster = glXCreateContext (dpy, vi, 0,
true);
185 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer couldn't create context."
195 status = XmuLookupStandardColormap (dpy,
205 XStandardColormap* standardCmaps = XAllocStandardColormap ();
206 status = XGetRGBColormaps (dpy,
207 XRootWindow (dpy, vi -> screen),
212 for (i = 0; i < numCmaps; i++) {
213 if (standardCmaps[i].visualid == vi -> visualid) {
214 cmap = standardCmaps[i].colormap;
219 XFree (standardCmaps);
227 cmap = XCreateColormap (dpy,
228 XRootWindow(dpy, vi -> screen),
238 cmap = XCreateColormap (dpy,
239 XRootWindow(dpy, vi -> screen),
252 G4cout <<
"G4OpenGLXViewer::G4OpenGLXViewer failed to allocate a Colormap."
259 void G4OpenGLXViewer::CreateMainWindow () {
263 swa.border_pixel = 0;
264 swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
265 swa.backing_store = WhenMapped;
268 size_hints = XAllocSizeHints();
270 ResizeWindow(fVP.GetWindowSizeHintX(),fVP.GetWindowSizeHintY());
272 G4int x_origin = fVP.GetWindowAbsoluteLocationHintX(DisplayWidth(dpy, vi -> screen));
276 G4int y_origin = fVP.GetWindowAbsoluteLocationHintY(DisplayHeight(dpy, vi -> screen));
278 size_hints->base_width = getWinWidth();
279 size_hints->base_height = getWinHeight();
280 size_hints->x = x_origin;
281 size_hints->y = y_origin;
282 if (fVP.IsWindowSizeHintX () && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
283 size_hints->flags |= PSize | PPosition;
284 }
else if (fVP.IsWindowSizeHintX () && !(fVP.IsWindowLocationHintX () || fVP.IsWindowLocationHintY ())) {
285 size_hints->flags |= PSize;
286 }
else if ((!fVP.IsWindowSizeHintX ()) && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
287 size_hints->flags |= PPosition;
290 G4cout <<
"Window name: " <<
fName << G4endl;
291 strncpy (charViewName,
fName, 99); charViewName[99] =
'\0';
292 char *window_name = charViewName;
293 char *icon_name = charViewName;
295 wm_hints = XAllocWMHints();
296 class_hints = XAllocClassHint();
298 XStringListToTextProperty (&window_name, 1, &windowName);
299 XStringListToTextProperty (&icon_name, 1, &iconName);
301 wm_hints -> initial_state = NormalState;
302 wm_hints -> input = True;
303 wm_hints -> icon_pixmap = icon_pixmap;
304 wm_hints -> flags = StateHint | IconPixmapHint | InputHint;
306 class_hints -> res_name =
NewString(
"G4OpenGL");
307 class_hints -> res_class =
NewString(
"G4OpenGL");
309 win = XCreateWindow (dpy, XRootWindow (dpy, vi -> screen), x_origin,
310 y_origin, getWinWidth(), getWinHeight(), 0, vi -> depth,
311 InputOutput, vi -> visual,
312 CWBorderPixel | CWColormap |
313 CWEventMask | CWBackingStore,
316 XSetWMProperties (dpy, win, &windowName, &iconName, 0, 0,
317 size_hints, wm_hints, class_hints);
320 XMapWindow (dpy, win);
323 XIfEvent (dpy, &event, G4OpenGLXViewerWaitForNotify, (
char*) win);
326 Bool success = glXMakeCurrent (dpy, win, cxMaster);
329 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer failed to attach a GLX context."
331 GLint
error = GL_NO_ERROR;
332 while ((error = glGetError()) != GL_NO_ERROR) {
334 case GL_INVALID_ENUM :
336 case GL_INVALID_VALUE :
338 case GL_INVALID_OPERATION :
339 G4cout <<
"GL Error: GL_INVALID_OPERATION" <<
G4endl;
break;
340 case GL_OUT_OF_MEMORY :
342 case GL_STACK_UNDERFLOW :
343 G4cout <<
"GL Error: GL_STACK_UNDERFLOW" <<
G4endl;
break;
344 case GL_STACK_OVERFLOW :
345 G4cout <<
"GL Error: GL_STACK_OVERFLOW" <<
G4endl;
break;
354 void G4OpenGLXViewer::CreateFontLists()
356 std::map<G4double,G4String> fonts;
357 fonts[10.] =
"-adobe-courier-bold-r-normal--10-100-75-75-m-60-iso8859-1";
358 fonts[11.] =
"-adobe-courier-bold-r-normal--11-80-100-100-m-60-iso8859-1";
359 fonts[12.] =
"-adobe-courier-bold-r-normal--12-120-75-75-m-70-iso8859-1";
360 fonts[13.] =
"fixed";
361 fonts[14.] =
"-adobe-courier-bold-r-normal--14-100-100-100-m-90-iso8859-1";
362 fonts[17.] =
"-adobe-courier-bold-r-normal--17-120-100-100-m-100-iso8859-1";
363 fonts[18.] =
"-adobe-courier-bold-r-normal--18-180-75-75-m-110-iso8859-1";
364 fonts[20.] =
"-adobe-courier-bold-r-normal--20-140-100-100-m-110-iso8859-1";
365 fonts[24.] =
"-adobe-courier-bold-r-normal--24-240-75-75-m-150-iso8859-1";
366 fonts[25.] =
"-adobe-courier-bold-r-normal--25-180-100-100-m-150-iso8859-1";
367 fonts[34.] =
"-adobe-courier-bold-r-normal--34-240-100-100-m-200-iso8859-1";
368 std::map<G4double,G4String>::const_iterator i;
369 for (i = fonts.begin(); i != fonts.end(); ++i) {
370 XFontStruct* font_info = XLoadQueryFont(dpy, i->second);
373 "G4OpenGLXViewer::CreateFontLists XLoadQueryFont failed for font\n "
378 G4int font_base = glGenLists(256);
381 "G4OpenGLXViewer::CreateFontLists out of display lists for fonts."
385 G4int first = font_info->min_char_or_byte2;
386 G4int last = font_info->max_char_or_byte2;
387 glXUseXFont(font_info->fid, first, last-first+1, font_base + first);
390 (
this, font_base, i->first, i->second, width);
394 void G4OpenGLXViewer::DrawText(
const G4Text& g4text)
396 if (isGl2psWriting()) {
398 G4OpenGLViewer::DrawText(g4text);
403 G4double size = fSceneHandler.GetMarkerSize(g4text,sizeType);
408 static G4int callCount = 0;
411 if (callCount <= 1) {
413 "G4OpenGLXViewer::DrawText: No fonts available for \""
422 const G4Colour&
c = fSceneHandler.GetTextColour(g4text);
428 const char* textCString = textString.c_str();
431 glRasterPos3d(position.x(),position.y(),position.z());
433 glPushAttrib(GL_LIST_BIT);
449 glBitmap(0,0,0,0,xmove,ymove,0);
453 glCallLists(strlen(textCString),GL_UNSIGNED_BYTE,(GLubyte*)textCString);
459 G4OpenGLXViewer::G4OpenGLXViewer (G4OpenGLSceneHandler& scene):
461 G4OpenGLViewer (scene),
471 windowName.value = 0;
474 if (fViewId < 0)
return;
478 if (!vi_single_buffer) {
480 glXChooseVisual (dpy, XDefaultScreen (dpy), snglBuf_RGBA);
483 if (!vi_double_buffer) {
485 glXChooseVisual (dpy, XDefaultScreen (dpy), dblBuf_RGBA);
489 if (vi_single_buffer || vi_double_buffer) {
490 if (!vi_double_buffer) {
492 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
493 "\n Working with a single buffer."
497 if (!vi_single_buffer) {
499 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a single buffer visual."
502 if (!vi_double_buffer) {
504 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
509 if (vi_single_buffer) {
510 vi_immediate = vi_single_buffer;
511 attributeList = snglBuf_RGBA;
516 if (vi_double_buffer) {
517 vi_immediate = vi_double_buffer;
518 attributeList = dblBuf_RGBA;
524 if (vi_double_buffer) {
525 vi_stored = vi_double_buffer;
526 attributeList = dblBuf_RGBA;
529 if (!vi_immediate || !vi_stored) {
531 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get required visuals."
540 G4OpenGLXViewer::~G4OpenGLXViewer () {
543 glXMakeCurrent (dpy,
None, NULL);
544 glXDestroyContext (dpy, cxMaster);
545 if (win) XDestroyWindow (dpy, win);