You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EmulateMB.cxx 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* Copyright 2020 Alex Tanskanen for Cendio AB
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. /*
  19. * Based on xf86-input-evdev
  20. *
  21. * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
  22. * Copyright 1993 by David Dawes <dawes@xfree86.org>
  23. * Copyright 2002 by SuSE Linux AG, Author: Egbert Eich
  24. * Copyright 1994-2002 by The XFree86 Project, Inc.
  25. * Copyright 2002 by Paul Elliott
  26. * (Ported from xf86-input-mouse, above copyrights taken from there)
  27. * Copyright © 2008 University of South Australia
  28. *
  29. * Permission to use, copy, modify, distribute, and sell this software
  30. * and its documentation for any purpose is hereby granted without
  31. * fee, provided that the above copyright notice appear in all copies
  32. * and that both that copyright notice and this permission notice
  33. * appear in supporting documentation, and that the name of the authors
  34. * not be used in advertising or publicity pertaining to distribution of the
  35. * software without specific, written prior permission. The authors make no
  36. * representations about the suitability of this software for any
  37. * purpose. It is provided "as is" without express or implied
  38. * warranty.
  39. *
  40. * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  41. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
  42. * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  43. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  44. * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  45. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  46. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  47. *
  48. */
  49. #ifdef HAVE_CONFIG_H
  50. #include <config.h>
  51. #endif
  52. #include <assert.h>
  53. #include <rfb/Exception.h>
  54. #include "parameters.h"
  55. #include "i18n.h"
  56. #include "EmulateMB.h"
  57. /*
  58. * Lets create a simple finite-state machine for 3 button emulation:
  59. *
  60. * We track buttons 1 and 3 (left and right). There are 11 states:
  61. * 0 ground - initial state
  62. * 1 delayed left - left pressed, waiting for right
  63. * 2 delayed right - right pressed, waiting for left
  64. * 3 pressed middle - right and left pressed, emulated middle sent
  65. * 4 pressed left - left pressed and sent
  66. * 5 pressed right - right pressed and sent
  67. * 6 released left - left released after emulated middle
  68. * 7 released right - right released after emulated middle
  69. * 8 repressed left - left pressed after released left
  70. * 9 repressed right - right pressed after released right
  71. * 10 pressed both - both pressed, not emulating middle
  72. *
  73. * At each state, we need handlers for the following events
  74. * 0: no buttons down
  75. * 1: left button down
  76. * 2: right button down
  77. * 3: both buttons down
  78. * 4: emulate3Timeout passed without a button change
  79. * Note that button events are not deltas, they are the set of buttons being
  80. * pressed now. It's possible (ie, mouse hardware does it) to go from (eg)
  81. * left down to right down without anything in between, so all cases must be
  82. * handled.
  83. *
  84. * a handler consists of three values:
  85. * 0: action1
  86. * 1: action2
  87. * 2: new emulation state
  88. *
  89. * action > 0: ButtonPress
  90. * action = 0: nothing
  91. * action < 0: ButtonRelease
  92. *
  93. * The comment preceeding each section is the current emulation state.
  94. * The comments to the right are of the form
  95. * <button state> (<events>) -> <new emulation state>
  96. * which should be read as
  97. * If the buttons are in <button state>, generate <events> then go to
  98. * <new emulation state>.
  99. */
  100. static const signed char stateTab[11][5][3] = {
  101. /* 0 ground */
  102. {
  103. { 0, 0, 0 }, /* nothing -> ground (no change) */
  104. { 0, 0, 1 }, /* left -> delayed left */
  105. { 0, 0, 2 }, /* right -> delayed right */
  106. { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
  107. { 0, 0, -1 } /* timeout N/A */
  108. },
  109. /* 1 delayed left */
  110. {
  111. { 1, -1, 0 }, /* nothing (left event) -> ground */
  112. { 0, 0, 1 }, /* left -> delayed left (no change) */
  113. { 1, -1, 2 }, /* right (left event) -> delayed right */
  114. { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
  115. { 1, 0, 4 }, /* timeout (left press) -> pressed left */
  116. },
  117. /* 2 delayed right */
  118. {
  119. { 3, -3, 0 }, /* nothing (right event) -> ground */
  120. { 3, -3, 1 }, /* left (right event) -> delayed left (no change) */
  121. { 0, 0, 2 }, /* right -> delayed right (no change) */
  122. { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
  123. { 3, 0, 5 }, /* timeout (right press) -> pressed right */
  124. },
  125. /* 3 pressed middle */
  126. {
  127. { -2, 0, 0 }, /* nothing (middle release) -> ground */
  128. { 0, 0, 7 }, /* left -> released right */
  129. { 0, 0, 6 }, /* right -> released left */
  130. { 0, 0, 3 }, /* left & right -> pressed middle (no change) */
  131. { 0, 0, -1 }, /* timeout N/A */
  132. },
  133. /* 4 pressed left */
  134. {
  135. { -1, 0, 0 }, /* nothing (left release) -> ground */
  136. { 0, 0, 4 }, /* left -> pressed left (no change) */
  137. { -1, 0, 2 }, /* right (left release) -> delayed right */
  138. { 3, 0, 10 }, /* left & right (right press) -> pressed both */
  139. { 0, 0, -1 }, /* timeout N/A */
  140. },
  141. /* 5 pressed right */
  142. {
  143. { -3, 0, 0 }, /* nothing (right release) -> ground */
  144. { -3, 0, 1 }, /* left (right release) -> delayed left */
  145. { 0, 0, 5 }, /* right -> pressed right (no change) */
  146. { 1, 0, 10 }, /* left & right (left press) -> pressed both */
  147. { 0, 0, -1 }, /* timeout N/A */
  148. },
  149. /* 6 released left */
  150. {
  151. { -2, 0, 0 }, /* nothing (middle release) -> ground */
  152. { -2, 0, 1 }, /* left (middle release) -> delayed left */
  153. { 0, 0, 6 }, /* right -> released left (no change) */
  154. { 1, 0, 8 }, /* left & right (left press) -> repressed left */
  155. { 0, 0, -1 }, /* timeout N/A */
  156. },
  157. /* 7 released right */
  158. {
  159. { -2, 0, 0 }, /* nothing (middle release) -> ground */
  160. { 0, 0, 7 }, /* left -> released right (no change) */
  161. { -2, 0, 2 }, /* right (middle release) -> delayed right */
  162. { 3, 0, 9 }, /* left & right (right press) -> repressed right */
  163. { 0, 0, -1 }, /* timeout N/A */
  164. },
  165. /* 8 repressed left */
  166. {
  167. { -2, -1, 0 }, /* nothing (middle release, left release) -> ground */
  168. { -2, 0, 4 }, /* left (middle release) -> pressed left */
  169. { -1, 0, 6 }, /* right (left release) -> released left */
  170. { 0, 0, 8 }, /* left & right -> repressed left (no change) */
  171. { 0, 0, -1 }, /* timeout N/A */
  172. },
  173. /* 9 repressed right */
  174. {
  175. { -2, -3, 0 }, /* nothing (middle release, right release) -> ground */
  176. { -3, 0, 7 }, /* left (right release) -> released right */
  177. { -2, 0, 5 }, /* right (middle release) -> pressed right */
  178. { 0, 0, 9 }, /* left & right -> repressed right (no change) */
  179. { 0, 0, -1 }, /* timeout N/A */
  180. },
  181. /* 10 pressed both */
  182. {
  183. { -1, -3, 0 }, /* nothing (left release, right release) -> ground */
  184. { -3, 0, 4 }, /* left (right release) -> pressed left */
  185. { -1, 0, 5 }, /* right (left release) -> pressed right */
  186. { 0, 0, 10 }, /* left & right -> pressed both (no change) */
  187. { 0, 0, -1 }, /* timeout N/A */
  188. },
  189. };
  190. EmulateMB::EmulateMB()
  191. : state(0), emulatedButtonMask(0), timer(this)
  192. {
  193. }
  194. void EmulateMB::filterPointerEvent(const rfb::Point& pos, int buttonMask)
  195. {
  196. int btstate;
  197. int action1, action2;
  198. int lastState;
  199. if (!emulateMiddleButton) {
  200. sendPointerEvent(pos, buttonMask);
  201. return;
  202. }
  203. lastButtonMask = buttonMask;
  204. lastPos = pos;
  205. btstate = 0;
  206. if (buttonMask & 0x1)
  207. btstate |= 0x1;
  208. if (buttonMask & 0x4)
  209. btstate |= 0x2;
  210. if ((state > 10) || (state < 0))
  211. throw rfb::Exception(_("Invalid state for 3 button emulation"));
  212. action1 = stateTab[state][btstate][0];
  213. if (action1 != 0)
  214. sendAction(pos, buttonMask, action1);
  215. action2 = stateTab[state][btstate][1];
  216. if (action2 != 0)
  217. sendAction(pos, buttonMask, action2);
  218. if ((action1 == 0) && (action2 == 0)) {
  219. buttonMask &= ~0x5;
  220. buttonMask |= emulatedButtonMask;
  221. sendPointerEvent(pos, buttonMask);
  222. }
  223. lastState = state;
  224. state = stateTab[state][btstate][2];
  225. if (lastState != state) {
  226. timer.stop();
  227. if (stateTab[state][4][2] >= 0)
  228. timer.start(50);
  229. }
  230. }
  231. bool EmulateMB::handleTimeout(rfb::Timer *t)
  232. {
  233. int action1, action2;
  234. if (&timer != t)
  235. return false;
  236. if ((state > 10) || (state < 0))
  237. throw rfb::Exception(_("Invalid state for 3 button emulation"));
  238. assert(stateTab[state][4][2] >= 0);
  239. action1 = stateTab[state][4][0];
  240. if (action1 != 0)
  241. sendAction(lastPos, lastButtonMask, action1);
  242. action2 = stateTab[state][4][1];
  243. if (action2 != 0)
  244. sendAction(lastPos, lastButtonMask, action2);
  245. state = stateTab[state][4][2];
  246. return false;
  247. }
  248. void EmulateMB::sendAction(const rfb::Point& pos, int buttonMask, int action)
  249. {
  250. assert(action != 0);
  251. if (action < 0)
  252. emulatedButtonMask &= ~(1 << ((-action) - 1));
  253. else
  254. emulatedButtonMask |= (1 << (action - 1));
  255. buttonMask &= ~0x5;
  256. buttonMask |= emulatedButtonMask;
  257. sendPointerEvent(pos, buttonMask);
  258. }