| 1 |
/** |
| 2 |
* Copyright (c) 2019 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { clone } from 'common/Clone'; |
| 7 |
import { Disposable } from 'vs/base/common/lifecycle'; |
| 8 |
import { IDecPrivateModes, IModes } from 'common/Types'; |
| 9 |
import { IBufferService, ICoreService, ILogService, IOptionsService } from 'common/services/Services'; |
| 10 |
import { Emitter } from 'vs/base/common/event'; |
| 11 |
|
| 12 |
const DEFAULT_MODES: IModes = Object.freeze({ |
| 13 |
insertMode: false |
| 14 |
}); |
| 15 |
|
| 16 |
const DEFAULT_DEC_PRIVATE_MODES: IDecPrivateModes = Object.freeze({ |
| 17 |
applicationCursorKeys: false, |
| 18 |
applicationKeypad: false, |
| 19 |
bracketedPasteMode: false, |
| 20 |
cursorBlink: undefined, |
| 21 |
cursorStyle: undefined, |
| 22 |
origin: false, |
| 23 |
reverseWraparound: false, |
| 24 |
sendFocus: false, |
| 25 |
synchronizedOutput: false, |
| 26 |
wraparound: true // defaults: xterm - true, vt100 - false |
| 27 |
}); |
| 28 |
|
| 29 |
export class CoreService extends Disposable implements ICoreService { |
| 30 |
public serviceBrand: any; |
| 31 |
|
| 32 |
public isCursorInitialized: boolean = false; |
| 33 |
public isCursorHidden: boolean = false; |
| 34 |
public modes: IModes; |
| 35 |
public decPrivateModes: IDecPrivateModes; |
| 36 |
|
| 37 |
private readonly _onData = this._register(new Emitter<string>()); |
| 38 |
public readonly onData = this._onData.event; |
| 39 |
private readonly _onUserInput = this._register(new Emitter<void>()); |
| 40 |
public readonly onUserInput = this._onUserInput.event; |
| 41 |
private readonly _onBinary = this._register(new Emitter<string>()); |
| 42 |
public readonly onBinary = this._onBinary.event; |
| 43 |
private readonly _onRequestScrollToBottom = this._register(new Emitter<void>()); |
| 44 |
public readonly onRequestScrollToBottom = this._onRequestScrollToBottom.event; |
| 45 |
|
| 46 |
constructor( |
| 47 |
@IBufferService private readonly _bufferService: IBufferService, |
| 48 |
@ILogService private readonly _logService: ILogService, |
| 49 |
@IOptionsService private readonly _optionsService: IOptionsService |
| 50 |
) { |
| 51 |
super(); |
| 52 |
this.modes = clone(DEFAULT_MODES); |
| 53 |
this.decPrivateModes = clone(DEFAULT_DEC_PRIVATE_MODES); |
| 54 |
} |
| 55 |
|
| 56 |
public reset(): void { |
| 57 |
this.modes = clone(DEFAULT_MODES); |
| 58 |
this.decPrivateModes = clone(DEFAULT_DEC_PRIVATE_MODES); |
| 59 |
} |
| 60 |
|
| 61 |
public triggerDataEvent(data: string, wasUserInput: boolean = false): void { |
| 62 |
// Prevents all events to pty process if stdin is disabled |
| 63 |
if (this._optionsService.rawOptions.disableStdin) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
// Input is being sent to the terminal, the terminal should focus the prompt. |
| 68 |
const buffer = this._bufferService.buffer; |
| 69 |
if (wasUserInput && this._optionsService.rawOptions.scrollOnUserInput && buffer.ybase !== buffer.ydisp) { |
| 70 |
this._onRequestScrollToBottom.fire(); |
| 71 |
} |
| 72 |
|
| 73 |
// Fire onUserInput so listeners can react as well (eg. clear selection) |
| 74 |
if (wasUserInput) { |
| 75 |
this._onUserInput.fire(); |
| 76 |
} |
| 77 |
|
| 78 |
// Fire onData API |
| 79 |
this._logService.debug(`sending data "${data}"`); |
| 80 |
this._logService.trace(`sending data (codes)`, () => data.split('').map(e => e.charCodeAt(0))); |
| 81 |
this._onData.fire(data); |
| 82 |
} |
| 83 |
|
| 84 |
public triggerBinaryEvent(data: string): void { |
| 85 |
if (this._optionsService.rawOptions.disableStdin) { |
| 86 |
return; |
| 87 |
} |
| 88 |
this._logService.debug(`sending binary "${data}"`); |
| 89 |
this._logService.trace(`sending binary (codes)`, () => data.split('').map(e => e.charCodeAt(0))); |
| 90 |
this._onBinary.fire(data); |
| 91 |
} |
| 92 |
} |