home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / LO241SRV.ZIP / COLORS.C < prev    next >
C/C++ Source or Header  |  1998-05-17  |  7KB  |  351 lines

  1.  
  2. // LoraBBS Version 2.41 Free Edition
  3. // Copyright (C) 1987-98 Marco Maccaferri
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdarg.h>
  22. #include <alloc.h>
  23.  
  24. #include <cxl\cxlvid.h>
  25. #include <cxl\cxlwin.h>
  26.  
  27. #include "lsetup.h"
  28. #include "sched.h"
  29. #include "msgapi.h"
  30. #include "externs.h"
  31. #include "prototyp.h"
  32.  
  33. extern short tcpip;
  34.  
  35. static byte last_color = 0x07;
  36.  
  37. static void ansi_print(char *format, ...);
  38.  
  39. void cls (void)
  40. {
  41.    byte pc;
  42.  
  43.    if (snooping) {
  44.       pc = last_color;
  45.       change_attr(LGREY|_BLACK);
  46.       last_color = pc;
  47.    }
  48.  
  49.    if (usr.ansi || usr.avatar) {
  50.       if (!local_mode) {
  51.             if (usr.ansi && (!usr.formfeed || tcpip))
  52.                 ansi_print ("\x1B[2J\x1B[1;1f");
  53.             else
  54.                 BUFFER_BYTE (CTRLL);
  55.       }
  56.       if (snooping)
  57.          wclear ();
  58.    }
  59.    else {
  60.       if (!local_mode) {
  61.          BUFFER_BYTE('\r');
  62.          BUFFER_BYTE('\n');
  63.       }
  64.       if (snooping) {
  65.          wputc ('\r');
  66.          wputc ('\n');
  67.       }
  68.    }
  69.  
  70.    restore_last_color ();
  71.  
  72.    if (!local_mode)
  73.       UNBUFFER_BYTES ();
  74. }
  75.  
  76. #define  NORM   0x07
  77. #define  BOLD   0x08
  78. #define  FAINT  0xF7
  79. #define  VBLINK 0x80
  80. #define  REVRS  0x77
  81.  
  82. void change_attr(i)
  83. int i;
  84. {
  85.    char s[30];
  86.  
  87.    if ((!usr.ansi && !usr.avatar) || !usr.color)
  88.       return;
  89.  
  90.    last_color = i;
  91.    if ((snooping || local_mode))
  92.       wtextattr (i);
  93.  
  94.    if (usr.avatar && !local_mode) {
  95.       BUFFER_BYTE (CTRLV);
  96.       BUFFER_BYTE (CTRLA);
  97.       BUFFER_BYTE (i & 0xFF);
  98.  
  99.       UNBUFFER_BYTES ();
  100.    }
  101.    else if (usr.ansi && !local_mode) {
  102.       strcpy(s,"\033[0");
  103.  
  104.       if (i & 0x80)
  105.          strcat (s, ";5");
  106.  
  107.       switch (i & 0x0F) {
  108.          case BLACK:
  109.             strcat(s,";30");
  110.             break;
  111.          case BLUE:
  112.             strcat(s,";34");
  113.             break;
  114.          case GREEN:
  115.             strcat(s,";32");
  116.             break;
  117.          case CYAN:
  118.             strcat(s,";36");
  119.             break;
  120.          case RED:
  121.             strcat(s,";31");
  122.             break;
  123.          case MAGENTA:
  124.             strcat(s,";35");
  125.             break;
  126.          case BROWN:
  127.             strcat(s,";33");
  128.             break;
  129.          case LGREY:
  130.             strcat(s,";37");
  131.             break;
  132.          case DGREY:
  133.             strcat(s,";1;30");
  134.             break;
  135.          case LBLUE:
  136.             strcat(s,";1;34");
  137.             break;
  138.          case LGREEN:
  139.             strcat(s,";1;32");
  140.             break;
  141.          case LCYAN:
  142.             strcat(s,";1;36");
  143.             break;
  144.          case LRED:
  145.             strcat(s,";1;31");
  146.             break;
  147.          case LMAGENTA:
  148.             strcat(s,";1;35");
  149.             break;
  150.          case YELLOW:
  151.             strcat(s,";1;33");
  152.             break;
  153.          case WHITE:
  154.             strcat(s,";1;37");
  155.             break;
  156.       }
  157.  
  158.       switch (i & 0xF0) {
  159.          case _BLACK:
  160.             strcat(s,";40");
  161.             break;
  162.          case _BLUE:
  163.             strcat(s,";44");
  164.             break;
  165.          case _GREEN:
  166.             strcat(s,";42");
  167.             break;
  168.          case _CYAN:
  169.             strcat(s,";46");
  170.             break;
  171.          case _RED:
  172.             strcat(s,";41");
  173.             break;
  174.          case _MAGENTA:
  175.             strcat(s,";45");
  176.             break;
  177.          case _BROWN:
  178.             strcat(s,";43");
  179.             break;
  180.          case _LGREY:
  181.             strcat(s,";47");
  182.             break;
  183.       }
  184.  
  185.       ansi_print ("%sm", s);
  186.    }
  187. }
  188.  
  189. void del_line()
  190. {
  191.    if (usr.ansi && !local_mode)
  192.       ansi_print("\x1B[K");
  193.    if (usr.avatar && !local_mode) {
  194.       BUFFER_BYTE(CTRLV);
  195.       BUFFER_BYTE(CTRLG);
  196.  
  197.       UNBUFFER_BYTES ();
  198.    }
  199.    if (snooping)
  200.       wclreol();
  201. }
  202.  
  203. void cpos(y, x)
  204. int y, x;
  205. {
  206.    if (usr.ansi && !local_mode)
  207.       ansi_print("\x1B[%d;%df",y,x);
  208.    if (usr.avatar && !local_mode) {
  209.       BUFFER_BYTE (CTRLV);
  210.       BUFFER_BYTE (CTRLH);
  211.       BUFFER_BYTE (y);
  212.       BUFFER_BYTE (x);
  213.  
  214.       UNBUFFER_BYTES ();
  215.    }
  216.    if (snooping && (usr.avatar || usr.ansi))
  217.         wgotoxy ( y ? y-1 : y ,  x ? x-1 : x);
  218.     }
  219.  
  220. void cup(y)
  221. int y;
  222. {
  223.    int a, b;
  224.  
  225.    if (snooping && (usr.avatar || usr.ansi)) {
  226.       wreadcur (&a, &b);
  227.       a -= y;
  228.       wgotoxy (a, b);
  229.    }
  230.  
  231.    if (usr.ansi && !local_mode)
  232.       ansi_print ("\x1B[%dA", y);
  233.    else if (usr.avatar && !local_mode)
  234.       for(;--y >= 0;)
  235.       {
  236.          SENDBYTE (CTRLV);
  237.          SENDBYTE (CTRLC);
  238.       }
  239. }
  240.  
  241. void cdo(y)
  242. int y;
  243. {
  244.    int a, b;
  245.  
  246.    if (snooping && (usr.avatar || usr.ansi)) {
  247.       wreadcur (&a, &b);
  248.       a += y;
  249.       wgotoxy (a, b);
  250.    }
  251.  
  252.    if (usr.ansi && !local_mode)
  253.       ansi_print ("\x1B[%dB", y);
  254.    else if (usr.avatar && !local_mode)
  255.       for(;--y >= 0;) {
  256.          SENDBYTE (CTRLV);
  257.          SENDBYTE (CTRLD);
  258.       }
  259. }
  260.  
  261. void cri(x)
  262. int x;
  263. {
  264.    int a, b;
  265.  
  266.    if (snooping && (usr.avatar || usr.ansi)) {
  267.       wreadcur (&a, &b);
  268.       b += x;
  269.       wgotoxy (a, b);
  270.    }
  271.  
  272.    if (usr.ansi && !local_mode)
  273.       ansi_print ("\x1B[%dC", x);
  274.    else if (usr.avatar && !local_mode) {
  275.       if (x > 6) {
  276.          SENDBYTE (CTRLV);
  277.          SENDBYTE (CTRLY);
  278.          SENDBYTE (2);
  279.          SENDBYTE (CTRLV);
  280.          SENDBYTE (CTRLF);
  281.          SENDBYTE (x);
  282.       }
  283.       else
  284.          for(;--x >= 0;) {
  285.             SENDBYTE (CTRLV);
  286.             SENDBYTE (CTRLF);
  287.          }
  288.    }
  289. }
  290.  
  291. void cle(x)
  292. int x;
  293. {
  294.    int a, b;
  295.  
  296.    if (snooping && (usr.avatar || usr.ansi)) {
  297.       wreadcur (&a, &b);
  298.       b -= x;
  299.       wgotoxy (a, b);
  300.    }
  301.  
  302.    if (usr.ansi && !local_mode)
  303.       ansi_print ("\x1B[%dD", x);
  304.    else if (usr.avatar && !local_mode)
  305.       for(;--x >= 0;) {
  306.          SENDBYTE (CTRLV);
  307.          SENDBYTE (CTRLE);
  308.       }
  309. }
  310.  
  311. void restore_last_color ()
  312. {
  313.    change_attr (last_color);
  314. }
  315.  
  316. static void ansi_print(char *format, ...)
  317. {
  318.    va_list var_args;
  319.    char *string, *q, visual;
  320.  
  321.    string = (char *)malloc (256);
  322.  
  323.    if (string == NULL || strlen (format) > 256) {
  324.       if (string)
  325.          free (string);
  326.       return;
  327.    }
  328.  
  329.    va_start (var_args, format);
  330.    vsprintf (string, format, var_args);
  331.    va_end (var_args);
  332.  
  333.    visual = 1;
  334.  
  335.    for (q=string; *q; q++) {
  336.       if (*q == 0x1B)
  337.          visual = 0;
  338.  
  339.       if (!local_mode)
  340.          BUFFER_BYTE ((*q));
  341.       if (snooping && visual)
  342.          wputc (*q);
  343.    }
  344.  
  345.    if (!local_mode)
  346.       UNBUFFER_BYTES ();
  347.  
  348.    free (string);
  349. }
  350.  
  351.