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 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
|
/*
* hw.c -- common functions to use HW/hw_* displays
*
* Copyright (C) 1993-2001 by Massimiliano Ghilardi
*
* 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 is a very sensible part of code, as it must
* correctly link both against twin and against twdisplay,
* picking the correct versions of functions like
* FlushHW(), PanicHW(), AllHWCanDragAreaNow(), DragAreaHW(), etc.
*
* This for example rules out calling methods like Delete()
* or referencing the variable All.
*/
#include <signal.h>
#include <sys/stat.h>
#include "autoconf.h"
#ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#endif
#ifdef HAVE_SYS_TTYDEFAULTS_H
# include <sys/ttydefaults.h>
#else
# include "my_ttydefaults.h"
#endif
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#include "tty_ioctl.h"
#include "twin.h"
#include "hw.h"
#include "common.h"
#ifndef VDISABLE
# ifdef _POSIX_VDISABLE
# define VDISABLE _POSIX_VDISABLE
# else
# define VDISABLE 255
# endif
#endif
display_hw HW, DisplayHWCTTY;
hwattr *Video, *OldVideo;
byte NeedOldVideo, CanDragArea;
byte ExpensiveFlushVideo, ValidOldVideo, NeedHW;
dat (*ChangedVideo)[2][2];
byte ChangedVideoFlag, ChangedVideoFlagAgain;
byte QueuedDrawArea2FullScreen;
dat DisplayWidth, DisplayHeight;
udat CursorX, CursorY;
uldat CursorType;
struct termios ttysave;
VOLATILE byte GotSignals;
static VOLATILE byte GotSignalWinch;
static VOLATILE byte GotSignalChild;
static VOLATILE byte GotSignalHangup;
#if RETSIGTYPE == void
# define RETFROMSIGNAL
#else
# define RETFROMSIGNAL return 0;
#endif
static RETSIGTYPE SignalWinch(int n) {
GotSignals = GotSignalWinch = TRUE;
signal(SIGWINCH, SignalWinch);
RETFROMSIGNAL
}
static RETSIGTYPE HandleSignalWinch(void) {
GotSignalWinch = FALSE;
if (DisplayHWCTTY && DisplayHWCTTY != HWCTTY_DETACHED
&& DisplayHWCTTY->DisplayIsCTTY) {
ResizeDisplayPrefer(DisplayHWCTTY);
}
RETFROMSIGNAL
}
static RETSIGTYPE SignalChild(int n) {
GotSignals = GotSignalChild = TRUE;
signal(SIGCHLD, SignalChild);
RETFROMSIGNAL
}
static void HandleSignalChild(void) {
pid_t pid;
int status;
GotSignalChild = FALSE;
while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) != 0 && pid != (pid_t)-1) {
if (WIFEXITED(status) || WIFSIGNALED(status))
RemotePidIsDead(pid);
}
}
/*
* got a SIGHUP. shutdown the display on controlling tty, if any
*/
static RETSIGTYPE SignalHangup(int n) {
GotSignals = GotSignalHangup = TRUE;
signal(SIGHUP, SignalHangup);
RETFROMSIGNAL
}
static void HandleSignalHangup(void) {
GotSignalHangup = FALSE;
if (DisplayHWCTTY && DisplayHWCTTY != HWCTTY_DETACHED
&& DisplayHWCTTY->DisplayIsCTTY) {
DisplayHWCTTY->NeedHW |= NEEDPanicHW, NeedHW |= NEEDPanicHW;
}
}
void HandleSignals(void) {
GotSignals = FALSE;
if (GotSignalWinch)
HandleSignalWinch();
if (GotSignalChild)
HandleSignalChild();
if (GotSignalHangup)
HandleSignalHangup();
}
#ifndef DONT_TRAP_SIGNALS
static RETSIGTYPE SignalPanic(int n) {
sigset_t s, t;
signal(n, SIG_DFL);
sigemptyset(&s);
sigaddset(&s, n);
sigprocmask(SIG_BLOCK, &s, &t);
Quit(-n);
kill(getpid(), n);
RETFROMSIGNAL
}
#endif
byte InitSignals(void) {
signal(SIGWINCH,SignalWinch);
signal(SIGCHLD, SignalChild);
signal(SIGPIPE, SIG_IGN);
signal(SIGIO, SIG_IGN);
#ifndef DONT_TRAP_SIGNALS
signal(SIGHUP, SignalHangup);
signal(SIGINT, SignalPanic);
signal(SIGQUIT, SignalPanic);
signal(SIGILL, SignalPanic);
signal(SIGABRT, SignalPanic);
signal(SIGBUS, SignalPanic);
signal(SIGFPE, SignalPanic);
signal(SIGSEGV, SignalPanic);
signal(SIGTERM, SignalPanic);
signal(SIGXCPU, SignalPanic);
signal(SIGXFSZ, SignalPanic);
# ifdef SIGPWR
signal(SIGPWR, SignalPanic);
# endif
#endif
return TRUE;
}
void QuitSignals(void) {
signal(SIGWINCH,SIG_IGN);
signal(SIGCHLD, SIG_IGN);
signal(SIGPIPE, SIG_IGN);
signal(SIGIO, SIG_IGN);
#ifndef DONT_TRAP_SIGNALS
signal(SIGHUP, SIG_DFL);
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
signal(SIGILL, SIG_DFL);
signal(SIGABRT, SIG_DFL);
signal(SIGBUS, SIG_DFL);
signal(SIGFPE, SIG_DFL);
signal(SIGSEGV, SIG_DFL);
signal(SIGTERM, SIG_DFL);
signal(SIGXCPU, SIG_DFL);
signal(SIGXFSZ, SIG_DFL);
# ifdef SIGPWR
signal(SIGPWR, SIG_DFL);
# endif
#endif
}
void MoveToXY(dat x, dat y) {
CursorX = x;
CursorY = y;
}
void SetCursorType(uldat type) {
if ((type & 0xF) == 0)
type |= LINECURSOR;
else if ((type & 0xF) > SOLIDCURSOR)
type = (type & ~(uldat)0xF) | SOLIDCURSOR;
CursorType = type;
}
void NeedRedrawVideo(dat Left, dat Up, dat Right, dat Down) {
if (HW->RedrawVideo) {
HW->RedrawLeft = Min2(HW->RedrawLeft, Left);
HW->RedrawUp = Min2(HW->RedrawUp, Up);
HW->RedrawRight = Max2(HW->RedrawRight, Right);
HW->RedrawDown = Max2(HW->RedrawDown, Down);
} else {
HW->RedrawVideo = TRUE;
HW->RedrawLeft = Left;
HW->RedrawUp = Up;
HW->RedrawRight = Right;
HW->RedrawDown = Down;
}
}
/*
* The following functions are quite os-independent,
* but they are part of the functionality implemented by hw.c
* so they are included here.
*
* Also, some implementations might hw accel some of these...
*/
/*
* for better cleannes, DirtyVideo()
* should be used *before* actually touching Video[]
*/
void DirtyVideo(dat Xstart, dat Ystart, dat Xend, dat Yend) {
dat s0, s1, e0, e1, len, min;
byte i;
if (QueuedDrawArea2FullScreen ||
Xstart > Xend || Xstart >= DisplayWidth || Xend < 0 ||
Ystart > Yend || Ystart >= DisplayHeight || Yend < 0)
return;
Xstart = Max2(Xstart, 0);
Ystart = Max2(Ystart, 0);
Xend = Min2(Xend, DisplayWidth-1);
Yend = Min2(Yend, DisplayHeight-1);
ChangedVideoFlag = ChangedVideoFlagAgain = TRUE;
for (; Ystart <= Yend; Ystart++) {
s0 = ChangedVideo[Ystart][0][0];
e0 = ChangedVideo[Ystart][0][1];
s1 = ChangedVideo[Ystart][1][0];
e1 = ChangedVideo[Ystart][1][1];
/* decide how to rearrange the slots to include Xstart..Xend */
if (s0 == -1) {
/* trivial ! */
ChangedVideo[Ystart][0][0] = Xstart;
ChangedVideo[Ystart][0][1] = Xend;
continue;
}
if ((s0 != -1 && Xstart >= s0 && Xend <= e0) ||
(s1 != -1 && Xstart >= s1 && Xend <= e1))
/* nothing to do :) */
continue;
if (s1 == -1) {
/*
* this is quite simple.
* if s0..e0 and Xstart..Xend intersect or touch, merge.
* else just put them in s0,e0 and s1,e1 :
* there is no point in forcing a merge now if it costs cells,
* we'll merge (if needed) when a third dirty segment appears.
*/
if (Xstart <= e0+1 && Xend+1 >= s0) {
ChangedVideo[Ystart][0][0] = Min2(s0, Xstart);
ChangedVideo[Ystart][0][1] = Max2(e0, Xend);
} else if (Xstart < s0) {
ChangedVideo[Ystart][0][0] = Xstart;
ChangedVideo[Ystart][0][1] = Xend;
ChangedVideo[Ystart][1][0] = s0;
ChangedVideo[Ystart][1][1] = e0;
} else {
ChangedVideo[Ystart][1][0] = Xstart;
ChangedVideo[Ystart][1][1] = Xend;
}
continue;
}
/*
* now the hairy thing... there are 5 possible actions:
*
* 0. put Xstart..Xend in slot 0 and merge s0..e0 with s1..e1
* 1. merge Xstart..Xend to s0..e0
* 2. merge all together, freeing slot 1
* 3. merge Xstart..Xend to s1..e1
* 4. put Xstart..Xend in slot 1 and merge s1..e1 with s0..e0
* 4. is the same as 0., except for slot inversion.
*
* do it the brutal way: calculate the 5 cases and choose
* the one resulting in less dirty cells.
*
* we do no checks about the relative ordering of s0..e0, s1..e1, Xstart..Xend
* and use Min2(sA, sB)..Max2(eA, eB) for merging.
* the less dirty pattern automatically selects the best solution
* which is non intersecting, and in the 0.,4. cases we choose manually
* to preserve ordering between slots.
*
* all lenghts are decreased by 2 to avoid always doing (...)+2
*/
/*0,4*/ min = Xend-Xstart + e1-s0; i = 0;
/* 1 */ len = Max2(Xend,e0)-Min2(Xstart,s0) + e1-e0; if (len < min) min = len, i = 1;
/* 2 */ len = Max2(Xend,e1)-Min2(Xstart,s0) - 1; if (len <= min) min = len, i = 2; /* prefer this if equal */
/* 3 */ len = e0-s0 + Max2(Xend,e1)-Min2(Xstart,s1); if (len < min) min = len, i = 3;
switch (i) {
case 0:
i = Xstart > s0;
ChangedVideo[Ystart][!i][0] = s0;
ChangedVideo[Ystart][!i][1] = e1;
ChangedVideo[Ystart][ i][0] = Xstart;
ChangedVideo[Ystart][ i][1] = Xend;
break;
case 1:
ChangedVideo[Ystart][0][0] = Min2(Xstart,s0);
ChangedVideo[Ystart][0][1] = Max2(Xend,e0);
break;
case 2:
ChangedVideo[Ystart][0][0] = Min2(Xstart,s0);
ChangedVideo[Ystart][0][1] = Max2(Xend,e1);
ChangedVideo[Ystart][1][0] = -1;
break;
case 3:
ChangedVideo[Ystart][1][0] = Min2(Xstart,s1);
ChangedVideo[Ystart][1][1] = Max2(Xend,e1);
break;
default:
break;
}
}
}
static void Video2OldVideo(dat Xstart, dat Ystart, dat Xend, dat Yend) {
hwattr *src, *dst;
uldat xc, yc;
if (Xstart > Xend || Xstart >= DisplayWidth || Xend < 0 ||
Ystart > Yend || Ystart >= DisplayHeight || Yend < 0)
return;
Xstart = Max2(Xstart, 0);
Ystart = Max2(Ystart, 0);
Xend = Min2(Xend, DisplayWidth-1);
Yend = Min2(Yend, DisplayHeight-1);
yc = Yend - Ystart + 1;
xc = sizeof(hwattr) * (Xend - Xstart + 1);
src = Video + Xstart + Ystart * DisplayWidth;
dst = OldVideo + Xstart + Ystart * DisplayWidth;
while (yc--) {
CopyMem(src, dst, xc);
src += DisplayWidth;
dst += DisplayWidth;
}
}
/* An important Video function: copy a rectangle. It must be _*FAST*_ !! */
void DragArea(dat Left, dat Up, dat Rgt, dat Dwn, dat DstLeft, dat DstUp) {
dat DstRgt = DstLeft + (Rgt - Left), DstDwn = DstUp + (Dwn - Up);
ldat len, count;
hwattr *src = Video, *dst = Video;
byte Accel;
if (QueuedDrawArea2FullScreen)
return;
count = Dwn - Up + 1;
len = (Rgt-Left+1) * sizeof(hwattr);
/* if HW can do the scroll, use it instead of redrawing */
/* HACK : for consistency problems, we actually drag only if all HW can drag */
Accel = AllHWCanDragAreaNow(Left, Up, Rgt, Dwn, DstLeft, DstUp);
if (Accel) {
FlushHW();
DragAreaHW(Left, Up, Rgt, Dwn, DstLeft, DstUp);
} else
DirtyVideo(DstLeft, DstUp, DstRgt, DstDwn);
/* do the drag inside Video[] */
if (DstUp <= Up) {
src += Left + Up * DisplayWidth;
dst += DstLeft + DstUp * DisplayWidth;
if (DstUp != Up) {
/* copy forward */
while (count--) {
CopyMem(src, dst, len);
dst += DisplayWidth;
src += DisplayWidth;
}
} else if (Left != DstLeft) {
/* the tricky case: DstUp == Up */
/* copy forward, but with memmove() */
while (count--) {
MoveMem(src, dst, len);
dst += DisplayWidth;
src += DisplayWidth;
}
}
} else if (DstUp > Up) {
/* copy backward */
src += Left + Dwn * DisplayWidth;
dst += DstLeft + DstDwn * DisplayWidth;
while (count--) {
CopyMem(src, dst, len);
dst -= DisplayWidth;
src -= DisplayWidth;
}
}
if (Accel && NeedOldVideo)
Video2OldVideo(DstLeft, DstUp, DstRgt, DstDwn);
}
byte InitTtysave(void) {
int _fd = open("/dev/tty", O_RDWR|O_NOCTTY);
int fd = _fd >= 0 ? _fd : 0;
byte ok = tty_getioctl(fd, &ttysave) == 0;
if (_fd >= 0)
close(_fd);
ttysave.c_cc [VINTR] = CINTR;
ttysave.c_cc [VQUIT] = CQUIT;
ttysave.c_cc [VERASE] = CERASE;
ttysave.c_cc [VKILL] = CKILL;
ttysave.c_cc [VSTART] = CSTART;
ttysave.c_cc [VSTOP] = CSTOP;
ttysave.c_cc [VSUSP] = CSUSP;
#ifdef VDSUSP
ttysave.c_cc [VDSUSP] = VDISABLE;
#endif
#ifdef VREPRINT
ttysave.c_cc [VREPRINT] = CRPRNT;
#endif
#ifdef VDISCRD
ttysave.c_cc [VDISCRD] = CFLUSH;
#endif
#ifdef VWERSE
ttysave.c_cc [VWERSE] = CWERASE;
#endif
#ifdef VLNEXT
ttysave.c_cc [VLNEXT] = CLNEXT;
#endif
ttysave.c_cc [VEOF] = CEOF;
ttysave.c_cc [VEOL] = VDISABLE;
#ifdef VEOL2
ttysave.c_cc [VEOL2] = VDISABLE;
#endif
#ifdef VSWTC
ttysave.c_cc [VSWTC] = VDISABLE;
#endif
#ifdef VSWTCH
ttysave.c_cc [VSWTCH] = VDISABLE;
#endif
ttysave.c_cc [VMIN] = 1;
ttysave.c_cc [VTIME] = 0;
if (ok) {
/* tweak as needed */
/* input modes */
ttysave.c_iflag |= (BRKINT | IGNPAR | ICRNL | IXON
#ifdef IMAXBEL
| IMAXBEL
#endif
);
ttysave.c_iflag &= ~(IGNBRK
#ifdef PARMRK
| PARMRK
#endif
#ifdef INPCK
| INPCK
#endif
#ifdef IUCLC
| IUCLC
#endif
| ISTRIP | INLCR | IXANY | IXOFF);
/* output modes */
ttysave.c_oflag |= (OPOST | ONLCR);
ttysave.c_oflag &= ~(0
#ifdef OLCUC
| OLCUC
#endif
#ifdef ONOCR
| ONOCR
#endif
#ifdef ONLRET
| ONLRET
#endif
#ifdef OFILL
| OFILL
#endif
#ifdef OFDEL
| OFDEL
#endif
#ifdef NLDLY
| NLDLY
#endif
#ifdef CRDLY
| CRDLY
#endif
#ifdef TABDLY
| TABDLY
#endif TABDLY
#ifdef BSDLY
| BSDLY
#endif
#ifdef VTDLY
| VTDLY
#endif
#ifdef FFDLY
| FFDLY
#endif
);
/* control modes */
ttysave.c_cflag |= (CS8 | CREAD);
ttysave.c_cflag &= ~(CSTOPB | PARENB | PARODD
#ifdef HUPCL
| HUPCL
#endif
#ifdef CLOCAL
| CLOCAL
#endif
#ifdef CMSPAR
| CMSPAR
#endif
#ifdef CRTSCTS
| CRTSCTS
#endif
);
/* line discipline modes */
ttysave.c_lflag |= (ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK
#ifdef ECHOKE
| ECHOKE
#endif
);
ttysave.c_lflag &= ~(ECHONL
#ifdef XCASE
| XCASE
#endif
#ifdef NOFLSH
| NOFLSH
#endif
#ifdef TOSTOP
| TOSTOP
#endif
#ifdef ECHOCTL
| ECHOCTL
#endif
#ifdef ECHOPRT
| ECHOPRT
#endif
#ifdef FLUSHO
| FLUSHO
#endif
#ifdef PENDIN
| PENDIN
#endif
);
} else {
/* full initialization */
/* input modes */
ttysave.c_iflag = (BRKINT | IGNPAR | ICRNL | IXON
#ifdef IMAXBEL
| IMAXBEL
#endif
);
/* output modes */
ttysave.c_oflag = (OPOST | ONLCR);
/* control modes */
ttysave.c_cflag = (CS8 | CREAD
#if defined(CBAUD) && defined(B38400)
| B38400
#endif
);
/* line discipline modes */
ttysave.c_lflag = (ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK
#ifdef ECHOKE
| ECHOKE
#endif
);
}
return TRUE;
}
|