home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / editors / tde150.arj / TDECFG.C < prev    next >
C/C++ Source or Header  |  1992-04-01  |  19KB  |  605 lines

  1. /*
  2.  * A configuration utility was written to customize the tde executable file.
  3.  * You only need one file to run tde - the executable.  No configuration files
  4.  * to worry about.  It doesn't take a lot of time to figure out the offsets of
  5.  * the variables to initialize in tde.exe, so it's fairly easy to do.
  6.  *
  7.  * Program name:  tdecfg
  8.  * Author:        Frank Davis
  9.  * Date:          October 5, 1991
  10.  *
  11.  * This program is released into the public domain.  You may distribute
  12.  * it freely, Frank Davis
  13.  */
  14.  
  15.  
  16. /********    EXTREMELY IMPORTANT   ************/
  17. /*
  18.  * If you modify tde and you want to use this configuration utility, it is your
  19.  * responsibility to find the offsets of the variables in your new executable.
  20.  *
  21.  */
  22. /*******     EXTREMELY IMPORTANT   ************/
  23.  
  24.  
  25. #include <bios.h>
  26. #include <dos.h>
  27. #include <io.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32. #include "tdecfg.h"
  33.  
  34.  
  35. struct vcfg cfg;                /* video stuff */
  36. FILE *tde_exe;                  /* FILE pointer to tde.exe */
  37.  
  38.  
  39. struct screen cfg_choice[] = {
  40.    {5,25,"1.  Change colors" },
  41.    {7,25,"2.  Redefine keys" },
  42.    {9,25,"3.  Install new help screen" },
  43.   {11,25,"4.  Set default modes" },
  44.   {13,25,"5.  Install permanent macro file" },
  45.   {15,25,"6.  Exit" },
  46.  {18,20,"Please enter choice: " },
  47.   {0,0,NULL}
  48. };
  49.  
  50.  
  51. char *greatest_composer_ever = "W. A. Mozart, 1756-1791";
  52.  
  53.  
  54. /*
  55.  * Name:    main
  56.  * Date:    October 5, 1991
  57.  * Notes:   Strategy is fairly straight forward -  1) initialize all the
  58.  *          variables  2) show the user a color sample  3) make the changes
  59.  *          permanent if desired.
  60.  */
  61. void main( int argc, char *argv[] )
  62. {
  63. int rc;
  64. int c;
  65. char fname[82];
  66.  
  67.    puts( "\nEnter tde executable file name (<Enter> = \"tde.exe\")  :" );
  68.    gets( fname );
  69.  
  70.    if (strlen(fname) == 0)
  71.       strcpy( fname, "tde.exe" );
  72.  
  73.    if ((rc = access( fname, EXIST )) != 0) {
  74.       puts( "\nFile not found." );
  75.       exit( 1 );
  76.    } else if ((tde_exe = fopen( fname, "r+b" )) == NULL ) {
  77.       puts( "\nCannot open executable file." );
  78.       exit( 2 );
  79.    }
  80.  
  81.    video_config( );
  82.    cls( );
  83.    show_box( 0, 0, cfg_choice, NORMAL );
  84.    for (rc=0; rc != 1;) {
  85.       xygoto( 42, 18 );
  86.       c = getkey( );
  87.       while (c != '1' && c != '2' && c != '3' && c != '4' && c != '5' &&
  88.              c != '6')
  89.          c = getkey( );
  90.       switch (c) {
  91.          case '1' :
  92.             tdecolor( );
  93.             show_box( 0, 0, cfg_choice, NORMAL );
  94.             break;
  95.          case '2' :
  96.             tdekeys( );
  97.             show_box( 0, 0, cfg_choice, NORMAL );
  98.             break;
  99.          case '3' :
  100.             tdehelp( );
  101.             show_box( 0, 0, cfg_choice, NORMAL );
  102.             break;
  103.          case '4' :
  104.             tdemodes( );
  105.             show_box( 0, 0, cfg_choice, NORMAL );
  106.             break;
  107.          case '5' :
  108.             tdemacro( );
  109.             show_box( 0, 0, cfg_choice, NORMAL );
  110.             break;
  111.          case '6' :
  112.             rc = 1;
  113.             break;
  114.       }
  115.    }
  116.    fcloseall( );
  117.    puts( " " );
  118.    puts( " " );
  119. }
  120.  
  121.  
  122. /*
  123.  * Name:    xygoto
  124.  * Date:    July 21, 1991
  125.  * Notes:   Use the video interrupt to set the cursor.
  126.  */
  127. void xygoto( int col, int row )
  128. {
  129. union REGS inregs, outregs;
  130.  
  131.    inregs.h.ah = 2;
  132.    inregs.h.bh = 0;
  133.    inregs.h.dh = row;
  134.    inregs.h.dl = col;
  135.    int86( VIDEO_INT, &inregs, &outregs );
  136. }
  137.  
  138.  
  139. /*
  140.  * Name:    video_config
  141.  * Date:    July 21, 1991
  142.  * Notes:   See main.c for more info.
  143.  */
  144. void video_config( void )
  145. {
  146. #pragma pack( 1 )    /* Use pragma to force packing on byte boundaries. */
  147.  
  148. struct LOWMEMVID
  149. {
  150.    char     vidmode;           /* 0x449 */
  151.    unsigned scrwid;            /* 0x44A */
  152.    unsigned scrlen;            /* 0x44C */
  153.    unsigned scroff;            /* 0x44E */
  154.    struct   LOCATE
  155.    {
  156.       unsigned char col;
  157.       unsigned char row;
  158.    } csrpos[8];                /* 0x450 */
  159.    struct   CURSIZE
  160.    {
  161.       unsigned char end;
  162.       unsigned char start;
  163.    } csrsize;                  /* 0x460 */
  164.    char      page;             /* 0x462 */
  165.    unsigned  addr_6845;        /* 0x463 */
  166.    char      crt_mode_set;     /* 0x465 */
  167.    char      crt_palette[30];  /* 0x466 */
  168.    char      rows;             /* 0x484 */
  169.    unsigned  points;           /* 0x485 */
  170.    char      ega_info;         /* 0x487 */
  171.    char      info_3;           /* 0x488 */
  172. } vid;
  173. struct LOWMEMVID _far *pvid = &vid;
  174.  
  175. #pragma pack( )    /* revert to previously defined pack pragma. */
  176.  
  177. union REGS in, out;
  178. unsigned char temp, active_display;
  179.  
  180.    /* Move system information into uninitialized structure variable. */
  181.    movedata( 0, 0x449, FP_SEG( pvid ), FP_OFF( pvid ), sizeof( vid ) );
  182.  
  183.    cfg.rescan = FALSE;
  184.    in.x.ax =  0x1a00;
  185.    int86( VIDEO_INT, &in, &out );
  186.    temp = out.h.al;
  187.    active_display = out.h.bl;
  188.    if (temp == 0x1a && (active_display == 7 || active_display == 8))
  189.       cfg.adapter = VGA;
  190.    else {
  191.       in.h.ah =  0x12;
  192.       in.h.bl =  0x10;
  193.       int86( VIDEO_INT, &in, &out );
  194.       if (out.h.bl != 0x10) {         /* EGA */
  195.          if (vid.ega_info & 0x08) {
  196.             if (vid.addr_6845 == 0x3d4)
  197.                cfg.adapter = CGA;
  198.             else
  199.                cfg.adapter = MDA;
  200.          } else
  201.             cfg.adapter = EGA;
  202.       } else if (vid.addr_6845 == 0x3d4)
  203.          cfg.adapter = CGA;
  204.       else
  205.          cfg.adapter = MDA;
  206.    }
  207.  
  208.    if (cfg.adapter == CGA)
  209.       cfg.rescan = TRUE;
  210.  
  211.    cfg.mode = vid.vidmode;
  212.    if (vid.addr_6845 == 0x3D4) {
  213.       cfg.color = TRUE;
  214.       FP_SEG( cfg.videomem ) = 0xb800;
  215.    } else {
  216.       cfg.color = FALSE;
  217.       FP_SEG( cfg.videomem ) = 0xb000;
  218.    }
  219.    FP_OFF( cfg.videomem ) = 0x0000;
  220.    if (cfg.color == TRUE)
  221.       cfg.attr = COLOR_ATTR;
  222.    else
  223.       cfg.attr = MONO_ATTR;
  224. }
  225.  
  226.  
  227. /*
  228.  * Name:    getkey
  229.  * Date:    July 21, 1991
  230.  * Notes:   Waits for keyboard input and returns the key pressed by the user.
  231.  */
  232. int getkey( void )
  233. {
  234. unsigned key, lo, scan;
  235.  
  236.    /*
  237.     *  _bios_keybrd == int 16.  It returns the scan code in ah, hi part of key,
  238.     *  and the ascii key code in al, lo part of key.
  239.     */
  240.    key = _bios_keybrd( 0 );
  241.    lo = key & 0X00FF;
  242.    lo = (int)((lo == 0) ? (((key & 0XFF00) >> 8) + 256) : lo);
  243.    return( lo );
  244. }
  245.  
  246.  
  247. /*
  248.  * Name:    s_output
  249.  * Date:    July 21, 1991
  250.  * Passed:  s:     the string to display
  251.  *          line:  line number to begin display
  252.  *          col:   column number to begin display
  253.  *          attr:  color to display string
  254.  * Notes:   See tdeasm.c for more info
  255.  */
  256. void s_output( char far *s, int line, int col, int attr )
  257. {
  258. int far *screen_ptr;
  259. int max_col;
  260. int off;
  261.  
  262.    max_col = 80;
  263.    screen_ptr = cfg.videomem;
  264.    off = line * 160 + col * 2;
  265.  
  266.    _asm {
  267.         push    ds              ; MUST save ds
  268.         push    di              ; save di on stack
  269.         push    si              ; save si on stack
  270.  
  271.         mov     bx, WORD PTR attr               ; keep attribute in bx
  272.         mov     cx, WORD PTR col                ; put cols in cx
  273.         mov     dx, WORD PTR max_col            ; keep max_col in dx
  274.         mov     di, WORD PTR screen_ptr         ; load offset of screen ptr
  275.         add     di, WORD PTR off
  276.         mov     ax, WORD PTR screen_ptr+2       ; load segment of screen ptr
  277.         mov     es, ax
  278.         mov     si, WORD PTR s  ; load offset of string ptr
  279.         or      si, si          ; is it == NULL?
  280.         je      getout          ; yes, no output needed
  281.         mov     ax, WORD PTR s+2        ; load segment of string ptr
  282.         or      ax, ax          ; is pointer == NULL?
  283.         je      getout          ; yes, no output needed
  284.         mov     ds, ax          ; load segment of text in ds
  285.         mov     ah, bl          ; put attribute in AH
  286. top:
  287.         cmp     cx, dx          ; col < max_cols?
  288.         jge     getout          ; no, thru with line
  289.         lodsb                   ; get next char in string - put in al
  290.         or      al, al          ; is it '\0'
  291.         je      getout          ; yes, end of string
  292.         stosw                   ; else show attr + char on screen (ah + al)
  293.         inc     cx              ; col++
  294.         jmp     SHORT top       ; get another character
  295. getout:
  296.         pop     si              ; get back si
  297.         pop     di              ; get back di
  298.         pop     ds              ; get back ds
  299.    }
  300. }
  301.  
  302.  
  303. /*
  304.  * Name:    hlight_line
  305.  * Date:    July 21, 1991
  306.  * Passed:  x:     column to begin hi lite
  307.  *          y:     line to begin hi lite
  308.  *          lgth:  number of characters to hi lite
  309.  *          attr:  attribute color
  310.  * Notes:   The attribute byte is the hi byte.
  311.  */
  312. void hlight_line( int x, int y, int lgth, int attr )
  313. {
  314. int off, far *pointer;
  315.  
  316.    pointer = cfg.videomem;
  317.    off = y * 160 + 2 * x + 1;  /* add one - so it points to attribute byte */
  318.    _asm {
  319.         push    di              ; save es
  320.  
  321.         mov     cx, lgth        ; number of characters to change color
  322.  
  323.         mov     di, WORD PTR pointer    ; get destination - video memory
  324.         add     di, off                 ; add offset
  325.         mov     ax, WORD PTR pointer+2  ;
  326.         mov     es, ax
  327.         mov     ax, attr        ; attribute
  328. lite_len:
  329.         stosb                   ; store a BYTE
  330.         inc     di              ; skip over character to next attribute
  331.         loop    lite_len        ; change next attribute
  332.         pop     di              ; restore di
  333.    }
  334. }
  335.  
  336.  
  337. /*
  338.  * Name:    scroll_window
  339.  * Date:    October 5, 1991
  340.  * Passed:  lines:  number of lines to scroll
  341.  *          r1:     row scroll starts on
  342.  *          c1:     column scroll start on
  343.  *          r2:     ending row of scroll
  344.  *          c2:     ending column of scroll
  345.  *          attr:   color of scroll
  346.  * Notes:   Clear a window using color.  Use standard BIOS call.  See
  347.  *          any technical reference for more info on VIDEO BIOS services.
  348.  */
  349. void scroll_window( int lines, int r1, int c1, int r2, int c2, int attr )
  350. {
  351. char rah, ral;
  352.    _asm {
  353.         mov     ax, lines
  354.         cmp     ax, 0           ; if line < 0  - scroll window down
  355.         jge     a1
  356.  
  357.         neg     ax                      ; make lines positive
  358.         mov     BYTE PTR ral, al        ; save number of lines
  359.         mov     BYTE PTR rah, 7         ; function 7 -  scroll window down
  360.         dec     r2                      ; decrement row 2
  361.         jmp     SHORT a2
  362. a1:
  363.         mov     BYTE PTR ral, al        ; number of lines to scroll
  364.         mov     BYTE PTR rah, 6         ; function 6 - scroll window up
  365. a2:
  366.         mov     ax, WORD PTR r1         ; get starting row
  367.         mov     ch, al                  ; put it in ch
  368.         mov     ax, WORD PTR c1         ; get starting column
  369.         mov     cl, al                  ; put it in cl
  370.         mov     ax, WORD PTR r2         ; get ending row
  371.         mov     dh, al                  ; put it in dh
  372.         mov     ax, WORD PTR c2         ; get ending column
  373.         mov     dl, al                  ; put it in cl
  374.         mov     ax, WORD PTR attr       ; get attribute
  375.         mov     bh, al                  ; put it in bh
  376.         mov     ah, BYTE PTR rah        ; get function number
  377.         mov     al, BYTE PTR ral        ; get number of lines
  378.         push    bp                      ; *** in some early versions of ***
  379.         int     VIDEO_INT               ; *** DOS, u must save bp       ***
  380.         pop     bp
  381.    }
  382. }
  383.  
  384.  
  385. /*
  386.  * Name:    cls
  387.  * Date:    October 5, 1991
  388.  * Notes:   Clear screen using color.
  389.  */
  390. void cls( void )
  391. {
  392.    scroll_window( 0, 0, 0, 24, 79, NORMAL );
  393. }
  394.  
  395.  
  396. /*
  397.  * Name:    show_box
  398.  * Date:    October 5, 1991
  399.  * Passed:  x:     number of lines to scroll
  400.  *          y:     row scroll starts on
  401.  *          p:     column scroll start on
  402.  *          attr:  ending row of scroll
  403.  * Notes:   Easy way to show text on screen.  Based on the display techiniques
  404.  *          in a very good morse code practice program, sorry I don't know
  405.  *          the author's name.  Several morse code pratice programs have been
  406.  *          written, but I am refering to the one written in C and released
  407.  *          around 1984-1986.  Seems like the author was living in California
  408.  *          at that time.
  409.  */
  410. void show_box( int x, int y, struct screen *p, int  attr )
  411. {
  412.  
  413.    while (p->text) {
  414.       s_output( p->text, p->row+y, p->col+x, attr );
  415.       p++;
  416.    }
  417. }
  418.  
  419.  
  420. /*
  421.  * Name:    make_window
  422.  * Date:    October 5, 1991
  423.  * Passed:  col:     upper left column to begin window
  424.  *          row:     upper left row to begin window
  425.  *          width:   number of columns in window
  426.  *          height:  number of rows in window
  427.  *          attr:    color of window border
  428.  * Notes:   Draw the outline of the window then clear all contents.
  429.  *          The begin and ending column have to incremented so when the
  430.  *          window is cleared the borders are not wiped out.
  431.  */
  432. void make_window( int col, int row, int width, int height, int attr )
  433. {
  434.    buf_box( col++, row++, width, height, attr );
  435.    clear_window( col, row, width-2, height-2 );
  436. }
  437.  
  438.  
  439. /*
  440.  * Name:    buf_box
  441.  * Date:    October 5, 1991
  442.  * Passed:  col:     upper left column to begin window
  443.  *          row:     upper left row to begin window
  444.  *          width:   number of columns in window
  445.  *          height:  number of rows in window
  446.  *          attr:    color of window border
  447.  * Notes:   Draw the outline of the window.  See tdecfg.h for the outline
  448.  *          characters.
  449.  */
  450. void buf_box( int col, int row, int width, int height, int attr )
  451. {
  452. int i, j, row_count;
  453. char string[82];
  454.  
  455.    if (height > 0 && width > 0 && height < 25 && width < 81) {
  456.       row_count = 1;
  457.       string[0]= U_LEFT;
  458.       for (i=1; i<width-1; i++)
  459.          string[i] = HOR_LINE;
  460.       string[i++] = U_RIGHT; string[i] = '\0';
  461.       s_output( string, row, col, attr );
  462.       ++row_count;
  463.       ++row;
  464.  
  465.       if (row_count < height) {
  466.          string[0] = VER_LINE;
  467.          string[1] = '\0';
  468.          for (i=1; i<height-1; i++) {
  469.             s_output( string, row, col, attr );
  470.             s_output( string, row, col+width-1, attr );
  471.             ++row;
  472.             ++row_count;
  473.          }
  474.       }
  475.  
  476.       if (row_count <= height) {
  477.          string[0] = L_LEFT;
  478.          for (i=1; i<width-1; i++)
  479.             string[i] = HOR_LINE;
  480.          string[i++] = L_RIGHT; string[i] = '\0';
  481.          s_output( string, row, col, attr );
  482.       }
  483.    }
  484. }
  485.  
  486.  
  487. /*
  488.  * Name:    clear_window
  489.  * Date:    October 5, 1991
  490.  * Passed:  col:     upper left column to begin window
  491.  *          row:     upper left row to begin window
  492.  *          width:   number of columns in window
  493.  *          height:  number of rows in window
  494.  * Notes:   Clear the insides of the window.
  495.  */
  496. void clear_window( int col, int row, int width, int height )
  497. {
  498. /*   scroll_window( height, row, col, row+height-1, col+width-1, NORMAL ); */
  499.    scroll_window( 0, row, col, row+height-1, col+width-1, NORMAL );
  500. }
  501.  
  502.  
  503. /*
  504.  * Name:    window_control
  505.  * Date:    October 5, 1991
  506.  * Passed:  window_ptr: pointer to pointer of window (head of window stack)
  507.  *          action:     are SAVEing or RESTOREing the contents of a window
  508.  *          col:        upper left column of window
  509.  *          row:        upper left row of window
  510.  *          width:      number of characters in window
  511.  *          height:     number of rows in window
  512.  * Notes:   Save or restore the contents of the screen under a pop-up or
  513.  *          pull-down window.  Use a stack to store the windows so an unlimited
  514.  *          number of windows may be saved and restored.
  515.  */
  516. void window_control( WINDOW **window_ptr, int action, int col, int row,
  517.                      int width, int height )
  518. {
  519. WINDOW  *p;
  520. size_t  store_me;
  521.  
  522.    if (action == SAVE) {
  523.       p = (WINDOW *)malloc( sizeof(WINDOW) );
  524.       if (p != NULL) {
  525.          p->n = NULL;
  526.  
  527.          /*
  528.           * push a window on the stack
  529.           */
  530.          if (*window_ptr != NULL)
  531.             p->n = *window_ptr;
  532.          *window_ptr = p;
  533.          store_me = (width * height) * sizeof( int );
  534.          p->buf = (int *)malloc( store_me );
  535.          save_window( p->buf, col, row, width, height );
  536.       }
  537.    } else if (action == RESTORE) {
  538.       if (*window_ptr != NULL) {
  539.          p = *window_ptr;
  540.          restore_window( p->buf, col, row, width, height );
  541.  
  542.          /*
  543.           * pop a window off the stack
  544.           */
  545.          *window_ptr = p->n;
  546.          free( p->buf );
  547.          free( p );
  548. }  }  }
  549.  
  550.  
  551. /*
  552.  * Name:    save_window
  553.  * Date:    October 5, 1991
  554.  * Passed:  destination: pointer to store contents of screen
  555.  *          col:         upper left column of window
  556.  *          row:         upper left row of window
  557.  *          width:       number of characters in window
  558.  *          height:      number of rows in window
  559.  * Notes:   Do an optimal save.  Save only the contents of the screen that the
  560.  *          window writes over.  Some algorithms save the entire contents of
  561.  *          the screen - wasteful.
  562.  */
  563. void save_window( int *destination, int col, int row, int width, int height )
  564. {
  565. int i, j, offset;
  566. int far *pointer;
  567.  
  568.    pointer = cfg.videomem;
  569.    offset = row * 80 + col;
  570.    pointer += offset;
  571.    for (i=0; i < height; i++) {
  572.       for (j=0; j < width; j++)
  573.          *destination++ = *(pointer + j);
  574.       pointer += 80;
  575.    }
  576. }
  577.  
  578.  
  579. /*
  580.  * Name:    restore_window
  581.  * Date:    October 5, 1991
  582.  * Passed:  source:      pointer of source of contents of screen
  583.  *          col:         upper left column of window
  584.  *          row:         upper left row of window
  585.  *          width:       number of characters in window
  586.  *          height:      number of rows in window
  587.  * Notes:   Do an optimal restore.  Restore only the contents of the screen
  588.  *          that the window writes over.  Some algorithms restore the entire
  589.  *          contents of the screen - wasteful.
  590.  */
  591. void restore_window( int *source, int col, int row, int width, int height )
  592. {
  593. int i, j, offset;
  594. int far *pointer;
  595.  
  596.    pointer = cfg.videomem;
  597.    offset = row * 80 + col;
  598.    pointer += offset;
  599.    for (i=0; i < height; i++) {
  600.       for (j=0; j < width; j++)
  601.          *(pointer + j) = *source++;
  602.       pointer += 80;
  603.    }
  604. }
  605.