1/* A conio.h like implementation for VTANSI displays.
2 *
3 * Copyright (c) 2009 Joachim Nilsson <troglobit@gmail.com>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#ifndef _TETRIS_IO_H_
19#define _TETRIS_IO_H_
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <assert.h>
24
25#include <refos/refos.h>
26#include <refos-io/stdio.h>
27#include <refos-util/init.h>
28
29/* Board dimensions. */
30#define B_COLS 12
31#define B_ROWS 23
32#define B_SIZE (B_COLS * B_ROWS)
33
34/* Attributes */
35#define RESETATTR    0
36#define BRIGHT       1
37#define DIM          2
38#define UNDERSCORE   4
39#define BLINK        5           /* May not work on all displays. */
40#define REVERSE      7
41#define HIDDEN       8
42
43/* Colours for text and background. */
44#define A_FG_K       30
45#define A_FG_R       31
46#define A_FG_G       32
47#define A_FG_Y       33
48#define A_FG_B       34
49#define A_FG_M       35
50#define A_FG_C       36
51#define A_FG_W       37
52#define A_FG_RESET   39
53
54#define A_BG_K       40
55#define A_BG_R       41
56#define A_BG_G       42
57#define A_BG_Y       43
58#define A_BG_B       44
59#define A_BG_M       45
60#define A_BG_C       46
61#define A_BG_W       47
62#define A_BG_RESET   49
63
64/*! @brief Esc[2JEsc[1;1H - Clear screen and move cursor to 1,1 (upper left) pos. */
65#define clrscr()              puts ("\e[2J\e[1;1H")
66/*! @brief Esc[K - Erases from the current cursor position to the end of the current line. */
67#define clreol()              puts ("\e[K")
68/*! @brief Esc[2K - Erases the entire current line. */
69#define delline()             puts ("\e[2K")
70/*! @brief Esc[Line;ColumnH - Moves the cursor to the specified position (coordinates) */
71#define gotoxy(x,y)           printf("\e[%d;%dH", y, x); fflush(stdout)
72/*! @brief Esc[?25l (lower case L) - Hide Cursor */
73#define hidecursor()          puts ("\e[?25l")
74/*! @brief Esc[?25h (lower case H) - Show Cursor */
75#define showcursor()          puts ("\e[?25h")
76/*! @brief Print a solid block. */
77#define printblock(x)         printf ("\e[%dm  ", x); fflush(stdout)
78
79/*! @brief Esc[Value;...;Valuem - Set Graphics Mode */
80#define __set_gm(attr,color,val)                                                \
81        if (!color) {                                                           \
82                printf("\e[%dm", attr);                                         \
83        } else {                                                                \
84                printf("\e[%d;%dm", color & 0x10 ? 1 : 0, (color & 0xF) + val); \
85        }                                                                       \
86        fflush(stdout)
87
88#define textattr(attr)        __set_gm(attr, 0, 0)
89#define textcolor(color)      __set_gm(RESETATTR, color, 30)
90#define textbackground(color) __set_gm(RESETATTR, color, 40)
91
92static inline int
93io_nonblock_getkey(void)
94{
95    return refos_async_getc();
96}
97
98#endif /* _TETRIS_IO_H_ */
99