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 AngbandMessageCatalog = @"Localizable";
40 static NSString * const AngbandTerminalsDefaultsKey = @"Terminals";
41 static NSString * const AngbandTerminalRowsDefaultsKey = @"Rows";
42 static NSString * const AngbandTerminalColumnsDefaultsKey = @"Columns";
43 static NSString * const AngbandTerminalVisibleDefaultsKey = @"Visible";
44 static NSString * const AngbandGraphicsDefaultsKey = @"GraphicsID";
45 static NSString * const AngbandFrameRateDefaultsKey = @"FramesPerSecond";
46 static NSString * const AngbandSoundDefaultsKey = @"AllowSound";
47 static NSInteger const AngbandWindowMenuItemTagBase = 1000;
48 static NSInteger const AngbandCommandMenuItemTagBase = 2000;
50 /* We can blit to a large layer or image and then scale it down during live
51 * resize, which makes resizing much faster, at the cost of some image quality
53 #ifndef USE_LIVE_RESIZE_CACHE
54 # define USE_LIVE_RESIZE_CACHE 1
57 /* Global defines etc from Angband 3.5-dev - NRM */
58 #define ANGBAND_TERM_MAX 8
60 static bool new_game = TRUE;
62 #define MAX_COLORS 256
63 #define MSG_MAX SOUND_MAX
65 /* End Angband stuff - NRM */
67 /* Application defined event numbers */
70 AngbandEventWakeup = 1
73 /* Redeclare some 10.7 constants and methods so we can build on 10.6 */
76 Angband_NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7,
77 Angband_NSWindowCollectionBehaviorFullScreenAuxiliary = 1 << 8
80 @interface NSWindow (AngbandLionRedeclares)
81 - (void)setRestorable:(BOOL)flag;
84 /* Delay handling of pre-emptive "quit" event */
85 static BOOL quit_when_ready = FALSE;
87 /* Set to indicate the game is over and we can quit without delay */
88 static Boolean game_is_finished = FALSE;
90 /* Our frames per second (e.g. 60). A value of 0 means unthrottled. */
91 static int frames_per_second;
93 /* Function to get the default font */
94 static NSFont *default_font;
99 * To handle fonts where an individual glyph's bounding box can extend into
100 * neighboring columns, Term_curs_cocoa(), Term_pict_cocoa(),
101 * Term_text_cocoa(), and Term_wipe_cocoa() merely record what needs to be
102 * done with the actual drawing happening in response to the notification to
103 * flush all rows, the TERM_XTRA_FRESH case in Term_xtra_cocoa(). Can not use
104 * the TERM_XTRA_FROSH notification (the per-row flush), since with a software
105 * cursor, there are calls to Term_pict_cocoa(), Term_text_cocoa(), or
106 * Term_wipe_cocoa() to take care of the old cursor position which are not
107 * followed by a row flush.
109 enum PendingCellChangeType {
110 CELL_CHANGE_NONE = 0,
115 struct PendingCellChange {
117 * For text rendering, stores the character as a wchar_t; for tile
118 * rendering, stores the column in the tile set for the source tile.
120 union { wchar_t w; char c; } c;
122 * For text rendering, stores the color; for tile rendering, stores the
123 * row in the tile set for the source tile.
127 * For text rendering, is one if wc is a character that takes up two
128 * columns (i.e. Japanese kanji); otherwise it is zero. For tile
129 * rendering, Stores the column in the tile set for the terrain tile. */
132 * For tile rendering, stores the row in the tile set for the
136 enum PendingCellChangeType change_type;
139 struct PendingRowChange
142 * These are the first and last columns, inclusive, that have been
143 * modified. xmin is greater than xmax if no changes have been made.
147 * This points to storage for a number of elements equal to the number
148 * of columns (implicitly gotten from the enclosing AngbandContext).
150 struct PendingCellChange* cell_changes;
153 static struct PendingRowChange* create_row_change(int ncol)
155 struct PendingRowChange* prc =
156 (struct PendingRowChange*) malloc(sizeof(struct PendingRowChange));
157 struct PendingCellChange* pcc = (struct PendingCellChange*)
158 malloc(ncol * sizeof(struct PendingCellChange));
161 if (prc == 0 || pcc == 0) {
173 prc->cell_changes = pcc;
174 for (i = 0; i < ncol; ++i) {
175 pcc[i].change_type = CELL_CHANGE_NONE;
181 static void destroy_row_change(struct PendingRowChange* prc)
184 if (prc->cell_changes != 0) {
185 free(prc->cell_changes);
192 struct PendingChanges
194 /* Hold the number of rows specified at creation. */
197 * Hold the position set for the software cursor. Use negative indices
198 * to indicate that the cursor is not displayed.
201 /* Is nonzero if the cursor should be drawn at double the tile width. */
203 /* Record whether the changes include any text, picts, or wipes. */
204 int has_text, has_pict, has_wipe;
206 * These are the first and last rows, inclusive, that have been
207 * modified. ymin is greater than ymax if no changes have been made.
211 * This is an array of pointers to the changes. The number of elements
212 * is the number of rows. An element will be a NULL pointer if no
213 * modifications have been made to the row.
215 struct PendingRowChange** rows;
219 static struct PendingChanges* create_pending_changes(int ncol, int nrow)
221 struct PendingChanges* pc =
222 (struct PendingChanges*) malloc(sizeof(struct PendingChanges));
223 struct PendingRowChange** pprc = (struct PendingRowChange**)
224 malloc(nrow * sizeof(struct PendingRowChange*));
227 if (pc == 0 || pprc == 0) {
247 for (i = 0; i < nrow; ++i) {
254 static void destroy_pending_changes(struct PendingChanges* pc)
260 for (i = 0; i < pc->nrow; ++i) {
261 if (pc->rows[i] != 0) {
262 destroy_row_change(pc->rows[i]);
272 static void clear_pending_changes(struct PendingChanges* pc)
285 for (i = 0; i < pc->nrow; ++i) {
286 if (pc->rows[i] != 0) {
287 destroy_row_change(pc->rows[i]);
295 /* Return zero if successful; otherwise return a nonzero value. */
296 static int resize_pending_changes(struct PendingChanges* pc, int nrow)
298 struct PendingRowChange** pprc;
305 pprc = (struct PendingRowChange**)
306 malloc(nrow * sizeof(struct PendingRowChange*));
310 for (i = 0; i < nrow; ++i) {
315 for (i = 0; i < pc->nrow; ++i) {
316 if (pc->rows[i] != 0) {
317 destroy_row_change(pc->rows[i]);
336 /* The max number of glyphs we support. Currently this only affects
337 * updateGlyphInfo() for the calculation of the tile size (the glyphArray and
338 * glyphWidths members of AngbandContext are only used in updateGlyphInfo()).
339 * The rendering in drawWChar will work for glyphs not in updateGlyphInfo()'s
340 * set, and that is used for rendering Japanese characters.
342 #define GLYPH_COUNT 256
344 /* An AngbandContext represents a logical Term (i.e. what Angband thinks is
345 * a window). This typically maps to one NSView, but may map to more than one
346 * NSView (e.g. the Test and real screen saver view). */
347 @interface AngbandContext : NSObject <NSWindowDelegate>
351 /* The Angband term */
354 /* Column and row cont, by default 80 x 24 */
358 /* The size of the border between the window edge and the contents */
361 /* Our array of views */
362 NSMutableArray *angbandViews;
364 /* The buffered image */
365 CGLayerRef angbandLayer;
367 /* The font of this context */
368 NSFont *angbandViewFont;
370 /* If this context owns a window, here it is */
371 NSWindow *primaryWindow;
373 /* "Glyph info": an array of the CGGlyphs and their widths corresponding to
375 CGGlyph glyphArray[GLYPH_COUNT];
376 CGFloat glyphWidths[GLYPH_COUNT];
378 /* The size of one tile */
381 /* Font's ascender and descender */
382 CGFloat fontAscender, fontDescender;
384 /* Whether we are currently in live resize, which affects how big we render
388 /* Last time we drew, so we can throttle drawing */
389 CFAbsoluteTime lastRefreshTime;
391 struct PendingChanges* changes;
393 * These are the number of columns before or after, respectively, a text
394 * change that may need to be redrawn.
396 int ncol_pre, ncol_post;
400 BOOL _hasSubwindowFlags;
401 BOOL _windowVisibilityChecked;
404 @property (nonatomic, assign) BOOL hasSubwindowFlags;
405 @property (nonatomic, assign) BOOL windowVisibilityChecked;
407 - (void)drawRect:(NSRect)rect inView:(NSView *)view;
409 /* Called at initialization to set the term */
410 - (void)setTerm:(term *)t;
412 /* Called when the context is going down. */
415 /* Returns the size of the image. */
418 /* Return the rect for a tile at given coordinates. */
419 - (NSRect)rectInImageForTileAtX:(int)x Y:(int)y;
421 /* Draw the given wide character into the given tile rect. */
422 - (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile context:(CGContextRef)ctx;
424 /* Locks focus on the Angband image, and scales the CTM appropriately. */
425 - (CGContextRef)lockFocus;
427 /* Locks focus on the Angband image but does NOT scale the CTM. Appropriate
428 * for drawing hairlines. */
429 - (CGContextRef)lockFocusUnscaled;
434 /* Returns the primary window for this angband context, creating it if
436 - (NSWindow *)makePrimaryWindow;
438 /* Called to add a new Angband view */
439 - (void)addAngbandView:(AngbandView *)view;
441 /* Make the context aware that one of its views changed size */
442 - (void)angbandViewDidScale:(AngbandView *)view;
444 /* Handle becoming the main window */
445 - (void)windowDidBecomeMain:(NSNotification *)notification;
447 /* Return whether the context's primary window is ordered in or not */
450 /* Return whether the context's primary window is key */
451 - (BOOL)isMainWindow;
453 /* Invalidate the whole image */
454 - (void)setNeedsDisplay:(BOOL)val;
456 /* Invalidate part of the image, with the rect expressed in base coordinates */
457 - (void)setNeedsDisplayInBaseRect:(NSRect)rect;
459 /* Display (flush) our Angband views */
460 - (void)displayIfNeeded;
462 /* Resize context to size of contentRect, and optionally save size to
464 - (void)resizeTerminalWithContentRect: (NSRect)contentRect saveToDefaults: (BOOL)saveToDefaults;
466 /* Called from the view to indicate that it is starting or ending live resize */
467 - (void)viewWillStartLiveResize:(AngbandView *)view;
468 - (void)viewDidEndLiveResize:(AngbandView *)view;
469 - (void)saveWindowVisibleToDefaults: (BOOL)windowVisible;
470 - (BOOL)windowVisibleUsingDefaults;
474 /* Begins an Angband game. This is the entry point for starting off. */
477 /* Ends an Angband game. */
480 /* Internal method */
481 - (AngbandView *)activeView;
486 * Generate a mask for the subwindow flags. The mask is just a safety check to
487 * make sure that our windows show and hide as expected. This function allows
488 * for future changes to the set of flags without needed to update it here
489 * (unless the underlying types change).
491 u32b AngbandMaskForValidSubwindowFlags(void)
493 int windowFlagBits = sizeof(*(window_flag)) * CHAR_BIT;
494 int maxBits = MIN( 16, windowFlagBits );
497 for( int i = 0; i < maxBits; i++ )
499 if( window_flag_desc[i] != NULL )
509 * Check for changes in the subwindow flags and update window visibility.
510 * This seems to be called for every user event, so we don't
511 * want to do any unnecessary hiding or showing of windows.
513 static void AngbandUpdateWindowVisibility(void)
515 /* Because this function is called frequently, we'll make the mask static.
516 * It doesn't change between calls, as the flags themselves are hardcoded */
517 static u32b validWindowFlagsMask = 0;
519 if( validWindowFlagsMask == 0 )
521 validWindowFlagsMask = AngbandMaskForValidSubwindowFlags();
524 /* Loop through all of the subwindows and see if there is a change in the
525 * flags. If so, show or hide the corresponding window. We don't care about
526 * the flags themselves; we just want to know if any are set. */
527 for( int i = 1; i < ANGBAND_TERM_MAX; i++ )
529 AngbandContext *angbandContext = angband_term[i]->data;
531 if( angbandContext == nil )
536 /* This horrible mess of flags is so that we can try to maintain some
537 * user visibility preference. This should allow the user a window and
538 * have it stay closed between application launches. However, this
539 * means that when a subwindow is turned on, it will no longer appear
540 * automatically. Angband has no concept of user control over window
541 * visibility, other than the subwindow flags. */
542 if( !angbandContext.windowVisibilityChecked )
544 if( [angbandContext windowVisibleUsingDefaults] )
546 [angbandContext->primaryWindow orderFront: nil];
547 angbandContext.windowVisibilityChecked = YES;
551 [angbandContext->primaryWindow close];
552 angbandContext.windowVisibilityChecked = NO;
557 BOOL termHasSubwindowFlags = ((window_flag[i] & validWindowFlagsMask) > 0);
559 if( angbandContext.hasSubwindowFlags && !termHasSubwindowFlags )
561 [angbandContext->primaryWindow close];
562 angbandContext.hasSubwindowFlags = NO;
563 [angbandContext saveWindowVisibleToDefaults: NO];
565 else if( !angbandContext.hasSubwindowFlags && termHasSubwindowFlags )
567 [angbandContext->primaryWindow orderFront: nil];
568 angbandContext.hasSubwindowFlags = YES;
569 [angbandContext saveWindowVisibleToDefaults: YES];
574 /* Make the main window key so that user events go to the right spot */
575 AngbandContext *mainWindow = angband_term[0]->data;
576 [mainWindow->primaryWindow makeKeyAndOrderFront: nil];
580 * ------------------------------------------------------------------------
582 * ------------------------------------------------------------------------ */
587 static CGImageRef pict_image;
590 * Numbers of rows and columns in a tileset,
591 * calculated by the PICT/PNG loading code
593 static int pict_cols = 0;
594 static int pict_rows = 0;
597 * Requested graphics mode (as a grafID).
598 * The current mode is stored in current_graphics_mode.
600 static int graf_mode_req = 0;
603 * Helper function to check the various ways that graphics can be enabled,
604 * guarding against NULL
606 static BOOL graphics_are_enabled(void)
608 return current_graphics_mode
609 && current_graphics_mode->grafID != GRAPHICS_NONE;
613 * Hack -- game in progress
615 static Boolean game_in_progress = FALSE;
618 #pragma mark Prototypes
619 static void wakeup_event_loop(void);
620 static void hook_plog(const char *str);
621 static void hook_quit(const char * str);
622 static void load_prefs(void);
623 static void load_sounds(void);
624 static void init_windows(void);
625 static void handle_open_when_ready(void);
626 static void play_sound(int event);
627 static BOOL check_events(int wait);
628 static BOOL send_event(NSEvent *event);
629 static void record_current_savefile(void);
631 static wchar_t convert_two_byte_eucjp_to_utf16_native(const char *cp);
635 * Available values for 'wait'
637 #define CHECK_EVENTS_DRAIN -1
638 #define CHECK_EVENTS_NO_WAIT 0
639 #define CHECK_EVENTS_WAIT 1
643 * Note when "open"/"new" become valid
645 static bool initialized = FALSE;
647 /* Methods for getting the appropriate NSUserDefaults */
648 @interface NSUserDefaults (AngbandDefaults)
649 + (NSUserDefaults *)angbandDefaults;
652 @implementation NSUserDefaults (AngbandDefaults)
653 + (NSUserDefaults *)angbandDefaults
655 return [NSUserDefaults standardUserDefaults];
659 /* Methods for pulling images out of the Angband bundle (which may be separate
660 * from the current bundle in the case of a screensaver */
661 @interface NSImage (AngbandImages)
662 + (NSImage *)angbandImage:(NSString *)name;
665 /* The NSView subclass that draws our Angband image */
666 @interface AngbandView : NSView
668 IBOutlet AngbandContext *angbandContext;
671 - (void)setAngbandContext:(AngbandContext *)context;
672 - (AngbandContext *)angbandContext;
676 @implementation NSImage (AngbandImages)
678 /* Returns an image in the resource directoy of the bundle containing the
679 * Angband view class. */
680 + (NSImage *)angbandImage:(NSString *)name
682 NSBundle *bundle = [NSBundle bundleForClass:[AngbandView class]];
683 NSString *path = [bundle pathForImageResource:name];
685 if (path) result = [[[NSImage alloc] initByReferencingFile:path] autorelease];
693 @implementation AngbandContext
695 @synthesize hasSubwindowFlags=_hasSubwindowFlags;
696 @synthesize windowVisibilityChecked=_windowVisibilityChecked;
698 - (NSFont *)selectionFont
700 return angbandViewFont;
703 - (BOOL)useLiveResizeOptimization
705 /* If we have graphics turned off, text rendering is fast enough that we
706 * don't need to use a live resize optimization. */
707 return inLiveResize && graphics_are_enabled();
712 /* We round the base size down. If we round it up, I believe we may end up
713 * with pixels that nobody "owns" that may accumulate garbage. In general
714 * rounding down is harmless, because any lost pixels may be sopped up by
716 return NSMakeSize(floor(cols * tileSize.width + 2 * borderSize.width), floor(rows * tileSize.height + 2 * borderSize.height));
719 /* qsort-compatible compare function for CGSizes */
720 static int compare_advances(const void *ap, const void *bp)
722 const CGSize *a = ap, *b = bp;
723 return (a->width > b->width) - (a->width < b->width);
726 - (void)updateGlyphInfo
728 /* Update glyphArray and glyphWidths */
729 NSFont *screenFont = [angbandViewFont screenFont];
731 /* Generate a string containing each MacRoman character */
732 unsigned char latinString[GLYPH_COUNT];
734 for (i=0; i < GLYPH_COUNT; i++) latinString[i] = (unsigned char)i;
736 /* Turn that into unichar. Angband uses ISO Latin 1. */
737 unichar unicharString[GLYPH_COUNT] = {0};
738 NSString *allCharsString = [[NSString alloc] initWithBytes:latinString length:sizeof latinString encoding:NSISOLatin1StringEncoding];
739 [allCharsString getCharacters:unicharString range:NSMakeRange(0, MIN(GLYPH_COUNT, [allCharsString length]))];
740 [allCharsString autorelease];
743 memset(glyphArray, 0, sizeof glyphArray);
744 CTFontGetGlyphsForCharacters((CTFontRef)screenFont, unicharString, glyphArray, GLYPH_COUNT);
746 /* Get advances. Record the max advance. */
747 CGSize advances[GLYPH_COUNT] = {};
748 CTFontGetAdvancesForGlyphs((CTFontRef)screenFont, kCTFontHorizontalOrientation, glyphArray, advances, GLYPH_COUNT);
749 for (i=0; i < GLYPH_COUNT; i++) {
750 glyphWidths[i] = advances[i].width;
753 /* For good non-mono-font support, use the median advance. Start by sorting
755 qsort(advances, GLYPH_COUNT, sizeof *advances, compare_advances);
757 /* Skip over any initially empty run */
759 for (startIdx = 0; startIdx < GLYPH_COUNT; startIdx++)
761 if (advances[startIdx].width > 0) break;
764 /* Pick the center to find the median */
765 CGFloat medianAdvance = 0;
766 if (startIdx < GLYPH_COUNT)
768 /* In case we have all zero advances for some reason */
769 medianAdvance = advances[(startIdx + GLYPH_COUNT)/2].width;
773 * Record the ascender and descender. Adjust both by 0.5 pixel to leave
774 * space for antialiasing/subpixel positioning.
776 fontAscender = [screenFont ascender] + 0.5;
777 fontDescender = [screenFont descender] - 0.5;
780 * Record the tile size. Add one to the median advance to leave space
781 * for antialiasing/subpixel positioning. Round both values up to
782 * have tile boundaries match pixel boundaries.
784 tileSize.width = ceil(medianAdvance + 1.);
785 tileSize.height = ceil(fontAscender - fontDescender);
788 * Determine whether neighboring columns need to redrawn when a character
791 CGRect boxes[GLYPH_COUNT] = {};
792 CGFloat beyond_right = 0.;
793 CGFloat beyond_left = 0.;
794 CTFontGetBoundingRectsForGlyphs(
795 (CTFontRef)screenFont,
796 kCTFontHorizontalOrientation,
800 for (i = 0; i < GLYPH_COUNT; i++) {
801 /* Account for the compression and offset used by drawWChar(). */
802 CGFloat compression, offset;
805 if (glyphWidths[i] <= tileSize.width) {
807 offset = 0.5 * (tileSize.width - glyphWidths[i]);
809 compression = tileSize.width / glyphWidths[i];
812 v = (offset + boxes[i].origin.x) * compression;
813 if (beyond_left > v) {
816 v = (offset + boxes[i].origin.x + boxes[i].size.width) * compression;
817 if (beyond_right < v) {
821 ncol_pre = ceil(-beyond_left / tileSize.width);
822 if (beyond_right > tileSize.width) {
823 ncol_post = ceil((beyond_right - tileSize.width) / tileSize.width);
831 NSSize size = NSMakeSize(1, 1);
833 AngbandView *activeView = [self activeView];
836 /* If we are in live resize, draw as big as the screen, so we can scale
837 * nicely to any size. If we are not in live resize, then use the
838 * bounds of the active view. */
840 if ([self useLiveResizeOptimization] && (screen = [[activeView window] screen]) != NULL)
842 size = [screen frame].size;
846 size = [activeView bounds].size;
850 CGLayerRelease(angbandLayer);
852 /* Use the highest monitor scale factor on the system to work out what
853 * scale to draw at - not the recommended method, but works where we
854 * can't easily get the monitor the current draw is occurring on. */
855 float angbandLayerScale = 1.0;
856 if ([[NSScreen mainScreen] respondsToSelector:@selector(backingScaleFactor)]) {
857 for (NSScreen *screen in [NSScreen screens]) {
858 angbandLayerScale = fmax(angbandLayerScale, [screen backingScaleFactor]);
862 /* Make a bitmap context as an example for our layer */
863 CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
864 CGContextRef exampleCtx = CGBitmapContextCreate(NULL, 1, 1, 8 /* bits per component */, 48 /* bytesPerRow */, cs, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host);
865 CGColorSpaceRelease(cs);
867 /* Create the layer at the appropriate size */
868 size.width = fmax(1, ceil(size.width * angbandLayerScale));
869 size.height = fmax(1, ceil(size.height * angbandLayerScale));
870 angbandLayer = CGLayerCreateWithContext(exampleCtx, *(CGSize *)&size, NULL);
872 CFRelease(exampleCtx);
874 /* Set the new context of the layer to draw at the correct scale */
875 CGContextRef ctx = CGLayerGetContext(angbandLayer);
876 CGContextScaleCTM(ctx, angbandLayerScale, angbandLayerScale);
879 [[NSColor blackColor] set];
880 NSRectFill((NSRect){NSZeroPoint, [self baseSize]});
884 - (void)requestRedraw
886 if (! self->terminal) return;
890 /* Activate the term */
891 Term_activate(self->terminal);
893 /* Redraw the contents */
896 /* Flush the output */
899 /* Restore the old term */
903 - (void)setTerm:(term *)t
908 - (void)viewWillStartLiveResize:(AngbandView *)view
910 #if USE_LIVE_RESIZE_CACHE
911 if (inLiveResize < INT_MAX) inLiveResize++;
912 else [NSException raise:NSInternalInconsistencyException format:@"inLiveResize overflow"];
914 if (inLiveResize == 1 && graphics_are_enabled())
918 [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
919 [self requestRedraw];
924 - (void)viewDidEndLiveResize:(AngbandView *)view
926 #if USE_LIVE_RESIZE_CACHE
927 if (inLiveResize > 0) inLiveResize--;
928 else [NSException raise:NSInternalInconsistencyException format:@"inLiveResize underflow"];
930 if (inLiveResize == 0 && graphics_are_enabled())
934 [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
935 [self requestRedraw];
941 * If we're trying to limit ourselves to a certain number of frames per second,
942 * then compute how long it's been since we last drew, and then wait until the
943 * next frame has passed. */
946 if (frames_per_second > 0)
948 CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
949 CFTimeInterval timeSinceLastRefresh = now - lastRefreshTime;
950 CFTimeInterval timeUntilNextRefresh = (1. / (double)frames_per_second) - timeSinceLastRefresh;
952 if (timeUntilNextRefresh > 0)
954 usleep((unsigned long)(timeUntilNextRefresh * 1000000.));
957 lastRefreshTime = CFAbsoluteTimeGetCurrent();
960 - (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile context:(CGContextRef)ctx
962 CGFloat tileOffsetY = fontAscender;
963 CGFloat tileOffsetX = 0.0;
964 NSFont *screenFont = [angbandViewFont screenFont];
965 UniChar unicharString[2] = {(UniChar)wchar, 0};
967 /* Get glyph and advance */
968 CGGlyph thisGlyphArray[1] = { 0 };
969 CGSize advances[1] = { { 0, 0 } };
970 CTFontGetGlyphsForCharacters((CTFontRef)screenFont, unicharString, thisGlyphArray, 1);
971 CGGlyph glyph = thisGlyphArray[0];
972 CTFontGetAdvancesForGlyphs((CTFontRef)screenFont, kCTFontHorizontalOrientation, thisGlyphArray, advances, 1);
973 CGSize advance = advances[0];
975 /* If our font is not monospaced, our tile width is deliberately not big
976 * enough for every character. In that event, if our glyph is too wide, we
977 * need to compress it horizontally. Compute the compression ratio.
978 * 1.0 means no compression. */
979 double compressionRatio;
980 if (advance.width <= NSWidth(tile))
982 /* Our glyph fits, so we can just draw it, possibly with an offset */
983 compressionRatio = 1.0;
984 tileOffsetX = (NSWidth(tile) - advance.width)/2;
988 /* Our glyph doesn't fit, so we'll have to compress it */
989 compressionRatio = NSWidth(tile) / advance.width;
995 CGAffineTransform textMatrix = CGContextGetTextMatrix(ctx);
996 CGFloat savedA = textMatrix.a;
998 /* Set the position */
999 textMatrix.tx = tile.origin.x + tileOffsetX;
1000 textMatrix.ty = tile.origin.y + tileOffsetY;
1002 /* Maybe squish it horizontally. */
1003 if (compressionRatio != 1.)
1005 textMatrix.a *= compressionRatio;
1008 textMatrix = CGAffineTransformScale( textMatrix, 1.0, -1.0 );
1009 CGContextSetTextMatrix(ctx, textMatrix);
1010 CGContextShowGlyphsWithAdvances(ctx, &glyph, &CGSizeZero, 1);
1012 /* Restore the text matrix if we messed with the compression ratio */
1013 if (compressionRatio != 1.)
1015 textMatrix.a = savedA;
1016 CGContextSetTextMatrix(ctx, textMatrix);
1019 textMatrix = CGAffineTransformScale( textMatrix, 1.0, -1.0 );
1020 CGContextSetTextMatrix(ctx, textMatrix);
1023 /* Lock and unlock focus on our image or layer, setting up the CTM
1025 - (CGContextRef)lockFocusUnscaled
1027 /* Create an NSGraphicsContext representing this CGLayer */
1028 CGContextRef ctx = CGLayerGetContext(angbandLayer);
1029 NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:ctx flipped:NO];
1030 [NSGraphicsContext saveGraphicsState];
1031 [NSGraphicsContext setCurrentContext:context];
1032 CGContextSaveGState(ctx);
1038 /* Restore the graphics state */
1039 CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];
1040 CGContextRestoreGState(ctx);
1041 [NSGraphicsContext restoreGraphicsState];
1046 /* Return the size of our layer */
1047 CGSize result = CGLayerGetSize(angbandLayer);
1048 return NSMakeSize(result.width, result.height);
1051 - (CGContextRef)lockFocus
1053 return [self lockFocusUnscaled];
1057 - (NSRect)rectInImageForTileAtX:(int)x Y:(int)y
1060 return NSMakeRect(x * tileSize.width + borderSize.width, flippedY * tileSize.height + borderSize.height, tileSize.width, tileSize.height);
1063 - (void)setSelectionFont:(NSFont*)font adjustTerminal: (BOOL)adjustTerminal
1065 /* Record the new font */
1067 [angbandViewFont release];
1068 angbandViewFont = font;
1070 /* Update our glyph info */
1071 [self updateGlyphInfo];
1073 if( adjustTerminal )
1075 /* Adjust terminal to fit window with new font; save the new columns
1076 * and rows since they could be changed */
1077 NSRect contentRect = [self->primaryWindow contentRectForFrameRect: [self->primaryWindow frame]];
1078 [self resizeTerminalWithContentRect: contentRect saveToDefaults: YES];
1081 /* Update our image */
1085 [self requestRedraw];
1090 if ((self = [super init]))
1092 /* Default rows and cols */
1096 /* Default border size */
1097 self->borderSize = NSMakeSize(2, 2);
1099 /* Allocate our array of views */
1100 angbandViews = [[NSMutableArray alloc] init];
1102 self->changes = create_pending_changes(self->cols, self->rows);
1103 if (self->changes == 0) {
1104 NSLog(@"AngbandContext init: out of memory for pending changes");
1107 self->ncol_post = 0;
1109 /* Make the image. Since we have no views, it'll just be a puny 1x1 image. */
1112 _windowVisibilityChecked = NO;
1118 * Destroy all the receiver's stuff. This is intended to be callable more than
1125 /* Disassociate ourselves from our angbandViews */
1126 [angbandViews makeObjectsPerformSelector:@selector(setAngbandContext:) withObject:nil];
1127 [angbandViews release];
1130 /* Destroy the layer/image */
1131 CGLayerRelease(angbandLayer);
1132 angbandLayer = NULL;
1135 [angbandViewFont release];
1136 angbandViewFont = nil;
1139 [primaryWindow setDelegate:nil];
1140 [primaryWindow close];
1141 [primaryWindow release];
1142 primaryWindow = nil;
1144 /* Pending changes */
1145 destroy_pending_changes(self->changes);
1149 /* Usual Cocoa fare */
1159 #pragma mark Directories and Paths Setup
1162 * Return the path for Angband's lib directory and bail if it isn't found. The
1163 * lib directory should be in the bundle's resources directory, since it's
1164 * copied when built.
1166 + (NSString *)libDirectoryPath
1168 NSString *bundleLibPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: AngbandDirectoryNameLib];
1169 BOOL isDirectory = NO;
1170 BOOL libExists = [[NSFileManager defaultManager] fileExistsAtPath: bundleLibPath isDirectory: &isDirectory];
1172 if( !libExists || !isDirectory )
1174 NSLog( @"[%@ %@]: can't find %@/ in bundle: isDirectory: %d libExists: %d", NSStringFromClass( [self class] ), NSStringFromSelector( _cmd ), AngbandDirectoryNameLib, isDirectory, libExists );
1176 NSString *msg = NSLocalizedStringWithDefaultValue(
1177 @"Error.MissingResources",
1178 AngbandMessageCatalog,
1179 [NSBundle mainBundle],
1180 @"Missing Resources",
1181 @"Alert text for missing resources");
1182 NSString *info = NSLocalizedStringWithDefaultValue(
1183 @"Error.MissingAngbandLib",
1184 AngbandMessageCatalog,
1185 [NSBundle mainBundle],
1186 @"Hengband was unable to find required resources and must quit. Please report a bug on the Angband forums.",
1187 @"Alert informative message for missing Angband lib/ folder");
1188 NSString *quit_label = NSLocalizedStringWithDefaultValue(
1189 @"Label.Quit", AngbandMessageCatalog, [NSBundle mainBundle],
1191 NSAlert *alert = [[NSAlert alloc] init];
1194 * Note that NSCriticalAlertStyle was deprecated in 10.10. The
1195 * replacement is NSAlertStyleCritical.
1197 alert.alertStyle = NSCriticalAlertStyle;
1198 alert.messageText = msg;
1199 alert.informativeText = info;
1200 [alert addButtonWithTitle:quit_label];
1201 NSModalResponse result = [alert runModal];
1206 return bundleLibPath;
1210 * Return the path for the directory where Angband should look for its standard
1213 + (NSString *)angbandDocumentsPath
1215 NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
1217 #if defined(SAFE_DIRECTORY)
1218 NSString *versionedDirectory = [NSString stringWithFormat: @"%@-%s", AngbandDirectoryNameBase, VERSION_STRING];
1219 return [documents stringByAppendingPathComponent: versionedDirectory];
1221 return [documents stringByAppendingPathComponent: AngbandDirectoryNameBase];
1226 * Adjust directory paths as needed to correct for any differences needed by
1227 * Angband. \c init_file_paths() currently requires that all paths provided have
1228 * a trailing slash and all other platforms honor this.
1230 * \param originalPath The directory path to adjust.
1231 * \return A path suitable for Angband or nil if an error occurred.
1233 static NSString *AngbandCorrectedDirectoryPath(NSString *originalPath)
1235 if ([originalPath length] == 0) {
1239 if (![originalPath hasSuffix: @"/"]) {
1240 return [originalPath stringByAppendingString: @"/"];
1243 return originalPath;
1247 * Give Angband the base paths that should be used for the various directories
1248 * it needs. It will create any needed directories.
1250 + (void)prepareFilePathsAndDirectories
1252 char libpath[PATH_MAX + 1] = "\0";
1253 NSString *libDirectoryPath = AngbandCorrectedDirectoryPath([self libDirectoryPath]);
1254 [libDirectoryPath getFileSystemRepresentation: libpath maxLength: sizeof(libpath)];
1256 char basepath[PATH_MAX + 1] = "\0";
1257 NSString *angbandDocumentsPath = AngbandCorrectedDirectoryPath([self angbandDocumentsPath]);
1258 [angbandDocumentsPath getFileSystemRepresentation: basepath maxLength: sizeof(basepath)];
1260 init_file_paths(libpath, libpath, basepath);
1261 create_needed_dirs();
1267 /* From the Linux mbstowcs(3) man page:
1268 * If dest is NULL, n is ignored, and the conversion proceeds as above,
1269 * except that the converted wide characters are not written out to mem‐
1270 * ory, and that no length limit exists.
1272 static size_t Term_mbcs_cocoa(wchar_t *dest, const char *src, int n)
1277 /* Unicode code point to UTF-8
1278 * 0x0000-0x007f: 0xxxxxxx
1279 * 0x0080-0x07ff: 110xxxxx 10xxxxxx
1280 * 0x0800-0xffff: 1110xxxx 10xxxxxx 10xxxxxx
1281 * 0x10000-0x1fffff: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
1282 * Note that UTF-16 limits Unicode to 0x10ffff. This code is not
1285 for (i = 0; i < n || dest == NULL; i++) {
1286 if ((src[i] & 0x80) == 0) {
1287 if (dest != NULL) dest[count] = src[i];
1288 if (src[i] == 0) break;
1289 } else if ((src[i] & 0xe0) == 0xc0) {
1290 if (dest != NULL) dest[count] =
1291 (((unsigned char)src[i] & 0x1f) << 6)|
1292 ((unsigned char)src[i+1] & 0x3f);
1294 } else if ((src[i] & 0xf0) == 0xe0) {
1295 if (dest != NULL) dest[count] =
1296 (((unsigned char)src[i] & 0x0f) << 12) |
1297 (((unsigned char)src[i+1] & 0x3f) << 6) |
1298 ((unsigned char)src[i+2] & 0x3f);
1300 } else if ((src[i] & 0xf8) == 0xf0) {
1301 if (dest != NULL) dest[count] =
1302 (((unsigned char)src[i] & 0x0f) << 18) |
1303 (((unsigned char)src[i+1] & 0x3f) << 12) |
1304 (((unsigned char)src[i+2] & 0x3f) << 6) |
1305 ((unsigned char)src[i+3] & 0x3f);
1308 /* Found an invalid multibyte sequence */
1318 * Entry point for initializing Angband
1322 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1324 /* Hooks in some "z-util.c" hooks */
1325 plog_aux = hook_plog;
1326 quit_aux = hook_quit;
1328 /* Initialize file paths */
1329 [self prepareFilePathsAndDirectories];
1331 /* Load preferences */
1334 /* Prepare the windows */
1337 /* Set up game event handlers */
1338 /* init_display(); */
1340 /* Register the sound hook */
1341 /* sound_hook = play_sound; */
1343 /* Initialise game */
1346 /* This is not incorporated into Hengband's init_angband() yet. */
1347 init_graphics_modes();
1349 /* Note the "system" */
1350 ANGBAND_SYS = "mac";
1352 /* Initialize some save file stuff */
1353 player_egid = getegid();
1355 /* We are now initialized */
1358 /* Handle "open_when_ready" */
1359 handle_open_when_ready();
1361 /* Handle pending events (most notably update) and flush input */
1364 /* Prompt the user. */
1365 int message_row = (Term->hgt - 23) / 5 + 23;
1366 Term_erase(0, message_row, 255);
1369 "['ファイル' メニューから '新' または '開く' を選択します]",
1370 message_row, (Term->wid - 57) / 2
1372 "[Choose 'New' or 'Open' from the 'File' menu]",
1373 message_row, (Term->wid - 45) / 2
1379 * Play a game -- "new_game" is set by "new", "open" or the open document
1380 * even handler as appropriate
1385 while (!game_in_progress) {
1386 NSAutoreleasePool *splashScreenPool = [[NSAutoreleasePool alloc] init];
1387 NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantFuture] inMode:NSDefaultRunLoopMode dequeue:YES];
1388 if (event) [NSApp sendEvent:event];
1389 [splashScreenPool drain];
1393 play_game(new_game);
1400 /* Hack -- Forget messages */
1403 p_ptr->playing = FALSE;
1404 p_ptr->leaving = TRUE;
1405 quit_when_ready = TRUE;
1408 - (void)addAngbandView:(AngbandView *)view
1410 if (! [angbandViews containsObject:view])
1412 [angbandViews addObject:view];
1414 [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
1415 [self requestRedraw];
1420 * We have this notion of an "active" AngbandView, which is the largest - the
1421 * idea being that in the screen saver, when the user hits Test in System
1422 * Preferences, we don't want to keep driving the AngbandView in the
1423 * background. Our active AngbandView is the widest - that's a hack all right.
1424 * Mercifully when we're just playing the game there's only one view.
1426 - (AngbandView *)activeView
1428 if ([angbandViews count] == 1)
1429 return [angbandViews objectAtIndex:0];
1431 AngbandView *result = nil;
1433 for (AngbandView *angbandView in angbandViews)
1435 float width = [angbandView frame].size.width;
1436 if (width > maxWidth)
1439 result = angbandView;
1445 - (void)angbandViewDidScale:(AngbandView *)view
1447 /* If we're live-resizing with graphics, we're using the live resize
1448 * optimization, so don't update the image. Otherwise do it. */
1449 if (! (inLiveResize && graphics_are_enabled()) && view == [self activeView])
1453 [self setNeedsDisplay:YES]; /*we'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
1454 [self requestRedraw];
1459 - (void)removeAngbandView:(AngbandView *)view
1461 if ([angbandViews containsObject:view])
1463 [angbandViews removeObject:view];
1465 [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
1466 if ([angbandViews count]) [self requestRedraw];
1471 static NSMenuItem *superitem(NSMenuItem *self)
1473 NSMenu *supermenu = [[self menu] supermenu];
1474 int index = [supermenu indexOfItemWithSubmenu:[self menu]];
1475 if (index == -1) return nil;
1476 else return [supermenu itemAtIndex:index];
1480 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
1482 int tag = [menuItem tag];
1483 SEL sel = [menuItem action];
1484 if (sel == @selector(setGraphicsMode:))
1486 [menuItem setState: (tag == graf_mode_req)];
1495 - (NSWindow *)makePrimaryWindow
1497 if (! primaryWindow)
1499 /* This has to be done after the font is set, which it already is in
1500 * term_init_cocoa() */
1501 CGFloat width = self->cols * tileSize.width + borderSize.width * 2.0;
1502 CGFloat height = self->rows * tileSize.height + borderSize.height * 2.0;
1503 NSRect contentRect = NSMakeRect( 0.0, 0.0, width, height );
1505 NSUInteger styleMask = NSTitledWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask;
1507 /* Make every window other than the main window closable */
1508 if( angband_term[0]->data != self )
1510 styleMask |= NSClosableWindowMask;
1513 primaryWindow = [[NSWindow alloc] initWithContentRect:contentRect styleMask: styleMask backing:NSBackingStoreBuffered defer:YES];
1515 /* Not to be released when closed */
1516 [primaryWindow setReleasedWhenClosed:NO];
1517 [primaryWindow setExcludedFromWindowsMenu: YES]; /* we're using custom window menu handling */
1520 AngbandView *angbandView = [[AngbandView alloc] initWithFrame:contentRect];
1521 [angbandView setAngbandContext:self];
1522 [angbandViews addObject:angbandView];
1523 [primaryWindow setContentView:angbandView];
1524 [angbandView release];
1526 /* We are its delegate */
1527 [primaryWindow setDelegate:self];
1529 /* Update our image, since this is probably the first angband view
1533 return primaryWindow;
1538 #pragma mark View/Window Passthrough
1541 * This is what our views call to get us to draw to the window
1543 - (void)drawRect:(NSRect)rect inView:(NSView *)view
1545 /* Take this opportunity to throttle so we don't flush faster than desired.
1547 BOOL viewInLiveResize = [view inLiveResize];
1548 if (! viewInLiveResize) [self throttle];
1550 /* With a GLayer, use CGContextDrawLayerInRect */
1551 CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
1552 NSRect bounds = [view bounds];
1553 if (viewInLiveResize) CGContextSetInterpolationQuality(context, kCGInterpolationLow);
1554 CGContextSetBlendMode(context, kCGBlendModeCopy);
1555 CGContextDrawLayerInRect(context, *(CGRect *)&bounds, angbandLayer);
1556 if (viewInLiveResize) CGContextSetInterpolationQuality(context, kCGInterpolationDefault);
1561 return [[[angbandViews lastObject] window] isVisible];
1564 - (BOOL)isMainWindow
1566 return [[[angbandViews lastObject] window] isMainWindow];
1569 - (void)setNeedsDisplay:(BOOL)val
1571 for (NSView *angbandView in angbandViews)
1573 [angbandView setNeedsDisplay:val];
1577 - (void)setNeedsDisplayInBaseRect:(NSRect)rect
1579 for (NSView *angbandView in angbandViews)
1581 [angbandView setNeedsDisplayInRect: rect];
1585 - (void)displayIfNeeded
1587 [[self activeView] displayIfNeeded];
1590 - (int)terminalIndex
1594 for( termIndex = 0; termIndex < ANGBAND_TERM_MAX; termIndex++ )
1596 if( angband_term[termIndex] == self->terminal )
1605 - (void)resizeTerminalWithContentRect: (NSRect)contentRect saveToDefaults: (BOOL)saveToDefaults
1607 CGFloat newRows = floor( (contentRect.size.height - (borderSize.height * 2.0)) / tileSize.height );
1608 CGFloat newColumns = ceil( (contentRect.size.width - (borderSize.width * 2.0)) / tileSize.width );
1610 if (newRows < 1 || newColumns < 1) return;
1611 self->cols = newColumns;
1612 self->rows = newRows;
1614 if (resize_pending_changes(self->changes, self->rows) != 0) {
1615 destroy_pending_changes(self->changes);
1617 NSLog(@"out of memory for pending changes with resize of terminal %d",
1618 [self terminalIndex]);
1621 if( saveToDefaults )
1623 int termIndex = [self terminalIndex];
1624 NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
1626 if( termIndex < (int)[terminals count] )
1628 NSMutableDictionary *mutableTerm = [[NSMutableDictionary alloc] initWithDictionary: [terminals objectAtIndex: termIndex]];
1629 [mutableTerm setValue: [NSNumber numberWithUnsignedInt: self->cols] forKey: AngbandTerminalColumnsDefaultsKey];
1630 [mutableTerm setValue: [NSNumber numberWithUnsignedInt: self->rows] forKey: AngbandTerminalRowsDefaultsKey];
1632 NSMutableArray *mutableTerminals = [[NSMutableArray alloc] initWithArray: terminals];
1633 [mutableTerminals replaceObjectAtIndex: termIndex withObject: mutableTerm];
1635 [[NSUserDefaults standardUserDefaults] setValue: mutableTerminals forKey: AngbandTerminalsDefaultsKey];
1636 [mutableTerminals release];
1637 [mutableTerm release];
1639 [[NSUserDefaults standardUserDefaults] synchronize];
1643 Term_activate( self->terminal );
1644 Term_resize( (int)newColumns, (int)newRows);
1646 Term_activate( old );
1649 - (void)saveWindowVisibleToDefaults: (BOOL)windowVisible
1651 int termIndex = [self terminalIndex];
1652 BOOL safeVisibility = (termIndex == 0) ? YES : windowVisible; /* Ensure main term doesn't go away because of these defaults */
1653 NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
1655 if( termIndex < (int)[terminals count] )
1657 NSMutableDictionary *mutableTerm = [[NSMutableDictionary alloc] initWithDictionary: [terminals objectAtIndex: termIndex]];
1658 [mutableTerm setValue: [NSNumber numberWithBool: safeVisibility] forKey: AngbandTerminalVisibleDefaultsKey];
1660 NSMutableArray *mutableTerminals = [[NSMutableArray alloc] initWithArray: terminals];
1661 [mutableTerminals replaceObjectAtIndex: termIndex withObject: mutableTerm];
1663 [[NSUserDefaults standardUserDefaults] setValue: mutableTerminals forKey: AngbandTerminalsDefaultsKey];
1664 [mutableTerminals release];
1665 [mutableTerm release];
1669 - (BOOL)windowVisibleUsingDefaults
1671 int termIndex = [self terminalIndex];
1673 if( termIndex == 0 )
1678 NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
1681 if( termIndex < (int)[terminals count] )
1683 NSDictionary *term = [terminals objectAtIndex: termIndex];
1684 NSNumber *visibleValue = [term valueForKey: AngbandTerminalVisibleDefaultsKey];
1686 if( visibleValue != nil )
1688 visible = [visibleValue boolValue];
1696 #pragma mark NSWindowDelegate Methods
1698 /*- (void)windowWillStartLiveResize: (NSNotification *)notification
1702 - (void)windowDidEndLiveResize: (NSNotification *)notification
1704 NSWindow *window = [notification object];
1705 NSRect contentRect = [window contentRectForFrameRect: [window frame]];
1706 [self resizeTerminalWithContentRect: contentRect saveToDefaults: YES];
1709 /*- (NSSize)windowWillResize: (NSWindow *)sender toSize: (NSSize)frameSize
1713 - (void)windowDidEnterFullScreen: (NSNotification *)notification
1715 NSWindow *window = [notification object];
1716 NSRect contentRect = [window contentRectForFrameRect: [window frame]];
1717 [self resizeTerminalWithContentRect: contentRect saveToDefaults: NO];
1720 - (void)windowDidExitFullScreen: (NSNotification *)notification
1722 NSWindow *window = [notification object];
1723 NSRect contentRect = [window contentRectForFrameRect: [window frame]];
1724 [self resizeTerminalWithContentRect: contentRect saveToDefaults: NO];
1727 - (void)windowDidBecomeMain:(NSNotification *)notification
1729 NSWindow *window = [notification object];
1731 if( window != self->primaryWindow )
1736 int termIndex = [self terminalIndex];
1737 NSMenuItem *item = [[[NSApplication sharedApplication] windowsMenu] itemWithTag: AngbandWindowMenuItemTagBase + termIndex];
1738 [item setState: NSOnState];
1740 if( [[NSFontPanel sharedFontPanel] isVisible] )
1742 [[NSFontPanel sharedFontPanel] setPanelFont: [self selectionFont] isMultiple: NO];
1746 - (void)windowDidResignMain: (NSNotification *)notification
1748 NSWindow *window = [notification object];
1750 if( window != self->primaryWindow )
1755 int termIndex = [self terminalIndex];
1756 NSMenuItem *item = [[[NSApplication sharedApplication] windowsMenu] itemWithTag: AngbandWindowMenuItemTagBase + termIndex];
1757 [item setState: NSOffState];
1760 - (void)windowWillClose: (NSNotification *)notification
1762 [self saveWindowVisibleToDefaults: NO];
1768 @implementation AngbandView
1780 - (void)drawRect:(NSRect)rect
1782 if (! angbandContext)
1784 /* Draw bright orange, 'cause this ain't right */
1785 [[NSColor orangeColor] set];
1786 NSRectFill([self bounds]);
1790 /* Tell the Angband context to draw into us */
1791 [angbandContext drawRect:rect inView:self];
1795 - (void)setAngbandContext:(AngbandContext *)context
1797 angbandContext = context;
1800 - (AngbandContext *)angbandContext
1802 return angbandContext;
1805 - (void)setFrameSize:(NSSize)size
1807 BOOL changed = ! NSEqualSizes(size, [self frame].size);
1808 [super setFrameSize:size];
1809 if (changed) [angbandContext angbandViewDidScale:self];
1812 - (void)viewWillStartLiveResize
1814 [angbandContext viewWillStartLiveResize:self];
1817 - (void)viewDidEndLiveResize
1819 [angbandContext viewDidEndLiveResize:self];
1825 * Delay handling of double-clicked savefiles
1827 Boolean open_when_ready = FALSE;
1832 * ------------------------------------------------------------------------
1833 * Some generic functions
1834 * ------------------------------------------------------------------------ */
1837 * Sets an Angband color at a given index
1839 static void set_color_for_index(int idx)
1843 /* Extract the R,G,B data */
1844 rv = angband_color_table[idx][1];
1845 gv = angband_color_table[idx][2];
1846 bv = angband_color_table[idx][3];
1848 CGContextSetRGBFillColor([[NSGraphicsContext currentContext] graphicsPort], rv/255., gv/255., bv/255., 1.);
1852 * Remember the current character in UserDefaults so we can select it by
1853 * default next time.
1855 static void record_current_savefile(void)
1857 NSString *savefileString = [[NSString stringWithCString:savefile encoding:NSMacOSRomanStringEncoding] lastPathComponent];
1860 NSUserDefaults *angbandDefs = [NSUserDefaults angbandDefaults];
1861 [angbandDefs setObject:savefileString forKey:@"SaveFile"];
1862 [angbandDefs synchronize];
1869 * Convert a two-byte EUC-JP encoded character (both *cp and (*cp + 1) are in
1870 * the range, 0xA1-0xFE, or *cp is 0x8E) to a utf16 value in the native byte
1873 static wchar_t convert_two_byte_eucjp_to_utf16_native(const char *cp)
1875 NSString* str = [[NSString alloc] initWithBytes:cp length:2
1876 encoding:NSJapaneseEUCStringEncoding];
1877 wchar_t result = [str characterAtIndex:0];
1886 * ------------------------------------------------------------------------
1887 * Support for the "z-term.c" package
1888 * ------------------------------------------------------------------------ */
1892 * Initialize a new Term
1894 static void Term_init_cocoa(term *t)
1896 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1897 AngbandContext *context = [[AngbandContext alloc] init];
1899 /* Give the term a hard retain on context (for GC) */
1900 t->data = (void *)CFRetain(context);
1903 /* Handle graphics */
1904 t->higher_pict = !! use_graphics;
1905 t->always_pict = FALSE;
1907 NSDisableScreenUpdates();
1909 /* Figure out the frame autosave name based on the index of this term */
1910 NSString *autosaveName = nil;
1912 for (termIdx = 0; termIdx < ANGBAND_TERM_MAX; termIdx++)
1914 if (angband_term[termIdx] == t)
1916 autosaveName = [NSString stringWithFormat:@"AngbandTerm-%d", termIdx];
1922 NSString *fontName = [[NSUserDefaults angbandDefaults] stringForKey:[NSString stringWithFormat:@"FontName-%d", termIdx]];
1923 if (! fontName) fontName = [default_font fontName];
1925 /* Use a smaller default font for the other windows, but only if the font
1926 * hasn't been explicitly set */
1927 float fontSize = (termIdx > 0) ? 10.0 : [default_font pointSize];
1928 NSNumber *fontSizeNumber = [[NSUserDefaults angbandDefaults] valueForKey: [NSString stringWithFormat: @"FontSize-%d", termIdx]];
1930 if( fontSizeNumber != nil )
1932 fontSize = [fontSizeNumber floatValue];
1935 [context setSelectionFont:[NSFont fontWithName:fontName size:fontSize] adjustTerminal: NO];
1937 NSArray *terminalDefaults = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
1938 NSInteger rows = 24;
1939 NSInteger columns = 80;
1941 if( termIdx < (int)[terminalDefaults count] )
1943 NSDictionary *term = [terminalDefaults objectAtIndex: termIdx];
1944 NSInteger defaultRows = [[term valueForKey: AngbandTerminalRowsDefaultsKey] integerValue];
1945 NSInteger defaultColumns = [[term valueForKey: AngbandTerminalColumnsDefaultsKey] integerValue];
1947 if (defaultRows > 0) rows = defaultRows;
1948 if (defaultColumns > 0) columns = defaultColumns;
1951 context->cols = columns;
1952 context->rows = rows;
1954 if (resize_pending_changes(context->changes, context->rows) != 0) {
1955 destroy_pending_changes(context->changes);
1956 context->changes = 0;
1957 NSLog(@"initializing terminal %d: out of memory for pending changes",
1961 /* Get the window */
1962 NSWindow *window = [context makePrimaryWindow];
1965 /* Set its title and, for auxiliary terms, tentative size */
1968 title = [NSString stringWithCString:angband_term_name[0]
1970 encoding:NSJapaneseEUCStringEncoding
1972 encoding:NSMacOSRomanStringEncoding
1975 [window setTitle:title];
1977 /* Set minimum size (80x24) */
1979 minsize.width = 80 * context->tileSize.width + context->borderSize.width * 2.0;
1980 minsize.height = 24 * context->tileSize.height + context->borderSize.height * 2.0;
1981 [window setContentMinSize:minsize];
1985 title = [NSString stringWithCString:angband_term_name[termIdx]
1987 encoding:NSJapaneseEUCStringEncoding
1989 encoding:NSMacOSRomanStringEncoding
1992 [window setTitle:title];
1993 /* Set minimum size (1x1) */
1995 minsize.width = context->tileSize.width + context->borderSize.width * 2.0;
1996 minsize.height = context->tileSize.height + context->borderSize.height * 2.0;
1997 [window setContentMinSize:minsize];
2001 /* If this is the first term, and we support full screen (Mac OS X Lion or
2002 * later), then allow it to go full screen (sweet). Allow other terms to be
2003 * FullScreenAuxilliary, so they can at least show up. Unfortunately in
2004 * Lion they don't get brought to the full screen space; but they would
2005 * only make sense on multiple displays anyways so it's not a big loss. */
2006 if ([window respondsToSelector:@selector(toggleFullScreen:)])
2008 NSWindowCollectionBehavior behavior = [window collectionBehavior];
2009 behavior |= (termIdx == 0 ? Angband_NSWindowCollectionBehaviorFullScreenPrimary : Angband_NSWindowCollectionBehaviorFullScreenAuxiliary);
2010 [window setCollectionBehavior:behavior];
2013 /* No Resume support yet, though it would not be hard to add */
2014 if ([window respondsToSelector:@selector(setRestorable:)])
2016 [window setRestorable:NO];
2019 /* default window placement */ {
2020 static NSRect overallBoundingRect;
2024 /* This is a bit of a trick to allow us to display multiple windows
2025 * in the "standard default" window position in OS X: the upper
2026 * center of the screen.
2027 * The term sizes set in load_prefs() are based on a 5-wide by
2028 * 3-high grid, with the main term being 4/5 wide by 2/3 high
2029 * (hence the scaling to find */
2031 /* What the containing rect would be). */
2032 NSRect originalMainTermFrame = [window frame];
2033 NSRect scaledFrame = originalMainTermFrame;
2034 scaledFrame.size.width *= 5.0 / 4.0;
2035 scaledFrame.size.height *= 3.0 / 2.0;
2036 scaledFrame.size.width += 1.0; /* spacing between window columns */
2037 scaledFrame.size.height += 1.0; /* spacing between window rows */
2038 [window setFrame: scaledFrame display: NO];
2040 overallBoundingRect = [window frame];
2041 [window setFrame: originalMainTermFrame display: NO];
2044 static NSRect mainTermBaseRect;
2045 NSRect windowFrame = [window frame];
2049 /* The height and width adjustments were determined experimentally,
2050 * so that the rest of the windows line up nicely without
2052 windowFrame.size.width += 7.0;
2053 windowFrame.size.height += 9.0;
2054 windowFrame.origin.x = NSMinX( overallBoundingRect );
2055 windowFrame.origin.y = NSMaxY( overallBoundingRect ) - NSHeight( windowFrame );
2056 mainTermBaseRect = windowFrame;
2058 else if( termIdx == 1 )
2060 windowFrame.origin.x = NSMinX( mainTermBaseRect );
2061 windowFrame.origin.y = NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
2063 else if( termIdx == 2 )
2065 windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
2066 windowFrame.origin.y = NSMaxY( mainTermBaseRect ) - NSHeight( windowFrame );
2068 else if( termIdx == 3 )
2070 windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
2071 windowFrame.origin.y = NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
2073 else if( termIdx == 4 )
2075 windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
2076 windowFrame.origin.y = NSMinY( mainTermBaseRect );
2078 else if( termIdx == 5 )
2080 windowFrame.origin.x = NSMinX( mainTermBaseRect ) + NSWidth( windowFrame ) + 1.0;
2081 windowFrame.origin.y = NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
2084 [window setFrame: windowFrame display: NO];
2087 /* Override the default frame above if the user has adjusted windows in
2089 if (autosaveName) [window setFrameAutosaveName:autosaveName];
2091 /* Tell it about its term. Do this after we've sized it so that the sizing
2092 * doesn't trigger redrawing and such. */
2093 [context setTerm:t];
2095 /* Only order front if it's the first term. Other terms will be ordered
2096 * front from AngbandUpdateWindowVisibility(). This is to work around a
2097 * problem where Angband aggressively tells us to initialize terms that
2098 * don't do anything! */
2099 if (t == angband_term[0]) [context->primaryWindow makeKeyAndOrderFront: nil];
2101 NSEnableScreenUpdates();
2103 /* Set "mapped" flag */
2104 t->mapped_flag = true;
2113 static void Term_nuke_cocoa(term *t)
2115 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2117 AngbandContext *context = t->data;
2120 /* Tell the context to get rid of its windows, etc. */
2123 /* Balance our CFRetain from when we created it */
2134 * Returns the CGImageRef corresponding to an image with the given name in the
2135 * resource directory, transferring ownership to the caller
2137 static CGImageRef create_angband_image(NSString *path)
2139 CGImageRef decodedImage = NULL, result = NULL;
2141 /* Try using ImageIO to load the image */
2144 NSURL *url = [[NSURL alloc] initFileURLWithPath:path isDirectory:NO];
2147 NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:(id)kCFBooleanTrue, kCGImageSourceShouldCache, nil];
2148 CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, (CFDictionaryRef)options);
2151 /* We really want the largest image, but in practice there's
2152 * only going to be one */
2153 decodedImage = CGImageSourceCreateImageAtIndex(source, 0, (CFDictionaryRef)options);
2161 /* Draw the sucker to defeat ImageIO's weird desire to cache and decode on
2162 * demand. Our images aren't that big! */
2165 size_t width = CGImageGetWidth(decodedImage), height = CGImageGetHeight(decodedImage);
2167 /* Compute our own bitmap info */
2168 CGBitmapInfo imageBitmapInfo = CGImageGetBitmapInfo(decodedImage);
2169 CGBitmapInfo contextBitmapInfo = kCGBitmapByteOrderDefault;
2171 switch (imageBitmapInfo & kCGBitmapAlphaInfoMask) {
2172 case kCGImageAlphaNone:
2173 case kCGImageAlphaNoneSkipLast:
2174 case kCGImageAlphaNoneSkipFirst:
2176 contextBitmapInfo |= kCGImageAlphaNone;
2179 /* Some alpha, use premultiplied last which is most efficient. */
2180 contextBitmapInfo |= kCGImageAlphaPremultipliedLast;
2184 /* Draw the source image flipped, since the view is flipped */
2185 CGContextRef ctx = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(decodedImage), CGImageGetBytesPerRow(decodedImage), CGImageGetColorSpace(decodedImage), contextBitmapInfo);
2186 CGContextSetBlendMode(ctx, kCGBlendModeCopy);
2187 CGContextTranslateCTM(ctx, 0.0, height);
2188 CGContextScaleCTM(ctx, 1.0, -1.0);
2189 CGContextDrawImage(ctx, CGRectMake(0, 0, width, height), decodedImage);
2190 result = CGBitmapContextCreateImage(ctx);
2192 /* Done with these things */
2194 CGImageRelease(decodedImage);
2202 static errr Term_xtra_cocoa_react(void)
2204 /* Don't actually switch graphics until the game is running */
2205 if (!initialized || !game_in_progress) return (-1);
2207 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2208 AngbandContext *angbandContext = Term->data;
2210 /* Handle graphics */
2211 int expected_graf_mode = (current_graphics_mode) ?
2212 current_graphics_mode->grafID : GRAPHICS_NONE;
2213 if (graf_mode_req != expected_graf_mode)
2215 graphics_mode *new_mode;
2216 if (graf_mode_req != GRAPHICS_NONE) {
2217 new_mode = get_graphics_mode(graf_mode_req);
2222 /* Get rid of the old image. CGImageRelease is NULL-safe. */
2223 CGImageRelease(pict_image);
2226 /* Try creating the image if we want one */
2227 if (new_mode != NULL)
2229 NSString *img_path = [NSString stringWithFormat:@"%s/%s", new_mode->path, new_mode->file];
2230 pict_image = create_angband_image(img_path);
2232 /* If we failed to create the image, set the new desired mode to
2238 /* Record what we did */
2239 use_graphics = new_mode ? new_mode->grafID : 0;
2241 /* This global is not in Hengband. */
2242 use_transparency = (new_mode != NULL);
2244 ANGBAND_GRAF = (new_mode ? new_mode->graf : "ascii");
2245 current_graphics_mode = new_mode;
2247 /* Enable or disable higher picts. Note: this should be done for all
2249 angbandContext->terminal->higher_pict = !! use_graphics;
2251 if (pict_image && current_graphics_mode)
2253 /* Compute the row and column count via the image height and width.
2255 pict_rows = (int)(CGImageGetHeight(pict_image) / current_graphics_mode->cell_height);
2256 pict_cols = (int)(CGImageGetWidth(pict_image) / current_graphics_mode->cell_width);
2265 if (initialized && game_in_progress)
2278 * Draws one tile as a helper function for Term_xtra_cocoa_fresh().
2280 static void draw_image_tile(
2281 NSGraphicsContext* nsContext,
2282 CGContextRef cgContext,
2286 NSCompositingOperation op)
2288 /* Flip the source rect since the source image is flipped */
2289 CGAffineTransform flip = CGAffineTransformIdentity;
2290 flip = CGAffineTransformTranslate(flip, 0.0, CGImageGetHeight(image));
2291 flip = CGAffineTransformScale(flip, 1.0, -1.0);
2292 CGRect flippedSourceRect =
2293 CGRectApplyAffineTransform(NSRectToCGRect(srcRect), flip);
2296 * When we use high-quality resampling to draw a tile, pixels from outside
2297 * the tile may bleed in, causing graphics artifacts. Work around that.
2299 CGImageRef subimage =
2300 CGImageCreateWithImageInRect(image, flippedSourceRect);
2301 [nsContext setCompositingOperation:op];
2302 CGContextDrawImage(cgContext, NSRectToCGRect(dstRect), subimage);
2303 CGImageRelease(subimage);
2308 * This is a helper function for Term_xtra_cocoa_fresh(): look before a block
2309 * of text on a row to see if the bounds for rendering and clipping need to be
2312 static void query_before_text(
2313 struct PendingRowChange* prc, int iy, int npre, int* pclip, int* prend)
2319 if (i < 0 || i < start - npre) {
2323 if (prc->cell_changes[i].change_type == CELL_CHANGE_PICT) {
2325 * The cell has been rendered with a tile. Do not want to modify
2326 * its contents so the clipping and rendering region can not be
2330 } else if (prc->cell_changes[i].change_type == CELL_CHANGE_NONE) {
2331 /* It has not changed so inquire what it is. */
2335 Term_what(i, iy, a + 1, c + 1);
2336 if (use_graphics && (a[1] & 0x80) && (c[1] & 0x80)) {
2338 * It is an unchanged location rendered with a tile. Do not
2339 * want to modify its contents so the clipping and rendering
2340 * region can not be extended.
2345 * It is unchanged text. A character from the changed region
2346 * may have extended into it so render it to clear that.
2349 /* Check to see if it is the second part of a kanji character. */
2351 Term_what(i - 1, iy, a, c);
2353 prc->cell_changes[i - 1].c.w =
2354 convert_two_byte_eucjp_to_utf16_native(c);
2355 prc->cell_changes[i - 1].a = a[0];
2356 prc->cell_changes[i - 1].tcol = 1;
2357 prc->cell_changes[i].c.w = 0;
2358 prc->cell_changes[i].a = a[0];
2359 prc->cell_changes[i].tcol = 0;
2364 prc->cell_changes[i].c.w = c[1];
2365 prc->cell_changes[i].a = a[1];
2366 prc->cell_changes[i].tcol = 0;
2371 prc->cell_changes[i].c.w = c[1];
2372 prc->cell_changes[i].a = a[1];
2373 prc->cell_changes[i].tcol = 0;
2378 prc->cell_changes[i].c.w = c[1];
2379 prc->cell_changes[i].a = a[1];
2380 prc->cell_changes[i].tcol = 0;
2387 * The cell has been wiped or had changed text rendered. Do
2388 * not need to render. Can extend the clipping rectangle into it.
2398 * This is a helper function for Term_xtra_cocoa_fresh(): look after a block
2399 * of text on a row to see if the bounds for rendering and clipping need to be
2402 static void query_after_text(
2403 struct PendingRowChange* prc,
2415 * Be willing to consolidate this block with the one after it. This
2416 * logic should be sufficient to avoid redraws of the region between
2417 * changed blocks of text if angbandContext->ncol_pre is zero or one.
2418 * For larger values of ncol_pre, would need to do something more to
2419 * avoid extra redraws.
2423 prc->cell_changes[i].change_type != CELL_CHANGE_TEXT &&
2424 prc->cell_changes[i].change_type != CELL_CHANGE_WIPE)) {
2428 if (prc->cell_changes[i].change_type == CELL_CHANGE_PICT) {
2430 * The cell has been rendered with a tile. Do not want to modify
2431 * its contents so the clipping and rendering region can not be
2435 } else if (prc->cell_changes[i].change_type == CELL_CHANGE_NONE) {
2436 /* It has not changed so inquire what it is. */
2440 Term_what(i, iy, a, c);
2441 if (use_graphics && (a[0] & 0x80) && (c[0] & 0x80)) {
2443 * It is an unchanged location rendered with a tile. Do not
2444 * want to modify its contents so the clipping and rendering
2445 * region can not be extended.
2450 * It is unchanged text. A character from the changed region
2451 * may have extended into it so render it to clear that.
2454 /* Check to see if it is the first part of a kanji character. */
2456 Term_what(i + 1, iy, a + 1, c + 1);
2458 prc->cell_changes[i].c.w =
2459 convert_two_byte_eucjp_to_utf16_native(c);
2460 prc->cell_changes[i].a = a[0];
2461 prc->cell_changes[i].tcol = 1;
2462 prc->cell_changes[i + 1].c.w = 0;
2463 prc->cell_changes[i + 1].a = a[0];
2464 prc->cell_changes[i + 1].tcol = 0;
2469 prc->cell_changes[i].c.w = c[0];
2470 prc->cell_changes[i].a = a[0];
2471 prc->cell_changes[i].tcol = 0;
2476 prc->cell_changes[i].c.w = c[0];
2477 prc->cell_changes[i].a = a[0];
2478 prc->cell_changes[i].tcol = 0;
2483 prc->cell_changes[i].c.w = c[0];
2484 prc->cell_changes[i].a = a[0];
2485 prc->cell_changes[i].tcol = 0;
2492 * Have come to another region of changed text or another region
2493 * to wipe. Combine the regions to minimize redraws.
2505 * Draw the pending changes saved in angbandContext->changes.
2507 static void Term_xtra_cocoa_fresh(AngbandContext* angbandContext)
2509 int graf_width, graf_height, alphablend;
2511 if (angbandContext->changes->has_pict) {
2512 graf_width = current_graphics_mode->cell_width;
2513 graf_height = current_graphics_mode->cell_height;
2515 * Transparency effect. We really want to check
2516 * current_graphics_mode->alphablend, but as of this writing
2517 * that's never set, so we do something lame.
2519 /* alphablend = current_graphics_mode->alphablend */
2520 alphablend = (graf_width > 8 || graf_height > 8);
2527 CGContextRef ctx = [angbandContext lockFocus];
2529 if (angbandContext->changes->has_text ||
2530 angbandContext->changes->has_wipe) {
2531 NSFont *selectionFont = [[angbandContext selectionFont] screenFont];
2532 [selectionFont set];
2536 for (iy = angbandContext->changes->ymin;
2537 iy <= angbandContext->changes->ymax;
2539 struct PendingRowChange* prc = angbandContext->changes->rows[iy];
2542 /* Skip untouched rows. */
2551 if (ix > prc->xmax) {
2555 switch (prc->cell_changes[ix].change_type) {
2556 case CELL_CHANGE_NONE:
2560 case CELL_CHANGE_PICT:
2563 * Because changes are made to the compositing mode, save
2564 * the incoming value.
2566 NSGraphicsContext *nsContext =
2567 [NSGraphicsContext currentContext];
2568 NSCompositingOperation op = nsContext.compositingOperation;
2571 while (jx <= prc->xmax &&
2572 prc->cell_changes[jx].change_type
2573 == CELL_CHANGE_PICT) {
2574 NSRect destinationRect =
2575 [angbandContext rectInImageForTileAtX:jx Y:iy];
2576 NSRect sourceRect, terrainRect;
2578 sourceRect.origin.x = graf_width *
2579 prc->cell_changes[jx].c.c;
2580 sourceRect.origin.y = graf_height *
2581 prc->cell_changes[jx].a;
2582 sourceRect.size.width = graf_width;
2583 sourceRect.size.height = graf_height;
2584 terrainRect.origin.x = graf_width *
2585 prc->cell_changes[jx].tcol;
2586 terrainRect.origin.y = graf_height *
2587 prc->cell_changes[jx].trow;
2588 terrainRect.size.width = graf_width;
2589 terrainRect.size.height = graf_height;
2604 NSCompositeSourceOver);
2617 [nsContext setCompositingOperation:op];
2620 [angbandContext rectInImageForTileAtX:ix Y:iy];
2622 angbandContext->tileSize.width * (jx - ix);
2623 [angbandContext setNeedsDisplayInBaseRect:rect];
2628 case CELL_CHANGE_WIPE:
2629 case CELL_CHANGE_TEXT:
2631 * For a wiped region, treat it as if it had text (the only
2632 * loss if it was not is some extra work rendering
2633 * neighboring unchanged text).
2636 while (jx < angbandContext->cols &&
2637 (prc->cell_changes[jx].change_type
2639 || prc->cell_changes[jx].change_type
2640 == CELL_CHANGE_WIPE)) {
2645 int ieclip = jx - 1;
2647 int ierend = jx - 1;
2649 TERM_COLOR alast = 0;
2654 prc, iy, angbandContext->ncol_pre, &isclip, &isrend);
2658 angbandContext->cols,
2659 angbandContext->ncol_post,
2665 /* Save the state since the clipping will be modified. */
2666 CGContextSaveGState(ctx);
2668 /* Clear the area that where rendering will be done. */
2669 r = [angbandContext rectInImageForTileAtX:isrend Y:iy];
2670 r.size.width = angbandContext->tileSize.width *
2671 (ierend - isrend + 1);
2672 [[NSColor blackColor] set];
2676 * Clear the current path so it does not affect clipping.
2677 * Then set the clipping rectangle. Using
2678 * CGContextSetTextDrawingMode() to include clipping does
2679 * not appear to be necessary on 10.14 and is actually
2680 * detrimental: when displaying more than one character,
2681 * only the first is visible.
2683 CGContextBeginPath(ctx);
2684 r = [angbandContext rectInImageForTileAtX:isclip Y:iy];
2685 r.size.width = angbandContext->tileSize.width *
2686 (ieclip - isclip + 1);
2687 CGContextClipToRect(ctx, r);
2691 while (k <= ierend) {
2694 if (prc->cell_changes[k].change_type
2695 == CELL_CHANGE_WIPE) {
2696 /* Skip over since no rendering is necessary. */
2701 if (set_color || alast != prc->cell_changes[k].a) {
2703 alast = prc->cell_changes[k].a;
2704 set_color_for_index(alast % MAX_COLORS);
2708 [angbandContext rectInImageForTileAtX:k Y:iy];
2709 if (prc->cell_changes[k].tcol) {
2710 rectToDraw.size.width *= 2.0;
2711 [angbandContext drawWChar:prc->cell_changes[k].c.w
2712 inRect:rectToDraw context:ctx];
2715 [angbandContext drawWChar:prc->cell_changes[k].c.w
2716 inRect:rectToDraw context:ctx];
2722 * Inform the context that the area in the clipping
2723 * rectangle needs to be redisplayed.
2725 [angbandContext setNeedsDisplayInBaseRect:r];
2727 CGContextRestoreGState(ctx);
2734 if (angbandContext->changes->xcurs >= 0 &&
2735 angbandContext->changes->ycurs >= 0) {
2736 NSRect rect = [angbandContext
2737 rectInImageForTileAtX:angbandContext->changes->xcurs
2738 Y:angbandContext->changes->ycurs];
2740 if (angbandContext->changes->bigcurs) {
2741 rect.size.width += angbandContext->tileSize.width;
2743 [[NSColor yellowColor] set];
2744 NSFrameRectWithWidth(rect, 1);
2745 /* Invalidate that rect */
2746 [angbandContext setNeedsDisplayInBaseRect:rect];
2749 [angbandContext unlockFocus];
2754 * Do a "special thing"
2756 static errr Term_xtra_cocoa(int n, int v)
2758 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2759 AngbandContext* angbandContext = Term->data;
2767 case TERM_XTRA_NOISE:
2776 case TERM_XTRA_SOUND:
2780 /* Process random events */
2781 case TERM_XTRA_BORED:
2783 /* Show or hide cocoa windows based on the subwindow flags set by
2785 AngbandUpdateWindowVisibility();
2787 /* Process an event */
2788 (void)check_events(CHECK_EVENTS_NO_WAIT);
2794 /* Process pending events */
2795 case TERM_XTRA_EVENT:
2797 /* Process an event */
2798 (void)check_events(v);
2804 /* Flush all pending events (if any) */
2805 case TERM_XTRA_FLUSH:
2807 /* Hack -- flush all events */
2808 while (check_events(CHECK_EVENTS_DRAIN)) /* loop */;
2814 /* Hack -- Change the "soft level" */
2815 case TERM_XTRA_LEVEL:
2817 /* Here we could activate (if requested), but I don't think Angband
2818 * should be telling us our window order (the user should decide
2819 * that), so do nothing. */
2823 /* Clear the screen */
2824 case TERM_XTRA_CLEAR:
2826 [angbandContext lockFocus];
2827 [[NSColor blackColor] set];
2828 NSRect imageRect = {NSZeroPoint, [angbandContext imageSize]};
2829 NSRectFillUsingOperation(imageRect, NSCompositeCopy);
2830 [angbandContext unlockFocus];
2831 [angbandContext setNeedsDisplay:YES];
2836 /* React to changes */
2837 case TERM_XTRA_REACT:
2839 /* React to changes */
2840 return (Term_xtra_cocoa_react());
2843 /* Delay (milliseconds) */
2844 case TERM_XTRA_DELAY:
2850 double seconds = v / 1000.;
2851 NSDate* date = [NSDate dateWithTimeIntervalSinceNow:seconds];
2857 event = [NSApp nextEventMatchingMask:-1 untilDate:date inMode:NSDefaultRunLoopMode dequeue:YES];
2858 if (event) send_event(event);
2860 } while ([date timeIntervalSinceNow] >= 0);
2868 case TERM_XTRA_FRESH:
2869 /* Draw the pending changes. */
2870 if (angbandContext->changes != 0) {
2871 Term_xtra_cocoa_fresh(angbandContext);
2872 clear_pending_changes(angbandContext->changes);
2888 static errr Term_curs_cocoa(int x, int y)
2890 AngbandContext *angbandContext = Term->data;
2892 if (angbandContext->changes == 0) {
2893 /* Bail out; there was an earlier memory allocation failure. */
2896 angbandContext->changes->xcurs = x;
2897 angbandContext->changes->ycurs = y;
2898 angbandContext->changes->bigcurs = 0;
2905 * Draw a cursor that's two tiles wide. For Japanese, that's used when
2906 * the cursor points at a kanji character, irregardless of whether operating
2909 static errr Term_bigcurs_cocoa(int x, int y)
2911 AngbandContext *angbandContext = Term->data;
2913 if (angbandContext->changes == 0) {
2914 /* Bail out; there was an earlier memory allocation failure. */
2917 angbandContext->changes->xcurs = x;
2918 angbandContext->changes->ycurs = y;
2919 angbandContext->changes->bigcurs = 1;
2926 * Low level graphics (Assumes valid input)
2928 * Erase "n" characters starting at (x,y)
2930 static errr Term_wipe_cocoa(int x, int y, int n)
2932 AngbandContext *angbandContext = Term->data;
2933 struct PendingCellChange *pc;
2935 if (angbandContext->changes == 0) {
2936 /* Bail out; there was an earlier memory allocation failure. */
2939 if (angbandContext->changes->rows[y] == 0) {
2940 angbandContext->changes->rows[y] =
2941 create_row_change(angbandContext->cols);
2942 if (angbandContext->changes->rows[y] == 0) {
2943 NSLog(@"failed to allocate changes for row %d", y);
2946 if (angbandContext->changes->ymin > y) {
2947 angbandContext->changes->ymin = y;
2949 if (angbandContext->changes->ymax < y) {
2950 angbandContext->changes->ymax = y;
2954 angbandContext->changes->has_wipe = 1;
2955 if (angbandContext->changes->rows[y]->xmin > x) {
2956 angbandContext->changes->rows[y]->xmin = x;
2958 if (angbandContext->changes->rows[y]->xmax < x + n - 1) {
2959 angbandContext->changes->rows[y]->xmax = x + n - 1;
2961 for (pc = angbandContext->changes->rows[y]->cell_changes + x;
2962 pc != angbandContext->changes->rows[y]->cell_changes + x + n;
2964 pc->change_type = CELL_CHANGE_WIPE;
2971 static errr Term_pict_cocoa(int x, int y, int n, TERM_COLOR *ap,
2972 const char *cp, const TERM_COLOR *tap,
2976 /* Paranoia: Bail if we don't have a current graphics mode */
2977 if (! current_graphics_mode) return -1;
2979 AngbandContext* angbandContext = Term->data;
2981 struct PendingCellChange *pc;
2983 if (angbandContext->changes == 0) {
2984 /* Bail out; there was an earlier memory allocation failure. */
2987 if (angbandContext->changes->rows[y] == 0) {
2988 angbandContext->changes->rows[y] =
2989 create_row_change(angbandContext->cols);
2990 if (angbandContext->changes->rows[y] == 0) {
2991 NSLog(@"failed to allocate changes for row %d", y);
2994 if (angbandContext->changes->ymin > y) {
2995 angbandContext->changes->ymin = y;
2997 if (angbandContext->changes->ymax < y) {
2998 angbandContext->changes->ymax = y;
3002 if (angbandContext->changes->rows[y]->xmin > x) {
3003 angbandContext->changes->rows[y]->xmin = x;
3005 if (angbandContext->changes->rows[y]->xmax < x + n - 1) {
3006 angbandContext->changes->rows[y]->xmax = x + n - 1;
3008 for (pc = angbandContext->changes->rows[y]->cell_changes + x;
3009 pc != angbandContext->changes->rows[y]->cell_changes + x + n;
3011 TERM_COLOR a = *ap++;
3013 TERM_COLOR ta = *tap++;
3016 if (use_graphics && (a & 0x80) && (c & 0x80)) {
3017 pc->c.c = ((byte)c & 0x7F) % pict_cols;
3018 pc->a = ((byte)a & 0x7F) % pict_rows;
3019 pc->tcol = ((byte)tc & 0x7F) & pict_cols;
3020 pc->trow = ((byte)ta & 0x7F) & pict_rows;
3021 pc->change_type = CELL_CHANGE_PICT;
3026 angbandContext->changes->has_pict = 1;
3034 * Low level graphics. Assumes valid input.
3036 * Draw several ("n") chars, with an attr, at a given location.
3038 static errr Term_text_cocoa(int x, int y, int n, byte_hack a, concptr cp)
3040 AngbandContext* angbandContext = Term->data;
3041 struct PendingCellChange *pc;
3043 if (angbandContext->changes == 0) {
3044 /* Bail out; there was an earlier memory allocation failure. */
3047 if (angbandContext->changes->rows[y] == 0) {
3048 angbandContext->changes->rows[y] =
3049 create_row_change(angbandContext->cols);
3050 if (angbandContext->changes->rows[y] == 0) {
3051 NSLog(@"failed to allocate changes for row %d", y);
3054 if (angbandContext->changes->ymin > y) {
3055 angbandContext->changes->ymin = y;
3057 if (angbandContext->changes->ymax < y) {
3058 angbandContext->changes->ymax = y;
3062 angbandContext->changes->has_text = 1;
3063 if (angbandContext->changes->rows[y]->xmin > x) {
3064 angbandContext->changes->rows[y]->xmin = x;
3066 if (angbandContext->changes->rows[y]->xmax < x + n - 1) {
3067 angbandContext->changes->rows[y]->xmax = x + n - 1;
3069 pc = angbandContext->changes->rows[y]->cell_changes + x;
3070 while (pc != angbandContext->changes->rows[y]->cell_changes + x + n) {
3074 angbandContext->changes->rows[y]->cell_changes + x + n) {
3076 * The second byte of the character is past the end. Ignore
3081 pc->c.w = convert_two_byte_eucjp_to_utf16_native(cp);
3084 pc->change_type = CELL_CHANGE_TEXT;
3087 * Fill in a dummy value since the previous character will take
3093 pc->change_type = CELL_CHANGE_TEXT;
3101 pc->change_type = CELL_CHANGE_TEXT;
3109 pc->change_type = CELL_CHANGE_TEXT;
3120 * Post a nonsense event so that our event loop wakes up
3122 static void wakeup_event_loop(void)
3124 /* Big hack - send a nonsense event to make us update */
3125 NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined location:NSZeroPoint modifierFlags:0 timestamp:0 windowNumber:0 context:NULL subtype:AngbandEventWakeup data1:0 data2:0];
3126 [NSApp postEvent:event atStart:NO];
3131 * Create and initialize window number "i"
3133 static term *term_data_link(int i)
3135 NSArray *terminalDefaults = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
3136 NSInteger rows = 24;
3137 NSInteger columns = 80;
3139 if( i < (int)[terminalDefaults count] )
3141 NSDictionary *term = [terminalDefaults objectAtIndex: i];
3142 rows = [[term valueForKey: AngbandTerminalRowsDefaultsKey] integerValue];
3143 columns = [[term valueForKey: AngbandTerminalColumnsDefaultsKey] integerValue];
3147 term *newterm = ZNEW(term);
3149 /* Initialize the term */
3150 term_init(newterm, columns, rows, 256 /* keypresses, for some reason? */);
3152 /* Differentiate between BS/^h, Tab/^i, etc. */
3153 /* newterm->complex_input = TRUE; */
3155 /* Use a "software" cursor */
3156 newterm->soft_cursor = TRUE;
3158 /* Disable the per-row flush notifications since they are not used. */
3159 newterm->never_frosh = TRUE;
3161 /* Erase with "white space" */
3162 newterm->attr_blank = TERM_WHITE;
3163 newterm->char_blank = ' ';
3165 /* Prepare the init/nuke hooks */
3166 newterm->init_hook = Term_init_cocoa;
3167 newterm->nuke_hook = Term_nuke_cocoa;
3169 /* Prepare the function hooks */
3170 newterm->xtra_hook = Term_xtra_cocoa;
3171 newterm->wipe_hook = Term_wipe_cocoa;
3172 newterm->curs_hook = Term_curs_cocoa;
3173 newterm->bigcurs_hook = Term_bigcurs_cocoa;
3174 newterm->text_hook = Term_text_cocoa;
3175 newterm->pict_hook = Term_pict_cocoa;
3176 /* newterm->mbcs_hook = Term_mbcs_cocoa; */
3178 /* Global pointer */
3179 angband_term[i] = newterm;
3185 * Load preferences from preferences file for current host+current user+
3186 * current application.
3188 static void load_prefs()
3190 NSUserDefaults *defs = [NSUserDefaults angbandDefaults];
3192 /* Make some default defaults */
3193 NSMutableArray *defaultTerms = [[NSMutableArray alloc] init];
3195 /* The following default rows/cols were determined experimentally by first
3196 * finding the ideal window/font size combinations. But because of awful
3197 * temporal coupling in Term_init_cocoa(), it's impossible to set up the
3198 * defaults there, so we do it this way. */
3199 for( NSUInteger i = 0; i < ANGBAND_TERM_MAX; i++ )
3237 NSDictionary *standardTerm = [NSDictionary dictionaryWithObjectsAndKeys:
3238 [NSNumber numberWithInt: rows], AngbandTerminalRowsDefaultsKey,
3239 [NSNumber numberWithInt: columns], AngbandTerminalColumnsDefaultsKey,
3240 [NSNumber numberWithBool: visible], AngbandTerminalVisibleDefaultsKey,
3242 [defaultTerms addObject: standardTerm];
3245 NSDictionary *defaults = [[NSDictionary alloc] initWithObjectsAndKeys:
3247 @"Osaka", @"FontName",
3249 @"Menlo", @"FontName",
3251 [NSNumber numberWithFloat:13.f], @"FontSize",
3252 [NSNumber numberWithInt:60], AngbandFrameRateDefaultsKey,
3253 [NSNumber numberWithBool:YES], AngbandSoundDefaultsKey,
3254 [NSNumber numberWithInt:GRAPHICS_NONE], AngbandGraphicsDefaultsKey,
3255 defaultTerms, AngbandTerminalsDefaultsKey,
3257 [defs registerDefaults:defaults];
3259 [defaultTerms release];
3261 /* Preferred graphics mode */
3262 graf_mode_req = [defs integerForKey:AngbandGraphicsDefaultsKey];
3264 /* Use sounds; set the Angband global */
3265 use_sound = ([defs boolForKey:AngbandSoundDefaultsKey] == YES) ? TRUE : FALSE;
3268 frames_per_second = [defs integerForKey:AngbandFrameRateDefaultsKey];
3271 default_font = [[NSFont fontWithName:[defs valueForKey:@"FontName-0"] size:[defs floatForKey:@"FontSize-0"]] retain];
3272 if (! default_font) default_font = [[NSFont fontWithName:@"Menlo" size:13.] retain];
3276 * Arbitary limit on number of possible samples per event
3278 #define MAX_SAMPLES 16
3281 * Struct representing all data for a set of event samples
3285 int num; /* Number of available samples for this event */
3286 NSSound *sound[MAX_SAMPLES];
3287 } sound_sample_list;
3290 * Array of event sound structs
3292 static sound_sample_list samples[MSG_MAX];
3296 * Load sound effects based on sound.cfg within the xtra/sound directory;
3297 * bridge to Cocoa to use NSSound for simple loading and playback, avoiding
3298 * I/O latency by cacheing all sounds at the start. Inherits full sound
3299 * format support from Quicktime base/plugins.
3300 * pelpel favoured a plist-based parser for the future but .cfg support
3301 * improves cross-platform compatibility.
3303 static void load_sounds(void)
3305 char sound_dir[1024];
3310 /* Build the "sound" path */
3311 path_build(sound_dir, sizeof(sound_dir), ANGBAND_DIR_XTRA, "sound");
3313 /* Find and open the config file */
3314 path_build(path, sizeof(path), sound_dir, "sound.cfg");
3315 fff = my_fopen(path, "r");
3320 NSLog(@"The sound configuration file could not be opened.");
3324 /* Instantiate an autorelease pool for use by NSSound */
3325 NSAutoreleasePool *autorelease_pool;
3326 autorelease_pool = [[NSAutoreleasePool alloc] init];
3328 /* Use a dictionary to unique sounds, so we can share NSSounds across
3329 * multiple events */
3330 NSMutableDictionary *sound_dict = [NSMutableDictionary dictionary];
3333 * This loop may take a while depending on the count and size of samples
3337 /* Parse the file */
3338 /* Lines are always of the form "name = sample [sample ...]" */
3339 while (my_fgets(fff, buffer, sizeof(buffer)) == 0)
3342 char *cfg_sample_list;
3348 /* Skip anything not beginning with an alphabetic character */
3349 if (!buffer[0] || !isalpha((unsigned char)buffer[0])) continue;
3351 /* Split the line into two: message name, and the rest */
3352 search = strchr(buffer, ' ');
3353 cfg_sample_list = strchr(search + 1, ' ');
3354 if (!search) continue;
3355 if (!cfg_sample_list) continue;
3357 /* Set the message name, and terminate at first space */
3361 /* Make sure this is a valid event name */
3362 for (event = MSG_MAX - 1; event >= 0; event--)
3364 if (strcmp(msg_name, angband_sound_name[event]) == 0)
3367 if (event < 0) continue;
3369 /* Advance the sample list pointer so it's at the beginning of text */
3371 if (!cfg_sample_list[0]) continue;
3373 /* Terminate the current token */
3374 cur_token = cfg_sample_list;
3375 search = strchr(cur_token, ' ');
3379 next_token = search + 1;
3387 * Now we find all the sample names and add them one by one
3391 int num = samples[event].num;
3393 /* Don't allow too many samples */
3394 if (num >= MAX_SAMPLES) break;
3396 NSString *token_string = [NSString stringWithUTF8String:cur_token];
3397 NSSound *sound = [sound_dict objectForKey:token_string];
3403 /* We have to load the sound. Build the path to the sample */
3404 path_build(path, sizeof(path), sound_dir, cur_token);
3405 if (stat(path, &stb) == 0)
3408 /* Load the sound into memory */
3409 sound = [[[NSSound alloc] initWithContentsOfFile:[NSString stringWithUTF8String:path] byReference:YES] autorelease];
3410 if (sound) [sound_dict setObject:sound forKey:token_string];
3414 /* Store it if we loaded it */
3417 samples[event].sound[num] = [sound retain];
3419 /* Imcrement the sample count */
3420 samples[event].num++;
3424 /* Figure out next token */
3425 cur_token = next_token;
3428 /* Try to find a space */
3429 search = strchr(cur_token, ' ');
3431 /* If we can find one, terminate, and set new "next" */
3435 next_token = search + 1;
3439 /* Otherwise prevent infinite looping */
3446 /* Release the autorelease pool */
3447 [autorelease_pool release];
3449 /* Close the file */
3454 * Play sound effects asynchronously. Select a sound from any available
3455 * for the required event, and bridge to Cocoa to play it.
3457 static void play_sound(int event)
3460 if (event < 0 || event >= MSG_MAX) return;
3462 /* Load sounds just-in-time (once) */
3463 static BOOL loaded = NO;
3469 /* Check there are samples for this event */
3470 if (!samples[event].num) return;
3472 /* Instantiate an autorelease pool for use by NSSound */
3473 NSAutoreleasePool *autorelease_pool;
3474 autorelease_pool = [[NSAutoreleasePool alloc] init];
3476 /* Choose a random event */
3477 int s = randint0(samples[event].num);
3479 /* Stop the sound if it's currently playing */
3480 if ([samples[event].sound[s] isPlaying])
3481 [samples[event].sound[s] stop];
3483 /* Play the sound */
3484 [samples[event].sound[s] play];
3486 /* Release the autorelease pool */
3487 [autorelease_pool drain];
3493 static void init_windows(void)
3495 /* Create the main window */
3496 term *primary = term_data_link(0);
3498 /* Prepare to create any additional windows */
3500 for (i=1; i < ANGBAND_TERM_MAX; i++) {
3504 /* Activate the primary term */
3505 Term_activate(primary);
3509 * Handle the "open_when_ready" flag
3511 static void handle_open_when_ready(void)
3513 /* Check the flag XXX XXX XXX make a function for this */
3514 if (open_when_ready && initialized && !game_in_progress)
3517 open_when_ready = FALSE;
3519 /* Game is in progress */
3520 game_in_progress = TRUE;
3522 /* Wait for a keypress */
3523 pause_line(Term->hgt - 1);
3529 * Handle quit_when_ready, by Peter Ammon,
3530 * slightly modified to check inkey_flag.
3532 static void quit_calmly(void)
3534 /* Quit immediately if game's not started */
3535 if (!game_in_progress || !character_generated) quit(NULL);
3537 /* Save the game and Quit (if it's safe) */
3540 /* Hack -- Forget messages and term */
3542 Term->mapped_flag = FALSE;
3545 do_cmd_save_game(FALSE);
3546 record_current_savefile();
3553 /* Wait until inkey_flag is set */
3559 * Returns YES if we contain an AngbandView (and hence should direct our events
3562 static BOOL contains_angband_view(NSView *view)
3564 if ([view isKindOfClass:[AngbandView class]]) return YES;
3565 for (NSView *subview in [view subviews]) {
3566 if (contains_angband_view(subview)) return YES;
3573 * Queue mouse presses if they occur in the map section of the main window.
3575 static void AngbandHandleEventMouseDown( NSEvent *event )
3578 AngbandContext *angbandContext = [[[event window] contentView] angbandContext];
3579 AngbandContext *mainAngbandContext = angband_term[0]->data;
3581 if (mainAngbandContext->primaryWindow && [[event window] windowNumber] == [mainAngbandContext->primaryWindow windowNumber])
3583 int cols, rows, x, y;
3584 Term_get_size(&cols, &rows);
3585 NSSize tileSize = angbandContext->tileSize;
3586 NSSize border = angbandContext->borderSize;
3587 NSPoint windowPoint = [event locationInWindow];
3589 /* Adjust for border; add border height because window origin is at
3591 windowPoint = NSMakePoint( windowPoint.x - border.width, windowPoint.y + border.height );
3593 NSPoint p = [[[event window] contentView] convertPoint: windowPoint fromView: nil];
3594 x = floor( p.x / tileSize.width );
3595 y = floor( p.y / tileSize.height );
3597 /* Being safe about this, since xcode doesn't seem to like the
3598 * bool_hack stuff */
3599 BOOL displayingMapInterface = ((int)inkey_flag != 0);
3601 /* Sidebar plus border == thirteen characters; top row is reserved. */
3602 /* Coordinates run from (0,0) to (cols-1, rows-1). */
3603 BOOL mouseInMapSection = (x > 13 && x <= cols - 1 && y > 0 && y <= rows - 2);
3605 /* If we are displaying a menu, allow clicks anywhere; if we are
3606 * displaying the main game interface, only allow clicks in the map
3608 if (!displayingMapInterface || (displayingMapInterface && mouseInMapSection))
3610 /* [event buttonNumber] will return 0 for left click,
3611 * 1 for right click, but this is safer */
3612 int button = ([event type] == NSLeftMouseDown) ? 1 : 2;
3615 NSUInteger eventModifiers = [event modifierFlags];
3616 byte angbandModifiers = 0;
3617 angbandModifiers |= (eventModifiers & NSShiftKeyMask) ? KC_MOD_SHIFT : 0;
3618 angbandModifiers |= (eventModifiers & NSControlKeyMask) ? KC_MOD_CONTROL : 0;
3619 angbandModifiers |= (eventModifiers & NSAlternateKeyMask) ? KC_MOD_ALT : 0;
3620 button |= (angbandModifiers & 0x0F) << 4; /* encode modifiers in the button number (see Term_mousepress()) */
3623 Term_mousepress(x, y, button);
3627 /* Pass click through to permit focus change, resize, etc. */
3628 [NSApp sendEvent:event];
3634 * Encodes an NSEvent Angband-style, or forwards it along. Returns YES if the
3635 * event was sent to Angband, NO if Cocoa (or nothing) handled it */
3636 static BOOL send_event(NSEvent *event)
3639 /* If the receiving window is not an Angband window, then do nothing */
3640 if (! contains_angband_view([[event window] contentView]))
3642 [NSApp sendEvent:event];
3646 /* Analyze the event */
3647 switch ([event type])
3651 /* Try performing a key equivalent */
3652 if ([[NSApp mainMenu] performKeyEquivalent:event]) break;
3654 unsigned modifiers = [event modifierFlags];
3656 /* Send all NSCommandKeyMasks through */
3657 if (modifiers & NSCommandKeyMask)
3659 [NSApp sendEvent:event];
3663 if (! [[event characters] length]) break;
3666 /* Extract some modifiers */
3668 /* Caught above so don't do anything with it here. */
3669 int mx = !! (modifiers & NSCommandKeyMask);
3671 int mc = !! (modifiers & NSControlKeyMask);
3672 int ms = !! (modifiers & NSShiftKeyMask);
3673 int mo = !! (modifiers & NSAlternateKeyMask);
3674 int kp = !! (modifiers & NSNumericPadKeyMask);
3677 /* Get the Angband char corresponding to this unichar */
3678 unichar c = [[event characters] characterAtIndex:0];
3682 * Convert some special keys to what would be the normal
3683 * alternative in the original keyset or, for things lke
3684 * Delete, Return, and Escape, what one might use from ASCII.
3685 * The rest of Hengband uses Angband 2.7's or so key handling:
3686 * so for the rest do something like the encoding that
3687 * main-win.c does: send a macro trigger with the Unicode
3688 * value encoded into printable ASCII characters. Since
3689 * macro triggers appear to assume at most two keys plus the
3690 * modifiers, can only handle values of c below 4096 with
3691 * 64 values per key.
3693 case NSUpArrowFunctionKey: ch = '8'; kp = 0; break;
3694 case NSDownArrowFunctionKey: ch = '2'; kp = 0; break;
3695 case NSLeftArrowFunctionKey: ch = '4'; kp = 0; break;
3696 case NSRightArrowFunctionKey: ch = '6'; kp = 0; break;
3697 case NSHelpFunctionKey: ch = '?'; break;
3698 case NSDeleteFunctionKey: ch = '\b'; break;
3708 /* override special keys */
3709 switch([event keyCode]) {
3710 case kVK_Return: ch = '\r'; break;
3711 case kVK_Escape: ch = 27; break;
3712 case kVK_Tab: ch = '\t'; break;
3713 case kVK_Delete: ch = '\b'; break;
3714 case kVK_ANSI_KeypadEnter: ch = '\r'; kp = TRUE; break;
3717 /* Hide the mouse pointer */
3718 [NSCursor setHiddenUntilMouseMoves:YES];
3724 /* Enqueue the keypress */
3727 if (mo) mods |= KC_MOD_ALT;
3728 if (mx) mods |= KC_MOD_META;
3729 if (mc && MODS_INCLUDE_CONTROL(ch)) mods |= KC_MOD_CONTROL;
3730 if (ms && MODS_INCLUDE_SHIFT(ch)) mods |= KC_MOD_SHIFT;
3731 if (kp) mods |= KC_MOD_KEYPAD;
3732 Term_keypress(ch, mods);
3736 } else if (c < 4096 || (c >= 0xF700 && c <= 0xF77F)) {
3740 /* Begin the macro trigger. */
3743 /* Send the modifiers. */
3744 if (mc) Term_keypress('C');
3745 if (ms) Term_keypress('S');
3746 if (mo) Term_keypress('A');
3747 if (kp) Term_keypress('K');
3750 * Put part of the range Apple reserves for special keys
3751 * into 0 - 127 since that range has been handled normally.
3757 /* Encode the value as two printable characters. */
3758 part = (c >> 6) & 63;
3760 cenc = 'a' + (part - 38);
3761 } else if (part > 12) {
3762 cenc = 'A' + (part - 12);
3766 Term_keypress(cenc);
3769 cenc = 'a' + (part - 38);
3770 } else if (part > 12) {
3771 cenc = 'A' + (part - 12);
3775 Term_keypress(cenc);
3777 /* End the macro trigger. */
3784 case NSLeftMouseDown:
3785 case NSRightMouseDown:
3786 AngbandHandleEventMouseDown(event);
3789 case NSApplicationDefined:
3791 if ([event subtype] == AngbandEventWakeup)
3799 [NSApp sendEvent:event];
3806 * Check for Events, return TRUE if we process any
3808 static BOOL check_events(int wait)
3811 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3813 /* Handles the quit_when_ready flag */
3814 if (quit_when_ready) quit_calmly();
3817 if (wait == CHECK_EVENTS_WAIT) endDate = [NSDate distantFuture];
3818 else endDate = [NSDate distantPast];
3822 if (quit_when_ready)
3824 /* send escape events until we quit */
3825 Term_keypress(0x1B);
3830 event = [NSApp nextEventMatchingMask:-1 untilDate:endDate inMode:NSDefaultRunLoopMode dequeue:YES];
3836 if (send_event(event)) break;
3842 /* Something happened */
3848 * Hook to tell the user something important
3850 static void hook_plog(const char * str)
3854 NSString *msg = NSLocalizedStringWithDefaultValue(
3855 @"Warning", AngbandMessageCatalog, [NSBundle mainBundle],
3856 @"Warning", @"Alert text for generic warning");
3857 NSString *info = [NSString stringWithCString:str
3859 encoding:NSJapaneseEUCStringEncoding
3861 encoding:NSMacOSRomanStringEncoding
3864 NSAlert *alert = [[NSAlert alloc] init];
3866 alert.messageText = msg;
3867 alert.informativeText = info;
3868 NSModalResponse result = [alert runModal];
3875 * Hook to tell the user something, and then quit
3877 static void hook_quit(const char * str)
3884 * ------------------------------------------------------------------------
3886 * ------------------------------------------------------------------------ */
3888 @interface AngbandAppDelegate : NSObject {
3889 IBOutlet NSMenu *terminalsMenu;
3890 NSMenu *_graphicsMenu;
3891 NSMenu *_commandMenu;
3892 NSDictionary *_commandMenuTagMap;
3895 @property (nonatomic, retain) IBOutlet NSMenu *graphicsMenu;
3896 @property (nonatomic, retain) IBOutlet NSMenu *commandMenu;
3897 @property (nonatomic, retain) NSDictionary *commandMenuTagMap;
3899 - (IBAction)newGame:sender;
3900 - (IBAction)openGame:sender;
3902 - (IBAction)editFont:sender;
3903 - (IBAction)setGraphicsMode:(NSMenuItem *)sender;
3904 - (IBAction)toggleSound:(NSMenuItem *)sender;
3906 - (IBAction)setRefreshRate:(NSMenuItem *)menuItem;
3907 - (IBAction)selectWindow: (id)sender;
3911 @implementation AngbandAppDelegate
3913 @synthesize graphicsMenu=_graphicsMenu;
3914 @synthesize commandMenu=_commandMenu;
3915 @synthesize commandMenuTagMap=_commandMenuTagMap;
3917 - (IBAction)newGame:sender
3919 /* Game is in progress */
3920 game_in_progress = TRUE;
3924 - (IBAction)editFont:sender
3926 NSFontPanel *panel = [NSFontPanel sharedFontPanel];
3927 NSFont *termFont = default_font;
3930 for (i=0; i < ANGBAND_TERM_MAX; i++) {
3931 if ([(id)angband_term[i]->data isMainWindow]) {
3932 termFont = [(id)angband_term[i]->data selectionFont];
3937 [panel setPanelFont:termFont isMultiple:NO];
3938 [panel orderFront:self];
3942 * Implent NSObject's changeFont() method to receive a notification about the
3943 * changed font. Note that, as of 10.14, changeFont() is deprecated in
3944 * NSObject - it will be removed at some point and the application delegate
3945 * will have to be declared as implementing the NSFontChanging protocol.
3947 - (void)changeFont:(id)sender
3950 for (mainTerm=0; mainTerm < ANGBAND_TERM_MAX; mainTerm++) {
3951 if ([(id)angband_term[mainTerm]->data isMainWindow]) {
3956 /* Bug #1709: Only change font for angband windows */
3957 if (mainTerm == ANGBAND_TERM_MAX) return;
3959 NSFont *oldFont = default_font;
3960 NSFont *newFont = [sender convertFont:oldFont];
3961 if (! newFont) return; /*paranoia */
3963 /* Store as the default font if we changed the first term */
3964 if (mainTerm == 0) {
3966 [default_font release];
3967 default_font = newFont;
3970 /* Record it in the preferences */
3971 NSUserDefaults *defs = [NSUserDefaults angbandDefaults];
3972 [defs setValue:[newFont fontName]
3973 forKey:[NSString stringWithFormat:@"FontName-%d", mainTerm]];
3974 [defs setFloat:[newFont pointSize]
3975 forKey:[NSString stringWithFormat:@"FontSize-%d", mainTerm]];
3978 NSDisableScreenUpdates();
3981 AngbandContext *angbandContext = angband_term[mainTerm]->data;
3982 [(id)angbandContext setSelectionFont:newFont adjustTerminal: YES];
3984 NSEnableScreenUpdates();
3987 - (IBAction)openGame:sender
3989 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
3990 BOOL selectedSomething = NO;
3993 /* Get where we think the save files are */
3994 NSURL *startingDirectoryURL = [NSURL fileURLWithPath:[NSString stringWithCString:ANGBAND_DIR_SAVE encoding:NSASCIIStringEncoding] isDirectory:YES];
3996 /* Set up an open panel */
3997 NSOpenPanel* panel = [NSOpenPanel openPanel];
3998 [panel setCanChooseFiles:YES];
3999 [panel setCanChooseDirectories:NO];
4000 [panel setResolvesAliases:YES];
4001 [panel setAllowsMultipleSelection:NO];
4002 [panel setTreatsFilePackagesAsDirectories:YES];
4003 [panel setDirectoryURL:startingDirectoryURL];
4006 panelResult = [panel runModal];
4007 if (panelResult == NSOKButton)
4009 NSArray* fileURLs = [panel URLs];
4010 if ([fileURLs count] > 0 && [[fileURLs objectAtIndex:0] isFileURL])
4012 NSURL* savefileURL = (NSURL *)[fileURLs objectAtIndex:0];
4013 /* The path property doesn't do the right thing except for
4014 * URLs with the file scheme. We had getFileSystemRepresentation
4015 * here before, but that wasn't introduced until OS X 10.9. */
4016 selectedSomething = [[savefileURL path] getCString:savefile
4017 maxLength:sizeof savefile encoding:NSMacOSRomanStringEncoding];
4021 if (selectedSomething)
4023 /* Remember this so we can select it by default next time */
4024 record_current_savefile();
4026 /* Game is in progress */
4027 game_in_progress = TRUE;
4034 - (IBAction)saveGame:sender
4036 /* Hack -- Forget messages */
4040 do_cmd_save_game(FALSE);
4042 /* Record the current save file so we can select it by default next time.
4043 * It's a little sketchy that this only happens when we save through the
4044 * menu; ideally game-triggered saves would trigger it too. */
4045 record_current_savefile();
4049 * Implement NSObject's validateMenuItem() method to override enabling or
4050 * disabling a menu item. Note that, as of 10.14, validateMenuItem() is
4051 * deprecated in NSObject - it will be removed at some point and the
4052 * application delegate will have to be declared as implementing the
4053 * NSMenuItemValidation protocol.
4055 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
4057 SEL sel = [menuItem action];
4058 NSInteger tag = [menuItem tag];
4060 if( tag >= AngbandWindowMenuItemTagBase && tag < AngbandWindowMenuItemTagBase + ANGBAND_TERM_MAX )
4062 if( tag == AngbandWindowMenuItemTagBase )
4064 /* The main window should always be available and visible */
4069 NSInteger subwindowNumber = tag - AngbandWindowMenuItemTagBase;
4070 return (window_flag[subwindowNumber] > 0);
4076 if (sel == @selector(newGame:))
4078 return ! game_in_progress;
4080 else if (sel == @selector(editFont:))
4084 else if (sel == @selector(openGame:))
4086 return ! game_in_progress;
4088 else if (sel == @selector(setRefreshRate:) && [superitem(menuItem) tag] == 150)
4090 NSInteger fps = [[NSUserDefaults standardUserDefaults] integerForKey:AngbandFrameRateDefaultsKey];
4091 [menuItem setState: ([menuItem tag] == fps)];
4094 else if( sel == @selector(setGraphicsMode:) )
4096 NSInteger requestedGraphicsMode = [[NSUserDefaults standardUserDefaults] integerForKey:AngbandGraphicsDefaultsKey];
4097 [menuItem setState: (tag == requestedGraphicsMode)];
4100 else if( sel == @selector(toggleSound:) )
4102 BOOL is_on = [[NSUserDefaults standardUserDefaults]
4103 boolForKey:AngbandSoundDefaultsKey];
4105 [menuItem setState: ((is_on) ? NSOnState : NSOffState)];
4108 else if( sel == @selector(sendAngbandCommand:) )
4110 /* we only want to be able to send commands during an active game */
4111 return !!game_in_progress;
4117 - (IBAction)setRefreshRate:(NSMenuItem *)menuItem
4119 frames_per_second = [menuItem tag];
4120 [[NSUserDefaults angbandDefaults] setInteger:frames_per_second forKey:AngbandFrameRateDefaultsKey];
4123 - (IBAction)selectWindow: (id)sender
4125 NSInteger subwindowNumber = [(NSMenuItem *)sender tag] - AngbandWindowMenuItemTagBase;
4126 AngbandContext *context = angband_term[subwindowNumber]->data;
4127 [context->primaryWindow makeKeyAndOrderFront: self];
4128 [context saveWindowVisibleToDefaults: YES];
4131 - (void)prepareWindowsMenu
4133 /* Get the window menu with default items and add a separator and item for
4134 * the main window */
4135 NSMenu *windowsMenu = [[NSApplication sharedApplication] windowsMenu];
4136 [windowsMenu addItem: [NSMenuItem separatorItem]];
4138 NSString *title1 = [NSString stringWithCString:angband_term_name[0]
4140 encoding:NSJapaneseEUCStringEncoding
4142 encoding:NSMacOSRomanStringEncoding
4145 NSMenuItem *angbandItem = [[NSMenuItem alloc] initWithTitle:title1 action: @selector(selectWindow:) keyEquivalent: @"0"];
4146 [angbandItem setTarget: self];
4147 [angbandItem setTag: AngbandWindowMenuItemTagBase];
4148 [windowsMenu addItem: angbandItem];
4149 [angbandItem release];
4151 /* Add items for the additional term windows */
4152 for( NSInteger i = 1; i < ANGBAND_TERM_MAX; i++ )
4154 NSString *title = [NSString stringWithCString:angband_term_name[i]
4156 encoding:NSJapaneseEUCStringEncoding
4158 encoding:NSMacOSRomanStringEncoding
4161 NSString *keyEquivalent = [NSString stringWithFormat: @"%ld", (long)i];
4162 NSMenuItem *windowItem = [[NSMenuItem alloc] initWithTitle: title action: @selector(selectWindow:) keyEquivalent: keyEquivalent];
4163 [windowItem setTarget: self];
4164 [windowItem setTag: AngbandWindowMenuItemTagBase + i];
4165 [windowsMenu addItem: windowItem];
4166 [windowItem release];
4170 - (IBAction)setGraphicsMode:(NSMenuItem *)sender
4172 /* We stashed the graphics mode ID in the menu item's tag */
4173 graf_mode_req = [sender tag];
4175 /* Stash it in UserDefaults */
4176 [[NSUserDefaults angbandDefaults] setInteger:graf_mode_req forKey:AngbandGraphicsDefaultsKey];
4177 [[NSUserDefaults angbandDefaults] synchronize];
4179 if (game_in_progress)
4181 /* Hack -- Force redraw */
4184 /* Wake up the event loop so it notices the change */
4185 wakeup_event_loop();
4189 - (IBAction) toggleSound: (NSMenuItem *) sender
4191 BOOL is_on = (sender.state == NSOnState);
4193 /* Toggle the state and update the Angband global and preferences. */
4194 sender.state = (is_on) ? NSOffState : NSOnState;
4195 use_sound = (is_on) ? FALSE : TRUE;
4196 [[NSUserDefaults angbandDefaults] setBool:(! is_on)
4197 forKey:AngbandSoundDefaultsKey];
4201 * Send a command to Angband via a menu item. This places the appropriate key
4202 * down events into the queue so that it seems like the user pressed them
4203 * (instead of trying to use the term directly).
4205 - (void)sendAngbandCommand: (id)sender
4207 NSMenuItem *menuItem = (NSMenuItem *)sender;
4208 NSString *command = [self.commandMenuTagMap objectForKey: [NSNumber numberWithInteger: [menuItem tag]]];
4209 NSInteger windowNumber = [((AngbandContext *)angband_term[0]->data)->primaryWindow windowNumber];
4211 /* Send a \ to bypass keymaps */
4212 NSEvent *escape = [NSEvent keyEventWithType: NSKeyDown
4213 location: NSZeroPoint
4216 windowNumber: windowNumber
4219 charactersIgnoringModifiers: @"\\"
4222 [[NSApplication sharedApplication] postEvent: escape atStart: NO];
4224 /* Send the actual command (from the original command set) */
4225 NSEvent *keyDown = [NSEvent keyEventWithType: NSKeyDown
4226 location: NSZeroPoint
4229 windowNumber: windowNumber
4232 charactersIgnoringModifiers: command
4235 [[NSApplication sharedApplication] postEvent: keyDown atStart: NO];
4239 * Set up the command menu dynamically, based on CommandMenu.plist.
4241 - (void)prepareCommandMenu
4243 NSString *commandMenuPath = [[NSBundle mainBundle] pathForResource: @"CommandMenu" ofType: @"plist"];
4244 NSArray *commandMenuItems = [[NSArray alloc] initWithContentsOfFile: commandMenuPath];
4245 NSMutableDictionary *angbandCommands = [[NSMutableDictionary alloc] init];
4246 NSString *tblname = @"CommandMenu";
4247 NSInteger tagOffset = 0;
4249 for( NSDictionary *item in commandMenuItems )
4251 BOOL useShiftModifier = [[item valueForKey: @"ShiftModifier"] boolValue];
4252 BOOL useOptionModifier = [[item valueForKey: @"OptionModifier"] boolValue];
4253 NSUInteger keyModifiers = NSCommandKeyMask;
4254 keyModifiers |= (useShiftModifier) ? NSShiftKeyMask : 0;
4255 keyModifiers |= (useOptionModifier) ? NSAlternateKeyMask : 0;
4257 NSString *lookup = [item valueForKey: @"Title"];
4258 NSString *title = NSLocalizedStringWithDefaultValue(
4259 lookup, tblname, [NSBundle mainBundle], lookup, @"");
4260 NSString *key = [item valueForKey: @"KeyEquivalent"];
4261 NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle: title action: @selector(sendAngbandCommand:) keyEquivalent: key];
4262 [menuItem setTarget: self];
4263 [menuItem setKeyEquivalentModifierMask: keyModifiers];
4264 [menuItem setTag: AngbandCommandMenuItemTagBase + tagOffset];
4265 [self.commandMenu addItem: menuItem];
4268 NSString *angbandCommand = [item valueForKey: @"AngbandCommand"];
4269 [angbandCommands setObject: angbandCommand forKey: [NSNumber numberWithInteger: [menuItem tag]]];
4273 [commandMenuItems release];
4275 NSDictionary *safeCommands = [[NSDictionary alloc] initWithDictionary: angbandCommands];
4276 self.commandMenuTagMap = safeCommands;
4277 [safeCommands release];
4278 [angbandCommands release];
4281 - (void)awakeFromNib
4283 [super awakeFromNib];
4285 [self prepareWindowsMenu];
4286 [self prepareCommandMenu];
4289 - (void)applicationDidFinishLaunching:sender
4291 [AngbandContext beginGame];
4293 /* Once beginGame finished, the game is over - that's how Angband works,
4294 * and we should quit */
4295 game_is_finished = TRUE;
4296 [NSApp terminate:self];
4299 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
4301 if (p_ptr->playing == FALSE || game_is_finished == TRUE)
4303 return NSTerminateNow;
4305 else if (! inkey_flag)
4307 /* For compatibility with other ports, do not quit in this case */
4308 return NSTerminateCancel;
4313 /* player->upkeep->playing = FALSE; */
4315 /* Post an escape event so that we can return from our get-key-event
4317 wakeup_event_loop();
4318 quit_when_ready = true;
4319 /* Must return Cancel, not Later, because we need to get out of the
4320 * run loop and back to Angband's loop */
4321 return NSTerminateCancel;
4326 * Dynamically build the Graphics menu
4328 - (void)menuNeedsUpdate:(NSMenu *)menu {
4330 /* Only the graphics menu is dynamic */
4331 if (! [menu isEqual:self.graphicsMenu])
4334 /* If it's non-empty, then we've already built it. Currently graphics modes
4335 * won't change once created; if they ever can we can remove this check.
4336 * Note that the check mark does change, but that's handled in
4337 * validateMenuItem: instead of menuNeedsUpdate: */
4338 if ([menu numberOfItems] > 0)
4341 /* This is the action for all these menu items */
4342 SEL action = @selector(setGraphicsMode:);
4344 /* Add an initial Classic ASCII menu item */
4345 NSString *tblname = @"GraphicsMenu";
4346 NSString *key = @"Classic ASCII";
4347 NSString *title = NSLocalizedStringWithDefaultValue(
4348 key, tblname, [NSBundle mainBundle], key, @"");
4349 NSMenuItem *classicItem = [menu addItemWithTitle:title action:action keyEquivalent:@""];
4350 [classicItem setTag:GRAPHICS_NONE];
4352 /* Walk through the list of graphics modes */
4353 if (graphics_modes) {
4356 for (i=0; graphics_modes[i].pNext; i++)
4358 const graphics_mode *graf = &graphics_modes[i];
4360 if (graf->grafID == GRAPHICS_NONE) {
4363 /* Make the title. NSMenuItem throws on a nil title, so ensure it's
4365 key = [[NSString alloc] initWithUTF8String:graf->menuname];
4366 title = NSLocalizedStringWithDefaultValue(
4367 key, tblname, [NSBundle mainBundle], key, @"");
4370 NSMenuItem *item = [menu addItemWithTitle:title action:action keyEquivalent:@""];
4372 [item setTag:graf->grafID];
4378 * Delegate method that gets called if we're asked to open a file.
4380 - (BOOL)application:(NSApplication *)sender openFiles:(NSArray *)filenames
4382 /* Can't open a file once we've started */
4383 if (game_in_progress) return NO;
4385 /* We can only open one file. Use the last one. */
4386 NSString *file = [filenames lastObject];
4387 if (! file) return NO;
4389 /* Put it in savefile */
4390 if (! [file getFileSystemRepresentation:savefile maxLength:sizeof savefile])
4393 game_in_progress = TRUE;
4396 /* Wake us up in case this arrives while we're sitting at the Welcome
4398 wakeup_event_loop();
4405 int main(int argc, char* argv[])
4407 NSApplicationMain(argc, (void*)argv);
4411 #endif /* MACINTOSH || MACH_O_COCOA */