/*
 * DANG_BEGIN_MODULE
 *
 * currently this file contains only global video vars and
 *  initialization/cleanup code.
 *
 * DANG_END_MODULE
 *
 * DANG_BEGIN_CHANGELOG
 *
 * $Date: $
 * $Source: $
 * $Revision: $
 * $State: $
 *
 * $Log: $
 *
 * DANG_END_CHANGELOG
 */

#include "dosemu.h"
#include "base/bios.h"
#include "base/port.h"
#include "base/memory.h"
#include "env/video.h"
#include "base/termio.h"
#include "env/vc.h"

#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/ioctl.h>

static Bit16u cursor_shape = 0x0607;
static Bit16u crtc_base    = 0x03d4;
struct video_system *Video_default = NULL;

void gettermcap(int i)
{
  struct winsize ws;		/* buffer for TIOCSWINSZ */

  li = 80;
  co = 25;
  if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0) {
    li = ws.ws_row;
    co = ws.ws_col;
  }
  if ((li <= 0) || (co <= 0)) {
    error("ERROR: unknown window sizes %dx%d, setting to 80x25\n", li, co);
    li = 80;
    co = 25;
  }
  v_printf("VID: Window size set to %dx%d\n", li, co);

  get_screen_size();
}

void video_config_reset(void)
{
  video_page  = 0;
  screen_mask = 1 << (((Bit32u)phys_text_base - 0xA0000) / 4096);
  screen_adr  = SCREEN_ADR(0);

  WRITE_BYTE(bios_current_screen_page, video_page);
  WRITE_WORD(bios_cursor_shape,   cursor_shape);
  WRITE_WORD(bios_video_port,     crtc_base);
  WRITE_BYTE(bios_vdu_control,    9);	/* current 3x8 (x=b or d) value */
  WRITE_BYTE(bios_video_mode,     video_mode);
  WRITE_WORD(bios_screen_cols,    co);
  WRITE_BYTE(bios_screen_rows,    li - 1);
  WRITE_WORD(bios_video_mem_used, 4096);
  WRITE_WORD(bios_video_mem_addr, 0);
  WRITE_WORD(bios_font_height,    16);
  WRITE_BYTE(bios_video_combo,    video_combo);
  WRITE_DWORD(bios_video_saveptr, 0);		/* pointer to video table */

  /* XXX - these are the values for VGA color!
     should reflect the real display hardware. */
  WRITE_BYTE(bios_video_info_0, 0x60);
  WRITE_BYTE(bios_video_info_1, 0xF9);
  WRITE_BYTE(bios_video_info_2, 0x51);
}

void video_config_init(void)
{
  switch (config.cardtype) {
  case CARD_MDA:
      configuration |= (MDA_CONF_SCREEN_MODE);
      video_mode     = MDA_INIT_SCREEN_MODE;
      phys_text_base = MDA_PHYS_TEXT_BASE;
      virt_text_base = MDA_VIRT_TEXT_BASE;
      video_combo    = MDA_VIDEO_COMBO;
      video_subsys   = MDA_VIDEO_SUBSYS;
      graph_base     = MDA_CARD_BASE;
      graph_size     = MDA_CARD_SIZE;
      crtc_base      = MDA_BASE_CRTC;
      cursor_shape   = 0x0a0b;
      break;
  case CARD_EGA:
      configuration |= (EGA_CONF_SCREEN_MODE);
      video_mode     = EGA_INIT_SCREEN_MODE;
      phys_text_base = EGA_PHYS_TEXT_BASE;
      virt_text_base = EGA_VIRT_TEXT_BASE;
      video_combo    = EGA_VIDEO_COMBO;
      video_subsys   = EGA_VIDEO_SUBSYS;
      graph_base     = EGA_CARD_BASE;
      graph_size     = EGA_CARD_SIZE;
      crtc_base      = EGA_BASE_CRTC;
      break;
  case CARD_VGA:
      configuration |= (VGA_CONF_SCREEN_MODE);
      video_mode     = VGA_INIT_SCREEN_MODE;
      phys_text_base = VGA_PHYS_TEXT_BASE;
      virt_text_base = VGA_VIRT_TEXT_BASE;
      video_combo    = VGA_VIDEO_COMBO;
      video_subsys   = VGA_VIDEO_SUBSYS;
      graph_base     = VGA_CARD_BASE;
      graph_size     = VGA_CARD_SIZE;
      crtc_base      = VGA_BASE_CRTC;
      break;
  case CARD_CGA:
  default:			/* or Terminal, is this correct ? */
      configuration |= (CGA_CONF_SCREEN_MODE);
      video_mode     = CGA_INIT_SCREEN_MODE;
      phys_text_base = CGA_PHYS_TEXT_BASE;
      virt_text_base = CGA_VIRT_TEXT_BASE;
      video_combo    = CGA_VIDEO_COMBO;
      video_subsys   = CGA_VIDEO_SUBSYS;
      graph_base     = CGA_CARD_BASE;
      graph_size     = CGA_CARD_SIZE;
      crtc_base      = CGA_BASE_CRTC;
      break;
  }
  memcheck_addtype('v', "Video memory");
  memcheck_reserve('v', graph_base, graph_size);

  gettermcap(0);
  scr_state_init();

  if ((config.console_keyb || config.console_video) && !config.usesX)
    set_process_control();
}


