Home | History | Annotate | Download | only in ibus
      1 // Copyright (c) 2012 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 CHROMEOS_DBUS_IBUS_IBUS_ENGINE_SERVICE_H_
      6 #define CHROMEOS_DBUS_IBUS_IBUS_ENGINE_SERVICE_H_
      7 
      8 #include <string>
      9 #include "base/bind.h"
     10 #include "base/callback.h"
     11 #include "base/memory/scoped_vector.h"
     12 #include "chromeos/chromeos_export.h"
     13 #include "chromeos/dbus/dbus_client_implementation_type.h"
     14 #include "chromeos/dbus/ibus/ibus_constants.h"
     15 
     16 namespace dbus {
     17 class Bus;
     18 class ObjectPath;
     19 }  // namespace dbus
     20 
     21 namespace chromeos {
     22 
     23 class IBusLookupTable;
     24 class IBusProperty;
     25 class IBusText;
     26 typedef ScopedVector<IBusProperty> IBusPropertyList;
     27 
     28 // A interface to handle the engine handler method call.
     29 class CHROMEOS_EXPORT IBusEngineHandlerInterface {
     30  public:
     31   typedef base::Callback<void (bool consumed)> KeyEventDoneCallback;
     32 
     33   // Following capability mask is introduced from
     34   // http://ibus.googlecode.com/svn/docs/ibus-1.4/ibus-ibustypes.html#IBusCapabilite
     35   // TODO(nona): Move to ibus_contants and merge one in ui/base/ime/*
     36   enum IBusCapability {
     37     IBUS_CAPABILITY_PREEDIT_TEXT = 1U,
     38     IBUS_CAPABILITY_FOCUS = 8U,
     39   };
     40 
     41   virtual ~IBusEngineHandlerInterface() {}
     42 
     43   // Called when the Chrome input field get the focus.
     44   virtual void FocusIn() = 0;
     45 
     46   // Called when the Chrome input field lose the focus.
     47   virtual void FocusOut() = 0;
     48 
     49   // Called when the IME is enabled.
     50   virtual void Enable() = 0;
     51 
     52   // Called when the IME is disabled.
     53   virtual void Disable() = 0;
     54 
     55   // Called when a property is activated or changed.
     56   virtual void PropertyActivate(const std::string& property_name,
     57                                 ibus::IBusPropertyState property_state) = 0;
     58 
     59   // Called when a property is shown.
     60   virtual void PropertyShow(const std::string& property_name) = 0;
     61 
     62   // Called when a property is hidden.
     63   virtual void PropertyHide(const std::string& property_name) = 0;
     64 
     65   // Called when the Chrome input field set their capabilities.
     66   virtual void SetCapability(IBusCapability capability) = 0;
     67 
     68   // Called when the IME is reset.
     69   virtual void Reset() = 0;
     70 
     71   // Called when the key event is received. The |keycode| is raw layout
     72   // independent keycode. The |keysym| is result of XLookupString function
     73   // which translate |keycode| to keyboard layout dependent symbol value.
     74   // Actual implementation must call |callback| after key event handling.
     75   // For example: key press event for 'd' key on us layout and dvorak layout.
     76   //                  keyval keycode state
     77   //      us layout :  0x64   0x20    0x00
     78   //  dvorak layout :  0x65   0x20    0x00
     79   virtual void ProcessKeyEvent(uint32 keysym, uint32 keycode, uint32 state,
     80                                const KeyEventDoneCallback& callback) = 0;
     81 
     82   // Called when the candidate in lookup table is clicked. The |index| is 0
     83   // based candidate index in lookup table. The |state| is same value as
     84   // GdkModifierType in
     85   // http://developer.gnome.org/gdk/stable/gdk-Windows.html#GdkModifierType
     86   virtual void CandidateClicked(uint32 index, ibus::IBusMouseButton button,
     87                                 uint32 state) = 0;
     88 
     89   // Called when a new surrounding text is set. The |text| is surrounding text
     90   // and |cursor_pos| is 0 based index of cursor position in |text|. If there is
     91   // selection range, |anchor_pos| represents opposite index from |cursor_pos|.
     92   // Otherwise |anchor_pos| is equal to |cursor_pos|.
     93   virtual void SetSurroundingText(const std::string& text, uint32 cursor_pos,
     94                                   uint32 anchor_pos) = 0;
     95 
     96  protected:
     97   IBusEngineHandlerInterface() {}
     98 };
     99 
    100 // A class to make the actual DBus method call handling for IBusEngine service.
    101 // The exported method call is used by ibus-demon to process key event, because
    102 // Chrome works engine service if the extension IME is enabled. This class is
    103 // managed by DBusThreadManager.
    104 class CHROMEOS_EXPORT IBusEngineService {
    105  public:
    106   // Following value should be same in
    107   // http://ibus.googlecode.com/svn/docs/ibus-1.4/ibus-ibustypes.html#IBusPreeditFocusMode
    108   enum IBusEnginePreeditFocusOutMode {
    109     IBUS_ENGINE_PREEEDIT_FOCUS_OUT_MODE_CLEAR = 0,
    110     IBUS_ENGINE_PREEEDIT_FOCUS_OUT_MODE_COMMIT = 1,
    111   };
    112 
    113   virtual ~IBusEngineService();
    114 
    115   // Sets a new IBus engine handler and old handler will be overridden.
    116   // This class doesn't take the ownership of |handler|.
    117   virtual void SetEngine(IBusEngineHandlerInterface* handler) = 0;
    118 
    119   // Unsets the IBus engine handler if |handler| equals to current engine
    120   // handler.
    121   virtual void UnsetEngine(IBusEngineHandlerInterface* handler) = 0;
    122 
    123   // Emits RegisterProperties signal.
    124   virtual void RegisterProperties(
    125       const IBusPropertyList& property_list) = 0;
    126   // Emits UpdatePreedit signal.
    127   virtual void UpdatePreedit(const IBusText& ibus_text,
    128                              uint32 cursor_pos,
    129                              bool is_visible,
    130                              IBusEnginePreeditFocusOutMode mode) = 0;
    131   // Emits UpdateAuxiliaryText signal.
    132   virtual void UpdateAuxiliaryText(const IBusText& ibus_text,
    133                                    bool is_visible) = 0;
    134   // Emits UpdateLookupTable signal.
    135   virtual void UpdateLookupTable(const IBusLookupTable& lookup_table,
    136                                  bool is_visible) = 0;
    137   // Emits UpdateProperty signal.
    138   virtual void UpdateProperty(const IBusProperty& property) = 0;
    139   // Emits ForwardKeyEvent signal.
    140   virtual void ForwardKeyEvent(uint32 keyval, uint32 keycode, uint32 state) = 0;
    141   // Emits RequireSurroundingText signal.
    142   virtual void RequireSurroundingText() = 0;
    143   // Emits CommitText signal.
    144   virtual void CommitText(const std::string& text) = 0;
    145   // Emits DeleteSurroundingText signal.
    146   virtual void DeleteSurroundingText(int32 offset, uint32 length) = 0;
    147 
    148   // Factory function, creates a new instance and returns ownership.
    149   // For normal usage, access the singleton via DBusThreadManager::Get().
    150   static CHROMEOS_EXPORT IBusEngineService* Create(
    151       DBusClientImplementationType type,
    152       dbus::Bus* bus,
    153       const dbus::ObjectPath& object_path);
    154 
    155  protected:
    156   // Create() should be used instead.
    157   IBusEngineService();
    158 
    159  private:
    160   DISALLOW_COPY_AND_ASSIGN(IBusEngineService);
    161 };
    162 
    163 }  // namespace chromeos
    164 
    165 #endif  // CHROMEOS_DBUS_IBUS_IBUS_ENGINE_SERVICE_H_
    166