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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
|
/* OpenCP Module Player
* copyright (c) '94-'10 Niklas Beisert <nbeisert@physik.tu-muenchen.de>
*
* Curses console driver
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* revision history: (please note changes here)
* -ss040613 Stian Skjelstad <stian@nixia.no>
* -first release
* -ss040902 Stian Skjelstad <stian@nixia.no>
* -added setcur, setcurshape and plDosShell
* -made displaystrattr not use bugfix for first blank cell (to avoid black
* cursor)
* -doj040907 Dirk Jagdmann <doj@cubic.org>
* -enchanced some of the entries in the chr_table
* -ss040909 Stian Skjelstad <stian@nixia.no>
* -Made NCURSES_BUGFIX1 a config option instead: [curses] fixbadgraphic
* -ss040918 Stian Skjelstad <stian@nixia.no>
* -Tweaked conRestore, conSave and curses_init, so we get the bahavior we
* expect
* -ss040919 Stian Skjelstad <stian@nixia.no>
* -New logic for setcurshape
*/
#define _CONSOLE_DRIVER
#include "config.h"
#include <curses.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include "types.h"
#include "poutput-curses.h"
#include "boot/console.h"
#include "stuff/poutput.h"
#include "boot/psetting.h"
#if defined(SIGWINCH) && defined(TIOCGWINSZ) && HAVE_RESIZETERM
#define CAN_RESIZE 1
#else
#undef CAN_RESIZE
#endif
#if CAN_RESIZE
static void adjust(int sig);
#endif
static chtype attr_table[256];
static chtype chr_table[256];
static int Width, Height;
static int fixbadgraphic;
static void displayvoid(unsigned short y, unsigned short x, unsigned short len)
{
move(y, x);
while(len)
{
chtype output;
output='X'|attr_table[plpalette[0]];
addch(output);
len--;
}
}
static void displaystr(unsigned short y, unsigned short x, unsigned char attr, const char *buf, unsigned short len)
{
move(y, x);
while(len)
{
unsigned char ch=(*buf)&0xff;
chtype output;
if (((!ch)||(ch==' '))&&(!(attr&0x80))&&fixbadgraphic)
{
output=chr_table['X']|attr_table[plpalette[(attr&0xf0)+((attr>>4)&0xf)]];
addch(output);
} else {
output=chr_table[ch]|attr_table[plpalette[attr]];
addch(output);
}
if (*buf)
buf++;
len--;
}
}
static void displaystrattr(unsigned short y, unsigned short x, const uint16_t *buf, unsigned short len)
{
int first=1; /* we need this since we sometimes place the cursor at empty spots... dirty */
move(y, x);
while(len)
{
unsigned char ch=(*buf)&0xff;
unsigned char attr=((*buf)>>8);
chtype output;
if (((!ch)||(ch==' '))&&(!(attr&0x80))&&fixbadgraphic)
{
if (first)
{
output=chr_table[ch]|attr_table[plpalette[attr]];
first=0;
} else
output=chr_table['X']|attr_table[plpalette[(attr&0xf0)+((attr>>4)&0xf)]];
addch(output);
} else {
first=1;
output=chr_table[ch]|attr_table[plpalette[attr]];
addch(output);
}
buf++;
len--;
}
}
static unsigned char bartops[18]=" ___...---===**#";
static unsigned char ibartops[18]=" ```^^^~~~===**#";
static void idrawbar(uint16_t x, uint16_t y, uint16_t height, uint32_t value, uint32_t c)
{
char buf[60];
unsigned int i;
uint16_t yh1=(height+2)/3;
uint16_t yh2=(height+yh1+1)/2;
if (value>((unsigned int)(height*16)-4))
value=(height*16)-4;
for (i=0; i<height; i++)
{
if (value>=16)
{
buf[i]=ibartops[16];
value-=16;
} else {
buf[i]=ibartops[value];
value=0;
}
}
y-=height-1;
for (i=0; i<yh1; i++)
{
displaystr(y, x, c&0xff, buf+i, 1);
y++;
}
c>>=8;
for (i=yh1; i<yh2; i++)
{
displaystr(y, x, c&0xff, buf+i, 1);
y++;
}
c>>=8;
for (i=yh2; i<height; i++)
{
displaystr(y, x, c&0xff, buf+i, 1);
y++;
}
}
static void drawbar(uint16_t x, uint16_t y, uint16_t height, uint32_t value, uint32_t c)
{
char buf[60];
unsigned int i;
uint16_t yh1=(height+2)/3;
uint16_t yh2=(height+yh1+1)/2;
if (value>((unsigned int)(height*16)-4))
value=(height*16)-4;
for (i=0; i<height; i++)
{
if (value>=16)
{
buf[i]=bartops[16];
value-=16;
} else {
buf[i]=bartops[value];
value=0;
}
}
for (i=0; i<yh1; i++)
{
displaystr(y, x, c&0xff, buf+i, 1);
y--;
}
c>>=8;
for (i=yh1; i<yh2; i++)
{
displaystr(y, x, c&0xff, buf+i, 1);
y--;
}
c>>=8;
for (i=yh2; i<height; i++)
{
displaystr(y, x, c&0xff, buf+i, 1);
y--;
}
}
static int ekbhit(void);
static int egetch(void);
static void plSetTextMode(unsigned char x)
{
unsigned int i;
_plSetGraphMode(-1);
___setup_key(ekbhit, egetch);
plScrHeight=Height;
plScrWidth=Width;
plScrMode=0;
for (i=0;i<plScrHeight;i++)
displayvoid(i, 0, plScrWidth);
}
#ifdef CAN_RESIZE
int resized=0;
static void adjust(int sig)
{
resized=1;
signal(SIGWINCH, adjust); /* some systems need this */
}
static void do_resize(void)
{
struct winsize size;
if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0)
{
resize_term(size.ws_row, size.ws_col);
wrefresh(curscr);
Height=plScrHeight=size.ws_row;
if ((Width=plScrWidth=size.ws_col)>CONSOLE_MAX_X)
Width=plScrWidth=CONSOLE_MAX_X;
else if (plScrWidth<80)
Width=plScrWidth=80; /* If a console gets smaller than THIS, the user deserves to get fucked up his or her ASS */
___push_key(VIRT_KEY_RESIZE);
}
resized=0;
}
#endif /* CAN_RESIZE */
static void RefreshScreen(void)
{
#ifdef CAN_RESIZE
if (resized)
do_resize();
#endif
refresh();
}
static int buffer=ERR;
static int ekbhit(void)
{
if (buffer!=ERR)
return 1;
buffer=getch();
if (buffer!=ERR)
return 1;
RefreshScreen();
return 0;
}
static int egetch(void)
{
int retval;
RefreshScreen();
if (buffer!=ERR)
{
retval=buffer;
buffer=ERR;
return retval;
}
retval=getch();
if (retval==ERR)
retval=0;
return retval;
}
static void setcur(unsigned char y, unsigned char x)
{
move(y, x);
}
static void setcurshape(unsigned short shape)
{
curs_set(!!shape);
}
static int conactive=0;
static int conRestore(void)
{
if (!conactive)
return 0;
endwin();
conactive=0;
return 0;
}
static void conSave(void)
{
if (conactive)
return;
fflush(stderr);
refresh();
cbreak();
nodelay(stdscr, TRUE);
noecho();
nonl();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
idlok(stdscr, FALSE);
start_color();
conactive=1;
}
static void plDosShell(void)
{
pid_t child;
if (!(child=fork()))
{
char *shell=getenv("SHELL");
if (!shell)
shell="/bin/sh";
if (!isatty(2))
{
close(2);
if (dup(1)!=2)
fprintf(stderr, __FILE__ ": dup(1) != 2\n");
}
execl(shell, shell, NULL);
perror("execl()");
exit(-1);
} else if (child>0)
{
while(1)
{
int status, retval;
if ((retval=waitpid(child, &status, 0))<0)
{
if (errno==EINTR)
continue;
}
break;
}
}
}
static const char *plGetDisplayTextModeName(void)
{
static char mode[16];
snprintf(mode, sizeof(mode), "%d x %d", Width, Height);
return mode;
}
int curses_init(void)
{
int i;
fprintf(stderr, "Initing curses... (%s)\n", curses_version());
if ((fixbadgraphic=cfGetProfileBool("curses", "fixbadgraphic", 0, 0)))
fprintf(stderr, "curses: fixbadgraphic is enabled in config\n");
if (!initscr())
{
fprintf(stderr, "curses failed to init\n");
return -1;
}
conSave();
#if CAN_RESIZE
signal(SIGWINCH, adjust); /* arrange interrupts to resize */
#endif
_displayvoid=displayvoid;
_displaystrattr=displaystrattr;
_displaystr=displaystr;
___setup_key(ekbhit, egetch); /* filters in more keys */
_plSetTextMode=plSetTextMode;
_drawbar=drawbar;
_idrawbar=idrawbar;
_conRestore=conRestore;
_conSave=conSave;
_plDosShell=plDosShell;
_setcur=setcur;
_setcurshape=setcurshape;
_plGetDisplayTextModeName = plGetDisplayTextModeName;
start_color();
attron(A_NORMAL);
/* the attr_table is insane... on a regular i386 ncurses, you have 64 pairs,
* and entry 0 is locked to grey text on black, and colors apear in wrong
* order compared to DOS etc, so this is just butt fucking ugly, but it seems
* to do the work. The code in displayattr etc looks nice then, and fast
* enough
*/
for (i=1;i<COLOR_PAIRS;i++)
{
unsigned char color_table[8] = {COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN, COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE};
unsigned char ch, fg, bg;
ch=i^0x07;
fg=color_table[ch&0x7];
bg=color_table[(ch&0x38)>>3];
init_pair(i, fg, bg);
}
for (i=0;i<256;i++)
{
attr_table[i]=COLOR_PAIR((((i&0x7)^0x07)+((i&0x70)>>1)));
if (i&0x08)
attr_table[i]|=A_BOLD;
if (i&0x80)
attr_table[i]|=A_STANDOUT;
if (i<32)
chr_table[i]=i+32;
else if (i>127)
chr_table[i]='_';
else
chr_table[i]=i;
}
chr_table[0x00]=' ';
chr_table[0x01]='S'; /* Surround */
chr_table[0x04]=ACS_DIAMOND; /* looks good on the header line */
chr_table[0x08]='?'; /* ??? */
chr_table[0x09]='?'; /* ??? */
chr_table[0x0a]='@'; /* when the heck does this occure */
chr_table[0x07]='@'; /* and this?*/
chr_table[0x0d]='@'; /* we want a note */
chr_table[0x10]=ACS_RARROW;
chr_table[0x11]=ACS_LARROW;
chr_table[0x12]=ACS_PLMINUS; /* we want an up+down arrow */
/*
chr_table[0x12]='&'; *//* retrigger */
chr_table[0x18]=ACS_UARROW;
chr_table[0x19]=ACS_DARROW;
chr_table[0x1a]='`'; /* pitch? */
chr_table[0x1b]='\''; /* pitch? */
chr_table[0x1d]=ACS_PLUS; /* speed pitch lock */
chr_table[(unsigned char)0x81]='u'; /* ?? */
chr_table[(unsigned char)0xb3]=ACS_VLINE;
chr_table[(unsigned char)0xba]=ACS_VLINE;
chr_table[(unsigned char)0xbf]=ACS_URCORNER;
chr_table[(unsigned char)0xc0]=ACS_LLCORNER;
chr_table[(unsigned char)0xc1]=ACS_BTEE;
chr_table[(unsigned char)0xc2]=ACS_TTEE;
chr_table[(unsigned char)0xc3]=ACS_LTEE;
chr_table[(unsigned char)0xc4]=ACS_HLINE;
chr_table[(unsigned char)0xd9]=ACS_LRCORNER;
chr_table[(unsigned char)0xda]=ACS_ULCORNER;
chr_table[(unsigned char)0xdd]='#';
chr_table[(unsigned char)0xf0]='#'; /* mid char of long peak power level */
chr_table[(unsigned char)0xfa]=ACS_BULLET;
chr_table[(unsigned char)0xf9]=ACS_BULLET/*|A_BOLD*/;
chr_table[(unsigned char)0xfe]=ACS_BLOCK; /* used by volume bars */
plVidType=vidNorm;
plScrType=0;
plScrMode=0;
RefreshScreen();
Height=plScrHeight=LINES;
if ((Width=plScrWidth=COLS)>CONSOLE_MAX_X)
Width=plScrWidth=CONSOLE_MAX_X;
else if (plScrWidth<80)
Width=plScrWidth=80; /* If a console gets smaller than THIS, the user deserves to get fucked up his or her ASS */
conRestore();
return 0;
}
void curses_done(void)
{
conRestore();
}
|