Guest User

Untitled

a guest
Jun 18th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.30 KB | None | 0 0
  1. //
  2. // Copyright 2005 The Android Open Source Project
  3. //
  4. // Handle events, like key input and vsync.
  5. //
  6. // The goal is to provide an optimized solution for Linux, not an
  7. // implementation that works well across all platforms. We expect
  8. // events to arrive on file descriptors, so that we can use a select()
  9. // select() call to sleep.
  10. //
  11. // We can't select() on anything but network sockets in Windows, so we
  12. // provide an alternative implementation of waitEvent for that platform.
  13. //
  14. #define LOG_TAG "EventHub"
  15.  
  16. //#define LOG_NDEBUG 0
  17.  
  18. #include <ui/EventHub.h>
  19. #include <ui/KeycodeLabels.h>
  20. #include <hardware_legacy/power.h>
  21.  
  22. #include <cutils/properties.h>
  23. #include <utils/Log.h>
  24. #include <utils/Timers.h>
  25. #include <utils/threads.h>
  26. #include <utils/Errors.h>
  27.  
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <unistd.h>
  31. #include <fcntl.h>
  32. #include <memory.h>
  33. #include <errno.h>
  34. #include <assert.h>
  35.  
  36. #include "KeyLayoutMap.h"
  37.  
  38. #include <string.h>
  39. #include <stdint.h>
  40. #include <dirent.h>
  41. #ifdef HAVE_INOTIFY
  42. # include <sys/inotify.h>
  43. #endif
  44. #ifdef HAVE_ANDROID_OS
  45. # include <sys/limits.h> /* not part of Linux */
  46. #endif
  47. #include <sys/poll.h>
  48. #include <sys/ioctl.h>
  49.  
  50. /* this macro is used to tell if "bit" is set in "array"
  51. * it selects a byte from the array, and does a boolean AND
  52. * operation with a byte that only has the relevant bit set.
  53. * eg. to check for the 12th bit, we do (array[1] & 1<<4)
  54. */
  55. #define test_bit(bit, array) (array[bit/8] & (1<<(bit%8)))
  56.  
  57. #define ID_MASK 0x0000ffff
  58. #define SEQ_MASK 0x7fff0000
  59. #define SEQ_SHIFT 16
  60. #define id_to_index(id) ((id&ID_MASK)+1)
  61.  
  62. #ifndef ABS_MT_TOUCH_MAJOR
  63. #define ABS_MT_TOUCH_MAJOR 0x30 /* Major axis of touching ellipse */
  64. #endif
  65.  
  66. #ifndef ABS_MT_POSITION_X
  67. #define ABS_MT_POSITION_X 0x35 /* Center X ellipse position */
  68. #endif
  69.  
  70. #ifndef ABS_MT_POSITION_Y
  71. #define ABS_MT_POSITION_Y 0x36 /* Center Y ellipse position */
  72. #endif
  73.  
  74. namespace android {
  75.  
  76. static const char *WAKE_LOCK_ID = "KeyEvents";
  77. static const char *device_path = "/dev/input";
  78.  
  79. /* return the larger integer */
  80. static inline int max(int v1, int v2)
  81. {
  82. return (v1 > v2) ? v1 : v2;
  83. }
  84.  
  85. EventHub::device_t::device_t(int32_t _id, const char* _path, const char* name)
  86. : id(_id), path(_path), name(name), classes(0)
  87. , keyBitmask(NULL), layoutMap(new KeyLayoutMap()), next(NULL) {
  88. }
  89.  
  90. EventHub::device_t::~device_t() {
  91. delete [] keyBitmask;
  92. delete layoutMap;
  93. }
  94.  
  95. EventHub::EventHub(void)
  96. : mError(NO_INIT), mHaveFirstKeyboard(false), mFirstKeyboardId(0)
  97. , mDevicesById(0), mNumDevicesById(0)
  98. , mOpeningDevices(0), mClosingDevices(0)
  99. , mDevices(0), mFDs(0), mFDCount(0), mOpened(false)
  100. {
  101. acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
  102. #ifdef EV_SW
  103. memset(mSwitches, 0, sizeof(mSwitches));
  104. #endif
  105. }
  106.  
  107. /*
  108. * Clean up.
  109. */
  110. EventHub::~EventHub(void)
  111. {
  112. release_wake_lock(WAKE_LOCK_ID);
  113. // we should free stuff here...
  114. }
  115.  
  116. status_t EventHub::errorCheck() const
  117. {
  118. return mError;
  119. }
  120.  
  121. String8 EventHub::getDeviceName(int32_t deviceId) const
  122. {
  123. AutoMutex _l(mLock);
  124. device_t* device = getDevice(deviceId);
  125. if (device == NULL) return String8();
  126. return device->name;
  127. }
  128.  
  129. uint32_t EventHub::getDeviceClasses(int32_t deviceId) const
  130. {
  131. AutoMutex _l(mLock);
  132. device_t* device = getDevice(deviceId);
  133. if (device == NULL) return 0;
  134. return device->classes;
  135. }
  136.  
  137. int EventHub::getAbsoluteInfo(int32_t deviceId, int axis, int *outMinValue,
  138. int* outMaxValue, int* outFlat, int* outFuzz) const
  139. {
  140. AutoMutex _l(mLock);
  141. device_t* device = getDevice(deviceId);
  142. if (device == NULL) return -1;
  143.  
  144. struct input_absinfo info;
  145.  
  146. if(ioctl(mFDs[id_to_index(device->id)].fd, EVIOCGABS(axis), &info)) {
  147. LOGE("Error reading absolute controller %d for device %s fd %d\n",
  148. axis, device->name.string(), mFDs[id_to_index(device->id)].fd);
  149. return -1;
  150. }
  151. *outMinValue = info.minimum;
  152. *outMaxValue = info.maximum;
  153. *outFlat = info.flat;
  154. *outFuzz = info.fuzz;
  155. return 0;
  156. }
  157.  
  158. int EventHub::getSwitchState(int sw) const
  159. {
  160. #ifdef EV_SW
  161. if (sw >= 0 && sw <= SW_MAX) {
  162. int32_t devid = mSwitches[sw];
  163. if (devid != 0) {
  164. return getSwitchState(devid, sw);
  165. }
  166. }
  167. #endif
  168. return -1;
  169. }
  170.  
  171. int EventHub::getSwitchState(int32_t deviceId, int sw) const
  172. {
  173. #ifdef EV_SW
  174. AutoMutex _l(mLock);
  175. device_t* device = getDevice(deviceId);
  176. if (device == NULL) return -1;
  177.  
  178. if (sw >= 0 && sw <= SW_MAX) {
  179. uint8_t sw_bitmask[(SW_MAX+1)/8];
  180. memset(sw_bitmask, 0, sizeof(sw_bitmask));
  181. if (ioctl(mFDs[id_to_index(device->id)].fd,
  182. EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) {
  183. return test_bit(sw, sw_bitmask) ? 1 : 0;
  184. }
  185. }
  186. #endif
  187.  
  188. return -1;
  189. }
  190.  
  191. int EventHub::getScancodeState(int code) const
  192. {
  193. return getScancodeState(mFirstKeyboardId, code);
  194. }
  195.  
  196. int EventHub::getScancodeState(int32_t deviceId, int code) const
  197. {
  198. AutoMutex _l(mLock);
  199. device_t* device = getDevice(deviceId);
  200. if (device == NULL) return -1;
  201.  
  202. if (code >= 0 && code <= KEY_MAX) {
  203. uint8_t key_bitmask[(KEY_MAX+1)/8];
  204. memset(key_bitmask, 0, sizeof(key_bitmask));
  205. if (ioctl(mFDs[id_to_index(device->id)].fd,
  206. EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
  207. return test_bit(code, key_bitmask) ? 1 : 0;
  208. }
  209. }
  210.  
  211. return -1;
  212. }
  213.  
  214. int EventHub::getKeycodeState(int code) const
  215. {
  216. return getKeycodeState(mFirstKeyboardId, code);
  217. }
  218.  
  219. int EventHub::getKeycodeState(int32_t deviceId, int code) const
  220. {
  221. AutoMutex _l(mLock);
  222. device_t* device = getDevice(deviceId);
  223. if (device == NULL || device->layoutMap == NULL) return -1;
  224.  
  225. Vector<int32_t> scanCodes;
  226. device->layoutMap->findScancodes(code, &scanCodes);
  227.  
  228. uint8_t key_bitmask[(KEY_MAX+1)/8];
  229. memset(key_bitmask, 0, sizeof(key_bitmask));
  230. if (ioctl(mFDs[id_to_index(device->id)].fd,
  231. EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
  232. #if 0
  233. for (size_t i=0; i<=KEY_MAX; i++) {
  234. LOGI("(Scan code %d: down=%d)", i, test_bit(i, key_bitmask));
  235. }
  236. #endif
  237. const size_t N = scanCodes.size();
  238. for (size_t i=0; i<N && i<=KEY_MAX; i++) {
  239. int32_t sc = scanCodes.itemAt(i);
  240. //LOGI("Code %d: down=%d", sc, test_bit(sc, key_bitmask));
  241. if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, key_bitmask)) {
  242. return 1;
  243. }
  244. }
  245. }
  246.  
  247. return 0;
  248. }
  249.  
  250. status_t EventHub::scancodeToKeycode(int32_t deviceId, int scancode,
  251. int32_t* outKeycode, uint32_t* outFlags) const
  252. {
  253. AutoMutex _l(mLock);
  254. device_t* device = getDevice(deviceId);
  255.  
  256. if (device != NULL && device->layoutMap != NULL) {
  257. status_t err = device->layoutMap->map(scancode, outKeycode, outFlags);
  258. if (err == NO_ERROR) {
  259. return NO_ERROR;
  260. }
  261. }
  262.  
  263. if (mHaveFirstKeyboard) {
  264. device = getDevice(mFirstKeyboardId);
  265.  
  266. if (device != NULL && device->layoutMap != NULL) {
  267. status_t err = device->layoutMap->map(scancode, outKeycode, outFlags);
  268. if (err == NO_ERROR) {
  269. return NO_ERROR;
  270. }
  271. }
  272. }
  273.  
  274. *outKeycode = 0;
  275. *outFlags = 0;
  276. return NAME_NOT_FOUND;
  277. }
  278.  
  279. void EventHub::addExcludedDevice(const char* deviceName)
  280. {
  281. String8 name(deviceName);
  282. mExcludedDevices.push_back(name);
  283. }
  284.  
  285. EventHub::device_t* EventHub::getDevice(int32_t deviceId) const
  286. {
  287. if (deviceId == 0) deviceId = mFirstKeyboardId;
  288. int32_t id = deviceId & ID_MASK;
  289. if (id >= mNumDevicesById || id < 0) return NULL;
  290. device_t* dev = mDevicesById[id].device;
  291. if (dev == NULL) return NULL;
  292. if (dev->id == deviceId) {
  293. return dev;
  294. }
  295. return NULL;
  296. }
  297.  
  298. bool EventHub::getEvent(int32_t* outDeviceId, int32_t* outType,
  299. int32_t* outScancode, int32_t* outKeycode, uint32_t *outFlags,
  300. int32_t* outValue, nsecs_t* outWhen)
  301. {
  302. *outDeviceId = 0;
  303. *outType = 0;
  304. *outScancode = 0;
  305. *outKeycode = 0;
  306. *outFlags = 0;
  307. *outValue = 0;
  308. *outWhen = 0;
  309.  
  310. status_t err;
  311.  
  312. fd_set readfds;
  313. int maxFd = -1;
  314. int cc;
  315. int i;
  316. int res;
  317. int pollres;
  318. struct input_event iev;
  319.  
  320. // Note that we only allow one caller to getEvent(), so don't need
  321. // to do locking here... only when adding/removing devices.
  322.  
  323. if (!mOpened) {
  324. mError = openPlatformInput() ? NO_ERROR : UNKNOWN_ERROR;
  325. mOpened = true;
  326. }
  327.  
  328. while(1) {
  329.  
  330. // First, report any devices that had last been added/removed.
  331. if (mClosingDevices != NULL) {
  332. device_t* device = mClosingDevices;
  333. LOGV("Reporting device closed: id=0x%x, name=%s\n",
  334. device->id, device->path.string());
  335. mClosingDevices = device->next;
  336. *outDeviceId = device->id;
  337. if (*outDeviceId == mFirstKeyboardId) *outDeviceId = 0;
  338. *outType = DEVICE_REMOVED;
  339. delete device;
  340. return true;
  341. }
  342. if (mOpeningDevices != NULL) {
  343. device_t* device = mOpeningDevices;
  344. LOGV("Reporting device opened: id=0x%x, name=%s\n",
  345. device->id, device->path.string());
  346. mOpeningDevices = device->next;
  347. *outDeviceId = device->id;
  348. if (*outDeviceId == mFirstKeyboardId) *outDeviceId = 0;
  349. *outType = DEVICE_ADDED;
  350. return true;
  351. }
  352.  
  353. release_wake_lock(WAKE_LOCK_ID);
  354.  
  355. pollres = poll(mFDs, mFDCount, -1);
  356.  
  357. acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
  358.  
  359. if (pollres <= 0) {
  360. if (errno != EINTR) {
  361. LOGW("select failed (errno=%d)\n", errno);
  362. usleep(100000);
  363. }
  364. continue;
  365. }
  366.  
  367. //printf("poll %d, returned %d\n", mFDCount, pollres);
  368.  
  369. // mFDs[0] is used for inotify, so process regular events starting at mFDs[1]
  370. for(i = 1; i < mFDCount; i++) {
  371. if(mFDs[i].revents) {
  372. // MODIFICATION: LOGVs
  373. LOGI("revents for %d = 0x%08x", i, mFDs[i].revents);
  374. if(mFDs[i].revents & POLLIN) {
  375. res = read(mFDs[i].fd, &iev, sizeof(iev));
  376. if (res == sizeof(iev)) {
  377. LOGI("%s got: t0=%d, t1=%d, type=%d, code=%d, v=%d",
  378. mDevices[i]->path.string(),
  379. (int) iev.time.tv_sec, (int) iev.time.tv_usec,
  380. iev.type, iev.code, iev.value);
  381. *outDeviceId = mDevices[i]->id;
  382. if (*outDeviceId == mFirstKeyboardId) *outDeviceId = 0;
  383. *outType = iev.type;
  384. *outScancode = iev.code;
  385. if (iev.type == EV_KEY) {
  386. err = mDevices[i]->layoutMap->map(iev.code, outKeycode, outFlags);
  387. LOGI("iev.code=%d outKeycode=%d outFlags=0x%08x err=%d\n",
  388. iev.code, *outKeycode, *outFlags, err);
  389. if (err != 0) {
  390. *outKeycode = 0;
  391. *outFlags = 0;
  392. }
  393. } else {
  394. *outKeycode = iev.code;
  395. }
  396. *outValue = iev.value;
  397. *outWhen = s2ns(iev.time.tv_sec) + us2ns(iev.time.tv_usec);
  398. return true;
  399. } else {
  400. if (res<0) {
  401. LOGW("could not get event (errno=%d)", errno);
  402. } else {
  403. LOGE("could not get event (wrong size: %d)", res);
  404. }
  405. continue;
  406. }
  407. }
  408. }
  409. }
  410.  
  411. // read_notify() will modify mFDs and mFDCount, so this must be done after
  412. // processing all other events.
  413. if(mFDs[0].revents & POLLIN) {
  414. read_notify(mFDs[0].fd);
  415. }
  416. }
  417. }
  418.  
  419. /*
  420. * Open the platform-specific input device.
  421. */
  422. bool EventHub::openPlatformInput(void)
  423. {
  424. /*
  425. * Open platform-specific input device(s).
  426. */
  427. int res;
  428.  
  429. mFDCount = 1;
  430. mFDs = (pollfd *)calloc(1, sizeof(mFDs[0]));
  431. mDevices = (device_t **)calloc(1, sizeof(mDevices[0]));
  432. mFDs[0].events = POLLIN;
  433. mDevices[0] = NULL;
  434. #ifdef HAVE_INOTIFY
  435. mFDs[0].fd = inotify_init();
  436. res = inotify_add_watch(mFDs[0].fd, device_path, IN_DELETE | IN_CREATE);
  437. if(res < 0) {
  438. LOGE("could not add watch for %s, %s\n", device_path, strerror(errno));
  439. }
  440. #else
  441. /*
  442. * The code in EventHub::getEvent assumes that mFDs[0] is an inotify fd.
  443. * We allocate space for it and set it to something invalid.
  444. */
  445. mFDs[0].fd = -1;
  446. #endif
  447.  
  448. res = scan_dir(device_path);
  449. if(res < 0) {
  450. LOGE("scan dir failed for %s\n", device_path);
  451. //open_device("/dev/input/event0");
  452. }
  453.  
  454. return true;
  455. }
  456.  
  457. /*
  458. * Inspect the known devices to determine whether physical keys exist for the given
  459. * framework-domain key codes.
  460. */
  461. bool EventHub::hasKeys(size_t numCodes, int32_t* keyCodes, uint8_t* outFlags) {
  462. for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
  463. outFlags[codeIndex] = 0;
  464.  
  465. // check each available hardware device for support for this keycode
  466. Vector<int32_t> scanCodes;
  467. for (int n = 0; (n < mFDCount) && (outFlags[codeIndex] == 0); n++) {
  468. if (mDevices[n]) {
  469. status_t err = mDevices[n]->layoutMap->findScancodes(keyCodes[codeIndex], &scanCodes);
  470. if (!err) {
  471. // check the possible scan codes identified by the layout map against the
  472. // map of codes actually emitted by the driver
  473. for (size_t sc = 0; sc < scanCodes.size(); sc++) {
  474. if (test_bit(scanCodes[sc], mDevices[n]->keyBitmask)) {
  475. outFlags[codeIndex] = 1;
  476. break;
  477. }
  478. }
  479. }
  480. }
  481. }
  482. }
  483.  
  484. return true;
  485. }
  486.  
  487. // ----------------------------------------------------------------------------
  488.  
  489. int EventHub::open_device(const char *deviceName)
  490. {
  491. int version;
  492. int fd;
  493. int attempt;
  494. struct pollfd *new_mFDs;
  495. device_t **new_devices;
  496. char **new_device_names;
  497. char name[80];
  498. char location[80];
  499. char idstr[80];
  500. struct input_id id;
  501.  
  502. LOGV("Opening device: %s", deviceName);
  503.  
  504. AutoMutex _l(mLock);
  505.  
  506. for (attempt = 0; attempt < 10; attempt++) {
  507. fd = open(deviceName, O_RDWR);
  508. if (fd >= 0) break;
  509. usleep(100);
  510. }
  511. if(fd < 0) {
  512. LOGE("could not open %s, %s\n", deviceName, strerror(errno));
  513. return -1;
  514. }
  515. LOGV("Opened device: %s (%d failures)", deviceName, attempt);
  516.  
  517. if(ioctl(fd, EVIOCGVERSION, &version)) {
  518. LOGE("could not get driver version for %s, %s\n", deviceName, strerror(errno));
  519. return -1;
  520. }
  521. if(ioctl(fd, EVIOCGID, &id)) {
  522. LOGE("could not get driver id for %s, %s\n", deviceName, strerror(errno));
  523. return -1;
  524. }
  525. name[sizeof(name) - 1] = '\0';
  526. location[sizeof(location) - 1] = '\0';
  527. idstr[sizeof(idstr) - 1] = '\0';
  528. if(ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
  529. //fprintf(stderr, "could not get device name for %s, %s\n", deviceName, strerror(errno));
  530. name[0] = '\0';
  531. }
  532.  
  533. // check to see if the device is on our excluded list
  534. List<String8>::iterator iter = mExcludedDevices.begin();
  535. List<String8>::iterator end = mExcludedDevices.end();
  536. for ( ; iter != end; iter++) {
  537. const char* test = *iter;
  538. if (strcmp(name, test) == 0) {
  539. LOGI("ignoring event id %s driver %s\n", deviceName, test);
  540. close(fd);
  541. fd = -1;
  542. return -1;
  543. }
  544. }
  545.  
  546. if(ioctl(fd, EVIOCGPHYS(sizeof(location) - 1), &location) < 1) {
  547. //fprintf(stderr, "could not get location for %s, %s\n", deviceName, strerror(errno));
  548. location[0] = '\0';
  549. }
  550. if(ioctl(fd, EVIOCGUNIQ(sizeof(idstr) - 1), &idstr) < 1) {
  551. //fprintf(stderr, "could not get idstring for %s, %s\n", deviceName, strerror(errno));
  552. idstr[0] = '\0';
  553. }
  554.  
  555. int devid = 0;
  556. while (devid < mNumDevicesById) {
  557. if (mDevicesById[devid].device == NULL) {
  558. break;
  559. }
  560. devid++;
  561. }
  562. if (devid >= mNumDevicesById) {
  563. device_ent* new_devids = (device_ent*)realloc(mDevicesById,
  564. sizeof(mDevicesById[0]) * (devid + 1));
  565. if (new_devids == NULL) {
  566. LOGE("out of memory");
  567. return -1;
  568. }
  569. mDevicesById = new_devids;
  570. mNumDevicesById = devid+1;
  571. mDevicesById[devid].device = NULL;
  572. mDevicesById[devid].seq = 0;
  573. }
  574.  
  575. mDevicesById[devid].seq = (mDevicesById[devid].seq+(1<<SEQ_SHIFT))&SEQ_MASK;
  576. if (mDevicesById[devid].seq == 0) {
  577. mDevicesById[devid].seq = 1<<SEQ_SHIFT;
  578. }
  579.  
  580. new_mFDs = (pollfd*)realloc(mFDs, sizeof(mFDs[0]) * (mFDCount + 1));
  581. new_devices = (device_t**)realloc(mDevices, sizeof(mDevices[0]) * (mFDCount + 1));
  582. if (new_mFDs == NULL || new_devices == NULL) {
  583. LOGE("out of memory");
  584. return -1;
  585. }
  586. mFDs = new_mFDs;
  587. mDevices = new_devices;
  588.  
  589. #if 0
  590. LOGI("add device %d: %s\n", mFDCount, deviceName);
  591. LOGI(" bus: %04x\n"
  592. " vendor %04x\n"
  593. " product %04x\n"
  594. " version %04x\n",
  595. id.bustype, id.vendor, id.product, id.version);
  596. LOGI(" name: \"%s\"\n", name);
  597. LOGI(" location: \"%s\"\n"
  598. " id: \"%s\"\n", location, idstr);
  599. LOGI(" version: %d.%d.%d\n",
  600. version >> 16, (version >> 8) & 0xff, version & 0xff);
  601. #endif
  602.  
  603. device_t* device = new device_t(devid|mDevicesById[devid].seq, deviceName, name);
  604. if (device == NULL) {
  605. LOGE("out of memory");
  606. return -1;
  607. }
  608.  
  609. mFDs[mFDCount].fd = fd;
  610. mFDs[mFDCount].events = POLLIN;
  611.  
  612. // figure out the kinds of events the device reports
  613.  
  614. // See if this is a keyboard, and classify it.
  615. uint8_t key_bitmask[(KEY_MAX+1)/8];
  616. memset(key_bitmask, 0, sizeof(key_bitmask));
  617. LOGV("Getting keys...");
  618. if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask) >= 0) {
  619. //LOGI("MAP\n");
  620. //for (int i=0; i<((KEY_MAX+1)/8); i++) {
  621. // LOGI("%d: 0x%02x\n", i, key_bitmask[i]);
  622. //}
  623. for (int i=0; i<((BTN_MISC+7)/8); i++) {
  624. if (key_bitmask[i] != 0) {
  625. device->classes |= CLASS_KEYBOARD;
  626. break;
  627. }
  628. }
  629. if ((device->classes & CLASS_KEYBOARD) != 0) {
  630. device->keyBitmask = new uint8_t[sizeof(key_bitmask)];
  631. if (device->keyBitmask != NULL) {
  632. memcpy(device->keyBitmask, key_bitmask, sizeof(key_bitmask));
  633. } else {
  634. delete device;
  635. LOGE("out of memory allocating key bitmask");
  636. return -1;
  637. }
  638. }
  639. }
  640.  
  641. // See if this is a trackball.
  642. if (test_bit(BTN_MOUSE, key_bitmask)) {
  643. uint8_t rel_bitmask[(REL_MAX+1)/8];
  644. memset(rel_bitmask, 0, sizeof(rel_bitmask));
  645. LOGV("Getting relative controllers...");
  646. if (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask) >= 0)
  647. {
  648. if (test_bit(REL_X, rel_bitmask) && test_bit(REL_Y, rel_bitmask)) {
  649. device->classes |= CLASS_TRACKBALL;
  650. }
  651. }
  652. }
  653.  
  654. uint8_t abs_bitmask[(ABS_MAX+1)/8];
  655. memset(abs_bitmask, 0, sizeof(abs_bitmask));
  656. LOGV("Getting absolute controllers...");
  657. ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask);
  658.  
  659. // MODIFICATION:
  660. LOGI("EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)): 0x%x\n", EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)));
  661. for(int i=0;i<sizeof(abs_bitmask);++i){
  662. LOGI("abs_bitmask[%d]:0x%x\n", i, abs_bitmask[i]);
  663. }
  664. if(test_bit(ABS_MT_TOUCH_MAJOR, abs_bitmask)){
  665. LOGI("open_device - - - ABS_MT_TOUCH_MAJOR");
  666. }
  667. if(test_bit(ABS_MT_TOUCH_MAJOR, abs_bitmask)){
  668. LOGI("open_device - - - ABS_MT_POSITION_X");
  669. }
  670. if(test_bit(ABS_MT_TOUCH_MAJOR, abs_bitmask)){
  671. LOGI("open_device - - - ABS_MT_POSITION_Y");
  672. }
  673. //////////////////////////////////////////////
  674.  
  675. // Is this a new modern multi-touch driver?
  676. if (test_bit(ABS_MT_TOUCH_MAJOR, abs_bitmask)
  677. && test_bit(ABS_MT_POSITION_X, abs_bitmask)
  678. && test_bit(ABS_MT_POSITION_Y, abs_bitmask)) {
  679. device->classes |= CLASS_TOUCHSCREEN | CLASS_TOUCHSCREEN_MT;
  680.  
  681. // Is this an old style single-touch driver?
  682. } else if (test_bit(BTN_TOUCH, key_bitmask)
  683. && test_bit(ABS_X, abs_bitmask) && test_bit(ABS_Y, abs_bitmask)) {
  684. device->classes |= CLASS_TOUCHSCREEN;
  685. }
  686.  
  687. #ifdef EV_SW
  688. // figure out the switches this device reports
  689. uint8_t sw_bitmask[(SW_MAX+1)/8];
  690. memset(sw_bitmask, 0, sizeof(sw_bitmask));
  691. if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof(sw_bitmask)), sw_bitmask) >= 0) {
  692. for (int i=0; i<EV_SW; i++) {
  693. //LOGI("Device 0x%x sw %d: has=%d", device->id, i, test_bit(i, sw_bitmask));
  694. if (test_bit(i, sw_bitmask)) {
  695. if (mSwitches[i] == 0) {
  696. mSwitches[i] = device->id;
  697. }
  698. }
  699. }
  700. }
  701. #endif
  702.  
  703. if ((device->classes&CLASS_KEYBOARD) != 0) {
  704. char tmpfn[sizeof(name)];
  705. char keylayoutFilename[300];
  706.  
  707. // a more descriptive name
  708. device->name = name;
  709.  
  710. // replace all the spaces with underscores
  711. strcpy(tmpfn, name);
  712. for (char *p = strchr(tmpfn, ' '); p && *p; p = strchr(tmpfn, ' '))
  713. *p = '_';
  714.  
  715. // find the .kl file we need for this device
  716. const char* root = getenv("ANDROID_ROOT");
  717. snprintf(keylayoutFilename, sizeof(keylayoutFilename),
  718. "%s/usr/keylayout/%s.kl", root, tmpfn);
  719. bool defaultKeymap = false;
  720. if (access(keylayoutFilename, R_OK)) {
  721. snprintf(keylayoutFilename, sizeof(keylayoutFilename),
  722. "%s/usr/keylayout/%s", root, "qwerty.kl");
  723. defaultKeymap = true;
  724. }
  725. device->layoutMap->load(keylayoutFilename);
  726.  
  727. // tell the world about the devname (the descriptive name)
  728. int32_t publicID;
  729. if (!mHaveFirstKeyboard && !defaultKeymap) {
  730. publicID = 0;
  731. // the built-in keyboard has a well-known device ID of 0,
  732. // this device better not go away.
  733. mHaveFirstKeyboard = true;
  734. mFirstKeyboardId = device->id;
  735. } else {
  736. publicID = device->id;
  737. // ensure mFirstKeyboardId is set to -something-.
  738. if (mFirstKeyboardId == 0) {
  739. mFirstKeyboardId = device->id;
  740. }
  741. }
  742. char propName[100];
  743. sprintf(propName, "hw.keyboards.%u.devname", publicID);
  744. property_set(propName, name);
  745.  
  746. // 'Q' key support = cheap test of whether this is an alpha-capable kbd
  747. if (hasKeycode(device, kKeyCodeQ)) {
  748. device->classes |= CLASS_ALPHAKEY;
  749. }
  750.  
  751. // See if this has a DPAD.
  752. if (hasKeycode(device, kKeyCodeDpadUp) &&
  753. hasKeycode(device, kKeyCodeDpadDown) &&
  754. hasKeycode(device, kKeyCodeDpadLeft) &&
  755. hasKeycode(device, kKeyCodeDpadRight) &&
  756. hasKeycode(device, kKeyCodeDpadCenter)) {
  757. device->classes |= CLASS_DPAD;
  758. }
  759.  
  760. LOGI("New keyboard: publicID=%d device->id=0x%x devname='%s' propName='%s' keylayout='%s'\n",
  761. publicID, device->id, name, propName, keylayoutFilename);
  762. }
  763.  
  764. LOGI("New device: path=%s name=%s id=0x%x (of 0x%x) index=%d fd=%d classes=0x%x\n",
  765. deviceName, name, device->id, mNumDevicesById, mFDCount, fd, device->classes);
  766.  
  767. LOGV("Adding device %s %p at %d, id = %d, classes = 0x%x\n",
  768. deviceName, device, mFDCount, devid, device->classes);
  769.  
  770. mDevicesById[devid].device = device;
  771. device->next = mOpeningDevices;
  772. mOpeningDevices = device;
  773. mDevices[mFDCount] = device;
  774.  
  775. mFDCount++;
  776. return 0;
  777. }
  778.  
  779. bool EventHub::hasKeycode(device_t* device, int keycode) const
  780. {
  781. if (device->keyBitmask == NULL || device->layoutMap == NULL) {
  782. return false;
  783. }
  784.  
  785. Vector<int32_t> scanCodes;
  786. device->layoutMap->findScancodes(keycode, &scanCodes);
  787. const size_t N = scanCodes.size();
  788. for (size_t i=0; i<N && i<=KEY_MAX; i++) {
  789. int32_t sc = scanCodes.itemAt(i);
  790. if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
  791. return true;
  792. }
  793. }
  794.  
  795. return false;
  796. }
  797.  
  798. int EventHub::close_device(const char *deviceName)
  799. {
  800. AutoMutex _l(mLock);
  801.  
  802. int i;
  803. for(i = 1; i < mFDCount; i++) {
  804. if(strcmp(mDevices[i]->path.string(), deviceName) == 0) {
  805. //LOGD("remove device %d: %s\n", i, deviceName);
  806. device_t* device = mDevices[i];
  807.  
  808. LOGI("Removed device: path=%s name=%s id=0x%x (of 0x%x) index=%d fd=%d classes=0x%x\n",
  809. device->path.string(), device->name.string(), device->id,
  810. mNumDevicesById, mFDCount, mFDs[i].fd, device->classes);
  811.  
  812. // Clear this device's entry.
  813. int index = (device->id&ID_MASK);
  814. mDevicesById[index].device = NULL;
  815.  
  816. // Close the file descriptor and compact the fd array.
  817. close(mFDs[i].fd);
  818. int count = mFDCount - i - 1;
  819. memmove(mDevices + i, mDevices + i + 1, sizeof(mDevices[0]) * count);
  820. memmove(mFDs + i, mFDs + i + 1, sizeof(mFDs[0]) * count);
  821. mFDCount--;
  822.  
  823. #ifdef EV_SW
  824. for (int j=0; j<EV_SW; j++) {
  825. if (mSwitches[j] == device->id) {
  826. mSwitches[j] = 0;
  827. }
  828. }
  829. #endif
  830.  
  831. device->next = mClosingDevices;
  832. mClosingDevices = device;
  833.  
  834. uint32_t publicID;
  835. if (device->id == mFirstKeyboardId) {
  836. LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
  837. device->path.string(), mFirstKeyboardId);
  838. mFirstKeyboardId = 0;
  839. publicID = 0;
  840. } else {
  841. publicID = device->id;
  842. }
  843. // clear the property
  844. char propName[100];
  845. sprintf(propName, "hw.keyboards.%u.devname", publicID);
  846. property_set(propName, NULL);
  847. return 0;
  848. }
  849. }
  850. LOGE("remove device: %s not found\n", deviceName);
  851. return -1;
  852. }
  853.  
  854. int EventHub::read_notify(int nfd)
  855. {
  856. #ifdef HAVE_INOTIFY
  857. int res;
  858. char devname[PATH_MAX];
  859. char *filename;
  860. char event_buf[512];
  861. int event_size;
  862. int event_pos = 0;
  863. struct inotify_event *event;
  864.  
  865. LOGV("EventHub::read_notify nfd: %d\n", nfd);
  866. res = read(nfd, event_buf, sizeof(event_buf));
  867. if(res < (int)sizeof(*event)) {
  868. if(errno == EINTR)
  869. return 0;
  870. LOGW("could not get event, %s\n", strerror(errno));
  871. return 1;
  872. }
  873. //printf("got %d bytes of event information\n", res);
  874.  
  875. strcpy(devname, device_path);
  876. filename = devname + strlen(devname);
  877. *filename++ = '/';
  878.  
  879. while(res >= (int)sizeof(*event)) {
  880. event = (struct inotify_event *)(event_buf + event_pos);
  881. //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
  882. if(event->len) {
  883. strcpy(filename, event->name);
  884. if(event->mask & IN_CREATE) {
  885. open_device(devname);
  886. }
  887. else {
  888. close_device(devname);
  889. }
  890. }
  891. event_size = sizeof(*event) + event->len;
  892. res -= event_size;
  893. event_pos += event_size;
  894. }
  895. #endif
  896. return 0;
  897. }
  898.  
  899.  
  900. int EventHub::scan_dir(const char *dirname)
  901. {
  902. char devname[PATH_MAX];
  903. char *filename;
  904. DIR *dir;
  905. struct dirent *de;
  906. dir = opendir(dirname);
  907. if(dir == NULL)
  908. return -1;
  909. strcpy(devname, dirname);
  910. filename = devname + strlen(devname);
  911. *filename++ = '/';
  912. while((de = readdir(dir))) {
  913. if(de->d_name[0] == '.' &&
  914. (de->d_name[1] == '\0' ||
  915. (de->d_name[1] == '.' && de->d_name[2] == '\0')))
  916. continue;
  917. strcpy(filename, de->d_name);
  918. open_device(devname);
  919. }
  920. closedir(dir);
  921. return 0;
  922. }
  923.  
  924. }; // namespace android
Add Comment
Please, Sign In to add comment