OSDN Git Service

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