3 * \brief OS X front end
5 * Copyright (c) 2011 Peter Ammon
7 * This work is free software; you can redistribute it and/or modify it
8 * under the terms of either:
10 * a) the GNU General Public License as published by the Free Software
11 * Foundation, version 2, or
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.
20 /* This is not included in angband.h in Hengband. */
23 #if defined(MACH_O_COCOA)
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
31 #define kVK_Delete 0x33
32 #define kVK_Escape 0x35
33 #define kVK_ANSI_KeypadEnter 0x4C
35 static NSSize const AngbandScaleIdentity = {1.0, 1.0};
36 static NSString * const AngbandDirectoryNameLib = @"lib";
37 static NSString * const AngbandDirectoryNameBase = @"Hengband";
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;
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
50 #ifndef USE_LIVE_RESIZE_CACHE
51 # define USE_LIVE_RESIZE_CACHE 1
54 /* Global defines etc from Angband 3.5-dev - NRM */
55 #define ANGBAND_TERM_MAX 8
57 static bool new_game = TRUE;
59 #define MAX_COLORS 256
60 #define MSG_MAX SOUND_MAX
62 /* End Angband stuff - NRM */
64 /* Application defined event numbers */
67 AngbandEventWakeup = 1
70 /* Redeclare some 10.7 constants and methods so we can build on 10.6 */
73 Angband_NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7,
74 Angband_NSWindowCollectionBehaviorFullScreenAuxiliary = 1 << 8
77 @interface NSWindow (AngbandLionRedeclares)
78 - (void)setRestorable:(BOOL)flag;
81 /* Delay handling of pre-emptive "quit" event */
82 static BOOL quit_when_ready = FALSE;
84 /* Set to indicate the game is over and we can quit without delay */
85 static Boolean game_is_finished = FALSE;
87 /* Our frames per second (e.g. 60). A value of 0 means unthrottled. */
88 static int frames_per_second;
90 /* Function to get the default font */
91 static NSFont *default_font;
95 /* The max number of glyphs we support */
96 #define GLYPH_COUNT 256
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>
105 /* The Angband term */
108 /* Column and row cont, by default 80 x 24 */
112 /* The size of the border between the window edge and the contents */
115 /* Our array of views */
116 NSMutableArray *angbandViews;
118 /* The buffered image */
119 CGLayerRef angbandLayer;
121 /* The font of this context */
122 NSFont *angbandViewFont;
124 /* If this context owns a window, here it is */
125 NSWindow *primaryWindow;
127 /* "Glyph info": an array of the CGGlyphs and their widths corresponding to
129 CGGlyph glyphArray[GLYPH_COUNT];
130 CGFloat glyphWidths[GLYPH_COUNT];
132 /* The size of one tile */
135 /* Font's descender */
136 CGFloat fontDescender;
138 /* Whether we are currently in live resize, which affects how big we render
142 /* Last time we drew, so we can throttle drawing */
143 CFAbsoluteTime lastRefreshTime;
147 BOOL _hasSubwindowFlags;
148 BOOL _windowVisibilityChecked;
151 @property (nonatomic, assign) BOOL hasSubwindowFlags;
152 @property (nonatomic, assign) BOOL windowVisibilityChecked;
154 - (void)drawRect:(NSRect)rect inView:(NSView *)view;
156 /* Called at initialization to set the term */
157 - (void)setTerm:(term *)t;
159 /* Called when the context is going down. */
162 /* Returns the size of the image. */
165 /* Return the rect for a tile at given coordinates. */
166 - (NSRect)rectInImageForTileAtX:(int)x Y:(int)y;
168 /* Draw the given wide character into the given tile rect. */
169 - (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile;
171 /* Locks focus on the Angband image, and scales the CTM appropriately. */
172 - (CGContextRef)lockFocus;
174 /* Locks focus on the Angband image but does NOT scale the CTM. Appropriate
175 * for drawing hairlines. */
176 - (CGContextRef)lockFocusUnscaled;
181 /* Returns the primary window for this angband context, creating it if
183 - (NSWindow *)makePrimaryWindow;
185 /* Called to add a new Angband view */
186 - (void)addAngbandView:(AngbandView *)view;
188 /* Make the context aware that one of its views changed size */
189 - (void)angbandViewDidScale:(AngbandView *)view;
191 /* Handle becoming the main window */
192 - (void)windowDidBecomeMain:(NSNotification *)notification;
194 /* Return whether the context's primary window is ordered in or not */
197 /* Return whether the context's primary window is key */
198 - (BOOL)isMainWindow;
200 /* Invalidate the whole image */
201 - (void)setNeedsDisplay:(BOOL)val;
203 /* Invalidate part of the image, with the rect expressed in base coordinates */
204 - (void)setNeedsDisplayInBaseRect:(NSRect)rect;
206 /* Display (flush) our Angband views */
207 - (void)displayIfNeeded;
209 /* Resize context to size of contentRect, and optionally save size to
211 - (void)resizeTerminalWithContentRect: (NSRect)contentRect saveToDefaults: (BOOL)saveToDefaults;
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;
221 /* Begins an Angband game. This is the entry point for starting off. */
224 /* Ends an Angband game. */
227 /* Internal method */
228 - (AngbandView *)activeView;
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).
238 u32b AngbandMaskForValidSubwindowFlags(void)
240 int windowFlagBits = sizeof(*(window_flag)) * CHAR_BIT;
241 int maxBits = MIN( 16, windowFlagBits );
244 for( int i = 0; i < maxBits; i++ )
246 if( window_flag_desc[i] != NULL )
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.
260 static void AngbandUpdateWindowVisibility(void)
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;
266 if( validWindowFlagsMask == 0 )
268 validWindowFlagsMask = AngbandMaskForValidSubwindowFlags();
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++ )
276 AngbandContext *angbandContext = angband_term[i]->data;
278 if( angbandContext == nil )
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 )
291 if( [angbandContext windowVisibleUsingDefaults] )
293 [angbandContext->primaryWindow orderFront: nil];
294 angbandContext.windowVisibilityChecked = YES;
298 [angbandContext->primaryWindow close];
299 angbandContext.windowVisibilityChecked = NO;
304 BOOL termHasSubwindowFlags = ((window_flag[i] & validWindowFlagsMask) > 0);
306 if( angbandContext.hasSubwindowFlags && !termHasSubwindowFlags )
308 [angbandContext->primaryWindow close];
309 angbandContext.hasSubwindowFlags = NO;
310 [angbandContext saveWindowVisibleToDefaults: NO];
312 else if( !angbandContext.hasSubwindowFlags && termHasSubwindowFlags )
314 [angbandContext->primaryWindow orderFront: nil];
315 angbandContext.hasSubwindowFlags = YES;
316 [angbandContext saveWindowVisibleToDefaults: YES];
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];
327 * Here is some support for rounding to pixels in a scaled context
329 static double push_pixel(double pixel, double scale, BOOL increase)
331 double scaledPixel = pixel * scale;
332 /* Have some tolerance! */
333 double roundedPixel = round(scaledPixel);
334 if (fabs(roundedPixel - scaledPixel) <= .0001)
336 scaledPixel = roundedPixel;
340 scaledPixel = (increase ? ceil : floor)(scaledPixel);
342 return scaledPixel / scale;
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. */
357 * Return a rect whose border is in the "cracks" between tiles
359 static NSRect crack_rect(NSRect rect, NSSize scale, unsigned pushOptions)
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);
369 * Returns the pixel push options (describing how we round) for the tile at a
370 * given index. Currently it's pretty uniform!
372 static unsigned push_options(unsigned x, unsigned y)
374 return PUSH_TOP | PUSH_LEFT;
378 * ------------------------------------------------------------------------
380 * ------------------------------------------------------------------------ */
385 static CGImageRef pict_image;
388 * Numbers of rows and columns in a tileset,
389 * calculated by the PICT/PNG loading code
391 static int pict_cols = 0;
392 static int pict_rows = 0;
395 * Requested graphics mode (as a grafID).
396 * The current mode is stored in current_graphics_mode.
398 static int graf_mode_req = 0;
401 * Helper function to check the various ways that graphics can be enabled,
402 * guarding against NULL
404 static BOOL graphics_are_enabled(void)
406 return current_graphics_mode
407 && current_graphics_mode->grafID != GRAPHICS_NONE;
411 * Hack -- game in progress
413 static Boolean game_in_progress = FALSE;
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);
430 * Available values for 'wait'
432 #define CHECK_EVENTS_DRAIN -1
433 #define CHECK_EVENTS_NO_WAIT 0
434 #define CHECK_EVENTS_WAIT 1
438 * Note when "open"/"new" become valid
440 static bool initialized = FALSE;
442 /* Methods for getting the appropriate NSUserDefaults */
443 @interface NSUserDefaults (AngbandDefaults)
444 + (NSUserDefaults *)angbandDefaults;
447 @implementation NSUserDefaults (AngbandDefaults)
448 + (NSUserDefaults *)angbandDefaults
450 return [NSUserDefaults standardUserDefaults];
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;
460 /* The NSView subclass that draws our Angband image */
461 @interface AngbandView : NSView
463 IBOutlet AngbandContext *angbandContext;
466 - (void)setAngbandContext:(AngbandContext *)context;
467 - (AngbandContext *)angbandContext;
471 @implementation NSImage (AngbandImages)
473 /* Returns an image in the resource directoy of the bundle containing the
474 * Angband view class. */
475 + (NSImage *)angbandImage:(NSString *)name
477 NSBundle *bundle = [NSBundle bundleForClass:[AngbandView class]];
478 NSString *path = [bundle pathForImageResource:name];
480 if (path) result = [[[NSImage alloc] initByReferencingFile:path] autorelease];
488 @implementation AngbandContext
490 @synthesize hasSubwindowFlags=_hasSubwindowFlags;
491 @synthesize windowVisibilityChecked=_windowVisibilityChecked;
493 - (NSFont *)selectionFont
495 return angbandViewFont;
498 - (BOOL)useLiveResizeOptimization
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();
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
511 return NSMakeSize(floor(cols * tileSize.width + 2 * borderSize.width), floor(rows * tileSize.height + 2 * borderSize.height));
514 /* qsort-compatible compare function for CGSizes */
515 static int compare_advances(const void *ap, const void *bp)
517 const CGSize *a = ap, *b = bp;
518 return (a->width > b->width) - (a->width < b->width);
521 - (void)updateGlyphInfo
523 /* Update glyphArray and glyphWidths */
524 NSFont *screenFont = [angbandViewFont screenFont];
526 /* Generate a string containing each MacRoman character */
527 unsigned char latinString[GLYPH_COUNT];
529 for (i=0; i < GLYPH_COUNT; i++) latinString[i] = (unsigned char)i;
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];
538 memset(glyphArray, 0, sizeof glyphArray);
539 CTFontGetGlyphsForCharacters((CTFontRef)screenFont, unicharString, glyphArray, GLYPH_COUNT);
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;
548 /* For good non-mono-font support, use the median advance. Start by sorting
550 qsort(advances, GLYPH_COUNT, sizeof *advances, compare_advances);
552 /* Skip over any initially empty run */
554 for (startIdx = 0; startIdx < GLYPH_COUNT; startIdx++)
556 if (advances[startIdx].width > 0) break;
559 /* Pick the center to find the median */
560 CGFloat medianAdvance = 0;
561 if (startIdx < GLYPH_COUNT)
563 /* In case we have all zero advances for some reason */
564 medianAdvance = advances[(startIdx + GLYPH_COUNT)/2].width;
567 /* Record the descender */
568 fontDescender = [screenFont descender];
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];
579 NSSize size = NSMakeSize(1, 1);
581 AngbandView *activeView = [self activeView];
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. */
588 if ([self useLiveResizeOptimization] && (screen = [[activeView window] screen]) != NULL)
590 size = [screen frame].size;
594 size = [activeView bounds].size;
598 CGLayerRelease(angbandLayer);
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]);
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);
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);
620 CFRelease(exampleCtx);
622 /* Set the new context of the layer to draw at the correct scale */
623 CGContextRef ctx = CGLayerGetContext(angbandLayer);
624 CGContextScaleCTM(ctx, angbandLayerScale, angbandLayerScale);
627 [[NSColor blackColor] set];
628 NSRectFill((NSRect){NSZeroPoint, [self baseSize]});
632 - (void)requestRedraw
634 if (! self->terminal) return;
638 /* Activate the term */
639 Term_activate(self->terminal);
641 /* Redraw the contents */
644 /* Flush the output */
647 /* Restore the old term */
651 - (void)setTerm:(term *)t
656 - (void)viewWillStartLiveResize:(AngbandView *)view
658 #if USE_LIVE_RESIZE_CACHE
659 if (inLiveResize < INT_MAX) inLiveResize++;
660 else [NSException raise:NSInternalInconsistencyException format:@"inLiveResize overflow"];
662 if (inLiveResize == 1 && graphics_are_enabled())
666 [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
667 [self requestRedraw];
672 - (void)viewDidEndLiveResize:(AngbandView *)view
674 #if USE_LIVE_RESIZE_CACHE
675 if (inLiveResize > 0) inLiveResize--;
676 else [NSException raise:NSInternalInconsistencyException format:@"inLiveResize underflow"];
678 if (inLiveResize == 0 && graphics_are_enabled())
682 [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
683 [self requestRedraw];
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. */
694 if (frames_per_second > 0)
696 CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
697 CFTimeInterval timeSinceLastRefresh = now - lastRefreshTime;
698 CFTimeInterval timeUntilNextRefresh = (1. / (double)frames_per_second) - timeSinceLastRefresh;
700 if (timeUntilNextRefresh > 0)
702 usleep((unsigned long)(timeUntilNextRefresh * 1000000.));
705 lastRefreshTime = CFAbsoluteTimeGetCurrent();
708 - (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile
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};
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];
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))
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;
737 /* Our glyph doesn't fit, so we'll have to compress it */
738 compressionRatio = NSWidth(tile) / advance.width;
744 CGAffineTransform textMatrix = CGContextGetTextMatrix(ctx);
745 CGFloat savedA = textMatrix.a;
747 /* Set the position */
748 textMatrix.tx = tile.origin.x + tileOffsetX;
749 textMatrix.ty = tile.origin.y + tileOffsetY;
751 /* Maybe squish it horizontally. */
752 if (compressionRatio != 1.)
754 textMatrix.a *= compressionRatio;
757 textMatrix = CGAffineTransformScale( textMatrix, 1.0, -1.0 );
758 CGContextSetTextMatrix(ctx, textMatrix);
759 CGContextShowGlyphsWithAdvances(ctx, &glyph, &CGSizeZero, 1);
761 /* Restore the text matrix if we messed with the compression ratio */
762 if (compressionRatio != 1.)
764 textMatrix.a = savedA;
765 CGContextSetTextMatrix(ctx, textMatrix);
768 textMatrix = CGAffineTransformScale( textMatrix, 1.0, -1.0 );
769 CGContextSetTextMatrix(ctx, textMatrix);
772 /* Lock and unlock focus on our image or layer, setting up the CTM
774 - (CGContextRef)lockFocusUnscaled
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);
787 /* Restore the graphics state */
788 CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];
789 CGContextRestoreGState(ctx);
790 [NSGraphicsContext restoreGraphicsState];
795 /* Return the size of our layer */
796 CGSize result = CGLayerGetSize(angbandLayer);
797 return NSMakeSize(result.width, result.height);
800 - (CGContextRef)lockFocus
802 return [self lockFocusUnscaled];
806 - (NSRect)rectInImageForTileAtX:(int)x Y:(int)y
809 return NSMakeRect(x * tileSize.width + borderSize.width, flippedY * tileSize.height + borderSize.height, tileSize.width, tileSize.height);
812 - (void)setSelectionFont:(NSFont*)font adjustTerminal: (BOOL)adjustTerminal
814 /* Record the new font */
816 [angbandViewFont release];
817 angbandViewFont = font;
819 /* Update our glyph info */
820 [self updateGlyphInfo];
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];
830 /* Update our image */
834 [self requestRedraw];
839 if ((self = [super init]))
841 /* Default rows and cols */
845 /* Default border size */
846 self->borderSize = NSMakeSize(2, 2);
848 /* Allocate our array of views */
849 angbandViews = [[NSMutableArray alloc] init];
851 /* Make the image. Since we have no views, it'll just be a puny 1x1 image. */
854 _windowVisibilityChecked = NO;
860 * Destroy all the receiver's stuff. This is intended to be callable more than
867 /* Disassociate ourselves from our angbandViews */
868 [angbandViews makeObjectsPerformSelector:@selector(setAngbandContext:) withObject:nil];
869 [angbandViews release];
872 /* Destroy the layer/image */
873 CGLayerRelease(angbandLayer);
877 [angbandViewFont release];
878 angbandViewFont = nil;
881 [primaryWindow setDelegate:nil];
882 [primaryWindow close];
883 [primaryWindow release];
887 /* Usual Cocoa fare */
897 #pragma mark Directories and Paths Setup
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
904 + (NSString *)libDirectoryPath
906 NSString *bundleLibPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: AngbandDirectoryNameLib];
907 BOOL isDirectory = NO;
908 BOOL libExists = [[NSFileManager defaultManager] fileExistsAtPath: bundleLibPath isDirectory: &isDirectory];
910 if( !libExists || !isDirectory )
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 );
917 return bundleLibPath;
921 * Return the path for the directory where Angband should look for its standard
924 + (NSString *)angbandDocumentsPath
926 NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
928 #if defined(SAFE_DIRECTORY)
929 NSString *versionedDirectory = [NSString stringWithFormat: @"%@-%s", AngbandDirectoryNameBase, VERSION_STRING];
930 return [documents stringByAppendingPathComponent: versionedDirectory];
932 return [documents stringByAppendingPathComponent: AngbandDirectoryNameBase];
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.
941 * \param originalPath The directory path to adjust.
942 * \return A path suitable for Angband or nil if an error occurred.
944 static NSString *AngbandCorrectedDirectoryPath(NSString *originalPath)
946 if ([originalPath length] == 0) {
950 if (![originalPath hasSuffix: @"/"]) {
951 return [originalPath stringByAppendingString: @"/"];
958 * Give Angband the base paths that should be used for the various directories
959 * it needs. It will create any needed directories.
961 + (void)prepareFilePathsAndDirectories
963 char libpath[PATH_MAX + 1] = "\0";
964 NSString *libDirectoryPath = AngbandCorrectedDirectoryPath([self libDirectoryPath]);
965 [libDirectoryPath getFileSystemRepresentation: libpath maxLength: sizeof(libpath)];
967 char basepath[PATH_MAX + 1] = "\0";
968 NSString *angbandDocumentsPath = AngbandCorrectedDirectoryPath([self angbandDocumentsPath]);
969 [angbandDocumentsPath getFileSystemRepresentation: basepath maxLength: sizeof(basepath)];
971 init_file_paths(libpath, libpath, basepath);
972 create_needed_dirs();
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.
983 static size_t Term_mbcs_cocoa(wchar_t *dest, const char *src, int n)
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
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);
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);
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);
1019 /* Found an invalid multibyte sequence */
1029 * Entry point for initializing Angband
1033 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1035 /* Hooks in some "z-util.c" hooks */
1036 plog_aux = hook_plog;
1037 quit_aux = hook_quit;
1039 /* Initialize file paths */
1040 [self prepareFilePathsAndDirectories];
1042 /* Load preferences */
1045 /* Prepare the windows */
1048 /* Set up game event handlers */
1049 /* init_display(); */
1051 /* Register the sound hook */
1052 /* sound_hook = play_sound; */
1054 /* Initialise game */
1057 /* This is not incorporated into Hengband's init_angband() yet. */
1058 init_graphics_modes();
1060 /* Note the "system" */
1061 ANGBAND_SYS = "mac";
1063 /* Initialize some save file stuff */
1064 player_egid = getegid();
1066 /* We are now initialized */
1069 /* Handle "open_when_ready" */
1070 handle_open_when_ready();
1072 /* Handle pending events (most notably update) and flush input */
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);
1083 * Play a game -- "new_game" is set by "new", "open" or the open document
1084 * even handler as appropriate
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];
1097 play_game(new_game);
1104 /* Hack -- Forget messages */
1107 p_ptr->playing = FALSE;
1108 p_ptr->leaving = TRUE;
1109 quit_when_ready = TRUE;
1112 - (void)addAngbandView:(AngbandView *)view
1114 if (! [angbandViews containsObject:view])
1116 [angbandViews addObject:view];
1118 [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
1119 [self requestRedraw];
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.
1130 - (AngbandView *)activeView
1132 if ([angbandViews count] == 1)
1133 return [angbandViews objectAtIndex:0];
1135 AngbandView *result = nil;
1137 for (AngbandView *angbandView in angbandViews)
1139 float width = [angbandView frame].size.width;
1140 if (width > maxWidth)
1143 result = angbandView;
1149 - (void)angbandViewDidScale:(AngbandView *)view
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])
1157 [self setNeedsDisplay:YES]; /*we'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
1158 [self requestRedraw];
1163 - (void)removeAngbandView:(AngbandView *)view
1165 if ([angbandViews containsObject:view])
1167 [angbandViews removeObject:view];
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];
1175 static NSMenuItem *superitem(NSMenuItem *self)
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];
1184 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
1186 int tag = [menuItem tag];
1187 SEL sel = [menuItem action];
1188 if (sel == @selector(setGraphicsMode:))
1190 [menuItem setState: (tag == graf_mode_req)];
1199 - (NSWindow *)makePrimaryWindow
1201 if (! primaryWindow)
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 );
1209 NSUInteger styleMask = NSTitledWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask;
1211 /* Make every window other than the main window closable */
1212 if( angband_term[0]->data != self )
1214 styleMask |= NSClosableWindowMask;
1217 primaryWindow = [[NSWindow alloc] initWithContentRect:contentRect styleMask: styleMask backing:NSBackingStoreBuffered defer:YES];
1219 /* Not to be released when closed */
1220 [primaryWindow setReleasedWhenClosed:NO];
1221 [primaryWindow setExcludedFromWindowsMenu: YES]; /* we're using custom window menu handling */
1224 AngbandView *angbandView = [[AngbandView alloc] initWithFrame:contentRect];
1225 [angbandView setAngbandContext:self];
1226 [angbandViews addObject:angbandView];
1227 [primaryWindow setContentView:angbandView];
1228 [angbandView release];
1230 /* We are its delegate */
1231 [primaryWindow setDelegate:self];
1233 /* Update our image, since this is probably the first angband view
1237 return primaryWindow;
1242 #pragma mark View/Window Passthrough
1245 * This is what our views call to get us to draw to the window
1247 - (void)drawRect:(NSRect)rect inView:(NSView *)view
1249 /* Take this opportunity to throttle so we don't flush faster than desired.
1251 BOOL viewInLiveResize = [view inLiveResize];
1252 if (! viewInLiveResize) [self throttle];
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);
1265 return [[[angbandViews lastObject] window] isVisible];
1268 - (BOOL)isMainWindow
1270 return [[[angbandViews lastObject] window] isMainWindow];
1273 - (void)setNeedsDisplay:(BOOL)val
1275 for (NSView *angbandView in angbandViews)
1277 [angbandView setNeedsDisplay:val];
1281 - (void)setNeedsDisplayInBaseRect:(NSRect)rect
1283 for (NSView *angbandView in angbandViews)
1285 [angbandView setNeedsDisplayInRect: rect];
1289 - (void)displayIfNeeded
1291 [[self activeView] displayIfNeeded];
1294 - (int)terminalIndex
1298 for( termIndex = 0; termIndex < ANGBAND_TERM_MAX; termIndex++ )
1300 if( angband_term[termIndex] == self->terminal )
1309 - (void)resizeTerminalWithContentRect: (NSRect)contentRect saveToDefaults: (BOOL)saveToDefaults
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 );
1314 if (newRows < 1 || newColumns < 1) return;
1315 self->cols = newColumns;
1316 self->rows = newRows;
1318 if( saveToDefaults )
1320 int termIndex = [self terminalIndex];
1321 NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
1323 if( termIndex < (int)[terminals count] )
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];
1329 NSMutableArray *mutableTerminals = [[NSMutableArray alloc] initWithArray: terminals];
1330 [mutableTerminals replaceObjectAtIndex: termIndex withObject: mutableTerm];
1332 [[NSUserDefaults standardUserDefaults] setValue: mutableTerminals forKey: AngbandTerminalsDefaultsKey];
1333 [mutableTerminals release];
1334 [mutableTerm release];
1336 [[NSUserDefaults standardUserDefaults] synchronize];
1340 Term_activate( self->terminal );
1341 Term_resize( (int)newColumns, (int)newRows);
1343 Term_activate( old );
1346 - (void)saveWindowVisibleToDefaults: (BOOL)windowVisible
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];
1352 if( termIndex < (int)[terminals count] )
1354 NSMutableDictionary *mutableTerm = [[NSMutableDictionary alloc] initWithDictionary: [terminals objectAtIndex: termIndex]];
1355 [mutableTerm setValue: [NSNumber numberWithBool: safeVisibility] forKey: AngbandTerminalVisibleDefaultsKey];
1357 NSMutableArray *mutableTerminals = [[NSMutableArray alloc] initWithArray: terminals];
1358 [mutableTerminals replaceObjectAtIndex: termIndex withObject: mutableTerm];
1360 [[NSUserDefaults standardUserDefaults] setValue: mutableTerminals forKey: AngbandTerminalsDefaultsKey];
1361 [mutableTerminals release];
1362 [mutableTerm release];
1366 - (BOOL)windowVisibleUsingDefaults
1368 int termIndex = [self terminalIndex];
1370 if( termIndex == 0 )
1375 NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
1378 if( termIndex < (int)[terminals count] )
1380 NSDictionary *term = [terminals objectAtIndex: termIndex];
1381 NSNumber *visibleValue = [term valueForKey: AngbandTerminalVisibleDefaultsKey];
1383 if( visibleValue != nil )
1385 visible = [visibleValue boolValue];
1393 #pragma mark NSWindowDelegate Methods
1395 /*- (void)windowWillStartLiveResize: (NSNotification *)notification
1399 - (void)windowDidEndLiveResize: (NSNotification *)notification
1401 NSWindow *window = [notification object];
1402 NSRect contentRect = [window contentRectForFrameRect: [window frame]];
1403 [self resizeTerminalWithContentRect: contentRect saveToDefaults: YES];
1406 /*- (NSSize)windowWillResize: (NSWindow *)sender toSize: (NSSize)frameSize
1410 - (void)windowDidEnterFullScreen: (NSNotification *)notification
1412 NSWindow *window = [notification object];
1413 NSRect contentRect = [window contentRectForFrameRect: [window frame]];
1414 [self resizeTerminalWithContentRect: contentRect saveToDefaults: NO];
1417 - (void)windowDidExitFullScreen: (NSNotification *)notification
1419 NSWindow *window = [notification object];
1420 NSRect contentRect = [window contentRectForFrameRect: [window frame]];
1421 [self resizeTerminalWithContentRect: contentRect saveToDefaults: NO];
1424 - (void)windowDidBecomeMain:(NSNotification *)notification
1426 NSWindow *window = [notification object];
1428 if( window != self->primaryWindow )
1433 int termIndex = [self terminalIndex];
1434 NSMenuItem *item = [[[NSApplication sharedApplication] windowsMenu] itemWithTag: AngbandWindowMenuItemTagBase + termIndex];
1435 [item setState: NSOnState];
1437 if( [[NSFontPanel sharedFontPanel] isVisible] )
1439 [[NSFontPanel sharedFontPanel] setPanelFont: [self selectionFont] isMultiple: NO];
1443 - (void)windowDidResignMain: (NSNotification *)notification
1445 NSWindow *window = [notification object];
1447 if( window != self->primaryWindow )
1452 int termIndex = [self terminalIndex];
1453 NSMenuItem *item = [[[NSApplication sharedApplication] windowsMenu] itemWithTag: AngbandWindowMenuItemTagBase + termIndex];
1454 [item setState: NSOffState];
1457 - (void)windowWillClose: (NSNotification *)notification
1459 [self saveWindowVisibleToDefaults: NO];
1465 @implementation AngbandView
1477 - (void)drawRect:(NSRect)rect
1479 if (! angbandContext)
1481 /* Draw bright orange, 'cause this ain't right */
1482 [[NSColor orangeColor] set];
1483 NSRectFill([self bounds]);
1487 /* Tell the Angband context to draw into us */
1488 [angbandContext drawRect:rect inView:self];
1492 - (void)setAngbandContext:(AngbandContext *)context
1494 angbandContext = context;
1497 - (AngbandContext *)angbandContext
1499 return angbandContext;
1502 - (void)setFrameSize:(NSSize)size
1504 BOOL changed = ! NSEqualSizes(size, [self frame].size);
1505 [super setFrameSize:size];
1506 if (changed) [angbandContext angbandViewDidScale:self];
1509 - (void)viewWillStartLiveResize
1511 [angbandContext viewWillStartLiveResize:self];
1514 - (void)viewDidEndLiveResize
1516 [angbandContext viewDidEndLiveResize:self];
1522 * Delay handling of double-clicked savefiles
1524 Boolean open_when_ready = FALSE;
1529 * ------------------------------------------------------------------------
1530 * Some generic functions
1531 * ------------------------------------------------------------------------ */
1534 * Sets an Angband color at a given index
1536 static void set_color_for_index(int idx)
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];
1545 CGContextSetRGBFillColor([[NSGraphicsContext currentContext] graphicsPort], rv/255., gv/255., bv/255., 1.);
1549 * Remember the current character in UserDefaults so we can select it by
1550 * default next time.
1552 static void record_current_savefile(void)
1554 NSString *savefileString = [[NSString stringWithCString:savefile encoding:NSMacOSRomanStringEncoding] lastPathComponent];
1557 NSUserDefaults *angbandDefs = [NSUserDefaults angbandDefaults];
1558 [angbandDefs setObject:savefileString forKey:@"SaveFile"];
1559 [angbandDefs synchronize];
1565 * ------------------------------------------------------------------------
1566 * Support for the "z-term.c" package
1567 * ------------------------------------------------------------------------ */
1571 * Initialize a new Term
1573 static void Term_init_cocoa(term *t)
1575 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1576 AngbandContext *context = [[AngbandContext alloc] init];
1578 /* Give the term a hard retain on context (for GC) */
1579 t->data = (void *)CFRetain(context);
1582 /* Handle graphics */
1583 t->higher_pict = !! use_graphics;
1584 t->always_pict = FALSE;
1586 NSDisableScreenUpdates();
1588 /* Figure out the frame autosave name based on the index of this term */
1589 NSString *autosaveName = nil;
1591 for (termIdx = 0; termIdx < ANGBAND_TERM_MAX; termIdx++)
1593 if (angband_term[termIdx] == t)
1595 autosaveName = [NSString stringWithFormat:@"AngbandTerm-%d", termIdx];
1601 NSString *fontName = [[NSUserDefaults angbandDefaults] stringForKey:[NSString stringWithFormat:@"FontName-%d", termIdx]];
1602 if (! fontName) fontName = [default_font fontName];
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]];
1609 if( fontSizeNumber != nil )
1611 fontSize = [fontSizeNumber floatValue];
1614 [context setSelectionFont:[NSFont fontWithName:fontName size:fontSize] adjustTerminal: NO];
1616 NSArray *terminalDefaults = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
1617 NSInteger rows = 24;
1618 NSInteger columns = 80;
1620 if( termIdx < (int)[terminalDefaults count] )
1622 NSDictionary *term = [terminalDefaults objectAtIndex: termIdx];
1623 NSInteger defaultRows = [[term valueForKey: AngbandTerminalRowsDefaultsKey] integerValue];
1624 NSInteger defaultColumns = [[term valueForKey: AngbandTerminalColumnsDefaultsKey] integerValue];
1626 if (defaultRows > 0) rows = defaultRows;
1627 if (defaultColumns > 0) columns = defaultColumns;
1630 context->cols = columns;
1631 context->rows = rows;
1633 /* Get the window */
1634 NSWindow *window = [context makePrimaryWindow];
1636 /* Set its title and, for auxiliary terms, tentative size */
1639 [window setTitle:@"Hengband"];
1641 /* Set minimum size (80x24) */
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];
1649 [window setTitle:[NSString stringWithFormat:@"Term %d", termIdx]];
1650 /* Set minimum size (1x1) */
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];
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:)])
1665 NSWindowCollectionBehavior behavior = [window collectionBehavior];
1666 behavior |= (termIdx == 0 ? Angband_NSWindowCollectionBehaviorFullScreenPrimary : Angband_NSWindowCollectionBehaviorFullScreenAuxiliary);
1667 [window setCollectionBehavior:behavior];
1670 /* No Resume support yet, though it would not be hard to add */
1671 if ([window respondsToSelector:@selector(setRestorable:)])
1673 [window setRestorable:NO];
1676 /* default window placement */ {
1677 static NSRect overallBoundingRect;
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 */
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];
1697 overallBoundingRect = [window frame];
1698 [window setFrame: originalMainTermFrame display: NO];
1701 static NSRect mainTermBaseRect;
1702 NSRect windowFrame = [window frame];
1706 /* The height and width adjustments were determined experimentally,
1707 * so that the rest of the windows line up nicely without
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;
1715 else if( termIdx == 1 )
1717 windowFrame.origin.x = NSMinX( mainTermBaseRect );
1718 windowFrame.origin.y = NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
1720 else if( termIdx == 2 )
1722 windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
1723 windowFrame.origin.y = NSMaxY( mainTermBaseRect ) - NSHeight( windowFrame );
1725 else if( termIdx == 3 )
1727 windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
1728 windowFrame.origin.y = NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
1730 else if( termIdx == 4 )
1732 windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
1733 windowFrame.origin.y = NSMinY( mainTermBaseRect );
1735 else if( termIdx == 5 )
1737 windowFrame.origin.x = NSMinX( mainTermBaseRect ) + NSWidth( windowFrame ) + 1.0;
1738 windowFrame.origin.y = NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
1741 [window setFrame: windowFrame display: NO];
1744 /* Override the default frame above if the user has adjusted windows in
1746 if (autosaveName) [window setFrameAutosaveName:autosaveName];
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];
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];
1758 NSEnableScreenUpdates();
1760 /* Set "mapped" flag */
1761 t->mapped_flag = true;
1770 static void Term_nuke_cocoa(term *t)
1772 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1774 AngbandContext *context = t->data;
1777 /* Tell the context to get rid of its windows, etc. */
1780 /* Balance our CFRetain from when we created it */
1791 * Returns the CGImageRef corresponding to an image with the given name in the
1792 * resource directory, transferring ownership to the caller
1794 static CGImageRef create_angband_image(NSString *path)
1796 CGImageRef decodedImage = NULL, result = NULL;
1798 /* Try using ImageIO to load the image */
1801 NSURL *url = [[NSURL alloc] initFileURLWithPath:path isDirectory:NO];
1804 NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:(id)kCFBooleanTrue, kCGImageSourceShouldCache, nil];
1805 CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, (CFDictionaryRef)options);
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);
1818 /* Draw the sucker to defeat ImageIO's weird desire to cache and decode on
1819 * demand. Our images aren't that big! */
1822 size_t width = CGImageGetWidth(decodedImage), height = CGImageGetHeight(decodedImage);
1824 /* Compute our own bitmap info */
1825 CGBitmapInfo imageBitmapInfo = CGImageGetBitmapInfo(decodedImage);
1826 CGBitmapInfo contextBitmapInfo = kCGBitmapByteOrderDefault;
1828 switch (imageBitmapInfo & kCGBitmapAlphaInfoMask) {
1829 case kCGImageAlphaNone:
1830 case kCGImageAlphaNoneSkipLast:
1831 case kCGImageAlphaNoneSkipFirst:
1833 contextBitmapInfo |= kCGImageAlphaNone;
1836 /* Some alpha, use premultiplied last which is most efficient. */
1837 contextBitmapInfo |= kCGImageAlphaPremultipliedLast;
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);
1849 /* Done with these things */
1851 CGImageRelease(decodedImage);
1859 static errr Term_xtra_cocoa_react(void)
1861 /* Don't actually switch graphics until the game is running */
1862 if (!initialized || !game_in_progress) return (-1);
1864 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1865 AngbandContext *angbandContext = Term->data;
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)
1872 graphics_mode *new_mode;
1873 if (graf_mode_req != GRAPHICS_NONE) {
1874 new_mode = get_graphics_mode(graf_mode_req);
1879 /* Get rid of the old image. CGImageRelease is NULL-safe. */
1880 CGImageRelease(pict_image);
1883 /* Try creating the image if we want one */
1884 if (new_mode != NULL)
1886 NSString *img_path = [NSString stringWithFormat:@"%s/%s", new_mode->path, new_mode->file];
1887 pict_image = create_angband_image(img_path);
1889 /* If we failed to create the image, set the new desired mode to
1895 /* Record what we did */
1896 use_graphics = new_mode ? new_mode->grafID : 0;
1898 /* This global is not in Hengband. */
1899 use_transparency = (new_mode != NULL);
1901 ANGBAND_GRAF = (new_mode ? new_mode->graf : "ascii");
1902 current_graphics_mode = new_mode;
1904 /* Enable or disable higher picts. Note: this should be done for all
1906 angbandContext->terminal->higher_pict = !! use_graphics;
1908 if (pict_image && current_graphics_mode)
1910 /* Compute the row and column count via the image height and width.
1912 pict_rows = (int)(CGImageGetHeight(pict_image) / current_graphics_mode->cell_height);
1913 pict_cols = (int)(CGImageGetWidth(pict_image) / current_graphics_mode->cell_width);
1922 if (initialized && game_in_progress)
1935 * Do a "special thing"
1937 static errr Term_xtra_cocoa(int n, int v)
1939 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1940 AngbandContext* angbandContext = Term->data;
1948 case TERM_XTRA_NOISE:
1957 case TERM_XTRA_SOUND:
1961 /* Process random events */
1962 case TERM_XTRA_BORED:
1964 /* Show or hide cocoa windows based on the subwindow flags set by
1966 AngbandUpdateWindowVisibility();
1968 /* Process an event */
1969 (void)check_events(CHECK_EVENTS_NO_WAIT);
1975 /* Process pending events */
1976 case TERM_XTRA_EVENT:
1978 /* Process an event */
1979 (void)check_events(v);
1985 /* Flush all pending events (if any) */
1986 case TERM_XTRA_FLUSH:
1988 /* Hack -- flush all events */
1989 while (check_events(CHECK_EVENTS_DRAIN)) /* loop */;
1995 /* Hack -- Change the "soft level" */
1996 case TERM_XTRA_LEVEL:
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. */
2004 /* Clear the screen */
2005 case TERM_XTRA_CLEAR:
2007 [angbandContext lockFocus];
2008 [[NSColor blackColor] set];
2009 NSRect imageRect = {NSZeroPoint, [angbandContext imageSize]};
2010 NSRectFillUsingOperation(imageRect, NSCompositeCopy);
2011 [angbandContext unlockFocus];
2012 [angbandContext setNeedsDisplay:YES];
2017 /* React to changes */
2018 case TERM_XTRA_REACT:
2020 /* React to changes */
2021 return (Term_xtra_cocoa_react());
2024 /* Delay (milliseconds) */
2025 case TERM_XTRA_DELAY:
2031 double seconds = v / 1000.;
2032 NSDate* date = [NSDate dateWithTimeIntervalSinceNow:seconds];
2038 event = [NSApp nextEventMatchingMask:-1 untilDate:date inMode:NSDefaultRunLoopMode dequeue:YES];
2039 if (event) send_event(event);
2041 } while ([date timeIntervalSinceNow] >= 0);
2049 case TERM_XTRA_FRESH:
2051 /* No-op -- see #1669
2052 * [angbandContext displayIfNeeded]; */
2068 static errr Term_curs_cocoa(int x, int y)
2070 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2071 AngbandContext *angbandContext = Term->data;
2074 NSRect rect = [angbandContext rectInImageForTileAtX:x Y:y];
2076 /* We'll need to redisplay in that rect */
2077 NSRect redisplayRect = rect;
2079 /* Go to the pixel boundaries corresponding to this tile */
2080 rect = crack_rect(rect, AngbandScaleIdentity, push_options(x, y));
2082 /* Lock focus and draw it */
2083 [angbandContext lockFocus];
2084 [[NSColor yellowColor] set];
2085 NSFrameRectWithWidth(rect, 1);
2086 [angbandContext unlockFocus];
2088 /* Invalidate that rect */
2089 [angbandContext setNeedsDisplayInBaseRect:redisplayRect];
2097 * Low level graphics (Assumes valid input)
2099 * Erase "n" characters starting at (x,y)
2101 static errr Term_wipe_cocoa(int x, int y, int n)
2103 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2104 AngbandContext *angbandContext = Term->data;
2108 * Erase the block of characters. Mimic the geometry calculations for
2109 * the cleared rectangle in Term_text_cocoa().
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));
2117 /* Lock focus and clear */
2118 [angbandContext lockFocus];
2119 [[NSColor blackColor] set];
2121 [angbandContext unlockFocus];
2122 [angbandContext setNeedsDisplayInBaseRect:rect];
2130 static void draw_image_tile(CGImageRef image, NSRect srcRect, NSRect dstRect, NSCompositingOperation op)
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);
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);
2147 static errr Term_pict_cocoa(int x, int y, int n, TERM_COLOR *ap,
2148 const char *cp, const TERM_COLOR *tap,
2152 /* Paranoia: Bail if we don't have a current graphics mode */
2153 if (! current_graphics_mode) return -1;
2155 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2156 AngbandContext* angbandContext = Term->data;
2159 [angbandContext lockFocus];
2161 NSRect destinationRect = [angbandContext rectInImageForTileAtX:x Y:y];
2163 /* Expand the rect to every touching pixel to figure out what to redisplay
2165 NSRect redisplayRect = crack_rect(destinationRect, AngbandScaleIdentity, PUSH_RIGHT | PUSH_TOP | PUSH_BOTTOM | PUSH_LEFT);
2167 /* Expand our destinationRect */
2168 destinationRect = crack_rect(destinationRect, AngbandScaleIdentity, push_options(x, y));
2170 /* Scan the input */
2172 int graf_width = current_graphics_mode->cell_width;
2173 int graf_height = current_graphics_mode->cell_height;
2175 for (i = 0; i < n; i++)
2178 TERM_COLOR a = *ap++;
2181 TERM_COLOR ta = *tap++;
2185 /* Graphics -- if Available and Needed */
2186 if (use_graphics && (a & 0x80) && (c & 0x80))
2192 /* Primary Row and Col */
2193 row = ((byte)a & 0x7F) % pict_rows;
2194 col = ((byte)c & 0x7F) % pict_cols;
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;
2202 /* Terrain Row and Col */
2203 t_row = ((byte)ta & 0x7F) % pict_rows;
2204 t_col = ((byte)tc & 0x7F) % pict_cols;
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;
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)
2218 draw_image_tile(pict_image, terrainRect, destinationRect, NSCompositeCopy);
2219 draw_image_tile(pict_image, sourceRect, destinationRect, NSCompositeSourceOver);
2223 draw_image_tile(pict_image, sourceRect, destinationRect, NSCompositeCopy);
2228 [angbandContext unlockFocus];
2229 [angbandContext setNeedsDisplayInBaseRect:redisplayRect];
2238 * Low level graphics. Assumes valid input.
2240 * Draw several ("n") chars, with an attr, at a given location.
2242 static errr Term_text_cocoa(int x, int y, int n, byte_hack a, concptr cp)
2244 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2245 NSRect redisplayRect = NSZeroRect;
2246 AngbandContext* angbandContext = Term->data;
2248 /* Focus on our layer */
2249 [angbandContext lockFocus];
2251 /* Starting pixel */
2252 NSRect charRect = [angbandContext rectInImageForTileAtX:x Y:y];
2254 const CGFloat tileWidth = angbandContext->tileSize.width;
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;
2262 switch (a / MAX_COLORS) {
2264 [[NSColor blackColor] set];
2267 set_color_for_index(a % MAX_COLORS);
2270 set_color_for_index(TERM_SHADE);
2274 NSRect rectToClear = charRect;
2275 rectToClear.size.width = tileWidth * n;
2276 rectToClear = crack_rect(rectToClear, AngbandScaleIdentity,
2277 leftPushOptions | rightPushOptions);
2278 NSRectFill(rectToClear);
2280 NSFont *selectionFont = [[angbandContext selectionFont] screenFont];
2281 [selectionFont set];
2284 set_color_for_index(a % MAX_COLORS);
2287 NSRect rectToDraw = charRect;
2289 for (i=0; i < n; i++) {
2290 [angbandContext drawWChar:cp[i] inRect:rectToDraw];
2291 rectToDraw.origin.x += tileWidth;
2294 [angbandContext unlockFocus];
2295 /* Invalidate what we just drew */
2296 [angbandContext setNeedsDisplayInBaseRect:rectToClear];
2305 * Post a nonsense event so that our event loop wakes up
2307 static void wakeup_event_loop(void)
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];
2316 * Create and initialize window number "i"
2318 static term *term_data_link(int i)
2320 NSArray *terminalDefaults = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
2321 NSInteger rows = 24;
2322 NSInteger columns = 80;
2324 if( i < (int)[terminalDefaults count] )
2326 NSDictionary *term = [terminalDefaults objectAtIndex: i];
2327 rows = [[term valueForKey: AngbandTerminalRowsDefaultsKey] integerValue];
2328 columns = [[term valueForKey: AngbandTerminalColumnsDefaultsKey] integerValue];
2332 term *newterm = ZNEW(term);
2334 /* Initialize the term */
2335 term_init(newterm, columns, rows, 256 /* keypresses, for some reason? */);
2337 /* Differentiate between BS/^h, Tab/^i, etc. */
2338 /* newterm->complex_input = TRUE; */
2340 /* Use a "software" cursor */
2341 newterm->soft_cursor = TRUE;
2343 /* Erase with "white space" */
2344 newterm->attr_blank = TERM_WHITE;
2345 newterm->char_blank = ' ';
2347 /* Prepare the init/nuke hooks */
2348 newterm->init_hook = Term_init_cocoa;
2349 newterm->nuke_hook = Term_nuke_cocoa;
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; */
2359 /* Global pointer */
2360 angband_term[i] = newterm;
2366 * Load preferences from preferences file for current host+current user+
2367 * current application.
2369 static void load_prefs()
2371 NSUserDefaults *defs = [NSUserDefaults angbandDefaults];
2373 /* Make some default defaults */
2374 NSMutableArray *defaultTerms = [[NSMutableArray alloc] init];
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++ )
2418 NSDictionary *standardTerm = [NSDictionary dictionaryWithObjectsAndKeys:
2419 [NSNumber numberWithInt: rows], AngbandTerminalRowsDefaultsKey,
2420 [NSNumber numberWithInt: columns], AngbandTerminalColumnsDefaultsKey,
2421 [NSNumber numberWithBool: visible], AngbandTerminalVisibleDefaultsKey,
2423 [defaultTerms addObject: standardTerm];
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,
2434 [defs registerDefaults:defaults];
2436 [defaultTerms release];
2438 /* Preferred graphics mode */
2439 graf_mode_req = [defs integerForKey:@"GraphicsID"];
2441 /* Use sounds; set the Angband global */
2442 use_sound = ([defs boolForKey:AngbandSoundDefaultsKey] == YES) ? TRUE : FALSE;
2445 frames_per_second = [defs integerForKey:@"FramesPerSecond"];
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];
2453 * Arbitary limit on number of possible samples per event
2455 #define MAX_SAMPLES 16
2458 * Struct representing all data for a set of event samples
2462 int num; /* Number of available samples for this event */
2463 NSSound *sound[MAX_SAMPLES];
2464 } sound_sample_list;
2467 * Array of event sound structs
2469 static sound_sample_list samples[MSG_MAX];
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.
2480 static void load_sounds(void)
2482 char sound_dir[1024];
2487 /* Build the "sound" path */
2488 path_build(sound_dir, sizeof(sound_dir), ANGBAND_DIR_XTRA, "sound");
2490 /* Find and open the config file */
2491 path_build(path, sizeof(path), sound_dir, "sound.cfg");
2492 fff = my_fopen(path, "r");
2497 NSLog(@"The sound configuration file could not be opened.");
2501 /* Instantiate an autorelease pool for use by NSSound */
2502 NSAutoreleasePool *autorelease_pool;
2503 autorelease_pool = [[NSAutoreleasePool alloc] init];
2505 /* Use a dictionary to unique sounds, so we can share NSSounds across
2506 * multiple events */
2507 NSMutableDictionary *sound_dict = [NSMutableDictionary dictionary];
2510 * This loop may take a while depending on the count and size of samples
2514 /* Parse the file */
2515 /* Lines are always of the form "name = sample [sample ...]" */
2516 while (my_fgets(fff, buffer, sizeof(buffer)) == 0)
2519 char *cfg_sample_list;
2525 /* Skip anything not beginning with an alphabetic character */
2526 if (!buffer[0] || !isalpha((unsigned char)buffer[0])) continue;
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;
2534 /* Set the message name, and terminate at first space */
2538 /* Make sure this is a valid event name */
2539 for (event = MSG_MAX - 1; event >= 0; event--)
2541 if (strcmp(msg_name, angband_sound_name[event]) == 0)
2544 if (event < 0) continue;
2546 /* Advance the sample list pointer so it's at the beginning of text */
2548 if (!cfg_sample_list[0]) continue;
2550 /* Terminate the current token */
2551 cur_token = cfg_sample_list;
2552 search = strchr(cur_token, ' ');
2556 next_token = search + 1;
2564 * Now we find all the sample names and add them one by one
2568 int num = samples[event].num;
2570 /* Don't allow too many samples */
2571 if (num >= MAX_SAMPLES) break;
2573 NSString *token_string = [NSString stringWithUTF8String:cur_token];
2574 NSSound *sound = [sound_dict objectForKey:token_string];
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)
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];
2591 /* Store it if we loaded it */
2594 samples[event].sound[num] = [sound retain];
2596 /* Imcrement the sample count */
2597 samples[event].num++;
2601 /* Figure out next token */
2602 cur_token = next_token;
2605 /* Try to find a space */
2606 search = strchr(cur_token, ' ');
2608 /* If we can find one, terminate, and set new "next" */
2612 next_token = search + 1;
2616 /* Otherwise prevent infinite looping */
2623 /* Release the autorelease pool */
2624 [autorelease_pool release];
2626 /* Close the file */
2631 * Play sound effects asynchronously. Select a sound from any available
2632 * for the required event, and bridge to Cocoa to play it.
2634 static void play_sound(int event)
2637 if (event < 0 || event >= MSG_MAX) return;
2639 /* Load sounds just-in-time (once) */
2640 static BOOL loaded = NO;
2646 /* Check there are samples for this event */
2647 if (!samples[event].num) return;
2649 /* Instantiate an autorelease pool for use by NSSound */
2650 NSAutoreleasePool *autorelease_pool;
2651 autorelease_pool = [[NSAutoreleasePool alloc] init];
2653 /* Choose a random event */
2654 int s = randint0(samples[event].num);
2656 /* Stop the sound if it's currently playing */
2657 if ([samples[event].sound[s] isPlaying])
2658 [samples[event].sound[s] stop];
2660 /* Play the sound */
2661 [samples[event].sound[s] play];
2663 /* Release the autorelease pool */
2664 [autorelease_pool drain];
2670 static void init_windows(void)
2672 /* Create the main window */
2673 term *primary = term_data_link(0);
2675 /* Prepare to create any additional windows */
2677 for (i=1; i < ANGBAND_TERM_MAX; i++) {
2681 /* Activate the primary term */
2682 Term_activate(primary);
2686 * Handle the "open_when_ready" flag
2688 static void handle_open_when_ready(void)
2690 /* Check the flag XXX XXX XXX make a function for this */
2691 if (open_when_ready && initialized && !game_in_progress)
2694 open_when_ready = FALSE;
2696 /* Game is in progress */
2697 game_in_progress = TRUE;
2699 /* Wait for a keypress */
2706 * Handle quit_when_ready, by Peter Ammon,
2707 * slightly modified to check inkey_flag.
2709 static void quit_calmly(void)
2711 /* Quit immediately if game's not started */
2712 if (!game_in_progress || !character_generated) quit(NULL);
2714 /* Save the game and Quit (if it's safe) */
2717 /* Hack -- Forget messages and term */
2719 Term->mapped_flag = FALSE;
2722 do_cmd_save_game(FALSE);
2723 record_current_savefile();
2730 /* Wait until inkey_flag is set */
2736 * Returns YES if we contain an AngbandView (and hence should direct our events
2739 static BOOL contains_angband_view(NSView *view)
2741 if ([view isKindOfClass:[AngbandView class]]) return YES;
2742 for (NSView *subview in [view subviews]) {
2743 if (contains_angband_view(subview)) return YES;
2750 * Queue mouse presses if they occur in the map section of the main window.
2752 static void AngbandHandleEventMouseDown( NSEvent *event )
2755 AngbandContext *angbandContext = [[[event window] contentView] angbandContext];
2756 AngbandContext *mainAngbandContext = angband_term[0]->data;
2758 if (mainAngbandContext->primaryWindow && [[event window] windowNumber] == [mainAngbandContext->primaryWindow windowNumber])
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];
2766 /* Adjust for border; add border height because window origin is at
2768 windowPoint = NSMakePoint( windowPoint.x - border.width, windowPoint.y + border.height );
2770 NSPoint p = [[[event window] contentView] convertPoint: windowPoint fromView: nil];
2771 x = floor( p.x / tileSize.width );
2772 y = floor( p.y / tileSize.height );
2774 /* Being safe about this, since xcode doesn't seem to like the
2775 * bool_hack stuff */
2776 BOOL displayingMapInterface = ((int)inkey_flag != 0);
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);
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
2785 if (!displayingMapInterface || (displayingMapInterface && mouseInMapSection))
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;
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()) */
2800 Term_mousepress(x, y, button);
2804 /* Pass click through to permit focus change, resize, etc. */
2805 [NSApp sendEvent:event];
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)
2816 /* If the receiving window is not an Angband window, then do nothing */
2817 if (! contains_angband_view([[event window] contentView]))
2819 [NSApp sendEvent:event];
2823 /* Analyze the event */
2824 switch ([event type])
2828 /* Try performing a key equivalent */
2829 if ([[NSApp mainMenu] performKeyEquivalent:event]) break;
2831 unsigned modifiers = [event modifierFlags];
2833 /* Send all NSCommandKeyMasks through */
2834 if (modifiers & NSCommandKeyMask)
2836 [NSApp sendEvent:event];
2840 if (! [[event characters] length]) break;
2843 /* Extract some modifiers */
2845 /* Caught above so don't do anything with it here. */
2846 int mx = !! (modifiers & NSCommandKeyMask);
2848 int mc = !! (modifiers & NSControlKeyMask);
2849 int ms = !! (modifiers & NSShiftKeyMask);
2850 int mo = !! (modifiers & NSAlternateKeyMask);
2851 int kp = !! (modifiers & NSNumericPadKeyMask);
2854 /* Get the Angband char corresponding to this unichar */
2855 unichar c = [[event characters] characterAtIndex:0];
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.
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;
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;
2894 /* Hide the mouse pointer */
2895 [NSCursor setHiddenUntilMouseMoves:YES];
2901 /* Enqueue the keypress */
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);
2913 } else if (c < 4096 || (c >= 0xF700 && c <= 0xF77F)) {
2917 /* Begin the macro trigger. */
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');
2927 * Put part of the range Apple reserves for special keys
2928 * into 0 - 127 since that range has been handled normally.
2934 /* Encode the value as two printable characters. */
2935 part = (c >> 6) & 63;
2937 cenc = 'a' + (part - 38);
2938 } else if (part > 12) {
2939 cenc = 'A' + (part - 12);
2943 Term_keypress(cenc);
2946 cenc = 'a' + (part - 38);
2947 } else if (part > 12) {
2948 cenc = 'A' + (part - 12);
2952 Term_keypress(cenc);
2954 /* End the macro trigger. */
2961 case NSLeftMouseDown:
2962 case NSRightMouseDown:
2963 AngbandHandleEventMouseDown(event);
2966 case NSApplicationDefined:
2968 if ([event subtype] == AngbandEventWakeup)
2976 [NSApp sendEvent:event];
2983 * Check for Events, return TRUE if we process any
2985 static BOOL check_events(int wait)
2988 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2990 /* Handles the quit_when_ready flag */
2991 if (quit_when_ready) quit_calmly();
2994 if (wait == CHECK_EVENTS_WAIT) endDate = [NSDate distantFuture];
2995 else endDate = [NSDate distantPast];
2999 if (quit_when_ready)
3001 /* send escape events until we quit */
3002 Term_keypress(0x1B);
3007 event = [NSApp nextEventMatchingMask:-1 untilDate:endDate inMode:NSDefaultRunLoopMode dequeue:YES];
3013 if (send_event(event)) break;
3019 /* Something happened */
3025 * Hook to tell the user something important
3027 static void hook_plog(const char * str)
3031 NSString *string = [NSString stringWithCString:str encoding:NSMacOSRomanStringEncoding];
3032 NSRunAlertPanel(@"Danger Will Robinson", @"%@", @"OK", nil, nil, string);
3038 * Hook to tell the user something, and then quit
3040 static void hook_quit(const char * str)
3047 * ------------------------------------------------------------------------
3049 * ------------------------------------------------------------------------ */
3051 @interface AngbandAppDelegate : NSObject {
3052 IBOutlet NSMenu *terminalsMenu;
3053 NSMenu *_commandMenu;
3054 NSDictionary *_commandMenuTagMap;
3057 @property (nonatomic, retain) IBOutlet NSMenu *commandMenu;
3058 @property (nonatomic, retain) NSDictionary *commandMenuTagMap;
3060 - (IBAction)newGame:sender;
3061 - (IBAction)openGame:sender;
3063 - (IBAction)editFont:sender;
3064 - (IBAction)setGraphicsMode:(NSMenuItem *)sender;
3065 - (IBAction)toggleSound:(NSMenuItem *)sender;
3067 - (IBAction)setRefreshRate:(NSMenuItem *)menuItem;
3068 - (IBAction)selectWindow: (id)sender;
3072 @implementation AngbandAppDelegate
3074 @synthesize commandMenu=_commandMenu;
3075 @synthesize commandMenuTagMap=_commandMenuTagMap;
3077 - (IBAction)newGame:sender
3079 /* Game is in progress */
3080 game_in_progress = TRUE;
3084 - (IBAction)editFont:sender
3086 NSFontPanel *panel = [NSFontPanel sharedFontPanel];
3087 NSFont *termFont = default_font;
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];
3097 [panel setPanelFont:termFont isMultiple:NO];
3098 [panel orderFront:self];
3101 - (void)changeFont:(id)sender
3104 for (mainTerm=0; mainTerm < ANGBAND_TERM_MAX; mainTerm++) {
3105 if ([(id)angband_term[mainTerm]->data isMainWindow]) {
3110 /* Bug #1709: Only change font for angband windows */
3111 if (mainTerm == ANGBAND_TERM_MAX) return;
3113 NSFont *oldFont = default_font;
3114 NSFont *newFont = [sender convertFont:oldFont];
3115 if (! newFont) return; /*paranoia */
3117 /* Store as the default font if we changed the first term */
3118 if (mainTerm == 0) {
3120 [default_font release];
3121 default_font = newFont;
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]];
3132 NSDisableScreenUpdates();
3135 AngbandContext *angbandContext = angband_term[mainTerm]->data;
3136 [(id)angbandContext setSelectionFont:newFont adjustTerminal: YES];
3138 NSEnableScreenUpdates();
3141 - (IBAction)openGame:sender
3143 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
3144 BOOL selectedSomething = NO;
3147 /* Get where we think the save files are */
3148 NSURL *startingDirectoryURL = [NSURL fileURLWithPath:[NSString stringWithCString:ANGBAND_DIR_SAVE encoding:NSASCIIStringEncoding] isDirectory:YES];
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];
3160 panelResult = [panel runModal];
3161 if (panelResult == NSOKButton)
3163 NSArray* fileURLs = [panel URLs];
3164 if ([fileURLs count] > 0 && [[fileURLs objectAtIndex:0] isFileURL])
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];
3175 if (selectedSomething)
3177 /* Remember this so we can select it by default next time */
3178 record_current_savefile();
3180 /* Game is in progress */
3181 game_in_progress = TRUE;
3188 - (IBAction)saveGame:sender
3190 /* Hack -- Forget messages */
3194 do_cmd_save_game(FALSE);
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();
3202 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
3204 SEL sel = [menuItem action];
3205 NSInteger tag = [menuItem tag];
3207 if( tag >= AngbandWindowMenuItemTagBase && tag < AngbandWindowMenuItemTagBase + ANGBAND_TERM_MAX )
3209 if( tag == AngbandWindowMenuItemTagBase )
3211 /* The main window should always be available and visible */
3216 NSInteger subwindowNumber = tag - AngbandWindowMenuItemTagBase;
3217 return (window_flag[subwindowNumber] > 0);
3223 if (sel == @selector(newGame:))
3225 return ! game_in_progress;
3227 else if (sel == @selector(editFont:))
3231 else if (sel == @selector(openGame:))
3233 return ! game_in_progress;
3235 else if (sel == @selector(setRefreshRate:) && [superitem(menuItem) tag] == 150)
3237 NSInteger fps = [[NSUserDefaults standardUserDefaults] integerForKey: @"FramesPerSecond"];
3238 [menuItem setState: ([menuItem tag] == fps)];
3241 else if( sel == @selector(setGraphicsMode:) )
3243 NSInteger requestedGraphicsMode = [[NSUserDefaults standardUserDefaults] integerForKey: @"GraphicsID"];
3244 [menuItem setState: (tag == requestedGraphicsMode)];
3247 else if( sel == @selector(toggleSound:) )
3249 BOOL is_on = [[NSUserDefaults standardUserDefaults]
3250 boolForKey:AngbandSoundDefaultsKey];
3252 [menuItem setState: ((is_on) ? NSOnState : NSOffState)];
3255 else if( sel == @selector(sendAngbandCommand:) )
3257 /* we only want to be able to send commands during an active game */
3258 return !!game_in_progress;
3264 - (IBAction)setRefreshRate:(NSMenuItem *)menuItem
3266 frames_per_second = [menuItem tag];
3267 [[NSUserDefaults angbandDefaults] setInteger:frames_per_second forKey:@"FramesPerSecond"];
3270 - (IBAction)selectWindow: (id)sender
3272 NSInteger subwindowNumber = [(NSMenuItem *)sender tag] - AngbandWindowMenuItemTagBase;
3273 AngbandContext *context = angband_term[subwindowNumber]->data;
3274 [context->primaryWindow makeKeyAndOrderFront: self];
3275 [context saveWindowVisibleToDefaults: YES];
3278 - (void)prepareWindowsMenu
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]];
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];
3291 /* Add items for the additional term windows */
3292 for( NSInteger i = 1; i < ANGBAND_TERM_MAX; i++ )
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];
3304 - (IBAction)setGraphicsMode:(NSMenuItem *)sender
3306 /* We stashed the graphics mode ID in the menu item's tag */
3307 graf_mode_req = [sender tag];
3309 /* Stash it in UserDefaults */
3310 [[NSUserDefaults angbandDefaults] setInteger:graf_mode_req forKey:@"GraphicsID"];
3311 [[NSUserDefaults angbandDefaults] synchronize];
3313 if (game_in_progress)
3315 /* Hack -- Force redraw */
3318 /* Wake up the event loop so it notices the change */
3319 wakeup_event_loop();
3323 - (IBAction) toggleSound: (NSMenuItem *) sender
3325 BOOL is_on = (sender.state == NSOnState);
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];
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).
3339 - (void)sendAngbandCommand: (id)sender
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];
3345 /* Send a \ to bypass keymaps */
3346 NSEvent *escape = [NSEvent keyEventWithType: NSKeyDown
3347 location: NSZeroPoint
3350 windowNumber: windowNumber
3353 charactersIgnoringModifiers: @"\\"
3356 [[NSApplication sharedApplication] postEvent: escape atStart: NO];
3358 /* Send the actual command (from the original command set) */
3359 NSEvent *keyDown = [NSEvent keyEventWithType: NSKeyDown
3360 location: NSZeroPoint
3363 windowNumber: windowNumber
3366 charactersIgnoringModifiers: command
3369 [[NSApplication sharedApplication] postEvent: keyDown atStart: NO];
3373 * Set up the command menu dynamically, based on CommandMenu.plist.
3375 - (void)prepareCommandMenu
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;
3382 for( NSDictionary *item in commandMenuItems )
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;
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];
3399 NSString *angbandCommand = [item valueForKey: @"AngbandCommand"];
3400 [angbandCommands setObject: angbandCommand forKey: [NSNumber numberWithInteger: [menuItem tag]]];
3404 [commandMenuItems release];
3406 NSDictionary *safeCommands = [[NSDictionary alloc] initWithDictionary: angbandCommands];
3407 self.commandMenuTagMap = safeCommands;
3408 [safeCommands release];
3409 [angbandCommands release];
3412 - (void)awakeFromNib
3414 [super awakeFromNib];
3416 [self prepareWindowsMenu];
3417 [self prepareCommandMenu];
3420 - (void)applicationDidFinishLaunching:sender
3422 [AngbandContext beginGame];
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];
3430 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
3432 if (p_ptr->playing == FALSE || game_is_finished == TRUE)
3434 return NSTerminateNow;
3436 else if (! inkey_flag)
3438 /* For compatibility with other ports, do not quit in this case */
3439 return NSTerminateCancel;
3444 /* player->upkeep->playing = FALSE; */
3446 /* Post an escape event so that we can return from our get-key-event
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;
3457 * Dynamically build the Graphics menu
3459 - (void)menuNeedsUpdate:(NSMenu *)menu {
3461 /* Only the graphics menu is dynamic */
3462 if (! [[menu title] isEqualToString:@"Graphics"])
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)
3472 /* This is the action for all these menu items */
3473 SEL action = @selector(setGraphicsMode:);
3475 /* Add an initial Classic ASCII menu item */
3476 NSMenuItem *classicItem = [menu addItemWithTitle:@"Classic ASCII" action:action keyEquivalent:@""];
3477 [classicItem setTag:GRAPHICS_NONE];
3479 /* Walk through the list of graphics modes */
3480 if (graphics_modes) {
3483 for (i=0; graphics_modes[i].pNext; i++)
3485 const graphics_mode *graf = &graphics_modes[i];
3487 if (graf->grafID == GRAPHICS_NONE) {
3490 /* Make the title. NSMenuItem throws on a nil title, so ensure it's
3493 [[NSString alloc] initWithUTF8String:graf->menuname];
3494 if (! title) title = [@"(Unknown)" copy];
3497 NSMenuItem *item = [menu addItemWithTitle:title action:action keyEquivalent:@""];
3498 [item setTag:graf->grafID];
3504 * Delegate method that gets called if we're asked to open a file.
3506 - (BOOL)application:(NSApplication *)sender openFiles:(NSArray *)filenames
3508 /* Can't open a file once we've started */
3509 if (game_in_progress) return NO;
3511 /* We can only open one file. Use the last one. */
3512 NSString *file = [filenames lastObject];
3513 if (! file) return NO;
3515 /* Put it in savefile */
3516 if (! [file getFileSystemRepresentation:savefile maxLength:sizeof savefile])
3519 game_in_progress = TRUE;
3521 /* Wake us up in case this arrives while we're sitting at the Welcome
3523 wakeup_event_loop();
3530 int main(int argc, char* argv[])
3532 NSApplicationMain(argc, (void*)argv);
3536 #endif /* MACINTOSH || MACH_O_COCOA */