Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

EmulateMB.cxx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 preceding 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. // Just pass through events if the emulate setting is disabled
  200. if (!emulateMiddleButton) {
  201. sendPointerEvent(pos, buttonMask);
  202. return;
  203. }
  204. lastButtonMask = buttonMask;
  205. lastPos = pos;
  206. btstate = 0;
  207. if (buttonMask & 0x1)
  208. btstate |= 0x1;
  209. if (buttonMask & 0x4)
  210. btstate |= 0x2;
  211. if ((state > 10) || (state < 0))
  212. throw rfb::Exception(_("Invalid state for 3 button emulation"));
  213. action1 = stateTab[state][btstate][0];
  214. if (action1 != 0) {
  215. // Some presses are delayed, that means we have to check if that's
  216. // the case and send the position corresponding to where the event
  217. // first was initiated
  218. if ((stateTab[state][4][2] >= 0) && action1 > 0)
  219. // We have a timeout state and a button press (a delayed press),
  220. // always use the original position when leaving a timeout state,
  221. // whether the timeout was triggered or not
  222. sendAction(origPos, buttonMask, action1);
  223. else
  224. // Normal non-delayed event
  225. sendAction(pos, buttonMask, action1);
  226. }
  227. action2 = stateTab[state][btstate][1];
  228. // In our case with the state machine, action2 always occurs during a button
  229. // release but if this change we need handle action2 accordingly
  230. if (action2 != 0) {
  231. if ((stateTab[state][4][2] >= 0) && action2 > 0)
  232. sendAction(origPos, buttonMask, action2);
  233. else
  234. // Normal non-delayed event
  235. sendAction(pos, buttonMask, action2);
  236. }
  237. // Still send a pointer move event even if there are no actions.
  238. // However if the timer is running then we are supressing _all_
  239. // events, even movement. The pointer's actual position will be
  240. // sent once the timer fires or is abandoned.
  241. if ((action1 == 0) && (action2 == 0) && !timer.isStarted()) {
  242. buttonMask = createButtonMask(buttonMask);
  243. sendPointerEvent(pos, buttonMask);
  244. }
  245. lastState = state;
  246. state = stateTab[state][btstate][2];
  247. if (lastState != state) {
  248. timer.stop();
  249. if (stateTab[state][4][2] >= 0) {
  250. // We need to save the original position so that
  251. // drags start from the correct position
  252. origPos = pos;
  253. timer.start(50);
  254. }
  255. }
  256. }
  257. bool EmulateMB::handleTimeout(rfb::Timer *t)
  258. {
  259. int action1, action2;
  260. int buttonMask;
  261. if (&timer != t)
  262. return false;
  263. if ((state > 10) || (state < 0))
  264. throw rfb::Exception(_("Invalid state for 3 button emulation"));
  265. // Timeout shouldn't trigger when there's no timeout action
  266. assert(stateTab[state][4][2] >= 0);
  267. action1 = stateTab[state][4][0];
  268. if (action1 != 0)
  269. sendAction(origPos, lastButtonMask, action1);
  270. action2 = stateTab[state][4][1];
  271. if (action2 != 0)
  272. sendAction(origPos, lastButtonMask, action2);
  273. buttonMask = lastButtonMask;
  274. // Pointer move events are not sent when waiting for the timeout.
  275. // However, we can't let the position get out of sync so when
  276. // the pointer has moved we have to send the latest position here.
  277. if (origPos != lastPos) {
  278. buttonMask = createButtonMask(buttonMask);
  279. sendPointerEvent(lastPos, buttonMask);
  280. }
  281. state = stateTab[state][4][2];
  282. return false;
  283. }
  284. void EmulateMB::sendAction(const rfb::Point& pos, int buttonMask, int action)
  285. {
  286. assert(action != 0);
  287. if (action < 0)
  288. emulatedButtonMask &= ~(1 << ((-action) - 1));
  289. else
  290. emulatedButtonMask |= (1 << (action - 1));
  291. buttonMask = createButtonMask(buttonMask);
  292. sendPointerEvent(pos, buttonMask);
  293. }
  294. int EmulateMB::createButtonMask(int buttonMask)
  295. {
  296. // Unset left and right buttons in the mask
  297. buttonMask &= ~0x5;
  298. // Set the left and right buttons according to the action
  299. return buttonMask |= emulatedButtonMask;
  300. }