OSDN Git Service

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