OSDN Git Service

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