Annotation of generator/main/uip-svgalib.c, revision 1.1.1.2

1.1       root        1: /* Generator is (c) James Ponder, 1997-2001 http://www.squish.net/generator/ */
                      2: 
                      3: /* user interface for svgalib (15/16 bit) */
                      4: 
1.1.1.2 ! root        5: /* this part of generator is 'slaved' from the ui-console.c file - only
        !             6:    platform specific code is contained here */
        !             7: 
1.1       root        8: #include <stdio.h>
                      9: #include <unistd.h>
                     10: 
                     11: #include "generator.h"
                     12: #include <vga.h>
                     13: #include <vgakeyboard.h>
1.1.1.2 ! root       14: #include <vgajoystick.h>
1.1       root       15: 
                     16: #include "uip.h"
                     17: #include "ui.h"
                     18: #include "ui-console.h"
                     19: #include "vdp.h"
1.1.1.2 ! root       20: #include "cpu68k.h"
1.1       root       21: #include "mem68k.h"
                     22: 
                     23: #define BANKSIZE 640*480*2
                     24: 
                     25: /*** forward reference declarations ***/
                     26: 
1.1.1.2 ! root       27: static int uip_getshifts(t_uipinfo * uipinfo);
1.1       root       28: 
                     29: /*** static variables ***/
                     30: 
1.1.1.2 ! root       31: static uint8 uip_vga = 0;       /* flag for whether in VGA mode */
        !            32: static uint8 uip_key[256];      /* keyboard state */
        !            33: static uint8 uip_displaybanknum = 0;    /* view this one, write to other one */
        !            34: static t_uipinfo *uip_uipinfo = NULL;   /* uipinfo */
        !            35: static int uip_forceredshift = -1;      /* if set, forces red shift pos */
        !            36: static int uip_forcegreenshift = -1;    /* if set, forces green shift pos */
        !            37: static int uip_forceblueshift = -1;     /* if set, forces blue shift pos */
1.1       root       38: 
                     39: /*** Code ***/
                     40: 
1.1.1.2 ! root       41: static void uip_keyboardhandler(int scancode, int press)
        !            42: {
1.1       root       43:   if (scancode >= 256)
                     44:     return;
                     45:   if (uip_key[scancode] == press)
                     46:     return;
                     47:   uip_key[scancode] = press;
                     48:   if (press) {
                     49:     if (scancode == SCANCODE_F1) {
1.1.1.2 ! root       50:       ui_fkeys |= 1 << 1;
1.1       root       51:     } else if (scancode == SCANCODE_F2) {
1.1.1.2 ! root       52:       ui_fkeys |= 1 << 2;
1.1       root       53:     } else if (scancode == SCANCODE_F3) {
1.1.1.2 ! root       54:       ui_fkeys |= 1 << 3;
1.1       root       55:     } else if (scancode == SCANCODE_F4) {
1.1.1.2 ! root       56:       ui_fkeys |= 1 << 4;
1.1       root       57:     } else if (scancode == SCANCODE_F5) {
1.1.1.2 ! root       58:       ui_fkeys |= 1 << 5;
1.1       root       59:     } else if (scancode == SCANCODE_F6) {
1.1.1.2 ! root       60:       ui_fkeys |= 1 << 6;
1.1       root       61:     } else if (scancode == SCANCODE_F7) {
1.1.1.2 ! root       62:       ui_fkeys |= 1 << 7;
1.1       root       63:     } else if (scancode == SCANCODE_F8) {
1.1.1.2 ! root       64:       ui_fkeys |= 1 << 8;
1.1       root       65:     } else if (scancode == SCANCODE_F9) {
1.1.1.2 ! root       66:       ui_fkeys |= 1 << 9;
1.1       root       67:     } else if (scancode == SCANCODE_F10) {
1.1.1.2 ! root       68:       ui_fkeys |= 1 << 10;
1.1       root       69:     } else if (scancode == SCANCODE_F11) {
1.1.1.2 ! root       70:       ui_fkeys |= 1 << 11;
1.1       root       71:     } else if (scancode == SCANCODE_F12) {
1.1.1.2 ! root       72:       ui_fkeys |= 1 << 12;
1.1       root       73:     }
                     74:   }
                     75: }
                     76: 
1.1.1.2 ! root       77: int uip_init(t_uipinfo * uipinfo)
1.1       root       78: {
                     79:   uip_uipinfo = uipinfo;
1.1.1.2 ! root       80:   LOG_REQUEST(("Initialising svgalib..."));
1.1       root       81:   sleep(1);
                     82:   if (vga_init()) {
                     83:     LOG_CRITICAL(("Failed to initialise SVGALib"));
                     84:     return 1;
                     85:   }
                     86:   return 0;
                     87: }
                     88: 
1.1.1.2 ! root       89: int uip_initjoysticks(void)
        !            90: {
        !            91:   LOG_REQUEST(("Checking for svgalib joysticks..."));
        !            92:   if (joystick_init(0, JOY_CALIB_STDOUT) < 0) {
        !            93:     LOG_VERBOSE(("Joystick 0 not found"));
        !            94:     return 0;
        !            95:   }
        !            96:   LOG_NORMAL(("Joystick 0 initialised"));
        !            97:   if (joystick_init(1, JOY_CALIB_STDOUT) < 0) {
        !            98:     LOG_VERBOSE(("Joystick 1 not found"));
        !            99:     return 1;
        !           100:   }
        !           101:   LOG_NORMAL(("Joystick 1 initialised"));
        !           102:   return 2;
        !           103: }
        !           104: 
1.1       root      105: int uip_vgamode(void)
                    106: {
                    107:   vga_modeinfo *modeinfo;
                    108:   int cardmemory;
                    109: 
                    110:   uip_vga = 1;
                    111:   if (vga_setmode(G640x480x32K) == -1) {
                    112:     LOG_CRITICAL(("640x480 @ 32K colours not supported by hardware"));
                    113:     LOG_CRITICAL(("Trying 640x480 @ 64K colours..."));
                    114:     if (vga_setmode(G640x480x64K) == -1) {
                    115:       LOG_CRITICAL(("640x480 @ 64K colours not supported by hardware"));
                    116:       uip_textmode();
                    117:       LOG_CRITICAL(("Unable to change SVGA mode, mode not supported by "
1.1.1.2 ! root      118:                     "hardware?"));
1.1       root      119:       LOG_CRITICAL(("Check /etc/vga/libvga.config to ensure your card "
1.1.1.2 ! root      120:                     "is not commented out"));
1.1       root      121:       return 1;
                    122:     }
                    123:   }
1.1.1.2 ! root      124:   if (vga_claimvideomemory(2 * BANKSIZE) == -1) {
1.1       root      125:     uip_textmode();
                    126:     LOG_CRITICAL(("Unable to allocated video memory - 2MB video card "
1.1.1.2 ! root      127:                   "required."));
1.1       root      128:     return 1;
                    129:   }
                    130:   if ((modeinfo = vga_getmodeinfo(vga_getcurrentmode())) == NULL) {
                    131:     uip_textmode();
                    132:     LOG_CRITICAL(("Unable to get current mode information"));
                    133:     return 1;
                    134:   }
                    135:   LOG_VERBOSE(("Resolution %dx%d bytesperpixel %d linewidth %d",
1.1.1.2 ! root      136:                modeinfo->width, modeinfo->height, modeinfo->bytesperpixel,
        !           137:                modeinfo->linewidth));
1.1       root      138:   if (modeinfo->width != 640 || modeinfo->height != 480 ||
1.1.1.2 ! root      139:       modeinfo->bytesperpixel != 2 || modeinfo->linewidth * 480 > BANKSIZE ||
1.1       root      140:       (modeinfo->startaddressrange & BANKSIZE) != BANKSIZE) {
                    141:     uip_textmode();
                    142:     LOG_CRITICAL(("Unexpected mode settings"));
                    143:     return 1;
                    144:   }
                    145:   uip_uipinfo->linewidth = modeinfo->linewidth;
                    146:   if (vga_setlinearaddressing() == -1) {
                    147:     uip_textmode();
                    148:     LOG_CRITICAL(("Linear addressing mode not supported by hardware"));
                    149:     return 1;
                    150:   }
                    151:   if ((uip_uipinfo->screenmem0 = vga_getgraphmem()) == NULL) {
                    152:     uip_textmode();
                    153:     LOG_CRITICAL(("Failed to find start of screen memory"));
                    154:     return 1;
                    155:   }
1.1.1.2 ! root      156:   uip_uipinfo->screenmem1 = uip_uipinfo->screenmem0 + BANKSIZE;
1.1       root      157:   if (uip_forceredshift != -1 && uip_forcegreenshift != -1 &&
                    158:       uip_forceblueshift != -1) {
                    159:     uip_uipinfo->redshift = uip_forceredshift;
                    160:     uip_uipinfo->greenshift = uip_forcegreenshift;
                    161:     uip_uipinfo->blueshift = uip_forceblueshift;
                    162:   } else {
                    163:     switch (modeinfo->colors) {
                    164:     case 32768:
                    165:       LOG_VERBOSE(("Assuming colour bit positions 10,5,0 for 32k colour mode"));
                    166:       uip_uipinfo->blueshift = 0;
                    167:       uip_uipinfo->greenshift = 5;
                    168:       uip_uipinfo->redshift = 10;
                    169:       break;
                    170:     case 65536:
                    171:       LOG_VERBOSE(("Assuming colour bit positions 11,6,0 for 64k colour mode"));
                    172:       uip_uipinfo->blueshift = 0;
                    173:       uip_uipinfo->greenshift = 6;
                    174:       uip_uipinfo->redshift = 11;
                    175:       break;
                    176:     default:
                    177:       LOG_CRITICAL(("Unknown mode - %d colours?? use -c", modeinfo->colors));
                    178:       return 1;
                    179:     }
                    180:   }
1.1.1.2 ! root      181:   uip_displaybank(0);           /* set current to 0th bank */
        !           182:   uip_clearscreen();            /* clear bank */
        !           183:   ui_setupscreen();             /* setup bank */
        !           184:   uip_displaybank(-1);          /* toggle bank */
        !           185:   uip_clearscreen();            /* clear bank */
        !           186:   ui_setupscreen();             /* setup bank */
1.1       root      187:   if (keyboard_init() == -1) {
                    188:     uip_textmode();
                    189:     LOG_CRITICAL(("Unable to initialise keyboard"));
                    190:     return 1;
                    191:   }
                    192:   memset(uip_key, 0, sizeof(uip_key));
                    193:   keyboard_seteventhandler(uip_keyboardhandler);
                    194:   return 0;
                    195: }
                    196: 
                    197: int uip_setcolourbits(int red, int green, int blue)
                    198: {
                    199:   uip_forceredshift = red;
                    200:   uip_forcegreenshift = green;
                    201:   uip_forceblueshift = blue;
                    202:   return 0;
                    203: }
                    204: 
1.1.1.2 ! root      205: void uip_displaybank(int bank)
        !           206: {
1.1       root      207:   if (bank == -1)
                    208:     bank = uip_displaybanknum ^ 1;
                    209:   vga_setdisplaystart(bank ? BANKSIZE : 0);
                    210:   uip_uipinfo->screenmem_w = bank ? uip_uipinfo->screenmem0 :
                    211:     uip_uipinfo->screenmem1;
                    212:   uip_displaybanknum = bank;
                    213: }
                    214: 
1.1.1.2 ! root      215: void uip_clearscreen(void)
        !           216: {
1.1       root      217:   memset(uip_uipinfo->screenmem_w, 0, BANKSIZE);
                    218: }
                    219: 
1.1.1.2 ! root      220: void uip_clearmiddle(void)
        !           221: {
1.1       root      222:   int i;
                    223: 
                    224:   for (i = 0; i < 240; i++) {
1.1.1.2 ! root      225:     memset(uip_uipinfo->screenmem_w +
        !           226:            (uip_uipinfo->linewidth) * (120 + i) + 160 * 2, 0, 2 * 320);
1.1       root      227:   }
                    228: }
                    229: 
                    230: /* uip_singlebank sets write address to the current display screen */
                    231: 
1.1.1.2 ! root      232: void uip_singlebank(void)
        !           233: {
1.1       root      234:   uip_uipinfo->screenmem_w = uip_displaybanknum ? uip_uipinfo->screenmem1 :
                    235:     uip_uipinfo->screenmem0;
                    236: }
                    237: 
                    238: /* uip_doublebank sets write address to hidden display screen */
                    239: 
1.1.1.2 ! root      240: void uip_doublebank(void)
        !           241: {
1.1       root      242:   uip_uipinfo->screenmem_w = uip_displaybanknum ? uip_uipinfo->screenmem0 :
                    243:     uip_uipinfo->screenmem1;
                    244: }
                    245: 
                    246: void uip_textmode(void)
                    247: {
                    248:   if (uip_vga) {
                    249:     keyboard_setdefaulteventhandler();
                    250:     vga_setmode(0);
                    251:   }
                    252:   uip_vga = 0;
                    253: }
                    254: 
                    255: int uip_checkkeyboard(void)
                    256: {
1.1.1.2 ! root      257:   int x, y;
        !           258:   int pad;
        !           259:   int joystick;
        !           260: 
        !           261:   joystick_update();
1.1       root      262:   keyboard_update();
1.1.1.2 ! root      263:   for (pad = 0; pad < 2; pad++) {
        !           264:     if ((joystick = ui_bindings[pad].joystick) >= 0) {
        !           265:       x = joystick_getaxis(joystick, 0);
        !           266:       y = joystick_getaxis(joystick, 1);
        !           267:       mem68k_cont[pad].a = joystick_getbutton(joystick, 0);
        !           268:       mem68k_cont[pad].b = joystick_getbutton(joystick, 1);
        !           269:       mem68k_cont[pad].c = joystick_getbutton(joystick, 2);
        !           270:       mem68k_cont[pad].start = joystick_getbutton(joystick, 3);
        !           271:       mem68k_cont[pad].left = x < -64 ? 1 : 0;
        !           272:       mem68k_cont[pad].right = x > 64 ? 1 : 0;
        !           273:       mem68k_cont[pad].up = y < -64 ? 1 : 0;
        !           274:       mem68k_cont[pad].down = y > 64 ? 1 : 0;
        !           275:     } else {
        !           276:       switch (ui_bindings[pad].keyboard) {
        !           277:       case 0: /* main keys */
        !           278:         mem68k_cont[pad].a = uip_key[SCANCODE_A] ? 1 : 0;
        !           279:         mem68k_cont[pad].b = (uip_key[SCANCODE_B] ||
        !           280:                               uip_key[SCANCODE_S]) ? 1 : 0;
        !           281:         mem68k_cont[pad].c = (uip_key[SCANCODE_C] ||
        !           282:                               uip_key[SCANCODE_D]) ? 1 : 0;
        !           283:         mem68k_cont[pad].left = uip_key[SCANCODE_CURSORBLOCKLEFT] ? 1 : 0;
        !           284:         mem68k_cont[pad].up = uip_key[SCANCODE_CURSORBLOCKUP] ? 1 : 0;
        !           285:         mem68k_cont[pad].right = uip_key[SCANCODE_CURSORBLOCKRIGHT] ? 1 : 0;
        !           286:         mem68k_cont[pad].down = uip_key[SCANCODE_CURSORBLOCKDOWN] ? 1 : 0;
        !           287:         mem68k_cont[pad].start = uip_key[SCANCODE_ENTER] ? 1 : 0;
        !           288:         break;
        !           289:       case 1: /* left side of keyboard */
        !           290:         mem68k_cont[pad].a = uip_key[SCANCODE_Z] ? 1 : 0;
        !           291:         mem68k_cont[pad].b = uip_key[SCANCODE_X] ? 1 : 0;
        !           292:         mem68k_cont[pad].c = uip_key[SCANCODE_C] ? 1 : 0;
        !           293:         mem68k_cont[pad].left = uip_key[SCANCODE_D] ? 1 : 0;
        !           294:         mem68k_cont[pad].up = uip_key[SCANCODE_R] ? 1 : 0;
        !           295:         mem68k_cont[pad].right = uip_key[SCANCODE_G] ? 1 : 0;
        !           296:         mem68k_cont[pad].down = uip_key[SCANCODE_F] ? 1 : 0;
        !           297:         mem68k_cont[pad].start = uip_key[SCANCODE_V] ? 1 : 0;
        !           298:         break;
        !           299:       case 2: /* right side of keyboard */
        !           300:         mem68k_cont[pad].a = uip_key[SCANCODE_COMMA] ? 1 : 0;
        !           301:         mem68k_cont[pad].b = uip_key[SCANCODE_PERIOD] ? 1 : 0;
        !           302:         mem68k_cont[pad].c = uip_key[SCANCODE_SLASH] ? 1 : 0;
        !           303:         mem68k_cont[pad].left = uip_key[SCANCODE_CURSORBLOCKLEFT] ? 1 : 0;
        !           304:         mem68k_cont[pad].up = uip_key[SCANCODE_CURSORBLOCKUP] ? 1 : 0;
        !           305:         mem68k_cont[pad].right = uip_key[SCANCODE_CURSORBLOCKRIGHT] ? 1 : 0;
        !           306:         mem68k_cont[pad].down = uip_key[SCANCODE_CURSORBLOCKDOWN] ? 1 : 0;
        !           307:         mem68k_cont[pad].start = uip_key[SCANCODE_ENTER] ? 1 : 0;
        !           308:         break;
        !           309:       }
        !           310:     }
        !           311:   }
1.1       root      312:   return (uip_key[SCANCODE_ESCAPE] ? 1 : 0);
                    313: }
                    314: 
                    315: void uip_vsync(void)
                    316: {
                    317:   vga_waitretrace();
                    318: }
                    319: 
                    320: unsigned int uip_whichbank(void)
                    321: {
                    322:   /* returns 0 or 1 - the bank being VIEWED */
                    323:   return uip_displaybanknum;
                    324: }
                    325: 
                    326: uint8 uip_getchar(void)
                    327: {
                    328:   char c;
                    329: 
                    330:   keyboard_setdefaulteventhandler();
                    331:   keyboard_close();
                    332:   c = getchar();
                    333:   keyboard_init();
                    334:   memset(uip_key, 0, sizeof(uip_key));
                    335:   keyboard_seteventhandler(uip_keyboardhandler);
                    336:   return c;
                    337: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.