make the bot print its name
[0verkill.git] / console.c
blobb4cef7f3e7f0ed717e5ab03c1275982857f50889
1 /* CONSOLE INTERFACE */
3 #ifndef WIN32
5 #include <ctype.h>
6 #include <termios.h>
7 #include <sys/time.h>
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <stdlib.h>
11 #include <sys/ioctl.h>
12 #include <unistd.h>
13 #include <string.h>
15 #include "config.h"
16 #include "cfg.h"
17 #include "console.h"
18 #include "kbd.h"
19 #include "error.h"
21 #define SCREEN_BUFFERING
24 /* ALL COORDINATES START FROM 0 */
26 int console_ok=1;
27 struct termios term_setting;
28 int screen_width;
31 #ifdef SCREEN_BUFFERING
32 char *screen_buffer;
33 int screen_buffer_pos=0;
34 int screen_buffer_size;
35 #endif
37 #ifdef HAVE_INLINE
38 inline void
39 #else
40 void
41 #endif
42 my_putc(char c)
45 #ifdef SCREEN_BUFFERING
46 if (screen_buffer_pos+1>screen_buffer_size)
48 screen_buffer_size<<=1;
49 screen_buffer=mem_realloc(screen_buffer,screen_buffer_size);
50 if (!screen_buffer){c_shutdown();fprintf(stderr,"Error: Can't reallocate screen buffer.\n");EXIT(1);}
52 screen_buffer[screen_buffer_pos]=c;
53 screen_buffer_pos++;
54 #else
55 fputc(c,stdout);
56 #endif
60 #ifdef HAVE_INLINE
61 inline
62 #endif
63 void my_print_l(char *str, int l)
65 #ifdef SCREEN_BUFFERING
66 if (screen_buffer_pos+l>screen_buffer_size)
68 while(screen_buffer_pos+l>screen_buffer_size)
69 screen_buffer_size<<=1;
70 screen_buffer=mem_realloc(screen_buffer,screen_buffer_size);
71 if (!screen_buffer){c_shutdown();fprintf(stderr,"Error: Can't reallocate screen buffer.\n");EXIT(1);}
73 memcpy(screen_buffer+screen_buffer_pos,str,l);
74 screen_buffer_pos+=l;
75 #else
76 fputs(str,stdout);
77 #endif
80 #ifdef HAVE_INLINE
81 inline
82 #endif
83 void my_print(char *str)
85 my_print_l(str,
86 #ifdef SCREEN_BUFFERING
87 strlen(str)
88 #else
90 #endif
94 void c_refresh(void)
96 #ifdef SCREEN_BUFFERING
97 ssize_t ret = 0;
98 ret = write(1,screen_buffer,screen_buffer_pos);
99 screen_buffer_pos=0;
100 #else
101 fflush(stdout);
102 #endif
106 /* initialize console */
107 void c_init(int w,int h)
109 struct termios t;
111 console_ok=0;
113 #ifdef SCREEN_BUFFERING
114 screen_buffer_size=w*h;
115 if (!(screen_buffer=mem_alloc(screen_buffer_size))){fprintf(stderr,"Error: Not enough memory for screen buffer.\n");EXIT(1);}
116 #else
117 w=w;h=h;
118 #endif
120 tcgetattr(0,&term_setting);
121 memcpy(&t, &term_setting, sizeof(struct termios));
122 t.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
123 t.c_oflag &= ~OPOST;
124 t.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
125 t.c_cflag &= ~(CSIZE|PARENB);
126 t.c_cflag |= CS8;
128 tcsetattr(0, TCSANOW, &t);
129 kbd_init();
131 c_cls();
132 my_print("\033[;H");
133 c_refresh();
137 /* close console */
138 void c_shutdown(void)
140 kbd_close();
141 tcsetattr(0,TCSANOW,&term_setting);
142 c_cursor(C_NORMAL);
143 c_setcolor(7);
144 c_cls();
145 my_print("\033[;H");
146 c_refresh();
147 #ifdef SCREEN_BUFFERING
148 mem_free(screen_buffer);
149 #endif
150 console_ok=1;
154 /* move cursor to [x,y] */
155 void c_goto(int x,int y)
157 char txt[16];
159 #ifdef TRI_D
160 if (TRI_D_ON&&tri_d)x+=screen_width;
161 #endif
162 snprintf(txt,16,"\033[%d;%dH",y+1,x+1);
163 my_print(txt);
167 /* set complete foreground color */
168 void c_setcolor(unsigned char a)
170 char txt[16];
171 a&=15;
172 snprintf(txt,16,"\033[%c;%dm",(a>>3)+'0',30+(a&7));
173 my_print(txt);
177 /* set complete foreground color and background color */
178 void c_setcolor_bg(unsigned char fg,unsigned char bg)
180 char txt[16];
181 fg&=15;
182 if (!(fg>>3)&&!bg) snprintf(txt,16,"\033[%c;%dm",(fg>>3)+'0',30+(fg&7));
183 else snprintf(txt,16,"\033[%c;%d;%dm",(fg>>3)+'0',40+(bg&7),30+(fg&7));
184 my_print(txt);
188 /* set background color */
189 void c_setbgcolor(unsigned char a)
191 char txt[16];
192 snprintf(txt,16,"\033[%dm",40+(a&7));
193 my_print(txt);
197 /* set highlight color */
198 void c_sethlt(unsigned char a)
200 my_print(a?"\033[1m":"\033[0m");
204 /* set highlight and background color */
205 void c_sethlt_bg(unsigned char hlt,unsigned char bg)
207 char txt[16];
209 snprintf(txt,16,"\033[%d;%dm",hlt&1,40+(bg&7));
210 my_print(txt);
214 /* set 3 bit foreground color */
215 void c_setcolor_3b(unsigned char a)
217 char txt[8];
218 snprintf(txt,sizeof(txt),"\033[%dm",30+(a&7));
219 my_print(txt);
223 /* set 3 bit foreground color and background color*/
224 void c_setcolor_3b_bg(unsigned char fg,unsigned char bg)
226 char txt[16];
228 snprintf(txt,16,"\033[%d;%dm",30+(fg&7),40+(bg&7));
229 my_print(txt);
233 /* print on the cursor position */
234 void c_print(char * text)
236 my_print(text);
239 void c_print_l(char * text, int len)
241 my_print_l(text, len);
245 /* print char on the cursor position */
246 void c_putc(char c)
248 my_putc(c);
252 /* clear the screen */
253 void c_cls(void)
255 my_print("\033[2J");
259 /* clear rectangle on the screen */
260 /* presumtions: x2>=x1 && y2>=y1 */
261 void c_clear(int x1,int y1,int x2,int y2)
263 char *line;
264 int y;
266 line=mem_alloc(x2-x1+1);
267 if (!line){fprintf(stderr,"Error: Not enough memory.\n");EXIT(1);}
268 for (y=0;y<x2-x1+1;y++)
269 line[y]=' ';
270 line[y]=0;
271 for(y=y1;y<=y2;y++)
273 c_goto(x1,y);c_print(line);
275 mem_free(line);
279 void c_update_kbd(void)
281 kbd_update();
285 int c_pressed(int k)
287 return kbd_is_pressed(k);
291 int c_was_pressed(int k)
293 return kbd_was_pressed(k);
297 void c_wait_for_key(void)
299 kbd_wait_for_key();
303 /* set cursor shape */
304 void c_cursor(int type)
306 switch (type)
308 case C_NORMAL:
309 my_print("\033[?25h");
310 break;
312 case C_HIDE:
313 my_print("\033[?25l");
314 break;
319 /* ring the bell */
320 void c_bell(void)
322 my_print("\007");
326 /* get screen dimensions */
327 void c_get_size(int *x, int *y)
329 #ifndef __EMX__
330 struct winsize ws;
331 if (ioctl(1, TIOCGWINSZ, &ws) != -1)
333 *x=ws.ws_col;
334 *y=ws.ws_row;
336 else
338 *x=80;
339 *y=24;
341 #ifdef TRI_D
342 if (TRI_D_ON)
343 (*x)>>=1;
344 screen_width=*x;
345 #endif
346 return;
347 #else
348 int s[2];
349 _scrsize(s);
350 *x = s[0];
351 *y = s[1];
352 #endif
355 #else
356 #include "winconsole.c"
357 #endif