Annotation of pmsdk/samples/browse/viobrows.c, revision 1.1.1.1

1.1       root        1: /* ------------------------------------ */
                      2: /*     VIO File Browser Demo           */
                      3: /*     Version 5                       */
                      4: /*     Created   1988 Microsoft Corp.  */
                      5: /* ------------------------------------ */
                      6: 
                      7: /* ------------------------------------ */
                      8: /* library includes                    */
                      9: /* ------------------------------------ */
                     10: #define INCL_BASE
                     11: #include <stdlib.h>
                     12: #include <stdio.h>
                     13: #include <os2.h>
                     14: #include <string.h>
                     15: #include "viobrows.h"
                     16: 
                     17: /* ------------------------------------ */
                     18: /* global variables                    */
                     19: /* ------------------------------------ */
                     20: FILE *pFile_In;
                     21: char *apchDataLines[ NUM_DATA_LINES ];
                     22: SHORT HighestLine= -1;
                     23: USHORT Vid_Cols, Vid_Rows;
                     24: SHORT HorScrollPos=0;
                     25: 
                     26: /* ================================================================ */
                     27: /*         Functions                                               */
                     28: /* ================================================================ */
                     29: 
                     30: void cdecl main( argc, argv )
                     31: int argc;
                     32: char *argv[];
                     33: {
                     34:     if (Initialize(argc,argv))
                     35:        exit(-1);
                     36:     ManipulateFile();
                     37:     exit(0);
                     38: }
                     39: 
                     40: /* ================================================================ */
                     41: 
                     42: SHORT Initialize(argc,argv)
                     43: int argc;
                     44: char *argv[];
                     45: 
                     46: /* tries to open input file and initialize global variables */
                     47: /* plus any other misc. functions                          */
                     48: {
                     49:     char *achInFile;
                     50:     VIOMODEINFO viomiMode;
                     51: 
                     52:     /* -------------------------- */
                     53:     /* get input file ready      */
                     54:     /* -------------------------- */
                     55:     if (argc==1)
                     56:        pFile_In=stdin;
                     57:     else {
                     58:        achInFile=argv[1];
                     59:        if ((pFile_In=fopen(achInFile,"r"))==NULL) {
                     60:            fprintf(stderr,"Error--could not open %s",achInFile);
                     61:            return(-1);
                     62:        }
                     63:     }
                     64: 
                     65:     /* -------------------------- */
                     66:     /* read input file           */
                     67:     /* -------------------------- */
                     68:     if (ReadFile())
                     69:        return(-1);
                     70: 
                     71: 
                     72:     /* -------------------------- */
                     73:     /* get video parameters      */
                     74:     /* -------------------------- */
                     75:     viomiMode.cb = sizeof(viomiMode);
                     76:     VioGetMode(&viomiMode, 0);
                     77:     Vid_Cols=viomiMode.col;
                     78:     Vid_Rows=viomiMode.row;
                     79: 
                     80:     DisplayScreen(0, TRUE);
                     81: }
                     82: 
                     83: /* ================================================================ */
                     84: 
                     85: SHORT ReadFile( void )
                     86: 
                     87: /* reads data lines from the file to memory.       */
                     88: /* if an error occurs, it will abort the program    */
                     89: /* stores the lines as null-terminated ascii, with  */
                     90: /* no /n's in the strings.                          */
                     91: {   char achLine[256];
                     92: 
                     93:     while (fgets(achLine,128,pFile_In)) {
                     94: 
                     95:        /* strip LF character from end of string before storing */
                     96:        /* by converting LF to /0                               */
                     97:        if (achLine[strlen(achLine)-1]=='\n')
                     98:            achLine[strlen(achLine)-1]=0;
                     99:        else {
                    100:            fprintf(stderr,"Internal error in ReadFile.\n");
                    101:            return(-1);
                    102:        }
                    103:        if (StoreLine(achLine)) {
                    104:            fprintf(stderr,"Error while storing line in ReadFile.\n");
                    105:            return(-1);
                    106:        }
                    107:     }
                    108: 
                    109:     fclose(pFile_In);
                    110:     return(0);
                    111: }
                    112: 
                    113: /* ================================================================ */
                    114: 
                    115: void ManipulateFile( void )
                    116: 
                    117: /* this takes lines from memory and displays to user's pleasure */
                    118: /* other functions such as DisplayLineNumber and SwitchFile    */
                    119: /* should be added here                                        */
                    120: {
                    121:     USHORT CurLine=0;
                    122: 
                    123:     UserLoop();
                    124:     /* set cursor at a nice place before exiting */
                    125:     VioSetCurPos(Vid_Rows-1,0,0);
                    126: }
                    127: 
                    128: /* ================================================================ */
                    129: /*     Line-handling subsystem                                     */
                    130: /* ================================================================ */
                    131: 
                    132: SHORT StoreLine( pchLine )
                    133: char *pchLine;
                    134: 
                    135: /* store a line in memory at end of list.                      */
                    136: /* creates the line number automatically                       */
                    137: /* extensions to allow >64K data go here and in RetrieveLine   */
                    138: {
                    139:     /* don't forget to malloc space for \0 */
                    140:     if (HighestLine==NUM_DATA_LINES ||
                    141:       (apchDataLines[++HighestLine]=malloc(strlen(pchLine)+1))==NULL )
                    142:        return(-1);
                    143:     /* copy (to, from) */
                    144:     strcpy(apchDataLines[HighestLine],pchLine);
                    145:     return(0);
                    146: }
                    147: 
                    148: /* ================================================================ */
                    149: 
                    150: SHORT RetrieveLine( ppchLine , LineNum )
                    151: char **ppchLine;
                    152: USHORT LineNum;
                    153: 
                    154: /* take line number and convert it to a string of data from memory  */
                    155: /* extensions to allow >64K data also incorporated here            */
                    156: {
                    157:     if (LineNum > HighestLine)
                    158:        return(-1);
                    159:     *ppchLine=apchDataLines[LineNum];
                    160:     return(0);
                    161: }
                    162: 
                    163: /* ================================================================ */
                    164: /*             Video-handling subsystem                            */
                    165: /* ================================================================ */
                    166: 
                    167: void ClearScreen( void )
                    168: 
                    169: /* self-explanitory I hope         */
                    170: /* uses technique from qh examples  */
                    171: /* clearing=scrolldown(maxlines)    */
                    172: {
                    173:     BYTE bBlank[2];
                    174:     bBlank[0] = 0x20;                      /* space character       */
                    175:     bBlank[1] = 0x07;                      /* white attribute (EGA) */
                    176:     VioScrollDn(0,                         /* top row               */
                    177:         0,                                 /* left column           */
                    178:         -1,                                /* bottom row            */
                    179:         -1,                                /* right column          */
                    180:         -1,                                /* number of lines       */
                    181:         bBlank,                            /* cell to write         */
                    182:        WINDOW);                           /* video handle          */
                    183: }
                    184: 
                    185: /* ================================================================ */
                    186: 
                    187: void DisplayScreen( USHORT TopLine, BOOL force)
                    188: 
                    189: /* display lines from memory to video, starting at line number TopLine */
                    190: /* has 'smarts' to scroll+paint remainder.                             */
                    191: {
                    192:     SHORT LineDiff;
                    193:     static USHORT OldTopLine;
                    194: 
                    195: /* horizontal shift should go here, but is not implemented yet */
                    196: 
                    197:     LineDiff=TopLine-OldTopLine;
                    198:     if ( (abs(LineDiff) < Vid_Rows ) && !force ) {
                    199:        /* is legal to apply scroll+paint remainder algorithm */
                    200:        /* (usually applied when user wants line up/down) */
                    201:        if (LineDiff<0) {
                    202:            /* user scrolls back; repaint top part of screen */
                    203:            ScrollDown( -LineDiff );
                    204:            VidRefresh(TopLine, -LineDiff, 0);
                    205:        }
                    206:        else {
                    207:            /* user scrolls forward; repaint bottom part */
                    208:            ScrollUp( LineDiff );
                    209:            VidRefresh( TopLine+Vid_Rows-LineDiff, LineDiff, Vid_Rows-LineDiff);
                    210:        }
                    211:     }
                    212:     else {
                    213:        /* repaint entire screen */
                    214:        ClearScreen();
                    215:        VidRefresh(TopLine, Vid_Rows, 0);
                    216:     }
                    217:     OldTopLine=TopLine;
                    218: }
                    219: 
                    220: /* ================================================================ */
                    221: 
                    222: void VidRefresh ( USHORT LineFrom, USHORT NumLines, USHORT VidRowStart)
                    223: /* Redisplay lines LineFrom to LineTo starting at video VidRowStart */
                    224: /* Will NOT check that you haven't given it stupid arguments        */
                    225: {
                    226:     USHORT LineOffset;
                    227:     char *pchLine;
                    228: 
                    229:     for (LineOffset=0; LineOffset < NumLines ; LineOffset++) {
                    230:        if (LineOffset + LineFrom > HighestLine)
                    231:            break;
                    232:        RetrieveLine(&pchLine, LineOffset + LineFrom);
                    233:        VioSetCurPos(
                    234:            VidRowStart+LineOffset,     /* vertical pos   */
                    235:            0                    ,      /* horizontal pos */
                    236:            WINDOW                      /* hvio */
                    237:        );
                    238:        VioWrtTTY(
                    239:            pchLine,            /* string to print */
                    240:            strlen(pchLine),    /* string length */
                    241:            WINDOW              /* hvio */
                    242:        );
                    243:     }
                    244: }
                    245: 
                    246: void ScrollUp(int Nlines)
                    247: {
                    248:     BYTE bBlank[2];
                    249:     bBlank[0] = 0x20;                      /* space character       */
                    250:     bBlank[1] = 0x07;                      /* white attribute (EGA) */
                    251:     VioScrollUp(0,                        /* top row               */
                    252:         0,                                 /* left column           */
                    253:         -1,                                /* bottom row            */
                    254:         -1,                                /* right column          */
                    255:        Nlines,                            /* number of lines       */
                    256:         bBlank,                            /* cell to write         */
                    257:        WINDOW);                           /* video handle          */
                    258: }
                    259: 
                    260: void ScrollDown(int Nlines)
                    261: {
                    262:     BYTE bBlank[2];
                    263:     bBlank[0] = 0x20;                      /* space character       */
                    264:     bBlank[1] = 0x07;                      /* white attribute (EGA) */
                    265:     VioScrollDn(0,                         /* top row               */
                    266:         0,                                 /* left column           */
                    267:         -1,                                /* bottom row            */
                    268:         -1,                                /* right column          */
                    269:        Nlines,                            /* number of lines       */
                    270:         bBlank,                            /* cell to write         */
                    271:        WINDOW);                           /* video handle          */
                    272: }
                    273: 
                    274: /* ================================================================ */
                    275: /*     User input subsystem                                        */
                    276: /* ================================================================ */
                    277: 
                    278: VOID UserLoop( VOID )
                    279: {
                    280:     CHAR ch;
                    281: 
                    282:     while ( (ch=GetKbdInput()) != EXIT )
                    283:        ExecuteAction(ch);
                    284: }
                    285: 
                    286: VOID ExecuteAction( CHAR ch)
                    287: /* interact with the user somehow to discover new line number  */
                    288: /* at top of screen                                            */
                    289: /* extensions to allow PM windowing go here                    */
                    290: /* returns 1 if successful                                     */
                    291: /* returns 0 if unsuccessful (user wants to quit)              */
                    292: {
                    293:     static USHORT OldLine ;
                    294:     BOOL bForce=FALSE;
                    295: 
                    296: #define downbound( X, A, B) ( (X) > ((B)-(A)) )? (B) : (X)+(A)
                    297: #define upbound( X, A, B) ( (X) < ((B)+(A)) )? (B) : (X)-(A)
                    298: 
                    299:     switch (ch) {
                    300:        case LINE_UP:
                    301:            OldLine=upbound( OldLine, 1, 0);
                    302:            break;
                    303:        case LINE_DOWN:
                    304:            OldLine=downbound( OldLine, 1, MAX_START_LINE);
                    305:            break;
                    306:        case PAGE_UP:
                    307:            OldLine=upbound( OldLine, Vid_Rows, 0);
                    308:            break;
                    309:        case PAGE_DOWN:
                    310:            OldLine=downbound( OldLine, Vid_Rows, MAX_START_LINE);
                    311:            break;
                    312:        case GOTO_TOP:
                    313:            OldLine= 0;
                    314:            break;
                    315:        case GOTO_BOTTOM:
                    316:            OldLine= MAX_START_LINE;
                    317:            break;
                    318:        case GOTO_LINE:
                    319:            /* Not Implemented */
                    320:            break;
                    321:        case CHAR_RIGHT:
                    322:            HorScrollPos= downbound ( HorScrollPos, 1, HIGHEST_HSCROLL);
                    323:            break;
                    324:        case CHAR_LEFT:
                    325:            HorScrollPos = upbound( HorScrollPos, 1, 0);
                    326:            break;
                    327:        case PAGE_RIGHT:
                    328:            HorScrollPos = downbound( HorScrollPos, HSCROLL_PAGESIZE, HIGHEST_HSCROLL);
                    329:            break;
                    330:        case PAGE_LEFT:
                    331:            HorScrollPos = upbound( HorScrollPos, HSCROLL_PAGESIZE, 0);
                    332:            break;
                    333:        case GOTO_HSCROLL:
                    334:            /* Not Implemented */
                    335:            break;
                    336:        case NULL:
                    337:            bForce= TRUE;
                    338:            break;
                    339:     }
                    340:     DisplayScreen( OldLine, bForce );
                    341: }
                    342: 
                    343: /* ================================================================ */
                    344: 
                    345: CHAR GetKbdInput( void )
                    346: 
                    347: /* get an action code based on keyboard input      */
                    348: /* action codes are #defined constants.            */
                    349: {
                    350:     switch(GetScanCode()) {
                    351:        case 1:                         /* escape */
                    352:            return ( EXIT );
                    353:        case 0x48:                      /* up-arrow */
                    354:            return( LINE_UP );
                    355:        case 0x50:                      /* down-arrow */
                    356:            return( LINE_DOWN );
                    357:        case 0x4d:                      /* right-arrow */
                    358:            return( CHAR_RIGHT );
                    359:        case 0x4b:                      /* left-arrow */
                    360:            return( CHAR_LEFT );
                    361:        case 0x49:                      /* PageUp */
                    362:            return( PAGE_UP );
                    363:        case 0x51:                      /* PageDown */
                    364:            return( PAGE_DOWN );
                    365:        case 0x47:                      /* Home */
                    366:            return( GOTO_TOP );
                    367:        case 0x4f:
                    368:            return( GOTO_BOTTOM );
                    369:        case 0x0f:                      /* Tab */
                    370:            return( GOTO_LINE );
                    371:     }
                    372: }
                    373: 
                    374: /* ================================================================ */
                    375: 
                    376: UCHAR GetScanCode( void )
                    377: 
                    378: /* wait for a single keystroke and return its scan code */
                    379: {
                    380:     KBDKEYINFO kbciKeyInfo;
                    381: 
                    382:     KbdCharIn(&kbciKeyInfo,                /* buffer for data */
                    383:        IO_WAIT,                            /* WAIT/NOWAIT     */
                    384:        0);                                 /* keyboard handle */
                    385:     return(kbciKeyInfo.chScan);
                    386: }

unix.superglobalmegacorp.com

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