893ae9405fb1c00378ef707b8b2a888ed4f6831c
[platform/core/uifw/dali-adaptor.git] / adaptors / wayland / input / text / text-input-manager.h
1 #ifndef __DALI_WAYLAND_TEXT_INPUT_MANAGER_H__
2 #define __DALI_WAYLAND_TEXT_INPUT_MANAGER_H__
3
4 /*
5  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <vector>
23
24 // INTERNAL INCLUDES
25 #include <input/text/text-input-interface.h>
26 #include <input/seat.h>
27 #include <virtual-keyboard.h>
28 #include <wl-types.h>
29 #include <base/interfaces/window-event-interface.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace Adaptor
38 {
39
40
41 /**
42  * Handles wayland text input interface which deals with the input panel (virtual keyboard ).
43  *
44  */
45 class TextInputManager : private TextInputInterface
46 {
47
48 public:
49
50   /**
51    * @brief Constructor
52    */
53   TextInputManager();
54
55   /**
56    * @brief Destructor
57    */
58   ~TextInputManager();
59
60   /**
61    * @brief Assign the window event interface
62    * Used to send key events to DALi
63    * @param[in]  eventInterface event interface
64    */
65   void AssignWindowEventInterface( WindowEventInterface* eventInterface );
66
67   /**
68    * @brief Assign the Wayland connection
69    * @param[in] display Wayland display
70    */
71   void AssignDisplay( WlDisplay* display );
72
73   /**
74    * @brief Add a seat
75    * @param seat seat pointer
76    */
77   void AddSeat( Seat* seat );
78
79   /**
80    * Get the global TextInputManager
81    */
82   static TextInputManager& Get();
83
84 private:  // TextInputInterface
85
86   /**
87    * @copydoc TextInputInterface::Enter
88    */
89   virtual void Enter( Seat* seat, WlSurface* surface );
90
91   /**
92    * @copydoc TextInputInterface::Leave
93    */
94   virtual void Leave( Seat* seat );
95
96   /**
97    * @copydoc TextInputInterface::ModifiersMap
98    */
99   virtual void ModifiersMap( Seat* seat, WlArray *map );
100
101   /**
102    * @copydoc TextInputInterface::InputPanelState
103    */
104   virtual void InputPanelState( Seat* seat, uint32_t state );
105
106   /**
107    * @copydoc TextInputInterface::PreeditString
108    */
109   virtual void PreeditString( Seat* seat, uint32_t serial, const char *text, const char *commit );
110
111   /**
112    * @copydoc TextInputInterface::PreeditStyling
113    */
114   virtual void PreeditStyling( Seat* seat, uint32_t index, uint32_t length, uint32_t style );
115
116   /**
117    * @copydoc TextInputInterface::PreeditCursor
118    */
119   virtual void PreeditCursor( Seat* seat, int32_t index );
120
121   /**
122    * @copydoc TextInputInterface::CommitString
123    */
124   virtual void CommitString( Seat* seat, uint32_t serial, const char *text );
125
126   /**
127    * @copydoc TextInputInterface::CursorPosition
128    */
129   virtual void CursorPosition( Seat* seat, int32_t index, int32_t anchor );
130
131   /**
132    * @copydoc TextInputInterface::DeleteSurroundingText
133    */
134   virtual void DeleteSurroundingText( Seat* seat, int32_t index, uint32_t length );
135
136
137   /**
138    * @copydoc TextInputInterface::Keysym
139    */
140   virtual void Keysym( Seat* seat,
141                        uint32_t serial,
142                        uint32_t time,
143                        uint32_t sym,
144                        uint32_t state,
145                        uint32_t modifiers);
146
147   /**
148    * @copydoc TextInputInterface::Language
149    */
150   virtual void Language( Seat* seat, uint32_t serial, const char *language );
151
152   /**
153    * @copydoc TextInputInterface::TextDirection
154    */
155   virtual void TextDirection( Seat* seat, uint32_t serial, uint32_t direction );
156
157   /**
158    * @copydoc TextInputInterface::SelectionRegion
159    */
160   virtual void SelectionRegion( Seat* seat, uint32_t serial, int32_t start, int32_t end );
161
162   /**
163    * @copydoc TextInputInterface::PrivateCommand
164    */
165   virtual void PrivateCommand( Seat* seat, uint32_t serial, const char *command );
166
167   /**
168    * @copydoc TextInputInterface::InputPanelGeometry
169    */
170   virtual void InputPanelGeometry( Seat* seat, uint32_t x, uint32_t y, uint32_t width, uint32_t height );
171
172   /**
173    * @copydoc TextInputInterface::InputPanelData
174    */
175   virtual void InputPanelData( Seat* seat,
176                                 uint32_t serial,
177                                 const char* data,
178                                 uint32_t dataLength );
179
180   /**
181    * @copydoc TextInputInterface::GetSeat
182    */
183   virtual Seat* GetSeat( const WlTextInput* textInput );
184
185 public:
186
187   /**
188    * @brief show the input panel (virtual keyboard)
189    */
190   void ShowInputPanel();
191
192   /**
193    * @brief hide the input panel
194    */
195   void HideInputPanel();
196
197   /**
198    * @brief see if the input panel is visible
199    * @return true if panel is visible
200    */
201   bool IsInputPanelVisible();
202
203   /**
204    * @brief set the return key type
205    * @param[in] type return key type
206    */
207   void SetReturnKeyType( const InputMethod::ActionButton type );
208
209   /**
210    * @brief reset inputs state
211    * Should be called by an editor widget when the input state should be
212    * reset, for example after the text was changed outside of the normal
213    * input method flow.
214    */
215   void Reset();
216
217   /**
218    * @brief
219    * Set the plain surrounding text around the input position. Text is
220    * UTF-8 encoded. Cursor is the byte offset within the
221    * surrounding text. Anchor is the byte offset of the
222    * selection anchor within the surrounding text. If there is no selected
223    * text anchor is the same as cursor.
224    * @param[in] text the text
225    * @param[in] cursor cursor position
226    * @param[in] anchor anchor position
227    */
228   void SetSurroundingText( std::string text, unsigned int cursor, unsigned int anchor );
229
230
231
232 public: // virtual keyboard signals
233
234   /**
235    * @copydoc Dali::VirtualKeyboard::StatusChangedSignal
236    */
237   Dali::VirtualKeyboard::StatusSignalType& StatusChangedSignal();
238
239   /**
240    * @copydoc Dali::VirtualKeyboard::ResizedSignal
241    */
242   Dali::VirtualKeyboard::VoidSignalType& ResizedSignal();
243
244   /**
245    * @copydoc Dali::VirtualKeyboard::LanguageChangedSignal
246    */
247   Dali::VirtualKeyboard::VoidSignalType& LanguageChangedSignal();
248
249 public:   ///< Input Panel Signals ( DALi currently doesn't use these, it only uses signals from IMF interface).
250
251    typedef Signal< void ( unsigned int, const std::string, const std::string ) > PreEditStringSignalType;
252    typedef Signal< void ( unsigned int, unsigned int length, unsigned int ) > PreEditStylingSignalType;
253    typedef Signal< void ( int ) > PreEditCursorSignalType;
254    typedef Signal< void ( unsigned int, const std::string ) > CommitStringSignalType;
255    typedef Signal< void ( int , int ) > CursorPositionSignalType;
256    typedef Signal< void ( int , unsigned int ) > DeleteSurroundingTextSignalType;
257    typedef Signal< void ( unsigned int ,int, int ) > SelectionRegionSignalType;
258
259    /**
260     * @brief Notify when composing new text
261     * Description from wayland-extension/protocol/text.xml:
262     * Notify when a new composing text (pre-edit) should be set around the
263     * current cursor position. Any previously set composing text should
264     * be removed.
265     *
266     * The commit text can be used to replace the preedit text on reset (for example on unfocus).
267     * The text input should also handle all preedit_style and preedit_cursor
268     * events occurring directly before preedit_string.
269     *
270     * @code
271     *   void YourCallbackName( unsigned int serial, std::string text, std::string commit );
272     * @endcode
273     */
274    PreEditStringSignalType& PreEditStringSignal();
275
276    /**
277     * @brief Pre-edit styling
278     *
279     * Description from wayland-extension/protocol/text.xml:
280     * Set styling information on composing text. The style is applied for
281     * length bytes from index relative to the beginning of the composing
282     * text (as byte offset). Multiple styles can be applied to a composing
283     * text by sending multiple preedit_styling events.
284     *
285     *
286     * @code
287     *   void YourCallbackName( unsigned int index, unsigned int length, unsigned int style );
288     * @endcode
289     */
290    PreEditStylingSignalType& PreEditStylingSignal();
291
292    /**
293     * @brief Notify pre-edit cursor position
294     * Description from wayland-extension/protocol/text.xml:
295     * Set the cursor position inside the composing text (as byte
296     * offset) relative to the start of the composing text. When index is a
297     * negative number no cursor is shown.
298     *
299     * @code
300     *   void YourCallbackName( int index );
301     * @endcode
302     */
303    PreEditCursorSignalType& PreEditCursorSignal();
304
305    /**
306     * @brief Commit string text
307     * From wayland-extension/protocol/text.xml:
308     * Notify when text should be inserted into the editor widget. The text to
309     * commit could be either just a single character after a key press or the
310     * result of some composing (pre-edit). It could be also an empty text
311     * when some text should be removed (see delete_surrounding_text) or when
312     * the input cursor should be moved (see cursor_position).
313     *
314     * Any previously set composing text should be removed.
315     *
316     * @code
317     *   void YourCallbackName(  const std::string text );
318     * @endcode
319     */
320    CommitStringSignalType& CommitStringSignal();
321
322    /**
323     * @brief Cursor position signal
324     * From wayland-extension/protocol/text.xml:
325     * Notify when the cursor or anchor position should be modified.
326     * This event should be handled as part of a following commit_string  event.
327     *  @code
328     *   void YourCallbackName( int index, int anchor );
329     * @endcode
330     */
331    CursorPositionSignalType& CursorPositionSignal();
332
333    /**
334     * @brief delete surrounding text
335     * From wayland-extension/protocol/text.xml:
336     * Notify when the text around the current cursor position should be deleted.
337     * @code
338     * Index is relative to the current cursor (in bytes).
339     * Length is the length of deleted text (in bytes).
340     *
341     *  void YourCallbackName( int index, unsigned int length );
342     * @endcode
343     * This event should be handled as part of a following commit_string event.
344     */
345    DeleteSurroundingTextSignalType& DeleteSurroundingTextSignal();
346
347    /**
348     * @brief text selection region
349     * Notify when the input panels ask to select the characters
350     * from the start cursor position to the end cursor position.
351     * * @code
352     * Index is relative to the current cursor (in bytes).
353     * Length is the length of deleted text (in bytes).
354     *
355     * void YourCallbackName( unsigned int serial, int start, int end );
356     * @endcode
357     */
358    SelectionRegionSignalType& SelectionRegionSignal();
359
360 public:
361
362   /**
363    * POD to store text input data for each seat.
364    */
365   struct SeatInfo
366   {
367     SeatInfo()
368     : mTextDirection( Dali::VirtualKeyboard::LeftToRight ),
369       mSeat( NULL ),
370       mInputPanelDimensions( 0,0,0,0 ),
371       mReturnKeyType( InputMethod::ACTION_UNSPECIFIED ),
372       mInputPanelVisible( false ),
373       mFocused( false )
374     {
375
376     }
377     Dali::VirtualKeyboard::TextDirection mTextDirection;
378     Seat* mSeat;
379     Dali::Rect<int> mInputPanelDimensions;    ///< size of the input panel
380     std::string mLanguage;                    ///< input panel language
381     InputMethod::ActionButton mReturnKeyType; ///< return key
382     bool mInputPanelVisible:1;                ///< panel status
383     bool mFocused:1;
384   };
385
386   /**
387    * @brief get the last active seat
388    * @return the last active seat
389    */
390   SeatInfo& GetLastActiveSeat();
391
392 private:
393
394   WlDisplay* mDisplay;        ///< Wayland display, handles all the data sent from and to the compositor
395   Seat* mLastActiveSeat;      ///< Seat that was last used
396   WindowEventInterface* mWindowEventInterface;
397
398   std::vector< SeatInfo > mSeats;         ///< keeps track of text input information for each seat
399   Dali::VirtualKeyboard::StatusSignalType mKeyboardStatusSignal;
400   Dali::VirtualKeyboard::VoidSignalType   mKeyboardResizeSignal;
401   Dali::VirtualKeyboard::VoidSignalType   mKeyboardLanguageChangedSignal;
402
403   // Input Panel (Virtual Keyboard) signals
404   PreEditStringSignalType                 mPreEditStringSignal;
405   PreEditStylingSignalType                mPreEditStylingSignal;
406   PreEditCursorSignalType                 mPreEditCursorSignal;
407   CommitStringSignalType                  mCommitStringSignal;
408   CursorPositionSignalType                mCursorPositionSignal;
409   DeleteSurroundingTextSignalType         mDeleteSurroundingTextSignal;
410   SelectionRegionSignalType               mSelectionRegionSignal;
411
412 };
413
414
415 } // Internal
416 } // Adaptor
417 } // Dali
418
419 #endif