#include <curspriv.h>
#define _INBUFSIZ 512
#define NUNGETCH 256
static int c_pindex = 0;
static int c_gindex = 1;
static int c_ungind = 0;
static int c_ungch[NUNGETCH];
static int _mouse_key(WINDOW *win)
{
int i, key = KEY_MOUSE;
unsigned long mbe = SP->_trap_mbe;
for (i = 0; i < 3; i++)
{
if (pdc_mouse_status.changes & (1 << i))
{
int shf = i * 5;
short button = pdc_mouse_status.button[i] & BUTTON_ACTION_MASK;
if ( (!(mbe & (BUTTON1_PRESSED << shf)) &&
(button == BUTTON_PRESSED))
|| (!(mbe & (BUTTON1_CLICKED << shf)) &&
(button == BUTTON_CLICKED))
|| (!(mbe & (BUTTON1_DOUBLE_CLICKED << shf)) &&
(button == BUTTON_DOUBLE_CLICKED))
|| (!(mbe & (BUTTON1_TRIPLE_CLICKED << shf)) &&
(button == BUTTON_TRIPLE_CLICKED))
|| (!(mbe & (BUTTON1_MOVED << shf)) &&
(button == BUTTON_MOVED))
|| (!(mbe & (BUTTON1_RELEASED << shf)) &&
(button == BUTTON_RELEASED))
)
pdc_mouse_status.changes ^= (1 << i);
}
}
if (pdc_mouse_status.changes & PDC_MOUSE_MOVED)
{
if (!(mbe & (BUTTON1_MOVED|BUTTON2_MOVED|BUTTON3_MOVED)))
pdc_mouse_status.changes ^= PDC_MOUSE_MOVED;
}
if (pdc_mouse_status.changes &
(PDC_MOUSE_WHEEL_UP|PDC_MOUSE_WHEEL_DOWN))
{
if (!(mbe & MOUSE_WHEEL_SCROLL))
pdc_mouse_status.changes &=
~(PDC_MOUSE_WHEEL_UP|PDC_MOUSE_WHEEL_DOWN);
}
if (!pdc_mouse_status.changes)
return -1;
i = PDC_mouse_in_slk(pdc_mouse_status.y, pdc_mouse_status.x);
if (i)
{
if (pdc_mouse_status.button[0] & (BUTTON_PRESSED|BUTTON_CLICKED))
key = KEY_F(i);
else
key = -1;
}
return key;
}
int wgetch(WINDOW *win)
{
static int buffer[_INBUFSIZ];
int key, waitcount;
PDC_LOG(("wgetch() - called\n"));
if (!win)
return ERR;
waitcount = 0;
if (SP->delaytenths)
waitcount = 2 * SP->delaytenths;
else
if (win->_delayms)
{
waitcount = win->_delayms / 50;
if (!waitcount)
waitcount = 1;
}
if (!(win->_flags & _PAD) && ((!win->_leaveit &&
(win->_begx + win->_curx != SP->curscol ||
win->_begy + win->_cury != SP->cursrow)) || is_wintouched(win)))
wrefresh(win);
if (c_ungind)
return c_ungch[--c_ungind];
if ((!SP->raw_inp && !SP->cbreak) && (c_gindex < c_pindex))
return buffer[c_gindex++];
c_pindex = 0;
c_gindex = 0;
for (;;)
{
if (!PDC_check_key())
{
if (SP->delaytenths || win->_delayms)
{
if (!waitcount)
return ERR;
waitcount--;
}
else
if (win->_nodelay)
return ERR;
napms(50);
continue;
}
key = PDC_get_key();
if (SP->key_code)
{
if (!win->_use_keypad)
key = -1;
else if (key == KEY_MOUSE)
key = _mouse_key(win);
}
if (key == -1)
continue;
if (key == '\r' && SP->autocr && !SP->raw_inp)
key = '\n';
if (SP->echo && !SP->key_code)
{
waddch(win, key);
wrefresh(win);
}
if (SP->raw_inp || SP->cbreak)
return key;
if (key == '\b')
{
if (c_pindex > c_gindex)
c_pindex--;
}
else
if (c_pindex < _INBUFSIZ - 2)
buffer[c_pindex++] = key;
if (key == '\n' || key == '\r')
return buffer[c_gindex++];
}
}
int mvgetch(int y, int x)
{
PDC_LOG(("mvgetch() - called\n"));
if (move(y, x) == ERR)
return ERR;
return wgetch(stdscr);
}
int mvwgetch(WINDOW *win, int y, int x)
{
PDC_LOG(("mvwgetch() - called\n"));
if (wmove(win, y, x) == ERR)
return ERR;
return wgetch(win);
}
int PDC_ungetch(int ch)
{
PDC_LOG(("ungetch() - called\n"));
if (c_ungind >= NUNGETCH)
return ERR;
c_ungch[c_ungind++] = ch;
return OK;
}
int flushinp(void)
{
PDC_LOG(("flushinp() - called\n"));
PDC_flushinp();
c_gindex = 1;
c_pindex = 0;
c_ungind = 0;
return OK;
}
unsigned long PDC_get_key_modifiers(void)
{
PDC_LOG(("PDC_get_key_modifiers() - called\n"));
return pdc_key_modifiers;
}
int PDC_save_key_modifiers(bool flag)
{
PDC_LOG(("PDC_save_key_modifiers() - called\n"));
SP->save_key_modifiers = flag;
return OK;
}
int PDC_return_key_modifiers(bool flag)
{
PDC_LOG(("PDC_return_key_modifiers() - called\n"));
SP->return_key_modifiers = flag;
return PDC_modifiers_set();
}
#ifdef PDC_WIDE
int wget_wch(WINDOW *win, wint_t *wch)
{
int key;
PDC_LOG(("wget_wch() - called\n"));
if (!wch)
return ERR;
key = wgetch(win);
if (key == ERR)
return ERR;
*wch = key;
return SP->key_code ? KEY_CODE_YES : OK;
}
int get_wch(wint_t *wch)
{
PDC_LOG(("get_wch() - called\n"));
return wget_wch(stdscr, wch);
}
int mvget_wch(int y, int x, wint_t *wch)
{
PDC_LOG(("mvget_wch() - called\n"));
if (move(y, x) == ERR)
return ERR;
return wget_wch(stdscr, wch);
}
int mvwget_wch(WINDOW *win, int y, int x, wint_t *wch)
{
PDC_LOG(("mvwget_wch() - called\n"));
if (wmove(win, y, x) == ERR)
return ERR;
return wget_wch(win, wch);
}
int unget_wch(const wchar_t wch)
{
return PDC_ungetch(wch);
}
#endif