/*-
 * Copyright (c) 2006 Robert N. M. Watson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <curses.h>
#include <stdlib.h>

#include "emul.h"

static int	cons_numlock;	/* Emulated using home key. */
static int	cons_endwin;	/* Endwin has been run already. */

/*
 * We call cons_destroy() as part of atexit(), but it may also be called
 * manually in some cases, so track that and make sure we run only once.
 */
void
cons_destroy(void)
{

	if (!cons_endwin) {
		endwin();
		cons_endwin = 1;
	}
}

void
cons_setxy(int x, int y)
{
#if 0
	int begy, begx, maxy, maxx;

	getbegyx(stdscr, begy, begx);
	getmaxyx(stdscr, maxy, maxx);

#endif
	if (move(y, x) == ERR);
#if 0
		warnx("cons_setxy x %d y %d (%d, %d) - (%d, %d)", x, y,
		    begx, begy, maxx, maxy);
#endif
}

int
cons_getx(void)
{
	int x, y;

	getyx(stdscr, y, x);
	return (x);
}

int
cons_gety(void)
{
	int x, y;

	getyx(stdscr, y, x);
	return (y);
}

int
cons_getwidth(void)
{
	int x, y;

	getmaxyx(stdscr, y, x);
	return (x + 1);
}

int
cons_getheight(void)
{
	int x, y;

	getmaxyx(stdscr, y, x);
	return (y + 1);
}

void
cons_init(void)
{

	initscr();
	cbreak();
	noecho();
	nonl();
	// nl();
	scrollok(stdscr, TRUE);
	intrflush(stdscr, FALSE);
	keypad(stdscr, TRUE);
	// immedok(stdscr, TRUE);
	clear();
	refresh();

	atexit(cons_destroy);
}

void
cons_putchar(char ch)
{

#if 0
	if (insch(ch) == ERR)
		panic("addch");
#else
	int x, y;

	x = cons_getx();
	y = cons_gety();
	addch(ch);
	cons_setxy(x, y);
#endif
	refresh();
}

int
cons_keycheck(void)
{
	int ch;

	nodelay(stdscr, 1);
	ch = getch();
	nodelay(stdscr, 0);
	if (ch == ERR)
		return (0);
	if (ch == KEY_HOME) {
		cons_numlock = !cons_numlock;
		ch = 0;
	} else
		ungetch(ch);
	return (ch);
}

int
cons_keyget(void)
{
	int ch;

	while ((ch = getch()) == KEY_HOME)
		cons_numlock = !cons_numlock;
	return (ch);
}

void
cons_winclear(int x1, int y1, int x2, int y2)
{
	WINDOW *win;

	win = newwin(y2 - y1, x2 - x1 + 1, y1, x1);
	if (win == NULL)
		panic("cons_winclear: newwin(%d, %d, %d, %d)", x1, y1, x2,
		    y2);
	if (werase(win) == ERR)
		panic("cons_winclear: werase()");
	if (wrefresh(win) == ERR)
		panic("cons_winclear: wrefresh()");
	if (delwin(win) == ERR)
		panic("cons_winclear: delwin()");
	refresh();
}

void
cons_winscrollup(int x1, int y1, int x2, int y2, int scroll)
{
	WINDOW *win;
	int i;

	win = newwin(y2 - y1, x2 - x1, y1, x1);
	if (scrollok(win, TRUE) == ERR)
		panic("cons_winscrollup: scrollok()");
	if (wscrl(win, scroll) == ERR)
		panic("cons_winscrollup(%d, %d, %d, %d, %d): wscrl(%d)",
		    x1, y1, x2, y2, scroll, scroll);
	wrefresh(win);
	delwin(win);
}

void
cons_winscrolldown(int x1, int y1, int x2, int y2, int scroll)
{

	cons_winscrollup(x1, y1, x2, y2, -1 * scroll);
}

void
cons_beep(void)
{

	if (beep() == ERR)
		panic("cons_beep: beep()");
}

int
cons_getnumlock(void)
{

	return (cons_numlock);
}
