Skip to content

Commit 0925be1

Browse files
committed
Use MouseEvent.buttons for button state tracking
Instead of keeping track of button states ourselves by looking at MouseEvent.button, we can use the MouseEvent.buttons which already contains the state of all buttons.
1 parent 52ddb20 commit 0925be1

File tree

2 files changed

+100
-73
lines changed

2 files changed

+100
-73
lines changed

core/rfb.js

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,35 @@ export default class RFB extends EventTargetMixin {
10311031
this.sendKey(keysym, code, down);
10321032
}
10331033

1034+
static _convertButtonMask(buttons) {
1035+
/* The bits in MouseEvent.buttons property correspond
1036+
* to the following mouse buttons:
1037+
* 0: Left
1038+
* 1: Right
1039+
* 2: Middle
1040+
* 3: Back
1041+
* 4: Forward
1042+
*
1043+
* These bits needs to be converted to what they are defined as
1044+
* in the RFB protocol.
1045+
*/
1046+
1047+
const buttonMaskMap = {
1048+
0: 1 << 0, // Left
1049+
1: 1 << 2, // Right
1050+
2: 1 << 1, // Middle
1051+
3: 1 << 7, // Back
1052+
};
1053+
1054+
let bmask = 0;
1055+
for (let i = 0; i < 4; i++) {
1056+
if (buttons & (1 << i)) {
1057+
bmask |= buttonMaskMap[i];
1058+
}
1059+
}
1060+
return bmask;
1061+
}
1062+
10341063
_handleMouse(ev) {
10351064
/*
10361065
* We don't check connection status or viewOnly here as the
@@ -1060,15 +1089,15 @@ export default class RFB extends EventTargetMixin {
10601089
let pos = clientToElement(ev.clientX, ev.clientY,
10611090
this._canvas);
10621091

1092+
let bmask = RFB._convertButtonMask(ev.buttons);
1093+
10631094
switch (ev.type) {
10641095
case 'mousedown':
10651096
setCapture(this._canvas);
1066-
this._handleMouseButton(pos.x, pos.y,
1067-
true, 1 << ev.button);
1097+
this._handleMouseButton(pos.x, pos.y, true, bmask);
10681098
break;
10691099
case 'mouseup':
1070-
this._handleMouseButton(pos.x, pos.y,
1071-
false, 1 << ev.button);
1100+
this._handleMouseButton(pos.x, pos.y, false, bmask);
10721101
break;
10731102
case 'mousemove':
10741103
this._handleMouseMove(pos.x, pos.y);
@@ -1097,7 +1126,7 @@ export default class RFB extends EventTargetMixin {
10971126
// Otherwise we treat this as a mouse click event.
10981127
// Send the button down event here, as the button up
10991128
// event is sent at the end of this function.
1100-
this._sendMouse(x, y, bmask);
1129+
this._sendMouse(x, y, this._mouseButtonMask);
11011130
}
11021131
}
11031132

@@ -1108,12 +1137,7 @@ export default class RFB extends EventTargetMixin {
11081137
this._sendMouse(x, y, this._mouseButtonMask);
11091138
}
11101139

1111-
if (down) {
1112-
this._mouseButtonMask |= bmask;
1113-
} else {
1114-
this._mouseButtonMask &= ~bmask;
1115-
}
1116-
1140+
this._mouseButtonMask = bmask;
11171141
this._sendMouse(x, y, this._mouseButtonMask);
11181142
}
11191143

@@ -1177,6 +1201,7 @@ export default class RFB extends EventTargetMixin {
11771201
let pos = clientToElement(ev.clientX, ev.clientY,
11781202
this._canvas);
11791203

1204+
let bmask = RFB._convertButtonMask(ev.buttons);
11801205
let dX = ev.deltaX;
11811206
let dY = ev.deltaY;
11821207

@@ -1196,26 +1221,27 @@ export default class RFB extends EventTargetMixin {
11961221
this._accumulatedWheelDeltaX += dX;
11971222
this._accumulatedWheelDeltaY += dY;
11981223

1224+
11991225
// Generate a mouse wheel step event when the accumulated delta
12001226
// for one of the axes is large enough.
12011227
if (Math.abs(this._accumulatedWheelDeltaX) >= WHEEL_STEP) {
12021228
if (this._accumulatedWheelDeltaX < 0) {
1203-
this._handleMouseButton(pos.x, pos.y, true, 1 << 5);
1204-
this._handleMouseButton(pos.x, pos.y, false, 1 << 5);
1229+
this._handleMouseButton(pos.x, pos.y, true, bmask | 1 << 5);
1230+
this._handleMouseButton(pos.x, pos.y, false, bmask);
12051231
} else if (this._accumulatedWheelDeltaX > 0) {
1206-
this._handleMouseButton(pos.x, pos.y, true, 1 << 6);
1207-
this._handleMouseButton(pos.x, pos.y, false, 1 << 6);
1232+
this._handleMouseButton(pos.x, pos.y, true, bmask | 1 << 6);
1233+
this._handleMouseButton(pos.x, pos.y, false, bmask);
12081234
}
12091235

12101236
this._accumulatedWheelDeltaX = 0;
12111237
}
12121238
if (Math.abs(this._accumulatedWheelDeltaY) >= WHEEL_STEP) {
12131239
if (this._accumulatedWheelDeltaY < 0) {
1214-
this._handleMouseButton(pos.x, pos.y, true, 1 << 3);
1215-
this._handleMouseButton(pos.x, pos.y, false, 1 << 3);
1240+
this._handleMouseButton(pos.x, pos.y, true, bmask | 1 << 3);
1241+
this._handleMouseButton(pos.x, pos.y, false, bmask);
12161242
} else if (this._accumulatedWheelDeltaY > 0) {
1217-
this._handleMouseButton(pos.x, pos.y, true, 1 << 4);
1218-
this._handleMouseButton(pos.x, pos.y, false, 1 << 4);
1243+
this._handleMouseButton(pos.x, pos.y, true, bmask | 1 << 4);
1244+
this._handleMouseButton(pos.x, pos.y, false, bmask);
12191245
}
12201246

12211247
this._accumulatedWheelDeltaY = 0;
@@ -1255,7 +1281,7 @@ export default class RFB extends EventTargetMixin {
12551281

12561282
this._fakeMouseMove(this._gestureFirstDoubleTapEv, pos.x, pos.y);
12571283
this._handleMouseButton(pos.x, pos.y, true, bmask);
1258-
this._handleMouseButton(pos.x, pos.y, false, bmask);
1284+
this._handleMouseButton(pos.x, pos.y, false, 0x0);
12591285
}
12601286

12611287
_handleGesture(ev) {
@@ -1313,23 +1339,23 @@ export default class RFB extends EventTargetMixin {
13131339
// every update.
13141340
this._fakeMouseMove(ev, pos.x, pos.y);
13151341
while ((ev.detail.magnitudeY - this._gestureLastMagnitudeY) > GESTURE_SCRLSENS) {
1316-
this._handleMouseButton(pos.x, pos.y, true, 0x8);
1317-
this._handleMouseButton(pos.x, pos.y, false, 0x8);
1342+
this._handleMouseButton(pos.x, pos.y, true, this._mouseButtonMask | 0x8);
1343+
this._handleMouseButton(pos.x, pos.y, false, this._mouseButtonMask & ~0x8);
13181344
this._gestureLastMagnitudeY += GESTURE_SCRLSENS;
13191345
}
13201346
while ((ev.detail.magnitudeY - this._gestureLastMagnitudeY) < -GESTURE_SCRLSENS) {
1321-
this._handleMouseButton(pos.x, pos.y, true, 0x10);
1322-
this._handleMouseButton(pos.x, pos.y, false, 0x10);
1347+
this._handleMouseButton(pos.x, pos.y, true, this._mouseButtonMask | 0x10);
1348+
this._handleMouseButton(pos.x, pos.y, false, this._mouseButtonMask & ~ 0x10);
13231349
this._gestureLastMagnitudeY -= GESTURE_SCRLSENS;
13241350
}
13251351
while ((ev.detail.magnitudeX - this._gestureLastMagnitudeX) > GESTURE_SCRLSENS) {
1326-
this._handleMouseButton(pos.x, pos.y, true, 0x20);
1327-
this._handleMouseButton(pos.x, pos.y, false, 0x20);
1352+
this._handleMouseButton(pos.x, pos.y, true, this._mouseButtonMask | 0x20);
1353+
this._handleMouseButton(pos.x, pos.y, false, this._mouseButtonMask & ~ 0x20);
13281354
this._gestureLastMagnitudeX += GESTURE_SCRLSENS;
13291355
}
13301356
while ((ev.detail.magnitudeX - this._gestureLastMagnitudeX) < -GESTURE_SCRLSENS) {
1331-
this._handleMouseButton(pos.x, pos.y, true, 0x40);
1332-
this._handleMouseButton(pos.x, pos.y, false, 0x40);
1357+
this._handleMouseButton(pos.x, pos.y, true, this._mouseButtonMask | 0x40);
1358+
this._handleMouseButton(pos.x, pos.y, false, this._mouseButtonMask & ~0x40);
13331359
this._gestureLastMagnitudeX -= GESTURE_SCRLSENS;
13341360
}
13351361
break;
@@ -1342,13 +1368,13 @@ export default class RFB extends EventTargetMixin {
13421368
if (Math.abs(magnitude - this._gestureLastMagnitudeX) > GESTURE_ZOOMSENS) {
13431369
this._handleKeyEvent(KeyTable.XK_Control_L, "ControlLeft", true);
13441370
while ((magnitude - this._gestureLastMagnitudeX) > GESTURE_ZOOMSENS) {
1345-
this._handleMouseButton(pos.x, pos.y, true, 0x8);
1346-
this._handleMouseButton(pos.x, pos.y, false, 0x8);
1371+
this._handleMouseButton(pos.x, pos.y, true, this._mouseButtonMask | 0x8);
1372+
this._handleMouseButton(pos.x, pos.y, false, this._mouseButtonMask & ~0x8);
13471373
this._gestureLastMagnitudeX += GESTURE_ZOOMSENS;
13481374
}
13491375
while ((magnitude - this._gestureLastMagnitudeX) < -GESTURE_ZOOMSENS) {
1350-
this._handleMouseButton(pos.x, pos.y, true, 0x10);
1351-
this._handleMouseButton(pos.x, pos.y, false, 0x10);
1376+
this._handleMouseButton(pos.x, pos.y, true, this._mouseButtonMask | 0x10);
1377+
this._handleMouseButton(pos.x, pos.y, false, this._mouseButtonMask & ~0x10);
13521378
this._gestureLastMagnitudeX -= GESTURE_ZOOMSENS;
13531379
}
13541380
}
@@ -1367,11 +1393,11 @@ export default class RFB extends EventTargetMixin {
13671393
break;
13681394
case 'drag':
13691395
this._fakeMouseMove(ev, pos.x, pos.y);
1370-
this._handleMouseButton(pos.x, pos.y, false, 0x1);
1396+
this._handleMouseButton(pos.x, pos.y, false, this._mouseButtonMask & ~0x1);
13711397
break;
13721398
case 'longpress':
13731399
this._fakeMouseMove(ev, pos.x, pos.y);
1374-
this._handleMouseButton(pos.x, pos.y, false, 0x4);
1400+
this._handleMouseButton(pos.x, pos.y, false, this._mouseButtonMask & ~0x4);
13751401
break;
13761402
}
13771403
break;
@@ -2941,6 +2967,7 @@ export default class RFB extends EventTargetMixin {
29412967
"raw", passwordChars, { name: "DES-ECB" }, false, ["encrypt"]);
29422968
return legacyCrypto.encrypt({ name: "DES-ECB" }, key, challenge);
29432969
}
2970+
29442971
}
29452972

29462973
// Class Methods

0 commit comments

Comments
 (0)