xaizek / vifm-pdcurses (License: mostly public domain) (since 2019-03-20)
PDCurses 3.4 with vifm-specific patches applied (a couple were upstreamed)
<root> / dos / pdcscrn.c (ddfb0956776374eea640bb651a30da84d64dc1ad) (16KiB) (mode 100644) [raw]
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 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
/* Public Domain Curses */

#include "pdcdos.h"

RCSID("$Id: pdcscrn.c,v 1.89 2008/07/13 16:08:17 wmcbrine Exp $")

#include <stdlib.h>

#ifdef CHTYPE_LONG
# define PDC_OFFSET 32
#else
# define PDC_OFFSET  8
#endif

/* COLOR_PAIR to attribute encoding table. */

unsigned char *pdc_atrtab = (unsigned char *)NULL;

int pdc_adapter;         /* screen type */
int pdc_scrnmode;        /* default screen mode */
int pdc_font;            /* default font size */
bool pdc_direct_video;   /* allow direct screen memory writes */
bool pdc_bogus_adapter;  /* TRUE if adapter has insane values */
unsigned pdc_video_seg;  /* video base segment */
unsigned pdc_video_ofs;  /* video base offset */

static short curstoreal[16], realtocurs[16] =
{
    COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN, COLOR_RED,
    COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE, COLOR_BLACK + 8,
    COLOR_BLUE + 8, COLOR_GREEN + 8, COLOR_CYAN + 8, COLOR_RED + 8,
    COLOR_MAGENTA + 8, COLOR_YELLOW + 8, COLOR_WHITE + 8
};

static bool sizeable = FALSE;   /* TRUE if adapter is resizeable    */

static unsigned short *saved_screen = NULL;
static int saved_lines = 0;
static int saved_cols = 0;

static int saved_scrnmode[3];
static int saved_font[3];

/* Thanks to Jeff Duntemann, K16RA for providing the impetus
   (through the Dr. Dobbs Journal, March 1989 issue) for getting
   the routines below merged into Bjorn Larsson's PDCurses 1.3...
    -- frotz@dri.com    900730 */

/* _get_font() - Get the current font size */

static int _get_font(void)
{
    int retval;

    retval = getdosmemword(0x485);

    /* Assume the MDS Genius is in 66 line mode. */

    if ((retval == 0) && (pdc_adapter == _MDS_GENIUS))
        retval = _FONT15;

    switch (pdc_adapter)
    {
    case _MDA:
        retval = 10;    /* POINTS is not certain on MDA/Hercules */
        break;

    case _EGACOLOR:
    case _EGAMONO:
        switch (retval)
        {
        case _FONT8:
        case _FONT14:
            break;
        default:
            retval = _FONT14;
        }
        break;

    case _CGA:
        retval = _FONT8;
    }

    return retval;
}

/* _set_font() - Sets the current font size, if the adapter allows such a 
   change. It is an error to attempt to change the font size on a 
   "bogus" adapter. The reason for this is that we have a known video 
   adapter identity problem. e.g. Two adapters report the same identifying 
   characteristics. */

static void _set_font(int size)
{
    PDCREGS regs;

    if (pdc_bogus_adapter)
        return;

    switch (pdc_adapter)
    {
    case _CGA:
    case _MDA:
    case _MCGACOLOR:
    case _MCGAMONO:
    case _MDS_GENIUS:
        break;

    case _EGACOLOR:
    case _EGAMONO:
        if (sizeable && (pdc_font != size))
        {
            switch (size)
            {
            case _FONT8:
                regs.W.ax = 0x1112;
                regs.h.bl = 0x00;
                PDCINT(0x10, regs);
                break;
            case _FONT14:
                regs.W.ax = 0x1111;
                regs.h.bl = 0x00;
                PDCINT(0x10, regs);
            }
        }
        break;

    case _VGACOLOR:
    case _VGAMONO:
        if (sizeable && (pdc_font != size))
        {
            switch (size)
            {
            case _FONT8:
                regs.W.ax = 0x1112;
                regs.h.bl = 0x00;
                PDCINT(0x10, regs);
                break;
            case _FONT14:
                regs.W.ax = 0x1111;
                regs.h.bl = 0x00;
                PDCINT(0x10, regs);
                break;
            case _FONT16:
                regs.W.ax = 0x1114;
                regs.h.bl = 0x00;
                PDCINT(0x10, regs);
            }
        }
    }

    curs_set(SP->visibility);

    pdc_font = _get_font();
}

/* _set_80x25() - force a known screen state: 80x25 text mode. Forces the 
   appropriate 80x25 alpha mode given the display adapter. */

static void _set_80x25(void)
{
    PDCREGS regs;

    switch (pdc_adapter)
    {
    case _CGA:
    case _EGACOLOR:
    case _EGAMONO:
    case _VGACOLOR:
    case _VGAMONO:
    case _MCGACOLOR:
    case _MCGAMONO:
        regs.h.ah = 0x00;
        regs.h.al = 0x03;
        PDCINT(0x10, regs);
        break;
    case _MDA:
        regs.h.ah = 0x00;
        regs.h.al = 0x07;
        PDCINT(0x10, regs);
    }
}

/* _get_scrn_mode() - Return the current BIOS video mode */

static int _get_scrn_mode(void)
{
    PDCREGS regs;

    regs.h.ah = 0x0f;
    PDCINT(0x10, regs);

    return (int)regs.h.al;
}

/* _set_scrn_mode() - Sets the BIOS Video Mode Number only if it is 
   different from the current video mode. */

static void _set_scrn_mode(int new_mode)
{
    PDCREGS regs;

    if (_get_scrn_mode() != new_mode)
    {
        regs.h.ah = 0;
        regs.h.al = (unsigned char) new_mode;
        PDCINT(0x10, regs);
    }

    pdc_font = _get_font();
    pdc_scrnmode = new_mode;
    LINES = PDC_get_rows();
    COLS = PDC_get_columns();
}

/* _sanity_check() - A video adapter identification sanity check. This 
   routine will force sane values for various control flags. */

static int _sanity_check(int adapter)
{
    int fontsize = _get_font();
    int rows = PDC_get_rows();

    PDC_LOG(("_sanity_check() - called: Adapter %d\n", adapter));

    switch (adapter)
    {
    case _EGACOLOR:
    case _EGAMONO:
        switch (rows)
        {
        case 25:
        case 43:    
            break;
        default:
            pdc_bogus_adapter = TRUE;
        }

        switch (fontsize)
        {
        case _FONT8:
        case _FONT14:
            break;
        default:
            pdc_bogus_adapter = TRUE;
        }
        break;

    case _VGACOLOR:
    case _VGAMONO:
        break;

    case _CGA:
    case _MDA:
    case _MCGACOLOR:
    case _MCGAMONO:
        switch (rows)
        {
        case 25:
            break;
        default:
            pdc_bogus_adapter = TRUE;
        }
        break;

    default:
        pdc_bogus_adapter = TRUE;
    }

    if (pdc_bogus_adapter)
    {
        sizeable = FALSE;
        pdc_direct_video = FALSE;
    }

    return adapter;
}

/* _query_adapter_type() - Determine PC video adapter type. */

static int _query_adapter_type(void)
{
    PDCREGS regs;
    int retval = _NONE;

    /* thanks to paganini@ax.apc.org for the GO32 fix */

#if !defined(__DJGPP__) && !defined(__WATCOMC__)
    struct SREGS segs;
#endif
    short video_base = getdosmemword(0x463);

    PDC_LOG(("_query_adapter_type() - called\n"));

    /* attempt to call VGA Identify Adapter Function */

    regs.W.ax = 0x1a00;
    PDCINT(0x10, regs);

    if ((regs.h.al == 0x1a) && (retval == _NONE))
    {
        /* We know that the PS/2 video BIOS is alive and well. */

        switch (regs.h.al)
        {
        case 0:
            retval = _NONE;
            break;
        case 1:
            retval = _MDA;
            break;
        case 2:
            retval = _CGA;
            break;
        case 4:
            retval = _EGACOLOR;
            sizeable = TRUE;
            break;
        case 5:
            retval = _EGAMONO;
            break;
        case 26:            /* ...alt. VGA BIOS... */
        case 7:
            retval = _VGACOLOR;
            sizeable = TRUE;
            break;
        case 8:
            retval = _VGAMONO;
            break;
        case 10:
        case 13:
            retval = _MCGACOLOR;
            break;
        case 12:
            retval = _MCGAMONO;
            break;
        default:
            retval = _CGA;
        }
    }
    else
    {
        /* No VGA BIOS, check for an EGA BIOS by selecting an
           Alternate Function Service...

           bx == 0x0010 --> return EGA information */

        regs.h.ah = 0x12;
        regs.W.bx = 0x10;
        PDCINT(0x10, regs);

        if ((regs.h.bl != 0x10) && (retval == _NONE))
        {
            /* An EGA BIOS exists */

            regs.h.ah = 0x12;
            regs.h.bl = 0x10;
            PDCINT(0x10, regs);

            if (regs.h.bh == 0)
                retval = _EGACOLOR;
            else
                retval = _EGAMONO;
        }
        else if (retval == _NONE)
        {
            /* Now we know we only have CGA or MDA */

            PDCINT(0x11, regs);

            switch (regs.h.al & 0x30)
            {
            case 0x10:
            case 0x20:
                retval = _CGA;
                break;
            case 0x30:
                retval = _MDA;
                break;
            default:
                retval = _NONE;
            }
        }
    }

    if (video_base == 0x3d4)
    {
        pdc_video_seg = 0xb800;
        switch (retval)
        {
        case _EGAMONO:
            retval = _EGACOLOR;
            break;
        case _VGAMONO:
            retval = _VGACOLOR;
        }
    }

    if (video_base == 0x3b4)
    {
        pdc_video_seg = 0xb000;
        switch (retval)
        {
        case _EGACOLOR:
            retval = _EGAMONO;
            break;
        case _VGACOLOR:
            retval = _VGAMONO;
        }
    }

    if ((retval == _NONE)
#ifndef CGA_DIRECT
    ||  (retval == _CGA)
#endif
    )
        pdc_direct_video = FALSE;

    if ((unsigned)pdc_video_seg == 0xb000)
        SP->mono = TRUE;
    else
        SP->mono = FALSE;

    /* Check for DESQview shadow buffer
       thanks to paganini@ax.apc.org for the GO32 fix */

#ifndef __WATCOMC__
    regs.h.ah = 0xfe;
    regs.h.al = 0;
    regs.x.di = pdc_video_ofs;
# ifdef __DJGPP__
    regs.x.es = pdc_video_seg;
    __dpmi_int(0x10, &regs);
    pdc_video_seg = regs.x.es;
# else
    segs.es   = pdc_video_seg;
    int86x(0x10, &regs, &regs, &segs);
    pdc_video_seg = segs.es;
# endif
    pdc_video_ofs = regs.x.di;
#endif
    if (!pdc_adapter)
        pdc_adapter = retval;

    return _sanity_check(retval);
}

/* close the physical screen -- may restore the screen to its state 
   before PDC_scr_open(); miscellaneous cleanup */

void PDC_scr_close(void)
{
#if SMALL || MEDIUM
# ifndef __PACIFIC__
    struct SREGS segregs;
# endif
    int ds;
#endif
    PDC_LOG(("PDC_scr_close() - called\n"));

    if (getenv("PDC_RESTORE_SCREEN") && saved_screen)
    {
#ifdef __DJGPP__
        dosmemput(saved_screen, saved_lines * saved_cols * 2,
            (unsigned long)_FAR_POINTER(pdc_video_seg,
            pdc_video_ofs));
#else
# if (SMALL || MEDIUM)
#  ifdef __PACIFIC__
        ds = FP_SEG((void far *)saved_screen);
#  else
        segread(&segregs);
        ds = segregs.ds;
#  endif
        movedata(ds, (int)saved_screen, pdc_video_seg, pdc_video_ofs,
        (saved_lines * saved_cols * 2));
# else
        memcpy((void *)_FAR_POINTER(pdc_video_seg, pdc_video_ofs),
        (void *)saved_screen, (saved_lines * saved_cols * 2));
# endif
#endif
        free(saved_screen);
        saved_screen = NULL;
    }

    reset_shell_mode();

    if (SP->visibility != 1)
        curs_set(1);

    /* Position cursor to the bottom left of the screen. */

    PDC_gotoyx(PDC_get_rows() - 2, 0);
}

void PDC_scr_free(void)
{
    if (SP)
        free(SP);
    if (pdc_atrtab)
        free(pdc_atrtab);

    pdc_atrtab = (unsigned char *)NULL;
}

/* open the physical screen -- allocate SP, miscellaneous intialization, 
   and may save the existing screen for later restoration */

int PDC_scr_open(int argc, char **argv)
{
#if SMALL || MEDIUM
# ifndef __PACIFIC__
    struct SREGS segregs;
# endif
    int ds;
#endif
    int i;

    PDC_LOG(("PDC_scr_open() - called\n"));

    SP = calloc(1, sizeof(SCREEN));
    pdc_atrtab = calloc(PDC_COLOR_PAIRS * PDC_OFFSET, 1);

    if (!SP || !pdc_atrtab)
        return ERR;

    for (i = 0; i < 16; i++)
        curstoreal[realtocurs[i]] = i;

    SP->orig_attr = FALSE;

    pdc_direct_video = TRUE; /* Assume that we can */
    pdc_video_seg = 0xb000;  /* Base screen segment addr */
    pdc_video_ofs = 0x0;     /* Base screen segment ofs */

    pdc_adapter = _query_adapter_type();
    pdc_scrnmode = _get_scrn_mode();
    pdc_font = _get_font();

    SP->lines = PDC_get_rows();
    SP->cols = PDC_get_columns();

    SP->mouse_wait = PDC_CLICK_PERIOD;
    SP->audible = TRUE;

    /* If the environment variable PDCURSES_BIOS is set, the DOS int10() 
       BIOS calls are used in place of direct video memory access. */

    if (getenv("PDCURSES_BIOS"))
        pdc_direct_video = FALSE;

    /* This code for preserving the current screen. */

    if (getenv("PDC_RESTORE_SCREEN"))
    {
        saved_lines = SP->lines;
        saved_cols = SP->cols;

        saved_screen = malloc(saved_lines * saved_cols * 2);

        if (!saved_screen)
        {
            SP->_preserve = FALSE;
            return OK;
        }
#ifdef __DJGPP__
        dosmemget((unsigned long)_FAR_POINTER(pdc_video_seg, pdc_video_ofs),
                  saved_lines * saved_cols * 2, saved_screen);
#else
# if SMALL || MEDIUM
#  ifdef __PACIFIC__
        ds = FP_SEG((void far *) saved_screen);
#  else
        segread(&segregs);
        ds = segregs.ds;
#  endif
        movedata(pdc_video_seg, pdc_video_ofs, ds, (int)saved_screen,
                 (saved_lines * saved_cols * 2));
# else
        memcpy((void *)saved_screen,
               (void *)_FAR_POINTER(pdc_video_seg, pdc_video_ofs),
               (saved_lines * saved_cols * 2));
# endif
#endif
    }

    SP->_preserve = (getenv("PDC_PRESERVE_SCREEN") != NULL);

    return OK;
}

/* the core of resize_term() */

int PDC_resize_screen(int nlines, int ncols)
{
    PDC_LOG(("PDC_resize_screen() - called. Lines: %d Cols: %d\n",
             nlines, ncols));

    /* Trash the stored value of orig_cursor -- it's only good if the 
       video mode doesn't change */

    SP->orig_cursor = 0x0607;

    switch (pdc_adapter)
    {
    case _EGACOLOR:
        if (nlines >= 43)
            _set_font(_FONT8);
        else
            _set_80x25();
        break;

    case _VGACOLOR:
        if (nlines > 28)
            _set_font(_FONT8);
        else
            if (nlines > 25)
                _set_font(_FONT14);
            else
                _set_80x25();
    }

    PDC_set_blink(COLORS == 8);

    return OK;
}

void PDC_reset_prog_mode(void)
{
        PDC_LOG(("PDC_reset_prog_mode() - called.\n"));
}

void PDC_reset_shell_mode(void)
{
        PDC_LOG(("PDC_reset_shell_mode() - called.\n"));
}

void PDC_restore_screen_mode(int i)
{
    if (i >= 0 && i <= 2)
    {
        pdc_font = _get_font();
        _set_font(saved_font[i]);

        if (_get_scrn_mode() != saved_scrnmode[i])
            _set_scrn_mode(saved_scrnmode[i]);
    }
}

void PDC_save_screen_mode(int i)
{
    if (i >= 0 && i <= 2)
    {
        saved_font[i] = pdc_font;
        saved_scrnmode[i] = pdc_scrnmode;
    }
}

void PDC_init_pair(short pair, short fg, short bg)
{
    unsigned char att, temp_bg;
    chtype i;

    fg = curstoreal[fg];
    bg = curstoreal[bg];

    for (i = 0; i < PDC_OFFSET; i++)
    {
        att = fg | (bg << 4);

        if (i & (A_REVERSE >> PDC_ATTR_SHIFT))
            att = bg | (fg << 4);
        if (i & (A_UNDERLINE >> PDC_ATTR_SHIFT))
            att = 1;
        if (i & (A_INVIS >> PDC_ATTR_SHIFT))
        {
            temp_bg = att >> 4;
            att = temp_bg << 4 | temp_bg;
        }
        if (i & (A_BOLD >> PDC_ATTR_SHIFT))
            att |= 8;
        if (i & (A_BLINK >> PDC_ATTR_SHIFT))
            att |= 128;

        pdc_atrtab[pair * PDC_OFFSET + i] = att;
    }
}

int PDC_pair_content(short pair, short *fg, short *bg)
{
    *fg = realtocurs[pdc_atrtab[pair * PDC_OFFSET] & 0x0F];
    *bg = realtocurs[(pdc_atrtab[pair * PDC_OFFSET] & 0xF0) >> 4];

    return OK;
}

/* _egapal() - Find the EGA palette value (0-63) for the color (0-15).
   On VGA, this is an index into the DAC. */

static short _egapal(short color)
{
    PDCREGS regs;

    regs.W.ax = 0x1007;
    regs.h.bl = curstoreal[color];

    PDCINT(0x10, regs);

    return regs.h.bh;
}

bool PDC_can_change_color(void)
{
    return (pdc_adapter == _VGACOLOR);
}

/* These are only valid when pdc_adapter == _VGACOLOR */

int PDC_color_content(short color, short *red, short *green, short *blue)
{
    PDCREGS regs;

    /* Read single DAC register */

    regs.W.ax = 0x1015;
    regs.h.bl = _egapal(color);

    PDCINT(0x10, regs);

    /* Scale and store */

    *red = DIVROUND((unsigned)(regs.h.dh) * 1000, 63);
    *green = DIVROUND((unsigned)(regs.h.ch) * 1000, 63);
    *blue = DIVROUND((unsigned)(regs.h.cl) * 1000, 63);

    return OK;
}

int PDC_init_color(short color, short red, short green, short blue)
{
    PDCREGS regs;

    /* Scale */

    regs.h.dh = DIVROUND((unsigned)red * 63, 1000);
    regs.h.ch = DIVROUND((unsigned)green * 63, 1000);
    regs.h.cl = DIVROUND((unsigned)blue * 63, 1000);

    /* Set single DAC register */

    regs.W.ax = 0x1010;
    regs.W.bx = _egapal(color);

    PDCINT(0x10, regs);

    return OK;
}
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/vifm-pdcurses

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/vifm-pdcurses

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master