Comitting EmuMogs 28008537/src.zip drop to branch

This commit is contained in:
Brian Matzon 2012-11-01 20:54:17 +00:00
parent 65009d8274
commit c09f5698b4
16 changed files with 1375 additions and 408 deletions

View file

@ -47,15 +47,65 @@
#include <OpenGL/glext.h>
#include "common_tools.h"
@class NSOpenGLContext, NSOpenGLPixelFormat, MacOSXOpenGLView, MacOSXKeyableWindow;
typedef struct {
NSOpenGLPixelFormat *pixel_format;
bool window;
MacOSXKeyableWindow *window;
NSRect display_rect;
MacOSXOpenGLView *view;
NSOpenGLContext *context;
// Native objects for Java callbacks
jobject jdisplay;
jobject jmouse;
jobject jkeyboard;
jboolean resized;
// Cached for window creation
NSApplicationPresentationOptions window_options;
} MacOSXWindowInfo;
@interface MacOSXKeyableWindow : NSWindow
- (BOOL)canBecomeKeyWindow;
@end
@interface MacOSXOpenGLView : NSView
{
@public
MacOSXWindowInfo* _parent;
@private
NSOpenGLContext* _openGLContext;
NSOpenGLPixelFormat* _pixelFormat;
NSUInteger _lastModifierFlags;
NSUInteger _modifierFlags;
}
+ (NSOpenGLPixelFormat*)defaultPixelFormat;
- (id)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat*)format;
- (void)setOpenGLContext:(NSOpenGLContext*)context;
- (NSOpenGLContext*)openGLContext;
- (void)clearGLContext;
- (void)prepareOpenGL;
- (void)update;
- (void)lockFocus;
- (void)setPixelFormat:(NSOpenGLPixelFormat*)pixelFormat;
- (NSOpenGLPixelFormat*)pixelFormat;
- (void)setParent:(MacOSXWindowInfo*)parent;
- (BOOL)acceptsFirstResponder;
@end
typedef struct {
bool isWindowed;
bool canDrawGL;
union {
NSView *nsview;
NSOpenGLPixelBuffer *pbuffer;
};
MacOSXWindowInfo *window_info;
NSOpenGLPixelFormat *pixel_format;
NSOpenGLPixelBuffer *pbuffer;
} MacOSXPeerInfo;
NSOpenGLPixelFormat *choosePixelFormat(JNIEnv *env, jobject pixel_format, bool gl32, bool use_display_bpp, bool support_window, bool support_pbuffer, bool double_buffered);
#endif

View file

@ -91,7 +91,16 @@ NSOpenGLPixelFormat *choosePixelFormat(JNIEnv *env, jobject pixel_format, bool g
int bpp;
jclass cls_pixel_format = (*env)->GetObjectClass(env, pixel_format);
if (use_display_bpp)
bpp = CGDisplayBitsPerPixel(kCGDirectMainDisplay);
{
CGDisplayModeRef mode = CGDisplayCopyDisplayMode(kCGDirectMainDisplay);
CFStringRef pixEnc = CGDisplayModeCopyPixelEncoding(mode);
if (CFStringCompare(pixEnc, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
bpp = 32;
else if(CFStringCompare(pixEnc, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
bpp = 16;
else if(CFStringCompare(pixEnc, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
bpp = 8;
}
else
bpp = (int)(*env)->GetIntField(env, pixel_format, (*env)->GetFieldID(env, cls_pixel_format, "bpp", "I"));

View file

@ -39,18 +39,433 @@
* @version $Revision$
*/
#import <AppKit/NSApplication.h>
#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
#import <jawt_md.h>
#import <jni.h>
#import <unistd.h>
//#import "display.h"
#import "common_tools.h"
#import "org_lwjgl_opengl_MacOSXDisplay.h"
#import "org_lwjgl_MacOSXSysImplementation.h"
#import "context.h"
#define WAIT_DELAY 100
static NSOpenGLPixelFormat *default_format = nil;
@implementation MacOSXKeyableWindow
- (BOOL)canBecomeKeyWindow;
{
return YES;
}
@end
@implementation MacOSXOpenGLView
+ (NSOpenGLPixelFormat*)defaultPixelFormat
{
NSOpenGLPixelFormatAttribute defaultAttribs[] = {
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 16,
NSOpenGLPFAColorSize, 32,
0
};
if (default_format == nil) {
default_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:defaultAttribs];
}
return default_format;
}
- (void) windowWillClose:(NSNotification *)notification
{
MacOSXKeyableWindow *closingWindow = [notification object];
if (_parent != nil && closingWindow == _parent->window) {
JNIEnv *env = attachCurrentThread();
jclass display_class = (*env)->GetObjectClass(env, _parent->jdisplay);
jmethodID close_callback = (*env)->GetMethodID(env, display_class, "doHandleQuit", "()V");
(*env)->CallVoidMethod(env, _parent->jdisplay, close_callback);
}
}
- (id)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat*)format
{
self = [super initWithFrame:frameRect];
_lastModifierFlags = 0;
_modifierFlags = 0;
if (self != nil) {
_pixelFormat = [format retain];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_surfaceNeedsUpdate:)
name:NSViewGlobalFrameDidChangeNotification
object:self];
}
return self;
}
- (void) _surfaceNeedsUpdate:(NSNotification*)notification
{
[self update];
}
- (void)setOpenGLContext:(NSOpenGLContext*)context
{
_openGLContext = context;
}
- (NSOpenGLContext*)openGLContext
{
return _openGLContext;
}
- (void)clearGLContext
{
[_openGLContext release];
_openGLContext = nil;
}
- (void)prepareOpenGL
{
}
- (void)update
{
[_openGLContext update];
}
- (void)lockFocus
{
NSOpenGLContext* context = [self openGLContext];
[super lockFocus];
if ([context view] != self) {
[context setView:self];
}
[context makeCurrentContext];
}
- (void)setPixelFormat:(NSOpenGLPixelFormat*)pixelFormat
{
_pixelFormat = [pixelFormat retain];
}
- (NSOpenGLPixelFormat*)pixelFormat
{
return _pixelFormat;
}
- (BOOL)acceptsFirstResponder {
return YES;
}
- (void)setParent:(MacOSXWindowInfo*)parent {
// Un-register for native window close events if we have a parent window already
if (_parent != nil) {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSWindowWillCloseNotification
object:_parent->window];
}
_parent = parent;
// Register for native window close events if we now have a parent window
if (_parent != nil) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowWillClose:) name:NSWindowWillCloseNotification
object:_parent->window];
}
}
- (void)keyDown:(NSEvent *)event {
JNIEnv *env = attachCurrentThread();
if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) {
return;
}
long time = [event timestamp] * 1000000000;
jclass keyboard_class = (*env)->GetObjectClass(env, _parent->jkeyboard);
jmethodID keydown = (*env)->GetMethodID(env, keyboard_class, "keyPressed", "(IIJ)V");
const char* charbuf = [[event characters] cStringUsingEncoding:NSASCIIStringEncoding];
int charcode = (charbuf == nil) ? 0 : charbuf[0];
(*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, [event keyCode], charcode, time);
}
- (void)keyUp:(NSEvent *)event {
JNIEnv *env = attachCurrentThread();
if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) {
return;
}
long time = [event timestamp] * 1000000000;
jclass keyboard_class = (*env)->GetObjectClass(env, _parent->jkeyboard);
jmethodID keyup = (*env)->GetMethodID(env, keyboard_class, "keyReleased", "(IIJ)V");
const char* charbuf = [[event characters] cStringUsingEncoding:NSASCIIStringEncoding];
int charcode = (charbuf == nil) ? 0 : charbuf[0];
(*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, [event keyCode], charcode, time);
}
- (void)flagsChanged:(NSEvent *)event {
JNIEnv *env = attachCurrentThread();
if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) {
return;
}
long time = [event timestamp] * 1000000000;
jclass keyboard_class = (*env)->GetObjectClass(env, _parent->jkeyboard);
jmethodID keydown = (*env)->GetMethodID(env, keyboard_class, "keyPressed", "(IIJ)V");
jmethodID keyup = (*env)->GetMethodID(env, keyboard_class, "keyReleased", "(IIJ)V");
_lastModifierFlags = _modifierFlags;
_modifierFlags = [event modifierFlags];
NSUInteger flagDown = ~_lastModifierFlags & _modifierFlags;
NSUInteger flagUp = _lastModifierFlags & ~_modifierFlags;
if (flagDown & NSAlphaShiftKeyMask) {
(*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf0, 0, time);
}
if (flagUp & NSAlphaShiftKeyMask) {
(*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf0, 0, time);
}
if (flagDown & NSShiftKeyMask) {
(*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf1, 0, time);
}
if (flagUp & NSShiftKeyMask) {
(*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf1, 0, time);
}
if (flagDown & NSControlKeyMask) {
(*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf2, 0, time);
}
if (flagUp & NSControlKeyMask) {
(*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf2, 0, time);
}
if (flagDown & NSAlternateKeyMask) {
(*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf3, 0, time);
}
if (flagUp & NSAlternateKeyMask) {
(*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf3, 0, time);
}
if (flagDown & NSCommandKeyMask) {
(*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf4, 0, time);
}
if (flagUp & NSCommandKeyMask) {
(*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf4, 0, time);
}
if (flagDown & NSNumericPadKeyMask) {
(*env)->CallVoidMethod(env, _parent->jkeyboard, keydown, 0xf5, 0, time);
}
if (flagUp & NSNumericPadKeyMask) {
(*env)->CallVoidMethod(env, _parent->jkeyboard, keyup, 0xf5, 0, time);
}
//const char* charbuf = [[event characters] cStringUsingEncoding:NSASCIIStringEncoding];
//(*env)->CallVoidMethod(env, _parent->jkeyboard, keymod, (jint)[event keyCode], (jint)charbuf[0], time);
}
- (void)mouseButtonState:(NSEvent *)event :(int)button :(int)state {
JNIEnv *env = attachCurrentThread();
if (env == nil || event == nil || _parent == nil || _parent->jkeyboard == nil) {
return;
}
long time = [event timestamp] * 1000000000;
jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse);
jmethodID mousebutton = (*env)->GetMethodID(env, mouse_class, "setButton", "(IIJ)V");
(*env)->CallVoidMethod(env, _parent->jmouse, mousebutton, button, state, time);
}
- (void)mouseDown:(NSEvent *)event {
[self mouseButtonState:event :0 :1];
}
- (void)rightMouseDown:(NSEvent *)event {
[self mouseButtonState:event :1 :1];
}
- (void)otherMouseDown:(NSEvent *)event {
[self mouseButtonState:event :2 :1];
}
- (void)mouseUp:(NSEvent *)event {
[self mouseButtonState:event :0 :0];
}
- (void)rightMouseUp:(NSEvent *)event {
[self mouseButtonState:event :1 :0];
}
- (void)otherMouseUp:(NSEvent *)event {
[self mouseButtonState:event :2 :0];
}
- (void)mouseDragged:(NSEvent *)event {
JNIEnv *env = attachCurrentThread();
if (env == nil || event == nil || _parent == nil) {
return;
}
long time = [event timestamp] * 1000000000;
jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse);
jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V");
NSPoint loc = [self convertPoint:[event locationInWindow] toView:self];
(*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time);
}
- (void)mouseMoved:(NSEvent *)event {
JNIEnv *env = attachCurrentThread();
if (env == nil || event == nil || _parent == nil || _parent->jmouse == nil) {
return;
}
long time = [event timestamp] * 1000000000;
jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse);
jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V");
NSPoint loc = [self convertPoint:[event locationInWindow] toView:self];
(*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], 0.0f, time);
}
- (void)scrollWheel:(NSEvent *)event {
JNIEnv *env = attachCurrentThread();
if (env == nil || event == nil || _parent == nil) {
return;
}
long time = [event timestamp] * 1000000000;
float dz = [event scrollingDeltaY];
if (![event hasPreciseScrollingDeltas]) {
dz *= 12; // or so
}
jclass mouse_class = (*env)->GetObjectClass(env, _parent->jmouse);
jmethodID mousemove = (*env)->GetMethodID(env, mouse_class, "mouseMoved", "(FFFFFJ)V");
NSPoint loc = [self convertPoint:[event locationInWindow] toView:self];
(*env)->CallVoidMethod(env, _parent->jmouse, mousemove, loc.x, loc.y, [event deltaX], [event deltaY], dz, time);
}
- (void)viewDidMoveToWindow
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowResized:)
name:NSWindowDidResizeNotification
object:[self window]];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (void)windowResized:(NSNotification *)notification;
{
if (_parent != nil) {
_parent->display_rect = [[self window] frame];
_parent->resized = JNI_TRUE;
}
}
@end
JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nIsMiniaturized(JNIEnv *env, jobject this, jobject window_handle) {
MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle);
return (jboolean)[window_info->window isMiniaturized];
}
JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nIsFocused(JNIEnv *env, jobject this, jobject window_handle) {
return JNI_TRUE;
//MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle);
//return (jboolean)([window_info->window isKeyWindow] || [window_info->window isMainWindow]);
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nResizeWindow(JNIEnv *env, jobject this, jobject window_handle, jint x, jint y, jint width, jint height) {
MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle);
window_info->display_rect = NSMakeRect(x, y, width, height);
[window_info->window setFrame:window_info->display_rect display:false];
[window_info->view update];
}
JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nWasResized(JNIEnv *env, jobject this, jobject window_handle) {
MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle);
jboolean was_resized = window_info->resized;
window_info->resized = JNI_FALSE;
return was_resized;
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nSetResizable(JNIEnv *env, jobject this, jobject window_handle, jboolean resizable) {
MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle);
NSUInteger style_mask = [window_info->window styleMask];
if (resizable == true) {
style_mask |= NSResizableWindowMask;
} else {
style_mask &= ~NSResizableWindowMask;
}
[window_info->window setStyleMask:style_mask];
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nDestroyWindow(JNIEnv *env, jobject this, jobject window_handle) {
MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle);
if (window_info->window != nil) {
[window_info->window close];
}
window_info->window = nil;
if (window_info->view != nil) {
[window_info->view release];
}
window_info->view = nil;
//[window_info->window release];
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nSetTitle(JNIEnv *env, jobject this, jobject window_handle, jobject title_buffer) {
MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle);
const char *title_cstr = (const char *)(*env)->GetDirectBufferAddress(env, title_buffer);
NSString *title = [[NSString alloc] initWithUTF8String:title_cstr];
[window_info->window setTitle:title];
}
JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nCreateWindow(JNIEnv *env, jobject this, jint x, jint y, jint width, jint height, jboolean fullscreen, jboolean undecorated, jobject peer_info_handle, jobject window_handle) {
if (window_handle == NULL) {
window_handle = newJavaManagedByteBuffer(env, sizeof(MacOSXWindowInfo));
if (window_handle == NULL) {
throwException(env, "Could not create handle buffer");
return NULL;
}
}
MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle);
MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle);
// Cache the necessary info for window-close callbacks into the JVM
if (window_info->jdisplay == NULL) {
window_info->jdisplay = (*env)->NewGlobalRef(env, this);
}
window_info->display_rect = NSMakeRect(x, y, width, height);
int default_window_mask = NSBorderlessWindowMask;
if (!undecorated && !fullscreen) {
printf("Resizeable\n"); fflush(stdout);
default_window_mask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask;
}
window_info->window = (MacOSXKeyableWindow*)[[NSApplication sharedApplication] mainWindow];
if (window_info->window == nil) {
window_info->window = [[MacOSXKeyableWindow alloc] initWithContentRect:window_info->display_rect styleMask:default_window_mask backing:NSBackingStoreBuffered defer:YES];
}
NSRect view_rect = NSMakeRect(0.0, 0.0, width, height);
window_info->view = [[MacOSXOpenGLView alloc] initWithFrame:view_rect pixelFormat:peer_info->pixel_format];
if (window_info->context != nil) {
printf("Setting context\n"); fflush(stdout);
[window_info->view setOpenGLContext:window_info->context];
}
// Inform the view of its parent window info; used to register for window-close callbacks
[window_info->view setParent:window_info];
[window_info->window setContentView:window_info->view];
[window_info->window makeKeyAndOrderFront:[NSApplication sharedApplication]];
[window_info->window makeFirstResponder:window_info->view];
[window_info->window setReleasedWhenClosed:YES];
[window_info->window setInitialFirstResponder:window_info->view];
if (window_info->window_options != NSApplicationPresentationDefault) {
printf("Non-default\n"); fflush(stdout);
[[NSApplication sharedApplication] setPresentationOptions:window_info->window_options];
}
peer_info->window_info = window_info;
return window_handle;
}
JNIEXPORT jint JNICALL Java_org_lwjgl_DefaultSysImplementation_getJNIVersion
(JNIEnv *env, jobject ignored) {
return org_lwjgl_MacOSXSysImplementation_JNI_VERSION;
@ -69,10 +484,30 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_setGammaRamp(JNIEnv *
}
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nHideUI(JNIEnv *env, jobject this, jboolean hide) {
if (hide == JNI_TRUE) {
SetSystemUIMode(kUIModeContentSuppressed, 0);
} else {
SetSystemUIMode(kUIModeNormal, 0);
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nHideUI(JNIEnv *env, jobject this, jobject window_handle, jboolean hide) {
if (window_handle == NULL) {
printf("Window handle is null\n");
return;
}
MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle);
if(floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) {
NSApplicationPresentationOptions options = NSApplicationPresentationDefault;
if (hide == JNI_TRUE) {
options = NSApplicationPresentationFullScreen;
options |= NSApplicationPresentationHideDock;
options |= NSApplicationPresentationHideMenuBar;
}
printf("Setting options\n");
window_info->window_options = options;
if (window_info->window != nil) {
[[NSApplication sharedApplication] setPresentationOptions:options];
}
} else {
if (hide == JNI_TRUE) {
SetSystemUIMode(kUIModeContentSuppressed, 0);
} else {
SetSystemUIMode(kUIModeNormal, 0);
}
}
}

View file

@ -42,217 +42,17 @@
#import <JavaNativeFoundation.h>
#include <jni.h>
#include <jawt_md.h>
#include "awt_tools.h"
#include "org_lwjgl_opengl_MacOSXCanvasPeerInfo.h"
#include "context.h"
#include "common_tools.h"
@interface AttachLayerOnMainThread : NSObject {
MacOSXPeerInfo *peer_info;
JAWT_MacOSXDrawingSurfaceInfo *macosx_dsi;
}
- (void) attachLayer;
- (MacOSXPeerInfo*) peer_info;
- (JAWT_MacOSXDrawingSurfaceInfo) macosx_dsi;
- (void) setPeer_info: (MacOSXPeerInfo*)input;
- (void) setMacosx_dsi: (JAWT_MacOSXDrawingSurfaceInfo*)input;
@end
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle
(JNIEnv *env, jclass clazz, jobject lock_buffer_handle, jobject peer_info_handle, jboolean allowCALayer) {
(JNIEnv *env, jclass clazz, jobject peer_info_handle) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle);
AWTSurfaceLock *surface = (AWTSurfaceLock *)(*env)->GetDirectBufferAddress(env, lock_buffer_handle);
JAWT_MacOSXDrawingSurfaceInfo *macosx_dsi = (JAWT_MacOSXDrawingSurfaceInfo *)surface->dsi->platformInfo;
if (allowCALayer) {
// check for CALayer support
if(surface->awt.version & 0x80000000) { //JAWT_MACOSX_USE_CALAYER) {
jint width = surface->dsi->bounds.width;
jint height = surface->dsi->bounds.height;
if(peer_info->pbuffer == NULL || peer_info->window || width != [peer_info->pbuffer pixelsWide] || height != [peer_info->pbuffer pixelsHigh]) {
if(peer_info->pbuffer != NULL) {
[peer_info->pbuffer release];
}
// make pbuffer
NSOpenGLPixelBuffer *pbuffer = nil;
NSLog(@"Make pbuffer: %d x %d", width, height);
pbuffer = [[NSOpenGLPixelBuffer alloc] initWithTextureTarget:GL_TEXTURE_RECTANGLE_EXT
textureInternalFormat:GL_RGBA
textureMaxMipMapLevel:0
pixelsWide:width
pixelsHigh:height];
peer_info->pbuffer = pbuffer;
peer_info->window = false;
peer_info->canDrawGL = true;
}
if (macosx_dsi != NULL) {
AttachLayerOnMainThread *attachLayerOnMainThread = [[AttachLayerOnMainThread new] autorelease];
attachLayerOnMainThread.peer_info = peer_info;
attachLayerOnMainThread.macosx_dsi = macosx_dsi;
[JNFRunLoop performOnMainThread:@selector(attachLayer)
on:attachLayerOnMainThread
withObject:nil
waitUntilDone:YES];
}
[pool release];
return;
}
}
peer_info->nsview = macosx_dsi->cocoaViewRef;
peer_info->window = true;
peer_info->isWindowed = true;
[pool release];
}
@interface PBufferGLLayer : NSOpenGLLayer {
MacOSXPeerInfo *peer_info;
GLuint textureID;
}
- (MacOSXPeerInfo*) peer_info;
- (GLuint) textureID;
- (void) setPeer_info: (MacOSXPeerInfo*)input;
- (void) setTextureID: (GLuint)input;
@end
// Object class to CALayer on AppKit Thread
@implementation AttachLayerOnMainThread
- (void) attachLayer {
// attach the "root layer" to the AWT Canvas surface layers
id <JAWT_SurfaceLayers> surfaceLayers = (id <JAWT_SurfaceLayers>)macosx_dsi;//dsi->platformInfo;
if(surfaceLayers.layer == NULL) {
PBufferGLLayer *caGLLayer = [[PBufferGLLayer new] autorelease];
caGLLayer.peer_info = peer_info;
caGLLayer.asynchronous = YES;
caGLLayer.needsDisplayOnBoundsChange = YES;
caGLLayer.opaque = YES;
surfaceLayers.layer = caGLLayer;
}
}
- (MacOSXPeerInfo*) peer_info {
return peer_info;
}
- (JAWT_MacOSXDrawingSurfaceInfo*) macosx_dsi {
return macosx_dsi;
}
- (void) setPeer_info: (MacOSXPeerInfo*)input {
peer_info = input;
}
- (void) setMacosx_dsi: (JAWT_MacOSXDrawingSurfaceInfo*)input {
macosx_dsi = input;
}
@end
// rotates a red square when asked to draw
@implementation PBufferGLLayer
// override to draw custom GL content
-(void)drawInCGLContext:(CGLContextObj)glContext
pixelFormat:(CGLPixelFormatObj)pixelFormat
forLayerTime:(CFTimeInterval)timeInterval
displayTime:(const CVTimeStamp *)timeStamp {
if(!peer_info || !peer_info->pbuffer) {
return;
}
peer_info->canDrawGL = false;
NSOpenGLPixelBuffer *pbuffer = self.peer_info->pbuffer;
// set the current context
CGLSetCurrentContext(glContext);
GLsizei width = [pbuffer pixelsWide];
GLsizei height = [pbuffer pixelsHigh];
if(textureID == 0) {
glGenTextures(1, &textureID);
}
glBindTexture(GL_TEXTURE_RECTANGLE_EXT, self.textureID);
CGLTexImagePBuffer(glContext,[pbuffer CGLPBufferObj], GL_FRONT);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glEnable(GL_TEXTURE_RECTANGLE_EXT);
static GLfloat verts[] = {
-1.0, -1.0,
-1.0, 1.0,
1.0, 1.0,
1.0, -1.0
};
GLfloat tex[] = {
0.0, 0.0,
0.0, height,
width, height,
width, 0.0
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, verts);
glTexCoordPointer(2, GL_FLOAT, 0, tex);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_RECTANGLE_EXT);
// call super to finalize the drawing - by default all it does is call glFlush()
[super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp];
}
-(BOOL)canDrawInCGLContext:(CGLContextObj)glContext
pixelFormat:(CGLPixelFormatObj)pixelFormat
forLayerTime:(CFTimeInterval)timeInterval
displayTime:(const CVTimeStamp *)timeStamp {
return (peer_info->canDrawGL && !peer_info->window) ? YES : NO;
}
- (MacOSXPeerInfo*) peer_info {
return peer_info;
}
- (GLuint) textureID {
return textureID;
}
- (void) setPeer_info: (MacOSXPeerInfo*)input {
peer_info = input;
}
- (void) setTextureID: (GLuint)input {
textureID = input;
}
@end

View file

@ -47,7 +47,7 @@
#import "common_tools.h"
typedef struct {
NSOpenGLContext *context;
NSOpenGLContext *context;
MacOSXPeerInfo *peer_info;
} MacOSXContext;
@ -57,8 +57,9 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre
MacOSXPeerInfo *peer_info;
MacOSXContext *shared_context_info;
MacOSXContext *context_info;
NSOpenGLContext *context;
NSOpenGLContext *shared_context = NULL;
NSOpenGLContext *context;
NSOpenGLContext *shared_context;
printf("nCreate\n");
jobject context_handle = newJavaManagedByteBuffer(env, sizeof(MacOSXContext));
if (context_handle == NULL) {
throwException(env, "Could not create handle buffer");
@ -67,16 +68,19 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre
peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle);
if (shared_context_handle != NULL) {
shared_context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, shared_context_handle);
shared_context = shared_context_info->context;
}
context = [[NSOpenGLContext alloc] initWithFormat:peer_info->pixel_format shareContext:shared_context];
if (context == NULL) {
throwException(env, "Could not create context");
return NULL;
shared_context = shared_context_info->context;
}
context = [[NSOpenGLContext alloc] initWithFormat:peer_info->pixel_format shareContext:shared_context];
if (context == NULL) {
throwException(env, "Could not create context");
return NULL;
}
[peer_info->window_info->view setOpenGLContext:context];
peer_info->window_info->context = context;
context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle);
context_info->context = context;
context_info->context = context;
context_info->peer_info = peer_info;
[pool release];
return context_handle;
}
@ -84,19 +88,19 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre
JNIEXPORT jlong JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_getCGLShareGroup
(JNIEnv *env, jclass clazz, jobject context_handle) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle);
CGLContextObj cgl_context = [context_info->context CGLContextObj];
MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info;
CGLContextObj cgl_context = [[peer_info->window_info->view openGLContext] CGLContextObj];
CGLShareGroupObj share_group = CGLGetShareGroup(cgl_context);
[pool release];
return share_group;
return (jlong)share_group;
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nSwapBuffers
(JNIEnv *env, jclass clazz, jobject context_handle) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle);
[context_info->context flushBuffer];
context_info->peer_info->canDrawGL = true;
MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info;
[[peer_info->window_info->view openGLContext] flushBuffer];
peer_info->canDrawGL = true;
[pool release];
}
@ -104,37 +108,35 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nSwapBu
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nUpdate
(JNIEnv *env, jclass clazz, jobject context_handle) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle);
[context_info->context update];
context_info->peer_info->canDrawGL = true;
MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info;
[[peer_info->window_info->view openGLContext] update];
peer_info->canDrawGL = true;
[pool release];
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_clearDrawable
(JNIEnv *env, jclass clazz, jobject context_handle) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle);
[context_info->context clearDrawable];
MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info;
printf("clearDrawable\n");
[[peer_info->window_info->view openGLContext] clearDrawable];
[pool release];
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nReleaseCurrentContext
(JNIEnv *env, jclass clazz) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
printf("nReleaseCurrentContexta\n");
[NSOpenGLContext clearCurrentContext];
[pool release];
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_setView
(JNIEnv *env, jclass clazz, jobject peer_info_handle, jobject context_handle) {
(JNIEnv *env, jclass clazz, jobject peer_info_handle) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle);
MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle);
if (peer_info->window) {
[context_info->context setView: peer_info->nsview];
} else {
[context_info->context setPixelBuffer:peer_info->pbuffer cubeMapFace:0 mipMapLevel:0 currentVirtualScreen:0];
}
MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle);
printf("setView\n");
[[peer_info->window_info->view openGLContext] setView: peer_info->window_info->view];
peer_info->canDrawGL = true;
[pool release];
}
@ -142,16 +144,17 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_setView
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nMakeCurrent
(JNIEnv *env, jclass clazz, jobject context_handle) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle);
[context_info->context makeCurrentContext];
MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info;
printf("nMakeCurrent\n");
[[peer_info->window_info->view openGLContext] makeCurrentContext];
[pool release];
}
JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nIsCurrent
(JNIEnv *env, jclass clazz, jobject context_handle) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle);
bool result = context_info->context == [NSOpenGLContext currentContext];
MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info;
bool result = [peer_info->window_info->view openGLContext] == [NSOpenGLContext currentContext];
[pool release];
return result ? JNI_TRUE : JNI_FALSE;
}
@ -159,17 +162,18 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nIs
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nSetSwapInterval
(JNIEnv *env, jclass clazz, jobject context_handle, jint int_value) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle);
MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info;
GLint value = int_value;
[context_info->context setValues:&value forParameter:NSOpenGLCPSwapInterval];
[[peer_info->window_info->view openGLContext] setValues:&value forParameter:NSOpenGLCPSwapInterval];
[pool release];
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nDestroy
(JNIEnv *env, jclass clazz, jobject context_handle) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MacOSXContext *context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle);
[context_info->context clearDrawable];
[context_info->context release];
MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info;
printf("nDestroy\n");
[[peer_info->window_info->view openGLContext] clearDrawable];
[[peer_info->window_info->view openGLContext] release];
[pool release];
}

View file

@ -0,0 +1,58 @@
/*
* Copyright (c) 2002-2012 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* $Id: org_lwjgl_opengl_MacOSXNativeKeyboard.m 3055 2012-08-29 0:46:00Z mojang $
*
* Mac OS X native keyboard functions.
*
* @author mojang
* @version $Revision: 3055 $
*/
#import <AppKit/NSApplication.h>
#import <Cocoa/Cocoa.h>
#import <jni.h>
#import <unistd.h>
#import "common_tools.h"
#import "org_lwjgl_opengl_MacOSXNativeKeyboard.h"
#import "context.h"
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeKeyboard_nRegisterKeyListener(JNIEnv *env, jobject this, jobject window_handle) {
MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle);
window_info->jkeyboard = (*env)->NewGlobalRef(env, this);
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeKeyboard_nUnregisterKeyListener(JNIEnv *env, jobject this, jobject window_handle) {
MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle);
window_info->jkeyboard = NULL;
}

View file

@ -0,0 +1,78 @@
/*
* Copyright (c) 2002-2012 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* $Id: org_lwjgl_opengl_MacOSXNativeKeyboard.m 3055 2012-08-29 0:46:00Z mojang $
*
* Mac OS X native keyboard functions.
*
* @author mojang
* @version $Revision: 3055 $
*/
#import <AppKit/NSApplication.h>
#import <Cocoa/Cocoa.h>
#import <jni.h>
#import <unistd.h>
#import "common_tools.h"
#import "org_lwjgl_opengl_MacOSXNativeMouse.h"
#import "context.h"
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nGrabMouse(JNIEnv *env, jclass this, jboolean grab) {
CGAssociateMouseAndMouseCursorPosition(grab == JNI_TRUE ? FALSE : TRUE);
if (grab)
CGDisplayHideCursor(kCGDirectMainDisplay);
else
CGDisplayShowCursor(kCGDirectMainDisplay);
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nWarpCursor(JNIEnv *env, jclass this, jobject window_handle, jint x, jint y) {
MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle);
NSPoint point = NSMakePoint(x, y);
point = [window_info->view convertPoint:point fromView:window_info->view];
CGPoint p;
p.x = point.x;
p.y = point.y;
CGWarpMouseCursorPosition(p);
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nRegisterMouseListener(JNIEnv *env, jobject _this, jobject window_handle) {
MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle);
[window_info->window setAcceptsMouseMovedEvents:YES];
window_info->jmouse = (*env)->NewGlobalRef(env, _this);
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXNativeMouse_nUnregisterMouseListener(JNIEnv *env, jobject this, jobject window_handle) {
MacOSXWindowInfo *window_info = (MacOSXWindowInfo *)(*env)->GetDirectBufferAddress(env, window_handle);
[window_info->window setAcceptsMouseMovedEvents:NO];
window_info->jmouse = nil;
}

View file

@ -65,7 +65,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXPbufferPeerInfo_nCreate(JNIEn
}
MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle);
peer_info->pbuffer = pbuffer;
peer_info->window = false;
peer_info->isWindowed = false;
[pool release];
}