2002-11-16 05:00:14 +00:00
|
|
|
/* -*- mode:ObjC -*-
|
|
|
|
XGGLContext - backend implementation of NSOpenGLContext
|
|
|
|
|
|
|
|
Copyright (C) 1998,2002 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Frederic De Jaeger
|
|
|
|
Date: Nov 2002
|
|
|
|
|
|
|
|
This file is part of the GNU Objective C User Interface Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-10-29 23:25:10 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
2002-11-16 05:00:14 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-10 04:12:46 +00:00
|
|
|
version 2 of the License, or (at your option) any later version.
|
2007-10-29 23:25:10 +00:00
|
|
|
|
2002-11-16 05:00:14 +00:00
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2007-10-29 23:25:10 +00:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; see the file COPYING.LIB.
|
|
|
|
If not, see <http://www.gnu.org/licenses/> or write to the
|
|
|
|
Free Software Foundation, 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
2002-11-16 05:00:14 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#ifdef HAVE_GLX
|
|
|
|
#include <Foundation/NSDebug.h>
|
|
|
|
#include <Foundation/NSException.h>
|
2003-07-31 23:57:11 +00:00
|
|
|
#include <GNUstepGUI/GSDisplayServer.h>
|
2002-11-16 05:00:14 +00:00
|
|
|
#include <AppKit/NSView.h>
|
|
|
|
#include <AppKit/NSWindow.h>
|
|
|
|
#include "x11/XGServerWindow.h"
|
|
|
|
#include "x11/XGOpenGL.h"
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
|
|
|
|
//FIXME
|
|
|
|
//should I store the display ?
|
|
|
|
#define MAKE_DISPLAY(dpy) Display *dpy;\
|
|
|
|
dpy = [(XGServer *)GSCurrentServer() xDisplay];\
|
|
|
|
NSAssert(dpy != NULL, NSInternalInconsistencyException)
|
|
|
|
|
|
|
|
@interface XGXSubWindow : NSObject
|
|
|
|
{
|
2008-01-21 21:27:58 +00:00
|
|
|
@public
|
|
|
|
Window xwindowid;
|
|
|
|
NSView *attached;
|
2002-11-16 05:00:14 +00:00
|
|
|
}
|
2008-01-21 21:27:58 +00:00
|
|
|
|
|
|
|
+ subwindowOnView:(NSView *)view visualinfo:(XVisualInfo *)xVisualInfo;
|
|
|
|
|
2002-11-16 05:00:14 +00:00
|
|
|
- (void) update;
|
2008-01-21 21:27:58 +00:00
|
|
|
|
2002-11-16 05:00:14 +00:00
|
|
|
@end
|
|
|
|
|
2008-01-21 21:27:58 +00:00
|
|
|
@implementation XGXSubWindow
|
|
|
|
|
|
|
|
//We assume that the current context is the same and is an XGServer
|
|
|
|
- initWithView:(NSView *)view visualinfo:(XVisualInfo *)xVisualInfo
|
2002-11-16 05:00:14 +00:00
|
|
|
{
|
|
|
|
NSRect rect;
|
|
|
|
gswindow_device_t *win_info;
|
|
|
|
XGServer *server;
|
2008-01-21 21:27:58 +00:00
|
|
|
NSWindow *window;
|
2002-11-16 05:00:14 +00:00
|
|
|
int x, y, width, height;
|
2008-01-21 21:27:58 +00:00
|
|
|
int mask;
|
|
|
|
XSetWindowAttributes window_attributes;
|
|
|
|
|
|
|
|
self = [super init];
|
|
|
|
if (!self)
|
|
|
|
return nil;
|
2002-11-16 05:00:14 +00:00
|
|
|
|
2008-01-21 21:27:58 +00:00
|
|
|
window = [view window];
|
|
|
|
NSAssert(window, @"request of an X window attachment on a view that is not on a NSWindow");
|
2002-11-16 05:00:14 +00:00
|
|
|
|
2005-11-16 11:40:31 +00:00
|
|
|
if ([view isRotatedOrScaledFromBase])
|
2008-01-21 21:27:58 +00:00
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Cannot attach an Xwindow to a view that is rotated or scaled"];
|
|
|
|
}
|
2002-11-16 05:00:14 +00:00
|
|
|
|
2008-01-21 21:27:58 +00:00
|
|
|
server = (XGServer *)GSServerForWindow(window);
|
2002-11-16 05:00:14 +00:00
|
|
|
NSAssert(server != nil, NSInternalInconsistencyException);
|
|
|
|
|
|
|
|
NSAssert([server isKindOfClass: [XGServer class]],
|
2008-01-21 21:27:58 +00:00
|
|
|
NSInternalInconsistencyException);
|
2002-11-16 05:00:14 +00:00
|
|
|
|
2008-01-21 21:27:58 +00:00
|
|
|
win_info = [XGServer _windowWithTag: [window windowNumber]];
|
2002-11-16 05:00:14 +00:00
|
|
|
NSAssert(win_info, NSInternalInconsistencyException);
|
|
|
|
|
2007-01-14 17:03:44 +00:00
|
|
|
if ([server handlesWindowDecorations] == YES)
|
|
|
|
{
|
|
|
|
/* The window manager handles window decorations, so the
|
|
|
|
* the parent X window is equal to the content view and
|
|
|
|
* we must therefore use content view coordinates.
|
|
|
|
*/
|
|
|
|
rect = [view convertRect: [view bounds]
|
2008-01-21 21:27:58 +00:00
|
|
|
toView: [[view window] contentView]];
|
2007-01-14 17:03:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The GUI library handles window decorations, so the
|
|
|
|
* the parent X window is equal to the NSWindow frame
|
|
|
|
* and we can use window base coordinates.
|
|
|
|
*/
|
|
|
|
rect = [view convertRect: [view bounds] toView: nil];
|
|
|
|
}
|
2002-11-16 05:00:14 +00:00
|
|
|
|
|
|
|
x = NSMinX(rect);
|
|
|
|
y = NSHeight(win_info->xframe) - NSMaxY(rect);
|
|
|
|
width = NSWidth(rect);
|
|
|
|
height = NSHeight(rect);
|
|
|
|
|
2008-01-21 21:27:58 +00:00
|
|
|
window_attributes.border_pixel = 255;
|
|
|
|
window_attributes.colormap = XCreateColormap(win_info->display,
|
|
|
|
win_info->ident,
|
|
|
|
xVisualInfo->visual, AllocNone);
|
|
|
|
window_attributes.event_mask = StructureNotifyMask;
|
2002-11-16 05:00:14 +00:00
|
|
|
|
2008-01-21 21:27:58 +00:00
|
|
|
mask = CWBorderPixel | CWColormap | CWEventMask;
|
2002-11-16 05:00:14 +00:00
|
|
|
|
2008-01-21 21:27:58 +00:00
|
|
|
xwindowid = XCreateWindow(win_info->display, win_info->ident,
|
|
|
|
x, y, width, height, 0,
|
2009-02-08 18:36:25 +00:00
|
|
|
xVisualInfo->depth, InputOutput, xVisualInfo->visual,
|
2008-01-21 21:27:58 +00:00
|
|
|
mask, &window_attributes);
|
2002-11-16 05:00:14 +00:00
|
|
|
|
2008-01-21 21:27:58 +00:00
|
|
|
XMapWindow(win_info->display, xwindowid);
|
2002-11-16 05:00:14 +00:00
|
|
|
|
|
|
|
attached = view;
|
2008-01-21 21:27:58 +00:00
|
|
|
|
2002-11-16 05:00:14 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) map
|
|
|
|
{
|
|
|
|
MAKE_DISPLAY(dpy);
|
2008-01-21 21:27:58 +00:00
|
|
|
XMapWindow(dpy, xwindowid);
|
2002-11-16 05:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) detach
|
|
|
|
{
|
|
|
|
//FIXME
|
|
|
|
//I assume that the current server is correct.
|
|
|
|
MAKE_DISPLAY(dpy);
|
|
|
|
attached = nil;
|
2008-01-21 21:27:58 +00:00
|
|
|
XDestroyWindow(dpy, xwindowid);
|
2002-11-16 05:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) update
|
|
|
|
{
|
|
|
|
NSRect rect;
|
|
|
|
gswindow_device_t *win_info;
|
|
|
|
GSDisplayServer *server;
|
|
|
|
NSWindow *win;
|
|
|
|
int x, y, width, height;
|
2008-01-21 21:27:58 +00:00
|
|
|
|
2002-11-16 05:00:14 +00:00
|
|
|
NSAssert(attached, NSInternalInconsistencyException);
|
|
|
|
|
|
|
|
win = [attached window];
|
|
|
|
NSAssert1(win, @"%@'s window is nil now!", attached);
|
|
|
|
|
|
|
|
NSAssert1(![attached isRotatedOrScaledFromBase],
|
|
|
|
@"%@ is rotated or scaled, now!", attached);
|
|
|
|
|
|
|
|
server = GSServerForWindow(win);
|
|
|
|
NSAssert(server != nil, NSInternalInconsistencyException);
|
|
|
|
|
|
|
|
NSAssert([server isKindOfClass: [XGServer class]],
|
|
|
|
NSInternalInconsistencyException);
|
|
|
|
|
|
|
|
//FIXME
|
|
|
|
//we should check that the window hasn't changed, maybe.
|
|
|
|
|
|
|
|
win_info = [XGServer _windowWithTag: [win windowNumber]];
|
|
|
|
NSAssert(win_info, NSInternalInconsistencyException);
|
|
|
|
|
2007-01-14 18:26:14 +00:00
|
|
|
if ([server handlesWindowDecorations] == YES)
|
|
|
|
{
|
|
|
|
/* The window manager handles window decorations, so the
|
|
|
|
* the parent X window is equal to the content view and
|
|
|
|
* we must therefore use content view coordinates.
|
|
|
|
*/
|
|
|
|
rect = [attached convertRect: [attached bounds]
|
|
|
|
toView: [[attached window] contentView]];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The GUI library handles window decorations, so the
|
|
|
|
* the parent X window is equal to the NSWindow frame
|
|
|
|
* and we can use window base coordinates.
|
|
|
|
*/
|
|
|
|
rect = [attached convertRect: [attached bounds] toView: nil];
|
|
|
|
}
|
2002-11-16 05:00:14 +00:00
|
|
|
|
|
|
|
x = NSMinX(rect);
|
|
|
|
y = NSHeight(win_info->xframe) - NSMaxY(rect);
|
|
|
|
width = NSWidth(rect);
|
|
|
|
height = NSHeight(rect);
|
|
|
|
|
|
|
|
|
2008-01-21 21:27:58 +00:00
|
|
|
XMoveResizeWindow(win_info->display, xwindowid,x, y, width, height);
|
2002-11-16 05:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
NSDebugMLLog(@"GLX", @"deallocating");
|
|
|
|
[self detach];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2008-01-21 21:27:58 +00:00
|
|
|
+ subwindowOnView:(NSView *)view visualinfo:(XVisualInfo *)xVisualInfo
|
2002-11-16 05:00:14 +00:00
|
|
|
{
|
2008-01-21 21:27:58 +00:00
|
|
|
XGXSubWindow *win = [[self alloc] initWithView: view visualinfo: xVisualInfo];
|
2002-11-16 05:00:14 +00:00
|
|
|
|
|
|
|
return AUTORELEASE(win);
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
//FIXME:
|
|
|
|
//should be on per thread basis.
|
|
|
|
static XGGLContext *currentGLContext;
|
|
|
|
|
|
|
|
|
|
|
|
@implementation XGGLContext
|
|
|
|
|
|
|
|
+ (void)clearCurrentContext
|
|
|
|
{
|
|
|
|
MAKE_DISPLAY(dpy);
|
|
|
|
|
2008-01-21 21:27:58 +00:00
|
|
|
if (GSglxMinorVersion(dpy) >= 3)
|
|
|
|
{
|
|
|
|
glXMakeContextCurrent(dpy, None, None, NULL);
|
|
|
|
}
|
2003-07-23 03:40:47 +00:00
|
|
|
else
|
2008-01-21 21:27:58 +00:00
|
|
|
{
|
|
|
|
glXMakeCurrent(dpy, None, NULL);
|
|
|
|
}
|
2003-07-23 03:40:47 +00:00
|
|
|
|
2002-11-16 05:00:14 +00:00
|
|
|
currentGLContext = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSOpenGLContext *)currentContext
|
|
|
|
{
|
|
|
|
return currentGLContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _detach
|
|
|
|
{
|
2008-01-21 21:27:58 +00:00
|
|
|
if (xSubWindow)
|
2002-11-16 05:00:14 +00:00
|
|
|
{
|
|
|
|
MAKE_DISPLAY(dpy);
|
2008-01-21 21:27:58 +00:00
|
|
|
|
2005-11-16 11:40:31 +00:00
|
|
|
if (currentGLContext == self)
|
2008-01-21 21:27:58 +00:00
|
|
|
{
|
|
|
|
[XGGLContext clearCurrentContext];
|
|
|
|
}
|
|
|
|
// FIXME:
|
2002-11-16 05:00:14 +00:00
|
|
|
// glXDestroyWindow(dpy, glx_drawable);
|
|
|
|
glx_drawable = None;
|
2008-01-21 21:27:58 +00:00
|
|
|
DESTROY(xSubWindow);
|
2002-11-16 05:00:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-21 21:27:58 +00:00
|
|
|
- (GLXContext)glxcontext
|
|
|
|
{
|
|
|
|
return glx_context;
|
|
|
|
}
|
|
|
|
|
2002-11-16 05:00:14 +00:00
|
|
|
- (void)clearDrawable
|
|
|
|
{
|
|
|
|
[self _detach];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)copyAttributesFromContext:(NSOpenGLContext *)context
|
|
|
|
withMask:(unsigned long)mask
|
|
|
|
{
|
|
|
|
MAKE_DISPLAY(dpy);
|
2008-01-21 21:27:58 +00:00
|
|
|
|
|
|
|
if (context == nil || ![context isKindOfClass: [XGGLContext class]])
|
2002-11-16 05:00:14 +00:00
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"%@ is an invalid context", context];
|
2008-01-21 21:27:58 +00:00
|
|
|
|
|
|
|
glXCopyContext(dpy, ((XGGLContext *)context)->glx_context,
|
|
|
|
glx_context, mask);
|
2002-11-16 05:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)createTexture:(unsigned long)target
|
|
|
|
fromView:(NSView*)view
|
|
|
|
internalFormat:(unsigned long)format
|
|
|
|
{
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (int)currentVirtualScreen
|
|
|
|
{
|
|
|
|
[self notImplemented: _cmd];
|
2008-01-21 21:27:58 +00:00
|
|
|
|
2002-11-16 05:00:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)flushBuffer
|
|
|
|
{
|
|
|
|
MAKE_DISPLAY(dpy);
|
|
|
|
|
|
|
|
glXSwapBuffers(dpy, glx_drawable);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)getValues:(long *)vals
|
|
|
|
forParameter:(NSOpenGLContextParameter)param
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-21 21:27:58 +00:00
|
|
|
- (id)initWithFormat: (NSOpenGLPixelFormat *)_format
|
|
|
|
shareContext: (NSOpenGLContext *)share
|
2002-11-16 05:00:14 +00:00
|
|
|
{
|
2008-01-21 21:27:58 +00:00
|
|
|
self = [super init];
|
|
|
|
if (!self)
|
|
|
|
return nil;
|
|
|
|
|
2002-11-16 05:00:14 +00:00
|
|
|
glx_context = None;
|
2002-11-17 04:53:51 +00:00
|
|
|
|
2008-01-21 21:27:58 +00:00
|
|
|
if (!_format || ![_format isKindOfClass: [XGGLPixelFormat class]])
|
2002-11-16 05:00:14 +00:00
|
|
|
{
|
|
|
|
NSDebugMLLog(@"GLX", @"invalid format %@", _format);
|
|
|
|
RELEASE(self);
|
2008-01-21 21:27:58 +00:00
|
|
|
|
2002-11-16 05:00:14 +00:00
|
|
|
return nil;
|
|
|
|
}
|
2008-01-21 21:27:58 +00:00
|
|
|
|
|
|
|
ASSIGN(pixelFormat, (XGGLPixelFormat *)_format);
|
|
|
|
|
|
|
|
//FIXME: allow index mode and sharing
|
|
|
|
glx_context = [pixelFormat createGLXContext: (XGGLContext *)share];
|
|
|
|
|
|
|
|
return self;
|
2002-11-16 05:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
NSDebugMLLog(@"GLX", @"deallocating");
|
|
|
|
[self _detach];
|
2008-01-21 21:27:58 +00:00
|
|
|
RELEASE(pixelFormat);
|
|
|
|
|
2005-11-16 11:40:31 +00:00
|
|
|
if (glx_context != None)
|
2002-11-16 05:00:14 +00:00
|
|
|
{
|
|
|
|
MAKE_DISPLAY(dpy);
|
|
|
|
glXDestroyContext(dpy, glx_context);
|
|
|
|
}
|
2008-01-21 21:27:58 +00:00
|
|
|
|
2002-11-16 05:00:14 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) makeCurrentContext
|
|
|
|
{
|
|
|
|
MAKE_DISPLAY(dpy);
|
2008-01-21 21:27:58 +00:00
|
|
|
|
|
|
|
if (xSubWindow == nil)
|
2002-11-16 05:00:14 +00:00
|
|
|
[NSException raise: NSGenericException
|
|
|
|
format: @"GL Context is not bind, cannot be made current"];
|
|
|
|
|
|
|
|
NSAssert(glx_context != None && glx_drawable != None,
|
|
|
|
NSInternalInconsistencyException);
|
|
|
|
|
2008-01-21 21:27:58 +00:00
|
|
|
if (GSglxMinorVersion(dpy) >= 3)
|
2003-07-23 03:40:47 +00:00
|
|
|
{
|
|
|
|
NSDebugMLLog(@"GLX", @"before glXMakeContextCurrent");
|
|
|
|
glXMakeContextCurrent(dpy, glx_drawable, glx_drawable, glx_context);
|
|
|
|
NSDebugMLLog(@"GLX", @"after glXMakeContextCurrent");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSDebugMLLog(@"GLX", @"before glXMakeCurrent");
|
|
|
|
glXMakeCurrent(dpy, glx_drawable, glx_context);
|
|
|
|
NSDebugMLLog(@"GLX", @"after glXMakeCurrent");
|
|
|
|
}
|
2002-11-16 05:00:14 +00:00
|
|
|
|
|
|
|
currentGLContext = self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setCurrentVirtualScreen:(int)screen
|
|
|
|
{
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setFullScreen
|
|
|
|
{
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setOffScreen:(void *)baseaddr
|
2008-01-21 21:27:58 +00:00
|
|
|
width:(long)width
|
|
|
|
height:(long)height
|
|
|
|
rowbytes:(long)rowbytes
|
2002-11-16 05:00:14 +00:00
|
|
|
{
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setValues:(const long *)vals
|
|
|
|
forParameter:(NSOpenGLContextParameter)param
|
|
|
|
{
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setView:(NSView *)view
|
|
|
|
{
|
2005-11-16 11:40:31 +00:00
|
|
|
if (!view)
|
2002-11-16 05:00:14 +00:00
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"setView called with a nil value"];
|
|
|
|
|
2008-01-21 21:27:58 +00:00
|
|
|
NSAssert(pixelFormat, NSInternalInconsistencyException);
|
2002-11-16 05:00:14 +00:00
|
|
|
|
2008-01-21 21:27:58 +00:00
|
|
|
ASSIGN(xSubWindow, [XGXSubWindow subwindowOnView: view
|
|
|
|
visualinfo: [pixelFormat xvinfo]]);
|
|
|
|
glx_drawable = [pixelFormat drawableForWindow: xSubWindow->xwindowid];
|
2002-11-16 05:00:14 +00:00
|
|
|
|
|
|
|
NSDebugMLLog(@"GLX", @"glx_window : %u", glx_drawable);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)update
|
|
|
|
{
|
2008-01-21 21:27:58 +00:00
|
|
|
[xSubWindow update];
|
2002-11-16 05:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSView *)view
|
|
|
|
{
|
2008-01-21 21:27:58 +00:00
|
|
|
if (xSubWindow)
|
|
|
|
{
|
|
|
|
return xSubWindow->attached;
|
|
|
|
}
|
2002-11-16 05:00:14 +00:00
|
|
|
else
|
2008-01-21 21:27:58 +00:00
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
2002-11-16 05:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
#endif
|