OSDN Git Service

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