3 * \brief OS X front end
5 * Copyright (c) 2011 Peter Ammon
7 * This work is free software; you can redistribute it and/or modify it
8 * under the terms of either:
10 * a) the GNU General Public License as published by the Free Software
11 * Foundation, version 2, or
13 * b) the "Angband licence":
14 * This software may be copied and distributed for educational, research,
15 * and not for profit purposes provided that this copyright and statement
16 * are included in all such copies. Other copyrights may also apply.
20 /* This is not included in angband.h in Hengband. */
23 #if defined(MACH_O_COCOA)
26 #include <cocoa/AppDelegate.h>
27 //#include <Carbon/Carbon.h> /* For keycodes */
28 /* Hack - keycodes to enable compiling in macOS 10.14 */
29 #define kVK_Return 0x24
31 #define kVK_Delete 0x33
32 #define kVK_Escape 0x35
33 #define kVK_ANSI_KeypadEnter 0x4C
35 static NSString * const AngbandDirectoryNameLib = @"lib";
36 static NSString * const AngbandDirectoryNameBase = @"Hengband";
38 static NSString * const AngbandMessageCatalog = @"Localizable";
39 static NSString * const AngbandTerminalsDefaultsKey = @"Terminals";
40 static NSString * const AngbandTerminalRowsDefaultsKey = @"Rows";
41 static NSString * const AngbandTerminalColumnsDefaultsKey = @"Columns";
42 static NSString * const AngbandTerminalVisibleDefaultsKey = @"Visible";
43 static NSString * const AngbandGraphicsDefaultsKey = @"GraphicsID";
44 static NSString * const AngbandBigTileDefaultsKey = @"UseBigTiles";
45 static NSString * const AngbandFrameRateDefaultsKey = @"FramesPerSecond";
46 static NSString * const AngbandSoundDefaultsKey = @"AllowSound";
47 static NSInteger const AngbandWindowMenuItemTagBase = 1000;
48 static NSInteger const AngbandCommandMenuItemTagBase = 2000;
50 /* We can blit to a large layer or image and then scale it down during live
51 * resize, which makes resizing much faster, at the cost of some image quality
53 #ifndef USE_LIVE_RESIZE_CACHE
54 # define USE_LIVE_RESIZE_CACHE 1
57 /* Global defines etc from Angband 3.5-dev - NRM */
58 #define ANGBAND_TERM_MAX 8
60 static bool new_game = TRUE;
62 #define MAX_COLORS 256
63 #define MSG_MAX SOUND_MAX
65 /* End Angband stuff - NRM */
67 /* Application defined event numbers */
70 AngbandEventWakeup = 1
73 /* Redeclare some 10.7 constants and methods so we can build on 10.6 */
76 Angband_NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7,
77 Angband_NSWindowCollectionBehaviorFullScreenAuxiliary = 1 << 8
80 @interface NSWindow (AngbandLionRedeclares)
81 - (void)setRestorable:(BOOL)flag;
84 /* Delay handling of pre-emptive "quit" event */
85 static BOOL quit_when_ready = FALSE;
87 /* Set to indicate the game is over and we can quit without delay */
88 static Boolean game_is_finished = FALSE;
90 /* Our frames per second (e.g. 60). A value of 0 means unthrottled. */
91 static int frames_per_second;
96 * Load sound effects based on sound.cfg within the xtra/sound directory;
97 * bridge to Cocoa to use NSSound for simple loading and playback, avoiding
98 * I/O latency by caching all sounds at the start. Inherits full sound
99 * format support from Quicktime base/plugins.
100 * pelpel favoured a plist-based parser for the future but .cfg support
101 * improves cross-platform compatibility.
103 @interface AngbandSoundCatalog : NSObject {
106 * Stores instances of NSSound keyed by path so the same sound can be
107 * used for multiple events.
109 NSMutableDictionary *soundsByPath;
111 * Stores arrays of NSSound keyed by event number.
113 NSMutableDictionary *soundArraysByEvent;
117 * If NO, then playSound effectively becomes a do nothing operation.
119 @property (getter=isEnabled) BOOL enabled;
122 * Set up for lazy initialization in playSound(). Set enabled to NO.
127 * Releases the resources acquired for the catalog.
132 * If self.enabled is YES and the given event has one or more sounds
133 * corresponding to it in the catalog, plays one of those sounds, chosen at
136 - (void)playSound:(int)event;
139 * Impose an arbitary limit on number of possible samples per event.
140 * Currently not declaring this as a class property for compatibility with
141 * versions of Xcode prior to 8.
146 * Return the shared sound catalog instance, creating it if it does not
147 * exist yet. Currently not declaring this as a class property for
148 * compatibility with versions of Xcode prior to 8.
150 + (AngbandSoundCatalog*)sharedSounds;
153 * Release any resouces associated with shared sounds.
155 + (void)clearSharedSounds;
160 * This is a helper function for AngbandSoundCatalog's sharedSounds and
161 * clearSharedSounds. Implement a Singleton pattern.
163 static __strong AngbandSoundCatalog** get_shared_sounds(void)
165 static __strong AngbandSoundCatalog* catalog = nil;
170 @implementation AngbandSoundCatalog
173 if (self = [super init]) {
174 self->soundsByPath = nil;
175 self->soundArraysByEvent = nil;
182 self->soundsByPath = nil;
183 self->soundArraysByEvent = nil;
186 - (void)playSound:(int)event {
187 if (! self.enabled) {
191 /* Initialize when the first sound is played. */
192 if (self->soundArraysByEvent == nil) {
193 /* Build the "sound" path */
194 char sound_dir[1024];
195 path_build(sound_dir, sizeof(sound_dir), ANGBAND_DIR_XTRA, "sound");
197 /* Find and open the config file */
199 path_build(path, sizeof(path), sound_dir, "sound.cfg");
200 FILE *fff = my_fopen(path, "r");
204 NSLog(@"The sound configuration file could not be opened.");
208 self->soundsByPath = [[NSMutableDictionary alloc] init];
209 self->soundArraysByEvent = [[NSMutableDictionary alloc] init];
212 * This loop may take a while depending on the count and size of
217 /* Lines are always of the form "name = sample [sample ...]" */
219 while (my_fgets(fff, buffer, sizeof(buffer)) == 0) {
221 char *cfg_sample_list;
227 /* Skip anything not beginning with an alphabetic character */
228 if (!buffer[0] || !isalpha((unsigned char)buffer[0])) continue;
230 /* Split the line into two: message name, and the rest */
231 search = strchr(buffer, ' ');
232 cfg_sample_list = strchr(search + 1, ' ');
233 if (!search) continue;
234 if (!cfg_sample_list) continue;
236 /* Set the message name, and terminate at first space */
240 /* Make sure this is a valid event name */
241 for (event = MSG_MAX - 1; event >= 0; event--) {
242 if (strcmp(msg_name, angband_sound_name[event]) == 0)
245 if (event < 0) continue;
248 * Advance the sample list pointer so it's at the beginning of
252 if (!cfg_sample_list[0]) continue;
254 /* Terminate the current token */
255 cur_token = cfg_sample_list;
256 search = strchr(cur_token, ' ');
259 next_token = search + 1;
265 * Now we find all the sample names and add them one by one
268 NSMutableArray *soundSamples =
269 [self->soundArraysByEvent
270 objectForKey:[NSNumber numberWithInteger:event]];
271 if (soundSamples == nil) {
272 soundSamples = [[NSMutableArray alloc] init];
273 [self->soundArraysByEvent
274 setObject:soundSamples
275 forKey:[NSNumber numberWithInteger:event]];
277 int num = (int) soundSamples.count;
279 /* Don't allow too many samples */
280 if (num >= [AngbandSoundCatalog maxSamples]) break;
282 NSString *token_string =
283 [NSString stringWithUTF8String:cur_token];
285 [self->soundsByPath objectForKey:token_string];
289 * We have to load the sound. Build the path to the
292 path_build(path, sizeof(path), sound_dir, cur_token);
294 if (stat(path, &stb) == 0) {
295 /* Load the sound into memory */
296 sound = [[NSSound alloc]
297 initWithContentsOfFile:[NSString stringWithUTF8String:path]
300 [self->soundsByPath setObject:sound
301 forKey:token_string];
306 /* Store it if we loaded it */
308 [soundSamples addObject:sound];
311 /* Figure out next token */
312 cur_token = next_token;
314 /* Try to find a space */
315 search = strchr(cur_token, ' ');
318 * If we can find one, terminate, and set new "next".
322 next_token = search + 1;
324 /* Otherwise prevent infinite looping */
337 NSMutableArray *samples =
338 [self->soundArraysByEvent
339 objectForKey:[NSNumber numberWithInteger:event]];
341 if (samples == nil || samples.count == 0) {
345 /* Choose a random event. */
346 int s = randint0((int) samples.count);
347 NSSound *sound = samples[s];
349 if ([sound isPlaying])
352 /* Play the sound. */
362 * For sharedSounds and clearSharedSounds.
364 static __strong AngbandSoundCatalog* gSharedSounds = nil;
366 + (AngbandSoundCatalog*)sharedSounds {
367 if (gSharedSounds == nil) {
368 gSharedSounds = [[AngbandSoundCatalog alloc] init];
370 return gSharedSounds;;
373 + (void)clearSharedSounds {
380 * To handle fonts where an individual glyph's bounding box can extend into
381 * neighboring columns, Term_curs_cocoa(), Term_pict_cocoa(),
382 * Term_text_cocoa(), and Term_wipe_cocoa() merely record what needs to be
383 * done with the actual drawing happening in response to the notification to
384 * flush all rows, the TERM_XTRA_FRESH case in Term_xtra_cocoa(). Can not use
385 * the TERM_XTRA_FROSH notification (the per-row flush), since with a software
386 * cursor, there are calls to Term_pict_cocoa(), Term_text_cocoa(), or
387 * Term_wipe_cocoa() to take care of the old cursor position which are not
388 * followed by a row flush.
390 enum PendingCellChangeType {
391 CELL_CHANGE_NONE = 0,
396 struct PendingTextChange {
400 * Is YES if glyph is a character that takes up two columns (i.e.
401 * Japanese kanji); otherwise it is NO.
405 struct PendingTileChange {
406 char fgdCol, fgdRow, bckCol, bckRow;
408 struct PendingCellChange {
409 union { struct PendingTextChange txc; struct PendingTileChange tic; } v;
410 enum PendingCellChangeType changeType;
413 @interface PendingTermChanges : NSObject {
416 struct PendingCellChange **changesByRow;
420 * Returns YES if nCol and nRow are a feasible size for the pending changes.
421 * Otherwise, returns NO.
423 + (BOOL)isValidSize:(int)nCol rows:(int)nRow;
426 * Initialize with zero columns and zero rows.
431 * Initialize with nCol columns and nRow rows. No changes will be marked.
433 - (id)initWithColumnsRows:(int)nCol rows:(int)nRow NS_DESIGNATED_INITIALIZER;
436 * Clears all marked changes.
441 * Changes the bounds over which changes are recorded. Has the side effect
442 * of clearing any marked changes. Will throw an exception if nCol or nRow
445 - (void)resize:(int)nCol rows:(int)nRow;
448 * Mark the cell, (iCol, iRow), as having changed text.
450 - (void)markTextChange:(int)iCol row:(int)iRow glyph:(wchar_t)g color:(int)c
451 isDoubleWidth:(BOOL)dw;
454 * Mark the cell, (iCol, iRow), as having a changed tile.
456 - (void)markTileChange:(int)iCol row:(int)iRow
457 foregroundCol:(char)fc foregroundRow:(char)fr
458 backgroundCol:(char)bc backgroundRow:(char)br;
461 * Mark the cells from (iCol, iRow) to (iCol + nCol - 1, iRow) as wiped.
463 - (void)markWipeRange:(int)iCol row:(int)iRow n:(int)nCol;
466 * Mark the location of the cursor. The cursor will be the standard size:
469 - (void)markCursor:(int)iCol row:(int)iRow;
472 * Mark the location of the cursor. The cursor will be w cells wide and
473 * h cells tall and the given location is the position of the upper left
476 - (void)markBigCursor:(int)iCol row:(int)iRow
477 cellsWide:(int)w cellsHigh:(int)h;
480 * Return the zero-based index of the first column changed for the given
481 * zero-based row index. If there are no changes in the row, the returned
482 * value will be the number of columns.
484 - (int)getFirstChangedColumnInRow:(int)iRow;
487 * Return the zero-based index of the last column changed for the given
488 * zero-based row index. If there are no changes in the row, the returned
491 - (int)getLastChangedColumnInRow:(int)iRow;
494 * Return the type of change at the given cell, (iCol, iRow).
496 - (enum PendingCellChangeType)getCellChangeType:(int)iCol row:(int)iRow;
499 * Return the nature of a text change at the given cell, (iCol, iRow).
500 * Will throw an exception if [obj getCellChangeType:iCol row:iRow] is
501 * neither CELL_CHANGE_TEXT nor CELL_CHANGE_WIPE.
503 - (struct PendingTextChange)getCellTextChange:(int)iCol row:(int)iRow;
506 * Return the nature of a tile change at the given cell, (iCol, iRow).
507 * Will throw an exception if [obj getCellChangeType:iCol row:iRow] is
508 * different than CELL_CHANGE_TILE.
510 - (struct PendingTileChange)getCellTileChange:(int)iCol row:(int)iRow;
513 * Is the number of columns for recording changes.
515 @property (readonly) int columnCount;
518 * Is the number of rows for recording changes.
520 @property (readonly) int rowCount;
523 * Will be YES if there are any pending changes to locations rendered as text.
524 * Otherwise, it will be NO.
526 @property (readonly) BOOL hasTextChanges;
529 * Will be YES if there are any pending changes to locations rendered as tiles.
530 * Otherwise, it will be NO.
532 @property (readonly) BOOL hasTileChanges;
535 * Will be YES if there are any pending wipes. Otherwise, it will be NO.
537 @property (readonly) BOOL hasWipeChanges;
540 * Is the zero-based index of the first row with changes. Will be equal to
541 * the number of rows if there are no changes.
543 @property (readonly) int firstChangedRow;
546 * Is the zero-based index of the last row with changes. Will be equal to
547 * -1 if there are no changes.
549 @property (readonly) int lastChangedRow;
552 * Is the zero-based index for the column with the upper left corner of the
553 * cursor. It will be -1 if the cursor position has not been set since the
554 * changes were cleared.
556 @property (readonly) int cursorColumn;
559 * Is the zero-based index for the row with the upper left corner of the
560 * cursor. It will be -1 if the cursor position has not been set since the
561 * changes were cleared.
563 @property (readonly) int cursorRow;
566 * Is the cursor width in number of cells.
568 @property (readonly) int cursorWidth;
571 * Is the cursor height in number of cells.
573 @property (readonly) int cursorHeight;
576 * This is a helper for the mark* messages.
578 - (void)setupForChange:(int)iCol row:(int)iRow n:(int)nCol;
581 * Throw an exception if the given range of column indices is invalid
582 * (including non-positive values for nCol).
584 - (void)checkColumnIndices:(int)iCol n:(int)nCol;
587 * Throw an exception if the given row index is invalid.
589 - (void)checkRowIndex:(int)iRow;
593 @implementation PendingTermChanges
595 + (BOOL)isValidSize:(int)nCol rows:(int)nRow
598 (size_t) nCol > SIZE_MAX / sizeof(struct PendingCellChange) ||
600 (size_t) nRow > SIZE_MAX / sizeof(struct PendingCellChange*) ||
601 (size_t) nRow > SIZE_MAX / (2 * sizeof(int))) {
609 return [self initWithColumnsRows:0 rows:0];
612 - (id)initWithColumnsRows:(int)nCol rows:(int)nRow
614 if (self = [super init]) {
615 if (! [PendingTermChanges isValidSize:nCol rows:nRow]) {
618 self->colBounds = malloc((size_t) 2 * sizeof(int) * nRow);
619 if (self->colBounds == 0 && nRow > 0) {
622 self->changesByRow = calloc(nRow, sizeof(struct PendingCellChange*));
623 if (self->changesByRow == 0 && nRow > 0) {
624 free(self->colBounds);
627 for (int i = 0; i < nRow + nRow; i += 2) {
628 self->colBounds[i] = nCol;
629 self->colBounds[i + 1] = -1;
631 self->_columnCount = nCol;
632 self->_rowCount = nRow;
633 self->_hasTextChanges = NO;
634 self->_hasTileChanges = NO;
635 self->_hasWipeChanges = NO;
636 self->_firstChangedRow = nRow;
637 self->_lastChangedRow = -1;
638 self->_cursorColumn = -1;
639 self->_cursorRow = -1;
640 self->_cursorWidth = 1;
641 self->_cursorHeight = 1;
648 if (self->changesByRow != 0) {
649 for (int i = 0; i < self.rowCount; ++i) {
650 if (self->changesByRow[i] != 0) {
651 free(self->changesByRow[i]);
652 self->changesByRow[i] = 0;
655 free(self->changesByRow);
656 self->changesByRow = 0;
658 if (self->colBounds != 0) {
659 free(self->colBounds);
666 for (int i = 0; i < self.rowCount; ++i) {
667 self->colBounds[i + i] = self.columnCount;
668 self->colBounds[i + i + 1] = -1;
669 if (self->changesByRow[i] != 0) {
670 free(self->changesByRow[i]);
671 self->changesByRow[i] = 0;
674 self->_hasTextChanges = NO;
675 self->_hasTileChanges = NO;
676 self->_hasWipeChanges = NO;
677 self->_firstChangedRow = self.rowCount;
678 self->_lastChangedRow = -1;
679 self->_cursorColumn = -1;
680 self->_cursorRow = -1;
681 self->_cursorWidth = 1;
682 self->_cursorHeight = 1;
685 - (void)resize:(int)nCol rows:(int)nRow
687 if (! [PendingTermChanges isValidSize:nCol rows:nRow]) {
688 NSException *exc = [NSException
689 exceptionWithName:@"PendingTermChangesRowsColumns"
690 reason:@"resize called with number of columns or rows that is negative or too large"
695 int *cb = malloc((size_t) 2 * sizeof(int) * nRow);
696 struct PendingCellChange** cbr =
697 calloc(nRow, sizeof(struct PendingCellChange*));
700 if ((cb == 0 || cbr == 0) && nRow > 0) {
708 NSException *exc = [NSException
709 exceptionWithName:@"OutOfMemory"
710 reason:@"resize called for PendingTermChanges"
715 for (i = 0; i < nRow; ++i) {
719 if (self->changesByRow != 0) {
720 for (i = 0; i < self.rowCount; ++i) {
721 if (self->changesByRow[i] != 0) {
722 free(self->changesByRow[i]);
723 self->changesByRow[i] = 0;
726 free(self->changesByRow);
728 if (self->colBounds != 0) {
729 free(self->colBounds);
732 self->colBounds = cb;
733 self->changesByRow = cbr;
734 self->_columnCount = nCol;
735 self->_rowCount = nRow;
736 self->_hasTextChanges = NO;
737 self->_hasTileChanges = NO;
738 self->_hasWipeChanges = NO;
739 self->_firstChangedRow = self.rowCount;
740 self->_lastChangedRow = -1;
741 self->_cursorColumn = -1;
742 self->_cursorRow = -1;
743 self->_cursorWidth = 1;
744 self->_cursorHeight = 1;
747 - (void)markTextChange:(int)iCol row:(int)iRow glyph:(wchar_t)g color:(int)c
748 isDoubleWidth:(BOOL)dw
750 [self setupForChange:iCol row:iRow n:((dw) ? 2 : 1)];
751 struct PendingCellChange *pcc = self->changesByRow[iRow] + iCol;
752 pcc->v.txc.glyph = g;
753 pcc->v.txc.color = c;
754 pcc->v.txc.doubleWidth = dw;
755 pcc->changeType = CELL_CHANGE_TEXT;
757 * Fill in a dummy since the previous character will take up two columns.
760 pcc[1].v.txc.glyph = 0;
761 pcc[1].v.txc.color = c;
762 pcc[1].v.txc.doubleWidth = NO;
763 pcc[1].changeType = CELL_CHANGE_TEXT;
765 self->_hasTextChanges = YES;
768 - (void)markTileChange:(int)iCol row:(int)iRow
769 foregroundCol:(char)fc foregroundRow:(char)fr
770 backgroundCol:(char)bc backgroundRow:(char)br
772 [self setupForChange:iCol row:iRow n:1];
773 struct PendingCellChange *pcc = self->changesByRow[iRow] + iCol;
774 pcc->v.tic.fgdCol = fc;
775 pcc->v.tic.fgdRow = fr;
776 pcc->v.tic.bckCol = bc;
777 pcc->v.tic.bckRow = br;
778 pcc->changeType = CELL_CHANGE_TILE;
779 self->_hasTileChanges = YES;
782 - (void)markWipeRange:(int)iCol row:(int)iRow n:(int)nCol
784 [self setupForChange:iCol row:iRow n:nCol];
785 struct PendingCellChange *pcc = self->changesByRow[iRow] + iCol;
786 for (int i = 0; i < nCol; ++i) {
787 pcc[i].v.txc.glyph = 0;
788 pcc[i].v.txc.color = 0;
789 pcc[i].changeType = CELL_CHANGE_WIPE;
791 self->_hasWipeChanges = YES;
794 - (void)markCursor:(int)iCol row:(int)iRow
796 /* Allow negative indices to indicate an invalid cursor. */
797 [self checkColumnIndices:((iCol >= 0) ? iCol : 0) n:1];
798 [self checkRowIndex:((iRow >= 0) ? iRow : 0)];
799 self->_cursorColumn = iCol;
800 self->_cursorRow = iRow;
801 self->_cursorWidth = 1;
802 self->_cursorHeight = 1;
805 - (void)markBigCursor:(int)iCol row:(int)iRow
806 cellsWide:(int)w cellsHigh:(int)h
808 /* Allow negative indices to indicate an invalid cursor. */
809 [self checkColumnIndices:((iCol >= 0) ? iCol : 0) n:1];
810 [self checkRowIndex:((iRow >= 0) ? iRow : 0)];
811 if (w < 1 || h < 1) {
812 NSException *exc = [NSException
813 exceptionWithName:@"InvalidCursorDimensions"
814 reason:@"markBigCursor called for PendingTermChanges"
818 self->_cursorColumn = iCol;
819 self->_cursorRow = iRow;
820 self->_cursorWidth = w;
821 self->_cursorHeight = h;
824 - (void)setupForChange:(int)iCol row:(int)iRow n:(int)nCol
826 [self checkColumnIndices:iCol n:nCol];
827 [self checkRowIndex:iRow];
828 if (self->changesByRow[iRow] == 0) {
829 self->changesByRow[iRow] =
830 malloc(self.columnCount * sizeof(struct PendingCellChange));
831 if (self->changesByRow[iRow] == 0 && self.columnCount > 0) {
832 NSException *exc = [NSException
833 exceptionWithName:@"OutOfMemory"
834 reason:@"setupForChange called for PendingTermChanges"
838 struct PendingCellChange* pcc = self->changesByRow[iRow];
839 for (int i = 0; i < self.columnCount; ++i) {
840 pcc[i].changeType = CELL_CHANGE_NONE;
843 if (self.firstChangedRow > iRow) {
844 self->_firstChangedRow = iRow;
846 if (self.lastChangedRow < iRow) {
847 self->_lastChangedRow = iRow;
849 if ([self getFirstChangedColumnInRow:iRow] > iCol) {
850 self->colBounds[iRow + iRow] = iCol;
852 if ([self getLastChangedColumnInRow:iRow] < iCol + nCol - 1) {
853 self->colBounds[iRow + iRow + 1] = iCol + nCol - 1;
857 - (int)getFirstChangedColumnInRow:(int)iRow
859 [self checkRowIndex:iRow];
860 return self->colBounds[iRow + iRow];
863 - (int)getLastChangedColumnInRow:(int)iRow
865 [self checkRowIndex:iRow];
866 return self->colBounds[iRow + iRow + 1];
869 - (enum PendingCellChangeType)getCellChangeType:(int)iCol row:(int)iRow
871 [self checkColumnIndices:iCol n:1];
872 [self checkRowIndex:iRow];
873 if (iRow < self.firstChangedRow || iRow > self.lastChangedRow) {
874 return CELL_CHANGE_NONE;
876 if (iCol < [self getFirstChangedColumnInRow:iRow] ||
877 iCol > [self getLastChangedColumnInRow:iRow]) {
878 return CELL_CHANGE_NONE;
880 return self->changesByRow[iRow][iCol].changeType;
883 - (struct PendingTextChange)getCellTextChange:(int)iCol row:(int)iRow
885 [self checkColumnIndices:iCol n:1];
886 [self checkRowIndex:iRow];
887 if (iRow < self.firstChangedRow || iRow > self.lastChangedRow ||
888 iCol < [self getFirstChangedColumnInRow:iRow] ||
889 iCol > [self getLastChangedColumnInRow:iRow] ||
890 (self->changesByRow[iRow][iCol].changeType != CELL_CHANGE_TEXT &&
891 self->changesByRow[iRow][iCol].changeType != CELL_CHANGE_WIPE)) {
892 NSException *exc = [NSException
893 exceptionWithName:@"NotTextChange"
894 reason:@"getCellTextChange called for PendingTermChanges"
898 return self->changesByRow[iRow][iCol].v.txc;
901 - (struct PendingTileChange)getCellTileChange:(int)iCol row:(int)iRow
903 [self checkColumnIndices:iCol n:1];
904 [self checkRowIndex:iRow];
905 if (iRow < self.firstChangedRow || iRow > self.lastChangedRow ||
906 iCol < [self getFirstChangedColumnInRow:iRow] ||
907 iCol > [self getLastChangedColumnInRow:iRow] ||
908 self->changesByRow[iRow][iCol].changeType != CELL_CHANGE_TILE) {
909 NSException *exc = [NSException
910 exceptionWithName:@"NotTileChange"
911 reason:@"getCellTileChange called for PendingTermChanges"
915 return self->changesByRow[iRow][iCol].v.tic;
918 - (void)checkColumnIndices:(int)iCol n:(int)nCol
921 NSException *exc = [NSException
922 exceptionWithName:@"InvalidColumnIndex"
923 reason:@"negative column index"
927 if (iCol >= self.columnCount || iCol + nCol > self.columnCount) {
928 NSException *exc = [NSException
929 exceptionWithName:@"InvalidColumnIndex"
930 reason:@"column index exceeds number of columns"
935 NSException *exc = [NSException
936 exceptionWithName:@"InvalidColumnIndex"
937 reason:@"empty column range"
943 - (void)checkRowIndex:(int)iRow
946 NSException *exc = [NSException
947 exceptionWithName:@"InvalidRowIndex"
948 reason:@"negative row index"
952 if (iRow >= self.rowCount) {
953 NSException *exc = [NSException
954 exceptionWithName:@"InvalidRowIndex"
955 reason:@"row index exceeds number of rows"
964 /* The max number of glyphs we support. Currently this only affects
965 * updateGlyphInfo() for the calculation of the tile size, fontAscender,
966 * fontDescender, nColPre, and nColPost. The rendering in drawWChar() will
967 * work for a glyph not in updateGlyphInfo()'s set, and that is used for
968 * rendering Japanese characters, though there may be clipping or clearing
969 * artifacts because it wasn't included in updateGlyphInfo()'s calculations.
971 #define GLYPH_COUNT 256
973 /* An AngbandContext represents a logical Term (i.e. what Angband thinks is
974 * a window). This typically maps to one NSView, but may map to more than one
975 * NSView (e.g. the Test and real screen saver view). */
976 @interface AngbandContext : NSObject <NSWindowDelegate>
980 /* The Angband term */
984 /* Is the last time we drew, so we can throttle drawing. */
985 CFAbsoluteTime lastRefreshTime;
988 * Whether we are currently in live resize, which affects how big we
993 /* Flags whether or not a fullscreen transition is in progress. */
994 BOOL inFullscreenTransition;
997 /* Column and row counts, by default 80 x 24 */
1001 /* The size of the border between the window edge and the contents */
1002 @property (readonly) NSSize borderSize;
1004 /* Our array of views */
1005 @property NSMutableArray *angbandViews;
1007 /* The buffered image */
1008 @property CGLayerRef angbandLayer;
1010 /* The font of this context */
1011 @property NSFont *angbandViewFont;
1013 /* The size of one tile */
1014 @property (readonly) NSSize tileSize;
1016 /* Font's ascender and descender */
1017 @property (readonly) CGFloat fontAscender;
1018 @property (readonly) CGFloat fontDescender;
1021 * These are the number of columns before or after, respectively, a text
1022 * change that may need to be redrawn.
1024 @property (readonly) int nColPre;
1025 @property (readonly) int nColPost;
1027 /* If this context owns a window, here it is. */
1028 @property NSWindow *primaryWindow;
1030 /* Is the record of changes to the contents for the next update. */
1031 @property PendingTermChanges *changes;
1033 @property (nonatomic, assign) BOOL hasSubwindowFlags;
1034 @property (nonatomic, assign) BOOL windowVisibilityChecked;
1036 - (void)drawRect:(NSRect)rect inView:(NSView *)view;
1038 /* Called at initialization to set the term */
1039 - (void)setTerm:(term *)t;
1041 /* Called when the context is going down. */
1044 /* Returns the size of the image. */
1045 - (NSSize)imageSize;
1047 /* Return the rect for a tile at given coordinates. */
1048 - (NSRect)rectInImageForTileAtX:(int)x Y:(int)y;
1050 /* Draw the given wide character into the given tile rect. */
1051 - (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile context:(CGContextRef)ctx;
1053 /* Locks focus on the Angband image, and scales the CTM appropriately. */
1054 - (CGContextRef)lockFocus;
1056 /* Locks focus on the Angband image but does NOT scale the CTM. Appropriate
1057 * for drawing hairlines. */
1058 - (CGContextRef)lockFocusUnscaled;
1060 /* Unlocks focus. */
1061 - (void)unlockFocus;
1063 /* Returns the primary window for this angband context, creating it if
1065 - (NSWindow *)makePrimaryWindow;
1067 /* Called to add a new Angband view */
1068 - (void)addAngbandView:(AngbandView *)view;
1070 /* Make the context aware that one of its views changed size */
1071 - (void)angbandViewDidScale:(AngbandView *)view;
1073 /* Handle becoming the main window */
1074 - (void)windowDidBecomeMain:(NSNotification *)notification;
1076 /* Return whether the context's primary window is ordered in or not */
1077 - (BOOL)isOrderedIn;
1079 /* Return whether the context's primary window is key */
1080 - (BOOL)isMainWindow;
1082 /* Invalidate the whole image */
1083 - (void)setNeedsDisplay:(BOOL)val;
1085 /* Invalidate part of the image, with the rect expressed in base coordinates */
1086 - (void)setNeedsDisplayInBaseRect:(NSRect)rect;
1088 /* Display (flush) our Angband views */
1089 - (void)displayIfNeeded;
1091 /* Resize context to size of contentRect, and optionally save size to
1093 - (void)resizeTerminalWithContentRect: (NSRect)contentRect saveToDefaults: (BOOL)saveToDefaults;
1096 * Change the minimum size for the window associated with the context.
1097 * termIdx is the index for the terminal: pass it so this function can be
1098 * used when self->terminal has not yet been set.
1100 - (void)setMinimumWindowSize:(int)termIdx;
1102 /* Called from the view to indicate that it is starting or ending live resize */
1103 - (void)viewWillStartLiveResize:(AngbandView *)view;
1104 - (void)viewDidEndLiveResize:(AngbandView *)view;
1105 - (void)saveWindowVisibleToDefaults: (BOOL)windowVisible;
1106 - (BOOL)windowVisibleUsingDefaults;
1110 * Gets the default font for all contexts. Currently not declaring this as
1111 * a class property for compatibility with versions of Xcode prior to 8.
1113 + (NSFont*)defaultFont;
1115 * Sets the default font for all contexts.
1117 + (void)setDefaultFont:(NSFont*)font;
1119 /* Internal method */
1120 - (AngbandView *)activeView;
1125 * Generate a mask for the subwindow flags. The mask is just a safety check to
1126 * make sure that our windows show and hide as expected. This function allows
1127 * for future changes to the set of flags without needed to update it here
1128 * (unless the underlying types change).
1130 u32b AngbandMaskForValidSubwindowFlags(void)
1132 int windowFlagBits = sizeof(*(window_flag)) * CHAR_BIT;
1133 int maxBits = MIN( 16, windowFlagBits );
1136 for( int i = 0; i < maxBits; i++ )
1138 if( window_flag_desc[i] != NULL )
1148 * Check for changes in the subwindow flags and update window visibility.
1149 * This seems to be called for every user event, so we don't
1150 * want to do any unnecessary hiding or showing of windows.
1152 static void AngbandUpdateWindowVisibility(void)
1154 /* Because this function is called frequently, we'll make the mask static.
1155 * It doesn't change between calls, as the flags themselves are hardcoded */
1156 static u32b validWindowFlagsMask = 0;
1158 if( validWindowFlagsMask == 0 )
1160 validWindowFlagsMask = AngbandMaskForValidSubwindowFlags();
1163 /* Loop through all of the subwindows and see if there is a change in the
1164 * flags. If so, show or hide the corresponding window. We don't care about
1165 * the flags themselves; we just want to know if any are set. */
1166 for( int i = 1; i < ANGBAND_TERM_MAX; i++ )
1168 AngbandContext *angbandContext =
1169 (__bridge AngbandContext*) (angband_term[i]->data);
1171 if( angbandContext == nil )
1176 /* This horrible mess of flags is so that we can try to maintain some
1177 * user visibility preference. This should allow the user a window and
1178 * have it stay closed between application launches. However, this
1179 * means that when a subwindow is turned on, it will no longer appear
1180 * automatically. Angband has no concept of user control over window
1181 * visibility, other than the subwindow flags. */
1182 if( !angbandContext.windowVisibilityChecked )
1184 if( [angbandContext windowVisibleUsingDefaults] )
1186 [angbandContext.primaryWindow orderFront: nil];
1187 angbandContext.windowVisibilityChecked = YES;
1191 [angbandContext.primaryWindow close];
1192 angbandContext.windowVisibilityChecked = NO;
1197 BOOL termHasSubwindowFlags = ((window_flag[i] & validWindowFlagsMask) > 0);
1199 if( angbandContext.hasSubwindowFlags && !termHasSubwindowFlags )
1201 [angbandContext.primaryWindow close];
1202 angbandContext.hasSubwindowFlags = NO;
1203 [angbandContext saveWindowVisibleToDefaults: NO];
1205 else if( !angbandContext.hasSubwindowFlags && termHasSubwindowFlags )
1207 [angbandContext.primaryWindow orderFront: nil];
1208 angbandContext.hasSubwindowFlags = YES;
1209 [angbandContext saveWindowVisibleToDefaults: YES];
1214 /* Make the main window key so that user events go to the right spot */
1215 AngbandContext *mainWindow =
1216 (__bridge AngbandContext*) (angband_term[0]->data);
1217 [mainWindow.primaryWindow makeKeyAndOrderFront: nil];
1222 * ------------------------------------------------------------------------
1224 * ------------------------------------------------------------------------ */
1229 static CGImageRef pict_image;
1232 * Numbers of rows and columns in a tileset,
1233 * calculated by the PICT/PNG loading code
1235 static int pict_cols = 0;
1236 static int pict_rows = 0;
1239 * Requested graphics mode (as a grafID).
1240 * The current mode is stored in current_graphics_mode.
1242 static int graf_mode_req = 0;
1245 * Helper function to check the various ways that graphics can be enabled,
1246 * guarding against NULL
1248 static BOOL graphics_are_enabled(void)
1250 return current_graphics_mode
1251 && current_graphics_mode->grafID != GRAPHICS_NONE;
1255 * Hack -- game in progress
1257 static Boolean game_in_progress = FALSE;
1260 #pragma mark Prototypes
1261 static void wakeup_event_loop(void);
1262 static void hook_plog(const char *str);
1263 static void hook_quit(const char * str);
1264 static NSString* get_lib_directory(void);
1265 static NSString* get_doc_directory(void);
1266 static NSString* AngbandCorrectedDirectoryPath(NSString *originalPath);
1267 static void prepare_paths_and_directories(void);
1268 static void handle_open_when_ready(void);
1269 static void play_sound(int event);
1270 static BOOL check_events(int wait);
1271 static BOOL send_event(NSEvent *event);
1272 static void record_current_savefile(void);
1274 static wchar_t convert_two_byte_eucjp_to_utf16_native(const char *cp);
1278 * Available values for 'wait'
1280 #define CHECK_EVENTS_DRAIN -1
1281 #define CHECK_EVENTS_NO_WAIT 0
1282 #define CHECK_EVENTS_WAIT 1
1286 * Note when "open"/"new" become valid
1288 static bool initialized = FALSE;
1290 /* Methods for getting the appropriate NSUserDefaults */
1291 @interface NSUserDefaults (AngbandDefaults)
1292 + (NSUserDefaults *)angbandDefaults;
1295 @implementation NSUserDefaults (AngbandDefaults)
1296 + (NSUserDefaults *)angbandDefaults
1298 return [NSUserDefaults standardUserDefaults];
1302 /* Methods for pulling images out of the Angband bundle (which may be separate
1303 * from the current bundle in the case of a screensaver */
1304 @interface NSImage (AngbandImages)
1305 + (NSImage *)angbandImage:(NSString *)name;
1308 /* The NSView subclass that draws our Angband image */
1309 @interface AngbandView : NSView
1311 AngbandContext *angbandContext;
1314 - (void)setAngbandContext:(AngbandContext *)context;
1315 - (AngbandContext *)angbandContext;
1319 @implementation NSImage (AngbandImages)
1321 /* Returns an image in the resource directoy of the bundle containing the
1322 * Angband view class. */
1323 + (NSImage *)angbandImage:(NSString *)name
1325 NSBundle *bundle = [NSBundle bundleForClass:[AngbandView class]];
1326 NSString *path = [bundle pathForImageResource:name];
1327 return (path) ? [[NSImage alloc] initByReferencingFile:path] : nil;
1333 @implementation AngbandContext
1335 @synthesize hasSubwindowFlags=_hasSubwindowFlags;
1336 @synthesize windowVisibilityChecked=_windowVisibilityChecked;
1338 - (BOOL)useLiveResizeOptimization
1340 /* If we have graphics turned off, text rendering is fast enough that we
1341 * don't need to use a live resize optimization. */
1342 return self->inLiveResize && graphics_are_enabled();
1347 /* We round the base size down. If we round it up, I believe we may end up
1348 * with pixels that nobody "owns" that may accumulate garbage. In general
1349 * rounding down is harmless, because any lost pixels may be sopped up by
1352 floor(self.cols * self.tileSize.width + 2 * self.borderSize.width),
1353 floor(self.rows * self.tileSize.height + 2 * self.borderSize.height));
1356 /* qsort-compatible compare function for CGSizes */
1357 static int compare_advances(const void *ap, const void *bp)
1359 const CGSize *a = ap, *b = bp;
1360 return (a->width > b->width) - (a->width < b->width);
1364 * Precompute certain metrics (tileSize, fontAscender, fontDescender, nColPre,
1365 * and nColPost) for the current font.
1367 - (void)updateGlyphInfo
1369 NSFont *screenFont = [self.angbandViewFont screenFont];
1371 /* Generate a string containing each MacRoman character */
1373 * Here and below, dynamically allocate working arrays rather than put them
1374 * on the stack in case limited stack space is an issue.
1376 unsigned char *latinString = malloc(GLYPH_COUNT);
1377 if (latinString == 0) {
1378 NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
1379 reason:@"latinString in updateGlyphInfo"
1384 for (i=0; i < GLYPH_COUNT; i++) latinString[i] = (unsigned char)i;
1386 /* Turn that into unichar. Angband uses ISO Latin 1. */
1387 NSString *allCharsString = [[NSString alloc] initWithBytes:latinString length:sizeof latinString encoding:NSISOLatin1StringEncoding];
1388 unichar *unicharString = malloc(GLYPH_COUNT * sizeof(unichar));
1389 if (unicharString == 0) {
1391 NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
1392 reason:@"unicharString in updateGlyphInfo"
1396 unicharString[0] = 0;
1397 [allCharsString getCharacters:unicharString range:NSMakeRange(0, MIN(GLYPH_COUNT, [allCharsString length]))];
1398 allCharsString = nil;
1402 CGGlyph *glyphArray = calloc(GLYPH_COUNT, sizeof(CGGlyph));
1403 if (glyphArray == 0) {
1404 free(unicharString);
1405 NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
1406 reason:@"glyphArray in updateGlyphInfo"
1410 CTFontGetGlyphsForCharacters((CTFontRef)screenFont, unicharString,
1411 glyphArray, GLYPH_COUNT);
1412 free(unicharString);
1414 /* Get advances. Record the max advance. */
1415 CGSize *advances = malloc(GLYPH_COUNT * sizeof(CGSize));
1416 if (advances == 0) {
1418 NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
1419 reason:@"advances in updateGlyphInfo"
1423 CTFontGetAdvancesForGlyphs(
1424 (CTFontRef)screenFont, kCTFontHorizontalOrientation, glyphArray,
1425 advances, GLYPH_COUNT);
1426 CGFloat *glyphWidths = malloc(GLYPH_COUNT * sizeof(CGFloat));
1427 if (glyphWidths == 0) {
1430 NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
1431 reason:@"glyphWidths in updateGlyphInfo"
1435 for (i=0; i < GLYPH_COUNT; i++) {
1436 glyphWidths[i] = advances[i].width;
1439 /* For good non-mono-font support, use the median advance. Start by sorting
1441 qsort(advances, GLYPH_COUNT, sizeof *advances, compare_advances);
1443 /* Skip over any initially empty run */
1445 for (startIdx = 0; startIdx < GLYPH_COUNT; startIdx++)
1447 if (advances[startIdx].width > 0) break;
1450 /* Pick the center to find the median */
1451 CGFloat medianAdvance = 0;
1452 if (startIdx < GLYPH_COUNT)
1454 /* In case we have all zero advances for some reason */
1455 medianAdvance = advances[(startIdx + GLYPH_COUNT)/2].width;
1461 * Record the ascender and descender. Some fonts, for instance DIN
1462 * Condensed and Rockwell in 10.14, the ascent on '@' exceeds that
1463 * reported by [screenFont ascender]. Get the overall bounding box
1464 * for the glyphs and use that instead of the ascender and descender
1465 * values if the bounding box result extends farther from the baseline.
1467 CGRect bounds = CTFontGetBoundingRectsForGlyphs(
1468 (CTFontRef) screenFont, kCTFontHorizontalOrientation, glyphArray,
1470 self->_fontAscender = [screenFont ascender];
1471 if (self->_fontAscender < bounds.origin.y + bounds.size.height) {
1472 self->_fontAscender = bounds.origin.y + bounds.size.height;
1474 self->_fontDescender = [screenFont descender];
1475 if (self->_fontDescender > bounds.origin.y) {
1476 self->_fontDescender = bounds.origin.y;
1480 * Record the tile size. Round both values up to have tile boundaries
1481 * match pixel boundaries.
1483 self->_tileSize.width = ceil(medianAdvance);
1484 self->_tileSize.height = ceil(self.fontAscender - self.fontDescender);
1487 * Determine whether neighboring columns need to be redrawn when a
1488 * character changes.
1490 CGRect *boxes = malloc(GLYPH_COUNT * sizeof(CGRect));
1494 NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
1495 reason:@"boxes in updateGlyphInfo"
1499 CGFloat beyond_right = 0.;
1500 CGFloat beyond_left = 0.;
1501 CTFontGetBoundingRectsForGlyphs(
1502 (CTFontRef)screenFont,
1503 kCTFontHorizontalOrientation,
1507 for (i = 0; i < GLYPH_COUNT; i++) {
1508 /* Account for the compression and offset used by drawWChar(). */
1509 CGFloat compression, offset;
1512 if (glyphWidths[i] <= self.tileSize.width) {
1514 offset = 0.5 * (self.tileSize.width - glyphWidths[i]);
1516 compression = self.tileSize.width / glyphWidths[i];
1519 v = (offset + boxes[i].origin.x) * compression;
1520 if (beyond_left > v) {
1523 v = (offset + boxes[i].origin.x + boxes[i].size.width) * compression;
1524 if (beyond_right < v) {
1529 self->_nColPre = ceil(-beyond_left / self.tileSize.width);
1530 if (beyond_right > self.tileSize.width) {
1532 ceil((beyond_right - self.tileSize.width) / self.tileSize.width);
1534 self->_nColPost = 0;
1543 NSSize size = NSMakeSize(1, 1);
1545 AngbandView *activeView = [self activeView];
1548 /* If we are in live resize, draw as big as the screen, so we can scale
1549 * nicely to any size. If we are not in live resize, then use the
1550 * bounds of the active view. */
1552 if ([self useLiveResizeOptimization] && (screen = [[activeView window] screen]) != NULL)
1554 size = [screen frame].size;
1558 size = [activeView bounds].size;
1562 CGLayerRelease(self.angbandLayer);
1564 /* Use the highest monitor scale factor on the system to work out what
1565 * scale to draw at - not the recommended method, but works where we
1566 * can't easily get the monitor the current draw is occurring on. */
1567 float angbandLayerScale = 1.0;
1568 if ([[NSScreen mainScreen] respondsToSelector:@selector(backingScaleFactor)]) {
1569 for (NSScreen *screen in [NSScreen screens]) {
1570 angbandLayerScale = fmax(angbandLayerScale, [screen backingScaleFactor]);
1574 /* Make a bitmap context as an example for our layer */
1575 CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
1576 CGContextRef exampleCtx = CGBitmapContextCreate(NULL, 1, 1, 8 /* bits per component */, 48 /* bytesPerRow */, cs, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host);
1577 CGColorSpaceRelease(cs);
1579 /* Create the layer at the appropriate size */
1580 size.width = fmax(1, ceil(size.width * angbandLayerScale));
1581 size.height = fmax(1, ceil(size.height * angbandLayerScale));
1583 CGLayerCreateWithContext(exampleCtx, *(CGSize *)&size, NULL);
1585 CFRelease(exampleCtx);
1587 /* Set the new context of the layer to draw at the correct scale */
1588 CGContextRef ctx = CGLayerGetContext(self.angbandLayer);
1589 CGContextScaleCTM(ctx, angbandLayerScale, angbandLayerScale);
1592 [[NSColor blackColor] set];
1593 NSRectFill((NSRect){NSZeroPoint, [self baseSize]});
1597 - (void)requestRedraw
1599 if (! self->terminal) return;
1603 /* Activate the term */
1604 Term_activate(self->terminal);
1606 /* Redraw the contents */
1609 /* Flush the output */
1612 /* Restore the old term */
1616 - (void)setTerm:(term *)t
1621 - (void)viewWillStartLiveResize:(AngbandView *)view
1623 #if USE_LIVE_RESIZE_CACHE
1624 if (self->inLiveResize < INT_MAX) self->inLiveResize++;
1625 else [NSException raise:NSInternalInconsistencyException format:@"inLiveResize overflow"];
1627 if (self->inLiveResize == 1 && graphics_are_enabled())
1631 [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
1632 [self requestRedraw];
1637 - (void)viewDidEndLiveResize:(AngbandView *)view
1639 #if USE_LIVE_RESIZE_CACHE
1640 if (self->inLiveResize > 0) self->inLiveResize--;
1641 else [NSException raise:NSInternalInconsistencyException format:@"inLiveResize underflow"];
1643 if (self->inLiveResize == 0 && graphics_are_enabled())
1647 [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
1648 [self requestRedraw];
1654 * If we're trying to limit ourselves to a certain number of frames per second,
1655 * then compute how long it's been since we last drew, and then wait until the
1656 * next frame has passed. */
1659 if (frames_per_second > 0)
1661 CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
1662 CFTimeInterval timeSinceLastRefresh = now - self->lastRefreshTime;
1663 CFTimeInterval timeUntilNextRefresh = (1. / (double)frames_per_second) - timeSinceLastRefresh;
1665 if (timeUntilNextRefresh > 0)
1667 usleep((unsigned long)(timeUntilNextRefresh * 1000000.));
1670 self->lastRefreshTime = CFAbsoluteTimeGetCurrent();
1673 - (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile context:(CGContextRef)ctx
1675 CGFloat tileOffsetY = self.fontAscender;
1676 CGFloat tileOffsetX = 0.0;
1677 NSFont *screenFont = [self.angbandViewFont screenFont];
1678 UniChar unicharString[2] = {(UniChar)wchar, 0};
1680 /* Get glyph and advance */
1681 CGGlyph thisGlyphArray[1] = { 0 };
1682 CGSize advances[1] = { { 0, 0 } };
1683 CTFontGetGlyphsForCharacters((CTFontRef)screenFont, unicharString, thisGlyphArray, 1);
1684 CGGlyph glyph = thisGlyphArray[0];
1685 CTFontGetAdvancesForGlyphs((CTFontRef)screenFont, kCTFontHorizontalOrientation, thisGlyphArray, advances, 1);
1686 CGSize advance = advances[0];
1688 /* If our font is not monospaced, our tile width is deliberately not big
1689 * enough for every character. In that event, if our glyph is too wide, we
1690 * need to compress it horizontally. Compute the compression ratio.
1691 * 1.0 means no compression. */
1692 double compressionRatio;
1693 if (advance.width <= NSWidth(tile))
1695 /* Our glyph fits, so we can just draw it, possibly with an offset */
1696 compressionRatio = 1.0;
1697 tileOffsetX = (NSWidth(tile) - advance.width)/2;
1701 /* Our glyph doesn't fit, so we'll have to compress it */
1702 compressionRatio = NSWidth(tile) / advance.width;
1708 CGAffineTransform textMatrix = CGContextGetTextMatrix(ctx);
1709 CGFloat savedA = textMatrix.a;
1711 /* Set the position */
1712 textMatrix.tx = tile.origin.x + tileOffsetX;
1713 textMatrix.ty = tile.origin.y + tileOffsetY;
1715 /* Maybe squish it horizontally. */
1716 if (compressionRatio != 1.)
1718 textMatrix.a *= compressionRatio;
1721 textMatrix = CGAffineTransformScale( textMatrix, 1.0, -1.0 );
1722 CGContextSetTextMatrix(ctx, textMatrix);
1723 CGContextShowGlyphsAtPositions(ctx, &glyph, &CGPointZero, 1);
1725 /* Restore the text matrix if we messed with the compression ratio */
1726 if (compressionRatio != 1.)
1728 textMatrix.a = savedA;
1729 CGContextSetTextMatrix(ctx, textMatrix);
1732 textMatrix = CGAffineTransformScale( textMatrix, 1.0, -1.0 );
1733 CGContextSetTextMatrix(ctx, textMatrix);
1736 /* Lock and unlock focus on our image or layer, setting up the CTM
1738 - (CGContextRef)lockFocusUnscaled
1740 /* Create an NSGraphicsContext representing this CGLayer */
1741 CGContextRef ctx = CGLayerGetContext(self.angbandLayer);
1742 NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:ctx flipped:NO];
1743 [NSGraphicsContext saveGraphicsState];
1744 [NSGraphicsContext setCurrentContext:context];
1745 CGContextSaveGState(ctx);
1751 /* Restore the graphics state */
1752 CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];
1753 CGContextRestoreGState(ctx);
1754 [NSGraphicsContext restoreGraphicsState];
1759 /* Return the size of our layer */
1760 CGSize result = CGLayerGetSize(self.angbandLayer);
1761 return NSMakeSize(result.width, result.height);
1764 - (CGContextRef)lockFocus
1766 return [self lockFocusUnscaled];
1770 - (NSRect)rectInImageForTileAtX:(int)x Y:(int)y
1773 return NSMakeRect(x * self.tileSize.width + self.borderSize.width,
1774 flippedY * self.tileSize.height + self.borderSize.height,
1775 self.tileSize.width, self.tileSize.height);
1778 - (void)setSelectionFont:(NSFont*)font adjustTerminal: (BOOL)adjustTerminal
1780 /* Record the new font */
1781 self.angbandViewFont = font;
1783 /* Update our glyph info */
1784 [self updateGlyphInfo];
1786 if( adjustTerminal )
1788 /* Adjust terminal to fit window with new font; save the new columns
1789 * and rows since they could be changed */
1790 NSRect contentRect =
1792 contentRectForFrameRect: [self.primaryWindow frame]];
1794 [self setMinimumWindowSize:[self terminalIndex]];
1795 NSSize size = self.primaryWindow.contentMinSize;
1796 BOOL windowNeedsResizing = NO;
1797 if (contentRect.size.width < size.width) {
1798 contentRect.size.width = size.width;
1799 windowNeedsResizing = YES;
1801 if (contentRect.size.height < size.height) {
1802 contentRect.size.height = size.height;
1803 windowNeedsResizing = YES;
1805 if (windowNeedsResizing) {
1806 size.width = contentRect.size.width;
1807 size.height = contentRect.size.height;
1808 [self.primaryWindow setContentSize:size];
1810 [self resizeTerminalWithContentRect: contentRect saveToDefaults: YES];
1813 /* Update our image */
1819 if ((self = [super init]))
1821 /* Default rows and cols */
1825 /* Default border size */
1826 self->_borderSize = NSMakeSize(2, 2);
1828 /* Allocate our array of views */
1829 self->_angbandViews = [[NSMutableArray alloc] init];
1832 self->_nColPost = 0;
1835 [[PendingTermChanges alloc] initWithColumnsRows:self->_cols
1837 self->lastRefreshTime = CFAbsoluteTimeGetCurrent();
1838 self->inLiveResize = 0;
1839 self->inFullscreenTransition = NO;
1841 /* Make the image. Since we have no views, it'll just be a puny 1x1 image. */
1844 self->_windowVisibilityChecked = NO;
1850 * Destroy all the receiver's stuff. This is intended to be callable more than
1855 self->terminal = NULL;
1857 /* Disassociate ourselves from our angbandViews */
1858 [self.angbandViews makeObjectsPerformSelector:@selector(setAngbandContext:) withObject:nil];
1859 self.angbandViews = nil;
1861 /* Destroy the layer/image */
1862 CGLayerRelease(self.angbandLayer);
1863 self.angbandLayer = NULL;
1866 self.angbandViewFont = nil;
1869 [self.primaryWindow setDelegate:nil];
1870 [self.primaryWindow close];
1871 self.primaryWindow = nil;
1873 /* Pending changes */
1877 /* Usual Cocoa fare */
1884 /* From the Linux mbstowcs(3) man page:
1885 * If dest is NULL, n is ignored, and the conversion proceeds as above,
1886 * except that the converted wide characters are not written out to mem‐
1887 * ory, and that no length limit exists.
1889 static size_t Term_mbcs_cocoa(wchar_t *dest, const char *src, int n)
1894 /* Unicode code point to UTF-8
1895 * 0x0000-0x007f: 0xxxxxxx
1896 * 0x0080-0x07ff: 110xxxxx 10xxxxxx
1897 * 0x0800-0xffff: 1110xxxx 10xxxxxx 10xxxxxx
1898 * 0x10000-0x1fffff: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
1899 * Note that UTF-16 limits Unicode to 0x10ffff. This code is not
1902 for (i = 0; i < n || dest == NULL; i++) {
1903 if ((src[i] & 0x80) == 0) {
1904 if (dest != NULL) dest[count] = src[i];
1905 if (src[i] == 0) break;
1906 } else if ((src[i] & 0xe0) == 0xc0) {
1907 if (dest != NULL) dest[count] =
1908 (((unsigned char)src[i] & 0x1f) << 6)|
1909 ((unsigned char)src[i+1] & 0x3f);
1911 } else if ((src[i] & 0xf0) == 0xe0) {
1912 if (dest != NULL) dest[count] =
1913 (((unsigned char)src[i] & 0x0f) << 12) |
1914 (((unsigned char)src[i+1] & 0x3f) << 6) |
1915 ((unsigned char)src[i+2] & 0x3f);
1917 } else if ((src[i] & 0xf8) == 0xf0) {
1918 if (dest != NULL) dest[count] =
1919 (((unsigned char)src[i] & 0x0f) << 18) |
1920 (((unsigned char)src[i+1] & 0x3f) << 12) |
1921 (((unsigned char)src[i+2] & 0x3f) << 6) |
1922 ((unsigned char)src[i+3] & 0x3f);
1925 /* Found an invalid multibyte sequence */
1934 - (void)addAngbandView:(AngbandView *)view
1936 if (! [self.angbandViews containsObject:view])
1938 [self.angbandViews addObject:view];
1940 [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
1941 [self requestRedraw];
1946 * For defaultFont and setDefaultFont.
1948 static __strong NSFont* gDefaultFont = nil;
1950 + (NSFont*)defaultFont
1952 return gDefaultFont;
1955 + (void)setDefaultFont:(NSFont*)font
1957 gDefaultFont = font;
1961 * We have this notion of an "active" AngbandView, which is the largest - the
1962 * idea being that in the screen saver, when the user hits Test in System
1963 * Preferences, we don't want to keep driving the AngbandView in the
1964 * background. Our active AngbandView is the widest - that's a hack all right.
1965 * Mercifully when we're just playing the game there's only one view.
1967 - (AngbandView *)activeView
1969 if ([self.angbandViews count] == 1)
1970 return [self.angbandViews objectAtIndex:0];
1972 AngbandView *result = nil;
1974 for (AngbandView *angbandView in self.angbandViews)
1976 float width = [angbandView frame].size.width;
1977 if (width > maxWidth)
1980 result = angbandView;
1986 - (void)angbandViewDidScale:(AngbandView *)view
1988 /* If we're live-resizing with graphics, we're using the live resize
1989 * optimization, so don't update the image. Otherwise do it. */
1990 if (! (self->inLiveResize && graphics_are_enabled()) && view == [self activeView])
1994 [self setNeedsDisplay:YES]; /*we'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
1995 [self requestRedraw];
2000 - (void)removeAngbandView:(AngbandView *)view
2002 if ([self.angbandViews containsObject:view])
2004 [self.angbandViews removeObject:view];
2006 [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
2007 if ([self.angbandViews count]) [self requestRedraw];
2012 - (NSWindow *)makePrimaryWindow
2014 if (! self.primaryWindow)
2016 /* This has to be done after the font is set, which it already is in
2017 * term_init_cocoa() */
2018 NSSize sz = self.baseSize;
2019 NSRect contentRect = NSMakeRect( 0.0, 0.0, sz.width, sz.height );
2021 NSUInteger styleMask = NSTitledWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask;
2023 /* Make every window other than the main window closable */
2024 if ((__bridge AngbandContext*) (angband_term[0]->data) != self)
2026 styleMask |= NSClosableWindowMask;
2029 self.primaryWindow = [[NSWindow alloc] initWithContentRect:contentRect styleMask: styleMask backing:NSBackingStoreBuffered defer:YES];
2031 /* Not to be released when closed */
2032 [self.primaryWindow setReleasedWhenClosed:NO];
2033 [self.primaryWindow setExcludedFromWindowsMenu: YES]; /* we're using custom window menu handling */
2036 AngbandView *angbandView = [[AngbandView alloc] initWithFrame:contentRect];
2037 [angbandView setAngbandContext:self];
2038 [self.angbandViews addObject:angbandView];
2039 [self.primaryWindow setContentView:angbandView];
2041 /* We are its delegate */
2042 [self.primaryWindow setDelegate:self];
2044 /* Update our image, since this is probably the first angband view
2048 return self.primaryWindow;
2053 #pragma mark View/Window Passthrough
2056 * This is what our views call to get us to draw to the window
2058 - (void)drawRect:(NSRect)rect inView:(NSView *)view
2060 /* Take this opportunity to throttle so we don't flush faster than desired.
2062 BOOL viewInLiveResize = [view inLiveResize];
2063 if (! viewInLiveResize) [self throttle];
2065 /* With a GLayer, use CGContextDrawLayerInRect */
2066 CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
2067 NSRect bounds = [view bounds];
2068 if (viewInLiveResize) CGContextSetInterpolationQuality(context, kCGInterpolationLow);
2069 CGContextSetBlendMode(context, kCGBlendModeCopy);
2070 CGContextDrawLayerInRect(context, *(CGRect *)&bounds, self.angbandLayer);
2071 if (viewInLiveResize) CGContextSetInterpolationQuality(context, kCGInterpolationDefault);
2076 return [[[self.angbandViews lastObject] window] isVisible];
2079 - (BOOL)isMainWindow
2081 return [[[self.angbandViews lastObject] window] isMainWindow];
2084 - (void)setNeedsDisplay:(BOOL)val
2086 for (NSView *angbandView in self.angbandViews)
2088 [angbandView setNeedsDisplay:val];
2092 - (void)setNeedsDisplayInBaseRect:(NSRect)rect
2094 for (NSView *angbandView in self.angbandViews)
2096 [angbandView setNeedsDisplayInRect: rect];
2100 - (void)displayIfNeeded
2102 [[self activeView] displayIfNeeded];
2105 - (int)terminalIndex
2109 for( termIndex = 0; termIndex < ANGBAND_TERM_MAX; termIndex++ )
2111 if( angband_term[termIndex] == self->terminal )
2120 - (void)resizeTerminalWithContentRect: (NSRect)contentRect saveToDefaults: (BOOL)saveToDefaults
2122 CGFloat newRows = floor(
2123 (contentRect.size.height - (self.borderSize.height * 2.0)) /
2124 self.tileSize.height);
2125 CGFloat newColumns = ceil(
2126 (contentRect.size.width - (self.borderSize.width * 2.0)) /
2127 self.tileSize.width);
2129 if (newRows < 1 || newColumns < 1) return;
2130 self->_cols = newColumns;
2131 self->_rows = newRows;
2132 [self.changes resize:self.cols rows:self.rows];
2134 if( saveToDefaults )
2136 int termIndex = [self terminalIndex];
2137 NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
2139 if( termIndex < (int)[terminals count] )
2141 NSMutableDictionary *mutableTerm = [[NSMutableDictionary alloc] initWithDictionary: [terminals objectAtIndex: termIndex]];
2142 [mutableTerm setValue: [NSNumber numberWithInteger: self.cols]
2143 forKey: AngbandTerminalColumnsDefaultsKey];
2144 [mutableTerm setValue: [NSNumber numberWithInteger: self.rows]
2145 forKey: AngbandTerminalRowsDefaultsKey];
2147 NSMutableArray *mutableTerminals = [[NSMutableArray alloc] initWithArray: terminals];
2148 [mutableTerminals replaceObjectAtIndex: termIndex withObject: mutableTerm];
2150 [[NSUserDefaults standardUserDefaults] setValue: mutableTerminals forKey: AngbandTerminalsDefaultsKey];
2152 [[NSUserDefaults standardUserDefaults] synchronize];
2156 Term_activate( self->terminal );
2157 Term_resize( self.cols, self.rows );
2159 Term_activate( old );
2162 - (void)setMinimumWindowSize:(int)termIdx
2168 minsize.height = 24;
2174 minsize.width * self.tileSize.width + self.borderSize.width * 2.0;
2176 minsize.height * self.tileSize.height + self.borderSize.height * 2.0;
2177 [[self makePrimaryWindow] setContentMinSize:minsize];
2180 - (void)saveWindowVisibleToDefaults: (BOOL)windowVisible
2182 int termIndex = [self terminalIndex];
2183 BOOL safeVisibility = (termIndex == 0) ? YES : windowVisible; /* Ensure main term doesn't go away because of these defaults */
2184 NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
2186 if( termIndex < (int)[terminals count] )
2188 NSMutableDictionary *mutableTerm = [[NSMutableDictionary alloc] initWithDictionary: [terminals objectAtIndex: termIndex]];
2189 [mutableTerm setValue: [NSNumber numberWithBool: safeVisibility] forKey: AngbandTerminalVisibleDefaultsKey];
2191 NSMutableArray *mutableTerminals = [[NSMutableArray alloc] initWithArray: terminals];
2192 [mutableTerminals replaceObjectAtIndex: termIndex withObject: mutableTerm];
2194 [[NSUserDefaults standardUserDefaults] setValue: mutableTerminals forKey: AngbandTerminalsDefaultsKey];
2198 - (BOOL)windowVisibleUsingDefaults
2200 int termIndex = [self terminalIndex];
2202 if( termIndex == 0 )
2207 NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
2210 if( termIndex < (int)[terminals count] )
2212 NSDictionary *term = [terminals objectAtIndex: termIndex];
2213 NSNumber *visibleValue = [term valueForKey: AngbandTerminalVisibleDefaultsKey];
2215 if( visibleValue != nil )
2217 visible = [visibleValue boolValue];
2225 #pragma mark NSWindowDelegate Methods
2227 /*- (void)windowWillStartLiveResize: (NSNotification *)notification
2231 - (void)windowDidEndLiveResize: (NSNotification *)notification
2233 NSWindow *window = [notification object];
2234 NSRect contentRect = [window contentRectForFrameRect: [window frame]];
2235 [self resizeTerminalWithContentRect: contentRect saveToDefaults: !(self->inFullscreenTransition)];
2238 /*- (NSSize)windowWillResize: (NSWindow *)sender toSize: (NSSize)frameSize
2242 - (void)windowWillEnterFullScreen: (NSNotification *)notification
2244 self->inFullscreenTransition = YES;
2247 - (void)windowDidEnterFullScreen: (NSNotification *)notification
2249 NSWindow *window = [notification object];
2250 NSRect contentRect = [window contentRectForFrameRect: [window frame]];
2251 self->inFullscreenTransition = NO;
2252 [self resizeTerminalWithContentRect: contentRect saveToDefaults: NO];
2255 - (void)windowWillExitFullScreen: (NSNotification *)notification
2257 self->inFullscreenTransition = YES;
2260 - (void)windowDidExitFullScreen: (NSNotification *)notification
2262 NSWindow *window = [notification object];
2263 NSRect contentRect = [window contentRectForFrameRect: [window frame]];
2264 self->inFullscreenTransition = NO;
2265 [self resizeTerminalWithContentRect: contentRect saveToDefaults: NO];
2268 - (void)windowDidBecomeMain:(NSNotification *)notification
2270 NSWindow *window = [notification object];
2272 if( window != self.primaryWindow )
2277 int termIndex = [self terminalIndex];
2278 NSMenuItem *item = [[[NSApplication sharedApplication] windowsMenu] itemWithTag: AngbandWindowMenuItemTagBase + termIndex];
2279 [item setState: NSOnState];
2281 if( [[NSFontPanel sharedFontPanel] isVisible] )
2283 [[NSFontPanel sharedFontPanel] setPanelFont:self.angbandViewFont
2288 - (void)windowDidResignMain: (NSNotification *)notification
2290 NSWindow *window = [notification object];
2292 if( window != self.primaryWindow )
2297 int termIndex = [self terminalIndex];
2298 NSMenuItem *item = [[[NSApplication sharedApplication] windowsMenu] itemWithTag: AngbandWindowMenuItemTagBase + termIndex];
2299 [item setState: NSOffState];
2302 - (void)windowWillClose: (NSNotification *)notification
2304 [self saveWindowVisibleToDefaults: NO];
2310 @implementation AngbandView
2322 - (void)drawRect:(NSRect)rect
2324 if (! angbandContext)
2326 /* Draw bright orange, 'cause this ain't right */
2327 [[NSColor orangeColor] set];
2328 NSRectFill([self bounds]);
2332 /* Tell the Angband context to draw into us */
2333 [angbandContext drawRect:rect inView:self];
2337 - (void)setAngbandContext:(AngbandContext *)context
2339 angbandContext = context;
2342 - (AngbandContext *)angbandContext
2344 return angbandContext;
2347 - (void)setFrameSize:(NSSize)size
2349 BOOL changed = ! NSEqualSizes(size, [self frame].size);
2350 [super setFrameSize:size];
2351 if (changed) [angbandContext angbandViewDidScale:self];
2354 - (void)viewWillStartLiveResize
2356 [angbandContext viewWillStartLiveResize:self];
2359 - (void)viewDidEndLiveResize
2361 [angbandContext viewDidEndLiveResize:self];
2367 * Delay handling of double-clicked savefiles
2369 Boolean open_when_ready = FALSE;
2374 * ------------------------------------------------------------------------
2375 * Some generic functions
2376 * ------------------------------------------------------------------------ */
2379 * Sets an Angband color at a given index
2381 static void set_color_for_index(int idx)
2385 /* Extract the R,G,B data */
2386 rv = angband_color_table[idx][1];
2387 gv = angband_color_table[idx][2];
2388 bv = angband_color_table[idx][3];
2390 CGContextSetRGBFillColor([[NSGraphicsContext currentContext] graphicsPort], rv/255., gv/255., bv/255., 1.);
2394 * Remember the current character in UserDefaults so we can select it by
2395 * default next time.
2397 static void record_current_savefile(void)
2399 NSString *savefileString = [[NSString stringWithCString:savefile encoding:NSMacOSRomanStringEncoding] lastPathComponent];
2402 NSUserDefaults *angbandDefs = [NSUserDefaults angbandDefaults];
2403 [angbandDefs setObject:savefileString forKey:@"SaveFile"];
2404 [angbandDefs synchronize];
2411 * Convert a two-byte EUC-JP encoded character (both *cp and (*cp + 1) are in
2412 * the range, 0xA1-0xFE, or *cp is 0x8E) to a utf16 value in the native byte
2415 static wchar_t convert_two_byte_eucjp_to_utf16_native(const char *cp)
2417 NSString* str = [[NSString alloc] initWithBytes:cp length:2
2418 encoding:NSJapaneseEUCStringEncoding];
2419 wchar_t result = [str characterAtIndex:0];
2427 * ------------------------------------------------------------------------
2428 * Support for the "z-term.c" package
2429 * ------------------------------------------------------------------------ */
2433 * Initialize a new Term
2435 static void Term_init_cocoa(term *t)
2438 AngbandContext *context = [[AngbandContext alloc] init];
2440 /* Give the term ownership of the context */
2441 t->data = (void *)CFBridgingRetain(context);
2443 /* Handle graphics */
2444 t->higher_pict = !! use_graphics;
2445 t->always_pict = FALSE;
2447 NSDisableScreenUpdates();
2450 * Figure out the frame autosave name based on the index of this term
2452 NSString *autosaveName = nil;
2454 for (termIdx = 0; termIdx < ANGBAND_TERM_MAX; termIdx++)
2456 if (angband_term[termIdx] == t)
2459 [NSString stringWithFormat:@"AngbandTerm-%d", termIdx];
2465 NSString *fontName =
2466 [[NSUserDefaults angbandDefaults]
2467 stringForKey:[NSString stringWithFormat:@"FontName-%d", termIdx]];
2468 if (! fontName) fontName = [[AngbandContext defaultFont] fontName];
2471 * Use a smaller default font for the other windows, but only if the
2472 * font hasn't been explicitly set.
2475 (termIdx > 0) ? 10.0 : [[AngbandContext defaultFont] pointSize];
2476 NSNumber *fontSizeNumber =
2477 [[NSUserDefaults angbandDefaults]
2478 valueForKey: [NSString stringWithFormat: @"FontSize-%d", termIdx]];
2480 if( fontSizeNumber != nil )
2482 fontSize = [fontSizeNumber floatValue];
2485 [context setSelectionFont:[NSFont fontWithName:fontName size:fontSize]
2486 adjustTerminal: NO];
2488 NSArray *terminalDefaults =
2489 [[NSUserDefaults standardUserDefaults]
2490 valueForKey: AngbandTerminalsDefaultsKey];
2491 NSInteger rows = 24;
2492 NSInteger columns = 80;
2494 if( termIdx < (int)[terminalDefaults count] )
2496 NSDictionary *term = [terminalDefaults objectAtIndex: termIdx];
2497 NSInteger defaultRows =
2498 [[term valueForKey: AngbandTerminalRowsDefaultsKey]
2500 NSInteger defaultColumns =
2501 [[term valueForKey: AngbandTerminalColumnsDefaultsKey]
2504 if (defaultRows > 0) rows = defaultRows;
2505 if (defaultColumns > 0) columns = defaultColumns;
2508 context.cols = columns;
2509 context.rows = rows;
2510 [context.changes resize:columns rows:rows];
2512 /* Get the window */
2513 NSWindow *window = [context makePrimaryWindow];
2515 /* Set its title and, for auxiliary terms, tentative size */
2517 [NSString stringWithCString:angband_term_name[termIdx]
2519 encoding:NSJapaneseEUCStringEncoding
2521 encoding:NSMacOSRomanStringEncoding
2524 [window setTitle:title];
2525 [context setMinimumWindowSize:termIdx];
2528 * If this is the first term, and we support full screen (Mac OS X Lion
2529 * or later), then allow it to go full screen (sweet). Allow other
2530 * terms to be FullScreenAuxilliary, so they can at least show up.
2531 * Unfortunately in Lion they don't get brought to the full screen
2532 * space; but they would only make sense on multiple displays anyways
2533 * so it's not a big loss.
2535 if ([window respondsToSelector:@selector(toggleFullScreen:)])
2537 NSWindowCollectionBehavior behavior = [window collectionBehavior];
2540 Angband_NSWindowCollectionBehaviorFullScreenPrimary :
2541 Angband_NSWindowCollectionBehaviorFullScreenAuxiliary);
2542 [window setCollectionBehavior:behavior];
2545 /* No Resume support yet, though it would not be hard to add */
2546 if ([window respondsToSelector:@selector(setRestorable:)])
2548 [window setRestorable:NO];
2551 /* default window placement */ {
2552 static NSRect overallBoundingRect;
2557 * This is a bit of a trick to allow us to display multiple
2558 * windows in the "standard default" window position in OS X:
2559 * the upper center of the screen. The term sizes set in
2560 * AngbandAppDelegate's loadPrefs() are based on a 5-wide by
2561 * 3-high grid, with the main term being 4/5 wide by 2/3 high
2562 * (hence the scaling to find what the containing rect would
2565 NSRect originalMainTermFrame = [window frame];
2566 NSRect scaledFrame = originalMainTermFrame;
2567 scaledFrame.size.width *= 5.0 / 4.0;
2568 scaledFrame.size.height *= 3.0 / 2.0;
2569 scaledFrame.size.width += 1.0; /* spacing between window columns */
2570 scaledFrame.size.height += 1.0; /* spacing between window rows */
2571 [window setFrame: scaledFrame display: NO];
2573 overallBoundingRect = [window frame];
2574 [window setFrame: originalMainTermFrame display: NO];
2577 static NSRect mainTermBaseRect;
2578 NSRect windowFrame = [window frame];
2583 * The height and width adjustments were determined
2584 * experimentally, so that the rest of the windows line up
2585 * nicely without overlapping.
2587 windowFrame.size.width += 7.0;
2588 windowFrame.size.height += 9.0;
2589 windowFrame.origin.x = NSMinX( overallBoundingRect );
2590 windowFrame.origin.y =
2591 NSMaxY( overallBoundingRect ) - NSHeight( windowFrame );
2592 mainTermBaseRect = windowFrame;
2594 else if( termIdx == 1 )
2596 windowFrame.origin.x = NSMinX( mainTermBaseRect );
2597 windowFrame.origin.y =
2598 NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
2600 else if( termIdx == 2 )
2602 windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
2603 windowFrame.origin.y =
2604 NSMaxY( mainTermBaseRect ) - NSHeight( windowFrame );
2606 else if( termIdx == 3 )
2608 windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
2609 windowFrame.origin.y =
2610 NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
2612 else if( termIdx == 4 )
2614 windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
2615 windowFrame.origin.y = NSMinY( mainTermBaseRect );
2617 else if( termIdx == 5 )
2619 windowFrame.origin.x =
2620 NSMinX( mainTermBaseRect ) + NSWidth( windowFrame ) + 1.0;
2621 windowFrame.origin.y =
2622 NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
2625 [window setFrame: windowFrame display: NO];
2628 /* Override the default frame above if the user has adjusted windows in
2630 if (autosaveName) [window setFrameAutosaveName:autosaveName];
2633 * Tell it about its term. Do this after we've sized it so that the
2634 * sizing doesn't trigger redrawing and such.
2636 [context setTerm:t];
2639 * Only order front if it's the first term. Other terms will be ordered
2640 * front from AngbandUpdateWindowVisibility(). This is to work around a
2641 * problem where Angband aggressively tells us to initialize terms that
2642 * don't do anything!
2644 if (t == angband_term[0])
2645 [context.primaryWindow makeKeyAndOrderFront: nil];
2647 NSEnableScreenUpdates();
2649 /* Set "mapped" flag */
2650 t->mapped_flag = true;
2659 static void Term_nuke_cocoa(term *t)
2662 AngbandContext *context = (__bridge AngbandContext*) (t->data);
2665 /* Tell the context to get rid of its windows, etc. */
2668 /* Balance our CFBridgingRetain from when we created it */
2678 * Returns the CGImageRef corresponding to an image with the given name in the
2679 * resource directory, transferring ownership to the caller
2681 static CGImageRef create_angband_image(NSString *path)
2683 CGImageRef decodedImage = NULL, result = NULL;
2685 /* Try using ImageIO to load the image */
2688 NSURL *url = [[NSURL alloc] initFileURLWithPath:path isDirectory:NO];
2691 NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:(id)kCFBooleanTrue, kCGImageSourceShouldCache, nil];
2692 CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, (CFDictionaryRef)options);
2695 /* We really want the largest image, but in practice there's
2696 * only going to be one */
2697 decodedImage = CGImageSourceCreateImageAtIndex(source, 0, (CFDictionaryRef)options);
2703 /* Draw the sucker to defeat ImageIO's weird desire to cache and decode on
2704 * demand. Our images aren't that big! */
2707 size_t width = CGImageGetWidth(decodedImage), height = CGImageGetHeight(decodedImage);
2709 /* Compute our own bitmap info */
2710 CGBitmapInfo imageBitmapInfo = CGImageGetBitmapInfo(decodedImage);
2711 CGBitmapInfo contextBitmapInfo = kCGBitmapByteOrderDefault;
2713 switch (imageBitmapInfo & kCGBitmapAlphaInfoMask) {
2714 case kCGImageAlphaNone:
2715 case kCGImageAlphaNoneSkipLast:
2716 case kCGImageAlphaNoneSkipFirst:
2718 contextBitmapInfo |= kCGImageAlphaNone;
2721 /* Some alpha, use premultiplied last which is most efficient. */
2722 contextBitmapInfo |= kCGImageAlphaPremultipliedLast;
2726 /* Draw the source image flipped, since the view is flipped */
2727 CGContextRef ctx = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(decodedImage), CGImageGetBytesPerRow(decodedImage), CGImageGetColorSpace(decodedImage), contextBitmapInfo);
2729 CGContextSetBlendMode(ctx, kCGBlendModeCopy);
2730 CGContextTranslateCTM(ctx, 0.0, height);
2731 CGContextScaleCTM(ctx, 1.0, -1.0);
2733 ctx, CGRectMake(0, 0, width, height), decodedImage);
2734 result = CGBitmapContextCreateImage(ctx);
2738 CGImageRelease(decodedImage);
2746 static errr Term_xtra_cocoa_react(void)
2748 /* Don't actually switch graphics until the game is running */
2749 if (!initialized || !game_in_progress) return (-1);
2752 AngbandContext *angbandContext =
2753 (__bridge AngbandContext*) (Term->data);
2755 /* Handle graphics */
2756 int expected_graf_mode = (current_graphics_mode) ?
2757 current_graphics_mode->grafID : GRAPHICS_NONE;
2758 if (graf_mode_req != expected_graf_mode)
2760 graphics_mode *new_mode;
2761 if (graf_mode_req != GRAPHICS_NONE) {
2762 new_mode = get_graphics_mode(graf_mode_req);
2767 /* Get rid of the old image. CGImageRelease is NULL-safe. */
2768 CGImageRelease(pict_image);
2771 /* Try creating the image if we want one */
2772 if (new_mode != NULL)
2774 NSString *img_path =
2775 [NSString stringWithFormat:@"%s/%s", new_mode->path, new_mode->file];
2776 pict_image = create_angband_image(img_path);
2778 /* If we failed to create the image, revert to ASCII. */
2782 arg_bigtile = FALSE;
2784 [[NSUserDefaults angbandDefaults]
2785 setInteger:GRAPHICS_NONE
2786 forKey:AngbandGraphicsDefaultsKey];
2787 [[NSUserDefaults angbandDefaults] synchronize];
2789 NSString *msg = NSLocalizedStringWithDefaultValue(
2790 @"Error.TileSetLoadFailed",
2791 AngbandMessageCatalog,
2792 [NSBundle mainBundle],
2793 @"Failed to Load Tile Set",
2794 @"Alert text for failed tile set load");
2795 NSString *info = NSLocalizedStringWithDefaultValue(
2796 @"Error.TileSetRevertToASCII",
2797 AngbandMessageCatalog,
2798 [NSBundle mainBundle],
2799 @"Could not load the tile set. Switched back to ASCII.",
2800 @"Alert informative message for failed tile set load");
2801 NSAlert *alert = [[NSAlert alloc] init];
2803 alert.messageText = msg;
2804 alert.informativeText = info;
2805 NSModalResponse result = [alert runModal];
2809 /* Record what we did */
2810 use_graphics = new_mode ? new_mode->grafID : 0;
2811 ANGBAND_GRAF = (new_mode ? new_mode->graf : "ascii");
2812 current_graphics_mode = new_mode;
2815 * Enable or disable higher picts. Note: this should be done for
2818 angbandContext->terminal->higher_pict = !! use_graphics;
2820 if (pict_image && current_graphics_mode)
2823 * Compute the row and column count via the image height and
2826 pict_rows = (int)(CGImageGetHeight(pict_image) /
2827 current_graphics_mode->cell_height);
2828 pict_cols = (int)(CGImageGetWidth(pict_image) /
2829 current_graphics_mode->cell_width);
2838 if (arg_bigtile == use_bigtile)
2844 if (arg_bigtile != use_bigtile) {
2848 Term_activate(angband_term[0]);
2849 Term_resize(angband_term[0]->wid, angband_term[0]->hgt);
2859 * Draws one tile as a helper function for Term_xtra_cocoa_fresh().
2861 static void draw_image_tile(
2862 NSGraphicsContext* nsContext,
2863 CGContextRef cgContext,
2867 NSCompositingOperation op)
2869 /* Flip the source rect since the source image is flipped */
2870 CGAffineTransform flip = CGAffineTransformIdentity;
2871 flip = CGAffineTransformTranslate(flip, 0.0, CGImageGetHeight(image));
2872 flip = CGAffineTransformScale(flip, 1.0, -1.0);
2873 CGRect flippedSourceRect =
2874 CGRectApplyAffineTransform(NSRectToCGRect(srcRect), flip);
2877 * When we use high-quality resampling to draw a tile, pixels from outside
2878 * the tile may bleed in, causing graphics artifacts. Work around that.
2880 CGImageRef subimage =
2881 CGImageCreateWithImageInRect(image, flippedSourceRect);
2882 [nsContext setCompositingOperation:op];
2883 CGContextDrawImage(cgContext, NSRectToCGRect(dstRect), subimage);
2884 CGImageRelease(subimage);
2889 * This is a helper function for Term_xtra_cocoa_fresh(): look before a block
2890 * of text on a row to see if the bounds for rendering and clipping need to be
2893 static void query_before_text(
2894 PendingTermChanges *tc, int iy, int npre, int* pclip, int* prend)
2900 if (i < 0 || i < start - npre) {
2903 enum PendingCellChangeType ctype = [tc getCellChangeType:i row:iy];
2905 if (ctype == CELL_CHANGE_TILE) {
2907 * The cell has been rendered with a tile. Do not want to modify
2908 * its contents so the clipping and rendering region can not be
2912 } else if (ctype == CELL_CHANGE_NONE) {
2914 * It has not changed (or using big tile mode and it is within
2915 * a changed tile but is not the left cell for that tile) so
2916 * inquire what it is.
2921 Term_what(i, iy, a + 1, c + 1);
2922 if (use_graphics && (a[1] & 0x80) && (c[1] & 0x80)) {
2924 * It is an unchanged location rendered with a tile. Do not
2925 * want to modify its contents so the clipping and rendering
2926 * region can not be extended.
2930 if (use_bigtile && i > 0) {
2931 Term_what(i - 1, iy, a, c);
2932 if (use_graphics && (a[0] & 0x80) && (c[0] & 0x80)) {
2934 * It is the right cell of a location rendered with a tile.
2935 * Do not want to modify its contents so the clipping and
2936 * rendering region can not be exteded.
2942 * It is unchanged text. A character from the changed region
2943 * may have extended into it so render it to clear that.
2946 /* Check to see if it is the second part of a kanji character. */
2948 Term_what(i - 1, iy, a, c);
2950 [tc markTextChange:i-1 row:iy
2951 glyph:convert_two_byte_eucjp_to_utf16_native(c)
2952 color:a[0] isDoubleWidth:YES];
2957 [tc markTextChange:i row:iy
2958 glyph:c[1] color:a[1] isDoubleWidth:NO];
2963 [tc markTextChange:i row:iy
2964 glyph:c[1] color:a[1] isDoubleWidth:NO];
2969 [tc markTextChange:i row:iy
2970 glyph:c[1] color:a[1] isDoubleWidth:NO];
2977 * The cell has been wiped or had changed text rendered. Do
2978 * not need to render. Can extend the clipping rectangle into it.
2988 * This is a helper function for Term_xtra_cocoa_fresh(): look after a block
2989 * of text on a row to see if the bounds for rendering and clipping need to be
2992 static void query_after_text(
2993 PendingTermChanges *tc, int iy, int npost, int* pclip, int* prend)
2997 int ncol = tc.columnCount;
3004 enum PendingCellChangeType ctype = [tc getCellChangeType:i row:iy];
3007 * Be willing to consolidate this block with the one after it. This
3008 * logic should be sufficient to avoid redraws of the region between
3009 * changed blocks of text if angbandContext.nColPre is zero or one.
3010 * For larger values of nColPre, would need to do something more to
3011 * avoid extra redraws.
3013 if (i > end + npost && ctype != CELL_CHANGE_TEXT &&
3014 ctype != CELL_CHANGE_WIPE) {
3018 if (ctype == CELL_CHANGE_TILE) {
3020 * The cell has been rendered with a tile. Do not want to modify
3021 * its contents so the clipping and rendering region can not be
3025 } else if (ctype == CELL_CHANGE_NONE) {
3026 /* It has not changed so inquire what it is. */
3030 Term_what(i, iy, a, c);
3031 if (use_graphics && (a[0] & 0x80) && (c[0] & 0x80)) {
3033 * It is an unchanged location rendered with a tile. Do not
3034 * want to modify its contents so the clipping and rendering
3035 * region can not be extended.
3040 * It is unchanged text. A character from the changed region
3041 * may have extended into it so render it to clear that.
3044 /* Check to see if it is the first part of a kanji character. */
3046 Term_what(i + 1, iy, a + 1, c + 1);
3048 [tc markTextChange:i row:iy
3049 glyph:convert_two_byte_eucjp_to_utf16_native(c)
3050 color:a[0] isDoubleWidth:YES];
3055 [tc markTextChange:i row:iy
3056 glyph:c[0] color:a[0] isDoubleWidth:NO];
3061 [tc markTextChange:i row:iy
3062 glyph:c[0] color:a[0] isDoubleWidth:NO];
3067 [tc markTextChange:i row:iy
3068 glyph:c[0] color:a[0] isDoubleWidth:NO];
3075 * Have come to another region of changed text or another region
3076 * to wipe. Combine the regions to minimize redraws.
3088 * Draw the pending changes saved in angbandContext->changes.
3090 static void Term_xtra_cocoa_fresh(AngbandContext* angbandContext)
3092 int graf_width, graf_height, alphablend;
3094 if (angbandContext.changes.hasTileChanges) {
3095 CGImageAlphaInfo ainfo = CGImageGetAlphaInfo(pict_image);
3097 graf_width = current_graphics_mode->cell_width;
3098 graf_height = current_graphics_mode->cell_height;
3100 * As of this writing, a value of zero for
3101 * current_graphics_mode->alphablend can mean either that the tile set
3102 * doesn't have an alpha channel or it does but it only takes on values
3103 * of 0 or 255. For main-cocoa.m's purposes, the latter is rendered
3104 * using the same procedure as if alphablend was nonzero. The former
3105 * is handled differently, but alphablend doesn't distinguish it from
3106 * the latter. So ignore alphablend and directly test whether an
3107 * alpha channel is present.
3109 alphablend = (ainfo & (kCGImageAlphaPremultipliedFirst |
3110 kCGImageAlphaPremultipliedLast)) ? 1 : 0;
3117 CGContextRef ctx = [angbandContext lockFocus];
3119 if (angbandContext.changes.hasTextChanges ||
3120 angbandContext.changes.hasWipeChanges) {
3121 NSFont *selectionFont = [angbandContext.angbandViewFont screenFont];
3122 [selectionFont set];
3125 for (int iy = angbandContext.changes.firstChangedRow;
3126 iy <= angbandContext.changes.lastChangedRow;
3128 /* Skip untouched rows. */
3129 if ([angbandContext.changes getFirstChangedColumnInRow:iy] >
3130 [angbandContext.changes getLastChangedColumnInRow:iy]) {
3133 int ix = [angbandContext.changes getFirstChangedColumnInRow:iy];
3134 int ixmax = [angbandContext.changes getLastChangedColumnInRow:iy];
3143 switch ([angbandContext.changes getCellChangeType:ix row:iy]) {
3144 case CELL_CHANGE_NONE:
3148 case CELL_CHANGE_TILE:
3151 * Because changes are made to the compositing mode, save
3152 * the incoming value.
3154 NSGraphicsContext *nsContext =
3155 [NSGraphicsContext currentContext];
3156 NSCompositingOperation op = nsContext.compositingOperation;
3157 int step = (use_bigtile) ? 2 : 1;
3160 while (jx <= ixmax &&
3161 [angbandContext.changes getCellChangeType:jx row:iy]
3162 == CELL_CHANGE_TILE) {
3163 NSRect destinationRect =
3164 [angbandContext rectInImageForTileAtX:jx Y:iy];
3165 struct PendingTileChange tileIndices =
3166 [angbandContext.changes
3167 getCellTileChange:jx row:iy];
3168 NSRect sourceRect, terrainRect;
3170 destinationRect.size.width *= step;
3171 sourceRect.origin.x = graf_width * tileIndices.fgdCol;
3172 sourceRect.origin.y = graf_height * tileIndices.fgdRow;
3173 sourceRect.size.width = graf_width;
3174 sourceRect.size.height = graf_height;
3175 terrainRect.origin.x = graf_width * tileIndices.bckCol;
3176 terrainRect.origin.y = graf_height *
3178 terrainRect.size.width = graf_width;
3179 terrainRect.size.height = graf_height;
3189 * Skip drawing the foreground if it is the same
3190 * as the background.
3192 if (sourceRect.origin.x != terrainRect.origin.x ||
3193 sourceRect.origin.y != terrainRect.origin.y) {
3200 NSCompositeSourceOver);
3214 [nsContext setCompositingOperation:op];
3217 [angbandContext rectInImageForTileAtX:ix Y:iy];
3219 angbandContext.tileSize.width * (jx - ix);
3220 [angbandContext setNeedsDisplayInBaseRect:rect];
3225 case CELL_CHANGE_WIPE:
3226 case CELL_CHANGE_TEXT:
3228 * For a wiped region, treat it as if it had text (the only
3229 * loss if it was not is some extra work rendering
3230 * neighboring unchanged text).
3234 if (jx >= angbandContext.cols) {
3237 enum PendingCellChangeType ctype =
3238 [angbandContext.changes getCellChangeType:jx row:iy];
3239 if (ctype != CELL_CHANGE_TEXT &&
3240 ctype != CELL_CHANGE_WIPE) {
3247 int ieclip = jx - 1;
3249 int ierend = jx - 1;
3251 TERM_COLOR alast = 0;
3256 angbandContext.changes,
3258 angbandContext.nColPre,
3262 angbandContext.changes,
3264 angbandContext.nColPost,
3270 /* Save the state since the clipping will be modified. */
3271 CGContextSaveGState(ctx);
3273 /* Clear the area where rendering will be done. */
3274 r = [angbandContext rectInImageForTileAtX:isrend Y:iy];
3275 r.size.width = angbandContext.tileSize.width *
3276 (ierend - isrend + 1);
3277 [[NSColor blackColor] set];
3281 * Clear the current path so it does not affect clipping.
3282 * Then set the clipping rectangle. Using
3283 * CGContextSetTextDrawingMode() to include clipping does
3284 * not appear to be necessary on 10.14 and is actually
3285 * detrimental: when displaying more than one character,
3286 * only the first is visible.
3288 CGContextBeginPath(ctx);
3289 r = [angbandContext rectInImageForTileAtX:isclip Y:iy];
3290 r.size.width = angbandContext.tileSize.width *
3291 (ieclip - isclip + 1);
3292 CGContextClipToRect(ctx, r);
3296 while (k <= ierend) {
3297 if ([angbandContext.changes getCellChangeType:k row:iy]
3298 == CELL_CHANGE_WIPE) {
3299 /* Skip over since no rendering is necessary. */
3304 struct PendingTextChange textChange =
3305 [angbandContext.changes getCellTextChange:k
3307 int anew = textChange.color % MAX_COLORS;
3308 if (set_color || alast != anew) {
3311 set_color_for_index(anew);
3315 [angbandContext rectInImageForTileAtX:k Y:iy];
3316 if (textChange.doubleWidth) {
3317 rectToDraw.size.width *= 2.0;
3318 [angbandContext drawWChar:textChange.glyph
3319 inRect:rectToDraw context:ctx];
3322 [angbandContext drawWChar:textChange.glyph
3323 inRect:rectToDraw context:ctx];
3329 * Inform the context that the area in the clipping
3330 * rectangle needs to be redisplayed.
3332 [angbandContext setNeedsDisplayInBaseRect:r];
3334 CGContextRestoreGState(ctx);
3341 if (angbandContext.changes.cursorColumn >= 0 &&
3342 angbandContext.changes.cursorRow >= 0) {
3343 NSRect rect = [angbandContext
3344 rectInImageForTileAtX:angbandContext.changes.cursorColumn
3345 Y:angbandContext.changes.cursorRow];
3347 rect.size.width *= angbandContext.changes.cursorWidth;
3348 rect.size.height *= angbandContext.changes.cursorHeight;
3349 [[NSColor yellowColor] set];
3350 NSFrameRectWithWidth(rect, 1);
3351 /* Invalidate that rect */
3352 [angbandContext setNeedsDisplayInBaseRect:rect];
3355 [angbandContext unlockFocus];
3360 * Do a "special thing"
3362 static errr Term_xtra_cocoa(int n, int v)
3366 AngbandContext* angbandContext =
3367 (__bridge AngbandContext*) (Term->data);
3372 case TERM_XTRA_NOISE:
3377 case TERM_XTRA_SOUND:
3381 /* Process random events */
3382 case TERM_XTRA_BORED:
3384 * Show or hide cocoa windows based on the subwindow flags set by
3387 AngbandUpdateWindowVisibility();
3388 /* Process an event */
3389 (void)check_events(CHECK_EVENTS_NO_WAIT);
3392 /* Process pending events */
3393 case TERM_XTRA_EVENT:
3394 /* Process an event */
3395 (void)check_events(v);
3398 /* Flush all pending events (if any) */
3399 case TERM_XTRA_FLUSH:
3400 /* Hack -- flush all events */
3401 while (check_events(CHECK_EVENTS_DRAIN)) /* loop */;
3405 /* Hack -- Change the "soft level" */
3406 case TERM_XTRA_LEVEL:
3408 * Here we could activate (if requested), but I don't think
3409 * Angband should be telling us our window order (the user
3410 * should decide that), so do nothing.
3414 /* Clear the screen */
3415 case TERM_XTRA_CLEAR:
3417 [angbandContext lockFocus];
3418 [[NSColor blackColor] set];
3419 NSRect imageRect = {NSZeroPoint, [angbandContext imageSize]};
3420 NSRectFillUsingOperation(imageRect, NSCompositeCopy);
3421 [angbandContext unlockFocus];
3422 [angbandContext setNeedsDisplay:YES];
3427 /* React to changes */
3428 case TERM_XTRA_REACT:
3429 result = Term_xtra_cocoa_react();
3432 /* Delay (milliseconds) */
3433 case TERM_XTRA_DELAY:
3436 double seconds = v / 1000.;
3437 NSDate* date = [NSDate dateWithTimeIntervalSinceNow:seconds];
3441 event = [NSApp nextEventMatchingMask:-1
3443 inMode:NSDefaultRunLoopMode
3445 if (event) send_event(event);
3447 } while ([date timeIntervalSinceNow] >= 0);
3451 /* Draw the pending changes. */
3452 case TERM_XTRA_FRESH:
3453 Term_xtra_cocoa_fresh(angbandContext);
3454 [angbandContext.changes clear];
3467 static errr Term_curs_cocoa(TERM_LEN x, TERM_LEN y)
3469 AngbandContext *angbandContext = (__bridge AngbandContext*) (Term->data);
3471 [angbandContext.changes markCursor:x row:y];
3478 * Draw a cursor that's two tiles wide. For Japanese, that's used when
3479 * the cursor points at a kanji character, irregardless of whether operating
3482 static errr Term_bigcurs_cocoa(TERM_LEN x, TERM_LEN y)
3484 AngbandContext *angbandContext = (__bridge AngbandContext*) (Term->data);
3486 [angbandContext.changes markBigCursor:x row:y cellsWide:2 cellsHigh:1];
3493 * Low level graphics (Assumes valid input)
3495 * Erase "n" characters starting at (x,y)
3497 static errr Term_wipe_cocoa(TERM_LEN x, TERM_LEN y, int n)
3499 AngbandContext *angbandContext = (__bridge AngbandContext*) (Term->data);
3501 [angbandContext.changes markWipeRange:x row:y n:n];
3507 static errr Term_pict_cocoa(TERM_LEN x, TERM_LEN y, int n,
3508 TERM_COLOR *ap, concptr cp,
3509 const TERM_COLOR *tap, concptr tcp)
3511 /* Paranoia: Bail if we don't have a current graphics mode */
3512 if (! current_graphics_mode) return -1;
3514 AngbandContext* angbandContext = (__bridge AngbandContext*) (Term->data);
3515 int step = (use_bigtile) ? 2 : 1;
3518 * In bigtile mode, it is sufficient that the bounds for the modified
3519 * region only encompass the left cell for the region affected by the
3520 * tile and that only that cell has to have the details of the changes.
3522 for (int i = x; i < x + n * step; i += step) {
3523 TERM_COLOR a = *ap++;
3525 TERM_COLOR ta = *tap++;
3528 if (use_graphics && (a & 0x80) && (c & 0x80)) {
3529 [angbandContext.changes markTileChange:i row:y
3530 foregroundCol:((byte)c & 0x7F) % pict_cols
3531 foregroundRow:((byte)a & 0x7F) % pict_rows
3532 backgroundCol:((byte)tc & 0x7F) % pict_cols
3533 backgroundRow:((byte)ta & 0x7F) % pict_rows];
3542 * Low level graphics. Assumes valid input.
3544 * Draw several ("n") chars, with an attr, at a given location.
3546 static errr Term_text_cocoa(
3547 TERM_LEN x, TERM_LEN y, int n, TERM_COLOR a, concptr cp)
3549 AngbandContext* angbandContext = (__bridge AngbandContext*) (Term->data);
3557 * The second byte of the character is past the end. Ignore
3562 [angbandContext.changes markTextChange:i+x row:y
3563 glyph:convert_two_byte_eucjp_to_utf16_native(cp)
3564 color:a isDoubleWidth:YES];
3569 [angbandContext.changes markTextChange:i+x row:y
3570 glyph:*cp color:a isDoubleWidth:NO];
3575 [angbandContext.changes markTextChange:i+x row:y
3576 glyph:*cp color:a isDoubleWidth:NO];
3587 * Post a nonsense event so that our event loop wakes up
3589 static void wakeup_event_loop(void)
3591 /* Big hack - send a nonsense event to make us update */
3592 NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined location:NSZeroPoint modifierFlags:0 timestamp:0 windowNumber:0 context:NULL subtype:AngbandEventWakeup data1:0 data2:0];
3593 [NSApp postEvent:event atStart:NO];
3598 * Handle the "open_when_ready" flag
3600 static void handle_open_when_ready(void)
3602 /* Check the flag XXX XXX XXX make a function for this */
3603 if (open_when_ready && initialized && !game_in_progress)
3606 open_when_ready = FALSE;
3608 /* Game is in progress */
3609 game_in_progress = TRUE;
3611 /* Wait for a keypress */
3612 pause_line(Term->hgt - 1);
3618 * Handle quit_when_ready, by Peter Ammon,
3619 * slightly modified to check inkey_flag.
3621 static void quit_calmly(void)
3623 /* Quit immediately if game's not started */
3624 if (!game_in_progress || !character_generated) quit(NULL);
3626 /* Save the game and Quit (if it's safe) */
3629 /* Hack -- Forget messages and term */
3631 Term->mapped_flag = FALSE;
3634 do_cmd_save_game(FALSE);
3635 record_current_savefile();
3641 /* Wait until inkey_flag is set */
3647 * Returns YES if we contain an AngbandView (and hence should direct our events
3650 static BOOL contains_angband_view(NSView *view)
3652 if ([view isKindOfClass:[AngbandView class]]) return YES;
3653 for (NSView *subview in [view subviews]) {
3654 if (contains_angband_view(subview)) return YES;
3661 * Queue mouse presses if they occur in the map section of the main window.
3663 static void AngbandHandleEventMouseDown( NSEvent *event )
3666 AngbandContext *angbandContext = [[[event window] contentView] angbandContext];
3667 AngbandContext *mainAngbandContext =
3668 (__bridge AngbandContext*) (angband_term[0]->data);
3670 if (mainAngbandContext.primaryWindow &&
3671 [[event window] windowNumber] ==
3672 [mainAngbandContext.primaryWindow windowNumber])
3674 int cols, rows, x, y;
3675 Term_get_size(&cols, &rows);
3676 NSSize tileSize = angbandContext.tileSize;
3677 NSSize border = angbandContext.borderSize;
3678 NSPoint windowPoint = [event locationInWindow];
3680 /* Adjust for border; add border height because window origin is at
3682 windowPoint = NSMakePoint( windowPoint.x - border.width, windowPoint.y + border.height );
3684 NSPoint p = [[[event window] contentView] convertPoint: windowPoint fromView: nil];
3685 x = floor( p.x / tileSize.width );
3686 y = floor( p.y / tileSize.height );
3688 /* Being safe about this, since xcode doesn't seem to like the
3689 * bool_hack stuff */
3690 BOOL displayingMapInterface = ((int)inkey_flag != 0);
3692 /* Sidebar plus border == thirteen characters; top row is reserved. */
3693 /* Coordinates run from (0,0) to (cols-1, rows-1). */
3694 BOOL mouseInMapSection = (x > 13 && x <= cols - 1 && y > 0 && y <= rows - 2);
3696 /* If we are displaying a menu, allow clicks anywhere; if we are
3697 * displaying the main game interface, only allow clicks in the map
3699 if (!displayingMapInterface || (displayingMapInterface && mouseInMapSection))
3701 /* [event buttonNumber] will return 0 for left click,
3702 * 1 for right click, but this is safer */
3703 int button = ([event type] == NSLeftMouseDown) ? 1 : 2;
3706 NSUInteger eventModifiers = [event modifierFlags];
3707 byte angbandModifiers = 0;
3708 angbandModifiers |= (eventModifiers & NSShiftKeyMask) ? KC_MOD_SHIFT : 0;
3709 angbandModifiers |= (eventModifiers & NSControlKeyMask) ? KC_MOD_CONTROL : 0;
3710 angbandModifiers |= (eventModifiers & NSAlternateKeyMask) ? KC_MOD_ALT : 0;
3711 button |= (angbandModifiers & 0x0F) << 4; /* encode modifiers in the button number (see Term_mousepress()) */
3714 Term_mousepress(x, y, button);
3719 /* Pass click through to permit focus change, resize, etc. */
3720 [NSApp sendEvent:event];
3726 * Encodes an NSEvent Angband-style, or forwards it along. Returns YES if the
3727 * event was sent to Angband, NO if Cocoa (or nothing) handled it */
3728 static BOOL send_event(NSEvent *event)
3731 /* If the receiving window is not an Angband window, then do nothing */
3732 if (! contains_angband_view([[event window] contentView]))
3734 [NSApp sendEvent:event];
3738 /* Analyze the event */
3739 switch ([event type])
3743 /* Try performing a key equivalent */
3744 if ([[NSApp mainMenu] performKeyEquivalent:event]) break;
3746 unsigned modifiers = [event modifierFlags];
3748 /* Send all NSCommandKeyMasks through */
3749 if (modifiers & NSCommandKeyMask)
3751 [NSApp sendEvent:event];
3755 if (! [[event characters] length]) break;
3758 /* Extract some modifiers */
3759 int mc = !! (modifiers & NSControlKeyMask);
3760 int ms = !! (modifiers & NSShiftKeyMask);
3761 int mo = !! (modifiers & NSAlternateKeyMask);
3762 int kp = !! (modifiers & NSNumericPadKeyMask);
3765 /* Get the Angband char corresponding to this unichar */
3766 unichar c = [[event characters] characterAtIndex:0];
3769 * Have anything from the numeric keypad generate a macro
3770 * trigger so that shift or control modifiers can be passed.
3772 if (c <= 0x7F && !kp)
3778 * The rest of Hengband uses Angband 2.7's or so key handling:
3779 * so for the rest do something like the encoding that
3780 * main-win.c does: send a macro trigger with the Unicode
3781 * value encoded into printable ASCII characters.
3786 /* override special keys */
3787 switch([event keyCode]) {
3788 case kVK_Return: ch = '\r'; break;
3789 case kVK_Escape: ch = 27; break;
3790 case kVK_Tab: ch = '\t'; break;
3791 case kVK_Delete: ch = '\b'; break;
3792 case kVK_ANSI_KeypadEnter: ch = '\r'; kp = TRUE; break;
3795 /* Hide the mouse pointer */
3796 [NSCursor setHiddenUntilMouseMoves:YES];
3806 * Could use the hexsym global but some characters overlap with
3807 * those used to indicate modifiers.
3809 const char encoded[16] = {
3810 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
3814 /* Begin the macro trigger. */
3817 /* Send the modifiers. */
3818 if (mc) Term_keypress('C');
3819 if (ms) Term_keypress('S');
3820 if (mo) Term_keypress('O');
3821 if (kp) Term_keypress('K');
3824 Term_keypress(encoded[c & 0xF]);
3828 /* End the macro trigger. */
3835 case NSLeftMouseDown:
3836 case NSRightMouseDown:
3837 AngbandHandleEventMouseDown(event);
3840 case NSApplicationDefined:
3842 if ([event subtype] == AngbandEventWakeup)
3850 [NSApp sendEvent:event];
3857 * Check for Events, return TRUE if we process any
3859 static BOOL check_events(int wait)
3864 /* Handles the quit_when_ready flag */
3865 if (quit_when_ready) quit_calmly();
3868 if (wait == CHECK_EVENTS_WAIT) endDate = [NSDate distantFuture];
3869 else endDate = [NSDate distantPast];
3873 if (quit_when_ready)
3875 /* send escape events until we quit */
3876 Term_keypress(0x1B);
3881 event = [NSApp nextEventMatchingMask:-1 untilDate:endDate
3882 inMode:NSDefaultRunLoopMode dequeue:YES];
3887 if (send_event(event)) break;
3896 * Hook to tell the user something important
3898 static void hook_plog(const char * str)
3902 NSString *msg = NSLocalizedStringWithDefaultValue(
3903 @"Warning", AngbandMessageCatalog, [NSBundle mainBundle],
3904 @"Warning", @"Alert text for generic warning");
3905 NSString *info = [NSString stringWithCString:str
3907 encoding:NSJapaneseEUCStringEncoding
3909 encoding:NSMacOSRomanStringEncoding
3912 NSAlert *alert = [[NSAlert alloc] init];
3914 alert.messageText = msg;
3915 alert.informativeText = info;
3916 NSModalResponse result = [alert runModal];
3922 * Hook to tell the user something, and then quit
3924 static void hook_quit(const char * str)
3931 * Return the path for Angband's lib directory and bail if it isn't found. The
3932 * lib directory should be in the bundle's resources directory, since it's
3933 * copied when built.
3935 static NSString* get_lib_directory(void)
3937 NSString *bundleLibPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: AngbandDirectoryNameLib];
3938 BOOL isDirectory = NO;
3939 BOOL libExists = [[NSFileManager defaultManager] fileExistsAtPath: bundleLibPath isDirectory: &isDirectory];
3941 if( !libExists || !isDirectory )
3943 NSLog( @"Hengband: can't find %@/ in bundle: isDirectory: %d libExists: %d", AngbandDirectoryNameLib, isDirectory, libExists );
3945 NSString *msg = NSLocalizedStringWithDefaultValue(
3946 @"Error.MissingResources",
3947 AngbandMessageCatalog,
3948 [NSBundle mainBundle],
3949 @"Missing Resources",
3950 @"Alert text for missing resources");
3951 NSString *info = NSLocalizedStringWithDefaultValue(
3952 @"Error.MissingAngbandLib",
3953 AngbandMessageCatalog,
3954 [NSBundle mainBundle],
3955 @"Hengband was unable to find required resources and must quit. Please report a bug on the Angband forums.",
3956 @"Alert informative message for missing Angband lib/ folder");
3957 NSString *quit_label = NSLocalizedStringWithDefaultValue(
3958 @"Label.Quit", AngbandMessageCatalog, [NSBundle mainBundle],
3960 NSAlert *alert = [[NSAlert alloc] init];
3963 * Note that NSCriticalAlertStyle was deprecated in 10.10. The
3964 * replacement is NSAlertStyleCritical.
3966 alert.alertStyle = NSCriticalAlertStyle;
3967 alert.messageText = msg;
3968 alert.informativeText = info;
3969 [alert addButtonWithTitle:quit_label];
3970 NSModalResponse result = [alert runModal];
3974 return bundleLibPath;
3978 * Return the path for the directory where Angband should look for its standard
3981 static NSString* get_doc_directory(void)
3983 NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
3985 #if defined(SAFE_DIRECTORY)
3986 NSString *versionedDirectory = [NSString stringWithFormat: @"%@-%s", AngbandDirectoryNameBase, VERSION_STRING];
3987 return [documents stringByAppendingPathComponent: versionedDirectory];
3989 return [documents stringByAppendingPathComponent: AngbandDirectoryNameBase];
3994 * Adjust directory paths as needed to correct for any differences needed by
3995 * Angband. init_file_paths() currently requires that all paths provided have
3996 * a trailing slash and all other platforms honor this.
3998 * \param originalPath The directory path to adjust.
3999 * \return A path suitable for Angband or nil if an error occurred.
4001 static NSString* AngbandCorrectedDirectoryPath(NSString *originalPath)
4003 if ([originalPath length] == 0) {
4007 if (![originalPath hasSuffix: @"/"]) {
4008 return [originalPath stringByAppendingString: @"/"];
4011 return originalPath;
4015 * Give Angband the base paths that should be used for the various directories
4016 * it needs. It will create any needed directories.
4018 static void prepare_paths_and_directories(void)
4020 char libpath[PATH_MAX + 1] = "\0";
4021 NSString *libDirectoryPath =
4022 AngbandCorrectedDirectoryPath(get_lib_directory());
4023 [libDirectoryPath getFileSystemRepresentation: libpath maxLength: sizeof(libpath)];
4025 char basepath[PATH_MAX + 1] = "\0";
4026 NSString *angbandDocumentsPath =
4027 AngbandCorrectedDirectoryPath(get_doc_directory());
4028 [angbandDocumentsPath getFileSystemRepresentation: basepath maxLength: sizeof(basepath)];
4030 init_file_paths(libpath, basepath);
4031 create_needed_dirs();
4035 * Play sound effects asynchronously. Select a sound from any available
4036 * for the required event, and bridge to Cocoa to play it.
4038 static void play_sound(int event)
4040 [[AngbandSoundCatalog sharedSounds] playSound:event];
4044 * ------------------------------------------------------------------------
4046 * ------------------------------------------------------------------------ */
4048 @implementation AngbandAppDelegate
4050 @synthesize graphicsMenu=_graphicsMenu;
4051 @synthesize commandMenu=_commandMenu;
4052 @synthesize commandMenuTagMap=_commandMenuTagMap;
4054 - (IBAction)newGame:sender
4056 /* Game is in progress */
4057 game_in_progress = TRUE;
4061 - (IBAction)editFont:sender
4063 NSFontPanel *panel = [NSFontPanel sharedFontPanel];
4064 NSFont *termFont = [AngbandContext defaultFont];
4067 for (i=0; i < ANGBAND_TERM_MAX; i++) {
4068 AngbandContext *context =
4069 (__bridge AngbandContext*) (angband_term[i]->data);
4070 if ([context isMainWindow]) {
4071 termFont = [context angbandViewFont];
4076 [panel setPanelFont:termFont isMultiple:NO];
4077 [panel orderFront:self];
4081 * Implent NSObject's changeFont() method to receive a notification about the
4082 * changed font. Note that, as of 10.14, changeFont() is deprecated in
4083 * NSObject - it will be removed at some point and the application delegate
4084 * will have to be declared as implementing the NSFontChanging protocol.
4086 - (void)changeFont:(id)sender
4089 for (mainTerm=0; mainTerm < ANGBAND_TERM_MAX; mainTerm++) {
4090 AngbandContext *context =
4091 (__bridge AngbandContext*) (angband_term[mainTerm]->data);
4092 if ([context isMainWindow]) {
4097 /* Bug #1709: Only change font for angband windows */
4098 if (mainTerm == ANGBAND_TERM_MAX) return;
4100 NSFont *oldFont = [AngbandContext defaultFont];
4101 NSFont *newFont = [sender convertFont:oldFont];
4102 if (! newFont) return; /*paranoia */
4104 /* Store as the default font if we changed the first term */
4105 if (mainTerm == 0) {
4106 [AngbandContext setDefaultFont:newFont];
4109 /* Record it in the preferences */
4110 NSUserDefaults *defs = [NSUserDefaults angbandDefaults];
4111 [defs setValue:[newFont fontName]
4112 forKey:[NSString stringWithFormat:@"FontName-%d", mainTerm]];
4113 [defs setFloat:[newFont pointSize]
4114 forKey:[NSString stringWithFormat:@"FontSize-%d", mainTerm]];
4117 NSDisableScreenUpdates();
4120 AngbandContext *angbandContext =
4121 (__bridge AngbandContext*) (angband_term[mainTerm]->data);
4122 [(id)angbandContext setSelectionFont:newFont adjustTerminal: YES];
4124 NSEnableScreenUpdates();
4126 if (mainTerm == 0 && game_in_progress) {
4127 /* Mimics the logic in setGraphicsMode(). */
4129 wakeup_event_loop();
4131 [(id)angbandContext requestRedraw];
4135 - (IBAction)openGame:sender
4138 BOOL selectedSomething = NO;
4141 /* Get where we think the save files are */
4142 NSURL *startingDirectoryURL =
4143 [NSURL fileURLWithPath:[NSString stringWithCString:ANGBAND_DIR_SAVE encoding:NSASCIIStringEncoding]
4146 /* Set up an open panel */
4147 NSOpenPanel* panel = [NSOpenPanel openPanel];
4148 [panel setCanChooseFiles:YES];
4149 [panel setCanChooseDirectories:NO];
4150 [panel setResolvesAliases:YES];
4151 [panel setAllowsMultipleSelection:NO];
4152 [panel setTreatsFilePackagesAsDirectories:YES];
4153 [panel setDirectoryURL:startingDirectoryURL];
4156 panelResult = [panel runModal];
4157 if (panelResult == NSOKButton)
4159 NSArray* fileURLs = [panel URLs];
4160 if ([fileURLs count] > 0 && [[fileURLs objectAtIndex:0] isFileURL])
4162 NSURL* savefileURL = (NSURL *)[fileURLs objectAtIndex:0];
4164 * The path property doesn't do the right thing except for
4165 * URLs with the file scheme. We had
4166 * getFileSystemRepresentation here before, but that wasn't
4167 * introduced until OS X 10.9.
4169 selectedSomething = [[savefileURL path]
4171 maxLength:sizeof savefile
4172 encoding:NSMacOSRomanStringEncoding];
4176 if (selectedSomething)
4178 /* Remember this so we can select it by default next time */
4179 record_current_savefile();
4181 /* Game is in progress */
4182 game_in_progress = TRUE;
4188 - (IBAction)saveGame:sender
4190 /* Hack -- Forget messages */
4194 do_cmd_save_game(FALSE);
4196 /* Record the current save file so we can select it by default next time.
4197 * It's a little sketchy that this only happens when we save through the
4198 * menu; ideally game-triggered saves would trigger it too. */
4199 record_current_savefile();
4203 * Create and initialize Angband terminal number "termIndex".
4205 - (void)linkTermData:(int)termIndex
4207 NSArray *terminalDefaults = [[NSUserDefaults standardUserDefaults]
4208 valueForKey: AngbandTerminalsDefaultsKey];
4209 NSInteger rows = 24;
4210 NSInteger columns = 80;
4212 if (termIndex < (int)[terminalDefaults count]) {
4213 NSDictionary *term = [terminalDefaults objectAtIndex:termIndex];
4214 rows = [[term valueForKey: AngbandTerminalRowsDefaultsKey]
4216 columns = [[term valueForKey: AngbandTerminalColumnsDefaultsKey]
4221 term *newterm = ZNEW(term);
4223 /* Initialize the term */
4224 term_init(newterm, columns, rows, 256 /* keypresses, for some reason? */);
4226 /* Differentiate between BS/^h, Tab/^i, etc. */
4227 /* newterm->complex_input = TRUE; */
4229 /* Use a "software" cursor */
4230 newterm->soft_cursor = TRUE;
4232 /* Disable the per-row flush notifications since they are not used. */
4233 newterm->never_frosh = TRUE;
4235 /* Erase with "white space" */
4236 newterm->attr_blank = TERM_WHITE;
4237 newterm->char_blank = ' ';
4239 /* Prepare the init/nuke hooks */
4240 newterm->init_hook = Term_init_cocoa;
4241 newterm->nuke_hook = Term_nuke_cocoa;
4243 /* Prepare the function hooks */
4244 newterm->xtra_hook = Term_xtra_cocoa;
4245 newterm->wipe_hook = Term_wipe_cocoa;
4246 newterm->curs_hook = Term_curs_cocoa;
4247 newterm->bigcurs_hook = Term_bigcurs_cocoa;
4248 newterm->text_hook = Term_text_cocoa;
4249 newterm->pict_hook = Term_pict_cocoa;
4250 /* newterm->mbcs_hook = Term_mbcs_cocoa; */
4252 /* Global pointer */
4253 angband_term[termIndex] = newterm;
4257 * Allocate the primary Angband terminal and activate it. Allocate the other
4258 * Angband terminals.
4260 - (void)initWindows {
4261 for (int i = 0; i < ANGBAND_TERM_MAX; i++) {
4262 [self linkTermData:i];
4265 Term_activate(angband_term[0]);
4269 * Load preferences from preferences file for current host+current user+
4270 * current application.
4274 NSUserDefaults *defs = [NSUserDefaults angbandDefaults];
4276 /* Make some default defaults */
4277 NSMutableArray *defaultTerms = [[NSMutableArray alloc] init];
4280 * The following default rows/cols were determined experimentally by first
4281 * finding the ideal window/font size combinations. But because of awful
4282 * temporal coupling in Term_init_cocoa(), it's impossible to set up the
4283 * defaults there, so we do it this way.
4285 for (NSUInteger i = 0; i < ANGBAND_TERM_MAX; i++) {
4321 NSDictionary *standardTerm =
4322 [NSDictionary dictionaryWithObjectsAndKeys:
4323 [NSNumber numberWithInt: rows], AngbandTerminalRowsDefaultsKey,
4324 [NSNumber numberWithInt: columns], AngbandTerminalColumnsDefaultsKey,
4325 [NSNumber numberWithBool: visible], AngbandTerminalVisibleDefaultsKey,
4327 [defaultTerms addObject: standardTerm];
4330 NSDictionary *defaults = [[NSDictionary alloc] initWithObjectsAndKeys:
4332 @"Osaka", @"FontName",
4334 @"Menlo", @"FontName",
4336 [NSNumber numberWithFloat:13.f], @"FontSize",
4337 [NSNumber numberWithInt:60], AngbandFrameRateDefaultsKey,
4338 [NSNumber numberWithBool:YES], AngbandSoundDefaultsKey,
4339 [NSNumber numberWithInt:GRAPHICS_NONE], AngbandGraphicsDefaultsKey,
4340 [NSNumber numberWithBool:YES], AngbandBigTileDefaultsKey,
4341 defaultTerms, AngbandTerminalsDefaultsKey,
4343 [defs registerDefaults:defaults];
4345 /* Preferred graphics mode */
4346 graf_mode_req = [defs integerForKey:AngbandGraphicsDefaultsKey];
4347 if (graf_mode_req != GRAPHICS_NONE &&
4348 get_graphics_mode(graf_mode_req)->grafID != GRAPHICS_NONE &&
4349 [defs boolForKey:AngbandBigTileDefaultsKey] == YES) {
4353 use_bigtile = FALSE;
4354 arg_bigtile = FALSE;
4357 /* Use sounds; set the Angband global */
4358 if ([defs boolForKey:AngbandSoundDefaultsKey] == YES) {
4360 [AngbandSoundCatalog sharedSounds].enabled = YES;
4363 [AngbandSoundCatalog sharedSounds].enabled = NO;
4367 frames_per_second = [defs integerForKey:AngbandFrameRateDefaultsKey];
4371 setDefaultFont:[NSFont fontWithName:[defs valueForKey:@"FontName-0"]
4372 size:[defs floatForKey:@"FontSize-0"]]];
4373 if (! [AngbandContext defaultFont])
4375 setDefaultFont:[NSFont fontWithName:@"Menlo" size:13.]];
4379 * Entry point for initializing Angband
4384 /* Hooks in some "z-util.c" hooks */
4385 plog_aux = hook_plog;
4386 quit_aux = hook_quit;
4388 /* Initialize file paths */
4389 prepare_paths_and_directories();
4391 /* Note the "system" */
4392 ANGBAND_SYS = "coc";
4394 /* Load possible graphics modes */
4395 init_graphics_modes();
4397 /* Load preferences */
4400 /* Prepare the windows */
4403 /* Set up game event handlers */
4404 /* init_display(); */
4406 /* Register the sound hook */
4407 /* sound_hook = play_sound; */
4409 /* Initialise game */
4412 /* Initialize some save file stuff */
4413 player_egid = getegid();
4415 /* We are now initialized */
4418 /* Handle "open_when_ready" */
4419 handle_open_when_ready();
4421 /* Handle pending events (most notably update) and flush input */
4424 /* Prompt the user */
4425 int message_row = (Term->hgt - 23) / 5 + 23;
4426 Term_erase(0, message_row, 255);
4429 "['ファイル' メニューから '新' または '開く' を選択します]",
4430 message_row, (Term->wid - 57) / 2
4432 "[Choose 'New' or 'Open' from the 'File' menu]",
4433 message_row, (Term->wid - 45) / 2
4439 while (!game_in_progress) {
4441 NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantFuture] inMode:NSDefaultRunLoopMode dequeue:YES];
4442 if (event) [NSApp sendEvent:event];
4447 * Play a game -- "new_game" is set by "new", "open" or the open document
4448 * even handler as appropriate
4451 play_game(new_game);
4457 * Implement NSObject's validateMenuItem() method to override enabling or
4458 * disabling a menu item. Note that, as of 10.14, validateMenuItem() is
4459 * deprecated in NSObject - it will be removed at some point and the
4460 * application delegate will have to be declared as implementing the
4461 * NSMenuItemValidation protocol.
4463 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
4465 SEL sel = [menuItem action];
4466 NSInteger tag = [menuItem tag];
4468 if( tag >= AngbandWindowMenuItemTagBase && tag < AngbandWindowMenuItemTagBase + ANGBAND_TERM_MAX )
4470 if( tag == AngbandWindowMenuItemTagBase )
4472 /* The main window should always be available and visible */
4478 * Another window is only usable after Term_init_cocoa() has
4479 * been called for it. For Angband if window_flag[i] is nonzero
4480 * then that has happened for window i. For Hengband, that is
4481 * not the case so also test angband_term[i]->data.
4483 NSInteger subwindowNumber = tag - AngbandWindowMenuItemTagBase;
4484 return (angband_term[subwindowNumber]->data != 0
4485 && window_flag[subwindowNumber] > 0);
4491 if (sel == @selector(newGame:))
4493 return ! game_in_progress;
4495 else if (sel == @selector(editFont:))
4499 else if (sel == @selector(openGame:))
4501 return ! game_in_progress;
4503 else if (sel == @selector(setRefreshRate:) &&
4504 [[menuItem parentItem] tag] == 150)
4506 NSInteger fps = [[NSUserDefaults standardUserDefaults] integerForKey:AngbandFrameRateDefaultsKey];
4507 [menuItem setState: ([menuItem tag] == fps)];
4510 else if( sel == @selector(setGraphicsMode:) )
4512 NSInteger requestedGraphicsMode = [[NSUserDefaults standardUserDefaults] integerForKey:AngbandGraphicsDefaultsKey];
4513 [menuItem setState: (tag == requestedGraphicsMode)];
4516 else if( sel == @selector(toggleSound:) )
4518 BOOL is_on = [[NSUserDefaults standardUserDefaults]
4519 boolForKey:AngbandSoundDefaultsKey];
4521 [menuItem setState: ((is_on) ? NSOnState : NSOffState)];
4524 else if( sel == @selector(sendAngbandCommand:) ||
4525 sel == @selector(saveGame:) )
4528 * we only want to be able to send commands during an active game
4529 * after the birth screens
4531 return !!game_in_progress && character_generated;
4537 - (IBAction)setRefreshRate:(NSMenuItem *)menuItem
4539 frames_per_second = [menuItem tag];
4540 [[NSUserDefaults angbandDefaults] setInteger:frames_per_second forKey:AngbandFrameRateDefaultsKey];
4543 - (void)setGraphicsMode:(NSMenuItem *)sender
4545 /* We stashed the graphics mode ID in the menu item's tag */
4546 graf_mode_req = [sender tag];
4548 /* Stash it in UserDefaults */
4549 [[NSUserDefaults angbandDefaults] setInteger:graf_mode_req forKey:AngbandGraphicsDefaultsKey];
4550 [[NSUserDefaults angbandDefaults] synchronize];
4552 if (graf_mode_req == GRAPHICS_NONE ||
4553 get_graphics_mode(graf_mode_req) == GRAPHICS_NONE) {
4555 arg_bigtile = FALSE;
4557 } else if ([[NSUserDefaults angbandDefaults] boolForKey:AngbandBigTileDefaultsKey] == YES &&
4562 if (game_in_progress)
4564 if (arg_bigtile != use_bigtile) {
4565 Term_activate(angband_term[0]);
4566 Term_resize(angband_term[0]->wid, angband_term[0]->hgt);
4569 /* Hack -- Force redraw */
4572 /* Wake up the event loop so it notices the change */
4573 wakeup_event_loop();
4577 - (void)selectWindow: (id)sender
4579 NSInteger subwindowNumber =
4580 [(NSMenuItem *)sender tag] - AngbandWindowMenuItemTagBase;
4581 AngbandContext *context =
4582 (__bridge AngbandContext*) (angband_term[subwindowNumber]->data);
4583 [context.primaryWindow makeKeyAndOrderFront: self];
4584 [context saveWindowVisibleToDefaults: YES];
4587 - (IBAction) toggleSound: (NSMenuItem *) sender
4589 BOOL is_on = (sender.state == NSOnState);
4591 /* Toggle the state and update the Angband global and preferences. */
4593 sender.state = NSOffState;
4595 [AngbandSoundCatalog sharedSounds].enabled = NO;
4597 sender.state = NSOnState;
4599 [AngbandSoundCatalog sharedSounds].enabled = YES;
4601 [[NSUserDefaults angbandDefaults] setBool:(! is_on)
4602 forKey:AngbandSoundDefaultsKey];
4605 - (IBAction)toggleWideTiles:(NSMenuItem *) sender
4607 BOOL is_on = (sender.state == NSOnState);
4609 /* Toggle the state and update the Angband globals and preferences. */
4610 sender.state = (is_on) ? NSOffState : NSOnState;
4611 [[NSUserDefaults angbandDefaults] setBool:(! is_on)
4612 forKey:AngbandBigTileDefaultsKey];
4613 [[NSUserDefaults angbandDefaults] synchronize];
4614 if (graphics_are_enabled()) {
4615 arg_bigtile = (is_on) ? FALSE : TRUE;
4616 /* Mimics the logic in setGraphicsMode(). */
4617 if (game_in_progress && arg_bigtile != use_bigtile) {
4618 Term_activate(angband_term[0]);
4619 Term_resize(angband_term[0]->wid, angband_term[0]->hgt);
4621 wakeup_event_loop();
4626 - (void)prepareWindowsMenu
4630 * Get the window menu with default items and add a separator and
4631 * item for the main window.
4633 NSMenu *windowsMenu = [[NSApplication sharedApplication] windowsMenu];
4634 [windowsMenu addItem: [NSMenuItem separatorItem]];
4636 NSString *title1 = [NSString stringWithCString:angband_term_name[0]
4638 encoding:NSJapaneseEUCStringEncoding
4640 encoding:NSMacOSRomanStringEncoding
4643 NSMenuItem *angbandItem = [[NSMenuItem alloc] initWithTitle:title1 action: @selector(selectWindow:) keyEquivalent: @"0"];
4644 [angbandItem setTarget: self];
4645 [angbandItem setTag: AngbandWindowMenuItemTagBase];
4646 [windowsMenu addItem: angbandItem];
4648 /* Add items for the additional term windows */
4649 for( NSInteger i = 1; i < ANGBAND_TERM_MAX; i++ )
4651 NSString *title = [NSString stringWithCString:angband_term_name[i]
4653 encoding:NSJapaneseEUCStringEncoding
4655 encoding:NSMacOSRomanStringEncoding
4658 NSString *keyEquivalent =
4659 [NSString stringWithFormat: @"%ld", (long)i];
4660 NSMenuItem *windowItem =
4661 [[NSMenuItem alloc] initWithTitle: title
4662 action: @selector(selectWindow:)
4663 keyEquivalent: keyEquivalent];
4664 [windowItem setTarget: self];
4665 [windowItem setTag: AngbandWindowMenuItemTagBase + i];
4666 [windowsMenu addItem: windowItem];
4672 * Send a command to Angband via a menu item. This places the appropriate key
4673 * down events into the queue so that it seems like the user pressed them
4674 * (instead of trying to use the term directly).
4676 - (void)sendAngbandCommand: (id)sender
4678 NSMenuItem *menuItem = (NSMenuItem *)sender;
4679 NSString *command = [self.commandMenuTagMap objectForKey: [NSNumber numberWithInteger: [menuItem tag]]];
4680 AngbandContext* context =
4681 (__bridge AngbandContext*) (angband_term[0]->data);
4682 NSInteger windowNumber = [context.primaryWindow windowNumber];
4684 /* Send a \ to bypass keymaps */
4685 NSEvent *escape = [NSEvent keyEventWithType: NSKeyDown
4686 location: NSZeroPoint
4689 windowNumber: windowNumber
4692 charactersIgnoringModifiers: @"\\"
4695 [[NSApplication sharedApplication] postEvent: escape atStart: NO];
4697 /* Send the actual command (from the original command set) */
4698 NSEvent *keyDown = [NSEvent keyEventWithType: NSKeyDown
4699 location: NSZeroPoint
4702 windowNumber: windowNumber
4705 charactersIgnoringModifiers: command
4708 [[NSApplication sharedApplication] postEvent: keyDown atStart: NO];
4712 * Set up the command menu dynamically, based on CommandMenu.plist.
4714 - (void)prepareCommandMenu
4717 NSString *commandMenuPath =
4718 [[NSBundle mainBundle] pathForResource: @"CommandMenu"
4720 NSArray *commandMenuItems =
4721 [[NSArray alloc] initWithContentsOfFile: commandMenuPath];
4722 NSMutableDictionary *angbandCommands =
4723 [[NSMutableDictionary alloc] init];
4724 NSString *tblname = @"CommandMenu";
4725 NSInteger tagOffset = 0;
4727 for( NSDictionary *item in commandMenuItems )
4729 BOOL useShiftModifier =
4730 [[item valueForKey: @"ShiftModifier"] boolValue];
4731 BOOL useOptionModifier =
4732 [[item valueForKey: @"OptionModifier"] boolValue];
4733 NSUInteger keyModifiers = NSCommandKeyMask;
4734 keyModifiers |= (useShiftModifier) ? NSShiftKeyMask : 0;
4735 keyModifiers |= (useOptionModifier) ? NSAlternateKeyMask : 0;
4737 NSString *lookup = [item valueForKey: @"Title"];
4738 NSString *title = NSLocalizedStringWithDefaultValue(
4739 lookup, tblname, [NSBundle mainBundle], lookup, @"");
4740 NSString *key = [item valueForKey: @"KeyEquivalent"];
4741 NSMenuItem *menuItem =
4742 [[NSMenuItem alloc] initWithTitle: title
4743 action: @selector(sendAngbandCommand:)
4744 keyEquivalent: key];
4745 [menuItem setTarget: self];
4746 [menuItem setKeyEquivalentModifierMask: keyModifiers];
4747 [menuItem setTag: AngbandCommandMenuItemTagBase + tagOffset];
4748 [self.commandMenu addItem: menuItem];
4750 NSString *angbandCommand = [item valueForKey: @"AngbandCommand"];
4751 [angbandCommands setObject: angbandCommand
4752 forKey: [NSNumber numberWithInteger: [menuItem tag]]];
4756 self.commandMenuTagMap = [[NSDictionary alloc]
4757 initWithDictionary: angbandCommands];
4761 - (void)awakeFromNib
4763 [super awakeFromNib];
4765 [self prepareWindowsMenu];
4766 [self prepareCommandMenu];
4769 - (void)applicationDidFinishLaunching:sender
4773 /* Once beginGame finished, the game is over - that's how Angband works,
4774 * and we should quit */
4775 game_is_finished = TRUE;
4776 [NSApp terminate:self];
4779 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
4781 if (p_ptr->playing == FALSE || game_is_finished == TRUE)
4783 return NSTerminateNow;
4785 else if (! inkey_flag)
4787 /* For compatibility with other ports, do not quit in this case */
4788 return NSTerminateCancel;
4793 /* player->upkeep->playing = FALSE; */
4795 /* Post an escape event so that we can return from our get-key-event
4797 wakeup_event_loop();
4798 quit_when_ready = true;
4799 /* Must return Cancel, not Later, because we need to get out of the
4800 * run loop and back to Angband's loop */
4801 return NSTerminateCancel;
4806 * Dynamically build the Graphics menu
4808 - (void)menuNeedsUpdate:(NSMenu *)menu {
4810 /* Only the graphics menu is dynamic */
4811 if (! [menu isEqual:self.graphicsMenu])
4814 /* If it's non-empty, then we've already built it. Currently graphics modes
4815 * won't change once created; if they ever can we can remove this check.
4816 * Note that the check mark does change, but that's handled in
4817 * validateMenuItem: instead of menuNeedsUpdate: */
4818 if ([menu numberOfItems] > 0)
4821 /* This is the action for all these menu items */
4822 SEL action = @selector(setGraphicsMode:);
4824 /* Add an initial Classic ASCII menu item */
4825 NSString *tblname = @"GraphicsMenu";
4826 NSString *key = @"Classic ASCII";
4827 NSString *title = NSLocalizedStringWithDefaultValue(
4828 key, tblname, [NSBundle mainBundle], key, @"");
4829 NSMenuItem *classicItem = [menu addItemWithTitle:title action:action keyEquivalent:@""];
4830 [classicItem setTag:GRAPHICS_NONE];
4832 /* Walk through the list of graphics modes */
4833 if (graphics_modes) {
4836 for (i=0; graphics_modes[i].pNext; i++)
4838 const graphics_mode *graf = &graphics_modes[i];
4840 if (graf->grafID == GRAPHICS_NONE) {
4843 /* Make the title. NSMenuItem throws on a nil title, so ensure it's
4845 key = [[NSString alloc] initWithUTF8String:graf->menuname];
4846 title = NSLocalizedStringWithDefaultValue(
4847 key, tblname, [NSBundle mainBundle], key, @"");
4850 NSMenuItem *item = [menu addItemWithTitle:title action:action keyEquivalent:@""];
4851 [item setTag:graf->grafID];
4857 * Delegate method that gets called if we're asked to open a file.
4859 - (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
4861 /* Can't open a file once we've started */
4862 if (game_in_progress) {
4863 [[NSApplication sharedApplication]
4864 replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
4868 /* We can only open one file. Use the last one. */
4869 NSString *file = [filenames lastObject];
4871 [[NSApplication sharedApplication]
4872 replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
4876 /* Put it in savefile */
4877 if (! [file getFileSystemRepresentation:savefile maxLength:sizeof savefile]) {
4878 [[NSApplication sharedApplication]
4879 replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
4883 game_in_progress = TRUE;
4886 /* Wake us up in case this arrives while we're sitting at the Welcome
4888 wakeup_event_loop();
4890 [[NSApplication sharedApplication]
4891 replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
4896 int main(int argc, char* argv[])
4898 NSApplicationMain(argc, (void*)argv);
4902 #endif /* MACINTOSH || MACH_O_COCOA */