OSDN Git Service

Merge branch 'develop' into macos-develop vauto3.0.0-alpha84-d1ec4adee
authorEric Branlund <ebranlund@fastmail.com>
Fri, 9 Jun 2023 16:51:30 +0000 (10:51 -0600)
committerEric Branlund <ebranlund@fastmail.com>
Fri, 9 Jun 2023 16:51:30 +0000 (10:51 -0600)
53 files changed:
.github/workflows/release.yaml [new file with mode: 0644]
.github/workflows/test-linux-build.yaml [new file with mode: 0644]
.github/workflows/test-mac-build.yaml [new file with mode: 0644]
.gitmodules
configure.ac
lib/edit/Makefile.am
lib/edit/quests/Makefile.am
lib/edit/towns/Makefile.am
lib/file/Makefile.am
lib/file/books/Makefile.am
lib/help/Makefile.am
lib/pref/Makefile.am
lib/pref/pref-mac.prf
lib/xtra
readme-eng.md
src/Makefile.am
src/cocoa/Angband-Cocoa.strings [new file with mode: 0644]
src/cocoa/Angband-Cocoa.xml [new file with mode: 0644]
src/cocoa/AngbandAudio.h [new file with mode: 0644]
src/cocoa/AngbandAudio.mm [new file with mode: 0644]
src/cocoa/AppDelegate.h [new file with mode: 0644]
src/cocoa/AppDelegate.m [new file with mode: 0644]
src/cocoa/Base.lproj/MainMenu.nib [new file with mode: 0644]
src/cocoa/Base.lproj/MainMenu.xib [new file with mode: 0644]
src/cocoa/Base.lproj/SoundAndMusic.nib [new file with mode: 0644]
src/cocoa/Base.lproj/SoundAndMusic.xib [new file with mode: 0644]
src/cocoa/CommandMenu.plist [new file with mode: 0644]
src/cocoa/Data.icns [new file with mode: 0644]
src/cocoa/Edit.icns [new file with mode: 0644]
src/cocoa/Save.icns [new file with mode: 0644]
src/cocoa/SoundAndMusic.h [new file with mode: 0644]
src/cocoa/SoundAndMusic.mm [new file with mode: 0644]
src/cocoa/en.lproj/CommandMenu.strings [new file with mode: 0644]
src/cocoa/en.lproj/GraphicsMenu.strings [new file with mode: 0644]
src/cocoa/en.lproj/Localizable.strings [new file with mode: 0644]
src/cocoa/hengband_Icons.icns [new file with mode: 0644]
src/cocoa/ja.lproj/CommandMenu.strings [new file with mode: 0644]
src/cocoa/ja.lproj/GraphicsMenu.strings [new file with mode: 0644]
src/cocoa/ja.lproj/Localizable.strings [new file with mode: 0644]
src/cocoa/ja.lproj/MainMenu.strings [new file with mode: 0644]
src/cocoa/ja.lproj/SoundAndMusic.strings [new file with mode: 0644]
src/core/asking-player.cpp
src/floor/floor-save.cpp
src/main-cocoa.mm [new file with mode: 0644]
src/main-win/graphics-win.cpp
src/main.cpp
src/main/angband-initializer.cpp
src/main/angband-initializer.h
src/system/grafmode.cpp [new file with mode: 0644]
src/system/grafmode.h [new file with mode: 0644]
src/system/h-config.h
src/util/angband-files.cpp
src/view/display-map.cpp

diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml
new file mode 100644 (file)
index 0000000..dc23dbb
--- /dev/null
@@ -0,0 +1,460 @@
+name: create-github-release
+
+on:
+  push:
+    branches: [ macos-develop ]
+
+env:
+    # If set and is not empty or all whitespace, this should be the list of
+    # architectures to build into a universal binary.  If not set, empty, or
+    # all whitespace, the default architecture for the combination of runner
+    # and hardware will be used.
+    UNIVERSAL_ARCHS: x86_64 arm64
+    # If set and is not empty or all whitespace, sets the name of the SDK to
+    # use.  Otherwise the default SDK for the runner will be used.  Want an
+    # SDK that can build all the architectures in UNIVERSAL_ARCHS.  For valid
+    # values of the sdk name, look at the xcrun man page.
+    #SDK_OVERRIDE: macosx11.3
+    # Used as the name when uploading or downloading the artifact for passing
+    # configuration data from the Setup job to those dependent on it.
+    CONFIG_ARTIFACT: release-config
+    # Used as the path for the file with the configuration data passed from
+    # the Setup job to those dependent on it.
+    CONFIG_ARTIFACT_PATH: release-config.txt
+    # Used as the name when uploading or downloading the artifact for passing
+    # the name of the source archive.
+    SRC_ARTIFACT: release-src
+    # Used as the path for the file with the name of the source archive in it.
+    SRC_ARTIFACT_PATH: release-src.txt
+    # Used as the name when uploading or downloading the artifact holding
+    # the source archive.
+    SRC_ARCHIVE_ARTIFACT: release-src-archive
+    # Used as the name when uploading or downloading the artifact for passing
+    # the name of the Mac archive for the English version.
+    MAC_ENGLISH_ARTIFACT: release-mac-en
+    # Used as the path for the file with the name of the Mac archive for the
+    # English version in it.
+    MAC_ENGLISH_ARTIFACT_PATH: release-mac-en.txt
+    # Used as the name when uploading or downloading the artifact holding
+    # the Mac archive for the English version.
+    MAC_ENGLISH_ARCHIVE_ARTIFACT: release-mac-en-archive
+    # Used as the name when uploading or downloading the artifact for passing
+    # the name of the Mac archive for the English version.
+    MAC_JAPANESE_ARTIFACT: release-mac-ja
+    # Used as the path for the file with the name of the Mac archive for the
+    # English version in it.
+    MAC_JAPANESE_ARTIFACT_PATH: release-mac-ja.txt
+    # Used as the name when uploading or downloading the artifact holding
+    # the Mac archive for the English version.
+    MAC_JAPANESE_ARCHIVE_ARTIFACT: release-mac-ja-archive
+
+jobs:
+  setup:
+    name: Setup
+    runs-on: ubuntu-latest
+    steps:
+      # Need commit history and tags to get the version so use 0 for
+      # fetch-depth.  Don't need the submodule(s) here.
+      - name: Clone Project
+        uses: actions/checkout@v3
+        with:
+          fetch-depth: 0
+
+      - name: Extract Names from configure.ac
+        id: get_names
+        run: |
+          name=`sed -E -n -e 's/^[[:blank:]]*AC_INIT\([[:blank:]]*\\[?//p' configure.ac | tail -1 | cut -d ']' -f 1 | cut -d ',' -f 1`
+          echo "name=$name" >> $GITHUB_OUTPUT
+          cap=`echo $name | sed -E -e 's/^a/A/' -e 's/^b/B/' -e 's/^c/C/' -e 's/^d/D/' -e 's/^e/E/' -e 's/^f/F/' -e 's/^g/G/' -e 's/^h/H/' -e 's/^i/I/' -e 's/^j/J/' -e 's/^k/K/' -e 's/^l/L/' -e 's/^m/M/' -e 's/^n/N/' -e 's/^o/O/' -e 's/^p/P/' -e 's/^q/Q/' -e 's/^r/R/' -e 's/^s/S/' -e 's/^t/T/' -e 's/^u/U/' -e 's/^v/V/' -e 's/^w/W/' -e 's/^x/X/' -e 's/^y/Y/' -e 's/^z/Z/'`
+          echo "cap=$cap" >> $GITHUB_OUTPUT
+
+      - name: Set Release Version
+        id: get_release_vars
+        run: |
+          verfile=src/system/angband-version.h
+          major=`sed -E -n -e 's/^[[:blank:]]*#define[[:blank:]]+H_VER_MAJOR[[:blank:]]+//p' "$verfile" | cut -d ' ' -f 1 | cut -f 1 | cut -d '/' -f 1`
+          minor=`sed -E -n -e 's/^[[:blank:]]*#define[[:blank:]]+H_VER_MINOR[[:blank:]]+//p' "$verfile" | cut -d ' ' -f 1 | cut -f 1 | cut -d '/' -f 1`
+          patch=`sed -E -n -e 's/^[[:blank:]]*#define[[:blank:]]+H_VER_PATCH[[:blank:]]+//p' "$verfile" | cut -d ' ' -f 1 | cut -f 1 | cut -d '/' -f 1`
+          extra=`sed -E -n -e 's/^[[:blank:]]*#define[[:blank:]]+H_VER_EXTRA[[:blank:]]+//p' "$verfile" | cut -d ' ' -f 1 | cut -f 1 | cut -d '/' -f 1`
+          version="$major"."$minor"."$patch"
+          tag=vauto"$version"
+          if test x$extra != x0 ; then
+            version="${version}-Alpha${extra}"
+            tag="${tag}-alpha${extra}"
+          fi
+          head=`git rev-parse --verify --short HEAD`
+          version="${version}-${head}"
+          tag="${tag}-${head}"
+          if git diff-index --quiet HEAD ; then
+            true
+          else
+            version="$version"-dirty
+            tag="$tag"-dirty
+          fi
+          echo "version=$version" >> $GITHUB_OUTPUT
+          echo "tag=$tag" >> $GITHUB_OUTPUT
+          prerelease=true
+          echo "prerelease=$prerelease" >> $GITHUB_OUTPUT
+          # Mark anything that isn't a prerelease as a draft.
+          draft=true
+          if test x$prerelease = xtrue ; then
+              draft=false
+          fi
+          echo "draft=$draft" >> $GITHUB_OUTPUT
+
+      # The quoting here may be too simple-minded:  what if there are single
+      # quotes in the steps.*.outputs.* stuff.
+      - name: Create Artifact with Configuration Details
+        run: |
+          echo name= '${{ steps.get_names.outputs.name }}' > $CONFIG_ARTIFACT_PATH
+          echo cap= '${{ steps.get_names.outputs.cap }}' >> $CONFIG_ARTIFACT_PATH
+          echo version= '${{ steps.get_release_vars.outputs.version }}' >> $CONFIG_ARTIFACT_PATH
+          echo tag= '${{ steps.get_release_vars.outputs.tag }}' >> $CONFIG_ARTIFACT_PATH
+          echo prerelease= '${{ steps.get_release_vars.outputs.prerelease }}' >> $CONFIG_ARTIFACT_PATH
+          echo draft= '${{ steps.get_release_vars.outputs.draft }}' >> $CONFIG_ARTIFACT_PATH
+
+      - name: Upload Artifact for Use by Dependent Steps
+        uses: actions/upload-artifact@v3
+        with:
+          name: ${{ env.CONFIG_ARTIFACT }}
+          path: ${{ env.CONFIG_ARTIFACT_PATH }}
+          retention-days: 1
+
+  source:
+    needs: setup
+    name: Source Archive
+    runs-on: ubuntu-latest
+    steps:
+      - name: Download Artifact with Configuration Information
+        uses: actions/download-artifact@v3
+        with:
+          name: ${{ env.CONFIG_ARTIFACT }}
+
+      - name: Extract Configuration Information and Store in Step Outputs
+        id: store_config
+        run: |
+          name=`sed -E -n -e 's/name= //p' $CONFIG_ARTIFACT_PATH`
+          echo "name=$name" >> $GITHUB_OUTPUT
+          version=`sed -E -n -e 's/version= //p' $CONFIG_ARTIFACT_PATH`
+          echo "version=$version" >> $GITHUB_OUTPUT
+
+      - name: Install Build Dependencies
+        run: |
+          sudo apt-get update
+          sudo apt-get install automake autoconf make tar gzip
+
+      - name: Clone Project
+        uses: actions/checkout@v3
+        with:
+          submodules: true
+
+      - name: Create Source Archive
+        id: create_source_archive
+        run: |
+          out="${{ steps.store_config.outputs.name }}"-"${{ steps.store_config.outputs.version }}"
+          echo "archive_file=${out}.tar.gz" >> $GITHUB_OUTPUT
+          ./bootstrap
+          ./configure --disable-japanese --disable-worldscore
+          make distdir
+          mv "${{ steps.store_config.outputs.name }}"-* "$out"
+          tar -cBf - "$out" | gzip -c - >"$out".tar.gz
+
+      - name: Create Artifact with Source Archive Path
+        run: |
+          echo archive_path= '${{ steps.create_source_archive.outputs.archive_file }}' > $SRC_ARTIFACT_PATH
+
+      - name: Upload Artifact with Source Archive Path
+        uses: actions/upload-artifact@v3
+        with:
+          name: ${{ env.SRC_ARTIFACT }}
+          path: ${{ env.SRC_ARTIFACT_PATH }}
+          retention-days: 1
+
+      - name: Upload Source Archive as Artifact
+        uses: actions/upload-artifact@v3
+        with:
+          name: ${{ env.SRC_ARCHIVE_ARTIFACT }}
+          path: ${{ steps.create_source_archive.outputs.archive_file }}
+          retention-days: 1
+
+  mac_en:
+    needs: [setup]
+    name: Mac English
+    runs-on: macos-latest
+    steps:
+      - name: Download Artifact with Configuration Information
+        uses: actions/download-artifact@v3
+        with:
+          name: ${{ env.CONFIG_ARTIFACT }}
+
+      - name: Extract Configuration Information and Store in Step Outputs
+        id: store_config
+        run: |
+          name=`sed -E -n -e 's/name= //p' $CONFIG_ARTIFACT_PATH`
+          echo "name=$name" >> $GITHUB_OUTPUT
+          cap=`sed -E -n -e 's/cap= //p' $CONFIG_ARTIFACT_PATH`
+          echo "cap=$cap" >> $GITHUB_OUTPUT
+          version=`sed -E -n -e 's/version= //p' $CONFIG_ARTIFACT_PATH`
+          echo "version=$version" >> $GITHUB_OUTPUT
+
+      - name: Clone Project
+        uses: actions/checkout@v3
+        with:
+          submodules: true
+
+      # Requires automake and autoconf; install those via homebrew (available
+      # by default).  Use autoconf 2.69 since autoconf 2.71 does not work well
+      # with the version of m4 (GNU M4 1.4.6) included with macOS 11 and 12.
+      - name: Install Build Dependencies
+        run: |
+          brew install m4
+          brew install autoconf@2.69
+          brew install automake
+
+      - name: Create Mac English Archive
+        id: create_mac_en_archive
+        run: |
+          if test -n `echo "${{ env.SDK_OVERRIDE }}" | tr -d ' \t\r\n'` ; then
+            SDKROOT=`echo "${{ env.SDK_OVERRIDE }}" | tr -d ' \t\r\n'`
+            export SDKROOT
+          fi
+          PATH=/usr/local/opt/autoconf@2.69/bin:"$PATH"
+          ./bootstrap
+          CFLAGS=""
+          CXXFLAGS=""
+          OBJCXXFLAGS=""
+          LDFLAGS=""
+          DEPENDENCY_TRACKING=""
+          if test -n `echo "${{ env.UNIVERSAL_ARCHS }}" | tr -d ' \t\r\n'` ; then
+            DEPENDENCY_TRACKING="--disable-dependency-tracking --disable-pch"
+            # Include what configure normally infers for the compiler flags.
+            # Without -O2, the generated executables take painfully long to
+            # read the data files.
+            CFLAGS="$CFLAGS -g -O2"
+            CXXFLAGS="$CXXFLAGS -g -O2"
+            OBJCXXFLAGS="$OBJCXXFLAGS -g -O2"
+            for arch in ${{ env.UNIVERSAL_ARCHS }} ; do
+              option="-arch $arch"
+              CFLAGS="$CFLAGS $option"
+              CXXFLAGS="$CXXFLAGS $option"
+              OBJCXXFLAGS="$OBJCXXFLAGS $option"
+              LDFLAGS="$LDFLAGS $option"
+            done
+            echo "Performing a univeral build:"
+            echo "  CFLAGS = $CFLAGS"
+            echo "  CXXFLAGS = $CXXFLAGS"
+            echo "  OBJCXXFLAGS = $OBJCXXFLAGS"
+            echo "  LDFLAGS = $LDFLAGS"
+          fi
+          env CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS" \
+            OBJCXXFLAGS="$OBJCXXFLAGS" LDFLAGS="$LDFLAGS" \
+            ./configure --enable-cocoa --disable-japanese $DEPENDENCY_TRACKING
+          make install
+          mkdir disttemp
+          mv "${{ steps.store_config.outputs.name }}".app disttemp
+          hdiutil create -quiet -volname "${{ steps.store_config.outputs.cap }}-${{ steps.store_config.outputs.version }}-English" -srcfolder disttemp disttemp.dmg
+          archive_prefix="${{ steps.store_config.outputs.cap }}-${{ steps.store_config.outputs.version }}-English"
+          echo "archive_file=${archive_prefix}.dmg" >> $GITHUB_OUTPUT
+          hdiutil convert disttemp.dmg -quiet -format UDZO -imagekey zlib-level=6 -o "${archive_prefix}.dmg"
+
+      - name: Create Artifact with Mac English Archive Path
+        run: |
+          echo archive_path= '${{ steps.create_mac_en_archive.outputs.archive_file }}' > $MAC_ENGLISH_ARTIFACT_PATH
+
+      - name: Upload Artifact with Mac English Archive Path
+        uses: actions/upload-artifact@v3
+        with:
+          name: ${{ env.MAC_ENGLISH_ARTIFACT }}
+          path: ${{ env.MAC_ENGLISH_ARTIFACT_PATH }}
+          retention-days: 1
+
+      - name: Upload Mac English Archive as Artifact
+        uses: actions/upload-artifact@v3
+        with:
+          name: ${{ env.MAC_ENGLISH_ARCHIVE_ARTIFACT }}
+          path: ${{ steps.create_mac_en_archive.outputs.archive_file }}
+          retention-days: 1
+
+  mac_ja:
+    needs: [setup]
+    name: Mac Japanese
+    runs-on: macos-latest
+    steps:
+      - name: Download Artifact with Configuration Information
+        uses: actions/download-artifact@v3
+        with:
+          name: ${{ env.CONFIG_ARTIFACT }}
+
+      - name: Extract Configuration Information and Store in Step Outputs
+        id: store_config
+        run: |
+          name=`sed -E -n -e 's/name= //p' $CONFIG_ARTIFACT_PATH`
+          echo "name=$name" >> $GITHUB_OUTPUT
+          cap=`sed -E -n -e 's/cap= //p' $CONFIG_ARTIFACT_PATH`
+          echo "cap=$cap" >> $GITHUB_OUTPUT
+          version=`sed -E -n -e 's/version= //p' $CONFIG_ARTIFACT_PATH`
+          echo "version=$version" >> $GITHUB_OUTPUT
+
+      - name: Clone Project
+        uses: actions/checkout@v3
+        with:
+          submodules: true
+
+      # Requires automake, autoconf, and nkf; install those via homebrew
+      # (available by default).  Use autoconf 2.69 since autoconf 2.71 does
+      # not work well with the version of m4 (GNU M4 1.4.6) included with
+      # macOS 11 and 12.
+      - name: Install Build Dependencies
+        run: |
+          brew install m4
+          brew install autoconf@2.69
+          brew install automake
+          brew install nkf
+
+      - name: Create Mac Japanese Archive
+        id: create_mac_ja_archive
+        run: |
+          if test -n `echo "${{ env.SDK_OVERRIDE }}" | tr -d ' \t\r\n'` ; then
+            SDKROOT=`echo "${{ env.SDK_OVERRIDE }}" | tr -d ' \t\r\n'`
+            export SDKROOT
+          fi
+          PATH=/usr/local/opt/autoconf@2.69/bin:"$PATH"
+          ./bootstrap
+          CFLAGS=""
+          CXXFLAGS=""
+          OBJCXXFLAGS=""
+          LDFLAGS=""
+          DEPENDENCY_TRACKING=""
+          if test -n `echo "${{ env.UNIVERSAL_ARCHS }}" | tr -d ' \t\r\n'` ; then
+            DEPENDENCY_TRACKING="--disable-dependency-tracking --disable-pch"
+            # Include what configure normally infers for the compiler flags.
+            # Without -O2, the generated executables take painfully long to
+            # read the data files.
+            CFLAGS="$CFLAGS -g -O2"
+            CXXFLAGS="$CXXFLAGS -g -O2"
+            OBJCXXFLAGS="$OBJCXXFLAGS -g -O2"
+            for arch in ${{ env.UNIVERSAL_ARCHS }} ; do
+              option="-arch $arch"
+              CFLAGS="$CFLAGS $option"
+              CXXFLAGS="$CXXFLAGS $option"
+              OBJCXXFLAGS="$OBJCXXFLAGS $option"
+              LDFLAGS="$LDFLAGS $option"
+            done
+            echo "Performing a univeral build:"
+            echo "  CFLAGS = $CFLAGS"
+            echo "  CXXFLAGS = $CXXFLAGS"
+            echo "  OBJCXXFLAGS = $OBJCXXFLAGS"
+            echo "  LDFLAGS = $LDFLAGS"
+          fi
+          env CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS" \
+            OBJCXXFLAGS="$OBJCXXFLAGS" LDFLAGS="$LDFLAGS" \
+            ./configure --enable-cocoa $DEPENDENCY_TRACKING
+          make install
+          mkdir disttemp
+          mv "${{ steps.store_config.outputs.name }}".app disttemp
+          hdiutil create -quiet -volname "${{ steps.store_config.outputs.cap }}-${{ steps.store_config.outputs.version}}-Japanese" -srcfolder disttemp disttemp.dmg
+          archive_prefix="${{ steps.store_config.outputs.cap }}-${{ steps.store_config.outputs.version }}-Japanese"
+          echo "archive_file=${archive_prefix}.dmg" >> $GITHUB_OUTPUT
+          hdiutil convert disttemp.dmg -quiet -format UDZO -imagekey zlib-level=6 -o "${archive_prefix}.dmg"
+
+      - name: Create Artifact with Mac Japanese Archive Path
+        run: |
+          echo archive_path= '${{ steps.create_mac_ja_archive.outputs.archive_file }}' > $MAC_JAPANESE_ARTIFACT_PATH
+
+      - name: Upload Artifact with Mac Japanese Archive Path
+        uses: actions/upload-artifact@v3
+        with:
+          name: ${{ env.MAC_JAPANESE_ARTIFACT }}
+          path: ${{ env.MAC_JAPANESE_ARTIFACT_PATH }}
+          retention-days: 1
+
+      - name: Upload Mac Japanese Archive as Artifact
+        uses: actions/upload-artifact@v3
+        with:
+          name: ${{ env.MAC_JAPANESE_ARCHIVE_ARTIFACT }}
+          path: ${{ steps.create_mac_ja_archive.outputs.archive_file }}
+          retention-days: 1
+
+  release:
+    needs: [ setup, source, mac_en, mac_ja ]
+    name: Create GitHub Release
+    runs-on: ubuntu-latest
+    steps:
+      - name: Download Artifact with Configuration Information
+        uses: actions/download-artifact@v3
+        with:
+          name: ${{ env.CONFIG_ARTIFACT }}
+
+      - name: Extract Configuration Information and Store in Step Outputs
+        id: store_config
+        run: |
+          version=`sed -E -n -e 's/version= //p' $CONFIG_ARTIFACT_PATH`
+          echo "version=$version" >> $GITHUB_OUTPUT
+          tag=`sed -E -n -e 's/tag= //p' $CONFIG_ARTIFACT_PATH`
+          echo "tag=$tag" >> $GITHUB_OUTPUT
+          prerelease=`sed -E -n -e 's/prerelease= //p' $CONFIG_ARTIFACT_PATH`
+          echo "prerelease=$prerelease" >> $GITHUB_OUTPUT
+          draft=`sed -E -n -e 's/draft= //p' $CONFIG_ARTIFACT_PATH`
+          echo "draft=$draft" >> $GITHUB_OUTPUT
+
+      - name: Download Artifact with Source Archive Path
+        uses: actions/download-artifact@v3
+        with:
+          name: ${{ env.SRC_ARTIFACT }}
+
+      - name: Extract Source Archive Path and Store in Step Outputs
+        id: store_src
+        run: |
+          archive_path=`sed -E -n -e 's/archive_path= //p' $SRC_ARTIFACT_PATH`
+          echo "archive_path=$archive_path" >> $GITHUB_OUTPUT
+
+      - name: Download Artifact with Source Archive
+        uses: actions/download-artifact@v3
+        with:
+          name: ${{ env.SRC_ARCHIVE_ARTIFACT }}
+
+      - name: Download Artifact with Mac English Archive Path
+        uses: actions/download-artifact@v3
+        with:
+          name: ${{ env.MAC_ENGLISH_ARTIFACT }}
+
+      - name: Extract Mac English Archive Path and Store in Step Outputs
+        id: store_mac_en
+        run: |
+          archive_path=`sed -E -n -e 's/archive_path= //p' $MAC_ENGLISH_ARTIFACT_PATH`
+          echo "archive_path=$archive_path" >> $GITHUB_OUTPUT
+
+      - name: Download Artifact with Mac English Archive
+        uses: actions/download-artifact@v3
+        with:
+          name: ${{ env.MAC_ENGLISH_ARCHIVE_ARTIFACT }}
+
+      - name: Download Artifact with Mac Japanese Archive Path
+        uses: actions/download-artifact@v3
+        with:
+          name: ${{ env.MAC_JAPANESE_ARTIFACT }}
+
+      - name: Extract Mac Japanese Archive Path and Store in Step Outputs
+        id: store_mac_ja
+        run: |
+          archive_path=`sed -E -n -e 's/archive_path= //p' $MAC_JAPANESE_ARTIFACT_PATH`
+          echo "archive_path=$archive_path" >> $GITHUB_OUTPUT
+
+      - name: Download Artifact with Mac Japanese Archive
+        uses: actions/download-artifact@v3
+        with:
+          name: ${{ env.MAC_JAPANESE_ARCHIVE_ARTIFACT }}
+
+      - name: Populate Release
+        uses: softprops/action-gh-release@v1
+        with:
+          tag_name: "${{ steps.store_config.outputs.tag }}"
+          name: ${{ steps.store_config.outputs.version }}
+          target_commitish: ${{ github.sha }}
+          draft: ${{ steps.store_config.outputs.draft }}
+          prerelease: ${{ steps.store_config.outputs.prerelease }}
+          files: |
+            ${{ steps.store_src.outputs.archive_path }}
+            ${{ steps.store_mac_en.outputs.archive_path }}
+            ${{ steps.store_mac_ja.outputs.archive_path }}
+          token: ${{ secrets.GITHUB_TOKEN }}
diff --git a/.github/workflows/test-linux-build.yaml b/.github/workflows/test-linux-build.yaml
new file mode 100644 (file)
index 0000000..503cec7
--- /dev/null
@@ -0,0 +1,72 @@
+# Verify that the software builds on Linux either with English or Japanese
+# enabled.
+
+name: test-linux-build
+
+on:
+  push:
+    branches: [ 'macos-develop', 'macos-2-2-1', 'macos-1-6-2' ]
+  pull_request:
+
+jobs:
+  english:
+    name: English
+    runs-on: ubuntu-latest
+    env:
+      # This will be passed to all invocations of configure for this test.
+      # Could drop --disable-worldscore if libcurl is installed.
+      DEFAULT_CONFIGURE_OPTIONS: --disable-japanese --disable-worldscore
+
+    steps:
+      - name: Clone Project
+        uses: actions/checkout@v3
+        with:
+          submodules: true
+
+      # Requires automake and autoconf; install those via apt-get.
+      - name: Install Build Dependencies
+        run: sudo apt-get install autoconf automake
+
+      - name: Build
+        run: |
+          ./bootstrap
+          ./configure ${{ env.DEFAULT_CONFIGURE_OPTIONS }}
+          make
+
+      # Use make distcheck to verify that configure.ac and the Makefile.am
+      # files properly list all the dependencies and that a build outside of
+      # of the source tree works.
+      - name: Distcheck
+        run: |
+          ./bootstrap
+          ./configure ${{ env.DEFAULT_CONFIGURE_OPTIONS }}
+          make DISTCHECK_CONFIGURE_FLAGS="${{ env.DEFAULT_CONFIGURE_OPTIONS }} --disable-pch" \
+            distcheck
+
+  # Unless building 1.6.2; make distcheck will not work with the Japanese
+  # build (the preprocessing to change the source encoding modifies the source
+  # tree) so don't test that for the Japanese build.
+  japanese:
+    name: Japanese
+    runs-on: ubuntu-latest
+    env:
+      # This will be passed to all invocations of configure for this test.
+      DEFAULT_CONFIGURE_OPTIONS: --disable-worldscore --disable-pch
+
+    steps:
+      - name: Clone Project
+        uses: actions/checkout@v3
+        with:
+          submodules: true
+
+      # Requires automake, autoconf, and, unless building the
+      # 1.6.2 version, nkf; install those via apt-get.
+      # macos-latest).
+      - name: Install Build Dependencies
+        run: sudo apt-get install automake autoconf nkf
+
+      - name: Build
+        run: |
+          ./bootstrap
+          ./configure ${{ env.DEFAULT_CONFIGURE_OPTIONS }}
+          make
diff --git a/.github/workflows/test-mac-build.yaml b/.github/workflows/test-mac-build.yaml
new file mode 100644 (file)
index 0000000..81290b4
--- /dev/null
@@ -0,0 +1,167 @@
+# Verify that the software builds on a Mac either with English or Japanese
+# enabled.
+
+name: test-mac-build
+
+on:
+  push:
+    branches: [ 'macos-develop', 'macos-2-2-1', 'macos-1-6-2' ]
+  pull_request:
+
+env:
+  # If set and is not empty or all whitespace, this should be the list of
+  # architectures to build into a universal binary.  If not set, empty, or
+  # all whitespace, the default architecture for the combination of runner and
+  # hardware will be used.
+  UNIVERSAL_ARCHS: x86_64 arm64
+  # If set and is not empty or all whitespace, sets the name of the SDK to use.
+  # Otherwise the default SDK for the runner will be used.  Want an SDK that
+  # can build all the architectures in UNIVERSAL_ARCHS.  For valid values for
+  # the sdk name, look at the xcrun man page.
+  #SDK_OVERRIDE: macosx11.3
+
+jobs:
+  english:
+    name: English
+    runs-on: macos-latest
+    env:
+      # This will be passed to all invocations of configure for this test.
+      DEFAULT_CONFIGURE_OPTIONS: --enable-cocoa --disable-japanese --disable-pch
+
+    steps:
+      - name: Clone Project
+        uses: actions/checkout@v3
+        with:
+          submodules: true
+
+      # Requires automake and autoconf; install those via homebrew (available
+      # by default).  Use autoconf 2.69 since autoconf 2.71 does not work well
+      # with the version of m4 (GNU M4 1.4.6) include with macOS 11 and 12.
+      - name: Install Build Dependencies
+        run: |
+          brew install m4
+          brew install autoconf@2.69
+          brew install automake
+
+      - name: Build
+        run: |
+          if test -n `echo "${{ env.SDK_OVERRIDE }}" | tr -d ' \t\r\n'` ; then
+            SDKROOT=`echo "${{ env.SDK_OVERRIDE }}" | tr -d ' \t\r\n'`
+            export SDKROOT
+          fi
+          PATH=/usr/local/opt/autoconf@2.69/bin:"$PATH"
+          ./bootstrap
+          CFLAGS=""
+          CXXFLAGS=""
+          OBJCXXFLAGS=""
+          LDFLAGS=""
+          DEPENDENCY_TRACKING=""
+          if test -n `echo "${{ env.UNIVERSAL_ARCHS }}" | tr -d ' \t\r\n'` ; then
+            DEPENDENCY_TRACKING=--disable-dependency-tracking
+            # Include what configure normally infers for the compiler flags.
+            # Without -O2, the generated executables take painfully long to
+            # read the data files.
+            CFLAGS="$CFLAGS -g -O2"
+            CXXFLAGS="$CXXFLAGS -g -O2"
+            OBJCXXFLAGS="$OBJCXXFLAGS -g -O2"
+            for arch in ${{ env.UNIVERSAL_ARCHS }} ; do
+              option="-arch $arch"
+              CFLAGS="$CFLAGS $option"
+              CXXFLAGS="$CXXFLAGS $option"
+              OBJCXXFLAGS="$OBJCXXFLAGS $option"
+              LDFLAGS="$LDFLAGS $option"
+            done
+            echo "Performing a univeral build:"
+            echo "  CFLAGS = $CFLAGS"
+            echo "  CXXFLAGS = $CXXFLAGS"
+            echo "  OBJCXXFLAGS = $OBJCXXFLAGS"
+            echo "  LDFLAGS = $LDFLAGS"
+          fi
+          env CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS" \
+            OBJCXXFLAGS="$OBJCXXFLAGS" LDFLAGS="$LDFLAGS" \
+            ./configure $DEPENDENCY_TRACKING ${{ env.DEFAULT_CONFIGURE_OPTIONS }}
+          # Print out the compiler version for debugging problems with a build.
+          g++ -v
+          make install
+          # Verify that some key files in the application bundle are there.
+          test -x hengband.app/Contents/MacOS/hengband || exit 1
+          test -r hengband.app/Contents/Info.plist || exit 1
+
+      # Use make distcheck to verify that configure.ac and the Makefile.am
+      # files properly list all the dependencies and that a build outside of
+      # of the source tree works.
+      - name: Distcheck
+        run: |
+          PATH=/usr/local/opt/autoconf@2.69/bin:"$PATH"
+          ./bootstrap
+          ./configure ${{ env.DEFAULT_CONFIGURE_OPTIONS }}
+          make DISTCHECK_CONFIGURE_FLAGS="${{ env.DEFAULT_CONFIGURE_OPTIONS }}" \
+            distcheck
+
+  # Unless building 1.6.2; make distcheck will not work with the Japanese
+  # build (the preprocessing to change the source encoding modifies the source
+  # tree) so don't test that for the Japanese build.
+  japanese:
+    name: Japanese
+    runs-on: macos-latest
+    env:
+      # This will be passed to all invocations of configure for this test.
+      DEFAULT_CONFIGURE_OPTIONS: --enable-cocoa --disable-pch
+
+    steps:
+      - name: Clone Project
+        uses: actions/checkout@v3
+        with:
+          submodules: true
+
+      # Requires automake, autoconf, and, unless building the 1.6.2 version,
+      # nkf; install those via homebrew (available by default).  For the same
+      # reason as above, use autoconf 2.69.
+      - name: Install Build Dependencies
+        run: |
+          brew install m4
+          brew install autoconf@2.69
+          brew install automake
+          brew install nkf
+
+      - name: Build
+        run: |
+          if test -n `echo "${{ env.SDK_OVERRIDE }}" | tr -d ' \t\r\n'` ; then
+            SDKROOT=`echo "${{ env.SDK_OVERRIDE }}" | tr -d ' \t\r\n'`
+            export SDKROOT
+          fi
+          PATH=/usr/local/opt/autoconf@2.69/bin:"$PATH"
+          ./bootstrap
+          CFLAGS=""
+          CXXFLAGS=""
+          OBJCXXFLAGS=""
+          LDFLAGS=""
+          DEPENDENCY_TRACKING=""
+          if test -n `echo "${{ env.UNIVERSAL_ARCHS }}" | tr -d ' \t\r\n'` ; then
+            DEPENDENCY_TRACKING=--disable-dependency-tracking
+            # Include what configure normally infers for the compiler flags.
+            # Without -O2, the generated executables take painfully long to
+            # read the data files.
+            CFLAGS="$CFLAGS -g -O2"
+            CXXFLAGS="$CXXFLAGS -g -O2"
+            OBJCXXFLAGS="$OBJCXXFLAGS -g -O2"
+            for arch in ${{ env.UNIVERSAL_ARCHS }} ; do
+              option="-arch $arch"
+              CFLAGS="$CFLAGS $option"
+              CXXFLAGS="$CXXFLAGS $option"
+              OBJCXXFLAGS="$OBJCXXFLAGS $option"
+              LDFLAGS="$LDFLAGS $option"
+            done
+            echo "Performing a univeral build:"
+            echo "  CFLAGS = $CFLAGS"
+            echo "  CXXFLAGS = $CXXFLAGS"
+            echo "  OBJCXXFLAGS = $OBJCXXFLAGS"
+            echo "  LDFLAGS = $LDFLAGS"
+          fi
+          env CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS" \
+            OBJCXXFLAGS="$OBJCXXFLAGS" LDFLAGS="$LDFLAGS" \
+            ./configure $DEPENDENCY_TRACKING ${{ env.DEFAULT_CONFIGURE_OPTIONS }}
+          make install
+          # Verify that some key files in the application bundle are there.
+          test -x hengband.app/Contents/MacOS/hengband || exit 1
+          test -r hengband.app/Contents/Info.plist || exit 1
index 0219e39..b600017 100644 (file)
@@ -1,4 +1,3 @@
 [submodule "lib/xtra"]
        path = lib/xtra
-       url = https://github.com/hengband/hengband.xtra
-       ignore = dirty
+       url = https://github.com/backwardsEric/hengband.xtra
index 626e82e..69c605a 100644 (file)
@@ -1,4 +1,4 @@
-dnl Process this file with autoconf to produce a configure script.
+,dnl Process this file with autoconf to produce a configure script.
 AC_INIT(hengband, 3.0.0.0)
 
 AC_CONFIG_MACRO_DIRS([m4])
@@ -21,7 +21,7 @@ if test "$GAMEGROUP" != ""; then
   MY_EXPAND_DIR(game_libpath, "$datarootdir/games/$PACKAGE/lib/")
 else
   MY_EXPAND_DIR(game_libpath, "./lib/")
-  bindir=".."
+  bindir="`pwd`"
 fi
 
 dnl overwrite the path with an user-specified value
@@ -45,6 +45,8 @@ AC_SUBST(DEFAULT_VAR_PATH)
 dnl Checks for programs.
 AC_LANG(C++)
 AC_PROG_CXX
+dnl Note that the -std= option for Objective-C++ is hardwired in
+dnl src/Makefile.am so that should be changed if this is.
 AX_CXX_COMPILE_STDCXX(20, [ext], [mandatory])
 PKG_PROG_PKG_CONFIG
 
@@ -61,6 +63,23 @@ AC_ARG_ENABLE(worldscore,
 [  --disable-worldscore    disable worldscore support], worldscore=no)
 AC_ARG_ENABLE(chuukei,
 [  --enable-chuukei        enable internet chuukei support], AC_DEFINE(CHUUKEI, 1, [Chuukei mode]))
+AC_ARG_ENABLE([cocoa],
+[  --enable-cocoa          enable a Cocoa user interface (OS X only)],
+[AC_DEFINE([MACH_O_COCOA], [1], [Use a Cocoa interface (OS X only)])], [])
+AM_CONDITIONAL([COCOA], [test x$enable_cocoa = xyes])
+dnl Since AC_PROG_OBJCXX has the side effect of setting this conditional,
+dnl guarantee that is is always set, regardless of what happens in the if
+dnl statement.
+AM_CONDITIONAL([am__fastdepOBJCXX], false)
+if test x"$enable_cocoa" = xyes ; then
+  dnl Use AC_LANG_PUSH and AC_LANG_POP to try to get back to the state that
+  dnl AC_LANG_C set in case there are side effects.
+  AC_LANG_PUSH([Objective C++])
+  AC_PROG_OBJCXX
+  AC_LANG_POP([Objective C++])
+  AC_DEFINE(SAFE_DIRECTORY, 1, [Mark var directory with version string])
+  AC_DEFINE(VERSION_STRING, "3.0.0", [Version string to use for var directory])
+fi
 AC_ARG_ENABLE([pch],
 [  --disable-pch           disable use of precompiled headers],
 enable_pch=no, enable_pch=yes)
@@ -68,13 +87,15 @@ AM_CONDITIONAL([PCH], [test x$enable_pch = xyes])
 
 dnl Checks for libraries.
 dnl Replace `main' with a function in -lncurses:
-AC_CHECK_LIB(ncursesw, initscr, [AC_DEFINE(USE_GCU, 1, [Allow -mGCU environment]) AC_DEFINE(USE_NCURSES, 1, [Use ncurses]) LIBS="$LIBS -lncursesw"])
-if test "$ac_cv_lib_ncursesw_initscr" != yes; then
-  AC_CHECK_LIB(ncurses, initscr, [AC_DEFINE(USE_GCU, 1, [Allow -mGCU environment]) AC_DEFINE(USE_NCURSES, 1, [Use ncurses]) LIBS="$LIBS -lncurses"])
-  if test "$ac_cv_lib_ncurses_initscr" != yes; then
-    AC_CHECK_LIB(curses, initscr, [AC_DEFINE(USE_GCU, 1, [Allow -mGCU environment]) LIBS="$LIBS -lcurses"])
-    if test "$ac_cv_lib_curses_initscr" != yes; then
-      AC_CHECK_LIB(termcap, tgetent, [AC_DEFINE(USE_CAP, 1, [Allow -mCAP environment]) LIBS="$LIBS -ltermcap"])
+if test x"$enable_cocoa" != xyes ; then
+  AC_CHECK_LIB(ncursesw, initscr, [AC_DEFINE(USE_GCU, 1, [Allow -mGCU environment]) AC_DEFINE(USE_NCURSES, 1, [Use ncurses]) LIBS="$LIBS -lncursesw"])
+  if test "$ac_cv_lib_ncursesw_initscr" != yes; then
+    AC_CHECK_LIB(ncurses, initscr, [AC_DEFINE(USE_GCU, 1, [Allow -mGCU environment]) AC_DEFINE(USE_NCURSES, 1, [Use ncurses]) LIBS="$LIBS -lncurses"])
+    if test "$ac_cv_lib_ncurses_initscr" != yes; then
+      AC_CHECK_LIB(curses, initscr, [AC_DEFINE(USE_GCU, 1, [Allow -mGCU environment]) LIBS="$LIBS -lcurses"])
+      if test "$ac_cv_lib_curses_initscr" != yes; then
+        AC_CHECK_LIB(termcap, tgetent, [AC_DEFINE(USE_CAP, 1, [Allow -mCAP environment]) LIBS="$LIBS -ltermcap"])
+      fi
     fi
   fi
 fi
@@ -86,13 +107,18 @@ if test "$use_japanese" = no; then
   worldscore=no
 fi
 if test "$worldscore" != no; then
-  PKG_CHECK_MODULES(libcurl, [libcurl])
+  PKG_CHECK_MODULES([libcurl], [libcurl], [true], [
+    AC_SEARCH_LIBS([curl_global_init], [curl curl-gnutls curl-nss], [true], [AC_MSG_ERROR([libcurl is necessary for posting scores])])])
   AC_DEFINE(WORLD_SCORE, 1, [Allow the game to send scores to the score server])
 fi
 
 dnl Checks for header files.
+dnl AC_PATH_XTRA has side effects on CPP which are beneficial on Mac OS X.
+dnl Removing it or conditionally executing it will likely lead to warnings
+dnl on that platform about the preprocessor disagreeing with the compiler on
+dnl the tests in AC_CHECK_HEADERS.
 AC_PATH_XTRA
-if test "$have_x" = yes; then
+AS_IF([test "$have_x" = yes && test x"$enable_cocoa" != xyes], [
   LIBS="$LIBS -lX11"
   AC_DEFINE(USE_X11, 1, [Allow -mX11 environment])
   CXXFLAGS="$X_CFLAGS $CXXFLAGS"
@@ -123,7 +149,7 @@ if test "$have_x" = yes; then
     PKG_CHECK_MODULES(XFT, [xft],
       [AC_DEFINE([USE_XFT], [1], [Use XFT])])
   ])
-fi
+])
 
 if test "$use_japanese" != no; then
   AC_CHECK_PROG(NKF, nkf, yes)
index 84004db..80493d3 100644 (file)
@@ -21,3 +21,16 @@ angband_DATA = \
   $(angband_files)
 endif
 
+if COCOA
+# APPNAME to APPRES duplicate what's in src/Makefile.am.  It would be nice to
+# avoid that, but until then, they should match.
+APPNAME = $(PACKAGE_NAME)
+APPDIR = $(APPNAME).app
+APPBNDL = $(bindir)/$(APPDIR)
+APPCONT = $(APPBNDL)/Contents
+APPRES = $(APPCONT)/Resources
+
+appdatadir = $(APPRES)/lib/edit
+appdata_DATA = \
+       $(angband_files)
+endif
index a03581d..86b4c22 100644 (file)
@@ -28,3 +28,17 @@ angbanddir = @DEFAULT_LIB_PATH@/edit/quests
 angband_DATA = \
        $(angband_files)
 endif
+
+if COCOA
+# APPNAME to APPRES duplicate what's in src/Makefile.am.  It would be nice to
+# avoid that, but until then, they should match.
+APPNAME = $(PACKAGE_NAME)
+APPDIR = ${APPNAME}.app
+APPBNDL = $(bindir)/$(APPDIR)
+APPCONT = $(APPBNDL)/Contents
+APPRES = $(APPCONT)/Resources
+
+appdatadir = $(APPRES)/lib/edit/quests
+appdata_DATA = \
+        $(angband_files)
+endif
index 3fdc489..d8fcff9 100644 (file)
@@ -19,3 +19,17 @@ angbanddir = @DEFAULT_LIB_PATH@/edit/towns
 angband_DATA = \
        $(angband_files)
 endif
+
+if COCOA
+# APPNAME to APPRES duplicate what's in src/Makefile.am.  It would be nice to
+# avoid that, but until then, they should match.
+APPNAME = $(PACKAGE_NAME)
+APPDIR = ${APPNAME}.app
+APPBNDL = $(bindir)/$(APPDIR)
+APPCONT = $(APPBNDL)/Contents
+APPRES = $(APPCONT)/Resources
+
+appdatadir = $(APPRES)/lib/edit/towns
+appdata_DATA = \
+       $(angband_files)
+endif
index 74de35b..ebfcbb3 100644 (file)
@@ -30,3 +30,16 @@ angband_DATA = \
   $(angband_files)
 endif
 
+if COCOA
+# APPNAME to APPRES duplicate what's in src/Makefile.am.  It would be nice to
+# avoid that, but until then, they should match.
+APPNAME = $(PACKAGE_NAME)
+APPDIR = $(APPNAME).app
+APPBNDL = $(bindir)/$(APPDIR)
+APPCONT = $(APPBNDL)/Contents
+APPRES = $(APPCONT)/Resources
+
+appfiledir = $(APPRES)/lib/file
+appfile_DATA = \
+       $(angband_files)
+endif
index 3857096..06ed683 100644 (file)
@@ -19,3 +19,17 @@ angbanddir = @DEFAULT_LIB_PATH@/file/books
 angband_DATA = \
        $(angband_files)
 endif
+
+if COCOA
+# APPNAME to APPRES duplicate what's in src/Makefile.am.  It would be nice to
+# avoid that, but until then, they should match.
+APPNAME = $(PACKAGE_NAME)
+APPDIR = $(APPNAME).app
+APPBNDL = $(bindir)/$(APPDIR)
+APPCONT = $(APPBNDL)/Contents
+APPRES = $(APPCONT)/Resources
+
+appfiledir = $(APPRES)/lib/file/books
+appfile_DATA = \
+       $(angband_files)
+endif
index 60b7be3..826cbf8 100644 (file)
@@ -32,3 +32,16 @@ angband_DATA = \
   $(angband_files)
 endif
 
+if COCOA
+# APPNAME to APPRES duplicate what's in src/Makefile.am.  It would be nice to
+# avoid that, but until then, they should match.
+APPNAME = $(PACKAGE_NAME)
+APPDIR = $(APPNAME).app
+APPBNDL = $(bindir)/$(APPDIR)
+APPCONT = $(APPBNDL)/Contents
+APPRES = $(APPCONT)/Resources
+
+apphelpdir = $(APPRES)/lib/help
+apphelp_DATA = \
+       $(angband_files)
+endif
index 88f2373..5b0173b 100644 (file)
@@ -23,3 +23,16 @@ angband_DATA = \
   $(angband_files)
 endif
 
+if COCOA
+# APPNAME to APPRES duplicate what's in src/Makefile.am.  It would be nice to
+# avoid that, but until then, they should match.
+APPNAME = $(PACKAGE_NAME)
+APPDIR = $(APPNAME).app
+APPBNDL = $(bindir)/$(APPDIR)
+APPCONT = $(APPBNDL)/Contents
+APPRES = $(APPCONT)/Resources
+
+appprefdir = $(APPRES)/lib/pref
+apppref_DATA = \
+       $(angband_files)
+endif
index e30e319..1694d83 100644 (file)
@@ -21,236 +21,269 @@ T:&#:CSOX:control-:shift-:option-:command-
 # T:<trigger name>:<keycode>:<keycode with shiftkey>
 # '\' in <trigger name> is escape character.
 
-T:KP_Decimal:1
-T:KP_Multiply:3
-T:KP_Add:5
-T:KP_Clear:7
-T:KP_Divide:11
-T:KP_Enter:12
-T:KP_Subtract:14
-T:KP_Equal:17
-T:KP_0:18
-T:KP_1:19
-T:KP_2:20
-T:KP_3:21
-T:KP_4:22
-T:KP_5:23
-T:KP_6:24
-T:KP_7:25
-T:KP_8:27
-T:KP_9:28
-T:F5:32
-T:F6:33
-T:F7:34
-T:F3:35
-T:F8:36
-T:F10:45
-T:F11:39
-T:F13:41
-T:F14:43
-T:F9:37
-T:F12:47
-T:F15:49
-T:Help:50
-T:Home:51
-T:Page_Up:52
-T:Delete:53
-T:F4:54
-T:End:55
-T:F2:56
-T:Page_Down:57
-T:F1:58
-T:Left:59
-T:Right:60
-T:Down:61
-T:Up:62
-
+# NSEvent.h assigns special keys to the range 0xF700 to 0XF8FF.  main_cocoa.m
+# converts all keys not in the range of 0x01 to 0x7F to a macro trigger where
+# the keycode is the key number written as a little-endian hexadecimal number
+# using '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
+# 'e', and 'f' as the digits.
+# NSUpArrowFunctionKey = 0xF700
+T:Up:K007f
+T:Down:K107f
+T:Left:K207f
+T:Right:K307f
+T:F1:407f
+T:F2:507f
+T:F3:607f
+T:F4:707f
+T:F5:807f
+T:F6:907f
+T:F7:a07f
+T:F8:b07f
+T:F9:c07f
+T:F10:d07f
+T:F11:e07f
+T:F12:f07f
+T:F13:017f
+T:F14:117f
+T:F15:217f
+T:F16:317f
+T:F17:417f
+T:F18:517f
+T:F19:617f
+# NSInsertFunctionKey = 0xF727
+T:Insert:727f
+T:Delete:827f
+T:Home:927f
+T:Begin:a27f
+T:End:b27f
+T:Page_Up:c27f
+T:Page_Down:d27f
+# NSClearLineFunctionKey = 0xF739
+T:KP_Clear:K937f
+# NSHelpFunctionKey = 0xF746
+T:Help:647f
+
+# The numeric keypad keys not handled above are also mapped (except for the
+# enter key on the keypad) to macro triggers by main-cocoa.m.  The encoding
+# of the keycode is the same as above but all of these have keycodes in the
+# range of 0x01 to 0x7f.
+T:KP_Decimal:Ke2
+T:KP_Multiply:Ka2
+T:KP_Add:Kb2
+T:KP_Divide:Kf2
+T:KP_Subtract:Kd2
+T:KP_Equal:Kd3
+# Below is what the numeric keypad enter key would be if it was handled like
+# the other numeric keypad keys by main-cocoa.m.
+T:KP_Enter:K3
+T:KP_0:K03
+T:KP_1:K13
+T:KP_2:K23
+T:KP_3:K33
+T:KP_4:K43
+T:KP_5:K53
+T:KP_6:K63
+T:KP_7:K73
+T:KP_8:K83
+T:KP_9:K93
 
 
 ##### Simple Macros #####
 
+#
+# Keys that are not digits on keypad -- same as corresponding key on main
+# keyboard
+#
+A:.
+P:\[KP_Decimal]
+
+A:*
+P:\[KP_Multiply]
+
+A:+
+P:\[KP_Add]
+
+A:/
+P:\[KP_Divide]
+
+A:\n
+P:\[KP_Enter]
+
+A:-
+P:\[KP_Subtract]
+
+A:=
+P:\[KP_Equal]
 
 #
 # Keypad -- Direction
 #
-
 A:0
-P:^_18\r
+P:\[KP_0]
 
 A:1
-P:^_19\r
+P:\[KP_1]
 
 A:2
-P:^_20\r
+P:\[KP_2]
 
 A:3
-P:^_21\r
+P:\[KP_3]
 
 A:4
-P:^_22\r
+P:\[KP_4]
 
 A:5
-P:^_23\r
+P:\[KP_5]
 
 A:6
-P:^_24\r
+P:\[KP_6]
 
 A:7
-P:^_25\r
+P:\[KP_7]
 
 A:8
-P:^_27\r
+P:\[KP_8]
 
 A:9
-P:^_28\r
-
-A:+
-P:^_05\r
-
-A:-
-P:^_14\r
-
-A:*
-P:^_03\r
-
-A:/
-P:^_11\r
-
-A:=
-P:^_17\r
-
-A:\n
-P:^_12\r
-
-A:_
-P:^_30\r
-
-A:.
-P:^_01\r
-
-A:,
-P:^_31\r
+P:\[KP_9]
 
 #
 # Shift-Keypad -- Directed running
 #
-
 A:\e\e\\.1
-P:^_S19\r
+P:\[shift-KP_1]
 
 A:\e\e\\.2
-P:^_S20\r
+P:\[shift-KP_2]
 
 A:\e\e\\.3
-P:^_S21\r
+P:\[shift-KP_3]
 
 A:\e\e\\.4
-P:^_S22\r
+P:\[shift-KP_4]
 
 A:\e\e\\.5
-P:^_S23\r
+P:\[shift-KP_5]
 
 A:\e\e\\.6
-P:^_S24\r
+P:\[shift-KP_6]
 
 A:\e\e\\.7
-P:^_S25\r
+P:\[shift-KP_7]
 
 A:\e\e\\.8
-P:^_S27\r
+P:\[shift-KP_8]
 
 A:\e\e\\.9
-P:^_S28\r
-
+P:\[shift-KP_9]
 
 #
 # Control-Keypad -- Directed tunneling
 #
-
 A:\e\e\\+1
-P:^_C19\r
+P:\[control-KP_1]
 
 A:\e\e\\+2
-P:^_C20\r
+P:\[control-KP_2]
 
 A:\e\e\\+3
-P:^_C21\r
+P:\[control-KP_3]
 
 A:\e\e\\+4
-P:^_C22\r
+P:\[control-KP_4]
 
 A:\e\e\\+5
-P:^_C23\r
+P:\[control-KP_5]
 
 A:\e\e\\+6
-P:^_C24\r
+P:\[control-KP_6]
 
 A:\e\e\\+7
-P:^_C25\r
+P:\[control-KP_7]
 
 A:\e\e\\+8
-P:^_C27\r
+P:\[control-KP_8]
 
 A:\e\e\\+9
-P:^_C28\r
-
-
+P:\[control-KP_9]
 
 #
 # Option-Control-Keypad -- wield {@0} and tunnel
 #
 
 A:\e\ew0\s\s\\+1
-P:^_CO19\r
+P:\[control-option-KP_1]
 
 A:\e\ew0\s\s\\+2
-P:^_CO20\r
+P:\[control-option-KP_2]
 
 A:\e\ew0\s\s\\+3
-P:^_CO21\r
+P:\[control-option-KP_3]
 
 A:\e\ew0\s\s\\+4
-P:^_CO22\r
+P:\[control-option-KP_4]
 
 A:\e\ew0\s\s\\+5
-P:^_CO23\r
+P:\[control-option-KP_5]
 
 A:\e\ew0\s\s\\+6
-P:^_CO24\r
+P:\[control-option-KP_6]
 
 A:\e\ew0\s\s\\+7
-P:^_CO25\r
+P:\[control-option-KP_7]
 
 A:\e\ew0\s\s\\+8
-P:^_CO27\r
+P:\[control-option-KP_8]
 
 A:\e\ew0\s\s\\+9
-P:^_CO28\r
-
+P:\[control-option-KP_9]
 
 #
 # Option-Control-Keypad-Zero -- wield {@0}
 #
 
 A:\e\ew0\s
-P:^_CO18\r
-
-
+P:\[control-option-KP_0]
 
 #
-# Hack -- Arrow-Keys
+# Use arrow keys without additional modifiers for movement
 #
 
 A:4
-P:^_59\r
+P:\[Left]
 
 A:6
-P:^_60\r
+P:\[Right]
 
 A:2
-P:^_61\r
+P:\[Down]
 
 A:8
-P:^_62\r
+P:\[Up]
 
+#
+# Shift + arrow key -- directed running
+#
+A:\e\e\\.4
+P:\[shift-Left]
+
+A:\e\e\\.6
+P:\[shift-Right]
 
+A:\e\e\\.2
+P:\[shift-Down]
+
+A:\e\e\\.8
+P:\[shift-Up]
+
+#
+# Ctrl + arrow key is trapped by the Cocoa interface so don't bother setting
+# up triggers to map it to tunneling.
+#
+
+#
+# Have the help function key bring up the in-game help.
+#
+A:?
+P:\[Help]
index 131d2b9..f4c59a9 160000 (submodule)
--- a/lib/xtra
+++ b/lib/xtra
@@ -1 +1 @@
-Subproject commit 131d2b9a14c3952c22d41de002d36e4fc6d41288
+Subproject commit f4c59a992c1952c809440944aac37986b1f5b81a
index b3d15bf..974c887 100644 (file)
@@ -102,6 +102,45 @@ You can also change other window properties:
 When using **XIM** with **XFree86 4.0** you need to set the environment variable `XMODIFIERS` to either `@im=skkinput` or `@im=kinput2`.
 
 
+## Macintosh
+
+Download the dmg file for Macintosh, double click on it to open the disk
+image, and then drag the hengband application inside it to where you want
+to store it.  Double clicking on the application will run it.  Since the
+application has not been signed, you'll likely have to make an exception
+for it to use the application.  Apple's instructions for how to do that
+are at https://support.apple.com/guide/mac-help/open-a-mac-app-from-an-unidentified-developer-mh40616/mac .
+
+In short, one way to grant the exception is:
+
+1. Control click (i.e. click while holding the control button down) on the
+application in the finder.
+2. Select "Open" in the menu that comes up.
+3. That will bring up a confirmation dialog about allowing the application to
+open on the system.
+4. If your account does not have administrator privileges, there'll be an
+additional dialog asking you to authenticate as an administrator to apply
+the exception to the security settings.
+
+For Mac OS X with Xcode installed, the Unix instructions can work to build a
+version usable from a terminal.  If XQuartz, https://www.xquartz.org , is
+installed, X11 will be used for display.
+
+For Mac OS X 10.8 or later with Xcode installed, a native version can be
+built by downloading the source archive and running
+
+```
+tar -zxvt hengband-x.x.x.tar.gz
+cd hengband-x.x.x
+./configure --disable-japanese --enable-cocoa
+make install
+```
+
+from a terminal where you subsitute x.x.x with the version number you
+downloaded.  That will build hengband.app in the hengband-x.x.x directory:
+you can then move hengband.app to where you want it to be.
+
+
 # Getting started
 
 Hengband is a roguelike game that has its roots in Moria, originally released in 1988. Across hundreds of updates
index 625e1b6..dab6a03 100644 (file)
@@ -1042,26 +1042,122 @@ EXTRA_hengband_SOURCES = \
        main-win/main-win-tokenizer.cpp main-win/main-win-tokenizer.h \
        main-win/main-win-utils.cpp main-win/main-win-utils.h \
        main-win/wav-reader.cpp main-win/wav-reader.h \
+       main-cap.cpp \
        wall.bmp \
        stdafx.cpp stdafx.h
 
+cocoa_xcode_files = \
+       cocoa/AppDelegate.m \
+       cocoa/Base.lproj/MainMenu.xib \
+       cocoa/Base.lproj/SoundAndMusic.xib
+cocoa_icon_files = \
+       cocoa/hengband_Icons.icns \
+       cocoa/Save.icns \
+       cocoa/Edit.icns \
+       cocoa/Data.icns
+cocoa_plist_template = cocoa/Angband-Cocoa.xml
+cocoa_plist_strings_template = cocoa/Angband-Cocoa.strings
+cocoa_plist_files = \
+       cocoa/CommandMenu.plist
+cocoa_en_nib_files = \
+       cocoa/Base.lproj/MainMenu.nib \
+       cocoa/Base.lproj/SoundAndMusic.nib
+cocoa_en_strings_files = \
+       cocoa/en.lproj/Localizable.strings \
+       cocoa/en.lproj/CommandMenu.strings \
+       cocoa/en.lproj/GraphicsMenu.strings
+cocoa_ja_strings_files = \
+       cocoa/ja.lproj/MainMenu.strings \
+       cocoa/ja.lproj/SoundAndMusic.strings \
+       cocoa/ja.lproj/Localizable.strings \
+       cocoa/ja.lproj/CommandMenu.strings \
+       cocoa/ja.lproj/GraphicsMenu.strings
+
 EXTRA_DIST = \
-       gcc-wrap
+       gcc-wrap \
+       $(cocoa_xcode_files) \
+       $(cocoa_icon_files) \
+       $(cocoa_plist_template) \
+       $(cocoa_plist_strings_template) \
+       $(cocoa_plist_files) \
+       $(cocoa_en_nib_files) \
+       $(cocoa_en_strings_files) \
+       $(cocoa_ja_strings_files)
 
-DEFAULT_INCLUDES = -I$(srcdir) -I$(top_builddir)/src
-CPPFLAGS += $(XFT_CFLAGS) $(libcurl_CFLAGS)
-if PCH
-CPPFLAGS += -include ../src/stdafx.h
+if COCOA
+hengband_SOURCES += \
+       main-cocoa.mm \
+       system/grafmode.h \
+       system/grafmode.cpp \
+       cocoa/AppDelegate.h \
+       cocoa/AngbandAudio.h \
+       cocoa/AngbandAudio.mm \
+       cocoa/SoundAndMusic.h \
+       cocoa/SoundAndMusic.mm
+AM_CFLAGS = -mmacosx-version-min=10.15 -Wunguarded-availability
+AM_OBJCXXFLAGS = -std=c++20 -fobjc-arc -mmacosx-version-min=10.15 -Wunguarded-availability -stdlib=libc++
+AM_CXXFLAGS = -mmacosx-version-min=10.15 -Wunguarded-availability -stdlib=libc++
+hengband_LDFLAGS = -framework cocoa -framework AVFoundation $(AM_LDFLAGS)
+hengband_LINK = MACOSX_DEPLOYMENT_TARGET=10.15 $(OBJCXXLINK) $(hengband_LDFLAGS) $(LDFLAGS) -o $@
+APPNAME = $(PACKAGE_NAME)
+APPEXE = hengband
+APPDIR = $(APPNAME).app
+BUNDLE_IDENTIFIER = jp.osdn.hengband
+BUNDLE_VERSION = $(PACKAGE_VERSION)
+BUNDLE_DISPLAY_NAME = $(APPNAME)
+BUNDLE_NAME = $(BUNDLE_DISPLAY_NAME)
+BUNDLE_DISPLAY_NAME_JA = 変愚蛮怒
+BUNDLE_NAME_JA = $(BUNDLE_DISPLAY_NAME_JA)
+# Be careful with characters (like '&') in the copyright that have special
+# meanings to sed.
+COPYRIGHT = Copyright © Mr. Hoge and many others
+# For now, using the line that appears in news_j.txt.  Is that appropriate?
+# The ampersand is a kanji so it doesn't seem to be treated as a special
+# character by sed.
+COPYRIGHT_JA = Mr.hoge (echizen@users.sourceforge.jp) & 多くの方々
+APPBNDL = $(bindir)/$(APPDIR)
+APPCONT = $(APPBNDL)/Contents
+APPBIN = $(APPCONT)/MacOS
+APPRES = $(APPCONT)/Resources
+appbin_PROGRAMS = $(APPEXE)
+appbindir = $(APPBIN)
+dist_appicon_DATA = $(cocoa_icon_files)
+appicondir = $(APPRES)
+appplist_DATA = $(cocoa_plist_files)
+appplistdir = $(APPRES)
+appennib_DATA = $(cocoa_en_nib_files)
+appennibdir = $(APPRES)/Base.lproj
+appen_DATA = $(cocoa_en_strings_files)
+appendir = $(APPRES)/en.lproj
+appja_DATA = $(cocoa_ja_strings_files)
+appjadir = $(APPRES)/ja.lproj
+else
+EXTRA_hengband_SOURCES += main-cocoa.mm system/grafmode.h system/grafmode.cpp \
+       cocoa/AppDelegate.h
+hengband_LINK = $(CXXLINK)
 endif
+
+# The "-I$(top_builddir)/src/cocoa" is there so can use the same include
+# directives in the cocoa/*.{h,mm} files when building here or rebuilding the
+# nib files in Xcode according to the procedure in cocoa/AppDelegate.m.
+DEFAULT_INCLUDES = -I$(srcdir) -I$(top_builddir)/src -I$(top_builddir)/src/cocoa
+CPPFLAGS += $(XFT_CFLAGS) $(libcurl_CFLAGS)
 LIBS += $(XFT_LIBS) $(libcurl_LIBS)
 COMPILE = $(srcdir)/gcc-wrap $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
        $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
+OBJCXXCOMPILE = $(srcdir)/gcc-wrap $(OBJCXX) $(DEFS) $(DEFAULT_INCLUDES) \
+       $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_OBJCXXFLAGS) $(OBJCXXFLAGS)
 CXXCOMPILE = $(srcdir)/gcc-wrap $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
-       $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CXXFLAGS)
+       $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)
 
 if PCH
+# Would use CPPFLAGS, but that affects both the C++ and Objective-C++
+# preprocessing:  don't want to use the precompiled header for the
+# Objective-C++ code.
+CXXCOMPILE += -include ../src/stdafx.h
+
 stdafx.h.gch: stdafx.h stdafx.cpp Makefile
-       $(CXX) -x c++-header $(CXXFLAGS) $(srcdir)/stdafx.cpp -o $@
+       $(CXX) -x c++-header $(CXXFLAGS) $(AM_CXXFLAGS) $(srcdir)/stdafx.cpp -o $@
        if test none != "$(PCH_CHECKSUMMER)" ; then \
                rm -f "$@".sum ; \
                $(PCH_CHECKSUMMER) $@ > "$@".sum ; \
@@ -1076,6 +1172,51 @@ if SET_GID
        chmod g+s "$(DESTDIR)$(bindir)/hengband"
 endif
 
+if COCOA
+# Suppress the warnings from the EUC-JP encoded strings used when building
+# the Japanese version.
+AM_CFLAGS += -Wno-invalid-source-encoding
+AM_CXXFLAGS += -Wno-invalid-source-encoding
+AM_OBJCXXFLAGS += -Wno-invalid-source-encoding
+
+# Create lib/info within the application bundle since not currently
+# installing anything to it.  Other relevant lib directories should
+# be created from the Makefiles in the lib directory.
+install-data-local:
+       @mkdir -p $(APPCONT)
+       @mkdir -p $(appendir)
+       @mkdir -p $(appjadir)
+       @mkdir -p $(APPRES)/lib/info
+       sed -e 's/\$$VERSION\$$/$(PACKAGE_VERSION)/' \
+               -e 's/\$$COPYRIGHT\$$/$(COPYRIGHT)/' \
+               -e 's/\$$NAME\$$/$(APPNAME)/' \
+               -e 's/\$$EXECUTABLE\$$/$(APPEXE)/' \
+               -e 's/\$$BUNDLE_VERSION\$$/$(BUNDLE_VERSION)/' \
+               -e 's/\$$BUNDLE_IDENTIFIER\$$/$(BUNDLE_IDENTIFIER)/' \
+               -e 's/\$$BUNDLE_DISPLAY_NAME\$$/$(BUNDLE_DISPLAY_NAME)/' \
+               -e 's/\$$BUNDLE_NAME\$$/$(BUNDLE_NAME)/' \
+               $(srcdir)/$(cocoa_plist_template) > $(APPCONT)/Info.plist
+       sed -e 's/\$$VERSION\$$/$(PACKAGE_VERSION)/' \
+               -e 's/\$$COPYRIGHT\$$/$(COPYRIGHT)/' \
+               -e 's/\$$BUNDLE_DISPLAY_NAME\$$/$(BUNDLE_DISPLAY_NAME)/' \
+               -e 's/\$$BUNDLE_NAME\$$/$(BUNDLE_NAME)/' \
+               $(srcdir)/$(cocoa_plist_strings_template) > $(appendir)/InfoPlist.strings
+       sed -e 's/\$$VERSION\$$/$(PACKAGE_VERSION)/' \
+               -e 's/\$$COPYRIGHT\$$/$(COPYRIGHT_JA)/' \
+               -e 's/\$$BUNDLE_DISPLAY_NAME\$$/$(BUNDLE_DISPLAY_NAME_JA)/' \
+               -e 's/\$$BUNDLE_NAME\$$/$(BUNDLE_NAME_JA)/' \
+               $(srcdir)/$(cocoa_plist_strings_template) > $(appjadir)/InfoPlist.strings
+
+uninstall-local:
+       -rm $(APPCONT)/Info.plist
+       -rm $(appendir)/InfoPlist.strings
+       -rm $(appjadir)/InfoPlist.strings
+
+endif
+
+distclean-local:
+       -rm -r -f $(DEPDIR) */$(DEPDIR) */*/$(DEPDIR)
+
 clean-local:
        -rm stdafx.h.gch stdafx.h.gch.sum
        -rm -rf ../build
diff --git a/src/cocoa/Angband-Cocoa.strings b/src/cocoa/Angband-Cocoa.strings
new file mode 100644 (file)
index 0000000..4414cc1
--- /dev/null
@@ -0,0 +1,5 @@
+CFBundleDisplayName = "$BUNDLE_DISPLAY_NAME$";
+CFBundleName = "$BUNDLE_NAME$";
+CFBundleShortVersionString = "$VERSION$";
+NSHumanReadableCopyright = "$COPYRIGHT$";
+
diff --git a/src/cocoa/Angband-Cocoa.xml b/src/cocoa/Angband-Cocoa.xml
new file mode 100644 (file)
index 0000000..7c20a8a
--- /dev/null
@@ -0,0 +1,87 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+       <key>CFBundleDevelopmentRegion</key>
+       <string>en</string>
+       <key>CFBundleExecutable</key>
+       <string>$EXECUTABLE$</string>
+       <key>CFBundleDisplayName</key>
+       <string>$BUNDLE_DISPLAY_NAME$</string>
+       <key>CFBundleName</key>
+       <string>$BUNDLE_NAME$</string>
+       <key>CFBundleIconFile</key>
+       <string>$NAME$_Icons</string>
+       <key>CFBundleIdentifier</key>
+       <string>$BUNDLE_IDENTIFIER$</string>
+       <key>CFBundleInfoDictionaryVersion</key>
+       <string>6.0</string>
+       <key>CFBundlePackageType</key>
+       <string>APPL</string>
+       <key>CFBundleShortVersionString</key>
+       <string>$VERSION$</string>
+       <key>NSHumanReadableCopyright</key>
+       <string>$COPYRIGHT$</string>
+       <key>CFBundleSignature</key>
+       <string>Heng</string>
+       <key>CFBundleVersion</key>
+       <string>$BUNDLE_VERSION$</string>
+       <key>CGDisableCoalescedUpdates</key>
+       <true/>
+       <key>NSMainNibFile</key>
+       <string>MainMenu</string>
+       <key>NSPrincipalClass</key>
+       <string>NSApplication</string>
+       <key>CFBundleDocumentTypes</key>
+       <array>
+               <dict>
+                       <key>CFBundleTypeExtensions</key>
+                       <array>
+                               <string>*</string>
+                       </array>
+                       <key>CFBundleTypeIconFile</key>
+                       <string>Save</string>
+                       <key>CFBundleTypeName</key>
+                       <string>$NAME$ saved game</string>
+                       <key>CFBundleTypeOSTypes</key>
+                       <array>
+                               <string>SAVE</string>
+                       </array>
+                       <key>CFBundleTypeRole</key>
+                       <string>Editor</string>
+               </dict>
+               <dict>
+                       <key>CFBundleTypeExtensions</key>
+                       <array>
+                               <string>*</string>
+                       </array>
+                       <key>CFBundleTypeIconFile</key>
+                       <string>Edit</string>
+                       <key>CFBundleTypeName</key>
+                       <string>$NAME$ game data</string>
+                       <key>CFBundleTypeOSTypes</key>
+                       <array>
+                               <string>TEXT</string>
+                       </array>
+                       <key>CFBundleTypeRole</key>
+                       <string>Editor</string>
+               </dict>
+               <dict>
+                       <key>CFBundleTypeExtensions</key>
+                       <array>
+                               <string>*</string>
+                       </array>
+                       <key>CFBundleTypeIconFile</key>
+                       <string>Data</string>
+                       <key>CFBundleTypeName</key>
+                       <string>$NAME$ game data</string>
+                       <key>CFBundleTypeOSTypes</key>
+                       <array>
+                               <string>DATA</string>
+                       </array>
+                       <key>CFBundleTypeRole</key>
+                       <string>Editor</string>
+               </dict>
+       </array>
+</dict>
+</plist>
diff --git a/src/cocoa/AngbandAudio.h b/src/cocoa/AngbandAudio.h
new file mode 100644 (file)
index 0000000..61aa5ac
--- /dev/null
@@ -0,0 +1,210 @@
+/**
+ * \file AngbandAudio.h
+ * \brief Declare an interface for handling incidental sounds and background
+ * music in the OS X front end.
+ */
+#ifndef INCLUDED_ANGBAND_AUDIO_H
+#define INCLUDED_ANGBAND_AUDIO_H
+
+#import <AVFAudio/AVFAudio.h>
+
+@class AngbandAudioManager;
+
+
+@interface AngbandActiveAudio : NSObject <AVAudioPlayerDelegate> {
+@private
+       AVAudioPlayer *player;
+       NSTimer *fadeTimer;
+}
+
+@property (nonatomic, readonly, getter=isPlaying) BOOL playing;
+
+@property (nonatomic, readonly, weak) AngbandActiveAudio *priorAudio;
+
+@property (nonatomic, readonly, strong) AngbandActiveAudio *nextAudio;
+
+- (id)initWithPlayer:(AVAudioPlayer *)aPlayer fadeInBy:(NSInteger)fadeIn
+               prior:(AngbandActiveAudio *)p paused:(BOOL)isPaused
+               NS_DESIGNATED_INITIALIZER;
+
+- (id)init NS_UNAVAILABLE;
+
+- (void)pause;
+
+- (void)resume;
+
+- (void)stop;
+
+- (void)fadeOutBy:(NSInteger)t;
+
+- (void)changeVolumeTo:(NSInteger)v;
+
+/* Methods for internal use to manage the lifetime of the active audio track. */
+- (void)handleFadeOutTimer:(NSTimer *)timer;
+
+- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)aPlayer
+               successfully:(BOOL)flag;
+
+@end
+
+
+@interface AngbandAudioManager : NSObject {
+@private
+       /**
+        * These are sentinels for the beginning and end, respectively, of
+        * the background muisc tracks currently playing in the order they
+        * were started.
+        */
+       AngbandActiveAudio *tracksPlayingHead;
+       AngbandActiveAudio *tracksPlayingTail;
+
+       /**
+        * Stores instances of AVAudioPlayer keyed by path so the same
+        * incidental sound can be used for multiple events.
+        */
+       NSMutableDictionary *soundsByPath;
+
+       /**
+        * Stores arrays of AVAudioPlayer instances keyed by event number.
+        */
+       NSMutableDictionary *soundArraysByEvent;
+
+       /**
+        * Stores paths to music tracks keyed first by the music type and
+        * then the music id.
+        */
+       NSMutableDictionary *musicByTypeAndID;
+
+       /**
+        * If NO, then do not play beeps or incidental sounds and pause
+        * the background music track if isPausedWhenInactive is YES.
+        */
+       BOOL appActive;
+}
+
+/**
+ * Holds whether a beep will be played.  If NO, then playBeep effectively
+ * becomes a do nothing operation.
+ */
+@property (nonatomic, getter=isBeepEnabled) BOOL beepEnabled;
+
+/**
+ * Holds whether incidental sounds will be played.  If NO, then playSound
+ * effectively becomes a do nothing operation.
+ */
+@property (nonatomic, getter=isSoundEnabled) BOOL soundEnabled;
+
+/**
+ * Holds the volume (as a percentage, 0 - 100, of the system volume) for
+ * incidental sounds.
+ */
+@property (nonatomic) NSInteger soundVolume;
+
+/**
+ * Holds whether background music will be played.  If NO, then playMusicType
+ * effectively becomes a do nothing operation.
+ */
+@property (nonatomic, getter=isMusicEnabled) BOOL musicEnabled;
+
+/**
+ * Holds whether music is paused when the application is iconified or otherwise
+ * marked as being an inactive application.  Beeps and incidental sounds are
+ * never played when the application is an inactive application.
+ */
+@property (nonatomic, getter=isMusicPausedWhenInactive)
+               BOOL musicPausedWhenInactive;
+
+/**
+ * Holds the volume (as a percentage, 0 - 100, of the system volume) for
+ * background music.
+ */
+@property (nonatomic) NSInteger musicVolume;
+
+/**
+ * Holds the amount of time, in milliseconds, for transitions between
+ * different music tracks.  If less than or equal to zero, there's no
+ * transition interval:  one track stops playing and the other starts playing
+ * at full volume.
+ */
+@property (nonatomic) NSInteger musicTransitionTime;
+
+/**
+ * Set up for lazy initialization in playSound() and playMusicType().  Set
+ * appActive to YES, beepEnabled to YES, soundEnabled to YES, soundVolume
+ * to 30, musicEnabled to YES, pausedWhenInactive to YES,
+ * musicVolume to 20, and musicTransitionTime to 3000.
+ */
+- (id)init;
+
+/**
+ * If self.beepEnabed is YES, make a beep.
+ */
+- (void)playBeep;
+
+/**
+ * If self.soundEnabled is YES and the given event has one or more sounds
+ * corresponding to it in the catalog, plays one of those sounds, chosen at
+ * random.
+ */
+- (void)playSound:(int)event;
+
+/**
+ * If self.musicEnabled is YES and the given music type and id exists, play
+ * it.
+ */
+- (void)playMusicType:(int)t ID:(int)i;
+
+/**
+ * Return YES if combination of the given type and ID is in the music
+ * catalog.  Otherwise, return NO.
+ */
+- (BOOL)musicExists:(int)t ID:(int)i;
+
+/**
+ * Stop all currently playing music tracks.
+ */
+- (void)stopAllMusic;
+
+/**
+ * Set up to act appropiately if the containing application is inactive.
+ */
+- (void)setupForInactiveApp;
+
+/**
+ * Set up to act appropriately if the containing application is active.
+ */
+- (void)setupForActiveApp;
+
+/**
+ * Impose an arbitrary limit on the number of possible samples for an
+ * incidental sound event or the background music tracks for a type/ID
+ * combination.
+ */
+@property (class, nonatomic, readonly) NSInteger maxSamples;
+
+/**
+ * Return the shared audio manager instance, creating it if it does not
+ * exist yet.
+ */
+@property (class, nonatomic, strong, readonly) AngbandAudioManager
+               *sharedManager;
+
+/**
+ * Release any resources associated with the shared audio manager.
+ */
++ (void)clearSharedManager;
+
+/**
+ * Help playSound() to set up a catalog of the incidental sounds.
+ */
++ (NSMutableDictionary *)setupSoundArraysByEvent;
+
+/**
+ * Help playMusicType() to set up a catalog of the background music.
+ */
++ (NSMutableDictionary *)setupMusicByTypeAndID;
+
+@end
+
+
+#endif /* include guard */
diff --git a/src/cocoa/AngbandAudio.mm b/src/cocoa/AngbandAudio.mm
new file mode 100644 (file)
index 0000000..b2520c8
--- /dev/null
@@ -0,0 +1,903 @@
+/**
+ * \file AngbandAudio.mm
+ * \brief Define an interface for handling incidental sounds and background
+ * music in the OS X front end.
+ */
+
+#import <Cocoa/Cocoa.h>
+#import "AngbandAudio.h"
+#include "io/files-util.h"
+#include "main/music-definitions-table.h"
+#include "main/sound-definitions-table.h"
+#include "main/sound-of-music.h"
+#include "util/angband-files.h"
+#include <limits.h>
+#include <string.h>
+#include <ctype.h>
+
+
+/*
+ * Helper functions to extract the appropriate number ID from a name in
+ * music.cfg.  Return true if the extraction failed and false if the extraction
+ * succeeded with *id holding the result.
+ */
+static bool get_basic_id(const char *s, int *id)
+{
+       int i = 0;
+
+       while (1) {
+               if (i >= MUSIC_BASIC_MAX) {
+                       return true;
+               }
+               if (!strcmp(angband_music_basic_name[i], s)) {
+                       *id = i;
+                       return false;
+               }
+               ++i;
+       }
+}
+
+
+static bool get_dungeon_id(const char *s, int *id)
+{
+       if (strcmp(s, "dungeon") == 0) {
+               char *pe;
+               long lv = strtol(s + 7, &pe, 10);
+
+               if (pe != s && !*pe && lv > INT_MIN && lv < INT_MAX) {
+                       *id = (int)lv;
+                       return false;
+               }
+       }
+       return true;
+}
+
+
+static bool get_quest_id(const char *s, int *id)
+{
+       if (strcmp(s, "quest") == 0) {
+               char *pe;
+               long lv = strtol(s + 5, &pe, 10);
+
+               if (pe != s && !*pe && lv > INT_MIN && lv < INT_MAX) {
+                       *id = (int)lv;
+                       return false;
+               }
+       }
+       return true;
+}
+
+
+static bool get_town_id(const char *s, int *id)
+{
+       if (strcmp(s, "town") == 0) {
+               char *pe;
+               long lv = strtol(s + 4, &pe, 10);
+
+               if (pe != s && !*pe && lv > INT_MIN && lv < INT_MAX) {
+                       *id = (int)lv;
+                       return false;
+               }
+       }
+       return true;
+}
+
+
+static bool get_monster_id(const char *s, int *id)
+{
+       if (strcmp(s, "monster") == 0) {
+               char *pe;
+               long lv = strtol(s + 7, &pe, 10);
+
+               if (pe != s && !*pe && lv > INT_MIN && lv < INT_MAX) {
+                       *id = (int)lv;
+                       return false;
+               }
+       }
+       return true;
+}
+
+
+@implementation AngbandActiveAudio
+
+/*
+ * Handle property methods where need more than is provided by the default
+ * synthesis.
+ */
+- (BOOL) isPlaying
+{
+       return self->player && [self->player isPlaying];
+}
+
+
+- (id)initWithPlayer:(AVAudioPlayer *)aPlayer fadeInBy:(NSInteger)fadeIn
+               prior:(AngbandActiveAudio *)p paused:(BOOL)isPaused
+{
+       if (self = [super init]) {
+               self->_priorAudio = p;
+               if (p) {
+                       self->_nextAudio = p->_nextAudio;
+                       if (p->_nextAudio) {
+                               p->_nextAudio->_priorAudio = self;
+                       }
+                       p->_nextAudio = self;
+               } else {
+                       self->_nextAudio = nil;
+               }
+               self->player = aPlayer;
+               self->fadeTimer = nil;
+               if (aPlayer) {
+                       aPlayer.delegate = self;
+                       if (fadeIn > 0) {
+                               float volume = [aPlayer volume];
+
+                               aPlayer.volume = 0;
+                               [aPlayer setVolume:volume
+                                       fadeDuration:(fadeIn * .001)];
+                       }
+                       if (!isPaused) {
+                               [aPlayer play];
+                       }
+               }
+       }
+       return self;
+}
+
+
+- (void)pause
+{
+       if (self->player) {
+               [self->player pause];
+       }
+}
+
+
+- (void)resume
+{
+       if (self->player) {
+               [self->player play];
+       }
+}
+
+
+- (void)stop
+{
+       if (self->player) {
+               if (self->fadeTimer) {
+                       [self->fadeTimer invalidate];
+                       self->fadeTimer = nil;
+               }
+               [self->player stop];
+       }
+}
+
+
+- (void)fadeOutBy:(NSInteger)t
+{
+       /* Only fade out if there's a track and it is not already fading out. */
+       if (self->player && !self->fadeTimer) {
+               [self->player setVolume:0.0f
+                       fadeDuration:(((t > 0) ? t : 0) * .001)];
+               /* Set up a timer to remove the faded out track. */
+               self->fadeTimer = [NSTimer
+                       scheduledTimerWithTimeInterval:(t * .001 + .01)
+                       target:self
+                       selector:@selector(handleFadeOutTimer:)
+                       userInfo:nil
+                       repeats:NO];
+               self->fadeTimer.tolerance = 0.02;
+       }
+}
+
+
+- (void)changeVolumeTo:(NSInteger)v
+{
+       if (self->player) {
+               NSInteger safev;
+
+               if (v < 0) {
+                       safev = 0;
+               } else if (v > 100) {
+                       safev = 100;
+               } else {
+                       safev = v;
+               }
+               self->player.volume = safev * .01f;
+       }
+}
+
+
+- (void)handleFadeOutTimer:(NSTimer *)timer
+{
+       assert(self->player && self->fadeTimer == timer);
+       self->fadeTimer = nil;
+       [self->player stop];
+}
+
+
+- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)aPlayer
+               successfully:(BOOL)flag
+{
+       assert(aPlayer == self->player);
+       if (self->fadeTimer) {
+               [self->fadeTimer invalidate];
+               self->fadeTimer = nil;
+       }
+       self->player = nil;
+       /* Unlink from the list of active tracks. */
+       assert([self priorAudio] && [self nextAudio]);
+       self->_priorAudio->_nextAudio = self->_nextAudio;
+       self->_nextAudio->_priorAudio = self->_priorAudio;
+       self->_priorAudio = nil;
+       self->_nextAudio = nil;
+}
+
+@end
+
+
+@implementation AngbandAudioManager
+
+@synthesize soundVolume=_soundVolume;
+@synthesize musicEnabled=_musicEnabled;
+@synthesize musicVolume=_musicVolume;
+
+/*
+ * Handle property methods where need more than provided by the default
+ * synthesis.
+ */
+- (void)setSoundVolume:(NSInteger)v
+{
+       /* Incidental sounds that are currently playing aren't changed. */
+       if (v < 0) {
+               self->_soundVolume = 0;
+       } else if (v > 100) {
+               self->_soundVolume = 100;
+       } else {
+               self->_soundVolume = v;
+       }
+}
+
+
+- (void)setMusicEnabled:(BOOL)b
+{
+       BOOL old = self->_musicEnabled;
+
+       self->_musicEnabled = b;
+
+       if (!b && old) {
+               AngbandActiveAudio *a = [self->tracksPlayingHead nextAudio];
+
+               while (a) {
+                       AngbandActiveAudio *t = a;
+
+                       a = [a nextAudio];
+                       [t stop];
+               };
+       }
+}
+
+
+- (void)setMusicVolume:(NSInteger)v
+{
+       NSInteger old = self->_musicVolume;
+
+       if (v < 0) {
+               self->_musicVolume = 0;
+       } else if (v > 100) {
+               self->_musicVolume = 100;
+       } else {
+               self->_musicVolume = v;
+       }
+
+       if (v != old) {
+               [[self->tracksPlayingTail priorAudio]
+                       changeVolumeTo:self->_musicVolume];
+       }
+}
+
+
+/* Define methods. */
+- (id)init
+{
+       if (self = [super init]) {
+               self->tracksPlayingHead = [[AngbandActiveAudio alloc]
+                       initWithPlayer:nil fadeInBy:0 prior:nil paused:NO];
+               self->tracksPlayingTail = [[AngbandActiveAudio alloc]
+                       initWithPlayer:nil fadeInBy:0
+                       prior:self->tracksPlayingHead paused:NO];
+               self->soundArraysByEvent = nil;
+               self->musicByTypeAndID = nil;
+               self->appActive = YES;
+               self->_beepEnabled = YES;
+               self->_soundEnabled = YES;
+               self->_soundVolume = 30;
+               self->_musicEnabled = YES;
+               self->_musicPausedWhenInactive = YES;
+               self->_musicVolume = 20;
+               self->_musicTransitionTime = 3000;
+       }
+       return self;
+}
+
+
+- (void)playBeep
+{
+       if (!self->appActive || ![self isBeepEnabled]) {
+               return;
+       }
+       /*
+        * Use NSBeep() for this, though that means it doesn't heed the
+        * volume set by the soundVolume property.
+        */
+       NSBeep();
+}
+
+- (void)playSound:(int)event
+{
+       if (!self->appActive || ![self isSoundEnabled]) {
+               return;
+       }
+
+       /* Initialize when the first sound is played. */
+       if (!self->soundArraysByEvent) {
+               self->soundArraysByEvent =
+                       [AngbandAudioManager setupSoundArraysByEvent];
+               if (!self->soundArraysByEvent) {
+                       return;
+               }
+       }
+
+       @autoreleasepool {
+               NSMutableArray *samples = [self->soundArraysByEvent
+                       objectForKey:[NSNumber numberWithInteger:event]];
+               AVAudioPlayer *player;
+               int s;
+
+               if (!samples || !samples.count) {
+                       return;
+               }
+
+               s = randint0((int)samples.count);
+               player = samples[s];
+
+               if ([player isPlaying]) {
+                       [player stop];
+                       player.currentTime = 0;
+               }
+               player.volume = self.soundVolume * .01f;
+               [player play];
+       }
+}
+
+
+- (void)playMusicType:(int)t ID:(int)i
+{
+       if (![self isMusicEnabled]) {
+               return;
+       }
+
+       /* Initialize when the first music track is played. */
+       if (!self->musicByTypeAndID) {
+               self->musicByTypeAndID =
+                       [AngbandAudioManager setupMusicByTypeAndID];
+       }
+
+       @autoreleasepool {
+               NSMutableDictionary *musicByID = [self->musicByTypeAndID
+                       objectForKey:[NSNumber numberWithInteger:t]];
+               NSMutableArray *paths;
+                NSData *audioData;
+               AVAudioPlayer *player;
+
+               if (!musicByID) {
+                       return;
+               }
+               paths = [musicByID
+                       objectForKey:[NSNumber numberWithInteger:i]];
+               if (!paths || !paths.count) {
+                       return;
+               }
+                audioData = [NSData dataWithContentsOfFile:paths[randint0((int)paths.count)]];
+               player = [[AVAudioPlayer alloc] initWithData:audioData
+                       error:nil];
+
+               if (player) {
+                       AngbandActiveAudio *prior_track =
+                               [self->tracksPlayingTail priorAudio];
+                       AngbandActiveAudio *active_track;
+                       NSInteger fade_time;
+
+                       player.volume = 0.01f * [self musicVolume];
+                       if ([prior_track isPlaying]) {
+                               fade_time = [self musicTransitionTime];
+                               if (fade_time < 0) {
+                                       fade_time = 0;
+                               }
+                               [prior_track fadeOutBy:fade_time];
+                       } else {
+                               fade_time = 0;
+                       }
+                       active_track = [[AngbandActiveAudio alloc]
+                               initWithPlayer:player fadeInBy:fade_time
+                               prior:prior_track
+                               paused:(!self->appActive && [self isMusicPausedWhenInactive])];
+               }
+       }
+}
+
+
+- (BOOL)musicExists:(int)t ID:(int)i
+{
+       NSMutableDictionary *musicByID;
+       BOOL exists;
+
+       /* Initialize on the first call if it hasn't already been done. */
+       if (!self->musicByTypeAndID) {
+               self->musicByTypeAndID =
+                       [AngbandAudioManager setupMusicByTypeAndID];
+       }
+
+       musicByID = [self->musicByTypeAndID
+               objectForKey:[NSNumber numberWithInteger:t]];
+
+       if (musicByID) {
+               NSString *path = [musicByID
+                       objectForKey:[NSNumber numberWithInteger:i]];
+
+               exists = (path != nil);
+       } else {
+               exists = NO;
+       }
+
+       return exists;
+}
+
+
+- (void)stopAllMusic
+{
+       AngbandActiveAudio *track = [self->tracksPlayingHead nextAudio];
+
+       while (1) {
+               [track stop];
+               track = [track nextAudio];
+               if (!track) {
+                       break;
+               }
+       }
+}
+
+
+- (void)setupForInactiveApp
+{
+       if (!self->appActive) {
+               return;
+       }
+       self->appActive = NO;
+       if ([self isMusicPausedWhenInactive]) {
+               AngbandActiveAudio *track =
+                       [self->tracksPlayingHead nextAudio];
+
+               while (1) {
+                       AngbandActiveAudio *next_track = [track nextAudio];
+
+                       if (next_track != self->tracksPlayingTail) {
+                               /* Stop all tracks but the last one playing. */
+                               [track stop];
+                       } else {
+                               /*
+                                * Pause the last track playing.  Set its
+                                * volume to maximum so, when resumed, it'll
+                                * play that way even if it was fading in when
+                                * paused.
+                                */
+                               [track pause];
+                               [track changeVolumeTo:[self musicVolume]];
+                       }
+                       track = next_track;
+                       if (!track) {
+                               break;
+                       }
+               }
+       }
+}
+
+
+- (void)setupForActiveApp
+{
+       if (self->appActive) {
+               return;
+       }
+       self->appActive = YES;
+       if ([self isMusicPausedWhenInactive]) {
+               /* Resume any tracks that were playing. */
+               AngbandActiveAudio *track =
+                       [self->tracksPlayingTail priorAudio];
+
+               while (1) {
+                       [track resume];
+                       track = [track priorAudio];
+                       if (!track) {
+                               break;
+                       }
+               }
+       }
+}
+
+
+/* Set up the class properties. */
+static NSInteger _maxSamples = 16;
++ (NSInteger)maxSamples
+{
+       return _maxSamples;
+}
+
+
+static AngbandAudioManager *_sharedManager = nil;
++ (AngbandAudioManager *)sharedManager
+{
+       if (!_sharedManager) {
+               _sharedManager = [[AngbandAudioManager alloc] init];
+       }
+       return _sharedManager;
+}
+
+
+/* Define class methods. */
++ (void)clearSharedManager
+{
+       _sharedManager = nil;
+}
+
+
++ (NSMutableDictionary *)setupSoundArraysByEvent
+{
+       std::filesystem::path psound, p;
+       FILE *fff;
+       NSMutableDictionary *arraysByEvent;
+
+       /* Build the "sound" path. */
+       psound = path_build(ANGBAND_DIR_XTRA, "sound");
+
+       /* Find and open the config file. */
+       p = path_build(psound, "sound.cfg");
+       fff = angband_fopen(p, FileOpenMode::READ);
+
+       if (!fff) {
+               NSLog(@"The sound configuration file could not be opened");
+               return nil;
+       }
+
+       arraysByEvent = [[NSMutableDictionary alloc] init];
+       @autoreleasepool {
+               /*
+                * This loop may take a while depending on the count and size
+                * of samples to load.
+                */
+               const char white[] = " \t";
+               NSMutableDictionary *playersByPath =
+                       [[NSMutableDictionary alloc] init];
+               char buffer[2048];
+
+               /* Parse the file. */
+               /* Lines are always of the form "name = sample [sample ...]". */
+               while (angband_fgets(fff, buffer, sizeof(buffer)) == 0) {
+                       NSMutableArray *soundSamples;
+                       char *msg_name;
+                       char *sample_name;
+                       char *search;
+                       int match;
+                       size_t skip;
+
+                       /* Skip leading whitespace. */
+                       skip = strspn(buffer, white);
+
+                       /*
+                        * Ignore anything not beginning with an alphabetic
+                        * character.
+                        */
+                       if (!buffer[skip] || !isalpha((unsigned char)buffer[skip])) {
+                               continue;
+                       }
+
+                       /*
+                        * Split the line into two; message name and the rest.
+                        */
+                       search = strchr(buffer + skip, '=');
+                       if (!search) {
+                               continue;
+                       }
+                       msg_name = buffer + skip;
+                       skip = strcspn(msg_name, white);
+                       if (skip > (size_t)(search - msg_name)) {
+                               /*
+                                *  No white space between the message name and
+                                * '='.
+                                */
+                               *search = '\0';
+                       } else {
+                               msg_name[skip] = '\0';
+                       }
+                       skip = strspn(search + 1, white);
+                       sample_name = search + 1 + skip;
+
+                       /* Make sure this is a valid event name. */
+                       for (match = SOUND_MAX - 1; match >= 0; --match) {
+                               if (!strcmp(msg_name, angband_sound_name[match])) {
+                                       break;
+                               }
+                       }
+                       if (match < 0) {
+                               continue;
+                       }
+
+                       soundSamples = [arraysByEvent
+                               objectForKey:[NSNumber numberWithInteger:match]];
+                       if (!soundSamples) {
+                               soundSamples = [[NSMutableArray alloc] init];
+                               [arraysByEvent
+                                       setObject:soundSamples
+                                       forKey:[NSNumber numberWithInteger:match]];
+                       }
+
+                       /*
+                        * Now find all the sample names and add them one by
+                        * one.
+                        */
+                       while (1) {
+                               int num;
+                               NSString *token_string;
+                               AVAudioPlayer *player;
+                               BOOL done;
+
+                               if (!sample_name[0]) {
+                                       break;
+                               }
+                               /* Terminate the current token. */
+                               skip = strcspn(sample_name, white);
+                               done = !sample_name[skip];
+                               sample_name[skip] = '\0';
+
+                               /* Don't allow too many samples. */
+                               num = (int) soundSamples.count;
+                               if (num >= [AngbandAudioManager maxSamples]) {
+                                       break;
+                               }
+
+                               token_string = [NSString
+                                       stringWithUTF8String:sample_name];
+                               player = [playersByPath
+                                       objectForKey:token_string];
+                               if (!player) {
+                                       /*
+                                        * We have to load the sound.
+                                        * Build the path to the sample.
+                                        */
+                                       struct stat stb;
+
+                                       p = path_build(psound, sample_name);
+                                       if (stat(p.native().data(), &stb) == 0
+                                                       && (stb.st_mode & S_IFREG)) {
+                                               NSData *audioData = [NSData
+                                                       dataWithContentsOfFile:[NSString stringWithUTF8String:p.native().data()]];
+
+                                               player = [[AVAudioPlayer alloc]
+                                                       initWithData:audioData
+                                                       error:nil];
+                                               if (player) {
+                                                       [playersByPath
+                                                               setObject:player
+                                                               forKey:token_string];
+                                               }
+                                       }
+                               }
+
+                               /* Store it if it was loaded. */
+                               if (player) {
+                                       [soundSamples addObject:player];
+                               }
+
+                               if (done) {
+                                       break;
+                               }
+                               sample_name += skip + 1;
+                               skip = strspn(sample_name, white);
+                               sample_name += skip;
+                       }
+               }
+               playersByPath = nil;
+       }
+       angband_fclose(fff);
+
+       return arraysByEvent;
+}
+
+
++ (NSMutableDictionary *)setupMusicByTypeAndID
+{
+       struct {
+               const char * name;
+               int type_code;
+               bool *p_has;
+               bool (*name2id_func)(const char*, int*);
+       } sections_of_interest[] = {
+               { "Basic", TERM_XTRA_MUSIC_BASIC, NULL, get_basic_id },
+               { "Dungeon", TERM_XTRA_MUSIC_DUNGEON, NULL, get_dungeon_id },
+               { "Quest", TERM_XTRA_MUSIC_QUEST, NULL, get_quest_id },
+               { "Town", TERM_XTRA_MUSIC_TOWN, NULL, get_town_id },
+               { "Monster", TERM_XTRA_MUSIC_MONSTER, &has_monster_music,
+                       get_monster_id },
+               { NULL, 0, NULL, NULL } /* terminating sentinel */
+       };
+       std::filesystem::path pmusic, p;
+       FILE *fff;
+       NSMutableDictionary *catalog;
+
+       /* Build the "music" path. */
+       pmusic = path_build(ANGBAND_DIR_XTRA, "music");
+
+       /* Find and open the config file. */
+       p = path_build(pmusic, "music.cfg");
+       fff = angband_fopen(p, FileOpenMode::READ);
+
+       if (!fff) {
+               NSLog(@"The music configuration file could not be opened");
+               return nil;
+       }
+
+       catalog = [[NSMutableDictionary alloc] init];
+       @autoreleasepool {
+               const char white[] = " \t";
+               NSMutableDictionary *catalogTypeRestricted = nil;
+               int isec = -1;
+               char buffer[2048];
+
+               /* Parse the file. */
+               while (angband_fgets(fff, buffer, sizeof(buffer)) == 0) {
+                       NSMutableArray *samples;
+                       char *id_name;
+                       char *sample_name;
+                       char *search;
+                       size_t skip;
+                       int id;
+
+                       /* Skip leading whitespace. */
+                       skip = strspn(buffer, white);
+
+                       /*
+                        * Ignore empty lines or ones that only have comments.
+                        */
+                       if (!buffer[skip] || buffer[skip] == '#') {
+                               continue;
+                       }
+
+                       if (buffer[skip] == '[') {
+                               /*
+                                * Found the start of a new section.  Will do
+                                * nothing if the section name is malformed
+                                * (i.e. missing the trailing bracket).
+                                */
+                               search = strchr(buffer + skip + 1, ']');
+                               if (search) {
+                                       isec = 0;
+
+                                       *search = '\0';
+                                       while (1) {
+                                               if (!sections_of_interest[isec].name) {
+                                                       catalogTypeRestricted =
+                                                               nil;
+                                                       isec = -1;
+                                                       break;
+                                               }
+                                               if (!strcmp(sections_of_interest[isec].name, buffer + skip + 1)) {
+                                                       NSNumber *key = [NSNumber
+                                                               numberWithInteger:sections_of_interest[isec].type_code];
+                                                       catalogTypeRestricted =
+                                                               [catalog objectForKey:key];
+                                                       if (!catalogTypeRestricted) {
+                                                               catalogTypeRestricted =
+                                                                       [[NSMutableDictionary alloc] init];
+                                                               [catalog
+                                                                       setObject:catalogTypeRestricted
+                                                                       forKey:key];
+                                                       }
+                                                       break;
+                                               }
+                                               ++isec;
+                                       }
+                               }
+                               /* Skip the rest of the line. */
+                               continue;
+                       }
+
+                       /*
+                        * Targets should begin with an alphabetical character.
+                        * Skip anything else.
+                        */
+                       if (!isalpha((unsigned char)buffer[skip])) {
+                               continue;
+                       }
+
+                       search = strchr(buffer + skip, '=');
+                       if (!search) {
+                               continue;
+                       }
+                       id_name = buffer + skip;
+                       skip = strcspn(id_name, white);
+                       if (skip > (size_t)(search - id_name)) {
+                               /*
+                                * No white space between the name on the left
+                                * and '='.
+                                */
+                               *search = '\0';
+                       } else {
+                               id_name[skip] = '\0';
+                       }
+                       skip = strspn(search + 1, white);
+                       sample_name = search + 1 + skip;
+
+                       if (!catalogTypeRestricted
+                                       || (*(sections_of_interest[isec].name2id_func))(id_name, &id)) {
+                               /*
+                                * It is not in a section of interest or did
+                                * not recognize what was on the left side of
+                                * '='.  Ignore the line.
+                                */
+                               continue;
+                       }
+                       if (sections_of_interest[isec].p_has) {
+                               *(sections_of_interest[isec].p_has) = true;
+                       }
+                       samples = [catalogTypeRestricted
+                               objectForKey:[NSNumber numberWithInteger:id]];
+                       if (!samples) {
+                               samples = [[NSMutableArray alloc] init];
+                               [catalogTypeRestricted
+                                       setObject:samples
+                                       forKey:[NSNumber numberWithInteger:id]];
+                       }
+
+                       /*
+                        * Now find all the sample names and add them one by
+                        * one.
+                        */
+                       while (1) {
+                               BOOL done;
+                               struct stat stb;
+
+                               if (!sample_name[0]) {
+                                       break;
+                               }
+                               /* Terminate the current token. */
+                               skip = strcspn(sample_name, white);
+                               done = !sample_name[skip];
+                               sample_name[skip] = '\0';
+
+                               /*
+                                * Check if the path actually corresponds to a
+                                * file.  Also restrict the number of samples
+                                * stored for any type/ID combination.
+                                */
+                               p = path_build(pmusic, sample_name);
+                               if (stat(p.native().data(), &stb) == 0
+                                               && (stb.st_mode & S_IFREG)
+                                               && (int)samples.count
+                                               < [AngbandAudioManager maxSamples]) {
+                                       [samples addObject:[NSString
+                                               stringWithUTF8String:p.native().data()]];
+                               }
+
+                               if (done) {
+                                       break;
+                               }
+                               sample_name += skip + 1;
+                               skip = strspn(sample_name, white);
+                               sample_name += skip;
+                       }
+               }
+       }
+       angband_fclose(fff);
+
+       return catalog;
+}
+
+@end
diff --git a/src/cocoa/AppDelegate.h b/src/cocoa/AppDelegate.h
new file mode 100644 (file)
index 0000000..5f04faf
--- /dev/null
@@ -0,0 +1,42 @@
+/**
+ *\file AppDelegate.h
+ *\brief Declare the application delegate used by the OS X front end.
+ *
+ * This work is free software; you can redistribute it and/or modify it
+ * under the terms of either:
+ *
+ * a) the GNU General Public License as published by the Free Software
+ *    Foundation, version 2, or
+ *
+ * b) the "Angband licence":
+ *    This software may be copied and distributed for educational, research,
+ *    and not for profit purposes provided that this copyright and statement
+ *    are included in all such copies.  Other copyrights may also apply.
+ */
+
+#import <Cocoa/Cocoa.h>
+#import "SoundAndMusic.h"
+
+@interface AngbandAppDelegate : NSObject <NSApplicationDelegate,
+        SoundAndMusicChanges> {
+    NSMenu *_graphicsMenu;
+    NSMenu *_commandMenu;
+    NSDictionary *_commandMenuTagMap;
+}
+@property (nonatomic, retain) IBOutlet NSMenu *graphicsMenu;
+@property (strong, nonatomic, retain) IBOutlet NSMenu *commandMenu;
+@property (strong, nonatomic, retain) NSDictionary *commandMenuTagMap;
+@property (strong, nonatomic) SoundAndMusicPanelController
+    *soundAndMusicPanelController;
+- (IBAction)newGame:(id)sender;
+- (IBAction)editFont:(id)sender;
+- (IBAction)openGame:(id)sender;
+- (IBAction)saveGame:(id)sender;
+- (IBAction)setRefreshRate:(NSMenuItem *)sender;
+- (IBAction)toggleWideTiles:(NSMenuItem *)sender;
+- (IBAction)showSoundAndMusicPanel:(NSMenuItem *)sender;
+- (void)setGraphicsMode:(NSMenuItem *)sender;
+- (void)selectWindow:(id)sender;
+- (void)beginGame;
+
+@end
diff --git a/src/cocoa/AppDelegate.m b/src/cocoa/AppDelegate.m
new file mode 100644 (file)
index 0000000..78a4cfc
--- /dev/null
@@ -0,0 +1,166 @@
+/**
+ * \file AppDelegate.m
+ * \brief This is a minimal implementation of the OS X front end.
+ *
+ * Use this file to rebuild the .nib file with Xcode without having to pull
+ * in all of the Hengband source.  This is the procedure with Xcode 14:
+ *
+ * 1) Create a new Xcode project for a macOS App.
+ * 2) You can set the "Product Name", "Team", "Organization Name",
+ *    "Organization Identifier" as you wish.  Setting the product name to
+ *    "hengband" and the "Organization Identifier" to "jp.osdn" will match
+ *    the bundle identifier used in the full builds for Hengband.  Set
+ *    "Language" to "Objective-C" and "User Interface" to "XIB".  Leave
+ *    "Use Core Data" off.  The setting for "Include Tests" doesn't matter;
+ *    you can turn it off to avoid extra clutter.
+ * 3) In hengband's project settings on the "Info" tab, set the deployment
+ *    target to what's used in Hengband's src/Makefile.am.  When this was
+ *    written, that was 10.15.  As least 10.8 is necessary for Base
+ *    localization.  In the localizations part of that tab, click the '+' and
+ *    add a Japanese localization.  That will prompt you for the files
+ *    involved.  Leave that as is:  one file, "MainMenu.xib", with Base as the
+ *    reference language and localizable strings as the file type.
+ * 4) Copy src/cocoa/AppDelegate.h, src/cocoa/AppDelegate.m,
+ *    src/cocoa/SoundAndMusic.h, and src/cocoa/SoundAndMusic.mm from the
+ *    Hengband source files to the directory in the project with main.m.  Copy
+ *    src/cocoa/Base.lproj/MainMenu.xib and
+ *    src/cocoa/Base.lproj/SoundAndMusic.xib to the Base.lproj subdirectory of
+ *    that directory.  Copy src/cocoa/ja.lproj/MainMenu.strings to the ja.lproj
+ *    subdirectory of that directory.  In Xcode, use "File->Add Files..." to
+ *    add the copies of SoundAndMusic.h, SoundAndMusic.mm, and
+ *    SoundAndMusic.xib to the project.  In the file view in Xcode, click on
+ *    SoundAndMusic.xib and in the Indentity and in the File inspector for that
+ *    file, turn on Japanese localization for that file.
+ * 5) (This annoyance seems to have gone away beween Xcode 11 and Xcode 13;
+ *    leaving it here just in case) If you modify MainMenu.xib after copying
+ *    it over, you may want to set it so that it can be opened in older
+ *    versions of Xcode.  Select it in Xcode, and select one of the things,
+ *    like "File's Owner" from it.  In the file information panel for it,
+ *    there will be a section labeled "Document Editing" with an option
+ *    menu for "Opens in".  Choosing one of the options other than
+ *    "Latest Xcode" will close the file and save it with the appropriate
+ *    flags.  Note that reopening the xib file in Xcode and saving it will
+ *    cause the version to revert to the latest Xcode.
+ * 6) If you want to change the Japanese strings for the menus, one way to
+ *    partly do it in Xcode is to export the localizations:  from the file view
+ *    select topmost category ("hengband" with an application icon) and then
+ *    select Product->Export Localizations... in Xcode's menu bar.  That
+ *    will prompt you for where to save the exported localizations.  That
+ *    export is done as a directory tree.  Within it, you'll find a
+ *    ja.xcloc/Localized Contents/ja.xliff file.  The strings bracketed with
+ *    <source></source> in that file are the English strings.  The strings
+ *    bracketed with <target></target> give what's currently used for the
+ *    Japanese version.  Adjust the strings bracketed with <target></target>,
+ *    save the modified file, and use Editor->Import Localizations... from
+ *    within Xcode to import the localization from the ja.xloc directory.
+ *    The result of that will be to regenerate ja.lproj/MainMenu.strings and
+ *    ja.lproj/SoundAndMusic.strings in the Xcode project files which you can
+ *    use to replace the versions in src/cocoa/ja.lproj/MainMenu.strings and
+ *    src/cocoa/ja.lproj/SoundAndMusic.strings in the Hengband source code.
+ * 7) Use Xcode's Product->Build For->Running menu entry to build the project.
+ * 8) The generated .nib files for English will be
+ *    Contents/Resources/Base.lproj/MainMenu.nib and
+ *    Contents/Resources/Base.lproj/SoundAndMusic.nib in the product
+ *    directory which is something like
+ *    ~/Library/Developer/Xcode/DerivedData/<product_name>-<some_string>/Build/Products/Debug/<product_name>.app
+ *    You can use those to replace src/cocoa/Base.lproj/MainMenu.nib and
+ *    src/cocoa/Base.lproj/SoundAndMusic.nib in the Hengband source files.
+ *    With Xcode 13 and later and if the development target is macOS 10.13 or
+ *    earlier, the generated .nib files are directories.  In that case, copy
+ *    Contents/Resources/Base.lproj/MainMenu.nib/keyedobjects.nib to replace
+ *    src/cocoa/Base.lproj/MainMenu.nib and
+ *    Contents/Resources/Base.lproj/SoundAndMusic.nib/keyedobjects.nib to
+ *    replace src/cocoa/Base.lproj/SoundAndMusic.nib (the
+ *    keyedobjects-101300.nib files in the build results are for macOS 10.13
+ *    or later).
+ *
+ * This work is free software; you can redistribute it and/or modify it
+ * under the terms of either:
+ *
+ * a) the GNU General Public License as published by the Free Software
+ *    Foundation, version 2, or
+ *
+ * b) the "Angband licence":
+ *    This software may be copied and distributed for educational, research,
+ *    and not for profit purposes provided that this copyright and statement
+ *    are included in all such copies.  Other copyrights may also apply.
+ */
+
+#import "AppDelegate.h"
+
+@implementation AngbandAppDelegate
+
+@synthesize graphicsMenu=_graphicsMenu;
+@synthesize commandMenu=_commandMenu;
+@synthesize commandMenuTagMap=_comandMenuTagMap;
+@synthesize soundAndMusicPanelController=_soundAndMusicPanelController;
+
+- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
+    // Insert code here to initialize your application
+}
+
+
+- (void)applicationWillTerminate:(NSNotification *)aNotification {
+    // Insert code here to tear down your application
+}
+
+
+- (IBAction)newGame:(id)sender {
+}
+
+- (IBAction)editFont:(id)sender {
+}
+
+- (IBAction)openGame:(id)sender {
+}
+
+- (IBAction)saveGame:(id)sender {
+}
+
+- (IBAction)setRefreshRate:(NSMenuItem *)sender {
+}
+
+- (IBAction)toggleWideTiles:(NSMenuItem *)sender {
+}
+
+- (IBAction)setGraphicsMode:(NSMenuItem *)sender {
+}
+
+- (IBAction)showSoundAndMusicPanel:(NSMenuItem *)sender {
+       if (!self.soundAndMusicPanelController) {
+               self.soundAndMusicPanelController =
+                       [[SoundAndMusicPanelController alloc]
+                       initWithWindow:nil];
+               self.soundAndMusicPanelController.changeHandler = self;
+       }
+       [self.soundAndMusicPanelController showWindow:sender];
+}
+
+- (void)selectWindow:(id)sender {
+}
+
+- (void)beginGame {
+}
+
+- (void)changeSoundEnabled:(BOOL)newv {
+}
+
+- (void)changeSoundVolume:(NSInteger)newv {
+}
+
+- (void)changeMusicEnabled:(BOOL)newv {
+}
+
+- (void)changeMusicPausedWhenInactive:(BOOL)newv {
+}
+
+- (void)changeMusicVolume:(NSInteger)newv {
+}
+
+- (void)changeMusicTransitionTime:(NSInteger)newv {
+}
+
+- (void)soundAndMusicPanelWillClose {
+}
+
+@end
diff --git a/src/cocoa/Base.lproj/MainMenu.nib b/src/cocoa/Base.lproj/MainMenu.nib
new file mode 100644 (file)
index 0000000..2e43fe3
Binary files /dev/null and b/src/cocoa/Base.lproj/MainMenu.nib differ
diff --git a/src/cocoa/Base.lproj/MainMenu.xib b/src/cocoa/Base.lproj/MainMenu.xib
new file mode 100644 (file)
index 0000000..8ffb260
--- /dev/null
@@ -0,0 +1,210 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="21507" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
+    <dependencies>
+        <deployment identifier="macosx"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21507"/>
+    </dependencies>
+    <objects>
+        <customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
+            <connections>
+                <outlet property="delegate" destination="Voe-Tx-rLC" id="GzC-gU-4Uq"/>
+            </connections>
+        </customObject>
+        <customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
+        <customObject id="-3" userLabel="Application" customClass="NSMenu"/>
+        <customObject id="Voe-Tx-rLC" customClass="AngbandAppDelegate">
+            <connections>
+                <outlet property="commandMenu" destination="V5r-G0-OsI" id="1Zs-qw-zFF"/>
+                <outlet property="graphicsMenu" destination="HjX-nW-gDH" id="viC-2N-jWh"/>
+            </connections>
+        </customObject>
+        <customObject id="YLy-65-1bz" customClass="NSFontManager"/>
+        <menu title="Main Menu" systemMenu="main" id="AYu-sK-qS6">
+            <items>
+                <menuItem title="Hengband" id="1Xt-HY-uBw">
+                    <modifierMask key="keyEquivalentModifierMask"/>
+                    <menu key="submenu" title="Hengband" systemMenu="apple" id="uQy-DD-JDr">
+                        <items>
+                            <menuItem title="About Hengband" id="5kV-Vb-QxS" userLabel="About Hengband">
+                                <modifierMask key="keyEquivalentModifierMask"/>
+                                <connections>
+                                    <action selector="orderFrontStandardAboutPanel:" target="-1" id="Exp-CZ-Vem"/>
+                                </connections>
+                            </menuItem>
+                            <menuItem isSeparatorItem="YES" id="VOq-y0-SEH"/>
+                            <menuItem title="Preferences…" keyEquivalent="," id="BOF-NM-1cW"/>
+                            <menuItem isSeparatorItem="YES" id="wFC-TO-SCJ"/>
+                            <menuItem title="Services" id="NMo-om-nkz">
+                                <modifierMask key="keyEquivalentModifierMask"/>
+                                <menu key="submenu" title="Services" systemMenu="services" id="hz9-B4-Xy5"/>
+                            </menuItem>
+                            <menuItem isSeparatorItem="YES" id="4je-JR-u6R"/>
+                            <menuItem title="Hide Hengband" keyEquivalent="h" id="Olw-nP-bQN" userLabel="Hide Hengband">
+                                <connections>
+                                    <action selector="hide:" target="-1" id="PnN-Uc-m68"/>
+                                </connections>
+                            </menuItem>
+                            <menuItem title="Hide Others" keyEquivalent="h" id="Vdr-fp-XzO">
+                                <modifierMask key="keyEquivalentModifierMask" option="YES" command="YES"/>
+                                <connections>
+                                    <action selector="hideOtherApplications:" target="-1" id="VT4-aY-XCT"/>
+                                </connections>
+                            </menuItem>
+                            <menuItem title="Show All" id="Kd2-mp-pUS">
+                                <modifierMask key="keyEquivalentModifierMask"/>
+                                <connections>
+                                    <action selector="unhideAllApplications:" target="-1" id="Dhg-Le-xox"/>
+                                </connections>
+                            </menuItem>
+                            <menuItem isSeparatorItem="YES" id="kCx-OE-vgT"/>
+                            <menuItem title="Quit Hengband" keyEquivalent="q" id="4sb-4s-VLi" userLabel="Quit Hengband">
+                                <connections>
+                                    <action selector="terminate:" target="-1" id="Te7-pn-YzF"/>
+                                </connections>
+                            </menuItem>
+                        </items>
+                    </menu>
+                </menuItem>
+                <menuItem title="File" id="dMs-cI-mzQ">
+                    <modifierMask key="keyEquivalentModifierMask"/>
+                    <menu key="submenu" title="File" id="bib-Uj-vzu">
+                        <items>
+                            <menuItem title="New" keyEquivalent="n" id="Was-JA-tGl">
+                                <connections>
+                                    <action selector="newGame:" target="Voe-Tx-rLC" id="der-R0-E3m"/>
+                                </connections>
+                            </menuItem>
+                            <menuItem title="Open…" keyEquivalent="o" id="IAo-SY-fd9">
+                                <connections>
+                                    <action selector="openGame:" target="Voe-Tx-rLC" id="lRd-hc-bRg"/>
+                                </connections>
+                            </menuItem>
+                            <menuItem isSeparatorItem="YES" id="m54-Is-iLE"/>
+                            <menuItem title="Save…" keyEquivalent="s" id="pxx-59-PXV">
+                                <connections>
+                                    <action selector="saveGame:" target="Voe-Tx-rLC" id="qzx-Gf-uzC"/>
+                                </connections>
+                            </menuItem>
+                        </items>
+                    </menu>
+                </menuItem>
+                <menuItem title="Settings" id="KRs-UN-gSY">
+                    <modifierMask key="keyEquivalentModifierMask"/>
+                    <menu key="submenu" title="Settings" id="yJq-0y-VBt">
+                        <items>
+                            <menuItem title="Choose Font ..." keyEquivalent="t" id="wd6-md-9jG">
+                                <connections>
+                                    <action selector="editFont:" target="Voe-Tx-rLC" id="Czs-ZF-d6u"/>
+                                </connections>
+                            </menuItem>
+                            <menuItem title="Graphics" id="WnC-Sx-T6W" userLabel="Graphics">
+                                <modifierMask key="keyEquivalentModifierMask"/>
+                                <menu key="submenu" title="Graphics" id="HjX-nW-gDH" userLabel="Graphics Menu">
+                                    <connections>
+                                        <outlet property="delegate" destination="Voe-Tx-rLC" id="chx-c6-Xwx"/>
+                                    </connections>
+                                </menu>
+                            </menuItem>
+                            <menuItem title="Frame Rate" tag="150" id="j2X-op-BDF" userLabel="Frame Rate">
+                                <modifierMask key="keyEquivalentModifierMask"/>
+                                <menu key="submenu" title="Frame Rate" id="bv6-jc-SiG">
+                                    <items>
+                                        <menuItem title="Unlimited (no animation)" keyEquivalent="1" id="86N-Gt-5KF" userLabel="Unlimited (no animation)">
+                                            <connections>
+                                                <action selector="setRefreshRate:" target="Voe-Tx-rLC" id="jkB-HC-ibR"/>
+                                            </connections>
+                                        </menuItem>
+                                        <menuItem title="60 per second" tag="60" keyEquivalent="2" id="Ijd-bJ-27K" userLabel="60 per second">
+                                            <connections>
+                                                <action selector="setRefreshRate:" target="Voe-Tx-rLC" id="5SR-6e-70y"/>
+                                            </connections>
+                                        </menuItem>
+                                        <menuItem title="50 per second" tag="50" keyEquivalent="3" id="VBb-sl-VxX">
+                                            <connections>
+                                                <action selector="setRefreshRate:" target="Voe-Tx-rLC" id="ukg-4G-kwU"/>
+                                            </connections>
+                                        </menuItem>
+                                        <menuItem title="40 per second" tag="40" keyEquivalent="4" id="ZN5-kE-LHc">
+                                            <connections>
+                                                <action selector="setRefreshRate:" target="Voe-Tx-rLC" id="paN-nZ-exb"/>
+                                            </connections>
+                                        </menuItem>
+                                        <menuItem title="30 per second" tag="30" keyEquivalent="5" id="PdF-nK-k8P">
+                                            <connections>
+                                                <action selector="setRefreshRate:" target="Voe-Tx-rLC" id="DmI-Tf-blY"/>
+                                            </connections>
+                                        </menuItem>
+                                        <menuItem title="20 per second" tag="20" keyEquivalent="6" id="Pka-9x-yi4" userLabel="20 per second">
+                                            <connections>
+                                                <action selector="setRefreshRate:" target="Voe-Tx-rLC" id="Lxs-Bp-ho6"/>
+                                            </connections>
+                                        </menuItem>
+                                        <menuItem title="10 per second" tag="10" keyEquivalent="7" id="uRE-OY-ABT" userLabel="10 per second">
+                                            <connections>
+                                                <action selector="setRefreshRate:" target="Voe-Tx-rLC" id="P0O-gy-9LJ"/>
+                                            </connections>
+                                        </menuItem>
+                                    </items>
+                                </menu>
+                            </menuItem>
+                            <menuItem title="Sound and Music ..." id="FdG-dF-c6Z" userLabel="Sound and Music ...">
+                                <modifierMask key="keyEquivalentModifierMask"/>
+                                <connections>
+                                    <action selector="showSoundAndMusicPanel:" target="Voe-Tx-rLC" id="8zL-wI-O0R"/>
+                                </connections>
+                            </menuItem>
+                            <menuItem title="Toggle Wide Tiles" state="on" id="tR9-z3-4SZ" userLabel="Toggle Wide Tiles">
+                                <modifierMask key="keyEquivalentModifierMask"/>
+                                <connections>
+                                    <action selector="toggleWideTiles:" target="Voe-Tx-rLC" id="fLa-ij-GCQ"/>
+                                </connections>
+                            </menuItem>
+                        </items>
+                    </menu>
+                </menuItem>
+                <menuItem title="Command" id="czb-8N-K9z" userLabel="Command">
+                    <modifierMask key="keyEquivalentModifierMask"/>
+                    <menu key="submenu" title="Command" id="V5r-G0-OsI" userLabel="Command Menu"/>
+                </menuItem>
+                <menuItem title="Window" id="aUF-d1-5bR">
+                    <modifierMask key="keyEquivalentModifierMask"/>
+                    <menu key="submenu" title="Window" systemMenu="window" id="Td7-aD-5lo">
+                        <items>
+                            <menuItem title="Minimize" keyEquivalent="m" id="OY7-WF-poV">
+                                <connections>
+                                    <action selector="performMiniaturize:" target="-1" id="VwT-WD-YPe"/>
+                                </connections>
+                            </menuItem>
+                            <menuItem title="Zoom" id="R4o-n2-Eq4">
+                                <modifierMask key="keyEquivalentModifierMask"/>
+                                <connections>
+                                    <action selector="performZoom:" target="-1" id="DIl-cC-cCs"/>
+                                </connections>
+                            </menuItem>
+                            <menuItem isSeparatorItem="YES" id="eu3-7i-yIM"/>
+                            <menuItem title="Bring All to Front" id="LE2-aR-0XJ">
+                                <modifierMask key="keyEquivalentModifierMask"/>
+                                <connections>
+                                    <action selector="arrangeInFront:" target="-1" id="DRN-fu-gQh"/>
+                                </connections>
+                            </menuItem>
+                        </items>
+                    </menu>
+                </menuItem>
+                <menuItem title="Help" id="wpr-3q-Mcd">
+                    <modifierMask key="keyEquivalentModifierMask"/>
+                    <menu key="submenu" title="Help" systemMenu="help" id="F2S-fz-NVQ">
+                        <items>
+                            <menuItem title="Hengband Help" keyEquivalent="?" id="FKE-Sm-Kum" userLabel="Hengband Help">
+                                <connections>
+                                    <action selector="showHelp:" target="-1" id="y7X-2Q-9no"/>
+                                </connections>
+                            </menuItem>
+                        </items>
+                    </menu>
+                </menuItem>
+            </items>
+            <point key="canvasLocation" x="132" y="154"/>
+        </menu>
+    </objects>
+</document>
diff --git a/src/cocoa/Base.lproj/SoundAndMusic.nib b/src/cocoa/Base.lproj/SoundAndMusic.nib
new file mode 100644 (file)
index 0000000..cf4acb8
Binary files /dev/null and b/src/cocoa/Base.lproj/SoundAndMusic.nib differ
diff --git a/src/cocoa/Base.lproj/SoundAndMusic.xib b/src/cocoa/Base.lproj/SoundAndMusic.xib
new file mode 100644 (file)
index 0000000..ad5b9c9
--- /dev/null
@@ -0,0 +1,165 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="21507" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
+    <dependencies>
+        <deployment identifier="macosx"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21507"/>
+        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
+    </dependencies>
+    <objects>
+        <customObject id="-2" userLabel="File's Owner" customClass="SoundAndMusicPanelController">
+            <connections>
+                <outlet property="musicEnabledControl" destination="yd0-H6-FT1" id="haI-My-g5n"/>
+                <outlet property="musicPausedWhenInactiveControl" destination="Z7a-h3-Z6B" id="08u-nG-2iz"/>
+                <outlet property="musicTransitionTimeControl" destination="AkU-q6-TkR" id="iwd-dc-xow"/>
+                <outlet property="musicVolumeControl" destination="r8d-9c-ObP" id="qf8-0E-lZp"/>
+                <outlet property="soundEnabledControl" destination="P10-1o-zNT" id="BVI-EV-7If"/>
+                <outlet property="soundVolumeControl" destination="JfF-pR-frk" id="NFw-u8-eL9"/>
+                <outlet property="window" destination="mVt-nk-Qo0" id="f7c-To-L8b"/>
+            </connections>
+        </customObject>
+        <customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
+        <customObject id="-3" userLabel="Application" customClass="NSObject"/>
+        <window title="Sound and Music" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" hidesOnDeactivate="YES" visibleAtLaunch="NO" frameAutosaveName="" animationBehavior="default" id="mVt-nk-Qo0" customClass="NSPanel">
+            <windowStyleMask key="styleMask" titled="YES" closable="YES" utility="YES"/>
+            <windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
+            <rect key="contentRect" x="120" y="64" width="265" height="232"/>
+            <rect key="screenRect" x="0.0" y="0.0" width="1280" height="775"/>
+            <view key="contentView" id="Nlk-Lb-y90">
+                <rect key="frame" x="0.0" y="0.0" width="337" height="234"/>
+                <autoresizingMask key="autoresizingMask"/>
+                <subviews>
+                    <textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Kei-Lu-1AE">
+                        <rect key="frame" x="5" y="211" width="113" height="16"/>
+                        <textFieldCell key="cell" lineBreakMode="clipping" title="Incidental Sounds" id="aWu-Lm-bgH">
+                            <font key="font" metaFont="system"/>
+                            <color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
+                            <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
+                        </textFieldCell>
+                    </textField>
+                    <slider verticalHuggingPriority="750" id="JfF-pR-frk" userLabel="soundVolumeSlider">
+                        <rect key="frame" x="115" y="155" width="204" height="28"/>
+                        <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
+                        <sliderCell key="cell" alignment="left" minValue="1" maxValue="100" doubleValue="30" tickMarkPosition="above" sliderType="linear" id="eKG-5Z-eGo"/>
+                        <connections>
+                            <action selector="respondToSoundVolumeSlider:" target="-2" id="FOm-uN-n61"/>
+                        </connections>
+                    </slider>
+                    <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="P10-1o-zNT" userLabel="soundEnabledCheckBox">
+                        <rect key="frame" x="25" y="186" width="75" height="18"/>
+                        <buttonCell key="cell" type="check" title="Enabled" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="u2I-E0-No1">
+                            <behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
+                            <font key="font" metaFont="system"/>
+                        </buttonCell>
+                        <connections>
+                            <action selector="respondToSoundEnabledToggle:" target="-2" id="1MV-lJ-Xs2"/>
+                        </connections>
+                    </button>
+                    <button identifier="musicEnabled" verticalHuggingPriority="750" id="yd0-H6-FT1" userLabel="musicEnabledCheckBox">
+                        <rect key="frame" x="25" y="95" width="92" height="22"/>
+                        <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
+                        <buttonCell key="cell" type="check" title="Enabled" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="Hur-85-rfj">
+                            <behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
+                            <font key="font" metaFont="system"/>
+                        </buttonCell>
+                        <connections>
+                            <action selector="respondToMusicEnabledToggle:" target="-2" id="sBT-EV-ENk"/>
+                        </connections>
+                    </button>
+                    <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Z7a-h3-Z6B">
+                        <rect key="frame" x="25" y="69" width="292" height="22"/>
+                        <buttonCell key="cell" type="check" title="Paused when application docked or inactive" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="mXt-mL-aNz">
+                            <behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
+                            <font key="font" metaFont="system"/>
+                        </buttonCell>
+                        <connections>
+                            <action selector="respondToMusicPausedWhenInactiveToggle:" target="-2" id="JtQ-Gi-tad"/>
+                        </connections>
+                    </button>
+                    <slider verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="r8d-9c-ObP" userLabel="musicVolumeSlider">
+                        <rect key="frame" x="115" y="38" width="204" height="28"/>
+                        <sliderCell key="cell" state="on" alignment="left" minValue="1" maxValue="100" doubleValue="20" tickMarkPosition="above" sliderType="linear" id="Xjg-wn-gAD"/>
+                        <connections>
+                            <action selector="respondToMusicVolumeSlider:" target="-2" id="5ed-hZ-UQO"/>
+                        </connections>
+                    </slider>
+                    <textField horizontalHuggingPriority="251" verticalHuggingPriority="750" id="2I9-zP-fUA">
+                        <rect key="frame" x="25" y="22" width="71" height="16"/>
+                        <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
+                        <textFieldCell key="cell" lineBreakMode="clipping" title="Transitions" id="h6K-gp-brC">
+                            <font key="font" metaFont="system"/>
+                            <color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
+                            <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
+                        </textFieldCell>
+                    </textField>
+                    <textField horizontalHuggingPriority="251" verticalHuggingPriority="751" translatesAutoresizingMaskIntoConstraints="NO" id="B3i-gX-R4x" userLabel="musicVolumeLabel">
+                        <rect key="frame" x="25" y="46" width="49" height="16"/>
+                        <textFieldCell key="cell" lineBreakMode="clipping" title="Volume" id="bjo-lV-gRq" userLabel="musicVolumeLabelCell">
+                            <font key="font" metaFont="system"/>
+                            <color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
+                            <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
+                        </textFieldCell>
+                    </textField>
+                    <slider toolTip="amount of time to switch from one track to another" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="AkU-q6-TkR" userLabel="transitionTimeSlider">
+                        <rect key="frame" x="115" y="14" width="204" height="28"/>
+                        <sliderCell key="cell" state="on" alignment="left" maxValue="10000" doubleValue="3000" tickMarkPosition="above" sliderType="linear" id="8Li-wU-4rN"/>
+                        <connections>
+                            <action selector="respondToMusicTransitionTimeSlider:" target="-2" id="p66-ub-MQ8"/>
+                        </connections>
+                    </slider>
+                    <textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="PS4-bM-MoX" userLabel="soundVolumeLabel">
+                        <rect key="frame" x="25" y="163" width="49" height="16"/>
+                        <textFieldCell key="cell" lineBreakMode="clipping" title="Volume" id="cYF-nO-lpm" userLabel="soundVolumeLabelCell">
+                            <font key="font" metaFont="system"/>
+                            <color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
+                            <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
+                        </textFieldCell>
+                    </textField>
+                    <textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="irV-Tu-qww">
+                        <rect key="frame" x="5" y="122" width="116" height="16"/>
+                        <textFieldCell key="cell" lineBreakMode="clipping" title="Background Music" id="uDO-28-V03">
+                            <font key="font" metaFont="system"/>
+                            <color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
+                            <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
+                        </textFieldCell>
+                    </textField>
+                </subviews>
+                <constraints>
+                    <constraint firstItem="Z7a-h3-Z6B" firstAttribute="top" secondItem="yd0-H6-FT1" secondAttribute="bottom" constant="6" symbolic="YES" id="1Fx-zh-MgX"/>
+                    <constraint firstItem="r8d-9c-ObP" firstAttribute="trailing" secondItem="JfF-pR-frk" secondAttribute="trailing" id="1Sr-qB-xJV"/>
+                    <constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="irV-Tu-qww" secondAttribute="trailing" constant="20" symbolic="YES" id="3RA-93-Lum"/>
+                    <constraint firstItem="2I9-zP-fUA" firstAttribute="top" secondItem="B3i-gX-R4x" secondAttribute="bottom" constant="8" symbolic="YES" id="4Xn-tz-Xxf"/>
+                    <constraint firstItem="Z7a-h3-Z6B" firstAttribute="leading" secondItem="P10-1o-zNT" secondAttribute="leading" id="5qF-wu-rlZ"/>
+                    <constraint firstItem="P10-1o-zNT" firstAttribute="leading" secondItem="Kei-Lu-1AE" secondAttribute="leading" constant="20" id="6Yb-ud-D6H"/>
+                    <constraint firstItem="irV-Tu-qww" firstAttribute="top" relation="greaterThanOrEqual" secondItem="PS4-bM-MoX" secondAttribute="bottom" priority="600" constant="25" id="CPh-y4-7zH"/>
+                    <constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="JfF-pR-frk" secondAttribute="trailing" constant="20" symbolic="YES" id="Ea1-o5-f6n"/>
+                    <constraint firstItem="r8d-9c-ObP" firstAttribute="trailing" secondItem="AkU-q6-TkR" secondAttribute="trailing" id="MvH-5j-fjB"/>
+                    <constraint firstItem="r8d-9c-ObP" firstAttribute="leading" secondItem="JfF-pR-frk" secondAttribute="leading" id="Rm8-rT-xet"/>
+                    <constraint firstItem="Kei-Lu-1AE" firstAttribute="leading" secondItem="Nlk-Lb-y90" secondAttribute="leading" constant="7" id="Un1-Zm-mKq"/>
+                    <constraint firstItem="AkU-q6-TkR" firstAttribute="centerY" secondItem="2I9-zP-fUA" secondAttribute="centerY" id="Vta-H6-ZLa"/>
+                    <constraint firstAttribute="bottom" relation="greaterThanOrEqual" secondItem="AkU-q6-TkR" secondAttribute="bottom" constant="20" symbolic="YES" id="XJN-fL-iDT"/>
+                    <constraint firstItem="2I9-zP-fUA" firstAttribute="leading" secondItem="P10-1o-zNT" secondAttribute="leading" id="dh3-4k-8rI"/>
+                    <constraint firstItem="Kei-Lu-1AE" firstAttribute="top" secondItem="Nlk-Lb-y90" secondAttribute="top" constant="7" id="e3r-su-yAX"/>
+                    <constraint firstItem="yd0-H6-FT1" firstAttribute="leading" secondItem="P10-1o-zNT" secondAttribute="leading" id="eN5-vD-Q0I"/>
+                    <constraint firstItem="B3i-gX-R4x" firstAttribute="leading" secondItem="P10-1o-zNT" secondAttribute="leading" id="eO4-Mm-Sac"/>
+                    <constraint firstItem="yd0-H6-FT1" firstAttribute="top" secondItem="irV-Tu-qww" secondAttribute="bottom" constant="6" id="eke-6I-DLg"/>
+                    <constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="P10-1o-zNT" secondAttribute="trailing" constant="20" symbolic="YES" id="emq-ZO-Kas"/>
+                    <constraint firstItem="yd0-H6-FT1" firstAttribute="leading" secondItem="P10-1o-zNT" secondAttribute="leading" id="fOW-1U-cFo"/>
+                    <constraint firstItem="P10-1o-zNT" firstAttribute="top" secondItem="Kei-Lu-1AE" secondAttribute="bottom" constant="8" symbolic="YES" id="gAp-bL-O4A"/>
+                    <constraint firstItem="r8d-9c-ObP" firstAttribute="centerY" secondItem="B3i-gX-R4x" secondAttribute="centerY" id="h9X-kb-hGr"/>
+                    <constraint firstItem="JfF-pR-frk" firstAttribute="centerY" secondItem="PS4-bM-MoX" secondAttribute="centerY" id="kU6-QK-HdF"/>
+                    <constraint firstItem="PS4-bM-MoX" firstAttribute="top" secondItem="P10-1o-zNT" secondAttribute="bottom" constant="8" symbolic="YES" id="lZR-2z-q6K"/>
+                    <constraint firstItem="AkU-q6-TkR" firstAttribute="leading" secondItem="JfF-pR-frk" secondAttribute="leading" id="m1M-dl-UP3"/>
+                    <constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="Kei-Lu-1AE" secondAttribute="trailing" constant="20" symbolic="YES" id="n1g-JZ-I2c"/>
+                    <constraint firstItem="irV-Tu-qww" firstAttribute="leading" secondItem="Kei-Lu-1AE" secondAttribute="leading" id="q0n-E3-aOl"/>
+                    <constraint firstItem="B3i-gX-R4x" firstAttribute="top" secondItem="Z7a-h3-Z6B" secondAttribute="bottom" constant="8" symbolic="YES" id="tHk-gR-ioR"/>
+                    <constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="Z7a-h3-Z6B" secondAttribute="trailing" constant="20" symbolic="YES" id="uN4-c7-IoR"/>
+                    <constraint firstItem="AkU-q6-TkR" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="2I9-zP-fUA" secondAttribute="trailing" constant="20" id="v1y-OQ-C7k"/>
+                    <constraint firstItem="r8d-9c-ObP" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="B3i-gX-R4x" secondAttribute="trailing" constant="20" id="vnr-Im-Q7Y"/>
+                    <constraint firstItem="JfF-pR-frk" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="PS4-bM-MoX" secondAttribute="trailing" constant="20" id="vyO-0w-T2I"/>
+                    <constraint firstItem="PS4-bM-MoX" firstAttribute="leading" secondItem="P10-1o-zNT" secondAttribute="leading" id="zuq-qh-7Bz"/>
+                </constraints>
+            </view>
+            <point key="canvasLocation" x="-343.5" y="-232"/>
+        </window>
+    </objects>
+</document>
diff --git a/src/cocoa/CommandMenu.plist b/src/cocoa/CommandMenu.plist
new file mode 100644 (file)
index 0000000..cf68853
--- /dev/null
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<array>
+       <dict>
+               <key>Title</key>
+               <string>Knowledge</string>
+               <key>KeyEquivalent</key>
+               <string>k</string>
+               <key>ShiftModifier</key>
+               <false/>
+               <key>OptionModifier</key>
+               <false/>
+               <key>AngbandCommand</key>
+               <string>~</string>
+       </dict>
+       <dict>
+               <key>Title</key>
+               <string>Inscribe</string>
+               <key>KeyEquivalent</key>
+               <string>i</string>
+               <key>ShiftModifier</key>
+               <false/>
+               <key>OptionModifier</key>
+               <false/>
+               <key>AngbandCommand</key>
+               <string>{</string>
+       </dict>
+       <dict>
+               <key>Title</key>
+               <string>Uninscribe</string>
+               <key>KeyEquivalent</key>
+               <string>i</string>
+               <key>ShiftModifier</key>
+               <true/>
+               <key>OptionModifier</key>
+               <false/>
+               <key>AngbandCommand</key>
+               <string>}</string>
+       </dict>
+</array>
+</plist>
diff --git a/src/cocoa/Data.icns b/src/cocoa/Data.icns
new file mode 100644 (file)
index 0000000..67d750f
Binary files /dev/null and b/src/cocoa/Data.icns differ
diff --git a/src/cocoa/Edit.icns b/src/cocoa/Edit.icns
new file mode 100644 (file)
index 0000000..532699c
Binary files /dev/null and b/src/cocoa/Edit.icns differ
diff --git a/src/cocoa/Save.icns b/src/cocoa/Save.icns
new file mode 100644 (file)
index 0000000..9374c4a
Binary files /dev/null and b/src/cocoa/Save.icns differ
diff --git a/src/cocoa/SoundAndMusic.h b/src/cocoa/SoundAndMusic.h
new file mode 100644 (file)
index 0000000..f9fa685
--- /dev/null
@@ -0,0 +1,81 @@
+/**
+ * \file SoundAndMusc.h
+ * \brief Declare interface to sound and music configuration panel used by
+ * the OS X front end.
+ */
+
+#import <Cocoa/Cocoa.h>
+
+
+/**
+ * Declare a protocol to encapsulate changes to the settings for sounds and
+ * music.
+ */
+@protocol SoundAndMusicChanges
+- (void)changeSoundEnabled:(BOOL)newv;
+- (void)changeSoundVolume:(NSInteger)newv;
+- (void)changeMusicEnabled:(BOOL)newv;
+- (void)changeMusicPausedWhenInactive:(BOOL)newv;
+- (void)changeMusicVolume:(NSInteger)newv;
+- (void)changeMusicTransitionTime:(NSInteger)newv;
+- (void)soundAndMusicPanelWillClose;
+@end
+
+
+/**
+ * Declare a NSWindowController subclass to load the panel from the nib file.
+ */
+@interface SoundAndMusicPanelController : NSWindowController <NSWindowDelegate>
+
+/**
+ * Hold whether or not incidental sounds (and beeps) are played.
+ */
+@property (nonatomic, getter=isSoundEnabled) BOOL soundEnabled;
+
+/**
+ * Hold the volume for incidental sounds as a percentage (1 to 100) of the
+ * system volume.
+ */
+@property (nonatomic) NSInteger soundVolume;
+
+/**
+ * Hold whether or not background music is played.
+ */
+@property (nonatomic, getter=isMusicEnabled) BOOL musicEnabled;
+
+/**
+ * Hold whether or not currently playing music tracks are paused when the
+ * containing application becomes inactive.
+ */
+@property (nonatomic, getter=isMusicPausedWhenInactive)
+               BOOL musicPausedWhenInactive;
+
+/**
+ * Hold the volume for background music as a percentage (1 to 100) of the
+ * system volume.
+ */
+@property (nonatomic) NSInteger musicVolume;
+
+/**
+ * Hold the transition time in milliseconds for when a background music track
+ * is started when another background music track is already playing.  If
+ * than or equal to zero, the current track is stopped and the new track
+ * starts playing at full volume without any extra delay.
+ */
+@property (nonatomic) NSInteger musicTransitionTime;
+
+/**
+ * Is the delegate that responds when one of the settings changes.
+ */
+@property (weak, nonatomic) id<SoundAndMusicChanges> changeHandler;
+
+/* These are implementation details. */
+@property (strong) IBOutlet NSPanel *window;
+@property (strong) IBOutlet NSButton *soundEnabledControl;
+@property (strong) IBOutlet NSSlider *soundVolumeControl;
+@property (strong) IBOutlet NSButton *musicEnabledControl;
+@property (strong) IBOutlet NSButton *musicPausedWhenInactiveControl;
+@property (strong) IBOutlet NSSlider *musicVolumeControl;
+@property (strong) IBOutlet NSSlider *musicTransitionTimeControl;
+
+@end
diff --git a/src/cocoa/SoundAndMusic.mm b/src/cocoa/SoundAndMusic.mm
new file mode 100644 (file)
index 0000000..e42206c
--- /dev/null
@@ -0,0 +1,230 @@
+/**
+ * \file SoundAndMusic.m
+ * \brief Define interface to sound and music configuration panel used by the
+ * OS X front end.
+ *
+ * Copyright (c) 2023 Eric Branlund
+ *
+ * This work is free software; you can redistribute it and/or modify it
+ * under the terms of either:
+ *
+ * a) the GNU General Public License as published by the Free Software
+ *    Foundation, version 2, or
+ *
+ * b) the "Angband licence":
+ *    This software may be copied and distributed for educational, research,
+ *    and not for profit purposes provided that this copyright and statement
+ *    are included in all such copies.  Other copyrights may also apply.
+ */
+
+#import "SoundAndMusic.h"
+
+
+@implementation SoundAndMusicPanelController
+
+/*
+ * Don't use the implicit @synthesize since this property is declared in a
+ * superclass, NSWindowController.
+ */
+@dynamic window;
+
+
+/*
+ * Handle property methods where need more than is provided by the default
+ * synthesis.
+ */
+- (BOOL)isSoundEnabled
+{
+       return ([self soundEnabledControl]
+               && [self soundEnabledControl].state != NSControlStateValueOff)
+               ? YES : NO;
+}
+
+
+- (void)setSoundEnabled:(BOOL)v
+{
+       if ([self soundEnabledControl]) {
+               [self soundEnabledControl].state = (v) ?
+                       NSControlStateValueOn : NSControlStateValueOff;
+       }
+}
+
+
+- (NSInteger)soundVolume
+{
+       return ([self soundVolumeControl]) ?
+               [self soundVolumeControl].integerValue : 100;
+}
+
+
+- (void)setSoundVolume:(NSInteger)v
+{
+       if ([self soundVolumeControl]) {
+               if (v < 1) {
+                       v = 1;
+               } else if (v > 100) {
+                       v = 100;
+               }
+               [self soundVolumeControl].integerValue = v;
+       }
+}
+
+
+- (BOOL)isMusicEnabled
+{
+       return ([self musicEnabledControl]
+               && [self musicEnabledControl].state != NSControlStateValueOff)
+               ? YES : NO;
+}
+
+
+- (void)setMusicEnabled:(BOOL)v
+{
+       if ([self musicEnabledControl]) {
+               [self musicEnabledControl].state = (v) ?
+                       NSControlStateValueOn : NSControlStateValueOff;
+       }
+}
+
+
+- (BOOL)isMusicPausedWhenInactive
+{
+       return ([self musicPausedWhenInactiveControl]
+               && [self musicPausedWhenInactiveControl].state
+               != NSControlStateValueOn) ? NO : YES;
+}
+
+
+- (void)setMusicPausedWhenInactive:(BOOL)v
+{
+       if ([self musicPausedWhenInactiveControl]) {
+               [self musicPausedWhenInactiveControl].state =
+                       (v) ? NSControlStateValueOn : NSControlStateValueOff;
+       }
+}
+
+
+- (NSInteger)musicVolume
+{
+       return ([self musicVolumeControl]) ?
+               [self musicVolumeControl].integerValue : 100;
+}
+
+
+- (void)setMusicVolume:(NSInteger)v
+{
+       if ([self musicVolumeControl]) {
+               if (v < 1) {
+                       v = 1;
+               } else if (v > 100) {
+                       v = 100;
+               }
+               [self musicVolumeControl].integerValue = v;
+       }
+}
+
+
+- (NSInteger)musicTransitionTime
+{
+       return ([self musicTransitionTimeControl]) ?
+               [self musicTransitionTimeControl].integerValue : 0;
+}
+
+
+- (void)setMusicTransitionTime:(NSInteger)v
+{
+       if ([self musicTransitionTimeControl]) {
+               if (v < 0) {
+                       v = 0;
+               } else if (v > [self musicTransitionTimeControl].maxValue) {
+                       v = (NSInteger)([self musicTransitionTimeControl].maxValue);
+               }
+               [self musicTransitionTimeControl].integerValue = v;
+       }
+}
+
+
+/**
+ * Supply the NSWindowDelegate's windowWillClose method to relay the close
+ * message through the SoundAndMusicChanges protocol.
+ */
+- (void)windowWillClose:(NSNotification *)notification
+{
+       if ([self changeHandler]) {
+               [[self changeHandler] soundAndMusicPanelWillClose];
+       }
+}
+
+
+/**
+ * Override the NSWindowController property getter to get the appropriate nib
+ * file.
+ */
+- (NSString *)windowNibName
+{
+       return @"SoundAndMusic";
+}
+
+
+/**
+ * Override the NSWindowController function to set some attributes on the
+ * window.
+ */
+- (void)windowDidLoad
+{
+       [super windowDidLoad];
+       self.window.floatingPanel = YES;
+       self.window.becomesKeyOnlyIfNeeded = YES;
+       [self.window center];
+       self.window.delegate = self;
+}
+
+
+- (IBAction)respondToSoundEnabledToggle:(id)sender
+{
+       if ([self changeHandler]) {
+               [[self changeHandler] changeSoundEnabled:self.isSoundEnabled];
+       }
+}
+
+
+- (IBAction)respondToSoundVolumeSlider:(id)sender
+{
+       if ([self changeHandler]) {
+               [[self changeHandler] changeSoundVolume:self.soundVolume];
+       }
+}
+
+
+- (IBAction)respondToMusicEnabledToggle:(id)sender
+{
+       if ([self changeHandler]) {
+               [[self changeHandler] changeMusicEnabled:self.isMusicEnabled];
+       }
+}
+
+
+- (IBAction)respondToMusicPausedWhenInactiveToggle:(id)sender
+{
+       if ([self changeHandler]) {
+               [[self changeHandler] changeMusicPausedWhenInactive:self.isMusicPausedWhenInactive];
+       }
+}
+
+
+- (IBAction)respondToMusicVolumeSlider:(id)sender
+{
+       if ([self changeHandler]) {
+               [[self changeHandler] changeMusicVolume:self.musicVolume];
+       }
+}
+
+
+- (IBAction)respondToMusicTransitionTimeSlider:(id)sender
+{
+       if ([self changeHandler]) {
+               [[self changeHandler] changeMusicTransitionTime:self.musicTransitionTime];
+       }
+}
+
+@end
diff --git a/src/cocoa/en.lproj/CommandMenu.strings b/src/cocoa/en.lproj/CommandMenu.strings
new file mode 100644 (file)
index 0000000..ab12d1d
--- /dev/null
@@ -0,0 +1,8 @@
+/* Knowledge; Angband command "~" */
+"Knowledge" = "Knowledge";
+
+/* Inscribe; Angband command "{" */
+"Inscribe" = "Inscribe";
+
+/* Uninscribe; Angband command "}" */
+"Uninscribe" = "Uninscribe";
diff --git a/src/cocoa/en.lproj/GraphicsMenu.strings b/src/cocoa/en.lproj/GraphicsMenu.strings
new file mode 100644 (file)
index 0000000..814b932
--- /dev/null
@@ -0,0 +1,8 @@
+/* Entry in lib/xtra/graf/list.txt for listing available graphics modes */
+"Classic ASCII" = "Classic ASCII";
+
+/* Entry in lib/xtra/graf/list.txt for listing available graphics modes */
+"Original Tiles" = "Original Tiles";
+
+/* Entry in lib/xtra/graf/list.txt for listing available graphics modes */
+"Adam Bolt's tiles" = "Adam Bolt's Tiles";
diff --git a/src/cocoa/en.lproj/Localizable.strings b/src/cocoa/en.lproj/Localizable.strings
new file mode 100644 (file)
index 0000000..b5d1359
--- /dev/null
@@ -0,0 +1,17 @@
+/* Alert text for generic warning */
+"Warning" = "Warning";
+
+/* Alert text for missing resources */
+"Error.MissingResources" = "Missing Resources";
+
+/* Alert informative message for missing Angband lib/ folder */
+"Error.MissingAngbandLib" = "Hengband was unable to find required resources and must quit. Please send a bug report to \"hengband-dev@lists.sourceforge.jp\".";
+
+/* Alert text for failed tile set load */
+"Error.TileSetLoadFailed" = "Failed to Load Tile Set";
+
+/* Alert informative message for failed tile set load */
+"Error.TileSetRevertToASCII" = "Could not load the tile set.  Switched back to ASCII.";
+
+/* Quit */
+"Label.Quit" = "Quit";
diff --git a/src/cocoa/hengband_Icons.icns b/src/cocoa/hengband_Icons.icns
new file mode 100644 (file)
index 0000000..a8cd4dd
Binary files /dev/null and b/src/cocoa/hengband_Icons.icns differ
diff --git a/src/cocoa/ja.lproj/CommandMenu.strings b/src/cocoa/ja.lproj/CommandMenu.strings
new file mode 100644 (file)
index 0000000..f163611
--- /dev/null
@@ -0,0 +1,8 @@
+/* Knowledge; Angband command "~" */
+"Knowledge" = "アーティファクトやユニークなどをチェックする";
+
+/* Inscribe; Angband command "{" */
+"Inscribe" = "アイテムに銘を刻む";
+
+/* Uninscribe; Angband command "}" */
+"Uninscribe" = "アイテムの銘を消す";
diff --git a/src/cocoa/ja.lproj/GraphicsMenu.strings b/src/cocoa/ja.lproj/GraphicsMenu.strings
new file mode 100644 (file)
index 0000000..886fb5f
--- /dev/null
@@ -0,0 +1,8 @@
+/* Entry in lib/xtra/graf/list.txt for listing available graphics modes */
+"Classic ASCII" = "文字";
+
+/* Entry in lib/xtra/graf/list.txt for listing available graphics modes */
+"Original Tiles" = "タイル";
+
+/* Entry in lib/xtra/graf/list.txt for listing available graphics modes */
+"Adam Bolt's tiles" = "Adam Boltのグラフィックビットマップ";
diff --git a/src/cocoa/ja.lproj/Localizable.strings b/src/cocoa/ja.lproj/Localizable.strings
new file mode 100644 (file)
index 0000000..ef8190c
--- /dev/null
@@ -0,0 +1,17 @@
+/* Alert text for generic warning */
+"Warning" = "警告";
+
+/* Alert text for missing resources */
+"Error.MissingResources" = "不足しているリソース";
+
+/* Alert informative message for missing Angband lib/ folder */
+"Error.MissingAngbandLib" = "変愚蛮怒 は必要なリソースを見つけることができなかったため、終了する必要があります。 バグレポートを「hengband-dev@lists.sourceforge.jp」に送信してください。";
+
+/* Alert text for failed tile set load */
+"Error.TileSetLoadFailed" = "タイルセットを読み込めませんでした";
+
+/* Alert informative message for failed tile set load */
+"Error.TileSetRevertToASCII" = "タイルセットを読み込めませんでした。 ASCIIに切り替えます。";
+
+/* Quit */
+"Label.Quit" = "を終了";
diff --git a/src/cocoa/ja.lproj/MainMenu.strings b/src/cocoa/ja.lproj/MainMenu.strings
new file mode 100644 (file)
index 0000000..4592b3c
--- /dev/null
@@ -0,0 +1,126 @@
+/* Class = "NSMenuItem"; title = "Hengband"; ObjectID = "1Xt-HY-uBw"; */
+"1Xt-HY-uBw.title" = "変愚蛮怒";
+
+/* Class = "NSMenuItem"; title = "Quit Hengband"; ObjectID = "4sb-4s-VLi"; */
+"4sb-4s-VLi.title" = "変愚蛮怒 を終了";
+
+/* Class = "NSMenuItem"; title = "About Hengband"; ObjectID = "5kV-Vb-QxS"; */
+"5kV-Vb-QxS.title" = "変愚蛮怒 について";
+
+/* Class = "NSMenuItem"; title = "Unlimited (no animation)"; ObjectID = "86N-Gt-5KF"; */
+"86N-Gt-5KF.title" = "無制限";
+
+/* Class = "NSMenuItem"; title = "Window"; ObjectID = "aUF-d1-5bR"; */
+"aUF-d1-5bR.title" = "ウインドウ";
+
+/* Class = "NSMenu"; title = "Main Menu"; ObjectID = "AYu-sK-qS6"; */
+"AYu-sK-qS6.title" = "Main Menu";
+
+/* Class = "NSMenu"; title = "File"; ObjectID = "bib-Uj-vzu"; */
+"bib-Uj-vzu.title" = "ファイル";
+
+/* Class = "NSMenuItem"; title = "Preferences…"; ObjectID = "BOF-NM-1cW"; */
+"BOF-NM-1cW.title" = "環境設定…";
+
+/* Class = "NSMenu"; title = "Frame Rate"; ObjectID = "bv6-jc-SiG"; */
+"bv6-jc-SiG.title" = "フレームレート";
+
+/* Class = "NSMenuItem"; title = "Command"; ObjectID = "czb-8N-K9z"; */
+"czb-8N-K9z.title" = "命令";
+
+/* Class = "NSMenuItem"; title = "File"; ObjectID = "dMs-cI-mzQ"; */
+"dMs-cI-mzQ.title" = "ファイル";
+
+/* Class = "NSMenu"; title = "Help"; ObjectID = "F2S-fz-NVQ"; */
+"F2S-fz-NVQ.title" = "ヘルプ";
+
+/* Class = "NSMenuItem"; title = "Sound and Music ..."; ObjectID = "FdG-dF-c6Z"; */
+"FdG-dF-c6Z.title" = "効果音とBGM…";
+
+/* Class = "NSMenuItem"; title = "Hengband Help"; ObjectID = "FKE-Sm-Kum"; */
+"FKE-Sm-Kum.title" = "変愚蛮怒 ヘルプ";
+
+/* Class = "NSMenu"; title = "Graphics"; ObjectID = "HjX-nW-gDH"; */
+"HjX-nW-gDH.title" = "グラフィックス";
+
+/* Class = "NSMenu"; title = "Services"; ObjectID = "hz9-B4-Xy5"; */
+"hz9-B4-Xy5.title" = "サービス";
+
+/* Class = "NSMenuItem"; title = "Open…"; ObjectID = "IAo-SY-fd9"; */
+"IAo-SY-fd9.title" = "開く…";
+
+/* Class = "NSMenuItem"; title = "60 per second"; ObjectID = "Ijd-bJ-27K"; */
+"Ijd-bJ-27K.title" = "60 毎秒";
+
+/* Class = "NSMenuItem"; title = "Frame Rate"; ObjectID = "j2X-op-BDF"; */
+"j2X-op-BDF.title" = "フレームレート";
+
+/* Class = "NSMenuItem"; title = "Show All"; ObjectID = "Kd2-mp-pUS"; */
+"Kd2-mp-pUS.title" = "すべてを表示";
+
+/* Class = "NSMenuItem"; title = "Settings"; ObjectID = "KRs-UN-gSY"; */
+"KRs-UN-gSY.title" = "設定";
+
+/* Class = "NSMenuItem"; title = "Bring All to Front"; ObjectID = "LE2-aR-0XJ"; */
+"LE2-aR-0XJ.title" = "すべてを手前に移動";
+
+/* Class = "NSMenuItem"; title = "Services"; ObjectID = "NMo-om-nkz"; */
+"NMo-om-nkz.title" = "サービス";
+
+/* Class = "NSMenuItem"; title = "Hide Hengband"; ObjectID = "Olw-nP-bQN"; */
+"Olw-nP-bQN.title" = "変愚蛮怒 を隠す";
+
+/* Class = "NSMenuItem"; title = "Minimize"; ObjectID = "OY7-WF-poV"; */
+"OY7-WF-poV.title" = "しまう";
+
+/* Class = "NSMenuItem"; title = "30 per second"; ObjectID = "PdF-nK-k8P"; */
+"PdF-nK-k8P.title" = "30 毎秒";
+
+/* Class = "NSMenuItem"; title = "20 per second"; ObjectID = "Pka-9x-yi4"; */
+"Pka-9x-yi4.title" = "20 毎秒";
+
+/* Class = "NSMenuItem"; title = "Save…"; ObjectID = "pxx-59-PXV"; */
+"pxx-59-PXV.title" = "セーブ";
+
+/* Class = "NSMenuItem"; title = "Zoom"; ObjectID = "R4o-n2-Eq4"; */
+"R4o-n2-Eq4.title" = "拡大/縮小";
+
+/* Class = "NSMenu"; title = "Window"; ObjectID = "Td7-aD-5lo"; */
+"Td7-aD-5lo.title" = "ウインドウ";
+
+/* Class = "NSMenuItem"; title = "Toggle Wide Tiles"; ObjectID = "tR9-z3-4SZ"; */
+"tR9-z3-4SZ.title" = "2倍幅タイル";
+
+/* Class = "NSMenu"; title = "Hengband"; ObjectID = "uQy-DD-JDr"; */
+"uQy-DD-JDr.title" = "変愚蛮怒";
+
+/* Class = "NSMenuItem"; title = "10 per second"; ObjectID = "uRE-OY-ABT"; */
+"uRE-OY-ABT.title" = "10 毎秒";
+
+/* Class = "NSMenu"; title = "Command"; ObjectID = "V5r-G0-OsI"; */
+"V5r-G0-OsI.title" = "命令";
+
+/* Class = "NSMenuItem"; title = "50 per second"; ObjectID = "VBb-sl-VxX"; */
+"VBb-sl-VxX.title" = "50 毎秒";
+
+/* Class = "NSMenuItem"; title = "Hide Others"; ObjectID = "Vdr-fp-XzO"; */
+"Vdr-fp-XzO.title" = "ほかを隠す";
+
+/* Class = "NSMenuItem"; title = "New"; ObjectID = "Was-JA-tGl"; */
+"Was-JA-tGl.title" = "新規";
+
+/* Class = "NSMenuItem"; title = "Choose Font ..."; ObjectID = "wd6-md-9jG"; */
+"wd6-md-9jG.title" = "フォントを選択…";
+
+/* Class = "NSMenuItem"; title = "Graphics"; ObjectID = "WnC-Sx-T6W"; */
+"WnC-Sx-T6W.title" = "グラフィックス";
+
+/* Class = "NSMenuItem"; title = "Help"; ObjectID = "wpr-3q-Mcd"; */
+"wpr-3q-Mcd.title" = "ヘルプ";
+
+/* Class = "NSMenu"; title = "Settings"; ObjectID = "yJq-0y-VBt"; */
+"yJq-0y-VBt.title" = "設定";
+
+/* Class = "NSMenuItem"; title = "40 per second"; ObjectID = "ZN5-kE-LHc"; */
+"ZN5-kE-LHc.title" = "40 毎秒";
+
diff --git a/src/cocoa/ja.lproj/SoundAndMusic.strings b/src/cocoa/ja.lproj/SoundAndMusic.strings
new file mode 100644 (file)
index 0000000..32fbfce
--- /dev/null
@@ -0,0 +1,30 @@
+/* Class = "NSSlider"; ibShadowedToolTip = "amount of time to switch from one track to another"; ObjectID = "AkU-q6-TkR"; */
+"AkU-q6-TkR.ibShadowedToolTip" = "あるトラックから別のトラックに切り替える時間";
+
+/* Class = "NSTextFieldCell"; title = "Incidental Sounds"; ObjectID = "aWu-Lm-bgH"; */
+"aWu-Lm-bgH.title" = "効果音";
+
+/* Class = "NSTextFieldCell"; title = "Volume"; ObjectID = "bjo-lV-gRq"; */
+"bjo-lV-gRq.title" = "音量";
+
+/* Class = "NSTextFieldCell"; title = "Volume"; ObjectID = "cYF-nO-lpm"; */
+"cYF-nO-lpm.title" = "音量";
+
+/* Class = "NSTextFieldCell"; title = "Transitions"; ObjectID = "h6K-gp-brC"; */
+"h6K-gp-brC.title" = "移行間隔";
+
+/* Class = "NSButtonCell"; title = "Enabled"; ObjectID = "Hur-85-rfj"; */
+"Hur-85-rfj.title" = "BGMを使用";
+
+/* Class = "NSWindow"; title = "Sound and Music"; ObjectID = "mVt-nk-Qo0"; */
+"mVt-nk-Qo0.title" = "効果音とBGM";
+
+/* Class = "NSButtonCell"; title = "Paused when application docked or inactive"; ObjectID = "mXt-mL-aNz"; */
+"mXt-mL-aNz.title" = "変愚蛮怒が非アクティブのときにBGMを止める";
+
+/* Class = "NSButtonCell"; title = "Enabled"; ObjectID = "u2I-E0-No1"; */
+"u2I-E0-No1.title" = "効果音を使用";
+
+/* Class = "NSTextFieldCell"; title = "Background Music"; ObjectID = "uDO-28-V03"; */
+"uDO-28-V03.title" = "BGM";
+
index fdc71fa..173999e 100644 (file)
@@ -443,7 +443,12 @@ bool get_value(std::string_view prompt, int min, int max, int *value)
     std::stringstream st;
     int val;
     char tmp_val[12] = "";
+    /* std::to_chars() requires Mac OS X 10.15 or later. */
+#if !defined(MACH_O_COCOA) || (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_15)
     std::to_chars(std::begin(tmp_val), std::end(tmp_val) - 1, *value);
+#else
+    snprintf(tmp_val, sizeof(tmp_val), "%d", *value);
+#endif
     st << prompt << "(" << min << "-" << max << "): ";
     int digit = std::max(std::to_string(min).length(), std::to_string(max).length());
     while (true) {
index 3e684bf..dd3afca 100644 (file)
@@ -63,17 +63,19 @@ static void check_saved_tmp_files(const int fd, bool *force)
 void init_saved_floors(PlayerType *player_ptr, bool force)
 {
     auto fd = -1;
-    for (int i = 0; i < MAX_SAVED_FLOORS; i++) {
-        saved_floor_type *sf_ptr = &saved_floors[i];
-        auto floor_savefile = get_saved_floor_name(i);
-        safe_setuid_grab();
-        fd = fd_make(floor_savefile);
-        safe_setuid_drop();
-        check_saved_tmp_files(fd, &force);
-        safe_setuid_grab();
-        (void)fd_kill(floor_savefile);
-        safe_setuid_drop();
-        sf_ptr->floor_id = 0;
+    if (!savefile.empty()) {
+        for (int i = 0; i < MAX_SAVED_FLOORS; i++) {
+            saved_floor_type *sf_ptr = &saved_floors[i];
+            auto floor_savefile = get_saved_floor_name(i);
+            safe_setuid_grab();
+            fd = fd_make(floor_savefile);
+            safe_setuid_drop();
+            check_saved_tmp_files(fd, &force);
+            safe_setuid_grab();
+            (void)fd_kill(floor_savefile);
+            safe_setuid_drop();
+            sf_ptr->floor_id = 0;
+        }
     }
 
     max_floor_id = 1;
diff --git a/src/main-cocoa.mm b/src/main-cocoa.mm
new file mode 100644 (file)
index 0000000..8db8dbf
--- /dev/null
@@ -0,0 +1,6317 @@
+/**
+ * \file main-cocoa.mm
+ * \brief OS X front end
+ *
+ * Copyright (c) 2011 Peter Ammon
+ *
+ * This work is free software; you can redistribute it and/or modify it
+ * under the terms of either:
+ *
+ * a) the GNU General Public License as published by the Free Software
+ *    Foundation, version 2, or
+ *
+ * b) the "Angband licence":
+ *    This software may be copied and distributed for educational, research,
+ *    and not for profit purposes provided that this copyright and statement
+ *    are included in all such copies.  Other copyrights may also apply.
+ */
+
+#include "system/angband.h"
+/* This is not included in angband.h in Hengband. */
+#include "system/grafmode.h"
+
+#ifdef MACH_O_COCOA
+/* Mac headers */
+#import "cocoa/AppDelegate.h"
+#import "cocoa/SoundAndMusic.h"
+#import "cocoa/AngbandAudio.h"
+//#include <Carbon/Carbon.h> /* For keycodes */
+/* Hack - keycodes to enable compiling in macOS 10.14 */
+#define kVK_Return 0x24
+#define kVK_Tab    0x30
+#define kVK_Delete 0x33
+#define kVK_Escape 0x35
+#define kVK_ANSI_KeypadEnter 0x4C
+#endif /* MACH_O_COCOA */
+
+#include "cmd-visual/cmd-draw.h"
+#include "cmd-io/cmd-save.h"
+#include "core/asking-player.h"
+#include "core/game-play.h"
+#include "core/visuals-reseter.h"
+#include "game-option/option-flags.h"
+#include "game-option/runtime-arguments.h"
+#include "game-option/special-options.h"
+#include "io/input-key-acceptor.h"
+#include "system/angband-version.h"
+#include "system/player-type-definition.h"
+#include "system/system-variables.h"
+#include "main/angband-initializer.h"
+#include "main-unix/unix-user-ids.h"
+#include "io/files-util.h"
+#include "io/input-key-acceptor.h"
+#include "world/world.h"
+#include "term/gameterm.h"
+#include "term/screen-processor.h"
+#include "term/term-color-types.h"
+#include "view/display-messages.h"
+#include "autopick/autopick-pref-processor.h"
+#include "main/scene-table.h"
+#include "main/sound-definitions-table.h"
+#include "util/angband-files.h"
+#include "util/enum-converter.h"
+#include "window/main-window-util.h"
+
+#ifdef MACH_O_COCOA
+static NSString * const FallbackFontName = @_("HiraMaruProN-W4", "Menlo");
+static float FallbackFontSizeMain = 13.0f;
+static float FallbackFontSizeSub = 10.0f;
+static NSString * const AngbandDirectoryNameLib = @"lib";
+static NSString * const AngbandDirectoryNameBase =
+    [NSString stringWithUTF8String:std::string(VARIANT_NAME).c_str()];
+
+static NSString * const AngbandMessageCatalog = @"Localizable";
+static NSString * const AngbandTerminalsDefaultsKey = @"Terminals";
+static NSString * const AngbandTerminalRowsDefaultsKey = @"Rows";
+static NSString * const AngbandTerminalColumnsDefaultsKey = @"Columns";
+static NSString * const AngbandTerminalVisibleDefaultsKey = @"Visible";
+static NSString * const AngbandGraphicsDefaultsKey = @"GraphicsID";
+static NSString * const AngbandBigTileDefaultsKey = @"UseBigTiles";
+static NSString * const AngbandFrameRateDefaultsKey = @"FramesPerSecond";
+static NSString * const AngbandSoundEnabledDefaultsKey = @"AllowSound";
+static NSString * const AngbandSoundVolumeDefaultsKey = @"SoundVolume";
+static NSString * const AngbandMusicEnabledDefaultsKey = @"AllowMusic";
+static NSString * const AngbandMusicPausedWhenInactiveDefaultsKey =
+       @"MusicPausedWhenInactive";
+static NSString * const AngbandMusicVolumeDefaultsKey = @"MusicVolume";
+static NSString * const AngbandMusicTransitionTimeDefaultsKey =
+       @"MusicTransitionTime";
+static NSInteger const AngbandWindowMenuItemTagBase = 1000;
+static NSInteger const AngbandCommandMenuItemTagBase = 2000;
+
+/* Global defines etc from Angband 3.5-dev - NRM */
+#define ANGBAND_TERM_MAX 8
+
+#define MAX_COLORS 256
+#define MSG_MAX SOUND_MAX
+
+/* End Angband stuff - NRM */
+
+/* Application defined event numbers */
+enum
+{
+    AngbandEventWakeup = 1
+};
+
+/* Delay handling of pre-emptive "quit" event */
+static BOOL quit_when_ready = NO;
+
+/* Set to indicate the game is over and we can quit without delay */
+static BOOL game_is_finished = NO;
+
+/* Our frames per second (e.g. 60). A value of 0 means unthrottled. */
+static int frames_per_second;
+
+/* Force a new game or not? */
+static bool new_game = false;
+
+@class AngbandView;
+
+#ifdef JP
+static wchar_t convert_two_byte_eucjp_to_utf32_native(const char *cp);
+#endif
+
+/**
+ * Each location in the terminal either stores a character, a tile,
+ * padding for a big tile, or padding for a big character (for example a
+ * kanji that takes two columns).  These structures represent that.  Note
+ * that tiles do not overlap with each other (excepting the double-height
+ * tiles, i.e. from the Shockbolt set; that's handled as a special case).
+ * Characters can overlap horizontally:  that is for handling fonts that
+ * aren't fixed width.
+ */
+struct TerminalCellChar {
+    wchar_t glyph;
+    int attr;
+};
+struct TerminalCellTile {
+    /*
+     * These are the coordinates, within the tile set, for the foreground
+     * tile and background tile.
+     */
+    char fgdCol, fgdRow, bckCol, bckRow;
+};
+struct TerminalCellPadding {
+       /*
+       * If the cell at (x, y) is padding, the cell at (x - hoff, y - voff)
+       * has the attributes affecting the padded region.
+       */
+    unsigned char hoff, voff;
+};
+struct TerminalCell {
+    union {
+       struct TerminalCellChar ch;
+       struct TerminalCellTile ti;
+       struct TerminalCellPadding pd;
+    } v;
+    /*
+     * Used for big characters or tiles which are hscl x vscl cells.
+     * The upper left corner of the big tile or character is marked as
+     * TERM_CELL_TILE or TERM_CELL_CHAR.  The remainder are marked as
+     * TERM_CELL_TILE_PADDING or TERM_CELL_CHAR_PADDING and have hscl and
+     * vscl set to matcn what's in the upper left corner.  Big tiles are
+     * tiles scaled up to occupy more space.  Big characters, on the other
+     * hand, are characters that naturally take up more space than standard
+     * for the font with the assumption that vscl will be one for any big
+     * character and hscl will hold the number of columns it occupies (likely
+     * just 2, i.e. for Japanese kanji).
+     */
+    unsigned char hscl;
+    unsigned char vscl;
+    /*
+     * Hold the offsets, as fractions of the tile size expressed as the
+     * rational numbers hoff_n / hoff_d and voff_n / voff_d, within the tile
+     * or character.  For something that is not a big tile or character, these
+     * will be 0, 0, 1, and 1.  For a big tile or character, these will be
+     * set when the tile or character is changed to be 0, 0, hscl, and vscl
+     * for the upper left corner and i, j, hscl, vscl for the padding element
+     * at (i, j) relative to the upper left corner.  For a big tile or
+     * character that is partially overwritten, these are not modified in the
+     * parts that are not overwritten while hscl, vscl, and, for padding,
+     * v.pd.hoff and v.pd.voff are.
+     */
+    unsigned char hoff_n;
+    unsigned char voff_n;
+    unsigned char hoff_d;
+    unsigned char voff_d;
+    /*
+     * Is either TERM_CELL_CHAR, TERM_CELL_CHAR_PADDING, TERM_CELL_TILE, or
+     * TERM_CELL_TILE_PADDING.
+     */
+    unsigned char form;
+};
+#define TERM_CELL_CHAR (0x1)
+#define TERM_CELL_CHAR_PADDING (0x2)
+#define TERM_CELL_TILE (0x4)
+#define TERM_CELL_TILE_PADDING (0x8)
+
+struct TerminalCellBlock {
+    int ulcol, ulrow, w, h;
+};
+
+struct TerminalCellLocation {
+    int col, row;
+};
+
+typedef int (*TerminalCellPredicate)(const struct TerminalCell*);
+
+static int isTileTop(const struct TerminalCell *c)
+{
+    return (c->form == TERM_CELL_TILE ||
+           (c->form == TERM_CELL_TILE_PADDING && c->v.pd.voff == 0)) ? 1 : 0;
+}
+
+static int isPartiallyOverwrittenBigChar(const struct TerminalCell *c)
+{
+    if ((c->form & (TERM_CELL_CHAR | TERM_CELL_CHAR_PADDING)) != 0) {
+       /*
+        * When the tile is set in Term_pict_cocoa, hoff_d is the same as hscl
+        * and voff_d is the same as vscl.  hoff_d and voff_d aren't modified
+        * after that, but hscl and vscl are in response to partial overwrites.
+        * If they're diffent, an overwrite has occurred.
+        */
+       return ((c->hoff_d > 1 || c->voff_d > 1) &&
+               (c->hoff_d != c->hscl || c->voff_d != c->vscl)) ? 1 : 0;
+    }
+    return 0;
+}
+
+static int isCharNoPartial(const struct TerminalCell *c)
+{
+    return ((c->form & (TERM_CELL_CHAR | TERM_CELL_CHAR_PADDING)) != 0 &&
+           ! isPartiallyOverwrittenBigChar(c)) ? 1 : 0;
+}
+
+/**
+ * Since the drawing is decoupled from Angband's calls to the text_hook,
+ * pict_hook, wipe_hook, curs_hook, and bigcurs_hook callbacks of a terminal,
+ * maintain a version of the Terminal contents.
+ */
+@interface TerminalContents : NSObject {
+@private
+    struct TerminalCell *cells;
+}
+
+/**
+ * Initialize with zero columns and zero rows.
+ */
+- (id)init;
+
+/**
+ * Initialize with nCol columns and nRow rows.  All elements will be set to
+ * blanks.
+ */
+- (id)initWithColumns:(int)nCol rows:(int)nRow NS_DESIGNATED_INITIALIZER;
+
+/**
+ * Resize to be nCol by nRow.  Current contents still within the new bounds
+ * are preserved.  Added areas are filled with blanks.
+ */
+- (void)resizeWithColumns:(int)nCol rows:(int)nRow;
+
+/**
+ * Get the contents of a given cell.
+ */
+- (const struct TerminalCell*)getCellAtColumn:(int)icol row:(int)irow;
+
+/**
+ * Scans the row, irow, starting at the column, icol0, and stopping before the
+ * column, icol1.  Returns the column index for the first cell that's within
+ * the given type mask, tm.  If all of the cells in that range are not within
+ * the given type mask, returns icol1.
+ */
+- (int)scanForTypeMaskInRow:(int)irow mask:(unsigned int)tm col0:(int)icol0
+                      col1:(int)icol1;
+
+/**
+ * Scans the w x h block whose upper left corner is at (icol, irow).  The
+ * scan starts at (icol + pcurs->col, irow + pcurs->row) and proceeds from
+ * left to right and top to bottom.  At exit, pcurs will have the location
+ * (relative to icol, irow) of the first cell encountered that's within the
+ * given type mask, tm.  If no such cell was found, pcurs->col will be w
+ * and pcurs->row will be h.
+ */
+- (void)scanForTypeMaskInBlockAtColumn:(int)icol row:(int)irow width:(int)w
+                               height:(int)h mask:(unsigned int)tm
+                               cursor:(struct TerminalCellLocation*)pcurs;
+
+/**
+ * Scans the row, irow, starting at the column, icol0, and stopping before the
+ * column, icol1.  Returns the column index for the first cell that
+ * func(cell_address) != rval.  If all of the cells in the range satisfy the
+ * predicate, returns icol1.
+ */
+- (int)scanForPredicateInRow:(int)irow
+                  predicate:(TerminalCellPredicate)func
+                    desired:(int)rval
+                       col0:(int)icol0
+                       col1:(int)icol1;
+
+/**
+ * Change the contents to have the given string of n characters appear with
+ * the leftmost character at (icol, irow).
+ */
+- (void)setUniformAttributeTextRunAtColumn:(int)icol
+                                      row:(int)irow
+                                        n:(int)n
+                                   glyphs:(const char*)g
+                                attribute:(int)a;
+
+/**
+ * Change the contents to have a tile scaled to w x h appear with its upper
+ * left corner at (icol, irow).
+ */
+- (void)setTileAtColumn:(int)icol
+                   row:(int)irow
+       foregroundColumn:(char)fgdCol
+         foregroundRow:(char)fgdRow
+       backgroundColumn:(char)bckCol
+         backgroundRow:(char)bckRow
+             tileWidth:(int)w
+            tileHeight:(int)h;
+
+/**
+ * Wipe the w x h block whose upper left corner is at (icol, irow).
+ */
+- (void)wipeBlockAtColumn:(int)icol row:(int)irow width:(int)w height:(int)h;
+
+/**
+ * Wipe all the contents.
+ */
+- (void)wipe;
+
+/**
+ * Wipe any tiles.
+ */
+- (void)wipeTiles;
+
+/**
+ * Thie is a helper function for wipeBlockAtColumn.
+ */
+- (void)wipeBlockAuxAtColumn:(int)icol row:(int)irow width:(int)w
+                     height:(int)h;
+
+/**
+ * This is a helper function for checkForBigStuffOverwriteAtColumn.
+ */
+- (void) splitBlockAtColumn:(int)icol row:(int)irow n:(int)nsub
+                    blocks:(const struct TerminalCellBlock*)b;
+
+/**
+ * This is a helper function for setUniformAttributeTextRunAtColumn,
+ * setTileAtColumn, and wipeBlockAtColumn.  If a modification could partially
+ * overwrite a big character or tile, make adjustments so what's left can
+ * be handled appropriately in rendering.
+ */
+- (void)checkForBigStuffOverwriteAtColumn:(int)icol row:(int)irow
+                                   width:(int)w height:(int)h;
+
+/**
+ * Position the upper left corner of the cursor at (icol, irow) and have it
+ * encompass w x h cells.
+ */
+- (void)setCursorAtColumn:(int)icol row:(int)irow width:(int)w height:(int)h;
+
+/**
+ * Remove the cursor.  cursorColumn and cursorRow will be -1 until
+ * setCursorAtColumn is called.
+ */
+- (void)removeCursor;
+
+/**
+ * Verify that everying is consistent.
+ */
+- (void)assertInvariants;
+
+/**
+ * Is the number of columns.
+ */
+@property (readonly) int columnCount;
+
+/**
+ * Is the number of rows.
+ */
+@property (readonly) int rowCount;
+
+/**
+ * Is the column index for the upper left corner of the cursor.  It will be -1
+ * if the cursor is disabled.
+ */
+@property (readonly) int cursorColumn;
+
+/**
+ * Is the row index for the upper left corner of the cursor.  It will be -1
+ * if the cursor is disabled.
+ */
+@property (readonly) int cursorRow;
+
+/**
+ * Is the cursor width in number of cells.
+ */
+@property (readonly) int cursorWidth;
+
+/**
+ * Is the cursor height in number of cells.
+ */
+@property (readonly) int cursorHeight;
+
+/**
+ * Return the character to be used for blanks.
+ */
++ (wchar_t)getBlankChar;
+
+/**
+ * Return the attribute to be used for blanks.
+ */
++ (int)getBlankAttribute;
+
+@end
+
+@implementation TerminalContents
+
+- (id)init
+{
+    return [self initWithColumns:0 rows:0];
+}
+
+- (id)initWithColumns:(int)nCol rows:(int)nRow
+{
+    if (self = [super init]) {
+       self->cells = (TerminalCell*)
+           malloc(nCol * nRow * sizeof(struct TerminalCell));
+       self->_columnCount = nCol;
+       self->_rowCount = nRow;
+       self->_cursorColumn = -1;
+       self->_cursorRow = -1;
+       self->_cursorWidth = 1;
+       self->_cursorHeight = 1;
+       [self wipe];
+    }
+    return self;
+}
+
+- (void)dealloc
+{
+    if (self->cells != 0) {
+       free(self->cells);
+       self->cells = 0;
+    }
+}
+
+- (void)resizeWithColumns:(int)nCol rows:(int)nRow
+{
+    /*
+     * Potential issue: big tiles or characters can become clipped by the
+     * resize.  That will only matter if drawing occurs before the contents
+     * are updated by Angband.  Even then, unless the drawing mode is used
+     * where AppKit doesn't clip to the window bounds, the only artifact will
+     * be clipping when drawn which is acceptable and doesn't require
+     * additional logic to either filter out the clipped big stuff here or
+     * to just clear it when drawing.
+     */
+    struct TerminalCell *newCells =
+       (TerminalCell*) malloc(nCol * nRow * sizeof(struct TerminalCell));
+    struct TerminalCell *cellsOutCursor = newCells;
+    const struct TerminalCell *cellsInCursor = self->cells;
+    int nColCommon = (nCol < self.columnCount) ? nCol : self.columnCount;
+    int nRowCommon = (nRow < self.rowCount) ? nRow : self.rowCount;
+    wchar_t blank = [TerminalContents getBlankChar];
+    int blank_attr = [TerminalContents getBlankAttribute];
+    int i;
+
+    for (i = 0; i < nRowCommon; ++i) {
+       (void) memcpy(
+           cellsOutCursor,
+           cellsInCursor,
+           nColCommon * sizeof(struct TerminalCell));
+       cellsInCursor += self.columnCount;
+       for (int j = nColCommon; j < nCol; ++j) {
+           cellsOutCursor[j].v.ch.glyph = blank;
+           cellsOutCursor[j].v.ch.attr = blank_attr;
+           cellsOutCursor[j].hscl = 1;
+           cellsOutCursor[j].vscl = 1;
+           cellsOutCursor[j].hoff_n = 0;
+           cellsOutCursor[j].voff_n = 0;
+           cellsOutCursor[j].hoff_d = 1;
+           cellsOutCursor[j].voff_d = 1;
+           cellsOutCursor[j].form = TERM_CELL_CHAR;
+       }
+       cellsOutCursor += nCol;
+    }
+    while (cellsOutCursor != newCells + nCol * nRow) {
+       cellsOutCursor->v.ch.glyph = blank;
+       cellsOutCursor->v.ch.attr = blank_attr;
+       cellsOutCursor->hscl = 1;
+       cellsOutCursor->vscl = 1;
+       cellsOutCursor->hoff_n = 0;
+       cellsOutCursor->voff_n = 0;
+       cellsOutCursor->hoff_d = 1;
+       cellsOutCursor->voff_d = 1;
+       cellsOutCursor->form = TERM_CELL_CHAR;
+       ++cellsOutCursor;
+    }
+
+    free(self->cells);
+    self->cells = newCells;
+    self->_columnCount = nCol;
+    self->_rowCount = nRow;
+    if (self->_cursorColumn >= nCol || self->_cursorRow >= nRow) {
+       self->_cursorColumn = -1;
+       self->_cursorRow = -1;
+    } else {
+       if (self->_cursorColumn + self->_cursorWidth > nCol) {
+           self->_cursorWidth = nCol - self->_cursorColumn;
+       }
+       if (self->_cursorRow + self->_cursorHeight > nRow) {
+           self->_cursorHeight = nRow - self->_cursorRow;
+       }
+    }
+}
+
+- (const struct TerminalCell*)getCellAtColumn:(int)icol row:(int)irow
+{
+    return self->cells + icol + irow * self.columnCount;
+}
+
+- (int)scanForTypeMaskInRow:(int)irow mask:(unsigned int)tm col0:(int)icol0
+                      col1:(int)icol1
+{
+    int i = icol0;
+    const struct TerminalCell *cellsRow =
+       self->cells + irow * self.columnCount;
+
+    while (1) {
+       if (i >= icol1) {
+           return icol1;
+       }
+       if ((cellsRow[i].form & tm) != 0) {
+           return i;
+       }
+       ++i;
+    }
+}
+
+- (void)scanForTypeMaskInBlockAtColumn:(int)icol row:(int)irow width:(int)w
+                               height:(int)h mask:(unsigned int)tm
+                               cursor:(struct TerminalCellLocation*)pcurs
+{
+    const struct TerminalCell *cellsRow =
+       self->cells + (irow + pcurs->row) * self.columnCount;
+    while (1) {
+       if (pcurs->col == w) {
+           if (pcurs->row >= h - 1) {
+               pcurs->row = h;
+               return;
+           }
+           ++pcurs->row;
+           pcurs->col = 0;
+           cellsRow += self.columnCount;
+       }
+
+       if ((cellsRow[icol + pcurs->col].form & tm) != 0) {
+           return;
+       }
+
+       ++pcurs->col;
+    }
+}
+
+- (int)scanForPredicateInRow:(int)irow
+                  predicate:(TerminalCellPredicate)func
+                    desired:(int)rval
+                       col0:(int)icol0
+                       col1:(int)icol1
+{
+    int i = icol0;
+    const struct TerminalCell *cellsRow =
+       self->cells + irow * self.columnCount;
+
+    while (1) {
+       if (i >= icol1) {
+           return icol1;
+       }
+       if (func(cellsRow + i) != rval) {
+           return i;
+       }
+       ++i;
+    }
+}
+
+- (void)setUniformAttributeTextRunAtColumn:(int)icol
+                                      row:(int)irow
+                                        n:(int)n
+                                   glyphs:(const char*)g
+                                attribute:(int)a
+{
+    [self checkForBigStuffOverwriteAtColumn:icol row:irow width:n height:1];
+
+    struct TerminalCell *cellsRow = self->cells + irow * self.columnCount;
+    int i = icol;
+
+    while (i < icol + n) {
+#ifdef JP
+       if (iskanji(*g)) {
+           if (i == icol + n - 1) {
+               /*
+                * The second byte of the character is past the end.  Ignore
+                * the character.
+                */
+               break;
+           }
+           cellsRow[i].v.ch.glyph = convert_two_byte_eucjp_to_utf32_native(g);
+           cellsRow[i].v.ch.attr = a;
+           cellsRow[i].hscl = 2;
+           cellsRow[i].vscl = 1;
+           cellsRow[i].hoff_n = 0;
+           cellsRow[i].voff_n = 0;
+           cellsRow[i].hoff_d = 2;
+           cellsRow[i].voff_d = 1;
+           cellsRow[i].form = TERM_CELL_CHAR;
+           ++i;
+           cellsRow[i].v.pd.hoff = 1;
+           cellsRow[i].v.pd.voff = 0;
+           cellsRow[i].hscl = 2;
+           cellsRow[i].vscl = 1;
+           cellsRow[i].hoff_n = 1;
+           cellsRow[i].voff_n = 0;
+           cellsRow[i].hoff_d = 2;
+           cellsRow[i].voff_d = 1;
+           cellsRow[i].form = TERM_CELL_CHAR_PADDING;
+           ++i;
+           g += 2;
+       } else {
+           cellsRow[i].v.ch.glyph = *g++;
+           cellsRow[i].v.ch.attr = a;
+           cellsRow[i].hscl = 1;
+           cellsRow[i].vscl = 1;
+           cellsRow[i].hoff_n = 0;
+           cellsRow[i].voff_n = 0;
+           cellsRow[i].hoff_d = 1;
+           cellsRow[i].voff_d = 1;
+           cellsRow[i].form = TERM_CELL_CHAR;
+           ++i;
+       }
+#else
+       cellsRow[i].v.ch.glyph = *g++;
+       cellsRow[i].v.ch.attr = a;
+       cellsRow[i].hscl = 1;
+       cellsRow[i].vscl = 1;
+       cellsRow[i].hoff_n = 0;
+       cellsRow[i].voff_n = 0;
+       cellsRow[i].hoff_d = 1;
+       cellsRow[i].voff_d = 1;
+       cellsRow[i].form = TERM_CELL_CHAR;
+       ++i;
+#endif /* JP */
+    }
+}
+
+- (void)setTileAtColumn:(int)icol
+                   row:(int)irow
+       foregroundColumn:(char)fgdCol
+         foregroundRow:(char)fgdRow
+       backgroundColumn:(char)bckCol
+         backgroundRow:(char)bckRow
+             tileWidth:(int)w
+            tileHeight:(int)h
+{
+    [self checkForBigStuffOverwriteAtColumn:icol row:irow width:w height:h];
+
+    struct TerminalCell *cellsRow = self->cells + irow * self.columnCount;
+
+    cellsRow[icol].v.ti.fgdCol = fgdCol;
+    cellsRow[icol].v.ti.fgdRow = fgdRow;
+    cellsRow[icol].v.ti.bckCol = bckCol;
+    cellsRow[icol].v.ti.bckRow = bckRow;
+    cellsRow[icol].hscl = w;
+    cellsRow[icol].vscl = h;
+    cellsRow[icol].hoff_n = 0;
+    cellsRow[icol].voff_n = 0;
+    cellsRow[icol].hoff_d = w;
+    cellsRow[icol].voff_d = h;
+    cellsRow[icol].form = TERM_CELL_TILE;
+
+    int ic;
+    for (ic = icol + 1; ic < icol + w; ++ic) {
+       cellsRow[ic].v.pd.hoff = ic - icol;
+       cellsRow[ic].v.pd.voff = 0;
+       cellsRow[ic].hscl = w;
+       cellsRow[ic].vscl = h;
+       cellsRow[ic].hoff_n = ic - icol;
+       cellsRow[ic].voff_n = 0;
+       cellsRow[ic].hoff_d = w;
+       cellsRow[ic].voff_d = h;
+       cellsRow[ic].form = TERM_CELL_TILE_PADDING;
+    }
+    cellsRow += self.columnCount;
+    for (int ir = irow + 1; ir < irow + h; ++ir) {
+       for (ic = icol; ic < icol + w; ++ic) {
+           cellsRow[ic].v.pd.hoff = ic - icol;
+           cellsRow[ic].v.pd.voff = ir - irow;
+           cellsRow[ic].hscl = w;
+           cellsRow[ic].vscl = h;
+           cellsRow[ic].hoff_n = ic - icol;
+           cellsRow[ic].voff_n = ir - irow;
+           cellsRow[ic].hoff_d = w;
+           cellsRow[ic].voff_d = h;
+           cellsRow[ic].form = TERM_CELL_TILE_PADDING;
+       }
+       cellsRow += self.columnCount;
+    }
+}
+
+- (void)wipeBlockAtColumn:(int)icol row:(int)irow width:(int)w height:(int)h
+{
+    [self checkForBigStuffOverwriteAtColumn:icol row:irow width:w height:h];
+    [self wipeBlockAuxAtColumn:icol row:irow width:w height:h];
+}
+
+- (void)wipe
+{
+    wchar_t blank = [TerminalContents getBlankChar];
+    int blank_attr = [TerminalContents getBlankAttribute];
+    struct TerminalCell *cellCursor = self->cells +
+       self.columnCount * self.rowCount;
+
+    while (cellCursor != self->cells) {
+       --cellCursor;
+       cellCursor->v.ch.glyph = blank;
+       cellCursor->v.ch.attr = blank_attr;
+       cellCursor->hscl = 1;
+       cellCursor->vscl = 1;
+       cellCursor->hoff_n = 0;
+       cellCursor->voff_n = 0;
+       cellCursor->hoff_d = 1;
+       cellCursor->voff_d = 1;
+       cellCursor->form = TERM_CELL_CHAR;
+    }
+}
+
+- (void)wipeTiles
+{
+    wchar_t blank = [TerminalContents getBlankChar];
+    int blank_attr = [TerminalContents getBlankAttribute];
+    struct TerminalCell *cellCursor = self->cells +
+       self.columnCount * self.rowCount;
+
+    while (cellCursor != self->cells) {
+       --cellCursor;
+       if ((cellCursor->form &
+            (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
+           cellCursor->v.ch.glyph = blank;
+           cellCursor->v.ch.attr = blank_attr;
+           cellCursor->hscl = 1;
+           cellCursor->vscl = 1;
+           cellCursor->hoff_n = 0;
+           cellCursor->voff_n = 0;
+           cellCursor->hoff_d = 1;
+           cellCursor->voff_d = 1;
+           cellCursor->form = TERM_CELL_CHAR;
+       }
+    }
+}
+
+- (void)wipeBlockAuxAtColumn:(int)icol row:(int)irow width:(int)w
+                     height:(int)h
+{
+    struct TerminalCell *cellsRow = self->cells + irow * self.columnCount;
+    wchar_t blank = [TerminalContents getBlankChar];
+    int blank_attr = [TerminalContents getBlankAttribute];
+
+    for (int ir = irow; ir < irow + h; ++ir) {
+       for (int ic = icol; ic < icol + w; ++ic) {
+           cellsRow[ic].v.ch.glyph = blank;
+           cellsRow[ic].v.ch.attr = blank_attr;
+           cellsRow[ic].hscl = 1;
+           cellsRow[ic].vscl = 1;
+           cellsRow[ic].hoff_n = 0;
+           cellsRow[ic].voff_n = 0;
+           cellsRow[ic].hoff_d = 1;
+           cellsRow[ic].voff_d = 1;
+           cellsRow[ic].form = TERM_CELL_CHAR;
+       }
+       cellsRow += self.columnCount;
+    }
+}
+
+- (void) splitBlockAtColumn:(int)icol row:(int)irow n:(int)nsub
+                    blocks:(const struct TerminalCellBlock*)b
+{
+    const struct TerminalCell *pulold = [self getCellAtColumn:icol row:irow];
+
+    for (int isub = 0; isub < nsub; ++isub) {
+       struct TerminalCell* cellsRow =
+           self->cells + b[isub].ulrow * self.columnCount;
+
+       /*
+        * Copy the data from the upper left corner of the big block to
+        * the upper left corner of the piece.
+        */
+       if (b[isub].ulcol != icol || b[isub].ulrow != irow) {
+           if (pulold->form == TERM_CELL_CHAR) {
+               cellsRow[b[isub].ulcol].v.ch = pulold->v.ch;
+               cellsRow[b[isub].ulcol].form = TERM_CELL_CHAR;
+           } else {
+               cellsRow[b[isub].ulcol].v.ti = pulold->v.ti;
+               cellsRow[b[isub].ulcol].form = TERM_CELL_TILE;
+           }
+       }
+       cellsRow[b[isub].ulcol].hscl = b[isub].w;
+       cellsRow[b[isub].ulcol].vscl = b[isub].h;
+
+       /*
+        * Point the padding elements in the piece to the new upper left
+        * corner.
+        */
+       int ic;
+       for (ic = b[isub].ulcol + 1; ic < b[isub].ulcol + b[isub].w; ++ic) {
+           cellsRow[ic].v.pd.hoff = ic - b[isub].ulcol;
+           cellsRow[ic].v.pd.voff = 0;
+           cellsRow[ic].hscl = b[isub].w;
+           cellsRow[ic].vscl = b[isub].h;
+       }
+       cellsRow += self.columnCount;
+       for (int ir = b[isub].ulrow + 1;
+            ir < b[isub].ulrow + b[isub].h;
+            ++ir) {
+           for (ic = b[isub].ulcol; ic < b[isub].ulcol + b[isub].w; ++ic) {
+               cellsRow[ic].v.pd.hoff = ic - b[isub].ulcol;
+               cellsRow[ic].v.pd.voff = ir - b[isub].ulrow;
+               cellsRow[ic].hscl = b[isub].w;
+               cellsRow[ic].vscl = b[isub].h;
+           }
+           cellsRow += self.columnCount;
+       }
+    }
+}
+
+- (void)checkForBigStuffOverwriteAtColumn:(int)icol row:(int)irow
+                                   width:(int)w height:(int)h
+{
+    int ire = irow + h, ice = icol + w;
+
+    for (int ir = irow; ir < ire; ++ir) {
+       for (int ic = icol; ic < ice; ++ic) {
+           const struct TerminalCell *pcell =
+               [self getCellAtColumn:ic row:ir];
+
+           if ((pcell->form & (TERM_CELL_CHAR | TERM_CELL_TILE)) != 0 &&
+               (pcell->hscl > 1 || pcell->vscl > 1)) {
+               /*
+                * Lost chunk including upper left corner.  Split into at most
+                * two new blocks.
+                */
+               /*
+                * Tolerate blocks that were clipped by a resize at some point.
+                */
+               int wb = (ic + pcell->hscl <= self.columnCount) ?
+                   pcell->hscl : self.columnCount - ic;
+               int hb = (ir + pcell->vscl <= self.rowCount) ?
+                   pcell->vscl : self.rowCount - ir;
+               struct TerminalCellBlock blocks[2];
+               int nsub = 0, ww, hw;
+
+               if (ice < ic + wb) {
+                   /* Have something to the right not overwritten. */
+                   blocks[nsub].ulcol = ice;
+                   blocks[nsub].ulrow = ir;
+                   blocks[nsub].w = ic + wb - ice;
+                   blocks[nsub].h = (ire < ir + hb) ? ire - ir : hb;
+                   ++nsub;
+                   ww = ice - ic;
+               } else {
+                   ww = wb;
+               }
+               if (ire < ir + hb) {
+                   /* Have something below not overwritten. */
+                   blocks[nsub].ulcol = ic;
+                   blocks[nsub].ulrow = ire;
+                   blocks[nsub].w = wb;
+                   blocks[nsub].h = ir + hb - ire;
+                   ++nsub;
+                   hw = ire - ir;
+               } else {
+                   hw = hb;
+               }
+               if (nsub > 0) {
+                   [self splitBlockAtColumn:ic row:ir n:nsub blocks:blocks];
+               }
+               /*
+                * Wipe the part of the block that's destined to be overwritten
+                * so it doesn't receive further consideration in this loop.
+                * For efficiency, would like to have the loop skip over it or
+                * fill it with the desired content, but this is easier to
+                * implement.
+                */
+               [self wipeBlockAuxAtColumn:ic row:ir width:ww height:hw];
+           } else if ((pcell->form & (TERM_CELL_CHAR_PADDING |
+                                      TERM_CELL_TILE_PADDING)) != 0) {
+               /*
+                * Lost a chunk that doesn't cover the upper left corner.  In
+                * general will split into up to four new blocks (one above,
+                * one to the left, one to the right, and one below).
+                */
+               int pcol = ic - pcell->v.pd.hoff;
+               int prow = ir - pcell->v.pd.voff;
+               const struct TerminalCell *pcell2 =
+                   [self getCellAtColumn:pcol row:prow];
+
+               /*
+                * Tolerate blocks that were clipped by a resize at some point.
+                */
+               int wb = (pcol + pcell2->hscl <= self.columnCount) ?
+                   pcell2->hscl : self.columnCount - pcol;
+               int hb = (prow + pcell2->vscl <= self.rowCount) ?
+                   pcell2->vscl : self.rowCount - prow;
+               struct TerminalCellBlock blocks[4];
+               int nsub = 0, ww, hw;
+
+               if (prow < ir) {
+                   /* Have something above not overwritten. */
+                   blocks[nsub].ulcol = pcol;
+                   blocks[nsub].ulrow = prow;
+                   blocks[nsub].w = wb;
+                   blocks[nsub].h = ir - prow;
+                   ++nsub;
+               }
+               if (pcol < ic) {
+                   /* Have something to the left not overwritten. */
+                   blocks[nsub].ulcol = pcol;
+                   blocks[nsub].ulrow = ir;
+                   blocks[nsub].w = ic - pcol;
+                   blocks[nsub].h =
+                       (ire < prow + hb) ? ire - ir : prow + hb - ir;
+                   ++nsub;
+               }
+               if (ice < pcol + wb) {
+                   /* Have something to the right not overwritten. */
+                   blocks[nsub].ulcol = ice;
+                   blocks[nsub].ulrow = ir;
+                   blocks[nsub].w = pcol + wb - ice;
+                   blocks[nsub].h =
+                       (ire < prow + hb) ? ire - ir : prow + hb - ir;
+                   ++nsub;
+                   ww = ice - ic;
+               } else {
+                   ww = pcol + wb - ic;
+               }
+               if (ire < prow + hb) {
+                   /* Have something below not overwritten. */
+                   blocks[nsub].ulcol = pcol;
+                   blocks[nsub].ulrow = ire;
+                   blocks[nsub].w = wb;
+                   blocks[nsub].h = prow + hb - ire;
+                   ++nsub;
+                   hw = ire - ir;
+               } else {
+                   hw = prow + hb - ir;
+               }
+
+               [self splitBlockAtColumn:pcol row:prow n:nsub blocks:blocks];
+               /* Same rationale for wiping as above. */
+               [self wipeBlockAuxAtColumn:ic row:ir width:ww height:hw];
+           }
+       }
+    }
+}
+
+- (void)setCursorAtColumn:(int)icol row:(int)irow width:(int)w height:(int)h
+{
+    self->_cursorColumn = icol;
+    self->_cursorRow = irow;
+    self->_cursorWidth = w;
+    self->_cursorHeight = h;
+}
+
+- (void)removeCursor
+{
+    self->_cursorColumn = -1;
+    self->_cursorHeight = -1;
+    self->_cursorWidth = 1;
+    self->_cursorHeight = 1;
+}
+
+- (void)assertInvariants
+{
+#ifndef NDEBUG
+    const struct TerminalCell *cellsRow = self->cells;
+
+    /*
+     * The comments with the definition for TerminalCell define the
+     * relationships of hoff_n, voff_n, hoff_d, voff_d, hscl, and vscl
+     * asserted here.
+     */
+    for (int ir = 0; ir < self.rowCount; ++ir) {
+       for (int ic = 0; ic < self.columnCount; ++ic) {
+           switch (cellsRow[ic].form) {
+           case TERM_CELL_CHAR:
+               assert(cellsRow[ic].hscl > 0 && cellsRow[ic].vscl > 0);
+               assert(cellsRow[ic].hoff_n < cellsRow[ic].hoff_d &&
+                      cellsRow[ic].voff_n < cellsRow[ic].voff_d);
+               if (cellsRow[ic].hscl == cellsRow[ic].hoff_d) {
+                   assert(cellsRow[ic].hoff_n == 0);
+               }
+               if (cellsRow[ic].vscl == cellsRow[ic].voff_d) {
+                   assert(cellsRow[ic].voff_n == 0);
+               }
+               /*
+                * Verify that the padding elements have the correct tag
+                * and point back to this cell.
+                */
+               if (cellsRow[ic].hscl > 1 || cellsRow[ic].vscl > 1) {
+                   const struct TerminalCell *cellsRow2 = cellsRow;
+
+                   for (int ir2 = ir; ir2 < ir + cellsRow[ic].vscl; ++ir2) {
+                       for (int ic2 = ic;
+                            ic2 < ic + cellsRow[ic].hscl;
+                            ++ic2) {
+                           if (ir2 == ir && ic2 == ic) {
+                               continue;
+                           }
+                           assert(cellsRow2[ic2].form ==
+                                  TERM_CELL_CHAR_PADDING);
+                           assert(ic2 - cellsRow2[ic2].v.pd.hoff == ic &&
+                                  ir2 - cellsRow2[ic2].v.pd.voff == ir);
+                       }
+                       cellsRow2 += self.columnCount;
+                   }
+               }
+               break;
+
+           case TERM_CELL_TILE:
+               assert(cellsRow[ic].hscl > 0 && cellsRow[ic].vscl > 0);
+               assert(cellsRow[ic].hoff_n < cellsRow[ic].hoff_d &&
+                      cellsRow[ic].voff_n < cellsRow[ic].voff_d);
+               if (cellsRow[ic].hscl == cellsRow[ic].hoff_d) {
+                   assert(cellsRow[ic].hoff_n == 0);
+               }
+               if (cellsRow[ic].vscl == cellsRow[ic].voff_d) {
+                   assert(cellsRow[ic].voff_n == 0);
+               }
+               /*
+                * Verify that the padding elements have the correct tag
+                * and point back to this cell.
+                */
+               if (cellsRow[ic].hscl > 1 || cellsRow[ic].vscl > 1) {
+                   const struct TerminalCell *cellsRow2 = cellsRow;
+
+                   for (int ir2 = ir; ir2 < ir + cellsRow[ic].vscl; ++ir2) {
+                       for (int ic2 = ic;
+                            ic2 < ic + cellsRow[ic].hscl;
+                            ++ic2) {
+                           if (ir2 == ir && ic2 == ic) {
+                               continue;
+                           }
+                           assert(cellsRow2[ic2].form ==
+                                  TERM_CELL_TILE_PADDING);
+                           assert(ic2 - cellsRow2[ic2].v.pd.hoff == ic &&
+                                  ir2 - cellsRow2[ic2].v.pd.voff == ir);
+                       }
+                       cellsRow2 += self.columnCount;
+                   }
+               }
+               break;
+
+           case TERM_CELL_CHAR_PADDING:
+               assert(cellsRow[ic].hscl > 0 && cellsRow[ic].vscl > 0);
+               assert(cellsRow[ic].hoff_n < cellsRow[ic].hoff_d &&
+                      cellsRow[ic].voff_n < cellsRow[ic].voff_d);
+               assert(cellsRow[ic].hoff_n > 0 || cellsRow[ic].voff_n > 0);
+               if (cellsRow[ic].hscl == cellsRow[ic].hoff_d) {
+                   assert(cellsRow[ic].hoff_n == cellsRow[ic].v.pd.hoff);
+               }
+               if (cellsRow[ic].vscl == cellsRow[ic].voff_d) {
+                   assert(cellsRow[ic].voff_n == cellsRow[ic].v.pd.voff);
+               }
+               assert(ic >= cellsRow[ic].v.pd.hoff &&
+                      ir >= cellsRow[ic].v.pd.voff);
+               /*
+                * Verify that it's padding for something that can point
+                * back to it.
+                */
+               {
+                   const struct TerminalCell *parent =
+                       [self getCellAtColumn:(ic - cellsRow[ic].v.pd.hoff)
+                             row:(ir - cellsRow[ic].v.pd.voff)];
+
+                   assert(parent->form == TERM_CELL_CHAR);
+                   assert(parent->hscl > cellsRow[ic].v.pd.hoff &&
+                          parent->vscl > cellsRow[ic].v.pd.voff);
+                   assert(parent->hscl == cellsRow[ic].hscl &&
+                          parent->vscl == cellsRow[ic].vscl);
+                   assert(parent->hoff_d == cellsRow[ic].hoff_d &&
+                          parent->voff_d == cellsRow[ic].voff_d);
+               }
+               break;
+
+           case TERM_CELL_TILE_PADDING:
+               assert(cellsRow[ic].hscl > 0 && cellsRow[ic].vscl > 0);
+               assert(cellsRow[ic].hoff_n < cellsRow[ic].hoff_d &&
+                      cellsRow[ic].voff_n < cellsRow[ic].voff_d);
+               assert(cellsRow[ic].hoff_n > 0 || cellsRow[ic].voff_n > 0);
+               if (cellsRow[ic].hscl == cellsRow[ic].hoff_d) {
+                   assert(cellsRow[ic].hoff_n == cellsRow[ic].v.pd.hoff);
+               }
+               if (cellsRow[ic].vscl == cellsRow[ic].voff_d) {
+                   assert(cellsRow[ic].voff_n == cellsRow[ic].v.pd.voff);
+               }
+               assert(ic >= cellsRow[ic].v.pd.hoff &&
+                      ir >= cellsRow[ic].v.pd.voff);
+               /*
+                * Verify that it's padding for something that can point
+                * back to it.
+                */
+               {
+                   const struct TerminalCell *parent =
+                       [self getCellAtColumn:(ic - cellsRow[ic].v.pd.hoff)
+                             row:(ir - cellsRow[ic].v.pd.voff)];
+
+                   assert(parent->form == TERM_CELL_TILE);
+                   assert(parent->hscl > cellsRow[ic].v.pd.hoff &&
+                          parent->vscl > cellsRow[ic].v.pd.voff);
+                   assert(parent->hscl == cellsRow[ic].hscl &&
+                          parent->vscl == cellsRow[ic].vscl);
+                   assert(parent->hoff_d == cellsRow[ic].hoff_d &&
+                          parent->voff_d == cellsRow[ic].voff_d);
+               }
+               break;
+
+           default:
+               assert(0);
+           }
+       }
+       cellsRow += self.columnCount;
+    }
+#endif
+}
+
++ (wchar_t)getBlankChar
+{
+    return L' ';
+}
+
++ (int)getBlankAttribute
+{
+    return 0;
+}
+
+@end
+
+/**
+ * TerminalChanges is used to track changes made via the text_hook, pict_hook,
+ * wipe_hook, curs_hook, and bigcurs_hook callbacks on the terminal since the
+ * last call to xtra_hook for TERM_XTRA_FRESH.  The locations marked as changed
+ * can then be used to make bounding rectangles for the regions that need to
+ * be redisplayed.
+ */
+@interface TerminalChanges : NSObject {
+    int* colBounds;
+    /*
+     * Outside of firstChangedRow, lastChangedRow and what's in colBounds, the
+     * contents of this are handled lazily.
+     */
+    BOOL* marks;
+}
+
+/**
+ * Initialize with zero columns and zero rows.
+ */
+- (id)init;
+
+/**
+ * Initialize with nCol columns and nRow rows.  No changes will be marked.
+ */
+- (id)initWithColumns:(int)nCol rows:(int)nRow NS_DESIGNATED_INITIALIZER;
+
+/**
+ * Resize to be nCol by nRow.  Current contents still within the new bounds
+ * are preserved.  Added areas are marked as unchanged.
+ */
+- (void)resizeWithColumns:(int)nCol rows:(int)nRow;
+
+/**
+ * Clears all marked changes.
+ */
+- (void)clear;
+
+- (BOOL)isChangedAtColumn:(int)icol row:(int)irow;
+
+/**
+ * Scans the row, irow, starting at the column, icol0, and stopping before the
+ * column, icol1.  Returns the column index for the first cell that is
+ * changed.  The returned index will be equal to icol1 if all of the cells in
+ * the range are unchanged.
+ */
+- (int)scanForChangedInRow:(int)irow col0:(int)icol0 col1:(int)icol1;
+
+/**
+ * Scans the row, irow, starting at the column, icol0, and stopping before the
+ * column, icol1.  returns the column index for the first cell that has not
+ * changed.  The returned index will be equal to icol1 if all of the cells in
+ * the range have changed.
+ */
+- (int)scanForUnchangedInRow:(int)irow col0:(int)icol0 col1:(int)icol1;
+
+- (void)markChangedAtColumn:(int)icol row:(int)irow;
+
+- (void)markChangedRangeAtColumn:(int)icol row:(int)irow width:(int)w;
+
+/**
+ * Marks the block as changed who's upper left hand corner is at (icol, irow).
+ */
+- (void)markChangedBlockAtColumn:(int)icol
+                            row:(int)irow
+                          width:(int)w
+                         height:(int)h;
+
+/**
+ * Returns the index of the first changed column in the given row.  That index
+ * will be equal to the number of columns if there are no changes in the row.
+ */
+- (int)getFirstChangedColumnInRow:(int)irow;
+
+/**
+ * Returns the index of the last changed column in the given row.  That index
+ * will be equal to -1 if there are no changes in the row.
+ */
+- (int)getLastChangedColumnInRow:(int)irow;
+
+/**
+ * Is the number of columns.
+ */
+@property (readonly) int columnCount;
+
+/**
+ * Is the number of rows.
+ */
+@property (readonly) int rowCount;
+
+/**
+ * Is the index of the first row with changes.  Will be equal to the number
+ * of rows if there are no changes.
+ */
+@property (readonly) int firstChangedRow;
+
+/**
+ * Is the index of the last row with changes.  Will be equal to -1 if there
+ * are no changes.
+ */
+@property (readonly) int lastChangedRow;
+
+@end
+
+@implementation TerminalChanges
+
+- (id)init
+{
+    return [self initWithColumns:0 rows:0];
+}
+
+- (id)initWithColumns:(int)nCol rows:(int)nRow
+{
+    if (self = [super init]) {
+       self->colBounds = (int*) malloc(2 * nRow * sizeof(int));
+       self->marks = (BOOL*) malloc(nCol * nRow * sizeof(BOOL));
+       self->_columnCount = nCol;
+       self->_rowCount = nRow;
+       [self clear];
+    }
+    return self;
+}
+
+- (void)dealloc
+{
+    if (self->marks != 0) {
+       free(self->marks);
+       self->marks = 0;
+    }
+    if (self->colBounds != 0) {
+       free(self->colBounds);
+       self->colBounds = 0;
+    }
+}
+
+- (void)resizeWithColumns:(int)nCol rows:(int)nRow
+{
+    int* newColBounds = (int*) malloc(2 * nRow * sizeof(int));
+    BOOL* newMarks = (BOOL*) malloc(nCol * nRow * sizeof(BOOL));
+    int nRowCommon = (nRow < self.rowCount) ? nRow : self.rowCount;
+
+    if (self.firstChangedRow <= self.lastChangedRow &&
+       self.firstChangedRow < nRowCommon) {
+       BOOL* marksOutCursor = newMarks + self.firstChangedRow * nCol;
+       const BOOL* marksInCursor =
+           self->marks + self.firstChangedRow * self.columnCount;
+       int nColCommon = (nCol < self.columnCount) ? nCol : self.columnCount;
+
+       if (self.lastChangedRow >= nRowCommon) {
+           self->_lastChangedRow = nRowCommon - 1;
+       }
+       for (int i = self.firstChangedRow; i <= self.lastChangedRow; ++i) {
+           if (self->colBounds[i + i] < nColCommon) {
+               newColBounds[i + i] = self->colBounds[i + i];
+               newColBounds[i + i + 1] =
+                   (self->colBounds[i + i + 1] < nColCommon) ?
+                   self->colBounds[i + i + 1] : nColCommon - 1;
+               (void) memcpy(
+                   marksOutCursor + self->colBounds[i + i],
+                   marksInCursor + self->colBounds[i + i],
+                   (newColBounds[i + i + 1] - newColBounds[i + i] + 1) *
+                       sizeof(BOOL));
+               marksInCursor += self.columnCount;
+               marksOutCursor += nCol;
+           } else {
+               self->colBounds[i + i] = nCol;
+               self->colBounds[i + i + 1] = -1;
+           }
+       }
+    } else {
+       self->_firstChangedRow = nRow;
+       self->_lastChangedRow = -1;
+    }
+
+    free(self->colBounds);
+    self->colBounds = newColBounds;
+    free(self->marks);
+    self->marks = newMarks;
+    self->_columnCount = nCol;
+    self->_rowCount = nRow;
+}
+
+- (void)clear
+{
+    self->_firstChangedRow = self.rowCount;
+    self->_lastChangedRow = -1;
+}
+
+- (BOOL)isChangedAtColumn:(int)icol row:(int)irow
+{
+    if (irow < self.firstChangedRow || irow > self.lastChangedRow) {
+       return NO;
+    }
+    if (icol < self->colBounds[irow + irow] ||
+       icol > self->colBounds[irow + irow + 1]) {
+       return NO;
+    }
+    return self->marks[icol + irow * self.columnCount];
+}
+
+- (int)scanForChangedInRow:(int)irow col0:(int)icol0 col1:(int)icol1
+{
+    if (irow < self.firstChangedRow || irow > self.lastChangedRow ||
+       icol0 > self->colBounds[irow + irow + 1]) {
+       return icol1;
+    }
+
+    int i = (icol0 > self->colBounds[irow + irow]) ?
+       icol0 : self->colBounds[irow + irow];
+    int i1 = (icol1 <= self->colBounds[irow + irow + 1]) ?
+       icol1 : self->colBounds[irow + irow + 1] + 1;
+    const BOOL* marksCursor = self->marks + irow * self.columnCount;
+    while (1) {
+       if (i >= i1) {
+           return icol1;
+       }
+       if (marksCursor[i]) {
+           return i;
+       }
+       ++i;
+    }
+}
+
+- (int)scanForUnchangedInRow:(int)irow col0:(int)icol0 col1:(int)icol1
+{
+    if (irow < self.firstChangedRow || irow > self.lastChangedRow ||
+       icol0 < self->colBounds[irow + irow] ||
+       icol0 > self->colBounds[irow + irow + 1]) {
+       return icol0;
+    }
+
+    int i = icol0;
+    int i1 = (icol1 <= self->colBounds[irow + irow + 1]) ?
+       icol1 : self->colBounds[irow + irow + 1] + 1;
+    const BOOL* marksCursor = self->marks + irow * self.columnCount;
+    while (1) {
+       if (i >= i1 || ! marksCursor[i]) {
+           return i;
+       }
+       ++i;
+    }
+}
+
+- (void)markChangedAtColumn:(int)icol row:(int)irow
+{
+    [self markChangedBlockAtColumn:icol row:irow width:1 height:1];
+}
+
+- (void)markChangedRangeAtColumn:(int)icol row:(int)irow width:(int)w
+{
+    [self markChangedBlockAtColumn:icol row:irow width:w height:1];
+}
+
+- (void)markChangedBlockAtColumn:(int)icol
+                            row:(int)irow
+                          width:(int)w
+                         height:(int)h
+{
+    if (irow + h <= self.firstChangedRow) {
+       /* All prior marked regions are on rows after the requested block. */
+       if (self.firstChangedRow > self.lastChangedRow) {
+           self->_lastChangedRow = irow + h - 1;
+       } else {
+           for (int i = irow + h; i < self.firstChangedRow; ++i) {
+               self->colBounds[i + i] = self.columnCount;
+               self->colBounds[i + i + 1] = -1;
+           }
+       }
+       self->_firstChangedRow = irow;
+
+       BOOL* marksCursor = self->marks + irow * self.columnCount;
+       for (int i = irow; i < irow + h; ++i) {
+           self->colBounds[i + i] = icol;
+           self->colBounds[i + i + 1] = icol + w - 1;
+           for (int j = icol; j < icol + w; ++j) {
+               marksCursor[j] = YES;
+           }
+           marksCursor += self.columnCount;
+       }
+    } else if (irow > self.lastChangedRow) {
+       /* All prior marked regions are on rows before the requested block. */
+       int i;
+
+       for (i = self.lastChangedRow + 1; i < irow; ++i) {
+           self->colBounds[i + i] = self.columnCount;
+           self->colBounds[i + i + 1] = -1;
+       }
+       self->_lastChangedRow = irow + h - 1;
+
+       BOOL* marksCursor = self->marks + irow * self.columnCount;
+       for (i = irow; i < irow + h; ++i) {
+           self->colBounds[i + i] = icol;
+           self->colBounds[i + i + 1] = icol + w - 1;
+           for (int j = icol; j < icol + w; ++j) {
+               marksCursor[j] = YES;
+           }
+           marksCursor += self.columnCount;
+       }
+    } else {
+       /*
+        * There's overlap between the rows of the requested block and prior
+        * marked regions.
+        */
+       BOOL* marksCursor = self->marks + irow * self.columnCount;
+       int irow0, h0;
+
+       if (irow < self.firstChangedRow) {
+           /* Handle any leading rows where there's no overlap. */
+           for (int i = irow; i < self.firstChangedRow; ++i) {
+               self->colBounds[i + i] = icol;
+               self->colBounds[i + i + 1] = icol + w - 1;
+               for (int j = icol; j < icol + w; ++j) {
+                   marksCursor[j] = YES;
+               }
+               marksCursor += self.columnCount;
+           }
+           irow0 = self.firstChangedRow;
+           h0 = irow + h - self.firstChangedRow;
+           self->_firstChangedRow = irow;
+       } else {
+           irow0 = irow;
+           h0 = h;
+       }
+
+       /* Handle potentially overlapping rows */
+       if (irow0 + h0 > self.lastChangedRow + 1) {
+           h0 = self.lastChangedRow + 1 - irow0;
+           self->_lastChangedRow = irow + h - 1;
+       }
+
+       int i;
+       for (i = irow0; i < irow0 + h0; ++i) {
+           if (icol + w <= self->colBounds[i + i]) {
+               int j;
+
+               for (j = icol; j < icol + w; ++j) {
+                   marksCursor[j] = YES;
+               }
+               if (self->colBounds[i + i] > self->colBounds[i + i + 1]) {
+                   self->colBounds[i + i + 1] = icol + w - 1;
+               } else {
+                   for (j = icol + w; j < self->colBounds[i + i]; ++j) {
+                       marksCursor[j] = NO;
+                   }
+               }
+               self->colBounds[i + i] = icol;
+           } else if (icol > self->colBounds[i + i + 1]) {
+               int j;
+
+               for (j = self->colBounds[i + i + 1] + 1; j < icol; ++j) {
+                   marksCursor[j] = NO;
+               }
+               for (j = icol; j < icol + w; ++j) {
+                   marksCursor[j] = YES;
+               }
+               self->colBounds[i + i + 1] = icol + w - 1;
+           } else {
+               if (icol < self->colBounds[i + i]) {
+                   self->colBounds[i + i] = icol;
+               }
+               if (icol + w > self->colBounds[i + i + 1]) {
+                   self->colBounds[i + i + 1] = icol + w - 1;
+               }
+               for (int j = icol; j < icol + w; ++j) {
+                   marksCursor[j] = YES;
+               }
+           }
+           marksCursor += self.columnCount;
+       }
+
+       /* Handle any trailing rows where there's no overlap. */
+       for (i = irow0 + h0; i < irow + h; ++i) {
+           self->colBounds[i + i] = icol;
+           self->colBounds[i + i + 1] = icol + w - 1;
+           for (int j = icol; j < icol + w; ++j) {
+               marksCursor[j] = YES;
+           }
+           marksCursor += self.columnCount;
+       }
+    }
+}
+
+- (int)getFirstChangedColumnInRow:(int)irow
+{
+    if (irow < self.firstChangedRow || irow > self.lastChangedRow) {
+       return self.columnCount;
+    }
+    return self->colBounds[irow + irow];
+}
+
+- (int)getLastChangedColumnInRow:(int)irow
+{
+    if (irow < self.firstChangedRow || irow > self.lastChangedRow) {
+       return -1;
+    }
+    return self->colBounds[irow + irow + 1];
+}
+
+@end
+
+
+/**
+ * Draws one tile as a helper function for AngbandContext's drawRect.
+ */
+static void draw_image_tile(
+    NSGraphicsContext* nsContext,
+    CGContextRef cgContext,
+    CGImageRef image,
+    NSRect srcRect,
+    NSRect dstRect,
+    NSCompositingOperation op)
+{
+    /* Flip the source rect since the source image is flipped */
+    CGAffineTransform flip = CGAffineTransformIdentity;
+    flip = CGAffineTransformTranslate(flip, 0.0, CGImageGetHeight(image));
+    flip = CGAffineTransformScale(flip, 1.0, -1.0);
+    CGRect flippedSourceRect =
+       CGRectApplyAffineTransform(NSRectToCGRect(srcRect), flip);
+
+    /*
+     * When we use high-quality resampling to draw a tile, pixels from outside
+     * the tile may bleed in, causing graphics artifacts. Work around that.
+     */
+    CGImageRef subimage =
+       CGImageCreateWithImageInRect(image, flippedSourceRect);
+    [nsContext setCompositingOperation:op];
+    CGContextDrawImage(cgContext, NSRectToCGRect(dstRect), subimage);
+    CGImageRelease(subimage);
+}
+
+
+/*
+ * The max number of glyphs we support.  Currently this only affects
+ * updateGlyphInfo() for the calculation of the tile size, fontAscender,
+ * fontDescender, nColPre, and nColPost.  The rendering in drawWChar() will
+ * work for a glyph not in updateGlyphInfo()'s set, and that is used for
+ * rendering Japanese characters, though there may be clipping or clearing
+ * artifacts because it wasn't included in updateGlyphInfo()'s calculations.
+ */
+#define GLYPH_COUNT 256
+
+/*
+ * An AngbandContext represents a logical Term (i.e. what Angband thinks is
+ * a window).
+ */
+@interface AngbandContext : NSObject <NSWindowDelegate>
+{
+@public
+
+    /* The Angband term */
+    term_type *terminal;
+
+@private
+    /* Is the last time we drew, so we can throttle drawing. */
+    CFAbsoluteTime lastRefreshTime;
+
+    /* Flags whether or not a fullscreen transition is in progress. */
+    BOOL inFullscreenTransition;
+
+    /* Our view */
+    AngbandView *angbandView;
+}
+
+/* Column and row counts, by default TERM_DEFAULT_COLS x TERM_DEFAULT_ROWS */
+@property (readonly) int cols;
+@property (readonly) int rows;
+
+/* The size of the border between the window edge and the contents */
+@property (readonly) NSSize borderSize;
+
+/* The font of this context */
+@property NSFont *angbandViewFont;
+
+/* The size of one tile */
+@property (readonly) NSSize tileSize;
+
+/* Font's ascender and descender */
+@property (readonly) CGFloat fontAscender;
+@property (readonly) CGFloat fontDescender;
+
+/*
+ * These are the number of columns before or after, respectively, a text
+ * change that may need to be redrawn.
+ */
+@property (readonly) int nColPre;
+@property (readonly) int nColPost;
+
+/* If this context owns a window, here it is. */
+@property NSWindow *primaryWindow;
+
+/* Holds our version of the contents of the terminal. */
+@property TerminalContents *contents;
+
+/*
+ * Marks which locations have been changed by the text_hook, pict_hook,
+ * wipe_hook, curs_hook, and bigcurs_hhok callbacks on the terminal since
+ * the last call to xtra_hook with TERM_XTRA_FRESH.
+ */
+@property TerminalChanges *changes;
+
+/*
+ * Record first possible row and column for tiles for double-height tile
+ * handling.
+ */
+@property int firstTileRow;
+@property int firstTileCol;
+
+@property (nonatomic, assign) BOOL hasSubwindowFlags;
+@property (nonatomic, assign) BOOL windowVisibilityChecked;
+
+- (void)resizeWithColumns:(int)nCol rows:(int)nRow;
+
+/**
+ * Based on what has been marked as changed, inform AppKit of the bounding
+ * rectangles for the changed areas.
+ */
+- (void)computeInvalidRects;
+
+- (void)drawRect:(NSRect)rect inView:(NSView *)view;
+
+/* Called at initialization to set the term */
+- (void)setTerm:(term_type *)t;
+
+/* Called when the context is going down. */
+- (void)dispose;
+
+/*
+ * Return the rect in view coordinates for the block of cells whose upper
+ * left corner is (x,y).
+ */
+- (NSRect)viewRectForCellBlockAtX:(int)x y:(int)y width:(int)w height:(int)h;
+
+/* Draw the given wide character into the given tile rect. */
+- (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile screenFont:(NSFont*)font
+         context:(CGContextRef)ctx;
+
+/*
+ * Returns the primary window for this angband context, creating it if
+ * necessary
+ */
+- (NSWindow *)makePrimaryWindow;
+
+/* Handle becoming the main window */
+- (void)windowDidBecomeMain:(NSNotification *)notification;
+
+/* Return whether the context's primary window is ordered in or not */
+- (BOOL)isOrderedIn;
+
+/*
+ * Return whether the context's primary window is the main window.
+ * Since the terminals other than terminal 0 are configured as panels in
+ * Hengband, this will only be true for terminal 0.
+ */
+- (BOOL)isMainWindow;
+
+/*
+ * Return whether the context's primary window is the destination for key
+ * input.
+ */
+- (BOOL)isKeyWindow;
+
+/* Invalidate the whole image */
+- (void)setNeedsDisplay:(BOOL)val;
+
+/* Invalidate part of the image, with the rect expressed in view coordinates */
+- (void)setNeedsDisplayInRect:(NSRect)rect;
+
+/* Display (flush) our Angband views */
+- (void)displayIfNeeded;
+
+/*
+ * Resize context to size of contentRect, and optionally save size to
+ * defaults
+ */
+- (void)resizeTerminalWithContentRect: (NSRect)contentRect saveToDefaults: (BOOL)saveToDefaults;
+
+/*
+ * Change the minimum size and size increments for the window associated with
+ * the context.  termIdx is the index for the terminal:  pass it so this
+ * function can be used when self->terminal has not yet been set.
+ */
+- (void)constrainWindowSize:(int)termIdx;
+
+- (void)saveWindowVisibleToDefaults: (BOOL)windowVisible;
+- (BOOL)windowVisibleUsingDefaults;
+
+/* Class methods */
+/**
+ * Gets the default font for all contexts.  Currently not declaring this as
+ * a class property for compatibility with versions of Xcode prior to 8.
+ */
++ (NSFont*)defaultFont;
+/**
+ * Sets the default font for all contexts.
+ */
++ (void)setDefaultFont:(NSFont*)font;
+
+/* Internal methods */
+/* Set the title for the primary window. */
+- (void)setDefaultTitle:(int)termIdx;
+
+@end
+
+/**
+ * Generate a mask for the subwindow flags. The mask is just a safety check to
+ * make sure that our windows show and hide as expected.
+ */
+static EnumClassFlagGroup<SubWindowRedrawingFlag> AngbandMaskForValidSubwindowFlags(void)
+{
+    EnumClassFlagGroup<SubWindowRedrawingFlag> mask;
+    int maxBits = (int)(sizeof(window_flag_desc)
+        / sizeof(window_flag_desc[0]));
+
+    for( int i = 0; i < maxBits; i++ )
+    {
+        if( window_flag_desc[i] != NULL )
+        {
+            mask.set(i2enum<SubWindowRedrawingFlag>(i));
+        }
+    }
+
+    return mask;
+}
+
+/**
+ * Check for changes in the subwindow flags and update window visibility.
+ * This seems to be called for every user event, so we don't
+ * want to do any unnecessary hiding or showing of windows.
+ */
+static void AngbandUpdateWindowVisibility(void)
+{
+    /*
+     * Because this function is called frequently, we'll make the mask static.
+     * It doesn't change between calls, as the flags themselves are hardcoded
+     */
+    static EnumClassFlagGroup<SubWindowRedrawingFlag> validWindowFlagsMask;
+    BOOL anyChanged = NO;
+
+    if( validWindowFlagsMask.none() )
+    {
+        validWindowFlagsMask = AngbandMaskForValidSubwindowFlags();
+    }
+
+    /*
+     * Loop through all of the subwindows and see if there is a change in the
+     * flags. If so, show or hide the corresponding window. We don't care about
+     * the flags themselves; we just want to know if any are set.
+     */
+    for( int i = 1; i < ANGBAND_TERM_MAX; i++ )
+    {
+        AngbandContext *angbandContext =
+           (__bridge AngbandContext*) (angband_terms[i]->data);
+
+        if( angbandContext == nil )
+        {
+            continue;
+        }
+
+        /*
+         * This horrible mess of flags is so that we can try to maintain some
+         * user visibility preference. This should allow the user a window and
+         * have it stay closed between application launches. However, this
+         * means that when a subwindow is turned on, it will no longer appear
+         * automatically. Angband has no concept of user control over window
+         * visibility, other than the subwindow flags.
+         */
+        if( !angbandContext.windowVisibilityChecked )
+        {
+            if( [angbandContext windowVisibleUsingDefaults] )
+            {
+                [angbandContext.primaryWindow orderFront: nil];
+                angbandContext.windowVisibilityChecked = YES;
+                anyChanged = YES;
+            }
+            else if ([angbandContext.primaryWindow isVisible])
+            {
+                [angbandContext.primaryWindow close];
+                angbandContext.windowVisibilityChecked = NO;
+                anyChanged = YES;
+            }
+        }
+        else
+        {
+            BOOL termHasSubwindowFlags =
+                g_window_flags[i].has_any_of(validWindowFlagsMask);
+
+            if( angbandContext.hasSubwindowFlags && !termHasSubwindowFlags )
+            {
+                [angbandContext.primaryWindow close];
+                angbandContext.hasSubwindowFlags = NO;
+                [angbandContext saveWindowVisibleToDefaults: NO];
+                anyChanged = YES;
+            }
+            else if( !angbandContext.hasSubwindowFlags && termHasSubwindowFlags )
+            {
+                [angbandContext.primaryWindow orderFront: nil];
+                angbandContext.hasSubwindowFlags = YES;
+                [angbandContext saveWindowVisibleToDefaults: YES];
+                anyChanged = YES;
+            }
+        }
+    }
+
+    /* Make the main window key so that user events go to the right spot */
+    if (anyChanged) {
+        AngbandContext *mainWindow =
+            (__bridge AngbandContext*) (angband_terms[0]->data);
+        [mainWindow.primaryWindow makeKeyAndOrderFront: nil];
+    }
+}
+
+/**
+ * ------------------------------------------------------------------------
+ * Graphics support
+ * ------------------------------------------------------------------------ */
+
+/**
+ * The tile image
+ */
+static CGImageRef pict_image;
+
+/**
+ * Numbers of rows and columns in a tileset,
+ * calculated by the PICT/PNG loading code
+ */
+static int pict_cols = 0;
+static int pict_rows = 0;
+
+/**
+ * Requested graphics mode (as a grafID).
+ * The current mode is stored in current_graphics_mode.
+ */
+static int graf_mode_req = 0;
+
+/**
+ * Helper function to check the various ways that graphics can be enabled,
+ * guarding against NULL
+ */
+static BOOL graphics_are_enabled(void)
+{
+    return current_graphics_mode
+       && current_graphics_mode->grafID != GRAPHICS_NONE;
+}
+
+/**
+ * Like graphics_are_enabled(), but test the requested graphics mode.
+ */
+static BOOL graphics_will_be_enabled(void)
+{
+    if (graf_mode_req == GRAPHICS_NONE) {
+       return NO;
+    }
+
+    graphics_mode *new_mode = get_graphics_mode(graf_mode_req);
+    return new_mode && new_mode->grafID != GRAPHICS_NONE;
+}
+
+/**
+ * Hack -- game in progress
+ */
+static BOOL game_in_progress = NO;
+
+
+#pragma mark Prototypes
+static void wakeup_event_loop(void);
+static void hook_plog(const char *str);
+static void hook_quit(const char * str);
+static NSString* get_lib_directory(void);
+static NSString* get_doc_directory(void);
+static NSString* AngbandCorrectedDirectoryPath(NSString *originalPath);
+static void prepare_paths_and_directories(void);
+static void load_prefs(void);
+static void init_windows(void);
+static void send_key(const char key);
+static BOOL check_events(int wait);
+static BOOL send_event(NSEvent *event);
+static void set_color_for_index(int idx);
+static void record_current_savefile(void);
+
+/**
+ * Available values for 'wait'
+ */
+#define CHECK_EVENTS_DRAIN -1
+#define CHECK_EVENTS_NO_WAIT   0
+#define CHECK_EVENTS_WAIT 1
+
+
+/**
+ * Note when "open"/"new" become valid
+ */
+static BOOL initialized = NO;
+
+/* Methods for getting the appropriate NSUserDefaults */
+@interface NSUserDefaults (AngbandDefaults)
++ (NSUserDefaults *)angbandDefaults;
+@end
+
+@implementation NSUserDefaults (AngbandDefaults)
++ (NSUserDefaults *)angbandDefaults
+{
+    return [NSUserDefaults standardUserDefaults];
+}
+@end
+
+/*
+ * Methods for pulling images out of the Angband bundle (which may be separate
+ * from the current bundle in the case of a screensaver
+ */
+@interface NSImage (AngbandImages)
++ (NSImage *)angbandImage:(NSString *)name;
+@end
+
+/* The NSView subclass that draws our Angband image */
+@interface AngbandView : NSView {
+@private
+    NSBitmapImageRep *cacheForResize;
+    NSRect cacheBounds;
+}
+
+@property (nonatomic, weak) AngbandContext *angbandContext;
+
+@end
+
+@implementation NSImage (AngbandImages)
+
+/*
+ * Returns an image in the resource directoy of the bundle containing the
+ * Angband view class.
+ */
++ (NSImage *)angbandImage:(NSString *)name
+{
+    NSBundle *bundle = [NSBundle bundleForClass:[AngbandView class]];
+    NSString *path = [bundle pathForImageResource:name];
+    return (path) ? [[NSImage alloc] initByReferencingFile:path] : nil;
+}
+
+@end
+
+
+@implementation AngbandContext
+
+- (NSSize)baseSize
+{
+    /*
+     * We round the base size down. If we round it up, I believe we may end up
+     * with pixels that nobody "owns" that may accumulate garbage. In general
+     * rounding down is harmless, because any lost pixels may be sopped up by
+     * the border.
+     */
+    return NSMakeSize(
+       floor(self.cols * self.tileSize.width + 2 * self.borderSize.width),
+       floor(self.rows * self.tileSize.height + 2 * self.borderSize.height));
+}
+
+/* qsort-compatible compare function for CGSizes */
+static int compare_advances(const void *ap, const void *bp)
+{
+    const CGSize *a = (CGSize*) ap, *b = (CGSize*) bp;
+    return (a->width > b->width) - (a->width < b->width);
+}
+
+/**
+ * Precompute certain metrics (tileSize, fontAscender, fontDescender, nColPre,
+ * and nColPost) for the current font.
+ */
+- (void)updateGlyphInfo
+{
+    NSFont *screenFont = [self.angbandViewFont screenFont];
+
+    /* Generate a string containing each MacRoman character */
+    /*
+     * Here and below, dynamically allocate working arrays rather than put them
+     * on the stack in case limited stack space is an issue.
+     */
+    unsigned char *latinString = (unsigned char*) malloc(GLYPH_COUNT);
+    if (latinString == 0) {
+       NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
+                                       reason:@"latinString in updateGlyphInfo"
+                                       userInfo:nil];
+       @throw exc;
+    }
+    size_t i;
+    for (i=0; i < GLYPH_COUNT; i++) latinString[i] = (unsigned char)i;
+
+    /* Turn that into unichar. Angband uses ISO Latin 1. */
+    NSString *allCharsString = [[NSString alloc] initWithBytes:latinString
+        length:GLYPH_COUNT encoding:NSISOLatin1StringEncoding];
+    unichar *unicharString = (unichar*) malloc(GLYPH_COUNT * sizeof(unichar));
+    if (unicharString == 0) {
+       free(latinString);
+       NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
+                                       reason:@"unicharString in updateGlyphInfo"
+                                       userInfo:nil];
+       @throw exc;
+    }
+    unicharString[0] = 0;
+    [allCharsString getCharacters:unicharString range:NSMakeRange(0, MIN(GLYPH_COUNT, [allCharsString length]))];
+    allCharsString = nil;
+    free(latinString);
+
+    /* Get glyphs */
+    CGGlyph *glyphArray = (CGGlyph*) calloc(GLYPH_COUNT, sizeof(CGGlyph));
+    if (glyphArray == 0) {
+       free(unicharString);
+       NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
+                                       reason:@"glyphArray in updateGlyphInfo"
+                                       userInfo:nil];
+       @throw exc;
+    }
+    CTFontGetGlyphsForCharacters((CTFontRef)screenFont, unicharString,
+                                glyphArray, GLYPH_COUNT);
+    free(unicharString);
+
+    /* Get advances. Record the max advance. */
+    CGSize *advances = (CGSize*) malloc(GLYPH_COUNT * sizeof(CGSize));
+    if (advances == 0) {
+       free(glyphArray);
+       NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
+                                       reason:@"advances in updateGlyphInfo"
+                                       userInfo:nil];
+       @throw exc;
+    }
+    CTFontGetAdvancesForGlyphs(
+       (CTFontRef)screenFont, kCTFontOrientationHorizontal, glyphArray,
+       advances, GLYPH_COUNT);
+    CGFloat *glyphWidths = (CGFloat*) malloc(GLYPH_COUNT * sizeof(CGFloat));
+    if (glyphWidths == 0) {
+       free(glyphArray);
+       free(advances);
+       NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
+                                       reason:@"glyphWidths in updateGlyphInfo"
+                                       userInfo:nil];
+       @throw exc;
+    }
+    for (i=0; i < GLYPH_COUNT; i++) {
+        glyphWidths[i] = advances[i].width;
+    }
+
+    /*
+     * For good non-mono-font support, use the median advance. Start by sorting
+     * all advances.
+     */
+    qsort(advances, GLYPH_COUNT, sizeof *advances, compare_advances);
+
+    /* Skip over any initially empty run */
+    size_t startIdx;
+    for (startIdx = 0; startIdx < GLYPH_COUNT; startIdx++)
+    {
+        if (advances[startIdx].width > 0) break;
+    }
+
+    /* Pick the center to find the median */
+    CGFloat medianAdvance = 0;
+    /* In case we have all zero advances for some reason */
+    if (startIdx < GLYPH_COUNT)
+    {
+        medianAdvance = advances[(startIdx + GLYPH_COUNT)/2].width;
+    }
+
+    free(advances);
+
+    /*
+     * Record the ascender and descender.  Some fonts, for instance DIN
+     * Condensed and Rockwell in 10.14, the ascent on '@' exceeds that
+     * reported by [screenFont ascender].  Get the overall bounding box
+     * for the glyphs and use that instead of the ascender and descender
+     * values if the bounding box result extends farther from the baseline.
+     */
+    CGRect bounds = CTFontGetBoundingRectsForGlyphs(
+       (CTFontRef) screenFont, kCTFontOrientationHorizontal, glyphArray,
+       NULL, GLYPH_COUNT);
+    self->_fontAscender = [screenFont ascender];
+    if (self->_fontAscender < bounds.origin.y + bounds.size.height) {
+       self->_fontAscender = bounds.origin.y + bounds.size.height;
+    }
+    self->_fontDescender = [screenFont descender];
+    if (self->_fontDescender > bounds.origin.y) {
+       self->_fontDescender = bounds.origin.y;
+    }
+
+    /*
+     * Record the tile size.  Round the values (height rounded up; width to
+     * nearest unless that would be zero) to have tile boundaries match pixel
+     * boundaries.
+     */
+    if (medianAdvance < 1.0) {
+       self->_tileSize.width = 1.0;
+    } else {
+       self->_tileSize.width = floor(medianAdvance + 0.5);
+    }
+    self->_tileSize.height = ceil(self.fontAscender - self.fontDescender);
+
+    /*
+     * Determine whether neighboring columns need to be redrawn when a
+     * character changes.
+     */
+    CGRect *boxes = (CGRect*) malloc(GLYPH_COUNT * sizeof(CGRect));
+    if (boxes == 0) {
+       free(glyphWidths);
+       free(glyphArray);
+       NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
+                                       reason:@"boxes in updateGlyphInfo"
+                                       userInfo:nil];
+       @throw exc;
+    }
+    CGFloat beyond_right = 0.;
+    CGFloat beyond_left = 0.;
+    CTFontGetBoundingRectsForGlyphs(
+       (CTFontRef)screenFont,
+       kCTFontOrientationHorizontal,
+       glyphArray,
+       boxes,
+       GLYPH_COUNT);
+    for (i = 0; i < GLYPH_COUNT; i++) {
+       /* Account for the compression and offset used by drawWChar(). */
+       CGFloat compression, offset;
+       CGFloat v;
+
+       if (glyphWidths[i] <= self.tileSize.width) {
+           compression = 1.;
+           offset = 0.5 * (self.tileSize.width - glyphWidths[i]);
+       } else {
+           compression = self.tileSize.width / glyphWidths[i];
+           offset = 0.;
+       }
+       v = (offset + boxes[i].origin.x) * compression;
+       if (beyond_left > v) {
+           beyond_left = v;
+       }
+       v = (offset + boxes[i].origin.x + boxes[i].size.width) * compression;
+       if (beyond_right < v) {
+           beyond_right = v;
+       }
+    }
+    free(boxes);
+    self->_nColPre = ceil(-beyond_left / self.tileSize.width);
+    if (beyond_right > self.tileSize.width) {
+       self->_nColPost =
+           ceil((beyond_right - self.tileSize.width) / self.tileSize.width);
+    } else {
+       self->_nColPost = 0;
+    }
+
+    free(glyphWidths);
+    free(glyphArray);
+}
+
+
+- (void)requestRedraw
+{
+    if (! self->terminal) return;
+    
+    term_type *old = game_term;
+    
+    /* Activate the term */
+    term_activate(self->terminal);
+    
+    /* Redraw the contents */
+    term_redraw();
+    
+    /* Flush the output */
+    term_fresh();
+    
+    /* Restore the old term */
+    term_activate(old);
+}
+
+- (void)setTerm:(term_type *)t
+{
+    self->terminal = t;
+}
+
+/**
+ * If we're trying to limit ourselves to a certain number of frames per second,
+ * then compute how long it's been since we last drew, and then wait until the
+ * next frame has passed. */
+- (void)throttle
+{
+    if (frames_per_second > 0)
+    {
+        CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
+        CFTimeInterval timeSinceLastRefresh = now - self->lastRefreshTime;
+        CFTimeInterval timeUntilNextRefresh = (1. / (double)frames_per_second) - timeSinceLastRefresh;
+        
+        if (timeUntilNextRefresh > 0)
+        {
+            usleep((unsigned long)(timeUntilNextRefresh * 1000000.));
+        }
+    }
+    self->lastRefreshTime = CFAbsoluteTimeGetCurrent();
+}
+
+- (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile screenFont:(NSFont*)font
+         context:(CGContextRef)ctx
+{
+    CGFloat tileOffsetY = self.fontAscender;
+    CGFloat tileOffsetX = 0.0;
+    UniChar unicharString[2];
+    int nuni;
+
+    if (CFStringGetSurrogatePairForLongCharacter(wchar, unicharString)) {
+       nuni = 2;
+    } else {
+       unicharString[0] = (UniChar) wchar;
+       nuni = 1;
+    }
+
+    /* Get glyph and advance */
+    CGGlyph thisGlyphArray[2] = { 0, 0 };
+    CGSize advances[2] = { { 0, 0 }, { 0, 0 } };
+    CTFontGetGlyphsForCharacters(
+       (CTFontRef)font, unicharString, thisGlyphArray, nuni);
+    CGGlyph glyph = thisGlyphArray[0];
+    CTFontGetAdvancesForGlyphs(
+       (CTFontRef)font, kCTFontOrientationHorizontal, thisGlyphArray,
+       advances, 1);
+    CGSize advance = advances[0];
+
+    /*
+     * If our font is not monospaced, our tile width is deliberately not big
+     * enough for every character. In that event, if our glyph is too wide, we
+     * need to compress it horizontally. Compute the compression ratio.
+     * 1.0 means no compression.
+     */
+    double compressionRatio;
+    if (advance.width <= NSWidth(tile))
+    {
+        /* Our glyph fits, so we can just draw it, possibly with an offset */
+        compressionRatio = 1.0;
+        tileOffsetX = (NSWidth(tile) - advance.width)/2;
+    }
+    else
+    {
+        /* Our glyph doesn't fit, so we'll have to compress it */
+        compressionRatio = NSWidth(tile) / advance.width;
+        tileOffsetX = 0;
+    }
+
+    /* Now draw it */
+    CGAffineTransform textMatrix = CGContextGetTextMatrix(ctx);
+    CGFloat savedA = textMatrix.a;
+
+    /* Set the position */
+    textMatrix.tx = tile.origin.x + tileOffsetX;
+    textMatrix.ty = tile.origin.y + tileOffsetY;
+
+    /* Maybe squish it horizontally. */
+    if (compressionRatio != 1.)
+    {
+        textMatrix.a *= compressionRatio;
+    }
+
+    CGContextSetTextMatrix(ctx, textMatrix);
+    CGContextShowGlyphsAtPositions(ctx, &glyph, &CGPointZero, 1);
+
+    /* Restore the text matrix if we messed with the compression ratio */
+    if (compressionRatio != 1.)
+    {
+        textMatrix.a = savedA;
+    }
+
+    CGContextSetTextMatrix(ctx, textMatrix);
+}
+
+- (NSRect)viewRectForCellBlockAtX:(int)x y:(int)y width:(int)w height:(int)h
+{
+    return NSMakeRect(
+       x * self.tileSize.width + self.borderSize.width,
+       y * self.tileSize.height + self.borderSize.height,
+       w * self.tileSize.width, h * self.tileSize.height);
+}
+
+- (void)setSelectionFont:(NSFont*)font adjustTerminal: (BOOL)adjustTerminal
+{
+    /* Record the new font */
+    self.angbandViewFont = font;
+
+    /* Update our glyph info */
+    [self updateGlyphInfo];
+
+    if( adjustTerminal )
+    {
+        /*
+         * Adjust terminal to fit window with new font; save the new columns
+         * and rows since they could be changed
+         */
+        NSRect contentRect =
+           [self.primaryWindow
+                contentRectForFrameRect: [self.primaryWindow frame]];
+
+       [self constrainWindowSize:[self terminalIndex]];
+       NSSize size = self.primaryWindow.contentMinSize;
+       BOOL windowNeedsResizing = NO;
+       if (contentRect.size.width < size.width) {
+           contentRect.size.width = size.width;
+           windowNeedsResizing = YES;
+       }
+       if (contentRect.size.height < size.height) {
+           contentRect.size.height = size.height;
+           windowNeedsResizing = YES;
+       }
+       if (windowNeedsResizing) {
+           size.width = contentRect.size.width;
+           size.height = contentRect.size.height;
+           [self.primaryWindow setContentSize:size];
+       }
+        [self resizeTerminalWithContentRect: contentRect saveToDefaults: YES];
+    }
+}
+
+- (id)init
+{
+    if ((self = [super init]))
+    {
+        /* Default rows and cols */
+        self->_cols = TERM_DEFAULT_COLS;
+        self->_rows = TERM_DEFAULT_ROWS;
+
+        /* Default border size */
+        self->_borderSize = NSMakeSize(2, 2);
+
+       self->_nColPre = 0;
+       self->_nColPost = 0;
+
+       self->_contents =
+           [[TerminalContents alloc] initWithColumns:self->_cols
+                                     rows:self->_rows];
+       self->_changes =
+           [[TerminalChanges alloc] initWithColumns:self->_cols
+                                    rows:self->_rows];
+       self->lastRefreshTime = CFAbsoluteTimeGetCurrent();
+       self->inFullscreenTransition = NO;
+
+       self->_firstTileRow = 0;
+       self->_firstTileCol = 0;
+       self->_windowVisibilityChecked = NO;
+    }
+    return self;
+}
+
+/**
+ * Destroy all the receiver's stuff. This is intended to be callable more than
+ * once.
+ */
+- (void)dispose
+{
+    self->terminal = NULL;
+
+    /* Disassociate ourselves from our view. */
+    [self->angbandView setAngbandContext:nil];
+    self->angbandView = nil;
+
+    /* Font */
+    self.angbandViewFont = nil;
+
+    /* Window */
+    [self.primaryWindow setDelegate:nil];
+    [self.primaryWindow close];
+    self.primaryWindow = nil;
+
+    /* Contents and pending changes */
+    self.contents = nil;
+    self.changes = nil;
+}
+
+/* Usual Cocoa fare */
+- (void)dealloc
+{
+    [self dispose];
+}
+
+- (void)resizeWithColumns:(int)nCol rows:(int)nRow
+{
+    [self.contents resizeWithColumns:nCol rows:nRow];
+    [self.changes resizeWithColumns:nCol rows:nRow];
+    self->_cols = nCol;
+    self->_rows = nRow;
+}
+
+/**
+ * For defaultFont and setDefaultFont.
+ */
+static __strong NSFont* gDefaultFont = nil;
+
++ (NSFont*)defaultFont
+{
+    return gDefaultFont;
+}
+
++ (void)setDefaultFont:(NSFont*)font
+{
+    gDefaultFont = font;
+}
+
+- (void)setDefaultTitle:(int)termIdx
+{
+    NSMutableString *title =
+       [NSMutableString stringWithCString:angband_term_name[termIdx]
+#ifdef JP
+                        encoding:NSJapaneseEUCStringEncoding
+#else
+                        encoding:NSMacOSRomanStringEncoding
+#endif
+       ];
+    [title appendFormat:@" %dx%d", self.cols, self.rows];
+    [[self makePrimaryWindow] setTitle:title];
+}
+
+- (NSWindow *)makePrimaryWindow
+{
+    if (! self.primaryWindow)
+    {
+        /*
+         * This has to be done after the font is set, which it already is in
+         * term_init_cocoa()
+         */
+        NSSize sz = self.baseSize;
+        NSRect contentRect = NSMakeRect( 0.0, 0.0, sz.width, sz.height );
+
+        NSUInteger styleMask = NSWindowStyleMaskTitled |
+           NSWindowStyleMaskResizable | NSWindowStyleMaskMiniaturizable;
+
+        /*
+        * Make every window other than the main window closable, also create
+        * them as utility panels to get the thinner title bar and other
+        * attributes that already match up with how those windows are used.
+        */
+        if ((__bridge AngbandContext*) (angband_terms[0]->data) != self)
+        {
+           NSPanel *panel = [[NSPanel alloc]
+               initWithContentRect:contentRect
+               styleMask:(styleMask | NSWindowStyleMaskClosable
+                   | NSWindowStyleMaskUtilityWindow)
+               backing:NSBackingStoreBuffered defer:YES];
+
+           panel.floatingPanel = NO;
+           self.primaryWindow = panel;
+        } else {
+           self.primaryWindow =
+               [[NSWindow alloc] initWithContentRect:contentRect
+                                 styleMask:styleMask
+                                 backing:NSBackingStoreBuffered defer:YES];
+       }
+
+        /* Not to be released when closed */
+        [self.primaryWindow setReleasedWhenClosed:NO];
+        [self.primaryWindow setExcludedFromWindowsMenu: YES]; /* we're using custom window menu handling */
+
+        /* Make the view */
+        self->angbandView = [[AngbandView alloc] initWithFrame:contentRect];
+        [angbandView setAngbandContext:self];
+        [angbandView setNeedsDisplay:YES];
+        [self.primaryWindow setContentView:angbandView];
+
+        /* We are its delegate */
+        [self.primaryWindow setDelegate:self];
+    }
+    return self.primaryWindow;
+}
+
+
+- (void)computeInvalidRects
+{
+    for (int irow = self.changes.firstChangedRow;
+        irow <= self.changes.lastChangedRow;
+        ++irow) {
+       int icol = [self.changes scanForChangedInRow:irow
+                       col0:0 col1:self.cols];
+
+       while (icol < self.cols) {
+           /* Find the end of the changed region. */
+           int jcol =
+               [self.changes scanForUnchangedInRow:irow col0:(icol + 1)
+                    col1:self.cols];
+
+           /*
+            * If the last column is a character, extend the region drawn
+            * because characters can exceed the horizontal bounds of the cell
+            * and those parts will need to be cleared.  Don't extend into a
+            * tile because the clipping is set while drawing to never
+            * extend text into a tile.  For a big character that's been
+            * partially overwritten, allow what comes after the point
+            * where the overwrite occurred to influence the stuff before
+            * but not vice versa.  If extending the region reaches another
+            * changed block, find the end of that block and repeat the
+            * process.
+            */
+           /*
+            * A value of zero means checking for a character immediately
+            * prior to the column, isrch.  A value of one means checking for
+            * something past the end that could either influence the changed
+            * region (within nColPre of it and no intervening tile) or be
+            * influenced by it (within nColPost of it and no intervening
+            * tile or partially overwritten big character).  A value of two
+            * means checking for something past the end which is both changed
+            * and could affect the part of the unchanged region that has to
+            * be redrawn because it is affected by the prior changed region
+            * Values of three and four are like one and two, respectively,
+            * but indicate that a partially overwritten big character was
+            * found.
+            */
+           int stage = 0;
+           int isrch = jcol;
+           int irng0 = jcol;
+           int irng1 = jcol;
+           while (1) {
+               if (stage == 0) {
+                   const struct TerminalCell *pcell =
+                       [self.contents getCellAtColumn:(isrch - 1) row:irow];
+                   if ((pcell->form &
+                        (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
+                       break;
+                   } else {
+                       irng0 = isrch + self.nColPre;
+                       if (irng0 > self.cols) {
+                           irng0 = self.cols;
+                       }
+                       irng1 = isrch + self.nColPost;
+                       if (irng1 > self.cols) {
+                           irng1 = self.cols;
+                       }
+                       if (isrch < irng0 || isrch < irng1) {
+                           stage = isPartiallyOverwrittenBigChar(pcell) ?
+                               3 : 1;
+                       } else {
+                           break;
+                       }
+                   }
+               }
+
+               if (stage == 1) {
+                   const struct TerminalCell *pcell =
+                       [self.contents getCellAtColumn:isrch row:irow];
+
+                   if ((pcell->form &
+                        (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
+                       /*
+                        * Check if still in the region that could be
+                        * influenced by the changed region.  If so,
+                        * everything up to the tile will be redrawn anyways
+                        * so combine the regions if the tile has changed
+                        * as well.  Otherwise, terminate the search since
+                        * the tile doesn't allow influence to propagate
+                        * through it and don't want to affect what's in the
+                        * tile.
+                        */
+                       if (isrch < irng1) {
+                           if ([self.changes isChangedAtColumn:isrch
+                                    row:irow]) {
+                               jcol = [self.changes scanForUnchangedInRow:irow
+                                           col0:(isrch + 1) col1:self.cols];
+                               if (jcol < self.cols) {
+                                   stage = 0;
+                                   isrch = jcol;
+                                   continue;
+                               }
+                           }
+                       }
+                       break;
+                   } else {
+                       /*
+                        * With a changed character, combine the regions (if
+                        * still in the region affected by the changed region
+                        * am going to redraw everything up to this new region
+                        * anyway; if only in the region that can affect the
+                        * changed region, this changed text could influence
+                        * the current changed region).
+                        */
+                       if ([self.changes isChangedAtColumn:isrch row:irow]) {
+                           jcol = [self.changes scanForUnchangedInRow:irow
+                                       col0:(isrch + 1) col1:self.cols];
+                           if (jcol < self.cols) {
+                               stage = 0;
+                               isrch = jcol;
+                               continue;
+                           }
+                           break;
+                       }
+
+                       if (isrch < irng1) {
+                           /*
+                            * Can be affected by the changed region so
+                            * has to be redrawn.
+                            */
+                           ++jcol;
+                       }
+                       ++isrch;
+                       if (isrch >= irng1) {
+                           irng0 = jcol + self.nColPre;
+                           if (irng0 > self.cols) {
+                               irng0 = self.cols;
+                           }
+                           if (isrch >= irng0) {
+                               break;
+                           }
+                           stage = isPartiallyOverwrittenBigChar(pcell) ?
+                               4 : 2;
+                       } else if (isPartiallyOverwrittenBigChar(pcell)) {
+                           stage = 3;
+                       }
+                   }
+               }
+
+               if (stage == 2) {
+                   /*
+                    * Looking for a later changed region that could influence
+                    * the region that has to be redrawn.  The region that has
+                    * to be redrawn ends just before jcol.
+                    */
+                   const struct TerminalCell *pcell =
+                       [self.contents getCellAtColumn:isrch row:irow];
+
+                   if ((pcell->form &
+                        (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
+                       /* Can not spread influence through a tile. */
+                       break;
+                   }
+                   if ([self.changes isChangedAtColumn:isrch row:irow]) {
+                       /*
+                        * Found one.  Combine with the one ending just before
+                        * jcol.
+                        */
+                       jcol = [self.changes scanForUnchangedInRow:irow
+                                   col0:(isrch + 1) col1:self.cols];
+                       if (jcol < self.cols) {
+                           stage = 0;
+                           isrch = jcol;
+                           continue;
+                       }
+                       break;
+                   }
+
+                   ++isrch;
+                   if (isrch >= irng0) {
+                       break;
+                   }
+                   if (isPartiallyOverwrittenBigChar(pcell)) {
+                       stage = 4;
+                   }
+               }
+
+               if (stage == 3) {
+                   const struct TerminalCell *pcell =
+                       [self.contents getCellAtColumn:isrch row:irow];
+
+                   /*
+                    * Have encountered a partially overwritten big character
+                    * but still may be in the region that could be influenced
+                    * by the changed region.  That influence can not extend
+                    * past the past the padding for the partially overwritten
+                    * character.
+                    */
+                   if ((pcell->form & (TERM_CELL_CHAR | TERM_CELL_TILE |
+                                       TERM_CELL_TILE_PADDING)) != 0) {
+                       if (isrch < irng1) {
+                           /*
+                            * Still can be affected by the changed region
+                            * so everything up to isrch will be redrawn
+                            * anyways.  If this location has changed,
+                            * merge the changed regions.
+                            */
+                           if ([self.changes isChangedAtColumn:isrch
+                                    row:irow]) {
+                               jcol = [self.changes scanForUnchangedInRow:irow
+                                           col0:(isrch + 1) col1:self.cols];
+                               if (jcol < self.cols) {
+                                   stage = 0;
+                                   isrch = jcol;
+                                   continue;
+                               }
+                               break;
+                           }
+                       }
+                       if ((pcell->form &
+                            (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
+                           /*
+                            * It's a tile.  That blocks influence in either
+                            * direction.
+                            */
+                           break;
+                       }
+
+                       /*
+                        * The partially overwritten big character was
+                        * overwritten by a character.  Check to see if it
+                        * can either influence the unchanged region that
+                        * has to redrawn or the changed region prior to
+                        * that.
+                        */
+                       if (isrch >= irng0) {
+                           break;
+                       }
+                       stage = 4;
+                   } else {
+                       if (isrch < irng1) {
+                           /*
+                            * Can be affected by the changed region so has to
+                            * be redrawn.
+                            */
+                           ++jcol;
+                       }
+                       ++isrch;
+                       if (isrch >= irng1) {
+                           irng0 = jcol + self.nColPre;
+                           if (irng0 > self.cols) {
+                               irng0 = self.cols;
+                           }
+                           if (isrch >= irng0) {
+                               break;
+                           }
+                           stage = 4;
+                       }
+                   }
+               }
+
+               if (stage == 4) {
+                   /*
+                    * Have already encountered a partially overwritten big
+                    * character.  Looking for a later changed region that
+                    * could influence the region that has to be redrawn
+                    * The region that has to be redrawn ends just before jcol.
+                    */
+                   const struct TerminalCell *pcell =
+                       [self.contents getCellAtColumn:isrch row:irow];
+
+                   if ((pcell->form &
+                        (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
+                       /* Can not spread influence through a tile. */
+                       break;
+                   }
+                   if (pcell->form == TERM_CELL_CHAR) {
+                       if ([self.changes isChangedAtColumn:isrch row:irow]) {
+                           /*
+                            * Found a changed region.  Combine with the one
+                            * ending just before jcol.
+                            */
+                           jcol = [self.changes scanForUnchangedInRow:irow
+                                       col0:(isrch + 1) col1:self.cols];
+                           if (jcol < self.cols) {
+                               stage = 0;
+                               isrch = jcol;
+                               continue;
+                           }
+                           break;
+                       }
+                   }
+                   ++isrch;
+                   if (isrch >= irng0) {
+                       break;
+                   }
+               }
+           }
+
+           /*
+            * Check to see if there's characters before the changed region
+            * that would have to be redrawn because it's influenced by the
+            * changed region.  Do not have to check for merging with a prior
+            * region because of the screening already done.
+            */
+           if (self.nColPre > 0 &&
+               ([self.contents getCellAtColumn:icol row:irow]->form &
+                (TERM_CELL_CHAR | TERM_CELL_CHAR_PADDING)) != 0) {
+               int irng = icol - self.nColPre;
+
+               if (irng < 0) {
+                   irng = 0;
+               }
+               while (icol > irng &&
+                      ([self.contents getCellAtColumn:(icol - 1)
+                            row:irow]->form &
+                       (TERM_CELL_CHAR | TERM_CELL_CHAR_PADDING)) != 0) {
+                   --icol;
+               }
+           }
+
+           NSRect r = [self viewRectForCellBlockAtX:icol y:irow
+                            width:(jcol - icol) height:1];
+           [self setNeedsDisplayInRect:r];
+
+           icol = [self.changes scanForChangedInRow:irow col0:jcol
+                       col1:self.cols];
+       }
+    }
+}
+
+
+#pragma mark View/Window Passthrough
+
+/*
+ * This is a qsort-compatible compare function for NSRect, to get them in
+ * ascending order by y origin.
+ */
+static int compare_nsrect_yorigin_greater(const void *ap, const void *bp)
+{
+    const NSRect *arp = (NSRect*) ap;
+    const NSRect *brp = (NSRect*) bp;
+    return (arp->origin.y > brp->origin.y) - (arp->origin.y < brp->origin.y);
+}
+
+/**
+ * This is a helper function for drawRect.
+ */
+- (void)renderTileRunInRow:(int)irow col0:(int)icol0 col1:(int)icol1
+                    nsctx:(NSGraphicsContext*)nsctx ctx:(CGContextRef)ctx
+                grafWidth:(int)graf_width grafHeight:(int)graf_height
+              overdrawRow:(int)overdraw_row overdrawMax:(int)overdraw_max
+{
+    /* Save the compositing mode since it is modified below. */
+    NSCompositingOperation op = nsctx.compositingOperation;
+
+    while (icol0 < icol1) {
+       const struct TerminalCell *pcell =
+           [self.contents getCellAtColumn:icol0 row:irow];
+       NSRect destinationRect =
+           [self viewRectForCellBlockAtX:icol0 y:irow
+                 width:pcell->hscl height:pcell->vscl];
+       NSRect fgdRect = NSMakeRect(
+           graf_width * (pcell->v.ti.fgdCol +
+                         pcell->hoff_n / (1.0 * pcell->hoff_d)),
+           graf_height * (pcell->v.ti.fgdRow +
+                          pcell->voff_n / (1.0 * pcell->voff_d)),
+           graf_width * pcell->hscl / (1.0 * pcell->hoff_d),
+           graf_height * pcell->vscl / (1.0 * pcell->voff_d));
+       NSRect bckRect = NSMakeRect(
+           graf_width * (pcell->v.ti.bckCol +
+                         pcell->hoff_n / (1.0 * pcell->hoff_d)),
+           graf_height * (pcell->v.ti.bckRow +
+                          pcell->voff_n / (1.0 * pcell->voff_d)),
+           graf_width * pcell->hscl / (1.0 * pcell->hoff_d),
+           graf_height * pcell->vscl / (1.0 * pcell->voff_d));
+       int dbl_height_bck = overdraw_row &&
+           irow >= self.firstTileRow + pcell->hoff_d &&
+           (pcell->v.ti.bckRow >= overdraw_row &&
+            pcell->v.ti.bckRow <= overdraw_max);
+       int dbl_height_fgd = overdraw_row &&
+           irow >= self.firstTileRow + pcell->hoff_d &&
+           (pcell->v.ti.fgdRow >= overdraw_row) &&
+           (pcell->v.ti.fgdRow <= overdraw_max);
+       int aligned_row = 0, aligned_col = 0;
+       int is_first_piece = 0, simple_upper = 0;
+
+       /* Initialize stuff for handling a double-height tile. */
+       if (dbl_height_bck || dbl_height_fgd) {
+           aligned_col = ((icol0 - self.firstTileCol) / pcell->hoff_d) *
+                   pcell->hoff_d + self.firstTileCol;
+           aligned_row = ((irow - self.firstTileRow) / pcell->voff_d) *
+                   pcell->voff_d + self.firstTileRow;
+
+           /*
+            * If the lower half has been broken into multiple pieces, only
+            * do the work of rendering whatever is necessary for the upper
+            * half when drawing the first piece (the one closest to the
+            * upper left corner).
+            */
+           struct TerminalCellLocation curs = { 0, 0 };
+
+           [self.contents scanForTypeMaskInBlockAtColumn:aligned_col
+                row:aligned_row width:pcell->hoff_d height:pcell->voff_d
+                mask:TERM_CELL_TILE cursor:&curs];
+           if (curs.col + aligned_col == icol0 &&
+               curs.row + aligned_row == irow) {
+               is_first_piece = 1;
+
+               /*
+                * Hack:  lookup the previous row to determine how much of the
+                * tile there is shown to apply it the upper half of the
+                * double-height tile.  That will do the right thing if there
+                * is a menu displayed in that row but isn't right if there's
+                * an object/creature/feature there that doesn't have a
+                * mapping to the tile set and is rendered with a character.
+                */
+               curs.col = 0;
+               curs.row = 0;
+               [self.contents scanForTypeMaskInBlockAtColumn:aligned_col
+                    row:(aligned_row - pcell->voff_d) width:pcell->hoff_d
+                    height:pcell->voff_d mask:TERM_CELL_TILE cursor:&curs];
+               if (curs.col == 0 && curs.row == 0) {
+                   const struct TerminalCell *pcell2 =
+                       [self.contents
+                            getCellAtColumn:(aligned_col + curs.col)
+                            row:(aligned_row + curs.row - pcell->voff_d)];
+
+                   if (pcell2->hscl == pcell2->hoff_d &&
+                       pcell2->vscl == pcell2->voff_d) {
+                       /*
+                        * The tile in the previous row hasn't been clipped
+                        * or partially overwritten.  Use a streamlined
+                        * rendering procedure.
+                        */
+                       simple_upper = 1;
+                   }
+               }
+           }
+       }
+
+       /*
+        * Draw the background.  For a double-height tile, this is only the
+        * the lower half.
+        */
+       draw_image_tile(
+           nsctx, ctx, pict_image, bckRect, destinationRect,
+           NSCompositingOperationCopy);
+       if (dbl_height_bck && is_first_piece) {
+           /* Combine upper half with previously drawn row. */
+           if (simple_upper) {
+               const struct TerminalCell *pcell2 =
+                   [self.contents getCellAtColumn:aligned_col
+                        row:(aligned_row - pcell->voff_d)];
+               NSRect drect2 =
+                   [self viewRectForCellBlockAtX:aligned_col
+                         y:(aligned_row - pcell->voff_d)
+                         width:pcell2->hscl height:pcell2->vscl];
+               NSRect brect2 = NSMakeRect(
+                   graf_width * pcell->v.ti.bckCol,
+                   graf_height * (pcell->v.ti.bckRow - 1),
+                   graf_width, graf_height);
+
+               draw_image_tile(nsctx, ctx, pict_image, brect2, drect2,
+                               NSCompositingOperationSourceOver);
+           } else {
+               struct TerminalCellLocation curs = { 0, 0 };
+
+               [self.contents scanForTypeMaskInBlockAtColumn:aligned_col
+                    row:(aligned_row - pcell->voff_d) width:pcell->hoff_d
+                    height:pcell->voff_d mask:TERM_CELL_TILE
+                    cursor:&curs];
+               while (curs.col < pcell->hoff_d &&
+                      curs.row < pcell->voff_d) {
+                   const struct TerminalCell *pcell2 =
+                       [self.contents getCellAtColumn:(aligned_col + curs.col)
+                            row:(aligned_row + curs.row - pcell->voff_d)];
+                   NSRect drect2 =
+                       [self viewRectForCellBlockAtX:(aligned_col + curs.col)
+                             y:(aligned_row + curs.row - pcell->voff_d)
+                             width:pcell2->hscl height:pcell2->vscl];
+                   /*
+                    * Column and row in the tile set are from the
+                    * double-height tile at *pcell, but the offsets within
+                    * that and size are from what's visible for *pcell2.
+                    */
+                   NSRect brect2 = NSMakeRect(
+                       graf_width * (pcell->v.ti.bckCol +
+                                     pcell2->hoff_n / (1.0 * pcell2->hoff_d)),
+                       graf_height * (pcell->v.ti.bckRow - 1 +
+                                      pcell2->voff_n /
+                                      (1.0 * pcell2->voff_d)),
+                       graf_width * pcell2->hscl / (1.0 * pcell2->hoff_d),
+                       graf_height * pcell2->vscl / (1.0 * pcell2->voff_d));
+
+                   draw_image_tile(nsctx, ctx, pict_image, brect2, drect2,
+                                   NSCompositingOperationSourceOver);
+                   curs.col += pcell2->hscl;
+                   [self.contents
+                        scanForTypeMaskInBlockAtColumn:aligned_col
+                        row:(aligned_row - pcell->voff_d)
+                        width:pcell->hoff_d height:pcell->voff_d
+                        mask:TERM_CELL_TILE cursor:&curs];
+               }
+           }
+       }
+
+       /* Skip drawing the foreground if it is the same as the background. */
+       if (fgdRect.origin.x != bckRect.origin.x ||
+           fgdRect.origin.y != bckRect.origin.y) {
+           if (is_first_piece && dbl_height_fgd) {
+               if (simple_upper) {
+                   if (pcell->hoff_n == 0 && pcell->voff_n == 0 &&
+                       pcell->hscl == pcell->hoff_d) {
+                       /*
+                        * Render upper and lower parts as one since they
+                        * are contiguous.
+                        */
+                       fgdRect.origin.y -= graf_height;
+                       fgdRect.size.height += graf_height;
+                       destinationRect.origin.y -=
+                           destinationRect.size.height;
+                       destinationRect.size.height +=
+                           destinationRect.size.height;
+                   } else {
+                       /* Not contiguous.  Render the upper half. */
+                       NSRect drect2 =
+                           [self viewRectForCellBlockAtX:aligned_col
+                                 y:(aligned_row - pcell->voff_d)
+                                 width:pcell->hoff_d height:pcell->voff_d];
+                       NSRect frect2 = NSMakeRect(
+                           graf_width * pcell->v.ti.fgdCol,
+                           graf_height * (pcell->v.ti.fgdRow - 1),
+                           graf_width, graf_height);
+
+                       draw_image_tile(
+                           nsctx, ctx, pict_image, frect2, drect2,
+                           NSCompositingOperationSourceOver);
+                   }
+               } else {
+                   /* Render the upper half pieces. */
+                   struct TerminalCellLocation curs = { 0, 0 };
+
+                   while (1) {
+                       [self.contents
+                            scanForTypeMaskInBlockAtColumn:aligned_col
+                            row:(aligned_row - pcell->voff_d)
+                            width:pcell->hoff_d height:pcell->voff_d
+                            mask:TERM_CELL_TILE cursor:&curs];
+
+                       if (curs.col >= pcell->hoff_d ||
+                           curs.row >= pcell->voff_d) {
+                           break;
+                       }
+
+                       const struct TerminalCell *pcell2 =
+                           [self.contents
+                                getCellAtColumn:(aligned_col + curs.col)
+                                row:(aligned_row + curs.row - pcell->voff_d)];
+                       NSRect drect2 =
+                           [self viewRectForCellBlockAtX:(aligned_col + curs.col)
+                                 y:(aligned_row + curs.row - pcell->voff_d)
+                                 width:pcell2->hscl height:pcell2->vscl];
+                       NSRect frect2 = NSMakeRect(
+                           graf_width * (pcell->v.ti.fgdCol +
+                                         pcell2->hoff_n /
+                                         (1.0 * pcell2->hoff_d)),
+                           graf_height * (pcell->v.ti.fgdRow - 1 +
+                                          pcell2->voff_n /
+                                          (1.0 * pcell2->voff_d)),
+                           graf_width * pcell2->hscl / (1.0 * pcell2->hoff_d),
+                           graf_height * pcell2->vscl /
+                               (1.0 * pcell2->voff_d));
+
+                       draw_image_tile(nsctx, ctx, pict_image, frect2, drect2,
+                                       NSCompositingOperationSourceOver);
+                       curs.col += pcell2->hscl;
+                   }
+               }
+           }
+           /*
+            * Render the foreground (if a double height tile and the bottom
+            * part is contiguous with the upper part this also render the
+            * upper part.
+            */
+           draw_image_tile(
+               nsctx, ctx, pict_image, fgdRect, destinationRect,
+               NSCompositingOperationSourceOver);
+       }
+       icol0 = [self.contents scanForTypeMaskInRow:irow mask:TERM_CELL_TILE
+                    col0:(icol0+pcell->hscl) col1:icol1];
+    }
+
+    /* Restore the compositing mode. */
+    nsctx.compositingOperation = op;
+}
+
+/**
+ * This is what our views call to get us to draw to the window
+ */
+- (void)drawRect:(NSRect)rect inView:(NSView *)view
+{
+    /*
+     * Take this opportunity to throttle so we don't flush faster than desired.
+     */
+    [self throttle];
+
+    CGFloat bottomY =
+       self.borderSize.height + self.tileSize.height * self.rows;
+    CGFloat rightX =
+       self.borderSize.width + self.tileSize.width * self.cols;
+
+    const NSRect *invalidRects;
+    NSInteger invalidCount;
+    [view getRectsBeingDrawn:&invalidRects count:&invalidCount];
+
+    /*
+     * If the non-border areas need rendering, set some things up so they can
+     * be reused for each invalid rectangle.
+     */
+    NSGraphicsContext *nsctx = nil;
+    CGContextRef ctx = 0;
+    NSFont* screenFont = nil;
+    int graf_width = 0, graf_height = 0;
+    int overdraw_row = 0, overdraw_max = 0;
+    wchar_t blank = 0;
+    if (rect.origin.x < rightX &&
+       rect.origin.x + rect.size.width > self.borderSize.width &&
+       rect.origin.y < bottomY &&
+       rect.origin.y + rect.size.height > self.borderSize.height) {
+       nsctx = [NSGraphicsContext currentContext];
+       ctx = (CGContextRef) [nsctx CGContext];
+       screenFont = [self.angbandViewFont screenFont];
+       [screenFont set];
+       blank = [TerminalContents getBlankChar];
+       if (use_graphics) {
+           graf_width = current_graphics_mode->cell_width;
+           graf_height = current_graphics_mode->cell_height;
+           overdraw_row = current_graphics_mode->overdrawRow;
+           overdraw_max = current_graphics_mode->overdrawMax;
+       }
+    }
+
+    /*
+     * With double height tiles, need to have rendered prior rows (i.e.
+     * smaller y) before the current one.  Since the invalid rectanges are
+     * processed in order, ensure that by sorting the invalid rectangles in
+     * increasing order of y origin (AppKit guarantees the invalid rectanges
+     * are non-overlapping).
+     */
+    NSRect* sortedRects = 0;
+    const NSRect* workingRects;
+    if (overdraw_row && invalidCount > 1) {
+       sortedRects = (NSRect*) malloc(invalidCount * sizeof(NSRect));
+       if (sortedRects == 0) {
+           NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
+                                           reason:@"sorted rects in drawRect"
+                                           userInfo:nil];
+           @throw exc;
+       }
+       (void) memcpy(
+           sortedRects, invalidRects, invalidCount * sizeof(NSRect));
+       qsort(sortedRects, invalidCount, sizeof(NSRect),
+             compare_nsrect_yorigin_greater);
+       workingRects = sortedRects;
+    } else {
+       workingRects = invalidRects;
+    }
+
+    /*
+     * Use -2 for unknown.  Use -1 for Cocoa's blackColor.  All others are the
+     * Angband color index.
+     */
+    int alast = -2;
+    int redrawCursor = 0;
+
+    for (NSInteger irect = 0; irect < invalidCount; ++irect) {
+       NSRect modRect, clearRect;
+       CGFloat edge;
+       int iRowFirst, iRowLast;
+       int iColFirst, iColLast;
+
+       /* Handle the top border. */
+       if (workingRects[irect].origin.y < self.borderSize.height) {
+           edge =
+               workingRects[irect].origin.y + workingRects[irect].size.height;
+           if (edge <= self.borderSize.height) {
+               if (alast != -1) {
+                   [[NSColor blackColor] set];
+                   alast = -1;
+               }
+               NSRectFill(workingRects[irect]);
+               continue;
+           }
+           clearRect = workingRects[irect];
+           clearRect.size.height =
+               self.borderSize.height - workingRects[irect].origin.y;
+           if (alast != -1) {
+               [[NSColor blackColor] set];
+               alast = -1;
+           }
+           NSRectFill(clearRect);
+           modRect.origin.x = workingRects[irect].origin.x;
+           modRect.origin.y = self.borderSize.height;
+           modRect.size.width = workingRects[irect].size.width;
+           modRect.size.height = edge - self.borderSize.height;
+       } else {
+           modRect = workingRects[irect];
+       }
+
+       /* Handle the left border. */
+       if (modRect.origin.x < self.borderSize.width) {
+           edge = modRect.origin.x + modRect.size.width;
+           if (edge <= self.borderSize.width) {
+               if (alast != -1) {
+                   alast = -1;
+                   [[NSColor blackColor] set];
+               }
+               NSRectFill(modRect);
+               continue;
+           }
+           clearRect = modRect;
+           clearRect.size.width = self.borderSize.width - clearRect.origin.x;
+           if (alast != -1) {
+               alast = -1;
+               [[NSColor blackColor] set];
+           }
+           NSRectFill(clearRect);
+           modRect.origin.x = self.borderSize.width;
+           modRect.size.width = edge - self.borderSize.width;
+       }
+
+       iRowFirst = floor((modRect.origin.y - self.borderSize.height) /
+                         self.tileSize.height);
+       iColFirst = floor((modRect.origin.x - self.borderSize.width) /
+                         self.tileSize.width);
+       edge = modRect.origin.y + modRect.size.height;
+       if (edge <= bottomY) {
+           iRowLast =
+               ceil((edge - self.borderSize.height) / self.tileSize.height);
+       } else {
+           iRowLast = self.rows;
+       }
+       edge = modRect.origin.x + modRect.size.width;
+       if (edge <= rightX) {
+           iColLast =
+               ceil((edge - self.borderSize.width) / self.tileSize.width);
+       } else {
+           iColLast = self.cols;
+       }
+
+       if (self.contents.cursorColumn != -1 &&
+           self.contents.cursorRow != -1 &&
+           self.contents.cursorColumn + self.contents.cursorWidth - 1 >=
+           iColFirst &&
+           self.contents.cursorColumn < iColLast &&
+           self.contents.cursorRow + self.contents.cursorHeight - 1 >=
+           iRowFirst &&
+           self.contents.cursorRow < iRowLast) {
+           redrawCursor = 1;
+       }
+
+       for (int irow = iRowFirst; irow < iRowLast; ++irow) {
+           int icol =
+               [self.contents scanForTypeMaskInRow:irow
+                    mask:(TERM_CELL_CHAR | TERM_CELL_TILE)
+                    col0:iColFirst col1:iColLast];
+
+           while (1) {
+               if (icol >= iColLast) {
+                   break;
+               }
+
+               if ([self.contents getCellAtColumn:icol row:irow]->form ==
+                   TERM_CELL_TILE) {
+                   /*
+                    * It is a tile.  Identify how far the run of tiles goes.
+                    */
+                   int jcol = [self.contents scanForPredicateInRow:irow
+                                   predicate:isTileTop desired:1
+                                   col0:(icol + 1) col1:iColLast];
+
+                   [self renderTileRunInRow:irow col0:icol col1:jcol
+                         nsctx:nsctx ctx:ctx
+                         grafWidth:graf_width grafHeight:graf_height
+                         overdrawRow:overdraw_row overdrawMax:overdraw_max];
+                   icol = jcol;
+               } else {
+                   /*
+                    * It is a character.  Identify how far the run of
+                    * characters goes.
+                    */
+                   int jcol = [self.contents scanForPredicateInRow:irow
+                                   predicate:isCharNoPartial desired:1
+                                   col0:(icol + 1) col1:iColLast];
+                   int jcol2;
+
+                   if (jcol < iColLast &&
+                       isPartiallyOverwrittenBigChar(
+                           [self.contents getCellAtColumn:jcol row:irow])) {
+                       jcol2 = [self.contents scanForTypeMaskInRow:irow
+                                    mask:~TERM_CELL_CHAR_PADDING
+                                    col0:(jcol + 1) col1:iColLast];
+                   } else {
+                       jcol2 = jcol;
+                   }
+
+                   /*
+                    * Set up clipping rectangle for text.  Save the
+                    * graphics context so the clipping rectangle can be
+                    * forgotten.  Use CGContextBeginPath to clear the current
+                    * path so it does not affect clipping.  Do not call
+                    * CGContextSetTextDrawingMode() to include clipping since
+                    * that does not appear to necessary on 10.14 and is
+                    * actually detrimental:  when displaying more than one
+                    * character, only the first is visible.
+                    */
+                   CGContextSaveGState(ctx);
+                   CGContextBeginPath(ctx);
+                   NSRect r = [self viewRectForCellBlockAtX:icol y:irow
+                                    width:(jcol2 - icol) height:1];
+                   CGContextClipToRect(ctx, r);
+
+                   /*
+                    * See if the region to be rendered needs to be expanded:
+                    * adjacent text that could influence what's in the clipped
+                    * region.
+                    */
+                   int isrch = icol;
+                   int irng = icol - self.nColPost;
+                   if (irng < 1) {
+                       irng = 1;
+                   }
+
+                   while (1) {
+                       if (isrch <= irng) {
+                           break;
+                       }
+
+                       const struct TerminalCell *pcell2 =
+                           [self.contents getCellAtColumn:(isrch - 1)
+                                row:irow];
+                       if (pcell2->form == TERM_CELL_CHAR) {
+                           --isrch;
+                           if (pcell2->v.ch.glyph != blank) {
+                               icol = isrch;
+                           }
+                       } else if (pcell2->form == TERM_CELL_CHAR_PADDING) {
+                           /*
+                            * Only extend the rendering if this is padding
+                            * for a character that hasn't been partially
+                            * overwritten.
+                            */
+                           if (! isPartiallyOverwrittenBigChar(pcell2)) {
+                               if (isrch - pcell2->v.pd.hoff >= 0) {
+                                   const struct TerminalCell* pcell3 =
+                                       [self.contents
+                                            getCellAtColumn:(isrch - pcell2->v.pd.hoff)
+                                            row:irow];
+
+                                   if (pcell3->v.ch.glyph != blank) {
+                                       icol = isrch - pcell2->v.pd.hoff;
+                                       isrch = icol - 1;
+                                   } else {
+                                       isrch = isrch - pcell2->v.pd.hoff - 1;
+                                   }
+                               } else {
+                                   /* Should not happen, corrupt offset. */
+                                   --isrch;
+                               }
+                           } else {
+                               break;
+                           }
+                       } else {
+                           /*
+                            * Tiles or tile padding block anything before
+                            * them from rendering after them.
+                            */
+                           break;
+                       }
+                   }
+
+                   isrch = jcol2;
+                   irng = jcol2 + self.nColPre;
+                   if (irng > self.cols) {
+                       irng = self.cols;
+                   }
+                   while (1) {
+                       if (isrch >= irng) {
+                           break;
+                       }
+
+                       const struct TerminalCell *pcell2 =
+                           [self.contents getCellAtColumn:isrch row:irow];
+                       if (pcell2->form == TERM_CELL_CHAR) {
+                           if (pcell2->v.ch.glyph != blank) {
+                               jcol2 = isrch;
+                           }
+                           ++isrch;
+                       } else if (pcell2->form == TERM_CELL_CHAR_PADDING) {
+                           ++isrch;
+                       } else {
+                           break;
+                       }
+                   }
+
+                   /* Render text. */
+                   /* Clear where rendering will be done. */
+                   if (alast != -1) {
+                       [[NSColor blackColor] set];
+                       alast = -1;
+                   }
+                   r = [self viewRectForCellBlockAtX:icol y:irow
+                             width:(jcol - icol) height:1];
+                   NSRectFill(r);
+
+                   while (icol < jcol) {
+                       const struct TerminalCell *pcell =
+                           [self.contents getCellAtColumn:icol row:irow];
+
+                       /*
+                        * For blanks, clearing was all that was necessary.
+                        * Don't redraw them.
+                        */
+                       if (pcell->v.ch.glyph != blank) {
+                           int a = pcell->v.ch.attr % MAX_COLORS;
+
+                           if (alast != a) {
+                               alast = a;
+                               set_color_for_index(a);
+                           }
+                           r = [self viewRectForCellBlockAtX:icol
+                                     y:irow width:pcell->hscl
+                                     height:1];
+                           [self drawWChar:pcell->v.ch.glyph inRect:r
+                                 screenFont:screenFont context:ctx];
+                       }
+                       icol += pcell->hscl;
+                   }
+
+                   /*
+                    * Forget the clipping rectangle.  As a side effect, lose
+                    * the color.
+                    */
+                   CGContextRestoreGState(ctx);
+                   alast = -2;
+               }
+               icol =
+                   [self.contents scanForTypeMaskInRow:irow
+                        mask:(TERM_CELL_CHAR | TERM_CELL_TILE)
+                        col0:icol col1:iColLast];
+           }
+       }
+
+       /* Handle the right border. */
+       edge = modRect.origin.x + modRect.size.width;
+       if (edge > rightX) {
+           if (modRect.origin.x >= rightX) {
+               if (alast != -1) {
+                   alast = -1;
+                   [[NSColor blackColor] set];
+               }
+               NSRectFill(modRect);
+               continue;
+           }
+           clearRect = modRect;
+           clearRect.origin.x = rightX;
+           clearRect.size.width = edge - rightX;
+           if (alast != -1) {
+               alast = -1;
+               [[NSColor blackColor] set];
+           }
+           NSRectFill(clearRect);
+           modRect.size.width = edge - modRect.origin.x;
+       }
+
+       /* Handle the bottom border. */
+       edge = modRect.origin.y + modRect.size.height;
+       if (edge > bottomY) {
+           if (modRect.origin.y < bottomY) {
+               modRect.origin.y = bottomY;
+               modRect.size.height = edge - bottomY;
+           }
+           if (alast != -1) {
+               alast = -1;
+               [[NSColor blackColor] set];
+           }
+           NSRectFill(modRect);
+       }
+    }
+
+    if (redrawCursor) {
+       NSRect r = [self viewRectForCellBlockAtX:self.contents.cursorColumn
+                        y:self.contents.cursorRow
+                        width:self.contents.cursorWidth
+                        height:self.contents.cursorHeight];
+       [[NSColor yellowColor] set];
+       NSFrameRectWithWidth(r, 1);
+    }
+
+    free(sortedRects);
+}
+
+- (BOOL)isOrderedIn
+{
+    return [[self->angbandView window] isVisible];
+}
+
+- (BOOL)isMainWindow
+{
+    return [[self->angbandView window] isMainWindow];
+}
+
+- (BOOL)isKeyWindow
+{
+    return [[self->angbandView window] isKeyWindow];
+}
+
+- (void)setNeedsDisplay:(BOOL)val
+{
+    [self->angbandView setNeedsDisplay:val];
+}
+
+- (void)setNeedsDisplayInRect:(NSRect)rect
+{
+    [self->angbandView setNeedsDisplayInRect:rect];
+}
+
+- (void)displayIfNeeded
+{
+    [self->angbandView displayIfNeeded];
+}
+
+- (int)terminalIndex
+{
+       int termIndex = 0;
+
+       for( termIndex = 0; termIndex < ANGBAND_TERM_MAX; termIndex++ )
+       {
+               if( angband_terms[termIndex] == self->terminal )
+               {
+                       break;
+               }
+       }
+
+       return termIndex;
+}
+
+- (void)resizeTerminalWithContentRect: (NSRect)contentRect saveToDefaults: (BOOL)saveToDefaults
+{
+    CGFloat newRows = floor(
+       (contentRect.size.height - (self.borderSize.height * 2.0)) /
+       self.tileSize.height);
+    CGFloat newColumns = floor(
+       (contentRect.size.width - (self.borderSize.width * 2.0)) /
+       self.tileSize.width);
+
+    if (newRows < 1 || newColumns < 1) return;
+    [self resizeWithColumns:newColumns rows:newRows];
+
+    int termIndex = [self terminalIndex];
+    [self setDefaultTitle:termIndex];
+
+    if( saveToDefaults )
+    {
+        NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
+
+        if( termIndex < (int)[terminals count] )
+        {
+            NSMutableDictionary *mutableTerm = [[NSMutableDictionary alloc] initWithDictionary: [terminals objectAtIndex: termIndex]];
+            [mutableTerm setValue: [NSNumber numberWithInteger: self.cols]
+                        forKey: AngbandTerminalColumnsDefaultsKey];
+            [mutableTerm setValue: [NSNumber numberWithInteger: self.rows]
+                        forKey: AngbandTerminalRowsDefaultsKey];
+
+            NSMutableArray *mutableTerminals = [[NSMutableArray alloc] initWithArray: terminals];
+            [mutableTerminals replaceObjectAtIndex: termIndex withObject: mutableTerm];
+
+            [[NSUserDefaults standardUserDefaults] setValue: mutableTerminals forKey: AngbandTerminalsDefaultsKey];
+        }
+    }
+
+    term_type *old = game_term;
+    term_activate( self->terminal );
+    term_resize( self.cols, self.rows );
+    term_redraw();
+    term_activate( old );
+}
+
+- (void)constrainWindowSize:(int)termIdx
+{
+    NSSize minsize;
+
+    if (termIdx == 0) {
+       minsize.width = MAIN_TERM_MIN_COLS;
+       minsize.height = MAIN_TERM_MIN_ROWS;
+    } else {
+       minsize.width = 1;
+       minsize.height = 1;
+    }
+    minsize.width =
+       minsize.width * self.tileSize.width + self.borderSize.width * 2.0;
+    minsize.height =
+        minsize.height * self.tileSize.height + self.borderSize.height * 2.0;
+    [[self makePrimaryWindow] setContentMinSize:minsize];
+    self.primaryWindow.contentResizeIncrements = self.tileSize;
+}
+
+- (void)saveWindowVisibleToDefaults: (BOOL)windowVisible
+{
+       int termIndex = [self terminalIndex];
+       BOOL safeVisibility = (termIndex == 0) ? YES : windowVisible; /* Ensure main term doesn't go away because of these defaults */
+       NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
+
+       if( termIndex < (int)[terminals count] )
+       {
+               NSMutableDictionary *mutableTerm = [[NSMutableDictionary alloc] initWithDictionary: [terminals objectAtIndex: termIndex]];
+               [mutableTerm setValue: [NSNumber numberWithBool: safeVisibility] forKey: AngbandTerminalVisibleDefaultsKey];
+
+               NSMutableArray *mutableTerminals = [[NSMutableArray alloc] initWithArray: terminals];
+               [mutableTerminals replaceObjectAtIndex: termIndex withObject: mutableTerm];
+
+               [[NSUserDefaults standardUserDefaults] setValue: mutableTerminals forKey: AngbandTerminalsDefaultsKey];
+       }
+}
+
+- (BOOL)windowVisibleUsingDefaults
+{
+       int termIndex = [self terminalIndex];
+
+       if( termIndex == 0 )
+       {
+               return YES;
+       }
+
+       NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
+       BOOL visible = NO;
+
+       if( termIndex < (int)[terminals count] )
+       {
+               NSDictionary *term = [terminals objectAtIndex: termIndex];
+               NSNumber *visibleValue = [term valueForKey: AngbandTerminalVisibleDefaultsKey];
+
+               if( visibleValue != nil )
+               {
+                       visible = [visibleValue boolValue];
+               }
+       }
+
+       return visible;
+}
+
+#pragma mark -
+#pragma mark NSWindowDelegate Methods
+
+/*- (void)windowWillStartLiveResize: (NSNotification *)notification
+{ 
+}*/ 
+
+- (void)windowDidEndLiveResize: (NSNotification *)notification
+{
+    NSWindow *window = [notification object];
+    NSRect contentRect = [window contentRectForFrameRect: [window frame]];
+    [self resizeTerminalWithContentRect: contentRect saveToDefaults: !(self->inFullscreenTransition)];
+}
+
+/*- (NSSize)windowWillResize: (NSWindow *)sender toSize: (NSSize)frameSize
+{
+} */
+
+- (void)windowWillEnterFullScreen: (NSNotification *)notification
+{
+    self->inFullscreenTransition = YES;
+}
+
+- (void)windowDidEnterFullScreen: (NSNotification *)notification
+{
+    NSWindow *window = [notification object];
+    NSRect contentRect = [window contentRectForFrameRect: [window frame]];
+    self->inFullscreenTransition = NO;
+    [self resizeTerminalWithContentRect: contentRect saveToDefaults: NO];
+}
+
+- (void)windowWillExitFullScreen: (NSNotification *)notification
+{
+    self->inFullscreenTransition = YES;
+}
+
+- (void)windowDidExitFullScreen: (NSNotification *)notification
+{
+    NSWindow *window = [notification object];
+    NSRect contentRect = [window contentRectForFrameRect: [window frame]];
+    self->inFullscreenTransition = NO;
+    [self resizeTerminalWithContentRect: contentRect saveToDefaults: NO];
+}
+
+- (void)windowWillMiniaturize:(NSNotification *)notification
+{
+    NSWindow *window = [notification object];
+
+    if (window != self.primaryWindow
+            || (__bridge AngbandContext*) (angband_terms[0]->data) != self)
+    {
+        return;
+    }
+    [[AngbandAudioManager sharedManager] setupForInactiveApp];
+}
+
+- (void)windowDidDeminiaturize:(NSNotification *)notification
+{
+    NSWindow *window = [notification object];
+
+    if (window != self.primaryWindow
+            || (__bridge AngbandContext*) (angband_terms[0]->data) != self)
+    {
+        return;
+    }
+    [[AngbandAudioManager sharedManager] setupForActiveApp];
+}
+
+- (void)windowDidBecomeMain:(NSNotification *)notification
+{
+    NSWindow *window = [notification object];
+
+    if( window != self.primaryWindow )
+    {
+        return;
+    }
+
+    int termIndex = [self terminalIndex];
+    NSMenuItem *item = [[[NSApplication sharedApplication] windowsMenu] itemWithTag: AngbandWindowMenuItemTagBase + termIndex];
+    [item setState: NSControlStateValueOn];
+
+    if( [[NSFontPanel sharedFontPanel] isVisible] )
+    {
+        [[NSFontPanel sharedFontPanel] setPanelFont:self.angbandViewFont
+                                      isMultiple: NO];
+    }
+}
+
+- (void)windowDidResignMain: (NSNotification *)notification
+{
+    NSWindow *window = [notification object];
+
+    if( window != self.primaryWindow )
+    {
+        return;
+    }
+
+    int termIndex = [self terminalIndex];
+    NSMenuItem *item = [[[NSApplication sharedApplication] windowsMenu] itemWithTag: AngbandWindowMenuItemTagBase + termIndex];
+    [item setState: NSControlStateValueOff];
+}
+
+- (void)windowWillClose: (NSNotification *)notification
+{
+    /*
+     * If closing only because the application is terminating, don't update
+     * the visible state for when the application is relaunched.
+     */
+    if (! quit_when_ready) {
+       [self saveWindowVisibleToDefaults: NO];
+    }
+}
+
+@end
+
+
+@implementation AngbandView
+
+- (BOOL)isOpaque
+{
+    return YES;
+}
+
+- (BOOL)isFlipped
+{
+    return YES;
+}
+
+- (void)drawRect:(NSRect)rect
+{
+    if ([self inLiveResize]) {
+       /*
+        * Always anchor the cached area to the upper left corner of the view.
+        * Any parts on the right or bottom that can't be drawn from the cached
+        * area are simply cleared.  Will fill them with appropriate content
+        * when resizing is done.
+        */
+       const NSRect *rects;
+       NSInteger count;
+
+       [self getRectsBeingDrawn:&rects count:&count];
+       if (count > 0) {
+           NSRect viewRect = [self visibleRect];
+
+           [[NSColor blackColor] set];
+           while (count-- > 0) {
+               CGFloat drawTop = rects[count].origin.y - viewRect.origin.y;
+               CGFloat drawBottom = drawTop + rects[count].size.height;
+               CGFloat drawLeft = rects[count].origin.x - viewRect.origin.x;
+               CGFloat drawRight = drawLeft + rects[count].size.width;
+               /*
+                * modRect and clrRect, like rects[count], are in the view
+                * coordinates with y flipped.  cacheRect is in the bitmap
+                * coordinates and y is not flipped.
+                */
+               NSRect modRect, clrRect, cacheRect;
+
+               /*
+                * Clip by bottom edge of cached area.  Clear what's below
+                * that.
+                */
+               if (drawTop >= self->cacheBounds.size.height) {
+                   NSRectFill(rects[count]);
+                   continue;
+               }
+               modRect.origin.x = rects[count].origin.x;
+               modRect.origin.y = rects[count].origin.y;
+               modRect.size.width = rects[count].size.width;
+               cacheRect.origin.y = drawTop;
+               if (drawBottom > self->cacheBounds.size.height) {
+                   CGFloat excess =
+                       drawBottom - self->cacheBounds.size.height;
+
+                   modRect.size.height = rects[count].size.height - excess;
+                   cacheRect.origin.y = 0;
+                   clrRect.origin.x = modRect.origin.x;
+                   clrRect.origin.y = modRect.origin.y + modRect.size.height;
+                   clrRect.size.width = modRect.size.width;
+                   clrRect.size.height = excess;
+                   NSRectFill(clrRect);
+               } else {
+                   modRect.size.height = rects[count].size.height;
+                   cacheRect.origin.y = self->cacheBounds.size.height -
+                       rects[count].size.height;
+               }
+               cacheRect.size.height = modRect.size.height;
+
+               /*
+                * Clip by right edge of cached area.  Clear what's to the
+                * right of that and copy the remainder from the cache.
+                */
+               if (drawLeft >= self->cacheBounds.size.width) {
+                   NSRectFill(modRect);
+                   continue;
+               }
+               cacheRect.origin.x = drawLeft;
+               if (drawRight > self->cacheBounds.size.width) {
+                   CGFloat excess = drawRight - self->cacheBounds.size.width;
+
+                   modRect.size.width -= excess;
+                   cacheRect.size.width =
+                       self->cacheBounds.size.width - drawLeft;
+                   clrRect.origin.x = modRect.origin.x + modRect.size.width;
+                   clrRect.origin.y = modRect.origin.y;
+                   clrRect.size.width = excess;
+                   clrRect.size.height = modRect.size.height;
+                   NSRectFill(clrRect);
+               } else {
+                   cacheRect.size.width = drawRight - drawLeft;
+               }
+               [self->cacheForResize drawInRect:modRect fromRect:cacheRect
+                    operation:NSCompositingOperationCopy fraction:1.0
+                    respectFlipped:YES hints:nil];
+           }
+       }
+    } else if (! self.angbandContext) {
+        /* Draw bright orange, 'cause this ain't right */
+        [[NSColor orangeColor] set];
+        NSRectFill([self bounds]);
+    } else {
+        /* Tell the Angband context to draw into us */
+        [self.angbandContext drawRect:rect inView:self];
+    }
+}
+
+/**
+ * Override NSView's method to set up a cache that's used in drawRect to
+ * handle drawing during a resize.
+ */
+- (void)viewWillStartLiveResize
+{
+    [super viewWillStartLiveResize];
+    self->cacheBounds = [self visibleRect];
+    self->cacheForResize =
+       [self bitmapImageRepForCachingDisplayInRect:self->cacheBounds];
+    if (self->cacheForResize != nil) {
+       [self cacheDisplayInRect:self->cacheBounds
+             toBitmapImageRep:self->cacheForResize];
+    } else {
+       self->cacheBounds.size.width = 0.;
+       self->cacheBounds.size.height = 0.;
+    }
+}
+
+/**
+ * Override NSView's method to release the cache set up in
+ * viewWillStartLiveResize.
+ */
+- (void)viewDidEndLiveResize
+{
+    [super viewDidEndLiveResize];
+    self->cacheForResize = nil;
+    [self setNeedsDisplay:YES];
+}
+
+@end
+
+
+
+/**
+ * ------------------------------------------------------------------------
+ * Some generic functions
+ * ------------------------------------------------------------------------ */
+
+/**
+ * Sets an Angband color at a given index
+ */
+static void set_color_for_index(int idx)
+{
+    byte rv, gv, bv;
+    
+    /* Extract the R,G,B data */
+    rv = angband_color_table[idx][1];
+    gv = angband_color_table[idx][2];
+    bv = angband_color_table[idx][3];
+    
+    CGContextSetRGBFillColor((CGContextRef) [[NSGraphicsContext currentContext] CGContext], rv/255., gv/255., bv/255., 1.);
+}
+
+/**
+ * Remember the current character in UserDefaults so we can select it by
+ * default next time.
+ */
+static void record_current_savefile(void)
+{
+    NSString *savefileString = [[NSString stringWithCString:savefile.native().data() encoding:NSMacOSRomanStringEncoding] lastPathComponent];
+    if (savefileString)
+    {
+        NSUserDefaults *angbandDefs = [NSUserDefaults angbandDefaults];
+        [angbandDefs setObject:savefileString forKey:@"SaveFile"];
+    }
+}
+
+
+#ifdef JP
+/**
+ * Convert a two-byte EUC-JP encoded character (both *cp and (*cp + 1) are in
+ * the range, 0xA1-0xFE, or *cp is 0x8E) to a UTF-32 (as a wchar_t) value in
+ * the native byte ordering.
+ */
+static wchar_t convert_two_byte_eucjp_to_utf32_native(const char *cp)
+{
+    NSString* str = [[NSString alloc] initWithBytes:cp length:2
+                                     encoding:NSJapaneseEUCStringEncoding];
+    wchar_t result;
+    UniChar first = [str characterAtIndex:0];
+
+    if (CFStringIsSurrogateHighCharacter(first)) {
+        result = CFStringGetLongCharacterForSurrogatePair(
+            first, [str characterAtIndex:1]);
+    } else {
+        result = first;
+    }
+    str = nil;
+    return result;
+}
+#endif /* JP */
+
+
+/**
+ * ------------------------------------------------------------------------
+ * Support for the "z-term.c" package
+ * ------------------------------------------------------------------------ */
+
+
+/**
+ * Initialize a new Term
+ */
+static void Term_init_cocoa(term_type *t)
+{
+    @autoreleasepool {
+       NSUserDefaults *defs = [NSUserDefaults angbandDefaults];
+       AngbandContext *context = [[AngbandContext alloc] init];
+
+       /* Give the term ownership of the context */
+       t->data = (void *)CFBridgingRetain(context);
+
+       /* Handle graphics */
+       t->higher_pict = !! use_graphics;
+       t->always_pict = false;
+
+       /*
+        * Figure out the frame autosave name based on the index of this term
+        */
+       NSString *autosaveName = nil;
+       int termIdx;
+       for (termIdx = 0; termIdx < ANGBAND_TERM_MAX; termIdx++)
+       {
+           if (angband_terms[termIdx] == t)
+           {
+               autosaveName =
+                   [NSString stringWithFormat:@"AngbandTerm-%d", termIdx];
+               break;
+           }
+       }
+
+       /* Set its font. */
+       NSString *fontName =
+           [defs
+               stringForKey:[NSString stringWithFormat:@"FontName-%d", termIdx]];
+       if (! fontName) fontName = [[AngbandContext defaultFont] fontName];
+
+       /*
+        * Use a smaller default font for the other windows, but only if the
+        * font hasn't been explicitly set.
+        */
+       float fontSize = (termIdx > 0) ?
+            FallbackFontSizeSub : [[AngbandContext defaultFont] pointSize];
+       NSNumber *fontSizeNumber =
+           [defs
+               valueForKey: [NSString stringWithFormat: @"FontSize-%d", termIdx]];
+
+       if( fontSizeNumber != nil )
+       {
+           fontSize = [fontSizeNumber floatValue];
+       }
+
+       NSFont *newFont = [NSFont fontWithName:fontName size:fontSize];
+       if (!newFont) {
+           float fallbackSize = (termIdx > 0) ?
+               FallbackFontSizeSub : FallbackFontSizeMain;
+
+           newFont = [NSFont fontWithName:FallbackFontName size:fallbackSize];
+           if (!newFont) {
+               newFont = [NSFont systemFontOfSize:fallbackSize];
+               if (!newFont) {
+                   newFont = [NSFont systemFontOfSize:0.0];
+               }
+           }
+           /* Override the bad preferences. */
+           [defs setValue:[newFont fontName]
+               forKey:[NSString stringWithFormat:@"FontName-%d", termIdx]];
+           [defs setFloat:[newFont pointSize]
+               forKey:[NSString stringWithFormat:@"FontSize-%d", termIdx]];
+       }
+       [context setSelectionFont:newFont adjustTerminal: NO];
+
+       NSArray *terminalDefaults =
+           [[NSUserDefaults standardUserDefaults]
+               valueForKey: AngbandTerminalsDefaultsKey];
+       NSInteger rows = TERM_DEFAULT_ROWS;
+       NSInteger columns = TERM_DEFAULT_COLS;
+
+       if( termIdx < (int)[terminalDefaults count] )
+       {
+           NSDictionary *term = [terminalDefaults objectAtIndex: termIdx];
+           NSInteger defaultRows =
+               [[term valueForKey: AngbandTerminalRowsDefaultsKey]
+                   integerValue];
+           NSInteger defaultColumns =
+               [[term valueForKey: AngbandTerminalColumnsDefaultsKey]
+                   integerValue];
+
+           if (defaultRows > 0) rows = defaultRows;
+           if (defaultColumns > 0) columns = defaultColumns;
+       }
+
+       [context resizeWithColumns:columns rows:rows];
+
+       /* Get the window */
+       NSWindow *window = [context makePrimaryWindow];
+
+       /* Set its title and, for auxiliary terms, tentative size */
+       [context setDefaultTitle:termIdx];
+       [context constrainWindowSize:termIdx];
+
+       /*
+        * If this is the first term, and we support full screen (Mac OS X Lion
+        * or later), then allow it to go full screen (sweet). Allow other
+        * terms to be FullScreenAuxilliary, so they can at least show up.
+        * Unfortunately in Lion they don't get brought to the full screen
+        * space; but they would only make sense on multiple displays anyways
+        * so it's not a big loss.
+        */
+       if ([window respondsToSelector:@selector(toggleFullScreen:)])
+       {
+           NSWindowCollectionBehavior behavior = [window collectionBehavior];
+           behavior |=
+               (termIdx == 0 ?
+                NSWindowCollectionBehaviorFullScreenPrimary :
+                NSWindowCollectionBehaviorFullScreenAuxiliary);
+           [window setCollectionBehavior:behavior];
+       }
+
+       /* No Resume support yet, though it would not be hard to add */
+       if ([window respondsToSelector:@selector(setRestorable:)])
+       {
+           [window setRestorable:NO];
+       }
+
+       /* default window placement */ {
+           static NSRect overallBoundingRect;
+
+           if( termIdx == 0 )
+           {
+               /*
+                * This is a bit of a trick to allow us to display multiple
+                * windows in the "standard default" window position in OS X:
+                * the upper center of the screen.  The term sizes set in
+                * load_prefs() are based on a 5-wide by 3-high grid, with the
+                * main term being 4/5 wide by 2/3 high (hence the scaling to
+                * find what the containing rect would be).
+                */
+               NSRect originalMainTermFrame = [window frame];
+               NSRect scaledFrame = originalMainTermFrame;
+               scaledFrame.size.width *= 5.0 / 4.0;
+               scaledFrame.size.height *= 3.0 / 2.0;
+               scaledFrame.size.width += 1.0; /* spacing between window columns */
+               scaledFrame.size.height += 1.0; /* spacing between window rows */
+               [window setFrame: scaledFrame  display: NO];
+               [window center];
+               overallBoundingRect = [window frame];
+               [window setFrame: originalMainTermFrame display: NO];
+           }
+
+           static NSRect mainTermBaseRect;
+           NSRect windowFrame = [window frame];
+
+           if( termIdx == 0 )
+           {
+               /*
+                * The height and width adjustments were determined
+                * experimentally, so that the rest of the windows line up
+                * nicely without overlapping.
+                */
+               windowFrame.size.width += 7.0;
+               windowFrame.size.height += 9.0;
+               windowFrame.origin.x = NSMinX( overallBoundingRect );
+               windowFrame.origin.y =
+                   NSMaxY( overallBoundingRect ) - NSHeight( windowFrame );
+               mainTermBaseRect = windowFrame;
+           }
+           else if( termIdx == 1 )
+           {
+               windowFrame.origin.x = NSMinX( mainTermBaseRect );
+               windowFrame.origin.y =
+                   NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
+           }
+           else if( termIdx == 2 )
+           {
+               windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
+               windowFrame.origin.y =
+                   NSMaxY( mainTermBaseRect ) - NSHeight( windowFrame );
+           }
+           else if( termIdx == 3 )
+           {
+               windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
+               windowFrame.origin.y =
+                   NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
+           }
+           else if( termIdx == 4 )
+           {
+               windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
+               windowFrame.origin.y = NSMinY( mainTermBaseRect );
+           }
+           else if( termIdx == 5 )
+           {
+               windowFrame.origin.x =
+                   NSMinX( mainTermBaseRect ) + NSWidth( windowFrame ) + 1.0;
+               windowFrame.origin.y =
+                   NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
+           }
+
+           [window setFrame: windowFrame display: NO];
+       }
+
+       /*
+        * Override the default frame above if the user has adjusted windows in
+        * the past
+        */
+       if (autosaveName) [window setFrameAutosaveName:autosaveName];
+
+       /*
+        * Tell it about its term. Do this after we've sized it so that the
+        * sizing doesn't trigger redrawing and such.
+        */
+       [context setTerm:t];
+
+       /*
+        * Only order front if it's the first term. Other terms will be ordered
+        * front from AngbandUpdateWindowVisibility(). This is to work around a
+        * problem where Angband aggressively tells us to initialize terms that
+        * don't do anything!
+        */
+       if (t == angband_terms[0])
+           [context.primaryWindow makeKeyAndOrderFront: nil];
+
+       /* Set "mapped" flag */
+       t->mapped_flag = true;
+    }
+}
+
+
+
+/**
+ * Nuke an old Term
+ */
+static void Term_nuke_cocoa(term_type *t)
+{
+    @autoreleasepool {
+        AngbandContext *context = (__bridge AngbandContext*) (t->data);
+       if (context)
+       {
+           /* Tell the context to get rid of its windows, etc. */
+           [context dispose];
+
+           /* Balance our CFBridgingRetain from when we created it */
+           CFRelease(t->data);
+
+           /* Done with it */
+           t->data = NULL;
+       }
+    }
+}
+
+/**
+ * Returns the CGImageRef corresponding to an image with the given path.
+ * Transfers ownership to the caller.
+ */
+static CGImageRef create_angband_image(NSString *path)
+{
+    CGImageRef decodedImage = NULL, result = NULL;
+    
+    /* Try using ImageIO to load the image */
+    if (path)
+    {
+        NSURL *url = [[NSURL alloc] initFileURLWithPath:path isDirectory:NO];
+        if (url)
+        {
+            NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:(id)kCFBooleanTrue, kCGImageSourceShouldCache, nil];
+            CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, (CFDictionaryRef)options);
+            if (source)
+            {
+                /*
+                 * We really want the largest image, but in practice there's
+                 * only going to be one
+                 */
+                decodedImage = CGImageSourceCreateImageAtIndex(source, 0, (CFDictionaryRef)options);
+                CFRelease(source);
+            }
+        }
+    }
+    
+    /*
+     * Draw the sucker to defeat ImageIO's weird desire to cache and decode on
+     * demand. Our images aren't that big!
+     */
+    if (decodedImage)
+    {
+        size_t width = CGImageGetWidth(decodedImage), height = CGImageGetHeight(decodedImage);
+        
+        /* Compute our own bitmap info */
+        CGBitmapInfo imageBitmapInfo = CGImageGetBitmapInfo(decodedImage);
+        CGBitmapInfo contextBitmapInfo = kCGBitmapByteOrderDefault;
+        
+        switch (imageBitmapInfo & kCGBitmapAlphaInfoMask) {
+            case kCGImageAlphaNone:
+            case kCGImageAlphaNoneSkipLast:
+            case kCGImageAlphaNoneSkipFirst:
+                /* No alpha */
+                contextBitmapInfo |= kCGImageAlphaNone;
+                break;
+            default:
+                /* Some alpha, use premultiplied last which is most efficient. */
+                contextBitmapInfo |= kCGImageAlphaPremultipliedLast;
+                break;
+        }
+
+        /* Draw the source image flipped, since the view is flipped */
+        CGContextRef ctx = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(decodedImage), CGImageGetBytesPerRow(decodedImage), CGImageGetColorSpace(decodedImage), contextBitmapInfo);
+       if (ctx) {
+           CGContextSetBlendMode(ctx, kCGBlendModeCopy);
+           CGContextTranslateCTM(ctx, 0.0, height);
+           CGContextScaleCTM(ctx, 1.0, -1.0);
+           CGContextDrawImage(
+               ctx, CGRectMake(0, 0, width, height), decodedImage);
+           result = CGBitmapContextCreateImage(ctx);
+           CFRelease(ctx);
+       }
+
+        CGImageRelease(decodedImage);
+    }
+    return result;
+}
+
+/**
+ * React to changes
+ */
+static errr Term_xtra_cocoa_react(void)
+{
+    /* Don't actually switch graphics until the game is running */
+    if (!initialized || !game_in_progress) return (-1);
+
+    @autoreleasepool {
+       /* Handle graphics */
+       int expected_graf_mode = (current_graphics_mode) ?
+           current_graphics_mode->grafID : GRAPHICS_NONE;
+       if (graf_mode_req != expected_graf_mode)
+       {
+           graphics_mode *new_mode;
+           if (graf_mode_req != GRAPHICS_NONE) {
+               new_mode = get_graphics_mode(graf_mode_req);
+           } else {
+               new_mode = NULL;
+           }
+
+           /* Get rid of the old image. CGImageRelease is NULL-safe. */
+           CGImageRelease(pict_image);
+           pict_image = NULL;
+
+           /* Try creating the image if we want one */
+           if (new_mode != NULL)
+           {
+               NSString *img_path =
+                   [NSString stringWithFormat:@"%s/%s", new_mode->path, new_mode->file];
+               pict_image = create_angband_image(img_path);
+
+               /* If we failed to create the image, revert to ASCII. */
+               if (! pict_image) {
+                   new_mode = NULL;
+                   if (use_bigtile) {
+                       arg_bigtile = false;
+                   }
+                   [[NSUserDefaults angbandDefaults]
+                       setInteger:GRAPHICS_NONE
+                       forKey:AngbandGraphicsDefaultsKey];
+
+                   NSString *msg = NSLocalizedStringWithDefaultValue(
+                       @"Error.TileSetLoadFailed",
+                       AngbandMessageCatalog,
+                       [NSBundle mainBundle],
+                       @"Failed to Load Tile Set",
+                       @"Alert text for failed tile set load");
+                   NSString *info = NSLocalizedStringWithDefaultValue(
+                       @"Error.TileSetRevertToASCII",
+                       AngbandMessageCatalog,
+                       [NSBundle mainBundle],
+                       @"Could not load the tile set.  Switched back to ASCII.",
+                       @"Alert informative message for failed tile set load");
+                   NSAlert *alert = [[NSAlert alloc] init];
+                   alert.messageText = msg;
+                   alert.informativeText = info;
+                   [alert runModal];
+               }
+           }
+
+           if (graphics_are_enabled()) {
+               /*
+                * The contents stored in the AngbandContext may have
+                * references to the old tile set.  Out of an abundance
+                * of caution, clear those references in case there's an
+                * attempt to redraw the contents before the core has the
+                * chance to update it via the text_hook, pict_hook, and
+                * wipe_hook.
+                */
+               for (int iterm = 0; iterm < ANGBAND_TERM_MAX; ++iterm) {
+                   AngbandContext* aContext =
+                       (__bridge AngbandContext*) (angband_terms[iterm]->data);
+
+                   [aContext.contents wipeTiles];
+               }
+           }
+
+           /* Record what we did */
+           use_graphics = new_mode ? new_mode->grafID : 0;
+           ANGBAND_GRAF = (new_mode ? new_mode->graf : "ascii");
+           current_graphics_mode = new_mode;
+
+           /* Enable or disable higher picts.  */
+           for (int iterm = 0; iterm < ANGBAND_TERM_MAX; ++iterm) {
+               if (angband_terms[iterm]) {
+                   angband_terms[iterm]->higher_pict = !! use_graphics;
+               }
+           }
+
+           if (pict_image && current_graphics_mode)
+           {
+               /*
+                * Compute the row and column count via the image height and
+                * width.
+                */
+               pict_rows = (int)(CGImageGetHeight(pict_image) /
+                                 current_graphics_mode->cell_height);
+               pict_cols = (int)(CGImageGetWidth(pict_image) /
+                                 current_graphics_mode->cell_width);
+           }
+           else
+           {
+               pict_rows = 0;
+               pict_cols = 0;
+           }
+
+           /* Reset visuals */
+           if (arg_bigtile == use_bigtile &&
+               w_ptr->character_generated)
+           {
+               reset_visuals(p_ptr);
+           }
+       }
+
+       if (arg_bigtile != use_bigtile) {
+           if (w_ptr->character_generated)
+           {
+               /* Reset visuals */
+               reset_visuals(p_ptr);
+           }
+
+           term_activate(angband_terms[0]);
+           term_resize(angband_terms[0]->wid, angband_terms[0]->hgt);
+       }
+    }
+
+    /* Success */
+    return (0);
+}
+
+
+/**
+ * Do a "special thing"
+ */
+static errr Term_xtra_cocoa(int n, int v)
+{
+    errr result = 0;
+    @autoreleasepool {
+       AngbandContext* angbandContext =
+           (__bridge AngbandContext*) (game_term->data);
+
+       /* Analyze */
+       switch (n) {
+           /* Make a noise */
+        case TERM_XTRA_NOISE:
+           [[AngbandAudioManager sharedManager] playBeep];
+           break;
+
+           /*  Make a sound */
+        case TERM_XTRA_SOUND:
+           [[AngbandAudioManager sharedManager] playSound:v];
+           break;
+
+            /* Start playing background music. */
+        case TERM_XTRA_MUSIC_BASIC:
+        case TERM_XTRA_MUSIC_DUNGEON:
+        case TERM_XTRA_MUSIC_QUEST:
+        case TERM_XTRA_MUSIC_TOWN:
+        case TERM_XTRA_MUSIC_MONSTER:
+           [[AngbandAudioManager sharedManager] playMusicType:n ID:v];
+           break;
+
+        case TERM_XTRA_MUSIC_MUTE:
+           [[AngbandAudioManager sharedManager] stopAllMusic];
+           break;
+
+        case TERM_XTRA_SCENE:
+           {
+               auto &list = get_scene_type_list(v);
+
+               for (auto &item : list) {
+                   if (![[AngbandAudioManager sharedManager]
+                           musicExists:item.type ID:item.val]) {
+                       continue;
+                   }
+                   [[AngbandAudioManager sharedManager]
+                       playMusicType:item.type ID:item.val];
+               }
+           }
+            break;
+
+           /* Process random events */
+        case TERM_XTRA_BORED:
+           /*
+            * Show or hide cocoa windows based on the subwindow flags set by
+            * the user.
+            */
+           AngbandUpdateWindowVisibility();
+           /* Process an event */
+           (void)check_events(CHECK_EVENTS_NO_WAIT);
+           break;
+
+           /* Process pending events */
+        case TERM_XTRA_EVENT:
+           /* Process an event */
+           (void)check_events(v);
+           break;
+
+           /* Flush all pending events (if any) */
+        case TERM_XTRA_FLUSH:
+           /* Hack -- flush all events */
+           while (check_events(CHECK_EVENTS_DRAIN)) /* loop */;
+
+           break;
+
+           /* Hack -- Change the "soft level" */
+        case TERM_XTRA_LEVEL:
+           /*
+            * Here we could activate (if requested), but I don't think
+            * Angband should be telling us our window order (the user
+            * should decide that), so do nothing.
+            */
+           break;
+
+           /* Clear the screen */
+        case TERM_XTRA_CLEAR:
+           [angbandContext.contents wipe];
+           [angbandContext setNeedsDisplay:YES];
+           break;
+
+           /* React to changes */
+        case TERM_XTRA_REACT:
+           result = Term_xtra_cocoa_react();
+           break;
+
+           /* Delay (milliseconds) */
+        case TERM_XTRA_DELAY:
+           /* If needed */
+           if (v > 0) {
+               double seconds = v / 1000.;
+               NSDate* date = [NSDate dateWithTimeIntervalSinceNow:seconds];
+               do {
+                   NSEvent* event;
+                   do {
+                       event = [NSApp nextEventMatchingMask:-1
+                                      untilDate:date
+                                      inMode:NSDefaultRunLoopMode
+                                      dequeue:YES];
+                       if (event) send_event(event);
+                   } while (event);
+               } while ([date timeIntervalSinceNow] >= 0);
+           }
+           break;
+
+           /* Draw the pending changes. */
+        case TERM_XTRA_FRESH:
+           {
+               /*
+                * Check the cursor visibility since the core will tell us
+                * explicitly to draw it, but tells us implicitly to forget it
+                * by simply telling us to redraw a location.
+                */
+               int isVisible = 0;
+
+               term_get_cursor(&isVisible);
+               if (! isVisible) {
+                   [angbandContext.contents removeCursor];
+               }
+               [angbandContext computeInvalidRects];
+               [angbandContext.changes clear];
+           }
+            break;
+
+        default:
+            /* Oops */
+            result = 1;
+            break;
+       }
+    }
+
+    return result;
+}
+
+static errr Term_curs_cocoa(TERM_LEN x, TERM_LEN y)
+{
+    AngbandContext *angbandContext =
+       (__bridge AngbandContext*) (game_term->data);
+
+    [angbandContext.contents setCursorAtColumn:x row:y width:1 height:1];
+    /*
+     * Unfortunately, this (and the same logic in Term_bigcurs_cocoa) will
+     * also trigger what's under the cursor to be redrawn as well, even if
+     * it has not changed.  In the current drawing implementation, that
+     * inefficiency seems unavoidable.
+     */
+    [angbandContext.changes markChangedAtColumn:x row:y];
+
+    /* Success */
+    return 0;
+}
+
+/**
+ * Draw a cursor that's two tiles wide.  For Japanese, that's used when
+ * the cursor points at a kanji character, irregardless of whether operating
+ * in big tile mode.
+ */
+static errr Term_bigcurs_cocoa(TERM_LEN x, TERM_LEN y)
+{
+    AngbandContext *angbandContext =
+       (__bridge AngbandContext*) (game_term->data);
+    /* Out of paranoia, coerce to remain in bounds. */
+    int w = (x + 2 <= angbandContext.cols) ? 2 : angbandContext.cols - x;
+
+    [angbandContext.contents setCursorAtColumn:x row:y width:w height:1];
+    [angbandContext.changes markChangedBlockAtColumn:x row:y width:w height:1];
+
+    /* Success */
+    return 0;
+}
+
+/**
+ * Low level graphics (Assumes valid input)
+ *
+ * Erase "n" characters starting at (x,y)
+ */
+static errr Term_wipe_cocoa(TERM_LEN x, TERM_LEN y, int n)
+{
+    AngbandContext *angbandContext =
+       (__bridge AngbandContext*) (game_term->data);
+
+    [angbandContext.contents wipeBlockAtColumn:x row:y width:n height:1];
+    [angbandContext.changes markChangedRangeAtColumn:x row:y width:n];
+
+    /* Success */
+    return 0;
+}
+
+static errr Term_pict_cocoa(TERM_LEN x, TERM_LEN y, int n,
+                           const TERM_COLOR *ap, concptr cp,
+                           const TERM_COLOR *tap, concptr tcp)
+{
+    /* Paranoia: Bail if graphics aren't enabled */
+    if (! graphics_are_enabled()) return -1;
+
+    AngbandContext* angbandContext =
+       (__bridge AngbandContext*) (game_term->data);
+    int step = (use_bigtile) ? 2 : 1;
+
+    int alphablend;
+    if (use_graphics) {
+       CGImageAlphaInfo ainfo = CGImageGetAlphaInfo(pict_image);
+
+       alphablend = (ainfo & (kCGImageAlphaPremultipliedFirst |
+                              kCGImageAlphaPremultipliedLast)) ? 1 : 0;
+    } else {
+       alphablend = 0;
+    }
+    angbandContext.firstTileRow = 0;
+    angbandContext.firstTileCol = 0;
+
+    for (int i = x; i < x + n * step; i += step) {
+       TERM_COLOR a = *ap;
+       char c = *cp;
+       TERM_COLOR ta = *tap;
+       char tc = *tcp;
+
+       ap += step;
+       cp += step;
+       tap += step;
+       tcp += step;
+       if (use_graphics && (a & 0x80) && (c & 0x80)) {
+           char fgdRow = ((byte)a & 0x7F) % pict_rows;
+           char fgdCol = ((byte)c & 0x7F) % pict_cols;
+           char bckRow, bckCol;
+
+           if (alphablend) {
+               bckRow = ((byte)ta & 0x7F) % pict_rows;
+               bckCol = ((byte)tc & 0x7F) % pict_cols;
+           } else {
+               /*
+                * Not blending so make the background the same as the
+                * the foreground.
+                */
+               bckRow = fgdRow;
+               bckCol = fgdCol;
+           }
+           [angbandContext.contents setTileAtColumn:i row:y
+                          foregroundColumn:fgdCol
+                          foregroundRow:fgdRow
+                          backgroundColumn:bckCol
+                          backgroundRow:bckRow
+                          tileWidth:step
+                          tileHeight:1];
+           [angbandContext.changes markChangedBlockAtColumn:i row:y
+                          width:step height:1];
+       }
+    }
+
+    /* Success */
+    return (0);
+}
+
+/**
+ * Low level graphics.  Assumes valid input.
+ *
+ * Draw several ("n") chars, with an attr, at a given location.
+ */
+static errr Term_text_cocoa(
+    TERM_LEN x, TERM_LEN y, int n, TERM_COLOR a, concptr cp)
+{
+    AngbandContext* angbandContext =
+       (__bridge AngbandContext*) (game_term->data);
+
+    [angbandContext.contents setUniformAttributeTextRunAtColumn:x
+                  row:y n:n glyphs:cp attribute:a];
+    [angbandContext.changes markChangedRangeAtColumn:x row:y width:n];
+
+    /* Success */
+    return 0;
+}
+
+#if 0
+/**
+ * Convert UTF-8 to UTF-32 with each UTF-32 stored in the native byte order as
+ * a wchar_t.  Return the total number of code points that would be generated
+ * by converting the UTF-8 input.
+ *
+ * \param dest Points to the buffer in which to store the conversion.  May be
+ * NULL.
+ * \param src Is a null-terminated UTF-8 sequence.
+ * \param n Is the maximum number of code points to store in dest.
+ *
+ * In case of malformed UTF-8, inserts a U+FFFD in the converted output at the
+ * point of the error.
+ */
+static size_t Term_mbcs_cocoa(wchar_t *dest, const char *src, int n)
+{
+    size_t nout = (n > 0) ? n : 0;
+    size_t count = 0;
+
+    while (1) {
+       /*
+        * Default to U+FFFD to indicate an erroneous UTF-8 sequence that
+        * could not be decoded.  Follow "best practice" recommended by the
+        * Unicode 6 standard:  an erroneous sequence ends as soon as a
+        * disallowed byte is encountered.
+         */
+       unsigned int decoded = 0xfffd;
+
+       if (((unsigned int) *src & 0x80) == 0) {
+            /* Encoded as single byte:  U+0000 to U+0007F -> 0xxxxxxx. */
+           if (*src == 0) {
+               if (dest && count < nout) {
+                    dest[count] = 0;
+               }
+               break;
+           }
+           decoded = *src;
+           ++src;
+       } else if (((unsigned int) *src & 0xe0) == 0xc0) {
+           /* Encoded as two bytes:  U+0080 to U+07FF -> 110xxxxx 10xxxxxx. */
+           unsigned int part = ((unsigned int) *src & 0x1f) << 6;
+
+           ++src;
+           /*
+            * Check that the first two bits of the continuation byte are
+            * valid and the encoding is not overlong.
+            */
+           if (((unsigned int) *src & 0xc0) == 0x80 && part > 0x40) {
+               decoded = part + ((unsigned int) *src & 0x3f);
+               ++src;
+           }
+       } else if (((unsigned int) *src & 0xf0) == 0xe0) {
+           /*
+            * Encoded as three bytes:  U+0800 to U+FFFF -> 1110xxxx 10xxxxxx
+            * 10xxxxxx.
+            */
+           unsigned int part = ((unsigned int) *src & 0xf) << 12;
+
+           ++src;
+           if (((unsigned int) *src & 0xc0) == 0x80) {
+               part += ((unsigned int) *src & 0x3f) << 6;
+               ++src;
+               /*
+                * The second part of the test rejects overlong encodings.  The
+                * third part rejects encodings of U+D800 to U+DFFF, reserved
+                * for surrogate pairs.
+                */
+               if (((unsigned int) *src & 0xc0) == 0x80 && part >= 0x800 &&
+                       (part & 0xf800) != 0xd800) {
+                   decoded = part + ((unsigned int) *src & 0x3f);
+                   ++src;
+               }
+           }
+       } else if (((unsigned int) *src & 0xf8) == 0xf0) {
+           /*
+            * Encoded as four bytes:  U+10000 to U+1FFFFF -> 11110xxx 10xxxxxx
+            * 10xxxxxx 10xxxxxx.
+            */
+           unsigned int part = ((unsigned int) *src & 0x7) << 18;
+
+           ++src;
+           if (((unsigned int) *src & 0xc0) == 0x80) {
+               part += ((unsigned int) *src & 0x3f) << 12;
+               ++src;
+               /*
+                * The second part of the test rejects overlong encodings.
+                * The third part rejects code points beyond U+10FFFF which
+                * can't be encoded in UTF-16.
+                */
+               if (((unsigned int) *src & 0xc0) == 0x80 && part >= 0x10000 &&
+                       (part & 0xff0000) <= 0x100000) {
+                   part += ((unsigned int) *src & 0x3f) << 6;
+                   ++src;
+                   if (((unsigned int) *src & 0xc0) == 0x80) {
+                       decoded = part + ((unsigned int) *src & 0x3f);
+                       ++src;
+                   }
+               }
+           }
+       } else {
+           /*
+            * Either an impossible byte or one that signals the start of a
+            * five byte or longer encoding.
+            */
+           ++src;
+       }
+       if (dest && count < nout) {
+           dest[count] = decoded;
+       }
+       ++count;
+    }
+    return count;
+}
+#endif
+
+/**
+ * Handle redrawing for a change to the tile set, tile scaling, or main window
+ * font.  Returns YES if the redrawing was initiated.  Otherwise returns NO.
+ */
+static BOOL redraw_for_tiles_or_term0_font(void)
+{
+    /*
+     * In Angband 4.2, do_cmd_redraw() will always clear, but only provides
+     * something to replace the erased content if a character has been
+     * generated.  In Hengband, do_cmd_redraw() isn't safe to call unless a
+     * character has been generated.  Therefore, only call it if a character
+     * has been generated.
+     */
+    if (w_ptr->character_generated) {
+       do_cmd_redraw(p_ptr);
+       wakeup_event_loop();
+       return YES;
+    }
+    return NO;
+}
+
+/**
+ * Post a nonsense event so that our event loop wakes up
+ */
+static void wakeup_event_loop(void)
+{
+    /* Big hack - send a nonsense event to make us update */
+    NSEvent *event = [NSEvent otherEventWithType:NSEventTypeApplicationDefined location:NSZeroPoint modifierFlags:0 timestamp:0 windowNumber:0 context:NULL subtype:AngbandEventWakeup data1:0 data2:0];
+    [NSApp postEvent:event atStart:NO];
+}
+
+
+/**
+ * Handle quit_when_ready, by Peter Ammon,
+ * slightly modified to check inkey_flag.
+ */
+static void quit_calmly(void)
+{
+    /* Quit immediately if game's not started */
+    if (!game_in_progress || !w_ptr->character_generated) quit(NULL);
+
+    /* Save the game and Quit (if it's safe) */
+    if (inkey_flag)
+    {
+        /* Hack -- Forget messages and term */
+        msg_flag = false;
+        game_term->mapped_flag = false;
+
+        /* Save the game */
+        do_cmd_save_game(p_ptr, 0);
+        record_current_savefile();
+
+        /* Quit */
+        quit(NULL);
+    }
+
+    /* Wait until inkey_flag is set */
+}
+
+
+
+/**
+ * Returns YES if we contain an AngbandView (and hence should direct our events
+ * to Angband)
+ */
+static BOOL contains_angband_view(NSView *view)
+{
+    if ([view isKindOfClass:[AngbandView class]]) return YES;
+    for (NSView *subview in [view subviews]) {
+        if (contains_angband_view(subview)) return YES;
+    }
+    return NO;
+}
+
+
+/**
+ * Queue mouse presses if they occur in the map section of the main window.
+ */
+static void AngbandHandleEventMouseDown( NSEvent *event )
+{
+#if 0
+       AngbandContext *angbandContext = [[[event window] contentView] angbandContext];
+       AngbandContext *mainAngbandContext =
+           (__bridge AngbandContext*) (angband_terms[0]->data);
+
+       if ([[event window] isKeyWindow] &&
+           mainAngbandContext.primaryWindow &&
+           [[event window] windowNumber] ==
+           [mainAngbandContext.primaryWindow windowNumber])
+       {
+               int cols, rows, x, y;
+               term_get_size(&cols, &rows);
+               NSSize tileSize = angbandContext.tileSize;
+               NSSize border = angbandContext.borderSize;
+               NSPoint windowPoint = [event locationInWindow];
+
+               /*
+                * Adjust for border; add border height because window origin
+                * is at bottom
+                */
+               windowPoint = NSMakePoint( windowPoint.x - border.width, windowPoint.y + border.height );
+
+               NSPoint p = [[[event window] contentView] convertPoint: windowPoint fromView: nil];
+               x = floor( p.x / tileSize.width );
+               y = floor( p.y / tileSize.height );
+
+               BOOL displayingMapInterface = (inkey_flag) ? YES : NO;
+
+               /* Sidebar plus border == thirteen characters; top row is reserved. */
+               /* Coordinates run from (0,0) to (cols-1, rows-1). */
+               BOOL mouseInMapSection = (x > 13 && x <= cols - 1 && y > 0  && y <= rows - 2);
+
+               /*
+                * If we are displaying a menu, allow clicks anywhere within
+                * the terminal bounds; if we are displaying the main game
+                * interface, only allow clicks in the map section
+                */
+               if ((!displayingMapInterface && x >= 0 && x < cols &&
+                    y >= 0 && y < rows) ||
+                    (displayingMapInterface && mouseInMapSection))
+               {
+                       /*
+                        * [event buttonNumber] will return 0 for left click,
+                        * 1 for right click, but this is safer
+                        */
+                       int button = ([event type] == NSEventTypeLeftMouseDown) ? 1 : 2;
+
+#ifdef KC_MOD_ALT
+                       NSUInteger eventModifiers = [event modifierFlags];
+                       byte angbandModifiers = 0;
+                       angbandModifiers |= (eventModifiers & NSEventModifierFlagShift) ? KC_MOD_SHIFT : 0;
+                       angbandModifiers |= (eventModifiers & NSEventModifierFlagControl) ? KC_MOD_CONTROL : 0;
+                       angbandModifiers |= (eventModifiers & NSEventModifierFlagOption) ? KC_MOD_ALT : 0;
+                       button |= (angbandModifiers & 0x0F) << 4; /* encode modifiers in the button number (see Term_mousepress()) */
+#endif
+
+                       Term_mousepress(x, y, button);
+               }
+       }
+#endif
+
+       /* Pass click through to permit focus change, resize, etc. */
+       [NSApp sendEvent:event];
+}
+
+
+
+/**
+ * Encodes an NSEvent Angband-style, or forwards it along.  Returns YES if the
+ * event was sent to Angband, NO if Cocoa (or nothing) handled it
+ */
+static BOOL send_event(NSEvent *event)
+{
+
+    /* If the receiving window is not an Angband window, then do nothing */
+    if (! contains_angband_view([[event window] contentView]))
+    {
+        [NSApp sendEvent:event];
+        return NO;
+    }
+
+    /* Analyze the event */
+    switch ([event type])
+    {
+        case NSEventTypeKeyDown:
+        {
+            /* Try performing a key equivalent */
+            if ([[NSApp mainMenu] performKeyEquivalent:event]) break;
+            
+            unsigned modifiers = [event modifierFlags];
+            
+            /* Send all events with NSEventModifierFlagCommand through */
+            if (modifiers & NSEventModifierFlagCommand)
+            {
+                [NSApp sendEvent:event];
+                break;
+            }
+            
+            if (! [[event characters] length]) break;
+            
+            
+            /* Extract some modifiers */
+            int mc = !! (modifiers & NSEventModifierFlagControl);
+            int ms = !! (modifiers & NSEventModifierFlagShift);
+            int mo = !! (modifiers & NSEventModifierFlagOption);
+            int kp = !! (modifiers & NSEventModifierFlagNumericPad);
+            
+            
+            /* Get the Angband char corresponding to this unichar */
+            unichar c = [[event characters] characterAtIndex:0];
+            char ch;
+           /*
+            * Have anything from the numeric keypad generate a macro
+            * trigger so that shift or control modifiers can be passed.
+            */
+           if (c <= 0x7F && !kp)
+           {
+               ch = (char) c;
+           }
+           else {
+               /*
+                * The rest of Hengband uses Angband 2.7's or so key handling:
+                * so for the rest do something like the encoding that
+                * main-win.c does:  send a macro trigger with the Unicode
+                * value encoded into printable ASCII characters.
+                */
+               ch = '\0';
+            }
+            
+            /* override special keys */
+            switch([event keyCode]) {
+                case kVK_Return: ch = '\r'; break;
+                case kVK_Escape: ch = 27; break;
+                case kVK_Tab: ch = '\t'; break;
+                case kVK_Delete: ch = '\b'; break;
+               case kVK_ANSI_KeypadEnter: ch = '\r'; kp = 1; break;
+            }
+
+            /* Hide the mouse pointer */
+            [NSCursor setHiddenUntilMouseMoves:YES];
+            
+            /* Enqueue it */
+            if (ch != '\0')
+            {
+                send_key(ch);
+            }
+           else
+           {
+               /*
+                * Could use the hexsym global but some characters overlap with
+                * those used to indicate modifiers.
+                */
+               const char encoded[16] = {
+                   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
+                   'c', 'd', 'e', 'f'
+               };
+
+               /* Begin the macro trigger. */
+               send_key(31);
+
+               /* Add the modifiers. */
+               if (mc) {
+                   send_key('C');
+               }
+               if (ms) {
+                   send_key('S');
+               }
+               if (mo) {
+                   send_key('O');
+               }
+               if (kp) {
+                   send_key('K');
+               }
+
+               do {
+                   send_key(encoded[c & 0xF]);
+                   c >>= 4;
+               } while (c > 0);
+
+               /* End the macro trigger. */
+               send_key(13);
+           }
+            
+            break;
+        }
+            
+        case NSEventTypeLeftMouseDown:
+       case NSEventTypeRightMouseDown:
+           AngbandHandleEventMouseDown(event);
+            break;
+
+        case NSEventTypeApplicationDefined:
+        {
+            if (enum2i([event subtype]) == enum2i(AngbandEventWakeup))
+            {
+                return YES;
+            }
+            break;
+        }
+            
+        default:
+            [NSApp sendEvent:event];
+            return YES;
+    }
+    return YES;
+}
+
+/**
+ * This is a replacement for the former Term_keypress():  push a key stroke
+ * to the tail of a terminal's circular buffer of key strokes.  It duplicates
+ * code in main-win.c and main-x11.c.
+ */
+static void send_key(char key)
+{
+    int next_head;
+
+    /* Refuse to enqueue non-keys. */
+    if (! key) return;
+
+    /* Refuse to enqueue if it would cause an overflow. */
+    next_head = game_term->key_head + 1;
+    if (next_head == game_term->key_size) {
+        next_head = 0;
+    }
+    if (next_head == game_term->key_tail) return;
+
+    game_term->key_queue[game_term->key_head] = key;
+    game_term->key_head = next_head;
+}
+
+/**
+ * Check for Events, return YES if we process any
+ */
+static BOOL check_events(int wait)
+{
+    BOOL result = YES;
+
+    @autoreleasepool {
+       /* Handles the quit_when_ready flag */
+       if (quit_when_ready) quit_calmly();
+
+       NSDate* endDate;
+       if (wait == CHECK_EVENTS_WAIT) endDate = [NSDate distantFuture];
+       else endDate = [NSDate distantPast];
+
+       NSEvent* event;
+       for (;;) {
+           if (quit_when_ready)
+           {
+               /* send escape events until we quit */
+               send_key(0x1B);
+               result = NO;
+               break;
+           }
+           else {
+               event = [NSApp nextEventMatchingMask:-1 untilDate:endDate
+                              inMode:NSDefaultRunLoopMode dequeue:YES];
+               if (! event) {
+                   result = NO;
+                   break;
+               }
+               if (send_event(event)) break;
+           }
+       }
+    }
+
+    return result;
+}
+
+/**
+ * Hook to tell the user something important
+ */
+static void hook_plog(const char * str)
+{
+    if (str)
+    {
+       NSString *msg = NSLocalizedStringWithDefaultValue(
+           @"Warning", AngbandMessageCatalog, [NSBundle mainBundle],
+           @"Warning", @"Alert text for generic warning");
+        NSString *info = [NSString stringWithCString:str
+#ifdef JP
+                                  encoding:NSJapaneseEUCStringEncoding
+#else
+                                  encoding:NSMacOSRomanStringEncoding
+#endif
+       ];
+       NSAlert *alert = [[NSAlert alloc] init];
+
+       alert.messageText = msg;
+       alert.informativeText = info;
+       [alert runModal];
+    }
+}
+
+
+/**
+ * Hook to tell the user something, and then quit
+ */
+static void hook_quit(const char * str)
+{
+    for (int i = ANGBAND_TERM_MAX - 1; i >= 0; --i) {
+        if (angband_terms[i]) {
+            term_nuke(angband_terms[i]);
+        }
+    }
+    [AngbandAudioManager clearSharedManager];
+    [AngbandContext setDefaultFont:nil];
+    plog(str);
+    exit(0);
+}
+
+/**
+ * Return the path for Angband's lib directory and bail if it isn't found. The
+ * lib directory should be in the bundle's resources directory, since it's
+ * copied when built.
+ */
+static NSString* get_lib_directory(void)
+{
+    NSString *bundleLibPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: AngbandDirectoryNameLib];
+    BOOL isDirectory = NO;
+    BOOL libExists = [[NSFileManager defaultManager] fileExistsAtPath: bundleLibPath isDirectory: &isDirectory];
+
+    if( !libExists || !isDirectory )
+    {
+       NSLog( @"%@: can't find %@/ in bundle: isDirectory: %d libExists: %d", [NSString stringWithUTF8String:std::string(VARIANT_NAME).c_str()], AngbandDirectoryNameLib, isDirectory, libExists );
+
+       NSString *msg = NSLocalizedStringWithDefaultValue(
+           @"Error.MissingResources",
+           AngbandMessageCatalog,
+           [NSBundle mainBundle],
+           @"Missing Resources",
+           @"Alert text for missing resources");
+       NSString *info = NSLocalizedStringWithDefaultValue(
+           @"Error.MissingAngbandLib",
+           AngbandMessageCatalog,
+           [NSBundle mainBundle],
+           @"Hengband was unable to find required resources and must quit. Please report a bug on the Angband forums.",
+           @"Alert informative message for missing Angband lib/ folder");
+       NSString *quit_label = NSLocalizedStringWithDefaultValue(
+           @"Label.Quit", AngbandMessageCatalog, [NSBundle mainBundle],
+           @"Quit", @"Quit");
+       NSAlert *alert = [[NSAlert alloc] init];
+       alert.alertStyle = NSAlertStyleCritical;
+       alert.messageText = msg;
+       alert.informativeText = info;
+       [alert addButtonWithTitle:quit_label];
+       [alert runModal];
+       exit(0);
+    }
+
+    return bundleLibPath;
+}
+
+/**
+ * Return the path for the directory where Angband should look for its standard
+ * user file tree.
+ */
+static NSString* get_doc_directory(void)
+{
+       NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
+
+#if defined(SAFE_DIRECTORY)
+       NSString *versionedDirectory = [NSString stringWithFormat: @"%@-%s", AngbandDirectoryNameBase, VERSION_STRING];
+       return [documents stringByAppendingPathComponent: versionedDirectory];
+#else
+       return [documents stringByAppendingPathComponent: AngbandDirectoryNameBase];
+#endif
+}
+
+/**
+ * Adjust directory paths as needed to correct for any differences needed by
+ * Angband.  init_file_paths() currently requires that all paths provided have
+ * a trailing slash and all other platforms honor this.
+ *
+ * \param originalPath The directory path to adjust.
+ * \return A path suitable for Angband or nil if an error occurred.
+ */
+static NSString* AngbandCorrectedDirectoryPath(NSString *originalPath)
+{
+       if ([originalPath length] == 0) {
+               return nil;
+       }
+
+       if (![originalPath hasSuffix: @"/"]) {
+               return [originalPath stringByAppendingString: @"/"];
+       }
+
+       return originalPath;
+}
+
+/**
+ * Give Angband the base paths that should be used for the various directories
+ * it needs. It will create any needed directories.
+ */
+static void prepare_paths_and_directories(void)
+{
+       char libpath[PATH_MAX + 1] = "\0";
+       NSString *libDirectoryPath =
+           AngbandCorrectedDirectoryPath(get_lib_directory());
+       [libDirectoryPath getFileSystemRepresentation: libpath maxLength: sizeof(libpath)];
+
+       char basepath[PATH_MAX + 1] = "\0";
+       NSString *angbandDocumentsPath =
+           AngbandCorrectedDirectoryPath(get_doc_directory());
+       [angbandDocumentsPath getFileSystemRepresentation: basepath maxLength: sizeof(basepath)];
+
+       init_file_paths(libpath, basepath);
+       create_needed_dirs();
+}
+
+/**
+ * Create and initialize Angband terminal number "i".
+ */
+static term_type *term_data_link(int i)
+{
+    NSArray *terminalDefaults = [[NSUserDefaults standardUserDefaults]
+                                   valueForKey: AngbandTerminalsDefaultsKey];
+    NSInteger rows = TERM_DEFAULT_ROWS;
+    NSInteger columns = TERM_DEFAULT_COLS;
+
+    if (i < (int)[terminalDefaults count]) {
+        NSDictionary *term = [terminalDefaults objectAtIndex:i];
+        rows = [[term valueForKey: AngbandTerminalRowsDefaultsKey]
+                  integerValue];
+        columns = [[term valueForKey: AngbandTerminalColumnsDefaultsKey]
+                     integerValue];
+    }
+
+    /* Allocate */
+    term_type *newterm = (term_type*) calloc(1, sizeof(*newterm));
+
+    /* Initialize the term */
+    term_init(newterm, columns, rows, 256 /* keypresses, for some reason? */);
+
+    /* Use a "software" cursor */
+    newterm->soft_cursor = true;
+
+    /* Disable the per-row flush notifications since they are not used. */
+    newterm->never_frosh = true;
+
+    /*
+     * Differentiate between BS/^h, Tab/^i, ... so ^h and ^j work under the
+     * roguelike command set.
+     */
+    /* newterm->complex_input = true; */
+
+    /* Erase with "white space" */
+    newterm->attr_blank = TERM_WHITE;
+    newterm->char_blank = ' ';
+
+    /* Prepare the init/nuke hooks */
+    newterm->init_hook = Term_init_cocoa;
+    newterm->nuke_hook = Term_nuke_cocoa;
+
+    /* Prepare the function hooks */
+    newterm->xtra_hook = Term_xtra_cocoa;
+    newterm->wipe_hook = Term_wipe_cocoa;
+    newterm->curs_hook = Term_curs_cocoa;
+    newterm->bigcurs_hook = Term_bigcurs_cocoa;
+    newterm->text_hook = Term_text_cocoa;
+    newterm->pict_hook = Term_pict_cocoa;
+    /* newterm->mbcs_hook = Term_mbcs_cocoa; */
+
+    /* Global pointer */
+    angband_terms[i] = newterm;
+
+    return newterm;
+}
+
+/**
+ * Load preferences from preferences file for current host+current user+
+ * current application.
+ */
+static void load_prefs(void)
+{
+    NSUserDefaults *defs = [NSUserDefaults angbandDefaults];
+
+    /* Make some default defaults */
+    NSMutableArray *defaultTerms = [[NSMutableArray alloc] init];
+
+    /*
+     * The following default rows/cols were determined experimentally by first
+     * finding the ideal window/font size combinations. But because of awful
+     * temporal coupling in Term_init_cocoa(), it's impossible to set up the
+     * defaults there, so we do it this way.
+     */
+    for (NSUInteger i = 0; i < ANGBAND_TERM_MAX; i++) {
+       int columns, rows;
+       BOOL visible = YES;
+
+       switch (i) {
+       case 0:
+           columns = 100;
+           rows = _(28, 30);
+           break;
+       case 1:
+           columns = 66;
+           rows = _(9, 10);
+           break;
+       case 2:
+           columns = 38;
+           rows = 24;
+           break;
+       case 3:
+           columns = 38;
+           rows = _(9, 10);
+           break;
+       case 4:
+           columns = 38;
+           rows = _(13, 15);
+           break;
+       case 5:
+           columns = 66;
+           rows = _(9, 10);
+           break;
+       default:
+           columns = TERM_DEFAULT_COLS;
+           rows = TERM_DEFAULT_ROWS;
+           visible = NO;
+           break;
+       }
+
+       NSDictionary *standardTerm =
+           [NSDictionary dictionaryWithObjectsAndKeys:
+                         [NSNumber numberWithInt: rows], AngbandTerminalRowsDefaultsKey,
+                         [NSNumber numberWithInt: columns], AngbandTerminalColumnsDefaultsKey,
+                         [NSNumber numberWithBool: visible], AngbandTerminalVisibleDefaultsKey,
+                         nil];
+        [defaultTerms addObject: standardTerm];
+    }
+
+    NSDictionary *defaults = [[NSDictionary alloc] initWithObjectsAndKeys:
+                              FallbackFontName, @"FontName-0",
+                              [NSNumber numberWithFloat:FallbackFontSizeMain], @"FontSize-0",
+                              [NSNumber numberWithInt:60], AngbandFrameRateDefaultsKey,
+                              [NSNumber numberWithBool:YES], AngbandSoundEnabledDefaultsKey,
+                              [NSNumber numberWithInt:30], AngbandSoundVolumeDefaultsKey,
+                              [NSNumber numberWithBool:YES], AngbandMusicEnabledDefaultsKey,
+                              [NSNumber numberWithBool:YES], AngbandMusicPausedWhenInactiveDefaultsKey,
+                              [NSNumber numberWithInt:20], AngbandMusicVolumeDefaultsKey,
+                              [NSNumber numberWithInt:3000], AngbandMusicTransitionTimeDefaultsKey,
+                              [NSNumber numberWithInt:GRAPHICS_NONE], AngbandGraphicsDefaultsKey,
+                              [NSNumber numberWithBool:YES], AngbandBigTileDefaultsKey,
+                              defaultTerms, AngbandTerminalsDefaultsKey,
+                              nil];
+    [defs registerDefaults:defaults];
+
+    /* Preferred graphics mode */
+    graf_mode_req = [defs integerForKey:AngbandGraphicsDefaultsKey];
+    if (graphics_will_be_enabled() &&
+       [defs boolForKey:AngbandBigTileDefaultsKey]) {
+       use_bigtile = true;
+       arg_bigtile = true;
+    } else {
+       use_bigtile = false;
+       arg_bigtile = false;
+    }
+
+    /* Use sounds; set the Angband global */
+    if ([defs boolForKey:AngbandSoundEnabledDefaultsKey]) {
+       use_sound = true;
+       [AngbandAudioManager sharedManager].beepEnabled = YES;
+       [AngbandAudioManager sharedManager].soundEnabled = YES;
+    } else {
+       use_sound = false;
+       [AngbandAudioManager sharedManager].beepEnabled = NO;
+       [AngbandAudioManager sharedManager].soundEnabled = NO;
+    }
+    [AngbandAudioManager sharedManager].soundVolume =
+        [defs integerForKey:AngbandSoundVolumeDefaultsKey];
+
+    /* Use music; set the Angband global */
+    if ([defs boolForKey:AngbandMusicEnabledDefaultsKey]) {
+        use_music = true;
+       [AngbandAudioManager sharedManager].musicEnabled = YES;
+    } else {
+        use_music = false;
+       [AngbandAudioManager sharedManager].musicEnabled = NO;
+    }
+    [AngbandAudioManager sharedManager].musicPausedWhenInactive =
+        [defs boolForKey:AngbandMusicPausedWhenInactiveDefaultsKey];
+    [AngbandAudioManager sharedManager].musicVolume =
+        [defs integerForKey:AngbandMusicVolumeDefaultsKey];
+    [AngbandAudioManager sharedManager].musicTransitionTime =
+        [defs integerForKey:AngbandMusicTransitionTimeDefaultsKey];
+
+    /* fps */
+    frames_per_second = [defs integerForKey:AngbandFrameRateDefaultsKey];
+
+    /* Font */
+    [AngbandContext
+       setDefaultFont:[NSFont fontWithName:[defs valueForKey:@"FontName-0"]
+                              size:[defs floatForKey:@"FontSize-0"]]];
+    if (! [AngbandContext defaultFont]) {
+       [AngbandContext
+           setDefaultFont:[NSFont fontWithName:FallbackFontName
+           size:FallbackFontSizeMain]];
+       if (! [AngbandContext defaultFont]) {
+           [AngbandContext
+               setDefaultFont:[NSFont systemFontOfSize:FallbackFontSizeMain]];
+           if (! [AngbandContext defaultFont]) {
+               [AngbandContext
+                   setDefaultFont:[NSFont systemFontOfSize:0.0]];
+           }
+       }
+    }
+}
+
+/**
+ * Allocate the primary Angband terminal and activate it.  Allocate the other
+ * Angband terminals.
+ */
+static void init_windows(void)
+{
+    /* Create the primary window */
+    term_type *primary = term_data_link(0);
+
+    /* Prepare to create any additional windows */
+    for (int i = 1; i < ANGBAND_TERM_MAX; i++) {
+        term_data_link(i);
+    }
+
+    /* Activate the primary term */
+    term_activate(primary);
+}
+
+/**
+ * ------------------------------------------------------------------------
+ * Main program
+ * ------------------------------------------------------------------------ */
+
+@implementation AngbandAppDelegate
+
+@synthesize graphicsMenu=_graphicsMenu;
+@synthesize commandMenu=_commandMenu;
+@synthesize commandMenuTagMap=_commandMenuTagMap;
+
+- (IBAction)newGame:sender
+{
+    /* Game is in progress */
+    game_in_progress = YES;
+    new_game = true;
+}
+
+- (IBAction)editFont:sender
+{
+    NSFontPanel *panel = [NSFontPanel sharedFontPanel];
+    NSFont *termFont = [AngbandContext defaultFont];
+
+    int i;
+    for (i=0; i < ANGBAND_TERM_MAX; i++) {
+       AngbandContext *context =
+           (__bridge AngbandContext*) (angband_terms[i]->data);
+        if ([context isKeyWindow]) {
+            termFont = [context angbandViewFont];
+            break;
+        }
+    }
+
+    [panel setPanelFont:termFont isMultiple:NO];
+    [panel orderFront:self];
+}
+
+/**
+ * Implement NSObject's changeFont() method to receive a notification about the
+ * changed font.  Note that, as of 10.14, changeFont() is deprecated in
+ * NSObject - it will be removed at some point and the application delegate
+ * will have to be declared as implementing the NSFontChanging protocol.
+ */
+- (void)changeFont:(id)sender
+{
+    int mainTerm;
+    for (mainTerm=0; mainTerm < ANGBAND_TERM_MAX; mainTerm++) {
+       AngbandContext *context =
+           (__bridge AngbandContext*) (angband_terms[mainTerm]->data);
+        if ([context isKeyWindow]) {
+            break;
+        }
+    }
+
+    /* Bug #1709: Only change font for angband windows */
+    if (mainTerm == ANGBAND_TERM_MAX) return;
+
+    NSFont *oldFont = [AngbandContext defaultFont];
+    NSFont *newFont = [sender convertFont:oldFont];
+    if (! newFont) return; /*paranoia */
+
+    /* Store as the default font if we changed the first term */
+    if (mainTerm == 0) {
+       [AngbandContext setDefaultFont:newFont];
+    }
+
+    /* Record it in the preferences */
+    NSUserDefaults *defs = [NSUserDefaults angbandDefaults];
+    [defs setValue:[newFont fontName] 
+        forKey:[NSString stringWithFormat:@"FontName-%d", mainTerm]];
+    [defs setFloat:[newFont pointSize]
+        forKey:[NSString stringWithFormat:@"FontSize-%d", mainTerm]];
+
+    /* Update window */
+    AngbandContext *angbandContext =
+       (__bridge AngbandContext*) (angband_terms[mainTerm]->data);
+    [(id)angbandContext setSelectionFont:newFont adjustTerminal: YES];
+
+    if (mainTerm != 0 || ! redraw_for_tiles_or_term0_font()) {
+       [(id)angbandContext requestRedraw];
+    }
+}
+
+- (IBAction)openGame:sender
+{
+    @autoreleasepool {
+       BOOL selectedSomething = NO;
+       int panelResult;
+
+       /* Get where we think the save files are */
+       NSURL *startingDirectoryURL =
+           [NSURL fileURLWithPath:[NSString stringWithCString:ANGBAND_DIR_SAVE.native().data() encoding:NSASCIIStringEncoding]
+                  isDirectory:YES];
+
+       /* Set up an open panel */
+       NSOpenPanel* panel = [NSOpenPanel openPanel];
+       [panel setCanChooseFiles:YES];
+       [panel setCanChooseDirectories:NO];
+       [panel setResolvesAliases:YES];
+       [panel setAllowsMultipleSelection:NO];
+       [panel setTreatsFilePackagesAsDirectories:YES];
+       [panel setDirectoryURL:startingDirectoryURL];
+
+       /* Run it */
+       panelResult = [panel runModal];
+       if (panelResult == NSModalResponseOK)
+       {
+           NSArray* fileURLs = [panel URLs];
+           if ([fileURLs count] > 0 && [[fileURLs objectAtIndex:0] isFileURL])
+           {
+               NSURL* savefileURL = (NSURL *)[fileURLs objectAtIndex:0];
+               char t[1024];
+
+               /*
+                * The path property doesn't do the right thing except for
+                * URLs with the file scheme. We had
+                * getFileSystemRepresentation here before, but that wasn't
+                * introduced until OS X 10.9.
+                */
+               selectedSomething = [[savefileURL path]
+                                       getCString:t
+                                       maxLength:sizeof(t)
+                                       encoding:NSMacOSRomanStringEncoding];
+               savefile = std::filesystem::path(t,
+                       std::filesystem::path::native_format);
+           }
+       }
+
+       if (selectedSomething)
+       {
+           /* Remember this so we can select it by default next time */
+           record_current_savefile();
+
+           /* Game is in progress */
+           game_in_progress = YES;
+       }
+    }
+}
+
+- (IBAction)saveGame:sender
+{
+    /* Hack -- Forget messages */
+    msg_flag = false;
+    
+    /* Save the game */
+    do_cmd_save_game(p_ptr, 0);
+    
+    /*
+     * Record the current save file so we can select it by default next time.
+     * It's a little sketchy that this only happens when we save through the
+     * menu; ideally game-triggered saves would trigger it too.
+     */
+    record_current_savefile();
+}
+
+/**
+ * Entry point for initializing Angband
+ */
+- (void)beginGame
+{
+    @autoreleasepool {
+       /* Hooks in some "z-util.c" hooks */
+       plog_aux = hook_plog;
+       quit_aux = hook_quit;
+
+       /* Initialize file paths */
+       prepare_paths_and_directories();
+
+       /* Note the "system" */
+       ANGBAND_SYS = "mac";
+
+       /* Load preferences */
+       load_prefs();
+
+       /* Prepare the windows */
+       init_windows();
+
+       /* Set up game event handlers */
+       /* init_display(); */
+
+       /* Initialize some save file stuff */
+       auto &ids = UnixUserIds::get_instance();
+       ids.set_user_id(getuid());
+       ids.set_effective_user_id(geteuid());
+       ids.set_effective_group_id(getegid());
+
+       /*
+        * Cause splash screen to be centered if the main window is bigger
+        * than MAIN_TERM_MIN_COLS x MAIN_TERM_MIN_ROWS.
+        */
+       constexpr auto splash_width = MAIN_TERM_MIN_COLS;
+       constexpr auto splash_height = MAIN_TERM_MIN_ROWS;
+       TermCenteredOffsetSetter tcos(splash_width, splash_height);
+
+       /* Initialise game */
+       init_angband(p_ptr, false);
+
+       /* Load possible graphics modes */
+       init_graphics_modes();
+
+       /*
+        * Reevaluate whether to use bigtiles (the tests in load_prefs() were
+        * done before init_graphics_modes()).
+        */
+       if (graphics_will_be_enabled()
+                       && [[NSUserDefaults angbandDefaults]
+                               boolForKey:AngbandBigTileDefaultsKey]
+                       && ! use_bigtile) {
+               arg_bigtile = true;
+               term_activate(angband_terms[0]);
+               term_resize(angband_terms[0]->wid, angband_terms[0]->hgt);
+               redraw_for_tiles_or_term0_font();
+       }
+
+       /* We are now initialized */
+       initialized = YES;
+
+       /* Handle pending events (most notably update) and flush input */
+       term_flush();
+
+       /*
+        * Prompt the user.  Overwrite the last line of the splash screen
+        * with the prompt.
+        */
+       prt(
+           _("[ファイル] メニューの [新規] または [開く] を選択してください。",
+           "[Choose 'New' or 'Open' from the 'File' menu]"), splash_height - 1,
+           (splash_width - _(63, 45)) / 2);
+       term_fresh();
+    }
+
+    while (!game_in_progress) {
+       @autoreleasepool {
+           NSEvent *event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:[NSDate distantFuture] inMode:NSDefaultRunLoopMode dequeue:YES];
+           if (event) [NSApp sendEvent:event];
+       }
+    }
+
+    /*
+     * Play a game -- "new_game" is set by "new", "open" or the open document
+     * even handler as appropriate
+     */
+    term_fresh();
+    play_game(p_ptr, new_game, false);
+
+    quit(NULL);
+}
+
+/**
+ * Implement NSObject's validateMenuItem() method to override enabling or
+ * disabling a menu item.  Note that, as of 10.14, validateMenuItem() is
+ * deprecated in NSObject - it will be removed at some point and the
+ * application delegate will have to be declared as implementing the
+ * NSMenuItemValidation protocol.
+ */
+- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
+{
+    SEL sel = [menuItem action];
+    NSInteger tag = [menuItem tag];
+
+    if( tag >= AngbandWindowMenuItemTagBase && tag < AngbandWindowMenuItemTagBase + ANGBAND_TERM_MAX )
+    {
+        if( tag == AngbandWindowMenuItemTagBase )
+        {
+            /* The main window should always be available and visible */
+            return YES;
+        }
+        else
+        {
+           /*
+            * Another window is only usable after Term_init_cocoa() has
+            * been called for it.  For Angband, if g_window_flags[i] has
+            * any bits set then that has happened for window i.  For
+            * Hengband, that is not the case so also test
+            * angband_terms[i]->data.
+            */
+            NSInteger subwindowNumber = tag - AngbandWindowMenuItemTagBase;
+            return (angband_terms[subwindowNumber]->data != 0
+                   && !g_window_flags[subwindowNumber].none());
+        }
+
+        return NO;
+    }
+
+    if (sel == @selector(newGame:))
+    {
+        return ! game_in_progress;
+    }
+    else if (sel == @selector(editFont:))
+    {
+        return YES;
+    }
+    else if (sel == @selector(openGame:))
+    {
+        return ! game_in_progress;
+    }
+    else if (sel == @selector(setRefreshRate:) &&
+            [[menuItem parentItem] tag] == 150)
+    {
+        NSInteger fps = [[NSUserDefaults standardUserDefaults] integerForKey:AngbandFrameRateDefaultsKey];
+        [menuItem setState: ([menuItem tag] == fps)];
+        return YES;
+    }
+    else if( sel == @selector(setGraphicsMode:) )
+    {
+        NSInteger requestedGraphicsMode = [[NSUserDefaults standardUserDefaults] integerForKey:AngbandGraphicsDefaultsKey];
+        [menuItem setState: (tag == requestedGraphicsMode)];
+        /*
+         * Only allow changes to the graphics mode when at the splash screen
+         * or in the game proper and at a command prompt.  In other situations
+         * the saved screens for overlayed menus could have tile references
+         * that become outdated when the graphics mode is changed.
+         */
+        return (!game_in_progress
+            || (w_ptr->character_generated && inkey_flag)) ?  YES : NO;
+    }
+    else if (sel == @selector(toggleWideTiles:)) {
+       BOOL is_on = [[NSUserDefaults standardUserDefaults]
+                        boolForKey:AngbandBigTileDefaultsKey];
+
+       [menuItem setState: ((is_on) ?
+           NSControlStateValueOn : NSControlStateValueOff)];
+        /*
+         * Only allow changes to the tile size if tiles are not being used
+         * (then there's no effect on appearance) or, like changes to the
+         * graphics mode, if at the splash screen or command prompt.
+         */
+       return (!graphics_are_enabled() || !game_in_progress
+            || (w_ptr->character_generated && inkey_flag)) ? YES : NO;
+    }
+    else if( sel == @selector(sendAngbandCommand:) ||
+            sel == @selector(saveGame:) )
+    {
+        /*
+         * we only want to be able to send commands during an active game
+         * after the birth screens when the core is waiting for a player
+         * command
+         */
+        return !!game_in_progress && w_ptr->character_generated && inkey_flag;
+    }
+    else return YES;
+}
+
+
+- (IBAction)setRefreshRate:(NSMenuItem *)menuItem
+{
+    frames_per_second = [menuItem tag];
+    [[NSUserDefaults angbandDefaults] setInteger:frames_per_second forKey:AngbandFrameRateDefaultsKey];
+}
+
+- (void)setGraphicsMode:(NSMenuItem *)sender
+{
+    /* We stashed the graphics mode ID in the menu item's tag */
+    graf_mode_req = [sender tag];
+
+    /* Stash it in UserDefaults */
+    [[NSUserDefaults angbandDefaults] setInteger:graf_mode_req forKey:AngbandGraphicsDefaultsKey];
+
+    if (! graphics_will_be_enabled()) {
+       if (use_bigtile) {
+           arg_bigtile = false;
+       }
+    } else if ([[NSUserDefaults angbandDefaults] boolForKey:AngbandBigTileDefaultsKey]
+               && ! use_bigtile) {
+       arg_bigtile = true;
+    }
+
+    if (arg_bigtile != use_bigtile) {
+       term_activate(angband_terms[0]);
+       term_resize(angband_terms[0]->wid, angband_terms[0]->hgt);
+    }
+    redraw_for_tiles_or_term0_font();
+}
+
+- (void)selectWindow: (id)sender
+{
+    NSInteger subwindowNumber =
+       [(NSMenuItem *)sender tag] - AngbandWindowMenuItemTagBase;
+    AngbandContext *context =
+       (__bridge AngbandContext*) (angband_terms[subwindowNumber]->data);
+    [context.primaryWindow makeKeyAndOrderFront: self];
+    [context saveWindowVisibleToDefaults: YES];
+}
+
+- (IBAction)toggleWideTiles:(NSMenuItem *) sender
+{
+    BOOL is_on = (sender.state == NSControlStateValueOn);
+
+    /* Toggle the state and update the Angband globals and preferences. */
+    sender.state = (is_on) ? NSControlStateValueOff : NSControlStateValueOn;
+    [[NSUserDefaults angbandDefaults] setBool:(! is_on)
+                                     forKey:AngbandBigTileDefaultsKey];
+    if (graphics_are_enabled()) {
+       arg_bigtile = (is_on) ? false : true;
+       if (arg_bigtile != use_bigtile) {
+           term_activate(angband_terms[0]);
+           term_resize(angband_terms[0]->wid, angband_terms[0]->hgt);
+           redraw_for_tiles_or_term0_font();
+       }
+    }
+}
+
+- (IBAction)showSoundAndMusicPanel:(NSMenuItem *)sender
+{
+    if (!self.soundAndMusicPanelController) {
+        self.soundAndMusicPanelController =
+            [[SoundAndMusicPanelController alloc] initWithWindow:nil];
+    } else {
+        self.soundAndMusicPanelController.changeHandler = nil;
+    }
+    self.soundAndMusicPanelController.soundEnabled =
+        [AngbandAudioManager sharedManager].isSoundEnabled;
+    self.soundAndMusicPanelController.soundVolume =
+        [AngbandAudioManager sharedManager].soundVolume;
+    self.soundAndMusicPanelController.musicEnabled =
+        [AngbandAudioManager sharedManager].isMusicEnabled;
+    self.soundAndMusicPanelController.musicPausedWhenInactive =
+        [AngbandAudioManager sharedManager].isMusicPausedWhenInactive;
+    self.soundAndMusicPanelController.musicVolume =
+        [AngbandAudioManager sharedManager].musicVolume;
+    self.soundAndMusicPanelController.musicTransitionTime =
+        [AngbandAudioManager sharedManager].musicTransitionTime;
+    [self.soundAndMusicPanelController showWindow:sender];
+    self.soundAndMusicPanelController.changeHandler = self;
+}
+
+/*
+ * Implement the SoundAndMusicChanges protocol to respond to changes from
+ * the Sound and Music dialog.
+ */
+- (void)changeSoundEnabled:(BOOL)newv
+{
+    use_sound = (newv) ? true : false;
+    [AngbandAudioManager sharedManager].beepEnabled = newv;
+    [AngbandAudioManager sharedManager].soundEnabled = newv;
+    [[NSUserDefaults angbandDefaults] setBool:newv
+        forKey:AngbandSoundEnabledDefaultsKey];
+}
+
+- (void)changeSoundVolume:(NSInteger)newv
+{
+    [AngbandAudioManager sharedManager].soundVolume = newv;
+    [[NSUserDefaults angbandDefaults] setInteger:newv
+        forKey:AngbandSoundVolumeDefaultsKey];
+}
+
+- (void)changeMusicEnabled:(BOOL)newv
+{
+    use_music = (newv) ? true : false;
+    [AngbandAudioManager sharedManager].musicEnabled = newv;
+    [[NSUserDefaults angbandDefaults] setBool:newv
+        forKey:AngbandMusicEnabledDefaultsKey];
+}
+
+- (void)changeMusicPausedWhenInactive:(BOOL)newv
+{
+    [AngbandAudioManager sharedManager].musicPausedWhenInactive = newv;
+    [[NSUserDefaults angbandDefaults] setBool:newv
+        forKey:AngbandMusicPausedWhenInactiveDefaultsKey];
+}
+
+- (void)changeMusicVolume:(NSInteger)newv
+{
+    [AngbandAudioManager sharedManager].musicVolume = newv;
+    [[NSUserDefaults angbandDefaults] setInteger:newv
+        forKey:AngbandMusicVolumeDefaultsKey];
+}
+
+- (void)changeMusicTransitionTime:(NSInteger)newv
+{
+    [AngbandAudioManager sharedManager].musicTransitionTime = newv;
+    [[NSUserDefaults angbandDefaults] setInteger:newv
+        forKey:AngbandMusicTransitionTimeDefaultsKey];
+}
+
+- (void)soundAndMusicPanelWillClose
+{
+    AngbandContext *mainWindow =
+        (__bridge AngbandContext*) (angband_terms[0]->data);
+
+    [mainWindow.primaryWindow makeKeyAndOrderFront: nil];
+    self.soundAndMusicPanelController.changeHandler = nil;
+}
+
+- (void)prepareWindowsMenu
+{
+    @autoreleasepool {
+       /*
+        * Get the window menu with default items and add a separator and
+        * item for the main window.
+        */
+       NSMenu *windowsMenu = [[NSApplication sharedApplication] windowsMenu];
+       [windowsMenu addItem: [NSMenuItem separatorItem]];
+
+       NSString *title1 = [NSString stringWithCString:angband_term_name[0]
+#ifdef JP
+                                    encoding:NSJapaneseEUCStringEncoding
+#else
+                                    encoding:NSMacOSRomanStringEncoding
+#endif
+       ];
+       NSMenuItem *angbandItem = [[NSMenuItem alloc] initWithTitle:title1 action: @selector(selectWindow:) keyEquivalent: @"0"];
+       [angbandItem setTarget: self];
+       [angbandItem setTag: AngbandWindowMenuItemTagBase];
+       [windowsMenu addItem: angbandItem];
+
+       /* Add items for the additional term windows */
+       for( NSInteger i = 1; i < ANGBAND_TERM_MAX; i++ )
+       {
+           NSString *title = [NSString stringWithCString:angband_term_name[i]
+#ifdef JP
+                                       encoding:NSJapaneseEUCStringEncoding
+#else
+                                       encoding:NSMacOSRomanStringEncoding
+#endif
+           ];
+           NSString *keyEquivalent =
+               [NSString stringWithFormat: @"%ld", (long)i];
+           NSMenuItem *windowItem =
+               [[NSMenuItem alloc] initWithTitle: title
+                                   action: @selector(selectWindow:)
+                                   keyEquivalent: keyEquivalent];
+           [windowItem setTarget: self];
+           [windowItem setTag: AngbandWindowMenuItemTagBase + i];
+           [windowsMenu addItem: windowItem];
+       }
+    }
+}
+
+/**
+ * Send a command to Angband via a menu item. This places the appropriate key
+ * down events into the queue so that it seems like the user pressed them
+ * (instead of trying to use the term directly).
+ */
+- (void)sendAngbandCommand: (id)sender
+{
+    NSMenuItem *menuItem = (NSMenuItem *)sender;
+    NSString *command = [self.commandMenuTagMap objectForKey: [NSNumber numberWithInteger: [menuItem tag]]];
+    AngbandContext* context =
+       (__bridge AngbandContext*) (angband_terms[0]->data);
+    NSInteger windowNumber = [context.primaryWindow windowNumber];
+
+    /* Send a \ to bypass keymaps */
+    NSEvent *escape = [NSEvent keyEventWithType: NSEventTypeKeyDown
+                                       location: NSZeroPoint
+                                  modifierFlags: 0
+                                      timestamp: 0.0
+                                   windowNumber: windowNumber
+                                        context: nil
+                                     characters: @"\\"
+                    charactersIgnoringModifiers: @"\\"
+                                      isARepeat: NO
+                                        keyCode: 0];
+    [[NSApplication sharedApplication] postEvent: escape atStart: NO];
+
+    /* Send the actual command (from the original command set) */
+    NSEvent *keyDown = [NSEvent keyEventWithType: NSEventTypeKeyDown
+                                        location: NSZeroPoint
+                                   modifierFlags: 0
+                                       timestamp: 0.0
+                                    windowNumber: windowNumber
+                                         context: nil
+                                      characters: command
+                     charactersIgnoringModifiers: command
+                                       isARepeat: NO
+                                         keyCode: 0];
+    [[NSApplication sharedApplication] postEvent: keyDown atStart: NO];
+}
+
+/**
+ * Set up the command menu dynamically, based on CommandMenu.plist.
+ */
+- (void)prepareCommandMenu
+{
+    @autoreleasepool {
+       NSString *commandMenuPath =
+           [[NSBundle mainBundle] pathForResource: @"CommandMenu"
+                                  ofType: @"plist"];
+       NSArray *commandMenuItems =
+           [[NSArray alloc] initWithContentsOfFile: commandMenuPath];
+       NSMutableDictionary *angbandCommands =
+           [[NSMutableDictionary alloc] init];
+       NSString *tblname = @"CommandMenu";
+       NSInteger tagOffset = 0;
+
+       for( NSDictionary *item in commandMenuItems )
+       {
+           BOOL useShiftModifier =
+               [[item valueForKey: @"ShiftModifier"] boolValue];
+           BOOL useOptionModifier =
+               [[item valueForKey: @"OptionModifier"] boolValue];
+           NSUInteger keyModifiers = NSEventModifierFlagCommand;
+           keyModifiers |= (useShiftModifier) ? NSEventModifierFlagShift : 0;
+           keyModifiers |= (useOptionModifier) ? NSEventModifierFlagOption : 0;
+
+           NSString *lookup = [item valueForKey: @"Title"];
+           NSString *title = NSLocalizedStringWithDefaultValue(
+               lookup, tblname, [NSBundle mainBundle], lookup, @"");
+           NSString *key = [item valueForKey: @"KeyEquivalent"];
+           NSMenuItem *menuItem =
+               [[NSMenuItem alloc] initWithTitle: title
+                                   action: @selector(sendAngbandCommand:)
+                                   keyEquivalent: key];
+           [menuItem setTarget: self];
+           [menuItem setKeyEquivalentModifierMask: keyModifiers];
+           [menuItem setTag: AngbandCommandMenuItemTagBase + tagOffset];
+           [self.commandMenu addItem: menuItem];
+
+           NSString *angbandCommand = [item valueForKey: @"AngbandCommand"];
+           [angbandCommands setObject: angbandCommand
+                            forKey: [NSNumber numberWithInteger: [menuItem tag]]];
+           tagOffset++;
+       }
+
+       self.commandMenuTagMap = [[NSDictionary alloc]
+                                    initWithDictionary: angbandCommands];
+    }
+}
+
+- (void)applicationDidBecomeActive:(NSNotification *)notification
+{
+    [[AngbandAudioManager sharedManager] setupForActiveApp];
+}
+
+- (void)applicationWillResignActive:(NSNotification *)notification
+{
+    [[AngbandAudioManager sharedManager] setupForInactiveApp];
+}
+
+- (void)awakeFromNib
+{
+    [super awakeFromNib];
+
+    [self prepareWindowsMenu];
+    [self prepareCommandMenu];
+}
+
+- (void)applicationDidFinishLaunching:sender
+{
+    [self beginGame];
+    
+    /*
+     * Once beginGame finished, the game is over - that's how Angband works,
+     * and we should quit
+     */
+    game_is_finished = YES;
+    [NSApp terminate:self];
+}
+
+- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
+{
+    if (!p_ptr->playing || game_is_finished)
+    {
+        quit_when_ready = YES;
+        return NSTerminateNow;
+    }
+    else if (! inkey_flag)
+    {
+        /* For compatibility with other ports, do not quit in this case */
+        return NSTerminateCancel;
+    }
+    else
+    {
+        /* Stop playing */
+        /* player->upkeep->playing = false; */
+
+        /*
+         * Post an escape event so that we can return from our get-key-event
+         * function
+         */
+        wakeup_event_loop();
+        quit_when_ready = YES;
+        /*
+         * Must return Cancel, not Later, because we need to get out of the
+         * run loop and back to Angband's loop
+         */
+        return NSTerminateCancel;
+    }
+}
+
+/**
+ * Dynamically build the Graphics menu
+ */
+- (void)menuNeedsUpdate:(NSMenu *)menu {
+    
+    /* Only the graphics menu is dynamic */
+    if (! [menu isEqual:self.graphicsMenu])
+        return;
+    
+    /*
+     * If it's non-empty, then we've already built it. Currently graphics modes
+     * won't change once created; if they ever can we can remove this check.
+     * Note that the check mark does change, but that's handled in
+     * validateMenuItem: instead of menuNeedsUpdate:
+     */
+    if ([menu numberOfItems] > 0)
+        return;
+    
+    /* This is the action for all these menu items */
+    SEL action = @selector(setGraphicsMode:);
+    
+    /* Add an initial Classic ASCII menu item */
+    NSString *tblname = @"GraphicsMenu";
+    NSString *key = @"Classic ASCII";
+    NSString *title = NSLocalizedStringWithDefaultValue(
+       key, tblname, [NSBundle mainBundle], key, @"");
+    NSMenuItem *classicItem = [menu addItemWithTitle:title action:action keyEquivalent:@""];
+    [classicItem setTag:GRAPHICS_NONE];
+    
+    /* Walk through the list of graphics modes */
+    if (graphics_modes) {
+       NSInteger i;
+
+       for (i=0; graphics_modes[i].pNext; i++)
+       {
+           const graphics_mode *graf = &graphics_modes[i];
+
+           if (graf->grafID == GRAPHICS_NONE) {
+               continue;
+           }
+           /*
+            * Make the title. NSMenuItem throws on a nil title, so ensure it's
+            * not nil.
+            */
+           key = [[NSString alloc] initWithUTF8String:graf->menuname];
+           title = NSLocalizedStringWithDefaultValue(
+               key, tblname, [NSBundle mainBundle], key, @"");
+
+           /* Make the item */
+           NSMenuItem *item = [menu addItemWithTitle:title action:action keyEquivalent:@""];
+           [item setTag:graf->grafID];
+       }
+    }
+}
+
+/**
+ * Delegate method that gets called if we're asked to open a file.
+ */
+- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
+{
+    /* Can't open a file once we've started */
+    if (game_in_progress) {
+       [[NSApplication sharedApplication]
+           replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
+       return;
+    }
+
+    /* We can only open one file. Use the last one. */
+    NSString *file = [filenames lastObject];
+    if (! file) {
+       [[NSApplication sharedApplication]
+           replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
+       return;
+    }
+
+    /* Put it in savefile */
+    char t[1024];
+    if (! [file getFileSystemRepresentation:t maxLength:sizeof(t)]) {
+       [[NSApplication sharedApplication]
+           replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
+       return;
+    }
+    savefile = std::filesystem::path(t, std::filesystem::path::native_format);
+
+    game_in_progress = YES;
+
+    /*
+     * Wake us up in case this arrives while we're sitting at the Welcome
+     * screen!
+     */
+    wakeup_event_loop();
+
+    [[NSApplication sharedApplication]
+       replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
+}
+
+@end
+
+int main(int argc, char* argv[])
+{
+    NSApplicationMain(argc, (const char **) argv);
+    return (0);
+}
+
+#endif /* MACH_O_COCOA */
index 6a1f631..5d93b3c 100644 (file)
@@ -100,7 +100,7 @@ graphics_mode change_graphics(graphics_mode arg)
         ox = 0;
         oy = 0;
         name = "16X16.BMP";
-        name_mask = "mask.bmp";
+        name_mask = "16x16-mask.bmp";
 
         ANGBAND_GRAF = "new";
     } else if (arg == graphics_mode::GRAPHICS_HENGBAND) {
index 22ff629..48a0428 100644 (file)
@@ -28,7 +28,6 @@
 #include "view/display-scores.h"
 #include "wizard/spoiler-util.h"
 #include "wizard/wizard-spoiler.h"
-#include <filesystem>
 #include <string>
 
 /*
@@ -44,7 +43,7 @@
  * all the others use this file for their "main()" function.
  */
 
-#ifndef WINDOWS
+#if !defined(WINDOWS) && !defined(MACH_O_COCOA)
 /*
  * A hook for "quit()".
  *
@@ -68,30 +67,6 @@ static void quit_hook(concptr s)
     }
 }
 
-/*
- * Set the stack size and overlay buffer (see main-286.c")
- */
-#ifdef PRIVATE_USER_PATH
-
-/*
- * Create an ".angband/" directory in the users home directory.
- *
- * ToDo: Add error handling.
- * ToDo: Only create the directories when actually writing files.
- */
-static void create_user_dir(void)
-{
-    const auto &dirpath = path_parse(PRIVATE_USER_PATH);
-    const auto &dir_str = dirpath.string();
-    mkdir(dir_str.data(), 0700);
-
-    const auto &subdirpath = path_build(dirpath, VARIANT_NAME);
-    const auto &subdir_str = subdirpath.string();
-    mkdir(subdir_str.data(), 0700);
-}
-
-#endif /* PRIVATE_USER_PATH */
-
 static void init_stuff()
 {
     char libpath[1024]{};
@@ -101,7 +76,7 @@ static void init_stuff()
         strcat(libpath, PATH_SEP);
     }
 
-    init_file_paths(libpath);
+    init_file_paths(libpath, libpath);
 }
 
 /*
@@ -280,7 +255,8 @@ int main(int argc, char *argv[])
 #ifdef SET_UID
     user_name(p_ptr->name, ids.get_user_id());
 #ifdef PRIVATE_USER_PATH
-    create_user_dir();
+    /* Create a directory for the user's files; handled by init.c. */
+    create_needed_dirs();
 #endif /* PRIVATE_USER_PATH */
 #endif /* SET_UID */
 
index e81fd88..1dcb286 100644 (file)
@@ -33,7 +33,9 @@
 #include "term/term-color-types.h"
 #include "time.h"
 #include "util/angband-files.h"
+#include "util/string-processor.h"
 #include "world/world.h"
+#include <vector>
 #include <chrono>
 
 /*!
@@ -71,25 +73,29 @@ static void remove_old_debug_savefiles()
 /*!
  * @brief 各データファイルを読み取るためのパスを取得する.
  * @param libpath 各PCのインストール環境における"lib/" を表す絶対パス
+ * @param varpath Is the base path for directories that have files which
+ * are not read-only: ANGBAND_DIR_APEX, ANGBAND_DIR_BONE, ANGBAND_DIR_DATA,
+ * and ANGBAND_DIR_SAVE.  If the PRIVATE_USER_PATH preprocessor macro has
+ * not been set, it is also used as the base path for ANGBAND_DIR_USER.
  */
-void init_file_paths(const std::filesystem::path &libpath)
+void init_file_paths(const std::filesystem::path &libpath, const std::filesystem::path &varpath)
 {
     ANGBAND_DIR = std::filesystem::path(libpath);
-    ANGBAND_DIR_APEX = std::filesystem::path(libpath).append("apex");
-    ANGBAND_DIR_BONE = std::filesystem::path(libpath).append("bone");
-    ANGBAND_DIR_DATA = std::filesystem::path(libpath).append("data");
+    ANGBAND_DIR_APEX = std::filesystem::path(varpath).append("apex");
+    ANGBAND_DIR_BONE = std::filesystem::path(varpath).append("bone");
+    ANGBAND_DIR_DATA = std::filesystem::path(varpath).append("data");
     ANGBAND_DIR_EDIT = std::filesystem::path(libpath).append("edit");
     ANGBAND_DIR_SCRIPT = std::filesystem::path(libpath).append("script");
     ANGBAND_DIR_FILE = std::filesystem::path(libpath).append("file");
     ANGBAND_DIR_HELP = std::filesystem::path(libpath).append("help");
     ANGBAND_DIR_INFO = std::filesystem::path(libpath).append("info");
     ANGBAND_DIR_PREF = std::filesystem::path(libpath).append("pref");
-    ANGBAND_DIR_SAVE = std::filesystem::path(libpath).append("save");
+    ANGBAND_DIR_SAVE = std::filesystem::path(varpath).append("save");
     ANGBAND_DIR_DEBUG_SAVE = std::filesystem::path(ANGBAND_DIR_SAVE).append("log");
 #ifdef PRIVATE_USER_PATH
-    ANGBAND_DIR_USER = std::filesystem::path(PRIVATE_USER_PATH).append(VARIANT_NAME);
+    ANGBAND_DIR_USER = path_parse(PRIVATE_USER_PATH).append(VARIANT_NAME);
 #else
-    ANGBAND_DIR_USER = std::filesystem::path(libpath).append("user");
+    ANGBAND_DIR_USER = std::filesystem::path(varpath).append("user");
 #endif
     ANGBAND_DIR_XTRA = std::filesystem::path(libpath).append("xtra");
 
@@ -101,6 +107,87 @@ void init_file_paths(const std::filesystem::path &libpath)
     remove_old_debug_savefiles();
 }
 
+/*
+ * Helper function for create_needed_dirs().  Copied over from PosChengband.
+ */
+static bool dir_exists(const std::filesystem::path path)
+{
+    struct stat buf;
+    if (stat(path.native().data(), &buf) != 0)
+       return false;
+#ifdef WIN32
+    else if (buf.st_mode & S_IFDIR)
+#else
+    else if (S_ISDIR(buf.st_mode))
+#endif
+       return true;
+    else
+       return false;
+}
+
+
+/*
+ * Helper function for create_needed_dirs().  Copied over from PosChengband
+ * but use the global definition for the path separator rather than a local
+ * one in PosChengband's code and check for paths that end with the path
+ * separator.
+ */
+static bool dir_create(const std::filesystem::path path)
+{
+#ifdef WIN32
+    /* If the directory already exists then we're done */
+    if (dir_exists(path)) return true;
+    return false;
+#else
+    std::vector<std::filesystem::path> missing;
+    std::filesystem::path next_path = path;
+
+    while (1) {
+        if (dir_exists(next_path)) {
+            break;
+        }
+        missing.push_back(next_path);
+        if (!next_path.has_relative_path()) {
+               break;
+       }
+       next_path = next_path.parent_path();
+    }
+    for (; !missing.empty(); missing.pop_back()) {
+        if (mkdir(missing.back().native().data(), 0755) != 0) {
+            return false;
+        }
+    }
+    return true;
+#endif
+}
+
+
+/*
+ * Create any missing directories. We create only those dirs which may be
+ * empty (user/, save/, apex/, bone/, data/). Only user/ is created when
+ * the PRIVATE_USER_PATH preprocessor maccro has been set. The others are
+ * assumed to contain required files and therefore must exist at startup
+ * (edit/, pref/, file/, xtra/).
+ *
+ * ToDo: Only create the directories when actually writing files.
+ * Copied over from PosChengband to support main-cocoa.m.  Dropped
+ * creation of help/ (and removed it and info/ in the comment)
+ * since init_file_paths() puts those in libpath which may not be writable
+ * by the user running the application.  Added bone/ since
+ * init_file_paths() puts that in varpath.
+ */
+void create_needed_dirs(void)
+{
+    if (!dir_create(ANGBAND_DIR_USER)) quit_fmt("Cannot create '%s'", ANGBAND_DIR_USER.native().data());
+#ifndef PRIVATE_USER_PATH
+    if (!dir_create(ANGBAND_DIR_SAVE)) quit_fmt("Cannot create '%s'", ANGBAND_DIR_SAVE.native().data());
+    if (!dir_create(ANGBAND_DIR_DEBUG_SAVE)) quit_fmt("Cannot create '%s'", ANGBAND_DIR_DEBUG_SAVE.native().data());
+    if (!dir_create(ANGBAND_DIR_APEX)) quit_fmt("Cannot create '%s'", ANGBAND_DIR_APEX.native().data());
+    if (!dir_create(ANGBAND_DIR_BONE)) quit_fmt("Cannot create '%s'", ANGBAND_DIR_BONE.native().data());
+    if (!dir_create(ANGBAND_DIR_DATA)) quit_fmt("Cannot create '%s'", ANGBAND_DIR_DATA.native().data());
+#endif /* ndef PRIVATE_USER_PATH */
+}
+
 /*!
  * @brief 画面左下にシステムメッセージを表示する / Take notes on line 23
  * @param str 初期化中のコンテンツ文字列
index 5b3187a..137ba6f 100644 (file)
@@ -15,4 +15,5 @@
 
 class PlayerType;
 void init_angband(PlayerType *player_ptr, bool no_term);
-void init_file_paths(const std::filesystem::path &libpath);
+void init_file_paths(const std::filesystem::path &libpath, const std::filesystem::path &varpath);
+void create_needed_dirs();
diff --git a/src/system/grafmode.cpp b/src/system/grafmode.cpp
new file mode 100644 (file)
index 0000000..eef4c39
--- /dev/null
@@ -0,0 +1,511 @@
+/**
+ * \file grafmode.cpp
+ * \brief Load a list of possible graphics modes.
+ *
+ * Copyright (c) 2011 Brett Reid
+ *
+ * This work is free software; you can redistribute it and/or modify it
+ * under the terms of either:
+ *
+ * a) the GNU General Public License as published by the Free Software
+ *    Foundation, version 2, or
+ *
+ * b) the "Angband license":
+ *    This software may be copied and distributed for educational, research,
+ *    and not for profit purposes provided that this copyright and statement
+ *    are included in all such copies.  Other copyrights may also apply.
+ */
+/*
+ * Imported from Angband 4.2.0 to support main-cocoa.mm for Hengband.  Did not
+ * bring over the datafile parsing (datafile.h and datafile.c) so the
+ * implementation here is different from that in Angband.
+ */
+#include "system/angband.h"
+#include "system/grafmode.h"
+#include "io/files-util.h"
+#include "util/angband-files.h"
+#include "util/string-processor.h"
+#include "view/display-messages.h"
+
+#define GFPARSE_HAVE_NOTHING (0)
+#define GFPARSE_HAVE_NAME (1)
+#define GFPARSE_HAVE_DIR (2)
+#define GFPARSE_HAVE_SIZE (4)
+#define GFPARSE_HAVE_PREF (8)
+#define GFPARSE_HAVE_EXTRA (16)
+#define GFPARSE_HAVE_GRAF (32)
+
+typedef struct GrafModeParserState {
+    graphics_mode* list;
+    char* file_name;
+    int dir_len;
+    int line_no;
+    int stage;
+    errr result;
+} GrafModeParserState;
+
+
+/* This is the global state exposed to Angband. */
+graphics_mode *graphics_modes;
+graphics_mode *current_graphics_mode = NULL;
+int graphics_mode_high_id;
+
+
+static int check_last_mode(GrafModeParserState* pgps) {
+    int result = 0;
+
+    if ((pgps->stage & GFPARSE_HAVE_DIR) == 0) {
+       result = 1;
+       msg_format("no directory set for tile set, %s, in %s",
+                  pgps->list->menuname, pgps->file_name);
+    }
+    if ((pgps->stage & GFPARSE_HAVE_SIZE) == 0) {
+       result = 1;
+       msg_format("no size set for tile set, %s, in %s",
+                  pgps->list->menuname, pgps->file_name);
+    }
+    if ((pgps->stage & GFPARSE_HAVE_PREF) == 0) {
+       result = 1;
+       msg_format("no preference file for tile set, %s, in %s",
+                  pgps->list->menuname, pgps->file_name);
+    }
+    if ((pgps->stage & GFPARSE_HAVE_GRAF) == 0) {
+       result = 1;
+       msg_format("no graf string set for tile set, %s, in %s",
+                  pgps->list->menuname, pgps->file_name);
+    }
+    return result;
+}
+
+
+static void parse_line(GrafModeParserState* pgps, const char* line) {
+    static const char whitespc[] = " \t\v\f";
+    int offset = 0;
+    int stage;
+
+    while (line[offset] && strchr(whitespc, line[offset]) != 0) {
+       ++offset;
+    }
+    if (! line[offset] || line[offset] == '#') {
+       return;
+    }
+
+    if (strncmp(line + offset, "name:", 5) == 0) {
+       stage = GFPARSE_HAVE_NAME;
+       offset += 5;
+    } else if (strncmp(line + offset, "directory:", 10) == 0) {
+       stage = GFPARSE_HAVE_DIR;
+       offset += 10;
+    } else if (strncmp(line + offset, "size:", 5) == 0) {
+       stage = GFPARSE_HAVE_SIZE;
+       offset += 5;
+    } else if (strncmp(line + offset, "pref:", 5) == 0) {
+       stage = GFPARSE_HAVE_PREF;
+       offset += 5;
+    } else if (strncmp(line + offset, "extra:", 6) == 0) {
+       stage = GFPARSE_HAVE_EXTRA;
+       offset += 6;
+    } else if (strncmp(line + offset, "graf:", 5) == 0) {
+       stage = GFPARSE_HAVE_GRAF;
+       offset += 5;
+    } else {
+        msg_format("Unexpected data at line %d of %s", pgps->line_no,
+                  pgps->file_name);
+       pgps->result = 1;
+       return;
+    }
+
+    if (stage == GFPARSE_HAVE_NAME) {
+       graphics_mode *new_mode;
+
+       if (pgps->stage != GFPARSE_HAVE_NOTHING) {
+           if (check_last_mode(pgps) != 0) {
+               pgps->result = 1;
+           }
+       }
+
+       pgps->stage = GFPARSE_HAVE_NAME;
+       new_mode = (graphics_mode *) malloc(sizeof(graphics_mode));
+       if (new_mode == 0) {
+           pgps->result = 1;
+           msg_format("failed memory allocation for tile set "
+                      "information at line %d of %s", pgps->line_no,
+                      pgps->file_name);
+           return;
+       }
+       new_mode->pNext = pgps->list;
+       new_mode->grafID = 0;
+       new_mode->alphablend = 0;
+       new_mode->overdrawRow = 0;
+       new_mode->overdrawMax = 0;
+       new_mode->cell_width = 0;
+       new_mode->cell_height = 0;
+       new_mode->path[0] = '\0';
+       new_mode->pref[0] = '\0';
+       new_mode->file[0] = '\0';
+       new_mode->menuname[0] = '\0';
+       new_mode->graf[0] = '\0';
+       pgps->list = new_mode;
+    } else {
+       if (pgps->stage == GFPARSE_HAVE_NOTHING) {
+           pgps->result = 1;
+           msg_format("values set before tile set name given at line %d"
+                      " of %s", pgps->line_no, pgps->file_name);
+           return;
+       }
+       if (pgps->stage & stage) {
+           msg_format("values set more than once for tile set, %s, at line "
+                      " %d of %s", pgps->list->menuname, pgps->line_no,
+                      pgps->file_name);
+       }
+    }
+
+    switch (stage) {
+    case GFPARSE_HAVE_NAME:
+       {
+           unsigned int id;
+           int nscan;
+
+           if (sscanf(line + offset, "%u:%n", &id, &nscan) == 1) {
+               if (id > 255) {
+                   pgps->result = 1;
+                   msg_format("ID greater than 255 for tile set at line"
+                              " %d of %s", pgps->line_no, pgps->file_name);
+               } else if (id == GRAPHICS_NONE) {
+                   pgps->result = 1;
+                   msg_format("ID of tile set matches value, %d, reserved "
+                              " for no graphics at line %d of %s",
+                              GRAPHICS_NONE, pgps->line_no, pgps->file_name);
+               } else {
+                   graphics_mode *mode = pgps->list->pNext;
+
+                   while (1) {
+                       if (mode == 0) {
+                           break;
+                       }
+                       if (mode->grafID == id) {
+                           pgps->result = 1;
+                           msg_format("ID for tile set, %s, at line %d of %s"
+                                      " is the same as for tile set %s",
+                                      pgps->list->menuname, pgps->line_no,
+                                      pgps->file_name, mode->menuname);
+                           break;
+                       }
+                       mode = mode->pNext;
+                   }
+                   pgps->list->grafID = id;
+               }
+               offset += nscan;
+               if (strlen(line + offset) >= sizeof(pgps->list->menuname)) {
+                   pgps->result = 1;
+                   msg_format("name is too long for tile set at line %d"
+                              " of %s", pgps->line_no, pgps->file_name);
+               } else if (line[offset] == '\0') {
+                   pgps->result = 1;
+                   msg_format("empty name for tile set at line %d of %s",
+                              pgps->line_no, pgps->file_name);
+               } else {
+                   strcpy(pgps->list->menuname, line + offset);
+               }
+           } else {
+               pgps->result = 1;
+               msg_format("malformed ID for tile set at line %d of %s",
+                          pgps->line_no, pgps->file_name);
+           }
+       }
+       break;
+
+    case GFPARSE_HAVE_DIR:
+       {
+           size_t len = strlen(line + offset);
+           size_t sep_len = strlen(PATH_SEP);
+
+           if (len >= sizeof(pgps->list->path) ||
+               len + pgps->dir_len + sep_len >= sizeof(pgps->list->path)) {
+               pgps->result = 1;
+               msg_format("directory name is too long for tile set, %s, at"
+                          " line %d of %s", pgps->list->menuname,
+                          pgps->line_no, pgps->file_name);
+           } else if (line[offset] == '\0') {
+               pgps->result = 1;
+               msg_format("empty directory name for tile set, %s, at line"
+                          " line %d of %s", pgps->list->menuname,
+                          pgps->line_no, pgps->file_name);
+           } else {
+               /*
+                * Temporarily hack the path to list.txt so it is not necessary
+                * to separately store the base directory for the tile files.
+                */
+               char chold = pgps->file_name[pgps->dir_len];
+               std::filesystem::path p;
+
+               pgps->file_name[pgps->dir_len] = '\0';
+               p = path_build(std::filesystem::path(pgps->file_name,
+                       std::filesystem::path::native_format), line + offset);
+               pgps->file_name[pgps->dir_len] = chold;
+               angband_strcpy(pgps->list->path, p.native().data(),
+                       sizeof(pgps->list->path));
+           }
+       }
+       break;
+
+    case GFPARSE_HAVE_SIZE:
+       {
+           unsigned w, h;
+           int nscan;
+
+           if (sscanf(line + offset, "%u:%u:%n", &w, &h, &nscan) == 2) {
+               if (w > 0) {
+                   pgps->list->cell_width = w;
+               } else {
+                   pgps->result = 1;
+                   msg_format("zero width for tile set, %s, at line"
+                              " %d of %s", pgps->list->menuname,
+                              pgps->line_no, pgps->file_name);
+               }
+               if (h > 0) {
+                   pgps->list->cell_height = h;
+               } else {
+                   pgps->result = 1;
+                   msg_format("zero height for tile set, %s, at line"
+                              " %d of %s", pgps->list->menuname,
+                              pgps->line_no, pgps->file_name);
+               }
+               offset += nscan;
+               if (strlen(line + offset) >= sizeof(pgps->list->file)) {
+                   pgps->result = 1;
+                   msg_format("file name is too long for tile set, %s,"
+                              " at line %d of %s", pgps->list->menuname,
+                              pgps->line_no, pgps->file_name);
+               } else if (line[offset] == '\0') {
+                   pgps->result = 1;
+                   msg_format("empty file name for tile set, %s, at line %d"
+                              " of %s", pgps->list->menuname, pgps->line_no,
+                              pgps->file_name);
+               } else {
+                   (void) strcpy(pgps->list->file, line + offset);
+               }
+           } else {
+               pgps->result = 1;
+               msg_format("malformed dimensions for tile set, %s, at line"
+                          " %d of %s", pgps->list->menuname, pgps->line_no,
+                          pgps->file_name);
+           }
+       }
+       break;
+
+    case GFPARSE_HAVE_PREF:
+       if (strlen(line + offset) >= sizeof(pgps->list->pref)) {
+           pgps->result = 1;
+           msg_format("preference file name is too long for tile set, %s, "
+                      "at line %d of %s", pgps->list->menuname, pgps->line_no,
+                      pgps->file_name);
+       } else if (line[offset] == '\0') {
+           pgps->result = 1;
+           msg_format("empty preference file name for tile set, %s, "
+                      "at line %d of %s", pgps->list->menuname, pgps->line_no,
+                      pgps->file_name);
+       } else {
+           strcpy(pgps->list->pref, line + offset);
+       }
+       break;
+
+    case GFPARSE_HAVE_EXTRA:
+       {
+           unsigned int alpha, startdbl, enddbl;
+           int nscan;
+
+           if (sscanf(line + offset, "%u:%u:%u%n",
+                      &alpha, &startdbl, &enddbl, &nscan) == 3 &&
+               (line[offset + nscan] == '\0' ||
+                strchr(whitespc, line[offset + nscan]) != 0 ||
+                line[offset + nscan] == '#')) {
+               if (startdbl > 255 || enddbl > 255) {
+                   pgps->result = 1;
+                   msg_format("overdrawMax or overdrawRow is greater than"
+                              " 255 for tile set, %s, at line %d of %s",
+                              pgps->list->menuname, pgps->line_no,
+                              pgps->file_name);
+               } else if (enddbl < startdbl) {
+                   pgps->result = 1;
+                   msg_format("overdrawMax less than overdrawRow for tile"
+                              "set, %s, at line %d of %s",
+                              pgps->list->menuname, pgps->line_no,
+                              pgps->file_name);
+               } else {
+                   pgps->list->alphablend = (alpha != 0);
+                   pgps->list->overdrawRow = startdbl;
+                   pgps->list->overdrawMax = enddbl;
+               }
+           } else {
+               pgps->result = 1;
+               msg_format("malformed data for tile set, %s, at line %d of"
+                          " %s", pgps->list->menuname, pgps->line_no,
+                          pgps->file_name);
+           }
+       }
+       break;
+
+    case GFPARSE_HAVE_GRAF:
+       if (strlen(line + offset) >= sizeof(pgps->list->graf)) {
+           pgps->result = 1;
+           msg_format("graf string is too long for tile set, %s, at line %d"
+                      " of %s", pgps->list->menuname, pgps->line_no,
+                      pgps->file_name);
+       } else if (line[offset] == '\0') {
+           pgps->result = 1;
+           msg_format("empty graf string for tile set, %s, at line %d of %s",
+                      pgps->list->menuname, pgps->line_no, pgps->file_name);
+       } else {
+           strcpy(pgps->list->graf, line + offset);
+       }
+       break;
+    }
+
+    if (pgps->result == 0) {
+       pgps->stage |= stage;
+    }
+}
+
+
+static void finish_parse_grafmode(GrafModeParserState* pgps,
+                                 int transfer_results) {
+    /*
+     * Check what was read for the last mode parsed, since parse_line did
+     * not.
+     */
+    if (transfer_results) {
+       if (pgps->list == 0 || pgps->stage == GFPARSE_HAVE_NOTHING) {
+           msg_format("no graphics modes in %s", pgps->file_name);
+       } else {
+           if (check_last_mode(pgps) != 0) {
+               transfer_results = 0;
+               pgps->result = 1;
+           }
+       }
+    }
+
+    if (transfer_results) {
+       graphics_mode *mode = pgps->list;
+       int max = GRAPHICS_NONE;
+       int count = 0;
+       graphics_mode *new_list;
+
+       while (mode) {
+           if (mode->grafID > max) {
+               max = mode->grafID;
+           }
+           ++count;
+           mode = mode->pNext;
+       }
+
+       /* Assemble the modes into a contiguous block of memory. */
+       new_list = (graphics_mode *)
+           malloc(sizeof(graphics_mode) * (count + 1));
+       if (new_list != 0) {
+           int i;
+
+           mode = pgps->list;
+           for (i = count - 1; i >= 0; --i, mode = mode->pNext) {
+               memcpy(&(new_list[i]), mode, sizeof(graphics_mode));
+               new_list[i].pNext = &(new_list[i + 1]);
+           }
+
+           /* Hardcode the no graphics option. */
+           new_list[count].pNext = NULL;
+           new_list[count].grafID = GRAPHICS_NONE;
+           new_list[count].alphablend = 0;
+           new_list[count].overdrawRow = 0;
+           new_list[count].overdrawMax = 0;
+           strncpy(
+               new_list[count].pref, "none", sizeof(new_list[count].pref));
+           strncpy(
+               new_list[count].path, "", sizeof(new_list[count].path));
+           strncpy(
+               new_list[count].file, "", sizeof(new_list[count].file));
+           strncpy(
+               new_list[count].menuname,
+               "Classic ASCII",
+               sizeof(new_list[count].menuname)
+           );
+           strncpy(
+               new_list[count].graf, "ascii", sizeof(new_list[count].graf));
+
+           /* Release the old global state. */
+           close_graphics_modes();
+
+           graphics_modes = new_list;
+           graphics_mode_high_id = max;
+           /* Set the default graphics mode to be no graphics */
+           current_graphics_mode = &(graphics_modes[count]);
+       } else {
+           pgps->result = 1;
+           msg_print("failed memory allocation for new graphics modes");
+       }
+    }
+
+    /* Release the memory allocated for parsing the file. */
+    while (pgps->list != 0) {
+       graphics_mode *mode = pgps->list;
+
+       pgps->list = mode->pNext;
+       free(mode);
+    }
+}
+
+
+bool init_graphics_modes(void) {
+    char buf[1024];
+    char line[1024];
+    GrafModeParserState gps = { 0, buf, 0, 0, GFPARSE_HAVE_NOTHING, 0 };
+    std::filesystem::path p;
+    FILE *f;
+
+    /* Build the filename */
+    p = path_build(ANGBAND_DIR_XTRA, "graf");
+    angband_strcpy(line, p.native().data(), sizeof(line));
+    gps.dir_len = strlen(line);
+    p = path_build(p, "list.txt");
+    angband_strcpy(buf, p.native().data(), sizeof(buf));
+
+    f = angband_fopen(buf, FileOpenMode::READ);
+    if (!f) {
+       msg_format("Cannot open '%s'.", buf);
+       gps.result = 1;
+    } else {
+       while (angband_fgets(f, line, sizeof line) == 0) {
+           ++gps.line_no;
+           parse_line(&gps, line);
+           if (gps.result != 0) {
+               break;
+           }
+       }
+
+       finish_parse_grafmode(&gps, gps.result == 0);
+       angband_fclose(f);
+    }
+
+    /* Result */
+    return gps.result == 0;
+}
+
+
+void close_graphics_modes(void) {
+    if (graphics_modes) {
+       free(graphics_modes);
+       graphics_modes = NULL;
+    }
+    current_graphics_mode = NULL;
+}
+
+
+graphics_mode *get_graphics_mode(byte id) {
+    graphics_mode *test = graphics_modes;
+    while (test) {
+       if (test->grafID == id) {
+           return test;
+       }
+       test = test->pNext;
+    }
+    return NULL;
+}
diff --git a/src/system/grafmode.h b/src/system/grafmode.h
new file mode 100644 (file)
index 0000000..d2acf03
--- /dev/null
@@ -0,0 +1,72 @@
+/**
+ * \file grafmode.h
+ * \brief Load a list of possible graphics modes.
+ *
+ * Copyright (c) 2011 Brett Reid
+ *
+ * This work is free software; you can redistribute it and/or modify it
+ * under the terms of either:
+ *
+ * a) the GNU General Public License as published by the Free Software
+ *    Foundation, version 2, or
+ *
+ * b) the "Angband licence":
+ *    This software may be copied and distributed for educational, research,
+ *    and not for profit purposes provided that this copyright and statement
+ *    are included in all such copies.  Other copyrights may also apply.
+ */
+/* Imported from Angband 4.2 to support main-cocoa.m in Hengband. */
+#ifndef INCLUDED_GRAFMODE_H
+#define INCLUDED_GRAFMODE_H
+
+#include "system/h-basic.h"
+
+/**
+ * Default graphic modes
+ */
+#define GRAPHICS_NONE           0
+
+
+/**
+ * Specifications for graphics modes.
+ * 
+ * grafID:      ID of tile set should be >0 and unique for anything new.
+ * alphablend:  Bool whether or not the tileset needs alpha blending.
+ * overdrawRow: Row in the file where tiles in that row or lower draw the tile
+ *              above as well.
+ * overdrawMax: Row in the file where tiles in that row or above draw the tile
+ *              above as well.
+ * cell_width:  Width of an individual tile in pixels.
+ * cell_height: Height of an individual tile in pixels.
+ * pref:        Value of ANGBAND_GRAF variable.
+ * file:        Name of PNG file (if any).
+ * menuname:    Name of the tileset in menu.
+ */
+typedef struct _graphics_mode {
+       struct _graphics_mode *pNext;
+       byte grafID;
+       byte alphablend;
+       byte overdrawRow;
+       byte overdrawMax;
+       uint16_t cell_width;
+       uint16_t cell_height;
+       char path[256];
+       char pref[32];
+       char file[32];
+       char menuname[32];
+        /*
+        * This is a hack for Hengband:  ANGBAND_GRAF set to this rather
+        * then the pref field above.
+        */
+        char graf[32];
+} graphics_mode;
+
+extern graphics_mode *graphics_modes;
+extern graphics_mode *current_graphics_mode;
+extern int graphics_mode_high_id;
+
+bool init_graphics_modes();
+void close_graphics_modes(void);
+graphics_mode* get_graphics_mode(byte id);
+
+#endif /* INCLUDED_GRAFMODE_H */
index 151fbc2..b6e9aaa 100644 (file)
@@ -88,6 +88,21 @@ constexpr auto MAINTAINER = "echizen@users.sourceforge.jp";
   #define DEFAULT_X11_FONT_6 DEFAULT_X11_FONT_SUB
   #define DEFAULT_X11_FONT_7 DEFAULT_X11_FONT_SUB
   
+  /*
+   * Hack -- Mach-O (native binary format of OS X) is basically a Un*x
+   * but has Mac OS/Windows-like user interface.  Disabling the Un*x-like
+   * behavior (PRIVATE_USER_PATH, SAVEFILE_USE_UID) for the modern Mac OS X
+   * interface (MACH_O_COCOA).
+   */
+  #if defined(MACH_O_CARBON) || defined(MACH_O_COCOA)
+  #ifdef PRIVATE_USER_PATH
+  #undef PRIVATE_USER_PATH
+  #endif
+  #ifdef SAVEFILE_USE_UID
+  #undef SAVEFILE_USE_UID
+  #endif
+  #endif
+
   #ifdef JP
     #ifdef EUC
       #define iskanji(x) (iseuckanji(x))
index 245edef..f62db2d 100644 (file)
@@ -273,6 +273,14 @@ errr angband_fgets(FILE *fff, char *buf, ulong n)
         guess_convert_to_system_encoding(file_read__tmp.data(), FILE_READ_BUFF_SIZE);
 #endif
         for (s = file_read__tmp.data(); *s; s++) {
+#ifdef MACH_O_COCOA
+            /*
+             * Be nice to the Macintosh, where a file can have Mac or Unix
+             * end of line, especially since the introduction of OS X.
+             * MPW tools were also very tolerant to the Unix EOL.
+             */
+            if (*s == '\r') *s = '\n';
+#endif /* MACH_O_COCOA */
             if (*s == '\n') {
                 buf[i] = '\0';
                 return 0;
index 81ea646..6eff8fd 100644 (file)
@@ -48,8 +48,7 @@ const std::string image_monsters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQR
 static void image_object(TERM_COLOR *ap, char *cp)
 {
     if (use_graphics) {
-        std::span<BaseitemInfo> candidates(baseitems_info.begin() + 1, baseitems_info.end());
-        const auto &baseitem = rand_choice(candidates);
+        const auto &baseitem = baseitems_info[randint1(baseitems_info.size() - 1)];
         *cp = baseitem.x_char;
         *ap = baseitem.x_attr;
         return;