Home | History | Annotate | Download | only in evdev
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "ui/base/ozone/evdev/key_event_converter_ozone.h"
      6 
      7 #include <linux/input.h>
      8 
      9 #include "ui/base/events/event.h"
     10 #include "ui/base/keycodes/keyboard_codes.h"
     11 
     12 namespace {
     13 
     14 ui::KeyboardCode KeyboardCodeFromButton(int code) {
     15   switch (code) {
     16     case KEY_VOLUMEDOWN:
     17       return ui::VKEY_VOLUME_DOWN;
     18 
     19     case KEY_VOLUMEUP:
     20       return ui::VKEY_VOLUME_UP;
     21 
     22     case KEY_POWER:
     23       return ui::VKEY_POWER;
     24   }
     25 
     26   LOG(ERROR) << "Unknown key code: " << code;
     27   return static_cast<ui::KeyboardCode>(0);
     28 }
     29 
     30 }  // namespace
     31 
     32 namespace ui {
     33 
     34 // TODO(rjkroege): Stop leaking file descriptor.
     35 KeyEventConverterOzone::KeyEventConverterOzone() {}
     36 KeyEventConverterOzone::~KeyEventConverterOzone() {}
     37 
     38 void KeyEventConverterOzone::OnFileCanReadWithoutBlocking(int fd) {
     39   input_event inputs[4];
     40   ssize_t read_size = read(fd, inputs, sizeof(inputs));
     41   if (read_size <= 0)
     42     return;
     43 
     44   CHECK_EQ(read_size % sizeof(*inputs), 0u);
     45   for (unsigned i = 0; i < read_size / sizeof(*inputs); ++i) {
     46     const input_event& input = inputs[i];
     47     if (input.type == EV_KEY) {
     48       scoped_ptr<KeyEvent> key(
     49           new KeyEvent(input.value == 1 ? ET_KEY_PRESSED : ET_KEY_RELEASED,
     50                        KeyboardCodeFromButton(input.code),
     51                        0,
     52                        true));
     53       DispatchEvent(key.PassAs<ui::Event>());
     54     } else if (input.type == EV_SYN) {
     55       // TODO(sadrul): Handle this case appropriately.
     56     }
     57   }
     58 }
     59 
     60 void KeyEventConverterOzone::OnFileCanWriteWithoutBlocking(int fd) {
     61   NOTREACHED();
     62 }
     63 
     64 }  // namespace ui
     65