OSDN Git Service

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