home *** CD-ROM | disk | FTP | other *** search
/ M.u.C.S. Disc 2000 / MUCS2000.iso / sound / mp2_099 / src / gem / console.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-03  |  8.4 KB  |  357 lines

  1.    /* Shoe console window
  2.  *
  3.  * Copyright (c) 1998 by Fredrik Noring <noring@nocrew.org>.
  4.  */
  5.  
  6. #include <tos.h>
  7. #include <vdi.h>
  8. #include <aes.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. #include "mp2audio.h"
  14. #include "mp2wind.h"
  15. #include "libshoe.h"
  16. #include "console.h"
  17.  
  18. /* global variables from mp2audio.c */
  19. extern WINDFORM windforms[5];
  20. extern int vdi_id;
  21.  
  22. void initialize_console(int w, int h);
  23.  
  24. static struct {
  25.     int h, w;
  26.     int char_w, char_h;
  27.     int cell_w, cell_h;
  28.     
  29.     int screen_w, screen_h;
  30.     
  31.     int row, scroll, cur_col, cur_row;
  32.     char scrollback[CONSOLE_H][CONSOLE_W+1];
  33.  
  34.     int hist_row, hist_off, hist_nr, hist_min, hist_max;
  35.     char hist_list[HISTORY_LEN][CONSOLE_W+1];
  36. } console;
  37.  
  38. #define transform(xywh) { (xywh)[2]+=(xywh)[0]-1; (xywh)[3]+=(xywh)[1]-1; }
  39. #define max(a,b) ((a)>(b)?(a):(b))
  40. #define min(a,b) ((a)<(b)?(a):(b))
  41.  
  42. void shrink(int *xywh1, int *xywh2)
  43. {
  44.     int clip[4];
  45.     clip[0] = xywh1[0]<xywh2[0]?xywh2[0]:xywh1[0];
  46.     clip[1] = xywh1[1]<xywh2[1]?xywh2[1]:xywh1[1];
  47.     clip[2] = xywh1[2]>xywh2[2]?xywh2[2]:xywh1[2];
  48.     clip[3] = xywh1[3]>xywh2[3]?xywh2[3]:xywh1[3];
  49.     vs_clip(vdi_id, 1, clip);
  50. }
  51.  
  52. void cursor(int mode)
  53. {
  54.     int cur[4], work[4];
  55.  
  56.     if(mode) {
  57.         console.cur_col = (int) strlen(console.scrollback[(console.row+console.scroll)%CONSOLE_H]);
  58.         console.cur_row = console.row;
  59.     }
  60.  
  61.     wind_get(windforms[WIND_SHOE].whandle, WF_WORKXYWH, work+0, work+1, work+2, work+3);
  62.     if(console.cur_col < 0) return;
  63.     cur[0] = work[0] + console.cell_w*console.cur_col+1;
  64.     cur[1] = work[1] + console.cell_h*console.cur_row+1;
  65.     cur[2] = cur[0] + console.cell_w-2;
  66.     cur[3] = cur[1] + console.cell_h-2;
  67.  
  68.     vsf_color(vdi_id, mode);
  69.     vr_recfl(vdi_id, cur);
  70. }
  71.  
  72. void redraw_console(int *xywh, int row)
  73. {
  74.     int i, j, line[4], work[4];
  75.  
  76.     transform(xywh);
  77.     wind_get(windforms[WIND_SHOE].whandle, WF_WORKXYWH, 
  78.         &work[0], &work[1], &work[2], &work[3]);
  79.     work[2]++; work[3]++;
  80.     transform(work);
  81.     shrink(work, xywh);
  82.  
  83.     graf_mouse(M_OFF,0);
  84.     wind_update(BEG_UPDATE);
  85.     cursor(0);
  86.  
  87.     vsf_color(vdi_id, 0);
  88.     if(row < 0)
  89.         i = 0;
  90.     else
  91.         i = console.row;
  92.     for(; i < CONSOLE_H; i++) {        
  93.         j = (console.scroll+i)%CONSOLE_H;
  94.         line[0] = work[0];
  95.         line[1] = work[1]+console.cell_h*i;
  96.         line[2] = line[0]+console.cell_w*(int)strlen(console.scrollback[j])-1;
  97.         line[3] = line[1];
  98.         vr_recfl(vdi_id, line);
  99.         line[0] = work[2];
  100.         line[3] += console.cell_h-1;
  101.         vr_recfl(vdi_id, line);
  102.         v_gtext(vdi_id, work[0],
  103.                 work[1]+console.cell_h-2+console.cell_h*i,
  104.                 console.scrollback[j]);
  105.  
  106.         if(row >= 0) break;
  107.     }
  108.  
  109.     line[0] = work[0];
  110.     line[1] = work[3];
  111.     line[2] = work[2];
  112.     line[3] = work[3];
  113.     vr_recfl(vdi_id, line);
  114.  
  115.     cursor(1);
  116.     vs_clip(vdi_id, 0, work); /* clipping off */
  117.  
  118.     wind_update(END_UPDATE);
  119.     graf_mouse(M_ON,0);
  120. }
  121.  
  122. void print_line(char *text)
  123. {
  124.     int xywh[4];
  125.     size_t left, len;
  126.     char *s, saved[CONSOLE_W+1];
  127.  
  128.     s = console.scrollback[(console.row+console.scroll)%CONSOLE_H];
  129.     strcpy(saved, s);
  130.  
  131.     left = strlen(text);
  132.     while(left > 0) {
  133.         s = console.scrollback[(console.row+console.scroll)%CONSOLE_H];
  134.         len = min(CONSOLE_W, left);
  135.         strncpy(s, text, len);
  136.         s[len] = '\0';
  137.         text += len;
  138.         left -= len;
  139.  
  140.         if(console.row+1 < CONSOLE_H)
  141.             console.row++;
  142.         else
  143.             console.scroll = (console.scroll+1)%CONSOLE_H;
  144.         console.scrollback[(console.row+console.scroll)%CONSOLE_H][0] = '\0';
  145.     }
  146.  
  147.     s = console.scrollback[(console.row+console.scroll)%CONSOLE_H];
  148.     strcpy(s, saved);    
  149.  
  150.     wind_get(windforms[WIND_SHOE].whandle, WF_FIRSTXYWH, 
  151.         &xywh[0], &xywh[1], &xywh[2], &xywh[3]);
  152.     while(xywh[2] || xywh[3]) {
  153.         redraw_console(xywh, -1);
  154.         wind_get(windforms[WIND_SHOE].whandle, WF_NEXTXYWH, 
  155.             &xywh[0], &xywh[1], &xywh[2], &xywh[3]);
  156.     }
  157. }
  158.  
  159. void clear_console(void)
  160. {
  161.     int i, xywh[4];
  162.     char *s, saved[CONSOLE_W+1];
  163.     
  164.     s = console.scrollback[(console.row+console.scroll)%CONSOLE_H];
  165.     strcpy(saved, s);
  166.  
  167.     console.row = 0;
  168.     console.scroll = 0;
  169.     console.cur_row = console.cur_col = 0;
  170.  
  171.     for(i = 0; i < CONSOLE_H; i++)
  172.         console.scrollback[i][0] = '\0';
  173.     
  174.     s = console.scrollback[(console.row+console.scroll)%CONSOLE_H];
  175.     strcpy(s, saved);    
  176.  
  177.     wind_get(windforms[WIND_SHOE].whandle, WF_FIRSTXYWH, 
  178.         &xywh[0], &xywh[1], &xywh[2], &xywh[3]);
  179.     while(xywh[2] || xywh[3]) {
  180.         redraw_console(xywh, -1);
  181.         wind_get(windforms[WIND_SHOE].whandle, WF_NEXTXYWH, 
  182.             &xywh[0], &xywh[1], &xywh[2], &xywh[3]);
  183.     }
  184. }
  185.  
  186. void prompt_console(int key, int delta)
  187. {
  188.     char *s, *h, *sap, *shoe_res;
  189.     int xywh[4], pbal;
  190.     size_t plen;
  191.     static char p[10];
  192.  
  193.     pbal = (int)inquire_balance();
  194.     if(pbal)
  195.         sprintf(p, "%d> ", pbal);
  196.     else
  197.         sprintf(p, "> ");
  198.  
  199.     plen = strlen(p);
  200.  
  201.     s = console.scrollback[(console.row+console.scroll)%CONSOLE_H];
  202.     if(strlen(s) == 0) {
  203.         strncpy(s, p, min(CONSOLE_W, plen));
  204.         s[min(CONSOLE_W, plen)] = '\0';
  205.     }
  206.  
  207.     sap = &s[plen];
  208.  
  209.     switch((key>>8) & 0xff) {
  210.     case 72: /* Arrow up */
  211.         if(console.hist_nr == console.hist_max) {
  212.             h = console.hist_list[console.hist_max];
  213.             strcpy(h, sap);
  214.         }
  215.         if(console.hist_nr != console.hist_min) {
  216.             console.hist_nr = (console.hist_nr-1+HISTORY_LEN) % HISTORY_LEN;
  217.             h = console.hist_list[console.hist_nr], 
  218.             strncpy(sap, h, min(CONSOLE_W-plen-1,strlen(h)));
  219.             sap[min(CONSOLE_W-plen-1,strlen(h))] = '\0';
  220.             prompt_console(0, -1);
  221.         }
  222.         break;
  223.  
  224.     case 80: /* Arrow down */
  225.         if(console.hist_nr != console.hist_max) {
  226.             console.hist_nr = (console.hist_nr+1) % HISTORY_LEN;
  227.             h = console.hist_list[console.hist_nr], 
  228.             strncpy(sap, h, min(CONSOLE_W-plen-1,strlen(h)));
  229.             sap[min(CONSOLE_W-plen-1,strlen(h))] = '\0';
  230.             prompt_console(0, -1);
  231.         }
  232.         break;
  233.  
  234.     default:
  235.         break;
  236.     }
  237.  
  238.     switch(key & 0xff) {
  239.     case 0x00: /* no key pressed */
  240.         break;
  241.         
  242.     case 0x08: /* backspace */
  243.         if(plen < strlen(s)) {
  244.             s[strlen(s)-1] = '\0';
  245.         }
  246.         break;
  247.  
  248.     case 0x0d: /* return */
  249.         /* Save row in history if not empty */
  250.         if(sap[0]) {
  251.             h = console.hist_list[(console.hist_row+console.hist_off)%HISTORY_LEN];
  252.             strcpy(h, sap);
  253.             if(console.hist_row+1 < HISTORY_LEN)
  254.                 console.hist_row++;
  255.             else
  256.                 console.hist_off = (console.hist_off+1) % HISTORY_LEN;
  257.  
  258.             console.hist_list[(console.hist_row+console.hist_off)%HISTORY_LEN][0] = '\0';
  259.  
  260.             console.hist_max=(console.hist_row+console.hist_off)%HISTORY_LEN;
  261.             if(console.hist_off)
  262.                 console.hist_min=(console.hist_max+1)%HISTORY_LEN;
  263.             else
  264.                 console.hist_min=0;
  265.         
  266.             console.hist_nr=console.hist_max;
  267.         }
  268.  
  269.         if(console.row+1 < CONSOLE_H)
  270.             console.row++;
  271.         else
  272.             console.scroll = (console.scroll+1)%CONSOLE_H;
  273.         console.scrollback[(console.row+console.scroll)%CONSOLE_H][0] = '\0';
  274.  
  275.         print_line("");
  276.  
  277.         shoe_res = parse_eval(sap);
  278.         if(shoe_res && shoe_res[0])
  279.             print_line(shoe_res);
  280.  
  281.         prompt_console(0, -1);
  282.         
  283.         return;
  284.  
  285.     default:
  286.         if(strlen(s) < CONSOLE_W-1) {
  287.             strncat(s, (char*)&key+1, 1);
  288.         }
  289.     }
  290.  
  291.     wind_get(windforms[WIND_SHOE].whandle, WF_FIRSTXYWH, 
  292.         &xywh[0], &xywh[1], &xywh[2], &xywh[3]);
  293.     while(xywh[2] || xywh[3]) {
  294.         redraw_console(xywh, delta);
  295.         wind_get(windforms[WIND_SHOE].whandle, WF_NEXTXYWH, 
  296.             &xywh[0], &xywh[1], &xywh[2], &xywh[3]);
  297.     }
  298. }
  299.  
  300. void notify_console(char *msg)
  301. {
  302.     int i;
  303.     char *s;
  304.     
  305.     s = windforms[WIND_SHOE].wind_title + strlen(windforms[WIND_SHOE].wind_title) - 5;
  306.     for(i = 0; i < 5; i++)
  307.         s[i] = ' ';
  308.     if(msg && strlen(msg)) {
  309.         s[0] = '(';
  310.         for(i = 0; i < 3 && msg[i]; i++)
  311.             s[i+1] = msg[i];
  312.         s[i+1] = ')';
  313.     }
  314.     wind_set(windforms[WIND_SHOE].whandle,WF_NAME,
  315.              windforms[WIND_SHOE].wind_title);
  316. }
  317.  
  318. void initialize_console(int w, int h)
  319. {
  320.     int i;
  321.     int dummy[4];
  322.  
  323.     console.w = w;
  324.     console.h = h;
  325.  
  326.     vst_point(vdi_id, 9, &console.char_w, &console.char_h,
  327.                          &console.cell_w, &console.cell_h);
  328.     console.cell_h++;
  329.  
  330.     console.screen_w = console.cell_w*console.w;
  331.     console.screen_h = console.cell_h*console.h;
  332.  
  333.     console.row = 0;
  334.     console.scroll = 0;
  335.     console.cur_col = console.cur_row = -1;
  336.  
  337.     for(i = 0; i < h; i++)
  338.         console.scrollback[i][0] = '\0';
  339.  
  340.     console.hist_row = 0;
  341.     console.hist_off = 0;
  342.     console.hist_nr = 0;
  343.     console.hist_min = 0;
  344.     console.hist_max = 0;
  345.  
  346.     for(i = 0; i < HISTORY_LEN; i++)
  347.         console.hist_list[i][0] = '\0';
  348.  
  349.     vsf_interior(vdi_id, FIS_SOLID);
  350.     vswr_mode(vdi_id, MD_REPLACE);
  351.     vst_color(vdi_id, 1);
  352.     vst_effects(vdi_id, 0);
  353.     vst_alignment(vdi_id, 0, 0, dummy, dummy);
  354.     vst_rotation(vdi_id, 0);
  355. }
  356.         
  357.