--- BasiliskII/src/AmigaOS/video_amiga.cpp 2000/07/14 21:29:12 1.6 +++ BasiliskII/src/AmigaOS/video_amiga.cpp 2001/10/14 18:00:44 1.20 @@ -1,7 +1,7 @@ /* * video_amiga.cpp - Video/graphics emulation, AmigaOS specific stuff * - * Basilisk II (C) 1997-2000 Christian Bauer + * Basilisk II (C) 1997-2001 Christian Bauer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,7 +22,7 @@ #include #include #include -#include +#include #include #include #include @@ -33,6 +33,7 @@ #include #include "sysdeps.h" +#include "cpu_emulation.h" #include "main.h" #include "adb.h" #include "prefs.h" @@ -57,8 +58,12 @@ static int display_type = DISPLAY_WINDOW static struct Screen *the_screen = NULL; static struct Window *the_win = NULL; static struct BitMap *the_bitmap = NULL; +static UWORD *null_pointer = NULL; // Blank mouse pointer data +static UWORD *current_pointer = (UWORD *)-1; // Currently visible mouse pointer data static LONG black_pen = -1, white_pen = -1; static struct Process *periodic_proc = NULL; // Periodic process +static int window_width, window_height; // width and height for window display +static ULONG screen_mode_id; // mode ID for screen display extern struct Task *MainTask; // Pointer to main task (from main_amiga.cpp) @@ -85,13 +90,20 @@ static const uint8 keycode2mac[0x80] = { // Prototypes +static bool video_open(const video_mode &mode); +static void video_close(); static void periodic_func(void); +static void add_mode(uint32 width, uint32 height, uint32 resolution_id, uint32 bytes_per_row, video_depth depth); +static void add_modes(uint32 width, uint32 height, video_depth depth); +static ULONG find_mode_for_depth(uint32 width, uint32 height, uint32 depth); +static ULONG bits_from_depth(video_depth depth); /* * Initialization */ + // Open window static bool init_window(int width, int height) { @@ -114,23 +126,19 @@ static bool init_window(int width, int h TAG_END ); if (the_win == NULL) { - ErrorAlert(GetString(STR_OPEN_WINDOW_ERR)); + ErrorAlert(STR_OPEN_WINDOW_ERR); return false; } // Create bitmap ("height + 2" for safety) the_bitmap = AllocBitMap(width, height + 2, 1, BMF_CLEAR, NULL); if (the_bitmap == NULL) { - ErrorAlert(GetString(STR_NO_MEM_ERR)); + ErrorAlert(STR_NO_MEM_ERR); return false; } - // Set VideoMonitor + // Add resolution and set VideoMonitor VideoMonitor.mac_frame_base = (uint32)the_bitmap->Planes[0]; - VideoMonitor.bytes_per_row = the_bitmap->BytesPerRow; - VideoMonitor.x = width; - VideoMonitor.y = height; - VideoMonitor.mode = VMODE_1BIT; // Set FgPen and BgPen black_pen = ObtainBestPenA(the_win->WScreen->ViewPort.ColorMap, 0, 0, 0, NULL); @@ -169,19 +177,16 @@ static bool init_pip(int width, int heig TAG_END ); if (the_win == NULL || error) { - ErrorAlert(GetString(STR_OPEN_WINDOW_ERR)); + ErrorAlert(STR_OPEN_WINDOW_ERR); return false; } // Find bitmap p96PIP_GetTags(the_win, P96PIP_SourceBitMap, (ULONG)&the_bitmap, TAG_END); - // Set VideoMonitor + // Add resolution and set VideoMonitor VideoMonitor.mac_frame_base = p96GetBitMapAttr(the_bitmap, P96BMA_MEMORY); - VideoMonitor.bytes_per_row = p96GetBitMapAttr(the_bitmap, P96BMA_BYTESPERROW); - VideoMonitor.x = width; - VideoMonitor.y = height; - VideoMonitor.mode = VMODE_16BIT; + return true; } @@ -197,26 +202,23 @@ static bool init_screen_p96(ULONG mode_i switch (depth) { case 8: - VideoMonitor.mode = VMODE_8BIT; break; case 15: case 16: if (format != RGBFB_R5G5B5) { - ErrorAlert(GetString(STR_WRONG_SCREEN_FORMAT_ERR)); + ErrorAlert(STR_WRONG_SCREEN_FORMAT_ERR); return false; } - VideoMonitor.mode = VMODE_16BIT; break; case 24: case 32: if (format != RGBFB_A8R8G8B8) { - ErrorAlert(GetString(STR_WRONG_SCREEN_FORMAT_ERR)); + ErrorAlert(STR_WRONG_SCREEN_FORMAT_ERR); return false; } - VideoMonitor.mode = VMODE_32BIT; break; default: - ErrorAlert(GetString(STR_WRONG_SCREEN_DEPTH_ERR)); + ErrorAlert(STR_WRONG_SCREEN_DEPTH_ERR); return false; } @@ -224,9 +226,6 @@ static bool init_screen_p96(ULONG mode_i uint32 width = p96GetModeIDAttr(mode_id, P96IDA_WIDTH); uint32 height = p96GetModeIDAttr(mode_id, P96IDA_HEIGHT); - VideoMonitor.x = width; - VideoMonitor.y = height; - // Open screen the_screen = p96OpenScreenTags( P96SA_DisplayID, mode_id, @@ -238,7 +237,7 @@ static bool init_screen_p96(ULONG mode_i TAG_END ); if (the_screen == NULL) { - ErrorAlert(GetString(STR_OPEN_SCREEN_ERR)); + ErrorAlert(STR_OPEN_SCREEN_ERR); return false; } @@ -246,6 +245,7 @@ static bool init_screen_p96(ULONG mode_i the_win = OpenWindowTags(NULL, WA_Left, 0, WA_Top, 0, WA_Width, width, WA_Height, height, + WA_SimpleRefresh, TRUE, WA_NoCareRefresh, TRUE, WA_Borderless, TRUE, WA_Activate, TRUE, @@ -255,14 +255,15 @@ static bool init_screen_p96(ULONG mode_i TAG_END ); if (the_win == NULL) { - ErrorAlert(GetString(STR_OPEN_WINDOW_ERR)); + ErrorAlert(STR_OPEN_WINDOW_ERR); return false; } - // Set VideoMonitor ScreenToFront(the_screen); + + // Add resolution and set VideoMonitor VideoMonitor.mac_frame_base = p96GetBitMapAttr(the_screen->RastPort.BitMap, P96BMA_MEMORY); - VideoMonitor.bytes_per_row = p96GetBitMapAttr(the_screen->RastPort.BitMap, P96BMA_BYTESPERROW); + return true; } @@ -278,26 +279,24 @@ static bool init_screen_cgfx(ULONG mode_ switch (depth) { case 8: - VideoMonitor.mode = VMODE_8BIT; break; case 15: case 16: - if (format != PIXFMT_RGB16) { - ErrorAlert(GetString(STR_WRONG_SCREEN_FORMAT_ERR)); + // !!! PIXFMT_RGB15 is correct !!! + if (format != PIXFMT_RGB15) { + ErrorAlert(STR_WRONG_SCREEN_FORMAT_ERR); return false; } - VideoMonitor.mode = VMODE_16BIT; break; case 24: case 32: if (format != PIXFMT_ARGB32) { - ErrorAlert(GetString(STR_WRONG_SCREEN_FORMAT_ERR)); + ErrorAlert(STR_WRONG_SCREEN_FORMAT_ERR); return false; } - VideoMonitor.mode = VMODE_32BIT; break; default: - ErrorAlert(GetString(STR_WRONG_SCREEN_DEPTH_ERR)); + ErrorAlert(STR_WRONG_SCREEN_DEPTH_ERR); return false; } @@ -305,9 +304,6 @@ static bool init_screen_cgfx(ULONG mode_ uint32 width = GetCyberIDAttr(CYBRIDATTR_WIDTH, mode_id); uint32 height = GetCyberIDAttr(CYBRIDATTR_HEIGHT, mode_id); - VideoMonitor.x = width; - VideoMonitor.y = height; - // Open screen the_screen = OpenScreenTags(NULL, SA_DisplayID, mode_id, @@ -317,7 +313,7 @@ static bool init_screen_cgfx(ULONG mode_ TAG_END ); if (the_screen == NULL) { - ErrorAlert(GetString(STR_OPEN_SCREEN_ERR)); + ErrorAlert(STR_OPEN_SCREEN_ERR); return false; } @@ -325,6 +321,7 @@ static bool init_screen_cgfx(ULONG mode_ the_win = OpenWindowTags(NULL, WA_Left, 0, WA_Top, 0, WA_Width, width, WA_Height, height, + WA_SimpleRefresh, TRUE, WA_NoCareRefresh, TRUE, WA_Borderless, TRUE, WA_Activate, TRUE, @@ -334,24 +331,38 @@ static bool init_screen_cgfx(ULONG mode_ TAG_END ); if (the_win == NULL) { - ErrorAlert(GetString(STR_OPEN_WINDOW_ERR)); + ErrorAlert(STR_OPEN_WINDOW_ERR); return false; } - // Set VideoMonitor ScreenToFront(the_screen); static UWORD ptr[] = { 0, 0, 0, 0 }; SetPointer(the_win, ptr, 0, 0, 0, 0); // Hide mouse pointer + + // Set VideoMonitor + ULONG frame_base; APTR handle = LockBitMapTags(the_screen->RastPort.BitMap, - LBMI_BASEADDRESS, (ULONG)&VideoMonitor.mac_frame_base, - TAG_END); + LBMI_BASEADDRESS, (ULONG)&frame_base, + TAG_END + ); UnLockBitMap(handle); - VideoMonitor.bytes_per_row = GetCyberMapAttr(the_screen->RastPort.BitMap, CYBRMATTR_XMOD); + + VideoMonitor.mac_frame_base = frame_base; + return true; } bool VideoInit(bool classic) { + int default_width, default_height, default_depth; + + // Allocate blank mouse pointer data + null_pointer = (UWORD *)AllocMem(12, MEMF_PUBLIC | MEMF_CHIP | MEMF_CLEAR); + if (null_pointer == NULL) { + ErrorAlert(STR_NO_MEM_ERR); + return false; + } + // Read frame skip prefs frame_skip = PrefsFindInt32("frameskip"); if (frame_skip == 0) @@ -366,44 +377,134 @@ bool VideoInit(bool classic) // Determine type and mode display_type = DISPLAY_WINDOW; - int width = 512, height = 384; - ULONG mode_id = 0; + + default_width = window_width = 512; + default_height = window_height = 384; + if (mode_str) { - if (sscanf(mode_str, "win/%d/%d", &width, &height) == 2) + if (sscanf(mode_str, "win/%d/%d", &window_width, &window_height) == 2) display_type = DISPLAY_WINDOW; - else if (sscanf(mode_str, "pip/%d/%d", &width, &height) == 2 && P96Base) + else if (sscanf(mode_str, "pip/%d/%d", &window_width, &window_height) == 2 && P96Base) display_type = DISPLAY_PIP; - else if (sscanf(mode_str, "scr/%08lx", &mode_id) == 1 && (CyberGfxBase || P96Base)) { - if (P96Base && p96GetModeIDAttr(mode_id, P96IDA_ISP96)) + else if (sscanf(mode_str, "scr/%08lx", &screen_mode_id) == 1 && (CyberGfxBase || P96Base)) { + if (P96Base && p96GetModeIDAttr(screen_mode_id, P96IDA_ISP96)) display_type = DISPLAY_SCREEN_P96; - else if (CyberGfxBase && IsCyberModeID(mode_id)) + else if (CyberGfxBase && IsCyberModeID(screen_mode_id)) display_type = DISPLAY_SCREEN_CGFX; else { - ErrorAlert(GetString(STR_NO_P96_MODE_ERR)); + ErrorAlert(STR_NO_P96_MODE_ERR); return false; } } } + // Construct list of supported modes + switch (display_type) { + case DISPLAY_WINDOW: + default_width = window_width; + default_height = window_height; + default_depth = 1; + add_modes(window_width, window_height, VDEPTH_1BIT); + break; + + case DISPLAY_PIP: + default_depth = 16; + default_width = window_width; + default_height = window_height; + add_modes(window_width, window_height, VDEPTH_16BIT); + break; + + case DISPLAY_SCREEN_P96: + case DISPLAY_SCREEN_CGFX: + struct DimensionInfo dimInfo; + DisplayInfoHandle handle = FindDisplayInfo(screen_mode_id); + + if (NULL == handle) + return false; + + if (GetDisplayInfoData(handle, (UBYTE *) &dimInfo, sizeof(dimInfo), DTAG_DIMS, 0) <= 0) + return false; + + default_width = 1 + dimInfo.Nominal.MaxX - dimInfo.Nominal.MinX; + default_height = 1 + dimInfo.Nominal.MaxY - dimInfo.Nominal.MinY; + default_depth = dimInfo.MaxDepth; + + for (unsigned d=VDEPTH_1BIT; d<=VDEPTH_32BIT; d++) + { + if (INVALID_ID != find_mode_for_depth(default_width, default_height, bits_from_depth(video_depth(d)))) + { + add_modes(default_width, default_height, video_depth(d)); + } + } + break; + } + + video_init_depth_list(); + +#if DEBUG + bug("Available video modes:\n"); + vector::const_iterator i = VideoModes.begin(), end = VideoModes.end(); + while (i != end) { + bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits_from_depth(i->depth)); + ++i; + } +#endif + + D(bug("VideoInit: def_width=%ld def_height=%ld def_depth=%ld\n", default_width, default_height, default_depth)); + + // Find requested default mode and open display + if (VideoModes.size() == 1) + return video_open(VideoModes[0]); + else { + // Find mode with specified dimensions + std::vector::const_iterator i, end = VideoModes.end(); + for (i = VideoModes.begin(); i != end; ++i) + { + D(bug("VideoInit: w=%ld h=%ld d=%ld\n", i->x, i->y, bits_from_depth(i->depth))); + if (i->x == default_width && i->y == default_height && bits_from_depth(i->depth) == default_depth) + return video_open(*i); + } + return video_open(VideoModes[0]); + } + + return true; +} + + +static bool video_open(const video_mode &mode) +{ + ULONG depth_bits = bits_from_depth(mode.depth); + ULONG ID = find_mode_for_depth(mode.x, mode.y, depth_bits); + + D(bug("video_open: width=%ld height=%ld depth=%ld ID=%08lx\n", mode.x, mode.y, depth_bits, ID)); + + if (INVALID_ID == ID) + { + ErrorAlert(STR_NO_VIDEO_MODE_ERR); + return false; + } + + VideoMonitor.mode = mode; + // Open display switch (display_type) { case DISPLAY_WINDOW: - if (!init_window(width, height)) + if (!init_window(mode.x, mode.y)) return false; break; case DISPLAY_PIP: - if (!init_pip(width, height)) + if (!init_pip(mode.x, mode.y)) return false; break; case DISPLAY_SCREEN_P96: - if (!init_screen_p96(mode_id)) + if (!init_screen_p96(ID)) return false; break; case DISPLAY_SCREEN_CGFX: - if (!init_screen_cgfx(mode_id)) + if (!init_screen_cgfx(ID)) return false; break; } @@ -416,18 +517,15 @@ bool VideoInit(bool classic) TAG_END ); if (periodic_proc == NULL) { - ErrorAlert(GetString(STR_NO_MEM_ERR)); + ErrorAlert(STR_NO_MEM_ERR); return false; } + return true; } -/* - * Deinitialization - */ - -void VideoExit(void) +static void video_close() { // Stop periodic process if (periodic_proc) { @@ -452,6 +550,7 @@ void VideoExit(void) ReleasePen(the_win->WScreen->ViewPort.ColorMap, white_pen); CloseWindow(the_win); + the_win = NULL; } break; @@ -466,7 +565,10 @@ void VideoExit(void) // Close window if (the_win) + { CloseWindow(the_win); + the_win = NULL; + } // Close screen if (the_screen) { @@ -479,7 +581,10 @@ void VideoExit(void) // Close window if (the_win) + { CloseWindow(the_win); + the_win = NULL; + } // Close screen if (the_screen) { @@ -488,6 +593,22 @@ void VideoExit(void) } break; } + + // Free mouse pointer + if (null_pointer) { + FreeMem(null_pointer, 12); + null_pointer = NULL; + } +} + + +/* + * Deinitialization + */ + +void VideoExit(void) +{ + video_close(); } @@ -495,15 +616,16 @@ void VideoExit(void) * Set palette */ -void video_set_palette(uint8 *pal) +void video_set_palette(uint8 *pal, int num) { - if (display_type == DISPLAY_SCREEN_P96 || display_type == DISPLAY_SCREEN_CGFX) { + if ((display_type == DISPLAY_SCREEN_P96 || display_type == DISPLAY_SCREEN_CGFX) + && !IsDirectMode(VideoMonitor.mode)) { // Convert palette to 32 bits ULONG table[2 + 256 * 3]; - table[0] = 256 << 16; - table[256 * 3 + 1] = 0; - for (int i=0; i<256; i++) { + table[0] = num << 16; + table[num * 3 + 1] = 0; + for (int i=0; iPlanes[0], 0, the_bitmap->BytesPerRow, the_win->RPort, - the_win->BorderLeft, the_win->BorderTop, VideoMonitor.x, VideoMonitor.y); + the_win->BorderLeft, the_win->BorderTop, VideoMonitor.mode.x, VideoMonitor.mode.y); // Restart timer timer_io->tr_node.io_Command = TR_ADDREQUEST; @@ -598,10 +745,26 @@ static __saveds void periodic_func(void) // Handle message according to class switch (cl) { case IDCMP_MOUSEMOVE: - if (display_type == DISPLAY_SCREEN || display_type == DISPLAY_SCREEN_CGFX) + if (display_type == DISPLAY_SCREEN_P96 || display_type == DISPLAY_SCREEN_CGFX) ADBMouseMoved(mx, my); - else + else { ADBMouseMoved(mx - the_win->BorderLeft, my - the_win->BorderTop); + if (mx < the_win->BorderLeft + || my < the_win->BorderTop + || mx >= the_win->BorderLeft + VideoMonitor.mode.x + || my >= the_win->BorderTop + VideoMonitor.mode.y) { + if (current_pointer) { + ClearPointer(the_win); + current_pointer = NULL; + } + } else { + if (current_pointer != null_pointer) { + // Hide mouse pointer inside window + SetPointer(the_win, null_pointer, 1, 16, 0, 0); + current_pointer = null_pointer; + } + } + } break; case IDCMP_MOUSEBUTTONS: @@ -622,6 +785,13 @@ static __saveds void periodic_func(void) case IDCMP_RAWKEY: if (qualifier & IEQUALIFIER_REPEAT) // Keyboard repeat is done by MacOS break; + if ((qualifier & (IEQUALIFIER_LALT | IEQUALIFIER_LSHIFT | IEQUALIFIER_CONTROL)) == + (IEQUALIFIER_LALT | IEQUALIFIER_LSHIFT | IEQUALIFIER_CONTROL) && code == 0x5f) { + SetInterruptFlag(INTFLAG_NMI); + TriggerInterrupt(); + break; + } + if (code & IECODE_UP_PREFIX) ADBKeyUp(keycode2mac[code & 0x7f]); else @@ -663,3 +833,64 @@ static __saveds void periodic_func(void) Forbid(); Signal(MainTask, SIGF_SINGLE); } + + +// Add mode to list of supported modes +static void add_mode(uint32 width, uint32 height, uint32 resolution_id, uint32 bytes_per_row, video_depth depth) +{ + video_mode mode; + mode.x = width; + mode.y = height; + mode.resolution_id = resolution_id; + mode.bytes_per_row = bytes_per_row; + mode.depth = depth; + + D(bug("Added video mode: w=%ld h=%ld d=%ld\n", width, height, depth)); + + VideoModes.push_back(mode); +} + +// Add standard list of windowed modes for given color depth +static void add_modes(uint32 width, uint32 height, video_depth depth) +{ + D(bug("add_modes: w=%ld h=%ld d=%ld\n", width, height, depth)); + + if (width >= 512 && height >= 384) + add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth), depth); + if (width >= 640 && height >= 480) + add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth), depth); + if (width >= 800 && height >= 600) + add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth), depth); + if (width >= 1024 && height >= 768) + add_mode(1024, 768, 0x83, TrivialBytesPerRow(1024, depth), depth); + if (width >= 1152 && height >= 870) + add_mode(1152, 870, 0x84, TrivialBytesPerRow(1152, depth), depth); + if (width >= 1280 && height >= 1024) + add_mode(1280, 1024, 0x85, TrivialBytesPerRow(1280, depth), depth); + if (width >= 1600 && height >= 1200) + add_mode(1600, 1200, 0x86, TrivialBytesPerRow(1600, depth), depth); +} + + +static ULONG find_mode_for_depth(uint32 width, uint32 height, uint32 depth) +{ + ULONG ID = BestModeID(BIDTAG_NominalWidth, width, + BIDTAG_NominalHeight, height, + BIDTAG_Depth, depth, + TAG_END); + + return ID; +} + + +static ULONG bits_from_depth(video_depth depth) +{ + int bits = 1 << depth; + if (bits == 16) + bits = 15; + else if (bits == 32) + bits = 24; + + return bits; +} +