source: abuse/trunk/src/console.cpp @ 39

Last change on this file since 39 was 39, checked in by Sam Hocevar, 15 years ago
  • Fix almost 2,000 warnings by using proper "const" keywords where needed.
File size: 3.6 KB
Line 
1#include "linked.hpp"
2#include "console.hpp"
3#include "jmalloc.hpp"
4#include <ctype.h>
5#include <stdarg.h>
6
7extern window_manager *eh;
8
9void console::put_string(char const *st)
10{
11  while (*st)
12  {
13    put_char(*st);
14    st++;
15  }
16}
17
18void console::redraw()
19{
20  if (con_win)
21  {
22    con_win->clear();
23    char *s=screen;
24    int dx,dy,xa=fnt->width(),ya=fnt->height(),i,j;
25    for (j=0,dy=wy();j<h;j++,dy+=ya)
26    {
27      for (i=0,dx=wx();i<w;i++,s++,dx+=xa)
28      {
29        if (*s)
30          fnt->put_char(con_win->screen,dx,dy,*s);
31      }
32    }
33    fnt->put_char(con_win->screen,wx()+cx*xa,wy()+cy*ya,'_');
34  }
35}
36
37void console::show()
38{
39  if (!con_win)
40  {
41    con_win=wm->new_window(lastx,lasty,screen_w(),screen_h(),NULL,name);
42    redraw();
43    con_win->screen->set_clip(con_win->x1(),con_win->y1(),con_win->x2(),con_win->y2());
44  }
45}
46
47void console::hide()
48{
49  if (con_win)
50  {
51    lastx=con_win->x;
52    lasty=con_win->y;
53    wm->close_window(con_win);
54    con_win=NULL;
55  }
56}
57
58console::~console()
59{
60  hide();
61  jfree(screen);
62  jfree(name);
63}
64
65console::console(window_manager *WM, JCFont *font, int width, int height, char const *Name)
66{
67  wm=WM;
68  con_win=NULL;
69  w=width;
70  h=height;
71  screen=(char *)jmalloc(w*h,"console screen");
72  memset(screen,' ',w*h);
73  cx=cy=0;
74  fnt=font;
75  lastx=xres/2-screen_w()/2;
76  lasty=yres/2-screen_h()/2;
77  name=(char *)strcpy((char *)jmalloc(strlen(Name)+1,"console name"),Name);
78}
79
80
81void console::draw_cursor()
82{
83  if (con_win)
84    fnt->put_char(con_win->screen,cx*fnt->width()+wx(),cy*fnt->height()+wy(),'_');
85}
86
87
88void console::draw_char(int x, int y, char ch)
89{
90  if (con_win)
91  {
92    int fw=fnt->width(),fh=fnt->height();
93    int dx=wx()+x*fw,dy=wy()+y*fh;
94    con_win->screen->bar(dx,dy,dx+fw-1,dy+fh-1,wm->black());
95    fnt->put_char(con_win->screen,dx,dy,ch);
96  }
97}
98
99void console::do_cr()
100{
101  if (cx<w && cy<h)  draw_char(cx,cy,screen[cy*w+cx]);
102  cx=0;
103  cy++; 
104  if (cy>=h)
105  {
106    cy=h-1;
107    if (con_win)
108    {
109      memmove(screen,screen+w,w*(h-1));
110      memset(screen+w*(h-1),' ',w);
111      redraw();
112      eh->flush_screen();
113    }
114  } else draw_cursor();   
115}
116
117void console::put_char(char ch)
118{
119
120 
121  switch (ch)
122  {
123    case JK_BACKSPACE :
124    {
125      if (cx)
126      {
127        if (con_win)
128          draw_char(cx,cy,screen[cy*w+cx]);
129        cx--;
130        if (con_win)
131          draw_cursor();
132      }
133    } break;
134    case '\n' :
135    case JK_ENTER :
136    {
137      do_cr();
138    } break;
139    default :
140    {
141      screen[cy*w+cx]=ch;
142      if (con_win)
143        draw_char(cx,cy,ch);
144      cx++;
145      if (cx>=w) do_cr(); else
146      if (con_win) draw_cursor();
147    }
148  } 
149}
150
151
152void console::print_f( const char *format, ...)
153{
154  char st[300];
155  va_list ap;
156  va_start(ap, format);
157  vsprintf(st,format,ap);
158  va_end(ap);
159  put_string(st);
160}
161
162
163shell_term::shell_term(window_manager *WM, JCFont *font, int width, int height, char const *Name) :
164  console(WM,font,width,height,Name)
165{
166  shcmd[0]=0;
167  prompt();
168}
169
170void shell_term::prompt()
171{
172  put_string("(?=help)>");
173}
174
175void shell_term::execute(char const *st)
176{
177  put_string(st);
178  put_string(" : unhandled\n");
179}
180
181int shell_term::handle_event(event &ev, window_manager *wm)
182{
183  if (ev.window==con_win && con_win)
184  {
185    switch (ev.type)
186    {
187      case EV_KEY :
188      {
189        switch (ev.key)
190        {
191          case JK_BACKSPACE:
192          {
193            if (shcmd[0]!=0)
194            {
195              shcmd[strlen(shcmd)-1]=0;
196              put_char(ev.key);
197            }
198          } break;
199          case JK_ENTER :
200          {
201            put_char(ev.key);
202            execute(shcmd);
203            prompt();
204            shcmd[0]=0;
205          } break;
206          default :
207          {
208            if (ev.key<256 && isprint(ev.key))
209            {
210              int x=strlen(shcmd);
211              shcmd[x+1]=0;
212              shcmd[x]=ev.key;
213              put_char(ev.key);
214            }
215          } break;
216        } break;
217      }
218    }
219    return 1;
220  }
221  return 0;
222}
223
Note: See TracBrowser for help on using the repository browser.