Home | History | Annotate | Download | only in dom4
      1 // Copyright 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 #ifndef UI_EVENTS_KEYCODES_DOM4_KEYCODE_CONVERTER_H_
      6 #define UI_EVENTS_KEYCODES_DOM4_KEYCODE_CONVERTER_H_
      7 
      8 #include <stdint.h>
      9 #include "base/basictypes.h"
     10 #include "base/memory/singleton.h"
     11 
     12 // For reference, the W3C UI Event spec is located at:
     13 // http://www.w3.org/TR/uievents/
     14 
     15 namespace ui {
     16 
     17 // This structure is used to define the keycode mapping table.
     18 // It is defined here because the unittests need access to it.
     19 typedef struct {
     20   // USB keycode:
     21   //  Upper 16-bits: USB Usage Page.
     22   //  Lower 16-bits: USB Usage Id: Assigned ID within this usage page.
     23   uint32_t usb_keycode;
     24 
     25   // Contains one of the following:
     26   //  On Linux: XKB scancode
     27   //  On Windows: Windows OEM scancode
     28   //  On Mac: Mac keycode
     29   uint16_t native_keycode;
     30 
     31   // The UIEvents (aka: DOM4Events) |code| value as defined in:
     32   // https://dvcs.w3.org/hg/d4e/raw-file/tip/source_respec.htm
     33   const char* code;
     34 } KeycodeMapEntry;
     35 
     36 // A class to convert between the current platform's native keycode (scancode)
     37 // and platform-neutral |code| values (as defined in the W3C UI Events
     38 // spec (http://www.w3.org/TR/uievents/).
     39 class KeycodeConverter {
     40  public:
     41   static KeycodeConverter* GetInstance();
     42 
     43   // Return the value that identifies an invalid native keycode.
     44   uint16_t InvalidNativeKeycode();
     45 
     46   // Return the string that indentifies an invalid UI Event |code|.
     47   // The returned pointer references a static global string.
     48   const char* InvalidKeyboardEventCode();
     49 
     50   // Convert a native (Mac/Win/Linux) keycode into the |code| string.
     51   // The returned pointer references a static global string.
     52   const char* NativeKeycodeToCode(uint16_t native_keycode);
     53 
     54   // Convert a UI Events |code| string value into a native keycode.
     55   uint16_t CodeToNativeKeycode(const char* code);
     56 
     57   // The following methods relate to USB keycodes.
     58   // Note that USB keycodes are not part of any web standard.
     59   // Please don't use USB keycodes in new code.
     60 
     61   // Return the value that identifies an invalid USB keycode.
     62   uint16_t InvalidUsbKeycode();
     63 
     64   // Convert a USB keycode into an equivalent platform native keycode.
     65   uint16_t UsbKeycodeToNativeKeycode(uint32_t usb_keycode);
     66 
     67   // Convert a platform native keycode into an equivalent USB keycode.
     68   uint32_t NativeKeycodeToUsbKeycode(uint16_t native_keycode);
     69 
     70   // Convert a USB keycode into the string with the DOM3 |code| value.
     71   // The returned pointer references a static global string.
     72   const char* UsbKeycodeToCode(uint32_t usb_keycode);
     73 
     74   // Convert a DOM3 Event |code| string into a USB keycode value.
     75   uint32_t CodeToUsbKeycode(const char* code);
     76 
     77   // Static methods to support testing.
     78   size_t NumKeycodeMapEntriesForTest();
     79   const KeycodeMapEntry* GetKeycodeMapForTest();
     80 
     81  private:
     82   KeycodeConverter();
     83   friend struct DefaultSingletonTraits<KeycodeConverter>;
     84 
     85   DISALLOW_COPY_AND_ASSIGN(KeycodeConverter);
     86 };
     87 
     88 }  // namespace ui
     89 
     90 #endif  // UI_EVENTS_KEYCODES_DOM4_KEYCODE_CONVERTER_H_
     91