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/AppDelegate.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 NSString * const AngbandDirectoryNameLib = @"lib";
36 static NSString * const AngbandDirectoryNameBase = @"Hengband";
38 static NSString * const AngbandMessageCatalog = @"Localizable";
39 static NSString * const AngbandTerminalsDefaultsKey = @"Terminals";
40 static NSString * const AngbandTerminalRowsDefaultsKey = @"Rows";
41 static NSString * const AngbandTerminalColumnsDefaultsKey = @"Columns";
42 static NSString * const AngbandTerminalVisibleDefaultsKey = @"Visible";
43 static NSString * const AngbandGraphicsDefaultsKey = @"GraphicsID";
44 static NSString * const AngbandBigTileDefaultsKey = @"UseBigTiles";
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, fontAscender,
338 * fontDescender, ncol_pre, and ncol_post (the glyphArray and glyphWidths
339 * members of AngbandContext are only used in updateGlyphInfo()). The
340 * rendering in drawWChar will work for glyphs not in updateGlyphInfo()'s
341 * set, and that is used for rendering Japanese characters.
343 #define GLYPH_COUNT 256
345 /* An AngbandContext represents a logical Term (i.e. what Angband thinks is
346 * a window). This typically maps to one NSView, but may map to more than one
347 * NSView (e.g. the Test and real screen saver view). */
348 @interface AngbandContext : NSObject <NSWindowDelegate>
352 /* The Angband term */
355 /* Column and row cont, by default 80 x 24 */
359 /* The size of the border between the window edge and the contents */
362 /* Our array of views */
363 NSMutableArray *angbandViews;
365 /* The buffered image */
366 CGLayerRef angbandLayer;
368 /* The font of this context */
369 NSFont *angbandViewFont;
371 /* If this context owns a window, here it is */
372 NSWindow *primaryWindow;
374 /* "Glyph info": an array of the CGGlyphs and their widths corresponding to
376 CGGlyph glyphArray[GLYPH_COUNT];
377 CGFloat glyphWidths[GLYPH_COUNT];
379 /* The size of one tile */
382 /* Font's ascender and descender */
383 CGFloat fontAscender, fontDescender;
385 /* Whether we are currently in live resize, which affects how big we render
389 /* Last time we drew, so we can throttle drawing */
390 CFAbsoluteTime lastRefreshTime;
392 struct PendingChanges* changes;
394 * These are the number of columns before or after, respectively, a text
395 * change that may need to be redrawn.
397 int ncol_pre, ncol_post;
399 /* Flags whether or not a fullscreen transition is in progress. */
400 BOOL in_fullscreen_transition;
404 BOOL _hasSubwindowFlags;
405 BOOL _windowVisibilityChecked;
408 @property (nonatomic, assign) BOOL hasSubwindowFlags;
409 @property (nonatomic, assign) BOOL windowVisibilityChecked;
411 - (void)drawRect:(NSRect)rect inView:(NSView *)view;
413 /* Called at initialization to set the term */
414 - (void)setTerm:(term *)t;
416 /* Called when the context is going down. */
419 /* Returns the size of the image. */
422 /* Return the rect for a tile at given coordinates. */
423 - (NSRect)rectInImageForTileAtX:(int)x Y:(int)y;
425 /* Draw the given wide character into the given tile rect. */
426 - (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile context:(CGContextRef)ctx;
428 /* Locks focus on the Angband image, and scales the CTM appropriately. */
429 - (CGContextRef)lockFocus;
431 /* Locks focus on the Angband image but does NOT scale the CTM. Appropriate
432 * for drawing hairlines. */
433 - (CGContextRef)lockFocusUnscaled;
438 /* Returns the primary window for this angband context, creating it if
440 - (NSWindow *)makePrimaryWindow;
442 /* Called to add a new Angband view */
443 - (void)addAngbandView:(AngbandView *)view;
445 /* Make the context aware that one of its views changed size */
446 - (void)angbandViewDidScale:(AngbandView *)view;
448 /* Handle becoming the main window */
449 - (void)windowDidBecomeMain:(NSNotification *)notification;
451 /* Return whether the context's primary window is ordered in or not */
454 /* Return whether the context's primary window is key */
455 - (BOOL)isMainWindow;
457 /* Invalidate the whole image */
458 - (void)setNeedsDisplay:(BOOL)val;
460 /* Invalidate part of the image, with the rect expressed in base coordinates */
461 - (void)setNeedsDisplayInBaseRect:(NSRect)rect;
463 /* Display (flush) our Angband views */
464 - (void)displayIfNeeded;
466 /* Resize context to size of contentRect, and optionally save size to
468 - (void)resizeTerminalWithContentRect: (NSRect)contentRect saveToDefaults: (BOOL)saveToDefaults;
471 * Change the minimum size for the window associated with the context.
472 * termIdx is the index for the terminal: pass it so this function can be
473 * used when self->terminal has not yet been set.
475 - (void)setMinimumWindowSize:(int)termIdx;
477 /* Called from the view to indicate that it is starting or ending live resize */
478 - (void)viewWillStartLiveResize:(AngbandView *)view;
479 - (void)viewDidEndLiveResize:(AngbandView *)view;
480 - (void)saveWindowVisibleToDefaults: (BOOL)windowVisible;
481 - (BOOL)windowVisibleUsingDefaults;
483 /* Internal method */
484 - (AngbandView *)activeView;
489 * Generate a mask for the subwindow flags. The mask is just a safety check to
490 * make sure that our windows show and hide as expected. This function allows
491 * for future changes to the set of flags without needed to update it here
492 * (unless the underlying types change).
494 u32b AngbandMaskForValidSubwindowFlags(void)
496 int windowFlagBits = sizeof(*(window_flag)) * CHAR_BIT;
497 int maxBits = MIN( 16, windowFlagBits );
500 for( int i = 0; i < maxBits; i++ )
502 if( window_flag_desc[i] != NULL )
512 * Check for changes in the subwindow flags and update window visibility.
513 * This seems to be called for every user event, so we don't
514 * want to do any unnecessary hiding or showing of windows.
516 static void AngbandUpdateWindowVisibility(void)
518 /* Because this function is called frequently, we'll make the mask static.
519 * It doesn't change between calls, as the flags themselves are hardcoded */
520 static u32b validWindowFlagsMask = 0;
522 if( validWindowFlagsMask == 0 )
524 validWindowFlagsMask = AngbandMaskForValidSubwindowFlags();
527 /* Loop through all of the subwindows and see if there is a change in the
528 * flags. If so, show or hide the corresponding window. We don't care about
529 * the flags themselves; we just want to know if any are set. */
530 for( int i = 1; i < ANGBAND_TERM_MAX; i++ )
532 AngbandContext *angbandContext = angband_term[i]->data;
534 if( angbandContext == nil )
539 /* This horrible mess of flags is so that we can try to maintain some
540 * user visibility preference. This should allow the user a window and
541 * have it stay closed between application launches. However, this
542 * means that when a subwindow is turned on, it will no longer appear
543 * automatically. Angband has no concept of user control over window
544 * visibility, other than the subwindow flags. */
545 if( !angbandContext.windowVisibilityChecked )
547 if( [angbandContext windowVisibleUsingDefaults] )
549 [angbandContext->primaryWindow orderFront: nil];
550 angbandContext.windowVisibilityChecked = YES;
554 [angbandContext->primaryWindow close];
555 angbandContext.windowVisibilityChecked = NO;
560 BOOL termHasSubwindowFlags = ((window_flag[i] & validWindowFlagsMask) > 0);
562 if( angbandContext.hasSubwindowFlags && !termHasSubwindowFlags )
564 [angbandContext->primaryWindow close];
565 angbandContext.hasSubwindowFlags = NO;
566 [angbandContext saveWindowVisibleToDefaults: NO];
568 else if( !angbandContext.hasSubwindowFlags && termHasSubwindowFlags )
570 [angbandContext->primaryWindow orderFront: nil];
571 angbandContext.hasSubwindowFlags = YES;
572 [angbandContext saveWindowVisibleToDefaults: YES];
577 /* Make the main window key so that user events go to the right spot */
578 AngbandContext *mainWindow = angband_term[0]->data;
579 [mainWindow->primaryWindow makeKeyAndOrderFront: nil];
583 * ------------------------------------------------------------------------
585 * ------------------------------------------------------------------------ */
590 static CGImageRef pict_image;
593 * Numbers of rows and columns in a tileset,
594 * calculated by the PICT/PNG loading code
596 static int pict_cols = 0;
597 static int pict_rows = 0;
600 * Requested graphics mode (as a grafID).
601 * The current mode is stored in current_graphics_mode.
603 static int graf_mode_req = 0;
606 * Helper function to check the various ways that graphics can be enabled,
607 * guarding against NULL
609 static BOOL graphics_are_enabled(void)
611 return current_graphics_mode
612 && current_graphics_mode->grafID != GRAPHICS_NONE;
616 * Hack -- game in progress
618 static Boolean game_in_progress = FALSE;
621 #pragma mark Prototypes
622 static void wakeup_event_loop(void);
623 static void hook_plog(const char *str);
624 static void hook_quit(const char * str);
625 static NSString* get_lib_directory(void);
626 static NSString* get_doc_directory(void);
627 static NSString* AngbandCorrectedDirectoryPath(NSString *originalPath);
628 static void prepare_paths_and_directories(void);
629 static void load_prefs(void);
630 static void load_sounds(void);
631 static void init_windows(void);
632 static void handle_open_when_ready(void);
633 static void play_sound(int event);
634 static BOOL check_events(int wait);
635 static BOOL send_event(NSEvent *event);
636 static void record_current_savefile(void);
638 static wchar_t convert_two_byte_eucjp_to_utf16_native(const char *cp);
642 * Available values for 'wait'
644 #define CHECK_EVENTS_DRAIN -1
645 #define CHECK_EVENTS_NO_WAIT 0
646 #define CHECK_EVENTS_WAIT 1
650 * Note when "open"/"new" become valid
652 static bool initialized = FALSE;
654 /* Methods for getting the appropriate NSUserDefaults */
655 @interface NSUserDefaults (AngbandDefaults)
656 + (NSUserDefaults *)angbandDefaults;
659 @implementation NSUserDefaults (AngbandDefaults)
660 + (NSUserDefaults *)angbandDefaults
662 return [NSUserDefaults standardUserDefaults];
666 /* Methods for pulling images out of the Angband bundle (which may be separate
667 * from the current bundle in the case of a screensaver */
668 @interface NSImage (AngbandImages)
669 + (NSImage *)angbandImage:(NSString *)name;
672 /* The NSView subclass that draws our Angband image */
673 @interface AngbandView : NSView
675 AngbandContext *angbandContext;
678 - (void)setAngbandContext:(AngbandContext *)context;
679 - (AngbandContext *)angbandContext;
683 @implementation NSImage (AngbandImages)
685 /* Returns an image in the resource directoy of the bundle containing the
686 * Angband view class. */
687 + (NSImage *)angbandImage:(NSString *)name
689 NSBundle *bundle = [NSBundle bundleForClass:[AngbandView class]];
690 NSString *path = [bundle pathForImageResource:name];
692 if (path) result = [[[NSImage alloc] initByReferencingFile:path] autorelease];
700 @implementation AngbandContext
702 @synthesize hasSubwindowFlags=_hasSubwindowFlags;
703 @synthesize windowVisibilityChecked=_windowVisibilityChecked;
705 - (NSFont *)selectionFont
707 return angbandViewFont;
710 - (BOOL)useLiveResizeOptimization
712 /* If we have graphics turned off, text rendering is fast enough that we
713 * don't need to use a live resize optimization. */
714 return inLiveResize && graphics_are_enabled();
719 /* We round the base size down. If we round it up, I believe we may end up
720 * with pixels that nobody "owns" that may accumulate garbage. In general
721 * rounding down is harmless, because any lost pixels may be sopped up by
723 return NSMakeSize(floor(cols * tileSize.width + 2 * borderSize.width), floor(rows * tileSize.height + 2 * borderSize.height));
726 /* qsort-compatible compare function for CGSizes */
727 static int compare_advances(const void *ap, const void *bp)
729 const CGSize *a = ap, *b = bp;
730 return (a->width > b->width) - (a->width < b->width);
733 - (void)updateGlyphInfo
735 /* Update glyphArray and glyphWidths */
736 NSFont *screenFont = [angbandViewFont screenFont];
738 /* Generate a string containing each MacRoman character */
739 unsigned char latinString[GLYPH_COUNT];
741 for (i=0; i < GLYPH_COUNT; i++) latinString[i] = (unsigned char)i;
743 /* Turn that into unichar. Angband uses ISO Latin 1. */
744 unichar unicharString[GLYPH_COUNT] = {0};
745 NSString *allCharsString = [[NSString alloc] initWithBytes:latinString length:sizeof latinString encoding:NSISOLatin1StringEncoding];
746 [allCharsString getCharacters:unicharString range:NSMakeRange(0, MIN(GLYPH_COUNT, [allCharsString length]))];
747 [allCharsString autorelease];
750 memset(glyphArray, 0, sizeof glyphArray);
751 CTFontGetGlyphsForCharacters((CTFontRef)screenFont, unicharString, glyphArray, GLYPH_COUNT);
753 /* Get advances. Record the max advance. */
754 CGSize advances[GLYPH_COUNT] = {};
755 CTFontGetAdvancesForGlyphs((CTFontRef)screenFont, kCTFontHorizontalOrientation, glyphArray, advances, GLYPH_COUNT);
756 for (i=0; i < GLYPH_COUNT; i++) {
757 glyphWidths[i] = advances[i].width;
760 /* For good non-mono-font support, use the median advance. Start by sorting
762 qsort(advances, GLYPH_COUNT, sizeof *advances, compare_advances);
764 /* Skip over any initially empty run */
766 for (startIdx = 0; startIdx < GLYPH_COUNT; startIdx++)
768 if (advances[startIdx].width > 0) break;
771 /* Pick the center to find the median */
772 CGFloat medianAdvance = 0;
773 if (startIdx < GLYPH_COUNT)
775 /* In case we have all zero advances for some reason */
776 medianAdvance = advances[(startIdx + GLYPH_COUNT)/2].width;
780 * Record the ascender and descender. Some fonts, for instance DIN
781 * Condensed and Rockwell in 10.14, the ascent on '@' exceeds that
782 * reported by [screenFont ascender]. Get the overall bounding box
783 * for the glyphs and use that instead of the ascender and descender
784 * values if the bounding box result extends farther from the baseline.
786 CGRect bounds = CTFontGetBoundingRectsForGlyphs((CTFontRef) screenFont, kCTFontHorizontalOrientation, glyphArray, NULL, GLYPH_COUNT);
787 fontAscender = [screenFont ascender];
788 if (fontAscender < bounds.origin.y + bounds.size.height) {
789 fontAscender = bounds.origin.y + bounds.size.height;
791 fontDescender = [screenFont descender];
792 if (fontDescender > bounds.origin.y) {
793 fontDescender = bounds.origin.y;
797 * Record the tile size. Round both values up to have tile boundaries
798 * match pixel boundaries.
800 tileSize.width = ceil(medianAdvance);
801 tileSize.height = ceil(fontAscender - fontDescender);
804 * Determine whether neighboring columns need to redrawn when a character
807 CGRect boxes[GLYPH_COUNT] = {};
808 CGFloat beyond_right = 0.;
809 CGFloat beyond_left = 0.;
810 CTFontGetBoundingRectsForGlyphs(
811 (CTFontRef)screenFont,
812 kCTFontHorizontalOrientation,
816 for (i = 0; i < GLYPH_COUNT; i++) {
817 /* Account for the compression and offset used by drawWChar(). */
818 CGFloat compression, offset;
821 if (glyphWidths[i] <= tileSize.width) {
823 offset = 0.5 * (tileSize.width - glyphWidths[i]);
825 compression = tileSize.width / glyphWidths[i];
828 v = (offset + boxes[i].origin.x) * compression;
829 if (beyond_left > v) {
832 v = (offset + boxes[i].origin.x + boxes[i].size.width) * compression;
833 if (beyond_right < v) {
837 ncol_pre = ceil(-beyond_left / tileSize.width);
838 if (beyond_right > tileSize.width) {
839 ncol_post = ceil((beyond_right - tileSize.width) / tileSize.width);
847 NSSize size = NSMakeSize(1, 1);
849 AngbandView *activeView = [self activeView];
852 /* If we are in live resize, draw as big as the screen, so we can scale
853 * nicely to any size. If we are not in live resize, then use the
854 * bounds of the active view. */
856 if ([self useLiveResizeOptimization] && (screen = [[activeView window] screen]) != NULL)
858 size = [screen frame].size;
862 size = [activeView bounds].size;
866 CGLayerRelease(angbandLayer);
868 /* Use the highest monitor scale factor on the system to work out what
869 * scale to draw at - not the recommended method, but works where we
870 * can't easily get the monitor the current draw is occurring on. */
871 float angbandLayerScale = 1.0;
872 if ([[NSScreen mainScreen] respondsToSelector:@selector(backingScaleFactor)]) {
873 for (NSScreen *screen in [NSScreen screens]) {
874 angbandLayerScale = fmax(angbandLayerScale, [screen backingScaleFactor]);
878 /* Make a bitmap context as an example for our layer */
879 CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
880 CGContextRef exampleCtx = CGBitmapContextCreate(NULL, 1, 1, 8 /* bits per component */, 48 /* bytesPerRow */, cs, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host);
881 CGColorSpaceRelease(cs);
883 /* Create the layer at the appropriate size */
884 size.width = fmax(1, ceil(size.width * angbandLayerScale));
885 size.height = fmax(1, ceil(size.height * angbandLayerScale));
886 angbandLayer = CGLayerCreateWithContext(exampleCtx, *(CGSize *)&size, NULL);
888 CFRelease(exampleCtx);
890 /* Set the new context of the layer to draw at the correct scale */
891 CGContextRef ctx = CGLayerGetContext(angbandLayer);
892 CGContextScaleCTM(ctx, angbandLayerScale, angbandLayerScale);
895 [[NSColor blackColor] set];
896 NSRectFill((NSRect){NSZeroPoint, [self baseSize]});
900 - (void)requestRedraw
902 if (! self->terminal) return;
906 /* Activate the term */
907 Term_activate(self->terminal);
909 /* Redraw the contents */
912 /* Flush the output */
915 /* Restore the old term */
919 - (void)setTerm:(term *)t
924 - (void)viewWillStartLiveResize:(AngbandView *)view
926 #if USE_LIVE_RESIZE_CACHE
927 if (inLiveResize < INT_MAX) inLiveResize++;
928 else [NSException raise:NSInternalInconsistencyException format:@"inLiveResize overflow"];
930 if (inLiveResize == 1 && 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];
940 - (void)viewDidEndLiveResize:(AngbandView *)view
942 #if USE_LIVE_RESIZE_CACHE
943 if (inLiveResize > 0) inLiveResize--;
944 else [NSException raise:NSInternalInconsistencyException format:@"inLiveResize underflow"];
946 if (inLiveResize == 0 && graphics_are_enabled())
950 [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
951 [self requestRedraw];
957 * If we're trying to limit ourselves to a certain number of frames per second,
958 * then compute how long it's been since we last drew, and then wait until the
959 * next frame has passed. */
962 if (frames_per_second > 0)
964 CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
965 CFTimeInterval timeSinceLastRefresh = now - lastRefreshTime;
966 CFTimeInterval timeUntilNextRefresh = (1. / (double)frames_per_second) - timeSinceLastRefresh;
968 if (timeUntilNextRefresh > 0)
970 usleep((unsigned long)(timeUntilNextRefresh * 1000000.));
973 lastRefreshTime = CFAbsoluteTimeGetCurrent();
976 - (void)drawWChar:(wchar_t)wchar inRect:(NSRect)tile context:(CGContextRef)ctx
978 CGFloat tileOffsetY = fontAscender;
979 CGFloat tileOffsetX = 0.0;
980 NSFont *screenFont = [angbandViewFont screenFont];
981 UniChar unicharString[2] = {(UniChar)wchar, 0};
983 /* Get glyph and advance */
984 CGGlyph thisGlyphArray[1] = { 0 };
985 CGSize advances[1] = { { 0, 0 } };
986 CTFontGetGlyphsForCharacters((CTFontRef)screenFont, unicharString, thisGlyphArray, 1);
987 CGGlyph glyph = thisGlyphArray[0];
988 CTFontGetAdvancesForGlyphs((CTFontRef)screenFont, kCTFontHorizontalOrientation, thisGlyphArray, advances, 1);
989 CGSize advance = advances[0];
991 /* If our font is not monospaced, our tile width is deliberately not big
992 * enough for every character. In that event, if our glyph is too wide, we
993 * need to compress it horizontally. Compute the compression ratio.
994 * 1.0 means no compression. */
995 double compressionRatio;
996 if (advance.width <= NSWidth(tile))
998 /* Our glyph fits, so we can just draw it, possibly with an offset */
999 compressionRatio = 1.0;
1000 tileOffsetX = (NSWidth(tile) - advance.width)/2;
1004 /* Our glyph doesn't fit, so we'll have to compress it */
1005 compressionRatio = NSWidth(tile) / advance.width;
1011 CGAffineTransform textMatrix = CGContextGetTextMatrix(ctx);
1012 CGFloat savedA = textMatrix.a;
1014 /* Set the position */
1015 textMatrix.tx = tile.origin.x + tileOffsetX;
1016 textMatrix.ty = tile.origin.y + tileOffsetY;
1018 /* Maybe squish it horizontally. */
1019 if (compressionRatio != 1.)
1021 textMatrix.a *= compressionRatio;
1024 textMatrix = CGAffineTransformScale( textMatrix, 1.0, -1.0 );
1025 CGContextSetTextMatrix(ctx, textMatrix);
1026 CGContextShowGlyphsAtPositions(ctx, &glyph, &CGPointZero, 1);
1028 /* Restore the text matrix if we messed with the compression ratio */
1029 if (compressionRatio != 1.)
1031 textMatrix.a = savedA;
1032 CGContextSetTextMatrix(ctx, textMatrix);
1035 textMatrix = CGAffineTransformScale( textMatrix, 1.0, -1.0 );
1036 CGContextSetTextMatrix(ctx, textMatrix);
1039 /* Lock and unlock focus on our image or layer, setting up the CTM
1041 - (CGContextRef)lockFocusUnscaled
1043 /* Create an NSGraphicsContext representing this CGLayer */
1044 CGContextRef ctx = CGLayerGetContext(angbandLayer);
1045 NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:ctx flipped:NO];
1046 [NSGraphicsContext saveGraphicsState];
1047 [NSGraphicsContext setCurrentContext:context];
1048 CGContextSaveGState(ctx);
1054 /* Restore the graphics state */
1055 CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];
1056 CGContextRestoreGState(ctx);
1057 [NSGraphicsContext restoreGraphicsState];
1062 /* Return the size of our layer */
1063 CGSize result = CGLayerGetSize(angbandLayer);
1064 return NSMakeSize(result.width, result.height);
1067 - (CGContextRef)lockFocus
1069 return [self lockFocusUnscaled];
1073 - (NSRect)rectInImageForTileAtX:(int)x Y:(int)y
1076 return NSMakeRect(x * tileSize.width + borderSize.width, flippedY * tileSize.height + borderSize.height, tileSize.width, tileSize.height);
1079 - (void)setSelectionFont:(NSFont*)font adjustTerminal: (BOOL)adjustTerminal
1081 /* Record the new font */
1083 [angbandViewFont release];
1084 angbandViewFont = font;
1086 /* Update our glyph info */
1087 [self updateGlyphInfo];
1089 if( adjustTerminal )
1091 /* Adjust terminal to fit window with new font; save the new columns
1092 * and rows since they could be changed */
1093 NSRect contentRect = [self->primaryWindow contentRectForFrameRect: [self->primaryWindow frame]];
1095 [self setMinimumWindowSize:[self terminalIndex]];
1096 NSSize size = self->primaryWindow.contentMinSize;
1097 BOOL windowNeedsResizing = NO;
1098 if (contentRect.size.width < size.width) {
1099 contentRect.size.width = size.width;
1100 windowNeedsResizing = YES;
1102 if (contentRect.size.height < size.height) {
1103 contentRect.size.height = size.height;
1104 windowNeedsResizing = YES;
1106 if (windowNeedsResizing) {
1107 size.width = contentRect.size.width;
1108 size.height = contentRect.size.height;
1109 [self->primaryWindow setContentSize:size];
1111 [self resizeTerminalWithContentRect: contentRect saveToDefaults: YES];
1114 /* Update our image */
1120 if ((self = [super init]))
1122 /* Default rows and cols */
1126 /* Default border size */
1127 self->borderSize = NSMakeSize(2, 2);
1129 /* Allocate our array of views */
1130 angbandViews = [[NSMutableArray alloc] init];
1132 self->changes = create_pending_changes(self->cols, self->rows);
1133 if (self->changes == 0) {
1134 NSLog(@"AngbandContext init: out of memory for pending changes");
1137 self->ncol_post = 0;
1139 self->in_fullscreen_transition = NO;
1141 /* Make the image. Since we have no views, it'll just be a puny 1x1 image. */
1144 _windowVisibilityChecked = NO;
1150 * Destroy all the receiver's stuff. This is intended to be callable more than
1157 /* Disassociate ourselves from our angbandViews */
1158 [angbandViews makeObjectsPerformSelector:@selector(setAngbandContext:) withObject:nil];
1159 [angbandViews release];
1162 /* Destroy the layer/image */
1163 CGLayerRelease(angbandLayer);
1164 angbandLayer = NULL;
1167 [angbandViewFont release];
1168 angbandViewFont = nil;
1171 [primaryWindow setDelegate:nil];
1172 [primaryWindow close];
1173 [primaryWindow release];
1174 primaryWindow = nil;
1176 /* Pending changes */
1177 destroy_pending_changes(self->changes);
1181 /* Usual Cocoa fare */
1189 /* From the Linux mbstowcs(3) man page:
1190 * If dest is NULL, n is ignored, and the conversion proceeds as above,
1191 * except that the converted wide characters are not written out to mem‐
1192 * ory, and that no length limit exists.
1194 static size_t Term_mbcs_cocoa(wchar_t *dest, const char *src, int n)
1199 /* Unicode code point to UTF-8
1200 * 0x0000-0x007f: 0xxxxxxx
1201 * 0x0080-0x07ff: 110xxxxx 10xxxxxx
1202 * 0x0800-0xffff: 1110xxxx 10xxxxxx 10xxxxxx
1203 * 0x10000-0x1fffff: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
1204 * Note that UTF-16 limits Unicode to 0x10ffff. This code is not
1207 for (i = 0; i < n || dest == NULL; i++) {
1208 if ((src[i] & 0x80) == 0) {
1209 if (dest != NULL) dest[count] = src[i];
1210 if (src[i] == 0) break;
1211 } else if ((src[i] & 0xe0) == 0xc0) {
1212 if (dest != NULL) dest[count] =
1213 (((unsigned char)src[i] & 0x1f) << 6)|
1214 ((unsigned char)src[i+1] & 0x3f);
1216 } else if ((src[i] & 0xf0) == 0xe0) {
1217 if (dest != NULL) dest[count] =
1218 (((unsigned char)src[i] & 0x0f) << 12) |
1219 (((unsigned char)src[i+1] & 0x3f) << 6) |
1220 ((unsigned char)src[i+2] & 0x3f);
1222 } else if ((src[i] & 0xf8) == 0xf0) {
1223 if (dest != NULL) dest[count] =
1224 (((unsigned char)src[i] & 0x0f) << 18) |
1225 (((unsigned char)src[i+1] & 0x3f) << 12) |
1226 (((unsigned char)src[i+2] & 0x3f) << 6) |
1227 ((unsigned char)src[i+3] & 0x3f);
1230 /* Found an invalid multibyte sequence */
1239 - (void)addAngbandView:(AngbandView *)view
1241 if (! [angbandViews containsObject:view])
1243 [angbandViews addObject:view];
1245 [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
1246 [self requestRedraw];
1251 * We have this notion of an "active" AngbandView, which is the largest - the
1252 * idea being that in the screen saver, when the user hits Test in System
1253 * Preferences, we don't want to keep driving the AngbandView in the
1254 * background. Our active AngbandView is the widest - that's a hack all right.
1255 * Mercifully when we're just playing the game there's only one view.
1257 - (AngbandView *)activeView
1259 if ([angbandViews count] == 1)
1260 return [angbandViews objectAtIndex:0];
1262 AngbandView *result = nil;
1264 for (AngbandView *angbandView in angbandViews)
1266 float width = [angbandView frame].size.width;
1267 if (width > maxWidth)
1270 result = angbandView;
1276 - (void)angbandViewDidScale:(AngbandView *)view
1278 /* If we're live-resizing with graphics, we're using the live resize
1279 * optimization, so don't update the image. Otherwise do it. */
1280 if (! (inLiveResize && graphics_are_enabled()) && view == [self activeView])
1284 [self setNeedsDisplay:YES]; /*we'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
1285 [self requestRedraw];
1290 - (void)removeAngbandView:(AngbandView *)view
1292 if ([angbandViews containsObject:view])
1294 [angbandViews removeObject:view];
1296 [self setNeedsDisplay:YES]; /* We'll need to redisplay everything anyways, so avoid creating all those little redisplay rects */
1297 if ([angbandViews count]) [self requestRedraw];
1302 - (NSWindow *)makePrimaryWindow
1304 if (! primaryWindow)
1306 /* This has to be done after the font is set, which it already is in
1307 * term_init_cocoa() */
1308 CGFloat width = self->cols * tileSize.width + borderSize.width * 2.0;
1309 CGFloat height = self->rows * tileSize.height + borderSize.height * 2.0;
1310 NSRect contentRect = NSMakeRect( 0.0, 0.0, width, height );
1312 NSUInteger styleMask = NSTitledWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask;
1314 /* Make every window other than the main window closable */
1315 if( angband_term[0]->data != self )
1317 styleMask |= NSClosableWindowMask;
1320 primaryWindow = [[NSWindow alloc] initWithContentRect:contentRect styleMask: styleMask backing:NSBackingStoreBuffered defer:YES];
1322 /* Not to be released when closed */
1323 [primaryWindow setReleasedWhenClosed:NO];
1324 [primaryWindow setExcludedFromWindowsMenu: YES]; /* we're using custom window menu handling */
1327 AngbandView *angbandView = [[AngbandView alloc] initWithFrame:contentRect];
1328 [angbandView setAngbandContext:self];
1329 [angbandViews addObject:angbandView];
1330 [primaryWindow setContentView:angbandView];
1331 [angbandView release];
1333 /* We are its delegate */
1334 [primaryWindow setDelegate:self];
1336 /* Update our image, since this is probably the first angband view
1340 return primaryWindow;
1345 #pragma mark View/Window Passthrough
1348 * This is what our views call to get us to draw to the window
1350 - (void)drawRect:(NSRect)rect inView:(NSView *)view
1352 /* Take this opportunity to throttle so we don't flush faster than desired.
1354 BOOL viewInLiveResize = [view inLiveResize];
1355 if (! viewInLiveResize) [self throttle];
1357 /* With a GLayer, use CGContextDrawLayerInRect */
1358 CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
1359 NSRect bounds = [view bounds];
1360 if (viewInLiveResize) CGContextSetInterpolationQuality(context, kCGInterpolationLow);
1361 CGContextSetBlendMode(context, kCGBlendModeCopy);
1362 CGContextDrawLayerInRect(context, *(CGRect *)&bounds, angbandLayer);
1363 if (viewInLiveResize) CGContextSetInterpolationQuality(context, kCGInterpolationDefault);
1368 return [[[angbandViews lastObject] window] isVisible];
1371 - (BOOL)isMainWindow
1373 return [[[angbandViews lastObject] window] isMainWindow];
1376 - (void)setNeedsDisplay:(BOOL)val
1378 for (NSView *angbandView in angbandViews)
1380 [angbandView setNeedsDisplay:val];
1384 - (void)setNeedsDisplayInBaseRect:(NSRect)rect
1386 for (NSView *angbandView in angbandViews)
1388 [angbandView setNeedsDisplayInRect: rect];
1392 - (void)displayIfNeeded
1394 [[self activeView] displayIfNeeded];
1397 - (int)terminalIndex
1401 for( termIndex = 0; termIndex < ANGBAND_TERM_MAX; termIndex++ )
1403 if( angband_term[termIndex] == self->terminal )
1412 - (void)resizeTerminalWithContentRect: (NSRect)contentRect saveToDefaults: (BOOL)saveToDefaults
1414 CGFloat newRows = floor( (contentRect.size.height - (borderSize.height * 2.0)) / tileSize.height );
1415 CGFloat newColumns = ceil( (contentRect.size.width - (borderSize.width * 2.0)) / tileSize.width );
1417 if (newRows < 1 || newColumns < 1) return;
1418 self->cols = newColumns;
1419 self->rows = newRows;
1421 if (resize_pending_changes(self->changes, self->rows) != 0) {
1422 destroy_pending_changes(self->changes);
1424 NSLog(@"out of memory for pending changes with resize of terminal %d",
1425 [self terminalIndex]);
1428 if( saveToDefaults )
1430 int termIndex = [self terminalIndex];
1431 NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
1433 if( termIndex < (int)[terminals count] )
1435 NSMutableDictionary *mutableTerm = [[NSMutableDictionary alloc] initWithDictionary: [terminals objectAtIndex: termIndex]];
1436 [mutableTerm setValue: [NSNumber numberWithUnsignedInt: self->cols] forKey: AngbandTerminalColumnsDefaultsKey];
1437 [mutableTerm setValue: [NSNumber numberWithUnsignedInt: self->rows] forKey: AngbandTerminalRowsDefaultsKey];
1439 NSMutableArray *mutableTerminals = [[NSMutableArray alloc] initWithArray: terminals];
1440 [mutableTerminals replaceObjectAtIndex: termIndex withObject: mutableTerm];
1442 [[NSUserDefaults standardUserDefaults] setValue: mutableTerminals forKey: AngbandTerminalsDefaultsKey];
1443 [mutableTerminals release];
1444 [mutableTerm release];
1446 [[NSUserDefaults standardUserDefaults] synchronize];
1450 Term_activate( self->terminal );
1451 Term_resize( (int)newColumns, (int)newRows);
1453 Term_activate( old );
1456 - (void)setMinimumWindowSize:(int)termIdx
1462 minsize.height = 24;
1468 minsize.width * self->tileSize.width + self->borderSize.width * 2.0;
1470 minsize.height * self->tileSize.height + self->borderSize.height * 2.0;
1471 [[self makePrimaryWindow] setContentMinSize:minsize];
1474 - (void)saveWindowVisibleToDefaults: (BOOL)windowVisible
1476 int termIndex = [self terminalIndex];
1477 BOOL safeVisibility = (termIndex == 0) ? YES : windowVisible; /* Ensure main term doesn't go away because of these defaults */
1478 NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
1480 if( termIndex < (int)[terminals count] )
1482 NSMutableDictionary *mutableTerm = [[NSMutableDictionary alloc] initWithDictionary: [terminals objectAtIndex: termIndex]];
1483 [mutableTerm setValue: [NSNumber numberWithBool: safeVisibility] forKey: AngbandTerminalVisibleDefaultsKey];
1485 NSMutableArray *mutableTerminals = [[NSMutableArray alloc] initWithArray: terminals];
1486 [mutableTerminals replaceObjectAtIndex: termIndex withObject: mutableTerm];
1488 [[NSUserDefaults standardUserDefaults] setValue: mutableTerminals forKey: AngbandTerminalsDefaultsKey];
1489 [mutableTerminals release];
1490 [mutableTerm release];
1494 - (BOOL)windowVisibleUsingDefaults
1496 int termIndex = [self terminalIndex];
1498 if( termIndex == 0 )
1503 NSArray *terminals = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
1506 if( termIndex < (int)[terminals count] )
1508 NSDictionary *term = [terminals objectAtIndex: termIndex];
1509 NSNumber *visibleValue = [term valueForKey: AngbandTerminalVisibleDefaultsKey];
1511 if( visibleValue != nil )
1513 visible = [visibleValue boolValue];
1521 #pragma mark NSWindowDelegate Methods
1523 /*- (void)windowWillStartLiveResize: (NSNotification *)notification
1527 - (void)windowDidEndLiveResize: (NSNotification *)notification
1529 NSWindow *window = [notification object];
1530 NSRect contentRect = [window contentRectForFrameRect: [window frame]];
1531 [self resizeTerminalWithContentRect: contentRect saveToDefaults: !(self->in_fullscreen_transition)];
1534 /*- (NSSize)windowWillResize: (NSWindow *)sender toSize: (NSSize)frameSize
1538 - (void)windowWillEnterFullScreen: (NSNotification *)notification
1540 self->in_fullscreen_transition = YES;
1543 - (void)windowDidEnterFullScreen: (NSNotification *)notification
1545 NSWindow *window = [notification object];
1546 NSRect contentRect = [window contentRectForFrameRect: [window frame]];
1547 self->in_fullscreen_transition = NO;
1548 [self resizeTerminalWithContentRect: contentRect saveToDefaults: NO];
1551 - (void)windowWillExitFullScreen: (NSNotification *)notification
1553 self->in_fullscreen_transition = YES;
1556 - (void)windowDidExitFullScreen: (NSNotification *)notification
1558 NSWindow *window = [notification object];
1559 NSRect contentRect = [window contentRectForFrameRect: [window frame]];
1560 self->in_fullscreen_transition = NO;
1561 [self resizeTerminalWithContentRect: contentRect saveToDefaults: NO];
1564 - (void)windowDidBecomeMain:(NSNotification *)notification
1566 NSWindow *window = [notification object];
1568 if( window != self->primaryWindow )
1573 int termIndex = [self terminalIndex];
1574 NSMenuItem *item = [[[NSApplication sharedApplication] windowsMenu] itemWithTag: AngbandWindowMenuItemTagBase + termIndex];
1575 [item setState: NSOnState];
1577 if( [[NSFontPanel sharedFontPanel] isVisible] )
1579 [[NSFontPanel sharedFontPanel] setPanelFont: [self selectionFont] isMultiple: NO];
1583 - (void)windowDidResignMain: (NSNotification *)notification
1585 NSWindow *window = [notification object];
1587 if( window != self->primaryWindow )
1592 int termIndex = [self terminalIndex];
1593 NSMenuItem *item = [[[NSApplication sharedApplication] windowsMenu] itemWithTag: AngbandWindowMenuItemTagBase + termIndex];
1594 [item setState: NSOffState];
1597 - (void)windowWillClose: (NSNotification *)notification
1599 [self saveWindowVisibleToDefaults: NO];
1605 @implementation AngbandView
1617 - (void)drawRect:(NSRect)rect
1619 if (! angbandContext)
1621 /* Draw bright orange, 'cause this ain't right */
1622 [[NSColor orangeColor] set];
1623 NSRectFill([self bounds]);
1627 /* Tell the Angband context to draw into us */
1628 [angbandContext drawRect:rect inView:self];
1632 - (void)setAngbandContext:(AngbandContext *)context
1634 angbandContext = context;
1637 - (AngbandContext *)angbandContext
1639 return angbandContext;
1642 - (void)setFrameSize:(NSSize)size
1644 BOOL changed = ! NSEqualSizes(size, [self frame].size);
1645 [super setFrameSize:size];
1646 if (changed) [angbandContext angbandViewDidScale:self];
1649 - (void)viewWillStartLiveResize
1651 [angbandContext viewWillStartLiveResize:self];
1654 - (void)viewDidEndLiveResize
1656 [angbandContext viewDidEndLiveResize:self];
1662 * Delay handling of double-clicked savefiles
1664 Boolean open_when_ready = FALSE;
1669 * ------------------------------------------------------------------------
1670 * Some generic functions
1671 * ------------------------------------------------------------------------ */
1674 * Sets an Angband color at a given index
1676 static void set_color_for_index(int idx)
1680 /* Extract the R,G,B data */
1681 rv = angband_color_table[idx][1];
1682 gv = angband_color_table[idx][2];
1683 bv = angband_color_table[idx][3];
1685 CGContextSetRGBFillColor([[NSGraphicsContext currentContext] graphicsPort], rv/255., gv/255., bv/255., 1.);
1689 * Remember the current character in UserDefaults so we can select it by
1690 * default next time.
1692 static void record_current_savefile(void)
1694 NSString *savefileString = [[NSString stringWithCString:savefile encoding:NSMacOSRomanStringEncoding] lastPathComponent];
1697 NSUserDefaults *angbandDefs = [NSUserDefaults angbandDefaults];
1698 [angbandDefs setObject:savefileString forKey:@"SaveFile"];
1699 [angbandDefs synchronize];
1706 * Convert a two-byte EUC-JP encoded character (both *cp and (*cp + 1) are in
1707 * the range, 0xA1-0xFE, or *cp is 0x8E) to a utf16 value in the native byte
1710 static wchar_t convert_two_byte_eucjp_to_utf16_native(const char *cp)
1712 NSString* str = [[NSString alloc] initWithBytes:cp length:2
1713 encoding:NSJapaneseEUCStringEncoding];
1714 wchar_t result = [str characterAtIndex:0];
1723 * ------------------------------------------------------------------------
1724 * Support for the "z-term.c" package
1725 * ------------------------------------------------------------------------ */
1729 * Initialize a new Term
1731 static void Term_init_cocoa(term *t)
1733 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1734 AngbandContext *context = [[AngbandContext alloc] init];
1736 /* Give the term a hard retain on context (for GC) */
1737 t->data = (void *)CFRetain(context);
1740 /* Handle graphics */
1741 t->higher_pict = !! use_graphics;
1742 t->always_pict = FALSE;
1744 NSDisableScreenUpdates();
1746 /* Figure out the frame autosave name based on the index of this term */
1747 NSString *autosaveName = nil;
1749 for (termIdx = 0; termIdx < ANGBAND_TERM_MAX; termIdx++)
1751 if (angband_term[termIdx] == t)
1753 autosaveName = [NSString stringWithFormat:@"AngbandTerm-%d", termIdx];
1759 NSString *fontName = [[NSUserDefaults angbandDefaults] stringForKey:[NSString stringWithFormat:@"FontName-%d", termIdx]];
1760 if (! fontName) fontName = [default_font fontName];
1762 /* Use a smaller default font for the other windows, but only if the font
1763 * hasn't been explicitly set */
1764 float fontSize = (termIdx > 0) ? 10.0 : [default_font pointSize];
1765 NSNumber *fontSizeNumber = [[NSUserDefaults angbandDefaults] valueForKey: [NSString stringWithFormat: @"FontSize-%d", termIdx]];
1767 if( fontSizeNumber != nil )
1769 fontSize = [fontSizeNumber floatValue];
1772 [context setSelectionFont:[NSFont fontWithName:fontName size:fontSize] adjustTerminal: NO];
1774 NSArray *terminalDefaults = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
1775 NSInteger rows = 24;
1776 NSInteger columns = 80;
1778 if( termIdx < (int)[terminalDefaults count] )
1780 NSDictionary *term = [terminalDefaults objectAtIndex: termIdx];
1781 NSInteger defaultRows = [[term valueForKey: AngbandTerminalRowsDefaultsKey] integerValue];
1782 NSInteger defaultColumns = [[term valueForKey: AngbandTerminalColumnsDefaultsKey] integerValue];
1784 if (defaultRows > 0) rows = defaultRows;
1785 if (defaultColumns > 0) columns = defaultColumns;
1788 context->cols = columns;
1789 context->rows = rows;
1791 if (resize_pending_changes(context->changes, context->rows) != 0) {
1792 destroy_pending_changes(context->changes);
1793 context->changes = 0;
1794 NSLog(@"initializing terminal %d: out of memory for pending changes",
1798 /* Get the window */
1799 NSWindow *window = [context makePrimaryWindow];
1801 /* Set its title and, for auxiliary terms, tentative size */
1802 NSString *title = [NSString stringWithCString:angband_term_name[termIdx]
1804 encoding:NSJapaneseEUCStringEncoding
1806 encoding:NSMacOSRomanStringEncoding
1809 [window setTitle:title];
1810 [context setMinimumWindowSize:termIdx];
1812 /* If this is the first term, and we support full screen (Mac OS X Lion or
1813 * later), then allow it to go full screen (sweet). Allow other terms to be
1814 * FullScreenAuxilliary, so they can at least show up. Unfortunately in
1815 * Lion they don't get brought to the full screen space; but they would
1816 * only make sense on multiple displays anyways so it's not a big loss. */
1817 if ([window respondsToSelector:@selector(toggleFullScreen:)])
1819 NSWindowCollectionBehavior behavior = [window collectionBehavior];
1820 behavior |= (termIdx == 0 ? Angband_NSWindowCollectionBehaviorFullScreenPrimary : Angband_NSWindowCollectionBehaviorFullScreenAuxiliary);
1821 [window setCollectionBehavior:behavior];
1824 /* No Resume support yet, though it would not be hard to add */
1825 if ([window respondsToSelector:@selector(setRestorable:)])
1827 [window setRestorable:NO];
1830 /* default window placement */ {
1831 static NSRect overallBoundingRect;
1835 /* This is a bit of a trick to allow us to display multiple windows
1836 * in the "standard default" window position in OS X: the upper
1837 * center of the screen.
1838 * The term sizes set in load_prefs() are based on a 5-wide by
1839 * 3-high grid, with the main term being 4/5 wide by 2/3 high
1840 * (hence the scaling to find */
1842 /* What the containing rect would be). */
1843 NSRect originalMainTermFrame = [window frame];
1844 NSRect scaledFrame = originalMainTermFrame;
1845 scaledFrame.size.width *= 5.0 / 4.0;
1846 scaledFrame.size.height *= 3.0 / 2.0;
1847 scaledFrame.size.width += 1.0; /* spacing between window columns */
1848 scaledFrame.size.height += 1.0; /* spacing between window rows */
1849 [window setFrame: scaledFrame display: NO];
1851 overallBoundingRect = [window frame];
1852 [window setFrame: originalMainTermFrame display: NO];
1855 static NSRect mainTermBaseRect;
1856 NSRect windowFrame = [window frame];
1860 /* The height and width adjustments were determined experimentally,
1861 * so that the rest of the windows line up nicely without
1863 windowFrame.size.width += 7.0;
1864 windowFrame.size.height += 9.0;
1865 windowFrame.origin.x = NSMinX( overallBoundingRect );
1866 windowFrame.origin.y = NSMaxY( overallBoundingRect ) - NSHeight( windowFrame );
1867 mainTermBaseRect = windowFrame;
1869 else if( termIdx == 1 )
1871 windowFrame.origin.x = NSMinX( mainTermBaseRect );
1872 windowFrame.origin.y = NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
1874 else if( termIdx == 2 )
1876 windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
1877 windowFrame.origin.y = NSMaxY( mainTermBaseRect ) - NSHeight( windowFrame );
1879 else if( termIdx == 3 )
1881 windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
1882 windowFrame.origin.y = NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
1884 else if( termIdx == 4 )
1886 windowFrame.origin.x = NSMaxX( mainTermBaseRect ) + 1.0;
1887 windowFrame.origin.y = NSMinY( mainTermBaseRect );
1889 else if( termIdx == 5 )
1891 windowFrame.origin.x = NSMinX( mainTermBaseRect ) + NSWidth( windowFrame ) + 1.0;
1892 windowFrame.origin.y = NSMinY( mainTermBaseRect ) - NSHeight( windowFrame ) - 1.0;
1895 [window setFrame: windowFrame display: NO];
1898 /* Override the default frame above if the user has adjusted windows in
1900 if (autosaveName) [window setFrameAutosaveName:autosaveName];
1902 /* Tell it about its term. Do this after we've sized it so that the sizing
1903 * doesn't trigger redrawing and such. */
1904 [context setTerm:t];
1906 /* Only order front if it's the first term. Other terms will be ordered
1907 * front from AngbandUpdateWindowVisibility(). This is to work around a
1908 * problem where Angband aggressively tells us to initialize terms that
1909 * don't do anything! */
1910 if (t == angband_term[0]) [context->primaryWindow makeKeyAndOrderFront: nil];
1912 NSEnableScreenUpdates();
1914 /* Set "mapped" flag */
1915 t->mapped_flag = true;
1924 static void Term_nuke_cocoa(term *t)
1926 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1928 AngbandContext *context = t->data;
1931 /* Tell the context to get rid of its windows, etc. */
1934 /* Balance our CFRetain from when we created it */
1945 * Returns the CGImageRef corresponding to an image with the given name in the
1946 * resource directory, transferring ownership to the caller
1948 static CGImageRef create_angband_image(NSString *path)
1950 CGImageRef decodedImage = NULL, result = NULL;
1952 /* Try using ImageIO to load the image */
1955 NSURL *url = [[NSURL alloc] initFileURLWithPath:path isDirectory:NO];
1958 NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:(id)kCFBooleanTrue, kCGImageSourceShouldCache, nil];
1959 CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, (CFDictionaryRef)options);
1962 /* We really want the largest image, but in practice there's
1963 * only going to be one */
1964 decodedImage = CGImageSourceCreateImageAtIndex(source, 0, (CFDictionaryRef)options);
1972 /* Draw the sucker to defeat ImageIO's weird desire to cache and decode on
1973 * demand. Our images aren't that big! */
1976 size_t width = CGImageGetWidth(decodedImage), height = CGImageGetHeight(decodedImage);
1978 /* Compute our own bitmap info */
1979 CGBitmapInfo imageBitmapInfo = CGImageGetBitmapInfo(decodedImage);
1980 CGBitmapInfo contextBitmapInfo = kCGBitmapByteOrderDefault;
1982 switch (imageBitmapInfo & kCGBitmapAlphaInfoMask) {
1983 case kCGImageAlphaNone:
1984 case kCGImageAlphaNoneSkipLast:
1985 case kCGImageAlphaNoneSkipFirst:
1987 contextBitmapInfo |= kCGImageAlphaNone;
1990 /* Some alpha, use premultiplied last which is most efficient. */
1991 contextBitmapInfo |= kCGImageAlphaPremultipliedLast;
1995 /* Draw the source image flipped, since the view is flipped */
1996 CGContextRef ctx = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(decodedImage), CGImageGetBytesPerRow(decodedImage), CGImageGetColorSpace(decodedImage), contextBitmapInfo);
1998 CGContextSetBlendMode(ctx, kCGBlendModeCopy);
1999 CGContextTranslateCTM(ctx, 0.0, height);
2000 CGContextScaleCTM(ctx, 1.0, -1.0);
2002 ctx, CGRectMake(0, 0, width, height), decodedImage);
2003 result = CGBitmapContextCreateImage(ctx);
2007 CGImageRelease(decodedImage);
2015 static errr Term_xtra_cocoa_react(void)
2017 /* Don't actually switch graphics until the game is running */
2018 if (!initialized || !game_in_progress) return (-1);
2020 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2021 AngbandContext *angbandContext = Term->data;
2023 /* Handle graphics */
2024 int expected_graf_mode = (current_graphics_mode) ?
2025 current_graphics_mode->grafID : GRAPHICS_NONE;
2026 if (graf_mode_req != expected_graf_mode)
2028 graphics_mode *new_mode;
2029 if (graf_mode_req != GRAPHICS_NONE) {
2030 new_mode = get_graphics_mode(graf_mode_req);
2035 /* Get rid of the old image. CGImageRelease is NULL-safe. */
2036 CGImageRelease(pict_image);
2039 /* Try creating the image if we want one */
2040 if (new_mode != NULL)
2042 NSString *img_path = [NSString stringWithFormat:@"%s/%s", new_mode->path, new_mode->file];
2043 pict_image = create_angband_image(img_path);
2045 /* If we failed to create the image, revert to ASCII. */
2049 arg_bigtile = FALSE;
2051 [[NSUserDefaults angbandDefaults]
2052 setInteger:GRAPHICS_NONE
2053 forKey:AngbandGraphicsDefaultsKey];
2054 [[NSUserDefaults angbandDefaults] synchronize];
2056 NSString *msg = NSLocalizedStringWithDefaultValue(
2057 @"Error.TileSetLoadFailed",
2058 AngbandMessageCatalog,
2059 [NSBundle mainBundle],
2060 @"Failed to Load Tile Set",
2061 @"Alert text for failed tile set load");
2062 NSString *info = NSLocalizedStringWithDefaultValue(
2063 @"Error.TileSetRevertToASCII",
2064 AngbandMessageCatalog,
2065 [NSBundle mainBundle],
2066 @"Could not load the tile set. Switched back to ASCII.",
2067 @"Alert informative message for failed tile set load");
2068 NSAlert *alert = [[NSAlert alloc] init];
2070 alert.messageText = msg;
2071 alert.informativeText = info;
2072 NSModalResponse result = [alert runModal];
2077 /* Record what we did */
2078 use_graphics = new_mode ? new_mode->grafID : 0;
2079 ANGBAND_GRAF = (new_mode ? new_mode->graf : "ascii");
2080 current_graphics_mode = new_mode;
2082 /* Enable or disable higher picts. Note: this should be done for all
2084 angbandContext->terminal->higher_pict = !! use_graphics;
2086 if (pict_image && current_graphics_mode)
2088 /* Compute the row and column count via the image height and width.
2090 pict_rows = (int)(CGImageGetHeight(pict_image) / current_graphics_mode->cell_height);
2091 pict_cols = (int)(CGImageGetWidth(pict_image) / current_graphics_mode->cell_width);
2100 if (arg_bigtile == use_bigtile)
2106 if (arg_bigtile != use_bigtile) {
2110 Term_activate(angband_term[0]);
2111 Term_resize(angband_term[0]->wid, angband_term[0]->hgt);
2122 * Draws one tile as a helper function for Term_xtra_cocoa_fresh().
2124 static void draw_image_tile(
2125 NSGraphicsContext* nsContext,
2126 CGContextRef cgContext,
2130 NSCompositingOperation op)
2132 /* Flip the source rect since the source image is flipped */
2133 CGAffineTransform flip = CGAffineTransformIdentity;
2134 flip = CGAffineTransformTranslate(flip, 0.0, CGImageGetHeight(image));
2135 flip = CGAffineTransformScale(flip, 1.0, -1.0);
2136 CGRect flippedSourceRect =
2137 CGRectApplyAffineTransform(NSRectToCGRect(srcRect), flip);
2140 * When we use high-quality resampling to draw a tile, pixels from outside
2141 * the tile may bleed in, causing graphics artifacts. Work around that.
2143 CGImageRef subimage =
2144 CGImageCreateWithImageInRect(image, flippedSourceRect);
2145 [nsContext setCompositingOperation:op];
2146 CGContextDrawImage(cgContext, NSRectToCGRect(dstRect), subimage);
2147 CGImageRelease(subimage);
2152 * This is a helper function for Term_xtra_cocoa_fresh(): look before a block
2153 * of text on a row to see if the bounds for rendering and clipping need to be
2156 static void query_before_text(
2157 struct PendingRowChange* prc, int iy, int npre, int* pclip, int* prend)
2163 if (i < 0 || i < start - npre) {
2167 if (prc->cell_changes[i].change_type == CELL_CHANGE_PICT) {
2169 * The cell has been rendered with a tile. Do not want to modify
2170 * its contents so the clipping and rendering region can not be
2174 } else if (prc->cell_changes[i].change_type == CELL_CHANGE_NONE) {
2176 * It has not changed (or using big tile mode and it is within
2177 * a changed tile but is not the left cell for that tile) so
2178 * inquire what it is.
2183 Term_what(i, iy, a + 1, c + 1);
2184 if (use_graphics && (a[1] & 0x80) && (c[1] & 0x80)) {
2186 * It is an unchanged location rendered with a tile. Do not
2187 * want to modify its contents so the clipping and rendering
2188 * region can not be extended.
2192 if (use_bigtile && i > 0) {
2193 Term_what(i - 1, iy, a, c);
2194 if (use_graphics && (a[0] & 0x80) && (c[0] & 0x80)) {
2196 * It is the right cell of a location rendered with a tile.
2197 * Do not want to modify its contents so the clipping and
2198 * rendering region can not be exteded.
2204 * It is unchanged text. A character from the changed region
2205 * may have extended into it so render it to clear that.
2208 /* Check to see if it is the second part of a kanji character. */
2210 Term_what(i - 1, iy, a, c);
2212 prc->cell_changes[i - 1].c.w =
2213 convert_two_byte_eucjp_to_utf16_native(c);
2214 prc->cell_changes[i - 1].a = a[0];
2215 prc->cell_changes[i - 1].tcol = 1;
2216 prc->cell_changes[i].c.w = 0;
2217 prc->cell_changes[i].a = a[0];
2218 prc->cell_changes[i].tcol = 0;
2223 prc->cell_changes[i].c.w = c[1];
2224 prc->cell_changes[i].a = a[1];
2225 prc->cell_changes[i].tcol = 0;
2230 prc->cell_changes[i].c.w = c[1];
2231 prc->cell_changes[i].a = a[1];
2232 prc->cell_changes[i].tcol = 0;
2237 prc->cell_changes[i].c.w = c[1];
2238 prc->cell_changes[i].a = a[1];
2239 prc->cell_changes[i].tcol = 0;
2246 * The cell has been wiped or had changed text rendered. Do
2247 * not need to render. Can extend the clipping rectangle into it.
2257 * This is a helper function for Term_xtra_cocoa_fresh(): look after a block
2258 * of text on a row to see if the bounds for rendering and clipping need to be
2261 static void query_after_text(
2262 struct PendingRowChange* prc,
2274 * Be willing to consolidate this block with the one after it. This
2275 * logic should be sufficient to avoid redraws of the region between
2276 * changed blocks of text if angbandContext->ncol_pre is zero or one.
2277 * For larger values of ncol_pre, would need to do something more to
2278 * avoid extra redraws.
2282 prc->cell_changes[i].change_type != CELL_CHANGE_TEXT &&
2283 prc->cell_changes[i].change_type != CELL_CHANGE_WIPE)) {
2287 if (prc->cell_changes[i].change_type == CELL_CHANGE_PICT) {
2289 * The cell has been rendered with a tile. Do not want to modify
2290 * its contents so the clipping and rendering region can not be
2294 } else if (prc->cell_changes[i].change_type == CELL_CHANGE_NONE) {
2295 /* It has not changed so inquire what it is. */
2299 Term_what(i, iy, a, c);
2300 if (use_graphics && (a[0] & 0x80) && (c[0] & 0x80)) {
2302 * It is an unchanged location rendered with a tile. Do not
2303 * want to modify its contents so the clipping and rendering
2304 * region can not be extended.
2309 * It is unchanged text. A character from the changed region
2310 * may have extended into it so render it to clear that.
2313 /* Check to see if it is the first part of a kanji character. */
2315 Term_what(i + 1, iy, a + 1, c + 1);
2317 prc->cell_changes[i].c.w =
2318 convert_two_byte_eucjp_to_utf16_native(c);
2319 prc->cell_changes[i].a = a[0];
2320 prc->cell_changes[i].tcol = 1;
2321 prc->cell_changes[i + 1].c.w = 0;
2322 prc->cell_changes[i + 1].a = a[0];
2323 prc->cell_changes[i + 1].tcol = 0;
2328 prc->cell_changes[i].c.w = c[0];
2329 prc->cell_changes[i].a = a[0];
2330 prc->cell_changes[i].tcol = 0;
2335 prc->cell_changes[i].c.w = c[0];
2336 prc->cell_changes[i].a = a[0];
2337 prc->cell_changes[i].tcol = 0;
2342 prc->cell_changes[i].c.w = c[0];
2343 prc->cell_changes[i].a = a[0];
2344 prc->cell_changes[i].tcol = 0;
2351 * Have come to another region of changed text or another region
2352 * to wipe. Combine the regions to minimize redraws.
2364 * Draw the pending changes saved in angbandContext->changes.
2366 static void Term_xtra_cocoa_fresh(AngbandContext* angbandContext)
2368 int graf_width, graf_height, alphablend;
2370 if (angbandContext->changes->has_pict) {
2371 CGImageAlphaInfo ainfo = CGImageGetAlphaInfo(pict_image);
2373 graf_width = current_graphics_mode->cell_width;
2374 graf_height = current_graphics_mode->cell_height;
2376 * As of this writing, a value of zero for
2377 * current_graphics_mode->alphablend can mean either that the tile set
2378 * doesn't have an alpha channel or it does but it only takes on values
2379 * of 0 or 255. For main-cocoa.m's purposes, the latter is rendered
2380 * using the same procedure as if alphablend was nonzero. The former
2381 * is handled differently, but alphablend doesn't distinguish it from
2382 * the latter. So ignore alphablend and directly test whether an
2383 * alpha channel is present.
2385 alphablend = (ainfo & (kCGImageAlphaPremultipliedFirst |
2386 kCGImageAlphaPremultipliedLast)) ? 1 : 0;
2393 CGContextRef ctx = [angbandContext lockFocus];
2395 if (angbandContext->changes->has_text ||
2396 angbandContext->changes->has_wipe) {
2397 NSFont *selectionFont = [[angbandContext selectionFont] screenFont];
2398 [selectionFont set];
2402 for (iy = angbandContext->changes->ymin;
2403 iy <= angbandContext->changes->ymax;
2405 struct PendingRowChange* prc = angbandContext->changes->rows[iy];
2408 /* Skip untouched rows. */
2417 if (ix > prc->xmax) {
2421 switch (prc->cell_changes[ix].change_type) {
2422 case CELL_CHANGE_NONE:
2426 case CELL_CHANGE_PICT:
2429 * Because changes are made to the compositing mode, save
2430 * the incoming value.
2432 NSGraphicsContext *nsContext =
2433 [NSGraphicsContext currentContext];
2434 NSCompositingOperation op = nsContext.compositingOperation;
2435 int step = (use_bigtile) ? 2 : 1;
2438 while (jx <= prc->xmax &&
2439 prc->cell_changes[jx].change_type
2440 == CELL_CHANGE_PICT) {
2441 NSRect destinationRect =
2442 [angbandContext rectInImageForTileAtX:jx Y:iy];
2443 NSRect sourceRect, terrainRect;
2445 destinationRect.size.width *= step;
2446 sourceRect.origin.x = graf_width *
2447 prc->cell_changes[jx].c.c;
2448 sourceRect.origin.y = graf_height *
2449 prc->cell_changes[jx].a;
2450 sourceRect.size.width = graf_width;
2451 sourceRect.size.height = graf_height;
2452 terrainRect.origin.x = graf_width *
2453 prc->cell_changes[jx].tcol;
2454 terrainRect.origin.y = graf_height *
2455 prc->cell_changes[jx].trow;
2456 terrainRect.size.width = graf_width;
2457 terrainRect.size.height = graf_height;
2467 * Skip drawing the foreground if it is the same
2468 * as the background.
2470 if (sourceRect.origin.x != terrainRect.origin.x ||
2471 sourceRect.origin.y != terrainRect.origin.y) {
2478 NSCompositeSourceOver);
2492 [nsContext setCompositingOperation:op];
2495 [angbandContext rectInImageForTileAtX:ix Y:iy];
2497 angbandContext->tileSize.width * (jx - ix);
2498 [angbandContext setNeedsDisplayInBaseRect:rect];
2503 case CELL_CHANGE_WIPE:
2504 case CELL_CHANGE_TEXT:
2506 * For a wiped region, treat it as if it had text (the only
2507 * loss if it was not is some extra work rendering
2508 * neighboring unchanged text).
2511 while (jx < angbandContext->cols &&
2512 (prc->cell_changes[jx].change_type
2514 || prc->cell_changes[jx].change_type
2515 == CELL_CHANGE_WIPE)) {
2520 int ieclip = jx - 1;
2522 int ierend = jx - 1;
2524 TERM_COLOR alast = 0;
2529 prc, iy, angbandContext->ncol_pre, &isclip, &isrend);
2533 angbandContext->cols,
2534 angbandContext->ncol_post,
2540 /* Save the state since the clipping will be modified. */
2541 CGContextSaveGState(ctx);
2543 /* Clear the area where rendering will be done. */
2544 r = [angbandContext rectInImageForTileAtX:isrend Y:iy];
2545 r.size.width = angbandContext->tileSize.width *
2546 (ierend - isrend + 1);
2547 [[NSColor blackColor] set];
2551 * Clear the current path so it does not affect clipping.
2552 * Then set the clipping rectangle. Using
2553 * CGContextSetTextDrawingMode() to include clipping does
2554 * not appear to be necessary on 10.14 and is actually
2555 * detrimental: when displaying more than one character,
2556 * only the first is visible.
2558 CGContextBeginPath(ctx);
2559 r = [angbandContext rectInImageForTileAtX:isclip Y:iy];
2560 r.size.width = angbandContext->tileSize.width *
2561 (ieclip - isclip + 1);
2562 CGContextClipToRect(ctx, r);
2566 while (k <= ierend) {
2569 if (prc->cell_changes[k].change_type
2570 == CELL_CHANGE_WIPE) {
2571 /* Skip over since no rendering is necessary. */
2576 if (set_color || alast != prc->cell_changes[k].a) {
2578 alast = prc->cell_changes[k].a;
2579 set_color_for_index(alast % MAX_COLORS);
2583 [angbandContext rectInImageForTileAtX:k Y:iy];
2584 if (prc->cell_changes[k].tcol) {
2585 rectToDraw.size.width *= 2.0;
2586 [angbandContext drawWChar:prc->cell_changes[k].c.w
2587 inRect:rectToDraw context:ctx];
2590 [angbandContext drawWChar:prc->cell_changes[k].c.w
2591 inRect:rectToDraw context:ctx];
2597 * Inform the context that the area in the clipping
2598 * rectangle needs to be redisplayed.
2600 [angbandContext setNeedsDisplayInBaseRect:r];
2602 CGContextRestoreGState(ctx);
2609 if (angbandContext->changes->xcurs >= 0 &&
2610 angbandContext->changes->ycurs >= 0) {
2611 NSRect rect = [angbandContext
2612 rectInImageForTileAtX:angbandContext->changes->xcurs
2613 Y:angbandContext->changes->ycurs];
2615 if (angbandContext->changes->bigcurs) {
2616 rect.size.width += angbandContext->tileSize.width;
2618 [[NSColor yellowColor] set];
2619 NSFrameRectWithWidth(rect, 1);
2620 /* Invalidate that rect */
2621 [angbandContext setNeedsDisplayInBaseRect:rect];
2624 [angbandContext unlockFocus];
2629 * Do a "special thing"
2631 static errr Term_xtra_cocoa(int n, int v)
2633 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2634 AngbandContext* angbandContext = Term->data;
2642 case TERM_XTRA_NOISE:
2651 case TERM_XTRA_SOUND:
2655 /* Process random events */
2656 case TERM_XTRA_BORED:
2658 /* Show or hide cocoa windows based on the subwindow flags set by
2660 AngbandUpdateWindowVisibility();
2662 /* Process an event */
2663 (void)check_events(CHECK_EVENTS_NO_WAIT);
2669 /* Process pending events */
2670 case TERM_XTRA_EVENT:
2672 /* Process an event */
2673 (void)check_events(v);
2679 /* Flush all pending events (if any) */
2680 case TERM_XTRA_FLUSH:
2682 /* Hack -- flush all events */
2683 while (check_events(CHECK_EVENTS_DRAIN)) /* loop */;
2689 /* Hack -- Change the "soft level" */
2690 case TERM_XTRA_LEVEL:
2692 /* Here we could activate (if requested), but I don't think Angband
2693 * should be telling us our window order (the user should decide
2694 * that), so do nothing. */
2698 /* Clear the screen */
2699 case TERM_XTRA_CLEAR:
2701 [angbandContext lockFocus];
2702 [[NSColor blackColor] set];
2703 NSRect imageRect = {NSZeroPoint, [angbandContext imageSize]};
2704 NSRectFillUsingOperation(imageRect, NSCompositeCopy);
2705 [angbandContext unlockFocus];
2706 [angbandContext setNeedsDisplay:YES];
2711 /* React to changes */
2712 case TERM_XTRA_REACT:
2714 /* React to changes */
2715 return (Term_xtra_cocoa_react());
2718 /* Delay (milliseconds) */
2719 case TERM_XTRA_DELAY:
2725 double seconds = v / 1000.;
2726 NSDate* date = [NSDate dateWithTimeIntervalSinceNow:seconds];
2732 event = [NSApp nextEventMatchingMask:-1 untilDate:date inMode:NSDefaultRunLoopMode dequeue:YES];
2733 if (event) send_event(event);
2735 } while ([date timeIntervalSinceNow] >= 0);
2743 case TERM_XTRA_FRESH:
2744 /* Draw the pending changes. */
2745 if (angbandContext->changes != 0) {
2746 Term_xtra_cocoa_fresh(angbandContext);
2747 clear_pending_changes(angbandContext->changes);
2763 static errr Term_curs_cocoa(TERM_LEN x, TERM_LEN y)
2765 AngbandContext *angbandContext = Term->data;
2767 if (angbandContext->changes == 0) {
2768 /* Bail out; there was an earlier memory allocation failure. */
2771 angbandContext->changes->xcurs = x;
2772 angbandContext->changes->ycurs = y;
2773 angbandContext->changes->bigcurs = 0;
2780 * Draw a cursor that's two tiles wide. For Japanese, that's used when
2781 * the cursor points at a kanji character, irregardless of whether operating
2784 static errr Term_bigcurs_cocoa(TERM_LEN x, TERM_LEN y)
2786 AngbandContext *angbandContext = Term->data;
2788 if (angbandContext->changes == 0) {
2789 /* Bail out; there was an earlier memory allocation failure. */
2792 angbandContext->changes->xcurs = x;
2793 angbandContext->changes->ycurs = y;
2794 angbandContext->changes->bigcurs = 1;
2801 * Low level graphics (Assumes valid input)
2803 * Erase "n" characters starting at (x,y)
2805 static errr Term_wipe_cocoa(TERM_LEN x, TERM_LEN y, int n)
2807 AngbandContext *angbandContext = Term->data;
2808 struct PendingCellChange *pc;
2810 if (angbandContext->changes == 0) {
2811 /* Bail out; there was an earlier memory allocation failure. */
2814 if (angbandContext->changes->rows[y] == 0) {
2815 angbandContext->changes->rows[y] =
2816 create_row_change(angbandContext->cols);
2817 if (angbandContext->changes->rows[y] == 0) {
2818 NSLog(@"failed to allocate changes for row %d", y);
2821 if (angbandContext->changes->ymin > y) {
2822 angbandContext->changes->ymin = y;
2824 if (angbandContext->changes->ymax < y) {
2825 angbandContext->changes->ymax = y;
2829 angbandContext->changes->has_wipe = 1;
2830 if (angbandContext->changes->rows[y]->xmin > x) {
2831 angbandContext->changes->rows[y]->xmin = x;
2833 if (angbandContext->changes->rows[y]->xmax < x + n - 1) {
2834 angbandContext->changes->rows[y]->xmax = x + n - 1;
2836 for (pc = angbandContext->changes->rows[y]->cell_changes + x;
2837 pc != angbandContext->changes->rows[y]->cell_changes + x + n;
2839 pc->change_type = CELL_CHANGE_WIPE;
2846 static errr Term_pict_cocoa(TERM_LEN x, TERM_LEN y, int n,
2847 TERM_COLOR *ap, concptr cp,
2848 const TERM_COLOR *tap, concptr tcp)
2851 /* Paranoia: Bail if we don't have a current graphics mode */
2852 if (! current_graphics_mode) return -1;
2854 AngbandContext* angbandContext = Term->data;
2856 int step = (use_bigtile) ? 2 : 1;
2857 struct PendingCellChange *pc;
2859 if (angbandContext->changes == 0) {
2860 /* Bail out; there was an earlier memory allocation failure. */
2864 * In bigtile mode, it is sufficient that the bounds for the modified
2865 * region only encompass the left cell for the region affected by the
2866 * tile and that only that cell has to have the details of the changes.
2868 if (angbandContext->changes->rows[y] == 0) {
2869 angbandContext->changes->rows[y] =
2870 create_row_change(angbandContext->cols);
2871 if (angbandContext->changes->rows[y] == 0) {
2872 NSLog(@"failed to allocate changes for row %d", y);
2875 if (angbandContext->changes->ymin > y) {
2876 angbandContext->changes->ymin = y;
2878 if (angbandContext->changes->ymax < y) {
2879 angbandContext->changes->ymax = y;
2883 if (angbandContext->changes->rows[y]->xmin > x) {
2884 angbandContext->changes->rows[y]->xmin = x;
2886 if (angbandContext->changes->rows[y]->xmax < x + step * (n - 1)) {
2887 angbandContext->changes->rows[y]->xmax = x + step * (n - 1);
2889 for (pc = angbandContext->changes->rows[y]->cell_changes + x;
2890 pc != angbandContext->changes->rows[y]->cell_changes + x + step * n;
2892 TERM_COLOR a = *ap++;
2894 TERM_COLOR ta = *tap++;
2897 if (use_graphics && (a & 0x80) && (c & 0x80)) {
2898 pc->c.c = ((byte)c & 0x7F) % pict_cols;
2899 pc->a = ((byte)a & 0x7F) % pict_rows;
2900 pc->tcol = ((byte)tc & 0x7F) % pict_cols;
2901 pc->trow = ((byte)ta & 0x7F) % pict_rows;
2902 pc->change_type = CELL_CHANGE_PICT;
2907 angbandContext->changes->has_pict = 1;
2915 * Low level graphics. Assumes valid input.
2917 * Draw several ("n") chars, with an attr, at a given location.
2919 static errr Term_text_cocoa(
2920 TERM_LEN x, TERM_LEN y, int n, TERM_COLOR a, concptr cp)
2922 AngbandContext* angbandContext = Term->data;
2923 struct PendingCellChange *pc;
2925 if (angbandContext->changes == 0) {
2926 /* Bail out; there was an earlier memory allocation failure. */
2929 if (angbandContext->changes->rows[y] == 0) {
2930 angbandContext->changes->rows[y] =
2931 create_row_change(angbandContext->cols);
2932 if (angbandContext->changes->rows[y] == 0) {
2933 NSLog(@"failed to allocate changes for row %d", y);
2936 if (angbandContext->changes->ymin > y) {
2937 angbandContext->changes->ymin = y;
2939 if (angbandContext->changes->ymax < y) {
2940 angbandContext->changes->ymax = y;
2944 angbandContext->changes->has_text = 1;
2945 if (angbandContext->changes->rows[y]->xmin > x) {
2946 angbandContext->changes->rows[y]->xmin = x;
2948 if (angbandContext->changes->rows[y]->xmax < x + n - 1) {
2949 angbandContext->changes->rows[y]->xmax = x + n - 1;
2951 pc = angbandContext->changes->rows[y]->cell_changes + x;
2952 while (pc != angbandContext->changes->rows[y]->cell_changes + x + n) {
2956 angbandContext->changes->rows[y]->cell_changes + x + n) {
2958 * The second byte of the character is past the end. Ignore
2963 pc->c.w = convert_two_byte_eucjp_to_utf16_native(cp);
2966 pc->change_type = CELL_CHANGE_TEXT;
2969 * Fill in a dummy value since the previous character will take
2975 pc->change_type = CELL_CHANGE_TEXT;
2983 pc->change_type = CELL_CHANGE_TEXT;
2991 pc->change_type = CELL_CHANGE_TEXT;
3002 * Post a nonsense event so that our event loop wakes up
3004 static void wakeup_event_loop(void)
3006 /* Big hack - send a nonsense event to make us update */
3007 NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined location:NSZeroPoint modifierFlags:0 timestamp:0 windowNumber:0 context:NULL subtype:AngbandEventWakeup data1:0 data2:0];
3008 [NSApp postEvent:event atStart:NO];
3013 * Create and initialize window number "i"
3015 static term *term_data_link(int i)
3017 NSArray *terminalDefaults = [[NSUserDefaults standardUserDefaults] valueForKey: AngbandTerminalsDefaultsKey];
3018 NSInteger rows = 24;
3019 NSInteger columns = 80;
3021 if( i < (int)[terminalDefaults count] )
3023 NSDictionary *term = [terminalDefaults objectAtIndex: i];
3024 rows = [[term valueForKey: AngbandTerminalRowsDefaultsKey] integerValue];
3025 columns = [[term valueForKey: AngbandTerminalColumnsDefaultsKey] integerValue];
3029 term *newterm = ZNEW(term);
3031 /* Initialize the term */
3032 term_init(newterm, columns, rows, 256 /* keypresses, for some reason? */);
3034 /* Differentiate between BS/^h, Tab/^i, etc. */
3035 /* newterm->complex_input = TRUE; */
3037 /* Use a "software" cursor */
3038 newterm->soft_cursor = TRUE;
3040 /* Disable the per-row flush notifications since they are not used. */
3041 newterm->never_frosh = TRUE;
3043 /* Erase with "white space" */
3044 newterm->attr_blank = TERM_WHITE;
3045 newterm->char_blank = ' ';
3047 /* Prepare the init/nuke hooks */
3048 newterm->init_hook = Term_init_cocoa;
3049 newterm->nuke_hook = Term_nuke_cocoa;
3051 /* Prepare the function hooks */
3052 newterm->xtra_hook = Term_xtra_cocoa;
3053 newterm->wipe_hook = Term_wipe_cocoa;
3054 newterm->curs_hook = Term_curs_cocoa;
3055 newterm->bigcurs_hook = Term_bigcurs_cocoa;
3056 newterm->text_hook = Term_text_cocoa;
3057 newterm->pict_hook = Term_pict_cocoa;
3058 /* newterm->mbcs_hook = Term_mbcs_cocoa; */
3060 /* Global pointer */
3061 angband_term[i] = newterm;
3067 * Load preferences from preferences file for current host+current user+
3068 * current application.
3070 static void load_prefs()
3072 NSUserDefaults *defs = [NSUserDefaults angbandDefaults];
3074 /* Make some default defaults */
3075 NSMutableArray *defaultTerms = [[NSMutableArray alloc] init];
3077 /* The following default rows/cols were determined experimentally by first
3078 * finding the ideal window/font size combinations. But because of awful
3079 * temporal coupling in Term_init_cocoa(), it's impossible to set up the
3080 * defaults there, so we do it this way. */
3081 for( NSUInteger i = 0; i < ANGBAND_TERM_MAX; i++ )
3119 NSDictionary *standardTerm = [NSDictionary dictionaryWithObjectsAndKeys:
3120 [NSNumber numberWithInt: rows], AngbandTerminalRowsDefaultsKey,
3121 [NSNumber numberWithInt: columns], AngbandTerminalColumnsDefaultsKey,
3122 [NSNumber numberWithBool: visible], AngbandTerminalVisibleDefaultsKey,
3124 [defaultTerms addObject: standardTerm];
3127 NSDictionary *defaults = [[NSDictionary alloc] initWithObjectsAndKeys:
3129 @"Osaka", @"FontName",
3131 @"Menlo", @"FontName",
3133 [NSNumber numberWithFloat:13.f], @"FontSize",
3134 [NSNumber numberWithInt:60], AngbandFrameRateDefaultsKey,
3135 [NSNumber numberWithBool:YES], AngbandSoundDefaultsKey,
3136 [NSNumber numberWithInt:GRAPHICS_NONE], AngbandGraphicsDefaultsKey,
3137 [NSNumber numberWithBool:YES], AngbandBigTileDefaultsKey,
3138 defaultTerms, AngbandTerminalsDefaultsKey,
3140 [defs registerDefaults:defaults];
3142 [defaultTerms release];
3144 /* Preferred graphics mode */
3145 graf_mode_req = [defs integerForKey:AngbandGraphicsDefaultsKey];
3146 if (graf_mode_req != GRAPHICS_NONE &&
3147 get_graphics_mode(graf_mode_req)->grafID != GRAPHICS_NONE &&
3148 [defs boolForKey:AngbandBigTileDefaultsKey] == YES) {
3152 use_bigtile = FALSE;
3153 arg_bigtile = FALSE;
3156 /* Use sounds; set the Angband global */
3157 use_sound = ([defs boolForKey:AngbandSoundDefaultsKey] == YES) ? TRUE : FALSE;
3160 frames_per_second = [defs integerForKey:AngbandFrameRateDefaultsKey];
3163 default_font = [[NSFont fontWithName:[defs valueForKey:@"FontName-0"] size:[defs floatForKey:@"FontSize-0"]] retain];
3164 if (! default_font) default_font = [[NSFont fontWithName:@"Menlo" size:13.] retain];
3168 * Arbitary limit on number of possible samples per event
3170 #define MAX_SAMPLES 16
3173 * Struct representing all data for a set of event samples
3177 int num; /* Number of available samples for this event */
3178 NSSound *sound[MAX_SAMPLES];
3179 } sound_sample_list;
3182 * Array of event sound structs
3184 static sound_sample_list samples[MSG_MAX];
3188 * Load sound effects based on sound.cfg within the xtra/sound directory;
3189 * bridge to Cocoa to use NSSound for simple loading and playback, avoiding
3190 * I/O latency by cacheing all sounds at the start. Inherits full sound
3191 * format support from Quicktime base/plugins.
3192 * pelpel favoured a plist-based parser for the future but .cfg support
3193 * improves cross-platform compatibility.
3195 static void load_sounds(void)
3197 char sound_dir[1024];
3202 /* Build the "sound" path */
3203 path_build(sound_dir, sizeof(sound_dir), ANGBAND_DIR_XTRA, "sound");
3205 /* Find and open the config file */
3206 path_build(path, sizeof(path), sound_dir, "sound.cfg");
3207 fff = my_fopen(path, "r");
3212 NSLog(@"The sound configuration file could not be opened.");
3216 /* Instantiate an autorelease pool for use by NSSound */
3217 NSAutoreleasePool *autorelease_pool;
3218 autorelease_pool = [[NSAutoreleasePool alloc] init];
3220 /* Use a dictionary to unique sounds, so we can share NSSounds across
3221 * multiple events */
3222 NSMutableDictionary *sound_dict = [NSMutableDictionary dictionary];
3225 * This loop may take a while depending on the count and size of samples
3229 /* Parse the file */
3230 /* Lines are always of the form "name = sample [sample ...]" */
3231 while (my_fgets(fff, buffer, sizeof(buffer)) == 0)
3234 char *cfg_sample_list;
3240 /* Skip anything not beginning with an alphabetic character */
3241 if (!buffer[0] || !isalpha((unsigned char)buffer[0])) continue;
3243 /* Split the line into two: message name, and the rest */
3244 search = strchr(buffer, ' ');
3245 cfg_sample_list = strchr(search + 1, ' ');
3246 if (!search) continue;
3247 if (!cfg_sample_list) continue;
3249 /* Set the message name, and terminate at first space */
3253 /* Make sure this is a valid event name */
3254 for (event = MSG_MAX - 1; event >= 0; event--)
3256 if (strcmp(msg_name, angband_sound_name[event]) == 0)
3259 if (event < 0) continue;
3261 /* Advance the sample list pointer so it's at the beginning of text */
3263 if (!cfg_sample_list[0]) continue;
3265 /* Terminate the current token */
3266 cur_token = cfg_sample_list;
3267 search = strchr(cur_token, ' ');
3271 next_token = search + 1;
3279 * Now we find all the sample names and add them one by one
3283 int num = samples[event].num;
3285 /* Don't allow too many samples */
3286 if (num >= MAX_SAMPLES) break;
3288 NSString *token_string = [NSString stringWithUTF8String:cur_token];
3289 NSSound *sound = [sound_dict objectForKey:token_string];
3295 /* We have to load the sound. Build the path to the sample */
3296 path_build(path, sizeof(path), sound_dir, cur_token);
3297 if (stat(path, &stb) == 0)
3300 /* Load the sound into memory */
3301 sound = [[[NSSound alloc] initWithContentsOfFile:[NSString stringWithUTF8String:path] byReference:YES] autorelease];
3302 if (sound) [sound_dict setObject:sound forKey:token_string];
3306 /* Store it if we loaded it */
3309 samples[event].sound[num] = [sound retain];
3311 /* Imcrement the sample count */
3312 samples[event].num++;
3316 /* Figure out next token */
3317 cur_token = next_token;
3320 /* Try to find a space */
3321 search = strchr(cur_token, ' ');
3323 /* If we can find one, terminate, and set new "next" */
3327 next_token = search + 1;
3331 /* Otherwise prevent infinite looping */
3338 /* Release the autorelease pool */
3339 [autorelease_pool release];
3341 /* Close the file */
3346 * Play sound effects asynchronously. Select a sound from any available
3347 * for the required event, and bridge to Cocoa to play it.
3349 static void play_sound(int event)
3352 if (event < 0 || event >= MSG_MAX) return;
3354 /* Load sounds just-in-time (once) */
3355 static BOOL loaded = NO;
3361 /* Check there are samples for this event */
3362 if (!samples[event].num) return;
3364 /* Instantiate an autorelease pool for use by NSSound */
3365 NSAutoreleasePool *autorelease_pool;
3366 autorelease_pool = [[NSAutoreleasePool alloc] init];
3368 /* Choose a random event */
3369 int s = randint0(samples[event].num);
3371 /* Stop the sound if it's currently playing */
3372 if ([samples[event].sound[s] isPlaying])
3373 [samples[event].sound[s] stop];
3375 /* Play the sound */
3376 [samples[event].sound[s] play];
3378 /* Release the autorelease pool */
3379 [autorelease_pool drain];
3385 static void init_windows(void)
3387 /* Create the main window */
3388 term *primary = term_data_link(0);
3390 /* Prepare to create any additional windows */
3392 for (i=1; i < ANGBAND_TERM_MAX; i++) {
3396 /* Activate the primary term */
3397 Term_activate(primary);
3401 * Handle the "open_when_ready" flag
3403 static void handle_open_when_ready(void)
3405 /* Check the flag XXX XXX XXX make a function for this */
3406 if (open_when_ready && initialized && !game_in_progress)
3409 open_when_ready = FALSE;
3411 /* Game is in progress */
3412 game_in_progress = TRUE;
3414 /* Wait for a keypress */
3415 pause_line(Term->hgt - 1);
3421 * Handle quit_when_ready, by Peter Ammon,
3422 * slightly modified to check inkey_flag.
3424 static void quit_calmly(void)
3426 /* Quit immediately if game's not started */
3427 if (!game_in_progress || !character_generated) quit(NULL);
3429 /* Save the game and Quit (if it's safe) */
3432 /* Hack -- Forget messages and term */
3434 Term->mapped_flag = FALSE;
3437 do_cmd_save_game(FALSE);
3438 record_current_savefile();
3444 /* Wait until inkey_flag is set */
3450 * Returns YES if we contain an AngbandView (and hence should direct our events
3453 static BOOL contains_angband_view(NSView *view)
3455 if ([view isKindOfClass:[AngbandView class]]) return YES;
3456 for (NSView *subview in [view subviews]) {
3457 if (contains_angband_view(subview)) return YES;
3464 * Queue mouse presses if they occur in the map section of the main window.
3466 static void AngbandHandleEventMouseDown( NSEvent *event )
3469 AngbandContext *angbandContext = [[[event window] contentView] angbandContext];
3470 AngbandContext *mainAngbandContext = angband_term[0]->data;
3472 if (mainAngbandContext->primaryWindow && [[event window] windowNumber] == [mainAngbandContext->primaryWindow windowNumber])
3474 int cols, rows, x, y;
3475 Term_get_size(&cols, &rows);
3476 NSSize tileSize = angbandContext->tileSize;
3477 NSSize border = angbandContext->borderSize;
3478 NSPoint windowPoint = [event locationInWindow];
3480 /* Adjust for border; add border height because window origin is at
3482 windowPoint = NSMakePoint( windowPoint.x - border.width, windowPoint.y + border.height );
3484 NSPoint p = [[[event window] contentView] convertPoint: windowPoint fromView: nil];
3485 x = floor( p.x / tileSize.width );
3486 y = floor( p.y / tileSize.height );
3488 /* Being safe about this, since xcode doesn't seem to like the
3489 * bool_hack stuff */
3490 BOOL displayingMapInterface = ((int)inkey_flag != 0);
3492 /* Sidebar plus border == thirteen characters; top row is reserved. */
3493 /* Coordinates run from (0,0) to (cols-1, rows-1). */
3494 BOOL mouseInMapSection = (x > 13 && x <= cols - 1 && y > 0 && y <= rows - 2);
3496 /* If we are displaying a menu, allow clicks anywhere; if we are
3497 * displaying the main game interface, only allow clicks in the map
3499 if (!displayingMapInterface || (displayingMapInterface && mouseInMapSection))
3501 /* [event buttonNumber] will return 0 for left click,
3502 * 1 for right click, but this is safer */
3503 int button = ([event type] == NSLeftMouseDown) ? 1 : 2;
3506 NSUInteger eventModifiers = [event modifierFlags];
3507 byte angbandModifiers = 0;
3508 angbandModifiers |= (eventModifiers & NSShiftKeyMask) ? KC_MOD_SHIFT : 0;
3509 angbandModifiers |= (eventModifiers & NSControlKeyMask) ? KC_MOD_CONTROL : 0;
3510 angbandModifiers |= (eventModifiers & NSAlternateKeyMask) ? KC_MOD_ALT : 0;
3511 button |= (angbandModifiers & 0x0F) << 4; /* encode modifiers in the button number (see Term_mousepress()) */
3514 Term_mousepress(x, y, button);
3518 /* Pass click through to permit focus change, resize, etc. */
3519 [NSApp sendEvent:event];
3525 * Encodes an NSEvent Angband-style, or forwards it along. Returns YES if the
3526 * event was sent to Angband, NO if Cocoa (or nothing) handled it */
3527 static BOOL send_event(NSEvent *event)
3530 /* If the receiving window is not an Angband window, then do nothing */
3531 if (! contains_angband_view([[event window] contentView]))
3533 [NSApp sendEvent:event];
3537 /* Analyze the event */
3538 switch ([event type])
3542 /* Try performing a key equivalent */
3543 if ([[NSApp mainMenu] performKeyEquivalent:event]) break;
3545 unsigned modifiers = [event modifierFlags];
3547 /* Send all NSCommandKeyMasks through */
3548 if (modifiers & NSCommandKeyMask)
3550 [NSApp sendEvent:event];
3554 if (! [[event characters] length]) break;
3557 /* Extract some modifiers */
3558 int mc = !! (modifiers & NSControlKeyMask);
3559 int ms = !! (modifiers & NSShiftKeyMask);
3560 int mo = !! (modifiers & NSAlternateKeyMask);
3561 int kp = !! (modifiers & NSNumericPadKeyMask);
3564 /* Get the Angband char corresponding to this unichar */
3565 unichar c = [[event characters] characterAtIndex:0];
3568 * Have anything from the numeric keypad generate a macro
3569 * trigger so that shift or control modifiers can be passed.
3571 if (c <= 0x7F && !kp)
3577 * The rest of Hengband uses Angband 2.7's or so key handling:
3578 * so for the rest do something like the encoding that
3579 * main-win.c does: send a macro trigger with the Unicode
3580 * value encoded into printable ASCII characters.
3585 /* override special keys */
3586 switch([event keyCode]) {
3587 case kVK_Return: ch = '\r'; break;
3588 case kVK_Escape: ch = 27; break;
3589 case kVK_Tab: ch = '\t'; break;
3590 case kVK_Delete: ch = '\b'; break;
3591 case kVK_ANSI_KeypadEnter: ch = '\r'; kp = TRUE; break;
3594 /* Hide the mouse pointer */
3595 [NSCursor setHiddenUntilMouseMoves:YES];
3605 * Could use the hexsym global but some characters overlap with
3606 * those used to indicate modifiers.
3608 const char encoded[16] = {
3609 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
3613 /* Begin the macro trigger. */
3616 /* Send the modifiers. */
3617 if (mc) Term_keypress('C');
3618 if (ms) Term_keypress('S');
3619 if (mo) Term_keypress('O');
3620 if (kp) Term_keypress('K');
3623 Term_keypress(encoded[c & 0xF]);
3627 /* End the macro trigger. */
3634 case NSLeftMouseDown:
3635 case NSRightMouseDown:
3636 AngbandHandleEventMouseDown(event);
3639 case NSApplicationDefined:
3641 if ([event subtype] == AngbandEventWakeup)
3649 [NSApp sendEvent:event];
3656 * Check for Events, return TRUE if we process any
3658 static BOOL check_events(int wait)
3661 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3663 /* Handles the quit_when_ready flag */
3664 if (quit_when_ready) quit_calmly();
3667 if (wait == CHECK_EVENTS_WAIT) endDate = [NSDate distantFuture];
3668 else endDate = [NSDate distantPast];
3672 if (quit_when_ready)
3674 /* send escape events until we quit */
3675 Term_keypress(0x1B);
3680 event = [NSApp nextEventMatchingMask:-1 untilDate:endDate inMode:NSDefaultRunLoopMode dequeue:YES];
3686 if (send_event(event)) break;
3692 /* Something happened */
3698 * Hook to tell the user something important
3700 static void hook_plog(const char * str)
3704 NSString *msg = NSLocalizedStringWithDefaultValue(
3705 @"Warning", AngbandMessageCatalog, [NSBundle mainBundle],
3706 @"Warning", @"Alert text for generic warning");
3707 NSString *info = [NSString stringWithCString:str
3709 encoding:NSJapaneseEUCStringEncoding
3711 encoding:NSMacOSRomanStringEncoding
3714 NSAlert *alert = [[NSAlert alloc] init];
3716 alert.messageText = msg;
3717 alert.informativeText = info;
3718 NSModalResponse result = [alert runModal];
3725 * Hook to tell the user something, and then quit
3727 static void hook_quit(const char * str)
3734 * Return the path for Angband's lib directory and bail if it isn't found. The
3735 * lib directory should be in the bundle's resources directory, since it's
3736 * copied when built.
3738 static NSString* get_lib_directory(void)
3740 NSString *bundleLibPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: AngbandDirectoryNameLib];
3741 BOOL isDirectory = NO;
3742 BOOL libExists = [[NSFileManager defaultManager] fileExistsAtPath: bundleLibPath isDirectory: &isDirectory];
3744 if( !libExists || !isDirectory )
3746 NSLog( @"Hengband: can't find %@/ in bundle: isDirectory: %d libExists: %d", AngbandDirectoryNameLib, isDirectory, libExists );
3748 NSString *msg = NSLocalizedStringWithDefaultValue(
3749 @"Error.MissingResources",
3750 AngbandMessageCatalog,
3751 [NSBundle mainBundle],
3752 @"Missing Resources",
3753 @"Alert text for missing resources");
3754 NSString *info = NSLocalizedStringWithDefaultValue(
3755 @"Error.MissingAngbandLib",
3756 AngbandMessageCatalog,
3757 [NSBundle mainBundle],
3758 @"Hengband was unable to find required resources and must quit. Please report a bug on the Angband forums.",
3759 @"Alert informative message for missing Angband lib/ folder");
3760 NSString *quit_label = NSLocalizedStringWithDefaultValue(
3761 @"Label.Quit", AngbandMessageCatalog, [NSBundle mainBundle],
3763 NSAlert *alert = [[NSAlert alloc] init];
3766 * Note that NSCriticalAlertStyle was deprecated in 10.10. The
3767 * replacement is NSAlertStyleCritical.
3769 alert.alertStyle = NSCriticalAlertStyle;
3770 alert.messageText = msg;
3771 alert.informativeText = info;
3772 [alert addButtonWithTitle:quit_label];
3773 NSModalResponse result = [alert runModal];
3778 return bundleLibPath;
3782 * Return the path for the directory where Angband should look for its standard
3785 static NSString* get_doc_directory(void)
3787 NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
3789 #if defined(SAFE_DIRECTORY)
3790 NSString *versionedDirectory = [NSString stringWithFormat: @"%@-%s", AngbandDirectoryNameBase, VERSION_STRING];
3791 return [documents stringByAppendingPathComponent: versionedDirectory];
3793 return [documents stringByAppendingPathComponent: AngbandDirectoryNameBase];
3798 * Adjust directory paths as needed to correct for any differences needed by
3799 * Angband. init_file_paths() currently requires that all paths provided have
3800 * a trailing slash and all other platforms honor this.
3802 * \param originalPath The directory path to adjust.
3803 * \return A path suitable for Angband or nil if an error occurred.
3805 static NSString* AngbandCorrectedDirectoryPath(NSString *originalPath)
3807 if ([originalPath length] == 0) {
3811 if (![originalPath hasSuffix: @"/"]) {
3812 return [originalPath stringByAppendingString: @"/"];
3815 return originalPath;
3819 * Give Angband the base paths that should be used for the various directories
3820 * it needs. It will create any needed directories.
3822 static void prepare_paths_and_directories(void)
3824 char libpath[PATH_MAX + 1] = "\0";
3825 NSString *libDirectoryPath =
3826 AngbandCorrectedDirectoryPath(get_lib_directory());
3827 [libDirectoryPath getFileSystemRepresentation: libpath maxLength: sizeof(libpath)];
3829 char basepath[PATH_MAX + 1] = "\0";
3830 NSString *angbandDocumentsPath =
3831 AngbandCorrectedDirectoryPath(get_doc_directory());
3832 [angbandDocumentsPath getFileSystemRepresentation: basepath maxLength: sizeof(basepath)];
3834 init_file_paths(libpath, basepath);
3835 create_needed_dirs();
3839 * ------------------------------------------------------------------------
3841 * ------------------------------------------------------------------------ */
3843 @implementation AngbandAppDelegate
3845 @synthesize graphicsMenu=_graphicsMenu;
3846 @synthesize commandMenu=_commandMenu;
3847 @synthesize commandMenuTagMap=_commandMenuTagMap;
3849 - (IBAction)newGame:sender
3851 /* Game is in progress */
3852 game_in_progress = TRUE;
3856 - (IBAction)editFont:sender
3858 NSFontPanel *panel = [NSFontPanel sharedFontPanel];
3859 NSFont *termFont = default_font;
3862 for (i=0; i < ANGBAND_TERM_MAX; i++) {
3863 if ([(id)angband_term[i]->data isMainWindow]) {
3864 termFont = [(id)angband_term[i]->data selectionFont];
3869 [panel setPanelFont:termFont isMultiple:NO];
3870 [panel orderFront:self];
3874 * Implent NSObject's changeFont() method to receive a notification about the
3875 * changed font. Note that, as of 10.14, changeFont() is deprecated in
3876 * NSObject - it will be removed at some point and the application delegate
3877 * will have to be declared as implementing the NSFontChanging protocol.
3879 - (void)changeFont:(id)sender
3882 for (mainTerm=0; mainTerm < ANGBAND_TERM_MAX; mainTerm++) {
3883 if ([(id)angband_term[mainTerm]->data isMainWindow]) {
3888 /* Bug #1709: Only change font for angband windows */
3889 if (mainTerm == ANGBAND_TERM_MAX) return;
3891 NSFont *oldFont = default_font;
3892 NSFont *newFont = [sender convertFont:oldFont];
3893 if (! newFont) return; /*paranoia */
3895 /* Store as the default font if we changed the first term */
3896 if (mainTerm == 0) {
3898 [default_font release];
3899 default_font = newFont;
3902 /* Record it in the preferences */
3903 NSUserDefaults *defs = [NSUserDefaults angbandDefaults];
3904 [defs setValue:[newFont fontName]
3905 forKey:[NSString stringWithFormat:@"FontName-%d", mainTerm]];
3906 [defs setFloat:[newFont pointSize]
3907 forKey:[NSString stringWithFormat:@"FontSize-%d", mainTerm]];
3910 NSDisableScreenUpdates();
3913 AngbandContext *angbandContext = angband_term[mainTerm]->data;
3914 [(id)angbandContext setSelectionFont:newFont adjustTerminal: YES];
3916 NSEnableScreenUpdates();
3918 if (mainTerm == 0 && game_in_progress) {
3919 /* Mimics the logic in setGraphicsMode(). */
3921 wakeup_event_loop();
3923 [(id)angbandContext requestRedraw];
3927 - (IBAction)openGame:sender
3929 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
3930 BOOL selectedSomething = NO;
3933 /* Get where we think the save files are */
3934 NSURL *startingDirectoryURL = [NSURL fileURLWithPath:[NSString stringWithCString:ANGBAND_DIR_SAVE encoding:NSASCIIStringEncoding] isDirectory:YES];
3936 /* Set up an open panel */
3937 NSOpenPanel* panel = [NSOpenPanel openPanel];
3938 [panel setCanChooseFiles:YES];
3939 [panel setCanChooseDirectories:NO];
3940 [panel setResolvesAliases:YES];
3941 [panel setAllowsMultipleSelection:NO];
3942 [panel setTreatsFilePackagesAsDirectories:YES];
3943 [panel setDirectoryURL:startingDirectoryURL];
3946 panelResult = [panel runModal];
3947 if (panelResult == NSOKButton)
3949 NSArray* fileURLs = [panel URLs];
3950 if ([fileURLs count] > 0 && [[fileURLs objectAtIndex:0] isFileURL])
3952 NSURL* savefileURL = (NSURL *)[fileURLs objectAtIndex:0];
3953 /* The path property doesn't do the right thing except for
3954 * URLs with the file scheme. We had getFileSystemRepresentation
3955 * here before, but that wasn't introduced until OS X 10.9. */
3956 selectedSomething = [[savefileURL path] getCString:savefile
3957 maxLength:sizeof savefile encoding:NSMacOSRomanStringEncoding];
3961 if (selectedSomething)
3963 /* Remember this so we can select it by default next time */
3964 record_current_savefile();
3966 /* Game is in progress */
3967 game_in_progress = TRUE;
3974 - (IBAction)saveGame:sender
3976 /* Hack -- Forget messages */
3980 do_cmd_save_game(FALSE);
3982 /* Record the current save file so we can select it by default next time.
3983 * It's a little sketchy that this only happens when we save through the
3984 * menu; ideally game-triggered saves would trigger it too. */
3985 record_current_savefile();
3989 * Entry point for initializing Angband
3993 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3995 /* Hooks in some "z-util.c" hooks */
3996 plog_aux = hook_plog;
3997 quit_aux = hook_quit;
3999 /* Initialize file paths */
4000 prepare_paths_and_directories();
4002 /* Note the "system" */
4003 ANGBAND_SYS = "coc";
4005 /* Load possible graphics modes */
4006 init_graphics_modes();
4008 /* Load preferences */
4011 /* Prepare the windows */
4014 /* Set up game event handlers */
4015 /* init_display(); */
4017 /* Register the sound hook */
4018 /* sound_hook = play_sound; */
4020 /* Initialise game */
4023 /* Initialize some save file stuff */
4024 player_egid = getegid();
4026 /* We are now initialized */
4029 /* Handle "open_when_ready" */
4030 handle_open_when_ready();
4032 /* Handle pending events (most notably update) and flush input */
4035 /* Prompt the user. */
4036 int message_row = (Term->hgt - 23) / 5 + 23;
4037 Term_erase(0, message_row, 255);
4040 "['ファイル' メニューから '新' または '開く' を選択します]",
4041 message_row, (Term->wid - 57) / 2
4043 "[Choose 'New' or 'Open' from the 'File' menu]",
4044 message_row, (Term->wid - 45) / 2
4051 while (!game_in_progress) {
4052 NSAutoreleasePool *splashScreenPool = [[NSAutoreleasePool alloc] init];
4053 NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantFuture] inMode:NSDefaultRunLoopMode dequeue:YES];
4054 if (event) [NSApp sendEvent:event];
4055 [splashScreenPool drain];
4059 * Play a game -- "new_game" is set by "new", "open" or the open document
4060 * even handler as appropriate
4063 play_game(new_game);
4069 * Implement NSObject's validateMenuItem() method to override enabling or
4070 * disabling a menu item. Note that, as of 10.14, validateMenuItem() is
4071 * deprecated in NSObject - it will be removed at some point and the
4072 * application delegate will have to be declared as implementing the
4073 * NSMenuItemValidation protocol.
4075 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
4077 SEL sel = [menuItem action];
4078 NSInteger tag = [menuItem tag];
4080 if( tag >= AngbandWindowMenuItemTagBase && tag < AngbandWindowMenuItemTagBase + ANGBAND_TERM_MAX )
4082 if( tag == AngbandWindowMenuItemTagBase )
4084 /* The main window should always be available and visible */
4090 * Another window is only usable after Term_init_cocoa() has
4091 * been called for it. For Angband if window_flag[i] is nonzero
4092 * then that has happened for window i. For Hengband, that is
4093 * not the case so also test angband_term[i]->data.
4095 NSInteger subwindowNumber = tag - AngbandWindowMenuItemTagBase;
4096 return (angband_term[subwindowNumber]->data != 0
4097 && window_flag[subwindowNumber] > 0);
4103 if (sel == @selector(newGame:))
4105 return ! game_in_progress;
4107 else if (sel == @selector(editFont:))
4111 else if (sel == @selector(openGame:))
4113 return ! game_in_progress;
4115 else if (sel == @selector(setRefreshRate:) &&
4116 [[menuItem parentItem] tag] == 150)
4118 NSInteger fps = [[NSUserDefaults standardUserDefaults] integerForKey:AngbandFrameRateDefaultsKey];
4119 [menuItem setState: ([menuItem tag] == fps)];
4122 else if( sel == @selector(setGraphicsMode:) )
4124 NSInteger requestedGraphicsMode = [[NSUserDefaults standardUserDefaults] integerForKey:AngbandGraphicsDefaultsKey];
4125 [menuItem setState: (tag == requestedGraphicsMode)];
4128 else if( sel == @selector(toggleSound:) )
4130 BOOL is_on = [[NSUserDefaults standardUserDefaults]
4131 boolForKey:AngbandSoundDefaultsKey];
4133 [menuItem setState: ((is_on) ? NSOnState : NSOffState)];
4136 else if( sel == @selector(sendAngbandCommand:) ||
4137 sel == @selector(saveGame:) )
4140 * we only want to be able to send commands during an active game
4141 * after the birth screens
4143 return !!game_in_progress && character_generated;
4149 - (IBAction)setRefreshRate:(NSMenuItem *)menuItem
4151 frames_per_second = [menuItem tag];
4152 [[NSUserDefaults angbandDefaults] setInteger:frames_per_second forKey:AngbandFrameRateDefaultsKey];
4155 - (void)selectWindow: (id)sender
4157 NSInteger subwindowNumber = [(NSMenuItem *)sender tag] - AngbandWindowMenuItemTagBase;
4158 AngbandContext *context = angband_term[subwindowNumber]->data;
4159 [context->primaryWindow makeKeyAndOrderFront: self];
4160 [context saveWindowVisibleToDefaults: YES];
4163 - (void)prepareWindowsMenu
4165 /* Get the window menu with default items and add a separator and item for
4166 * the main window */
4167 NSMenu *windowsMenu = [[NSApplication sharedApplication] windowsMenu];
4168 [windowsMenu addItem: [NSMenuItem separatorItem]];
4170 NSString *title1 = [NSString stringWithCString:angband_term_name[0]
4172 encoding:NSJapaneseEUCStringEncoding
4174 encoding:NSMacOSRomanStringEncoding
4177 NSMenuItem *angbandItem = [[NSMenuItem alloc] initWithTitle:title1 action: @selector(selectWindow:) keyEquivalent: @"0"];
4178 [angbandItem setTarget: self];
4179 [angbandItem setTag: AngbandWindowMenuItemTagBase];
4180 [windowsMenu addItem: angbandItem];
4181 [angbandItem release];
4183 /* Add items for the additional term windows */
4184 for( NSInteger i = 1; i < ANGBAND_TERM_MAX; i++ )
4186 NSString *title = [NSString stringWithCString:angband_term_name[i]
4188 encoding:NSJapaneseEUCStringEncoding
4190 encoding:NSMacOSRomanStringEncoding
4193 NSString *keyEquivalent = [NSString stringWithFormat: @"%ld", (long)i];
4194 NSMenuItem *windowItem = [[NSMenuItem alloc] initWithTitle: title action: @selector(selectWindow:) keyEquivalent: keyEquivalent];
4195 [windowItem setTarget: self];
4196 [windowItem setTag: AngbandWindowMenuItemTagBase + i];
4197 [windowsMenu addItem: windowItem];
4198 [windowItem release];
4202 - (void)setGraphicsMode:(NSMenuItem *)sender
4204 /* We stashed the graphics mode ID in the menu item's tag */
4205 graf_mode_req = [sender tag];
4207 /* Stash it in UserDefaults */
4208 [[NSUserDefaults angbandDefaults] setInteger:graf_mode_req forKey:AngbandGraphicsDefaultsKey];
4209 [[NSUserDefaults angbandDefaults] synchronize];
4211 if (graf_mode_req == GRAPHICS_NONE ||
4212 get_graphics_mode(graf_mode_req) == GRAPHICS_NONE) {
4214 arg_bigtile = FALSE;
4216 } else if ([[NSUserDefaults angbandDefaults] boolForKey:AngbandBigTileDefaultsKey] == YES &&
4221 if (game_in_progress)
4223 if (arg_bigtile != use_bigtile) {
4224 Term_activate(angband_term[0]);
4225 Term_resize(angband_term[0]->wid, angband_term[0]->hgt);
4228 /* Hack -- Force redraw */
4231 /* Wake up the event loop so it notices the change */
4232 wakeup_event_loop();
4236 - (IBAction) toggleSound: (NSMenuItem *) sender
4238 BOOL is_on = (sender.state == NSOnState);
4240 /* Toggle the state and update the Angband global and preferences. */
4241 sender.state = (is_on) ? NSOffState : NSOnState;
4242 use_sound = (is_on) ? FALSE : TRUE;
4243 [[NSUserDefaults angbandDefaults] setBool:(! is_on)
4244 forKey:AngbandSoundDefaultsKey];
4247 - (IBAction)toggleWideTiles:(NSMenuItem *) sender
4249 BOOL is_on = (sender.state == NSOnState);
4251 /* Toggle the state and update the Angband globals and preferences. */
4252 sender.state = (is_on) ? NSOffState : NSOnState;
4253 [[NSUserDefaults angbandDefaults] setBool:(! is_on)
4254 forKey:AngbandBigTileDefaultsKey];
4255 [[NSUserDefaults angbandDefaults] synchronize];
4256 if (graphics_are_enabled()) {
4257 arg_bigtile = (is_on) ? FALSE : TRUE;
4258 /* Mimics the logic in setGraphicsMode(). */
4259 if (game_in_progress && arg_bigtile != use_bigtile) {
4260 Term_activate(angband_term[0]);
4261 Term_resize(angband_term[0]->wid, angband_term[0]->hgt);
4263 wakeup_event_loop();
4269 * Send a command to Angband via a menu item. This places the appropriate key
4270 * down events into the queue so that it seems like the user pressed them
4271 * (instead of trying to use the term directly).
4273 - (void)sendAngbandCommand: (id)sender
4275 NSMenuItem *menuItem = (NSMenuItem *)sender;
4276 NSString *command = [self.commandMenuTagMap objectForKey: [NSNumber numberWithInteger: [menuItem tag]]];
4277 NSInteger windowNumber = [((AngbandContext *)angband_term[0]->data)->primaryWindow windowNumber];
4279 /* Send a \ to bypass keymaps */
4280 NSEvent *escape = [NSEvent keyEventWithType: NSKeyDown
4281 location: NSZeroPoint
4284 windowNumber: windowNumber
4287 charactersIgnoringModifiers: @"\\"
4290 [[NSApplication sharedApplication] postEvent: escape atStart: NO];
4292 /* Send the actual command (from the original command set) */
4293 NSEvent *keyDown = [NSEvent keyEventWithType: NSKeyDown
4294 location: NSZeroPoint
4297 windowNumber: windowNumber
4300 charactersIgnoringModifiers: command
4303 [[NSApplication sharedApplication] postEvent: keyDown atStart: NO];
4307 * Set up the command menu dynamically, based on CommandMenu.plist.
4309 - (void)prepareCommandMenu
4311 NSString *commandMenuPath = [[NSBundle mainBundle] pathForResource: @"CommandMenu" ofType: @"plist"];
4312 NSArray *commandMenuItems = [[NSArray alloc] initWithContentsOfFile: commandMenuPath];
4313 NSMutableDictionary *angbandCommands = [[NSMutableDictionary alloc] init];
4314 NSString *tblname = @"CommandMenu";
4315 NSInteger tagOffset = 0;
4317 for( NSDictionary *item in commandMenuItems )
4319 BOOL useShiftModifier = [[item valueForKey: @"ShiftModifier"] boolValue];
4320 BOOL useOptionModifier = [[item valueForKey: @"OptionModifier"] boolValue];
4321 NSUInteger keyModifiers = NSCommandKeyMask;
4322 keyModifiers |= (useShiftModifier) ? NSShiftKeyMask : 0;
4323 keyModifiers |= (useOptionModifier) ? NSAlternateKeyMask : 0;
4325 NSString *lookup = [item valueForKey: @"Title"];
4326 NSString *title = NSLocalizedStringWithDefaultValue(
4327 lookup, tblname, [NSBundle mainBundle], lookup, @"");
4328 NSString *key = [item valueForKey: @"KeyEquivalent"];
4329 NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle: title action: @selector(sendAngbandCommand:) keyEquivalent: key];
4330 [menuItem setTarget: self];
4331 [menuItem setKeyEquivalentModifierMask: keyModifiers];
4332 [menuItem setTag: AngbandCommandMenuItemTagBase + tagOffset];
4333 [self.commandMenu addItem: menuItem];
4336 NSString *angbandCommand = [item valueForKey: @"AngbandCommand"];
4337 [angbandCommands setObject: angbandCommand forKey: [NSNumber numberWithInteger: [menuItem tag]]];
4341 [commandMenuItems release];
4343 NSDictionary *safeCommands = [[NSDictionary alloc] initWithDictionary: angbandCommands];
4344 self.commandMenuTagMap = safeCommands;
4345 [safeCommands release];
4346 [angbandCommands release];
4349 - (void)awakeFromNib
4351 [super awakeFromNib];
4353 [self prepareWindowsMenu];
4354 [self prepareCommandMenu];
4357 - (void)applicationDidFinishLaunching:sender
4361 /* Once beginGame finished, the game is over - that's how Angband works,
4362 * and we should quit */
4363 game_is_finished = TRUE;
4364 [NSApp terminate:self];
4367 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
4369 if (p_ptr->playing == FALSE || game_is_finished == TRUE)
4371 return NSTerminateNow;
4373 else if (! inkey_flag)
4375 /* For compatibility with other ports, do not quit in this case */
4376 return NSTerminateCancel;
4381 /* player->upkeep->playing = FALSE; */
4383 /* Post an escape event so that we can return from our get-key-event
4385 wakeup_event_loop();
4386 quit_when_ready = true;
4387 /* Must return Cancel, not Later, because we need to get out of the
4388 * run loop and back to Angband's loop */
4389 return NSTerminateCancel;
4394 * Dynamically build the Graphics menu
4396 - (void)menuNeedsUpdate:(NSMenu *)menu {
4398 /* Only the graphics menu is dynamic */
4399 if (! [menu isEqual:self.graphicsMenu])
4402 /* If it's non-empty, then we've already built it. Currently graphics modes
4403 * won't change once created; if they ever can we can remove this check.
4404 * Note that the check mark does change, but that's handled in
4405 * validateMenuItem: instead of menuNeedsUpdate: */
4406 if ([menu numberOfItems] > 0)
4409 /* This is the action for all these menu items */
4410 SEL action = @selector(setGraphicsMode:);
4412 /* Add an initial Classic ASCII menu item */
4413 NSString *tblname = @"GraphicsMenu";
4414 NSString *key = @"Classic ASCII";
4415 NSString *title = NSLocalizedStringWithDefaultValue(
4416 key, tblname, [NSBundle mainBundle], key, @"");
4417 NSMenuItem *classicItem = [menu addItemWithTitle:title action:action keyEquivalent:@""];
4418 [classicItem setTag:GRAPHICS_NONE];
4420 /* Walk through the list of graphics modes */
4421 if (graphics_modes) {
4424 for (i=0; graphics_modes[i].pNext; i++)
4426 const graphics_mode *graf = &graphics_modes[i];
4428 if (graf->grafID == GRAPHICS_NONE) {
4431 /* Make the title. NSMenuItem throws on a nil title, so ensure it's
4433 key = [[NSString alloc] initWithUTF8String:graf->menuname];
4434 title = NSLocalizedStringWithDefaultValue(
4435 key, tblname, [NSBundle mainBundle], key, @"");
4438 NSMenuItem *item = [menu addItemWithTitle:title action:action keyEquivalent:@""];
4440 [item setTag:graf->grafID];
4446 * Delegate method that gets called if we're asked to open a file.
4448 - (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
4450 /* Can't open a file once we've started */
4451 if (game_in_progress) {
4452 [[NSApplication sharedApplication]
4453 replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
4457 /* We can only open one file. Use the last one. */
4458 NSString *file = [filenames lastObject];
4460 [[NSApplication sharedApplication]
4461 replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
4465 /* Put it in savefile */
4466 if (! [file getFileSystemRepresentation:savefile maxLength:sizeof savefile]) {
4467 [[NSApplication sharedApplication]
4468 replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
4472 game_in_progress = TRUE;
4475 /* Wake us up in case this arrives while we're sitting at the Welcome
4477 wakeup_event_loop();
4479 [[NSApplication sharedApplication]
4480 replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
4485 int main(int argc, char* argv[])
4487 NSApplicationMain(argc, (void*)argv);
4491 #endif /* MACINTOSH || MACH_O_COCOA */