src/os/macosx/macos.mm
changeset 6192 c6adfc929c6b
parent 5838 9c3129cb019b
child 6298 c30fe89622df
equal deleted inserted replaced
6191:12d69f54e920 6192:c6adfc929c6b
       
     1 /* $Id$ */
       
     2 
       
     3 #include <AppKit/AppKit.h>
       
     4 
       
     5 #include <mach/mach.h>
       
     6 #include <mach/mach_host.h>
       
     7 #include <mach/host_info.h>
       
     8 #include <mach/machine.h>
       
     9 #include <stdio.h>
       
    10 #include "../../stdafx.h"
       
    11 #include "../../macros.h"
       
    12 
       
    13 #ifndef CPU_SUBTYPE_POWERPC_970
       
    14 #define CPU_SUBTYPE_POWERPC_970 ((cpu_subtype_t) 100)
       
    15 #endif
       
    16 
       
    17 /*
       
    18  * This file contains objective C
       
    19  * Apple uses objective C instead of plain C to interact with OS specific/native functions
       
    20  *
       
    21  * Note: TrueLight's crosscompiler can handle this, but it likely needs a manual modification for each change in this file.
       
    22  * To insure that the crosscompiler still works, let him try any changes before they are committed
       
    23  */
       
    24 
       
    25 void ToggleFullScreen(bool fs);
       
    26 
       
    27 static char *GetOSString(void)
       
    28 {
       
    29 	static char buffer[175];
       
    30 	const char* CPU;
       
    31 	char OS[20];
       
    32 	char newgrf[125];
       
    33 	long sysVersion;
       
    34 	extern const char _openttd_revision[];
       
    35 
       
    36 	// get the hardware info
       
    37 	host_basic_info_data_t hostInfo;
       
    38 	mach_msg_type_number_t infoCount;
       
    39 
       
    40 	infoCount = HOST_BASIC_INFO_COUNT;
       
    41 	host_info(
       
    42 		mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount
       
    43 	);
       
    44 
       
    45 	// replace the hardware info with strings, that tells a bit more than just an int
       
    46 	switch (hostInfo.cpu_subtype) {
       
    47 #ifdef __POWERPC__
       
    48 		case CPU_SUBTYPE_POWERPC_750:  CPU = "G3"; break;
       
    49 		case CPU_SUBTYPE_POWERPC_7400:
       
    50 		case CPU_SUBTYPE_POWERPC_7450: CPU = "G4"; break;
       
    51 		case CPU_SUBTYPE_POWERPC_970:  CPU = "G5"; break;
       
    52 		default:                       CPU = "Unknown PPC"; break;
       
    53 #else
       
    54 		/* it looks odd to have a switch for two cases, but it leaves room for easy
       
    55 		 * expansion. Odds are that Apple will some day use newer CPUs than i686
       
    56 		 */
       
    57 		case CPU_SUBTYPE_PENTPRO: CPU = "i686"; break;
       
    58 		default:                  CPU = "Unknown Intel"; break;
       
    59 #endif
       
    60 	}
       
    61 
       
    62 	// get the version of OSX
       
    63 	if (Gestalt(gestaltSystemVersion, &sysVersion) != noErr) {
       
    64 		sprintf(OS, "Undetected");
       
    65 	} else {
       
    66 		int majorHiNib = GB(sysVersion, 12, 4);
       
    67 		int majorLoNib = GB(sysVersion,  8, 4);
       
    68 		int minorNib   = GB(sysVersion,  4, 4);
       
    69 		int bugNib     = GB(sysVersion,  0, 4);
       
    70 
       
    71 		sprintf(OS, "%d%d.%d.%d", majorHiNib, majorLoNib, minorNib, bugNib);
       
    72 	}
       
    73 
       
    74 	// make a list of used newgrf files
       
    75 /*	if (_first_grffile != NULL) {
       
    76 		char* n = newgrf;
       
    77 		const GRFFile* file;
       
    78 
       
    79 		for (file = _first_grffile; file != NULL; file = file->next) {
       
    80 			n = strecpy(n, " ", lastof(newgrf));
       
    81 			n = strecpy(n, file->filename, lastof(newgrf));
       
    82 		}
       
    83 	} else {*/
       
    84 		sprintf(newgrf, "none");
       
    85 //	}
       
    86 
       
    87 	snprintf(
       
    88 		buffer, lengthof(buffer),
       
    89 		"Please add this info: (tip: copy-paste works)\n"
       
    90 		"CPU: %s, OSX: %s, OpenTTD version: %s\n"
       
    91 		"NewGRF files:%s",
       
    92 		CPU, OS, _openttd_revision, newgrf
       
    93 	);
       
    94 	return buffer;
       
    95 }
       
    96 
       
    97 
       
    98 #ifdef WITH_SDL
       
    99 
       
   100 void ShowMacDialog(const char* title, const char* message, const char* buttonLabel)
       
   101 {
       
   102 	NSRunAlertPanel([NSString stringWithCString: title], [NSString stringWithCString: message], [NSString stringWithCString: buttonLabel], nil, nil);
       
   103 }
       
   104 
       
   105 #elif defined WITH_COCOA
       
   106 
       
   107 void CocoaDialog(const char* title, const char* message, const char* buttonLabel);
       
   108 
       
   109 void ShowMacDialog(const char* title, const char* message, const char* buttonLabel)
       
   110 {
       
   111 	CocoaDialog(title, message, buttonLabel);
       
   112 }
       
   113 
       
   114 
       
   115 #else
       
   116 
       
   117 void ShowMacDialog(const char* title, const char* message, const char* buttonLabel)
       
   118 {
       
   119 	fprintf(stderr, "%s: %s\n", title, message);
       
   120 }
       
   121 
       
   122 #endif
       
   123 
       
   124 void ShowMacAssertDialog(const char* function, const char* file, const int line, const char* expression)
       
   125 {
       
   126 	const char* buffer =
       
   127 		[[NSString stringWithFormat:@
       
   128 			"An assertion has failed and OpenTTD must quit.\n"
       
   129 			"%s in %s (line %d)\n"
       
   130 			"\"%s\"\n"
       
   131 			"\n"
       
   132 			"You should report this error the OpenTTD developers if you think you found a bug.\n"
       
   133 			"\n"
       
   134 			"%s",
       
   135 			function, file, line, expression, GetOSString()] cString
       
   136 		];
       
   137 	NSLog(@"%s", buffer);
       
   138 	ToggleFullScreen(0);
       
   139 	ShowMacDialog("Assertion Failed", buffer, "Quit");
       
   140 
       
   141 	// abort so that a debugger has a chance to notice
       
   142 	abort();
       
   143 }
       
   144 
       
   145 
       
   146 void ShowMacErrorDialog(const char *error)
       
   147 {
       
   148 	const char* buffer =
       
   149 		[[NSString stringWithFormat:@
       
   150 			"Please update to the newest version of OpenTTD\n"
       
   151 			"If the problem presists, please report this to\n"
       
   152 			"http://bugs.openttd.org\n"
       
   153 			"\n"
       
   154 			"%s",
       
   155 			GetOSString()] cString
       
   156 		];
       
   157 	ToggleFullScreen(0);
       
   158 	ShowMacDialog(error, buffer, "Quit");
       
   159 	abort();
       
   160 }