OSDN Git Service

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