Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include<menuet/os.h>
  2. #include<stdlib.h>
  3. #include"textcon.h"
  4.  
  5. console_t * consoles[MAX_CONSOLES];
  6. console_t * visible_console;
  7.  
  8. static unsigned long color_conv_tbl[]=COLOR_CONV_B_2_D;
  9.  
  10. void init_consoles(void)
  11. {
  12.  int i;
  13.  visible_console=NULL;
  14.  for(i=0;i<MAX_CONSOLES;i++)
  15.  {
  16.   consoles[i]=(console_t *)malloc(sizeof(console_t));
  17.   consoles[i]->text_color=7;
  18.   consoles[i]->back_color=1;
  19.   consoles[i]->id=i+1;
  20.   consoles[i]->cur_x=0;
  21.   consoles[i]->cur_y=0;
  22.   consoles[i]->cur_visible=1;
  23.   consoles[i]->cur_color=4;
  24.   consoles[i]->esc_seq.esc[0]=0;
  25.   consoles[i]->esc_seq.esc[1]=0;
  26.   consoles[i]->esc_seq.esc[2]=0;
  27.   consoles[i]->esc_seq.esc[3]=0;
  28.   fill_empty_sem(&consoles[i]->io_lock);
  29.   lcon_clrscr(consoles[i]);
  30.  }
  31.  visible_console=consoles[0];
  32.  lcon_flush_console(visible_console);
  33. }
  34.  
  35. void lcon_flush_console(console_t * con)
  36. {
  37.  int x,y;
  38.  int ax,ay;
  39.  char tbl[2];
  40.  unsigned long c1,c2;
  41.  if(con!=visible_console) return;
  42.  tbl[1]='\0';
  43.  for(y=0;y<NR_CHARS_Y;y++)
  44.  {
  45.   ay=CON_AT_Y+y*CHAR_SIZE_Y;
  46.   for(x=0;x<NR_CHARS_X;x++)
  47.   {
  48.    ax=CON_AT_X+x*CHAR_SIZE_X;
  49.    c1=color_conv_tbl[con->char_table[x][y].c_back];
  50.    tbl[0]=(char)con->char_table[x][y].c_char&0xFF;
  51.    if(con->cur_x==x && con->cur_y==y && con->cur_visible)
  52.    {
  53.     c2=c1;
  54.     c1=color_conv_tbl[con->cur_color];
  55.    } else {
  56.     c2=color_conv_tbl[con->char_table[x][y].c_color];
  57.    }
  58.    __menuet__bar(ax,ay,CHAR_SIZE_X,CHAR_SIZE_Y,c1);
  59.    __menuet__write_text(ax,ay,c2,tbl,1);
  60.   }
  61.  }
  62. }
  63.  
  64. void lcon_clrscr(console_t * con)
  65. {
  66.  int x,y;
  67.  if(!con) return;
  68.  for(y=0;y<NR_CHARS_Y;y++)
  69.   for(x=0;x<NR_CHARS_X;x++)
  70.   {
  71.    con->char_table[x][y].c_char=' ';
  72.    con->char_table[x][y].c_back=con->back_color;
  73.    con->char_table[x][y].c_color=con->text_color;
  74.   }
  75.  lcon_flush_console(con);
  76. }
  77.  
  78. void lcon_flushxy(console_t * con,int x,int y)
  79. {
  80.  char tbl[2];
  81.  int ax,ay;
  82.  unsigned long c1,c2;
  83.  if(con!=visible_console) return;
  84.  ay=CON_AT_Y+y*CHAR_SIZE_Y;
  85.  ax=CON_AT_X+x*CHAR_SIZE_X;
  86.  c1=color_conv_tbl[con->char_table[x][y].c_back];
  87.  tbl[0]=(char)con->char_table[x][y].c_char&0xFF;
  88.  if(con->cur_x==x && con->cur_y==y && con->cur_visible)
  89.  {
  90.   c2=c1;
  91.   c1=color_conv_tbl[con->cur_color];
  92.  } else {
  93.   c2=color_conv_tbl[con->char_table[x][y].c_color];
  94.  }
  95.  __menuet__bar(ax,ay,CHAR_SIZE_X,CHAR_SIZE_Y,c1);
  96.  __menuet__write_text(ax,ay,c2,tbl,1);
  97. }
  98.  
  99. void lcon_scroll(console_t * con,int update)
  100. {
  101.  int y,x;
  102.  for(y=0;y<NR_CHARS_Y-1;y++)
  103.  {
  104.   for(x=0;x<NR_CHARS_X;x++)
  105.   {
  106.    memcpy(&con->char_table[x][y],&con->char_table[x][y+1],sizeof(char_info_t));
  107.   }
  108.  }
  109.  for(x=0;x<NR_CHARS_X;x++)
  110.  {
  111.   con->char_table[x][NR_CHARS_Y-1].c_char=' ';
  112.   con->char_table[x][NR_CHARS_Y-1].c_color=con->text_color;
  113.   con->char_table[x][NR_CHARS_Y-1].c_back=con->back_color;
  114.  }
  115.  if(update) lcon_flush_console(con);
  116. }
  117.  
  118. static int __con_update(console_t * con)
  119. {
  120.  int m;
  121.  m=0;
  122.  if(con->cur_x<0 || con->cur_y<0)
  123.  {
  124.   if(con->cur_x<0)
  125.   {
  126.    con->cur_x=0;
  127.   }
  128.   if(con->cur_y<0)
  129.   {
  130.    con->cur_y=0;
  131.   }
  132.   m=1;
  133.  }
  134.  if(con->cur_x>=NR_CHARS_X)
  135.  {
  136.   con->cur_y++;
  137.   con->cur_x=0;
  138.   m=1;
  139.   if(con->cur_y>=NR_CHARS_Y)
  140.   {
  141.    con->cur_y=NR_CHARS_Y-1;
  142.    lcon_scroll(con,0);
  143.   }
  144.  }
  145.  return m;
  146. }
  147.  
  148. void __lcon_putch_raw(console_t * con,char c)
  149. {
  150.  int tx,ty;
  151.  __con_update(con);
  152.  tx=con->cur_x;
  153.  ty=con->cur_y;
  154.  if(c=='\r')
  155.  {
  156.   con->cur_x=0;
  157.  } else if(c=='\n')
  158.  {
  159.   con->cur_x=0;
  160.   con->cur_y++;
  161.  } else if(c=='\t')
  162.  {
  163.   con->cur_x=(con->cur_x+8)&~(8-1);
  164.  } else if(c=='\b')
  165.  {
  166.   con->cur_x--;
  167.   if(con->cur_x<0)
  168.   {
  169.    con->cur_y--;
  170.    con->cur_x=0;
  171.   }
  172.   if(con->cur_y<0)
  173.   {
  174.    con->cur_y=0;
  175.   }
  176.  } else {      
  177.   con->char_table[con->cur_x][con->cur_y].c_char=c;
  178.   con->char_table[con->cur_x][con->cur_y].c_color=con->text_color;
  179.   con->char_table[con->cur_x][con->cur_y].c_back=con->back_color;
  180.   con->cur_x++;
  181.  }
  182.  if(__con_update(con))
  183.  {
  184.   lcon_flush_console(con);
  185.  } else {
  186.   lcon_flushxy(con,tx,ty);
  187.   if(con->cur_visible) lcon_flushxy(con,con->cur_x,con->cur_y);
  188.  }
  189. }
  190.  
  191. void lcon_gotoxy(console_t * con,int x,int y)
  192. {
  193.  int ox,oy;
  194.  if(x<0 || x>=NR_CHARS_X || y<0 || y>=NR_CHARS_Y) return;
  195.  ox=con->cur_x;
  196.  oy=con->cur_y;
  197.  con->cur_x=x;
  198.  con->cur_y=y;
  199.  if(con->cur_visible)
  200.  {
  201.   lcon_flushxy(con,ox,oy);
  202.   lcon_flushxy(con,x,y);
  203.  }
  204. }
  205.  
  206. void lcon_set_text_color(console_t * con,int color)
  207. {
  208.  con->text_color=color&0xFF;
  209. }
  210.  
  211. void lcon_set_back_color(console_t * con,int color)
  212. {
  213.  con->back_color=color&0xFF;
  214. }
  215.  
  216. void lcon_switch_to_console(int i)
  217. {
  218.  if(i<0 || i>=MAX_CONSOLES || !consoles[i]) return;
  219.  visible_console=consoles[i];
  220.  lcon_flush_console(visible_console);
  221. }
  222.  
  223. unsigned char lcon_getcxy(console_t * con,int x,int y)
  224. {
  225.  if(x<0 || x>=NR_CHARS_X || y<0 || y>=NR_CHARS_Y) return;
  226.  return con->char_table[x][y].c_char;
  227. }
  228.  
  229. void lcon_putcxy(console_t * con,int x,int y,unsigned char c)
  230. {
  231.  if(x<0 || x>=NR_CHARS_X || y<0 || y>=NR_CHARS_Y) return;
  232.  con->char_table[x][y].c_char=c;
  233.  con->char_table[x][y].c_color=con->text_color;
  234.  con->char_table[x][y].c_back=con->back_color;
  235. }
  236.  
  237. static void lcon_set_attrib(console_t * con,unsigned char Att)
  238. {
  239.  static const unsigned ansi_to_vga[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
  240.  if(Att>=30 && Att<=37)
  241.  {
  242.   con->text_color=ansi_to_vga[Att-30];
  243.  } else if(Att>=40 && Att<=48)
  244.  {
  245.   con->back_color=ansi_to_vga[Att-40];
  246.  }
  247. }
  248.  
  249. #undef isdigit
  250. #define isdigit(c) ((c)>='0' && (c)<='9')
  251.  
  252. void __lcon_putch_help(console_t * con,unsigned char c)
  253. {
  254.  if(con->esc_seq.esc[0]==1)
  255.  {
  256.   if(c=='[')
  257.   {
  258.    con->esc_seq.esc[0]++;
  259.    con->esc_seq.esc[1]=0;
  260.    return;
  261.   }
  262.  } else if(con->esc_seq.esc[0]==2)
  263.  {
  264.   if(isdigit(c))
  265.   {
  266.    con->esc_seq.esc[1]=con->esc_seq.esc[1]*10+c-'0';
  267.    return;
  268.   } else if(c==';')
  269.   {
  270.    con->esc_seq.esc[0]++;
  271.    con->esc_seq.esc[2]=0;
  272.    return;
  273.   } else if(c=='J')
  274.   {
  275.    if(con->esc_seq.esc[1]==2)
  276.    {
  277.     lcon_clrscr(con);
  278.     lcon_gotoxy(con,0,0);
  279.    }
  280.   } else if(c=='m') lcon_set_attrib(con,con->esc_seq.esc[1]);
  281.   con->esc_seq.esc[0]=0;
  282.   return;
  283.  } else if(con->esc_seq.esc[0]==3)
  284.  {
  285.   if(isdigit(c))
  286.   {
  287.    con->esc_seq.esc[2]=(con->esc_seq.esc[2]*10)+c-'0';
  288.    return;
  289.   } else if(c==';')
  290.   {
  291.    con->esc_seq.esc[0]++;
  292.    con->esc_seq.esc[3]=0;
  293.    return;
  294.   } else if(c=='H')
  295.   {
  296.    int nx,ny;
  297.    if(con->esc_seq.esc[2]<NR_CHARS_X)
  298.     nx=con->esc_seq.esc[2]; else nx=con->cur_x;
  299.    if(con->esc_seq.esc[1]<NR_CHARS_Y)
  300.     ny=con->esc_seq.esc[1]; else ny=con->cur_y;
  301.    lcon_gotoxy(con,nx,ny);
  302.   } else if(c=='m')
  303.   {
  304.    lcon_set_attrib(con,con->esc_seq.esc[1]);
  305.    lcon_set_attrib(con,con->esc_seq.esc[2]);
  306.   }
  307.   con->esc_seq.esc[0]=0;
  308.   return;
  309.  } else if(con->esc_seq.esc[0]==4)
  310.  {
  311.   if(isdigit(c))
  312.   {
  313.    con->esc_seq.esc[3]=con->esc_seq.esc[3]*10+c-'0';
  314.    return;
  315.   } else if(c=='m')
  316.   {
  317.    lcon_set_attrib(con,con->esc_seq.esc[1]);
  318.    lcon_set_attrib(con,con->esc_seq.esc[2]);
  319.    lcon_set_attrib(con,con->esc_seq.esc[3]);
  320.   }
  321.   con->esc_seq.esc[0]=0;
  322.   return;
  323.  }
  324.  con->esc_seq.esc[0]=0;
  325.  if(c==0x1B)
  326.  {
  327.   con->esc_seq.esc[0]=1;
  328.   return;
  329.  }
  330.  __lcon_putch_raw(con,c);
  331. }
  332.  
  333. void lcon_putch(console_t * con,char c)
  334. {
  335.  sem_lock(&con->io_lock);
  336.  __lcon_putch_help(con,c);
  337.  sem_unlock(&con->io_lock);
  338. }
  339.  
  340. console_t * create_private_console(void)
  341. {
  342.  console_t * con=(console_t *)malloc(sizeof(console_t));
  343.  if(!con) return NULL;
  344.  con->id=MAX_CONSOLES|0x8000;
  345.  con->cur_x=0;
  346.  con->cur_y=0;
  347.  con->cur_visible=0;
  348.  con->cur_color=3;
  349.  con->text_color=3;
  350.  con->back_color=1;
  351.  con->esc_seq.esc[0]=0;
  352.  con->esc_seq.esc[1]=0;
  353.  con->esc_seq.esc[2]=0;
  354.  con->esc_seq.esc[3]=0;
  355.  fill_empty_sem(&con->io_lock);
  356.  lcon_clrscr(con);
  357.  return con;
  358. }
  359.  
  360. void free_private_console(console_t * con)
  361. {
  362.  if(con)
  363.  {
  364.   sem_lock(&con->io_lock); /* Wait for lock release and lock it */
  365.   sem_unlock(&con->io_lock); /* Unlock immediately */
  366.   free((void *)con); /* Free structure */
  367.  }
  368. }
  369.