<commit_before>
import events from 'events';
import createLog from 'silk-log/device';
import fs from 'fs';
import * as util from 'silk-sysutils';
const log = createLog('input');

//
// Note: The InputDevice class has been inspired by
// https://github.com/Bornholm/node-keyboard (MIT)
//
class InputDevice extends events.EventEmitter {
  constructor(dev) {
    super();
    log.info(`InputDevice: ${dev}`);
    this.dev = dev;
    this.bufferSize = 16; // input event size for a 32-bit kernel ONLY
    this.buf = new Buffer(this.bufferSize);
    fs.open(this.dev, 'r', (err, fd) => {
      if (err) {
        log.warn(`Unable to open ${dev}`);
        return;
      }
      this.fd = fd;
      this._read();
    });
  }

  _read() {
    fs.read(this.fd, this.buf, 0, this.bufferSize, null,
      (err, bytesRead, buffer) => this._onRead(err, bytesRead, buffer));
  }

  _onRead(err, bytesRead, buffer) {
    if (err) {
      log.warn(`Unable to read ${this.dev}: ${err}`);
      return;
    }
    let event;
    if (buffer.readUInt16LE(8) === 1 /*EV_KEY*/ ) {

      /**
       * Key event type
       *
       * @name KeyEventType
       * @typedef {Object} KeyEventType
       *
       * @property {number} timeS Timestamp (Seconds part)
       * @property {number} timeMS  Timestamp (Microseconds part)
       * @property {number} keyCode Keyboard code
       * @memberof silk-input
       */
      event = {
        timeS: buffer.readUInt32LE(0),
        timeMS: buffer.readUInt32LE(4),
        keyCode: buffer.readUInt16LE(10),
        type: ['up', 'down', 'repeat'][buffer.readUInt32LE(12)],
      };
      const keys = {
        113: 'mute',
        114: 'volumedown',
        115: 'volumeup',
        116: 'power',
        224: 'brightnessdown',
        225: 'brightnessup',
        330: 'touch',
      };
      event.keyId = keys[event.keyCode];
    }

    if (event) {
      log.debug(`input event: ${event.type}, ${event.keyId}`);
      this.emit(event.type, event);
    }
    this._read();
  }
}

let inputDevices = {};

/**
 * Silk Input
 *
 * @module silk-input
 * @example
 * 'use strict';
 *
 * const Input = require('silk-input').default;
 * const log = require('silk-alog');
 *
 * let input = new Input();
 * input.on('down', e => log.info('Key down event', JSON.stringify(e));
 * input.on('up', e => log.info('Key up event', JSON.stringify(e));
 */
export default class Input extends events.EventEmitter {
  constructor() {
    super();

    const devices = util.getlistprop('ro.silk.ui.inputevents', null);
    if (devices) {
      this._open(devices);
    } else {<commit_msg>Adds missing closing parentheses to input example code.<commit_after>
import events from 'events';
import createLog from 'silk-log/device';
import fs from 'fs';
import * as util from 'silk-sysutils';
const log = createLog('input');

//
// Note: The InputDevice class has been inspired by
// https://github.com/Bornholm/node-keyboard (MIT)
//
class InputDevice extends events.EventEmitter {
  constructor(dev) {
    super();
    log.info(`InputDevice: ${dev}`);
    this.dev = dev;
    this.bufferSize = 16; // input event size for a 32-bit kernel ONLY
    this.buf = new Buffer(this.bufferSize);
    fs.open(this.dev, 'r', (err, fd) => {
      if (err) {
        log.warn(`Unable to open ${dev}`);
        return;
      }
      this.fd = fd;
      this._read();
    });
  }

  _read() {
    fs.read(this.fd, this.buf, 0, this.bufferSize, null,
      (err, bytesRead, buffer) => this._onRead(err, bytesRead, buffer));
  }

  _onRead(err, bytesRead, buffer) {
    if (err) {
      log.warn(`Unable to read ${this.dev}: ${err}`);
      return;
    }
    let event;
    if (buffer.readUInt16LE(8) === 1 /*EV_KEY*/ ) {

      /**
       * Key event type
       *
       * @name KeyEventType
       * @typedef {Object} KeyEventType
       *
       * @property {number} timeS Timestamp (Seconds part)
       * @property {number} timeMS  Timestamp (Microseconds part)
       * @property {number} keyCode Keyboard code
       * @memberof silk-input
       */
      event = {
        timeS: buffer.readUInt32LE(0),
        timeMS: buffer.readUInt32LE(4),
        keyCode: buffer.readUInt16LE(10),
        type: ['up', 'down', 'repeat'][buffer.readUInt32LE(12)],
      };
      const keys = {
        113: 'mute',
        114: 'volumedown',
        115: 'volumeup',
        116: 'power',
        224: 'brightnessdown',
        225: 'brightnessup',
        330: 'touch',
      };
      event.keyId = keys[event.keyCode];
    }

    if (event) {
      log.debug(`input event: ${event.type}, ${event.keyId}`);
      this.emit(event.type, event);
    }
    this._read();
  }
}

let inputDevices = {};

/**
 * Silk Input
 *
 * @module silk-input
 * @example
 * 'use strict';
 *
 * const Input = require('silk-input').default;
 * const log = require('silk-alog');
 *
 * let input = new Input();
 * input.on('down', e => log.info('Key down event', JSON.stringify(e)));
 * input.on('up', e => log.info('Key up event', JSON.stringify(e)));
 */
export default class Input extends events.EventEmitter {
  constructor() {
    super();

    const devices = util.getlistprop('ro.silk.ui.inputevents', null);
    if (devices) {
      this._open(devices);
    } else {
{'fraction_non_alphanumeric': 0.16361206567392134, 'fraction_numerical': 0.016800305460099276, 'mean_word_length': 2.4903397734843438, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2130094', 'n_tokens_mistral': 1795, 'n_tokens_neox': 1724, 'n_words': 425}