OSDN Git Service

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