/*==========================================================================*\
 | DESQQWIK.C                                             ver 2.0, 12-03-88 |
 |                                                                          |
 | Demo for DESQview interface routines and the use QWIKC                   |
 |                                                                          |
 | It is assumed that you are an operator of DESQview and that you are      |
 | familiar with its operation.                                             |
 |                                                                          |
 |  TO TEST:                                                                |
 |    1. Build the project file DESQQWIK.PRJ.                               |
 |    2. Build the project file DESQLOOP.PRJ.                               |
 |    3. Add these programs to DV with Direct video = "N".                  |
 |    4. Run DESQloop in one or more windows, say 1 and 2.                  |
 |    5. Run this program in another one, say 3.                            |
 |    6. Observe results.                                                   |
 |                                                                          |
 |  TO USE:                                                                 |
 |    1. Follow instruction provided by DESQview.                           |
 |    2. For high speed buffer writing to a DESQview window, use            |
 |       QWIKC20.ARC.                                                       |
 |                                                                          |
 |  by  James H. LeMay                                                      |
 |                                                                          |
 | converted to Turbo C by                                                  |
 |     Jordan Gallagher                                                     |
 | for Eagle Performance Software                                           |
 |     TC Products                                                          |
 |     P.O. Box 292786                                                      |
 |     Lewisville, TX  75029-2786                                           |
 |                                                                          |
 | Conversion to Turbo C by Jordan Gallagher / Wisdom Research              |
\*==========================================================================*/

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <process.h>

#include "desqc20.h"
#include "qwikc20.h"

#define LO_CHAR(v) (v & 0x0f)
#define HI_CHAR(v) (v >> 4)
#define LO_INT(v) (v & 0xff)
#define HI_INT(v) (v >> 8)

int dv_version;
char str[90];

/********************************| htoa |************************************\
Converts a long to a string.  The target string contains the long in
hexadecimal form, i.e. "21CF89A5".
The parameter hcnt specifies the amount of characters the target
string should contain.
The return value is a pointer to the target string.
\****************************************************************************/
char *htoa( long val, char hcnt, char *string )
{
    if(hcnt > 4)
        sprintf( string, "0x%*lX", hcnt, val );
    else
    if(hcnt > 2)
        sprintf( string, "0x%0*X", hcnt, (int) val );
    else
    if(hcnt <= 2)
        sprintf( string, "0x%0*hX", hcnt, (unsigned char) val );

    return(string);
}


void clearscr(int attrib)
{
    qfill( 1, 1, crt_rows, crt_cols, attrib, ' ');
}


void main()
{
    qinit();

    page0seg=dv_get_video_buffer(page0seg);   /* Base of video segment */
    qscrseg=page0seg;                         /* Segment for QWIKC writing */
    dv_version=dv_get_version();              /* Optional */

    if(!in_dv) {
        clearscr(LIGHTGRAY);
        qwrite( 1, 1, SAMEATTR, "DESQview not active" );
        gotorc( 2, 1 );
    } else {
        /* QWIKC uses FAR pointers for the screen base.  DESQview only
           changes the segment and is therefore paragraph aligned (meaning the
           offset is always 0).  Since dv_get_video_buffer does not work with
           the offset, DESQview assumes it to be zero for the screen base.
        */

        qscrofs = 0;            /* Screen base offset */
        qsnow = 0;
        clearscr(SAMEATTR);
        qwrite( 1, 1, SAMEATTR, "DESQview version = " );
        sprintf( str, "%4.2f", (float) (HI_INT(dv_version) +
                 (float) LO_INT(dv_version) / 100 ) );
        qwriteeos( SAMEATTR, str );
        qwrite( 2, 1, SAMEATTR, "Video Segment = " );
        qwriteeos( SAMEATTR, htoa( page0seg, 4, str ) );
        qwrite( 3, 1, SAMEATTR, "First character in row 1 is = " );
        sprintf( str, "%c", peekb( page0seg, 0 ) );
        qwriteeos( SAMEATTR, str );
        qwrite( 4, 1, SAMEATTR, "All windows should now freeze "
                                "for 3 seconds" );
        gotorc( 5, 1 );
        delay(1000);
        dv_begin_critical();
        delay(3000);
        dv_end_critical();
        qwrite( 5, 1, SAMEATTR, "Now all windows will continue." );
        qwrite( 6, 1, SAMEATTR, "Test completed.  Scroll back "
                                "to see row 1." );
        gotorc( 7, 1 );
    }

    exit(0);
}
