OSDN Git Service

Adjusted logic in Term_xtra_cocoa_fresh() to support big tile mode.
[hengbandforosx/hengbandosx.git] / src / main-cocoa.m
1 /**
2  * \file main-cocoa.m
3  * \brief OS X front end
4  *
5  * Copyright (c) 2011 Peter Ammon
6  *
7  * This work is free software; you can redistribute it and/or modify it
8  * under the terms of either:
9  *
10  * a) the GNU General Public License as published by the Free Software
11  *    Foundation, version 2, or
12  *
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.
17  */
18
19 #include "angband.h"
20 /* This is not included in angband.h in Hengband. */
21 #include "grafmode.h"
22
23 #if defined(MACH_O_COCOA)
24
25 /* Mac headers */
26 #include <cocoa/AppDelegate.h>
27 //#include <Carbon/Carbon.h> /* For keycodes */
28 /* Hack - keycodes to enable compiling in macOS 10.14 */
29 #define kVK_Return 0x24
30 #define kVK_Tab    0x30
31 #define kVK_Delete 0x33
32 #define kVK_Escape 0x35
33 #define kVK_ANSI_KeypadEnter 0x4C
34
35 static NSString * const AngbandDirectoryNameLib = @"lib";
36 static NSString * const AngbandDirectoryNameBase = @"Hengband";
37
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 AngbandFrameRateDefaultsKey = @"FramesPerSecond";
45 static NSString * const AngbandSoundDefaultsKey = @"AllowSound";
46 static NSInteger const AngbandWindowMenuItemTagBase = 1000;
47 static NSInteger const AngbandCommandMenuItemTagBase = 2000;
48
49 /* We can blit to a large layer or image and then scale it down during live
50  * resize, which makes resizing much faster, at the cost of some image quality
51  * during resizing */
52 #ifndef USE_LIVE_RESIZE_CACHE
53 # define USE_LIVE_RESIZE_CACHE 1
54 #endif
55
56 /* Global defines etc from Angband 3.5-dev - NRM */
57 #define ANGBAND_TERM_MAX 8
58
59 static bool new_game = TRUE;
60
61 #define MAX_COLORS 256
62 #define MSG_MAX SOUND_MAX
63
64 /* End Angband stuff - NRM */
65
66 /* Application defined event numbers */
67 enum
68 {
69     AngbandEventWakeup = 1
70 };
71
72 /* Redeclare some 10.7 constants and methods so we can build on 10.6 */
73 enum
74 {
75     Angband_NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7,
76     Angband_NSWindowCollectionBehaviorFullScreenAuxiliary = 1 << 8
77 };
78
79 @interface NSWindow (AngbandLionRedeclares)
80 - (void)setRestorable:(BOOL)flag;
81 @end
82
83 /* Delay handling of pre-emptive "quit" event */
84 static BOOL quit_when_ready = FALSE;
85
86 /* Set to indicate the game is over and we can quit without delay */
87 static Boolean game_is_finished = FALSE;
88
89 /* Our frames per second (e.g. 60). A value of 0 means unthrottled. */
90 static int frames_per_second;
91
92 /* Function to get the default font */
93 static NSFont *default_font;
94
95 @class AngbandView;
96
97 /*
98  * To handle fonts where an individual glyph's bounding box can extend into
99  * neighboring columns, Term_curs_cocoa(), Term_pict_cocoa(),
100  * Term_text_cocoa(), and Term_wipe_cocoa() merely record what needs to be
101  * done with the actual drawing happening in response to the notification to
102  * flush all rows, the TERM_XTRA_FRESH case in Term_xtra_cocoa().  Can not use
103  * the TERM_XTRA_FROSH notification (the per-row flush), since with a software
104  * cursor, there are calls to Term_pict_cocoa(), Term_text_cocoa(), or
105  * Term_wipe_cocoa() to take care of the old cursor position which are not
106  * followed by a row flush.
107  */
108 enum PendingCellChangeType {
109     CELL_CHANGE_NONE = 0,
110     CELL_CHANGE_WIPE,
111     CELL_CHANGE_TEXT,
112     CELL_CHANGE_PICT
113 };
114 struct PendingCellChange {
115     /*
116      * For text rendering, stores the character as a wchar_t; for tile
117      * rendering, stores the column in the tile set for the source tile.
118      */
119     union { wchar_t w; char c; } c;
120     /*
121      * For text rendering, stores the color; for tile rendering, stores the
122      * row in the tile set for the source tile.
123      */
124     TERM_COLOR a;
125     /*
126      * For text rendering, is one if wc is a character that takes up two
127      * columns (i.e. Japanese kanji); otherwise it is zero.  For tile
128      * rendering, Stores the column in the tile set for the terrain tile. */
129     char tcol;
130     /*
131      * For tile rendering, stores the row in the tile set for the
132      * terrain tile.
133      */
134     TERM_COLOR trow;
135     enum PendingCellChangeType change_type;
136 };
137
138 struct PendingRowChange
139 {
140     /*
141      * These are the first and last columns, inclusive, that have been
142      * modified.  xmin is greater than xmax if no changes have been made.
143      */
144     int xmin, xmax;
145     /*
146      * This points to storage for a number of elements equal to the number
147      * of columns (implicitly gotten from the enclosing AngbandContext).
148      */
149     struct PendingCellChange* cell_changes;
150 };
151
152 static struct PendingRowChange* create_row_change(int ncol)
153 {
154     struct PendingRowChange* prc =
155         (struct PendingRowChange*) malloc(sizeof(struct PendingRowChange));
156     struct PendingCellChange* pcc = (struct PendingCellChange*)
157         malloc(ncol * sizeof(struct PendingCellChange));
158     int i;
159
160     if (prc == 0 || pcc == 0) {
161         if (pcc != 0) {
162             free(pcc);
163         }
164         if (prc != 0) {
165             free(prc);
166         }
167         return 0;
168     }
169
170     prc->xmin = ncol;
171     prc->xmax = -1;
172     prc->cell_changes = pcc;
173     for (i = 0; i < ncol; ++i) {
174         pcc[i].change_type = CELL_CHANGE_NONE;
175     }
176     return prc;
177 }
178
179
180 static void destroy_row_change(struct PendingRowChange* prc)
181 {
182     if (prc != 0) {
183         if (prc->cell_changes != 0) {
184             free(prc->cell_changes);
185         }
186         free(prc);
187     }
188 }
189
190
191 struct PendingChanges
192 {
193     /* Hold the number of rows specified at creation. */
194     int nrow;
195     /*
196      * Hold the position set for the software cursor.  Use negative indices
197      * to indicate that the cursor is not displayed.
198      */
199     int xcurs, ycurs;
200     /* Is nonzero if the cursor should be drawn at double the tile width. */
201     int bigcurs;
202     /* Record whether the changes include any text, picts, or wipes. */
203     int has_text, has_pict, has_wipe;
204     /*
205      * These are the first and last rows, inclusive, that have been
206      * modified.  ymin is greater than ymax if no changes have been made.
207      */
208     int ymin, ymax;
209     /*
210      * This is an array of pointers to the changes.  The number of elements
211      * is the number of rows.  An element will be a NULL pointer if no
212      * modifications have been made to the row.
213      */
214     struct PendingRowChange** rows;
215 };
216
217
218 static struct PendingChanges* create_pending_changes(int ncol, int nrow)
219 {
220     struct PendingChanges* pc =
221         (struct PendingChanges*) malloc(sizeof(struct PendingChanges));
222     struct PendingRowChange** pprc = (struct PendingRowChange**)
223         malloc(nrow * sizeof(struct PendingRowChange*));
224     int i;
225
226     if (pc == 0 || pprc == 0) {
227         if (pprc != 0) {
228             free(pprc);
229         }
230         if (pc != 0) {
231             free(pc);
232         }
233         return 0;
234     }
235
236     pc->nrow = nrow;
237     pc->xcurs = -1;
238     pc->ycurs = -1;
239     pc->bigcurs = 0;
240     pc->has_text = 0;
241     pc->has_pict = 0;
242     pc->has_wipe = 0;
243     pc->ymin = nrow;
244     pc->ymax = -1;
245     pc->rows = pprc;
246     for (i = 0; i < nrow; ++i) {
247         pprc[i] = 0;
248     }
249     return pc;
250 }
251
252
253 static void destroy_pending_changes(struct PendingChanges* pc)
254 {
255     if (pc != 0) {
256         if (pc->rows != 0) {
257             int i;
258
259             for (i = 0; i < pc->nrow; ++i) {
260                 if (pc->rows[i] != 0) {
261                     destroy_row_change(pc->rows[i]);
262                 }
263             }
264             free(pc->rows);
265         }
266         free(pc);
267     }
268 }
269
270
271 static void clear_pending_changes(struct PendingChanges* pc)
272 {
273     pc->xcurs = -1;
274     pc->ycurs = -1;
275     pc->bigcurs = 0;
276     pc->has_text = 0;
277     pc->has_pict = 0;
278     pc->has_wipe = 0;
279     pc->ymin = pc->nrow;
280     pc->ymax = -1;
281     if (pc->rows != 0) {
282         int i;
283
284         for (i = 0; i < pc->nrow; ++i) {
285             if (pc->rows[i] != 0) {
286                 destroy_row_change(pc->rows[i]);
287                 pc->rows[i] = 0;
288             }
289         }
290     }
291 }
292
293
294 /* Return zero if successful; otherwise return a nonzero value. */
295 static int resize_pending_changes(struct PendingChanges* pc, int nrow)
296 {
297     struct PendingRowChange** pprc;
298     int i;
299
300     if (pc == 0) {
301         return 1;
302     }
303
304     pprc = (struct PendingRowChange**)
305         malloc(nrow * sizeof(struct PendingRowChange*));
306     if (pprc == 0) {
307         return 1;
308     }
309     for (i = 0; i < nrow; ++i) {
310         pprc[i] = 0;
311     }
312
313     if (pc->rows != 0) {
314         for (i = 0; i < pc->nrow; ++i) {
315             if (pc->rows[i] != 0) {
316                 destroy_row_change(pc->rows[i]);
317             }
318         }
319         free(pc->rows);
320     }
321     pc->nrow = nrow;
322     pc->xcurs = -1;
323     pc->ycurs = -1;
324     pc->bigcurs = 0;
325     pc->has_text = 0;
326     pc->has_pict = 0;
327     pc->has_wipe = 0;
328     pc->ymin = nrow;
329     pc->ymax = -1;
330     pc->rows = pprc;
331     return 0;
332 }
333
334
335 /* The max number of glyphs we support.  Currently this only affects
336  * updateGlyphInfo() for the calculation of the tile size, fontAscender,
337  * fontDescender, ncol_pre, and ncol_post (the glyphArray and glyphWidths
338  * members of AngbandContext are only used in updateGlyphInfo()).  The
339  * rendering in drawWChar will work for glyphs not in updateGlyphInfo()'s
340  * set, and that is used for rendering Japanese characters.
341  */
342 #define GLYPH_COUNT 256
343
344 /* An AngbandContext represents a logical Term (i.e. what Angband thinks is
345  * a window). This typically maps to one NSView, but may map to more than one
346  * NSView (e.g. the Test and real screen saver view). */
347 @interface AngbandContext : NSObject <NSWindowDelegate>
348 {
349 @public
350     
351     /* The Angband term */
352     term *terminal;
353     
354     /* Column and row cont, by default 80 x 24 */
355     size_t cols;
356     size_t rows;
357     
358     /* The size of the border between the window edge and the contents */
359     NSSize borderSize;
360     
361     /* Our array of views */
362     NSMutableArray *angbandViews;
363     
364     /* The buffered image */
365     CGLayerRef angbandLayer;
366
367     /* The font of this context */
368     NSFont *angbandViewFont;
369     
370     /* If this context owns a window, here it is */
371     NSWindow *primaryWindow;
372     
373     /* "Glyph info": an array of the CGGlyphs and their widths corresponding to
374          * the above font. */
375     CGGlyph glyphArray[GLYPH_COUNT];
376     CGFloat glyphWidths[GLYPH_COUNT];
377     
378     /* The size of one tile */
379     NSSize tileSize;
380     
381     /* Font's ascender and descender */
382     CGFloat fontAscender, fontDescender;
383     
384     /* Whether we are currently in live resize, which affects how big we render
385          * our image */
386     int inLiveResize;
387     
388     /* Last time we drew, so we can throttle drawing */
389     CFAbsoluteTime lastRefreshTime;
390
391     struct PendingChanges* changes;
392     /*
393      * These are the number of columns before or after, respectively, a text
394      * change that may need to be redrawn.
395      */
396     int ncol_pre, ncol_post;
397
398     /* Flags whether or not a fullscreen transition is in progress. */
399     BOOL in_fullscreen_transition;
400
401 @private
402
403     BOOL _hasSubwindowFlags;
404     BOOL _windowVisibilityChecked;
405 }
406
407 @property (nonatomic, assign) BOOL hasSubwindowFlags;
408 @property (nonatomic, assign) BOOL windowVisibilityChecked;
409
410 - (void)drawRect:(NSRect)rect inView:(NSView *)view;
411
412 /* Called at initialization to set the term */
413 - (void)setTerm:(term *)t;
414
415 /* Called when the context is going down. */
416 - (void)dispose;
417
418 /* Returns the size of the image. */
419 - (NSSize)imageSize;
420
421 /* Return the rect for a tile at given coordinates. */
422 - (NSRect)rectInImageForTileAtX:(int)x Y:(int)y;
423
424 /* Draw the given wide character into the given tile rect. */
425 - (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile context:(CGContextRef)ctx;
426
427 /* Locks focus on the Angband image, and scales the CTM appropriately. */
428 - (CGContextRef)lockFocus;
429
430 /* Locks focus on the Angband image but does NOT scale the CTM. Appropriate
431  * for drawing hairlines. */
432 - (CGContextRef)lockFocusUnscaled;
433
434 /* Unlocks focus. */
435 - (void)unlockFocus;
436
437 /* Returns the primary window for this angband context, creating it if
438  * necessary */
439 - (NSWindow *)makePrimaryWindow;
440
441 /* Called to add a new Angband view */
442 - (void)addAngbandView:(AngbandView *)view;
443
444 /* Make the context aware that one of its views changed size */
445 - (void)angbandViewDidScale:(AngbandView *)view;
446
447 /* Handle becoming the main window */
448 - (void)windowDidBecomeMain:(NSNotification *)notification;
449
450 /* Return whether the context's primary window is ordered in or not */
451 - (BOOL)isOrderedIn;
452
453 /* Return whether the context's primary window is key */
454 - (BOOL)isMainWindow;
455
456 /* Invalidate the whole image */
457 - (void)setNeedsDisplay:(BOOL)val;
458
459 /* Invalidate part of the image, with the rect expressed in base coordinates */
460 - (void)setNeedsDisplayInBaseRect:(NSRect)rect;
461
462 /* Display (flush) our Angband views */
463 - (void)displayIfNeeded;
464
465 /* Resize context to size of contentRect, and optionally save size to
466  * defaults */
467 - (void)resizeTerminalWithContentRect: (NSRect)contentRect saveToDefaults: (BOOL)saveToDefaults;
468
469 /*
470  * Change the minimum size for the window associated with the context.
471  * termIdx is the index for the terminal:  pass it so this function can be
472  * used when self->terminal has not yet been set.
473  */
474 - (void)setMinimumWindowSize:(int)termIdx;
475
476 /* Called from the view to indicate that it is starting or ending live resize */
477 - (void)viewWillStartLiveResize:(AngbandView *)view;
478 - (void)viewDidEndLiveResize:(AngbandView *)view;
479 - (void)saveWindowVisibleToDefaults: (BOOL)windowVisible;
480 - (BOOL)windowVisibleUsingDefaults;
481
482 /* Class methods */
483
484 /* Begins an Angband game. This is the entry point for starting off. */
485 + (void)beginGame;
486
487 /* Internal method */
488 - (AngbandView *)activeView;
489
490 @end
491
492 /**
493  * Generate a mask for the subwindow flags. The mask is just a safety check to
494  * make sure that our windows show and hide as expected.  This function allows
495  * for future changes to the set of flags without needed to update it here
496  * (unless the underlying types change).
497  */
498 u32b AngbandMaskForValidSubwindowFlags(void)
499 {
500     int windowFlagBits = sizeof(*(window_flag)) * CHAR_BIT;
501     int maxBits = MIN( 16, windowFlagBits );
502     u32b mask = 0;
503
504     for( int i = 0; i < maxBits; i++ )
505     {
506         if( window_flag_desc[i] != NULL )
507         {
508             mask |= (1 << i);
509         }
510     }
511
512     return mask;
513 }
514
515 /**
516  * Check for changes in the subwindow flags and update window visibility.
517  * This seems to be called for every user event, so we don't
518  * want to do any unnecessary hiding or showing of windows.
519  */
520 static void AngbandUpdateWindowVisibility(void)
521 {
522     /* Because this function is called frequently, we'll make the mask static.
523          * It doesn't change between calls, as the flags themselves are hardcoded */
524     static u32b validWindowFlagsMask = 0;
525
526     if( validWindowFlagsMask == 0 )
527     {
528         validWindowFlagsMask = AngbandMaskForValidSubwindowFlags();
529     }
530
531     /* Loop through all of the subwindows and see if there is a change in the
532          * flags. If so, show or hide the corresponding window. We don't care about
533          * the flags themselves; we just want to know if any are set. */
534     for( int i = 1; i < ANGBAND_TERM_MAX; i++ )
535     {
536         AngbandContext *angbandContext = angband_term[i]->data;
537
538         if( angbandContext == nil )
539         {
540             continue;
541         }
542
543         /* This horrible mess of flags is so that we can try to maintain some
544                  * user visibility preference. This should allow the user a window and
545                  * have it stay closed between application launches. However, this
546                  * means that when a subwindow is turned on, it will no longer appear
547                  * automatically. Angband has no concept of user control over window
548                  * visibility, other than the subwindow flags. */
549         if( !angbandContext.windowVisibilityChecked )
550         {
551             if( [angbandContext windowVisibleUsingDefaults] )
552             {
553                 [angbandContext->primaryWindow orderFront: nil];
554                 angbandContext.windowVisibilityChecked = YES;
555             }
556             else
557             {
558                 [angbandContext->primaryWindow close];
559                 angbandContext.windowVisibilityChecked = NO;
560             }
561         }
562         else
563         {
564             BOOL termHasSubwindowFlags = ((window_flag[i] & validWindowFlagsMask) > 0);
565
566             if( angbandContext.hasSubwindowFlags && !termHasSubwindowFlags )
567             {
568                 [angbandContext->primaryWindow close];
569                 angbandContext.hasSubwindowFlags = NO;
570                 [angbandContext saveWindowVisibleToDefaults: NO];
571             }
572             else if( !angbandContext.hasSubwindowFlags && termHasSubwindowFlags )
573             {
574                 [angbandContext->primaryWindow orderFront: nil];
575                 angbandContext.hasSubwindowFlags = YES;
576                 [angbandContext saveWindowVisibleToDefaults: YES];
577             }
578         }
579     }
580
581     /* Make the main window key so that user events go to the right spot */
582     AngbandContext *mainWindow = angband_term[0]->data;
583     [mainWindow->primaryWindow makeKeyAndOrderFront: nil];
584 }
585
586 /**
587  * ------------------------------------------------------------------------
588  * Graphics support
589  * ------------------------------------------------------------------------ */
590
591 /**
592  * The tile image
593  */
594 static CGImageRef pict_image;
595
596 /**
597  * Numbers of rows and columns in a tileset,
598  * calculated by the PICT/PNG loading code
599  */
600 static int pict_cols = 0;
601 static int pict_rows = 0;
602
603 /**
604  * Requested graphics mode (as a grafID).
605  * The current mode is stored in current_graphics_mode.
606  */
607 static int graf_mode_req = 0;
608
609 /**
610  * Helper function to check the various ways that graphics can be enabled,
611  * guarding against NULL
612  */
613 static BOOL graphics_are_enabled(void)
614 {
615     return current_graphics_mode
616         && current_graphics_mode->grafID != GRAPHICS_NONE;
617 }
618
619 /**
620  * Hack -- game in progress
621  */
622 static Boolean game_in_progress = FALSE;
623
624
625 #pragma mark Prototypes
626 static void wakeup_event_loop(void);
627 static void hook_plog(const char *str);
628 static void hook_quit(const char * str);
629 static void load_prefs(void);
630 static void load_sounds(void);
631 static void init_windows(void);
632 static void handle_open_when_ready(void);
633 static void play_sound(int event);
634 static BOOL check_events(int wait);
635 static BOOL send_event(NSEvent *event);
636 static void record_current_savefile(void);
637 #ifdef JP
638 static wchar_t convert_two_byte_eucjp_to_utf16_native(const char *cp);
639 #endif
640
641 /**
642  * Available values for 'wait'
643  */
644 #define CHECK_EVENTS_DRAIN -1
645 #define CHECK_EVENTS_NO_WAIT    0
646 #define CHECK_EVENTS_WAIT 1
647
648
649 /**
650  * Note when "open"/"new" become valid
651  */
652 static bool initialized = FALSE;
653
654 /* Methods for getting the appropriate NSUserDefaults */
655 @interface NSUserDefaults (AngbandDefaults)
656 + (NSUserDefaults *)angbandDefaults;
657 @end
658
659 @implementation NSUserDefaults (AngbandDefaults)
660 + (NSUserDefaults *)angbandDefaults
661 {
662     return [NSUserDefaults standardUserDefaults];
663 }
664 @end
665
666 /* Methods for pulling images out of the Angband bundle (which may be separate
667  * from the current bundle in the case of a screensaver */
668 @interface NSImage (AngbandImages)
669 + (NSImage *)angbandImage:(NSString *)name;
670 @end
671
672 /* The NSView subclass that draws our Angband image */
673 @interface AngbandView : NSView
674 {
675     AngbandContext *angbandContext;
676 }
677
678 - (void)setAngbandContext:(AngbandContext *)context;
679 - (AngbandContext *)angbandContext;
680
681 @end
682
683 @implementation NSImage (AngbandImages)
684
685 /* Returns an image in the resource directoy of the bundle containing the
686  * Angband view class. */
687 + (NSImage *)angbandImage:(NSString *)name
688 {
689     NSBundle *bundle = [NSBundle bundleForClass:[AngbandView class]];
690     NSString *path = [bundle pathForImageResource:name];
691     NSImage *result;
692     if (path) result = [[[NSImage alloc] initByReferencingFile:path] autorelease];
693     else result = nil;
694     return result;
695 }
696
697 @end
698
699
700 @implementation AngbandContext
701
702 @synthesize hasSubwindowFlags=_hasSubwindowFlags;
703 @synthesize windowVisibilityChecked=_windowVisibilityChecked;
704
705 - (NSFont *)selectionFont
706 {
707     return angbandViewFont;
708 }
709
710 - (BOOL)useLiveResizeOptimization
711 {
712     /* If we have graphics turned off, text rendering is fast enough that we
713          * don't need to use a live resize optimization. */
714     return inLiveResize && graphics_are_enabled();
715 }
716
717 - (NSSize)baseSize
718 {
719     /* We round the base size down. If we round it up, I believe we may end up
720          * with pixels that nobody "owns" that may accumulate garbage. In general
721          * rounding down is harmless, because any lost pixels may be sopped up by
722          * the border. */
723     return NSMakeSize(floor(cols * tileSize.width + 2 * borderSize.width), floor(rows * tileSize.height + 2 * borderSize.height));
724 }
725
726 /* qsort-compatible compare function for CGSizes */
727 static int compare_advances(const void *ap, const void *bp)
728 {
729     const CGSize *a = ap, *b = bp;
730     return (a->width > b->width) - (a->width < b->width);
731 }
732
733 - (void)updateGlyphInfo
734 {
735     /* Update glyphArray and glyphWidths */
736     NSFont *screenFont = [angbandViewFont screenFont];
737
738     /* Generate a string containing each MacRoman character */
739     unsigned char latinString[GLYPH_COUNT];
740     size_t i;
741     for (i=0; i < GLYPH_COUNT; i++) latinString[i] = (unsigned char)i;
742     
743     /* Turn that into unichar. Angband uses ISO Latin 1. */
744     unichar unicharString[GLYPH_COUNT] = {0};
745     NSString *allCharsString = [[NSString alloc] initWithBytes:latinString length:sizeof latinString encoding:NSISOLatin1StringEncoding];
746     [allCharsString getCharacters:unicharString range:NSMakeRange(0, MIN(GLYPH_COUNT, [allCharsString length]))];
747     [allCharsString autorelease];
748     
749     /* Get glyphs */
750     memset(glyphArray, 0, sizeof glyphArray);
751     CTFontGetGlyphsForCharacters((CTFontRef)screenFont, unicharString, glyphArray, GLYPH_COUNT);
752     
753     /* Get advances. Record the max advance. */
754     CGSize advances[GLYPH_COUNT] = {};
755     CTFontGetAdvancesForGlyphs((CTFontRef)screenFont, kCTFontHorizontalOrientation, glyphArray, advances, GLYPH_COUNT);
756     for (i=0; i < GLYPH_COUNT; i++) {
757         glyphWidths[i] = advances[i].width;
758     }
759     
760     /* For good non-mono-font support, use the median advance. Start by sorting
761          * all advances. */
762     qsort(advances, GLYPH_COUNT, sizeof *advances, compare_advances);
763     
764     /* Skip over any initially empty run */
765     size_t startIdx;
766     for (startIdx = 0; startIdx < GLYPH_COUNT; startIdx++)
767     {
768         if (advances[startIdx].width > 0) break;
769     }
770     
771     /* Pick the center to find the median */
772     CGFloat medianAdvance = 0;
773     if (startIdx < GLYPH_COUNT)
774     {
775                 /* In case we have all zero advances for some reason */
776         medianAdvance = advances[(startIdx + GLYPH_COUNT)/2].width;
777     }
778     
779     /*
780      * Record the ascender and descender.  Some fonts, for instance DIN
781      * Condensed and Rockwell in 10.14, the ascent on '@' exceeds that
782      * reported by [screenFont ascender].  Get the overall bounding box
783      * for the glyphs and use that instead of the ascender and descender
784      * values if the bounding box result extends farther from the baseline.
785      */
786     CGRect bounds = CTFontGetBoundingRectsForGlyphs((CTFontRef) screenFont, kCTFontHorizontalOrientation, glyphArray, NULL, GLYPH_COUNT);
787     fontAscender = [screenFont ascender];
788     if (fontAscender < bounds.origin.y + bounds.size.height) {
789         fontAscender = bounds.origin.y + bounds.size.height;
790     }
791     fontDescender = [screenFont descender];
792     if (fontDescender > bounds.origin.y) {
793         fontDescender = bounds.origin.y;
794     }
795
796     /*
797      * Record the tile size.  Round both values up to have tile boundaries
798      * match pixel boundaries.
799      */
800     tileSize.width = ceil(medianAdvance);
801     tileSize.height = ceil(fontAscender - fontDescender);
802
803     /*
804      * Determine whether neighboring columns need to redrawn when a character
805      * changes.
806      */
807     CGRect boxes[GLYPH_COUNT] = {};
808     CGFloat beyond_right = 0.;
809     CGFloat beyond_left = 0.;
810     CTFontGetBoundingRectsForGlyphs(
811         (CTFontRef)screenFont,
812         kCTFontHorizontalOrientation,
813         glyphArray,
814         boxes,
815         GLYPH_COUNT);
816     for (i = 0; i < GLYPH_COUNT; i++) {
817         /* Account for the compression and offset used by drawWChar(). */
818         CGFloat compression, offset;
819         CGFloat v;
820
821         if (glyphWidths[i] <= tileSize.width) {
822             compression = 1.;
823             offset = 0.5 * (tileSize.width - glyphWidths[i]);
824         } else {
825             compression = tileSize.width / glyphWidths[i];
826             offset = 0.;
827         }
828         v = (offset + boxes[i].origin.x) * compression;
829         if (beyond_left > v) {
830             beyond_left = v;
831         }
832         v = (offset + boxes[i].origin.x + boxes[i].size.width) * compression;
833         if (beyond_right < v) {
834             beyond_right = v;
835         }
836     }
837     ncol_pre = ceil(-beyond_left / tileSize.width);
838     if (beyond_right > tileSize.width) {
839         ncol_post = ceil((beyond_right - tileSize.width) / tileSize.width);
840     } else {
841         ncol_post = 0;
842     }
843 }
844
845 - (void)updateImage
846 {
847     NSSize size = NSMakeSize(1, 1);
848     
849     AngbandView *activeView = [self activeView];
850     if (activeView)
851     {
852         /* If we are in live resize, draw as big as the screen, so we can scale
853                  * nicely to any size. If we are not in live resize, then use the
854                  * bounds of the active view. */
855         NSScreen *screen;
856         if ([self useLiveResizeOptimization] && (screen = [[activeView window] screen]) != NULL)
857         {
858             size = [screen frame].size;
859         }
860         else
861         {
862             size = [activeView bounds].size;
863         }
864     }
865
866     CGLayerRelease(angbandLayer);
867     
868     /* Use the highest monitor scale factor on the system to work out what
869      * scale to draw at - not the recommended method, but works where we
870      * can't easily get the monitor the current draw is occurring on. */
871     float angbandLayerScale = 1.0;
872     if ([[NSScreen mainScreen] respondsToSelector:@selector(backingScaleFactor)]) {
873         for (NSScreen *screen in [NSScreen screens]) {
874             angbandLayerScale = fmax(angbandLayerScale, [screen backingScaleFactor]);
875         }
876     }
877
878     /* Make a bitmap context as an example for our layer */
879     CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
880     CGContextRef exampleCtx = CGBitmapContextCreate(NULL, 1, 1, 8 /* bits per component */, 48 /* bytesPerRow */, cs, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host);
881     CGColorSpaceRelease(cs);
882
883     /* Create the layer at the appropriate size */
884     size.width = fmax(1, ceil(size.width * angbandLayerScale));
885     size.height = fmax(1, ceil(size.height * angbandLayerScale));
886     angbandLayer = CGLayerCreateWithContext(exampleCtx, *(CGSize *)&size, NULL);
887
888     CFRelease(exampleCtx);
889
890     /* Set the new context of the layer to draw at the correct scale */
891     CGContextRef ctx = CGLayerGetContext(angbandLayer);
892     CGContextScaleCTM(ctx, angbandLayerScale, angbandLayerScale);
893
894     [self lockFocus];
895     [[NSColor blackColor] set];
896     NSRectFill((NSRect){NSZeroPoint, [self baseSize]});
897     [self unlockFocus];
898 }
899
900 - (void)requestRedraw
901 {
902     if (! self->terminal) return;
903     
904     term *old = Term;
905     
906     /* Activate the term */
907     Term_activate(self->terminal);
908     
909     /* Redraw the contents */
910     Term_redraw();
911     
912     /* Flush the output */
913     Term_fresh();
914     
915     /* Restore the old term */
916     Term_activate(old);
917 }
918
919 - (void)setTerm:(term *)t
920 {
921     terminal = t;
922 }
923
924 - (void)viewWillStartLiveResize:(AngbandView *)view
925 {
926 #if USE_LIVE_RESIZE_CACHE
927     if (inLiveResize < INT_MAX) inLiveResize++;
928     else [NSException raise:NSInternalInconsistencyException format:@"inLiveResize overflow"];
929     
930     if (inLiveResize == 1 && graphics_are_enabled())
931     {
932         [self updateImage];
933         
934         [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
935         [self requestRedraw];
936     }
937 #endif
938 }
939
940 - (void)viewDidEndLiveResize:(AngbandView *)view
941 {
942 #if USE_LIVE_RESIZE_CACHE
943     if (inLiveResize > 0) inLiveResize--;
944     else [NSException raise:NSInternalInconsistencyException format:@"inLiveResize underflow"];
945     
946     if (inLiveResize == 0 && graphics_are_enabled())
947     {
948         [self updateImage];
949         
950         [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
951         [self requestRedraw];
952     }
953 #endif
954 }
955
956 /**
957  * If we're trying to limit ourselves to a certain number of frames per second,
958  * then compute how long it's been since we last drew, and then wait until the
959  * next frame has passed. */
960 - (void)throttle
961 {
962     if (frames_per_second > 0)
963     {
964         CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
965         CFTimeInterval timeSinceLastRefresh = now - lastRefreshTime;
966         CFTimeInterval timeUntilNextRefresh = (1. / (double)frames_per_second) - timeSinceLastRefresh;
967         
968         if (timeUntilNextRefresh > 0)
969         {
970             usleep((unsigned long)(timeUntilNextRefresh * 1000000.));
971         }
972     }
973     lastRefreshTime = CFAbsoluteTimeGetCurrent();
974 }
975
976 - (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile context:(CGContextRef)ctx
977 {
978     CGFloat tileOffsetY = fontAscender;
979     CGFloat tileOffsetX = 0.0;
980     NSFont *screenFont = [angbandViewFont screenFont];
981     UniChar unicharString[2] = {(UniChar)wchar, 0};
982
983     /* Get glyph and advance */
984     CGGlyph thisGlyphArray[1] = { 0 };
985     CGSize advances[1] = { { 0, 0 } };
986     CTFontGetGlyphsForCharacters((CTFontRef)screenFont, unicharString, thisGlyphArray, 1);
987     CGGlyph glyph = thisGlyphArray[0];
988     CTFontGetAdvancesForGlyphs((CTFontRef)screenFont, kCTFontHorizontalOrientation, thisGlyphArray, advances, 1);
989     CGSize advance = advances[0];
990     
991     /* If our font is not monospaced, our tile width is deliberately not big
992          * enough for every character. In that event, if our glyph is too wide, we
993          * need to compress it horizontally. Compute the compression ratio.
994          * 1.0 means no compression. */
995     double compressionRatio;
996     if (advance.width <= NSWidth(tile))
997     {
998         /* Our glyph fits, so we can just draw it, possibly with an offset */
999         compressionRatio = 1.0;
1000         tileOffsetX = (NSWidth(tile) - advance.width)/2;
1001     }
1002     else
1003     {
1004         /* Our glyph doesn't fit, so we'll have to compress it */
1005         compressionRatio = NSWidth(tile) / advance.width;
1006         tileOffsetX = 0;
1007     }
1008
1009     
1010     /* Now draw it */
1011     CGAffineTransform textMatrix = CGContextGetTextMatrix(ctx);
1012     CGFloat savedA = textMatrix.a;
1013
1014     /* Set the position */
1015     textMatrix.tx = tile.origin.x + tileOffsetX;
1016     textMatrix.ty = tile.origin.y + tileOffsetY;
1017
1018     /* Maybe squish it horizontally. */
1019     if (compressionRatio != 1.)
1020     {
1021         textMatrix.a *= compressionRatio;
1022     }
1023
1024     textMatrix = CGAffineTransformScale( textMatrix, 1.0, -1.0 );
1025     CGContextSetTextMatrix(ctx, textMatrix);
1026     CGContextShowGlyphsAtPositions(ctx, &glyph, &CGPointZero, 1);
1027     
1028     /* Restore the text matrix if we messed with the compression ratio */
1029     if (compressionRatio != 1.)
1030     {
1031         textMatrix.a = savedA;
1032         CGContextSetTextMatrix(ctx, textMatrix);
1033     }
1034
1035     textMatrix = CGAffineTransformScale( textMatrix, 1.0, -1.0 );
1036     CGContextSetTextMatrix(ctx, textMatrix);
1037 }
1038
1039 /* Lock and unlock focus on our image or layer, setting up the CTM
1040  * appropriately. */
1041 - (CGContextRef)lockFocusUnscaled
1042 {
1043     /* Create an NSGraphicsContext representing this CGLayer */
1044     CGContextRef ctx = CGLayerGetContext(angbandLayer);
1045     NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:ctx flipped:NO];
1046     [NSGraphicsContext saveGraphicsState];
1047     [NSGraphicsContext setCurrentContext:context];
1048     CGContextSaveGState(ctx);
1049     return ctx;
1050 }
1051
1052 - (void)unlockFocus
1053 {
1054     /* Restore the graphics state */
1055     CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];
1056     CGContextRestoreGState(ctx);
1057     [NSGraphicsContext restoreGraphicsState];
1058 }
1059
1060 - (NSSize)imageSize
1061 {
1062     /* Return the size of our layer */
1063     CGSize result = CGLayerGetSize(angbandLayer);
1064     return NSMakeSize(result.width, result.height);
1065 }
1066
1067 - (CGContextRef)lockFocus
1068 {
1069     return [self lockFocusUnscaled];
1070 }
1071
1072
1073 - (NSRect)rectInImageForTileAtX:(int)x Y:(int)y
1074 {
1075     int flippedY = y;
1076     return NSMakeRect(x * tileSize.width + borderSize.width, flippedY * tileSize.height + borderSize.height, tileSize.width, tileSize.height);
1077 }
1078
1079 - (void)setSelectionFont:(NSFont*)font adjustTerminal: (BOOL)adjustTerminal
1080 {
1081     /* Record the new font */
1082     [font retain];
1083     [angbandViewFont release];
1084     angbandViewFont = font;
1085     
1086     /* Update our glyph info */
1087     [self updateGlyphInfo];
1088
1089     if( adjustTerminal )
1090     {
1091         /* Adjust terminal to fit window with new font; save the new columns
1092                  * and rows since they could be changed */
1093         NSRect contentRect = [self->primaryWindow contentRectForFrameRect: [self->primaryWindow frame]];
1094
1095         [self setMinimumWindowSize:[self terminalIndex]];
1096         NSSize size = self->primaryWindow.contentMinSize;
1097         BOOL windowNeedsResizing = NO;
1098         if (contentRect.size.width < size.width) {
1099             contentRect.size.width = size.width;
1100             windowNeedsResizing = YES;
1101         }
1102         if (contentRect.size.height < size.height) {
1103             contentRect.size.height = size.height;
1104             windowNeedsResizing = YES;
1105         }
1106         if (windowNeedsResizing) {
1107             size.width = contentRect.size.width;
1108             size.height = contentRect.size.height;
1109             [self->primaryWindow setContentSize:size];
1110         }
1111         [self resizeTerminalWithContentRect: contentRect saveToDefaults: YES];
1112     }
1113
1114     /* Update our image */
1115     [self updateImage];
1116 }
1117
1118 - (id)init
1119 {
1120     if ((self = [super init]))
1121     {
1122         /* Default rows and cols */
1123         self->cols = 80;
1124         self->rows = 24;
1125
1126         /* Default border size */
1127         self->borderSize = NSMakeSize(2, 2);
1128
1129         /* Allocate our array of views */
1130         angbandViews = [[NSMutableArray alloc] init];
1131         
1132         self->changes = create_pending_changes(self->cols, self->rows);
1133         if (self->changes == 0) {
1134             NSLog(@"AngbandContext init:  out of memory for pending changes");
1135         }
1136         self->ncol_pre = 0;
1137         self->ncol_post = 0;
1138
1139         self->in_fullscreen_transition = NO;
1140
1141         /* Make the image. Since we have no views, it'll just be a puny 1x1 image. */
1142         [self updateImage];
1143
1144         _windowVisibilityChecked = NO;
1145     }
1146     return self;
1147 }
1148
1149 /**
1150  * Destroy all the receiver's stuff. This is intended to be callable more than
1151  * once.
1152  */
1153 - (void)dispose
1154 {
1155     terminal = NULL;
1156     
1157     /* Disassociate ourselves from our angbandViews */
1158     [angbandViews makeObjectsPerformSelector:@selector(setAngbandContext:) withObject:nil];
1159     [angbandViews release];
1160     angbandViews = nil;
1161     
1162     /* Destroy the layer/image */
1163     CGLayerRelease(angbandLayer);
1164     angbandLayer = NULL;
1165
1166     /* Font */
1167     [angbandViewFont release];
1168     angbandViewFont = nil;
1169     
1170     /* Window */
1171     [primaryWindow setDelegate:nil];
1172     [primaryWindow close];
1173     [primaryWindow release];
1174     primaryWindow = nil;
1175
1176     /* Pending changes */
1177     destroy_pending_changes(self->changes);
1178     self->changes = 0;
1179 }
1180
1181 /* Usual Cocoa fare */
1182 - (void)dealloc
1183 {
1184     [self dispose];
1185     [super dealloc];
1186 }
1187
1188
1189
1190 #pragma mark -
1191 #pragma mark Directories and Paths Setup
1192
1193 /**
1194  * Return the path for Angband's lib directory and bail if it isn't found. The
1195  * lib directory should be in the bundle's resources directory, since it's
1196  * copied when built.
1197  */
1198 + (NSString *)libDirectoryPath
1199 {
1200     NSString *bundleLibPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: AngbandDirectoryNameLib];
1201     BOOL isDirectory = NO;
1202     BOOL libExists = [[NSFileManager defaultManager] fileExistsAtPath: bundleLibPath isDirectory: &isDirectory];
1203
1204     if( !libExists || !isDirectory )
1205     {
1206         NSLog( @"[%@ %@]: can't find %@/ in bundle: isDirectory: %d libExists: %d", NSStringFromClass( [self class] ), NSStringFromSelector( _cmd ), AngbandDirectoryNameLib, isDirectory, libExists );
1207
1208         NSString *msg = NSLocalizedStringWithDefaultValue(
1209             @"Error.MissingResources",
1210             AngbandMessageCatalog,
1211             [NSBundle mainBundle],
1212             @"Missing Resources",
1213             @"Alert text for missing resources");
1214         NSString *info = NSLocalizedStringWithDefaultValue(
1215             @"Error.MissingAngbandLib",
1216             AngbandMessageCatalog,
1217             [NSBundle mainBundle],
1218             @"Hengband was unable to find required resources and must quit. Please report a bug on the Angband forums.",
1219             @"Alert informative message for missing Angband lib/ folder");
1220         NSString *quit_label = NSLocalizedStringWithDefaultValue(
1221             @"Label.Quit", AngbandMessageCatalog, [NSBundle mainBundle],
1222             @"Quit", @"Quit");
1223         NSAlert *alert = [[NSAlert alloc] init];
1224
1225         /*
1226          * Note that NSCriticalAlertStyle was deprecated in 10.10.  The
1227          * replacement is NSAlertStyleCritical.
1228          */
1229         alert.alertStyle = NSCriticalAlertStyle;
1230         alert.messageText = msg;
1231         alert.informativeText = info;
1232         [alert addButtonWithTitle:quit_label];
1233         NSModalResponse result = [alert runModal];
1234         [alert release];
1235         exit( 0 );
1236     }
1237
1238         return bundleLibPath;
1239 }
1240
1241 /**
1242  * Return the path for the directory where Angband should look for its standard
1243  * user file tree.
1244  */
1245 + (NSString *)angbandDocumentsPath
1246 {
1247         NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
1248
1249 #if defined(SAFE_DIRECTORY)
1250         NSString *versionedDirectory = [NSString stringWithFormat: @"%@-%s", AngbandDirectoryNameBase, VERSION_STRING];
1251         return [documents stringByAppendingPathComponent: versionedDirectory];
1252 #else
1253         return [documents stringByAppendingPathComponent: AngbandDirectoryNameBase];
1254 #endif
1255 }
1256
1257 /**
1258  * Adjust directory paths as needed to correct for any differences needed by
1259  * Angband. \c init_file_paths() currently requires that all paths provided have
1260  * a trailing slash and all other platforms honor this.
1261  *
1262  * \param originalPath The directory path to adjust.
1263  * \return A path suitable for Angband or nil if an error occurred.
1264  */
1265 static NSString *AngbandCorrectedDirectoryPath(NSString *originalPath)
1266 {
1267         if ([originalPath length] == 0) {
1268                 return nil;
1269         }
1270
1271         if (![originalPath hasSuffix: @"/"]) {
1272                 return [originalPath stringByAppendingString: @"/"];
1273         }
1274
1275         return originalPath;
1276 }
1277
1278 /**
1279  * Give Angband the base paths that should be used for the various directories
1280  * it needs. It will create any needed directories.
1281  */
1282 + (void)prepareFilePathsAndDirectories
1283 {
1284         char libpath[PATH_MAX + 1] = "\0";
1285         NSString *libDirectoryPath = AngbandCorrectedDirectoryPath([self libDirectoryPath]);
1286         [libDirectoryPath getFileSystemRepresentation: libpath maxLength: sizeof(libpath)];
1287
1288         char basepath[PATH_MAX + 1] = "\0";
1289         NSString *angbandDocumentsPath = AngbandCorrectedDirectoryPath([self angbandDocumentsPath]);
1290         [angbandDocumentsPath getFileSystemRepresentation: basepath maxLength: sizeof(basepath)];
1291
1292         init_file_paths(libpath, basepath);
1293         create_needed_dirs();
1294 }
1295
1296 #pragma mark -
1297
1298 #if 0
1299 /* From the Linux mbstowcs(3) man page:
1300  *   If dest is NULL, n is ignored, and the conversion  proceeds  as  above,
1301  *   except  that  the converted wide characters are not written out to mem‐
1302  *   ory, and that no length limit exists.
1303  */
1304 static size_t Term_mbcs_cocoa(wchar_t *dest, const char *src, int n)
1305 {
1306     int i;
1307     int count = 0;
1308
1309     /* Unicode code point to UTF-8
1310      *  0x0000-0x007f:   0xxxxxxx
1311      *  0x0080-0x07ff:   110xxxxx 10xxxxxx
1312      *  0x0800-0xffff:   1110xxxx 10xxxxxx 10xxxxxx
1313      * 0x10000-0x1fffff: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
1314      * Note that UTF-16 limits Unicode to 0x10ffff. This code is not
1315      * endian-agnostic.
1316      */
1317     for (i = 0; i < n || dest == NULL; i++) {
1318         if ((src[i] & 0x80) == 0) {
1319             if (dest != NULL) dest[count] = src[i];
1320             if (src[i] == 0) break;
1321         } else if ((src[i] & 0xe0) == 0xc0) {
1322             if (dest != NULL) dest[count] = 
1323                             (((unsigned char)src[i] & 0x1f) << 6)| 
1324                             ((unsigned char)src[i+1] & 0x3f);
1325             i++;
1326         } else if ((src[i] & 0xf0) == 0xe0) {
1327             if (dest != NULL) dest[count] = 
1328                             (((unsigned char)src[i] & 0x0f) << 12) | 
1329                             (((unsigned char)src[i+1] & 0x3f) << 6) |
1330                             ((unsigned char)src[i+2] & 0x3f);
1331             i += 2;
1332         } else if ((src[i] & 0xf8) == 0xf0) {
1333             if (dest != NULL) dest[count] = 
1334                             (((unsigned char)src[i] & 0x0f) << 18) | 
1335                             (((unsigned char)src[i+1] & 0x3f) << 12) |
1336                             (((unsigned char)src[i+2] & 0x3f) << 6) |
1337                             ((unsigned char)src[i+3] & 0x3f);
1338             i += 3;
1339         } else {
1340             /* Found an invalid multibyte sequence */
1341             return (size_t)-1;
1342         }
1343         count++;
1344     }
1345     return count;
1346 }
1347 #endif
1348
1349 /**
1350  * Entry point for initializing Angband
1351  */
1352 + (void)beginGame
1353 {
1354     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1355     
1356     /* Hooks in some "z-util.c" hooks */
1357     plog_aux = hook_plog;
1358     quit_aux = hook_quit;
1359     
1360     /* Initialize file paths */
1361     [self prepareFilePathsAndDirectories];
1362
1363     /* Note the "system" */
1364     ANGBAND_SYS = "coc";
1365
1366     /* Load possible graphics modes */
1367     init_graphics_modes();
1368
1369     /* Load preferences */
1370     load_prefs();
1371
1372     /* Prepare the windows */
1373     init_windows();
1374     
1375     /* Set up game event handlers */
1376     /* init_display(); */
1377     
1378     /* Register the sound hook */
1379     /* sound_hook = play_sound; */
1380     
1381     /* Initialise game */
1382     init_angband();
1383
1384     /* Initialize some save file stuff */
1385     player_egid = getegid();
1386     
1387     /* We are now initialized */
1388     initialized = TRUE;
1389     
1390     /* Handle "open_when_ready" */
1391     handle_open_when_ready();
1392     
1393     /* Handle pending events (most notably update) and flush input */
1394     Term_flush();
1395
1396     /* Prompt the user. */
1397     int message_row = (Term->hgt - 23) / 5 + 23;
1398     Term_erase(0, message_row, 255);
1399     put_str(
1400 #ifdef JP
1401         "['ファイル' メニューから '新' または '開く' を選択します]",
1402         message_row, (Term->wid - 57) / 2
1403 #else
1404         "[Choose 'New' or 'Open' from the 'File' menu]",
1405         message_row, (Term->wid - 45) / 2
1406 #endif
1407     );
1408     Term_fresh();
1409
1410     /*
1411      * Play a game -- "new_game" is set by "new", "open" or the open document
1412      * even handler as appropriate
1413      */
1414         
1415     [pool drain];
1416     
1417     while (!game_in_progress) {
1418         NSAutoreleasePool *splashScreenPool = [[NSAutoreleasePool alloc] init];
1419         NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantFuture] inMode:NSDefaultRunLoopMode dequeue:YES];
1420         if (event) [NSApp sendEvent:event];
1421         [splashScreenPool drain];
1422     }
1423
1424     Term_fresh();
1425     play_game(new_game);
1426
1427     quit(NULL);
1428 }
1429
1430 - (void)addAngbandView:(AngbandView *)view
1431 {
1432     if (! [angbandViews containsObject:view])
1433     {
1434         [angbandViews addObject:view];
1435         [self updateImage];
1436         [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
1437         [self requestRedraw];
1438     }
1439 }
1440
1441 /**
1442  * We have this notion of an "active" AngbandView, which is the largest - the
1443  * idea being that in the screen saver, when the user hits Test in System
1444  * Preferences, we don't want to keep driving the AngbandView in the
1445  * background.  Our active AngbandView is the widest - that's a hack all right.
1446  * Mercifully when we're just playing the game there's only one view.
1447  */
1448 - (AngbandView *)activeView
1449 {
1450     if ([angbandViews count] == 1)
1451         return [angbandViews objectAtIndex:0];
1452     
1453     AngbandView *result = nil;
1454     float maxWidth = 0;
1455     for (AngbandView *angbandView in angbandViews)
1456     {
1457         float width = [angbandView frame].size.width;
1458         if (width > maxWidth)
1459         {
1460             maxWidth = width;
1461             result = angbandView;
1462         }
1463     }
1464     return result;
1465 }
1466
1467 - (void)angbandViewDidScale:(AngbandView *)view
1468 {
1469     /* If we're live-resizing with graphics, we're using the live resize
1470          * optimization, so don't update the image. Otherwise do it. */
1471     if (! (inLiveResize && graphics_are_enabled()) && view == [self activeView])
1472     {
1473         [self updateImage];
1474         
1475         [self setNeedsDisplay:YES]; /*we'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
1476         [self requestRedraw];
1477     }
1478 }
1479
1480
1481 - (void)removeAngbandView:(AngbandView *)view
1482 {
1483     if ([angbandViews containsObject:view])
1484     {
1485         [angbandViews removeObject:view];
1486         [self updateImage];
1487         [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
1488         if ([angbandViews count]) [self requestRedraw];
1489     }
1490 }
1491
1492
1493 - (NSWindow *)makePrimaryWindow
1494 {
1495     if (! primaryWindow)
1496     {
1497         /* This has to be done after the font is set, which it already is in
1498                  * term_init_cocoa() */
1499         CGFloat width = self->cols * tileSize.width + borderSize.width * 2.0;
1500         CGFloat height = self->rows * tileSize.height + borderSize.height * 2.0;
1501         NSRect contentRect = NSMakeRect( 0.0, 0.0, width, height );
1502
1503         NSUInteger styleMask = NSTitledWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask;
1504
1505         /* Make every window other than the main window closable */
1506         if( angband_term[0]->data != self )
1507         {
1508             styleMask |= NSClosableWindowMask;
1509         }
1510
1511         primaryWindow = [[NSWindow alloc] initWithContentRect:contentRect styleMask: styleMask backing:NSBackingStoreBuffered defer:YES];
1512
1513         /* Not to be released when closed */
1514         [primaryWindow setReleasedWhenClosed:NO];
1515         [primaryWindow setExcludedFromWindowsMenu: YES]; /* we're using custom window menu handling */
1516
1517         /* Make the view */
1518         AngbandView *angbandView = [[AngbandView alloc] initWithFrame:contentRect];
1519         [angbandView setAngbandContext:self];
1520         [angbandViews addObject:angbandView];
1521         [primaryWindow setContentView:angbandView];
1522         [angbandView release];
1523
1524         /* We are its delegate */
1525         [primaryWindow setDelegate:self];
1526
1527         /* Update our image, since this is probably the first angband view
1528                  * we've gotten. */
1529         [self updateImage];
1530     }
1531     return primaryWindow;
1532 }
1533
1534
1535
1536 #pragma mark View/Window Passthrough
1537
1538 /**
1539  * This is what our views call to get us to draw to the window
1540  */
1541 - (void)drawRect:(NSRect)rect inView:(NSView *)view
1542 {
1543     /* Take this opportunity to throttle so we don't flush faster than desired.
1544          */
1545     BOOL viewInLiveResize = [view inLiveResize];
1546     if (! viewInLiveResize) [self throttle];
1547
1548     /* With a GLayer, use CGContextDrawLayerInRect */
1549     CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
1550     NSRect bounds = [view bounds];
1551     if (viewInLiveResize) CGContextSetInterpolationQuality(context, kCGInterpolationLow);
1552     CGContextSetBlendMode(context, kCGBlendModeCopy);
1553     CGContextDrawLayerInRect(context, *(CGRect *)&bounds, angbandLayer);
1554     if (viewInLiveResize) CGContextSetInterpolationQuality(context, kCGInterpolationDefault);
1555 }
1556
1557 - (BOOL)isOrderedIn
1558 {
1559     return [[[angbandViews lastObject] window] isVisible];
1560 }
1561
1562 - (BOOL)isMainWindow
1563 {
1564     return [[[angbandViews lastObject] window] isMainWindow];
1565 }
1566
1567 - (void)setNeedsDisplay:(BOOL)val
1568 {
1569     for (NSView *angbandView in angbandViews)
1570     {
1571         [angbandView setNeedsDisplay:val];
1572     }
1573 }
1574
1575 - (void)setNeedsDisplayInBaseRect:(NSRect)rect
1576 {
1577     for (NSView *angbandView in angbandViews)
1578     {
1579         [angbandView setNeedsDisplayInRect: rect];
1580     }
1581 }
1582
1583 - (void)displayIfNeeded
1584 {
1585     [[self activeView] displayIfNeeded];
1586 }
1587
1588 - (int)terminalIndex
1589 {
1590         int termIndex = 0;
1591
1592         for( termIndex = 0; termIndex < ANGBAND_TERM_MAX; termIndex++ )
1593         {
1594                 if( angband_term[termIndex] == self->terminal )
1595                 {
1596                         break;
1597                 }
1598         }
1599
1600         return termIndex;
1601 }
1602
1603 - (void)resizeTerminalWithContentRect: (NSRect)contentRect saveToDefaults: (BOOL)saveToDefaults
1604 {
1605     CGFloat newRows = floor( (contentRect.size.height - (borderSize.height * 2.0)) / tileSize.height );
1606     CGFloat newColumns = ceil( (contentRect.size.width - (borderSize.width * 2.0)) / tileSize.width );
1607
1608     if (newRows < 1 || newColumns < 1) return;
1609     self->cols = newColumns;
1610     self->rows = newRows;
1611
1612     if (resize_pending_changes(self->changes, self->rows) != 0) {
1613         destroy_pending_changes(self->changes);
1614         self->changes = 0;
1615         NSLog(@"out of memory for pending changes with resize of terminal %d",
1616               [self terminalIndex]);
1617     }
1618
1619     if( saveToDefaults )
1620     {
1621         int termIndex = [self terminalIndex];
1622         NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
1623
1624         if( termIndex < (int)[terminals count] )
1625         {
1626             NSMutableDictionary *mutableTerm = [[NSMutableDictionary alloc] initWithDictionary: [terminals objectAtIndex: termIndex]];
1627             [mutableTerm setValue: [NSNumber numberWithUnsignedInt: self->cols] forKey: AngbandTerminalColumnsDefaultsKey];
1628             [mutableTerm setValue: [NSNumber numberWithUnsignedInt: self->rows] forKey: AngbandTerminalRowsDefaultsKey];
1629
1630             NSMutableArray *mutableTerminals = [[NSMutableArray alloc] initWithArray: terminals];
1631             [mutableTerminals replaceObjectAtIndex: termIndex withObject: mutableTerm];
1632
1633             [[NSUserDefaults standardUserDefaults] setValue: mutableTerminals forKey: AngbandTerminalsDefaultsKey];
1634             [mutableTerminals release];
1635             [mutableTerm release];
1636         }
1637         [[NSUserDefaults standardUserDefaults] synchronize];
1638     }
1639
1640     term *old = Term;
1641     Term_activate( self->terminal );
1642     Term_resize( (int)newColumns, (int)newRows);
1643     Term_redraw();
1644     Term_activate( old );
1645 }
1646
1647 - (void)setMinimumWindowSize:(int)termIdx
1648 {
1649     NSSize minsize;
1650
1651     if (termIdx == 0) {
1652         minsize.width = 80;
1653         minsize.height = 24;
1654     } else {
1655         minsize.width = 1;
1656         minsize.height = 1;
1657     }
1658     minsize.width =
1659         minsize.width * self->tileSize.width + self->borderSize.width * 2.0;
1660     minsize.height =
1661         minsize.height * self->tileSize.height + self->borderSize.height * 2.0;
1662     [[self makePrimaryWindow] setContentMinSize:minsize];
1663 }
1664
1665 - (void)saveWindowVisibleToDefaults: (BOOL)windowVisible
1666 {
1667         int termIndex = [self terminalIndex];
1668         BOOL safeVisibility = (termIndex == 0) ? YES : windowVisible; /* Ensure main term doesn't go away because of these defaults */
1669         NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
1670
1671         if( termIndex < (int)[terminals count] )
1672         {
1673                 NSMutableDictionary *mutableTerm = [[NSMutableDictionary alloc] initWithDictionary: [terminals objectAtIndex: termIndex]];
1674                 [mutableTerm setValue: [NSNumber numberWithBool: safeVisibility] forKey: AngbandTerminalVisibleDefaultsKey];
1675
1676                 NSMutableArray *mutableTerminals = [[NSMutableArray alloc] initWithArray: terminals];
1677                 [mutableTerminals replaceObjectAtIndex: termIndex withObject: mutableTerm];
1678
1679                 [[NSUserDefaults standardUserDefaults] setValue: mutableTerminals forKey: AngbandTerminalsDefaultsKey];
1680                 [mutableTerminals release];
1681                 [mutableTerm release];
1682         }
1683 }
1684
1685 - (BOOL)windowVisibleUsingDefaults
1686 {
1687         int termIndex = [self terminalIndex];
1688
1689         if( termIndex == 0 )
1690         {
1691                 return YES;
1692         }
1693
1694         NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
1695         BOOL visible = NO;
1696
1697         if( termIndex < (int)[terminals count] )
1698         {
1699                 NSDictionary *term = [terminals objectAtIndex: termIndex];
1700                 NSNumber *visibleValue = [term valueForKey: AngbandTerminalVisibleDefaultsKey];
1701
1702                 if( visibleValue != nil )
1703                 {
1704                         visible = [visibleValue boolValue];
1705                 }
1706         }
1707
1708         return visible;
1709 }
1710
1711 #pragma mark -
1712 #pragma mark NSWindowDelegate Methods
1713
1714 /*- (void)windowWillStartLiveResize: (NSNotification *)notification
1715
1716 }*/ 
1717
1718 - (void)windowDidEndLiveResize: (NSNotification *)notification
1719 {
1720     NSWindow *window = [notification object];
1721     NSRect contentRect = [window contentRectForFrameRect: [window frame]];
1722     [self resizeTerminalWithContentRect: contentRect saveToDefaults: !(self->in_fullscreen_transition)];
1723 }
1724
1725 /*- (NSSize)windowWillResize: (NSWindow *)sender toSize: (NSSize)frameSize
1726 {
1727 } */
1728
1729 - (void)windowWillEnterFullScreen: (NSNotification *)notification
1730 {
1731     self->in_fullscreen_transition = YES;
1732 }
1733
1734 - (void)windowDidEnterFullScreen: (NSNotification *)notification
1735 {
1736     NSWindow *window = [notification object];
1737     NSRect contentRect = [window contentRectForFrameRect: [window frame]];
1738     self->in_fullscreen_transition = NO;
1739     [self resizeTerminalWithContentRect: contentRect saveToDefaults: NO];
1740 }
1741
1742 - (void)windowWillExitFullScreen: (NSNotification *)notification
1743 {
1744     self->in_fullscreen_transition = YES;
1745 }
1746
1747 - (void)windowDidExitFullScreen: (NSNotification *)notification
1748 {
1749     NSWindow *window = [notification object];
1750     NSRect contentRect = [window contentRectForFrameRect: [window frame]];
1751     self->in_fullscreen_transition = NO;
1752     [self resizeTerminalWithContentRect: contentRect saveToDefaults: NO];
1753 }
1754
1755 - (void)windowDidBecomeMain:(NSNotification *)notification
1756 {
1757     NSWindow *window = [notification object];
1758
1759     if( window != self->primaryWindow )
1760     {
1761         return;
1762     }
1763
1764     int termIndex = [self terminalIndex];
1765     NSMenuItem *item = [[[NSApplication sharedApplication] windowsMenu] itemWithTag: AngbandWindowMenuItemTagBase + termIndex];
1766     [item setState: NSOnState];
1767
1768     if( [[NSFontPanel sharedFontPanel] isVisible] )
1769     {
1770         [[NSFontPanel sharedFontPanel] setPanelFont: [self selectionFont] isMultiple: NO];
1771     }
1772 }
1773
1774 - (void)windowDidResignMain: (NSNotification *)notification
1775 {
1776     NSWindow *window = [notification object];
1777
1778     if( window != self->primaryWindow )
1779     {
1780         return;
1781     }
1782
1783     int termIndex = [self terminalIndex];
1784     NSMenuItem *item = [[[NSApplication sharedApplication] windowsMenu] itemWithTag: AngbandWindowMenuItemTagBase + termIndex];
1785     [item setState: NSOffState];
1786 }
1787
1788 - (void)windowWillClose: (NSNotification *)notification
1789 {
1790         [self saveWindowVisibleToDefaults: NO];
1791 }
1792
1793 @end
1794
1795
1796 @implementation AngbandView
1797
1798 - (BOOL)isOpaque
1799 {
1800     return YES;
1801 }
1802
1803 - (BOOL)isFlipped
1804 {
1805     return YES;
1806 }
1807
1808 - (void)drawRect:(NSRect)rect
1809 {
1810     if (! angbandContext)
1811     {
1812         /* Draw bright orange, 'cause this ain't right */
1813         [[NSColor orangeColor] set];
1814         NSRectFill([self bounds]);
1815     }
1816     else
1817     {
1818         /* Tell the Angband context to draw into us */
1819         [angbandContext drawRect:rect inView:self];
1820     }
1821 }
1822
1823 - (void)setAngbandContext:(AngbandContext *)context
1824 {
1825     angbandContext = context;
1826 }
1827
1828 - (AngbandContext *)angbandContext
1829 {
1830     return angbandContext;
1831 }
1832
1833 - (void)setFrameSize:(NSSize)size
1834 {
1835     BOOL changed = ! NSEqualSizes(size, [self frame].size);
1836     [super setFrameSize:size];
1837     if (changed) [angbandContext angbandViewDidScale:self];
1838 }
1839
1840 - (void)viewWillStartLiveResize
1841 {
1842     [angbandContext viewWillStartLiveResize:self];
1843 }
1844
1845 - (void)viewDidEndLiveResize
1846 {
1847     [angbandContext viewDidEndLiveResize:self];
1848 }
1849
1850 @end
1851
1852 /**
1853  * Delay handling of double-clicked savefiles
1854  */
1855 Boolean open_when_ready = FALSE;
1856
1857
1858
1859 /**
1860  * ------------------------------------------------------------------------
1861  * Some generic functions
1862  * ------------------------------------------------------------------------ */
1863
1864 /**
1865  * Sets an Angband color at a given index
1866  */
1867 static void set_color_for_index(int idx)
1868 {
1869     u16b rv, gv, bv;
1870     
1871     /* Extract the R,G,B data */
1872     rv = angband_color_table[idx][1];
1873     gv = angband_color_table[idx][2];
1874     bv = angband_color_table[idx][3];
1875     
1876     CGContextSetRGBFillColor([[NSGraphicsContext currentContext] graphicsPort], rv/255., gv/255., bv/255., 1.);
1877 }
1878
1879 /**
1880  * Remember the current character in UserDefaults so we can select it by
1881  * default next time.
1882  */
1883 static void record_current_savefile(void)
1884 {
1885     NSString *savefileString = [[NSString stringWithCString:savefile encoding:NSMacOSRomanStringEncoding] lastPathComponent];
1886     if (savefileString)
1887     {
1888         NSUserDefaults *angbandDefs = [NSUserDefaults angbandDefaults];
1889         [angbandDefs setObject:savefileString forKey:@"SaveFile"];
1890         [angbandDefs synchronize];
1891     }
1892 }
1893
1894
1895 #ifdef JP
1896 /**
1897  * Convert a two-byte EUC-JP encoded character (both *cp and (*cp + 1) are in
1898  * the range, 0xA1-0xFE, or *cp is 0x8E) to a utf16 value in the native byte
1899  * ordering.
1900  */
1901 static wchar_t convert_two_byte_eucjp_to_utf16_native(const char *cp)
1902 {
1903     NSString* str = [[NSString alloc] initWithBytes:cp length:2
1904                                       encoding:NSJapaneseEUCStringEncoding];
1905     wchar_t result = [str characterAtIndex:0];
1906
1907     [str release];
1908     return result;
1909 }
1910 #endif /* JP */
1911
1912
1913 /**
1914  * ------------------------------------------------------------------------
1915  * Support for the "z-term.c" package
1916  * ------------------------------------------------------------------------ */
1917
1918
1919 /**
1920  * Initialize a new Term
1921  */
1922 static void Term_init_cocoa(term *t)
1923 {
1924     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1925     AngbandContext *context = [[AngbandContext alloc] init];
1926     
1927     /* Give the term a hard retain on context (for GC) */
1928     t->data = (void *)CFRetain(context);
1929     [context release];
1930     
1931     /* Handle graphics */
1932     t->higher_pict = !! use_graphics;
1933     t->always_pict = FALSE;
1934     
1935     NSDisableScreenUpdates();
1936     
1937     /* Figure out the frame autosave name based on the index of this term */
1938     NSString *autosaveName = nil;
1939     int termIdx;
1940     for (termIdx = 0; termIdx < ANGBAND_TERM_MAX; termIdx++)
1941     {
1942         if (angband_term[termIdx] == t)
1943         {
1944             autosaveName = [NSString stringWithFormat:@"AngbandTerm-%d", termIdx];
1945             break;
1946         }
1947     }
1948
1949     /* Set its font. */
1950     NSString *fontName = [[NSUserDefaults angbandDefaults] stringForKey:[NSString stringWithFormat:@"FontName-%d", termIdx]];
1951     if (! fontName) fontName = [default_font fontName];
1952
1953     /* Use a smaller default font for the other windows, but only if the font
1954          * hasn't been explicitly set */
1955     float fontSize = (termIdx > 0) ? 10.0 : [default_font pointSize];
1956     NSNumber *fontSizeNumber = [[NSUserDefaults angbandDefaults] valueForKey: [NSString stringWithFormat: @"FontSize-%d", termIdx]];
1957
1958     if( fontSizeNumber != nil )
1959     {
1960         fontSize = [fontSizeNumber floatValue];
1961     }
1962
1963     [context setSelectionFont:[NSFont fontWithName:fontName size:fontSize] adjustTerminal: NO];
1964
1965     NSArray *terminalDefaults = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
1966     NSInteger rows = 24;
1967     NSInteger columns = 80;
1968
1969     if( termIdx < (int)[terminalDefaults count] )
1970     {
1971         NSDictionary *term = [terminalDefaults objectAtIndex: termIdx];
1972         NSInteger defaultRows = [[term valueForKey: AngbandTerminalRowsDefaultsKey] integerValue];
1973         NSInteger defaultColumns = [[term valueForKey: AngbandTerminalColumnsDefaultsKey] integerValue];
1974
1975         if (defaultRows > 0) rows = defaultRows;
1976         if (defaultColumns > 0) columns = defaultColumns;
1977     }
1978
1979     context->cols = columns;
1980     context->rows = rows;
1981
1982     if (resize_pending_changes(context->changes, context->rows) != 0) {
1983         destroy_pending_changes(context->changes);
1984         context->changes = 0;
1985         NSLog(@"initializing terminal %d:  out of memory for pending changes",
1986               termIdx);
1987     }
1988
1989     /* Get the window */
1990     NSWindow *window = [context makePrimaryWindow];
1991
1992     /* Set its title and, for auxiliary terms, tentative size */
1993     NSString *title = [NSString stringWithCString:angband_term_name[termIdx]
1994 #ifdef JP
1995                                 encoding:NSJapaneseEUCStringEncoding
1996 #else
1997                                 encoding:NSMacOSRomanStringEncoding
1998 #endif
1999     ];
2000     [window setTitle:title];
2001     [context setMinimumWindowSize:termIdx];
2002
2003     /* If this is the first term, and we support full screen (Mac OS X Lion or
2004          * later), then allow it to go full screen (sweet). Allow other terms to be
2005          * FullScreenAuxilliary, so they can at least show up. Unfortunately in
2006          * Lion they don't get brought to the full screen space; but they would
2007          * only make sense on multiple displays anyways so it's not a big loss. */
2008     if ([window respondsToSelector:@selector(toggleFullScreen:)])
2009     {
2010         NSWindowCollectionBehavior behavior = [window collectionBehavior];
2011         behavior |= (termIdx == 0 ? Angband_NSWindowCollectionBehaviorFullScreenPrimary : Angband_NSWindowCollectionBehaviorFullScreenAuxiliary);
2012         [window setCollectionBehavior:behavior];
2013     }
2014     
2015     /* No Resume support yet, though it would not be hard to add */
2016     if ([window respondsToSelector:@selector(setRestorable:)])
2017     {
2018         [window setRestorable:NO];
2019     }
2020
2021         /* default window placement */ {
2022                 static NSRect overallBoundingRect;
2023
2024                 if( termIdx == 0 )
2025                 {
2026                         /* This is a bit of a trick to allow us to display multiple windows
2027                          * in the "standard default" window position in OS X: the upper
2028                          * center of the screen.
2029                          * The term sizes set in load_prefs() are based on a 5-wide by
2030                          * 3-high grid, with the main term being 4/5 wide by 2/3 high
2031                          * (hence the scaling to find */
2032
2033                         /* What the containing rect would be). */
2034                         NSRect originalMainTermFrame = [window frame];
2035                         NSRect scaledFrame = originalMainTermFrame;
2036                         scaledFrame.size.width *= 5.0 / 4.0;
2037                         scaledFrame.size.height *= 3.0 / 2.0;
2038                         scaledFrame.size.width += 1.0; /* spacing between window columns */
2039                         scaledFrame.size.height += 1.0; /* spacing between window rows */
2040                         [window setFrame: scaledFrame  display: NO];
2041                         [window center];
2042                         overallBoundingRect = [window frame];
2043                         [window setFrame: originalMainTermFrame display: NO];
2044                 }
2045
2046                 static NSRect mainTermBaseRect;
2047                 NSRect windowFrame = [window frame];
2048
2049                 if( termIdx == 0 )
2050                 {
2051                         /* The height and width adjustments were determined experimentally,
2052                          * so that the rest of the windows line up nicely without
2053                          * overlapping */
2054             windowFrame.size.width += 7.0;
2055                         windowFrame.size.height += 9.0;
2056                         windowFrame.origin.x = NSMinX( overallBoundingRect );
2057                         windowFrame.origin.y = NSMaxY( overallBoundingRect ) - NSHeight( windowFrame );
2058                         mainTermBaseRect = windowFrame;
2059                 }
2060                 else if( termIdx == 1 )
2061                 {
2062                         windowFrame.origin.x = NSMinX( mainTermBaseRect );
2063                         windowFrame.origin.y = NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
2064                 }
2065                 else if( termIdx == 2 )
2066                 {
2067                         windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
2068                         windowFrame.origin.y = NSMaxY( mainTermBaseRect ) - NSHeight( windowFrame );
2069                 }
2070                 else if( termIdx == 3 )
2071                 {
2072                         windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
2073                         windowFrame.origin.y = NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
2074                 }
2075                 else if( termIdx == 4 )
2076                 {
2077                         windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
2078                         windowFrame.origin.y = NSMinY( mainTermBaseRect );
2079                 }
2080                 else if( termIdx == 5 )
2081                 {
2082                         windowFrame.origin.x = NSMinX( mainTermBaseRect ) + NSWidth( windowFrame ) + 1.0;
2083                         windowFrame.origin.y = NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
2084                 }
2085
2086                 [window setFrame: windowFrame display: NO];
2087         }
2088
2089         /* Override the default frame above if the user has adjusted windows in
2090          * the past */
2091         if (autosaveName) [window setFrameAutosaveName:autosaveName];
2092
2093     /* Tell it about its term. Do this after we've sized it so that the sizing
2094          * doesn't trigger redrawing and such. */
2095     [context setTerm:t];
2096     
2097     /* Only order front if it's the first term. Other terms will be ordered
2098          * front from AngbandUpdateWindowVisibility(). This is to work around a
2099          * problem where Angband aggressively tells us to initialize terms that
2100          * don't do anything! */
2101     if (t == angband_term[0]) [context->primaryWindow makeKeyAndOrderFront: nil];
2102     
2103     NSEnableScreenUpdates();
2104     
2105     /* Set "mapped" flag */
2106     t->mapped_flag = true;
2107     [pool drain];
2108 }
2109
2110
2111
2112 /**
2113  * Nuke an old Term
2114  */
2115 static void Term_nuke_cocoa(term *t)
2116 {
2117     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2118     
2119     AngbandContext *context = t->data;
2120     if (context)
2121     {
2122         /* Tell the context to get rid of its windows, etc. */
2123         [context dispose];
2124         
2125         /* Balance our CFRetain from when we created it */
2126         CFRelease(context);
2127         
2128         /* Done with it */
2129         t->data = NULL;
2130     }
2131     
2132     [pool drain];
2133 }
2134
2135 /**
2136  * Returns the CGImageRef corresponding to an image with the given name in the
2137  * resource directory, transferring ownership to the caller
2138  */
2139 static CGImageRef create_angband_image(NSString *path)
2140 {
2141     CGImageRef decodedImage = NULL, result = NULL;
2142     
2143     /* Try using ImageIO to load the image */
2144     if (path)
2145     {
2146         NSURL *url = [[NSURL alloc] initFileURLWithPath:path isDirectory:NO];
2147         if (url)
2148         {
2149             NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:(id)kCFBooleanTrue, kCGImageSourceShouldCache, nil];
2150             CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, (CFDictionaryRef)options);
2151             if (source)
2152             {
2153                 /* We really want the largest image, but in practice there's
2154                                  * only going to be one */
2155                 decodedImage = CGImageSourceCreateImageAtIndex(source, 0, (CFDictionaryRef)options);
2156                 CFRelease(source);
2157             }
2158             [options release];
2159             [url release];
2160         }
2161     }
2162     
2163     /* Draw the sucker to defeat ImageIO's weird desire to cache and decode on
2164          * demand. Our images aren't that big! */
2165     if (decodedImage)
2166     {
2167         size_t width = CGImageGetWidth(decodedImage), height = CGImageGetHeight(decodedImage);
2168         
2169         /* Compute our own bitmap info */
2170         CGBitmapInfo imageBitmapInfo = CGImageGetBitmapInfo(decodedImage);
2171         CGBitmapInfo contextBitmapInfo = kCGBitmapByteOrderDefault;
2172         
2173         switch (imageBitmapInfo & kCGBitmapAlphaInfoMask) {
2174             case kCGImageAlphaNone:
2175             case kCGImageAlphaNoneSkipLast:
2176             case kCGImageAlphaNoneSkipFirst:
2177                 /* No alpha */
2178                 contextBitmapInfo |= kCGImageAlphaNone;
2179                 break;
2180             default:
2181                 /* Some alpha, use premultiplied last which is most efficient. */
2182                 contextBitmapInfo |= kCGImageAlphaPremultipliedLast;
2183                 break;
2184         }
2185
2186         /* Draw the source image flipped, since the view is flipped */
2187         CGContextRef ctx = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(decodedImage), CGImageGetBytesPerRow(decodedImage), CGImageGetColorSpace(decodedImage), contextBitmapInfo);
2188         if (ctx) {
2189             CGContextSetBlendMode(ctx, kCGBlendModeCopy);
2190             CGContextTranslateCTM(ctx, 0.0, height);
2191             CGContextScaleCTM(ctx, 1.0, -1.0);
2192             CGContextDrawImage(
2193                 ctx, CGRectMake(0, 0, width, height), decodedImage);
2194             result = CGBitmapContextCreateImage(ctx);
2195             CFRelease(ctx);
2196         }
2197
2198         CGImageRelease(decodedImage);
2199     }
2200     return result;
2201 }
2202
2203 /**
2204  * React to changes
2205  */
2206 static errr Term_xtra_cocoa_react(void)
2207 {
2208     /* Don't actually switch graphics until the game is running */
2209     if (!initialized || !game_in_progress) return (-1);
2210
2211     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2212     AngbandContext *angbandContext = Term->data;
2213
2214     /* Handle graphics */
2215     int expected_graf_mode = (current_graphics_mode) ?
2216         current_graphics_mode->grafID : GRAPHICS_NONE;
2217     if (graf_mode_req != expected_graf_mode)
2218     {
2219         graphics_mode *new_mode;
2220         if (graf_mode_req != GRAPHICS_NONE) {
2221             new_mode = get_graphics_mode(graf_mode_req);
2222         } else {
2223             new_mode = NULL;
2224         }
2225         
2226         /* Get rid of the old image. CGImageRelease is NULL-safe. */
2227         CGImageRelease(pict_image);
2228         pict_image = NULL;
2229         
2230         /* Try creating the image if we want one */
2231         if (new_mode != NULL)
2232         {
2233             NSString *img_path = [NSString stringWithFormat:@"%s/%s", new_mode->path, new_mode->file];
2234             pict_image = create_angband_image(img_path);
2235
2236             /* If we failed to create the image, revert to ASCII. */
2237             if (! pict_image) {
2238                 new_mode = NULL;
2239                 [[NSUserDefaults angbandDefaults]
2240                     setInteger:GRAPHICS_NONE
2241                     forKey:AngbandGraphicsDefaultsKey];
2242                 [[NSUserDefaults angbandDefaults] synchronize];
2243
2244                 NSString *msg = NSLocalizedStringWithDefaultValue(
2245                     @"Error.TileSetLoadFailed",
2246                     AngbandMessageCatalog,
2247                     [NSBundle mainBundle],
2248                     @"Failed to Load Tile Set",
2249                     @"Alert text for failed tile set load");
2250                 NSString *info = NSLocalizedStringWithDefaultValue(
2251                     @"Error.TileSetRevertToASCII",
2252                     AngbandMessageCatalog,
2253                     [NSBundle mainBundle],
2254                     @"Could not load the tile set.  Switched back to ASCII.",
2255                     @"Alert informative message for failed tile set load");
2256                 NSAlert *alert = [[NSAlert alloc] init];
2257
2258                 alert.messageText = msg;
2259                 alert.informativeText = info;
2260                 NSModalResponse result = [alert runModal];
2261                 [alert release];
2262             }
2263         }
2264         
2265         /* Record what we did */
2266         use_graphics = new_mode ? new_mode->grafID : 0;
2267         ANGBAND_GRAF = (new_mode ? new_mode->graf : "ascii");
2268         current_graphics_mode = new_mode;
2269         
2270         /* Enable or disable higher picts. Note: this should be done for all
2271                  * terms. */
2272         angbandContext->terminal->higher_pict = !! use_graphics;
2273         
2274         if (pict_image && current_graphics_mode)
2275         {
2276             /* Compute the row and column count via the image height and width.
2277                          */
2278             pict_rows = (int)(CGImageGetHeight(pict_image) / current_graphics_mode->cell_height);
2279             pict_cols = (int)(CGImageGetWidth(pict_image) / current_graphics_mode->cell_width);
2280         }
2281         else
2282         {
2283             pict_rows = 0;
2284             pict_cols = 0;
2285         }
2286         
2287         /* Reset visuals */
2288         if (initialized && game_in_progress)
2289         {
2290             reset_visuals();
2291         }
2292     }
2293
2294     [pool drain];
2295     
2296     /* Success */
2297     return (0);
2298 }
2299
2300
2301 /**
2302  * Draws one tile as a helper function for Term_xtra_cocoa_fresh().
2303  */
2304 static void draw_image_tile(
2305     NSGraphicsContext* nsContext,
2306     CGContextRef cgContext,
2307     CGImageRef image,
2308     NSRect srcRect,
2309     NSRect dstRect,
2310     NSCompositingOperation op)
2311 {
2312     /* Flip the source rect since the source image is flipped */
2313     CGAffineTransform flip = CGAffineTransformIdentity;
2314     flip = CGAffineTransformTranslate(flip, 0.0, CGImageGetHeight(image));
2315     flip = CGAffineTransformScale(flip, 1.0, -1.0);
2316     CGRect flippedSourceRect =
2317         CGRectApplyAffineTransform(NSRectToCGRect(srcRect), flip);
2318
2319     /*
2320      * When we use high-quality resampling to draw a tile, pixels from outside
2321      * the tile may bleed in, causing graphics artifacts. Work around that.
2322      */
2323     CGImageRef subimage =
2324         CGImageCreateWithImageInRect(image, flippedSourceRect);
2325     [nsContext setCompositingOperation:op];
2326     CGContextDrawImage(cgContext, NSRectToCGRect(dstRect), subimage);
2327     CGImageRelease(subimage);
2328 }
2329
2330
2331 /**
2332  * This is a helper function for Term_xtra_cocoa_fresh():  look before a block
2333  * of text on a row to see if the bounds for rendering and clipping need to be
2334  * extended.
2335  */
2336 static void query_before_text(
2337     struct PendingRowChange* prc, int iy, int npre, int* pclip, int* prend)
2338 {
2339     int start = *prend;
2340     int i = start - 1;
2341
2342     while (1) {
2343         if (i < 0 || i < start - npre) {
2344             break;
2345         }
2346
2347         if (prc->cell_changes[i].change_type == CELL_CHANGE_PICT) {
2348             /*
2349              * The cell has been rendered with a tile.  Do not want to modify
2350              * its contents so the clipping and rendering region can not be
2351              * extended.
2352              */
2353             break;
2354         } else if (prc->cell_changes[i].change_type == CELL_CHANGE_NONE) {
2355             /*
2356              * It has not changed (or using big tile mode and it is within
2357              * a changed tile but is not the left cell for that tile) so
2358              * inquire what it is.
2359              */
2360             TERM_COLOR a[2];
2361             char c[2];
2362
2363             Term_what(i, iy, a + 1, c + 1);
2364             if (use_graphics && (a[1] & 0x80) && (c[1] & 0x80)) {
2365                 /*
2366                  * It is an unchanged location rendered with a tile.  Do not
2367                  * want to modify its contents so the clipping and rendering
2368                  * region can not be extended.
2369                  */
2370                 break;
2371             }
2372             if (use_bigtile && i > 0) {
2373                 Term_what(i - 1, iy, a, c);
2374                 if (use_graphics && (a[0] & 0x80) && (c[0] & 0x80)) {
2375                     /*
2376                      * It is the right cell of a location rendered with a tile.
2377                      * Do not want to modify its contents so the clipping and
2378                      * rendering region can not be exteded.
2379                      */
2380                     break;
2381                 }
2382             }
2383             /*
2384              * It is unchanged text.  A character from the changed region
2385              * may have extended into it so render it to clear that.
2386              */
2387 #ifdef JP
2388             /* Check to see if it is the second part of a kanji character. */
2389             if (i > 0) {
2390                 Term_what(i - 1, iy, a, c);
2391                 if (iskanji(c)) {
2392                     prc->cell_changes[i - 1].c.w =
2393                         convert_two_byte_eucjp_to_utf16_native(c);
2394                     prc->cell_changes[i - 1].a = a[0];
2395                     prc->cell_changes[i - 1].tcol = 1;
2396                     prc->cell_changes[i].c.w = 0;
2397                     prc->cell_changes[i].a = a[0];
2398                     prc->cell_changes[i].tcol = 0;
2399                     *pclip = i - 1;
2400                     *prend = i - 1;
2401                     --i;
2402                 } else {
2403                     prc->cell_changes[i].c.w = c[1];
2404                     prc->cell_changes[i].a = a[1];
2405                     prc->cell_changes[i].tcol = 0;
2406                     *pclip = i;
2407                     *prend = i;
2408                 }
2409             } else {
2410                 prc->cell_changes[i].c.w = c[1];
2411                 prc->cell_changes[i].a = a[1];
2412                 prc->cell_changes[i].tcol = 0;
2413                 *pclip = i;
2414                 *prend = i;
2415             }
2416 #else
2417             prc->cell_changes[i].c.w = c[1];
2418             prc->cell_changes[i].a = a[1];
2419             prc->cell_changes[i].tcol = 0;
2420             *pclip = i;
2421             *prend = i;
2422 #endif
2423             --i;
2424         } else {
2425             /*
2426              * The cell has been wiped or had changed text rendered.  Do
2427              * not need to render.  Can extend the clipping rectangle into it.
2428              */
2429             *pclip = i;
2430             --i;
2431         }
2432     }
2433 }
2434
2435
2436 /**
2437  * This is a helper function for Term_xtra_cocoa_fresh():  look after a block
2438  * of text on a row to see if the bounds for rendering and clipping need to be
2439  * extended.
2440  */
2441 static void query_after_text(
2442     struct PendingRowChange* prc,
2443     int iy,
2444     int ncol,
2445     int npost,
2446     int* pclip,
2447     int* prend)
2448 {
2449     int end = *prend;
2450     int i = end + 1;
2451
2452     while (1) {
2453         /*
2454          * Be willing to consolidate this block with the one after it.  This
2455          * logic should be sufficient to avoid redraws of the region between
2456          * changed blocks of text if angbandContext->ncol_pre is zero or one.
2457          * For larger values of ncol_pre, would need to do something more to
2458          * avoid extra redraws.
2459          */
2460         if (i >= ncol ||
2461             (i > end + npost &&
2462              prc->cell_changes[i].change_type != CELL_CHANGE_TEXT &&
2463              prc->cell_changes[i].change_type != CELL_CHANGE_WIPE)) {
2464             break;
2465         }
2466
2467         if (prc->cell_changes[i].change_type == CELL_CHANGE_PICT) {
2468             /*
2469              * The cell has been rendered with a tile.  Do not want to modify
2470              * its contents so the clipping and rendering region can not be
2471              * extended.
2472              */
2473             break;
2474         } else if (prc->cell_changes[i].change_type == CELL_CHANGE_NONE) {
2475             /* It has not changed so inquire what it is. */
2476             TERM_COLOR a[2];
2477             char c[2];
2478
2479             Term_what(i, iy, a, c);
2480             if (use_graphics && (a[0] & 0x80) && (c[0] & 0x80)) {
2481                 /*
2482                  * It is an unchanged location rendered with a tile.  Do not
2483                  * want to modify its contents so the clipping and rendering
2484                  * region can not be extended.
2485                  */
2486                 break;
2487             }
2488             /*
2489              * It is unchanged text.  A character from the changed region
2490              * may have extended into it so render it to clear that.
2491              */
2492 #ifdef JP
2493             /* Check to see if it is the first part of a kanji character. */
2494             if (i < ncol - 1) {
2495                 Term_what(i + 1, iy, a + 1, c + 1);
2496                 if (iskanji(c)) {
2497                     prc->cell_changes[i].c.w =
2498                         convert_two_byte_eucjp_to_utf16_native(c);
2499                     prc->cell_changes[i].a = a[0];
2500                     prc->cell_changes[i].tcol = 1;
2501                     prc->cell_changes[i + 1].c.w = 0;
2502                     prc->cell_changes[i + 1].a = a[0];
2503                     prc->cell_changes[i + 1].tcol = 0;
2504                     *pclip = i + 1;
2505                     *prend = i + 1;
2506                     ++i;
2507                 } else {
2508                     prc->cell_changes[i].c.w = c[0];
2509                     prc->cell_changes[i].a = a[0];
2510                     prc->cell_changes[i].tcol = 0;
2511                     *pclip = i;
2512                     *prend = i;
2513                 }
2514             } else {
2515                 prc->cell_changes[i].c.w = c[0];
2516                 prc->cell_changes[i].a = a[0];
2517                 prc->cell_changes[i].tcol = 0;
2518                 *pclip = i;
2519                 *prend = i;
2520             }
2521 #else
2522             prc->cell_changes[i].c.w = c[0];
2523             prc->cell_changes[i].a = a[0];
2524             prc->cell_changes[i].tcol = 0;
2525             *pclip = i;
2526             *prend = i;
2527 #endif
2528             ++i;
2529         } else {
2530             /*
2531              * Have come to another region of changed text or another region
2532              * to wipe.  Combine the regions to minimize redraws.
2533              */
2534             *pclip = i;
2535             *prend = i;
2536             end = i;
2537             ++i;
2538         }
2539     }
2540 }
2541
2542
2543 /**
2544  * Draw the pending changes saved in angbandContext->changes.
2545  */
2546 static void Term_xtra_cocoa_fresh(AngbandContext* angbandContext)
2547 {
2548     int graf_width, graf_height, alphablend;
2549
2550     if (angbandContext->changes->has_pict) {
2551         CGImageAlphaInfo ainfo = CGImageGetAlphaInfo(pict_image);
2552
2553         graf_width = current_graphics_mode->cell_width;
2554         graf_height = current_graphics_mode->cell_height;
2555         /*
2556          * As of this writing, a value of zero for
2557          * current_graphics_mode->alphablend can mean either that the tile set
2558          * doesn't have an alpha channel or it does but it only takes on values
2559          * of 0 or 255.  For main-cocoa.m's purposes, the latter is rendered
2560          * using the same procedure as if alphablend was nonzero.  The former
2561          * is handled differently, but alphablend doesn't distinguish it from
2562          * the latter.  So ignore alphablend and directly test whether an
2563          * alpha channel is present.
2564          */
2565         alphablend = (ainfo & (kCGImageAlphaPremultipliedFirst |
2566                                kCGImageAlphaPremultipliedLast)) ? 1 : 0;
2567     } else {
2568         graf_width = 0;
2569         graf_height = 0;
2570         alphablend = 0;
2571     }
2572
2573     CGContextRef ctx = [angbandContext lockFocus];
2574
2575     if (angbandContext->changes->has_text ||
2576         angbandContext->changes->has_wipe) {
2577         NSFont *selectionFont = [[angbandContext selectionFont] screenFont];
2578         [selectionFont set];
2579     }
2580
2581     int iy;
2582     for (iy = angbandContext->changes->ymin;
2583          iy <= angbandContext->changes->ymax;
2584          ++iy) {
2585         struct PendingRowChange* prc = angbandContext->changes->rows[iy];
2586         int ix;
2587
2588         /* Skip untouched rows. */
2589         if (prc == 0) {
2590             continue;
2591         }
2592
2593         ix = prc->xmin;
2594         while (1) {
2595             int jx;
2596
2597             if (ix > prc->xmax) {
2598                 break;
2599             }
2600
2601             switch (prc->cell_changes[ix].change_type) {
2602             case CELL_CHANGE_NONE:
2603                 ++ix;
2604                 break;
2605
2606             case CELL_CHANGE_PICT:
2607                 {
2608                     /*
2609                      * Because changes are made to the compositing mode, save
2610                      * the incoming value.
2611                      */
2612                     NSGraphicsContext *nsContext =
2613                         [NSGraphicsContext currentContext];
2614                     NSCompositingOperation op = nsContext.compositingOperation;
2615                     int step = (use_bigtile) ? 2 : 1;
2616
2617                     jx = ix;
2618                     while (jx <= prc->xmax &&
2619                            prc->cell_changes[jx].change_type
2620                            == CELL_CHANGE_PICT) {
2621                         NSRect destinationRect =
2622                             [angbandContext rectInImageForTileAtX:jx Y:iy];
2623                         NSRect sourceRect, terrainRect;
2624
2625                         destinationRect.size.width *= step;
2626                         sourceRect.origin.x = graf_width *
2627                             prc->cell_changes[jx].c.c;
2628                         sourceRect.origin.y = graf_height *
2629                             prc->cell_changes[jx].a;
2630                         sourceRect.size.width = graf_width;
2631                         sourceRect.size.height = graf_height;
2632                         terrainRect.origin.x = graf_width *
2633                             prc->cell_changes[jx].tcol;
2634                         terrainRect.origin.y = graf_height *
2635                             prc->cell_changes[jx].trow;
2636                         terrainRect.size.width = graf_width;
2637                         terrainRect.size.height = graf_height;
2638                         if (alphablend) {
2639                             draw_image_tile(
2640                                 nsContext,
2641                                 ctx,
2642                                 pict_image,
2643                                 terrainRect,
2644                                 destinationRect,
2645                                 NSCompositeCopy);
2646                             /*
2647                              * Skip drawing the foreground if it is the same
2648                              * as the background.
2649                              */
2650                             if (sourceRect.origin.x != terrainRect.origin.x ||
2651                                 sourceRect.origin.y != terrainRect.origin.y) {
2652                                 draw_image_tile(
2653                                     nsContext,
2654                                     ctx,
2655                                     pict_image,
2656                                     sourceRect,
2657                                     destinationRect,
2658                                     NSCompositeSourceOver);
2659                             }
2660                         } else {
2661                             draw_image_tile(
2662                                 nsContext,
2663                                 ctx,
2664                                 pict_image,
2665                                 sourceRect,
2666                                 destinationRect,
2667                                 NSCompositeCopy);
2668                         }
2669                         jx += step;
2670                     }
2671
2672                     [nsContext setCompositingOperation:op];
2673
2674                     NSRect rect =
2675                         [angbandContext rectInImageForTileAtX:ix Y:iy];
2676                     rect.size.width =
2677                         angbandContext->tileSize.width * (jx - ix);
2678                     [angbandContext setNeedsDisplayInBaseRect:rect];
2679                 }
2680                 ix = jx;
2681                 break;
2682
2683             case CELL_CHANGE_WIPE:
2684             case CELL_CHANGE_TEXT:
2685                 /*
2686                  * For a wiped region, treat it as if it had text (the only
2687                  * loss if it was not is some extra work rendering
2688                  * neighboring unchanged text).
2689                  */
2690                 jx = ix + 1;
2691                 while (jx < angbandContext->cols &&
2692                        (prc->cell_changes[jx].change_type
2693                         == CELL_CHANGE_TEXT
2694                         || prc->cell_changes[jx].change_type
2695                         == CELL_CHANGE_WIPE)) {
2696                     ++jx;
2697                 }
2698                 {
2699                     int isclip = ix;
2700                     int ieclip = jx - 1;
2701                     int isrend = ix;
2702                     int ierend = jx - 1;
2703                     int set_color = 1;
2704                     TERM_COLOR alast = 0;
2705                     NSRect r;
2706                     int k;
2707
2708                     query_before_text(
2709                         prc, iy, angbandContext->ncol_pre, &isclip, &isrend);
2710                     query_after_text(
2711                         prc,
2712                         iy,
2713                         angbandContext->cols,
2714                         angbandContext->ncol_post,
2715                         &ieclip,
2716                         &ierend
2717                     );
2718                     ix = ierend + 1;
2719
2720                     /* Save the state since the clipping will be modified. */
2721                     CGContextSaveGState(ctx);
2722
2723                     /* Clear the area where rendering will be done. */
2724                     r = [angbandContext rectInImageForTileAtX:isrend Y:iy];
2725                     r.size.width = angbandContext->tileSize.width *
2726                         (ierend - isrend + 1);
2727                     [[NSColor blackColor] set];
2728                     NSRectFill(r);
2729
2730                     /*
2731                      * Clear the current path so it does not affect clipping.
2732                      * Then set the clipping rectangle.  Using
2733                      * CGContextSetTextDrawingMode() to include clipping does
2734                      * not appear to be necessary on 10.14 and is actually
2735                      * detrimental:  when displaying more than one character,
2736                      * only the first is visible.
2737                      */
2738                     CGContextBeginPath(ctx);
2739                     r = [angbandContext rectInImageForTileAtX:isclip Y:iy];
2740                     r.size.width = angbandContext->tileSize.width *
2741                         (ieclip - isclip + 1);
2742                     CGContextClipToRect(ctx, r);
2743
2744                     /* Render. */
2745                     k = isrend;
2746                     while (k <= ierend) {
2747                         NSRect rectToDraw;
2748
2749                         if (prc->cell_changes[k].change_type
2750                             == CELL_CHANGE_WIPE) {
2751                             /* Skip over since no rendering is necessary. */
2752                             ++k;
2753                             continue;
2754                         }
2755
2756                         if (set_color || alast != prc->cell_changes[k].a) {
2757                             set_color = 0;
2758                             alast = prc->cell_changes[k].a;
2759                             set_color_for_index(alast % MAX_COLORS);
2760                         }
2761
2762                         rectToDraw =
2763                             [angbandContext rectInImageForTileAtX:k Y:iy];
2764                         if (prc->cell_changes[k].tcol) {
2765                             rectToDraw.size.width *= 2.0;
2766                             [angbandContext drawWChar:prc->cell_changes[k].c.w
2767                                             inRect:rectToDraw context:ctx];
2768                             k += 2;
2769                         } else {
2770                             [angbandContext drawWChar:prc->cell_changes[k].c.w
2771                                             inRect:rectToDraw context:ctx];
2772                             ++k;
2773                         }
2774                     }
2775
2776                     /*
2777                      * Inform the context that the area in the clipping
2778                      * rectangle needs to be redisplayed.
2779                      */
2780                     [angbandContext setNeedsDisplayInBaseRect:r];
2781
2782                     CGContextRestoreGState(ctx);
2783                 }
2784                 break;
2785             }
2786         }
2787     }
2788
2789     if (angbandContext->changes->xcurs >= 0 &&
2790         angbandContext->changes->ycurs >= 0) {
2791         NSRect rect = [angbandContext
2792                           rectInImageForTileAtX:angbandContext->changes->xcurs
2793                           Y:angbandContext->changes->ycurs];
2794
2795         if (angbandContext->changes->bigcurs) {
2796             rect.size.width += angbandContext->tileSize.width;
2797         }
2798         [[NSColor yellowColor] set];
2799         NSFrameRectWithWidth(rect, 1);
2800         /* Invalidate that rect */
2801         [angbandContext setNeedsDisplayInBaseRect:rect];
2802     }
2803
2804     [angbandContext unlockFocus];
2805 }
2806
2807
2808 /**
2809  * Do a "special thing"
2810  */
2811 static errr Term_xtra_cocoa(int n, int v)
2812 {
2813     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2814     AngbandContext* angbandContext = Term->data;
2815     
2816     errr result = 0;
2817     
2818     /* Analyze */
2819     switch (n)
2820     {
2821                 /* Make a noise */
2822         case TERM_XTRA_NOISE:
2823         {
2824             NSBeep();
2825             
2826             /* Success */
2827             break;
2828         }
2829
2830         /*  Make a sound */
2831         case TERM_XTRA_SOUND:
2832             play_sound(v);
2833             break;
2834
2835             /* Process random events */
2836         case TERM_XTRA_BORED:
2837         {
2838             /* Show or hide cocoa windows based on the subwindow flags set by
2839                          * the user */
2840             AngbandUpdateWindowVisibility();
2841
2842             /* Process an event */
2843             (void)check_events(CHECK_EVENTS_NO_WAIT);
2844             
2845             /* Success */
2846             break;
2847         }
2848             
2849                 /* Process pending events */
2850         case TERM_XTRA_EVENT:
2851         {
2852             /* Process an event */
2853             (void)check_events(v);
2854             
2855             /* Success */
2856             break;
2857         }
2858             
2859                 /* Flush all pending events (if any) */
2860         case TERM_XTRA_FLUSH:
2861         {
2862             /* Hack -- flush all events */
2863             while (check_events(CHECK_EVENTS_DRAIN)) /* loop */;
2864             
2865             /* Success */
2866             break;
2867         }
2868             
2869                 /* Hack -- Change the "soft level" */
2870         case TERM_XTRA_LEVEL:
2871         {
2872             /* Here we could activate (if requested), but I don't think Angband
2873                          * should be telling us our window order (the user should decide
2874                          * that), so do nothing. */            
2875             break;
2876         }
2877             
2878                 /* Clear the screen */
2879         case TERM_XTRA_CLEAR:
2880         {        
2881             [angbandContext lockFocus];
2882             [[NSColor blackColor] set];
2883             NSRect imageRect = {NSZeroPoint, [angbandContext imageSize]};
2884             NSRectFillUsingOperation(imageRect, NSCompositeCopy);
2885             [angbandContext unlockFocus];
2886             [angbandContext setNeedsDisplay:YES];
2887             /* Success */
2888             break;
2889         }
2890             
2891                 /* React to changes */
2892         case TERM_XTRA_REACT:
2893         {
2894             /* React to changes */
2895             return (Term_xtra_cocoa_react());
2896         }
2897             
2898                 /* Delay (milliseconds) */
2899         case TERM_XTRA_DELAY:
2900         {
2901             /* If needed */
2902             if (v > 0)
2903             {
2904                 
2905                 double seconds = v / 1000.;
2906                 NSDate* date = [NSDate dateWithTimeIntervalSinceNow:seconds];
2907                 do
2908                 {
2909                     NSEvent* event;
2910                     do
2911                     {
2912                         event = [NSApp nextEventMatchingMask:-1 untilDate:date inMode:NSDefaultRunLoopMode dequeue:YES];
2913                         if (event) send_event(event);
2914                     } while (event);
2915                 } while ([date timeIntervalSinceNow] >= 0);
2916                 
2917             }
2918             
2919             /* Success */
2920             break;
2921         }
2922             
2923         case TERM_XTRA_FRESH:
2924             /* Draw the pending changes. */
2925             if (angbandContext->changes != 0) {
2926                 Term_xtra_cocoa_fresh(angbandContext);
2927                 clear_pending_changes(angbandContext->changes);
2928             }
2929             break;
2930             
2931         default:
2932             /* Oops */
2933             result = 1;
2934             break;
2935     }
2936     
2937     [pool drain];
2938     
2939     /* Oops */
2940     return result;
2941 }
2942
2943 static errr Term_curs_cocoa(int x, int y)
2944 {
2945     AngbandContext *angbandContext = Term->data;
2946
2947     if (angbandContext->changes == 0) {
2948         /* Bail out; there was an earlier memory allocation failure. */
2949         return 1;
2950     }
2951     angbandContext->changes->xcurs = x;
2952     angbandContext->changes->ycurs = y;
2953     angbandContext->changes->bigcurs = 0;
2954
2955     /* Success */
2956     return 0;
2957 }
2958
2959 /**
2960  * Draw a cursor that's two tiles wide.  For Japanese, that's used when
2961  * the cursor points at a kanji character, irregardless of whether operating
2962  * in big tile mode.
2963  */
2964 static errr Term_bigcurs_cocoa(int x, int y)
2965 {
2966     AngbandContext *angbandContext = Term->data;
2967
2968     if (angbandContext->changes == 0) {
2969         /* Bail out; there was an earlier memory allocation failure. */
2970         return 1;
2971     }
2972     angbandContext->changes->xcurs = x;
2973     angbandContext->changes->ycurs = y;
2974     angbandContext->changes->bigcurs = 1;
2975
2976     /* Success */
2977     return 0;
2978 }
2979
2980 /**
2981  * Low level graphics (Assumes valid input)
2982  *
2983  * Erase "n" characters starting at (x,y)
2984  */
2985 static errr Term_wipe_cocoa(int x, int y, int n)
2986 {
2987     AngbandContext *angbandContext = Term->data;
2988     struct PendingCellChange *pc;
2989
2990     if (angbandContext->changes == 0) {
2991         /* Bail out; there was an earlier memory allocation failure. */
2992         return 1;
2993     }
2994     if (angbandContext->changes->rows[y] == 0) {
2995         angbandContext->changes->rows[y] =
2996             create_row_change(angbandContext->cols);
2997         if (angbandContext->changes->rows[y] == 0) {
2998             NSLog(@"failed to allocate changes for row %d", y);
2999             return 1;
3000         }
3001         if (angbandContext->changes->ymin > y) {
3002             angbandContext->changes->ymin = y;
3003         }
3004         if (angbandContext->changes->ymax < y) {
3005             angbandContext->changes->ymax = y;
3006         }
3007     }
3008
3009     angbandContext->changes->has_wipe = 1;
3010     if (angbandContext->changes->rows[y]->xmin > x) {
3011         angbandContext->changes->rows[y]->xmin = x;
3012     }
3013     if (angbandContext->changes->rows[y]->xmax < x + n - 1) {
3014         angbandContext->changes->rows[y]->xmax = x + n - 1;
3015     }
3016     for (pc = angbandContext->changes->rows[y]->cell_changes + x;
3017          pc != angbandContext->changes->rows[y]->cell_changes + x + n;
3018          ++pc) {
3019         pc->change_type = CELL_CHANGE_WIPE;
3020     }
3021     
3022     /* Success */
3023     return (0);
3024 }
3025
3026 static errr Term_pict_cocoa(int x, int y, int n, TERM_COLOR *ap,
3027                             const char *cp, const TERM_COLOR *tap,
3028                             const char *tcp)
3029 {
3030     
3031     /* Paranoia: Bail if we don't have a current graphics mode */
3032     if (! current_graphics_mode) return -1;
3033     
3034     AngbandContext* angbandContext = Term->data;
3035     int any_change = 0;
3036     int step = (use_bigtile) ? 2 : 1;
3037     struct PendingCellChange *pc;
3038
3039     if (angbandContext->changes == 0) {
3040         /* Bail out; there was an earlier memory allocation failure. */
3041         return 1;
3042     }
3043     /*
3044      * In bigtile mode, it is sufficient that the bounds for the modified
3045      * region only encompass the left cell for the region affected by the
3046      * tile and that only that cell has to have the details of the changes.
3047      */
3048     if (angbandContext->changes->rows[y] == 0) {
3049         angbandContext->changes->rows[y] =
3050             create_row_change(angbandContext->cols);
3051         if (angbandContext->changes->rows[y] == 0) {
3052             NSLog(@"failed to allocate changes for row %d", y);
3053             return 1;
3054         }
3055         if (angbandContext->changes->ymin > y) {
3056             angbandContext->changes->ymin = y;
3057         }
3058         if (angbandContext->changes->ymax < y) {
3059             angbandContext->changes->ymax = y;
3060         }
3061     }
3062
3063     if (angbandContext->changes->rows[y]->xmin > x) {
3064         angbandContext->changes->rows[y]->xmin = x;
3065     }
3066     if (angbandContext->changes->rows[y]->xmax < x + step * (n - 1)) {
3067         angbandContext->changes->rows[y]->xmax = x + step * (n - 1);
3068     }
3069     for (pc = angbandContext->changes->rows[y]->cell_changes + x;
3070          pc != angbandContext->changes->rows[y]->cell_changes + x + step * n;
3071          pc += step) {
3072         TERM_COLOR a = *ap++;
3073         char c = *cp++;
3074         TERM_COLOR ta = *tap++;
3075         char tc = *tcp++;
3076
3077         if (use_graphics && (a & 0x80) && (c & 0x80)) {
3078             pc->c.c = ((byte)c & 0x7F) % pict_cols;
3079             pc->a = ((byte)a & 0x7F) % pict_rows;
3080             pc->tcol = ((byte)tc & 0x7F) % pict_cols;
3081             pc->trow = ((byte)ta & 0x7F) % pict_rows;
3082             pc->change_type = CELL_CHANGE_PICT;
3083             any_change = 1;
3084         }
3085     }
3086     if (any_change) {
3087         angbandContext->changes->has_pict = 1;
3088     }
3089     
3090     /* Success */
3091     return (0);
3092 }
3093
3094 /**
3095  * Low level graphics.  Assumes valid input.
3096  *
3097  * Draw several ("n") chars, with an attr, at a given location.
3098  */
3099 static errr Term_text_cocoa(int x, int y, int n, byte_hack a, concptr cp)
3100 {
3101     AngbandContext* angbandContext = Term->data;
3102     struct PendingCellChange *pc;
3103
3104     if (angbandContext->changes == 0) {
3105         /* Bail out; there was an earlier memory allocation failure. */
3106         return 1;
3107     }
3108     if (angbandContext->changes->rows[y] == 0) {
3109         angbandContext->changes->rows[y] =
3110             create_row_change(angbandContext->cols);
3111         if (angbandContext->changes->rows[y] == 0) {
3112             NSLog(@"failed to allocate changes for row %d", y);
3113             return 1;
3114         }
3115         if (angbandContext->changes->ymin > y) {
3116             angbandContext->changes->ymin = y;
3117         }
3118         if (angbandContext->changes->ymax < y) {
3119             angbandContext->changes->ymax = y;
3120         }
3121     }
3122
3123     angbandContext->changes->has_text = 1;
3124     if (angbandContext->changes->rows[y]->xmin > x) {
3125         angbandContext->changes->rows[y]->xmin = x;
3126     }
3127     if (angbandContext->changes->rows[y]->xmax < x + n - 1) {
3128         angbandContext->changes->rows[y]->xmax = x + n - 1;
3129     }
3130     pc = angbandContext->changes->rows[y]->cell_changes + x;
3131     while (pc != angbandContext->changes->rows[y]->cell_changes + x + n) {
3132 #ifdef JP
3133         if (iskanji(*cp)) {
3134             if (pc + 1 ==
3135                 angbandContext->changes->rows[y]->cell_changes + x + n) {
3136                 /*
3137                  * The second byte of the character is past the end.  Ignore
3138                  * the character.
3139                  */
3140                 break;
3141             } else {
3142                 pc->c.w = convert_two_byte_eucjp_to_utf16_native(cp);
3143                 pc->a = a;
3144                 pc->tcol = 1;
3145                 pc->change_type = CELL_CHANGE_TEXT;
3146                 ++pc;
3147                 /*
3148                  * Fill in a dummy value since the previous character will take
3149                  * up two columns.
3150                  */
3151                 pc->c.w = 0;
3152                 pc->a = a;
3153                 pc->tcol = 0;
3154                 pc->change_type = CELL_CHANGE_TEXT;
3155                 ++pc;
3156                 cp += 2;
3157             }
3158         } else {
3159             pc->c.w = *cp;
3160             pc->a = a;
3161             pc->tcol = 0;
3162             pc->change_type = CELL_CHANGE_TEXT;
3163             ++pc;
3164             ++cp;
3165         }
3166 #else
3167         pc->c.w = *cp;
3168         pc->a = a;
3169         pc->tcol = 0;
3170         pc->change_type = CELL_CHANGE_TEXT;
3171         ++pc;
3172         ++cp;
3173 #endif
3174     }
3175     
3176     /* Success */
3177     return (0);
3178 }
3179
3180 /**
3181  * Post a nonsense event so that our event loop wakes up
3182  */
3183 static void wakeup_event_loop(void)
3184 {
3185     /* Big hack - send a nonsense event to make us update */
3186     NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined location:NSZeroPoint modifierFlags:0 timestamp:0 windowNumber:0 context:NULL subtype:AngbandEventWakeup data1:0 data2:0];
3187     [NSApp postEvent:event atStart:NO];
3188 }
3189
3190
3191 /**
3192  * Create and initialize window number "i"
3193  */
3194 static term *term_data_link(int i)
3195 {
3196     NSArray *terminalDefaults = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
3197     NSInteger rows = 24;
3198     NSInteger columns = 80;
3199
3200     if( i < (int)[terminalDefaults count] )
3201     {
3202         NSDictionary *term = [terminalDefaults objectAtIndex: i];
3203         rows = [[term valueForKey: AngbandTerminalRowsDefaultsKey] integerValue];
3204         columns = [[term valueForKey: AngbandTerminalColumnsDefaultsKey] integerValue];
3205     }
3206
3207     /* Allocate */
3208     term *newterm = ZNEW(term);
3209
3210     /* Initialize the term */
3211     term_init(newterm, columns, rows, 256 /* keypresses, for some reason? */);
3212     
3213     /* Differentiate between BS/^h, Tab/^i, etc. */
3214     /* newterm->complex_input = TRUE; */
3215
3216     /* Use a "software" cursor */
3217     newterm->soft_cursor = TRUE;
3218     
3219     /* Disable the per-row flush notifications since they are not used. */
3220     newterm->never_frosh = TRUE;
3221
3222     /* Erase with "white space" */
3223     newterm->attr_blank = TERM_WHITE;
3224     newterm->char_blank = ' ';
3225     
3226     /* Prepare the init/nuke hooks */
3227     newterm->init_hook = Term_init_cocoa;
3228     newterm->nuke_hook = Term_nuke_cocoa;
3229     
3230     /* Prepare the function hooks */
3231     newterm->xtra_hook = Term_xtra_cocoa;
3232     newterm->wipe_hook = Term_wipe_cocoa;
3233     newterm->curs_hook = Term_curs_cocoa;
3234     newterm->bigcurs_hook = Term_bigcurs_cocoa;
3235     newterm->text_hook = Term_text_cocoa;
3236     newterm->pict_hook = Term_pict_cocoa;
3237     /* newterm->mbcs_hook = Term_mbcs_cocoa; */
3238     
3239     /* Global pointer */
3240     angband_term[i] = newterm;
3241     
3242     return newterm;
3243 }
3244
3245 /**
3246  * Load preferences from preferences file for current host+current user+
3247  * current application.
3248  */
3249 static void load_prefs()
3250 {
3251     NSUserDefaults *defs = [NSUserDefaults angbandDefaults];
3252     
3253     /* Make some default defaults */
3254     NSMutableArray *defaultTerms = [[NSMutableArray alloc] init];
3255
3256     /* The following default rows/cols were determined experimentally by first
3257          * finding the ideal window/font size combinations. But because of awful
3258          * temporal coupling in Term_init_cocoa(), it's impossible to set up the
3259          * defaults there, so we do it this way. */
3260     for( NSUInteger i = 0; i < ANGBAND_TERM_MAX; i++ )
3261     {
3262                 int columns, rows;
3263                 BOOL visible = YES;
3264
3265                 switch( i )
3266                 {
3267                         case 0:
3268                                 columns = 129;
3269                                 rows = 32;
3270                                 break;
3271                         case 1:
3272                                 columns = 84;
3273                                 rows = 20;
3274                                 break;
3275                         case 2:
3276                                 columns = 42;
3277                                 rows = 24;
3278                                 break;
3279                         case 3:
3280                                 columns = 42;
3281                                 rows = 20;
3282                                 break;
3283                         case 4:
3284                                 columns = 42;
3285                                 rows = 16;
3286                                 break;
3287                         case 5:
3288                                 columns = 84;
3289                                 rows = 20;
3290                                 break;
3291                         default:
3292                                 columns = 80;
3293                                 rows = 24;
3294                                 visible = NO;
3295                                 break;
3296                 }
3297
3298                 NSDictionary *standardTerm = [NSDictionary dictionaryWithObjectsAndKeys:
3299                                                                           [NSNumber numberWithInt: rows], AngbandTerminalRowsDefaultsKey,
3300                                                                           [NSNumber numberWithInt: columns], AngbandTerminalColumnsDefaultsKey,
3301                                                                           [NSNumber numberWithBool: visible], AngbandTerminalVisibleDefaultsKey,
3302                                                                           nil];
3303         [defaultTerms addObject: standardTerm];
3304     }
3305
3306     NSDictionary *defaults = [[NSDictionary alloc] initWithObjectsAndKeys:
3307 #ifdef JP
3308                               @"Osaka", @"FontName",
3309 #else
3310                               @"Menlo", @"FontName",
3311 #endif
3312                               [NSNumber numberWithFloat:13.f], @"FontSize",
3313                               [NSNumber numberWithInt:60], AngbandFrameRateDefaultsKey,
3314                               [NSNumber numberWithBool:YES], AngbandSoundDefaultsKey,
3315                               [NSNumber numberWithInt:GRAPHICS_NONE], AngbandGraphicsDefaultsKey,
3316                               defaultTerms, AngbandTerminalsDefaultsKey,
3317                               nil];
3318     [defs registerDefaults:defaults];
3319     [defaults release];
3320     [defaultTerms release];
3321     
3322     /* Preferred graphics mode */
3323     graf_mode_req = [defs integerForKey:AngbandGraphicsDefaultsKey];
3324     
3325     /* Use sounds; set the Angband global */
3326     use_sound = ([defs boolForKey:AngbandSoundDefaultsKey] == YES) ? TRUE : FALSE;
3327     
3328     /* fps */
3329     frames_per_second = [defs integerForKey:AngbandFrameRateDefaultsKey];
3330     
3331     /* Font */
3332     default_font = [[NSFont fontWithName:[defs valueForKey:@"FontName-0"] size:[defs floatForKey:@"FontSize-0"]] retain];
3333     if (! default_font) default_font = [[NSFont fontWithName:@"Menlo" size:13.] retain];
3334 }
3335
3336 /**
3337  * Arbitary limit on number of possible samples per event
3338  */
3339 #define MAX_SAMPLES            16
3340
3341 /**
3342  * Struct representing all data for a set of event samples
3343  */
3344 typedef struct
3345 {
3346         int num;        /* Number of available samples for this event */
3347         NSSound *sound[MAX_SAMPLES];
3348 } sound_sample_list;
3349
3350 /**
3351  * Array of event sound structs
3352  */
3353 static sound_sample_list samples[MSG_MAX];
3354
3355
3356 /**
3357  * Load sound effects based on sound.cfg within the xtra/sound directory;
3358  * bridge to Cocoa to use NSSound for simple loading and playback, avoiding
3359  * I/O latency by cacheing all sounds at the start.  Inherits full sound
3360  * format support from Quicktime base/plugins.
3361  * pelpel favoured a plist-based parser for the future but .cfg support
3362  * improves cross-platform compatibility.
3363  */
3364 static void load_sounds(void)
3365 {
3366         char sound_dir[1024];
3367         char path[1024];
3368         char buffer[2048];
3369         FILE *fff;
3370     
3371         /* Build the "sound" path */
3372         path_build(sound_dir, sizeof(sound_dir), ANGBAND_DIR_XTRA, "sound");
3373     
3374         /* Find and open the config file */
3375         path_build(path, sizeof(path), sound_dir, "sound.cfg");
3376         fff = my_fopen(path, "r");
3377     
3378         /* Handle errors */
3379         if (!fff)
3380         {
3381                 NSLog(@"The sound configuration file could not be opened.");
3382                 return;
3383         }
3384         
3385         /* Instantiate an autorelease pool for use by NSSound */
3386         NSAutoreleasePool *autorelease_pool;
3387         autorelease_pool = [[NSAutoreleasePool alloc] init];
3388     
3389     /* Use a dictionary to unique sounds, so we can share NSSounds across
3390          * multiple events */
3391     NSMutableDictionary *sound_dict = [NSMutableDictionary dictionary];
3392     
3393         /*
3394          * This loop may take a while depending on the count and size of samples
3395          * to load.
3396          */
3397     
3398         /* Parse the file */
3399         /* Lines are always of the form "name = sample [sample ...]" */
3400         while (my_fgets(fff, buffer, sizeof(buffer)) == 0)
3401         {
3402                 char *msg_name;
3403                 char *cfg_sample_list;
3404                 char *search;
3405                 char *cur_token;
3406                 char *next_token;
3407                 int event;
3408         
3409                 /* Skip anything not beginning with an alphabetic character */
3410                 if (!buffer[0] || !isalpha((unsigned char)buffer[0])) continue;
3411         
3412                 /* Split the line into two: message name, and the rest */
3413                 search = strchr(buffer, ' ');
3414                 cfg_sample_list = strchr(search + 1, ' ');
3415                 if (!search) continue;
3416                 if (!cfg_sample_list) continue;
3417         
3418                 /* Set the message name, and terminate at first space */
3419                 msg_name = buffer;
3420                 search[0] = '\0';
3421         
3422                 /* Make sure this is a valid event name */
3423                 for (event = MSG_MAX - 1; event >= 0; event--)
3424                 {
3425                         if (strcmp(msg_name, angband_sound_name[event]) == 0)
3426                                 break;
3427                 }
3428                 if (event < 0) continue;
3429         
3430                 /* Advance the sample list pointer so it's at the beginning of text */
3431                 cfg_sample_list++;
3432                 if (!cfg_sample_list[0]) continue;
3433         
3434                 /* Terminate the current token */
3435                 cur_token = cfg_sample_list;
3436                 search = strchr(cur_token, ' ');
3437                 if (search)
3438                 {
3439                         search[0] = '\0';
3440                         next_token = search + 1;
3441                 }
3442                 else
3443                 {
3444                         next_token = NULL;
3445                 }
3446         
3447                 /*
3448                  * Now we find all the sample names and add them one by one
3449                  */
3450                 while (cur_token)
3451                 {
3452                         int num = samples[event].num;
3453             
3454                         /* Don't allow too many samples */
3455                         if (num >= MAX_SAMPLES) break;
3456             
3457             NSString *token_string = [NSString stringWithUTF8String:cur_token];
3458             NSSound *sound = [sound_dict objectForKey:token_string];
3459             
3460             if (! sound)
3461             {
3462                 struct stat stb;
3463
3464                 /* We have to load the sound. Build the path to the sample */
3465                 path_build(path, sizeof(path), sound_dir, cur_token);
3466                 if (stat(path, &stb) == 0)
3467                 {
3468                     
3469                     /* Load the sound into memory */
3470                     sound = [[[NSSound alloc] initWithContentsOfFile:[NSString stringWithUTF8String:path] byReference:YES] autorelease];
3471                     if (sound) [sound_dict setObject:sound forKey:token_string];
3472                 }
3473             }
3474             
3475             /* Store it if we loaded it */
3476             if (sound)
3477             {
3478                 samples[event].sound[num] = [sound retain];
3479                 
3480                 /* Imcrement the sample count */
3481                 samples[event].num++;
3482             }
3483             
3484             
3485                         /* Figure out next token */
3486                         cur_token = next_token;
3487                         if (next_token)
3488                         {
3489                                 /* Try to find a space */
3490                                 search = strchr(cur_token, ' ');
3491                 
3492                                 /* If we can find one, terminate, and set new "next" */
3493                                 if (search)
3494                                 {
3495                                         search[0] = '\0';
3496                                         next_token = search + 1;
3497                                 }
3498                                 else
3499                                 {
3500                                         /* Otherwise prevent infinite looping */
3501                                         next_token = NULL;
3502                                 }
3503                         }
3504                 }
3505         }
3506     
3507         /* Release the autorelease pool */
3508         [autorelease_pool release];
3509     
3510         /* Close the file */
3511         my_fclose(fff);
3512 }
3513
3514 /**
3515  * Play sound effects asynchronously.  Select a sound from any available
3516  * for the required event, and bridge to Cocoa to play it.
3517  */
3518 static void play_sound(int event)
3519 {    
3520         /* Paranoia */
3521         if (event < 0 || event >= MSG_MAX) return;
3522     
3523     /* Load sounds just-in-time (once) */
3524     static BOOL loaded = NO;
3525     if (!loaded) {
3526         loaded = YES;
3527         load_sounds();
3528     }
3529     
3530     /* Check there are samples for this event */
3531     if (!samples[event].num) return;
3532     
3533     /* Instantiate an autorelease pool for use by NSSound */
3534     NSAutoreleasePool *autorelease_pool;
3535     autorelease_pool = [[NSAutoreleasePool alloc] init];
3536     
3537     /* Choose a random event */
3538     int s = randint0(samples[event].num);
3539     
3540     /* Stop the sound if it's currently playing */
3541     if ([samples[event].sound[s] isPlaying])
3542         [samples[event].sound[s] stop];
3543     
3544     /* Play the sound */
3545     [samples[event].sound[s] play];
3546     
3547     /* Release the autorelease pool */
3548     [autorelease_pool drain];
3549 }
3550
3551 /*
3552  * 
3553  */
3554 static void init_windows(void)
3555 {
3556     /* Create the main window */
3557     term *primary = term_data_link(0);
3558     
3559     /* Prepare to create any additional windows */
3560     int i;
3561     for (i=1; i < ANGBAND_TERM_MAX; i++) {
3562         term_data_link(i);
3563     }
3564     
3565     /* Activate the primary term */
3566     Term_activate(primary);
3567 }
3568
3569 /**
3570  * Handle the "open_when_ready" flag
3571  */
3572 static void handle_open_when_ready(void)
3573 {
3574     /* Check the flag XXX XXX XXX make a function for this */
3575     if (open_when_ready && initialized && !game_in_progress)
3576     {
3577         /* Forget */
3578         open_when_ready = FALSE;
3579         
3580         /* Game is in progress */
3581         game_in_progress = TRUE;
3582         
3583         /* Wait for a keypress */
3584         pause_line(Term->hgt - 1);
3585     }
3586 }
3587
3588
3589 /**
3590  * Handle quit_when_ready, by Peter Ammon,
3591  * slightly modified to check inkey_flag.
3592  */
3593 static void quit_calmly(void)
3594 {
3595     /* Quit immediately if game's not started */
3596     if (!game_in_progress || !character_generated) quit(NULL);
3597
3598     /* Save the game and Quit (if it's safe) */
3599     if (inkey_flag)
3600     {
3601         /* Hack -- Forget messages and term */
3602         msg_flag = FALSE;
3603                 Term->mapped_flag = FALSE;
3604
3605         /* Save the game */
3606         do_cmd_save_game(FALSE);
3607         record_current_savefile();
3608
3609         /* Quit */
3610         quit(NULL);
3611     }
3612
3613     /* Wait until inkey_flag is set */
3614 }
3615
3616
3617
3618 /**
3619  * Returns YES if we contain an AngbandView (and hence should direct our events
3620  * to Angband)
3621  */
3622 static BOOL contains_angband_view(NSView *view)
3623 {
3624     if ([view isKindOfClass:[AngbandView class]]) return YES;
3625     for (NSView *subview in [view subviews]) {
3626         if (contains_angband_view(subview)) return YES;
3627     }
3628     return NO;
3629 }
3630
3631
3632 /**
3633  * Queue mouse presses if they occur in the map section of the main window.
3634  */
3635 static void AngbandHandleEventMouseDown( NSEvent *event )
3636 {
3637 #if 0
3638         AngbandContext *angbandContext = [[[event window] contentView] angbandContext];
3639         AngbandContext *mainAngbandContext = angband_term[0]->data;
3640
3641         if (mainAngbandContext->primaryWindow && [[event window] windowNumber] == [mainAngbandContext->primaryWindow windowNumber])
3642         {
3643                 int cols, rows, x, y;
3644                 Term_get_size(&cols, &rows);
3645                 NSSize tileSize = angbandContext->tileSize;
3646                 NSSize border = angbandContext->borderSize;
3647                 NSPoint windowPoint = [event locationInWindow];
3648
3649                 /* Adjust for border; add border height because window origin is at
3650                  * bottom */
3651                 windowPoint = NSMakePoint( windowPoint.x - border.width, windowPoint.y + border.height );
3652
3653                 NSPoint p = [[[event window] contentView] convertPoint: windowPoint fromView: nil];
3654                 x = floor( p.x / tileSize.width );
3655                 y = floor( p.y / tileSize.height );
3656
3657                 /* Being safe about this, since xcode doesn't seem to like the
3658                  * bool_hack stuff */
3659                 BOOL displayingMapInterface = ((int)inkey_flag != 0);
3660
3661                 /* Sidebar plus border == thirteen characters; top row is reserved. */
3662                 /* Coordinates run from (0,0) to (cols-1, rows-1). */
3663                 BOOL mouseInMapSection = (x > 13 && x <= cols - 1 && y > 0  && y <= rows - 2);
3664
3665                 /* If we are displaying a menu, allow clicks anywhere; if we are
3666                  * displaying the main game interface, only allow clicks in the map
3667                  * section */
3668                 if (!displayingMapInterface || (displayingMapInterface && mouseInMapSection))
3669                 {
3670                         /* [event buttonNumber] will return 0 for left click,
3671                          * 1 for right click, but this is safer */
3672                         int button = ([event type] == NSLeftMouseDown) ? 1 : 2;
3673
3674 #ifdef KC_MOD_ALT
3675                         NSUInteger eventModifiers = [event modifierFlags];
3676                         byte angbandModifiers = 0;
3677                         angbandModifiers |= (eventModifiers & NSShiftKeyMask) ? KC_MOD_SHIFT : 0;
3678                         angbandModifiers |= (eventModifiers & NSControlKeyMask) ? KC_MOD_CONTROL : 0;
3679                         angbandModifiers |= (eventModifiers & NSAlternateKeyMask) ? KC_MOD_ALT : 0;
3680                         button |= (angbandModifiers & 0x0F) << 4; /* encode modifiers in the button number (see Term_mousepress()) */
3681 #endif
3682
3683                         Term_mousepress(x, y, button);
3684                 }
3685         }
3686 #endif
3687     /* Pass click through to permit focus change, resize, etc. */
3688     [NSApp sendEvent:event];
3689 }
3690
3691
3692
3693 /**
3694  * Encodes an NSEvent Angband-style, or forwards it along.  Returns YES if the
3695  * event was sent to Angband, NO if Cocoa (or nothing) handled it */
3696 static BOOL send_event(NSEvent *event)
3697 {
3698
3699     /* If the receiving window is not an Angband window, then do nothing */
3700     if (! contains_angband_view([[event window] contentView]))
3701     {
3702         [NSApp sendEvent:event];
3703         return NO;
3704     }
3705
3706     /* Analyze the event */
3707     switch ([event type])
3708     {
3709         case NSKeyDown:
3710         {
3711             /* Try performing a key equivalent */
3712             if ([[NSApp mainMenu] performKeyEquivalent:event]) break;
3713             
3714             unsigned modifiers = [event modifierFlags];
3715             
3716             /* Send all NSCommandKeyMasks through */
3717             if (modifiers & NSCommandKeyMask)
3718             {
3719                 [NSApp sendEvent:event];
3720                 break;
3721             }
3722             
3723             if (! [[event characters] length]) break;
3724             
3725             
3726             /* Extract some modifiers */
3727             int mc = !! (modifiers & NSControlKeyMask);
3728             int ms = !! (modifiers & NSShiftKeyMask);
3729             int mo = !! (modifiers & NSAlternateKeyMask);
3730             int kp = !! (modifiers & NSNumericPadKeyMask);
3731             
3732             
3733             /* Get the Angband char corresponding to this unichar */
3734             unichar c = [[event characters] characterAtIndex:0];
3735             char ch;
3736             /*
3737              * Have anything from the numeric keypad generate a macro
3738              * trigger so that shift or control modifiers can be passed.
3739              */
3740             if (c <= 0x7F && !kp)
3741             {
3742                 ch = (char) c;
3743             }
3744             else {
3745                 /*
3746                  * The rest of Hengband uses Angband 2.7's or so key handling:
3747                  * so for the rest do something like the encoding that
3748                  * main-win.c does:  send a macro trigger with the Unicode
3749                  * value encoded into printable ASCII characters.
3750                  */
3751                 ch = '\0';
3752             }
3753             
3754             /* override special keys */
3755             switch([event keyCode]) {
3756                 case kVK_Return: ch = '\r'; break;
3757                 case kVK_Escape: ch = 27; break;
3758                 case kVK_Tab: ch = '\t'; break;
3759                 case kVK_Delete: ch = '\b'; break;
3760                 case kVK_ANSI_KeypadEnter: ch = '\r'; kp = TRUE; break;
3761             }
3762
3763             /* Hide the mouse pointer */
3764             [NSCursor setHiddenUntilMouseMoves:YES];
3765             
3766             /* Enqueue it */
3767             if (ch != '\0')
3768             {
3769                 Term_keypress(ch);
3770             }
3771             else
3772             {
3773                 /*
3774                  * Could use the hexsym global but some characters overlap with
3775                  * those used to indicate modifiers.
3776                  */
3777                 const char encoded[16] = {
3778                     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
3779                     'c', 'd', 'e', 'f'
3780                 };
3781
3782                 /* Begin the macro trigger. */
3783                 Term_keypress(31);
3784
3785                 /* Send the modifiers. */
3786                 if (mc) Term_keypress('C');
3787                 if (ms) Term_keypress('S');
3788                 if (mo) Term_keypress('O');
3789                 if (kp) Term_keypress('K');
3790
3791                 do {
3792                     Term_keypress(encoded[c & 0xF]);
3793                     c >>= 4;
3794                 } while (c > 0);
3795
3796                 /* End the macro trigger. */
3797                 Term_keypress(13);
3798             }
3799             
3800             break;
3801         }
3802             
3803         case NSLeftMouseDown:
3804                 case NSRightMouseDown:
3805                         AngbandHandleEventMouseDown(event);
3806             break;
3807
3808         case NSApplicationDefined:
3809         {
3810             if ([event subtype] == AngbandEventWakeup)
3811             {
3812                 return YES;
3813             }
3814             break;
3815         }
3816             
3817         default:
3818             [NSApp sendEvent:event];
3819             return YES;
3820     }
3821     return YES;
3822 }
3823
3824 /**
3825  * Check for Events, return TRUE if we process any
3826  */
3827 static BOOL check_events(int wait)
3828
3829     
3830     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3831     
3832     /* Handles the quit_when_ready flag */
3833     if (quit_when_ready) quit_calmly();
3834     
3835     NSDate* endDate;
3836     if (wait == CHECK_EVENTS_WAIT) endDate = [NSDate distantFuture];
3837     else endDate = [NSDate distantPast];
3838     
3839     NSEvent* event;
3840     for (;;) {
3841         if (quit_when_ready)
3842         {
3843             /* send escape events until we quit */
3844             Term_keypress(0x1B);
3845             [pool drain];
3846             return false;
3847         }
3848         else {
3849             event = [NSApp nextEventMatchingMask:-1 untilDate:endDate inMode:NSDefaultRunLoopMode dequeue:YES];
3850             if (! event)
3851             {
3852                 [pool drain];
3853                 return FALSE;
3854             }
3855             if (send_event(event)) break;
3856         }
3857     }
3858     
3859     [pool drain];
3860     
3861     /* Something happened */
3862     return YES;
3863     
3864 }
3865
3866 /**
3867  * Hook to tell the user something important
3868  */
3869 static void hook_plog(const char * str)
3870 {
3871     if (str)
3872     {
3873         NSString *msg = NSLocalizedStringWithDefaultValue(
3874             @"Warning", AngbandMessageCatalog, [NSBundle mainBundle],
3875             @"Warning", @"Alert text for generic warning");
3876         NSString *info = [NSString stringWithCString:str
3877 #ifdef JP
3878                                    encoding:NSJapaneseEUCStringEncoding
3879 #else
3880                                    encoding:NSMacOSRomanStringEncoding
3881 #endif
3882         ];
3883         NSAlert *alert = [[NSAlert alloc] init];
3884
3885         alert.messageText = msg;
3886         alert.informativeText = info;
3887         NSModalResponse result = [alert runModal];
3888         [alert release];
3889     }
3890 }
3891
3892
3893 /**
3894  * Hook to tell the user something, and then quit
3895  */
3896 static void hook_quit(const char * str)
3897 {
3898     plog(str);
3899     exit(0);
3900 }
3901
3902 /**
3903  * ------------------------------------------------------------------------
3904  * Main program
3905  * ------------------------------------------------------------------------ */
3906
3907 @implementation AngbandAppDelegate
3908
3909 @synthesize graphicsMenu=_graphicsMenu;
3910 @synthesize commandMenu=_commandMenu;
3911 @synthesize commandMenuTagMap=_commandMenuTagMap;
3912
3913 - (IBAction)newGame:sender
3914 {
3915     /* Game is in progress */
3916     game_in_progress = TRUE;
3917     new_game = TRUE;
3918 }
3919
3920 - (IBAction)editFont:sender
3921 {
3922     NSFontPanel *panel = [NSFontPanel sharedFontPanel];
3923     NSFont *termFont = default_font;
3924
3925     int i;
3926     for (i=0; i < ANGBAND_TERM_MAX; i++) {
3927         if ([(id)angband_term[i]->data isMainWindow]) {
3928             termFont = [(id)angband_term[i]->data selectionFont];
3929             break;
3930         }
3931     }
3932     
3933     [panel setPanelFont:termFont isMultiple:NO];
3934     [panel orderFront:self];
3935 }
3936
3937 /**
3938  * Implent NSObject's changeFont() method to receive a notification about the
3939  * changed font.  Note that, as of 10.14, changeFont() is deprecated in
3940  * NSObject - it will be removed at some point and the application delegate
3941  * will have to be declared as implementing the NSFontChanging protocol.
3942  */
3943 - (void)changeFont:(id)sender
3944 {
3945     int mainTerm;
3946     for (mainTerm=0; mainTerm < ANGBAND_TERM_MAX; mainTerm++) {
3947         if ([(id)angband_term[mainTerm]->data isMainWindow]) {
3948             break;
3949         }
3950     }
3951
3952     /* Bug #1709: Only change font for angband windows */
3953     if (mainTerm == ANGBAND_TERM_MAX) return;
3954     
3955     NSFont *oldFont = default_font;
3956     NSFont *newFont = [sender convertFont:oldFont];
3957     if (! newFont) return; /*paranoia */
3958     
3959     /* Store as the default font if we changed the first term */
3960     if (mainTerm == 0) {
3961         [newFont retain];
3962         [default_font release];
3963         default_font = newFont;
3964     }
3965     
3966     /* Record it in the preferences */
3967     NSUserDefaults *defs = [NSUserDefaults angbandDefaults];
3968     [defs setValue:[newFont fontName] 
3969         forKey:[NSString stringWithFormat:@"FontName-%d", mainTerm]];
3970     [defs setFloat:[newFont pointSize]
3971         forKey:[NSString stringWithFormat:@"FontSize-%d", mainTerm]];
3972     [defs synchronize];
3973     
3974     NSDisableScreenUpdates();
3975     
3976     /* Update window */
3977     AngbandContext *angbandContext = angband_term[mainTerm]->data;
3978     [(id)angbandContext setSelectionFont:newFont adjustTerminal: YES];
3979     
3980     NSEnableScreenUpdates();
3981
3982     if (mainTerm == 0 && game_in_progress) {
3983         /* Mimics the logic in setGraphicsMode(). */
3984         do_cmd_redraw();
3985         wakeup_event_loop();
3986     } else {
3987         [(id)angbandContext requestRedraw];
3988     }
3989 }
3990
3991 - (IBAction)openGame:sender
3992 {
3993     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
3994     BOOL selectedSomething = NO;
3995     int panelResult;
3996     
3997     /* Get where we think the save files are */
3998     NSURL *startingDirectoryURL = [NSURL fileURLWithPath:[NSString stringWithCString:ANGBAND_DIR_SAVE encoding:NSASCIIStringEncoding] isDirectory:YES];
3999     
4000     /* Set up an open panel */
4001     NSOpenPanel* panel = [NSOpenPanel openPanel];
4002     [panel setCanChooseFiles:YES];
4003     [panel setCanChooseDirectories:NO];
4004     [panel setResolvesAliases:YES];
4005     [panel setAllowsMultipleSelection:NO];
4006     [panel setTreatsFilePackagesAsDirectories:YES];
4007     [panel setDirectoryURL:startingDirectoryURL];
4008     
4009     /* Run it */
4010     panelResult = [panel runModal];
4011     if (panelResult == NSOKButton)
4012     {
4013         NSArray* fileURLs = [panel URLs];
4014         if ([fileURLs count] > 0 && [[fileURLs objectAtIndex:0] isFileURL])
4015         {
4016             NSURL* savefileURL = (NSURL *)[fileURLs objectAtIndex:0];
4017             /* The path property doesn't do the right thing except for
4018              * URLs with the file scheme. We had getFileSystemRepresentation
4019              * here before, but that wasn't introduced until OS X 10.9. */
4020             selectedSomething = [[savefileURL path] getCString:savefile 
4021                 maxLength:sizeof savefile encoding:NSMacOSRomanStringEncoding];
4022         }
4023     }
4024     
4025     if (selectedSomething)
4026     {
4027         /* Remember this so we can select it by default next time */
4028         record_current_savefile();
4029         
4030         /* Game is in progress */
4031         game_in_progress = TRUE;
4032         new_game = FALSE;
4033     }
4034     
4035     [pool drain];
4036 }
4037
4038 - (IBAction)saveGame:sender
4039 {
4040     /* Hack -- Forget messages */
4041     msg_flag = FALSE;
4042     
4043     /* Save the game */
4044     do_cmd_save_game(FALSE);
4045     
4046     /* Record the current save file so we can select it by default next time.
4047          * It's a little sketchy that this only happens when we save through the
4048          * menu; ideally game-triggered saves would trigger it too. */
4049     record_current_savefile();
4050 }
4051
4052 /**
4053  * Implement NSObject's validateMenuItem() method to override enabling or
4054  * disabling a menu item.  Note that, as of 10.14, validateMenuItem() is
4055  * deprecated in NSObject - it will be removed at some point and  the
4056  * application delegate will have to be declared as implementing the
4057  * NSMenuItemValidation protocol.
4058  */
4059 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
4060 {
4061     SEL sel = [menuItem action];
4062     NSInteger tag = [menuItem tag];
4063
4064     if( tag >= AngbandWindowMenuItemTagBase && tag < AngbandWindowMenuItemTagBase + ANGBAND_TERM_MAX )
4065     {
4066         if( tag == AngbandWindowMenuItemTagBase )
4067         {
4068             /* The main window should always be available and visible */
4069             return YES;
4070         }
4071         else
4072         {
4073             /*
4074              * Another window is only usable after Term_init_cocoa() has
4075              * been called for it.  For Angband if window_flag[i] is nonzero
4076              * then that has happened for window i.  For Hengband, that is
4077              * not the case so also test angband_term[i]->data.
4078              */
4079             NSInteger subwindowNumber = tag - AngbandWindowMenuItemTagBase;
4080             return (angband_term[subwindowNumber]->data != 0
4081                     && window_flag[subwindowNumber] > 0);
4082         }
4083
4084         return NO;
4085     }
4086
4087     if (sel == @selector(newGame:))
4088     {
4089         return ! game_in_progress;
4090     }
4091     else if (sel == @selector(editFont:))
4092     {
4093         return YES;
4094     }
4095     else if (sel == @selector(openGame:))
4096     {
4097         return ! game_in_progress;
4098     }
4099     else if (sel == @selector(setRefreshRate:) &&
4100              [[menuItem parentItem] tag] == 150)
4101     {
4102         NSInteger fps = [[NSUserDefaults standardUserDefaults] integerForKey:AngbandFrameRateDefaultsKey];
4103         [menuItem setState: ([menuItem tag] == fps)];
4104         return YES;
4105     }
4106     else if( sel == @selector(setGraphicsMode:) )
4107     {
4108         NSInteger requestedGraphicsMode = [[NSUserDefaults standardUserDefaults] integerForKey:AngbandGraphicsDefaultsKey];
4109         [menuItem setState: (tag == requestedGraphicsMode)];
4110         return YES;
4111     }
4112     else if( sel == @selector(toggleSound:) )
4113     {
4114         BOOL is_on = [[NSUserDefaults standardUserDefaults]
4115                          boolForKey:AngbandSoundDefaultsKey];
4116
4117         [menuItem setState: ((is_on) ? NSOnState : NSOffState)];
4118         return YES;
4119     }
4120     else if( sel == @selector(sendAngbandCommand:) ||
4121              sel == @selector(saveGame:) )
4122     {
4123         /*
4124          * we only want to be able to send commands during an active game
4125          * after the birth screens
4126          */
4127         return !!game_in_progress && character_generated;
4128     }
4129     else return YES;
4130 }
4131
4132
4133 - (IBAction)setRefreshRate:(NSMenuItem *)menuItem
4134 {
4135     frames_per_second = [menuItem tag];
4136     [[NSUserDefaults angbandDefaults] setInteger:frames_per_second forKey:AngbandFrameRateDefaultsKey];
4137 }
4138
4139 - (void)selectWindow: (id)sender
4140 {
4141     NSInteger subwindowNumber = [(NSMenuItem *)sender tag] - AngbandWindowMenuItemTagBase;
4142     AngbandContext *context = angband_term[subwindowNumber]->data;
4143     [context->primaryWindow makeKeyAndOrderFront: self];
4144         [context saveWindowVisibleToDefaults: YES];
4145 }
4146
4147 - (void)prepareWindowsMenu
4148 {
4149     /* Get the window menu with default items and add a separator and item for
4150          * the main window */
4151     NSMenu *windowsMenu = [[NSApplication sharedApplication] windowsMenu];
4152     [windowsMenu addItem: [NSMenuItem separatorItem]];
4153
4154     NSString *title1 = [NSString stringWithCString:angband_term_name[0]
4155 #ifdef JP
4156                                 encoding:NSJapaneseEUCStringEncoding
4157 #else
4158                                 encoding:NSMacOSRomanStringEncoding
4159 #endif
4160     ];
4161     NSMenuItem *angbandItem = [[NSMenuItem alloc] initWithTitle:title1 action: @selector(selectWindow:) keyEquivalent: @"0"];
4162     [angbandItem setTarget: self];
4163     [angbandItem setTag: AngbandWindowMenuItemTagBase];
4164     [windowsMenu addItem: angbandItem];
4165     [angbandItem release];
4166
4167     /* Add items for the additional term windows */
4168     for( NSInteger i = 1; i < ANGBAND_TERM_MAX; i++ )
4169     {
4170         NSString *title = [NSString stringWithCString:angband_term_name[i]
4171 #ifdef JP
4172                                     encoding:NSJapaneseEUCStringEncoding
4173 #else
4174                                     encoding:NSMacOSRomanStringEncoding
4175 #endif
4176         ];
4177         NSString *keyEquivalent = [NSString stringWithFormat: @"%ld", (long)i];
4178         NSMenuItem *windowItem = [[NSMenuItem alloc] initWithTitle: title action: @selector(selectWindow:) keyEquivalent: keyEquivalent];
4179         [windowItem setTarget: self];
4180         [windowItem setTag: AngbandWindowMenuItemTagBase + i];
4181         [windowsMenu addItem: windowItem];
4182         [windowItem release];
4183     }
4184 }
4185
4186 - (void)setGraphicsMode:(NSMenuItem *)sender
4187 {
4188     /* We stashed the graphics mode ID in the menu item's tag */
4189     graf_mode_req = [sender tag];
4190
4191     /* Stash it in UserDefaults */
4192     [[NSUserDefaults angbandDefaults] setInteger:graf_mode_req forKey:AngbandGraphicsDefaultsKey];
4193     [[NSUserDefaults angbandDefaults] synchronize];
4194     
4195     if (game_in_progress)
4196     {
4197         /* Hack -- Force redraw */
4198         do_cmd_redraw();
4199         
4200         /* Wake up the event loop so it notices the change */
4201         wakeup_event_loop();
4202     }
4203 }
4204
4205 - (IBAction) toggleSound: (NSMenuItem *) sender
4206 {
4207     BOOL is_on = (sender.state == NSOnState);
4208
4209     /* Toggle the state and update the Angband global and preferences. */
4210     sender.state = (is_on) ? NSOffState : NSOnState;
4211     use_sound = (is_on) ? FALSE : TRUE;
4212     [[NSUserDefaults angbandDefaults] setBool:(! is_on)
4213                                       forKey:AngbandSoundDefaultsKey];
4214 }
4215
4216 /**
4217  *  Send a command to Angband via a menu item. This places the appropriate key
4218  * down events into the queue so that it seems like the user pressed them
4219  * (instead of trying to use the term directly).
4220  */
4221 - (void)sendAngbandCommand: (id)sender
4222 {
4223     NSMenuItem *menuItem = (NSMenuItem *)sender;
4224     NSString *command = [self.commandMenuTagMap objectForKey: [NSNumber numberWithInteger: [menuItem tag]]];
4225     NSInteger windowNumber = [((AngbandContext *)angband_term[0]->data)->primaryWindow windowNumber];
4226
4227     /* Send a \ to bypass keymaps */
4228     NSEvent *escape = [NSEvent keyEventWithType: NSKeyDown
4229                                        location: NSZeroPoint
4230                                   modifierFlags: 0
4231                                       timestamp: 0.0
4232                                    windowNumber: windowNumber
4233                                         context: nil
4234                                      characters: @"\\"
4235                     charactersIgnoringModifiers: @"\\"
4236                                       isARepeat: NO
4237                                         keyCode: 0];
4238     [[NSApplication sharedApplication] postEvent: escape atStart: NO];
4239
4240     /* Send the actual command (from the original command set) */
4241     NSEvent *keyDown = [NSEvent keyEventWithType: NSKeyDown
4242                                         location: NSZeroPoint
4243                                    modifierFlags: 0
4244                                        timestamp: 0.0
4245                                     windowNumber: windowNumber
4246                                          context: nil
4247                                       characters: command
4248                      charactersIgnoringModifiers: command
4249                                        isARepeat: NO
4250                                          keyCode: 0];
4251     [[NSApplication sharedApplication] postEvent: keyDown atStart: NO];
4252 }
4253
4254 /**
4255  *  Set up the command menu dynamically, based on CommandMenu.plist.
4256  */
4257 - (void)prepareCommandMenu
4258 {
4259     NSString *commandMenuPath = [[NSBundle mainBundle] pathForResource: @"CommandMenu" ofType: @"plist"];
4260     NSArray *commandMenuItems = [[NSArray alloc] initWithContentsOfFile: commandMenuPath];
4261     NSMutableDictionary *angbandCommands = [[NSMutableDictionary alloc] init];
4262     NSString *tblname = @"CommandMenu";
4263     NSInteger tagOffset = 0;
4264
4265     for( NSDictionary *item in commandMenuItems )
4266     {
4267         BOOL useShiftModifier = [[item valueForKey: @"ShiftModifier"] boolValue];
4268         BOOL useOptionModifier = [[item valueForKey: @"OptionModifier"] boolValue];
4269         NSUInteger keyModifiers = NSCommandKeyMask;
4270         keyModifiers |= (useShiftModifier) ? NSShiftKeyMask : 0;
4271         keyModifiers |= (useOptionModifier) ? NSAlternateKeyMask : 0;
4272
4273         NSString *lookup = [item valueForKey: @"Title"];
4274         NSString *title = NSLocalizedStringWithDefaultValue(
4275             lookup, tblname, [NSBundle mainBundle], lookup, @"");
4276         NSString *key = [item valueForKey: @"KeyEquivalent"];
4277         NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle: title action: @selector(sendAngbandCommand:) keyEquivalent: key];
4278         [menuItem setTarget: self];
4279         [menuItem setKeyEquivalentModifierMask: keyModifiers];
4280         [menuItem setTag: AngbandCommandMenuItemTagBase + tagOffset];
4281         [self.commandMenu addItem: menuItem];
4282         [menuItem release];
4283
4284         NSString *angbandCommand = [item valueForKey: @"AngbandCommand"];
4285         [angbandCommands setObject: angbandCommand forKey: [NSNumber numberWithInteger: [menuItem tag]]];
4286         tagOffset++;
4287     }
4288
4289     [commandMenuItems release];
4290
4291     NSDictionary *safeCommands = [[NSDictionary alloc] initWithDictionary: angbandCommands];
4292     self.commandMenuTagMap = safeCommands;
4293     [safeCommands release];
4294     [angbandCommands release];
4295 }
4296
4297 - (void)awakeFromNib
4298 {
4299     [super awakeFromNib];
4300
4301     [self prepareWindowsMenu];
4302     [self prepareCommandMenu];
4303 }
4304
4305 - (void)applicationDidFinishLaunching:sender
4306 {
4307     [AngbandContext beginGame];
4308     
4309     /* Once beginGame finished, the game is over - that's how Angband works,
4310          * and we should quit */
4311     game_is_finished = TRUE;
4312     [NSApp terminate:self];
4313 }
4314
4315 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
4316 {
4317     if (p_ptr->playing == FALSE || game_is_finished == TRUE)
4318     {
4319         return NSTerminateNow;
4320     }
4321     else if (! inkey_flag)
4322     {
4323         /* For compatibility with other ports, do not quit in this case */
4324         return NSTerminateCancel;
4325     }
4326     else
4327     {
4328         /* Stop playing */
4329         /* player->upkeep->playing = FALSE; */
4330
4331         /* Post an escape event so that we can return from our get-key-event
4332                  * function */
4333         wakeup_event_loop();
4334         quit_when_ready = true;
4335         /* Must return Cancel, not Later, because we need to get out of the
4336                  * run loop and back to Angband's loop */
4337         return NSTerminateCancel;
4338     }
4339 }
4340
4341 /**
4342  * Dynamically build the Graphics menu
4343  */
4344 - (void)menuNeedsUpdate:(NSMenu *)menu {
4345     
4346     /* Only the graphics menu is dynamic */
4347     if (! [menu isEqual:self.graphicsMenu])
4348         return;
4349     
4350     /* If it's non-empty, then we've already built it. Currently graphics modes
4351          * won't change once created; if they ever can we can remove this check.
4352      * Note that the check mark does change, but that's handled in
4353          * validateMenuItem: instead of menuNeedsUpdate: */
4354     if ([menu numberOfItems] > 0)
4355         return;
4356     
4357     /* This is the action for all these menu items */
4358     SEL action = @selector(setGraphicsMode:);
4359     
4360     /* Add an initial Classic ASCII menu item */
4361     NSString *tblname = @"GraphicsMenu";
4362     NSString *key = @"Classic ASCII";
4363     NSString *title = NSLocalizedStringWithDefaultValue(
4364         key, tblname, [NSBundle mainBundle], key, @"");
4365     NSMenuItem *classicItem = [menu addItemWithTitle:title action:action keyEquivalent:@""];
4366     [classicItem setTag:GRAPHICS_NONE];
4367     
4368     /* Walk through the list of graphics modes */
4369     if (graphics_modes) {
4370         NSInteger i;
4371
4372         for (i=0; graphics_modes[i].pNext; i++)
4373         {
4374             const graphics_mode *graf = &graphics_modes[i];
4375
4376             if (graf->grafID == GRAPHICS_NONE) {
4377                 continue;
4378             }
4379             /* Make the title. NSMenuItem throws on a nil title, so ensure it's
4380                    * not nil. */
4381             key = [[NSString alloc] initWithUTF8String:graf->menuname];
4382             title = NSLocalizedStringWithDefaultValue(
4383                 key, tblname, [NSBundle mainBundle], key, @"");
4384         
4385             /* Make the item */
4386             NSMenuItem *item = [menu addItemWithTitle:title action:action keyEquivalent:@""];
4387             [key release];
4388             [item setTag:graf->grafID];
4389         }
4390     }
4391 }
4392
4393 /**
4394  * Delegate method that gets called if we're asked to open a file.
4395  */
4396 - (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
4397 {
4398     /* Can't open a file once we've started */
4399     if (game_in_progress) {
4400         [[NSApplication sharedApplication]
4401             replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
4402         return;
4403     }
4404
4405     /* We can only open one file. Use the last one. */
4406     NSString *file = [filenames lastObject];
4407     if (! file) {
4408         [[NSApplication sharedApplication]
4409             replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
4410         return;
4411     }
4412
4413     /* Put it in savefile */
4414     if (! [file getFileSystemRepresentation:savefile maxLength:sizeof savefile]) {
4415         [[NSApplication sharedApplication]
4416             replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
4417         return;
4418     }
4419
4420     game_in_progress = TRUE;
4421     new_game = FALSE;
4422
4423     /* Wake us up in case this arrives while we're sitting at the Welcome
4424          * screen! */
4425     wakeup_event_loop();
4426
4427     [[NSApplication sharedApplication]
4428         replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
4429 }
4430
4431 @end
4432
4433 int main(int argc, char* argv[])
4434 {
4435     NSApplicationMain(argc, (void*)argv);
4436     return (0);
4437 }
4438
4439 #endif /* MACINTOSH || MACH_O_COCOA */