os/macosx/macos.m
author rubidium
Mon, 01 Jan 2007 22:00:54 +0000
changeset 5462 ebb6fe93a23a
parent 3130 c65ad41ee90b
permissions -rw-r--r--
(svn r7731) -Fix (r5999): off-by-one error in the date to YMD calculation for first 4 years of a century that was not divisable by 400.
2847
65c54f935db6 (svn r3395) Add Id tag (and svn property) to macosx files.
peter1138
parents: 2827
diff changeset
     1
/* $Id$ */
65c54f935db6 (svn r3395) Add Id tag (and svn property) to macosx files.
peter1138
parents: 2827
diff changeset
     2
2188
640a0a5f77ea (svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
diff changeset
     3
#include <AppKit/AppKit.h>
640a0a5f77ea (svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
diff changeset
     4
3127
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
     5
#include <mach/mach.h>
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
     6
#include <mach/mach_host.h>
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
     7
#include <mach/host_info.h>
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
     8
#include <mach/machine.h>
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
     9
#include <stdio.h>
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    10
#include "../../stdafx.h"
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    11
#include "../../openttd.h"
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    12
#include "../../newgrf.h"
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    13
#include "../../gfx.h"
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    14
#include "../../macros.h"
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    15
#include "../../string.h"
3127
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    16
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    17
#ifndef CPU_SUBTYPE_POWERPC_970
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    18
#define CPU_SUBTYPE_POWERPC_970 ((cpu_subtype_t) 100)
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    19
#endif
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    20
2188
640a0a5f77ea (svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
diff changeset
    21
/*
640a0a5f77ea (svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
diff changeset
    22
 * This file contains objective C
640a0a5f77ea (svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
diff changeset
    23
 * Apple uses objective C instead of plain C to interact with OS specific/native functions
640a0a5f77ea (svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
diff changeset
    24
 *
640a0a5f77ea (svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
diff changeset
    25
 * Note: TrueLight's crosscompiler can handle this, but it likely needs a manual modification for each change in this file.
640a0a5f77ea (svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
diff changeset
    26
 * To insure that the crosscompiler still works, let him try any changes before they are committed
640a0a5f77ea (svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
diff changeset
    27
 */
640a0a5f77ea (svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
diff changeset
    28
3127
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    29
static char *GetOSString(void)
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    30
{
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    31
	static char buffer[175];
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    32
	const char* CPU;
3127
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    33
	char OS[20];
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    34
	char newgrf[125];
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    35
	long sysVersion;
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    36
	extern const char _openttd_revision[];
3127
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    37
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    38
	// get the hardware info
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    39
	host_basic_info_data_t hostInfo;
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    40
	mach_msg_type_number_t infoCount;
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    41
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    42
	infoCount = HOST_BASIC_INFO_COUNT;
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    43
	host_info(
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    44
		mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    45
	);
3127
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    46
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    47
	// replace the hardware info with strings, that tells a bit more than just an int
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    48
	switch (hostInfo.cpu_subtype) {
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    49
#ifdef __POWERPC__
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    50
		case CPU_SUBTYPE_POWERPC_750:  CPU = "G3"; break;
3127
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    51
		case CPU_SUBTYPE_POWERPC_7400:
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    52
		case CPU_SUBTYPE_POWERPC_7450: CPU = "G4"; break;
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    53
		case CPU_SUBTYPE_POWERPC_970:  CPU = "G5"; break;
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    54
		default:                       CPU = "Unknown PPC"; break;
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    55
#else
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    56
		/* it looks odd to have a switch for two cases, but it leaves room for easy
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    57
		 * expansion. Odds are that Apple will some day use newer CPUs than i686
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    58
		 */
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    59
		case CPU_SUBTYPE_PENTPRO: CPU = "i686"; break;
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    60
		default:                  CPU = "Unknown Intel"; break;
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    61
#endif
3127
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    62
	}
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    63
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    64
	// get the version of OSX
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    65
	if (Gestalt(gestaltSystemVersion, &sysVersion) != noErr) {
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    66
		sprintf(OS, "Undetected");
3127
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    67
	} else {
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    68
		int majorHiNib = GB(sysVersion, 12, 4);
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    69
		int majorLoNib = GB(sysVersion,  8, 4);
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    70
		int minorNib   = GB(sysVersion,  4, 4);
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    71
		int bugNib     = GB(sysVersion,  0, 4);
3127
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    72
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    73
		sprintf(OS, "%d%d.%d.%d", majorHiNib, majorLoNib, minorNib, bugNib);
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    74
	}
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    75
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    76
	// make a list of used newgrf files
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    77
	if (_first_grffile != NULL) {
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    78
		char* n = newgrf;
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    79
		const GRFFile* file;
3127
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    80
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    81
		for (file = _first_grffile; file != NULL; file = file->next) {
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    82
			n = strecpy(n, " ", lastof(newgrf));
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    83
			n = strecpy(n, file->filename, lastof(newgrf));
3127
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    84
		}
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    85
	} else {
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    86
		sprintf(newgrf, "none");
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    87
	}
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    88
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    89
	snprintf(
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    90
		buffer, lengthof(buffer),
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    91
		"Please add this info: (tip: copy-paste works)\n"
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    92
		"CPU: %s, OSX: %s, OpenTTD version: %s\n"
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    93
		"NewGRF files:%s",
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    94
		CPU, OS, _openttd_revision, newgrf
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
    95
	);
3127
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    96
	return buffer;
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    97
}
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
    98
2827
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
    99
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   100
#ifdef WITH_SDL
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   101
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   102
void ShowMacDialog(const char* title, const char* message, const char* buttonLabel)
2188
640a0a5f77ea (svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
diff changeset
   103
{
640a0a5f77ea (svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
diff changeset
   104
	NSRunAlertPanel([NSString stringWithCString: title], [NSString stringWithCString: message], [NSString stringWithCString: buttonLabel], nil, nil);
640a0a5f77ea (svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
diff changeset
   105
}
640a0a5f77ea (svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
diff changeset
   106
2827
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   107
#elif defined WITH_COCOA
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   108
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   109
void CocoaDialog(const char* title, const char* message, const char* buttonLabel);
2827
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   110
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   111
void ShowMacDialog(const char* title, const char* message, const char* buttonLabel)
2827
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   112
{
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   113
	CocoaDialog(title, message, buttonLabel);
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   114
}
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   115
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   116
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   117
#else
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   118
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   119
void ShowMacDialog(const char* title, const char* message, const char* buttonLabel)
2827
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   120
{
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   121
	fprintf(stderr, "%s: %s\n", title, message);
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   122
}
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   123
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   124
#endif
12c9eda7edcd (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2741
diff changeset
   125
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   126
void ShowMacAssertDialog(const char* function, const char* file, const int line, const char* expression)
2223
b9564d70ec3d (svn r2741) -Feature: [OSX] added a window to display asserts on OSX (Tobin)
bjarni
parents: 2188
diff changeset
   127
{
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   128
	const char* buffer =
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   129
		[[NSString stringWithFormat:@
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   130
			"An assertion has failed and OpenTTD must quit.\n"
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   131
			"%s in %s (line %d)\n"
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   132
			"\"%s\"\n"
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   133
			"\n"
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   134
			"You should report this error the OpenTTD developers if you think you found a bug.\n"
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   135
			"\n"
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   136
			"%s",
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   137
			function, file, line, expression, GetOSString()] cString
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   138
		];
2223
b9564d70ec3d (svn r2741) -Feature: [OSX] added a window to display asserts on OSX (Tobin)
bjarni
parents: 2188
diff changeset
   139
	NSLog(@"%s", buffer);
3127
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
   140
	ToggleFullScreen(0);
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   141
	ShowMacDialog("Assertion Failed", buffer, "Quit");
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2223
diff changeset
   142
2223
b9564d70ec3d (svn r2741) -Feature: [OSX] added a window to display asserts on OSX (Tobin)
bjarni
parents: 2188
diff changeset
   143
	// abort so that a debugger has a chance to notice
b9564d70ec3d (svn r2741) -Feature: [OSX] added a window to display asserts on OSX (Tobin)
bjarni
parents: 2188
diff changeset
   144
	abort();
b9564d70ec3d (svn r2741) -Feature: [OSX] added a window to display asserts on OSX (Tobin)
bjarni
parents: 2188
diff changeset
   145
}
3127
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
   146
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
   147
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
   148
void ShowMacErrorDialog(const char *error)
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
   149
{
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   150
	const char* buffer =
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   151
		[[NSString stringWithFormat:@
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   152
			"Please update to the newest version of OpenTTD\n"
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   153
			"If the problem presists, please report this to\n"
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   154
			"http://bugs.openttd.org\n"
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   155
			"\n"
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   156
			"%s",
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   157
			GetOSString()] cString
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   158
		];
3127
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
   159
	ToggleFullScreen(0);
3130
c65ad41ee90b (svn r3745) Fix two buffer overflows, one case of undefined behavior (the destination buffer of sprintf() may not alias with one of its arguments) and some other minor stuff introduced in r3740
tron
parents: 3127
diff changeset
   160
	ShowMacDialog(error, buffer, "Quit");
3127
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
   161
	abort();
56a9c0db7c18 (svn r3740) -Feature: [OSX] added OS version, CPU type and newGRF settings to assert window and a message to tell people to add that to a bug report
bjarni
parents: 2847
diff changeset
   162
}