This source file includes following definitions.
- InjectKeyEvent
- GenerateKeyupEvents
- CreateNormalizingInputFilter
#include "remoting/client/plugin/normalizing_input_filter.h"
#include <map>
#include <vector>
#include "base/logging.h"
#include "remoting/proto/event.pb.h"
namespace remoting {
namespace {
const unsigned int kUsbCapsLock = 0x070039;
const unsigned int kUsbLeftControl = 0x0700e0;
const unsigned int kUsbLeftShift = 0x0700e1;
const unsigned int kUsbLeftOption = 0x0700e2;
const unsigned int kUsbLeftCmd = 0x0700e3;
const unsigned int kUsbRightControl = 0x0700e4;
const unsigned int kUsbRightShift = 0x0700e5;
const unsigned int kUsbRightOption = 0x0700e6;
const unsigned int kUsbRightCmd = 0x0700e7;
const unsigned int kUsbTab = 0x07002b;
}
class NormalizingInputFilterMac : public protocol::InputFilter {
public:
explicit NormalizingInputFilterMac(protocol::InputStub* input_stub);
virtual ~NormalizingInputFilterMac() {}
virtual void InjectKeyEvent(const protocol::KeyEvent& event) OVERRIDE;
private:
void GenerateKeyupEvents();
typedef std::map<int, protocol::KeyEvent> KeyPressedMap;
KeyPressedMap key_pressed_map_;
DISALLOW_COPY_AND_ASSIGN(NormalizingInputFilterMac);
};
NormalizingInputFilterMac::NormalizingInputFilterMac(
protocol::InputStub* input_stub)
: protocol::InputFilter(input_stub) {
}
void NormalizingInputFilterMac::InjectKeyEvent(const protocol::KeyEvent& event)
{
DCHECK(event.has_usb_keycode());
bool is_special_key = event.usb_keycode() == kUsbLeftControl ||
event.usb_keycode() == kUsbLeftShift ||
event.usb_keycode() == kUsbLeftOption ||
event.usb_keycode() == kUsbRightControl ||
event.usb_keycode() == kUsbRightShift ||
event.usb_keycode() == kUsbRightOption ||
event.usb_keycode() == kUsbTab;
bool is_cmd_key = event.usb_keycode() == kUsbLeftCmd ||
event.usb_keycode() == kUsbRightCmd;
if (event.usb_keycode() == kUsbCapsLock) {
protocol::KeyEvent newEvent(event);
newEvent.set_pressed(true);
InputFilter::InjectKeyEvent(newEvent);
newEvent.set_pressed(false);
InputFilter::InjectKeyEvent(newEvent);
return;
} else if (!is_cmd_key && !is_special_key) {
if (event.pressed()) {
key_pressed_map_[event.usb_keycode()] = event;
} else {
key_pressed_map_.erase(event.usb_keycode());
}
}
if (is_cmd_key && !event.pressed()) {
GenerateKeyupEvents();
}
InputFilter::InjectKeyEvent(event);
}
void NormalizingInputFilterMac::GenerateKeyupEvents() {
for (KeyPressedMap::iterator i = key_pressed_map_.begin();
i != key_pressed_map_.end(); ++i) {
protocol::KeyEvent event = i->second;
event.set_pressed(false);
InputFilter::InjectKeyEvent(event);
}
key_pressed_map_.clear();
}
scoped_ptr<protocol::InputFilter> CreateNormalizingInputFilter(
protocol::InputStub* input_stub) {
return scoped_ptr<protocol::InputFilter>(
new NormalizingInputFilterMac(input_stub));
}
}