/* vinit.c (emx+gcc) -- Copyright (c) 1987-1993 by Eberhard Mattes */

#include <stdlib.h>
#include <dos.h>
#include <sys/hw.h>
#define INCL_VIO
#include <os2emx.h>
#include <os2thunk.h>
#include <sys/video.h>
#include "video2.h"

int _v_width = 0;
int _v_height = 0;
int _v_x = 0;
int _v_y = 0;
int _v_attr = 0x07;
char *_v_mem = NULL;

static void _v_getxy (int *x, int *y)
{
  if (_osmode == OS2_MODE)
    {
      USHORT row, col;

      VioGetCurPos (&row, &col, 0);
      *x = col; *y = row;
    }
  else
    {
      union REGS r;

      r.h.ah = 0x03;
      r.h.bh = 0x00;
      _int86 (0x10, &r, &r);
      *x = r.h.dl; *y = r.h.dh;
    }
}


int v_init (void)
{
  int two[2];
  unsigned addr, end_addr;

  _scrsize (two);
  _v_width = two[0];
  _v_height = two[1];
  _v_getxy (&_v_x, &_v_y);
  _v_mem = NULL;
  if (_osmode == OS2_MODE)
    {
      _far16ptr addr;
      USHORT len;

      len = _v_width * _v_height * 2;
      if (VioGetBuf ((PULONG)&addr, &len, 0) == 0)
        _v_mem = MAKEP (SELECTOROF (addr), OFFSETOF (addr));
    }
  else
    {
      union REGS r;

      r.h.ah = 0x0f;
      _int86 (0x10, &r, &r);
      if (r.h.al == 7 || r.h.al < 4)
        {
          _int86 (0x11, &r, &r);
          if ((r.h.al & 0x30) == 0x30)
            addr = 0xb0000;
          else
            addr = 0xb8000;
          end_addr = (addr + _v_height * _v_width * 2 - 1) | 0xfff;
          _v_mem = _memaccess (addr, end_addr, 1);
        }
    }
  return (1);
}
