OSDN Git Service

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