1
0

mouse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /* PDCurses */
  2. #include <curspriv.h>
  3. /*man-start**************************************************************
  4. mouse
  5. -----
  6. ### Synopsis
  7. int mouse_set(mmask_t mbe);
  8. int mouse_on(mmask_t mbe);
  9. int mouse_off(mmask_t mbe);
  10. int request_mouse_pos(void);
  11. void wmouse_position(WINDOW *win, int *y, int *x);
  12. mmask_t getmouse(void);
  13. int mouseinterval(int wait);
  14. bool wenclose(const WINDOW *win, int y, int x);
  15. bool wmouse_trafo(const WINDOW *win, int *y, int *x, bool to_screen);
  16. bool mouse_trafo(int *y, int *x, bool to_screen);
  17. mmask_t mousemask(mmask_t mask, mmask_t *oldmask);
  18. int nc_getmouse(MEVENT *event);
  19. int ungetmouse(MEVENT *event);
  20. bool has_mouse(void);
  21. ### Description
  22. As of PDCurses 3.0, there are two separate mouse interfaces: the
  23. classic interface, which is based on the undocumented Sys V mouse
  24. functions; and an ncurses-compatible interface. Both are active at
  25. all times, and you can mix and match functions from each, though it's
  26. not recommended. The ncurses interface is essentially an emulation
  27. layer built on top of the classic interface; it's here to allow
  28. easier porting of ncurses apps.
  29. The classic interface: mouse_set(), mouse_on(), mouse_off(),
  30. request_mouse_pos(), wmouse_position(), and getmouse(). An
  31. application using this interface would start by calling mouse_set()
  32. or mouse_on() with a non-zero value, often ALL_MOUSE_EVENTS. Then it
  33. would check for a KEY_MOUSE return from getch(). If found, it would
  34. call request_mouse_pos() to get the current mouse status.
  35. mouse_set(), mouse_on() and mouse_off() are analagous to attrset(),
  36. attron() and attroff(). These functions set the mouse button events
  37. to trap. The button masks used in these functions are defined in
  38. curses.h and can be or'ed together. They are the group of masks
  39. starting with BUTTON1_RELEASED.
  40. request_mouse_pos() requests curses to fill in the Mouse_status
  41. structure with the current state of the mouse.
  42. wmouse_position() determines if the current mouse position is within
  43. the window passed as an argument. If the mouse is outside the current
  44. window, -1 is returned in the y and x arguments; otherwise the y and
  45. x coordinates of the mouse (relative to the top left corner of the
  46. window) are returned in y and x.
  47. getmouse() returns the current status of the trapped mouse buttons as
  48. set by mouse_set() or mouse_on().
  49. The ncurses interface: mouseinterval(), wenclose(), wmouse_trafo(),
  50. mouse_trafo(), mousemask(), nc_getmouse(), ungetmouse() and
  51. has_mouse(). A typical application using this interface would start
  52. by calling mousemask() with a non-zero value, often ALL_MOUSE_EVENTS.
  53. Then it would check for a KEY_MOUSE return from getch(). If found, it
  54. would call nc_getmouse() to get the current mouse status.
  55. mouseinterval() sets the timeout for a mouse click. On all current
  56. platforms, PDCurses receives mouse button press and release events,
  57. but must synthesize click events. It does this by checking whether a
  58. release event is queued up after a press event. If it gets a press
  59. event, and there are no more events waiting, it will wait for the
  60. timeout interval, then check again for a release. A press followed by
  61. a release is reported as BUTTON_CLICKED; otherwise it's passed
  62. through as BUTTON_PRESSED. The default timeout is 150ms; valid values
  63. are 0 (no clicks reported) through 1000ms. In x11, the timeout can
  64. also be set via the clickPeriod resource. The return value from
  65. mouseinterval() is the old timeout. To check the old value without
  66. setting a new one, call it with a parameter of -1. Note that although
  67. there's no classic equivalent for this function (apart from the
  68. clickPeriod resource), the value set applies in both interfaces.
  69. wenclose() reports whether the given screen-relative y, x coordinates
  70. fall within the given window.
  71. wmouse_trafo() converts between screen-relative and window-relative
  72. coordinates. A to_screen parameter of TRUE means to convert from
  73. window to screen; otherwise the reverse. The function returns FALSE
  74. if the coordinates aren't within the window, or if any of the
  75. parameters are NULL. The coordinates have been converted when the
  76. function returns TRUE.
  77. mouse_trafo() is the stdscr version of wmouse_trafo().
  78. mousemask() is nearly equivalent to mouse_set(), but instead of
  79. OK/ERR, it returns the value of the mask after setting it. (This
  80. isn't necessarily the same value passed in, since the mask could be
  81. altered on some platforms.) And if the second parameter is a non-null
  82. pointer, mousemask() stores the previous mask value there. Also,
  83. since the ncurses interface doesn't work with PDCurses' BUTTON_MOVED
  84. events, mousemask() filters them out.
  85. nc_getmouse() returns the current mouse status in an MEVENT struct.
  86. This is equivalent to ncurses' getmouse(), renamed to avoid conflict
  87. with PDCurses' getmouse(). But if you define PDC_NCMOUSE before
  88. including curses.h, it defines getmouse() to nc_getmouse(), along
  89. with a few other redefintions needed for compatibility with ncurses
  90. code. nc_getmouse() calls request_mouse_pos(), which (not getmouse())
  91. is the classic equivalent.
  92. ungetmouse() is the mouse equivalent of ungetch(). However, PDCurses
  93. doesn't maintain a queue of mouse events; only one can be pushed
  94. back, and it can overwrite or be overwritten by real mouse events.
  95. has_mouse() reports whether the mouse is available at all on the
  96. current platform.
  97. ### Portability
  98. X/Open ncurses NetBSD
  99. mouse_set - - -
  100. mouse_on - - -
  101. mouse_off - - -
  102. request_mouse_pos - - -
  103. wmouse_position - - -
  104. getmouse - * -
  105. mouseinterval - Y -
  106. wenclose - Y -
  107. wmouse_trafo - Y -
  108. mouse_trafo - Y -
  109. mousemask - Y -
  110. nc_getmouse - * -
  111. ungetmouse - Y -
  112. has_mouse - Y -
  113. * See above, under Description
  114. **man-end****************************************************************/
  115. #include <string.h>
  116. static bool ungot = FALSE;
  117. int mouse_set(mmask_t mbe)
  118. {
  119. PDC_LOG(("mouse_set() - called: event %x\n", mbe));
  120. if (!SP)
  121. return ERR;
  122. SP->_trap_mbe = mbe;
  123. return PDC_mouse_set();
  124. }
  125. int mouse_on(mmask_t mbe)
  126. {
  127. PDC_LOG(("mouse_on() - called: event %x\n", mbe));
  128. if (!SP)
  129. return ERR;
  130. SP->_trap_mbe |= mbe;
  131. return PDC_mouse_set();
  132. }
  133. int mouse_off(mmask_t mbe)
  134. {
  135. PDC_LOG(("mouse_off() - called: event %x\n", mbe));
  136. if (!SP)
  137. return ERR;
  138. SP->_trap_mbe &= ~mbe;
  139. return PDC_mouse_set();
  140. }
  141. int request_mouse_pos(void)
  142. {
  143. PDC_LOG(("request_mouse_pos() - called\n"));
  144. Mouse_status = SP->mouse_status;
  145. return OK;
  146. }
  147. void wmouse_position(WINDOW *win, int *y, int *x)
  148. {
  149. PDC_LOG(("wmouse_position() - called\n"));
  150. if (win && wenclose(win, MOUSE_Y_POS, MOUSE_X_POS))
  151. {
  152. if (y)
  153. *y = MOUSE_Y_POS - win->_begy;
  154. if (x)
  155. *x = MOUSE_X_POS - win->_begx;
  156. }
  157. else
  158. {
  159. if (y)
  160. *y = -1;
  161. if (x)
  162. *x = -1;
  163. }
  164. }
  165. mmask_t getmouse(void)
  166. {
  167. PDC_LOG(("getmouse() - called\n"));
  168. return SP ? SP->_trap_mbe : (mmask_t)0;
  169. }
  170. /* ncurses mouse interface */
  171. int mouseinterval(int wait)
  172. {
  173. int old_wait;
  174. PDC_LOG(("mouseinterval() - called: %d\n", wait));
  175. if (!SP)
  176. return ERR;
  177. old_wait = SP->mouse_wait;
  178. if (wait >= 0 && wait <= 1000)
  179. SP->mouse_wait = wait;
  180. return old_wait;
  181. }
  182. bool wenclose(const WINDOW *win, int y, int x)
  183. {
  184. PDC_LOG(("wenclose() - called: %p %d %d\n", win, y, x));
  185. return (win && y >= win->_begy && y < win->_begy + win->_maxy
  186. && x >= win->_begx && x < win->_begx + win->_maxx);
  187. }
  188. bool wmouse_trafo(const WINDOW *win, int *y, int *x, bool to_screen)
  189. {
  190. int newy, newx;
  191. PDC_LOG(("wmouse_trafo() - called\n"));
  192. if (!win || !y || !x)
  193. return FALSE;
  194. newy = *y;
  195. newx = *x;
  196. if (to_screen)
  197. {
  198. newy += win->_begy;
  199. newx += win->_begx;
  200. if (!wenclose(win, newy, newx))
  201. return FALSE;
  202. }
  203. else
  204. {
  205. if (wenclose(win, newy, newx))
  206. {
  207. newy -= win->_begy;
  208. newx -= win->_begx;
  209. }
  210. else
  211. return FALSE;
  212. }
  213. *y = newy;
  214. *x = newx;
  215. return TRUE;
  216. }
  217. bool mouse_trafo(int *y, int *x, bool to_screen)
  218. {
  219. PDC_LOG(("mouse_trafo() - called\n"));
  220. return wmouse_trafo(stdscr, y, x, to_screen);
  221. }
  222. mmask_t mousemask(mmask_t mask, mmask_t *oldmask)
  223. {
  224. PDC_LOG(("mousemask() - called\n"));
  225. if (!SP)
  226. return (mmask_t)0;
  227. if (oldmask)
  228. *oldmask = SP->_trap_mbe;
  229. /* The ncurses interface doesn't work with our move events, so
  230. filter them here */
  231. mask &= ~(BUTTON1_MOVED | BUTTON2_MOVED | BUTTON3_MOVED);
  232. mouse_set(mask);
  233. return SP->_trap_mbe;
  234. }
  235. int nc_getmouse(MEVENT *event)
  236. {
  237. int i;
  238. mmask_t bstate = 0;
  239. PDC_LOG(("nc_getmouse() - called\n"));
  240. if (!event || !SP)
  241. return ERR;
  242. ungot = FALSE;
  243. request_mouse_pos();
  244. event->id = 0;
  245. event->x = Mouse_status.x;
  246. event->y = Mouse_status.y;
  247. event->z = 0;
  248. for (i = 0; i < 3; i++)
  249. {
  250. if (Mouse_status.changes & (1 << i))
  251. {
  252. int shf = i * 5;
  253. short button = Mouse_status.button[i] & BUTTON_ACTION_MASK;
  254. if (button == BUTTON_RELEASED)
  255. bstate |= (BUTTON1_RELEASED << shf);
  256. else if (button == BUTTON_PRESSED)
  257. bstate |= (BUTTON1_PRESSED << shf);
  258. else if (button == BUTTON_CLICKED)
  259. bstate |= (BUTTON1_CLICKED << shf);
  260. else if (button == BUTTON_DOUBLE_CLICKED)
  261. bstate |= (BUTTON1_DOUBLE_CLICKED << shf);
  262. button = Mouse_status.button[i] & BUTTON_MODIFIER_MASK;
  263. if (button & PDC_BUTTON_SHIFT)
  264. bstate |= BUTTON_MODIFIER_SHIFT;
  265. if (button & PDC_BUTTON_CONTROL)
  266. bstate |= BUTTON_MODIFIER_CONTROL;
  267. if (button & PDC_BUTTON_ALT)
  268. bstate |= BUTTON_MODIFIER_ALT;
  269. }
  270. }
  271. if (MOUSE_WHEEL_UP)
  272. bstate |= BUTTON4_PRESSED;
  273. else if (MOUSE_WHEEL_DOWN)
  274. bstate |= BUTTON5_PRESSED;
  275. /* extra filter pass -- mainly for button modifiers */
  276. event->bstate = bstate & SP->_trap_mbe;
  277. return OK;
  278. }
  279. int ungetmouse(MEVENT *event)
  280. {
  281. int i;
  282. mmask_t bstate;
  283. PDC_LOG(("ungetmouse() - called\n"));
  284. if (!event || ungot)
  285. return ERR;
  286. ungot = TRUE;
  287. SP->mouse_status.x = event->x;
  288. SP->mouse_status.y = event->y;
  289. SP->mouse_status.changes = 0;
  290. bstate = event->bstate;
  291. for (i = 0; i < 3; i++)
  292. {
  293. int shf = i * 5;
  294. short button = 0;
  295. if (bstate & ((BUTTON1_RELEASED | BUTTON1_PRESSED |
  296. BUTTON1_CLICKED | BUTTON1_DOUBLE_CLICKED) << shf))
  297. {
  298. SP->mouse_status.changes |= 1 << i;
  299. if (bstate & (BUTTON1_PRESSED << shf))
  300. button = BUTTON_PRESSED;
  301. if (bstate & (BUTTON1_CLICKED << shf))
  302. button = BUTTON_CLICKED;
  303. if (bstate & (BUTTON1_DOUBLE_CLICKED << shf))
  304. button = BUTTON_DOUBLE_CLICKED;
  305. if (bstate & BUTTON_MODIFIER_SHIFT)
  306. button |= PDC_BUTTON_SHIFT;
  307. if (bstate & BUTTON_MODIFIER_CONTROL)
  308. button |= PDC_BUTTON_CONTROL;
  309. if (bstate & BUTTON_MODIFIER_ALT)
  310. button |= PDC_BUTTON_ALT;
  311. }
  312. SP->mouse_status.button[i] = button;
  313. }
  314. if (bstate & BUTTON4_PRESSED)
  315. SP->mouse_status.changes |= PDC_MOUSE_WHEEL_UP;
  316. else if (bstate & BUTTON5_PRESSED)
  317. SP->mouse_status.changes |= PDC_MOUSE_WHEEL_DOWN;
  318. return PDC_ungetch(KEY_MOUSE);
  319. }
  320. bool has_mouse(void)
  321. {
  322. return PDC_has_mouse();
  323. }