libcaca  0.99.beta19
About: libcaca is a graphics library that outputs text instead of pixels, so that it can work on older video cards or text terminals (something like an advanced AAlib library).
  Fossies Dox: libcaca-0.99.beta19.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

caca_conio.c
Go to the documentation of this file.
1 /*
2  * libcaca Colour ASCII-Art library
3  * Copyright (c) 2002-2012 Sam Hocevar <sam@hocevar.net>
4  * All Rights Reserved
5  *
6  * This library is free software. It comes without any warranty, to
7  * the extent permitted by applicable law. You can redistribute it
8  * and/or modify it under the terms of the Do What the Fuck You Want
9  * to Public License, Version 2, as published by Sam Hocevar. See
10  * http://www.wtfpl.net/ for more details.
11  */
12 
13 /*
14  * This file contains a full conio.h reimplementation. More information
15  * on conio.h can be found on http://poli.cs.vsb.cz/c/help/conio.htm
16  */
17 
18 #include "config.h"
19 
20 #if !defined(__KERNEL__)
21 # include <stdio.h>
22 # include <stdlib.h>
23 #endif
24 
25 #include "caca.h"
26 #include "caca_internals.h"
27 #include "caca_conio.h"
28 
29 static caca_canvas_t *cv;
31 
32 static caca_timer_t refresh_timer = {0, 0};
33 static uint64_t refresh_ticks;
34 
35 static int unget_ch = -1;
36 static int kbhit_ch = -1;
37 static char pass_buffer[8 + 1];
38 
39 static void conio_init(void);
40 static void conio_refresh(void);
41 static void conio_fini(void);
42 
45 
47 char * caca_conio_cgets(char *str)
48 {
49  int len = ((uint8_t *)str)[0];
50  int pos = 0;
51 
52  conio_init();
53 
54  while (pos < len)
55  {
56  int ch = caca_conio_getch();
57  if (ch == '\n' || ch == '\r')
58  break;
59  str[2 + pos] = (char)(uint8_t)ch;
60  /* FIXME: handle scrolling */
63  pos++;
64  }
65 
66  str[2 + pos] = '\0';
67  str[1] = (char)(uint8_t)pos;
68 
69  conio_refresh();
70 
71  return str + 2;
72 }
73 
76 {
77  conio_init();
78 
79  /* FIXME: must work within the currently active text window */
82 
83  conio_refresh();
84 }
85 
88 {
89  conio_init();
90 
91  /* FIXME: must work within the currently active text window */
93  caca_gotoxy(cv, 0, 0);
94 
95  conio_refresh();
96 }
97 
99 int caca_conio_cprintf(const char *format, ...)
100 {
101  va_list args;
102  int ret;
103 
104  conio_init();
105 
106  /* FIXME: handle scrolling */
107  va_start(args, format);
108  ret = caca_vprintf(cv, caca_wherex(cv), caca_wherey(cv), format, args);
109  va_end(args);
110 
112 
113  conio_refresh();
114 
115  return ret;
116 }
117 
119 int caca_conio_cputs(const char *str)
120 {
121  int ch;
122 
123  conio_init();
124 
125  while ((ch = (uint8_t)*str++))
126  {
127  /* FIXME: handle windows, scrolling, '\n' and '\r' */
130  }
131 
132  conio_refresh();
133 
134  return ch;
135 }
136 
138 int caca_conio_cscanf(char *format, ...)
139 {
140  conio_init();
141 
142  /* TODO: implement this function */
143 
144  return 0;
145 }
146 
148 void caca_conio_delay(unsigned int milliseconds)
149 {
150  int64_t usec = (int64_t)milliseconds * 1000;
151  caca_timer_t timer = {0, 0};
152 
153  conio_init();
154 
156 
157  /* Refresh screen as long as we have enough time */
158  while(usec > 5000)
159  {
160  conio_refresh();
161  _caca_sleep(5000);
162  usec -= _caca_getticks(&timer);
163  }
164 
165  if(usec > 0)
166  _caca_sleep(usec);
167 
168  conio_refresh();
169 }
170 
173 {
174  conio_init();
175 
176  /* TODO: implement this function */
177 }
178 
181 {
182  caca_event_t ev;
183  int ret;
184 
185  conio_init();
186 
187  if(unget_ch >= 0)
188  {
189  int tmp = unget_ch;
190  unget_ch = -1;
191  return tmp;
192  }
193 
194  if(kbhit_ch >= 0)
195  {
196  int tmp = kbhit_ch;
197  kbhit_ch = -1;
198  return tmp;
199  }
200 
201  while(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 1000) == 0)
202  conio_refresh();
203 
204  ret = caca_get_event_key_ch(&ev);
205 
206  conio_refresh();
207 
208  return ret;
209 }
210 
213 {
214  /* conio_init() is called here. */
215  int tmp = caca_conio_getch();
216  /* conio_refresh() is called here. */
217  caca_conio_printf("%c", tmp);
218 
219  return tmp;
220 }
221 
223 char * caca_conio_getpass(const char *prompt)
224 {
225  int pos = 0;
226 
227  conio_init();
228 
229  while (pos < 8)
230  {
231  int ch = caca_conio_getch();
232  if (ch == '\n' || ch == '\r')
233  break;
234  pass_buffer[pos] = (char)(uint8_t)ch;
235  pos++;
236  }
237 
238  pass_buffer[pos] = '\0';
239 
240  conio_refresh();
241 
242  return pass_buffer;
243 }
244 
246 int caca_conio_gettext(int left, int top, int right, int bottom, void *destin)
247 {
248  conio_init();
249 
250  /* TODO: implement this function */
251 
252  return 0;
253 }
254 
257 {
258  conio_init();
259 
260  /* TODO: implement this function */
261 }
262 
264 void caca_conio_gotoxy(int x, int y)
265 {
266  conio_init();
267 
268  caca_gotoxy(cv, x - 1, y - 1);
269 
270  conio_refresh();
271 }
272 
275 {
276  conio_init();
277 
278  /* TODO: implement this function */
279 }
280 
283 {
284  conio_init();
285 
286  /* TODO: implement this function */
287 }
288 
291 {
292  static caca_timer_t timer = {0, 0};
293  static int last_failed = 0;
294  caca_event_t ev;
295 
296  conio_init();
297 
298  /* If last call failed and this call is made less than 100┬Ás
299  * afterwards, we assume the caller is in a busy loop and we
300  * delay it slightly to avoid resource leakage. */
301  if(last_failed && _caca_getticks(&timer) < 100)
302  {
303  _caca_sleep(1000);
304  conio_refresh();
305  }
306 
307  last_failed = 0;
308 
309  if(kbhit_ch >= 0)
310  return 1;
311 
313  {
315  return 1;
316  }
317 
318  last_failed = 1;
319 
320  return 0;
321 }
322 
325 {
326  conio_init();
327 
328  /* TODO: implement this function */
329 }
330 
332 int caca_conio_movetext(int left, int top, int right, int bottom,
333  int destleft, int desttop)
334 {
335  caca_canvas_t *tmp;
336 
337  conio_init();
338 
339  if (left < 1 || top < 1 || left > right || top > bottom
340  || destleft < 1 || desttop < 1 || destleft > right
341  || desttop > bottom || right > caca_get_canvas_width(cv)
342  || bottom > caca_get_canvas_width(cv))
343  return 0;
344 
345  tmp = caca_create_canvas(right - left + 1, bottom - top + 1);
346  caca_blit(tmp, 1 - left, 1 - top, cv, NULL);
347  caca_blit(cv, destleft - 1, desttop - 1, tmp, NULL);
348 
349  conio_refresh();
350 
351  return 1;
352 }
353 
356 {
357  conio_init();
358 
359  /* TODO: implement this function */
360 }
361 
364 {
365  conio_init();
366 
367  /* TODO: implement this function */
368 }
369 
371 int caca_conio_printf(const char *format, ...)
372 {
373  va_list args;
374  int ret;
375 
376  conio_init();
377 
378  va_start(args, format);
379  ret = caca_vprintf(cv, caca_wherex(cv), caca_wherey(cv), format, args);
380  va_end(args);
381 
383 
384  conio_refresh();
385 
386  return 0;
387 }
388 
390 int caca_conio_putch(int ch)
391 {
392  conio_init();
393 
394  /* FIXME: handle scrolling, windows */
397 
398  return ch;
399 }
400 
402 int caca_conio_puttext(int left, int top, int right, int bottom, void *destin)
403 {
404  conio_init();
405 
406  /* TODO: implement this function */
407 
408  return 0;
409 }
410 
413 {
414  conio_init();
415 
416  switch(cur_t)
417  {
419  caca_set_cursor(dp, 0);
420  break;
423  caca_set_cursor(dp, 1);
424  break;
425  }
426 
427  conio_refresh();
428 }
429 
431 void caca_conio_sleep(unsigned int seconds)
432 {
433  int64_t usec = (int64_t)seconds * 1000000;
434  caca_timer_t timer = {0, 0};
435 
436  conio_init();
437 
439 
440  /* Refresh screen as long as we have enough time */
441  while(usec > 5000)
442  {
443  conio_refresh();
444  _caca_sleep(5000);
445  usec -= _caca_getticks(&timer);
446  }
447 
448  if(usec > 0)
449  _caca_sleep(usec);
450 
451  conio_refresh();
452 }
453 
455 void caca_conio_sound(unsigned int frequency)
456 {
457  conio_init();
458 
459  /* TODO: implement this function */
460 }
461 
463 void caca_conio_textattr(int newattr)
464 {
465  conio_init();
466 
467  /* TODO: implement this function */
468 }
469 
471 void caca_conio_textbackground(int newcolor)
472 {
473  conio_init();
474 
476  newcolor);
477 }
478 
480 void caca_conio_textcolor(int newcolor)
481 {
482  conio_init();
483 
484  caca_set_color_ansi(cv, newcolor,
486 }
487 
489 void caca_conio_textmode(int newmode)
490 {
491  conio_init();
492 
493  /* TODO: implement this function */
494 }
495 
498 {
499  conio_init();
500 
501  if(unget_ch >= 0)
502  return EOF;
503 
504  unget_ch = ch;
505 
506  return ch;
507 }
508 
511 {
512  conio_init();
513 
514  return caca_wherex(cv) + 1;
515 }
516 
519 {
520  conio_init();
521 
522  return caca_wherey(cv) + 1;
523 }
524 
526 void caca_conio_window(int left, int top, int right, int bottom)
527 {
528  conio_init();
529 
530  /* TODO: implement this function */
531 }
532 
533 /* XXX: the following functions are local. */
534 
535 static void conio_init(void)
536 {
537  if(!cv)
538  cv = caca_create_canvas(80, 25);
539  if(!dp)
540  {
543  caca_set_cursor(dp, 1);
545  refresh_ticks = 0;
546 #if defined HAVE_ATEXIT
547  atexit(conio_fini);
548 #endif
549  }
550 }
551 
552 static void conio_refresh(void)
553 {
555  if(refresh_ticks > 10000)
556  {
559  refresh_ticks = 0;
560  }
561 }
562 
563 static void conio_fini(void)
564 {
566  dp = NULL;
568  cv = NULL;
569 }
570 
caca_conio_getpass
char * caca_conio_getpass(const char *prompt)
DOS conio.h getpass() equivalent.
Definition: caca_conio.c:223
caca_wherey
int caca_wherey(caca_canvas_t const *)
Get Y cursor position.
Definition: string.c:89
caca_wherex
int caca_wherex(caca_canvas_t const *)
Get X cursor position.
Definition: string.c:75
refresh_timer
static caca_timer_t refresh_timer
Definition: caca_conio.c:32
caca_conio_cprintf
int caca_conio_cprintf(const char *format,...)
DOS conio.h cprintf() equivalent.
Definition: caca_conio.c:99
caca_blit
int caca_blit(caca_canvas_t *, int, int, caca_canvas_t const *, caca_canvas_t const *)
Blit a canvas onto another one.
Definition: string.c:455
caca_create_display
caca_display_t * caca_create_display(caca_canvas_t *cv)
Attach a caca graphical context to a caca canvas.
Definition: caca.c:68
caca_conio_gotoxy
void caca_conio_gotoxy(int x, int y)
DOS conio.h gotoxy() equivalent.
Definition: caca_conio.c:264
y
static int y
Definition: cacadraw.c:27
dp
static caca_display_t * dp
Definition: caca_conio.c:30
caca_conio_highvideo
void caca_conio_highvideo(void)
DOS conio.h highvideo() equivalent.
Definition: caca_conio.c:274
unget_ch
static int unget_ch
Definition: caca_conio.c:35
cv
static caca_canvas_t * cv
Definition: caca_conio.c:29
caca_conio_textcolor
void caca_conio_textcolor(int newcolor)
DOS conio.h textcolor() equivalent.
Definition: caca_conio.c:480
caca_conio_cputs
int caca_conio_cputs(const char *str)
DOS conio.h cputs() equivalent.
Definition: caca_conio.c:119
caca_conio_gettextinfo
void caca_conio_gettextinfo(struct caca_conio_text_info *r)
DOS conio.h gettextinfo() equivalent.
Definition: caca_conio.c:256
caca_event
Handling of user events.
Definition: caca.h:129
caca_conio_lowvideo
void caca_conio_lowvideo(void)
DOS conio.h lowvideo() equivalent.
Definition: caca_conio.c:324
caca_create_canvas
caca_canvas_t * caca_create_canvas(int, int)
Initialise a libcaca canvas.
Definition: canvas.c:54
caca_display
Definition: caca_internals.h:167
caca_free_canvas
int caca_free_canvas(caca_canvas_t *)
Free a libcaca canvas.
Definition: canvas.c:308
caca_conio_textbackground
void caca_conio_textbackground(int newcolor)
DOS conio.h textbackground() equivalent.
Definition: caca_conio.c:471
caca_timer
Definition: caca_internals.h:140
caca_conio_text_info
DOS text area information.
Definition: caca.h:608
caca_conio_clrscr
void caca_conio_clrscr(void)
DOS conio.h clrscr() equivalent.
Definition: caca_conio.c:87
caca_conio_sleep
void caca_conio_sleep(unsigned int seconds)
DOS dos.h sleep() equivalent.
Definition: caca_conio.c:431
caca_conio_kbhit
int caca_conio_kbhit(void)
DOS conio.h kbhit() equivalent.
Definition: caca_conio.c:290
caca_conio_insline
void caca_conio_insline(void)
DOS conio.h insline() equivalent.
Definition: caca_conio.c:282
caca_conio_textattr
void caca_conio_textattr(int newattr)
DOS conio.h textattr() equivalent.
Definition: caca_conio.c:463
caca_get_canvas_width
int caca_get_canvas_width(caca_canvas_t const *)
Get the canvas width.
Definition: canvas.c:239
caca_conio_clreol
void caca_conio_clreol(void)
DOS conio.h clreol() equivalent.
Definition: caca_conio.c:75
caca_conio_movetext
int caca_conio_movetext(int left, int top, int right, int bottom, int destleft, int desttop)
DOS conio.h movetext() equivalent.
Definition: caca_conio.c:332
caca_conio_window
void caca_conio_window(int left, int top, int right, int bottom)
DOS conio.h window() equivalent.
Definition: caca_conio.c:526
caca_conio_nosound
void caca_conio_nosound(void)
DOS dos.h nosound() equivalent.
Definition: caca_conio.c:363
caca_conio__setcursortype
void caca_conio__setcursortype(int cur_t)
DOS conio.h _setcursortype() equivalent.
Definition: caca_conio.c:412
caca_set_cursor
int caca_set_cursor(caca_display_t *, int)
Show or hide the cursor.
Definition: graphics.c:218
caca_conio_ungetch
int caca_conio_ungetch(int ch)
DOS conio.h ungetch() equivalent.
Definition: caca_conio.c:497
conio_fini
static void conio_fini(void)
Definition: caca_conio.c:563
pass_buffer
static char pass_buffer[8+1]
Definition: caca_conio.c:37
caca_conio_cscanf
int caca_conio_cscanf(char *format,...)
DOS stdio.h cscanf() equivalent.
Definition: caca_conio.c:138
caca_conio_normvideo
void caca_conio_normvideo(void)
DOS conio.h normvideo() equivalent.
Definition: caca_conio.c:355
caca_get_attr
uint32_t caca_get_attr(caca_canvas_t const *cv, int x, int y)
Get the text attribute at the given coordinates.
Definition: attr.c:66
caca_conio_cgets
char * caca_conio_cgets(char *str)
DOS conio.h cgets() equivalent.
Definition: caca_conio.c:47
caca_conio_wherex
int caca_conio_wherex(void)
DOS conio.h wherex() equivalent.
Definition: caca_conio.c:510
caca_refresh_display
int caca_refresh_display(caca_display_t *)
Flush pending changes and redraw the screen.
Definition: graphics.c:146
caca_gotoxy
int caca_gotoxy(caca_canvas_t *, int, int)
Set cursor position.
Definition: string.c:58
_caca_getticks
int _caca_getticks(caca_timer_t *)
Definition: time.c:47
caca_attr_to_ansi_bg
uint8_t caca_attr_to_ansi_bg(uint32_t attr)
Get ANSI background information from attribute.
Definition: attr.c:347
caca_conio_delline
void caca_conio_delline(void)
DOS conio.h delline() equivalent.
Definition: caca_conio.c:172
caca_put_char
int caca_put_char(caca_canvas_t *, int, int, uint32_t)
Print an ASCII or Unicode character.
Definition: string.c:120
caca_conio_delay
void caca_conio_delay(unsigned int milliseconds)
DOS dos.h delay() equivalent.
Definition: caca_conio.c:148
caca_free_display
int caca_free_display(caca_display_t *dp)
Detach a caca graphical context from a caca backend context.
Definition: caca.c:238
caca_internals.h
caca_conio_getch
int caca_conio_getch(void)
DOS conio.h getch() equivalent.
Definition: caca_conio.c:180
caca_conio_puttext
int caca_conio_puttext(int left, int top, int right, int bottom, void *destin)
DOS conio.h puttext() equivalent.
Definition: caca_conio.c:402
CACA_CONIO__NOCURSOR
Definition: caca.h:582
caca_vprintf
int caca_vprintf(caca_canvas_t *, int, int, char const *, va_list)
Print a formated string (va_list version).
Definition: string.c:335
conio_refresh
static void conio_refresh(void)
Definition: caca_conio.c:552
CACA_CONIO__NORMALCURSOR
Definition: caca.h:584
caca_conio_sound
void caca_conio_sound(unsigned int frequency)
DOS dos.h sound() equivalent.
Definition: caca_conio.c:455
caca_conio_gettext
int caca_conio_gettext(int left, int top, int right, int bottom, void *destin)
DOS conio.h gettext() equivalent.
Definition: caca_conio.c:246
caca_set_color_ansi
int caca_set_color_ansi(caca_canvas_t *cv, uint8_t fg, uint8_t bg)
Set the default colour pair for text (ANSI version).
Definition: attr.c:234
caca.h
The libcaca public header.
caca_conio_directvideo
int caca_conio_directvideo
DOS direct video control.
Definition: caca_conio.c:43
timer
static caca_timer_t timer
Generate a random integer within a range.
Definition: canvas.c:344
config.h
caca_conio_textmode
void caca_conio_textmode(int newmode)
DOS conio.h textmode() equivalent.
Definition: caca_conio.c:489
caca_get_event
#define caca_get_event(x)
Definition: caca0.h:136
caca_conio_putch
int caca_conio_putch(int ch)
DOS conio.h putch() equivalent.
Definition: caca_conio.c:390
kbhit_ch
static int kbhit_ch
Definition: caca_conio.c:36
caca_clear_canvas
int caca_clear_canvas(caca_canvas_t *)
Clear the canvas.
Definition: string.c:374
caca_conio__wscroll
int caca_conio__wscroll
DOS scrolling control.
Definition: caca_conio.c:44
CACA_EVENT_KEY_PRESS
Definition: caca.h:111
caca_attr_to_ansi_fg
uint8_t caca_attr_to_ansi_fg(uint32_t attr)
Get ANSI foreground information from attribute.
Definition: attr.c:327
caca_conio_wherey
int caca_conio_wherey(void)
DOS conio.h wherey() equivalent.
Definition: caca_conio.c:518
caca_conio_printf
int caca_conio_printf(const char *format,...)
DOS stdio.h printf() equivalent.
Definition: caca_conio.c:371
caca_conio.h
conio_init
static void conio_init(void)
Definition: caca_conio.c:535
caca_get_event_key_ch
int caca_get_event_key_ch(caca_event_t const *)
Return a key press or key release event's value.
Definition: event.c:219
CACA_CONIO__SOLIDCURSOR
Definition: caca.h:583
caca_fill_box
#define caca_fill_box(x, y, z, t, c)
Definition: caca0.h:171
_caca_sleep
void _caca_sleep(int)
Definition: time.c:36
refresh_ticks
static uint64_t refresh_ticks
Definition: caca_conio.c:33
caca_canvas
Definition: caca_internals.h:47
caca_conio_getche
int caca_conio_getche(void)
DOS conio.h getche() equivalent.
Definition: caca_conio.c:212
x
static int x
Definition: cacadraw.c:27