HelenOS sources
This source file includes following definitions.
- putstr
- start_standout
- resume_normal
- clear_screen
- scr_clear
- scr_init
- moveto
- get_display_size
- get_display_color_sup
- scr_set
- scr_end
- stop
- scr_update
- scr_msg
- tsleep
- tgetchar
- twait
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <str.h>
#include <vfs/vfs.h>
#include <async.h>
#include <stdbool.h>
#include <io/console.h>
#include <io/style.h>
#include "screen.h"
#include "tetris.h"
#define STOP (B_COLS - 3)
static cell curscreen[B_SIZE];
static int curscore;
static int isset;
static bool use_rgb;
static bool use_color;
static const struct shape *lastshape;
static usec_t timeleft = 0;
bool size_changed;
console_ctrl_t *console;
static inline void putstr(const char *s)
{
while (*s)
putchar(*(s++));
}
static void start_standout(uint32_t color)
{
uint8_t bg;
uint8_t attr;
console_flush(console);
if (use_rgb) {
console_set_rgb_color(console, color, 0xffffff);
} else if (use_color) {
bg = 0x00;
attr = 0;
if ((color & 0xff0000) != 0)
bg |= 0x4;
if ((color & 0x00ff00) != 0)
bg |= 0x2;
if ((color & 0x0000ff) != 0)
bg |= 0x1;
console_set_color(console, bg, 0x00, attr);
}
}
static void resume_normal(void)
{
console_flush(console);
console_set_style(console, STYLE_NORMAL);
}
void clear_screen(void)
{
console_clear(console);
moveto(0, 0);
}
void scr_clear(void)
{
resume_normal();
console_clear(console);
curscore = -1;
memset(curscreen, 0, sizeof(curscreen));
}
void scr_init(void)
{
console_cursor_visibility(console, 0);
resume_normal();
scr_set();
}
void moveto(sysarg_t r, sysarg_t c)
{
console_flush(console);
console_set_pos(console, c, r);
}
winsize_t winsize;
static errno_t get_display_size(winsize_t *ws)
{
return console_get_size(console, &ws->ws_col, &ws->ws_row);
}
static void get_display_color_sup(bool *rgb, bool *color)
{
sysarg_t ccap;
errno_t rc = console_get_color_cap(console, &ccap);
if (rc != EOK) {
*rgb = false;
*color = false;
return;
}
if ((ccap & CONSOLE_CAP_CURSORCTL) == 0) {
stop("Your screen does not support cursor control.\n");
return;
}
*rgb = ((ccap & CONSOLE_CAP_RGB) == CONSOLE_CAP_RGB);
*color = ((ccap & CONSOLE_CAP_INDEXED) == CONSOLE_CAP_INDEXED);
}
void scr_set(void)
{
winsize_t ws;
Rows = 0;
Cols = 0;
if (get_display_size(&ws) == 0) {
Rows = ws.ws_row;
Cols = ws.ws_col;
}
get_display_color_sup(&use_rgb, &use_color);
if ((Rows < MINROWS) || (Cols < MINCOLS)) {
char smallscr[55];
snprintf(smallscr, sizeof(smallscr),
"the screen is too small (must be at least %dx%d)\n",
MINROWS, MINCOLS);
stop(smallscr);
}
isset = 1;
scr_clear();
}
void scr_end(void)
{
console_cursor_visibility(console, 1);
}
void stop(const char *why)
{
if (isset)
scr_end();
fprintf(stderr, "aborting: %s", why);
exit(1);
}
void scr_update(void)
{
cell *bp;
cell *sp;
cell so;
cell cur_so = 0;
int i;
int j;
int ccol;
curscreen[D_LAST * B_COLS - 1] = -1;
if (score != curscore) {
moveto(0, 0);
printf("Score: %d", score);
curscore = score;
}
if ((showpreview) && (nextshape != lastshape)) {
int i;
static int r = 5, c = 2;
int tr, tc, t;
lastshape = nextshape;
resume_normal();
moveto(r - 1, c - 1);
putstr(" ");
moveto(r, c - 1);
putstr(" ");
moveto(r + 1, c - 1);
putstr(" ");
moveto(r + 2, c - 1);
putstr(" ");
moveto(r - 3, c - 2);
putstr("Next shape:");
start_standout(nextshape->color);
moveto(r, 2 * c);
putstr(" ");
for (i = 0; i < 3; i++) {
t = c + r * B_COLS;
t += nextshape->off[i];
tr = t / B_COLS;
tc = t % B_COLS;
moveto(tr, 2 * tc);
putstr(" ");
}
resume_normal();
}
bp = &board[D_FIRST * B_COLS];
sp = &curscreen[D_FIRST * B_COLS];
for (j = D_FIRST; j < D_LAST; j++) {
ccol = -1;
for (i = 0; i < B_COLS; bp++, sp++, i++) {
if (*sp == (so = *bp))
continue;
*sp = so;
if (i != ccol) {
if (cur_so) {
resume_normal();
cur_so = 0;
}
moveto(RTOD(j), CTOD(i));
}
if (so != cur_so) {
if (so)
start_standout(so);
else
resume_normal();
cur_so = so;
}
putstr(" ");
ccol = i + 1;
if ((i > STOP) || (sp[1] != bp[1]) || (so != bp[1]))
continue;
if (sp[2] != bp[2])
sp[1] = -1;
else if ((i < STOP) && (so == bp[2]) && (sp[3] != bp[3])) {
sp[2] = -1;
sp[1] = -1;
}
}
}
if (cur_so)
resume_normal();
console_flush(console);
}
void scr_msg(char *s, bool set)
{
int l = str_size(s);
moveto(Rows - 2, ((Cols - l) >> 1) - 1);
if (set)
putstr(s);
else
while (--l >= 0)
(void) putchar(' ');
}
void tsleep(void)
{
usec_t timeout = fallrate;
errno_t rc;
while (timeout > 0) {
cons_event_t event;
rc = console_get_event_timeout(console, &event, &timeout);
if (rc == ETIMEOUT)
break;
if (rc != EOK)
exit(1);
}
}
int tgetchar(void)
{
errno_t rc;
if (timeleft <= 0) {
faster();
timeleft = fallrate;
}
char32_t c = 0;
while (c == 0) {
cons_event_t event;
rc = console_get_event_timeout(console, &event, &timeleft);
if (rc == ETIMEOUT) {
timeleft = 0;
return -1;
}
if (rc != EOK)
exit(1);
if (event.type == CEV_RESIZE)
size_changed = true;
if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS)
c = event.ev.key.c;
}
return (int) c;
}
int twait(void)
{
char32_t c = 0;
errno_t rc;
while (c == 0) {
cons_event_t event;
rc = console_get_event(console, &event);
if (rc == ETIMEOUT)
return -1;
if (rc != EOK)
exit(1);
if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS)
c = event.ev.key.c;
}
return (int) c;
}
HelenOS homepage, sources at GitHub