source: abuse/branches/pd/macabuse/src/console.c @ 161

Last change on this file since 161 was 49, checked in by Sam Hocevar, 15 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
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 *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 *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
152
153
154
155void console::printf(const char *format, ...)
156{
157  char st[300],a,*sp;
158  int y;
159  va_list ap;
160  va_start(ap, format);
161  vsprintf(st,format,ap);
162  va_end(ap);
163  put_string(st);
164}
165
166
167shell_term::shell_term(window_manager *WM, JCFont *font, int width, int height, char *Name) :
168  console(WM,font,width,height,Name)
169{
170  shcmd[0]=0;
171  prompt();
172}
173
174void shell_term::prompt()
175{
176  put_string("(?=help)>");
177}
178
179void shell_term::execute(char *st)
180{
181  put_string(st);
182  put_string(" : unhandled\n");
183}
184
185int shell_term::handle_event(event &ev, window_manager *wm)
186{
187  if (ev.window==con_win && con_win)
188  {
189    switch (ev.type)
190    {
191      case EV_KEY :
192      {
193        switch (ev.key)
194        {
195          case JK_BACKSPACE:
196          {
197            if (shcmd[0]!=0)
198            {
199              shcmd[strlen(shcmd)-1]=0;
200              put_char(ev.key);
201            }
202          } break;
203          case JK_ENTER :
204          {
205            put_char(ev.key);
206            execute(shcmd);
207            prompt();
208            shcmd[0]=0;
209          } break;
210          default :
211          {
212            if (ev.key<256 && isprint(ev.key))
213            {
214              int x=strlen(shcmd);
215              shcmd[x+1]=0;
216              shcmd[x]=ev.key;
217              put_char(ev.key);
218            }
219          } break;
220        } break;
221      }
222    }
223    return 1;
224  }
225  return 0;
226}
227
Note: See TracBrowser for help on using the repository browser.