ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/AmigaOS/video_amiga.cpp
Revision: 1.17
Committed: 2001-07-01T21:09:27Z (23 years, 4 months ago) by cebix
Branch: MAIN
Changes since 1.16: +4 -4 lines
Log Message:
- video_set_palette() gets passed the number of used palette entries
- video_x.cpp supports 2- and 4-bit modes on truecolor screens

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * video_amiga.cpp - Video/graphics emulation, AmigaOS specific stuff
3     *
4 cebix 1.12 * Basilisk II (C) 1997-2001 Christian Bauer
5 cebix 1.1 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     #include <exec/types.h>
22     #include <intuition/intuition.h>
23     #include <graphics/rastport.h>
24     #include <graphics/gfx.h>
25 cebix 1.7 #include <cybergraphics/cybergraphics.h>
26 cebix 1.1 #include <dos/dostags.h>
27     #include <devices/timer.h>
28     #include <proto/exec.h>
29     #include <proto/dos.h>
30     #include <proto/intuition.h>
31     #include <proto/graphics.h>
32     #include <proto/Picasso96.h>
33 cebix 1.4 #include <proto/cybergraphics.h>
34 cebix 1.1
35     #include "sysdeps.h"
36 jlachmann 1.9 #include "cpu_emulation.h"
37 cebix 1.1 #include "main.h"
38     #include "adb.h"
39     #include "prefs.h"
40     #include "user_strings.h"
41     #include "video.h"
42    
43 cebix 1.4 #define DEBUG 0
44 cebix 1.1 #include "debug.h"
45    
46    
47     // Display types
48     enum {
49     DISPLAY_WINDOW,
50     DISPLAY_PIP,
51 cebix 1.6 DISPLAY_SCREEN_P96,
52     DISPLAY_SCREEN_CGFX
53 cebix 1.1 };
54    
55     // Global variables
56     static int32 frame_skip;
57     static int display_type = DISPLAY_WINDOW; // See enum above
58     static struct Screen *the_screen = NULL;
59     static struct Window *the_win = NULL;
60     static struct BitMap *the_bitmap = NULL;
61 cebix 1.8 static UWORD *null_pointer = NULL; // Blank mouse pointer data
62     static UWORD *current_pointer = (UWORD *)-1; // Currently visible mouse pointer data
63 cebix 1.1 static LONG black_pen = -1, white_pen = -1;
64     static struct Process *periodic_proc = NULL; // Periodic process
65    
66     extern struct Task *MainTask; // Pointer to main task (from main_amiga.cpp)
67    
68    
69     // Amiga -> Mac raw keycode translation table
70     static const uint8 keycode2mac[0x80] = {
71     0x0a, 0x12, 0x13, 0x14, 0x15, 0x17, 0x16, 0x1a, // ` 1 2 3 4 5 6 7
72     0x1c, 0x19, 0x1d, 0x1b, 0x18, 0x2a, 0xff, 0x52, // 8 9 0 - = \ inv 0
73     0x0c, 0x0d, 0x0e, 0x0f, 0x11, 0x10, 0x20, 0x22, // Q W E R T Y U I
74     0x1f, 0x23, 0x21, 0x1e, 0xff, 0x53, 0x54, 0x55, // O P [ ] inv 1 2 3
75     0x00, 0x01, 0x02, 0x03, 0x05, 0x04, 0x26, 0x28, // A S D F G H J K
76     0x25, 0x29, 0x27, 0x2a, 0xff, 0x56, 0x57, 0x58, // L ; ' # inv 4 5 6
77     0x32, 0x06, 0x07, 0x08, 0x09, 0x0b, 0x2d, 0x2e, // < Z X C V B N M
78     0x2b, 0x2f, 0x2c, 0xff, 0x41, 0x59, 0x5b, 0x5c, // , . / inv . 7 8 9
79     0x31, 0x33, 0x30, 0x4c, 0x24, 0x35, 0x75, 0xff, // SPC BSP TAB ENT RET ESC DEL inv
80     0xff, 0xff, 0x4e, 0xff, 0x3e, 0x3d, 0x3c, 0x3b, // inv inv - inv CUP CDN CRT CLF
81     0x7a, 0x78, 0x63, 0x76, 0x60, 0x61, 0x62, 0x64, // F1 F2 F3 F4 F5 F6 F7 F8
82     0x65, 0x6d, 0x47, 0x51, 0x4b, 0x43, 0x45, 0x72, // F9 F10 ( ) / * + HLP
83     0x38, 0x38, 0x39, 0x36, 0x3a, 0x3a, 0x37, 0x37, // SHL SHR CAP CTL ALL ALR AML AMR
84     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // inv inv inv inv inv inv inv inv
85     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // inv inv inv inv inv inv inv inv
86     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff // inv inv inv inv inv inv inv inv
87     };
88    
89    
90     // Prototypes
91     static void periodic_func(void);
92    
93    
94     /*
95     * Initialization
96     */
97    
98 cebix 1.13 // Add resolution to list of supported modes and set VideoMonitor
99     static void set_video_monitor(uint32 width, uint32 height, uint32 bytes_per_row, int depth)
100     {
101     video_mode mode;
102    
103     mode.x = width;
104     mode.y = height;
105     mode.resolution_id = 0x80;
106     mode.bytes_per_row = bytes_per_row;
107    
108     switch (depth) {
109     case 1:
110     mode.depth = VDEPTH_1BIT;
111     break;
112     case 2:
113     mode.depth = VDEPTH_2BIT;
114     break;
115     case 4:
116     mode.depth = VDEPTH_4BIT;
117     break;
118     case 8:
119     mode.depth = VDEPTH_8BIT;
120     break;
121     case 15:
122     case 16:
123     mode.depth = VDEPTH_16BIT;
124     break;
125     case 24:
126     case 32:
127     mode.depth = VDEPTH_32BIT;
128     break;
129     }
130    
131     VideoModes.push_back(mode);
132     VideoMonitor.mode = mode;
133     }
134    
135 cebix 1.1 // Open window
136     static bool init_window(int width, int height)
137     {
138     // Set absolute mouse mode
139     ADBSetRelMouseMode(false);
140    
141     // Open window
142     the_win = OpenWindowTags(NULL,
143     WA_Left, 0, WA_Top, 0,
144     WA_InnerWidth, width, WA_InnerHeight, height,
145     WA_SimpleRefresh, TRUE,
146     WA_NoCareRefresh, TRUE,
147     WA_Activate, TRUE,
148     WA_RMBTrap, TRUE,
149     WA_ReportMouse, TRUE,
150     WA_DragBar, TRUE,
151     WA_DepthGadget, TRUE,
152     WA_SizeGadget, FALSE,
153 cebix 1.2 WA_Title, (ULONG)GetString(STR_WINDOW_TITLE),
154 cebix 1.1 TAG_END
155     );
156     if (the_win == NULL) {
157 cebix 1.16 ErrorAlert(STR_OPEN_WINDOW_ERR);
158 cebix 1.1 return false;
159     }
160    
161     // Create bitmap ("height + 2" for safety)
162     the_bitmap = AllocBitMap(width, height + 2, 1, BMF_CLEAR, NULL);
163     if (the_bitmap == NULL) {
164 cebix 1.16 ErrorAlert(STR_NO_MEM_ERR);
165 cebix 1.1 return false;
166     }
167    
168 cebix 1.13 // Add resolution and set VideoMonitor
169     set_video_monitor(width, height, the_bitmap->BytesPerRow, 1);
170 cebix 1.1 VideoMonitor.mac_frame_base = (uint32)the_bitmap->Planes[0];
171    
172     // Set FgPen and BgPen
173     black_pen = ObtainBestPenA(the_win->WScreen->ViewPort.ColorMap, 0, 0, 0, NULL);
174     white_pen = ObtainBestPenA(the_win->WScreen->ViewPort.ColorMap, 0xffffffff, 0xffffffff, 0xffffffff, NULL);
175     SetAPen(the_win->RPort, black_pen);
176     SetBPen(the_win->RPort, white_pen);
177     SetDrMd(the_win->RPort, JAM2);
178     return true;
179     }
180    
181     // Open PIP (requires Picasso96)
182     static bool init_pip(int width, int height)
183     {
184     // Set absolute mouse mode
185     ADBSetRelMouseMode(false);
186    
187     // Open window
188     ULONG error = 0;
189     the_win = p96PIP_OpenTags(
190     P96PIP_SourceFormat, RGBFB_R5G5B5,
191     P96PIP_SourceWidth, width,
192     P96PIP_SourceHeight, height,
193 cebix 1.2 P96PIP_ErrorCode, (ULONG)&error,
194 cebix 1.1 WA_Left, 0, WA_Top, 0,
195     WA_InnerWidth, width, WA_InnerHeight, height,
196     WA_SimpleRefresh, TRUE,
197     WA_NoCareRefresh, TRUE,
198     WA_Activate, TRUE,
199     WA_RMBTrap, TRUE,
200     WA_ReportMouse, TRUE,
201     WA_DragBar, TRUE,
202     WA_DepthGadget, TRUE,
203     WA_SizeGadget, FALSE,
204 cebix 1.2 WA_Title, (ULONG)GetString(STR_WINDOW_TITLE),
205     WA_PubScreenName, (ULONG)"Workbench",
206 cebix 1.1 TAG_END
207     );
208     if (the_win == NULL || error) {
209 cebix 1.16 ErrorAlert(STR_OPEN_WINDOW_ERR);
210 cebix 1.1 return false;
211     }
212    
213     // Find bitmap
214 cebix 1.2 p96PIP_GetTags(the_win, P96PIP_SourceBitMap, (ULONG)&the_bitmap, TAG_END);
215 cebix 1.1
216 cebix 1.13 // Add resolution and set VideoMonitor
217 cebix 1.15 VideoMonitor.mac_frame_base = p96GetBitMapAttr(the_bitmap, P96BMA_MEMORY);
218 cebix 1.13 set_video_monitor(width, height, p96GetBitMapAttr(the_bitmap, P96BMA_BYTESPERROW), 16);
219 cebix 1.1 return true;
220     }
221    
222 cebix 1.6 // Open Picasso96 screen
223     static bool init_screen_p96(ULONG mode_id)
224 cebix 1.1 {
225     // Set relative mouse mode
226     ADBSetRelMouseMode(true);
227    
228 cebix 1.6 // Check if the mode is one we can handle
229     uint32 depth = p96GetModeIDAttr(mode_id, P96IDA_DEPTH);
230     uint32 format = p96GetModeIDAttr(mode_id, P96IDA_RGBFORMAT);
231    
232     switch (depth) {
233     case 8:
234     break;
235     case 15:
236     case 16:
237     if (format != RGBFB_R5G5B5) {
238 cebix 1.16 ErrorAlert(STR_WRONG_SCREEN_FORMAT_ERR);
239 cebix 1.6 return false;
240     }
241     break;
242     case 24:
243     case 32:
244     if (format != RGBFB_A8R8G8B8) {
245 cebix 1.16 ErrorAlert(STR_WRONG_SCREEN_FORMAT_ERR);
246 cebix 1.6 return false;
247     }
248     break;
249     default:
250 cebix 1.16 ErrorAlert(STR_WRONG_SCREEN_DEPTH_ERR);
251 cebix 1.6 return false;
252     }
253    
254     // Yes, get width and height
255     uint32 width = p96GetModeIDAttr(mode_id, P96IDA_WIDTH);
256     uint32 height = p96GetModeIDAttr(mode_id, P96IDA_HEIGHT);
257    
258     // Open screen
259     the_screen = p96OpenScreenTags(
260     P96SA_DisplayID, mode_id,
261     P96SA_Title, (ULONG)GetString(STR_WINDOW_TITLE),
262     P96SA_Quiet, TRUE,
263     P96SA_NoMemory, TRUE,
264     P96SA_NoSprite, TRUE,
265     P96SA_Exclusive, TRUE,
266     TAG_END
267     );
268     if (the_screen == NULL) {
269 cebix 1.16 ErrorAlert(STR_OPEN_SCREEN_ERR);
270 cebix 1.1 return false;
271     }
272    
273 cebix 1.6 // Open window
274     the_win = OpenWindowTags(NULL,
275     WA_Left, 0, WA_Top, 0,
276     WA_Width, width, WA_Height, height,
277     WA_NoCareRefresh, TRUE,
278     WA_Borderless, TRUE,
279     WA_Activate, TRUE,
280     WA_RMBTrap, TRUE,
281     WA_ReportMouse, TRUE,
282     WA_CustomScreen, (ULONG)the_screen,
283     TAG_END
284     );
285     if (the_win == NULL) {
286 cebix 1.16 ErrorAlert(STR_OPEN_WINDOW_ERR);
287 cebix 1.6 return false;
288     }
289    
290     ScreenToFront(the_screen);
291 cebix 1.13
292     // Add resolution and set VideoMonitor
293     set_video_monitor(width, height, p96GetBitMapAttr(the_screen->RastPort.BitMap, P96BMA_BYTESPERROW), depth);
294 cebix 1.6 VideoMonitor.mac_frame_base = p96GetBitMapAttr(the_screen->RastPort.BitMap, P96BMA_MEMORY);
295     return true;
296     }
297    
298     // Open CyberGraphX screen
299     static bool init_screen_cgfx(ULONG mode_id)
300     {
301     // Set relative mouse mode
302     ADBSetRelMouseMode(true);
303 cebix 1.4
304 cebix 1.1 // Check if the mode is one we can handle
305 cebix 1.6 uint32 depth = GetCyberIDAttr(CYBRIDATTR_DEPTH, mode_id);
306     uint32 format = GetCyberIDAttr(CYBRIDATTR_PIXFMT, mode_id);
307 cebix 1.4
308 cebix 1.1 switch (depth) {
309     case 8:
310     break;
311     case 15:
312     case 16:
313 jlachmann 1.9 // !!! PIXFMT_RGB15 is correct !!!
314     if (format != PIXFMT_RGB15) {
315 cebix 1.16 ErrorAlert(STR_WRONG_SCREEN_FORMAT_ERR);
316 cebix 1.1 return false;
317     }
318     break;
319     case 24:
320     case 32:
321 cebix 1.6 if (format != PIXFMT_ARGB32) {
322 cebix 1.16 ErrorAlert(STR_WRONG_SCREEN_FORMAT_ERR);
323 cebix 1.1 return false;
324     }
325     break;
326     default:
327 cebix 1.16 ErrorAlert(STR_WRONG_SCREEN_DEPTH_ERR);
328 cebix 1.1 return false;
329     }
330    
331     // Yes, get width and height
332 cebix 1.6 uint32 width = GetCyberIDAttr(CYBRIDATTR_WIDTH, mode_id);
333     uint32 height = GetCyberIDAttr(CYBRIDATTR_HEIGHT, mode_id);
334 cebix 1.4
335 cebix 1.1 // Open screen
336 cebix 1.6 the_screen = OpenScreenTags(NULL,
337     SA_DisplayID, mode_id,
338     SA_Title, (ULONG)GetString(STR_WINDOW_TITLE),
339     SA_Quiet, TRUE,
340     SA_Exclusive, TRUE,
341     TAG_END
342     );
343 cebix 1.1 if (the_screen == NULL) {
344 cebix 1.16 ErrorAlert(STR_OPEN_SCREEN_ERR);
345 cebix 1.1 return false;
346     }
347    
348     // Open window
349     the_win = OpenWindowTags(NULL,
350     WA_Left, 0, WA_Top, 0,
351     WA_Width, width, WA_Height, height,
352     WA_NoCareRefresh, TRUE,
353     WA_Borderless, TRUE,
354     WA_Activate, TRUE,
355     WA_RMBTrap, TRUE,
356     WA_ReportMouse, TRUE,
357 cebix 1.2 WA_CustomScreen, (ULONG)the_screen,
358 cebix 1.1 TAG_END
359     );
360     if (the_win == NULL) {
361 cebix 1.16 ErrorAlert(STR_OPEN_WINDOW_ERR);
362 cebix 1.1 return false;
363     }
364    
365     ScreenToFront(the_screen);
366 cebix 1.6 static UWORD ptr[] = { 0, 0, 0, 0 };
367     SetPointer(the_win, ptr, 0, 0, 0, 0); // Hide mouse pointer
368 cebix 1.13
369     // Set VideoMonitor
370     ULONG frame_base;
371 cebix 1.6 APTR handle = LockBitMapTags(the_screen->RastPort.BitMap,
372 cebix 1.13 LBMI_BASEADDRESS, (ULONG)&frame_base,
373     TAG_END
374     );
375 cebix 1.6 UnLockBitMap(handle);
376 cebix 1.13 set_video_monitor(width, height, GetCyberMapAttr(the_screen->RastPort.BitMap, CYBRMATTR_XMOD), depth);
377     VideoMonitor.mac_frame_base = frame_base;
378 cebix 1.1 return true;
379     }
380    
381     bool VideoInit(bool classic)
382     {
383 cebix 1.8 // Allocate blank mouse pointer data
384     null_pointer = (UWORD *)AllocMem(12, MEMF_PUBLIC | MEMF_CHIP | MEMF_CLEAR);
385     if (null_pointer == NULL) {
386 cebix 1.16 ErrorAlert(STR_NO_MEM_ERR);
387 cebix 1.8 return false;
388     }
389    
390 cebix 1.1 // Read frame skip prefs
391     frame_skip = PrefsFindInt32("frameskip");
392     if (frame_skip == 0)
393     frame_skip = 1;
394    
395     // Get screen mode from preferences
396     const char *mode_str;
397     if (classic)
398     mode_str = "win/512/342";
399     else
400     mode_str = PrefsFindString("screen");
401    
402     // Determine type and mode
403     display_type = DISPLAY_WINDOW;
404     int width = 512, height = 384;
405     ULONG mode_id = 0;
406     if (mode_str) {
407     if (sscanf(mode_str, "win/%d/%d", &width, &height) == 2)
408     display_type = DISPLAY_WINDOW;
409     else if (sscanf(mode_str, "pip/%d/%d", &width, &height) == 2 && P96Base)
410     display_type = DISPLAY_PIP;
411 cebix 1.6 else if (sscanf(mode_str, "scr/%08lx", &mode_id) == 1 && (CyberGfxBase || P96Base)) {
412     if (P96Base && p96GetModeIDAttr(mode_id, P96IDA_ISP96))
413     display_type = DISPLAY_SCREEN_P96;
414     else if (CyberGfxBase && IsCyberModeID(mode_id))
415     display_type = DISPLAY_SCREEN_CGFX;
416     else {
417 cebix 1.16 ErrorAlert(STR_NO_P96_MODE_ERR);
418 cebix 1.6 return false;
419     }
420     }
421 cebix 1.1 }
422    
423     // Open display
424     switch (display_type) {
425     case DISPLAY_WINDOW:
426     if (!init_window(width, height))
427     return false;
428     break;
429    
430     case DISPLAY_PIP:
431     if (!init_pip(width, height))
432     return false;
433     break;
434    
435 cebix 1.6 case DISPLAY_SCREEN_P96:
436     if (!init_screen_p96(mode_id))
437     return false;
438     break;
439    
440     case DISPLAY_SCREEN_CGFX:
441     if (!init_screen_cgfx(mode_id))
442 cebix 1.1 return false;
443     break;
444     }
445    
446     // Start periodic process
447     periodic_proc = CreateNewProcTags(
448 cebix 1.2 NP_Entry, (ULONG)periodic_func,
449     NP_Name, (ULONG)"Basilisk II IDCMP Handler",
450 cebix 1.1 NP_Priority, 0,
451     TAG_END
452     );
453     if (periodic_proc == NULL) {
454 cebix 1.16 ErrorAlert(STR_NO_MEM_ERR);
455 cebix 1.1 return false;
456     }
457     return true;
458     }
459    
460    
461     /*
462     * Deinitialization
463     */
464    
465     void VideoExit(void)
466     {
467     // Stop periodic process
468     if (periodic_proc) {
469     SetSignal(0, SIGF_SINGLE);
470     Signal(&periodic_proc->pr_Task, SIGBREAKF_CTRL_C);
471     Wait(SIGF_SINGLE);
472     }
473    
474     switch (display_type) {
475    
476     case DISPLAY_WINDOW:
477    
478     // Window mode, free bitmap
479     if (the_bitmap) {
480     WaitBlit();
481     FreeBitMap(the_bitmap);
482     }
483    
484     // Free pens and close window
485     if (the_win) {
486     ReleasePen(the_win->WScreen->ViewPort.ColorMap, black_pen);
487     ReleasePen(the_win->WScreen->ViewPort.ColorMap, white_pen);
488    
489     CloseWindow(the_win);
490     }
491     break;
492    
493     case DISPLAY_PIP:
494    
495     // Close PIP
496     if (the_win)
497     p96PIP_Close(the_win);
498     break;
499    
500 cebix 1.6 case DISPLAY_SCREEN_P96:
501 cebix 1.1
502     // Close window
503     if (the_win)
504     CloseWindow(the_win);
505    
506     // Close screen
507 cebix 1.4 if (the_screen) {
508 cebix 1.6 p96CloseScreen(the_screen);
509     the_screen = NULL;
510     }
511     break;
512    
513     case DISPLAY_SCREEN_CGFX:
514    
515     // Close window
516     if (the_win)
517     CloseWindow(the_win);
518 cebix 1.4
519 cebix 1.6 // Close screen
520     if (the_screen) {
521     CloseScreen(the_screen);
522 cebix 1.4 the_screen = NULL;
523     }
524 cebix 1.1 break;
525     }
526 cebix 1.8
527     // Free mouse pointer
528     if (null_pointer) {
529     FreeMem(null_pointer, 12);
530     null_pointer = NULL;
531     }
532 cebix 1.1 }
533    
534    
535     /*
536     * Set palette
537     */
538    
539 cebix 1.17 void video_set_palette(uint8 *pal, in num)
540 cebix 1.1 {
541 cebix 1.15 if ((display_type == DISPLAY_SCREEN_P96 || display_type == DISPLAY_SCREEN_CGFX)
542     && !IsDirectMode(VideoMonitor.mode)) {
543 cebix 1.1
544     // Convert palette to 32 bits
545     ULONG table[2 + 256 * 3];
546 cebix 1.17 table[0] = num << 16;
547     table[num * 3 + 1] = 0;
548     for (int i=0; i<num; i++) {
549 cebix 1.1 table[i*3+1] = pal[i*3] * 0x01010101;
550     table[i*3+2] = pal[i*3+1] * 0x01010101;
551     table[i*3+3] = pal[i*3+2] * 0x01010101;
552     }
553    
554     // And load it
555     LoadRGB32(&the_screen->ViewPort, table);
556     }
557     }
558    
559    
560     /*
561 cebix 1.14 * Switch video mode
562     */
563    
564     void video_switch_to_mode(const video_mode &mode)
565     {
566     }
567    
568    
569     /*
570 cebix 1.1 * Video message handling (not neccessary under AmigaOS, handled by periodic_func())
571     */
572    
573     void VideoInterrupt(void)
574     {
575     }
576    
577    
578     /*
579     * Process for window refresh and message handling
580     */
581    
582     static __saveds void periodic_func(void)
583     {
584     struct MsgPort *timer_port = NULL;
585     struct timerequest *timer_io = NULL;
586     struct IntuiMessage *msg;
587     ULONG win_mask = 0, timer_mask = 0;
588    
589     // Create message port for window and attach it
590     struct MsgPort *win_port = CreateMsgPort();
591     if (win_port) {
592     win_mask = 1 << win_port->mp_SigBit;
593     the_win->UserPort = win_port;
594 cebix 1.6 ModifyIDCMP(the_win, IDCMP_MOUSEBUTTONS | IDCMP_MOUSEMOVE | IDCMP_RAWKEY | ((display_type == DISPLAY_SCREEN_P96 || display_type == DISPLAY_SCREEN_CGFX) ? IDCMP_DELTAMOVE : 0));
595 cebix 1.1 }
596    
597     // Start 60Hz timer for window refresh
598     if (display_type == DISPLAY_WINDOW) {
599     timer_port = CreateMsgPort();
600     if (timer_port) {
601     timer_io = (struct timerequest *)CreateIORequest(timer_port, sizeof(struct timerequest));
602     if (timer_io) {
603     if (!OpenDevice((UBYTE *)TIMERNAME, UNIT_MICROHZ, (struct IORequest *)timer_io, 0)) {
604     timer_mask = 1 << timer_port->mp_SigBit;
605     timer_io->tr_node.io_Command = TR_ADDREQUEST;
606     timer_io->tr_time.tv_secs = 0;
607     timer_io->tr_time.tv_micro = 16667 * frame_skip;
608     SendIO((struct IORequest *)timer_io);
609     }
610     }
611     }
612     }
613    
614     // Main loop
615     for (;;) {
616    
617     // Wait for timer and/or window (CTRL_C is used for quitting the task)
618     ULONG sig = Wait(win_mask | timer_mask | SIGBREAKF_CTRL_C);
619    
620     if (sig & SIGBREAKF_CTRL_C)
621     break;
622    
623     if (sig & timer_mask) {
624    
625     // Timer tick, update display
626     BltTemplate(the_bitmap->Planes[0], 0, the_bitmap->BytesPerRow, the_win->RPort,
627 cebix 1.13 the_win->BorderLeft, the_win->BorderTop, VideoMonitor.mode.x, VideoMonitor.mode.y);
628 cebix 1.1
629     // Restart timer
630     timer_io->tr_node.io_Command = TR_ADDREQUEST;
631     timer_io->tr_time.tv_secs = 0;
632     timer_io->tr_time.tv_micro = 16667 * frame_skip;
633     SendIO((struct IORequest *)timer_io);
634     }
635    
636     if (sig & win_mask) {
637    
638     // Handle window messages
639     while (msg = (struct IntuiMessage *)GetMsg(win_port)) {
640    
641     // Get data from message and reply
642     ULONG cl = msg->Class;
643     UWORD code = msg->Code;
644     UWORD qualifier = msg->Qualifier;
645     WORD mx = msg->MouseX;
646     WORD my = msg->MouseY;
647     ReplyMsg((struct Message *)msg);
648    
649     // Handle message according to class
650     switch (cl) {
651     case IDCMP_MOUSEMOVE:
652 cebix 1.7 if (display_type == DISPLAY_SCREEN_P96 || display_type == DISPLAY_SCREEN_CGFX)
653 cebix 1.1 ADBMouseMoved(mx, my);
654 cebix 1.8 else {
655 cebix 1.1 ADBMouseMoved(mx - the_win->BorderLeft, my - the_win->BorderTop);
656 cebix 1.8 if (mx < the_win->BorderLeft
657     || my < the_win->BorderTop
658 cebix 1.13 || mx >= the_win->BorderLeft + VideoMonitor.mode.x
659     || my >= the_win->BorderTop + VideoMonitor.mode.y) {
660 cebix 1.8 if (current_pointer) {
661     ClearPointer(the_win);
662     current_pointer = NULL;
663     }
664     } else {
665     if (current_pointer != null_pointer) {
666     // Hide mouse pointer inside window
667     SetPointer(the_win, null_pointer, 1, 16, 0, 0);
668     current_pointer = null_pointer;
669     }
670     }
671     }
672 cebix 1.1 break;
673    
674     case IDCMP_MOUSEBUTTONS:
675     if (code == SELECTDOWN)
676     ADBMouseDown(0);
677     else if (code == SELECTUP)
678     ADBMouseUp(0);
679     else if (code == MENUDOWN)
680     ADBMouseDown(1);
681     else if (code == MENUUP)
682     ADBMouseUp(1);
683     else if (code == MIDDLEDOWN)
684     ADBMouseDown(2);
685     else if (code == MIDDLEUP)
686     ADBMouseUp(2);
687     break;
688    
689     case IDCMP_RAWKEY:
690     if (qualifier & IEQUALIFIER_REPEAT) // Keyboard repeat is done by MacOS
691     break;
692 cebix 1.11 if ((qualifier & (IEQUALIFIER_LALT | IEQUALIFIER_LSHIFT | IEQUALIFIER_CONTROL)) ==
693     (IEQUALIFIER_LALT | IEQUALIFIER_LSHIFT | IEQUALIFIER_CONTROL) && code == 0x5f) {
694 jlachmann 1.9 SetInterruptFlag(INTFLAG_NMI);
695     TriggerInterrupt();
696     break;
697 cebix 1.10 }
698 jlachmann 1.9
699 cebix 1.1 if (code & IECODE_UP_PREFIX)
700     ADBKeyUp(keycode2mac[code & 0x7f]);
701     else
702     ADBKeyDown(keycode2mac[code & 0x7f]);
703     break;
704     }
705     }
706     }
707     }
708    
709     // Stop timer
710     if (timer_io) {
711     if (!CheckIO((struct IORequest *)timer_io))
712     AbortIO((struct IORequest *)timer_io);
713     WaitIO((struct IORequest *)timer_io);
714     CloseDevice((struct IORequest *)timer_io);
715     DeleteIORequest(timer_io);
716     }
717     if (timer_port)
718     DeleteMsgPort(timer_port);
719    
720     // Remove port from window and delete it
721     Forbid();
722     msg = (struct IntuiMessage *)win_port->mp_MsgList.lh_Head;
723     struct Node *succ;
724     while (succ = msg->ExecMessage.mn_Node.ln_Succ) {
725     if (msg->IDCMPWindow == the_win) {
726     Remove((struct Node *)msg);
727     ReplyMsg((struct Message *)msg);
728     }
729     msg = (struct IntuiMessage *)succ;
730     }
731     the_win->UserPort = NULL;
732     ModifyIDCMP(the_win, 0);
733     Permit();
734     DeleteMsgPort(win_port);
735    
736     // Main task asked for termination, send signal
737     Forbid();
738     Signal(MainTask, SIGF_SINGLE);
739     }