1 /* CONSOLE INTERFACE */
11 #include <sys/ioctl.h>
21 #define SCREEN_BUFFERING
24 /* ALL COORDINATES START FROM 0 */
27 struct termios term_setting
;
31 #ifdef SCREEN_BUFFERING
33 int screen_buffer_pos
=0;
34 int screen_buffer_size
;
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
;
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
);
83 void my_print(char *str
)
86 #ifdef SCREEN_BUFFERING
96 #ifdef SCREEN_BUFFERING
98 ret
= write(1,screen_buffer
,screen_buffer_pos
);
106 /* initialize console */
107 void c_init(int w
,int h
)
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);}
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
);
124 t
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
125 t
.c_cflag
&= ~(CSIZE
|PARENB
);
128 tcsetattr(0, TCSANOW
, &t
);
138 void c_shutdown(void)
141 tcsetattr(0,TCSANOW
,&term_setting
);
147 #ifdef SCREEN_BUFFERING
148 mem_free(screen_buffer
);
154 /* move cursor to [x,y] */
155 void c_goto(int x
,int y
)
160 if (TRI_D_ON
&&tri_d
)x
+=screen_width
;
162 snprintf(txt
,16,"\033[%d;%dH",y
+1,x
+1);
167 /* set complete foreground color */
168 void c_setcolor(unsigned char a
)
172 snprintf(txt
,16,"\033[%c;%dm",(a
>>3)+'0',30+(a
&7));
177 /* set complete foreground color and background color */
178 void c_setcolor_bg(unsigned char fg
,unsigned char bg
)
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));
188 /* set background color */
189 void c_setbgcolor(unsigned char a
)
192 snprintf(txt
,16,"\033[%dm",40+(a
&7));
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
)
209 snprintf(txt
,16,"\033[%d;%dm",hlt
&1,40+(bg
&7));
214 /* set 3 bit foreground color */
215 void c_setcolor_3b(unsigned char a
)
218 snprintf(txt
,sizeof(txt
),"\033[%dm",30+(a
&7));
223 /* set 3 bit foreground color and background color*/
224 void c_setcolor_3b_bg(unsigned char fg
,unsigned char bg
)
228 snprintf(txt
,16,"\033[%d;%dm",30+(fg
&7),40+(bg
&7));
233 /* print on the cursor position */
234 void c_print(char * text
)
239 void c_print_l(char * text
, int len
)
241 my_print_l(text
, len
);
245 /* print char on the cursor position */
252 /* clear the screen */
259 /* clear rectangle on the screen */
260 /* presumtions: x2>=x1 && y2>=y1 */
261 void c_clear(int x1
,int y1
,int x2
,int y2
)
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
++)
273 c_goto(x1
,y
);c_print(line
);
279 void c_update_kbd(void)
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)
303 /* set cursor shape */
304 void c_cursor(int type
)
309 my_print("\033[?25h");
313 my_print("\033[?25l");
326 /* get screen dimensions */
327 void c_get_size(int *x
, int *y
)
331 if (ioctl(1, TIOCGWINSZ
, &ws
) != -1)
356 #include "winconsole.c"