/* Ported to GNU C openVMS/Alpha by Klaus Kämpf, kkaempf@progis.de  */
/* <curses.h>
 *
 * Curses screen management
 */

#ifndef _CURSES_H
#define _CURSES_H

#ifdef _cplusplus
extern "C" {
#endif

#include <ssdef.h>
#include <smgdef.h>

#ifndef __ALPHA
# ifndef __VMS_CURSES
#  define __VMS_CURSES 1
# endif
#endif

#if !defined(__VMS_CURSES) && (__VMS_VER < 70000000)
#define __VMS_CURSES 1
#endif

#ifndef TRUE
# define TRUE	1
# define FALSE	0
#endif

#define ERR	0
#define OK	1

/* _win_st._flags, upper byte  */

#ifdef __VMS_CURSES
#define _SUBWIN		0001
#define _ENDLINE	0002
#define _FULLWIN	0004
#define _SCROLLWIN	0010
#define _FLUSH		0020
#define _STANDOUT	0200
#else
#define _ENDLINE	001
#define _FULLWIN	002
#define _SCROLLWIN	004
#define _FLUSH		010
#define _FULLLINE	020
#define _IDLINE		040
#define _STANDOUT	0200
#define _NOCHANGE	-1
#endif

/* _win_st._flags, lower byte  */

#define _BLINK		SMG$M_BLINK
#define _BOLD		SMG$M_BOLD
#define _REVERSE	SMG$M_REVERSE
#define _UNDERLINE	SMG$M_UNDERLINE

/* _kb_st._flags  */

#define _NOECHO		001
#define _NONL		002
#define _NOCRMODE	004
#define _NORAW		010

#define SMG_BUFSIZ 512

#ifdef __VMS_CURSES

struct	_win_st
{
  int	 _cur_y;
  int	 _cur_x;
  int	 _max_y;
  int	 _max_x;
  int	 _beg_y;
  int	 _beg_x;
  short	 _flags;
  char	 _clear;
  char   _leave;
  char	 _scroll;
  char	 _wrap;
  char	 **_y;
  short	 *_firstch;
  short	 *_lastch;
  struct _win_st *_next;
  struct _win_st *_parent;
  struct _win_st *_child;
  int	 _id;
} __attribute__ ((packed));

#else /* !__VMS_CURSES means BSD curses  */

typedef char bool;

/* map to bsd curses names  */

#define _cur_x	_curx
#define _cur_y	_cury
#define _max_x	_maxx
#define _max_y	_maxy
#define _beg_x	_begx
#define _beg_y	_begy

struct _win_st {
  short _cury;
  short _curx;
  short _maxy;
  short _maxx;
  short	_begy;
  short _begx;
  short	_flags;
  short	_ch_off;
  bool	_clear;
  bool	_leave;
  bool	_scroll;
  bool	_wrap;
  char	**_y;
  char	**_rendition;
  short	*_firstch;
  short	*_lastch;
  struct _win_st *_nextp;
  struct _win_st *_orig;
  struct _win_st *_next;
  struct _win_st *_parent;
  struct _win_st *_child;
  int	_id;
};
#endif  /* !__VMS_CURSES  */

struct	_kb_st
{
  int _id;			/* SMG keybd id  */
  unsigned char _flags;		/* raw, break, ...  */
  struct
    {
      unsigned short length;
      unsigned char type;
#ifdef _cplusplus
      unsigned char _class;
#else
      unsigned char class;
#endif
      char *address;
    } _buffer_desc;		/* input buffer desc  */
  int _count;			/* unread, pending chars  */
  char *_ptr;			/* ptr to next pending char  */
};

struct	_pb_st
{
  int _id;
  int _rows;
  int _cols;
  union	smgdef *_attr;
  int _attr_size;
};

#define _KEYBOARD	struct _kb_st
#define	 WINDOW		struct _win_st
#define _PASTEBOARD	struct _pb_st

#ifndef __ALPHA
#define LINES	(decc$ga_LINES)
#define COLS	(decc$ga_COLS)
#define stdscr	(decc$ga_stdscr)
#define curscr	(decc$ga_curscr)
#define stdkb	(decc$ga_stdkb)
#define stdpb	(decc$ga_stdpb)
#endif

extern int LINES __asm ("decc$ga_LINES");
extern int COLS __asm("decc$ga_COLS");
extern WINDOW *stdscr __asm("decc$ga_stdscr");
extern WINDOW *curscr __asm("decc$ga_curscr");
extern _KEYBOARD *stdkb __asm("decc$ga_stdkb");
extern _PASTEBOARD *stdpb __asm("decc$ga_stdpb");

#ifdef __VMS_CURSES
# define __fdecl(_func,_arglist) _func _arglist __asm("decc$bsd_" #_func)
# if _IEEE_FLOAT
#  define __gdecl(_func, _arglist) _func _arglist __asm("decc$txbsd_" #_func)
#  define __xdecl(_func, _arglist) _func _arglist __asm("gnuc$txbsd_" #_func)
# elif _G_FLOAT
#  define __gdecl(_func, _arglist) _func _arglist __asm("decc$gxbsd_" #_func)
#  define __xdecl(_func, _arglist) _func _arglist __asm("gnuc$gxbsd_" #_func)
# else
#  define __gdecl(_func, _arglist) _func _arglist __asm("decc$dxbsd_" #_func)
#  define __xdecl(_func, _arglist) _func _arglist __asm("gnuc$dxbsd_" #_func)
# endif
#else
# define __fdecl(_func,_arglist) _func _arglist __asm("decc$" #_func)
# if _IEEE_FLOAT
#  define __gdecl(_func, _arglist) _func _arglist __asm("decc$tx" #_func)
#  define __xdecl(_func, _arglist) _func _arglist __asm("gnuc$tx" #_func)
# elif _G_FLOAT
#  define __gdecl(_func, _arglist) _func _arglist __asm("decc$gx" #_func)
#  define __xdecl(_func, _arglist) _func _arglist __asm("gnuc$gx" #_func)
# else
#  define __gdecl(_func, _arglist) _func _arglist __asm("decc$dx" #_func)
#  define __xdecl(_func, _arglist) _func _arglist __asm("gnuc$dx" #_func)
# endif
#endif

int __gdecl(printw, (char *_format_spec, ...));
int __gdecl(scanw, (char *_format_spec, ...));
int __gdecl(wprintw, (WINDOW *_win, char *_format_spec, ...));
int __gdecl(wscanw, (WINDOW *_win, char *_format_spec, ...));

#ifdef __VMS_CURSES
#define crmode()		((stdkb->_flags &= ~_NOCRMODE), nonl ())
#define nocrmode()		(stdkb->_flags |= _NOCRMODE)
#define getch()			wgetch (stdscr)
#define addch(_ch)		waddch (stdscr, _ch)
#define addstr(_str)		waddstr (stdscr, _str)
#define move(_y, _x)		wmove (stdscr, _y, _x)
#define refresh()		wrefresh (stdscr)
#define clear()			wclear (stdscr)
#define clrtobot()		wclrtobot (stdscr)
#define clrtoeol()		wclrtoeol (stdscr)
#define delch()			wdelch (stdscr)
#define erase()			werase (stdscr)
#define insch(_ch)		winsch (stdscr, _ch)
#define insertln()		winsertln (stdscr)
#define standout()		wstandout (stdscr)
#define standend()		wstandend (stdscr)
#define getstr(_str)		wgetstr (stdscr, _str)
#define inch()			winch (stdscr)
#define setattr(attr)		wsetattr (stdscr, attr)
#define clrattr(attr)		wclrattr (stdscr, attr)
#define deleteln()		wdeleteln (stdscr)
#define insstr(_str)		winsstr (stdscr, _str)
#define mvwaddch(win,_y,_x,_ch)	(wmove(win,_y,_x)==ERR ? ERR : waddch(win,_ch))
#define mvwgetch(win,_y,_x)	(wmove(win,_y,_x)==ERR ? ERR : wgetch(win))
#define mvwinsstr(win,_y,_x,str) (wmove(win,_y,_x)==ERR ? ERR : winsstr(win,str))
#define mvwgetstr(win,_y,_x,str) (wmove(win,_y,_x)==ERR ? ERR : wgetstr(win,str))
#define mvwinch(win,_y,_x)	(wmove(win,_y,_x)==ERR ? ERR : winch(win))
#define mvwdelch(win,_y,_x)	(wmove(win,_y,_x)==ERR ? ERR : wdelch(win))
#define mvwinsch(win,_y,_x,_ch)	(wmove(win,_y,_x)==ERR ? ERR : winsch(win,_ch))
#define mvwdeleteln(win,_y,_x)	(wmove(win,_y,_x)==ERR ? ERR : wdeleteln(win))
#define mvaddch(_y,_x,_ch)	mvwaddch (stdscr, _y, _x, _ch)
#define mvgetch(_y,_x)		mvwgetch (stdscr, _y, _x)
#define mvaddstr(_y,_x,_str)	mvwaddstr (stdscr, _y, _x, _str)
#define mvinsstr(_y,_x,_str)	mvwinsstr (stdscr, _y, _x, _str)
#define mvgetstr(_y,_x,_str)	mvwgetstr (stdscr, _y, _x, _str)
#define mvinch(_y,_x)		mvwinch (stdscr, _y, _x)
#define mvdelch(_y,_x)		mvwdelch (stdscr, _y, _x)
#define mvinsch(_y,_x,_ch)	mvwinsch (stdscr, _y, _x, _ch)
#define mvdeleteln(_y,_x)	mvwdeleteln (stdscr, _y, _x)
#define mvcur(_ly,_lx,_ny,_nx)	wmove (stdscr, _ny, _nx)
#define clearok(win, bf)	(win->_clear = bf)
#define leaveok(win, bf)	(win->_leave = bf)
#define scrollok(win, bf)	(win->_scroll = bf)
#define wrapok(win, bf)		(win->_wrap = bf)
#define flushok(win,bf)		(bf ? win->_flags |= _FLUSH : (win->_flags &= ~_FLUSH))
#define getyx(win,_y,_x)	_y = win->_cur_y, _x = win->_cur_x
#define check(status)		if (!(status & SS$_NORMAL))	\
				  {				\
				     decc$$translate (status);	\
				     return ERR;		\
				}

#else  /* !__VMS_CURSES  */

#define mvwaddch(_win,_y,_x,_ch)	wmove(_win,_y,_x)==ERR?ERR:waddch(_win,_ch)
#define mvwgetch(_win,_y,_x)		wmove(_win,_y,_x)==ERR?ERR:wgetch(_win)
#define mvwaddbytes(_win,_y,_x,_da,_co) wmove(_win,_y,_x)==ERR?ERR:waddbytes(_win,_da,_co)
#define mvwaddstr(_win,_y,_x,_str)	wmove(_win,_y,_x)==ERR?ERR:waddbytes(_win,_str,strlen(_str))
#define mvwgetstr(_win,_y,_x,_str)	wmove(_win,_y,_x)==ERR?ERR:wgetstr(_win,_str)
#define mvwinch(_win,_y,_x)		wmove(_win,_y,_x) == ERR ? ERR : winch(_win)
#define mvwdelch(_win,_y,_x)		wmove(_win,_y,_x) == ERR ? ERR : wdelch(_win)
#define mvwinsch(_win,_y,_x,_c)		wmove(_win,_y,_x) == ERR ? ERR:winsch(_win,_c)
#define mvaddch(_y,_x,_ch)		mvwaddch(stdscr,_y,_x,_ch)
#define mvgetch(_y,_x)			mvwgetch(stdscr,_y,_x)
#define mvaddbytes(_y,_x,_da,_co)	mvwaddbytes(stdscr,_y,_x,_da,_co)
#define mvaddstr(_y,_x,_str)		mvwaddstr(stdscr,_y,_x,_str)
#define mvgetstr(_y,_x,_str)		mvwgetstr(stdscr,_y,_x,_str)
#define mvinch(_y,_x)			mvwinch(stdscr,_y,_x)
#define mvdelch(_y,_x)			mvwdelch(stdscr,_y,_x)
#define mvinsch(_y,_x,_c)		mvwinsch(stdscr,_y,_x,_c)
#define wrapok(_win,_bf)		(_win->_wrap = _bf)
#define clearok(_win,_bf)		(_win->_clear = _bf)
#define leaveok(_win,_bf)		(_win->_leave = _bf)
#define scrollok(_win,_bf)		(_win->_scroll = _bf)
#define flushok(_win,_bf)		(_bf ? (_win->_flags |= _FLUSH):(_win->_flags &= ~_FLUSH))
#define getyx(_win,_y,_x)		_y = _win->_cury, _x = _win->_curx
#define winch(_win)			(_win->_y[_win->_cury][_win->_curx] & 0177)
#define raw()			(stdkb->_flags &= ~_NORAW)
#define noraw()			(stdkb->_flags |= _NORAW)
#define cbreak()		((stdkb->_flags &= ~_NOCRMODE), nonl ())
#define nocbreak()		((stdkb->_flags |= _NOCRMODE), nl())
#define crmode()		cbreak()
#define nocrmode()		nocbreak()
#define savetty()
#define resetty()
int __gdecl(mvprintw, (WINDOW *_win, int _y, int _x, char *_format_spec, ...));
int __gdecl(mvwprintw, (int _y, int _x, char *_format_spec, ...));
int __gdecl(mvscanw, (WINDOW *_win, int _y, int _x, char *_format_spec, ...));
int __gdecl(mvwscanw, (int _y, int _x, char *_format_spec, ...));
int __fdecl(waddbytes, (WINDOW *_win, char *_bytes, int _count));
int __fdecl(waddch, (WINDOW *_win, char _ch));
int __fdecl(waddstr, (WINDOW *_win, char *_str));
int __fdecl(box, (WINDOW *_win, char _vert, char _hor));
int __fdecl(wclear, (WINDOW *_win));
int __fdecl(wclrtobot, (WINDOW *_win));
int __fdecl(wclrtoeol, (WINDOW *_win));
int __fdecl(fgoto, ());
int __fdecl(mvcur, (int _ly, int _lx, int _y, int _x));
int __fdecl(plod, ());
int __fdecl(plodput, ());
int __fdecl(tabcol, ());
int __fdecl(getcap, (char *_name));
int __fdecl(gettmode, (void));
int __fdecl(setterm, (char *_type));
int __fdecl(zap, ());
int __fdecl(wdelch, (WINDOW *_win));
int __fdecl(wdeleteln, (WINDOW *_win));
int __fdecl(delwin, (WINDOW *_win));
int __fdecl(endwin, (void));
int __fdecl(werase, (WINDOW *_win));
int __fdecl(fullname, ());
int __fdecl(wgetch, (WINDOW *_win));
int __fdecl(wgetstr, (WINDOW *_win, char *_str));
int __fdecl(idlok, ());
WINDOW * __fdecl(initscr, (void));
int __fdecl(winsch, (WINDOW *_win, char _ch));
int __fdecl(winsstr, (WINDOW *_win, char *_str));
int __fdecl(winsertln, (WINDOW *_win));
int __fdecl(longname, (char *termbuf, char *_name));
int __fdecl(wmove, (WINDOW *_win, int _y, int _x));
int __fdecl(mvwin, (WINDOW *_win, int _st_row, int _st_col));
WINDOW * __fdecl(newwin, (int _numlines, int _numcols, int _begin_y, int _begin_x));
WINDOW * __fdecl(subwin, (WINDOW *_win, int _numlines, int _numcols, int _begin_y, int _begin_x));
int __fdecl(overlay, (WINDOW *_win1, WINDOW *_win2));
int __fdecl(overwrite, (WINDOW *_win1, WINDOW *_win2));
int __fdecl(makech, ());
int __fdecl(wrefresh, (WINDOW *_win));
int __fdecl(scroll, (WINDOW *_win));
int __fdecl(wstandend, (WINDOW *_win));
int __fdecl(wstandout, (WINDOW *_win));
int __fdecl(tgoto, ());
int __fdecl(touchoverlap, ());
int __fdecl(touchline, ());
int __fdecl(touchwin, (WINDOW *_win));
int __fdecl(tputs, ());
int __fdecl(tstp, ());
int __fdecl(wsetattr, (WINDOW *_win, int _attr));
int __fdecl(wclrattr, (WINDOW *_win, int _attr));
#define setattr(_attr) wsetattr(stdscr, _attr)
#define clrattr(_attr) wclrattr(stdscr, _attr)

#ifdef _V13_000
extern unsigned decc$$scr_is_tty;

#define addch(ch)	(! decc$$scr_is_tty ? ERR : VOID(waddch(stdscr, ch)))
#define getch()		(! decc$$scr_is_tty ? ERR : VOID(wgetch(stdscr)))
#define addbytes(da,co) (! decc$$scr_is_tty ? ERR : VOID(waddbytes(stdscr, da,co)))
#define addstr(str)	(! decc$$scr_is_tty ? ERR : VOID(waddbytes(stdscr, str, strlen(str))))
#define getstr(str)	(! decc$$scr_is_tty ? ERR : VOID(wgetstr(stdscr, str)))
#define move(y, x)	(! decc$$scr_is_tty ? ERR : VOID(wmove(stdscr, y, x)))
#define clear()		(! decc$$scr_is_tty ? ERR : VOID(wclear(stdscr)))
#define erase()		(! decc$$scr_is_tty ? ERR : VOID(werase(stdscr)))
#define clrtobot()	(! decc$$scr_is_tty ? ERR : VOID(wclrtobot(stdscr)))
#define clrtoeol()	(! decc$$scr_is_tty ? ERR : VOID(wclrtoeol(stdscr)))
#define insertln()	(! decc$$scr_is_tty ? ERR : VOID(winsertln(stdscr)))
#define deleteln()	(! decc$$scr_is_tty ? ERR : VOID(wdeleteln(stdscr)))
#define refresh()	(! decc$$scr_is_tty ? ERR : VOID(wrefresh(stdscr)))
#define inch()		(! decc$$scr_is_tty ? ERR : VOID(winch(stdscr)))
#define insch(c)	(! decc$$scr_is_tty ? ERR : VOID(winsch(stdscr,c)))
#define delch()		(! decc$$scr_is_tty ? ERR : VOID(wdelch(stdscr)))
#define standout()	(! decc$$scr_is_tty ? ERR : VOID(wstandout(stdscr)))
#define standend()	(! decc$$scr_is_tty ? ERR : VOID(wstandend(stdscr)))
#define insstr(c)	(! decc$$scr_is_tty ? ERR : VOID(winsstr(stdscr,c)))

#else

#define addch(_ch)	waddch(stdscr, ch)
#define getch()		wgetch(stdscr)
#define addbytes(_da,_co) waddbytes(stdscr, da,co)
#define addstr(_str)	waddbytes(stdscr, str, strlen(str))
#define getstr(_str)	wgetstr(stdscr, str)
#define move(_y, _x)	wmove(stdscr, y, x)
#define clear()		wclear(stdscr)
#define erase()		werase(stdscr)
#define clrtobot()	wclrtobot(stdscr)
#define clrtoeol()	wclrtoeol(stdscr)
#define insertln()	winsertln(stdscr)
#define deleteln()	wdeleteln(stdscr)
#define refresh()	wrefresh(stdscr)
#define inch()		winch(stdscr)
#define insch(_c)	winsch(stdscr,c)
#define delch()		wdelch(stdscr)
#define standout()	wstandout(stdscr)
#define standend()	wstandend(stdscr)
#define insstr(_c)	winsstr(stdscr,c)

#endif /* !_V13_000  */

#endif /* ! __VMS_CURSES  */


#define echo()			(stdkb->_flags &= ~_NOECHO)
#define noecho()		(stdkb->_flags |= _NOECHO)
#define raw()			(stdkb->_flags &= ~_NORAW)
#define noraw()			(stdkb->_flags |= _NORAW)
#define nl()			(stdkb->_flags &= ~_NONL)
#define nonl()			(stdkb->_flags |= _NONL)


#ifdef _cplusplus
  }
#endif

#endif	/* _CURSES_H */
