Skip to content

Commit a4b9d78

Browse files
notbigdataBuildJet
authored andcommitted
Auto detect hardware acceleration on Linux
1 parent a9c9936 commit a4b9d78

File tree

6 files changed

+40
-14
lines changed

6 files changed

+40
-14
lines changed

__tests__/input-validator.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,10 @@ describe('disable-linux-hw-accel validator tests', () => {
211211
const func = () => {
212212
validator.checkDisableLinuxHardwareAcceleration('yes');
213213
};
214-
expect(func).toThrowError(`Input for input.disable-linux-hw-accel should be either 'true' or 'false'.`);
214+
expect(func).toThrowError(`Input for input.disable-linux-hw-accel should be either 'true' or 'false' or 'auto'.`);
215215
});
216216

217-
it('Validates successfully if disable-linux-hw-accel is either true or false', () => {
217+
it('Validates successfully if disable-linux-hw-accel is either true or false or auto', () => {
218218
const func1 = () => {
219219
validator.checkDisableLinuxHardwareAcceleration('true');
220220
};
@@ -224,6 +224,11 @@ describe('disable-linux-hw-accel validator tests', () => {
224224
validator.checkDisableLinuxHardwareAcceleration('false');
225225
};
226226
expect(func2).not.toThrow();
227+
228+
const func3 = () => {
229+
validator.checkDisableLinuxHardwareAcceleration('auto');
230+
};
231+
expect(func3).not.toThrow();
227232
});
228233
});
229234

action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ inputs:
4343
description: 'whether to disable spellchecker - `true` or `false`'
4444
default: 'false'
4545
disable-linux-hw-accel:
46-
description: 'whether to disable hardware acceleration on Linux machines - `true` or `false`'
47-
default: 'true'
46+
description: 'whether to disable hardware acceleration on Linux machines - `true` or `false` or `auto`'
47+
default: 'auto'
4848
enable-hw-keyboard:
4949
description: 'whether to enable hardware keyboard - `true` or `false`.'
5050
default: 'false'

lib/input-validator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ function checkDisableSpellchecker(disableSpellchecker) {
5151
}
5252
exports.checkDisableSpellchecker = checkDisableSpellchecker;
5353
function checkDisableLinuxHardwareAcceleration(disableLinuxHardwareAcceleration) {
54-
if (!isValidBoolean(disableLinuxHardwareAcceleration)) {
55-
throw new Error(`Input for input.disable-linux-hw-accel should be either 'true' or 'false'.`);
54+
if (!(isValidBoolean(disableLinuxHardwareAcceleration) || disableLinuxHardwareAcceleration === 'auto')) {
55+
throw new Error(`Input for input.disable-linux-hw-accel should be either 'true' or 'false' or 'auto'.`);
5656
}
5757
}
5858
exports.checkDisableLinuxHardwareAcceleration = checkDisableLinuxHardwareAcceleration;

lib/main.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,22 @@ const emulator_manager_1 = require("./emulator-manager");
3535
const exec = __importStar(require("@actions/exec"));
3636
const script_parser_1 = require("./script-parser");
3737
const channel_id_mapper_1 = require("./channel-id-mapper");
38+
const fs_1 = require("fs");
3839
function run() {
3940
return __awaiter(this, void 0, void 0, function* () {
4041
try {
4142
console.log(`::group::Configure emulator`);
43+
let linuxSupportKVM = false;
4244
// only support running on macOS or Linux
4345
if (process.platform !== 'darwin') {
4446
if (process.platform === 'linux') {
45-
console.warn(`You're running a Linux VM where hardware acceleration is not available. Please consider using a macOS VM instead to take advantage of native hardware acceleration support provided by HAXM.`);
47+
try {
48+
fs_1.accessSync('/dev/kvm', fs_1.constants.R_OK | fs_1.constants.W_OK);
49+
linuxSupportKVM = true;
50+
}
51+
catch (_a) {
52+
console.warn(`You're running a Linux VM where hardware acceleration is not available. Please consider using a macOS VM instead to take advantage of native hardware acceleration support provided by HAXM.`);
53+
}
4654
}
4755
else {
4856
throw new Error('Unsupported virtual machine: please use either macos or ubuntu VM.');
@@ -102,8 +110,11 @@ function run() {
102110
const disableSpellchecker = disableSpellcheckerInput === 'true';
103111
console.log(`disable spellchecker: ${disableSpellchecker}`);
104112
// disable linux hardware acceleration
105-
const disableLinuxHardwareAccelerationInput = core.getInput('disable-linux-hw-accel');
113+
let disableLinuxHardwareAccelerationInput = core.getInput('disable-linux-hw-accel');
106114
input_validator_1.checkDisableLinuxHardwareAcceleration(disableLinuxHardwareAccelerationInput);
115+
if (disableLinuxHardwareAccelerationInput === 'auto' && process.platform === 'linux') {
116+
disableLinuxHardwareAccelerationInput = linuxSupportKVM ? 'false' : 'true';
117+
}
107118
const disableLinuxHardwareAcceleration = disableLinuxHardwareAccelerationInput === 'true';
108119
console.log(`disable Linux hardware acceleration: ${disableLinuxHardwareAcceleration}`);
109120
// enable hardware keyboard

src/input-validator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ export function checkDisableSpellchecker(disableSpellchecker: string): void {
4949
}
5050

5151
export function checkDisableLinuxHardwareAcceleration(disableLinuxHardwareAcceleration: string): void {
52-
if (!isValidBoolean(disableLinuxHardwareAcceleration)) {
53-
throw new Error(`Input for input.disable-linux-hw-accel should be either 'true' or 'false'.`);
52+
if (!(isValidBoolean(disableLinuxHardwareAcceleration) || disableLinuxHardwareAcceleration === 'auto')) {
53+
throw new Error(`Input for input.disable-linux-hw-accel should be either 'true' or 'false' or 'auto'.`);
5454
}
5555
}
5656

src/main.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,23 @@ import { launchEmulator, killEmulator } from './emulator-manager';
1717
import * as exec from '@actions/exec';
1818
import { parseScript } from './script-parser';
1919
import { getChannelId } from './channel-id-mapper';
20+
import { accessSync, constants } from 'fs';
2021

2122
async function run() {
2223
try {
2324
console.log(`::group::Configure emulator`);
25+
let linuxSupportKVM = false;
2426
// only support running on macOS or Linux
2527
if (process.platform !== 'darwin') {
2628
if (process.platform === 'linux') {
27-
console.warn(
28-
`You're running a Linux VM where hardware acceleration is not available. Please consider using a macOS VM instead to take advantage of native hardware acceleration support provided by HAXM.`
29-
);
29+
try {
30+
accessSync('/dev/kvm', constants.R_OK | constants.W_OK);
31+
linuxSupportKVM = true;
32+
} catch {
33+
console.warn(
34+
`You're running a Linux VM where hardware acceleration is not available. Please consider using a macOS VM instead to take advantage of native hardware acceleration support provided by HAXM.`
35+
);
36+
}
3037
} else {
3138
throw new Error('Unsupported virtual machine: please use either macos or ubuntu VM.');
3239
}
@@ -100,8 +107,11 @@ async function run() {
100107
console.log(`disable spellchecker: ${disableSpellchecker}`);
101108

102109
// disable linux hardware acceleration
103-
const disableLinuxHardwareAccelerationInput = core.getInput('disable-linux-hw-accel');
110+
let disableLinuxHardwareAccelerationInput = core.getInput('disable-linux-hw-accel');
104111
checkDisableLinuxHardwareAcceleration(disableLinuxHardwareAccelerationInput);
112+
if (disableLinuxHardwareAccelerationInput === 'auto' && process.platform === 'linux') {
113+
disableLinuxHardwareAccelerationInput = linuxSupportKVM ? 'false' : 'true';
114+
}
105115
const disableLinuxHardwareAcceleration = disableLinuxHardwareAccelerationInput === 'true';
106116
console.log(`disable Linux hardware acceleration: ${disableLinuxHardwareAcceleration}`);
107117

0 commit comments

Comments
 (0)