| 1 |
/** |
| 2 |
* Copyright (c) 2019 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
import { IBufferService, ICoreService, ICoreMouseService, IOptionsService } from 'common/services/Services'; |
| 6 |
import { ICoreMouseProtocol, ICoreMouseEvent, CoreMouseEncoding, CoreMouseEventType, CoreMouseButton, CoreMouseAction } from 'common/Types'; |
| 7 |
import { Disposable } from 'vs/base/common/lifecycle'; |
| 8 |
import { Emitter } from 'vs/base/common/event'; |
| 9 |
|
| 10 |
/** |
| 11 |
* Supported default protocols. |
| 12 |
*/ |
| 13 |
const DEFAULT_PROTOCOLS: { [key: string]: ICoreMouseProtocol } = { |
| 14 |
/** |
| 15 |
* NONE |
| 16 |
* Events: none |
| 17 |
* Modifiers: none |
| 18 |
*/ |
| 19 |
NONE: { |
| 20 |
events: CoreMouseEventType.NONE, |
| 21 |
restrict: () => false |
| 22 |
}, |
| 23 |
/** |
| 24 |
* X10 |
| 25 |
* Events: mousedown |
| 26 |
* Modifiers: none |
| 27 |
*/ |
| 28 |
X10: { |
| 29 |
events: CoreMouseEventType.DOWN, |
| 30 |
restrict: (e: ICoreMouseEvent) => { |
| 31 |
// no wheel, no move, no up |
| 32 |
if (e.button === CoreMouseButton.WHEEL || e.action !== CoreMouseAction.DOWN) { |
| 33 |
return false; |
| 34 |
} |
| 35 |
// no modifiers |
| 36 |
e.ctrl = false; |
| 37 |
e.alt = false; |
| 38 |
e.shift = false; |
| 39 |
return true; |
| 40 |
} |
| 41 |
}, |
| 42 |
/** |
| 43 |
* VT200 |
| 44 |
* Events: mousedown / mouseup / wheel |
| 45 |
* Modifiers: all |
| 46 |
*/ |
| 47 |
VT200: { |
| 48 |
events: CoreMouseEventType.DOWN | CoreMouseEventType.UP | CoreMouseEventType.WHEEL, |
| 49 |
restrict: (e: ICoreMouseEvent) => { |
| 50 |
// no move |
| 51 |
if (e.action === CoreMouseAction.MOVE) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
return true; |
| 55 |
} |
| 56 |
}, |
| 57 |
/** |
| 58 |
* DRAG |
| 59 |
* Events: mousedown / mouseup / wheel / mousedrag |
| 60 |
* Modifiers: all |
| 61 |
*/ |
| 62 |
DRAG: { |
| 63 |
events: CoreMouseEventType.DOWN | CoreMouseEventType.UP | CoreMouseEventType.WHEEL | CoreMouseEventType.DRAG, |
| 64 |
restrict: (e: ICoreMouseEvent) => { |
| 65 |
// no move without button |
| 66 |
if (e.action === CoreMouseAction.MOVE && e.button === CoreMouseButton.NONE) { |
| 67 |
return false; |
| 68 |
} |
| 69 |
return true; |
| 70 |
} |
| 71 |
}, |
| 72 |
/** |
| 73 |
* ANY |
| 74 |
* Events: all mouse related events |
| 75 |
* Modifiers: all |
| 76 |
*/ |
| 77 |
ANY: { |
| 78 |
events: |
| 79 |
CoreMouseEventType.DOWN | CoreMouseEventType.UP | CoreMouseEventType.WHEEL |
| 80 |
| CoreMouseEventType.DRAG | CoreMouseEventType.MOVE, |
| 81 |
restrict: (e: ICoreMouseEvent) => true |
| 82 |
} |
| 83 |
}; |
| 84 |
|
| 85 |
const enum Modifiers { |
| 86 |
SHIFT = 4, |
| 87 |
ALT = 8, |
| 88 |
CTRL = 16 |
| 89 |
} |
| 90 |
|
| 91 |
// helper for default encoders to generate the event code. |
| 92 |
function eventCode(e: ICoreMouseEvent, isSGR: boolean): number { |
| 93 |
let code = (e.ctrl ? Modifiers.CTRL : 0) | (e.shift ? Modifiers.SHIFT : 0) | (e.alt ? Modifiers.ALT : 0); |
| 94 |
if (e.button === CoreMouseButton.WHEEL) { |
| 95 |
code |= 64; |
| 96 |
code |= e.action; |
| 97 |
} else { |
| 98 |
code |= e.button & 3; |
| 99 |
if (e.button & 4) { |
| 100 |
code |= 64; |
| 101 |
} |
| 102 |
if (e.button & 8) { |
| 103 |
code |= 128; |
| 104 |
} |
| 105 |
if (e.action === CoreMouseAction.MOVE) { |
| 106 |
code |= CoreMouseAction.MOVE; |
| 107 |
} else if (e.action === CoreMouseAction.UP && !isSGR) { |
| 108 |
// special case - only SGR can report button on release |
| 109 |
// all others have to go with NONE |
| 110 |
code |= CoreMouseButton.NONE; |
| 111 |
} |
| 112 |
} |
| 113 |
return code; |
| 114 |
} |
| 115 |
|
| 116 |
const S = String.fromCharCode; |
| 117 |
|
| 118 |
/** |
| 119 |
* Supported default encodings. |
| 120 |
*/ |
| 121 |
const DEFAULT_ENCODINGS: { [key: string]: CoreMouseEncoding } = { |
| 122 |
/** |
| 123 |
* DEFAULT - CSI M Pb Px Py |
| 124 |
* Single byte encoding for coords and event code. |
| 125 |
* Can encode values up to 223 (1-based). |
| 126 |
*/ |
| 127 |
DEFAULT: (e: ICoreMouseEvent) => { |
| 128 |
const params = [eventCode(e, false) + 32, e.col + 32, e.row + 32]; |
| 129 |
// supress mouse report if we exceed addressible range |
| 130 |
// Note this is handled differently by emulators |
| 131 |
// - xterm: sends 0;0 coords instead |
| 132 |
// - vte, konsole: no report |
| 133 |
if (params[0] > 255 || params[1] > 255 || params[2] > 255) { |
| 134 |
return ''; |
| 135 |
} |
| 136 |
return `\x1b[M${S(params[0])}${S(params[1])}${S(params[2])}`; |
| 137 |
}, |
| 138 |
/** |
| 139 |
* SGR - CSI < Pb ; Px ; Py M|m |
| 140 |
* No encoding limitation. |
| 141 |
* Can report button on release and works with a well formed sequence. |
| 142 |
*/ |
| 143 |
SGR: (e: ICoreMouseEvent) => { |
| 144 |
const final = (e.action === CoreMouseAction.UP && e.button !== CoreMouseButton.WHEEL) ? 'm' : 'M'; |
| 145 |
return `\x1b[<${eventCode(e, true)};${e.col};${e.row}${final}`; |
| 146 |
}, |
| 147 |
SGR_PIXELS: (e: ICoreMouseEvent) => { |
| 148 |
const final = (e.action === CoreMouseAction.UP && e.button !== CoreMouseButton.WHEEL) ? 'm' : 'M'; |
| 149 |
return `\x1b[<${eventCode(e, true)};${e.x};${e.y}${final}`; |
| 150 |
} |
| 151 |
}; |
| 152 |
|
| 153 |
/** |
| 154 |
* CoreMouseService |
| 155 |
* |
| 156 |
* Provides mouse tracking reports with different protocols and encodings. |
| 157 |
* - protocols: NONE (default), X10, VT200, DRAG, ANY |
| 158 |
* - encodings: DEFAULT, SGR (UTF8, URXVT removed in #2507) |
| 159 |
* |
| 160 |
* Custom protocols/encodings can be added by `addProtocol` / `addEncoding`. |
| 161 |
* To activate a protocol/encoding, set `activeProtocol` / `activeEncoding`. |
| 162 |
* Switching a protocol will send a notification event `onProtocolChange` |
| 163 |
* with a list of needed events to track. |
| 164 |
* |
| 165 |
* The service handles the mouse tracking state and decides whether to send |
| 166 |
* a tracking report to the backend based on protocol and encoding limitations. |
| 167 |
* To send a mouse event call `triggerMouseEvent`. |
| 168 |
*/ |
| 169 |
export class CoreMouseService extends Disposable implements ICoreMouseService { |
| 170 |
public serviceBrand: any; |
| 171 |
|
| 172 |
private _protocols: { [name: string]: ICoreMouseProtocol } = {}; |
| 173 |
private _encodings: { [name: string]: CoreMouseEncoding } = {}; |
| 174 |
private _activeProtocol: string = ''; |
| 175 |
private _activeEncoding: string = ''; |
| 176 |
private _lastEvent: ICoreMouseEvent | null = null; |
| 177 |
private _wheelPartialScroll: number = 0; |
| 178 |
|
| 179 |
private readonly _onProtocolChange = this._register(new Emitter<CoreMouseEventType>()); |
| 180 |
public readonly onProtocolChange = this._onProtocolChange.event; |
| 181 |
|
| 182 |
constructor( |
| 183 |
@IBufferService private readonly _bufferService: IBufferService, |
| 184 |
@ICoreService private readonly _coreService: ICoreService, |
| 185 |
@IOptionsService private readonly _optionsService: IOptionsService |
| 186 |
) { |
| 187 |
super(); |
| 188 |
// register default protocols and encodings |
| 189 |
for (const name of Object.keys(DEFAULT_PROTOCOLS)) this.addProtocol(name, DEFAULT_PROTOCOLS[name]); |
| 190 |
for (const name of Object.keys(DEFAULT_ENCODINGS)) this.addEncoding(name, DEFAULT_ENCODINGS[name]); |
| 191 |
// call reset to set defaults |
| 192 |
this.reset(); |
| 193 |
} |
| 194 |
|
| 195 |
public addProtocol(name: string, protocol: ICoreMouseProtocol): void { |
| 196 |
this._protocols[name] = protocol; |
| 197 |
} |
| 198 |
|
| 199 |
public addEncoding(name: string, encoding: CoreMouseEncoding): void { |
| 200 |
this._encodings[name] = encoding; |
| 201 |
} |
| 202 |
|
| 203 |
public get activeProtocol(): string { |
| 204 |
return this._activeProtocol; |
| 205 |
} |
| 206 |
|
| 207 |
public get areMouseEventsActive(): boolean { |
| 208 |
return this._protocols[this._activeProtocol].events !== 0; |
| 209 |
} |
| 210 |
|
| 211 |
public set activeProtocol(name: string) { |
| 212 |
if (!this._protocols[name]) { |
| 213 |
throw new Error(`unknown protocol "${name}"`); |
| 214 |
} |
| 215 |
this._activeProtocol = name; |
| 216 |
this._onProtocolChange.fire(this._protocols[name].events); |
| 217 |
} |
| 218 |
|
| 219 |
public get activeEncoding(): string { |
| 220 |
return this._activeEncoding; |
| 221 |
} |
| 222 |
|
| 223 |
public set activeEncoding(name: string) { |
| 224 |
if (!this._encodings[name]) { |
| 225 |
throw new Error(`unknown encoding "${name}"`); |
| 226 |
} |
| 227 |
this._activeEncoding = name; |
| 228 |
} |
| 229 |
|
| 230 |
public reset(): void { |
| 231 |
this.activeProtocol = 'NONE'; |
| 232 |
this.activeEncoding = 'DEFAULT'; |
| 233 |
this._lastEvent = null; |
| 234 |
this._wheelPartialScroll = 0; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Processes a wheel event, accounting for partial scrolls for trackpad, mouse scrolls. |
| 239 |
* This prevents hyper-sensitive scrolling in alt buffer. |
| 240 |
*/ |
| 241 |
public consumeWheelEvent(ev: WheelEvent, cellHeight?: number, dpr?: number): number { |
| 242 |
// Do nothing if it's not a vertical scroll event |
| 243 |
if (ev.deltaY === 0 || ev.shiftKey) { |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
if (cellHeight === undefined || dpr === undefined) { |
| 248 |
return 0; |
| 249 |
} |
| 250 |
|
| 251 |
const targetWheelEventPixels = cellHeight / dpr; |
| 252 |
let amount = this._applyScrollModifier(ev.deltaY, ev); |
| 253 |
|
| 254 |
if (ev.deltaMode === WheelEvent.DOM_DELTA_PIXEL) { |
| 255 |
amount /= (targetWheelEventPixels + 0.0); // Prevent integer division |
| 256 |
|
| 257 |
const isLikelyTrackpad = Math.abs(ev.deltaY) < 50; |
| 258 |
if (isLikelyTrackpad) { |
| 259 |
amount *= 0.3; |
| 260 |
} |
| 261 |
|
| 262 |
this._wheelPartialScroll += amount; |
| 263 |
amount = Math.floor(Math.abs(this._wheelPartialScroll)) * (this._wheelPartialScroll > 0 ? 1 : -1); |
| 264 |
this._wheelPartialScroll %= 1; |
| 265 |
} else if (ev.deltaMode === WheelEvent.DOM_DELTA_PAGE) { |
| 266 |
amount *= this._bufferService.rows; |
| 267 |
} |
| 268 |
return amount; |
| 269 |
} |
| 270 |
|
| 271 |
private _applyScrollModifier(amount: number, ev: WheelEvent): number { |
| 272 |
// Multiply the scroll speed when the modifier key is pressed |
| 273 |
if (ev.altKey || ev.ctrlKey || ev.shiftKey) { |
| 274 |
return amount * this._optionsService.rawOptions.fastScrollSensitivity * this._optionsService.rawOptions.scrollSensitivity; |
| 275 |
} |
| 276 |
return amount * this._optionsService.rawOptions.scrollSensitivity; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Triggers a mouse event to be sent. |
| 281 |
* |
| 282 |
* Returns true if the event passed all protocol restrictions and a report |
| 283 |
* was sent, otherwise false. The return value may be used to decide whether |
| 284 |
* the default event action in the bowser component should be omitted. |
| 285 |
* |
| 286 |
* Note: The method will change values of the given event object |
| 287 |
* to fullfill protocol and encoding restrictions. |
| 288 |
*/ |
| 289 |
public triggerMouseEvent(e: ICoreMouseEvent): boolean { |
| 290 |
// range check for col/row |
| 291 |
if (e.col < 0 || e.col >= this._bufferService.cols |
| 292 |
|| e.row < 0 || e.row >= this._bufferService.rows) { |
| 293 |
return false; |
| 294 |
} |
| 295 |
|
| 296 |
// filter nonsense combinations of button + action |
| 297 |
if (e.button === CoreMouseButton.WHEEL && e.action === CoreMouseAction.MOVE) { |
| 298 |
return false; |
| 299 |
} |
| 300 |
if (e.button === CoreMouseButton.NONE && e.action !== CoreMouseAction.MOVE) { |
| 301 |
return false; |
| 302 |
} |
| 303 |
if (e.button !== CoreMouseButton.WHEEL && (e.action === CoreMouseAction.LEFT || e.action === CoreMouseAction.RIGHT)) { |
| 304 |
return false; |
| 305 |
} |
| 306 |
|
| 307 |
// report 1-based coords |
| 308 |
e.col++; |
| 309 |
e.row++; |
| 310 |
|
| 311 |
// debounce move events at grid or pixel level |
| 312 |
if (e.action === CoreMouseAction.MOVE |
| 313 |
&& this._lastEvent |
| 314 |
&& this._equalEvents(this._lastEvent, e, this._activeEncoding === 'SGR_PIXELS') |
| 315 |
) { |
| 316 |
return false; |
| 317 |
} |
| 318 |
|
| 319 |
// apply protocol restrictions |
| 320 |
if (!this._protocols[this._activeProtocol].restrict(e)) { |
| 321 |
return false; |
| 322 |
} |
| 323 |
|
| 324 |
// encode report and send |
| 325 |
const report = this._encodings[this._activeEncoding](e); |
| 326 |
if (report) { |
| 327 |
// always send DEFAULT as binary data |
| 328 |
if (this._activeEncoding === 'DEFAULT') { |
| 329 |
this._coreService.triggerBinaryEvent(report); |
| 330 |
} else { |
| 331 |
this._coreService.triggerDataEvent(report, true); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
this._lastEvent = e; |
| 336 |
|
| 337 |
return true; |
| 338 |
} |
| 339 |
|
| 340 |
public explainEvents(events: CoreMouseEventType): { [event: string]: boolean } { |
| 341 |
return { |
| 342 |
down: !!(events & CoreMouseEventType.DOWN), |
| 343 |
up: !!(events & CoreMouseEventType.UP), |
| 344 |
drag: !!(events & CoreMouseEventType.DRAG), |
| 345 |
move: !!(events & CoreMouseEventType.MOVE), |
| 346 |
wheel: !!(events & CoreMouseEventType.WHEEL) |
| 347 |
}; |
| 348 |
} |
| 349 |
|
| 350 |
private _equalEvents(e1: ICoreMouseEvent, e2: ICoreMouseEvent, pixels: boolean): boolean { |
| 351 |
if (pixels) { |
| 352 |
if (e1.x !== e2.x) return false; |
| 353 |
if (e1.y !== e2.y) return false; |
| 354 |
} else { |
| 355 |
if (e1.col !== e2.col) return false; |
| 356 |
if (e1.row !== e2.row) return false; |
| 357 |
} |
| 358 |
if (e1.button !== e2.button) return false; |
| 359 |
if (e1.action !== e2.action) return false; |
| 360 |
if (e1.ctrl !== e2.ctrl) return false; |
| 361 |
if (e1.alt !== e2.alt) return false; |
| 362 |
if (e1.shift !== e2.shift) return false; |
| 363 |
return true; |
| 364 |
} |
| 365 |
} |