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 #import "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 = @VERSION_NAME;
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 /* Global defines etc from Angband 3.5-dev - NRM */
51 #define ANGBAND_TERM_MAX 8
53 #define MAX_COLORS 256
54 #define MSG_MAX SOUND_MAX
56 /* End Angband stuff - NRM */
58 /* Application defined event numbers */
61 AngbandEventWakeup = 1
64 /* Delay handling of pre-emptive "quit" event */
65 static BOOL quit_when_ready = FALSE;
67 /* Set to indicate the game is over and we can quit without delay */
68 static Boolean game_is_finished = FALSE;
70 /* Our frames per second (e.g. 60). A value of 0 means unthrottled. */
71 static int frames_per_second;
73 /* Force a new game or not? */
74 static bool new_game = FALSE;
79 static wchar_t convert_two_byte_eucjp_to_utf32_native(const char *cp);
83 * Load sound effects based on sound.cfg within the xtra/sound directory;
84 * bridge to Cocoa to use NSSound for simple loading and playback, avoiding
85 * I/O latency by caching all sounds at the start. Inherits full sound
86 * format support from Quicktime base/plugins.
87 * pelpel favoured a plist-based parser for the future but .cfg support
88 * improves cross-platform compatibility.
90 @interface AngbandSoundCatalog : NSObject {
93 * Stores instances of NSSound keyed by path so the same sound can be
94 * used for multiple events.
96 NSMutableDictionary *soundsByPath;
98 * Stores arrays of NSSound keyed by event number.
100 NSMutableDictionary *soundArraysByEvent;
104 * If NO, then playSound effectively becomes a do nothing operation.
106 @property (getter=isEnabled) BOOL enabled;
109 * Set up for lazy initialization in playSound(). Set enabled to NO.
114 * If self.enabled is YES and the given event has one or more sounds
115 * corresponding to it in the catalog, plays one of those sounds, chosen at
118 - (void)playSound:(int)event;
121 * Impose an arbitrary limit on the number of possible samples per event.
122 * Currently not declaring this as a class property for compatibility with
123 * versions of Xcode prior to 8.
128 * Return the shared sound catalog instance, creating it if it does not
129 * exist yet. Currently not declaring this as a class property for
130 * compatibility with versions of Xcode prior to 8.
132 + (AngbandSoundCatalog*)sharedSounds;
135 * Release any resources associated with shared sounds.
137 + (void)clearSharedSounds;
141 @implementation AngbandSoundCatalog
144 if (self = [super init]) {
145 self->soundsByPath = nil;
146 self->soundArraysByEvent = nil;
152 - (void)playSound:(int)event {
153 if (! self.enabled) {
157 /* Initialize when the first sound is played. */
158 if (self->soundArraysByEvent == nil) {
159 /* Build the "sound" path */
160 char sound_dir[1024];
161 path_build(sound_dir, sizeof(sound_dir), ANGBAND_DIR_XTRA, "sound");
163 /* Find and open the config file */
165 path_build(path, sizeof(path), sound_dir, "sound.cfg");
166 FILE *fff = my_fopen(path, "r");
170 NSLog(@"The sound configuration file could not be opened.");
174 self->soundsByPath = [[NSMutableDictionary alloc] init];
175 self->soundArraysByEvent = [[NSMutableDictionary alloc] init];
178 * This loop may take a while depending on the count and size of
183 /* Lines are always of the form "name = sample [sample ...]" */
185 while (my_fgets(fff, buffer, sizeof(buffer)) == 0) {
187 char *cfg_sample_list;
193 /* Skip anything not beginning with an alphabetic character */
194 if (!buffer[0] || !isalpha((unsigned char)buffer[0])) continue;
196 /* Split the line into two: message name, and the rest */
197 search = strchr(buffer, ' ');
198 cfg_sample_list = strchr(search + 1, ' ');
199 if (!search) continue;
200 if (!cfg_sample_list) continue;
202 /* Set the message name, and terminate at first space */
206 /* Make sure this is a valid event name */
207 for (event = MSG_MAX - 1; event >= 0; event--) {
208 if (strcmp(msg_name, angband_sound_name[event]) == 0)
211 if (event < 0) continue;
214 * Advance the sample list pointer so it's at the beginning of
218 if (!cfg_sample_list[0]) continue;
220 /* Terminate the current token */
221 cur_token = cfg_sample_list;
222 search = strchr(cur_token, ' ');
225 next_token = search + 1;
231 * Now we find all the sample names and add them one by one
234 NSMutableArray *soundSamples =
235 [self->soundArraysByEvent
236 objectForKey:[NSNumber numberWithInteger:event]];
237 if (soundSamples == nil) {
238 soundSamples = [[NSMutableArray alloc] init];
239 [self->soundArraysByEvent
240 setObject:soundSamples
241 forKey:[NSNumber numberWithInteger:event]];
243 int num = (int) soundSamples.count;
245 /* Don't allow too many samples */
246 if (num >= [AngbandSoundCatalog maxSamples]) break;
248 NSString *token_string =
249 [NSString stringWithUTF8String:cur_token];
251 [self->soundsByPath objectForKey:token_string];
255 * We have to load the sound. Build the path to the
258 path_build(path, sizeof(path), sound_dir, cur_token);
260 if (stat(path, &stb) == 0) {
261 /* Load the sound into memory */
262 sound = [[NSSound alloc]
263 initWithContentsOfFile:[NSString stringWithUTF8String:path]
266 [self->soundsByPath setObject:sound
267 forKey:token_string];
272 /* Store it if we loaded it */
274 [soundSamples addObject:sound];
277 /* Figure out next token */
278 cur_token = next_token;
280 /* Try to find a space */
281 search = strchr(cur_token, ' ');
284 * If we can find one, terminate, and set new "next".
288 next_token = search + 1;
290 /* Otherwise prevent infinite looping */
303 NSMutableArray *samples =
304 [self->soundArraysByEvent
305 objectForKey:[NSNumber numberWithInteger:event]];
307 if (samples == nil || samples.count == 0) {
311 /* Choose a random event. */
312 int s = randint0((int) samples.count);
313 NSSound *sound = samples[s];
315 if ([sound isPlaying])
318 /* Play the sound. */
328 * For sharedSounds and clearSharedSounds.
330 static __strong AngbandSoundCatalog* gSharedSounds = nil;
332 + (AngbandSoundCatalog*)sharedSounds {
333 if (gSharedSounds == nil) {
334 gSharedSounds = [[AngbandSoundCatalog alloc] init];
336 return gSharedSounds;
339 + (void)clearSharedSounds {
346 * Each location in the terminal either stores a character, a tile,
347 * padding for a big tile, or padding for a big character (for example a
348 * kanji that takes two columns). These structures represent that. Note
349 * that tiles do not overlap with each other (excepting the double-height
350 * tiles, i.e. from the Shockbolt set; that's handled as a special case).
351 * Characters can overlap horizontally: that is for handling fonts that
352 * aren't fixed width.
354 struct TerminalCellChar {
358 struct TerminalCellTile {
360 * These are the coordinates, within the tile set, for the foreground
361 * tile and background tile.
363 char fgdCol, fgdRow, bckCol, bckRow;
365 struct TerminalCellPadding {
367 * If the cell at (x, y) is padding, the cell at (x - hoff, y - voff)
368 * has the attributes affecting the padded region.
370 unsigned char hoff, voff;
372 struct TerminalCell {
374 struct TerminalCellChar ch;
375 struct TerminalCellTile ti;
376 struct TerminalCellPadding pd;
379 * Used for big characters or tiles which are hscl x vscl cells.
380 * The upper left corner of the big tile or character is marked as
381 * TERM_CELL_TILE or TERM_CELL_CHAR. The remainder are marked as
382 * TERM_CELL_TILE_PADDING or TERM_CELL_CHAR_PADDING and have hscl and
383 * vscl set to matcn what's in the upper left corner. Big tiles are
384 * tiles scaled up to occupy more space. Big characters, on the other
385 * hand, are characters that naturally take up more space than standard
386 * for the font with the assumption that vscl will be one for any big
387 * character and hscl will hold the number of columns it occupies (likely
388 * just 2, i.e. for Japanese kanji).
393 * Hold the offsets, as fractions of the tile size expressed as the
394 * rational numbers hoff_n / hoff_d and voff_n / voff_d, within the tile
395 * or character. For something that is not a big tile or character, these
396 * will be 0, 0, 1, and 1. For a big tile or character, these will be
397 * set when the tile or character is changed to be 0, 0, hscl, and vscl
398 * for the upper left corner and i, j, hscl, vscl for the padding element
399 * at (i, j) relative to the upper left corner. For a big tile or
400 * character that is partially overwritten, these are not modified in the
401 * parts that are not overwritten while hscl, vscl, and, for padding,
402 * v.pd.hoff and v.pd.voff are.
404 unsigned char hoff_n;
405 unsigned char voff_n;
406 unsigned char hoff_d;
407 unsigned char voff_d;
409 * Is either TERM_CELL_CHAR, TERM_CELL_CHAR_PADDING, TERM_CELL_TILE, or
410 * TERM_CELL_TILE_PADDING.
414 #define TERM_CELL_CHAR (0x1)
415 #define TERM_CELL_CHAR_PADDING (0x2)
416 #define TERM_CELL_TILE (0x4)
417 #define TERM_CELL_TILE_PADDING (0x8)
419 struct TerminalCellBlock {
420 int ulcol, ulrow, w, h;
423 struct TerminalCellLocation {
427 typedef int (*TerminalCellPredicate)(const struct TerminalCell*);
429 static int isTileTop(const struct TerminalCell *c)
431 return (c->form == TERM_CELL_TILE ||
432 (c->form == TERM_CELL_TILE_PADDING && c->v.pd.voff == 0)) ? 1 : 0;
435 static int isPartiallyOverwrittenBigChar(const struct TerminalCell *c)
437 if ((c->form & (TERM_CELL_CHAR | TERM_CELL_CHAR_PADDING)) != 0) {
439 * When the tile is set in Term_pict_cocoa, hoff_d is the same as hscl
440 * and voff_d is the same as vscl. hoff_d and voff_d aren't modified
441 * after that, but hscl and vscl are in response to partial overwrites.
442 * If they're diffent, an overwrite has occurred.
444 return ((c->hoff_d > 1 || c->voff_d > 1) &&
445 (c->hoff_d != c->hscl || c->voff_d != c->vscl)) ? 1 : 0;
450 static int isCharNoPartial(const struct TerminalCell *c)
452 return ((c->form & (TERM_CELL_CHAR | TERM_CELL_CHAR_PADDING)) != 0 &&
453 ! isPartiallyOverwrittenBigChar(c)) ? 1 : 0;
457 * Since the drawing is decoupled from Angband's calls to the text_hook,
458 * pict_hook, wipe_hook, curs_hook, and bigcurs_hook callbacks of a terminal,
459 * maintain a version of the Terminal contents.
461 @interface TerminalContents : NSObject {
463 struct TerminalCell *cells;
467 * Initialize with zero columns and zero rows.
472 * Initialize with nCol columns and nRow rows. All elements will be set to
475 - (id)initWithColumns:(int)nCol rows:(int)nRow NS_DESIGNATED_INITIALIZER;
478 * Resize to be nCol by nRow. Current contents still within the new bounds
479 * are preserved. Added areas are filled with blanks.
481 - (void)resizeWithColumns:(int)nCol rows:(int)nRow;
484 * Get the contents of a given cell.
486 - (const struct TerminalCell*)getCellAtColumn:(int)icol row:(int)irow;
489 * Scans the row, irow, starting at the column, icol0, and stopping before the
490 * column, icol1. Returns the column index for the first cell that's within
491 * the given type mask, tm. If all of the cells in that range are not within
492 * the given type mask, returns icol1.
494 - (int)scanForTypeMaskInRow:(int)irow mask:(unsigned int)tm col0:(int)icol0
498 * Scans the w x h block whose upper left corner is at (icol, irow). The
499 * scan starts at (icol + pcurs->col, irow + pcurs->row) and proceeds from
500 * left to right and top to bottom. At exit, pcurs will have the location
501 * (relative to icol, irow) of the first cell encountered that's within the
502 * given type mask, tm. If no such cell was found, pcurs->col will be w
503 * and pcurs->row will be h.
505 - (void)scanForTypeMaskInBlockAtColumn:(int)icol row:(int)irow width:(int)w
506 height:(int)h mask:(unsigned int)tm
507 cursor:(struct TerminalCellLocation*)pcurs;
510 * Scans the row, irow, starting at the column, icol0, and stopping before the
511 * column, icol1. Returns the column index for the first cell that
512 * func(cell_address) != rval. If all of the cells in the range satisfy the
513 * predicate, returns icol1.
515 - (int)scanForPredicateInRow:(int)irow
516 predicate:(TerminalCellPredicate)func
522 * Change the contents to have the given string of n characters appear with
523 * the leftmost character at (icol, irow).
525 - (void)setUniformAttributeTextRunAtColumn:(int)icol
528 glyphs:(const char*)g
532 * Change the contents to have a tile scaled to w x h appear with its upper
533 * left corner at (icol, irow).
535 - (void)setTileAtColumn:(int)icol
537 foregroundColumn:(char)fgdCol
538 foregroundRow:(char)fgdRow
539 backgroundColumn:(char)bckCol
540 backgroundRow:(char)bckRow
545 * Wipe the w x h block whose upper left corner is at (icol, irow).
547 - (void)wipeBlockAtColumn:(int)icol row:(int)irow width:(int)w height:(int)h;
550 * Wipe all the contents.
560 * Thie is a helper function for wipeBlockAtColumn.
562 - (void)wipeBlockAuxAtColumn:(int)icol row:(int)irow width:(int)w
566 * This is a helper function for checkForBigStuffOverwriteAtColumn.
568 - (void) splitBlockAtColumn:(int)icol row:(int)irow n:(int)nsub
569 blocks:(const struct TerminalCellBlock*)b;
572 * This is a helper function for setUniformAttributeTextRunAtColumn,
573 * setTileAtColumn, and wipeBlockAtColumn. If a modification could partially
574 * overwrite a big character or tile, make adjustments so what's left can
575 * be handled appropriately in rendering.
577 - (void)checkForBigStuffOverwriteAtColumn:(int)icol row:(int)irow
578 width:(int)w height:(int)h;
581 * Position the upper left corner of the cursor at (icol, irow) and have it
582 * encompass w x h cells.
584 - (void)setCursorAtColumn:(int)icol row:(int)irow width:(int)w height:(int)h;
587 * Remove the cursor. cursorColumn and cursorRow will be -1 until
588 * setCursorAtColumn is called.
590 - (void)removeCursor;
593 * Verify that everying is consistent.
595 - (void)assertInvariants;
598 * Is the number of columns.
600 @property (readonly) int columnCount;
603 * Is the number of rows.
605 @property (readonly) int rowCount;
608 * Is the column index for the upper left corner of the cursor. It will be -1
609 * if the cursor is disabled.
611 @property (readonly) int cursorColumn;
614 * Is the row index for the upper left corner of the cursor. It will be -1
615 * if the cursor is disabled.
617 @property (readonly) int cursorRow;
620 * Is the cursor width in number of cells.
622 @property (readonly) int cursorWidth;
625 * Is the cursor height in number of cells.
627 @property (readonly) int cursorHeight;
630 * Return the character to be used for blanks.
632 + (wchar_t)getBlankChar;
635 * Return the attribute to be used for blanks.
637 + (int)getBlankAttribute;
641 @implementation TerminalContents
645 return [self initWithColumns:0 rows:0];
648 - (id)initWithColumns:(int)nCol rows:(int)nRow
650 if (self = [super init]) {
651 self->cells = malloc(nCol * nRow * sizeof(struct TerminalCell));
652 self->_columnCount = nCol;
653 self->_rowCount = nRow;
654 self->_cursorColumn = -1;
655 self->_cursorRow = -1;
656 self->_cursorWidth = 1;
657 self->_cursorHeight = 1;
665 if (self->cells != 0) {
671 - (void)resizeWithColumns:(int)nCol rows:(int)nRow
674 * Potential issue: big tiles or characters can become clipped by the
675 * resize. That will only matter if drawing occurs before the contents
676 * are updated by Angband. Even then, unless the drawing mode is used
677 * where AppKit doesn't clip to the window bounds, the only artifact will
678 * be clipping when drawn which is acceptable and doesn't require
679 * additional logic to either filter out the clipped big stuff here or
680 * to just clear it when drawing.
682 struct TerminalCell *newCells =
683 malloc(nCol * nRow * sizeof(struct TerminalCell));
684 struct TerminalCell *cellsOutCursor = newCells;
685 const struct TerminalCell *cellsInCursor = self->cells;
686 int nColCommon = (nCol < self.columnCount) ? nCol : self.columnCount;
687 int nRowCommon = (nRow < self.rowCount) ? nRow : self.rowCount;
688 wchar_t blank = [TerminalContents getBlankChar];
689 int blank_attr = [TerminalContents getBlankAttribute];
692 for (i = 0; i < nRowCommon; ++i) {
696 nColCommon * sizeof(struct TerminalCell));
697 cellsInCursor += self.columnCount;
698 for (int j = nColCommon; j < nCol; ++j) {
699 cellsOutCursor[j].v.ch.glyph = blank;
700 cellsOutCursor[j].v.ch.attr = blank_attr;
701 cellsOutCursor[j].hscl = 1;
702 cellsOutCursor[j].vscl = 1;
703 cellsOutCursor[j].hoff_n = 0;
704 cellsOutCursor[j].voff_n = 0;
705 cellsOutCursor[j].hoff_d = 1;
706 cellsOutCursor[j].voff_d = 1;
707 cellsOutCursor[j].form = TERM_CELL_CHAR;
709 cellsOutCursor += nCol;
711 while (cellsOutCursor != newCells + nCol * nRow) {
712 cellsOutCursor->v.ch.glyph = blank;
713 cellsOutCursor->v.ch.attr = blank_attr;
714 cellsOutCursor->hscl = 1;
715 cellsOutCursor->vscl = 1;
716 cellsOutCursor->hoff_n = 0;
717 cellsOutCursor->voff_n = 0;
718 cellsOutCursor->hoff_d = 1;
719 cellsOutCursor->voff_d = 1;
720 cellsOutCursor->form = TERM_CELL_CHAR;
725 self->cells = newCells;
726 self->_columnCount = nCol;
727 self->_rowCount = nRow;
728 if (self->_cursorColumn >= nCol || self->_cursorRow >= nRow) {
729 self->_cursorColumn = -1;
730 self->_cursorRow = -1;
732 if (self->_cursorColumn + self->_cursorWidth > nCol) {
733 self->_cursorWidth = nCol - self->_cursorColumn;
735 if (self->_cursorRow + self->_cursorHeight > nRow) {
736 self->_cursorHeight = nRow - self->_cursorRow;
741 - (const struct TerminalCell*)getCellAtColumn:(int)icol row:(int)irow
743 return self->cells + icol + irow * self.columnCount;
746 - (int)scanForTypeMaskInRow:(int)irow mask:(unsigned int)tm col0:(int)icol0
750 const struct TerminalCell *cellsRow =
751 self->cells + irow * self.columnCount;
757 if ((cellsRow[i].form & tm) != 0) {
764 - (void)scanForTypeMaskInBlockAtColumn:(int)icol row:(int)irow width:(int)w
765 height:(int)h mask:(unsigned int)tm
766 cursor:(struct TerminalCellLocation*)pcurs
768 const struct TerminalCell *cellsRow =
769 self->cells + (irow + pcurs->row) * self.columnCount;
771 if (pcurs->col == w) {
772 if (pcurs->row >= h - 1) {
778 cellsRow += self.columnCount;
781 if ((cellsRow[icol + pcurs->col].form & tm) != 0) {
789 - (int)scanForPredicateInRow:(int)irow
790 predicate:(TerminalCellPredicate)func
796 const struct TerminalCell *cellsRow =
797 self->cells + irow * self.columnCount;
803 if (func(cellsRow + i) != rval) {
810 - (void)setUniformAttributeTextRunAtColumn:(int)icol
813 glyphs:(const char*)g
816 [self checkForBigStuffOverwriteAtColumn:icol row:irow width:n height:1];
818 struct TerminalCell *cellsRow = self->cells + irow * self.columnCount;
821 while (i < icol + n) {
826 * The second byte of the character is past the end. Ignore
831 cellsRow[i].v.ch.glyph = convert_two_byte_eucjp_to_utf32_native(g);
832 cellsRow[i].v.ch.attr = a;
833 cellsRow[i].hscl = 2;
834 cellsRow[i].vscl = 1;
835 cellsRow[i].hoff_n = 0;
836 cellsRow[i].voff_n = 0;
837 cellsRow[i].hoff_d = 2;
838 cellsRow[i].voff_d = 1;
839 cellsRow[i].form = TERM_CELL_CHAR;
841 cellsRow[i].v.pd.hoff = 1;
842 cellsRow[i].v.pd.voff = 0;
843 cellsRow[i].hscl = 2;
844 cellsRow[i].vscl = 1;
845 cellsRow[i].hoff_n = 1;
846 cellsRow[i].voff_n = 0;
847 cellsRow[i].hoff_d = 2;
848 cellsRow[i].voff_d = 1;
849 cellsRow[i].form = TERM_CELL_CHAR_PADDING;
853 cellsRow[i].v.ch.glyph = *g++;
854 cellsRow[i].v.ch.attr = a;
855 cellsRow[i].hscl = 1;
856 cellsRow[i].vscl = 1;
857 cellsRow[i].hoff_n = 0;
858 cellsRow[i].voff_n = 0;
859 cellsRow[i].hoff_d = 1;
860 cellsRow[i].voff_d = 1;
861 cellsRow[i].form = TERM_CELL_CHAR;
865 cellsRow[i].v.ch.glyph = *g++;
866 cellsRow[i].v.ch.attr = a;
867 cellsRow[i].hscl = 1;
868 cellsRow[i].vscl = 1;
869 cellsRow[i].hoff_n = 0;
870 cellsRow[i].voff_n = 0;
871 cellsRow[i].hoff_d = 1;
872 cellsRow[i].voff_d = 1;
873 cellsRow[i].form = TERM_CELL_CHAR;
879 - (void)setTileAtColumn:(int)icol
881 foregroundColumn:(char)fgdCol
882 foregroundRow:(char)fgdRow
883 backgroundColumn:(char)bckCol
884 backgroundRow:(char)bckRow
888 [self checkForBigStuffOverwriteAtColumn:icol row:irow width:w height:h];
890 struct TerminalCell *cellsRow = self->cells + irow * self.columnCount;
892 cellsRow[icol].v.ti.fgdCol = fgdCol;
893 cellsRow[icol].v.ti.fgdRow = fgdRow;
894 cellsRow[icol].v.ti.bckCol = bckCol;
895 cellsRow[icol].v.ti.bckRow = bckRow;
896 cellsRow[icol].hscl = w;
897 cellsRow[icol].vscl = h;
898 cellsRow[icol].hoff_n = 0;
899 cellsRow[icol].voff_n = 0;
900 cellsRow[icol].hoff_d = w;
901 cellsRow[icol].voff_d = h;
902 cellsRow[icol].form = TERM_CELL_TILE;
905 for (ic = icol + 1; ic < icol + w; ++ic) {
906 cellsRow[ic].v.pd.hoff = ic - icol;
907 cellsRow[ic].v.pd.voff = 0;
908 cellsRow[ic].hscl = w;
909 cellsRow[ic].vscl = h;
910 cellsRow[ic].hoff_n = ic - icol;
911 cellsRow[ic].voff_n = 0;
912 cellsRow[ic].hoff_d = w;
913 cellsRow[ic].voff_d = h;
914 cellsRow[ic].form = TERM_CELL_TILE_PADDING;
916 cellsRow += self.columnCount;
917 for (int ir = irow + 1; ir < irow + h; ++ir) {
918 for (ic = icol; ic < icol + w; ++ic) {
919 cellsRow[ic].v.pd.hoff = ic - icol;
920 cellsRow[ic].v.pd.voff = ir - irow;
921 cellsRow[ic].hscl = w;
922 cellsRow[ic].vscl = h;
923 cellsRow[ic].hoff_n = ic - icol;
924 cellsRow[ic].voff_n = ir - irow;
925 cellsRow[ic].hoff_d = w;
926 cellsRow[ic].voff_d = h;
927 cellsRow[ic].form = TERM_CELL_TILE_PADDING;
929 cellsRow += self.columnCount;
933 - (void)wipeBlockAtColumn:(int)icol row:(int)irow width:(int)w height:(int)h
935 [self checkForBigStuffOverwriteAtColumn:icol row:irow width:w height:h];
936 [self wipeBlockAuxAtColumn:icol row:irow width:w height:h];
941 wchar_t blank = [TerminalContents getBlankChar];
942 int blank_attr = [TerminalContents getBlankAttribute];
943 struct TerminalCell *cellCursor = self->cells +
944 self.columnCount * self.rowCount;
946 while (cellCursor != self->cells) {
948 cellCursor->v.ch.glyph = blank;
949 cellCursor->v.ch.attr = blank_attr;
950 cellCursor->hscl = 1;
951 cellCursor->vscl = 1;
952 cellCursor->hoff_n = 0;
953 cellCursor->voff_n = 0;
954 cellCursor->hoff_d = 1;
955 cellCursor->voff_d = 1;
956 cellCursor->form = TERM_CELL_CHAR;
962 wchar_t blank = [TerminalContents getBlankChar];
963 int blank_attr = [TerminalContents getBlankAttribute];
964 struct TerminalCell *cellCursor = self->cells +
965 self.columnCount * self.rowCount;
967 while (cellCursor != self->cells) {
969 if ((cellCursor->form &
970 (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
971 cellCursor->v.ch.glyph = blank;
972 cellCursor->v.ch.attr = blank_attr;
973 cellCursor->hscl = 1;
974 cellCursor->vscl = 1;
975 cellCursor->hoff_n = 0;
976 cellCursor->voff_n = 0;
977 cellCursor->hoff_d = 1;
978 cellCursor->voff_d = 1;
979 cellCursor->form = TERM_CELL_CHAR;
984 - (void)wipeBlockAuxAtColumn:(int)icol row:(int)irow width:(int)w
987 struct TerminalCell *cellsRow = self->cells + irow * self.columnCount;
988 wchar_t blank = [TerminalContents getBlankChar];
989 int blank_attr = [TerminalContents getBlankAttribute];
991 for (int ir = irow; ir < irow + h; ++ir) {
992 for (int ic = icol; ic < icol + w; ++ic) {
993 cellsRow[ic].v.ch.glyph = blank;
994 cellsRow[ic].v.ch.attr = blank_attr;
995 cellsRow[ic].hscl = 1;
996 cellsRow[ic].vscl = 1;
997 cellsRow[ic].hoff_n = 0;
998 cellsRow[ic].voff_n = 0;
999 cellsRow[ic].hoff_d = 1;
1000 cellsRow[ic].voff_d = 1;
1001 cellsRow[ic].form = TERM_CELL_CHAR;
1003 cellsRow += self.columnCount;
1007 - (void) splitBlockAtColumn:(int)icol row:(int)irow n:(int)nsub
1008 blocks:(const struct TerminalCellBlock*)b
1010 const struct TerminalCell *pulold = [self getCellAtColumn:icol row:irow];
1012 for (int isub = 0; isub < nsub; ++isub) {
1013 struct TerminalCell* cellsRow =
1014 self->cells + b[isub].ulrow * self.columnCount;
1017 * Copy the data from the upper left corner of the big block to
1018 * the upper left corner of the piece.
1020 if (b[isub].ulcol != icol || b[isub].ulrow != irow) {
1021 if (pulold->form == TERM_CELL_CHAR) {
1022 cellsRow[b[isub].ulcol].v.ch = pulold->v.ch;
1023 cellsRow[b[isub].ulcol].form = TERM_CELL_CHAR;
1025 cellsRow[b[isub].ulcol].v.ti = pulold->v.ti;
1026 cellsRow[b[isub].ulcol].form = TERM_CELL_TILE;
1029 cellsRow[b[isub].ulcol].hscl = b[isub].w;
1030 cellsRow[b[isub].ulcol].vscl = b[isub].h;
1033 * Point the padding elements in the piece to the new upper left
1037 for (ic = b[isub].ulcol + 1; ic < b[isub].ulcol + b[isub].w; ++ic) {
1038 cellsRow[ic].v.pd.hoff = ic - b[isub].ulcol;
1039 cellsRow[ic].v.pd.voff = 0;
1040 cellsRow[ic].hscl = b[isub].w;
1041 cellsRow[ic].vscl = b[isub].h;
1043 cellsRow += self.columnCount;
1044 for (int ir = b[isub].ulrow + 1;
1045 ir < b[isub].ulrow + b[isub].h;
1047 for (ic = b[isub].ulcol; ic < b[isub].ulcol + b[isub].w; ++ic) {
1048 cellsRow[ic].v.pd.hoff = ic - b[isub].ulcol;
1049 cellsRow[ic].v.pd.voff = ir - b[isub].ulrow;
1050 cellsRow[ic].hscl = b[isub].w;
1051 cellsRow[ic].vscl = b[isub].h;
1053 cellsRow += self.columnCount;
1058 - (void)checkForBigStuffOverwriteAtColumn:(int)icol row:(int)irow
1059 width:(int)w height:(int)h
1061 int ire = irow + h, ice = icol + w;
1063 for (int ir = irow; ir < ire; ++ir) {
1064 for (int ic = icol; ic < ice; ++ic) {
1065 const struct TerminalCell *pcell =
1066 [self getCellAtColumn:ic row:ir];
1068 if ((pcell->form & (TERM_CELL_CHAR | TERM_CELL_TILE)) != 0 &&
1069 (pcell->hscl > 1 || pcell->vscl > 1)) {
1071 * Lost chunk including upper left corner. Split into at most
1075 * Tolerate blocks that were clipped by a resize at some point.
1077 int wb = (ic + pcell->hscl <= self.columnCount) ?
1078 pcell->hscl : self.columnCount - ic;
1079 int hb = (ir + pcell->vscl <= self.rowCount) ?
1080 pcell->vscl : self.rowCount - ir;
1081 struct TerminalCellBlock blocks[2];
1082 int nsub = 0, ww, hw;
1084 if (ice < ic + wb) {
1085 /* Have something to the right not overwritten. */
1086 blocks[nsub].ulcol = ice;
1087 blocks[nsub].ulrow = ir;
1088 blocks[nsub].w = ic + wb - ice;
1089 blocks[nsub].h = (ire < ir + hb) ? ire - ir : hb;
1095 if (ire < ir + hb) {
1096 /* Have something below not overwritten. */
1097 blocks[nsub].ulcol = ic;
1098 blocks[nsub].ulrow = ire;
1099 blocks[nsub].w = wb;
1100 blocks[nsub].h = ir + hb - ire;
1107 [self splitBlockAtColumn:ic row:ir n:nsub blocks:blocks];
1110 * Wipe the part of the block that's destined to be overwritten
1111 * so it doesn't receive further consideration in this loop.
1112 * For efficiency, would like to have the loop skip over it or
1113 * fill it with the desired content, but this is easier to
1116 [self wipeBlockAuxAtColumn:ic row:ir width:ww height:hw];
1117 } else if ((pcell->form & (TERM_CELL_CHAR_PADDING |
1118 TERM_CELL_TILE_PADDING)) != 0) {
1120 * Lost a chunk that doesn't cover the upper left corner. In
1121 * general will split into up to four new blocks (one above,
1122 * one to the left, one to the right, and one below).
1124 int pcol = ic - pcell->v.pd.hoff;
1125 int prow = ir - pcell->v.pd.voff;
1126 const struct TerminalCell *pcell2 =
1127 [self getCellAtColumn:pcol row:prow];
1130 * Tolerate blocks that were clipped by a resize at some point.
1132 int wb = (pcol + pcell2->hscl <= self.columnCount) ?
1133 pcell2->hscl : self.columnCount - pcol;
1134 int hb = (prow + pcell2->vscl <= self.rowCount) ?
1135 pcell2->vscl : self.rowCount - prow;
1136 struct TerminalCellBlock blocks[4];
1137 int nsub = 0, ww, hw;
1140 /* Have something above not overwritten. */
1141 blocks[nsub].ulcol = pcol;
1142 blocks[nsub].ulrow = prow;
1143 blocks[nsub].w = wb;
1144 blocks[nsub].h = ir - prow;
1148 /* Have something to the left not overwritten. */
1149 blocks[nsub].ulcol = pcol;
1150 blocks[nsub].ulrow = ir;
1151 blocks[nsub].w = ic - pcol;
1153 (ire < prow + hb) ? ire - ir : prow + hb - ir;
1156 if (ice < pcol + wb) {
1157 /* Have something to the right not overwritten. */
1158 blocks[nsub].ulcol = ice;
1159 blocks[nsub].ulrow = ir;
1160 blocks[nsub].w = pcol + wb - ice;
1162 (ire < prow + hb) ? ire - ir : prow + hb - ir;
1166 ww = pcol + wb - ic;
1168 if (ire < prow + hb) {
1169 /* Have something below not overwritten. */
1170 blocks[nsub].ulcol = pcol;
1171 blocks[nsub].ulrow = ire;
1172 blocks[nsub].w = wb;
1173 blocks[nsub].h = prow + hb - ire;
1177 hw = prow + hb - ir;
1180 [self splitBlockAtColumn:pcol row:prow n:nsub blocks:blocks];
1181 /* Same rationale for wiping as above. */
1182 [self wipeBlockAuxAtColumn:ic row:ir width:ww height:hw];
1188 - (void)setCursorAtColumn:(int)icol row:(int)irow width:(int)w height:(int)h
1190 self->_cursorColumn = icol;
1191 self->_cursorRow = irow;
1192 self->_cursorWidth = w;
1193 self->_cursorHeight = h;
1196 - (void)removeCursor
1198 self->_cursorColumn = -1;
1199 self->_cursorHeight = -1;
1200 self->_cursorWidth = 1;
1201 self->_cursorHeight = 1;
1204 - (void)assertInvariants
1206 const struct TerminalCell *cellsRow = self->cells;
1209 * The comments with the definition for TerminalCell define the
1210 * relationships of hoff_n, voff_n, hoff_d, voff_d, hscl, and vscl
1213 for (int ir = 0; ir < self.rowCount; ++ir) {
1214 for (int ic = 0; ic < self.columnCount; ++ic) {
1215 switch (cellsRow[ic].form) {
1216 case TERM_CELL_CHAR:
1217 assert(cellsRow[ic].hscl > 0 && cellsRow[ic].vscl > 0);
1218 assert(cellsRow[ic].hoff_n < cellsRow[ic].hoff_d &&
1219 cellsRow[ic].voff_n < cellsRow[ic].voff_d);
1220 if (cellsRow[ic].hscl == cellsRow[ic].hoff_d) {
1221 assert(cellsRow[ic].hoff_n == 0);
1223 if (cellsRow[ic].vscl == cellsRow[ic].voff_d) {
1224 assert(cellsRow[ic].voff_n == 0);
1227 * Verify that the padding elements have the correct tag
1228 * and point back to this cell.
1230 if (cellsRow[ic].hscl > 1 || cellsRow[ic].vscl > 1) {
1231 const struct TerminalCell *cellsRow2 = cellsRow;
1233 for (int ir2 = ir; ir2 < ir + cellsRow[ic].vscl; ++ir2) {
1235 ic2 < ic + cellsRow[ic].hscl;
1237 if (ir2 == ir && ic2 == ic) {
1240 assert(cellsRow2[ic2].form ==
1241 TERM_CELL_CHAR_PADDING);
1242 assert(ic2 - cellsRow2[ic2].v.pd.hoff == ic &&
1243 ir2 - cellsRow2[ic2].v.pd.voff == ir);
1245 cellsRow2 += self.columnCount;
1250 case TERM_CELL_TILE:
1251 assert(cellsRow[ic].hscl > 0 && cellsRow[ic].vscl > 0);
1252 assert(cellsRow[ic].hoff_n < cellsRow[ic].hoff_d &&
1253 cellsRow[ic].voff_n < cellsRow[ic].voff_d);
1254 if (cellsRow[ic].hscl == cellsRow[ic].hoff_d) {
1255 assert(cellsRow[ic].hoff_n == 0);
1257 if (cellsRow[ic].vscl == cellsRow[ic].voff_d) {
1258 assert(cellsRow[ic].voff_n == 0);
1261 * Verify that the padding elements have the correct tag
1262 * and point back to this cell.
1264 if (cellsRow[ic].hscl > 1 || cellsRow[ic].vscl > 1) {
1265 const struct TerminalCell *cellsRow2 = cellsRow;
1267 for (int ir2 = ir; ir2 < ir + cellsRow[ic].vscl; ++ir2) {
1269 ic2 < ic + cellsRow[ic].hscl;
1271 if (ir2 == ir && ic2 == ic) {
1274 assert(cellsRow2[ic2].form ==
1275 TERM_CELL_TILE_PADDING);
1276 assert(ic2 - cellsRow2[ic2].v.pd.hoff == ic &&
1277 ir2 - cellsRow2[ic2].v.pd.voff == ir);
1279 cellsRow2 += self.columnCount;
1284 case TERM_CELL_CHAR_PADDING:
1285 assert(cellsRow[ic].hscl > 0 && cellsRow[ic].vscl > 0);
1286 assert(cellsRow[ic].hoff_n < cellsRow[ic].hoff_d &&
1287 cellsRow[ic].voff_n < cellsRow[ic].voff_d);
1288 assert(cellsRow[ic].hoff_n > 0 || cellsRow[ic].voff_n > 0);
1289 if (cellsRow[ic].hscl == cellsRow[ic].hoff_d) {
1290 assert(cellsRow[ic].hoff_n == cellsRow[ic].v.pd.hoff);
1292 if (cellsRow[ic].vscl == cellsRow[ic].voff_d) {
1293 assert(cellsRow[ic].voff_n == cellsRow[ic].v.pd.voff);
1295 assert(ic >= cellsRow[ic].v.pd.hoff &&
1296 ir >= cellsRow[ic].v.pd.voff);
1298 * Verify that it's padding for something that can point
1302 const struct TerminalCell *parent =
1303 [self getCellAtColumn:(ic - cellsRow[ic].v.pd.hoff)
1304 row:(ir - cellsRow[ic].v.pd.voff)];
1306 assert(parent->form == TERM_CELL_CHAR);
1307 assert(parent->hscl > cellsRow[ic].v.pd.hoff &&
1308 parent->vscl > cellsRow[ic].v.pd.voff);
1309 assert(parent->hscl == cellsRow[ic].hscl &&
1310 parent->vscl == cellsRow[ic].vscl);
1311 assert(parent->hoff_d == cellsRow[ic].hoff_d &&
1312 parent->voff_d == cellsRow[ic].voff_d);
1316 case TERM_CELL_TILE_PADDING:
1317 assert(cellsRow[ic].hscl > 0 && cellsRow[ic].vscl > 0);
1318 assert(cellsRow[ic].hoff_n < cellsRow[ic].hoff_d &&
1319 cellsRow[ic].voff_n < cellsRow[ic].voff_d);
1320 assert(cellsRow[ic].hoff_n > 0 || cellsRow[ic].voff_n > 0);
1321 if (cellsRow[ic].hscl == cellsRow[ic].hoff_d) {
1322 assert(cellsRow[ic].hoff_n == cellsRow[ic].v.pd.hoff);
1324 if (cellsRow[ic].vscl == cellsRow[ic].voff_d) {
1325 assert(cellsRow[ic].voff_n == cellsRow[ic].v.pd.voff);
1327 assert(ic >= cellsRow[ic].v.pd.hoff &&
1328 ir >= cellsRow[ic].v.pd.voff);
1330 * Verify that it's padding for something that can point
1334 const struct TerminalCell *parent =
1335 [self getCellAtColumn:(ic - cellsRow[ic].v.pd.hoff)
1336 row:(ir - cellsRow[ic].v.pd.voff)];
1338 assert(parent->form == TERM_CELL_TILE);
1339 assert(parent->hscl > cellsRow[ic].v.pd.hoff &&
1340 parent->vscl > cellsRow[ic].v.pd.voff);
1341 assert(parent->hscl == cellsRow[ic].hscl &&
1342 parent->vscl == cellsRow[ic].vscl);
1343 assert(parent->hoff_d == cellsRow[ic].hoff_d &&
1344 parent->voff_d == cellsRow[ic].voff_d);
1352 cellsRow += self.columnCount;
1356 + (wchar_t)getBlankChar
1361 + (int)getBlankAttribute
1369 * TerminalChanges is used to track changes made via the text_hook, pict_hook,
1370 * wipe_hook, curs_hook, and bigcurs_hook callbacks on the terminal since the
1371 * last call to xtra_hook for TERM_XTRA_FRESH. The locations marked as changed
1372 * can then be used to make bounding rectangles for the regions that need to
1375 @interface TerminalChanges : NSObject {
1378 * Outside of firstChangedRow, lastChangedRow and what's in colBounds, the
1379 * contents of this are handled lazily.
1385 * Initialize with zero columns and zero rows.
1390 * Initialize with nCol columns and nRow rows. No changes will be marked.
1392 - (id)initWithColumns:(int)nCol rows:(int)nRow NS_DESIGNATED_INITIALIZER;
1395 * Resize to be nCol by nRow. Current contents still within the new bounds
1396 * are preserved. Added areas are marked as unchanged.
1398 - (void)resizeWithColumns:(int)nCol rows:(int)nRow;
1401 * Clears all marked changes.
1405 - (BOOL)isChangedAtColumn:(int)icol row:(int)irow;
1408 * Scans the row, irow, starting at the column, icol0, and stopping before the
1409 * column, icol1. Returns the column index for the first cell that is
1410 * changed. The returned index will be equal to icol1 if all of the cells in
1411 * the range are unchanged.
1413 - (int)scanForChangedInRow:(int)irow col0:(int)icol0 col1:(int)icol1;
1416 * Scans the row, irow, starting at the column, icol0, and stopping before the
1417 * column, icol1. returns the column index for the first cell that has not
1418 * changed. The returned index will be equal to icol1 if all of the cells in
1419 * the range have changed.
1421 - (int)scanForUnchangedInRow:(int)irow col0:(int)icol0 col1:(int)icol1;
1423 - (void)markChangedAtColumn:(int)icol row:(int)irow;
1425 - (void)markChangedRangeAtColumn:(int)icol row:(int)irow width:(int)w;
1428 * Marks the block as changed who's upper left hand corner is at (icol, irow).
1430 - (void)markChangedBlockAtColumn:(int)icol
1436 * Returns the index of the first changed column in the given row. That index
1437 * will be equal to the number of columns if there are no changes in the row.
1439 - (int)getFirstChangedColumnInRow:(int)irow;
1442 * Returns the index of the last changed column in the given row. That index
1443 * will be equal to -1 if there are no changes in the row.
1445 - (int)getLastChangedColumnInRow:(int)irow;
1448 * Is the number of columns.
1450 @property (readonly) int columnCount;
1453 * Is the number of rows.
1455 @property (readonly) int rowCount;
1458 * Is the index of the first row with changes. Will be equal to the number
1459 * of rows if there are no changes.
1461 @property (readonly) int firstChangedRow;
1464 * Is the index of the last row with changes. Will be equal to -1 if there
1467 @property (readonly) int lastChangedRow;
1471 @implementation TerminalChanges
1475 return [self initWithColumns:0 rows:0];
1478 - (id)initWithColumns:(int)nCol rows:(int)nRow
1480 if (self = [super init]) {
1481 self->colBounds = malloc(2 * nRow * sizeof(int));
1482 self->marks = malloc(nCol * nRow * sizeof(BOOL));
1483 self->_columnCount = nCol;
1484 self->_rowCount = nRow;
1492 if (self->marks != 0) {
1496 if (self->colBounds != 0) {
1497 free(self->colBounds);
1498 self->colBounds = 0;
1502 - (void)resizeWithColumns:(int)nCol rows:(int)nRow
1504 int* newColBounds = malloc(2 * nRow * sizeof(int));
1505 BOOL* newMarks = malloc(nCol * nRow * sizeof(BOOL));
1506 int nRowCommon = (nRow < self.rowCount) ? nRow : self.rowCount;
1508 if (self.firstChangedRow <= self.lastChangedRow &&
1509 self.firstChangedRow < nRowCommon) {
1510 BOOL* marksOutCursor = newMarks + self.firstChangedRow * nCol;
1511 const BOOL* marksInCursor =
1512 self->marks + self.firstChangedRow * self.columnCount;
1513 int nColCommon = (nCol < self.columnCount) ? nCol : self.columnCount;
1515 if (self.lastChangedRow >= nRowCommon) {
1516 self->_lastChangedRow = nRowCommon - 1;
1518 for (int i = self.firstChangedRow; i <= self.lastChangedRow; ++i) {
1519 if (self->colBounds[i + i] < nColCommon) {
1520 newColBounds[i + i] = self->colBounds[i + i];
1521 newColBounds[i + i + 1] =
1522 (self->colBounds[i + i + 1] < nColCommon) ?
1523 self->colBounds[i + i + 1] : nColCommon - 1;
1525 marksOutCursor + self->colBounds[i + i],
1526 marksInCursor + self->colBounds[i + i],
1527 (newColBounds[i + i + 1] - newColBounds[i + i] + 1) *
1529 marksInCursor += self.columnCount;
1530 marksOutCursor += nCol;
1532 self->colBounds[i + i] = nCol;
1533 self->colBounds[i + i + 1] = -1;
1537 self->_firstChangedRow = nRow;
1538 self->_lastChangedRow = -1;
1541 free(self->colBounds);
1542 self->colBounds = newColBounds;
1544 self->marks = newMarks;
1545 self->_columnCount = nCol;
1546 self->_rowCount = nRow;
1551 self->_firstChangedRow = self.rowCount;
1552 self->_lastChangedRow = -1;
1555 - (BOOL)isChangedAtColumn:(int)icol row:(int)irow
1557 if (irow < self.firstChangedRow || irow > self.lastChangedRow) {
1560 if (icol < self->colBounds[irow + irow] ||
1561 icol > self->colBounds[irow + irow + 1]) {
1564 return self->marks[icol + irow * self.columnCount];
1567 - (int)scanForChangedInRow:(int)irow col0:(int)icol0 col1:(int)icol1
1569 if (irow < self.firstChangedRow || irow > self.lastChangedRow ||
1570 icol0 > self->colBounds[irow + irow + 1]) {
1574 int i = (icol0 > self->colBounds[irow + irow]) ?
1575 icol0 : self->colBounds[irow + irow];
1576 int i1 = (icol1 <= self->colBounds[irow + irow + 1]) ?
1577 icol1 : self->colBounds[irow + irow + 1] + 1;
1578 const BOOL* marksCursor = self->marks + irow * self.columnCount;
1583 if (marksCursor[i]) {
1590 - (int)scanForUnchangedInRow:(int)irow col0:(int)icol0 col1:(int)icol1
1592 if (irow < self.firstChangedRow || irow > self.lastChangedRow ||
1593 icol0 < self->colBounds[irow + irow] ||
1594 icol0 > self->colBounds[irow + irow + 1]) {
1599 int i1 = (icol1 <= self->colBounds[irow + irow + 1]) ?
1600 icol1 : self->colBounds[irow + irow + 1] + 1;
1601 const BOOL* marksCursor = self->marks + irow * self.columnCount;
1603 if (i >= i1 || ! marksCursor[i]) {
1610 - (void)markChangedAtColumn:(int)icol row:(int)irow
1612 [self markChangedBlockAtColumn:icol row:irow width:1 height:1];
1615 - (void)markChangedRangeAtColumn:(int)icol row:(int)irow width:(int)w
1617 [self markChangedBlockAtColumn:icol row:irow width:w height:1];
1620 - (void)markChangedBlockAtColumn:(int)icol
1625 if (irow + h <= self.firstChangedRow) {
1626 /* All prior marked regions are on rows after the requested block. */
1627 if (self.firstChangedRow > self.lastChangedRow) {
1628 self->_lastChangedRow = irow + h - 1;
1630 for (int i = irow + h; i < self.firstChangedRow; ++i) {
1631 self->colBounds[i + i] = self.columnCount;
1632 self->colBounds[i + i + 1] = -1;
1635 self->_firstChangedRow = irow;
1637 BOOL* marksCursor = self->marks + irow * self.columnCount;
1638 for (int i = irow; i < irow + h; ++i) {
1639 self->colBounds[i + i] = icol;
1640 self->colBounds[i + i + 1] = icol + w - 1;
1641 for (int j = icol; j < icol + w; ++j) {
1642 marksCursor[j] = YES;
1644 marksCursor += self.columnCount;
1646 } else if (irow > self.lastChangedRow) {
1647 /* All prior marked regions are on rows before the requested block. */
1650 for (i = self.lastChangedRow + 1; i < irow; ++i) {
1651 self->colBounds[i + i] = self.columnCount;
1652 self->colBounds[i + i + 1] = -1;
1654 self->_lastChangedRow = irow + h - 1;
1656 BOOL* marksCursor = self->marks + irow * self.columnCount;
1657 for (i = irow; i < irow + h; ++i) {
1658 self->colBounds[i + i] = icol;
1659 self->colBounds[i + i + 1] = icol + w - 1;
1660 for (int j = icol; j < icol + w; ++j) {
1661 marksCursor[j] = YES;
1663 marksCursor += self.columnCount;
1667 * There's overlap between the rows of the requested block and prior
1670 BOOL* marksCursor = self->marks + irow * self.columnCount;
1673 if (irow < self.firstChangedRow) {
1674 /* Handle any leading rows where there's no overlap. */
1675 for (int i = irow; i < self.firstChangedRow; ++i) {
1676 self->colBounds[i + i] = icol;
1677 self->colBounds[i + i + 1] = icol + w - 1;
1678 for (int j = icol; j < icol + w; ++j) {
1679 marksCursor[j] = YES;
1681 marksCursor += self.columnCount;
1683 irow0 = self.firstChangedRow;
1684 h0 = irow + h - self.firstChangedRow;
1685 self->_firstChangedRow = irow;
1691 /* Handle potentially overlapping rows */
1692 if (irow0 + h0 > self.lastChangedRow + 1) {
1693 h0 = self.lastChangedRow + 1 - irow0;
1694 self->_lastChangedRow = irow + h - 1;
1698 for (i = irow0; i < irow0 + h0; ++i) {
1699 if (icol + w <= self->colBounds[i + i]) {
1702 for (j = icol; j < icol + w; ++j) {
1703 marksCursor[j] = YES;
1705 if (self->colBounds[i + i] > self->colBounds[i + i + 1]) {
1706 self->colBounds[i + i + 1] = icol + w - 1;
1708 for (j = icol + w; j < self->colBounds[i + i]; ++j) {
1709 marksCursor[j] = NO;
1712 self->colBounds[i + i] = icol;
1713 } else if (icol > self->colBounds[i + i + 1]) {
1716 for (j = self->colBounds[i + i + 1] + 1; j < icol; ++j) {
1717 marksCursor[j] = NO;
1719 for (j = icol; j < icol + w; ++j) {
1720 marksCursor[j] = YES;
1722 self->colBounds[i + i + 1] = icol + w - 1;
1724 if (icol < self->colBounds[i + i]) {
1725 self->colBounds[i + i] = icol;
1727 if (icol + w > self->colBounds[i + i + 1]) {
1728 self->colBounds[i + i + 1] = icol + w - 1;
1730 for (int j = icol; j < icol + w; ++j) {
1731 marksCursor[j] = YES;
1734 marksCursor += self.columnCount;
1737 /* Handle any trailing rows where there's no overlap. */
1738 for (i = irow0 + h0; i < irow + h; ++i) {
1739 self->colBounds[i + i] = icol;
1740 self->colBounds[i + i + 1] = icol + w - 1;
1741 for (int j = icol; j < icol + w; ++j) {
1742 marksCursor[j] = YES;
1744 marksCursor += self.columnCount;
1749 - (int)getFirstChangedColumnInRow:(int)irow
1751 if (irow < self.firstChangedRow || irow > self.lastChangedRow) {
1752 return self.columnCount;
1754 return self->colBounds[irow + irow];
1757 - (int)getLastChangedColumnInRow:(int)irow
1759 if (irow < self.firstChangedRow || irow > self.lastChangedRow) {
1762 return self->colBounds[irow + irow + 1];
1769 * Draws one tile as a helper function for AngbandContext's drawRect.
1771 static void draw_image_tile(
1772 NSGraphicsContext* nsContext,
1773 CGContextRef cgContext,
1777 NSCompositingOperation op)
1779 /* Flip the source rect since the source image is flipped */
1780 CGAffineTransform flip = CGAffineTransformIdentity;
1781 flip = CGAffineTransformTranslate(flip, 0.0, CGImageGetHeight(image));
1782 flip = CGAffineTransformScale(flip, 1.0, -1.0);
1783 CGRect flippedSourceRect =
1784 CGRectApplyAffineTransform(NSRectToCGRect(srcRect), flip);
1787 * When we use high-quality resampling to draw a tile, pixels from outside
1788 * the tile may bleed in, causing graphics artifacts. Work around that.
1790 CGImageRef subimage =
1791 CGImageCreateWithImageInRect(image, flippedSourceRect);
1792 [nsContext setCompositingOperation:op];
1793 CGContextDrawImage(cgContext, NSRectToCGRect(dstRect), subimage);
1794 CGImageRelease(subimage);
1799 * The max number of glyphs we support. Currently this only affects
1800 * updateGlyphInfo() for the calculation of the tile size, fontAscender,
1801 * fontDescender, nColPre, and nColPost. The rendering in drawWChar() will
1802 * work for a glyph not in updateGlyphInfo()'s set, and that is used for
1803 * rendering Japanese characters, though there may be clipping or clearing
1804 * artifacts because it wasn't included in updateGlyphInfo()'s calculations.
1806 #define GLYPH_COUNT 256
1809 * An AngbandContext represents a logical Term (i.e. what Angband thinks is
1812 @interface AngbandContext : NSObject <NSWindowDelegate>
1816 /* The Angband term */
1820 /* Is the last time we drew, so we can throttle drawing. */
1821 CFAbsoluteTime lastRefreshTime;
1823 /* Flags whether or not a fullscreen transition is in progress. */
1824 BOOL inFullscreenTransition;
1827 AngbandView *angbandView;
1830 /* Column and row counts, by default 80 x 24 */
1831 @property (readonly) int cols;
1832 @property (readonly) int rows;
1834 /* The size of the border between the window edge and the contents */
1835 @property (readonly) NSSize borderSize;
1837 /* The font of this context */
1838 @property NSFont *angbandViewFont;
1840 /* The size of one tile */
1841 @property (readonly) NSSize tileSize;
1843 /* Font's ascender and descender */
1844 @property (readonly) CGFloat fontAscender;
1845 @property (readonly) CGFloat fontDescender;
1848 * These are the number of columns before or after, respectively, a text
1849 * change that may need to be redrawn.
1851 @property (readonly) int nColPre;
1852 @property (readonly) int nColPost;
1854 /* If this context owns a window, here it is. */
1855 @property NSWindow *primaryWindow;
1857 /* Holds our version of the contents of the terminal. */
1858 @property TerminalContents *contents;
1861 * Marks which locations have been changed by the text_hook, pict_hook,
1862 * wipe_hook, curs_hook, and bigcurs_hhok callbacks on the terminal since
1863 * the last call to xtra_hook with TERM_XTRA_FRESH.
1865 @property TerminalChanges *changes;
1867 @property (nonatomic, assign) BOOL hasSubwindowFlags;
1868 @property (nonatomic, assign) BOOL windowVisibilityChecked;
1870 - (void)resizeWithColumns:(int)nCol rows:(int)nRow;
1873 * Based on what has been marked as changed, inform AppKit of the bounding
1874 * rectangles for the changed areas.
1876 - (void)computeInvalidRects;
1878 - (void)drawRect:(NSRect)rect inView:(NSView *)view;
1880 /* Called at initialization to set the term */
1881 - (void)setTerm:(term *)t;
1883 /* Called when the context is going down. */
1887 * Return the rect in view coordinates for the block of cells whose upper
1888 * left corner is (x,y).
1890 - (NSRect)viewRectForCellBlockAtX:(int)x y:(int)y width:(int)w height:(int)h;
1892 /* Draw the given wide character into the given tile rect. */
1893 - (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile screenFont:(NSFont*)font
1894 context:(CGContextRef)ctx;
1897 * Returns the primary window for this angband context, creating it if
1900 - (NSWindow *)makePrimaryWindow;
1902 /* Handle becoming the main window */
1903 - (void)windowDidBecomeMain:(NSNotification *)notification;
1905 /* Return whether the context's primary window is ordered in or not */
1906 - (BOOL)isOrderedIn;
1909 * Return whether the context's primary window is the main window.
1910 * Since the terminals other than terminal 0 are configured as panels in
1911 * Hengband, this will only be true for terminal 0.
1913 - (BOOL)isMainWindow;
1916 * Return whether the context's primary window is the destination for key
1919 - (BOOL)isKeyWindow;
1921 /* Invalidate the whole image */
1922 - (void)setNeedsDisplay:(BOOL)val;
1924 /* Invalidate part of the image, with the rect expressed in view coordinates */
1925 - (void)setNeedsDisplayInRect:(NSRect)rect;
1927 /* Display (flush) our Angband views */
1928 - (void)displayIfNeeded;
1931 * Resize context to size of contentRect, and optionally save size to
1934 - (void)resizeTerminalWithContentRect: (NSRect)contentRect saveToDefaults: (BOOL)saveToDefaults;
1937 * Change the minimum size and size increments for the window associated with
1938 * the context. termIdx is the index for the terminal: pass it so this
1939 * function can be used when self->terminal has not yet been set.
1941 - (void)constrainWindowSize:(int)termIdx;
1943 - (void)saveWindowVisibleToDefaults: (BOOL)windowVisible;
1944 - (BOOL)windowVisibleUsingDefaults;
1948 * Gets the default font for all contexts. Currently not declaring this as
1949 * a class property for compatibility with versions of Xcode prior to 8.
1951 + (NSFont*)defaultFont;
1953 * Sets the default font for all contexts.
1955 + (void)setDefaultFont:(NSFont*)font;
1957 /* Internal methods */
1958 /* Set the title for the primary window. */
1959 - (void)setDefaultTitle:(int)termIdx;
1964 * Generate a mask for the subwindow flags. The mask is just a safety check to
1965 * make sure that our windows show and hide as expected. This function allows
1966 * for future changes to the set of flags without needed to update it here
1967 * (unless the underlying types change).
1969 u32b AngbandMaskForValidSubwindowFlags(void)
1971 int windowFlagBits = sizeof(*(window_flag)) * CHAR_BIT;
1972 int maxBits = MIN( 16, windowFlagBits );
1975 for( int i = 0; i < maxBits; i++ )
1977 if( window_flag_desc[i] != NULL )
1987 * Check for changes in the subwindow flags and update window visibility.
1988 * This seems to be called for every user event, so we don't
1989 * want to do any unnecessary hiding or showing of windows.
1991 static void AngbandUpdateWindowVisibility(void)
1994 * Because this function is called frequently, we'll make the mask static.
1995 * It doesn't change between calls, as the flags themselves are hardcoded
1997 static u32b validWindowFlagsMask = 0;
1999 if( validWindowFlagsMask == 0 )
2001 validWindowFlagsMask = AngbandMaskForValidSubwindowFlags();
2005 * Loop through all of the subwindows and see if there is a change in the
2006 * flags. If so, show or hide the corresponding window. We don't care about
2007 * the flags themselves; we just want to know if any are set.
2009 for( int i = 1; i < ANGBAND_TERM_MAX; i++ )
2011 AngbandContext *angbandContext =
2012 (__bridge AngbandContext*) (angband_term[i]->data);
2014 if( angbandContext == nil )
2020 * This horrible mess of flags is so that we can try to maintain some
2021 * user visibility preference. This should allow the user a window and
2022 * have it stay closed between application launches. However, this
2023 * means that when a subwindow is turned on, it will no longer appear
2024 * automatically. Angband has no concept of user control over window
2025 * visibility, other than the subwindow flags.
2027 if( !angbandContext.windowVisibilityChecked )
2029 if( [angbandContext windowVisibleUsingDefaults] )
2031 [angbandContext.primaryWindow orderFront: nil];
2032 angbandContext.windowVisibilityChecked = YES;
2036 [angbandContext.primaryWindow close];
2037 angbandContext.windowVisibilityChecked = NO;
2042 BOOL termHasSubwindowFlags = ((window_flag[i] & validWindowFlagsMask) > 0);
2044 if( angbandContext.hasSubwindowFlags && !termHasSubwindowFlags )
2046 [angbandContext.primaryWindow close];
2047 angbandContext.hasSubwindowFlags = NO;
2048 [angbandContext saveWindowVisibleToDefaults: NO];
2050 else if( !angbandContext.hasSubwindowFlags && termHasSubwindowFlags )
2052 [angbandContext.primaryWindow orderFront: nil];
2053 angbandContext.hasSubwindowFlags = YES;
2054 [angbandContext saveWindowVisibleToDefaults: YES];
2059 /* Make the main window key so that user events go to the right spot */
2060 AngbandContext *mainWindow =
2061 (__bridge AngbandContext*) (angband_term[0]->data);
2062 [mainWindow.primaryWindow makeKeyAndOrderFront: nil];
2066 * ------------------------------------------------------------------------
2068 * ------------------------------------------------------------------------ */
2073 static CGImageRef pict_image;
2076 * Numbers of rows and columns in a tileset,
2077 * calculated by the PICT/PNG loading code
2079 static int pict_cols = 0;
2080 static int pict_rows = 0;
2083 * Requested graphics mode (as a grafID).
2084 * The current mode is stored in current_graphics_mode.
2086 static int graf_mode_req = 0;
2089 * Helper function to check the various ways that graphics can be enabled,
2090 * guarding against NULL
2092 static BOOL graphics_are_enabled(void)
2094 return current_graphics_mode
2095 && current_graphics_mode->grafID != GRAPHICS_NONE;
2099 * Like graphics_are_enabled(), but test the requested graphics mode.
2101 static BOOL graphics_will_be_enabled(void)
2103 if (graf_mode_req == GRAPHICS_NONE) {
2107 graphics_mode *new_mode = get_graphics_mode(graf_mode_req);
2108 return new_mode && new_mode->grafID != GRAPHICS_NONE;
2112 * Hack -- game in progress
2114 static Boolean game_in_progress = FALSE;
2117 #pragma mark Prototypes
2118 static BOOL redraw_for_tiles_or_term0_font(void);
2119 static void wakeup_event_loop(void);
2120 static void hook_plog(const char *str);
2121 static void hook_quit(const char * str);
2122 static NSString* get_lib_directory(void);
2123 static NSString* get_doc_directory(void);
2124 static NSString* AngbandCorrectedDirectoryPath(NSString *originalPath);
2125 static void prepare_paths_and_directories(void);
2126 static void load_prefs(void);
2127 static void init_windows(void);
2128 static void handle_open_when_ready(void);
2129 static void play_sound(int event);
2130 static BOOL check_events(int wait);
2131 static BOOL send_event(NSEvent *event);
2132 static void set_color_for_index(int idx);
2133 static void record_current_savefile(void);
2136 * Available values for 'wait'
2138 #define CHECK_EVENTS_DRAIN -1
2139 #define CHECK_EVENTS_NO_WAIT 0
2140 #define CHECK_EVENTS_WAIT 1
2144 * Note when "open"/"new" become valid
2146 static bool initialized = FALSE;
2148 /* Methods for getting the appropriate NSUserDefaults */
2149 @interface NSUserDefaults (AngbandDefaults)
2150 + (NSUserDefaults *)angbandDefaults;
2153 @implementation NSUserDefaults (AngbandDefaults)
2154 + (NSUserDefaults *)angbandDefaults
2156 return [NSUserDefaults standardUserDefaults];
2161 * Methods for pulling images out of the Angband bundle (which may be separate
2162 * from the current bundle in the case of a screensaver
2164 @interface NSImage (AngbandImages)
2165 + (NSImage *)angbandImage:(NSString *)name;
2168 /* The NSView subclass that draws our Angband image */
2169 @interface AngbandView : NSView {
2171 NSBitmapImageRep *cacheForResize;
2175 @property (nonatomic, weak) AngbandContext *angbandContext;
2179 @implementation NSImage (AngbandImages)
2182 * Returns an image in the resource directoy of the bundle containing the
2183 * Angband view class.
2185 + (NSImage *)angbandImage:(NSString *)name
2187 NSBundle *bundle = [NSBundle bundleForClass:[AngbandView class]];
2188 NSString *path = [bundle pathForImageResource:name];
2189 return (path) ? [[NSImage alloc] initByReferencingFile:path] : nil;
2195 @implementation AngbandContext
2200 * We round the base size down. If we round it up, I believe we may end up
2201 * with pixels that nobody "owns" that may accumulate garbage. In general
2202 * rounding down is harmless, because any lost pixels may be sopped up by
2206 floor(self.cols * self.tileSize.width + 2 * self.borderSize.width),
2207 floor(self.rows * self.tileSize.height + 2 * self.borderSize.height));
2210 /* qsort-compatible compare function for CGSizes */
2211 static int compare_advances(const void *ap, const void *bp)
2213 const CGSize *a = ap, *b = bp;
2214 return (a->width > b->width) - (a->width < b->width);
2218 * Precompute certain metrics (tileSize, fontAscender, fontDescender, nColPre,
2219 * and nColPost) for the current font.
2221 - (void)updateGlyphInfo
2223 NSFont *screenFont = [self.angbandViewFont screenFont];
2225 /* Generate a string containing each MacRoman character */
2227 * Here and below, dynamically allocate working arrays rather than put them
2228 * on the stack in case limited stack space is an issue.
2230 unsigned char *latinString = malloc(GLYPH_COUNT);
2231 if (latinString == 0) {
2232 NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
2233 reason:@"latinString in updateGlyphInfo"
2238 for (i=0; i < GLYPH_COUNT; i++) latinString[i] = (unsigned char)i;
2240 /* Turn that into unichar. Angband uses ISO Latin 1. */
2241 NSString *allCharsString = [[NSString alloc] initWithBytes:latinString
2242 length:GLYPH_COUNT encoding:NSISOLatin1StringEncoding];
2243 unichar *unicharString = malloc(GLYPH_COUNT * sizeof(unichar));
2244 if (unicharString == 0) {
2246 NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
2247 reason:@"unicharString in updateGlyphInfo"
2251 unicharString[0] = 0;
2252 [allCharsString getCharacters:unicharString range:NSMakeRange(0, MIN(GLYPH_COUNT, [allCharsString length]))];
2253 allCharsString = nil;
2257 CGGlyph *glyphArray = calloc(GLYPH_COUNT, sizeof(CGGlyph));
2258 if (glyphArray == 0) {
2259 free(unicharString);
2260 NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
2261 reason:@"glyphArray in updateGlyphInfo"
2265 CTFontGetGlyphsForCharacters((CTFontRef)screenFont, unicharString,
2266 glyphArray, GLYPH_COUNT);
2267 free(unicharString);
2269 /* Get advances. Record the max advance. */
2270 CGSize *advances = malloc(GLYPH_COUNT * sizeof(CGSize));
2271 if (advances == 0) {
2273 NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
2274 reason:@"advances in updateGlyphInfo"
2278 CTFontGetAdvancesForGlyphs(
2279 (CTFontRef)screenFont, kCTFontHorizontalOrientation, glyphArray,
2280 advances, GLYPH_COUNT);
2281 CGFloat *glyphWidths = malloc(GLYPH_COUNT * sizeof(CGFloat));
2282 if (glyphWidths == 0) {
2285 NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
2286 reason:@"glyphWidths in updateGlyphInfo"
2290 for (i=0; i < GLYPH_COUNT; i++) {
2291 glyphWidths[i] = advances[i].width;
2295 * For good non-mono-font support, use the median advance. Start by sorting
2298 qsort(advances, GLYPH_COUNT, sizeof *advances, compare_advances);
2300 /* Skip over any initially empty run */
2302 for (startIdx = 0; startIdx < GLYPH_COUNT; startIdx++)
2304 if (advances[startIdx].width > 0) break;
2307 /* Pick the center to find the median */
2308 CGFloat medianAdvance = 0;
2309 /* In case we have all zero advances for some reason */
2310 if (startIdx < GLYPH_COUNT)
2312 medianAdvance = advances[(startIdx + GLYPH_COUNT)/2].width;
2318 * Record the ascender and descender. Some fonts, for instance DIN
2319 * Condensed and Rockwell in 10.14, the ascent on '@' exceeds that
2320 * reported by [screenFont ascender]. Get the overall bounding box
2321 * for the glyphs and use that instead of the ascender and descender
2322 * values if the bounding box result extends farther from the baseline.
2324 CGRect bounds = CTFontGetBoundingRectsForGlyphs(
2325 (CTFontRef) screenFont, kCTFontHorizontalOrientation, glyphArray,
2327 self->_fontAscender = [screenFont ascender];
2328 if (self->_fontAscender < bounds.origin.y + bounds.size.height) {
2329 self->_fontAscender = bounds.origin.y + bounds.size.height;
2331 self->_fontDescender = [screenFont descender];
2332 if (self->_fontDescender > bounds.origin.y) {
2333 self->_fontDescender = bounds.origin.y;
2337 * Record the tile size. Round both values up to have tile boundaries
2338 * match pixel boundaries.
2340 self->_tileSize.width = ceil(medianAdvance);
2341 self->_tileSize.height = ceil(self.fontAscender - self.fontDescender);
2344 * Determine whether neighboring columns need to be redrawn when a
2345 * character changes.
2347 CGRect *boxes = malloc(GLYPH_COUNT * sizeof(CGRect));
2351 NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
2352 reason:@"boxes in updateGlyphInfo"
2356 CGFloat beyond_right = 0.;
2357 CGFloat beyond_left = 0.;
2358 CTFontGetBoundingRectsForGlyphs(
2359 (CTFontRef)screenFont,
2360 kCTFontHorizontalOrientation,
2364 for (i = 0; i < GLYPH_COUNT; i++) {
2365 /* Account for the compression and offset used by drawWChar(). */
2366 CGFloat compression, offset;
2369 if (glyphWidths[i] <= self.tileSize.width) {
2371 offset = 0.5 * (self.tileSize.width - glyphWidths[i]);
2373 compression = self.tileSize.width / glyphWidths[i];
2376 v = (offset + boxes[i].origin.x) * compression;
2377 if (beyond_left > v) {
2380 v = (offset + boxes[i].origin.x + boxes[i].size.width) * compression;
2381 if (beyond_right < v) {
2386 self->_nColPre = ceil(-beyond_left / self.tileSize.width);
2387 if (beyond_right > self.tileSize.width) {
2389 ceil((beyond_right - self.tileSize.width) / self.tileSize.width);
2391 self->_nColPost = 0;
2399 - (void)requestRedraw
2401 if (! self->terminal) return;
2405 /* Activate the term */
2406 Term_activate(self->terminal);
2408 /* Redraw the contents */
2411 /* Flush the output */
2414 /* Restore the old term */
2418 - (void)setTerm:(term *)t
2424 * If we're trying to limit ourselves to a certain number of frames per second,
2425 * then compute how long it's been since we last drew, and then wait until the
2426 * next frame has passed. */
2429 if (frames_per_second > 0)
2431 CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
2432 CFTimeInterval timeSinceLastRefresh = now - self->lastRefreshTime;
2433 CFTimeInterval timeUntilNextRefresh = (1. / (double)frames_per_second) - timeSinceLastRefresh;
2435 if (timeUntilNextRefresh > 0)
2437 usleep((unsigned long)(timeUntilNextRefresh * 1000000.));
2440 self->lastRefreshTime = CFAbsoluteTimeGetCurrent();
2443 - (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile screenFont:(NSFont*)font
2444 context:(CGContextRef)ctx
2446 CGFloat tileOffsetY = self.fontAscender;
2447 CGFloat tileOffsetX = 0.0;
2448 UniChar unicharString[2];
2451 if (CFStringGetSurrogatePairForLongCharacter(wchar, unicharString)) {
2454 unicharString[0] = (UniChar) wchar;
2458 /* Get glyph and advance */
2459 CGGlyph thisGlyphArray[2] = { 0, 0 };
2460 CGSize advances[2] = { { 0, 0 }, { 0, 0 } };
2461 CTFontGetGlyphsForCharacters(
2462 (CTFontRef)font, unicharString, thisGlyphArray, nuni);
2463 CGGlyph glyph = thisGlyphArray[0];
2464 CTFontGetAdvancesForGlyphs(
2465 (CTFontRef)font, kCTFontHorizontalOrientation, thisGlyphArray,
2467 CGSize advance = advances[0];
2470 * If our font is not monospaced, our tile width is deliberately not big
2471 * enough for every character. In that event, if our glyph is too wide, we
2472 * need to compress it horizontally. Compute the compression ratio.
2473 * 1.0 means no compression.
2475 double compressionRatio;
2476 if (advance.width <= NSWidth(tile))
2478 /* Our glyph fits, so we can just draw it, possibly with an offset */
2479 compressionRatio = 1.0;
2480 tileOffsetX = (NSWidth(tile) - advance.width)/2;
2484 /* Our glyph doesn't fit, so we'll have to compress it */
2485 compressionRatio = NSWidth(tile) / advance.width;
2490 CGAffineTransform textMatrix = CGContextGetTextMatrix(ctx);
2491 CGFloat savedA = textMatrix.a;
2493 /* Set the position */
2494 textMatrix.tx = tile.origin.x + tileOffsetX;
2495 textMatrix.ty = tile.origin.y + tileOffsetY;
2497 /* Maybe squish it horizontally. */
2498 if (compressionRatio != 1.)
2500 textMatrix.a *= compressionRatio;
2503 CGContextSetTextMatrix(ctx, textMatrix);
2504 CGContextShowGlyphsAtPositions(ctx, &glyph, &CGPointZero, 1);
2506 /* Restore the text matrix if we messed with the compression ratio */
2507 if (compressionRatio != 1.)
2509 textMatrix.a = savedA;
2512 CGContextSetTextMatrix(ctx, textMatrix);
2515 - (NSRect)viewRectForCellBlockAtX:(int)x y:(int)y width:(int)w height:(int)h
2518 x * self.tileSize.width + self.borderSize.width,
2519 y * self.tileSize.height + self.borderSize.height,
2520 w * self.tileSize.width, h * self.tileSize.height);
2523 - (void)setSelectionFont:(NSFont*)font adjustTerminal: (BOOL)adjustTerminal
2525 /* Record the new font */
2526 self.angbandViewFont = font;
2528 /* Update our glyph info */
2529 [self updateGlyphInfo];
2531 if( adjustTerminal )
2534 * Adjust terminal to fit window with new font; save the new columns
2535 * and rows since they could be changed
2537 NSRect contentRect =
2539 contentRectForFrameRect: [self.primaryWindow frame]];
2541 [self constrainWindowSize:[self terminalIndex]];
2542 NSSize size = self.primaryWindow.contentMinSize;
2543 BOOL windowNeedsResizing = NO;
2544 if (contentRect.size.width < size.width) {
2545 contentRect.size.width = size.width;
2546 windowNeedsResizing = YES;
2548 if (contentRect.size.height < size.height) {
2549 contentRect.size.height = size.height;
2550 windowNeedsResizing = YES;
2552 if (windowNeedsResizing) {
2553 size.width = contentRect.size.width;
2554 size.height = contentRect.size.height;
2555 [self.primaryWindow setContentSize:size];
2557 [self resizeTerminalWithContentRect: contentRect saveToDefaults: YES];
2563 if ((self = [super init]))
2565 /* Default rows and cols */
2569 /* Default border size */
2570 self->_borderSize = NSMakeSize(2, 2);
2573 self->_nColPost = 0;
2576 [[TerminalContents alloc] initWithColumns:self->_cols
2579 [[TerminalChanges alloc] initWithColumns:self->_cols
2581 self->lastRefreshTime = CFAbsoluteTimeGetCurrent();
2582 self->inFullscreenTransition = NO;
2584 self->_windowVisibilityChecked = NO;
2590 * Destroy all the receiver's stuff. This is intended to be callable more than
2595 self->terminal = NULL;
2597 /* Disassociate ourselves from our view. */
2598 [self->angbandView setAngbandContext:nil];
2599 self->angbandView = nil;
2602 self.angbandViewFont = nil;
2605 [self.primaryWindow setDelegate:nil];
2606 [self.primaryWindow close];
2607 self.primaryWindow = nil;
2609 /* Contents and pending changes */
2610 self.contents = nil;
2614 /* Usual Cocoa fare */
2620 - (void)resizeWithColumns:(int)nCol rows:(int)nRow
2622 [self.contents resizeWithColumns:nCol rows:nRow];
2623 [self.changes resizeWithColumns:nCol rows:nRow];
2629 * For defaultFont and setDefaultFont.
2631 static __strong NSFont* gDefaultFont = nil;
2633 + (NSFont*)defaultFont
2635 return gDefaultFont;
2638 + (void)setDefaultFont:(NSFont*)font
2640 gDefaultFont = font;
2643 - (void)setDefaultTitle:(int)termIdx
2645 NSMutableString *title =
2646 [NSMutableString stringWithCString:angband_term_name[termIdx]
2648 encoding:NSJapaneseEUCStringEncoding
2650 encoding:NSMacOSRomanStringEncoding
2653 [title appendFormat:@" %dx%d", self.cols, self.rows];
2654 [[self makePrimaryWindow] setTitle:title];
2657 - (NSWindow *)makePrimaryWindow
2659 if (! self.primaryWindow)
2662 * This has to be done after the font is set, which it already is in
2665 NSSize sz = self.baseSize;
2666 NSRect contentRect = NSMakeRect( 0.0, 0.0, sz.width, sz.height );
2668 NSUInteger styleMask = NSTitledWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask;
2671 * Make every window other than the main window closable, also create
2672 * them as utility panels to get the thinner title bar and other
2673 * attributes that already match up with how those windows are used.
2675 if ((__bridge AngbandContext*) (angband_term[0]->data) != self)
2678 [[NSPanel alloc] initWithContentRect:contentRect
2679 styleMask:(styleMask | NSClosableWindowMask |
2680 NSUtilityWindowMask)
2681 backing:NSBackingStoreBuffered defer:YES];
2683 panel.floatingPanel = NO;
2684 self.primaryWindow = panel;
2686 self.primaryWindow =
2687 [[NSWindow alloc] initWithContentRect:contentRect
2689 backing:NSBackingStoreBuffered defer:YES];
2692 /* Not to be released when closed */
2693 [self.primaryWindow setReleasedWhenClosed:NO];
2694 [self.primaryWindow setExcludedFromWindowsMenu: YES]; /* we're using custom window menu handling */
2697 self->angbandView = [[AngbandView alloc] initWithFrame:contentRect];
2698 [angbandView setAngbandContext:self];
2699 [angbandView setNeedsDisplay:YES];
2700 [self.primaryWindow setContentView:angbandView];
2702 /* We are its delegate */
2703 [self.primaryWindow setDelegate:self];
2705 return self.primaryWindow;
2709 - (void)computeInvalidRects
2711 for (int irow = self.changes.firstChangedRow;
2712 irow <= self.changes.lastChangedRow;
2714 int icol = [self.changes scanForChangedInRow:irow
2715 col0:0 col1:self.cols];
2717 while (icol < self.cols) {
2718 /* Find the end of the changed region. */
2720 [self.changes scanForUnchangedInRow:irow col0:(icol + 1)
2724 * If the last column is a character, extend the region drawn
2725 * because characters can exceed the horizontal bounds of the cell
2726 * and those parts will need to be cleared. Don't extend into a
2727 * tile because the clipping is set while drawing to never
2728 * extend text into a tile. For a big character that's been
2729 * partially overwritten, allow what comes after the point
2730 * where the overwrite occurred to influence the stuff before
2731 * but not vice versa. If extending the region reaches another
2732 * changed block, find the end of that block and repeat the
2736 * A value of zero means checking for a character immediately
2737 * prior to the column, isrch. A value of one means checking for
2738 * something past the end that could either influence the changed
2739 * region (within nColPre of it and no intervening tile) or be
2740 * influenced by it (within nColPost of it and no intervening
2741 * tile or partially overwritten big character). A value of two
2742 * means checking for something past the end which is both changed
2743 * and could affect the part of the unchanged region that has to
2744 * be redrawn because it is affected by the prior changed region
2745 * Values of three and four are like one and two, respectively,
2746 * but indicate that a partially overwritten big character was
2755 const struct TerminalCell *pcell =
2756 [self.contents getCellAtColumn:(isrch - 1) row:irow];
2758 (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
2761 irng0 = isrch + self.nColPre;
2762 if (irng0 > self.cols) {
2765 irng1 = isrch + self.nColPost;
2766 if (irng1 > self.cols) {
2769 if (isrch < irng0 || isrch < irng1) {
2770 stage = isPartiallyOverwrittenBigChar(pcell) ?
2779 const struct TerminalCell *pcell =
2780 [self.contents getCellAtColumn:isrch row:irow];
2783 (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
2785 * Check if still in the region that could be
2786 * influenced by the changed region. If so,
2787 * everything up to the tile will be redrawn anyways
2788 * so combine the regions if the tile has changed
2789 * as well. Otherwise, terminate the search since
2790 * the tile doesn't allow influence to propagate
2791 * through it and don't want to affect what's in the
2794 if (isrch < irng1) {
2795 if ([self.changes isChangedAtColumn:isrch
2797 jcol = [self.changes scanForUnchangedInRow:irow
2798 col0:(isrch + 1) col1:self.cols];
2799 if (jcol < self.cols) {
2809 * With a changed character, combine the regions (if
2810 * still in the region affected by the changed region
2811 * am going to redraw everything up to this new region
2812 * anyway; if only in the region that can affect the
2813 * changed region, this changed text could influence
2814 * the current changed region).
2816 if ([self.changes isChangedAtColumn:isrch row:irow]) {
2817 jcol = [self.changes scanForUnchangedInRow:irow
2818 col0:(isrch + 1) col1:self.cols];
2819 if (jcol < self.cols) {
2827 if (isrch < irng1) {
2829 * Can be affected by the changed region so
2830 * has to be redrawn.
2835 if (isrch >= irng1) {
2836 irng0 = jcol + self.nColPre;
2837 if (irng0 > self.cols) {
2840 if (isrch >= irng0) {
2843 stage = isPartiallyOverwrittenBigChar(pcell) ?
2845 } else if (isPartiallyOverwrittenBigChar(pcell)) {
2853 * Looking for a later changed region that could influence
2854 * the region that has to be redrawn. The region that has
2855 * to be redrawn ends just before jcol.
2857 const struct TerminalCell *pcell =
2858 [self.contents getCellAtColumn:isrch row:irow];
2861 (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
2862 /* Can not spread influence through a tile. */
2865 if ([self.changes isChangedAtColumn:isrch row:irow]) {
2867 * Found one. Combine with the one ending just before
2870 jcol = [self.changes scanForUnchangedInRow:irow
2871 col0:(isrch + 1) col1:self.cols];
2872 if (jcol < self.cols) {
2881 if (isrch >= irng0) {
2884 if (isPartiallyOverwrittenBigChar(pcell)) {
2890 const struct TerminalCell *pcell =
2891 [self.contents getCellAtColumn:isrch row:irow];
2894 * Have encountered a partially overwritten big character
2895 * but still may be in the region that could be influenced
2896 * by the changed region. That influence can not extend
2897 * past the past the padding for the partially overwritten
2900 if ((pcell->form & (TERM_CELL_CHAR | TERM_CELL_TILE |
2901 TERM_CELL_TILE_PADDING)) != 0) {
2902 if (isrch < irng1) {
2904 * Still can be affected by the changed region
2905 * so everything up to isrch will be redrawn
2906 * anyways. If this location has changed,
2907 * merge the changed regions.
2909 if ([self.changes isChangedAtColumn:isrch
2911 jcol = [self.changes scanForUnchangedInRow:irow
2912 col0:(isrch + 1) col1:self.cols];
2913 if (jcol < self.cols) {
2922 (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
2924 * It's a tile. That blocks influence in either
2931 * The partially overwritten big character was
2932 * overwritten by a character. Check to see if it
2933 * can either influence the unchanged region that
2934 * has to redrawn or the changed region prior to
2937 if (isrch >= irng0) {
2942 if (isrch < irng1) {
2944 * Can be affected by the changed region so has to
2950 if (isrch >= irng1) {
2951 irng0 = jcol + self.nColPre;
2952 if (irng0 > self.cols) {
2955 if (isrch >= irng0) {
2965 * Have already encountered a partially overwritten big
2966 * character. Looking for a later changed region that
2967 * could influence the region that has to be redrawn
2968 * The region that has to be redrawn ends just before jcol.
2970 const struct TerminalCell *pcell =
2971 [self.contents getCellAtColumn:isrch row:irow];
2974 (TERM_CELL_TILE | TERM_CELL_TILE_PADDING)) != 0) {
2975 /* Can not spread influence through a tile. */
2978 if (pcell->form == TERM_CELL_CHAR) {
2979 if ([self.changes isChangedAtColumn:isrch row:irow]) {
2981 * Found a changed region. Combine with the one
2982 * ending just before jcol.
2984 jcol = [self.changes scanForUnchangedInRow:irow
2985 col0:(isrch + 1) col1:self.cols];
2986 if (jcol < self.cols) {
2995 if (isrch >= irng0) {
3002 * Check to see if there's characters before the changed region
3003 * that would have to be redrawn because it's influenced by the
3004 * changed region. Do not have to check for merging with a prior
3005 * region because of the screening already done.
3007 if (self.nColPre > 0 &&
3008 ([self.contents getCellAtColumn:icol row:irow]->form &
3009 (TERM_CELL_CHAR | TERM_CELL_CHAR_PADDING)) != 0) {
3010 int irng = icol - self.nColPre;
3015 while (icol > irng &&
3016 ([self.contents getCellAtColumn:(icol - 1)
3018 (TERM_CELL_CHAR | TERM_CELL_CHAR_PADDING)) != 0) {
3023 NSRect r = [self viewRectForCellBlockAtX:icol y:irow
3024 width:(jcol - icol) height:1];
3025 [self setNeedsDisplayInRect:r];
3027 icol = [self.changes scanForChangedInRow:irow col0:jcol
3034 #pragma mark View/Window Passthrough
3037 * This is a qsort-compatible compare function for NSRect, to get them in
3038 * ascending order by y origin.
3040 static int compare_nsrect_yorigin_greater(const void *ap, const void *bp)
3042 const NSRect *arp = ap;
3043 const NSRect *brp = bp;
3044 return (arp->origin.y > brp->origin.y) - (arp->origin.y < brp->origin.y);
3048 * This is a helper function for drawRect.
3050 - (void)renderTileRunInRow:(int)irow col0:(int)icol0 col1:(int)icol1
3051 nsctx:(NSGraphicsContext*)nsctx ctx:(CGContextRef)ctx
3052 grafWidth:(int)graf_width grafHeight:(int)graf_height
3053 overdrawRow:(int)overdraw_row overdrawMax:(int)overdraw_max
3055 /* Save the compositing mode since it is modified below. */
3056 NSCompositingOperation op = nsctx.compositingOperation;
3058 while (icol0 < icol1) {
3059 const struct TerminalCell *pcell =
3060 [self.contents getCellAtColumn:icol0 row:irow];
3061 NSRect destinationRect =
3062 [self viewRectForCellBlockAtX:icol0 y:irow
3063 width:pcell->hscl height:pcell->vscl];
3064 NSRect fgdRect = NSMakeRect(
3065 graf_width * (pcell->v.ti.fgdCol +
3066 pcell->hoff_n / (1.0 * pcell->hoff_d)),
3067 graf_height * (pcell->v.ti.fgdRow +
3068 pcell->voff_n / (1.0 * pcell->voff_d)),
3069 graf_width * pcell->hscl / (1.0 * pcell->hoff_d),
3070 graf_height * pcell->vscl / (1.0 * pcell->voff_d));
3071 NSRect bckRect = NSMakeRect(
3072 graf_width * (pcell->v.ti.bckCol +
3073 pcell->hoff_n / (1.0 * pcell->hoff_d)),
3074 graf_height * (pcell->v.ti.bckRow +
3075 pcell->voff_n / (1.0 * pcell->voff_d)),
3076 graf_width * pcell->hscl / (1.0 * pcell->hoff_d),
3077 graf_height * pcell->vscl / (1.0 * pcell->voff_d));
3078 int dbl_height_bck = overdraw_row && (irow > 2) &&
3079 (pcell->v.ti.bckRow >= overdraw_row &&
3080 pcell->v.ti.bckRow <= overdraw_max);
3081 int dbl_height_fgd = overdraw_row && (irow > 2) &&
3082 (pcell->v.ti.fgdRow >= overdraw_row) &&
3083 (pcell->v.ti.fgdRow <= overdraw_max);
3084 int aligned_row = 0, aligned_col = 0;
3085 int is_first_piece = 0, simple_upper = 0;
3087 /* Initialize stuff for handling a double-height tile. */
3088 if (dbl_height_bck || dbl_height_fgd) {
3089 if (self->terminal == angband_term[0]) {
3090 aligned_col = ((icol0 - COL_MAP) / pcell->hoff_d) *
3091 pcell->hoff_d + COL_MAP;
3093 aligned_col = (icol0 / pcell->hoff_d) * pcell->hoff_d;
3095 aligned_row = ((irow - ROW_MAP) / pcell->voff_d) *
3096 pcell->voff_d + ROW_MAP;
3099 * If the lower half has been broken into multiple pieces, only
3100 * do the work of rendering whatever is necessary for the upper
3101 * half when drawing the first piece (the one closest to the
3102 * upper left corner).
3104 struct TerminalCellLocation curs = { 0, 0 };
3106 [self.contents scanForTypeMaskInBlockAtColumn:aligned_col
3107 row:aligned_row width:pcell->hoff_d height:pcell->voff_d
3108 mask:TERM_CELL_TILE cursor:&curs];
3109 if (curs.col + aligned_col == icol0 &&
3110 curs.row + aligned_row == irow) {
3114 * Hack: lookup the previous row to determine how much of the
3115 * tile there is shown to apply it the upper half of the
3116 * double-height tile. That will do the right thing if there
3117 * is a menu displayed in that row but isn't right if there's
3118 * an object/creature/feature there that doesn't have a
3119 * mapping to the tile set and is rendered with a character.
3123 [self.contents scanForTypeMaskInBlockAtColumn:aligned_col
3124 row:(aligned_row - pcell->voff_d) width:pcell->hoff_d
3125 height:pcell->voff_d mask:TERM_CELL_TILE cursor:&curs];
3126 if (curs.col == 0 && curs.row == 0) {
3127 const struct TerminalCell *pcell2 =
3129 getCellAtColumn:(aligned_col + curs.col)
3130 row:(aligned_row + curs.row - pcell->voff_d)];
3132 if (pcell2->hscl == pcell2->hoff_d &&
3133 pcell2->vscl == pcell2->voff_d) {
3135 * The tile in the previous row hasn't been clipped
3136 * or partially overwritten. Use a streamlined
3137 * rendering procedure.
3146 * Draw the background. For a double-height tile, this is only the
3150 nsctx, ctx, pict_image, bckRect, destinationRect, NSCompositeCopy);
3151 if (dbl_height_bck && is_first_piece) {
3152 /* Combine upper half with previously drawn row. */
3154 const struct TerminalCell *pcell2 =
3155 [self.contents getCellAtColumn:aligned_col
3156 row:(aligned_row - pcell->voff_d)];
3158 [self viewRectForCellBlockAtX:aligned_col
3159 y:(aligned_row - pcell->voff_d)
3160 width:pcell2->hscl height:pcell2->vscl];
3161 NSRect brect2 = NSMakeRect(
3162 graf_width * pcell->v.ti.bckCol,
3163 graf_height * (pcell->v.ti.bckRow - 1),
3164 graf_width, graf_height);
3166 draw_image_tile(nsctx, ctx, pict_image, brect2, drect2,
3167 NSCompositeSourceOver);
3169 struct TerminalCellLocation curs = { 0, 0 };
3171 [self.contents scanForTypeMaskInBlockAtColumn:aligned_col
3172 row:(aligned_row - pcell->voff_d) width:pcell->hoff_d
3173 height:pcell->voff_d mask:TERM_CELL_TILE
3175 while (curs.col < pcell->hoff_d &&
3176 curs.row < pcell->voff_d) {
3177 const struct TerminalCell *pcell2 =
3178 [self.contents getCellAtColumn:(aligned_col + curs.col)
3179 row:(aligned_row + curs.row - pcell->voff_d)];
3181 [self viewRectForCellBlockAtX:(aligned_col + curs.col)
3182 y:(aligned_row + curs.row - pcell->voff_d)
3183 width:pcell2->hscl height:pcell2->vscl];
3185 * Column and row in the tile set are from the
3186 * double-height tile at *pcell, but the offsets within
3187 * that and size are from what's visible for *pcell2.
3189 NSRect brect2 = NSMakeRect(
3190 graf_width * (pcell->v.ti.bckCol +
3191 pcell2->hoff_n / (1.0 * pcell2->hoff_d)),
3192 graf_height * (pcell->v.ti.bckRow - 1 +
3194 (1.0 * pcell2->voff_d)),
3195 graf_width * pcell2->hscl / (1.0 * pcell2->hoff_d),
3196 graf_height * pcell2->vscl / (1.0 * pcell2->voff_d));
3198 draw_image_tile(nsctx, ctx, pict_image, brect2, drect2,
3199 NSCompositeSourceOver);
3200 curs.col += pcell2->hscl;
3202 scanForTypeMaskInBlockAtColumn:aligned_col
3203 row:(aligned_row - pcell->voff_d)
3204 width:pcell->hoff_d height:pcell->voff_d
3205 mask:TERM_CELL_TILE cursor:&curs];
3210 /* Skip drawing the foreground if it is the same as the background. */
3211 if (fgdRect.origin.x != bckRect.origin.x ||
3212 fgdRect.origin.y != bckRect.origin.y) {
3213 if (is_first_piece && dbl_height_fgd) {
3215 if (pcell->hoff_n == 0 && pcell->voff_n == 0 &&
3216 pcell->hscl == pcell->hoff_d) {
3218 * Render upper and lower parts as one since they
3221 fgdRect.origin.y -= graf_height;
3222 fgdRect.size.height += graf_height;
3223 destinationRect.origin.y -=
3224 destinationRect.size.height;
3225 destinationRect.size.height +=
3226 destinationRect.size.height;
3228 /* Not contiguous. Render the upper half. */
3230 [self viewRectForCellBlockAtX:aligned_col
3231 y:(aligned_row - pcell->voff_d)
3232 width:pcell->hoff_d height:pcell->voff_d];
3233 NSRect frect2 = NSMakeRect(
3234 graf_width * pcell->v.ti.fgdCol,
3235 graf_height * (pcell->v.ti.fgdRow - 1),
3236 graf_width, graf_height);
3239 nsctx, ctx, pict_image, frect2, drect2,
3240 NSCompositeSourceOver);
3243 /* Render the upper half pieces. */
3244 struct TerminalCellLocation curs = { 0, 0 };
3248 scanForTypeMaskInBlockAtColumn:aligned_col
3249 row:(aligned_row - pcell->voff_d)
3250 width:pcell->hoff_d height:pcell->voff_d
3251 mask:TERM_CELL_TILE cursor:&curs];
3253 if (curs.col >= pcell->hoff_d ||
3254 curs.row >= pcell->voff_d) {
3258 const struct TerminalCell *pcell2 =
3260 getCellAtColumn:(aligned_col + curs.col)
3261 row:(aligned_row + curs.row - pcell->voff_d)];
3263 [self viewRectForCellBlockAtX:(aligned_col + curs.col)
3264 y:(aligned_row + curs.row - pcell->voff_d)
3265 width:pcell2->hscl height:pcell2->vscl];
3266 NSRect frect2 = NSMakeRect(
3267 graf_width * (pcell->v.ti.fgdCol +
3269 (1.0 * pcell2->hoff_d)),
3270 graf_height * (pcell->v.ti.fgdRow - 1 +
3272 (1.0 * pcell2->voff_d)),
3273 graf_width * pcell2->hscl / (1.0 * pcell2->hoff_d),
3274 graf_height * pcell2->vscl /
3275 (1.0 * pcell2->voff_d));
3277 draw_image_tile(nsctx, ctx, pict_image, frect2, drect2,
3278 NSCompositeSourceOver);
3279 curs.col += pcell2->hscl;
3284 * Render the foreground (if a double height tile and the bottom
3285 * part is contiguous with the upper part this also render the
3289 nsctx, ctx, pict_image, fgdRect, destinationRect,
3290 NSCompositeSourceOver);
3292 icol0 = [self.contents scanForTypeMaskInRow:irow mask:TERM_CELL_TILE
3293 col0:(icol0+pcell->hscl) col1:icol1];
3296 /* Restore the compositing mode. */
3297 nsctx.compositingOperation = op;
3301 * This is what our views call to get us to draw to the window
3303 - (void)drawRect:(NSRect)rect inView:(NSView *)view
3306 * Take this opportunity to throttle so we don't flush faster than desired.
3311 self.borderSize.height + self.tileSize.height * self.rows;
3313 self.borderSize.width + self.tileSize.width * self.cols;
3315 const NSRect *invalidRects;
3316 NSInteger invalidCount;
3317 [view getRectsBeingDrawn:&invalidRects count:&invalidCount];
3320 * If the non-border areas need rendering, set some things up so they can
3321 * be reused for each invalid rectangle.
3323 NSGraphicsContext *nsctx = nil;
3324 CGContextRef ctx = 0;
3325 NSFont* screenFont = nil;
3326 int graf_width = 0, graf_height = 0;
3327 int overdraw_row = 0, overdraw_max = 0;
3329 if (rect.origin.x < rightX &&
3330 rect.origin.x + rect.size.width > self.borderSize.width &&
3331 rect.origin.y < bottomY &&
3332 rect.origin.y + rect.size.height > self.borderSize.height) {
3333 nsctx = [NSGraphicsContext currentContext];
3334 ctx = [nsctx graphicsPort];
3335 screenFont = [self.angbandViewFont screenFont];
3337 blank = [TerminalContents getBlankChar];
3339 graf_width = current_graphics_mode->cell_width;
3340 graf_height = current_graphics_mode->cell_height;
3341 overdraw_row = current_graphics_mode->overdrawRow;
3342 overdraw_max = current_graphics_mode->overdrawMax;
3347 * With double height tiles, need to have rendered prior rows (i.e.
3348 * smaller y) before the current one. Since the invalid rectanges are
3349 * processed in order, ensure that by sorting the invalid rectangles in
3350 * increasing order of y origin (AppKit guarantees the invalid rectanges
3351 * are non-overlapping).
3353 NSRect* sortedRects = 0;
3354 const NSRect* workingRects;
3355 if (overdraw_row && invalidCount > 1) {
3356 sortedRects = malloc(invalidCount * sizeof(NSRect));
3357 if (sortedRects == 0) {
3358 NSException *exc = [NSException exceptionWithName:@"OutOfMemory"
3359 reason:@"sorted rects in drawRect"
3364 sortedRects, invalidRects, invalidCount * sizeof(NSRect));
3365 qsort(sortedRects, invalidCount, sizeof(NSRect),
3366 compare_nsrect_yorigin_greater);
3367 workingRects = sortedRects;
3369 workingRects = invalidRects;
3373 * Use -2 for unknown. Use -1 for Cocoa's blackColor. All others are the
3374 * Angband color index.
3377 int redrawCursor = 0;
3379 for (NSInteger irect = 0; irect < invalidCount; ++irect) {
3380 NSRect modRect, clearRect;
3382 int iRowFirst, iRowLast;
3383 int iColFirst, iColLast;
3385 /* Handle the top border. */
3386 if (workingRects[irect].origin.y < self.borderSize.height) {
3388 workingRects[irect].origin.y + workingRects[irect].size.height;
3389 if (edge <= self.borderSize.height) {
3391 [[NSColor blackColor] set];
3394 NSRectFill(workingRects[irect]);
3397 clearRect = workingRects[irect];
3398 clearRect.size.height =
3399 self.borderSize.height - workingRects[irect].origin.y;
3401 [[NSColor blackColor] set];
3404 NSRectFill(clearRect);
3405 modRect.origin.x = workingRects[irect].origin.x;
3406 modRect.origin.y = self.borderSize.height;
3407 modRect.size.width = workingRects[irect].size.width;
3408 modRect.size.height = edge - self.borderSize.height;
3410 modRect = workingRects[irect];
3413 /* Handle the left border. */
3414 if (modRect.origin.x < self.borderSize.width) {
3415 edge = modRect.origin.x + modRect.size.width;
3416 if (edge <= self.borderSize.width) {
3419 [[NSColor blackColor] set];
3421 NSRectFill(modRect);
3424 clearRect = modRect;
3425 clearRect.size.width = self.borderSize.width - clearRect.origin.x;
3428 [[NSColor blackColor] set];
3430 NSRectFill(clearRect);
3431 modRect.origin.x = self.borderSize.width;
3432 modRect.size.width = edge - self.borderSize.width;
3435 iRowFirst = floor((modRect.origin.y - self.borderSize.height) /
3436 self.tileSize.height);
3437 iColFirst = floor((modRect.origin.x - self.borderSize.width) /
3438 self.tileSize.width);
3439 edge = modRect.origin.y + modRect.size.height;
3440 if (edge <= bottomY) {
3442 ceil((edge - self.borderSize.height) / self.tileSize.height);
3444 iRowLast = self.rows;
3446 edge = modRect.origin.x + modRect.size.width;
3447 if (edge <= rightX) {
3449 ceil((edge - self.borderSize.width) / self.tileSize.width);
3451 iColLast = self.cols;
3454 if (self.contents.cursorColumn != -1 &&
3455 self.contents.cursorRow != -1 &&
3456 self.contents.cursorColumn + self.contents.cursorWidth - 1 >=
3458 self.contents.cursorColumn < iColLast &&
3459 self.contents.cursorRow + self.contents.cursorHeight - 1 >=
3461 self.contents.cursorRow < iRowLast) {
3465 for (int irow = iRowFirst; irow < iRowLast; ++irow) {
3467 [self.contents scanForTypeMaskInRow:irow
3468 mask:(TERM_CELL_CHAR | TERM_CELL_TILE)
3469 col0:iColFirst col1:iColLast];
3472 if (icol >= iColLast) {
3476 if ([self.contents getCellAtColumn:icol row:irow]->form ==
3479 * It is a tile. Identify how far the run of tiles goes.
3481 int jcol = [self.contents scanForPredicateInRow:irow
3482 predicate:isTileTop desired:1
3483 col0:(icol + 1) col1:iColLast];
3485 [self renderTileRunInRow:irow col0:icol col1:jcol
3487 grafWidth:graf_width grafHeight:graf_height
3488 overdrawRow:overdraw_row overdrawMax:overdraw_max];
3492 * It is a character. Identify how far the run of
3495 int jcol = [self.contents scanForPredicateInRow:irow
3496 predicate:isCharNoPartial desired:1
3497 col0:(icol + 1) col1:iColLast];
3500 if (jcol < iColLast &&
3501 isPartiallyOverwrittenBigChar(
3502 [self.contents getCellAtColumn:jcol row:irow])) {
3503 jcol2 = [self.contents scanForTypeMaskInRow:irow
3504 mask:~TERM_CELL_CHAR_PADDING
3505 col0:(jcol + 1) col1:iColLast];
3511 * Set up clipping rectangle for text. Save the
3512 * graphics context so the clipping rectangle can be
3513 * forgotten. Use CGContextBeginPath to clear the current
3514 * path so it does not affect clipping. Do not call
3515 * CGContextSetTextDrawingMode() to include clipping since
3516 * that does not appear to necessary on 10.14 and is
3517 * actually detrimental: when displaying more than one
3518 * character, only the first is visible.
3520 CGContextSaveGState(ctx);
3521 CGContextBeginPath(ctx);
3522 NSRect r = [self viewRectForCellBlockAtX:icol y:irow
3523 width:(jcol2 - icol) height:1];
3524 CGContextClipToRect(ctx, r);
3527 * See if the region to be rendered needs to be expanded:
3528 * adjacent text that could influence what's in the clipped
3532 int irng = icol - self.nColPost;
3538 if (isrch <= irng) {
3542 const struct TerminalCell *pcell2 =
3543 [self.contents getCellAtColumn:(isrch - 1)
3545 if (pcell2->form == TERM_CELL_CHAR) {
3547 if (pcell2->v.ch.glyph != blank) {
3550 } else if (pcell2->form == TERM_CELL_CHAR_PADDING) {
3552 * Only extend the rendering if this is padding
3553 * for a character that hasn't been partially
3556 if (! isPartiallyOverwrittenBigChar(pcell2)) {
3557 if (isrch - pcell2->v.pd.hoff >= 0) {
3558 const struct TerminalCell* pcell3 =
3560 getCellAtColumn:(isrch - pcell2->v.pd.hoff)
3563 if (pcell3->v.ch.glyph != blank) {
3564 icol = isrch - pcell2->v.pd.hoff;
3567 isrch = isrch - pcell2->v.pd.hoff - 1;
3570 /* Should not happen, corrupt offset. */
3578 * Tiles or tile padding block anything before
3579 * them from rendering after them.
3586 irng = jcol2 + self.nColPre;
3587 if (irng > self.cols) {
3591 if (isrch >= irng) {
3595 const struct TerminalCell *pcell2 =
3596 [self.contents getCellAtColumn:isrch row:irow];
3597 if (pcell2->form == TERM_CELL_CHAR) {
3598 if (pcell2->v.ch.glyph != blank) {
3602 } else if (pcell2->form == TERM_CELL_CHAR_PADDING) {
3610 /* Clear where rendering will be done. */
3612 [[NSColor blackColor] set];
3615 r = [self viewRectForCellBlockAtX:icol y:irow
3616 width:(jcol - icol) height:1];
3619 while (icol < jcol) {
3620 const struct TerminalCell *pcell =
3621 [self.contents getCellAtColumn:icol row:irow];
3624 * For blanks, clearing was all that was necessary.
3625 * Don't redraw them.
3627 if (pcell->v.ch.glyph != blank) {
3628 int a = pcell->v.ch.attr % MAX_COLORS;
3632 set_color_for_index(a);
3634 r = [self viewRectForCellBlockAtX:icol
3635 y:irow width:pcell->hscl
3637 [self drawWChar:pcell->v.ch.glyph inRect:r
3638 screenFont:screenFont context:ctx];
3640 icol += pcell->hscl;
3644 * Forget the clipping rectangle. As a side effect, lose
3647 CGContextRestoreGState(ctx);
3651 [self.contents scanForTypeMaskInRow:irow
3652 mask:(TERM_CELL_CHAR | TERM_CELL_TILE)
3653 col0:icol col1:iColLast];
3657 /* Handle the right border. */
3658 edge = modRect.origin.x + modRect.size.width;
3659 if (edge > rightX) {
3660 if (modRect.origin.x >= rightX) {
3663 [[NSColor blackColor] set];
3665 NSRectFill(modRect);
3668 clearRect = modRect;
3669 clearRect.origin.x = rightX;
3670 clearRect.size.width = edge - rightX;
3673 [[NSColor blackColor] set];
3675 NSRectFill(clearRect);
3676 modRect.size.width = edge - modRect.origin.x;
3679 /* Handle the bottom border. */
3680 edge = modRect.origin.y + modRect.size.height;
3681 if (edge > bottomY) {
3682 if (modRect.origin.y < bottomY) {
3683 modRect.origin.y = bottomY;
3684 modRect.size.height = edge - bottomY;
3688 [[NSColor blackColor] set];
3690 NSRectFill(modRect);
3695 NSRect r = [self viewRectForCellBlockAtX:self.contents.cursorColumn
3696 y:self.contents.cursorRow
3697 width:self.contents.cursorWidth
3698 height:self.contents.cursorHeight];
3699 [[NSColor yellowColor] set];
3700 NSFrameRectWithWidth(r, 1);
3708 return [[self->angbandView window] isVisible];
3711 - (BOOL)isMainWindow
3713 return [[self->angbandView window] isMainWindow];
3718 return [[self->angbandView window] isKeyWindow];
3721 - (void)setNeedsDisplay:(BOOL)val
3723 [self->angbandView setNeedsDisplay:val];
3726 - (void)setNeedsDisplayInRect:(NSRect)rect
3728 [self->angbandView setNeedsDisplayInRect:rect];
3731 - (void)displayIfNeeded
3733 [self->angbandView displayIfNeeded];
3736 - (int)terminalIndex
3740 for( termIndex = 0; termIndex < ANGBAND_TERM_MAX; termIndex++ )
3742 if( angband_term[termIndex] == self->terminal )
3751 - (void)resizeTerminalWithContentRect: (NSRect)contentRect saveToDefaults: (BOOL)saveToDefaults
3753 CGFloat newRows = floor(
3754 (contentRect.size.height - (self.borderSize.height * 2.0)) /
3755 self.tileSize.height);
3756 CGFloat newColumns = floor(
3757 (contentRect.size.width - (self.borderSize.width * 2.0)) /
3758 self.tileSize.width);
3760 if (newRows < 1 || newColumns < 1) return;
3761 [self resizeWithColumns:newColumns rows:newRows];
3763 int termIndex = [self terminalIndex];
3764 [self setDefaultTitle:termIndex];
3766 if( saveToDefaults )
3768 NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
3770 if( termIndex < (int)[terminals count] )
3772 NSMutableDictionary *mutableTerm = [[NSMutableDictionary alloc] initWithDictionary: [terminals objectAtIndex: termIndex]];
3773 [mutableTerm setValue: [NSNumber numberWithInteger: self.cols]
3774 forKey: AngbandTerminalColumnsDefaultsKey];
3775 [mutableTerm setValue: [NSNumber numberWithInteger: self.rows]
3776 forKey: AngbandTerminalRowsDefaultsKey];
3778 NSMutableArray *mutableTerminals = [[NSMutableArray alloc] initWithArray: terminals];
3779 [mutableTerminals replaceObjectAtIndex: termIndex withObject: mutableTerm];
3781 [[NSUserDefaults standardUserDefaults] setValue: mutableTerminals forKey: AngbandTerminalsDefaultsKey];
3786 Term_activate( self->terminal );
3787 Term_resize( self.cols, self.rows );
3789 Term_activate( old );
3792 - (void)constrainWindowSize:(int)termIdx
3798 minsize.height = 24;
3804 minsize.width * self.tileSize.width + self.borderSize.width * 2.0;
3806 minsize.height * self.tileSize.height + self.borderSize.height * 2.0;
3807 [[self makePrimaryWindow] setContentMinSize:minsize];
3808 self.primaryWindow.contentResizeIncrements = self.tileSize;
3811 - (void)saveWindowVisibleToDefaults: (BOOL)windowVisible
3813 int termIndex = [self terminalIndex];
3814 BOOL safeVisibility = (termIndex == 0) ? YES : windowVisible; /* Ensure main term doesn't go away because of these defaults */
3815 NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
3817 if( termIndex < (int)[terminals count] )
3819 NSMutableDictionary *mutableTerm = [[NSMutableDictionary alloc] initWithDictionary: [terminals objectAtIndex: termIndex]];
3820 [mutableTerm setValue: [NSNumber numberWithBool: safeVisibility] forKey: AngbandTerminalVisibleDefaultsKey];
3822 NSMutableArray *mutableTerminals = [[NSMutableArray alloc] initWithArray: terminals];
3823 [mutableTerminals replaceObjectAtIndex: termIndex withObject: mutableTerm];
3825 [[NSUserDefaults standardUserDefaults] setValue: mutableTerminals forKey: AngbandTerminalsDefaultsKey];
3829 - (BOOL)windowVisibleUsingDefaults
3831 int termIndex = [self terminalIndex];
3833 if( termIndex == 0 )
3838 NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
3841 if( termIndex < (int)[terminals count] )
3843 NSDictionary *term = [terminals objectAtIndex: termIndex];
3844 NSNumber *visibleValue = [term valueForKey: AngbandTerminalVisibleDefaultsKey];
3846 if( visibleValue != nil )
3848 visible = [visibleValue boolValue];
3856 #pragma mark NSWindowDelegate Methods
3858 /*- (void)windowWillStartLiveResize: (NSNotification *)notification
3862 - (void)windowDidEndLiveResize: (NSNotification *)notification
3864 NSWindow *window = [notification object];
3865 NSRect contentRect = [window contentRectForFrameRect: [window frame]];
3866 [self resizeTerminalWithContentRect: contentRect saveToDefaults: !(self->inFullscreenTransition)];
3869 /*- (NSSize)windowWillResize: (NSWindow *)sender toSize: (NSSize)frameSize
3873 - (void)windowWillEnterFullScreen: (NSNotification *)notification
3875 self->inFullscreenTransition = YES;
3878 - (void)windowDidEnterFullScreen: (NSNotification *)notification
3880 NSWindow *window = [notification object];
3881 NSRect contentRect = [window contentRectForFrameRect: [window frame]];
3882 self->inFullscreenTransition = NO;
3883 [self resizeTerminalWithContentRect: contentRect saveToDefaults: NO];
3886 - (void)windowWillExitFullScreen: (NSNotification *)notification
3888 self->inFullscreenTransition = YES;
3891 - (void)windowDidExitFullScreen: (NSNotification *)notification
3893 NSWindow *window = [notification object];
3894 NSRect contentRect = [window contentRectForFrameRect: [window frame]];
3895 self->inFullscreenTransition = NO;
3896 [self resizeTerminalWithContentRect: contentRect saveToDefaults: NO];
3899 - (void)windowDidBecomeMain:(NSNotification *)notification
3901 NSWindow *window = [notification object];
3903 if( window != self.primaryWindow )
3908 int termIndex = [self terminalIndex];
3909 NSMenuItem *item = [[[NSApplication sharedApplication] windowsMenu] itemWithTag: AngbandWindowMenuItemTagBase + termIndex];
3910 [item setState: NSOnState];
3912 if( [[NSFontPanel sharedFontPanel] isVisible] )
3914 [[NSFontPanel sharedFontPanel] setPanelFont:self.angbandViewFont
3919 - (void)windowDidResignMain: (NSNotification *)notification
3921 NSWindow *window = [notification object];
3923 if( window != self.primaryWindow )
3928 int termIndex = [self terminalIndex];
3929 NSMenuItem *item = [[[NSApplication sharedApplication] windowsMenu] itemWithTag: AngbandWindowMenuItemTagBase + termIndex];
3930 [item setState: NSOffState];
3933 - (void)windowWillClose: (NSNotification *)notification
3936 * If closing only because the application is terminating, don't update
3937 * the visible state for when the application is relaunched.
3939 if (! quit_when_ready) {
3940 [self saveWindowVisibleToDefaults: NO];
3947 @implementation AngbandView
3959 - (void)drawRect:(NSRect)rect
3961 if ([self inLiveResize]) {
3963 * Always anchor the cached area to the upper left corner of the view.
3964 * Any parts on the right or bottom that can't be drawn from the cached
3965 * area are simply cleared. Will fill them with appropriate content
3966 * when resizing is done.
3968 const NSRect *rects;
3971 [self getRectsBeingDrawn:&rects count:&count];
3973 NSRect viewRect = [self visibleRect];
3975 [[NSColor blackColor] set];
3976 while (count-- > 0) {
3977 CGFloat drawTop = rects[count].origin.y - viewRect.origin.y;
3978 CGFloat drawBottom = drawTop + rects[count].size.height;
3979 CGFloat drawLeft = rects[count].origin.x - viewRect.origin.x;
3980 CGFloat drawRight = drawLeft + rects[count].size.width;
3982 * modRect and clrRect, like rects[count], are in the view
3983 * coordinates with y flipped. cacheRect is in the bitmap
3984 * coordinates and y is not flipped.
3986 NSRect modRect, clrRect, cacheRect;
3989 * Clip by bottom edge of cached area. Clear what's below
3992 if (drawTop >= self->cacheBounds.size.height) {
3993 NSRectFill(rects[count]);
3996 modRect.origin.x = rects[count].origin.x;
3997 modRect.origin.y = rects[count].origin.y;
3998 modRect.size.width = rects[count].size.width;
3999 cacheRect.origin.y = drawTop;
4000 if (drawBottom > self->cacheBounds.size.height) {
4002 drawBottom - self->cacheBounds.size.height;
4004 modRect.size.height = rects[count].size.height - excess;
4005 cacheRect.origin.y = 0;
4006 clrRect.origin.x = modRect.origin.x;
4007 clrRect.origin.y = modRect.origin.y + modRect.size.height;
4008 clrRect.size.width = modRect.size.width;
4009 clrRect.size.height = excess;
4010 NSRectFill(clrRect);
4012 modRect.size.height = rects[count].size.height;
4013 cacheRect.origin.y = self->cacheBounds.size.height -
4014 rects[count].size.height;
4016 cacheRect.size.height = modRect.size.height;
4019 * Clip by right edge of cached area. Clear what's to the
4020 * right of that and copy the remainder from the cache.
4022 if (drawLeft >= self->cacheBounds.size.width) {
4023 NSRectFill(modRect);
4026 cacheRect.origin.x = drawLeft;
4027 if (drawRight > self->cacheBounds.size.width) {
4028 CGFloat excess = drawRight - self->cacheBounds.size.width;
4030 modRect.size.width -= excess;
4031 cacheRect.size.width =
4032 self->cacheBounds.size.width - drawLeft;
4033 clrRect.origin.x = modRect.origin.x + modRect.size.width;
4034 clrRect.origin.y = modRect.origin.y;
4035 clrRect.size.width = excess;
4036 clrRect.size.height = modRect.size.height;
4037 NSRectFill(clrRect);
4039 cacheRect.size.width = drawRight - drawLeft;
4041 [self->cacheForResize drawInRect:modRect fromRect:cacheRect
4042 operation:NSCompositeCopy fraction:1.0
4043 respectFlipped:YES hints:nil];
4046 } else if (! self.angbandContext) {
4047 /* Draw bright orange, 'cause this ain't right */
4048 [[NSColor orangeColor] set];
4049 NSRectFill([self bounds]);
4051 /* Tell the Angband context to draw into us */
4052 [self.angbandContext drawRect:rect inView:self];
4057 * Override NSView's method to set up a cache that's used in drawRect to
4058 * handle drawing during a resize.
4060 - (void)viewWillStartLiveResize
4062 [super viewWillStartLiveResize];
4063 self->cacheBounds = [self visibleRect];
4064 self->cacheForResize =
4065 [self bitmapImageRepForCachingDisplayInRect:self->cacheBounds];
4066 if (self->cacheForResize != nil) {
4067 [self cacheDisplayInRect:self->cacheBounds
4068 toBitmapImageRep:self->cacheForResize];
4070 self->cacheBounds.size.width = 0.;
4071 self->cacheBounds.size.height = 0.;
4076 * Override NSView's method to release the cache set up in
4077 * viewWillStartLiveResize.
4079 - (void)viewDidEndLiveResize
4081 [super viewDidEndLiveResize];
4082 self->cacheForResize = nil;
4083 [self setNeedsDisplay:YES];
4089 * Delay handling of double-clicked savefiles
4091 Boolean open_when_ready = FALSE;
4096 * ------------------------------------------------------------------------
4097 * Some generic functions
4098 * ------------------------------------------------------------------------ */
4101 * Sets an Angband color at a given index
4103 static void set_color_for_index(int idx)
4107 /* Extract the R,G,B data */
4108 rv = angband_color_table[idx][1];
4109 gv = angband_color_table[idx][2];
4110 bv = angband_color_table[idx][3];
4112 CGContextSetRGBFillColor([[NSGraphicsContext currentContext] graphicsPort], rv/255., gv/255., bv/255., 1.);
4116 * Remember the current character in UserDefaults so we can select it by
4117 * default next time.
4119 static void record_current_savefile(void)
4121 NSString *savefileString = [[NSString stringWithCString:savefile encoding:NSMacOSRomanStringEncoding] lastPathComponent];
4124 NSUserDefaults *angbandDefs = [NSUserDefaults angbandDefaults];
4125 [angbandDefs setObject:savefileString forKey:@"SaveFile"];
4132 * Convert a two-byte EUC-JP encoded character (both *cp and (*cp + 1) are in
4133 * the range, 0xA1-0xFE, or *cp is 0x8E) to a UTF-32 (as a wchar_t) value in
4134 * the native byte ordering.
4136 static wchar_t convert_two_byte_eucjp_to_utf32_native(const char *cp)
4138 NSString* str = [[NSString alloc] initWithBytes:cp length:2
4139 encoding:NSJapaneseEUCStringEncoding];
4141 UniChar first = [str characterAtIndex:0];
4143 if (CFStringIsSurrogateHighCharacter(first)) {
4144 result = CFStringGetLongCharacterForSurrogatePair(
4145 first, [str characterAtIndex:1]);
4156 * ------------------------------------------------------------------------
4157 * Support for the "z-term.c" package
4158 * ------------------------------------------------------------------------ */
4162 * Initialize a new Term
4164 static void Term_init_cocoa(term *t)
4167 AngbandContext *context = [[AngbandContext alloc] init];
4169 /* Give the term ownership of the context */
4170 t->data = (void *)CFBridgingRetain(context);
4172 /* Handle graphics */
4173 t->higher_pict = !! use_graphics;
4174 t->always_pict = FALSE;
4176 NSDisableScreenUpdates();
4179 * Figure out the frame autosave name based on the index of this term
4181 NSString *autosaveName = nil;
4183 for (termIdx = 0; termIdx < ANGBAND_TERM_MAX; termIdx++)
4185 if (angband_term[termIdx] == t)
4188 [NSString stringWithFormat:@"AngbandTerm-%d", termIdx];
4194 NSString *fontName =
4195 [[NSUserDefaults angbandDefaults]
4196 stringForKey:[NSString stringWithFormat:@"FontName-%d", termIdx]];
4197 if (! fontName) fontName = [[AngbandContext defaultFont] fontName];
4200 * Use a smaller default font for the other windows, but only if the
4201 * font hasn't been explicitly set.
4204 (termIdx > 0) ? 10.0 : [[AngbandContext defaultFont] pointSize];
4205 NSNumber *fontSizeNumber =
4206 [[NSUserDefaults angbandDefaults]
4207 valueForKey: [NSString stringWithFormat: @"FontSize-%d", termIdx]];
4209 if( fontSizeNumber != nil )
4211 fontSize = [fontSizeNumber floatValue];
4214 [context setSelectionFont:[NSFont fontWithName:fontName size:fontSize]
4215 adjustTerminal: NO];
4217 NSArray *terminalDefaults =
4218 [[NSUserDefaults standardUserDefaults]
4219 valueForKey: AngbandTerminalsDefaultsKey];
4220 NSInteger rows = 24;
4221 NSInteger columns = 80;
4223 if( termIdx < (int)[terminalDefaults count] )
4225 NSDictionary *term = [terminalDefaults objectAtIndex: termIdx];
4226 NSInteger defaultRows =
4227 [[term valueForKey: AngbandTerminalRowsDefaultsKey]
4229 NSInteger defaultColumns =
4230 [[term valueForKey: AngbandTerminalColumnsDefaultsKey]
4233 if (defaultRows > 0) rows = defaultRows;
4234 if (defaultColumns > 0) columns = defaultColumns;
4237 [context resizeWithColumns:columns rows:rows];
4239 /* Get the window */
4240 NSWindow *window = [context makePrimaryWindow];
4242 /* Set its title and, for auxiliary terms, tentative size */
4243 [context setDefaultTitle:termIdx];
4244 [context constrainWindowSize:termIdx];
4247 * If this is the first term, and we support full screen (Mac OS X Lion
4248 * or later), then allow it to go full screen (sweet). Allow other
4249 * terms to be FullScreenAuxilliary, so they can at least show up.
4250 * Unfortunately in Lion they don't get brought to the full screen
4251 * space; but they would only make sense on multiple displays anyways
4252 * so it's not a big loss.
4254 if ([window respondsToSelector:@selector(toggleFullScreen:)])
4256 NSWindowCollectionBehavior behavior = [window collectionBehavior];
4259 NSWindowCollectionBehaviorFullScreenPrimary :
4260 NSWindowCollectionBehaviorFullScreenAuxiliary);
4261 [window setCollectionBehavior:behavior];
4264 /* No Resume support yet, though it would not be hard to add */
4265 if ([window respondsToSelector:@selector(setRestorable:)])
4267 [window setRestorable:NO];
4270 /* default window placement */ {
4271 static NSRect overallBoundingRect;
4276 * This is a bit of a trick to allow us to display multiple
4277 * windows in the "standard default" window position in OS X:
4278 * the upper center of the screen. The term sizes set in
4279 * load_prefs() are based on a 5-wide by 3-high grid, with the
4280 * main term being 4/5 wide by 2/3 high (hence the scaling to
4281 * find what the containing rect would be).
4283 NSRect originalMainTermFrame = [window frame];
4284 NSRect scaledFrame = originalMainTermFrame;
4285 scaledFrame.size.width *= 5.0 / 4.0;
4286 scaledFrame.size.height *= 3.0 / 2.0;
4287 scaledFrame.size.width += 1.0; /* spacing between window columns */
4288 scaledFrame.size.height += 1.0; /* spacing between window rows */
4289 [window setFrame: scaledFrame display: NO];
4291 overallBoundingRect = [window frame];
4292 [window setFrame: originalMainTermFrame display: NO];
4295 static NSRect mainTermBaseRect;
4296 NSRect windowFrame = [window frame];
4301 * The height and width adjustments were determined
4302 * experimentally, so that the rest of the windows line up
4303 * nicely without overlapping.
4305 windowFrame.size.width += 7.0;
4306 windowFrame.size.height += 9.0;
4307 windowFrame.origin.x = NSMinX( overallBoundingRect );
4308 windowFrame.origin.y =
4309 NSMaxY( overallBoundingRect ) - NSHeight( windowFrame );
4310 mainTermBaseRect = windowFrame;
4312 else if( termIdx == 1 )
4314 windowFrame.origin.x = NSMinX( mainTermBaseRect );
4315 windowFrame.origin.y =
4316 NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
4318 else if( termIdx == 2 )
4320 windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
4321 windowFrame.origin.y =
4322 NSMaxY( mainTermBaseRect ) - NSHeight( windowFrame );
4324 else if( termIdx == 3 )
4326 windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
4327 windowFrame.origin.y =
4328 NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
4330 else if( termIdx == 4 )
4332 windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
4333 windowFrame.origin.y = NSMinY( mainTermBaseRect );
4335 else if( termIdx == 5 )
4337 windowFrame.origin.x =
4338 NSMinX( mainTermBaseRect ) + NSWidth( windowFrame ) + 1.0;
4339 windowFrame.origin.y =
4340 NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
4343 [window setFrame: windowFrame display: NO];
4347 * Override the default frame above if the user has adjusted windows in
4350 if (autosaveName) [window setFrameAutosaveName:autosaveName];
4353 * Tell it about its term. Do this after we've sized it so that the
4354 * sizing doesn't trigger redrawing and such.
4356 [context setTerm:t];
4359 * Only order front if it's the first term. Other terms will be ordered
4360 * front from AngbandUpdateWindowVisibility(). This is to work around a
4361 * problem where Angband aggressively tells us to initialize terms that
4362 * don't do anything!
4364 if (t == angband_term[0])
4365 [context.primaryWindow makeKeyAndOrderFront: nil];
4367 NSEnableScreenUpdates();
4369 /* Set "mapped" flag */
4370 t->mapped_flag = true;
4379 static void Term_nuke_cocoa(term *t)
4382 AngbandContext *context = (__bridge AngbandContext*) (t->data);
4385 /* Tell the context to get rid of its windows, etc. */
4388 /* Balance our CFBridgingRetain from when we created it */
4398 * Returns the CGImageRef corresponding to an image with the given path.
4399 * Transfers ownership to the caller.
4401 static CGImageRef create_angband_image(NSString *path)
4403 CGImageRef decodedImage = NULL, result = NULL;
4405 /* Try using ImageIO to load the image */
4408 NSURL *url = [[NSURL alloc] initFileURLWithPath:path isDirectory:NO];
4411 NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:(id)kCFBooleanTrue, kCGImageSourceShouldCache, nil];
4412 CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, (CFDictionaryRef)options);
4416 * We really want the largest image, but in practice there's
4417 * only going to be one
4419 decodedImage = CGImageSourceCreateImageAtIndex(source, 0, (CFDictionaryRef)options);
4426 * Draw the sucker to defeat ImageIO's weird desire to cache and decode on
4427 * demand. Our images aren't that big!
4431 size_t width = CGImageGetWidth(decodedImage), height = CGImageGetHeight(decodedImage);
4433 /* Compute our own bitmap info */
4434 CGBitmapInfo imageBitmapInfo = CGImageGetBitmapInfo(decodedImage);
4435 CGBitmapInfo contextBitmapInfo = kCGBitmapByteOrderDefault;
4437 switch (imageBitmapInfo & kCGBitmapAlphaInfoMask) {
4438 case kCGImageAlphaNone:
4439 case kCGImageAlphaNoneSkipLast:
4440 case kCGImageAlphaNoneSkipFirst:
4442 contextBitmapInfo |= kCGImageAlphaNone;
4445 /* Some alpha, use premultiplied last which is most efficient. */
4446 contextBitmapInfo |= kCGImageAlphaPremultipliedLast;
4450 /* Draw the source image flipped, since the view is flipped */
4451 CGContextRef ctx = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(decodedImage), CGImageGetBytesPerRow(decodedImage), CGImageGetColorSpace(decodedImage), contextBitmapInfo);
4453 CGContextSetBlendMode(ctx, kCGBlendModeCopy);
4454 CGContextTranslateCTM(ctx, 0.0, height);
4455 CGContextScaleCTM(ctx, 1.0, -1.0);
4457 ctx, CGRectMake(0, 0, width, height), decodedImage);
4458 result = CGBitmapContextCreateImage(ctx);
4462 CGImageRelease(decodedImage);
4470 static errr Term_xtra_cocoa_react(void)
4472 /* Don't actually switch graphics until the game is running */
4473 if (!initialized || !game_in_progress) return (-1);
4476 /* Handle graphics */
4477 int expected_graf_mode = (current_graphics_mode) ?
4478 current_graphics_mode->grafID : GRAPHICS_NONE;
4479 if (graf_mode_req != expected_graf_mode)
4481 graphics_mode *new_mode;
4482 if (graf_mode_req != GRAPHICS_NONE) {
4483 new_mode = get_graphics_mode(graf_mode_req);
4488 /* Get rid of the old image. CGImageRelease is NULL-safe. */
4489 CGImageRelease(pict_image);
4492 /* Try creating the image if we want one */
4493 if (new_mode != NULL)
4495 NSString *img_path =
4496 [NSString stringWithFormat:@"%s/%s", new_mode->path, new_mode->file];
4497 pict_image = create_angband_image(img_path);
4499 /* If we failed to create the image, revert to ASCII. */
4503 arg_bigtile = FALSE;
4505 [[NSUserDefaults angbandDefaults]
4506 setInteger:GRAPHICS_NONE
4507 forKey:AngbandGraphicsDefaultsKey];
4509 NSString *msg = NSLocalizedStringWithDefaultValue(
4510 @"Error.TileSetLoadFailed",
4511 AngbandMessageCatalog,
4512 [NSBundle mainBundle],
4513 @"Failed to Load Tile Set",
4514 @"Alert text for failed tile set load");
4515 NSString *info = NSLocalizedStringWithDefaultValue(
4516 @"Error.TileSetRevertToASCII",
4517 AngbandMessageCatalog,
4518 [NSBundle mainBundle],
4519 @"Could not load the tile set. Switched back to ASCII.",
4520 @"Alert informative message for failed tile set load");
4521 NSAlert *alert = [[NSAlert alloc] init];
4522 alert.messageText = msg;
4523 alert.informativeText = info;
4528 if (graphics_are_enabled()) {
4530 * The contents stored in the AngbandContext may have
4531 * references to the old tile set. Out of an abundance
4532 * of caution, clear those references in case there's an
4533 * attempt to redraw the contents before the core has the
4534 * chance to update it via the text_hook, pict_hook, and
4537 for (int iterm = 0; iterm < ANGBAND_TERM_MAX; ++iterm) {
4538 AngbandContext* aContext =
4539 (__bridge AngbandContext*) (angband_term[iterm]->data);
4541 [aContext.contents wipeTiles];
4545 /* Record what we did */
4546 use_graphics = new_mode ? new_mode->grafID : 0;
4547 ANGBAND_GRAF = (new_mode ? new_mode->graf : "ascii");
4548 current_graphics_mode = new_mode;
4550 /* Enable or disable higher picts. */
4551 for (int iterm = 0; iterm < ANGBAND_TERM_MAX; ++iterm) {
4552 if (angband_term[iterm]) {
4553 angband_term[iterm]->higher_pict = !! use_graphics;
4557 if (pict_image && current_graphics_mode)
4560 * Compute the row and column count via the image height and
4563 pict_rows = (int)(CGImageGetHeight(pict_image) /
4564 current_graphics_mode->cell_height);
4565 pict_cols = (int)(CGImageGetWidth(pict_image) /
4566 current_graphics_mode->cell_width);
4575 if (arg_bigtile == use_bigtile && character_generated)
4581 if (arg_bigtile != use_bigtile) {
4582 if (character_generated)
4588 Term_activate(angband_term[0]);
4589 Term_resize(angband_term[0]->wid, angband_term[0]->hgt);
4599 * Do a "special thing"
4601 static errr Term_xtra_cocoa(int n, int v)
4605 AngbandContext* angbandContext =
4606 (__bridge AngbandContext*) (Term->data);
4611 case TERM_XTRA_NOISE:
4616 case TERM_XTRA_SOUND:
4620 /* Process random events */
4621 case TERM_XTRA_BORED:
4623 * Show or hide cocoa windows based on the subwindow flags set by
4626 AngbandUpdateWindowVisibility();
4627 /* Process an event */
4628 (void)check_events(CHECK_EVENTS_NO_WAIT);
4631 /* Process pending events */
4632 case TERM_XTRA_EVENT:
4633 /* Process an event */
4634 (void)check_events(v);
4637 /* Flush all pending events (if any) */
4638 case TERM_XTRA_FLUSH:
4639 /* Hack -- flush all events */
4640 while (check_events(CHECK_EVENTS_DRAIN)) /* loop */;
4644 /* Hack -- Change the "soft level" */
4645 case TERM_XTRA_LEVEL:
4647 * Here we could activate (if requested), but I don't think
4648 * Angband should be telling us our window order (the user
4649 * should decide that), so do nothing.
4653 /* Clear the screen */
4654 case TERM_XTRA_CLEAR:
4655 [angbandContext.contents wipe];
4656 [angbandContext setNeedsDisplay:YES];
4659 /* React to changes */
4660 case TERM_XTRA_REACT:
4661 result = Term_xtra_cocoa_react();
4664 /* Delay (milliseconds) */
4665 case TERM_XTRA_DELAY:
4668 double seconds = v / 1000.;
4669 NSDate* date = [NSDate dateWithTimeIntervalSinceNow:seconds];
4673 event = [NSApp nextEventMatchingMask:-1
4675 inMode:NSDefaultRunLoopMode
4677 if (event) send_event(event);
4679 } while ([date timeIntervalSinceNow] >= 0);
4683 /* Draw the pending changes. */
4684 case TERM_XTRA_FRESH:
4687 * Check the cursor visibility since the core will tell us
4688 * explicitly to draw it, but tells us implicitly to forget it
4689 * by simply telling us to redraw a location.
4693 Term_get_cursor(&isVisible);
4695 [angbandContext.contents removeCursor];
4697 [angbandContext computeInvalidRects];
4698 [angbandContext.changes clear];
4712 static errr Term_curs_cocoa(TERM_LEN x, TERM_LEN y)
4714 AngbandContext *angbandContext = (__bridge AngbandContext*) (Term->data);
4716 [angbandContext.contents setCursorAtColumn:x row:y width:1 height:1];
4718 * Unfortunately, this (and the same logic in Term_bigcurs_cocoa) will
4719 * also trigger what's under the cursor to be redrawn as well, even if
4720 * it has not changed. In the current drawing implementation, that
4721 * inefficiency seems unavoidable.
4723 [angbandContext.changes markChangedAtColumn:x row:y];
4730 * Draw a cursor that's two tiles wide. For Japanese, that's used when
4731 * the cursor points at a kanji character, irregardless of whether operating
4734 static errr Term_bigcurs_cocoa(TERM_LEN x, TERM_LEN y)
4736 AngbandContext *angbandContext = (__bridge AngbandContext*) (Term->data);
4738 [angbandContext.contents setCursorAtColumn:x row:y width:2 height:1];
4739 [angbandContext.changes markChangedBlockAtColumn:x row:y width:2 height:1];
4746 * Low level graphics (Assumes valid input)
4748 * Erase "n" characters starting at (x,y)
4750 static errr Term_wipe_cocoa(TERM_LEN x, TERM_LEN y, int n)
4752 AngbandContext *angbandContext = (__bridge AngbandContext*) (Term->data);
4754 [angbandContext.contents wipeBlockAtColumn:x row:y width:n height:1];
4755 [angbandContext.changes markChangedRangeAtColumn:x row:y width:n];
4761 static errr Term_pict_cocoa(TERM_LEN x, TERM_LEN y, int n,
4762 TERM_COLOR *ap, concptr cp,
4763 const TERM_COLOR *tap, concptr tcp)
4765 /* Paranoia: Bail if graphics aren't enabled */
4766 if (! graphics_are_enabled()) return -1;
4768 AngbandContext* angbandContext = (__bridge AngbandContext*) (Term->data);
4769 int step = (use_bigtile) ? 2 : 1;
4773 CGImageAlphaInfo ainfo = CGImageGetAlphaInfo(pict_image);
4775 alphablend = (ainfo & (kCGImageAlphaPremultipliedFirst |
4776 kCGImageAlphaPremultipliedLast)) ? 1 : 0;
4781 for (int i = x; i < x + n * step; i += step) {
4784 TERM_COLOR ta = *tap;
4791 if (use_graphics && (a & 0x80) && (c & 0x80)) {
4792 char fgdRow = ((byte)a & 0x7F) % pict_rows;
4793 char fgdCol = ((byte)c & 0x7F) % pict_cols;
4794 char bckRow, bckCol;
4797 bckRow = ((byte)ta & 0x7F) % pict_rows;
4798 bckCol = ((byte)tc & 0x7F) % pict_cols;
4801 * Not blending so make the background the same as the
4807 [angbandContext.contents setTileAtColumn:i row:y
4808 foregroundColumn:fgdCol
4809 foregroundRow:fgdRow
4810 backgroundColumn:bckCol
4811 backgroundRow:bckRow
4814 [angbandContext.changes markChangedBlockAtColumn:i row:y
4815 width:step height:1];
4824 * Low level graphics. Assumes valid input.
4826 * Draw several ("n") chars, with an attr, at a given location.
4828 static errr Term_text_cocoa(
4829 TERM_LEN x, TERM_LEN y, int n, TERM_COLOR a, concptr cp)
4831 AngbandContext* angbandContext = (__bridge AngbandContext*) (Term->data);
4833 [angbandContext.contents setUniformAttributeTextRunAtColumn:x
4834 row:y n:n glyphs:cp attribute:a];
4835 [angbandContext.changes markChangedRangeAtColumn:x row:y width:n];
4843 * Convert UTF-8 to UTF-32 with each UTF-32 stored in the native byte order as
4844 * a wchar_t. Return the total number of code points that would be generated
4845 * by converting the UTF-8 input.
4847 * \param dest Points to the buffer in which to store the conversion. May be
4849 * \param src Is a null-terminated UTF-8 sequence.
4850 * \param n Is the maximum number of code points to store in dest.
4852 * In case of malformed UTF-8, inserts a U+FFFD in the converted output at the
4853 * point of the error.
4855 static size_t Term_mbcs_cocoa(wchar_t *dest, const char *src, int n)
4857 size_t nout = (n > 0) ? n : 0;
4862 * Default to U+FFFD to indicate an erroneous UTF-8 sequence that
4863 * could not be decoded. Follow "best practice" recommended by the
4864 * Unicode 6 standard: an erroneous sequence ends as soon as a
4865 * disallowed byte is encountered.
4867 unsigned int decoded = 0xfffd;
4869 if (((unsigned int) *src & 0x80) == 0) {
4870 /* Encoded as single byte: U+0000 to U+0007F -> 0xxxxxxx. */
4872 if (dest && count < nout) {
4879 } else if (((unsigned int) *src & 0xe0) == 0xc0) {
4880 /* Encoded as two bytes: U+0080 to U+07FF -> 110xxxxx 10xxxxxx. */
4881 unsigned int part = ((unsigned int) *src & 0x1f) << 6;
4885 * Check that the first two bits of the continuation byte are
4886 * valid and the encoding is not overlong.
4888 if (((unsigned int) *src & 0xc0) == 0x80 && part > 0x40) {
4889 decoded = part + ((unsigned int) *src & 0x3f);
4892 } else if (((unsigned int) *src & 0xf0) == 0xe0) {
4894 * Encoded as three bytes: U+0800 to U+FFFF -> 1110xxxx 10xxxxxx
4897 unsigned int part = ((unsigned int) *src & 0xf) << 12;
4900 if (((unsigned int) *src & 0xc0) == 0x80) {
4901 part += ((unsigned int) *src & 0x3f) << 6;
4904 * The second part of the test rejects overlong encodings. The
4905 * third part rejects encodings of U+D800 to U+DFFF, reserved
4906 * for surrogate pairs.
4908 if (((unsigned int) *src & 0xc0) == 0x80 && part >= 0x800 &&
4909 (part & 0xf800) != 0xd800) {
4910 decoded = part + ((unsigned int) *src & 0x3f);
4914 } else if (((unsigned int) *src & 0xf8) == 0xf0) {
4916 * Encoded as four bytes: U+10000 to U+1FFFFF -> 11110xxx 10xxxxxx
4917 * 10xxxxxx 10xxxxxx.
4919 unsigned int part = ((unsigned int) *src & 0x7) << 18;
4922 if (((unsigned int) *src & 0xc0) == 0x80) {
4923 part += ((unsigned int) *src & 0x3f) << 12;
4926 * The second part of the test rejects overlong encodings.
4927 * The third part rejects code points beyond U+10FFFF which
4928 * can't be encoded in UTF-16.
4930 if (((unsigned int) *src & 0xc0) == 0x80 && part >= 0x10000 &&
4931 (part & 0xff0000) <= 0x100000) {
4932 part += ((unsigned int) *src & 0x3f) << 6;
4934 if (((unsigned int) *src & 0xc0) == 0x80) {
4935 decoded = part + ((unsigned int) *src & 0x3f);
4942 * Either an impossible byte or one that signals the start of a
4943 * five byte or longer encoding.
4947 if (dest && count < nout) {
4948 dest[count] = decoded;
4957 * Handle redrawing for a change to the tile set, tile scaling, or main window
4958 * font. Returns YES if the redrawing was initiated. Otherwise returns NO.
4960 static BOOL redraw_for_tiles_or_term0_font(void)
4963 * In Angband 4.2, do_cmd_redraw() will always clear, but only provides
4964 * something to replace the erased content if a character has been
4965 * generated. In Hengband, do_cmd_redraw() isn't safe to call unless a
4966 * character has been generated. Therefore, only call it if a character
4967 * has been generated.
4969 if (character_generated) {
4971 wakeup_event_loop();
4978 * Post a nonsense event so that our event loop wakes up
4980 static void wakeup_event_loop(void)
4982 /* Big hack - send a nonsense event to make us update */
4983 NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined location:NSZeroPoint modifierFlags:0 timestamp:0 windowNumber:0 context:NULL subtype:AngbandEventWakeup data1:0 data2:0];
4984 [NSApp postEvent:event atStart:NO];
4989 * Handle the "open_when_ready" flag
4991 static void handle_open_when_ready(void)
4993 /* Check the flag XXX XXX XXX make a function for this */
4994 if (open_when_ready && initialized && !game_in_progress)
4997 open_when_ready = FALSE;
4999 /* Game is in progress */
5000 game_in_progress = TRUE;
5002 /* Wait for a keypress */
5003 pause_line(Term->hgt - 1);
5009 * Handle quit_when_ready, by Peter Ammon,
5010 * slightly modified to check inkey_flag.
5012 static void quit_calmly(void)
5014 /* Quit immediately if game's not started */
5015 if (!game_in_progress || !character_generated) quit(NULL);
5017 /* Save the game and Quit (if it's safe) */
5020 /* Hack -- Forget messages and term */
5022 Term->mapped_flag = FALSE;
5025 do_cmd_save_game(FALSE);
5026 record_current_savefile();
5032 /* Wait until inkey_flag is set */
5038 * Returns YES if we contain an AngbandView (and hence should direct our events
5041 static BOOL contains_angband_view(NSView *view)
5043 if ([view isKindOfClass:[AngbandView class]]) return YES;
5044 for (NSView *subview in [view subviews]) {
5045 if (contains_angband_view(subview)) return YES;
5052 * Queue mouse presses if they occur in the map section of the main window.
5054 static void AngbandHandleEventMouseDown( NSEvent *event )
5057 AngbandContext *angbandContext = [[[event window] contentView] angbandContext];
5058 AngbandContext *mainAngbandContext =
5059 (__bridge AngbandContext*) (angband_term[0]->data);
5061 if (mainAngbandContext.primaryWindow &&
5062 [[event window] windowNumber] ==
5063 [mainAngbandContext.primaryWindow windowNumber])
5065 int cols, rows, x, y;
5066 Term_get_size(&cols, &rows);
5067 NSSize tileSize = angbandContext.tileSize;
5068 NSSize border = angbandContext.borderSize;
5069 NSPoint windowPoint = [event locationInWindow];
5072 * Adjust for border; add border height because window origin
5075 windowPoint = NSMakePoint( windowPoint.x - border.width, windowPoint.y + border.height );
5077 NSPoint p = [[[event window] contentView] convertPoint: windowPoint fromView: nil];
5078 x = floor( p.x / tileSize.width );
5079 y = floor( p.y / tileSize.height );
5082 * Being safe about this, since xcode doesn't seem to like the
5085 BOOL displayingMapInterface = ((int)inkey_flag != 0);
5087 /* Sidebar plus border == thirteen characters; top row is reserved. */
5088 /* Coordinates run from (0,0) to (cols-1, rows-1). */
5089 BOOL mouseInMapSection = (x > 13 && x <= cols - 1 && y > 0 && y <= rows - 2);
5092 * If we are displaying a menu, allow clicks anywhere within
5093 * the terminal bounds; if we are displaying the main game
5094 * interface, only allow clicks in the map section
5096 if ((!displayingMapInterface && x >= 0 && x < cols &&
5097 y >= 0 && y < rows) ||
5098 (displayingMapInterface && mouseInMapSection))
5101 * [event buttonNumber] will return 0 for left click,
5102 * 1 for right click, but this is safer
5104 int button = ([event type] == NSLeftMouseDown) ? 1 : 2;
5107 NSUInteger eventModifiers = [event modifierFlags];
5108 byte angbandModifiers = 0;
5109 angbandModifiers |= (eventModifiers & NSShiftKeyMask) ? KC_MOD_SHIFT : 0;
5110 angbandModifiers |= (eventModifiers & NSControlKeyMask) ? KC_MOD_CONTROL : 0;
5111 angbandModifiers |= (eventModifiers & NSAlternateKeyMask) ? KC_MOD_ALT : 0;
5112 button |= (angbandModifiers & 0x0F) << 4; /* encode modifiers in the button number (see Term_mousepress()) */
5115 Term_mousepress(x, y, button);
5120 /* Pass click through to permit focus change, resize, etc. */
5121 [NSApp sendEvent:event];
5127 * Encodes an NSEvent Angband-style, or forwards it along. Returns YES if the
5128 * event was sent to Angband, NO if Cocoa (or nothing) handled it
5130 static BOOL send_event(NSEvent *event)
5133 /* If the receiving window is not an Angband window, then do nothing */
5134 if (! contains_angband_view([[event window] contentView]))
5136 [NSApp sendEvent:event];
5140 /* Analyze the event */
5141 switch ([event type])
5145 /* Try performing a key equivalent */
5146 if ([[NSApp mainMenu] performKeyEquivalent:event]) break;
5148 unsigned modifiers = [event modifierFlags];
5150 /* Send all NSCommandKeyMasks through */
5151 if (modifiers & NSCommandKeyMask)
5153 [NSApp sendEvent:event];
5157 if (! [[event characters] length]) break;
5160 /* Extract some modifiers */
5161 int mc = !! (modifiers & NSControlKeyMask);
5162 int ms = !! (modifiers & NSShiftKeyMask);
5163 int mo = !! (modifiers & NSAlternateKeyMask);
5164 int kp = !! (modifiers & NSNumericPadKeyMask);
5167 /* Get the Angband char corresponding to this unichar */
5168 unichar c = [[event characters] characterAtIndex:0];
5171 * Have anything from the numeric keypad generate a macro
5172 * trigger so that shift or control modifiers can be passed.
5174 if (c <= 0x7F && !kp)
5180 * The rest of Hengband uses Angband 2.7's or so key handling:
5181 * so for the rest do something like the encoding that
5182 * main-win.c does: send a macro trigger with the Unicode
5183 * value encoded into printable ASCII characters.
5188 /* override special keys */
5189 switch([event keyCode]) {
5190 case kVK_Return: ch = '\r'; break;
5191 case kVK_Escape: ch = 27; break;
5192 case kVK_Tab: ch = '\t'; break;
5193 case kVK_Delete: ch = '\b'; break;
5194 case kVK_ANSI_KeypadEnter: ch = '\r'; kp = TRUE; break;
5197 /* Hide the mouse pointer */
5198 [NSCursor setHiddenUntilMouseMoves:YES];
5208 * Could use the hexsym global but some characters overlap with
5209 * those used to indicate modifiers.
5211 const char encoded[16] = {
5212 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
5216 /* Begin the macro trigger. */
5219 /* Send the modifiers. */
5220 if (mc) Term_keypress('C');
5221 if (ms) Term_keypress('S');
5222 if (mo) Term_keypress('O');
5223 if (kp) Term_keypress('K');
5226 Term_keypress(encoded[c & 0xF]);
5230 /* End the macro trigger. */
5237 case NSLeftMouseDown:
5238 case NSRightMouseDown:
5239 AngbandHandleEventMouseDown(event);
5242 case NSApplicationDefined:
5244 if ([event subtype] == AngbandEventWakeup)
5252 [NSApp sendEvent:event];
5259 * Check for Events, return TRUE if we process any
5261 static BOOL check_events(int wait)
5266 /* Handles the quit_when_ready flag */
5267 if (quit_when_ready) quit_calmly();
5270 if (wait == CHECK_EVENTS_WAIT) endDate = [NSDate distantFuture];
5271 else endDate = [NSDate distantPast];
5275 if (quit_when_ready)
5277 /* send escape events until we quit */
5278 Term_keypress(0x1B);
5283 event = [NSApp nextEventMatchingMask:-1 untilDate:endDate
5284 inMode:NSDefaultRunLoopMode dequeue:YES];
5289 if (send_event(event)) break;
5298 * Hook to tell the user something important
5300 static void hook_plog(const char * str)
5304 NSString *msg = NSLocalizedStringWithDefaultValue(
5305 @"Warning", AngbandMessageCatalog, [NSBundle mainBundle],
5306 @"Warning", @"Alert text for generic warning");
5307 NSString *info = [NSString stringWithCString:str
5309 encoding:NSJapaneseEUCStringEncoding
5311 encoding:NSMacOSRomanStringEncoding
5314 NSAlert *alert = [[NSAlert alloc] init];
5316 alert.messageText = msg;
5317 alert.informativeText = info;
5324 * Hook to tell the user something, and then quit
5326 static void hook_quit(const char * str)
5328 for (int i = ANGBAND_TERM_MAX - 1; i >= 0; --i) {
5329 if (angband_term[i]) {
5330 term_nuke(angband_term[i]);
5333 [AngbandSoundCatalog clearSharedSounds];
5334 [AngbandContext setDefaultFont:nil];
5340 * Return the path for Angband's lib directory and bail if it isn't found. The
5341 * lib directory should be in the bundle's resources directory, since it's
5342 * copied when built.
5344 static NSString* get_lib_directory(void)
5346 NSString *bundleLibPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: AngbandDirectoryNameLib];
5347 BOOL isDirectory = NO;
5348 BOOL libExists = [[NSFileManager defaultManager] fileExistsAtPath: bundleLibPath isDirectory: &isDirectory];
5350 if( !libExists || !isDirectory )
5352 NSLog( @"%@: can't find %@/ in bundle: isDirectory: %d libExists: %d", @VERSION_NAME, AngbandDirectoryNameLib, isDirectory, libExists );
5354 NSString *msg = NSLocalizedStringWithDefaultValue(
5355 @"Error.MissingResources",
5356 AngbandMessageCatalog,
5357 [NSBundle mainBundle],
5358 @"Missing Resources",
5359 @"Alert text for missing resources");
5360 NSString *info = NSLocalizedStringWithDefaultValue(
5361 @"Error.MissingAngbandLib",
5362 AngbandMessageCatalog,
5363 [NSBundle mainBundle],
5364 @"Hengband was unable to find required resources and must quit. Please report a bug on the Angband forums.",
5365 @"Alert informative message for missing Angband lib/ folder");
5366 NSString *quit_label = NSLocalizedStringWithDefaultValue(
5367 @"Label.Quit", AngbandMessageCatalog, [NSBundle mainBundle],
5369 NSAlert *alert = [[NSAlert alloc] init];
5371 * Note that NSCriticalAlertStyle was deprecated in 10.10. The
5372 * replacement is NSAlertStyleCritical.
5374 alert.alertStyle = NSCriticalAlertStyle;
5375 alert.messageText = msg;
5376 alert.informativeText = info;
5377 [alert addButtonWithTitle:quit_label];
5382 return bundleLibPath;
5386 * Return the path for the directory where Angband should look for its standard
5389 static NSString* get_doc_directory(void)
5391 NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
5393 #if defined(SAFE_DIRECTORY)
5394 NSString *versionedDirectory = [NSString stringWithFormat: @"%@-%s", AngbandDirectoryNameBase, VERSION_STRING];
5395 return [documents stringByAppendingPathComponent: versionedDirectory];
5397 return [documents stringByAppendingPathComponent: AngbandDirectoryNameBase];
5402 * Adjust directory paths as needed to correct for any differences needed by
5403 * Angband. init_file_paths() currently requires that all paths provided have
5404 * a trailing slash and all other platforms honor this.
5406 * \param originalPath The directory path to adjust.
5407 * \return A path suitable for Angband or nil if an error occurred.
5409 static NSString* AngbandCorrectedDirectoryPath(NSString *originalPath)
5411 if ([originalPath length] == 0) {
5415 if (![originalPath hasSuffix: @"/"]) {
5416 return [originalPath stringByAppendingString: @"/"];
5419 return originalPath;
5423 * Give Angband the base paths that should be used for the various directories
5424 * it needs. It will create any needed directories.
5426 static void prepare_paths_and_directories(void)
5428 char libpath[PATH_MAX + 1] = "\0";
5429 NSString *libDirectoryPath =
5430 AngbandCorrectedDirectoryPath(get_lib_directory());
5431 [libDirectoryPath getFileSystemRepresentation: libpath maxLength: sizeof(libpath)];
5433 char basepath[PATH_MAX + 1] = "\0";
5434 NSString *angbandDocumentsPath =
5435 AngbandCorrectedDirectoryPath(get_doc_directory());
5436 [angbandDocumentsPath getFileSystemRepresentation: basepath maxLength: sizeof(basepath)];
5438 init_file_paths(libpath, basepath);
5439 create_needed_dirs();
5443 * Create and initialize Angband terminal number "i".
5445 static term *term_data_link(int i)
5447 NSArray *terminalDefaults = [[NSUserDefaults standardUserDefaults]
5448 valueForKey: AngbandTerminalsDefaultsKey];
5449 NSInteger rows = 24;
5450 NSInteger columns = 80;
5452 if (i < (int)[terminalDefaults count]) {
5453 NSDictionary *term = [terminalDefaults objectAtIndex:i];
5454 rows = [[term valueForKey: AngbandTerminalRowsDefaultsKey]
5456 columns = [[term valueForKey: AngbandTerminalColumnsDefaultsKey]
5461 term *newterm = ZNEW(term);
5463 /* Initialize the term */
5464 term_init(newterm, columns, rows, 256 /* keypresses, for some reason? */);
5466 /* Use a "software" cursor */
5467 newterm->soft_cursor = TRUE;
5469 /* Disable the per-row flush notifications since they are not used. */
5470 newterm->never_frosh = TRUE;
5473 * Differentiate between BS/^h, Tab/^i, ... so ^h and ^j work under the
5474 * roguelike command set.
5476 /* newterm->complex_input = TRUE; */
5478 /* Erase with "white space" */
5479 newterm->attr_blank = TERM_WHITE;
5480 newterm->char_blank = ' ';
5482 /* Prepare the init/nuke hooks */
5483 newterm->init_hook = Term_init_cocoa;
5484 newterm->nuke_hook = Term_nuke_cocoa;
5486 /* Prepare the function hooks */
5487 newterm->xtra_hook = Term_xtra_cocoa;
5488 newterm->wipe_hook = Term_wipe_cocoa;
5489 newterm->curs_hook = Term_curs_cocoa;
5490 newterm->bigcurs_hook = Term_bigcurs_cocoa;
5491 newterm->text_hook = Term_text_cocoa;
5492 newterm->pict_hook = Term_pict_cocoa;
5493 /* newterm->mbcs_hook = Term_mbcs_cocoa; */
5495 /* Global pointer */
5496 angband_term[i] = newterm;
5502 * Load preferences from preferences file for current host+current user+
5503 * current application.
5505 static void load_prefs(void)
5507 NSUserDefaults *defs = [NSUserDefaults angbandDefaults];
5509 /* Make some default defaults */
5510 NSMutableArray *defaultTerms = [[NSMutableArray alloc] init];
5513 * The following default rows/cols were determined experimentally by first
5514 * finding the ideal window/font size combinations. But because of awful
5515 * temporal coupling in Term_init_cocoa(), it's impossible to set up the
5516 * defaults there, so we do it this way.
5518 for (NSUInteger i = 0; i < ANGBAND_TERM_MAX; i++) {
5554 NSDictionary *standardTerm =
5555 [NSDictionary dictionaryWithObjectsAndKeys:
5556 [NSNumber numberWithInt: rows], AngbandTerminalRowsDefaultsKey,
5557 [NSNumber numberWithInt: columns], AngbandTerminalColumnsDefaultsKey,
5558 [NSNumber numberWithBool: visible], AngbandTerminalVisibleDefaultsKey,
5560 [defaultTerms addObject: standardTerm];
5563 NSDictionary *defaults = [[NSDictionary alloc] initWithObjectsAndKeys:
5565 @"Osaka", @"FontName-0",
5567 @"Menlo", @"FontName-0",
5569 [NSNumber numberWithFloat:13.f], @"FontSize-0",
5570 [NSNumber numberWithInt:60], AngbandFrameRateDefaultsKey,
5571 [NSNumber numberWithBool:YES], AngbandSoundDefaultsKey,
5572 [NSNumber numberWithInt:GRAPHICS_NONE], AngbandGraphicsDefaultsKey,
5573 [NSNumber numberWithBool:YES], AngbandBigTileDefaultsKey,
5574 defaultTerms, AngbandTerminalsDefaultsKey,
5576 [defs registerDefaults:defaults];
5578 /* Preferred graphics mode */
5579 graf_mode_req = [defs integerForKey:AngbandGraphicsDefaultsKey];
5580 if (graphics_will_be_enabled() &&
5581 [defs boolForKey:AngbandBigTileDefaultsKey] == YES) {
5585 use_bigtile = FALSE;
5586 arg_bigtile = FALSE;
5589 /* Use sounds; set the Angband global */
5590 if ([defs boolForKey:AngbandSoundDefaultsKey] == YES) {
5592 [AngbandSoundCatalog sharedSounds].enabled = YES;
5595 [AngbandSoundCatalog sharedSounds].enabled = NO;
5599 frames_per_second = [defs integerForKey:AngbandFrameRateDefaultsKey];
5603 setDefaultFont:[NSFont fontWithName:[defs valueForKey:@"FontName-0"]
5604 size:[defs floatForKey:@"FontSize-0"]]];
5605 if (! [AngbandContext defaultFont])
5607 setDefaultFont:[NSFont fontWithName:@"Menlo" size:13.]];
5611 * Play sound effects asynchronously. Select a sound from any available
5612 * for the required event, and bridge to Cocoa to play it.
5614 static void play_sound(int event)
5616 [[AngbandSoundCatalog sharedSounds] playSound:event];
5620 * Allocate the primary Angband terminal and activate it. Allocate the other
5621 * Angband terminals.
5623 static void init_windows(void)
5625 /* Create the primary window */
5626 term *primary = term_data_link(0);
5628 /* Prepare to create any additional windows */
5629 for (int i = 1; i < ANGBAND_TERM_MAX; i++) {
5633 /* Activate the primary term */
5634 Term_activate(primary);
5638 * ------------------------------------------------------------------------
5640 * ------------------------------------------------------------------------ */
5642 @implementation AngbandAppDelegate
5644 @synthesize graphicsMenu=_graphicsMenu;
5645 @synthesize commandMenu=_commandMenu;
5646 @synthesize commandMenuTagMap=_commandMenuTagMap;
5648 - (IBAction)newGame:sender
5650 /* Game is in progress */
5651 game_in_progress = TRUE;
5655 - (IBAction)editFont:sender
5657 NSFontPanel *panel = [NSFontPanel sharedFontPanel];
5658 NSFont *termFont = [AngbandContext defaultFont];
5661 for (i=0; i < ANGBAND_TERM_MAX; i++) {
5662 AngbandContext *context =
5663 (__bridge AngbandContext*) (angband_term[i]->data);
5664 if ([context isKeyWindow]) {
5665 termFont = [context angbandViewFont];
5670 [panel setPanelFont:termFont isMultiple:NO];
5671 [panel orderFront:self];
5675 * Implement NSObject's changeFont() method to receive a notification about the
5676 * changed font. Note that, as of 10.14, changeFont() is deprecated in
5677 * NSObject - it will be removed at some point and the application delegate
5678 * will have to be declared as implementing the NSFontChanging protocol.
5680 - (void)changeFont:(id)sender
5683 for (mainTerm=0; mainTerm < ANGBAND_TERM_MAX; mainTerm++) {
5684 AngbandContext *context =
5685 (__bridge AngbandContext*) (angband_term[mainTerm]->data);
5686 if ([context isKeyWindow]) {
5691 /* Bug #1709: Only change font for angband windows */
5692 if (mainTerm == ANGBAND_TERM_MAX) return;
5694 NSFont *oldFont = [AngbandContext defaultFont];
5695 NSFont *newFont = [sender convertFont:oldFont];
5696 if (! newFont) return; /*paranoia */
5698 /* Store as the default font if we changed the first term */
5699 if (mainTerm == 0) {
5700 [AngbandContext setDefaultFont:newFont];
5703 /* Record it in the preferences */
5704 NSUserDefaults *defs = [NSUserDefaults angbandDefaults];
5705 [defs setValue:[newFont fontName]
5706 forKey:[NSString stringWithFormat:@"FontName-%d", mainTerm]];
5707 [defs setFloat:[newFont pointSize]
5708 forKey:[NSString stringWithFormat:@"FontSize-%d", mainTerm]];
5710 NSDisableScreenUpdates();
5713 AngbandContext *angbandContext =
5714 (__bridge AngbandContext*) (angband_term[mainTerm]->data);
5715 [(id)angbandContext setSelectionFont:newFont adjustTerminal: YES];
5717 NSEnableScreenUpdates();
5719 if (mainTerm != 0 || ! redraw_for_tiles_or_term0_font()) {
5720 [(id)angbandContext requestRedraw];
5724 - (IBAction)openGame:sender
5727 BOOL selectedSomething = NO;
5730 /* Get where we think the save files are */
5731 NSURL *startingDirectoryURL =
5732 [NSURL fileURLWithPath:[NSString stringWithCString:ANGBAND_DIR_SAVE encoding:NSASCIIStringEncoding]
5735 /* Set up an open panel */
5736 NSOpenPanel* panel = [NSOpenPanel openPanel];
5737 [panel setCanChooseFiles:YES];
5738 [panel setCanChooseDirectories:NO];
5739 [panel setResolvesAliases:YES];
5740 [panel setAllowsMultipleSelection:NO];
5741 [panel setTreatsFilePackagesAsDirectories:YES];
5742 [panel setDirectoryURL:startingDirectoryURL];
5745 panelResult = [panel runModal];
5746 if (panelResult == NSOKButton)
5748 NSArray* fileURLs = [panel URLs];
5749 if ([fileURLs count] > 0 && [[fileURLs objectAtIndex:0] isFileURL])
5751 NSURL* savefileURL = (NSURL *)[fileURLs objectAtIndex:0];
5753 * The path property doesn't do the right thing except for
5754 * URLs with the file scheme. We had
5755 * getFileSystemRepresentation here before, but that wasn't
5756 * introduced until OS X 10.9.
5758 selectedSomething = [[savefileURL path]
5760 maxLength:sizeof savefile
5761 encoding:NSMacOSRomanStringEncoding];
5765 if (selectedSomething)
5767 /* Remember this so we can select it by default next time */
5768 record_current_savefile();
5770 /* Game is in progress */
5771 game_in_progress = TRUE;
5776 - (IBAction)saveGame:sender
5778 /* Hack -- Forget messages */
5782 do_cmd_save_game(FALSE);
5785 * Record the current save file so we can select it by default next time.
5786 * It's a little sketchy that this only happens when we save through the
5787 * menu; ideally game-triggered saves would trigger it too.
5789 record_current_savefile();
5793 * Entry point for initializing Angband
5798 /* Hooks in some "z-util.c" hooks */
5799 plog_aux = hook_plog;
5800 quit_aux = hook_quit;
5802 /* Initialize file paths */
5803 prepare_paths_and_directories();
5805 /* Note the "system" */
5806 ANGBAND_SYS = "coc";
5808 /* Load possible graphics modes */
5809 init_graphics_modes();
5811 /* Load preferences */
5814 /* Prepare the windows */
5817 /* Set up game event handlers */
5818 /* init_display(); */
5820 /* Register the sound hook */
5821 /* sound_hook = play_sound; */
5823 /* Initialize some save file stuff */
5824 player_euid = geteuid();
5825 player_egid = getegid();
5827 /* Initialise game */
5830 /* We are now initialized */
5833 /* Handle "open_when_ready" */
5834 handle_open_when_ready();
5836 /* Handle pending events (most notably update) and flush input */
5840 * Prompt the user; assume the splash screen is 80 x 23 and position
5841 * relative to that rather than center based on the full size of the
5844 int message_row = 23;
5845 Term_erase(0, message_row, 255);
5848 "['ファイル' メニューから '新規' または '開く' を選択します]",
5849 message_row, (80 - 59) / 2
5851 "[Choose 'New' or 'Open' from the 'File' menu]",
5852 message_row, (80 - 45) / 2
5858 while (!game_in_progress) {
5860 NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantFuture] inMode:NSDefaultRunLoopMode dequeue:YES];
5861 if (event) [NSApp sendEvent:event];
5866 * Play a game -- "new_game" is set by "new", "open" or the open document
5867 * even handler as appropriate
5870 play_game(new_game);
5876 * Implement NSObject's validateMenuItem() method to override enabling or
5877 * disabling a menu item. Note that, as of 10.14, validateMenuItem() is
5878 * deprecated in NSObject - it will be removed at some point and the
5879 * application delegate will have to be declared as implementing the
5880 * NSMenuItemValidation protocol.
5882 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
5884 SEL sel = [menuItem action];
5885 NSInteger tag = [menuItem tag];
5887 if( tag >= AngbandWindowMenuItemTagBase && tag < AngbandWindowMenuItemTagBase + ANGBAND_TERM_MAX )
5889 if( tag == AngbandWindowMenuItemTagBase )
5891 /* The main window should always be available and visible */
5897 * Another window is only usable after Term_init_cocoa() has
5898 * been called for it. For Angband, if window_flag[i] is nonzero
5899 * then that has happened for window i. For Hengband, that is
5900 * not the case so also test angband_term[i]->data.
5902 NSInteger subwindowNumber = tag - AngbandWindowMenuItemTagBase;
5903 return (angband_term[subwindowNumber]->data != 0
5904 && window_flag[subwindowNumber] > 0);
5910 if (sel == @selector(newGame:))
5912 return ! game_in_progress;
5914 else if (sel == @selector(editFont:))
5918 else if (sel == @selector(openGame:))
5920 return ! game_in_progress;
5922 else if (sel == @selector(setRefreshRate:) &&
5923 [[menuItem parentItem] tag] == 150)
5925 NSInteger fps = [[NSUserDefaults standardUserDefaults] integerForKey:AngbandFrameRateDefaultsKey];
5926 [menuItem setState: ([menuItem tag] == fps)];
5929 else if( sel == @selector(setGraphicsMode:) )
5931 NSInteger requestedGraphicsMode = [[NSUserDefaults standardUserDefaults] integerForKey:AngbandGraphicsDefaultsKey];
5932 [menuItem setState: (tag == requestedGraphicsMode)];
5935 else if( sel == @selector(toggleSound:) )
5937 BOOL is_on = [[NSUserDefaults standardUserDefaults]
5938 boolForKey:AngbandSoundDefaultsKey];
5940 [menuItem setState: ((is_on) ? NSOnState : NSOffState)];
5943 else if (sel == @selector(toggleWideTiles:)) {
5944 BOOL is_on = [[NSUserDefaults standardUserDefaults]
5945 boolForKey:AngbandBigTileDefaultsKey];
5947 [menuItem setState: ((is_on) ? NSOnState : NSOffState)];
5950 else if( sel == @selector(sendAngbandCommand:) ||
5951 sel == @selector(saveGame:) )
5954 * we only want to be able to send commands during an active game
5955 * after the birth screens
5957 return !!game_in_progress && character_generated;
5963 - (IBAction)setRefreshRate:(NSMenuItem *)menuItem
5965 frames_per_second = [menuItem tag];
5966 [[NSUserDefaults angbandDefaults] setInteger:frames_per_second forKey:AngbandFrameRateDefaultsKey];
5969 - (void)setGraphicsMode:(NSMenuItem *)sender
5971 /* We stashed the graphics mode ID in the menu item's tag */
5972 graf_mode_req = [sender tag];
5974 /* Stash it in UserDefaults */
5975 [[NSUserDefaults angbandDefaults] setInteger:graf_mode_req forKey:AngbandGraphicsDefaultsKey];
5977 if (! graphics_will_be_enabled()) {
5979 arg_bigtile = FALSE;
5981 } else if ([[NSUserDefaults angbandDefaults] boolForKey:AngbandBigTileDefaultsKey] == YES &&
5986 if (arg_bigtile != use_bigtile) {
5987 Term_activate(angband_term[0]);
5988 Term_resize(angband_term[0]->wid, angband_term[0]->hgt);
5990 redraw_for_tiles_or_term0_font();
5993 - (void)selectWindow: (id)sender
5995 NSInteger subwindowNumber =
5996 [(NSMenuItem *)sender tag] - AngbandWindowMenuItemTagBase;
5997 AngbandContext *context =
5998 (__bridge AngbandContext*) (angband_term[subwindowNumber]->data);
5999 [context.primaryWindow makeKeyAndOrderFront: self];
6000 [context saveWindowVisibleToDefaults: YES];
6003 - (IBAction) toggleSound: (NSMenuItem *) sender
6005 BOOL is_on = (sender.state == NSOnState);
6007 /* Toggle the state and update the Angband global and preferences. */
6009 sender.state = NSOffState;
6011 [AngbandSoundCatalog sharedSounds].enabled = NO;
6013 sender.state = NSOnState;
6015 [AngbandSoundCatalog sharedSounds].enabled = YES;
6017 [[NSUserDefaults angbandDefaults] setBool:(! is_on)
6018 forKey:AngbandSoundDefaultsKey];
6021 - (IBAction)toggleWideTiles:(NSMenuItem *) sender
6023 BOOL is_on = (sender.state == NSOnState);
6025 /* Toggle the state and update the Angband globals and preferences. */
6026 sender.state = (is_on) ? NSOffState : NSOnState;
6027 [[NSUserDefaults angbandDefaults] setBool:(! is_on)
6028 forKey:AngbandBigTileDefaultsKey];
6029 if (graphics_are_enabled()) {
6030 arg_bigtile = (is_on) ? FALSE : TRUE;
6031 if (arg_bigtile != use_bigtile) {
6032 Term_activate(angband_term[0]);
6033 Term_resize(angband_term[0]->wid, angband_term[0]->hgt);
6034 redraw_for_tiles_or_term0_font();
6039 - (void)prepareWindowsMenu
6043 * Get the window menu with default items and add a separator and
6044 * item for the main window.
6046 NSMenu *windowsMenu = [[NSApplication sharedApplication] windowsMenu];
6047 [windowsMenu addItem: [NSMenuItem separatorItem]];
6049 NSString *title1 = [NSString stringWithCString:angband_term_name[0]
6051 encoding:NSJapaneseEUCStringEncoding
6053 encoding:NSMacOSRomanStringEncoding
6056 NSMenuItem *angbandItem = [[NSMenuItem alloc] initWithTitle:title1 action: @selector(selectWindow:) keyEquivalent: @"0"];
6057 [angbandItem setTarget: self];
6058 [angbandItem setTag: AngbandWindowMenuItemTagBase];
6059 [windowsMenu addItem: angbandItem];
6061 /* Add items for the additional term windows */
6062 for( NSInteger i = 1; i < ANGBAND_TERM_MAX; i++ )
6064 NSString *title = [NSString stringWithCString:angband_term_name[i]
6066 encoding:NSJapaneseEUCStringEncoding
6068 encoding:NSMacOSRomanStringEncoding
6071 NSString *keyEquivalent =
6072 [NSString stringWithFormat: @"%ld", (long)i];
6073 NSMenuItem *windowItem =
6074 [[NSMenuItem alloc] initWithTitle: title
6075 action: @selector(selectWindow:)
6076 keyEquivalent: keyEquivalent];
6077 [windowItem setTarget: self];
6078 [windowItem setTag: AngbandWindowMenuItemTagBase + i];
6079 [windowsMenu addItem: windowItem];
6085 * Send a command to Angband via a menu item. This places the appropriate key
6086 * down events into the queue so that it seems like the user pressed them
6087 * (instead of trying to use the term directly).
6089 - (void)sendAngbandCommand: (id)sender
6091 NSMenuItem *menuItem = (NSMenuItem *)sender;
6092 NSString *command = [self.commandMenuTagMap objectForKey: [NSNumber numberWithInteger: [menuItem tag]]];
6093 AngbandContext* context =
6094 (__bridge AngbandContext*) (angband_term[0]->data);
6095 NSInteger windowNumber = [context.primaryWindow windowNumber];
6097 /* Send a \ to bypass keymaps */
6098 NSEvent *escape = [NSEvent keyEventWithType: NSKeyDown
6099 location: NSZeroPoint
6102 windowNumber: windowNumber
6105 charactersIgnoringModifiers: @"\\"
6108 [[NSApplication sharedApplication] postEvent: escape atStart: NO];
6110 /* Send the actual command (from the original command set) */
6111 NSEvent *keyDown = [NSEvent keyEventWithType: NSKeyDown
6112 location: NSZeroPoint
6115 windowNumber: windowNumber
6118 charactersIgnoringModifiers: command
6121 [[NSApplication sharedApplication] postEvent: keyDown atStart: NO];
6125 * Set up the command menu dynamically, based on CommandMenu.plist.
6127 - (void)prepareCommandMenu
6130 NSString *commandMenuPath =
6131 [[NSBundle mainBundle] pathForResource: @"CommandMenu"
6133 NSArray *commandMenuItems =
6134 [[NSArray alloc] initWithContentsOfFile: commandMenuPath];
6135 NSMutableDictionary *angbandCommands =
6136 [[NSMutableDictionary alloc] init];
6137 NSString *tblname = @"CommandMenu";
6138 NSInteger tagOffset = 0;
6140 for( NSDictionary *item in commandMenuItems )
6142 BOOL useShiftModifier =
6143 [[item valueForKey: @"ShiftModifier"] boolValue];
6144 BOOL useOptionModifier =
6145 [[item valueForKey: @"OptionModifier"] boolValue];
6146 NSUInteger keyModifiers = NSCommandKeyMask;
6147 keyModifiers |= (useShiftModifier) ? NSShiftKeyMask : 0;
6148 keyModifiers |= (useOptionModifier) ? NSAlternateKeyMask : 0;
6150 NSString *lookup = [item valueForKey: @"Title"];
6151 NSString *title = NSLocalizedStringWithDefaultValue(
6152 lookup, tblname, [NSBundle mainBundle], lookup, @"");
6153 NSString *key = [item valueForKey: @"KeyEquivalent"];
6154 NSMenuItem *menuItem =
6155 [[NSMenuItem alloc] initWithTitle: title
6156 action: @selector(sendAngbandCommand:)
6157 keyEquivalent: key];
6158 [menuItem setTarget: self];
6159 [menuItem setKeyEquivalentModifierMask: keyModifiers];
6160 [menuItem setTag: AngbandCommandMenuItemTagBase + tagOffset];
6161 [self.commandMenu addItem: menuItem];
6163 NSString *angbandCommand = [item valueForKey: @"AngbandCommand"];
6164 [angbandCommands setObject: angbandCommand
6165 forKey: [NSNumber numberWithInteger: [menuItem tag]]];
6169 self.commandMenuTagMap = [[NSDictionary alloc]
6170 initWithDictionary: angbandCommands];
6174 - (void)awakeFromNib
6176 [super awakeFromNib];
6178 [self prepareWindowsMenu];
6179 [self prepareCommandMenu];
6182 - (void)applicationDidFinishLaunching:sender
6187 * Once beginGame finished, the game is over - that's how Angband works,
6188 * and we should quit
6190 game_is_finished = TRUE;
6191 [NSApp terminate:self];
6194 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
6196 if (p_ptr->playing == FALSE || game_is_finished == TRUE)
6198 quit_when_ready = true;
6199 return NSTerminateNow;
6201 else if (! inkey_flag)
6203 /* For compatibility with other ports, do not quit in this case */
6204 return NSTerminateCancel;
6209 /* player->upkeep->playing = FALSE; */
6212 * Post an escape event so that we can return from our get-key-event
6215 wakeup_event_loop();
6216 quit_when_ready = true;
6218 * Must return Cancel, not Later, because we need to get out of the
6219 * run loop and back to Angband's loop
6221 return NSTerminateCancel;
6226 * Dynamically build the Graphics menu
6228 - (void)menuNeedsUpdate:(NSMenu *)menu {
6230 /* Only the graphics menu is dynamic */
6231 if (! [menu isEqual:self.graphicsMenu])
6235 * If it's non-empty, then we've already built it. Currently graphics modes
6236 * won't change once created; if they ever can we can remove this check.
6237 * Note that the check mark does change, but that's handled in
6238 * validateMenuItem: instead of menuNeedsUpdate:
6240 if ([menu numberOfItems] > 0)
6243 /* This is the action for all these menu items */
6244 SEL action = @selector(setGraphicsMode:);
6246 /* Add an initial Classic ASCII menu item */
6247 NSString *tblname = @"GraphicsMenu";
6248 NSString *key = @"Classic ASCII";
6249 NSString *title = NSLocalizedStringWithDefaultValue(
6250 key, tblname, [NSBundle mainBundle], key, @"");
6251 NSMenuItem *classicItem = [menu addItemWithTitle:title action:action keyEquivalent:@""];
6252 [classicItem setTag:GRAPHICS_NONE];
6254 /* Walk through the list of graphics modes */
6255 if (graphics_modes) {
6258 for (i=0; graphics_modes[i].pNext; i++)
6260 const graphics_mode *graf = &graphics_modes[i];
6262 if (graf->grafID == GRAPHICS_NONE) {
6266 * Make the title. NSMenuItem throws on a nil title, so ensure it's
6269 key = [[NSString alloc] initWithUTF8String:graf->menuname];
6270 title = NSLocalizedStringWithDefaultValue(
6271 key, tblname, [NSBundle mainBundle], key, @"");
6274 NSMenuItem *item = [menu addItemWithTitle:title action:action keyEquivalent:@""];
6275 [item setTag:graf->grafID];
6281 * Delegate method that gets called if we're asked to open a file.
6283 - (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
6285 /* Can't open a file once we've started */
6286 if (game_in_progress) {
6287 [[NSApplication sharedApplication]
6288 replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
6292 /* We can only open one file. Use the last one. */
6293 NSString *file = [filenames lastObject];
6295 [[NSApplication sharedApplication]
6296 replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
6300 /* Put it in savefile */
6301 if (! [file getFileSystemRepresentation:savefile maxLength:sizeof savefile]) {
6302 [[NSApplication sharedApplication]
6303 replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
6307 game_in_progress = TRUE;
6310 * Wake us up in case this arrives while we're sitting at the Welcome
6313 wakeup_event_loop();
6315 [[NSApplication sharedApplication]
6316 replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
6321 int main(int argc, char* argv[])
6323 NSApplicationMain(argc, (void*)argv);
6327 #endif /* MACINTOSH || MACH_O_COCOA */