33#import <Carbon/Carbon.h>
39#import <Foundation/NSDebug.h>
44static NSString *
kOOLogKeyUp =
@"input.keyMapping.keyPress.keyUp";
55- (
int) translateKeyCode:(
int)input;
62#if !OOLITE_MAC_OS_X_10_7
63@interface NSView (Lion)
66- (void) setWantsBestResolutionOpenGLSurface:(BOOL)flag;
68- (NSPoint) convertPointToBacking:(NSPoint)aPoint;
69- (NSPoint) convertPointFromBacking:(NSPoint)aPoint;
70- (NSSize) convertSizeToBacking:(NSSize)aSize;
71- (NSSize) convertSizeFromBacking:(NSSize)aSize;
72- (NSRect) convertRectToBacking:(NSRect)aRect;
73- (NSRect) convertRectFromBacking:(NSRect)aRect;
81- (id) initWithFrame:(NSRect)frameRect
86 OOLog(
@"debug.zombieEnabled",
@"*** ZOMBIES WILL EAT YOUR BRAIN ***");
91 NSOpenGLPixelFormatAttribute attrs[] =
99 NSOpenGLPFAScreenMask, CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay),
104 NSOpenGLPFANoRecovery,
107 NSOpenGLPFACompliant,
109 NSOpenGLPFAColorSize, 32,
110 NSOpenGLPFADepthSize, 32,
111 NSOpenGLPFADoubleBuffer,
112 NSOpenGLPFAAccelerated,
115 NSOpenGLPFAMultisample,
116 NSOpenGLPFASampleBuffers, 1,
117 NSOpenGLPFASamples,4,
123 NSOpenGLPixelFormat *pixelFormat = [[[
NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease];
125 if ((
self = [super initWithFrame:frameRect pixelFormat:pixelFormat]))
127 if ([
self respondsToSelector:
@selector(setAcceptsTouchEvents:)])
129 [
self setAcceptsTouchEvents:YES];
132 if ([
self respondsToSelector:
@selector(setWantsBestResolutionOpenGLSurface:)])
135 [
self setWantsBestResolutionOpenGLSurface:YES];
81- (id) initWithFrame:(NSRect)frameRect {
…}
158- (void) initKeyMappingData
160 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
163 NSString *kbd = [prefs oo_stringForKey:@"keyboard-code" defaultValue:@"default"];
164 NSDictionary *subset = [kmap objectForKey:kbd];
166 [keyMappings_normal release];
167 keyMappings_normal = [[subset objectForKey:@"mapping_normal"] copy];
168 [keyMappings_shifted release];
169 keyMappings_shifted = [[subset objectForKey:@"mapping_shifted"] copy];
180 [keyMappings_normal release];
182 [keyMappings_shifted release];
190 allowingStringInput = value;
194- (void) allowStringInput:(BOOL)value
208 return allowingStringInput;
212- (NSString *) typedString
218- (void) resetTypedString
220 [typedString setString:@""];
224- (void) setTypedString:(NSString *)value
226 [typedString setString:value];
236- (NSSize) backingViewSize
238 return backingViewSize;
260- (GameController *) gameController
262 return gameController;
266- (void) setGameController:(GameController *) controller
268 gameController = controller;
281 if ([[
self window] isVisible])
283 [
self drawRect:NSMakeRect(0, 0, viewSize.width, viewSize.height)];
290 _mouseWheelDelta = 0.0f;
293- (void) drawRect:(NSRect)rect
295 if ((viewSize.width != [
self frame].size.width)||(viewSize.height != [
self frame].size.height))
297 m_glContextInitialized = NO;
298 viewSize = [
self frame].size;
301 if (!m_glContextInitialized) [
self initialiseGLWithSize:viewSize];
308 OOGL(glClearColor(0.0, 0.0, 0.0, 0.0));
309 OOGL(glClear(GL_COLOR_BUFFER_BIT));
312 [[
self openGLContext] flushBuffer];
318 NSRunInformationalAlertPanel(
DESC(
@"oolite-mac-bad-display"),
@"%@",
nil,
nil,
nil,
DESC(
@"oolite-mac-bad-display-2"));
324 NSOpenGLContext *context = [
self openGLContext];
327 int virtualScreen = [
context currentVirtualScreen];
335 @catch (NSException *exception)
342 [
context setCurrentVirtualScreen:_virtualScreen];
358- (void) initialiseGLWithSize:(NSSize)v_size
361 if (viewSize.width/viewSize.height > 4.0/3.0) {
362 display_z = 480.0 * viewSize.width/viewSize.height;
363 x_offset = 240.0 * viewSize.width/viewSize.height;
368 y_offset = 320.0 * viewSize.height/viewSize.width;
371 if ([
self respondsToSelector:
@selector(convertSizeToBacking:)])
374 backingViewSize = [
self convertSizeToBacking:viewSize];
378 backingViewSize = viewSize;
381 [
self openGLContext];
382 [[
self gameController] setUpBasicOpenGLStateWithSize:v_size];
383 [[
self openGLContext] flushBuffer];
385 m_glContextInitialized = YES;
395#ifdef MAC_OS_X_VERSION_10_7
396- (void) viewDidMoveToWindow
403 if (&NSWindowDidChangeBackingPropertiesNotification != NULL && [
self.window respondsToSelector:
@selector(backingScaleFactor)])
405 [[NSNotificationCenter defaultCenter] addObserver:
self
406 selector:@selector(backingPropertiesChanged:)
407 name:NSWindowDidChangeBackingPropertiesNotification
411 [
self backingPropertiesChanged:nil];
416- (void) backingPropertiesChanged:(NSNotification *)notification
423- (BOOL) snapShot:(NSString *)filename
425 BOOL snapShotOK = YES;
426 NSSize v_size = backingViewSize;
428 int w = v_size.width;
429 int h = v_size.height;
434 long nPixels = w * h + 1;
436 unsigned char *red = (
unsigned char *)malloc(nPixels);
437 unsigned char *green = (
unsigned char *)malloc(nPixels);
438 unsigned char *blue = (
unsigned char *)malloc(nPixels);
441 NSString *originalDirectory = [[NSFileManager defaultManager] currentDirectoryPath];
444 if (![[NSFileManager defaultManager] changeCurrentDirectoryPath:snapshotsDirectory])
447 OOLog(
@"savedSnapshot.defaultPath.chdir.failed",
@"Could not navigate to %@", snapshotsDirectory);
453 static unsigned imageNo = 0;
454 unsigned tmpImageNo = 0;
455 NSString *pathToPic =
nil;
456 NSString *baseName =
@"oolite";
461 pathToPic = [filename stringByAppendingString:@".png"];
465 tmpImageNo = imageNo;
468 if (withFilename && [[NSFileManager defaultManager] fileExistsAtPath:pathToPic])
470 OOLog(
@"screenshot.filenameExists",
@"Snapshot \"%@.png\
" already exists - adding numerical sequence.", pathToPic);
474 if (pathToPic ==
nil)
479 pathToPic = [NSString stringWithFormat:@"%@-%03d.png", baseName, tmpImageNo];
480 }
while ([[NSFileManager defaultManager] fileExistsAtPath:pathToPic]);
485 imageNo = tmpImageNo;
488 OOLog(
@"screenshot",
@"Saved screen shot \"%@\
" (%u x %u pixels).", pathToPic, w, h);
490 NSBitmapImageRep* bitmapRep =
491 [[NSBitmapImageRep alloc]
492 initWithBitmapDataPlanes:NULL
499 colorSpaceName: NSDeviceRGBColorSpace
504 unsigned char *pixels = [bitmapRep bitmapData];
506 OOGL(glReadPixels(0,0, w,h, GL_RED, GL_UNSIGNED_BYTE, red));
507 OOGL(glReadPixels(0,0, w,h, GL_GREEN, GL_UNSIGNED_BYTE, green));
508 OOGL(glReadPixels(0,0, w,h, GL_BLUE, GL_UNSIGNED_BYTE, blue));
511 for (
y = 0;
y < h;
y++)
513 long index = (h -
y - 1)*w;
514 for (
x = 0;
x < w;
x++)
516 *pixels++ = red[index];
517 *pixels++ = green[index];
518 *pixels++ = blue[index++];
522 [[bitmapRep representationUsingType:NSPNGFileType properties:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSImageInterlaced, NULL]]
523 writeToFile:pathToPic atomically:YES];
534 [[NSFileManager defaultManager] changeCurrentDirectoryPath:originalDirectory];
539- (void) stringToClipboard:(NSString *)stringToCopy
541 NSPasteboard *clipBoard = [NSPasteboard generalPasteboard];
542 [clipBoard declareTypes:[NSArray arrayWithObject:NSPasteboardTypeString] owner:nil];
543 [clipBoard setString:stringToCopy forType:NSPasteboardTypeString];
553- (void) keyUp:(NSEvent *)theEvent
555 NSString *stringValue =
nil;
559 stringValue = [
theEvent charactersIgnoringModifiers];
568 if ([stringValue length] < 1)
return;
575 OOLog(
kOOLogKeyUp,
@"Key up: stringValue = \"%@\
", keyCode = %d, key = %u", stringValue, keycode, key);
578 if ([theEvent modifierFlags] & NSCommandKeyMask)
596 if (key == NSF12FunctionKey)
604 if ((key >= 0)&&(key < [
self numKeys])&&(
keys[key]))
553- (void) keyUp:(NSEvent *)theEvent {
…}
616- (void) keyDown:(NSEvent *)theEvent
618 NSString *stringValue =
nil;
622 stringValue = [
theEvent charactersIgnoringModifiers];
635 if ([stringValue length] < 1)
639 NSString *keyNormal = [keyMappings_normal objectForKey:[
NSString stringWithFormat:@"%d", keycode]];
640 if (keyNormal) stringValue = [
NSString stringWithFormat:@"%c", [
keyNormal integerValue]];
644 NSString *keyShifted = [keyMappings_shifted objectForKey:[
NSString stringWithFormat:@"%d", keycode]];
645 if (keyShifted) stringValue = [
NSString stringWithFormat:@"%c", [
keyShifted integerValue]];
648 if ([stringValue length] < 1)
return;
654 OOLog(
kOOLogKeyDown,
@"Key down: stringValue = \"%@\
", keyCode = %d, key = %u", stringValue, keycode, key);
657 if ([theEvent modifierFlags] & NSCommandKeyMask)
678 if (key == NSF12FunctionKey)
683 [
gameController performSelector:@selector(toggleFullScreenAction:) withObject:nil afterDelay:0.0];
689 if ((key >= 0)&&(key < [
self numKeys])&&(!
keys[key]))
698 [typedString deleteCharactersInRange:NSMakeRange([typedString length] - 1, 1)];
711 [typedString appendFormat:@"%c", tolower(key)];
719 if (isprint(key) && key !=
'/')
722 [typedString appendFormat:@"%c", key];
727 [typedString appendFormat:@"%c", key];
616- (void) keyDown:(NSEvent *)theEvent {
…}
743- (void)flagsChanged:(NSEvent *)theEvent
745 NSUInteger flags = [
theEvent modifierFlags];
746 opt = (flags & NSAlternateKeyMask) ? YES : NO;
747 ctrl = (flags & NSControlKeyMask) ? YES : NO;
748 command = (flags & NSCommandKeyMask) ? YES : NO;
749 shift = ( flags & NSShiftKeyMask ) ? YES : NO;
750 if ([theEvent keyCode] == 0x39)
752 capsLockOn = (flags & NSAlphaShiftKeyMask) ? YES : NO;
743- (void)flagsChanged:(NSEvent *)theEvent {
…}
757- (void)mouseDown:(NSEvent *)theEvent
762 keys[gvMouseDoubleClick] = NO;
764 keys[gvMouseLeftButton] = YES;
757- (void)mouseDown:(NSEvent *)theEvent {
…}
768- (void)mouseUp:(NSEvent *)theEvent
779 keys[gvMouseLeftButton] = NO;
768- (void)mouseUp:(NSEvent *)theEvent {
…}
785 CGPoint centerPoint = CGPointMake(viewSize.width / 2.0, viewSize.height / 2.0);
786 CGWarpMouseCursorPosition(centerPoint);
787 [
self setVirtualJoystick:0.0 :0.0];
791- (void)mouseMoved:(NSEvent *)theEvent
807 [
self setVirtualJoystick:mx :-my];
791- (void)mouseMoved:(NSEvent *)theEvent {
…}
811- (void) mouseDragged:(NSEvent *)theEvent
811- (void) mouseDragged:(NSEvent *)theEvent {
…}
816- (void)scrollWheel:(NSEvent *)theEvent
818 float dy = [
theEvent scrollingDeltaY];
816- (void)scrollWheel:(NSEvent *)theEvent {
…}
840- (void) otherMouseDragged:(NSEvent *)theEvent
840- (void) otherMouseDragged:(NSEvent *)theEvent {
…}
846- (void) rightMouseDown:(NSEvent *)theEvent
846- (void) rightMouseDown:(NSEvent *)theEvent {
…}
852- (void) rightMouseUp:(NSEvent *)theEvent
852- (void) rightMouseUp:(NSEvent *)theEvent {
…}
858- (void) touchesEndedWithEvent:(NSEvent *)theEvent
858- (void) touchesEndedWithEvent:(NSEvent *)theEvent {
…}
866 if ([
PLAYER guiScreen] == GUI_SCREEN_MAIN)
875- (
int) translateKeyCode: (
int) input
880 case NSUpArrowFunctionKey:
884 case NSDownArrowFunctionKey:
888 case NSLeftArrowFunctionKey:
892 case NSRightArrowFunctionKey:
896 case NSF1FunctionKey:
900 case NSF2FunctionKey:
904 case NSF3FunctionKey:
908 case NSF4FunctionKey:
912 case NSF5FunctionKey:
916 case NSF6FunctionKey:
920 case NSF7FunctionKey:
924 case NSF8FunctionKey:
928 case NSF9FunctionKey:
932 case NSF10FunctionKey:
936 case NSF11FunctionKey:
940 case NSHomeFunctionKey:
944 case NSBackspaceCharacter:
948 case NSDeleteCharacter:
952 case NSInsertFunctionKey:
956 case NSEndFunctionKey:
960 case NSPageUpFunctionKey:
964 case NSPageDownFunctionKey:
968 case NSPrintScreenFunctionKey:
972 case NSPauseFunctionKey:
875- (
int) translateKeyCode: (
int) input {
…}
983- (void) setVirtualJoystick:(
double) vmx :(
double) vmy
985 virtualJoystickPosition.x = vmx;
986 virtualJoystickPosition.y = vmy;
990- (NSPoint) virtualJoystickPosition
992 return virtualJoystickPosition;
1001 lastKeyShifted = NO;
1002 for (i = 0; i < [
self numKeys]; i++)
1009 keys[gvMouseDoubleClick] = NO;
1010 keys[gvMouseLeftButton] = NO;
1015- (void) clearKey: (
int)theKey
1017 if (theKey >= 0 && theKey < [
self numKeys])
1024- (BOOL) isAlphabetKeyDown
1026 return isAlphabetKeyDown = NO;
1033- (void) suppressKeysUntilKeyUp
1047- (BOOL) isDown: (
int) key
1053 if ( key >= [
self numKeys] )
1071- (BOOL) isCommandDown
1083- (BOOL) isCapsLockOn
1089- (BOOL) lastKeyWasShifted
1091 return lastKeyShifted;
1101- (
int) mouseWheelState
1103 if (_mouseWheelDelta > 0.0f)
1105 else if (_mouseWheelDelta < 0.0f)
1112- (float) mouseWheelDelta
1118- (void) setMouseWheelDelta: (
float) newWheelDelta
1124- (BOOL) isCommandQDown
1130- (BOOL) isCommandFDown
1136- (void) clearCommandF
1144 #define KEYMAP_GET(m, index) ((((uint8_t*)(m))[(index) >> 3] & (1L << ((index) & 7))) ? 1 : 0)
1152- (OOOpenGLMatrixManager *) getOpenGLMatrixManager
1154 return matrixManager;
1159- (void) dumpRGBAToFileNamed:(NSString *)name
1160 bytes:(uint8_t *)bytes
1161 width:(NSUInteger)width
1162 height:(NSUInteger)height
1163 rowBytes:(NSUInteger)rowBytes
1165 if (name ==
nil || bytes == NULL || width == 0 || height == 0 || rowBytes < width * 4)
return;
1167 NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&bytes
1174 colorSpaceName:NSCalibratedRGBColorSpace
1175 bytesPerRow:rowBytes
1180 [bitmap autorelease];
1182 NSString *filepath = [[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:name] stringByAppendingPathExtension:@"png"];
1183 [[bitmap representationUsingType:NSPNGFileType properties:[NSDictionary dictionary]] writeToFile:filepath atomically:YES];
1187- (void) dumpRGBToFileNamed:(NSString *)name
1188 bytes:(uint8_t *)bytes
1189 width:(NSUInteger)width
1190 height:(NSUInteger)height
1191 rowBytes:(NSUInteger)rowBytes
1193 if (name ==
nil || bytes == NULL || width == 0 || height == 0 || rowBytes < width * 3)
return;
1195 NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&bytes
1202 colorSpaceName:NSCalibratedRGBColorSpace
1203 bytesPerRow:rowBytes
1208 [bitmap autorelease];
1210 NSString *filepath = [[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:name] stringByAppendingPathExtension:@"png"];
1211 [[bitmap representationUsingType:NSPNGFileType properties:[NSDictionary dictionary]] writeToFile:filepath atomically:YES];
1216- (void) dumpGrayToFileNamed:(NSString *)name
1217 bytes:(uint8_t *)bytes
1218 width:(NSUInteger)width
1219 height:(NSUInteger)height
1220 rowBytes:(NSUInteger)rowBytes
1222 if (name ==
nil || bytes == NULL || width == 0 || height == 0 || rowBytes < width)
return;
1224 NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&bytes
1231 colorSpaceName:NSCalibratedWhiteColorSpace
1232 bytesPerRow:rowBytes
1237 [bitmap autorelease];
1239 NSString *filepath = [[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:name] stringByAppendingPathExtension:@"png"];
1240 [[bitmap representationUsingType:NSPNGFileType properties:[NSDictionary dictionary]] writeToFile:filepath atomically:YES];
1245- (void) dumpGrayAlphaToFileNamed:(NSString *)name
1246 bytes:(uint8_t *)bytes
1247 width:(NSUInteger)width
1248 height:(NSUInteger)height
1249 rowBytes:(NSUInteger)rowBytes
1251 if (name ==
nil || bytes == NULL || width == 0 || height == 0 || rowBytes < width * 2)
return;
1253 NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&bytes
1260 colorSpaceName:NSCalibratedWhiteColorSpace
1261 bytesPerRow:rowBytes
1266 [bitmap autorelease];
1268 NSString *filepath = [[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:name] stringByAppendingPathExtension:@"png"];
1269 [[bitmap representationUsingType:NSPNGFileType properties:[NSDictionary dictionary]] writeToFile:filepath atomically:YES];
1274- (void) dumpRGBAToRGBFileNamed:(NSString *)rgbName
1275 andGrayFileNamed:(NSString *)grayName
1276 bytes:(uint8_t *)bytes
1277 width:(NSUInteger)width
1278 height:(NSUInteger)height
1279 rowBytes:(NSUInteger)rowBytes
1281 if ((rgbName ==
nil && grayName ==
nil) || bytes == NULL || width == 0 || height == 0 || rowBytes < width * 4)
return;
1283 uint8_t *rgbBytes, *rgbPx, *grayBytes, *grayPx, *srcPx;
1285 BOOL trivalAlpha = YES;
1287 rgbPx = rgbBytes = malloc(width * height * 3);
1288 if (rgbBytes == NULL)
return;
1290 grayPx = grayBytes = malloc(width * height);
1291 if (grayBytes == NULL)
1297 for (
y = 0;
y < height;
y++)
1299 srcPx = bytes + rowBytes *
y;
1301 for (
x = 0;
x < width;
x++)
1303 *rgbPx++ = *srcPx++;
1304 *rgbPx++ = *srcPx++;
1305 *rgbPx++ = *srcPx++;
1306 trivalAlpha = trivalAlpha && ((*srcPx == 0xFF) || (*srcPx == 0x00));
1307 *grayPx++ = *srcPx++;
1311 [
self dumpRGBToFileNamed:rgbName
1315 rowBytes:width * 3];
1320 [
self dumpGrayToFileNamed:grayName
1334 NSCParameterAssert(outHidden != NULL && outObscured != NULL);
1343 BOOL hidden, obscured;
1346 if (obscured) [
NSCursor setHiddenUntilMouseMoves:YES];
1352 BOOL hidden, obscured;
1355 if (obscured) [
NSCursor setHiddenUntilMouseMoves:NO];
1359- (void) setGammaValue: (
float) value
1370- (void) setFov:(
float)value fromFraction:(BOOL)fromFraction
1372 _fov = fromFraction ? value : tanf((value / 2.0f) *
M_PI / 180.0f);
1376- (float) fov:(BOOL)inFraction
1378 return inFraction ? _fov : 2 * atanf(_fov) * 180.0f /
M_PI;
#define OOMOUSEWHEEL_EVENTS_DELAY_INTERVAL
#define OOMOUSEWHEEL_DELTA
static NSString * kOOLogKeyCodeOutOfRange
static void GetDesiredCursorState(OOMouseInteractionMode mode, BOOL *outHidden, BOOL *outObscured)
static NSString * kOOLogKeyDown
static NSString * kOOLogKeyUp
static void ApplyCursorState(OOMouseInteractionMode mode)
#define KEYMAP_GET(m, index)
static void UnapplyCursorState(OOMouseInteractionMode mode)
#define MAIN_GUI_PIXEL_WIDTH
#define MAIN_GUI_PIXEL_HEIGHT
#define OOLog(class, format,...)
@ MOUSE_MODE_FLIGHT_WITH_MOUSE_CONTROL
@ MOUSE_MODE_FLIGHT_NO_MOUSE_CONTROL
void GLSetDisplayScaleFactor(GLfloat factor)
#define MOUSE_DOUBLE_CLICK_INTERVAL
void recenterVirtualJoystick()
BOOL wantsBestResolutionOpenGLSurface()
GameController * sharedController()
void recenterVirtualJoystick()
NSURL * snapshotsURLCreatingIfNeeded:(BOOL create)
void pauseFullScreenModeToPerform:onTarget:(SEL selector,[onTarget] id target)
void recenterVirtualJoystick()
void mouseMoved:(NSEvent *theEvent)
static void ApplyCursorState(OOMouseInteractionMode mode)
void noteMovedToBadDisplay()
void initKeyMappingData()
NSDictionary * keyMappings_shifted
NSMutableString * typedString
int scancode2Unicode[NUM_KEYS]
static void UnapplyCursorState(OOMouseInteractionMode mode)
NSTimeInterval timeIntervalAtLastMouseWheel
NSData * _pixelFormatAttributes
NSData * pixelFormatAttributes()
NSPoint virtualJoystickPosition
int translateKeyCode:(int input)
OOOpenGLMatrixManager * matrixManager
static void GetDesiredCursorState(OOMouseInteractionMode mode, BOOL *outHidden, BOOL *outObscured)
NSTimeInterval timeIntervalAtLastClick
BOOL acceptsFirstResponder()
NSDictionary * keyMappings_normal
GameController * gameController
void resetGraphicsState()
OOGraphicsResetManager * sharedManager()
NSDictionary * dictionaryFromFilesNamed:inFolder:mergeMode:cache:(NSString *fileName,[inFolder] NSString *folderName,[mergeMode] OOResourceMergeMode mergeMode,[cache] BOOL useCache)
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque