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 /*
20  * Based on xf86-input-evdev
21  *
22  * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
23  * Copyright 1993 by David Dawes <dawes@xfree86.org>
24  * Copyright 2002 by SuSE Linux AG, Author: Egbert Eich
25  * Copyright 1994-2002 by The XFree86 Project, Inc.
26  * Copyright 2002 by Paul Elliott
27  * (Ported from xf86-input-mouse, above copyrights taken from there)
28  * Copyright © 2008 University of South Australia
29  *
30  * Permission to use, copy, modify, distribute, and sell this software
31  * and its documentation for any purpose is hereby granted without
32  * fee, provided that the above copyright notice appear in all copies
33  * and that both that copyright notice and this permission notice
34  * appear in supporting documentation, and that the name of the authors
35  * not be used in advertising or publicity pertaining to distribution of the
36  * software without specific, written prior permission.  The authors make no
37  * representations about the suitability of this software for any
38  * purpose.  It is provided "as is" without express or implied
39  * warranty.
40  *
41  * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
42  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
43  * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
44  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
45  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
46  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
47  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48  *
49  */
50 
51 #ifdef HAVE_CONFIG_H
52 #include <config.h>
53 #endif
54 
55 #include <assert.h>
56 
57 #include <rfb/Exception.h>
58 
59 #include "parameters.h"
60 #include "i18n.h"
61 #include "EmulateMB.h"
62 
63 /*
64  * Lets create a simple finite-state machine for 3 button emulation:
65  *
66  * We track buttons 1 and 3 (left and right).  There are 11 states:
67  *   0 ground           - initial state
68  *   1 delayed left     - left pressed, waiting for right
69  *   2 delayed right    - right pressed, waiting for left
70  *   3 pressed middle   - right and left pressed, emulated middle sent
71  *   4 pressed left     - left pressed and sent
72  *   5 pressed right    - right pressed and sent
73  *   6 released left    - left released after emulated middle
74  *   7 released right   - right released after emulated middle
75  *   8 repressed left   - left pressed after released left
76  *   9 repressed right  - right pressed after released right
77  *  10 pressed both     - both pressed, not emulating middle
78  *
79  * At each state, we need handlers for the following events
80  *   0: no buttons down
81  *   1: left button down
82  *   2: right button down
83  *   3: both buttons down
84  *   4: emulate3Timeout passed without a button change
85  * Note that button events are not deltas, they are the set of buttons being
86  * pressed now.  It's possible (ie, mouse hardware does it) to go from (eg)
87  * left down to right down without anything in between, so all cases must be
88  * handled.
89  *
90  * a handler consists of three values:
91  *   0: action1
92  *   1: action2
93  *   2: new emulation state
94  *
95  * action > 0: ButtonPress
96  * action = 0: nothing
97  * action < 0: ButtonRelease
98  *
99  * The comment preceeding each section is the current emulation state.
100  * The comments to the right are of the form
101  *      <button state> (<events>) -> <new emulation state>
102  * which should be read as
103  *      If the buttons are in <button state>, generate <events> then go to
104  *      <new emulation state>.
105  */
106 static const signed char stateTab[11][5][3] = {
107 /* 0 ground */
108   {
109     {  0,  0,  0 },   /* nothing -> ground (no change) */
110     {  0,  0,  1 },   /* left -> delayed left */
111     {  0,  0,  2 },   /* right -> delayed right */
112     {  2,  0,  3 },   /* left & right (middle press) -> pressed middle */
113     {  0,  0, -1 }    /* timeout N/A */
114   },
115 /* 1 delayed left */
116   {
117     {  1, -1,  0 },   /* nothing (left event) -> ground */
118     {  0,  0,  1 },   /* left -> delayed left (no change) */
119     {  1, -1,  2 },   /* right (left event) -> delayed right */
120     {  2,  0,  3 },   /* left & right (middle press) -> pressed middle */
121     {  1,  0,  4 },   /* timeout (left press) -> pressed left */
122   },
123 /* 2 delayed right */
124   {
125     {  3, -3,  0 },   /* nothing (right event) -> ground */
126     {  3, -3,  1 },   /* left (right event) -> delayed left (no change) */
127     {  0,  0,  2 },   /* right -> delayed right (no change) */
128     {  2,  0,  3 },   /* left & right (middle press) -> pressed middle */
129     {  3,  0,  5 },   /* timeout (right press) -> pressed right */
130   },
131 /* 3 pressed middle */
132   {
133     { -2,  0,  0 },   /* nothing (middle release) -> ground */
134     {  0,  0,  7 },   /* left -> released right */
135     {  0,  0,  6 },   /* right -> released left */
136     {  0,  0,  3 },   /* left & right -> pressed middle (no change) */
137     {  0,  0, -1 },   /* timeout N/A */
138   },
139 /* 4 pressed left */
140   {
141     { -1,  0,  0 },   /* nothing (left release) -> ground */
142     {  0,  0,  4 },   /* left -> pressed left (no change) */
143     { -1,  0,  2 },   /* right (left release) -> delayed right */
144     {  3,  0, 10 },   /* left & right (right press) -> pressed both */
145     {  0,  0, -1 },   /* timeout N/A */
146   },
147 /* 5 pressed right */
148   {
149     { -3,  0,  0 },   /* nothing (right release) -> ground */
150     { -3,  0,  1 },   /* left (right release) -> delayed left */
151     {  0,  0,  5 },   /* right -> pressed right (no change) */
152     {  1,  0, 10 },   /* left & right (left press) -> pressed both */
153     {  0,  0, -1 },   /* timeout N/A */
154   },
155 /* 6 released left */
156   {
157     { -2,  0,  0 },   /* nothing (middle release) -> ground */
158     { -2,  0,  1 },   /* left (middle release) -> delayed left */
159     {  0,  0,  6 },   /* right -> released left (no change) */
160     {  1,  0,  8 },   /* left & right (left press) -> repressed left */
161     {  0,  0, -1 },   /* timeout N/A */
162   },
163 /* 7 released right */
164   {
165     { -2,  0,  0 },   /* nothing (middle release) -> ground */
166     {  0,  0,  7 },   /* left -> released right (no change) */
167     { -2,  0,  2 },   /* right (middle release) -> delayed right */
168     {  3,  0,  9 },   /* left & right (right press) -> repressed right */
169     {  0,  0, -1 },   /* timeout N/A */
170   },
171 /* 8 repressed left */
172   {
173     { -2, -1,  0 },   /* nothing (middle release, left release) -> ground */
174     { -2,  0,  4 },   /* left (middle release) -> pressed left */
175     { -1,  0,  6 },   /* right (left release) -> released left */
176     {  0,  0,  8 },   /* left & right -> repressed left (no change) */
177     {  0,  0, -1 },   /* timeout N/A */
178   },
179 /* 9 repressed right */
180   {
181     { -2, -3,  0 },   /* nothing (middle release, right release) -> ground */
182     { -3,  0,  7 },   /* left (right release) -> released right */
183     { -2,  0,  5 },   /* right (middle release) -> pressed right */
184     {  0,  0,  9 },   /* left & right -> repressed right (no change) */
185     {  0,  0, -1 },   /* timeout N/A */
186   },
187 /* 10 pressed both */
188   {
189     { -1, -3,  0 },   /* nothing (left release, right release) -> ground */
190     { -3,  0,  4 },   /* left (right release) -> pressed left */
191     { -1,  0,  5 },   /* right (left release) -> pressed right */
192     {  0,  0, 10 },   /* left & right -> pressed both (no change) */
193     {  0,  0, -1 },   /* timeout N/A */
194   },
195 };
196 
EmulateMB()197 EmulateMB::EmulateMB()
198   : state(0), emulatedButtonMask(0), timer(this)
199 {
200 }
201 
filterPointerEvent(const rfb::Point & pos,int buttonMask)202 void EmulateMB::filterPointerEvent(const rfb::Point& pos, int buttonMask)
203 {
204   int btstate;
205   int action1, action2;
206   int lastState;
207 
208   // Just pass through events if the emulate setting is disabled
209   if (!emulateMiddleButton) {
210      sendPointerEvent(pos, buttonMask);
211      return;
212   }
213 
214   lastButtonMask = buttonMask;
215   lastPos = pos;
216 
217   btstate = 0;
218 
219   if (buttonMask & 0x1)
220     btstate |= 0x1;
221 
222   if (buttonMask & 0x4)
223     btstate |= 0x2;
224 
225   if ((state > 10) || (state < 0))
226     throw rfb::Exception(_("Invalid state for 3 button emulation"));
227 
228   action1 = stateTab[state][btstate][0];
229 
230   if (action1 != 0) {
231     // Some presses are delayed, that means we have to check if that's
232     // the case and send the position corresponding to where the event
233     // first was initiated
234     if ((stateTab[state][4][2] >= 0) && action1 > 0)
235       // We have a timeout state and a button press (a delayed press),
236       // always use the original position when leaving a timeout state,
237       // whether the timeout was triggered or not
238       sendAction(origPos, buttonMask, action1);
239     else
240       // Normal non-delayed event
241       sendAction(pos, buttonMask, action1);
242   }
243 
244   action2 = stateTab[state][btstate][1];
245 
246   // In our case with the state machine, action2 always occurs during a button
247   // release but if this change we need handle action2 accordingly
248   if (action2 != 0) {
249     if ((stateTab[state][4][2] >= 0) && action2 > 0)
250       sendAction(origPos, buttonMask, action2);
251     else
252       // Normal non-delayed event
253       sendAction(pos, buttonMask, action2);
254   }
255 
256   // Still send a pointer move event even if there are no actions.
257   // However if the timer is running then we are supressing _all_
258   // events, even movement. The pointer's actual position will be
259   // sent once the timer fires or is abandoned.
260   if ((action1 == 0) && (action2 == 0) && !timer.isStarted()) {
261     buttonMask = createButtonMask(buttonMask);
262     sendPointerEvent(pos, buttonMask);
263   }
264 
265   lastState = state;
266   state = stateTab[state][btstate][2];
267 
268   if (lastState != state) {
269     timer.stop();
270 
271     if (stateTab[state][4][2] >= 0) {
272       // We need to save the original position so that
273       // drags start from the correct position
274       origPos = pos;
275       timer.start(50);
276     }
277   }
278 }
279 
handleTimeout(rfb::Timer * t)280 bool EmulateMB::handleTimeout(rfb::Timer *t)
281 {
282   int action1, action2;
283   int buttonMask;
284 
285   if (&timer != t)
286     return false;
287 
288   if ((state > 10) || (state < 0))
289     throw rfb::Exception(_("Invalid state for 3 button emulation"));
290 
291   // Timeout shouldn't trigger when there's no timeout action
292   assert(stateTab[state][4][2] >= 0);
293 
294   action1 = stateTab[state][4][0];
295   if (action1 != 0)
296     sendAction(origPos, lastButtonMask, action1);
297 
298   action2 = stateTab[state][4][1];
299   if (action2 != 0)
300     sendAction(origPos, lastButtonMask, action2);
301 
302   buttonMask = lastButtonMask;
303 
304   // Pointer move events are not sent when waiting for the timeout.
305   // However, we can't let the position get out of sync so when
306   // the pointer has moved we have to send the latest position here.
307   if (!origPos.equals(lastPos)) {
308     buttonMask = createButtonMask(buttonMask);
309     sendPointerEvent(lastPos, buttonMask);
310   }
311 
312   state = stateTab[state][4][2];
313 
314   return false;
315 }
316 
sendAction(const rfb::Point & pos,int buttonMask,int action)317 void EmulateMB::sendAction(const rfb::Point& pos, int buttonMask, int action)
318 {
319   assert(action != 0);
320 
321   if (action < 0)
322     emulatedButtonMask &= ~(1 << ((-action) - 1));
323   else
324     emulatedButtonMask |= (1 << (action - 1));
325 
326   buttonMask = createButtonMask(buttonMask);
327   sendPointerEvent(pos, buttonMask);
328 }
329 
createButtonMask(int buttonMask)330 int EmulateMB::createButtonMask(int buttonMask)
331 {
332   // Unset left and right buttons in the mask
333   buttonMask &= ~0x5;
334 
335   // Set the left and right buttons according to the action
336   return buttonMask |= emulatedButtonMask;
337 }