/* PDCurses */12#include <curspriv.h>34/*man-start**************************************************************56mouse7-----89### Synopsis1011int mouse_set(mmask_t mbe);12int mouse_on(mmask_t mbe);13int mouse_off(mmask_t mbe);14int request_mouse_pos(void);15void wmouse_position(WINDOW *win, int *y, int *x);16mmask_t getmouse(void);1718int mouseinterval(int wait);19bool wenclose(const WINDOW *win, int y, int x);20bool wmouse_trafo(const WINDOW *win, int *y, int *x, bool to_screen);21bool mouse_trafo(int *y, int *x, bool to_screen);22mmask_t mousemask(mmask_t mask, mmask_t *oldmask);23int nc_getmouse(MEVENT *event);24int ungetmouse(MEVENT *event);25bool has_mouse(void);2627### Description2829As of PDCurses 3.0, there are two separate mouse interfaces: the30classic interface, which is based on the undocumented Sys V mouse31functions; and an ncurses-compatible interface. Both are active at32all times, and you can mix and match functions from each, though it's33not recommended. The ncurses interface is essentially an emulation34layer built on top of the classic interface; it's here to allow35easier porting of ncurses apps.3637The classic interface: mouse_set(), mouse_on(), mouse_off(),38request_mouse_pos(), wmouse_position(), and getmouse(). An39application using this interface would start by calling mouse_set()40or mouse_on() with a non-zero value, often ALL_MOUSE_EVENTS. Then it41would check for a KEY_MOUSE return from getch(). If found, it would42call request_mouse_pos() to get the current mouse status.4344mouse_set(), mouse_on() and mouse_off() are analagous to attrset(),45attron() and attroff(). These functions set the mouse button events46to trap. The button masks used in these functions are defined in47curses.h and can be or'ed together. They are the group of masks48starting with BUTTON1_RELEASED.4950request_mouse_pos() requests curses to fill in the Mouse_status51structure with the current state of the mouse.5253wmouse_position() determines if the current mouse position is within54the window passed as an argument. If the mouse is outside the current55window, -1 is returned in the y and x arguments; otherwise the y and56x coordinates of the mouse (relative to the top left corner of the57window) are returned in y and x.5859getmouse() returns the current status of the trapped mouse buttons as60set by mouse_set() or mouse_on().6162The ncurses interface: mouseinterval(), wenclose(), wmouse_trafo(),63mouse_trafo(), mousemask(), nc_getmouse(), ungetmouse() and64has_mouse(). A typical application using this interface would start65by calling mousemask() with a non-zero value, often ALL_MOUSE_EVENTS.66Then it would check for a KEY_MOUSE return from getch(). If found, it67would call nc_getmouse() to get the current mouse status.6869mouseinterval() sets the timeout for a mouse click. On all current70platforms, PDCurses receives mouse button press and release events,71but must synthesize click events. It does this by checking whether a72release event is queued up after a press event. If it gets a press73event, and there are no more events waiting, it will wait for the74timeout interval, then check again for a release. A press followed by75a release is reported as BUTTON_CLICKED; otherwise it's passed76through as BUTTON_PRESSED. The default timeout is 150ms; valid values77are 0 (no clicks reported) through 1000ms. In x11, the timeout can78also be set via the clickPeriod resource. The return value from79mouseinterval() is the old timeout. To check the old value without80setting a new one, call it with a parameter of -1. Note that although81there's no classic equivalent for this function (apart from the82clickPeriod resource), the value set applies in both interfaces.8384wenclose() reports whether the given screen-relative y, x coordinates85fall within the given window.8687wmouse_trafo() converts between screen-relative and window-relative88coordinates. A to_screen parameter of TRUE means to convert from89window to screen; otherwise the reverse. The function returns FALSE90if the coordinates aren't within the window, or if any of the91parameters are NULL. The coordinates have been converted when the92function returns TRUE.9394mouse_trafo() is the stdscr version of wmouse_trafo().9596mousemask() is nearly equivalent to mouse_set(), but instead of97OK/ERR, it returns the value of the mask after setting it. (This98isn't necessarily the same value passed in, since the mask could be99altered on some platforms.) And if the second parameter is a non-null100pointer, mousemask() stores the previous mask value there. Also,101since the ncurses interface doesn't work with PDCurses' BUTTON_MOVED102events, mousemask() filters them out.103104nc_getmouse() returns the current mouse status in an MEVENT struct.105This is equivalent to ncurses' getmouse(), renamed to avoid conflict106with PDCurses' getmouse(). But if you define PDC_NCMOUSE before107including curses.h, it defines getmouse() to nc_getmouse(), along108with a few other redefintions needed for compatibility with ncurses109code. nc_getmouse() calls request_mouse_pos(), which (not getmouse())110is the classic equivalent.111112ungetmouse() is the mouse equivalent of ungetch(). However, PDCurses113doesn't maintain a queue of mouse events; only one can be pushed114back, and it can overwrite or be overwritten by real mouse events.115116has_mouse() reports whether the mouse is available at all on the117current platform.118119### Portability120X/Open ncurses NetBSD121mouse_set - - -122mouse_on - - -123mouse_off - - -124request_mouse_pos - - -125wmouse_position - - -126getmouse - * -127mouseinterval - Y -128wenclose - Y -129wmouse_trafo - Y -130mouse_trafo - Y -131mousemask - Y -132nc_getmouse - * -133ungetmouse - Y -134has_mouse - Y -135136* See above, under Description137138**man-end****************************************************************/139140#include <string.h>141142static bool ungot = FALSE;143144int mouse_set(mmask_t mbe)145{146PDC_LOG(("mouse_set() - called: event %x\n", mbe));147148if (!SP)149return ERR;150151SP->_trap_mbe = mbe;152return PDC_mouse_set();153}154155int mouse_on(mmask_t mbe)156{157PDC_LOG(("mouse_on() - called: event %x\n", mbe));158159if (!SP)160return ERR;161162SP->_trap_mbe |= mbe;163return PDC_mouse_set();164}165166int mouse_off(mmask_t mbe)167{168PDC_LOG(("mouse_off() - called: event %x\n", mbe));169170if (!SP)171return ERR;172173SP->_trap_mbe &= ~mbe;174return PDC_mouse_set();175}176177int request_mouse_pos(void)178{179PDC_LOG(("request_mouse_pos() - called\n"));180181Mouse_status = SP->mouse_status;182183return OK;184}185186void wmouse_position(WINDOW *win, int *y, int *x)187{188PDC_LOG(("wmouse_position() - called\n"));189190if (win && wenclose(win, MOUSE_Y_POS, MOUSE_X_POS))191{192if (y)193*y = MOUSE_Y_POS - win->_begy;194if (x)195*x = MOUSE_X_POS - win->_begx;196}197else198{199if (y)200*y = -1;201if (x)202*x = -1;203}204}205206mmask_t getmouse(void)207{208PDC_LOG(("getmouse() - called\n"));209210return SP ? SP->_trap_mbe : (mmask_t)0;211}212213/* ncurses mouse interface */214215int mouseinterval(int wait)216{217int old_wait;218219PDC_LOG(("mouseinterval() - called: %d\n", wait));220221if (!SP)222return ERR;223224old_wait = SP->mouse_wait;225226if (wait >= 0 && wait <= 1000)227SP->mouse_wait = wait;228229return old_wait;230}231232bool wenclose(const WINDOW *win, int y, int x)233{234PDC_LOG(("wenclose() - called: %p %d %d\n", win, y, x));235236return (win && y >= win->_begy && y < win->_begy + win->_maxy237&& x >= win->_begx && x < win->_begx + win->_maxx);238}239240bool wmouse_trafo(const WINDOW *win, int *y, int *x, bool to_screen)241{242int newy, newx;243244PDC_LOG(("wmouse_trafo() - called\n"));245246if (!win || !y || !x)247return FALSE;248249newy = *y;250newx = *x;251252if (to_screen)253{254newy += win->_begy;255newx += win->_begx;256257if (!wenclose(win, newy, newx))258return FALSE;259}260else261{262if (wenclose(win, newy, newx))263{264newy -= win->_begy;265newx -= win->_begx;266}267else268return FALSE;269}270271*y = newy;272*x = newx;273274return TRUE;275}276277bool mouse_trafo(int *y, int *x, bool to_screen)278{279PDC_LOG(("mouse_trafo() - called\n"));280281return wmouse_trafo(stdscr, y, x, to_screen);282}283284mmask_t mousemask(mmask_t mask, mmask_t *oldmask)285{286PDC_LOG(("mousemask() - called\n"));287288if (!SP)289return (mmask_t)0;290291if (oldmask)292*oldmask = SP->_trap_mbe;293294/* The ncurses interface doesn't work with our move events, so295filter them here */296297mask &= ~(BUTTON1_MOVED | BUTTON2_MOVED | BUTTON3_MOVED);298299mouse_set(mask);300301return SP->_trap_mbe;302}303304int nc_getmouse(MEVENT *event)305{306int i;307mmask_t bstate = 0;308309PDC_LOG(("nc_getmouse() - called\n"));310311if (!event || !SP)312return ERR;313314ungot = FALSE;315316request_mouse_pos();317318event->id = 0;319320event->x = Mouse_status.x;321event->y = Mouse_status.y;322event->z = 0;323324for (i = 0; i < 3; i++)325{326if (Mouse_status.changes & (1 << i))327{328int shf = i * 5;329short button = Mouse_status.button[i] & BUTTON_ACTION_MASK;330331if (button == BUTTON_RELEASED)332bstate |= (BUTTON1_RELEASED << shf);333else if (button == BUTTON_PRESSED)334bstate |= (BUTTON1_PRESSED << shf);335else if (button == BUTTON_CLICKED)336bstate |= (BUTTON1_CLICKED << shf);337else if (button == BUTTON_DOUBLE_CLICKED)338bstate |= (BUTTON1_DOUBLE_CLICKED << shf);339340button = Mouse_status.button[i] & BUTTON_MODIFIER_MASK;341342if (button & PDC_BUTTON_SHIFT)343bstate |= BUTTON_MODIFIER_SHIFT;344if (button & PDC_BUTTON_CONTROL)345bstate |= BUTTON_MODIFIER_CONTROL;346if (button & PDC_BUTTON_ALT)347bstate |= BUTTON_MODIFIER_ALT;348}349}350351if (MOUSE_WHEEL_UP)352bstate |= BUTTON4_PRESSED;353else if (MOUSE_WHEEL_DOWN)354bstate |= BUTTON5_PRESSED;355356/* extra filter pass -- mainly for button modifiers */357358event->bstate = bstate & SP->_trap_mbe;359360return OK;361}362363int ungetmouse(MEVENT *event)364{365int i;366mmask_t bstate;367368PDC_LOG(("ungetmouse() - called\n"));369370if (!event || ungot)371return ERR;372373ungot = TRUE;374375SP->mouse_status.x = event->x;376SP->mouse_status.y = event->y;377378SP->mouse_status.changes = 0;379bstate = event->bstate;380381for (i = 0; i < 3; i++)382{383int shf = i * 5;384short button = 0;385386if (bstate & ((BUTTON1_RELEASED | BUTTON1_PRESSED |387BUTTON1_CLICKED | BUTTON1_DOUBLE_CLICKED) << shf))388{389SP->mouse_status.changes |= 1 << i;390391if (bstate & (BUTTON1_PRESSED << shf))392button = BUTTON_PRESSED;393if (bstate & (BUTTON1_CLICKED << shf))394button = BUTTON_CLICKED;395if (bstate & (BUTTON1_DOUBLE_CLICKED << shf))396button = BUTTON_DOUBLE_CLICKED;397398if (bstate & BUTTON_MODIFIER_SHIFT)399button |= PDC_BUTTON_SHIFT;400if (bstate & BUTTON_MODIFIER_CONTROL)401button |= PDC_BUTTON_CONTROL;402if (bstate & BUTTON_MODIFIER_ALT)403button |= PDC_BUTTON_ALT;404}405406SP->mouse_status.button[i] = button;407}408409if (bstate & BUTTON4_PRESSED)410SP->mouse_status.changes |= PDC_MOUSE_WHEEL_UP;411else if (bstate & BUTTON5_PRESSED)412SP->mouse_status.changes |= PDC_MOUSE_WHEEL_DOWN;413414return PDC_ungetch(KEY_MOUSE);415}416417bool has_mouse(void)418{419return PDC_has_mouse();420}421422423