Merge branch 'master' into jgrpp

# Conflicts:
#	src/os/macosx/macos.mm
#	src/video/cocoa/cocoa_v.mm
#	src/video/cocoa/fullscreen.mm
#	src/video/cocoa/wnd_quartz.mm
#	src/video/cocoa/wnd_quickdraw.mm
This commit is contained in:
Jonathan G Rennison
2020-04-30 23:59:06 +01:00
22 changed files with 218 additions and 2157 deletions

View File

@@ -189,17 +189,9 @@ public:
extern CocoaSubdriver *_cocoa_subdriver;
CocoaSubdriver *QZ_CreateFullscreenSubdriver(int width, int height, int bpp);
#ifdef ENABLE_COCOA_QUICKDRAW
CocoaSubdriver *QZ_CreateWindowQuickdrawSubdriver(int width, int height, int bpp);
#endif
#ifdef ENABLE_COCOA_QUARTZ
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
CocoaSubdriver *QZ_CreateWindowQuartzSubdriver(int width, int height, int bpp);
#endif
#endif
void QZ_GameSizeChanged();
@@ -229,16 +221,7 @@ uint QZ_ListModes(OTTD_Point *modes, uint max_modes, CGDirectDisplayID display_i
@end
/** Subclass of NSView to fix Quartz rendering and mouse awareness */
@interface OTTD_CocoaView : NSView
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
# if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4
<NSTextInputClient, NSTextInput>
# else
<NSTextInputClient>
# endif /* MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4 */
#else
<NSTextInput>
#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 */
@interface OTTD_CocoaView : NSView <NSTextInputClient>
{
CocoaSubdriver *driver;
NSTrackingRectTag trackingtag;
@@ -258,10 +241,7 @@ uint QZ_ListModes(OTTD_Point *modes, uint max_modes, CGDirectDisplayID display_i
@end
/** Delegate for our NSWindow to send ask for quit on close */
@interface OTTD_CocoaWindowDelegate : NSObject
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
<NSWindowDelegate>
#endif
@interface OTTD_CocoaWindowDelegate : NSObject <NSWindowDelegate>
{
CocoaSubdriver *driver;
}

View File

@@ -45,10 +45,7 @@
*/
@interface OTTDMain : NSObject
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
<NSApplicationDelegate>
#endif
@interface OTTDMain : NSObject <NSApplicationDelegate>
@end
@@ -159,7 +156,9 @@ static void setApplicationMenu()
/* Tell the application object that this is now the application menu.
* This interesting Objective-C construct is used because not all SDK
* versions define this method publicly. */
[ NSApp performSelector:@selector(setAppleMenu:) withObject:appleMenu ];
if ([ NSApp respondsToSelector:@selector(setAppleMenu:) ]) {
[ NSApp performSelector:@selector(setAppleMenu:) withObject:appleMenu ];
}
/* Finally give up our references to the objects */
[ appleMenu release ];
@@ -204,13 +203,9 @@ static void setupApplication()
/* Ensure the application object is initialised */
[ NSApplication sharedApplication ];
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
/* Tell the dock about us */
if (MacOSVersionIsAtLeast(10, 3, 0)) {
OSStatus returnCode = TransformProcessType(&psn, kProcessTransformToForegroundApplication);
if (returnCode != 0) DEBUG(driver, 0, "Could not change to foreground application. Error %d", (int)returnCode);
}
#endif
OSStatus returnCode = TransformProcessType(&psn, kProcessTransformToForegroundApplication);
if (returnCode != 0) DEBUG(driver, 0, "Could not change to foreground application. Error %d", (int)returnCode);
/* Disable the system-wide tab feature as we only have one window. */
if ([ NSWindow respondsToSelector:@selector(setAllowsAutomaticWindowTabbing:) ]) {
@@ -243,57 +238,27 @@ static bool ModeSorter(const OTTD_Point &p1, const OTTD_Point &p2)
static void QZ_GetDisplayModeInfo(CFArrayRef modes, CFIndex i, int &bpp, uint16 &width, uint16 &height)
{
bpp = 0;
width = 0;
height = 0;
CGDisplayModeRef mode = (CGDisplayModeRef)CFArrayGetValueAtIndex(modes, i);
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
if (MacOSVersionIsAtLeast(10, 6, 0)) {
CGDisplayModeRef mode = (CGDisplayModeRef)CFArrayGetValueAtIndex(modes, i);
width = (uint16)CGDisplayModeGetWidth(mode);
height = (uint16)CGDisplayModeGetHeight(mode);
width = (uint16)CGDisplayModeGetWidth(mode);
height = (uint16)CGDisplayModeGetHeight(mode);
#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_11)
/* Extract bit depth from mode string. */
CFAutoRelease<CFStringRef> pixEnc(CGDisplayModeCopyPixelEncoding(mode));
if (CFStringCompare(pixEnc.get(), CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) bpp = 32;
if (CFStringCompare(pixEnc.get(), CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) bpp = 16;
if (CFStringCompare(pixEnc.get(), CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) bpp = 8;
/* Extract bit depth from mode string. */
CFAutoRelease<CFStringRef> pixEnc(CGDisplayModeCopyPixelEncoding(mode));
if (CFStringCompare(pixEnc.get(), CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) bpp = 32;
if (CFStringCompare(pixEnc.get(), CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) bpp = 16;
if (CFStringCompare(pixEnc.get(), CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) bpp = 8;
#else
/* CGDisplayModeCopyPixelEncoding is deprecated on OSX 10.11+, but there are no 8 bpp modes anyway... */
bpp = 32;
/* CGDisplayModeCopyPixelEncoding is deprecated on OSX 10.11+, but there are no 8 bpp modes anyway... */
bpp = 32;
#endif
} else
#endif
{
int intvalue;
CFDictionaryRef onemode = (const __CFDictionary*)CFArrayGetValueAtIndex(modes, i);
CFNumberRef number = (const __CFNumber*)CFDictionaryGetValue(onemode, kCGDisplayBitsPerPixel);
CFNumberGetValue(number, kCFNumberSInt32Type, &intvalue);
bpp = intvalue;
number = (const __CFNumber*)CFDictionaryGetValue(onemode, kCGDisplayWidth);
CFNumberGetValue(number, kCFNumberSInt32Type, &intvalue);
width = (uint16)intvalue;
number = (const __CFNumber*)CFDictionaryGetValue(onemode, kCGDisplayHeight);
CFNumberGetValue(number, kCFNumberSInt32Type, &intvalue);
height = (uint16)intvalue;
}
}
uint QZ_ListModes(OTTD_Point *modes, uint max_modes, CGDirectDisplayID display_id, int device_depth)
{
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6) && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6)
CFArrayRef mode_list = MacOSVersionIsAtLeast(10, 6, 0) ? CGDisplayCopyAllDisplayModes(display_id, nullptr) : CGDisplayAvailableModes(display_id);
#elif (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
CFArrayRef mode_list = CGDisplayCopyAllDisplayModes(display_id, nullptr);
#else
CFArrayRef mode_list = CGDisplayAvailableModes(display_id);
#endif
CFIndex num_modes = CFArrayGetCount(mode_list);
CFAutoRelease<CFArrayRef> mode_list(CGDisplayCopyAllDisplayModes(display_id, nullptr));
CFIndex num_modes = CFArrayGetCount(mode_list.get());
/* Build list of modes with the requested bpp */
uint count = 0;
@@ -301,7 +266,7 @@ uint QZ_ListModes(OTTD_Point *modes, uint max_modes, CGDirectDisplayID display_i
int bpp;
uint16 width, height;
QZ_GetDisplayModeInfo(mode_list, i, bpp, width, height);
QZ_GetDisplayModeInfo(mode_list.get(), i, bpp, width, height);
if (bpp != device_depth) continue;
@@ -325,29 +290,9 @@ uint QZ_ListModes(OTTD_Point *modes, uint max_modes, CGDirectDisplayID display_i
/* Sort list smallest to largest */
std::sort(modes, modes + count, ModeSorter);
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
if (MacOSVersionIsAtLeast(10, 6, 0)) CFRelease(mode_list);
#endif
return count;
}
/** Small function to test if the main display can display 8 bpp in fullscreen */
bool QZ_CanDisplay8bpp()
{
/* 8bpp modes are deprecated starting in 10.5. CoreGraphics will return them
* as available in the display list, but many features (e.g. palette animation)
* will be broken. */
if (MacOSVersionIsAtLeast(10, 5, 0)) return false;
OTTD_Point p;
/* We want to know if 8 bpp is possible in fullscreen and not anything about
* resolutions. Because of this we want to fill a list of 1 resolution of 8 bpp
* on display 0 (main) and return if we found one. */
return QZ_ListModes(&p, 1, 0, 8);
}
/**
* Update the video modus.
*
@@ -395,35 +340,11 @@ void QZ_GameSizeChanged()
*/
static CocoaSubdriver *QZ_CreateWindowSubdriver(int width, int height, int bpp)
{
#if defined(ENABLE_COCOA_QUARTZ) || defined(ENABLE_COCOA_QUICKDRAW)
CocoaSubdriver *ret;
#endif
#if defined(ENABLE_COCOA_QUARTZ) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
/* The reason for the version mismatch is due to the fact that the 10.4 binary needs to work on 10.5 as well. */
if (MacOSVersionIsAtLeast(10, 5, 0)) {
ret = QZ_CreateWindowQuartzSubdriver(width, height, bpp);
if (ret != nullptr) return ret;
}
#endif
#ifdef ENABLE_COCOA_QUICKDRAW
ret = QZ_CreateWindowQuickdrawSubdriver(width, height, bpp);
if (ret != nullptr) return ret;
#endif
#if defined(ENABLE_COCOA_QUARTZ) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
/*
* If we get here we are running 10.4 or earlier and either openttd was compiled without the QuickDraw driver
* or it failed to load for some reason. Fall back to Quartz if possible even though that driver is slower.
*/
if (MacOSVersionIsAtLeast(10, 4, 0)) {
ret = QZ_CreateWindowQuartzSubdriver(width, height, bpp);
if (ret != nullptr) return ret;
}
#endif
#if defined(ENABLE_COCOA_QUARTZ)
return QZ_CreateWindowQuartzSubdriver(width, height, bpp);
#else
return nullptr;
#endif
}
/**
@@ -438,17 +359,8 @@ static CocoaSubdriver *QZ_CreateWindowSubdriver(int width, int height, int bpp)
*/
static CocoaSubdriver *QZ_CreateSubdriver(int width, int height, int bpp, bool fullscreen, bool fallback)
{
CocoaSubdriver *ret = nullptr;
/* OSX 10.7 allows to toggle fullscreen mode differently */
if (MacOSVersionIsAtLeast(10, 7, 0)) {
ret = QZ_CreateWindowSubdriver(width, height, bpp);
if (ret != nullptr && fullscreen) ret->ToggleFullscreen();
}
#if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_9)
else {
ret = fullscreen ? QZ_CreateFullscreenSubdriver(width, height, bpp) : QZ_CreateWindowSubdriver(width, height, bpp);
}
#endif /* (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_9) */
CocoaSubdriver *ret = QZ_CreateWindowSubdriver(width, height, bpp);
if (ret != nullptr && fullscreen) ret->ToggleFullscreen();
if (ret != nullptr) return ret;
if (!fallback) return nullptr;
@@ -458,16 +370,6 @@ static CocoaSubdriver *QZ_CreateSubdriver(int width, int height, int bpp, bool f
ret = QZ_CreateWindowSubdriver(640, 480, bpp);
if (ret != nullptr) return ret;
#if defined(_DEBUG) && (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_9)
/* This Fullscreen mode crashes on OSX 10.7 */
if (!MacOSVersionIsAtLeast(10, 7, 0)) {
/* Try fullscreen too when in debug mode */
DEBUG(driver, 0, "Setting video mode failed, falling back to 640x480 fullscreen mode.");
ret = QZ_CreateFullscreenSubdriver(640, 480, bpp);
if (ret != nullptr) return ret;
}
#endif /* defined(_DEBUG) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_9) */
return nullptr;
}
@@ -496,7 +398,7 @@ void VideoDriver_Cocoa::Stop()
*/
const char *VideoDriver_Cocoa::Start(const char * const *parm)
{
if (!MacOSVersionIsAtLeast(10, 3, 0)) return "The Cocoa video driver requires Mac OS X 10.3 or later.";
if (!MacOSVersionIsAtLeast(10, 6, 0)) return "The Cocoa video driver requires Mac OS X 10.6 or later.";
if (_cocoa_video_started) return "Already started";
_cocoa_video_started = true;
@@ -579,29 +481,7 @@ bool VideoDriver_Cocoa::ToggleFullscreen(bool full_screen)
{
assert(_cocoa_subdriver != nullptr);
/* For 10.7 and later, we try to toggle using the quartz subdriver. */
if (_cocoa_subdriver->ToggleFullscreen()) return true;
bool oldfs = _cocoa_subdriver->IsFullscreen();
if (full_screen != oldfs) {
int width = _cocoa_subdriver->GetWidth();
int height = _cocoa_subdriver->GetHeight();
int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
delete _cocoa_subdriver;
_cocoa_subdriver = nullptr;
_cocoa_subdriver = QZ_CreateSubdriver(width, height, bpp, full_screen, false);
if (_cocoa_subdriver == nullptr) {
_cocoa_subdriver = QZ_CreateSubdriver(width, height, bpp, oldfs, true);
if (_cocoa_subdriver == nullptr) error("Cocoa: Failed to create subdriver");
}
}
QZ_GameSizeChanged();
QZ_UpdateVideoModes();
return _cocoa_subdriver->IsFullscreen() == full_screen;
return _cocoa_subdriver->ToggleFullscreen();
}
/**
@@ -619,16 +499,7 @@ bool VideoDriver_Cocoa::AfterBlitterChange()
*/
void VideoDriver_Cocoa::EditBoxLostFocus()
{
if (_cocoa_subdriver != nullptr) {
if ([ _cocoa_subdriver->cocoaview respondsToSelector:@selector(inputContext) ] && [ [ _cocoa_subdriver->cocoaview performSelector:@selector(inputContext) ] respondsToSelector:@selector(discardMarkedText) ]) {
[ [ _cocoa_subdriver->cocoaview performSelector:@selector(inputContext) ] performSelector:@selector(discardMarkedText) ];
}
#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6)
else {
[ [ NSInputManager currentInputManager ] markedTextAbandoned:_cocoa_subdriver->cocoaview ];
}
#endif
}
if (_cocoa_subdriver != NULL) [ [ _cocoa_subdriver->cocoaview inputContext ] discardMarkedText ];
/* Clear any marked string from the current edit box. */
HandleTextInput(nullptr, true);
}
@@ -654,22 +525,13 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
return;
}
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3)
if (MacOSVersionIsAtLeast(10, 3, 0)) {
NSAlert *alert = [ [ NSAlert alloc ] init ];
[ alert setAlertStyle: NSCriticalAlertStyle ];
[ alert setMessageText:[ NSString stringWithUTF8String:title ] ];
[ alert setInformativeText:[ NSString stringWithUTF8String:message ] ];
[ alert addButtonWithTitle: [ NSString stringWithUTF8String:buttonLabel ] ];
[ alert runModal ];
[ alert release ];
} else
#endif
{
#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3)
NSRunAlertPanel([ NSString stringWithUTF8String:title ], [ NSString stringWithUTF8String:message ], [ NSString stringWithUTF8String:buttonLabel ], nil, nil);
#endif
}
NSAlert *alert = [ [ NSAlert alloc ] init ];
[ alert setAlertStyle: NSCriticalAlertStyle ];
[ alert setMessageText:[ NSString stringWithUTF8String:title ] ];
[ alert setInformativeText:[ NSString stringWithUTF8String:message ] ];
[ alert addButtonWithTitle: [ NSString stringWithUTF8String:buttonLabel ] ];
[ alert runModal ];
[ alert release ];
if (!wasstarted && VideoDriver::GetInstance() != nullptr) VideoDriver::GetInstance()->Stop();
@@ -1095,17 +957,7 @@ static const char *Utf8AdvanceByUtf16Units(const char *str, NSUInteger count)
{
if (!EditBoxInGlobalFocus()) return NSNotFound;
NSPoint view_pt = NSZeroPoint;
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
if ([ [ self window ] respondsToSelector:@selector(convertRectFromScreen:) ]) {
view_pt = [ self convertRect:[ [ self window ] convertRectFromScreen:NSMakeRect(thePoint.x, thePoint.y, 0, 0) ] fromView:nil ].origin;
} else
#endif
{
#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7)
view_pt = [ self convertPoint:[ [ self window ] convertScreenToBase:thePoint ] fromView:nil ];
#endif
}
NSPoint view_pt = [ self convertRect:[ [ self window ] convertRectFromScreen:NSMakeRect(thePoint.x, thePoint.y, 0, 0) ] fromView:nil ].origin;
Point pt = { (int)view_pt.x, (int)[ self frame ].size.height - (int)view_pt.y };
@@ -1128,19 +980,7 @@ static const char *Utf8AdvanceByUtf16Units(const char *str, NSUInteger count)
Rect r = _focused_window->GetTextBoundingRect(start, end);
NSRect view_rect = NSMakeRect(_focused_window->left + r.left, [ self frame ].size.height - _focused_window->top - r.bottom, r.right - r.left, r.bottom - r.top);
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
if ([ [ self window ] respondsToSelector:@selector(convertRectToScreen:) ]) {
return [ [ self window ] convertRectToScreen:[ self convertRect:view_rect toView:nil ] ];
}
#endif
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7
NSRect window_rect = [ self convertRect:view_rect toView:nil ];
NSPoint origin = [ [ self window ] convertBaseToScreen:window_rect.origin ];
return NSMakeRect(origin.x, origin.y, window_rect.size.width, window_rect.size.height);
#else
return NSMakeRect(0, 0, 0, 0);;
#endif
return [ [ self window ] convertRectToScreen:[ self convertRect:view_rect toView:nil ] ];
}
/** Get the bounding rect for the given range. */
@@ -1155,16 +995,6 @@ static const char *Utf8AdvanceByUtf16Units(const char *str, NSUInteger count)
return [ NSArray array ];
}
/** Identifier for this text input instance. */
#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
- (long)conversationIdentifier
#else
- (NSInteger)conversationIdentifier
#endif
{
return 0;
}
/** Delete single character left of the cursor. */
- (void)deleteBackward:(id)sender
{

View File

@@ -59,28 +59,12 @@ enum RightMouseButtonEmulationState {
static unsigned int _current_mods;
static bool _tab_is_down;
static bool _emulating_right_button;
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
static float _current_magnification;
#endif
#ifdef _DEBUG
static uint32 _tEvent;
#endif
/* Support for touch gestures is only available starting with the
* 10.6 SDK, even if it says that support starts in fact with 10.5.2.
* Replicate the needed stuff for older SDKs. */
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 && MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6)
static const NSUInteger NSEventTypeMagnify = 30;
static const NSUInteger NSEventTypeEndGesture = 20;
@interface NSEvent ()
/* This message is valid for events of type NSEventTypeMagnify, on 10.5.2 or later */
- (CGFloat)magnification WEAK_IMPORT_ATTRIBUTE;
@end
#endif
static uint32 GetTick()
{
struct timeval tim;
@@ -579,16 +563,13 @@ static bool QZ_PollEvent()
CGFloat deltaY;
/* Use precise scrolling-specific deltas if they're supported. */
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
if ([event respondsToSelector:@selector(hasPreciseScrollingDeltas)]) {
/* No precise deltas indicates a scroll wheel is being used, so we don't want 2D scrolling. */
if (![ event hasPreciseScrollingDeltas ]) break;
deltaX = [ event scrollingDeltaX ] * 0.5f;
deltaY = [ event scrollingDeltaY ] * 0.5f;
} else
#endif
{
} else {
deltaX = [ event deltaX ] * 5;
deltaY = [ event deltaY ] * 5;
}
@@ -598,7 +579,6 @@ static bool QZ_PollEvent()
break;
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
case NSEventTypeMagnify:
/* Pinch open or close gesture. */
_current_magnification += [ event magnification ] * 5.0f;
@@ -619,7 +599,6 @@ static bool QZ_PollEvent()
/* Gesture ended. */
_current_magnification = 0.0f;
break;
#endif
case NSCursorUpdate:
case NSMouseEntered:

View File

@@ -1,523 +0,0 @@
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/******************************************************************************
* Cocoa video driver *
* Known things left to do: *
* Scale&copy the old pixel buffer to the new one when switching resolution. *
******************************************************************************/
#ifdef WITH_COCOA
#include "../../stdafx.h"
#if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_9)
#define Rect OTTDRect
#define Point OTTDPoint
#import <Cocoa/Cocoa.h>
#undef Rect
#undef Point
#include "../../debug.h"
#include "../../core/geometry_type.hpp"
#include "../../core/sort_func.hpp"
#include "cocoa_v.h"
#include "../../gfx_func.h"
#include "../../thread.h"
#include "../../os/macosx/macos.h"
/**
* Important notice regarding all modifications!!!!!!!
* There are certain limitations because the file is objective C++.
* gdb has limitations.
* C++ and objective C code can't be joined in all cases (classes stuff).
* Read http://developer.apple.com/releasenotes/Cocoa/Objective-C++.html for more information.
*/
/* From Menus.h (according to Xcode Developer Documentation) */
extern "C" void ShowMenuBar();
extern "C" void HideMenuBar();
/* Structure for rez switch gamma fades
* We can hide the monitor flicker by setting the gamma tables to 0
*/
#define QZ_GAMMA_TABLE_SIZE 256
struct OTTD_QuartzGammaTable {
CGGammaValue red[QZ_GAMMA_TABLE_SIZE];
CGGammaValue green[QZ_GAMMA_TABLE_SIZE];
CGGammaValue blue[QZ_GAMMA_TABLE_SIZE];
};
/* Add methods to get at private members of NSScreen.
* Since there is a bug in Apple's screen switching code that does not update
* this variable when switching to fullscreen, we'll set it manually (but only
* for the main screen).
*/
@interface NSScreen (NSScreenAccess)
- (void) setFrame:(NSRect)frame;
@end
@implementation NSScreen (NSScreenAccess)
- (void) setFrame:(NSRect)frame
{
/* The 64 bits libraries don't seem to know about _frame, so this hack won't work. */
#ifndef __LP64__
_frame = frame;
#endif
}
@end
class FullscreenSubdriver : public CocoaSubdriver {
CGDirectDisplayID display_id; ///< 0 == main display (only support single display)
CFDictionaryRef cur_mode; ///< current mode of the display
CFDictionaryRef save_mode; ///< original mode of the display
CGDirectPaletteRef palette; ///< palette of an 8-bit display
/* Gamma functions to try to hide the flash from a res switch
* Fade the display from normal to black
* Save gamma tables for fade back to normal
*/
uint32 FadeGammaOut(OTTD_QuartzGammaTable *table)
{
CGGammaValue redTable[QZ_GAMMA_TABLE_SIZE];
CGGammaValue greenTable[QZ_GAMMA_TABLE_SIZE];
CGGammaValue blueTable[QZ_GAMMA_TABLE_SIZE];
unsigned int actual;
if (CGGetDisplayTransferByTable(this->display_id, QZ_GAMMA_TABLE_SIZE, table->red, table->green, table->blue, &actual) != CGDisplayNoErr
|| actual != QZ_GAMMA_TABLE_SIZE) {
return 1;
}
memcpy(redTable, table->red, sizeof(redTable));
memcpy(greenTable, table->green, sizeof(greenTable));
memcpy(blueTable, table->blue, sizeof(greenTable));
for (float percent = 1.0; percent >= 0.0; percent -= 0.01) {
for (int j = 0; j < QZ_GAMMA_TABLE_SIZE; j++) {
redTable[j] = redTable[j] * percent;
greenTable[j] = greenTable[j] * percent;
blueTable[j] = blueTable[j] * percent;
}
if (CGSetDisplayTransferByTable(this->display_id, QZ_GAMMA_TABLE_SIZE, redTable, greenTable, blueTable) != CGDisplayNoErr) {
CGDisplayRestoreColorSyncSettings();
return 1;
}
CSleep(10);
}
return 0;
}
/* Fade the display from black to normal
* Restore previously saved gamma values
*/
uint32 FadeGammaIn(const OTTD_QuartzGammaTable *table)
{
CGGammaValue redTable[QZ_GAMMA_TABLE_SIZE];
CGGammaValue greenTable[QZ_GAMMA_TABLE_SIZE];
CGGammaValue blueTable[QZ_GAMMA_TABLE_SIZE];
memset(redTable, 0, sizeof(redTable));
memset(greenTable, 0, sizeof(greenTable));
memset(blueTable, 0, sizeof(greenTable));
for (float percent = 0.0; percent <= 1.0; percent += 0.01) {
for (int j = 0; j < QZ_GAMMA_TABLE_SIZE; j++) {
redTable[j] = table->red[j] * percent;
greenTable[j] = table->green[j] * percent;
blueTable[j] = table->blue[j] * percent;
}
if (CGSetDisplayTransferByTable(this->display_id, QZ_GAMMA_TABLE_SIZE, redTable, greenTable, blueTable) != CGDisplayNoErr) {
CGDisplayRestoreColorSyncSettings();
return 1;
}
CSleep(10);
}
return 0;
}
/** Wait for the VBL to occur (estimated since we don't have a hardware interrupt) */
void WaitForVerticalBlank()
{
/* The VBL delay is based on Ian Ollmann's RezLib <iano@cco.caltech.edu> */
CFNumberRef refreshRateCFNumber = (const __CFNumber*)CFDictionaryGetValue(this->cur_mode, kCGDisplayRefreshRate);
if (refreshRateCFNumber == nullptr) return;
double refreshRate;
if (CFNumberGetValue(refreshRateCFNumber, kCFNumberDoubleType, &refreshRate) == 0) return;
if (refreshRate == 0) return;
double linesPerSecond = refreshRate * this->device_height;
double target = this->device_height;
/* Figure out the first delay so we start off about right */
double position = CGDisplayBeamPosition(this->display_id);
if (position > target) position = 0;
double adjustment = (target - position) / linesPerSecond;
CSleep((uint32)adjustment * 1000);
}
bool SetVideoMode(int w, int h, int bpp)
{
/* Define this variables at the top (against coding style) because
* otherwise GCC 4.2 barfs at the goto's jumping over variable initialization. */
NSRect screen_rect;
int gamma_error;
NSPoint mouseLocation;
/* Destroy any previous mode */
if (this->pixel_buffer != nullptr) {
free(this->pixel_buffer);
this->pixel_buffer = nullptr;
}
/* See if requested mode exists */
boolean_t exact_match;
this->cur_mode = CGDisplayBestModeForParameters(this->display_id, bpp, w, h, &exact_match);
/* If the mode wasn't an exact match, check if it has the right bpp, and update width and height */
if (!exact_match) {
int act_bpp;
CFNumberRef number = (const __CFNumber*) CFDictionaryGetValue(this->cur_mode, kCGDisplayBitsPerPixel);
CFNumberGetValue(number, kCFNumberSInt32Type, &act_bpp);
if (act_bpp != bpp) {
DEBUG(driver, 0, "Failed to find display resolution");
goto ERR_NO_MATCH;
}
number = (const __CFNumber*)CFDictionaryGetValue(this->cur_mode, kCGDisplayWidth);
CFNumberGetValue(number, kCFNumberSInt32Type, &w);
number = (const __CFNumber*)CFDictionaryGetValue(this->cur_mode, kCGDisplayHeight);
CFNumberGetValue(number, kCFNumberSInt32Type, &h);
}
/* Capture the main screen */
CGDisplayCapture(this->display_id);
/* Store the mouse coordinates relative to the total screen */
mouseLocation = [ NSEvent mouseLocation ];
mouseLocation.x /= this->device_width;
mouseLocation.y /= this->device_height;
/* Fade display to zero gamma */
OTTD_QuartzGammaTable gamma_table;
gamma_error = this->FadeGammaOut(&gamma_table);
/* Put up the blanking window (a window above all other windows) */
if (CGDisplayCapture(this->display_id) != CGDisplayNoErr ) {
DEBUG(driver, 0, "Failed capturing display");
goto ERR_NO_CAPTURE;
}
/* Do the physical switch */
if (CGDisplaySwitchToMode(this->display_id, this->cur_mode) != CGDisplayNoErr) {
DEBUG(driver, 0, "Failed switching display resolution");
goto ERR_NO_SWITCH;
}
/* Since CGDisplayBaseAddress and CGDisplayBytesPerRow are no longer available on 10.7,
* disable until a replacement can be found. */
if (MacOSVersionIsAtLeast(10, 7, 0)) {
this->window_buffer = nullptr;
this->window_pitch = 0;
} else {
#if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7)
this->window_buffer = CGDisplayBaseAddress(this->display_id);
this->window_pitch = CGDisplayBytesPerRow(this->display_id);
#endif
}
this->device_width = CGDisplayPixelsWide(this->display_id);
this->device_height = CGDisplayPixelsHigh(this->display_id);
this->device_depth = bpp;
/* Setup double-buffer emulation */
this->pixel_buffer = malloc(this->device_width * this->device_height * this->device_depth / 8);
if (this->pixel_buffer == nullptr) {
DEBUG(driver, 0, "Failed to allocate memory for double buffering");
goto ERR_DOUBLEBUF;
}
if (this->device_depth == 8 && !CGDisplayCanSetPalette(this->display_id)) {
DEBUG(driver, 0, "Not an indexed display mode.");
goto ERR_NOT_INDEXED;
}
/* If we don't hide menu bar, it will get events and interrupt the program */
HideMenuBar();
/* Hide the OS cursor */
CGDisplayHideCursor(this->display_id);
/* Fade the display to original gamma */
if (!gamma_error) FadeGammaIn(&gamma_table);
/* There is a bug in Cocoa where NSScreen doesn't synchronize
* with CGDirectDisplay, so the main screen's frame is wrong.
* As a result, coordinate translation produces incorrect results.
* We can hack around this bug by setting the screen rect ourselves.
* This hack should be removed if/when the bug is fixed.
*/
screen_rect = NSMakeRect(0, 0, this->device_width, this->device_height);
[ [ NSScreen mainScreen ] setFrame:screen_rect ];
this->UpdatePalette(0, 256);
/* Move the mouse cursor to approx. the same location */
CGPoint display_mouseLocation;
display_mouseLocation.x = mouseLocation.x * this->device_width;
display_mouseLocation.y = this->device_height - (mouseLocation.y * this->device_height);
_cursor.in_window = true;
CGDisplayMoveCursorToPoint(this->display_id, display_mouseLocation);
return true;
/* Since the blanking window covers *all* windows (even force quit) correct recovery is crucial */
ERR_NOT_INDEXED:
free(this->pixel_buffer);
this->pixel_buffer = nullptr;
ERR_DOUBLEBUF:
CGDisplaySwitchToMode(this->display_id, this->save_mode);
ERR_NO_SWITCH:
CGReleaseAllDisplays();
ERR_NO_CAPTURE:
if (!gamma_error) this->FadeGammaIn(&gamma_table);
ERR_NO_MATCH:
this->device_width = 0;
this->device_height = 0;
return false;
}
void RestoreVideoMode()
{
/* Release fullscreen resources */
OTTD_QuartzGammaTable gamma_table;
int gamma_error = this->FadeGammaOut(&gamma_table);
/* Restore original screen resolution/bpp */
CGDisplaySwitchToMode(this->display_id, this->save_mode);
CGReleaseAllDisplays();
/* Bring back the cursor */
CGDisplayShowCursor(this->display_id);
ShowMenuBar();
/* Reset the main screen's rectangle
* See comment in SetVideoMode for why we do this */
NSRect screen_rect = NSMakeRect(0, 0, CGDisplayPixelsWide(this->display_id), CGDisplayPixelsHigh(this->display_id));
[ [ NSScreen mainScreen ] setFrame:screen_rect ];
/* Destroy the pixel buffer */
if (this->pixel_buffer != nullptr) {
free(this->pixel_buffer);
this->pixel_buffer = nullptr;
}
if (!gamma_error) this->FadeGammaIn(&gamma_table);
this->device_width = CGDisplayPixelsWide(this->display_id);
this->device_height = CGDisplayPixelsHigh(this->display_id);
}
public:
FullscreenSubdriver()
{
/* Initialize the video settings; this data persists between mode switches */
this->display_id = kCGDirectMainDisplay;
this->save_mode = CGDisplayCurrentMode(this->display_id);
this->palette = CGPaletteCreateDefaultColorPalette();
this->device_width = CGDisplayPixelsWide(this->display_id);
this->device_height = CGDisplayPixelsHigh(this->display_id);
this->device_depth = 0;
this->pixel_buffer = nullptr;
this->num_dirty_rects = MAX_DIRTY_RECTS;
}
virtual ~FullscreenSubdriver()
{
this->RestoreVideoMode();
}
virtual void Draw(bool force_update)
{
const uint8 *src = (uint8 *)this->pixel_buffer;
uint8 *dst = (uint8 *)this->window_buffer;
uint pitch = this->window_pitch;
uint width = this->device_width;
uint num_dirty = this->num_dirty_rects;
uint bytesperpixel = this->device_depth / 8;
/* Check if we need to do anything */
if (num_dirty == 0) return;
if (num_dirty >= MAX_DIRTY_RECTS) {
num_dirty = 1;
this->dirty_rects[0].left = 0;
this->dirty_rects[0].top = 0;
this->dirty_rects[0].right = this->device_width;
this->dirty_rects[0].bottom = this->device_height;
}
WaitForVerticalBlank();
/* Build the region of dirty rectangles */
for (uint i = 0; i < num_dirty; i++) {
uint y = this->dirty_rects[i].top;
uint left = this->dirty_rects[i].left;
uint length = this->dirty_rects[i].right - left;
uint bottom = this->dirty_rects[i].bottom;
for (; y < bottom; y++) {
memcpy(dst + y * pitch + left * bytesperpixel, src + y * width * bytesperpixel + left * bytesperpixel, length * bytesperpixel);
}
}
this->num_dirty_rects = 0;
}
virtual void MakeDirty(int left, int top, int width, int height)
{
if (this->num_dirty_rects < MAX_DIRTY_RECTS) {
this->dirty_rects[this->num_dirty_rects].left = left;
this->dirty_rects[this->num_dirty_rects].top = top;
this->dirty_rects[this->num_dirty_rects].right = left + width;
this->dirty_rects[this->num_dirty_rects].bottom = top + height;
}
this->num_dirty_rects++;
}
virtual void UpdatePalette(uint first_color, uint num_colors)
{
if (this->device_depth != 8) return;
for (uint32_t index = first_color; index < first_color + num_colors; index++) {
/* Clamp colors between 0.0 and 1.0 */
CGDeviceColor color;
color.red = _cur_palette.palette[index].r / 255.0;
color.blue = _cur_palette.palette[index].b / 255.0;
color.green = _cur_palette.palette[index].g / 255.0;
CGPaletteSetColorAtIndex(this->palette, color, index);
}
CGDisplaySetPalette(this->display_id, this->palette);
}
virtual uint ListModes(OTTD_Point *modes, uint max_modes)
{
return QZ_ListModes(modes, max_modes, this->display_id, this->device_depth);
}
virtual bool ChangeResolution(int w, int h, int bpp)
{
int old_width = this->device_width;
int old_height = this->device_height;
int old_bpp = this->device_depth;
if (bpp != 8 && bpp != 32) error("Cocoa: This video driver only supports 8 and 32 bpp blitters.");
if (SetVideoMode(w, h, bpp)) return true;
if (old_width != 0 && old_height != 0) SetVideoMode(old_width, old_height, old_bpp);
return false;
}
virtual bool IsFullscreen()
{
return true;
}
virtual int GetWidth()
{
return this->device_width;
}
virtual int GetHeight()
{
return this->device_height;
}
virtual void *GetPixelBuffer()
{
return this->pixel_buffer;
}
/*
* Convert local coordinate to window server (CoreGraphics) coordinate.
* In fullscreen mode this just means copying the coords.
*/
virtual CGPoint PrivateLocalToCG(NSPoint *p)
{
return CGPointMake(p->x, p->y);
}
virtual NSPoint GetMouseLocation(NSEvent *event)
{
NSPoint pt = [ NSEvent mouseLocation ];
pt.y = this->device_height - pt.y;
return pt;
}
virtual bool MouseIsInsideView(NSPoint *pt)
{
return pt->x >= 0 && pt->y >= 0 && pt->x < this->device_width && pt->y < this->device_height;
}
virtual bool IsActive()
{
return true;
}
};
CocoaSubdriver *QZ_CreateFullscreenSubdriver(int width, int height, int bpp)
{
/* OSX 10.7 doesn't support this way of the fullscreen driver. If we end up here
* OpenTTD was compiled without SDK 10.7 available and - and thus we don't support
* fullscreen mode in OSX 10.7 or higher, as necessary elements for this way have
* been removed from the API.
*/
if (MacOSVersionIsAtLeast(10, 7, 0)) {
return nullptr;
}
FullscreenSubdriver *ret = new FullscreenSubdriver();
if (!ret->ChangeResolution(width, height, bpp)) {
delete ret;
return nullptr;
}
return ret;
}
#endif /* (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_9) */
#endif /* WITH_COCOA */

View File

@@ -17,8 +17,6 @@
#include "../../stdafx.h"
#include "../../os/macosx/macos.h"
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
#define Rect OTTDRect
#define Point OTTDPoint
#import <Cocoa/Cocoa.h>
@@ -194,21 +192,6 @@ void WindowQuartzSubdriver::GetDeviceInfo()
/* Initialize the video settings; this data persists between mode switches
* and gather some information that is useful to know about the display */
# if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
/* This way is deprecated as of OSX 10.6 but continues to work.Thus use it
* always, unless allowed to skip compatibility with 10.5 and earlier */
CFDictionaryRef cur_mode = CGDisplayCurrentMode(kCGDirectMainDisplay);
CFNumberGetValue(
(const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayWidth),
kCFNumberSInt32Type, &this->device_width
);
CFNumberGetValue(
(const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayHeight),
kCFNumberSInt32Type, &this->device_height
);
# else
/* Use the new API when compiling for OSX 10.6 or later */
CGDisplayModeRef cur_mode = CGDisplayCopyDisplayMode(kCGDirectMainDisplay);
if (cur_mode == nullptr) { return; }
@@ -217,7 +200,6 @@ void WindowQuartzSubdriver::GetDeviceInfo()
this->device_height = CGDisplayModeGetHeight(cur_mode);
CGDisplayModeRelease(cur_mode);
# endif
}
/** Switch to full screen mode on OSX 10.7
@@ -265,18 +247,11 @@ bool WindowQuartzSubdriver::SetVideoMode(int width, int height, int bpp)
return false;
}
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
/* Add built in full-screen support when available (OS X 10.7 and higher)
* This code actually compiles for 10.5 and later, but only makes sense in conjunction
* with the quartz fullscreen support as found only in 10.7 and later
*/
if ([this->window respondsToSelector:@selector(toggleFullScreen:)]) {
#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
/* Constants needed to build on pre-10.7 SDKs. Source: NSWindow documentation. */
const int NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7;
const int NSWindowFullScreenButton = 7;
#endif
NSWindowCollectionBehavior behavior = [ this->window collectionBehavior ];
behavior |= NSWindowCollectionBehaviorFullScreenPrimary;
[ this->window setCollectionBehavior:behavior ];
@@ -287,7 +262,6 @@ bool WindowQuartzSubdriver::SetVideoMode(int width, int height, int bpp)
[ this->window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary ];
}
#endif
[ this->window setDriver:this ];
@@ -303,10 +277,6 @@ bool WindowQuartzSubdriver::SetVideoMode(int width, int height, int bpp)
[ this->window setAcceptsMouseMovedEvents:YES ];
[ this->window setViewsNeedDisplay:NO ];
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_10
if ([ this->window respondsToSelector:@selector(useOptimizedDrawing:) ]) [ this->window useOptimizedDrawing:YES ];
#endif
delegate = [ [ OTTD_CocoaWindowDelegate alloc ] init ];
[ delegate setDriver:this ];
[ this->window setDelegate:[ delegate autorelease ] ];
@@ -494,17 +464,8 @@ CGPoint WindowQuartzSubdriver::PrivateLocalToCG(NSPoint *p)
p->y = this->window_height - p->y;
*p = [ this->cocoaview convertPoint:*p toView:nil ];
*p = [ this->window convertRectToScreen:NSMakeRect(p->x, p->y, 0, 0) ].origin;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
if ([ this->window respondsToSelector:@selector(convertRectToScreen:) ]) {
*p = [ this->window convertRectToScreen:NSMakeRect(p->x, p->y, 0, 0) ].origin;
} else
#endif
{
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7
*p = [ this->window convertBaseToScreen:*p ];
#endif
}
p->y = this->device_height - p->y;
CGPoint cgp;
@@ -519,17 +480,7 @@ NSPoint WindowQuartzSubdriver::GetMouseLocation(NSEvent *event)
NSPoint pt;
if ( [ event window ] == nil) {
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
if ([ [ this->cocoaview window ] respondsToSelector:@selector(convertRectFromScreen:) ]) {
pt = [ this->cocoaview convertPoint:[ [ this->cocoaview window ] convertRectFromScreen:NSMakeRect([ event locationInWindow ].x, [ event locationInWindow ].y, 0, 0) ].origin fromView:nil ];
}
else
#endif
{
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7
pt = [ this->cocoaview convertPoint:[ [ this->cocoaview window ] convertScreenToBase:[ event locationInWindow ] ] fromView:nil ];
#endif
}
pt = [ this->cocoaview convertPoint:[ [ this->cocoaview window ] convertRectFromScreen:NSMakeRect([ event locationInWindow ].x, [ event locationInWindow ].y, 0, 0) ].origin fromView:nil ];
} else {
pt = [ event locationInWindow ];
}
@@ -609,9 +560,9 @@ bool WindowQuartzSubdriver::WindowResized()
CocoaSubdriver *QZ_CreateWindowQuartzSubdriver(int width, int height, int bpp)
{
if (!MacOSVersionIsAtLeast(10, 4, 0)) {
DEBUG(driver, 0, "The cocoa quartz subdriver requires Mac OS X 10.4 or later.");
return nullptr;
if (!MacOSVersionIsAtLeast(10, 7, 0)) {
DEBUG(driver, 0, "The cocoa quartz subdriver requires Mac OS X 10.7 or later.");
return NULL;
}
if (bpp != 8 && bpp != 32) {
@@ -630,6 +581,5 @@ CocoaSubdriver *QZ_CreateWindowQuartzSubdriver(int width, int height, int bpp)
}
#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 */
#endif /* ENABLE_COCOA_QUARTZ */
#endif /* WITH_COCOA */

View File

@@ -1,561 +0,0 @@
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/******************************************************************************
* Cocoa video driver *
* Known things left to do: *
* List available resolutions. *
******************************************************************************/
#ifdef WITH_COCOA
#ifdef ENABLE_COCOA_QUICKDRAW
#define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_3
#include "../../stdafx.h"
#include "../../os/macosx/macos.h"
#define Rect OTTDRect
#define Point OTTDPoint
#import <Cocoa/Cocoa.h>
#undef Rect
#undef Point
#include "../../debug.h"
#include "../../rev.h"
#include "../../core/geometry_type.hpp"
#include "cocoa_v.h"
#include "../../core/math_func.hpp"
#include "../../gfx_func.h"
#include "../../framerate_type.h"
/**
* Important notice regarding all modifications!!!!!!!
* There are certain limitations because the file is objective C++.
* gdb has limitations.
* C++ and objective C code can't be joined in all cases (classes stuff).
* Read http://developer.apple.com/releasenotes/Cocoa/Objective-C++.html for more information.
*/
class WindowQuickdrawSubdriver;
class WindowQuickdrawSubdriver : public CocoaSubdriver {
private:
/**
* This function copies 32bpp pixels from the screen buffer in 16bpp windowed mode.
*
* @param left The x coord for the left edge of the box to blit.
* @param top The y coord for the top edge of the box to blit.
* @param right The x coord for the right edge of the box to blit.
* @param bottom The y coord for the bottom edge of the box to blit.
*/
void Blit32ToView32(int left, int top, int right, int bottom);
/**
* This function copies 8bpp pixels from the screen buffer in 32bpp windowed mode.
*
* @param left The x coord for the left edge of the box to blit.
* @param top The y coord for the top edge of the box to blit.
* @param right The x coord for the right edge of the box to blit.
* @param bottom The y coord for the bottom edge of the box to blit.
*/
void BlitIndexedToView32(int left, int top, int right, int bottom);
/**
* This function copies 8bpp pixels from the screen buffer in 16bpp windowed mode.
*
* @param left The x coord for the left edge of the box to blit.
* @param top The y coord for the top edge of the box to blit.
* @param right The x coord for the right edge of the box to blit.
* @param bottom The y coord for the bottom edge of the box to blit.
*/
void BlitIndexedToView16(int left, int top, int right, int bottom);
inline void BlitToView(int left, int top, int right, int bottom);
void DrawResizeIcon();
virtual void GetDeviceInfo();
virtual bool SetVideoMode(int width, int height, int bpp);
public:
WindowQuickdrawSubdriver();
virtual ~WindowQuickdrawSubdriver();
virtual void Draw(bool force_update);
virtual void MakeDirty(int left, int top, int width, int height);
virtual void UpdatePalette(uint first_color, uint num_colors);
virtual uint ListModes(OTTD_Point *modes, uint max_modes);
virtual bool ChangeResolution(int w, int h, int bpp);
virtual bool IsFullscreen() { return false; }
virtual int GetWidth() { return window_width; }
virtual int GetHeight() { return window_height; }
virtual void *GetPixelBuffer() { return pixel_buffer; }
/* Convert local coordinate to window server (CoreGraphics) coordinate */
virtual CGPoint PrivateLocalToCG(NSPoint *p);
virtual NSPoint GetMouseLocation(NSEvent *event);
virtual bool MouseIsInsideView(NSPoint *pt);
virtual bool IsActive() { return active; }
void SetPortAlphaOpaque();
bool WindowResized();
};
static const int _resize_icon_width = 16;
static const int _resize_icon_height = 16;
static bool _resize_icon[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0
};
void WindowQuickdrawSubdriver::GetDeviceInfo()
{
/* Initialize the video settings; this data persists between mode switches */
CFDictionaryRef cur_mode = CGDisplayCurrentMode(kCGDirectMainDisplay);
/* Gather some information that is useful to know about the display */
CFNumberGetValue((const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayBitsPerPixel),
kCFNumberSInt32Type, &this->device_depth);
CFNumberGetValue((const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayWidth),
kCFNumberSInt32Type, &this->device_width);
CFNumberGetValue((const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayHeight),
kCFNumberSInt32Type, &this->device_height);
}
bool WindowQuickdrawSubdriver::SetVideoMode(int width, int height, int bpp)
{
this->setup = true;
this->GetDeviceInfo();
if (bpp > this->device_depth) {
DEBUG(driver, 0, "Cannot use a blitter with a higher screen depth than the display when running in windowed mode.");
this->setup = false;
return false;
}
if (width > this->device_width) width = this->device_width;
if (height > this->device_height) height = this->device_height;
NSRect contentRect = NSMakeRect(0, 0, width, height);
/* Check if we should recreate the window */
if (this->window == nil) {
OTTD_CocoaWindowDelegate *delegate;
/* Set the window style */
unsigned int style = NSTitledWindowMask;
style |= (NSMiniaturizableWindowMask | NSClosableWindowMask);
style |= NSResizableWindowMask;
/* Manually create a window, avoids having a nib file resource */
this->window = [ [ OTTD_CocoaWindow alloc ] initWithContentRect:contentRect
styleMask:style backing:NSBackingStoreBuffered defer:NO ];
if (this->window == nil) {
DEBUG(driver, 0, "Could not create the Cocoa window.");
this->setup = false;
return false;
}
[ this->window setDriver:this ];
char caption[50];
snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision);
NSString *nsscaption = [ [ NSString alloc ] initWithUTF8String:caption ];
[ this->window setTitle:nsscaption ];
[ this->window setMiniwindowTitle:nsscaption ];
[ nsscaption release ];
[ this->window setContentMinSize:NSMakeSize(64.0f, 64.0f) ];
[ this->window setAcceptsMouseMovedEvents:YES ];
[ this->window setViewsNeedDisplay:NO ];
delegate = [ [ OTTD_CocoaWindowDelegate alloc ] init ];
[ delegate setDriver:this ];
[ this->window setDelegate: [ delegate autorelease ] ];
} else {
/* We already have a window, just change its size */
[ this->window setContentSize:contentRect.size ];
/* Ensure frame height - title bar height >= view height
* The height of title bar of the window is 22 pixels */
contentRect.size.height = Clamp(height, 0, [ this->window frame ].size.height - 22);
height = contentRect.size.height;
[ this->cocoaview setFrameSize:contentRect.size ];
}
/* Update again */
this->window_width = width;
this->window_height = height;
this->buffer_depth = bpp;
[ this->window center ];
/* Only recreate the view if it doesn't already exist */
if (this->cocoaview == nil) {
this->cocoaview = [ [ NSQuickDrawView alloc ] initWithFrame:contentRect ];
if (this->cocoaview == nil) {
DEBUG(driver, 0, "Could not create the Quickdraw view.");
this->setup = false;
return false;
}
[ this->cocoaview setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable ];
[ [ this->window contentView ] addSubview:this->cocoaview ];
[ this->cocoaview release ];
[ this->window makeKeyAndOrderFront:nil ];
}
bool ret = this->WindowResized();
this->UpdatePalette(0, 256);
this->setup = false;
return ret;
}
void WindowQuickdrawSubdriver::Blit32ToView32(int left, int top, int right, int bottom)
{
const uint32 *src = (uint32*)this->pixel_buffer;
uint32 *dst = (uint32*)this->window_buffer;
uint width = this->window_width;
uint pitch = this->window_pitch / 4;
dst += top * pitch + left;
src += top * width + left;
for (int y = top; y < bottom; y++, dst+= pitch, src+= width) {
memcpy(dst, src, (right - left) * 4);
}
}
void WindowQuickdrawSubdriver::BlitIndexedToView32(int left, int top, int right, int bottom)
{
const uint32 *pal = this->palette;
const uint8 *src = (uint8*)this->pixel_buffer;
uint32 *dst = (uint32*)this->window_buffer;
uint width = this->window_width;
uint pitch = this->window_pitch / 4;
for (int y = top; y < bottom; y++) {
for (int x = left; x < right; x++) {
dst[y * pitch + x] = pal[src[y * width + x]];
}
}
}
void WindowQuickdrawSubdriver::BlitIndexedToView16(int left, int top, int right, int bottom)
{
const uint32 *pal = this->palette;
const uint8 *src = (uint8*)this->pixel_buffer;
uint16 *dst = (uint16*)this->window_buffer;
uint width = this->window_width;
uint pitch = this->window_pitch / 2;
for (int y = top; y < bottom; y++) {
for (int x = left; x < right; x++) {
dst[y * pitch + x] = pal[src[y * width + x]];
}
}
}
inline void WindowQuickdrawSubdriver::BlitToView(int left, int top, int right, int bottom)
{
switch (this->device_depth) {
case 32:
switch (this->buffer_depth) {
case 32:
this->Blit32ToView32(left, top, right, bottom);
break;
case 8:
this->BlitIndexedToView32(left, top, right, bottom);
break;
}
break;
case 16:
this->BlitIndexedToView16(left, top, right, bottom);
break;
}
}
void WindowQuickdrawSubdriver::DrawResizeIcon()
{
int xoff = this->window_width - _resize_icon_width;
int yoff = this->window_height - _resize_icon_height;
switch (this->device_depth) {
case 32:
for (int y = 0; y < _resize_icon_height; y++) {
uint32 *trg = (uint32*)this->window_buffer + (yoff + y) * this->window_pitch / 4 + xoff;
for (int x = 0; x < _resize_icon_width; x++, trg++) {
if (_resize_icon[y * _resize_icon_width + x]) *trg = 0xff000000;
}
}
break;
case 16:
for (int y = 0; y < _resize_icon_height; y++) {
uint16 *trg = (uint16*)this->window_buffer + (yoff + y) * this->window_pitch / 2 + xoff;
for (int x = 0; x < _resize_icon_width; x++, trg++) {
if (_resize_icon[y * _resize_icon_width + x]) *trg = 0x0000;
}
}
break;
}
}
WindowQuickdrawSubdriver::WindowQuickdrawSubdriver()
{
this->window_width = 0;
this->window_height = 0;
this->buffer_depth = 0;
this->pixel_buffer = nullptr;
this->active = false;
this->setup = false;
this->window = nil;
this->cocoaview = nil;
this->num_dirty_rects = MAX_DIRTY_RECTS;
}
WindowQuickdrawSubdriver::~WindowQuickdrawSubdriver()
{
/* Release window mode resources */
if (this->window != nil) [ this->window close ];
free(this->pixel_buffer);
}
void WindowQuickdrawSubdriver::Draw(bool force_update)
{
PerformanceMeasurer framerate(PFE_VIDEO);
/* Check if we need to do anything */
if (this->num_dirty_rects == 0 || [ this->window isMiniaturized ]) return;
if (this->num_dirty_rects >= MAX_DIRTY_RECTS) {
this->num_dirty_rects = 1;
this->dirty_rects[0].left = 0;
this->dirty_rects[0].top = 0;
this->dirty_rects[0].right = this->window_width;
this->dirty_rects[0].bottom = this->window_height;
}
RgnHandle dirty = NewRgn();
RgnHandle temp = NewRgn();
SetEmptyRgn(dirty);
/* Build the region of dirty rectangles */
for (int i = 0; i < this->num_dirty_rects; i++) {
this->BlitToView(this->dirty_rects[i].left, this->dirty_rects[i].top,
this->dirty_rects[i].right, this->dirty_rects[i].bottom);
MacSetRectRgn(temp, this->dirty_rects[i].left, this->dirty_rects[i].top,
this->dirty_rects[i].right, this->dirty_rects[i].bottom);
MacUnionRgn(dirty, temp, dirty);
}
this->DrawResizeIcon();
/* Flush the dirty region */
QDFlushPortBuffer( (OpaqueGrafPtr*) [ this->cocoaview qdPort ], dirty);
DisposeRgn(dirty);
DisposeRgn(temp);
this->num_dirty_rects = 0;
}
void WindowQuickdrawSubdriver::MakeDirty(int left, int top, int width, int height)
{
if (this->num_dirty_rects < MAX_DIRTY_RECTS) {
this->dirty_rects[this->num_dirty_rects].left = left;
this->dirty_rects[this->num_dirty_rects].top = top;
this->dirty_rects[this->num_dirty_rects].right = left + width;
this->dirty_rects[this->num_dirty_rects].bottom = top + height;
}
this->num_dirty_rects++;
}
void WindowQuickdrawSubdriver::UpdatePalette(uint first_color, uint num_colors)
{
if (this->buffer_depth != 8) return;
switch (this->device_depth) {
case 32:
for (uint i = first_color; i < first_color + num_colors; i++) {
uint32 clr32 = 0xff000000;
clr32 |= (uint32)_cur_palette.palette[i].r << 16;
clr32 |= (uint32)_cur_palette.palette[i].g << 8;
clr32 |= (uint32)_cur_palette.palette[i].b;
this->palette[i] = clr32;
}
break;
case 16:
for (uint i = first_color; i < first_color + num_colors; i++) {
uint16 clr16 = 0x0000;
clr16 |= (uint16)((_cur_palette.palette[i].r >> 3) & 0x1f) << 10;
clr16 |= (uint16)((_cur_palette.palette[i].g >> 3) & 0x1f) << 5;
clr16 |= (uint16)((_cur_palette.palette[i].b >> 3) & 0x1f);
this->palette[i] = clr16;
}
break;
}
this->num_dirty_rects = MAX_DIRTY_RECTS;
}
uint WindowQuickdrawSubdriver::ListModes(OTTD_Point *modes, uint max_modes)
{
return QZ_ListModes(modes, max_modes, kCGDirectMainDisplay, this->buffer_depth);
}
bool WindowQuickdrawSubdriver::ChangeResolution(int w, int h, int bpp)
{
int old_width = this->window_width;
int old_height = this->window_height;
int old_bpp = this->buffer_depth;
if (this->SetVideoMode(w, h, bpp)) return true;
if (old_width != 0 && old_height != 0) this->SetVideoMode(old_width, old_height, old_bpp);
return false;
}
/* Convert local coordinate to window server (CoreGraphics) coordinate */
CGPoint WindowQuickdrawSubdriver::PrivateLocalToCG(NSPoint *p)
{
*p = [ this->cocoaview convertPoint:*p toView: nil ];
*p = [ this->window convertBaseToScreen:*p ];
p->y = this->device_height - p->y;
return CGPointMake(p->x, p->y);
}
NSPoint WindowQuickdrawSubdriver::GetMouseLocation(NSEvent *event)
{
NSPoint pt = [ event locationInWindow ];
pt = [ this->cocoaview convertPoint:pt fromView:nil ];
return pt;
}
bool WindowQuickdrawSubdriver::MouseIsInsideView(NSPoint *pt)
{
return [ this->cocoaview mouse:*pt inRect:[ this->cocoaview bounds ] ];
}
/* This function makes the *game region* of the window 100% opaque.
* The genie effect uses the alpha component. Otherwise,
* it doesn't seem to matter what value it has.
*/
void WindowQuickdrawSubdriver::SetPortAlphaOpaque()
{
if (this->device_depth != 32) return;
uint32 *pixels = (uint32*)this->window_buffer;
uint32 pitch = this->window_pitch / 4;
for (int y = 0; y < this->window_height; y++)
for (int x = 0; x < this->window_width; x++) {
pixels[y * pitch + x] |= 0xFF000000;
}
}
bool WindowQuickdrawSubdriver::WindowResized()
{
if (this->window == nil || this->cocoaview == nil) return true;
NSRect newframe = [ this->cocoaview frame ];
CGrafPtr thePort = (OpaqueGrafPtr*) [ this->cocoaview qdPort ];
LockPortBits(thePort);
this->window_buffer = GetPixBaseAddr(GetPortPixMap(thePort));
this->window_pitch = GetPixRowBytes(GetPortPixMap(thePort));
UnlockPortBits(thePort);
/* _cocoa_video_data.realpixels now points to the window's pixels
* We want it to point to the *view's* pixels
*/
int voff = [ this->window frame ].size.height - newframe.size.height - newframe.origin.y;
int hoff = [ this->cocoaview frame ].origin.x;
this->window_buffer = (uint8*)this->window_buffer + (voff * this->window_pitch) + hoff * (this->device_depth / 8);
this->window_width = newframe.size.width;
this->window_height = newframe.size.height;
free(this->pixel_buffer);
this->pixel_buffer = malloc(this->window_width * this->window_height * this->buffer_depth / 8);
if (this->pixel_buffer == nullptr) {
DEBUG(driver, 0, "Failed to allocate pixel buffer");
return false;
}
QZ_GameSizeChanged();
/* Redraw screen */
this->num_dirty_rects = MAX_DIRTY_RECTS;
return true;
}
CocoaSubdriver *QZ_CreateWindowQuickdrawSubdriver(int width, int height, int bpp)
{
WindowQuickdrawSubdriver *ret;
if (MacOSVersionIsAtLeast(10, 5, 0)) {
DEBUG(driver, 0, "The cocoa quickdraw subdriver is not recommended for Mac OS X 10.5 or later.");
}
if (bpp != 8 && bpp != 32) {
DEBUG(driver, 0, "The cocoa quickdraw subdriver only supports 8 and 32 bpp.");
return nullptr;
}
ret = new WindowQuickdrawSubdriver();
if (!ret->ChangeResolution(width, height, bpp)) {
delete ret;
return nullptr;
}
return ret;
}
#endif /* ENABLE_COCOA_QUICKDRAW */
#endif /* WITH_COCOA */