OSDN Git Service

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