OSDN Git Service

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