1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/**
* @namespace biewlib
* @file biewlib/sysdep/generic/linux/vio.c
* @brief general implementation of video i/o functions for linux.
* @version -
* @remark this source file is part of Binary vIEW project (BIEW).
* The Binary vIEW (BIEW) is copyright (C) 1995 Nick Kurshev.
* All rights reserved. This software is redistributable under the
* licence given in the file "Licence.en" ("Licence.ru" in russian
* translation) distributed in the BIEW archive.
* @note Requires POSIX compatible development system
*
* @author Konstantin Boldyshev
* @since 1999
* @note Development, fixes and improvements
**/
/* $Id: vio.c,v 1.2 2000/10/29 09:15:14 konst Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/vt.h>
#include "biewlib/biewlib.h"
#include "console.h"
#define VIO_FILE "/dev/vcsa0"
#define VMAX_X __TVIO_MAXSCREENWIDTH
#define VMAX_Y 0x400
#define VTMP_LEN 0x80
tAbsCoord tvioWidth, tvioHeight;
unsigned tvioNumColors = 16;
extern unsigned char alt2koi[];
int console_flags = 0, on_console = 0;
static int __ansi_color[8] = {0,4,2,6,1,5,3,7};
static tAbsCoord saveX, saveY, firstX, firstY;
static int cursor_type = __TVIO_CUR_NORM;
static int viohandle, out_fd, initialized, output_7 = 0;
static unsigned violen;
static unsigned char *viomem;
static char *vtmp;
#define _addr(x, y) (viomem + (x) + (y) * tvioWidth)
#define twrite(x) write(out_fd, (x), strlen(x))
#define _bg(x) ((x) >> 4)
#define _fg(x) ((x) & 0x0f)
#define _2color(x) (char)__ansi_color[((x) & 7)]
struct {
unsigned char last;
unsigned char color[0x10];
} tp = { 0 };
static char *__FASTCALL__ _2ansi(unsigned char attr)
{
int bg = _bg(attr);
int bc = _2color(bg);
if (transparent) {
int i;
for (i = 0; i < tp.last; i++)
if (bg == tp.color[i]) bc = 9;
}
sprintf(vtmp,
"\033[%d;3%d;4%d%sm",
_fg(attr) > 7,
_2color(_fg(attr) & 7),
bc,
bg > 7 ? ";5" : ""
);
return vtmp;
}
inline static int __FASTCALL__ printable(unsigned char c)
{
int result = !(c < 0x20 || c == 0x7f || c == 0x9b); /* 0x80< c < 0xA0 */
if (result && terminal == TERM_XTERM)
result = !(
c == 0x84 || c == 0x85 || c == 0x88 ||
(c >= 0x8D && c <= 0x90) ||
(c >= 0x96 && c <= 0x98) ||
(c >= 0x9A && c <= 0x9F) );
return result;
}
static void __FASTCALL__ gotoxy(tAbsCoord x, tAbsCoord y)
{
sprintf(vtmp,"\033[%d;%dH", (unsigned)(y + 1), (unsigned)(x + 1));
twrite(vtmp);
}
int __FASTCALL__ __vioGetCursorType( void )
{
return cursor_type;
}
void __FASTCALL__ __vioSetCursorType(int type)
{
cursor_type = type;
if (terminal == TERM_LINUX && (cursor_type == __TVIO_CUR_SOLID || type == __TVIO_CUR_SOLID)) {
sprintf(vtmp,"\033[?%dc", type == __TVIO_CUR_SOLID ? 7 : 0);
twrite(vtmp);
}
sprintf(vtmp,"\033[?25%c", type ? 'h' : 'l');
twrite(vtmp);
}
void __FASTCALL__ __viodGetCursorPos(tAbsCoord *x, tAbsCoord *y)
{
*x = saveX;
*y = saveY;
}
void __FASTCALL__ __vioSetCursorPos(tAbsCoord x, tAbsCoord y)
{
saveX = x;
saveY = y;
gotoxy(x, y);
}
void __FASTCALL__ __vioReadBuff(tAbsCoord x, tAbsCoord y, tvioBuff *buff, unsigned len)
{
unsigned char *addr = _addr(x, y);
memcpy(buff->attrs, addr, len);
memcpy(buff->chars, addr + violen, len);
memcpy(buff->oem_pg, addr + (violen << 1), len);
}
void __FASTCALL__ __vioWriteBuff(tAbsCoord x, tAbsCoord y, const tvioBuff *buff, unsigned len)
{
unsigned i;
unsigned char *p, *pb, *addr;
if (!len) return;
pb = malloc(len * 20);
if (pb == NULL) {
printm("Memory allocation failed: %s\nExiting..", strerror(errno));
exit(errno);
}
memset(pb, 0, len * 20);
addr = _addr(x, y);
memcpy(addr, buff->attrs, len);
memcpy(addr + violen, buff->chars, len);
memcpy(addr + (violen << 1), buff->oem_pg, len);
if (on_console) {
off_t offset = 4 + ((x + y * tvioWidth) << 1);
for (i = 0; i < len; i++) {
p = (unsigned char *) buff->oem_pg + i;
if (*p) {
pb[i + i] = *p;
} else {
p = (unsigned char *) buff->chars + i;
pb[i + i] = *p == 0 ? ' ' : *p;
}
p = (unsigned char *) buff->attrs + i;
pb[i + i + 1] = *p;
}
lseek(viohandle, offset, SEEK_SET);
write(viohandle, pb, len * 2);
} else {
unsigned char c, ca, cp;
gotoxy(x, y);
for (i = 0; i < len; i++) {
c = buff->chars[i];
ca = buff->attrs[i];
cp = buff->oem_pg[i];
if (cp) {
c = output_7 ? _2ps7(cp) : alt2koi[cp - 0x80];
}
if (output_7) c &= 0x7f;
if (!c) c = ' '; else if (!printable(c)) c = '.';
if ((ca != buff->attrs[i - 1] && i) || i == len || !i)
strcat(pb, _2ansi(ca));
strncat(pb, &c, 1);
}
twrite(pb);
}
free(pb);
}
void __FASTCALL__ __term_vio(void)
{
if (!initialized) return;
__vioSetCursorType(__TVIO_CUR_NORM);
if (!on_console) {
if (terminal == TERM_XTERM) twrite("\033[?1000l");
twrite("\033[3l\033[0m\033[2J");
}
__vioSetCursorPos(firstX, firstY);
if (on_console) close(viohandle);
free(vtmp);
free(viomem);
close(out_fd);
initialized = 0;
}
void __FASTCALL__ __init_vio(unsigned long flags)
{
struct vt_mode vtmode;
console_flags = flags;
out_fd = open(ttyname(STDOUT_FILENO), O_WRONLY);
if (out_fd < 0) out_fd = STDOUT_FILENO;
if (TESTFLAG(console_flags, __TVIO_FLG_DIRECT_CONSOLE_ACCESS))
on_console = ioctl(out_fd, VT_GETMODE, &vtmode) >= 0;
if (on_console && (viohandle = open(VIO_FILE, O_RDWR)) < 0) {
printm("Can't open %s: %s\nDirect console access disabled..\n", VIO_FILE, strerror(errno));
on_console = 0;
}
if (on_console) {
unsigned char b[4];
read(viohandle, &b, 4);
tvioHeight = b[0]; tvioWidth = b[1];
firstX = b[2]; firstY = b[3];
} else {
struct winsize w;
ioctl(out_fd, TIOCGWINSZ, &w);
tvioWidth = w.ws_col;
tvioHeight = w.ws_row;
}
output_7 = TESTFLAG(console_flags, __TVIO_FLG_USE_7BIT);
if (tvioWidth <= 0) tvioWidth = 80;
if (tvioHeight <= 0) tvioHeight = 25;
if (tvioWidth > VMAX_X) tvioWidth = VMAX_X;
if (tvioHeight > VMAX_Y) tvioHeight = VMAX_Y;
saveX = firstX;
saveY = firstY;
violen = tvioWidth * tvioHeight;
vtmp = malloc(VTMP_LEN);
viomem = malloc((violen << 1) + violen);
if (vtmp == NULL || viomem == NULL) {
printm("Can't allocate memory for output: %s\nExiting..\n", strerror(errno));
exit(errno);
}
memset(viomem, 0, (violen << 1) + violen);
memset(vtmp, 0, VTMP_LEN);
if (on_console) {
unsigned i;
for (i = 0; i < violen; i ++) {
read(viohandle, viomem + violen + i, 1);
read(viohandle, viomem + i, 1);
}
}
if (!on_console) {
twrite("\033[0m\033[3h");
if (terminal == TERM_XTERM) {
twrite("\033[?1000h\033]0;BIEW: Binary vIEWer\007");
output_7 = 1;
}
}
initialized = 1;
}
void __FASTCALL__ __vioSetTransparentColor(unsigned char value)
{
if (value == 0xff) {
int i = 0;
for (i = 0; i < 0x10; i++) tp.color[i] = 0xff;
tp.last = 0;
} else if (tp.last < 0x10) {
tp.color[tp.last] = value;
tp.last++;
}
}
|