5 #define Uses_SCIM_DEBUG
6 #define Uses_SCIM_BACKEND
7 #define Uses_SCIM_IMENGINE_MODULE
8 #define Uses_SCIM_HOTKEY
9 #define Uses_SCIM_PANEL_CLIENT
11 #include <sys/types.h>
14 #include <sys/times.h>
17 #include <Ecore_Evas.h>
23 #include <X11/keysym.h>
24 #include <X11/Xutil.h>
27 #include "scim_imcontext.h"
31 struct _EcoreIMFContextISFImpl
33 EcoreIMFContextISF *parent;
34 IMEngineInstancePointer si;
35 Ecore_X_Window client_window;
37 Ecore_IMF_Input_Mode input_mode;
38 WideString preedit_string;
39 AttributeList preedit_attrlist;
40 Ecore_IMF_Autocapital_Type autocapital_type;
49 bool preedit_updating;
50 bool need_commit_preedit;
52 bool prediction_allow;
54 EcoreIMFContextISFImpl *next;
57 /* Input Context handling functions. */
58 static EcoreIMFContextISFImpl *new_ic_impl (EcoreIMFContextISF *parent);
59 static void delete_ic_impl (EcoreIMFContextISFImpl *impl);
60 static void delete_all_ic_impl (void);
62 static EcoreIMFContextISF *find_ic (int id);
65 /* private functions */
66 static void panel_slot_reload_config (int context);
67 static void panel_slot_exit (int context);
68 static void panel_slot_update_lookup_table_page_size(int context,
70 static void panel_slot_lookup_table_page_up (int context);
71 static void panel_slot_lookup_table_page_down (int context);
72 static void panel_slot_trigger_property (int context,
73 const String &property);
74 static void panel_slot_process_helper_event (int context,
75 const String &target_uuid,
76 const String &helper_uuid,
77 const Transaction &trans);
78 static void panel_slot_move_preedit_caret (int context,
80 static void panel_slot_select_candidate (int context,
82 static void panel_slot_process_key_event (int context,
84 static void panel_slot_commit_string (int context,
85 const WideString &wstr);
86 static void panel_slot_forward_key_event (int context,
88 static void panel_slot_request_help (int context);
89 static void panel_slot_request_factory_menu (int context);
90 static void panel_slot_change_factory (int context,
93 static void panel_req_focus_in (EcoreIMFContextISF *ic);
94 static void panel_req_update_factory_info (EcoreIMFContextISF *ic);
95 static void panel_req_update_spot_location (EcoreIMFContextISF *ic);
96 static void panel_req_show_help (EcoreIMFContextISF *ic);
97 static void panel_req_show_factory_menu (EcoreIMFContextISF *ic);
99 /* Panel iochannel handler*/
100 static bool panel_initialize (void);
101 static void panel_finalize (void);
102 static Eina_Bool panel_iochannel_handler (void *data,
103 Ecore_Fd_Handler *fd_handler);
105 /* utility functions */
106 static bool filter_hotkeys (EcoreIMFContextISF *ic,
107 const KeyEvent &key);
108 static void turn_on_ic (EcoreIMFContextISF *ic);
109 static void turn_off_ic (EcoreIMFContextISF *ic);
110 static void set_ic_capabilities (EcoreIMFContextISF *ic);
112 static void initialize (void);
113 static void finalize (void);
115 static void open_next_factory (EcoreIMFContextISF *ic);
116 static void open_previous_factory (EcoreIMFContextISF *ic);
117 static void open_specific_factory (EcoreIMFContextISF *ic,
119 static void initialize_modifier_bits (Display *display);
120 static unsigned int scim_x11_keymask_scim_to_x11 (Display *display, uint16 scimkeymask);
121 static XKeyEvent createKeyEvent (Display *display, Window &win,
122 Window &winRoot, bool press,
123 int keycode, int modifiers);
124 static void _x_send_key_event (const KeyEvent &key);
126 static void attach_instance (const IMEngineInstancePointer &si);
129 static void slot_show_preedit_string (IMEngineInstanceBase *si);
130 static void slot_show_aux_string (IMEngineInstanceBase *si);
131 static void slot_show_lookup_table (IMEngineInstanceBase *si);
133 static void slot_hide_preedit_string (IMEngineInstanceBase *si);
134 static void slot_hide_aux_string (IMEngineInstanceBase *si);
135 static void slot_hide_lookup_table (IMEngineInstanceBase *si);
137 static void slot_update_preedit_caret (IMEngineInstanceBase *si,
139 static void slot_update_preedit_string (IMEngineInstanceBase *si,
140 const WideString &str,
141 const AttributeList &attrs);
142 static void slot_update_aux_string (IMEngineInstanceBase *si,
143 const WideString &str,
144 const AttributeList &attrs);
145 static void slot_commit_string (IMEngineInstanceBase *si,
146 const WideString &str);
147 static void slot_forward_key_event (IMEngineInstanceBase *si,
148 const KeyEvent &key);
149 static void slot_update_lookup_table (IMEngineInstanceBase *si,
150 const LookupTable &table);
152 static void slot_register_properties (IMEngineInstanceBase *si,
153 const PropertyList &properties);
154 static void slot_update_property (IMEngineInstanceBase *si,
155 const Property &property);
156 static void slot_beep (IMEngineInstanceBase *si);
157 static void slot_start_helper (IMEngineInstanceBase *si,
158 const String &helper_uuid);
159 static void slot_stop_helper (IMEngineInstanceBase *si,
160 const String &helper_uuid);
161 static void slot_send_helper_event (IMEngineInstanceBase *si,
162 const String &helper_uuid,
163 const Transaction &trans);
164 static bool slot_get_surrounding_text (IMEngineInstanceBase *si,
169 static bool slot_delete_surrounding_text (IMEngineInstanceBase *si,
173 static void reload_config_callback (const ConfigPointer &config);
175 static void fallback_commit_string_cb (IMEngineInstanceBase *si,
176 const WideString &str);
178 static void caps_mode_check (Ecore_IMF_Context *ctx, Eina_Bool force);
180 /* Local variables declaration */
181 static String _language;
182 static EcoreIMFContextISFImpl *_used_ic_impl_list = 0;
183 static EcoreIMFContextISFImpl *_free_ic_impl_list = 0;
184 static EcoreIMFContextISF *_ic_list = 0;
186 static KeyboardLayout _keyboard_layout = SCIM_KEYBOARD_Default;
187 static int _valid_key_mask = SCIM_KEY_AllMasks;
189 static FrontEndHotkeyMatcher _frontend_hotkey_matcher;
190 static IMEngineHotkeyMatcher _imengine_hotkey_matcher;
192 static IMEngineInstancePointer _default_instance;
194 static ConfigModule *_config_module = 0;
195 static ConfigPointer _config;
196 static BackEndPointer _backend;
198 static EcoreIMFContextISF *_focused_ic = 0;
200 static bool _scim_initialized = false;
202 static int _instance_count = 0;
203 static int _context_count = 0;
205 static IMEngineFactoryPointer _fallback_factory;
206 static IMEngineInstancePointer _fallback_instance;
207 static PanelClient _panel_client;
209 static Ecore_Fd_Handler *_panel_iochannel_read_handler = 0;
210 static Ecore_Fd_Handler *_panel_iochannel_err_handler = 0;
212 static Ecore_X_Window _client_window = 0;
214 static bool _on_the_spot = true;
215 static bool _shared_input_method = false;
217 static Eina_Bool autocap_allow = EINA_FALSE;
219 static Display *__current_display = 0;
220 static int __current_alt_mask = Mod1Mask;
221 static int __current_meta_mask = 0;
222 static int __current_super_mask = 0;
223 static int __current_hyper_mask = 0;
224 static int __current_numlock_mask = Mod2Mask;
226 // A hack to shutdown the immodule cleanly even if im_module_exit() is not called when exiting.
227 class FinalizeHandler
232 SCIM_DEBUG_FRONTEND(1) << "FinalizeHandler::FinalizeHandler()\n";
236 SCIM_DEBUG_FRONTEND(1) << "FinalizeHandler::~FinalizeHandler()\n";
237 isf_imf_context_shutdown();
241 static FinalizeHandler _finalize_handler;
244 utf8_offset_to_index(const char *str, int offset)
248 for (i = 0; i < offset; i++)
250 eina_unicode_utf8_get_next(str, &index);
261 struct timezone tz; /* is not used since ages */
262 gettimeofday(&tv, &tz);
263 tint = tv.tv_sec * 1000;
264 tint = tint / 1000 * 1000;
265 tint = tint + tv.tv_usec / 1000;
269 /* Function Implementations */
270 static EcoreIMFContextISFImpl *
271 new_ic_impl(EcoreIMFContextISF *parent)
273 EcoreIMFContextISFImpl *impl = NULL;
275 if (_free_ic_impl_list != NULL)
277 impl = _free_ic_impl_list;
278 _free_ic_impl_list = _free_ic_impl_list->next;
282 impl = new EcoreIMFContextISFImpl;
287 impl->uppercase = false;
288 impl->autocapital_type = ECORE_IMF_AUTOCAPITAL_TYPE_NONE;
289 impl->next = _used_ic_impl_list;
290 _used_ic_impl_list = impl;
292 impl->parent = parent;
298 delete_ic_impl(EcoreIMFContextISFImpl *impl)
300 EcoreIMFContextISFImpl *rec = _used_ic_impl_list, *last = 0;
302 for (; rec != 0; last = rec, rec = rec->next)
307 last->next = rec->next;
309 _used_ic_impl_list = rec->next;
311 rec->next = _free_ic_impl_list;
312 _free_ic_impl_list = rec;
316 rec->client_window = 0;
317 rec->preedit_string = WideString();
318 rec->preedit_attrlist.clear();
326 delete_all_ic_impl(void)
328 EcoreIMFContextISFImpl *it = _used_ic_impl_list;
332 _used_ic_impl_list = it->next;
334 it = _used_ic_impl_list;
337 it = _free_ic_impl_list;
340 _free_ic_impl_list = it->next;
342 it = _free_ic_impl_list;
346 static EcoreIMFContextISF *
349 EcoreIMFContextISFImpl *rec = _used_ic_impl_list;
353 if (rec->parent && rec->parent->id == id)
362 analyze_surrounding_text(Ecore_IMF_Context *ctx)
364 char *plain_str = NULL;
365 char *markup_str = NULL;
366 const char *puncs[] = {". ", "! ", "? "};
367 Eina_Bool ret = EINA_FALSE;
370 Eina_Unicode *tail = NULL;
371 Eina_Unicode *ustr = NULL;
372 const int punc_num = sizeof(puncs) / sizeof(puncs[0]);
373 Eina_Unicode *uni_puncs[punc_num];
374 EcoreIMFContextISF *context_scim;
376 if (!ctx) return EINA_FALSE;
377 context_scim = (EcoreIMFContextISF *)ecore_imf_context_data_get(ctx);
378 if (!context_scim || !context_scim->impl) return EINA_FALSE;
380 switch (context_scim->impl->autocapital_type)
382 case ECORE_IMF_AUTOCAPITAL_TYPE_NONE:
384 case ECORE_IMF_AUTOCAPITAL_TYPE_ALLCHARACTER:
390 for (i = 0; i < punc_num; i++)
391 uni_puncs[i] = eina_unicode_utf8_to_unicode(puncs[i], NULL);
393 ecore_imf_context_surrounding_get(ctx, &markup_str, &cursor_pos);
394 if (!markup_str) goto done;
402 // Convert into plain string
403 plain_str = evas_textblock_text_markup_to_utf8(NULL, markup_str);
404 if (!plain_str) goto done;
406 // Convert string from UTF-8 to unicode
407 ustr = eina_unicode_utf8_to_unicode(plain_str, NULL);
408 if (!ustr) goto done;
412 if (context_scim->impl->autocapital_type == ECORE_IMF_AUTOCAPITAL_TYPE_WORD)
414 if (ustr[cursor_pos-1] == ' ')
421 // Check paragraph separator <PS> and carriage return <br>
422 if ((ustr[cursor_pos-1] == 0x2029) || (ustr[cursor_pos-1] == '\n'))
432 tail = eina_unicode_strndup(ustr+cursor_pos-2, 2);
436 for (i = 0; i < punc_num; i++)
438 if (!eina_unicode_strcmp(tail, uni_puncs[i]))
450 if (ustr) free(ustr);
451 if (markup_str) free(markup_str);
452 if (plain_str) free(plain_str);
454 for (i = 0; i < punc_num; i++)
455 if (uni_puncs[i]) free(uni_puncs[i]);
461 caps_mode_check(Ecore_IMF_Context *ctx, Eina_Bool force)
464 EcoreIMFContextISF *context_scim;
467 context_scim = (EcoreIMFContextISF *)ecore_imf_context_data_get(ctx);
469 if (autocap_allow == EINA_FALSE)
472 // Check autocapital type
473 if (!context_scim || !context_scim->impl)
476 if (analyze_surrounding_text(ctx))
477 uppercase = EINA_TRUE;
479 uppercase = EINA_FALSE;
482 context_scim->impl->uppercase = uppercase;
484 if (context_scim->impl->uppercase != uppercase)
485 context_scim->impl->uppercase = uppercase;
489 feed_key_event(Evas *evas, const char *str, Eina_Bool fake)
491 char key_string[128] = {0};
492 unsigned int timestamp = 0;
495 timestamp = get_time();
497 if (strncmp(str, "KeyRelease+", 11) == 0)
499 strncpy(key_string, str + 11, strlen(str)-11);
500 evas_event_feed_key_up(evas, key_string, key_string, NULL, NULL, timestamp, NULL);
501 SCIM_DEBUG_FRONTEND(1) << " evas_event_feed_key_up()...\n";
505 strncpy(key_string, str, strlen(str));
506 evas_event_feed_key_down(evas, key_string, key_string, NULL, NULL, timestamp, NULL);
507 SCIM_DEBUG_FRONTEND(1) << " evas_event_feed_key_down()...\n";
512 window_to_screen_geometry_get(Ecore_X_Window client_win, int *x, int *y)
514 Ecore_X_Window root_window, win;
516 int sum_x = 0, sum_y = 0;
518 root_window = ecore_x_window_root_get(client_win);
521 while (root_window != win)
523 ecore_x_window_geometry_get(win, &win_x, &win_y, NULL, NULL);
526 win = ecore_x_window_parent_get(win);
535 /* Public functions */
537 * isf_imf_context_new
539 * This function will be called by Ecore IMF.
540 * Create a instance of type EcoreIMFContextISF.
542 * Return value: A pointer to the newly created EcoreIMFContextISF instance
544 EAPI EcoreIMFContextISF *
545 isf_imf_context_new(void)
547 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
550 EcoreIMFContextISF *context_scim = new EcoreIMFContextISF;
551 if (context_scim == NULL)
553 std::cerr << "memory allocation failed in " << __FUNCTION__ << "\n";
557 context_scim->id = _context_count++;
559 if (!_scim_initialized)
562 _scim_initialized = true;
565 env = getenv("ECORE_IMF_AUTOCAPITAL_ALLOW");
567 autocap_allow = !!atoi(env);
573 * isf_imf_context_shutdown
575 * It will be called when the scim im module is unloaded by ecore. It will do some
579 isf_imf_context_shutdown(void)
581 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
583 if (_scim_initialized)
585 _scim_initialized = false;
591 isf_imf_context_add(Ecore_IMF_Context *ctx)
593 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
595 EcoreIMFContextISF *context_scim = (EcoreIMFContextISF*)ecore_imf_context_data_get(ctx);
597 if (!context_scim) return;
599 context_scim->impl = NULL;
604 IMEngineInstancePointer si;
606 // Use the default instance if "shared input method" mode is enabled.
607 if (_shared_input_method && !_default_instance.null())
609 si = _default_instance;
610 SCIM_DEBUG_FRONTEND(2) << "use default instance: " << si->get_id() << " " << si->get_factory_uuid() << "\n";
613 // Not in "shared input method" mode, or no default instance, create an instance.
616 IMEngineFactoryPointer factory = _backend->get_default_factory(_language, "UTF-8");
617 if (factory.null()) return;
618 si = factory->create_instance("UTF-8", _instance_count++);
619 if (si.null()) return;
621 SCIM_DEBUG_FRONTEND(2) << "create new instance: " << si->get_id() << " " << si->get_factory_uuid() << "\n";
624 // If "shared input method" mode is enabled, and there is no default instance,
625 // then store this instance as default one.
626 if (_shared_input_method && _default_instance.null())
628 SCIM_DEBUG_FRONTEND(2) << "update default instance.\n";
629 _default_instance = si;
632 context_scim->ctx = ctx;
633 context_scim->impl = new_ic_impl(context_scim);
634 if (context_scim->impl == NULL)
636 std::cerr << "memory allocation failed in " << __FUNCTION__ << "\n";
640 context_scim->impl->si = si;
641 context_scim->impl->client_window = 0;
642 context_scim->impl->client_canvas = NULL;
643 context_scim->impl->preedit_caret = 0;
644 context_scim->impl->cursor_x = 0;
645 context_scim->impl->cursor_y = 0;
646 context_scim->impl->cursor_pos = -1;
647 context_scim->impl->is_on = false;
648 context_scim->impl->shared_si = _shared_input_method;
649 context_scim->impl->use_preedit = _on_the_spot;
650 context_scim->impl->preedit_started = false;
651 context_scim->impl->preedit_updating = false;
652 context_scim->impl->need_commit_preedit = false;
655 context_scim->next = NULL;
657 context_scim->next = _ic_list;
658 _ic_list = context_scim;
660 if (_shared_input_method)
661 context_scim->impl->is_on = _config->read(String(SCIM_CONFIG_FRONTEND_IM_OPENED_BY_DEFAULT), context_scim->impl->is_on);
663 _panel_client.prepare(context_scim->id);
664 _panel_client.register_input_context(context_scim->id, si->get_factory_uuid());
665 set_ic_capabilities(context_scim);
666 _panel_client.send();
668 SCIM_DEBUG_FRONTEND(2) << "input context created: id = " << context_scim->id << "\n";
672 isf_imf_context_del(Ecore_IMF_Context *ctx)
674 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
676 if (!_ic_list) return;
678 EcoreIMFContextISF *context_scim = (EcoreIMFContextISF*)ecore_imf_context_data_get(ctx);
682 if (context_scim->id != _ic_list->id)
684 EcoreIMFContextISF * pre = _ic_list;
685 EcoreIMFContextISF * cur = _ic_list->next;
688 if (cur->id == context_scim->id)
690 pre->next = cur->next;
698 _ic_list = _ic_list->next;
701 if (context_scim && context_scim->impl)
703 _panel_client.prepare(context_scim->id);
705 if (context_scim == _focused_ic)
706 context_scim->impl->si->focus_out();
708 // Delete the instance.
709 EcoreIMFContextISF *old_focused = _focused_ic;
710 _focused_ic = context_scim;
711 context_scim->impl->si.reset();
712 _focused_ic = old_focused;
714 if (context_scim == _focused_ic)
716 _panel_client.turn_off(context_scim->id);
717 _panel_client.focus_out(context_scim->id);
720 _panel_client.remove_input_context(context_scim->id);
721 _panel_client.send();
723 if (context_scim->impl->client_window)
724 isf_imf_context_client_window_set(ctx, NULL);
726 if (context_scim->impl)
728 delete_ic_impl(context_scim->impl);
729 context_scim->impl = 0;
733 if (context_scim == _focused_ic)
744 * isf_imf_context_client_canvas_set
745 * @ctx: a #Ecore_IMF_Context
746 * @canvas: the client canvas
748 * This function will be called by Ecore IMF.
750 * Set the client canvas for the Input Method Context; this is the canvas
751 * in which the input appears.
753 * The canvas type can be determined by using the context canvas type.
754 * Actually only canvas with type "evas" (Evas *) is supported. This canvas
755 * may be used in order to correctly position status windows, and may also
756 * be used for purposes internal to the Input Method Context.
759 isf_imf_context_client_canvas_set(Ecore_IMF_Context *ctx, void *canvas)
761 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
763 EcoreIMFContextISF *context_scim = (EcoreIMFContextISF *)ecore_imf_context_data_get(ctx);
765 if (context_scim && context_scim->impl && context_scim->impl->client_canvas != (Evas*) canvas)
766 context_scim->impl->client_canvas = (Evas*)canvas;
770 * isf_imf_context_client_window_set
771 * @ctx: a #Ecore_IMF_Context
772 * @window: the client window
774 * This function will be called by Ecore IMF.
776 * Set the client window for the Input Method Context; this is the Ecore_X_Window
777 * when using X11, Ecore_Win32_Window when using Win32, etc.
779 * This window is used in order to correctly position status windows,
780 * and may also be used for purposes internal to the Input Method Context.
783 isf_imf_context_client_window_set(Ecore_IMF_Context *ctx, void *window)
785 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
787 EcoreIMFContextISF *context_scim = (EcoreIMFContextISF *)ecore_imf_context_data_get(ctx);
789 if (context_scim && context_scim->impl && context_scim->impl->client_window != (Ecore_X_Window)((Ecore_Window)window))
791 context_scim->impl->client_window = (Ecore_X_Window)((Ecore_Window)window);
793 if ((context_scim->impl->client_window != 0) &&
794 (context_scim->impl->client_window != _client_window))
795 _client_window = context_scim->impl->client_window;
800 * isf_imf_context_reset
801 * @ctx: a #Ecore_IMF_Context
803 * This function will be called by Ecore IMF.
805 * Notify the Input Method Context that a change such as a change in cursor
806 * position has been made. This will typically cause the Input Method Context
807 * to clear the preedit state.
810 isf_imf_context_reset(Ecore_IMF_Context *ctx)
812 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
814 EcoreIMFContextISF *context_scim = (EcoreIMFContextISF *)ecore_imf_context_data_get(ctx);
816 if (context_scim && context_scim->impl && context_scim == _focused_ic)
818 WideString wstr = context_scim->impl->preedit_string;
820 _panel_client.prepare(context_scim->id);
821 context_scim->impl->si->reset();
822 _panel_client.send();
824 if (context_scim->impl->need_commit_preedit)
828 ecore_imf_context_commit_event_add(context_scim->ctx, utf8_wcstombs(wstr).c_str());
829 ecore_imf_context_event_callback_call(context_scim->ctx, ECORE_IMF_CALLBACK_COMMIT, (void *)utf8_wcstombs(wstr).c_str());
831 _panel_client.prepare(context_scim->id);
832 _panel_client.send();
838 * isf_imf_context_focus_in
839 * @ctx: a #Ecore_IMF_Context
841 * This function will be called by Ecore IMF.
843 * Notify the Input Method Context that the widget to which its correspond has gained focus.
846 isf_imf_context_focus_in(Ecore_IMF_Context *ctx)
848 EcoreIMFContextISF *context_scim = (EcoreIMFContextISF *)ecore_imf_context_data_get(ctx);
853 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__<< "(" << context_scim->id << ")...\n";
857 if (_focused_ic == context_scim)
859 SCIM_DEBUG_FRONTEND(1) << "It's already focused.\n";
862 SCIM_DEBUG_FRONTEND(1) << "Focus out previous IC first: " << _focused_ic->id << "\n";
863 if (_focused_ic->ctx)
864 isf_imf_context_focus_out(_focused_ic->ctx);
867 bool need_cap = false;
868 bool need_reset = false;
869 bool need_reg = false;
871 if (context_scim && context_scim->impl)
873 _focused_ic = context_scim;
874 _panel_client.prepare(context_scim->id);
876 // Handle the "Shared Input Method" mode.
877 if (_shared_input_method)
879 SCIM_DEBUG_FRONTEND(2) << "shared input method.\n";
880 IMEngineFactoryPointer factory = _backend->get_default_factory(_language, "UTF-8");
883 if (_default_instance.null() || _default_instance->get_factory_uuid() != factory->get_uuid())
885 _default_instance = factory->create_instance("UTF-8", _default_instance.null() ? _instance_count++ : _default_instance->get_id());
886 attach_instance(_default_instance);
887 SCIM_DEBUG_FRONTEND(2) << "create new default instance: " << _default_instance->get_id() << " " << _default_instance->get_factory_uuid() << "\n";
890 context_scim->impl->shared_si = true;
891 context_scim->impl->si = _default_instance;
893 context_scim->impl->is_on = _config->read(String(SCIM_CONFIG_FRONTEND_IM_OPENED_BY_DEFAULT), context_scim->impl->is_on);
894 context_scim->impl->preedit_string.clear();
895 context_scim->impl->preedit_attrlist.clear();
896 context_scim->impl->preedit_caret = 0;
897 context_scim->impl->preedit_started = false;
903 else if (context_scim->impl->shared_si)
905 SCIM_DEBUG_FRONTEND(2) << "exit shared input method.\n";
906 IMEngineFactoryPointer factory = _backend->get_default_factory(_language, "UTF-8");
909 context_scim->impl->si = factory->create_instance("UTF-8", _instance_count++);
910 context_scim->impl->preedit_string.clear();
911 context_scim->impl->preedit_attrlist.clear();
912 context_scim->impl->preedit_caret = 0;
913 context_scim->impl->preedit_started = false;
914 attach_instance(context_scim->impl->si);
917 context_scim->impl->shared_si = false;
918 SCIM_DEBUG_FRONTEND(2) << "create new instance: " << context_scim->impl->si->get_id() << " " << context_scim->impl->si->get_factory_uuid() << "\n";
922 context_scim->impl->si->set_frontend_data(static_cast <void*>(context_scim));
924 if (need_reg) _panel_client.register_input_context(context_scim->id, context_scim->impl->si->get_factory_uuid());
925 if (need_cap) set_ic_capabilities(context_scim);
926 if (need_reset) context_scim->impl->si->reset();
928 panel_req_focus_in(context_scim);
929 panel_req_update_spot_location(context_scim);
930 panel_req_update_factory_info(context_scim);
932 if (context_scim->impl->is_on)
934 _panel_client.turn_on(context_scim->id);
935 _panel_client.hide_preedit_string(context_scim->id);
936 _panel_client.hide_aux_string(context_scim->id);
937 _panel_client.hide_lookup_table(context_scim->id);
938 context_scim->impl->si->focus_in();
942 _panel_client.turn_off(context_scim->id);
945 _panel_client.send();
948 if (ecore_imf_context_input_panel_enabled_get(ctx))
949 ecore_imf_context_input_panel_show(ctx);
953 * isf_imf_context_focus_out
954 * @ctx: a #Ecore_IMF_Context
956 * This function will be called by Ecore IMF.
958 * Notify the Input Method Context that the widget to which its correspond has lost focus.
961 isf_imf_context_focus_out(Ecore_IMF_Context *ctx)
963 EcoreIMFContextISF *context_scim = (EcoreIMFContextISF *)ecore_imf_context_data_get(ctx);
965 if (!context_scim) return;
967 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "(" << context_scim->id << ")...\n";
969 if (context_scim && context_scim->impl && context_scim == _focused_ic)
971 WideString wstr = context_scim->impl->preedit_string;
973 if (context_scim->impl->need_commit_preedit)
977 ecore_imf_context_commit_event_add(context_scim->ctx, utf8_wcstombs(wstr).c_str());
978 ecore_imf_context_event_callback_call(context_scim->ctx, ECORE_IMF_CALLBACK_COMMIT, (void *)utf8_wcstombs(wstr).c_str());
980 _panel_client.prepare(context_scim->id);
981 _panel_client.send();
984 _panel_client.prepare(context_scim->id);
985 context_scim->impl->si->focus_out();
986 context_scim->impl->si->reset();
987 _panel_client.turn_off(context_scim->id);
988 _panel_client.focus_out(context_scim->id);
989 _panel_client.send();
993 if (ecore_imf_context_input_panel_enabled_get(ctx))
994 ecore_imf_context_input_panel_hide(ctx);
998 * isf_imf_context_cursor_location_set
999 * @ctx: a #Ecore_IMF_Context
1000 * @x: x position of New cursor.
1001 * @y: y position of New cursor.
1002 * @w: the width of New cursor.
1003 * @h: the height of New cursor.
1005 * This function will be called by Ecore IMF.
1007 * Notify the Input Method Context that a change in the cursor location has been made.
1010 isf_imf_context_cursor_location_set(Ecore_IMF_Context *ctx, int cx, int cy, int cw, int ch)
1012 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1014 EcoreIMFContextISF *context_scim = (EcoreIMFContextISF *)ecore_imf_context_data_get(ctx);
1016 int canvas_x, canvas_y;
1017 int new_cursor_x, new_cursor_y;
1019 if (cw == 0 && ch == 0)
1022 if (context_scim && context_scim->impl && context_scim == _focused_ic)
1024 if (context_scim->impl->client_canvas)
1026 ee = ecore_evas_ecore_evas_get(context_scim->impl->client_canvas);
1029 ecore_evas_geometry_get(ee, &canvas_x, &canvas_y, NULL, NULL);
1033 if (context_scim->impl->client_window)
1034 window_to_screen_geometry_get(context_scim->impl->client_window, &canvas_x, &canvas_y);
1039 new_cursor_x = canvas_x + cx;
1040 new_cursor_y = canvas_y + cy + ch;
1042 // Don't update spot location while updating preedit string.
1043 if (context_scim->impl->preedit_updating && (context_scim->impl->cursor_y == new_cursor_y))
1046 if (context_scim->impl->cursor_x != new_cursor_x || context_scim->impl->cursor_y != new_cursor_y)
1048 context_scim->impl->cursor_x = new_cursor_x;
1049 context_scim->impl->cursor_y = new_cursor_y;
1050 _panel_client.prepare(context_scim->id);
1051 panel_req_update_spot_location(context_scim);
1052 _panel_client.send();
1053 SCIM_DEBUG_FRONTEND(2) << "new cursor location = " << context_scim->impl->cursor_x << "," << context_scim->impl->cursor_y << "\n";
1059 * isf_imf_context_use_preedit_set
1060 * @ctx: a #Ecore_IMF_Context
1061 * @use_preedit: Whether the IM context should use the preedit string.
1063 * This function will be called by Ecore IMF.
1065 * Set whether the IM context should use the preedit string to display feedback.
1066 * If is 0 (default is 1), then the IM context may use some other method to
1067 * display feedback, such as displaying it in a child of the root window.
1070 isf_imf_context_use_preedit_set(Ecore_IMF_Context* ctx, Eina_Bool use_preedit)
1072 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " = " << (use_preedit ? "true" : "false") << "...\n";
1074 EcoreIMFContextISF *context_scim = (EcoreIMFContextISF*)ecore_imf_context_data_get(ctx);
1076 if (!_on_the_spot) return;
1078 if (context_scim && context_scim->impl)
1080 bool old = context_scim->impl->use_preedit;
1081 context_scim->impl->use_preedit = use_preedit;
1082 if (context_scim == _focused_ic)
1084 _panel_client.prepare(context_scim->id);
1086 if (old != use_preedit)
1087 set_ic_capabilities(context_scim);
1089 if (context_scim->impl->preedit_string.length())
1090 slot_show_preedit_string(context_scim->impl->si);
1092 _panel_client.send();
1098 isf_imf_context_preedit_string_with_attributes_get(Ecore_IMF_Context *ctx, char** str, Eina_List **attrs, int *cursor_pos)
1100 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1102 EcoreIMFContextISF *context_scim = (EcoreIMFContextISF*)ecore_imf_context_data_get(ctx);
1104 if (context_scim && context_scim->impl && context_scim->impl->is_on)
1106 String mbs = utf8_wcstombs(context_scim->impl->preedit_string);
1111 *str = strdup(mbs.c_str());
1118 *cursor_pos = context_scim->impl->preedit_caret;
1125 int start_index, end_index;
1126 int wlen = context_scim->impl->preedit_string.length();
1128 Ecore_IMF_Preedit_Attr *attr = NULL;
1129 AttributeList::const_iterator i;
1130 bool *attrs_flag = new bool [mbs.length()];
1131 memset(attrs_flag, 0, mbs.length() *sizeof(bool));
1133 for (i = context_scim->impl->preedit_attrlist.begin();
1134 i != context_scim->impl->preedit_attrlist.end(); ++i)
1136 start_index = i->get_start();
1137 end_index = i->get_end();
1139 if (end_index <= wlen && start_index < end_index && i->get_type() != SCIM_ATTR_DECORATE_NONE)
1141 start_index = utf8_offset_to_index(mbs.c_str(), i->get_start());
1142 end_index = utf8_offset_to_index(mbs.c_str(), i->get_end());
1144 if (i->get_type() == SCIM_ATTR_DECORATE)
1146 attr = (Ecore_IMF_Preedit_Attr *)calloc(1, sizeof(Ecore_IMF_Preedit_Attr));
1149 attr->start_index = start_index;
1150 attr->end_index = end_index;
1152 if (i->get_value() == SCIM_ATTR_DECORATE_UNDERLINE)
1154 attr->preedit_type = ECORE_IMF_PREEDIT_TYPE_SUB1;
1155 *attrs = eina_list_append(*attrs, (void *)attr);
1157 else if (i->get_value() == SCIM_ATTR_DECORATE_REVERSE)
1159 attr->preedit_type = ECORE_IMF_PREEDIT_TYPE_SUB2;
1160 *attrs = eina_list_append(*attrs, (void *)attr);
1162 else if (i->get_value() == SCIM_ATTR_DECORATE_HIGHLIGHT)
1164 attr->preedit_type = ECORE_IMF_PREEDIT_TYPE_SUB3;
1165 *attrs = eina_list_append(*attrs, (void *)attr);
1172 switch(i->get_value())
1174 case SCIM_ATTR_DECORATE_UNDERLINE:
1175 case SCIM_ATTR_DECORATE_REVERSE:
1176 case SCIM_ATTR_DECORATE_HIGHLIGHT:
1177 // Record which character has attribute.
1178 for (int pos = start_index; pos < end_index; ++pos)
1179 attrs_flag [pos] = 1;
1185 else if (i->get_type() == SCIM_ATTR_FOREGROUND)
1187 SCIM_DEBUG_FRONTEND(4) << "SCIM_ATTR_FOREGROUND\n";
1189 else if (i->get_type() == SCIM_ATTR_BACKGROUND)
1191 SCIM_DEBUG_FRONTEND(4) << "SCIM_ATTR_BACKGROUND\n";
1196 // Add underline for all characters which don't have attribute.
1197 for (unsigned int pos = 0; pos < mbs.length(); ++pos)
1199 if (!attrs_flag [pos])
1201 int begin_pos = pos;
1203 while (pos < mbs.length() && !attrs_flag[pos])
1206 // use REVERSE style as default
1207 attr = (Ecore_IMF_Preedit_Attr *)calloc(1, sizeof(Ecore_IMF_Preedit_Attr));
1210 attr->preedit_type = ECORE_IMF_PREEDIT_TYPE_SUB2;
1211 attr->start_index = begin_pos;
1212 attr->end_index = pos;
1213 *attrs = eina_list_append(*attrs, (void *)attr);
1217 delete [] attrs_flag;
1235 * isf_imf_context_preedit_string_get
1236 * @ctx: a #Ecore_IMF_Context
1237 * @str: the preedit string
1238 * @cursor_pos: the cursor position
1240 * This function will be called by Ecore IMF.
1242 * To get the preedit string of the input method.
1245 isf_imf_context_preedit_string_get(Ecore_IMF_Context *ctx, char** str, int *cursor_pos)
1247 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1249 EcoreIMFContextISF *context_scim = (EcoreIMFContextISF*)ecore_imf_context_data_get(ctx);
1251 if (context_scim && context_scim->impl && context_scim->impl->is_on)
1253 String mbs = utf8_wcstombs(context_scim->impl->preedit_string);
1258 *str = strdup(mbs.c_str());
1264 *cursor_pos = context_scim->impl->preedit_caret;
1277 * isf_imf_context_cursor_position_set
1278 * @ctx: a #Ecore_IMF_Context
1279 * @cursor_pos: New cursor position in characters.
1281 * This function will be called by Ecore IMF.
1283 * Notify the Input Method Context that a change in the cursor position has been made.
1286 isf_imf_context_cursor_position_set(Ecore_IMF_Context *ctx, int cursor_pos)
1288 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1290 EcoreIMFContextISF *context_scim = (EcoreIMFContextISF *)ecore_imf_context_data_get(ctx);
1292 if (context_scim && context_scim->impl && context_scim == _focused_ic)
1294 // Don't update spot location while updating preedit string.
1295 if (context_scim->impl->preedit_updating)
1298 if (context_scim->impl->cursor_pos != cursor_pos)
1300 context_scim->impl->cursor_pos = cursor_pos;
1301 caps_mode_check(ctx, EINA_FALSE);
1307 * isf_imf_context_input_mode_set
1308 * @ctx: a #Ecore_IMF_Context
1309 * @input_mode: the input mode
1311 * This function will be called by Ecore IMF.
1313 * To set the input mode of input method. The definition of Ecore_IMF_Input_Mode
1314 * is in Ecore_IMF.h.
1317 isf_imf_context_input_mode_set(Ecore_IMF_Context *ctx, Ecore_IMF_Input_Mode input_mode)
1319 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1321 EcoreIMFContextISF *context_scim = (EcoreIMFContextISF*)ecore_imf_context_data_get(ctx);
1322 if (context_scim && context_scim->impl)
1323 context_scim->impl->input_mode = input_mode;
1327 * isf_imf_context_prediction_allow_set
1328 * @ctx: a #Ecore_IMF_Context
1329 * @use_prediction: Whether the IM context should use the prediction.
1331 * This function will be called by Ecore IMF.
1333 * Set whether the IM context should use the prediction.
1336 isf_imf_context_prediction_allow_set(Ecore_IMF_Context* ctx, Eina_Bool prediction)
1338 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " = " << (prediction ? "true" : "false") << "...\n";
1340 EcoreIMFContextISF *context_scim = (EcoreIMFContextISF *)ecore_imf_context_data_get(ctx);
1342 if (context_scim && context_scim->impl && context_scim->impl->prediction_allow != prediction)
1343 context_scim->impl->prediction_allow = prediction;
1347 isf_imf_context_autocapital_type_set(Ecore_IMF_Context* ctx, Ecore_IMF_Autocapital_Type autocapital_type)
1349 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " = " << autocapital_type << "...\n";
1351 EcoreIMFContextISF *context_scim = (EcoreIMFContextISF *)ecore_imf_context_data_get(ctx);
1353 if (context_scim && context_scim->impl && context_scim->impl->autocapital_type != autocapital_type)
1354 context_scim->impl->autocapital_type = autocapital_type;
1358 * isf_imf_context_filter_event
1359 * @ctx: a #Ecore_IMF_Context
1360 * @type: The type of event defined by Ecore_IMF_Event_Type.
1361 * @event: The event itself.
1362 * Return value: %TRUE if the input method handled the key event.
1364 * This function will be called by Ecore IMF.
1366 * Allow an Ecore Input Context to internally handle an event. If this function
1367 * returns 1, then no further processing should be done for this event. Input
1368 * methods must be able to accept all types of events (simply returning 0 if
1369 * the event was not handled), but there is no obligation of any events to be
1370 * submitted to this function.
1373 isf_imf_context_filter_event(Ecore_IMF_Context *ctx, Ecore_IMF_Event_Type type, Ecore_IMF_Event *event)
1375 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1377 EcoreIMFContextISF *ic = (EcoreIMFContextISF*)ecore_imf_context_data_get(ctx);
1378 Eina_Bool ret = EINA_FALSE;
1380 if (ic == NULL || ic->impl == NULL)
1385 if (type == ECORE_IMF_EVENT_KEY_DOWN)
1387 Ecore_IMF_Event_Key_Down *ev = (Ecore_IMF_Event_Key_Down *)event;
1388 scim_string_to_key(key, ev->key);
1389 if (ev->modifiers & ECORE_IMF_KEYBOARD_MODIFIER_SHIFT) key.mask |=SCIM_KEY_ShiftMask;
1390 if (ev->modifiers & ECORE_IMF_KEYBOARD_MODIFIER_CTRL) key.mask |=SCIM_KEY_ControlMask;
1391 if (ev->modifiers & ECORE_IMF_KEYBOARD_MODIFIER_ALT) key.mask |=SCIM_KEY_AltMask;
1392 if (ev->locks & ECORE_IMF_KEYBOARD_LOCK_CAPS) key.mask |=SCIM_KEY_CapsLockMask;
1393 if (ev->locks & ECORE_IMF_KEYBOARD_LOCK_NUM) key.mask |=SCIM_KEY_NumLockMask;
1395 else if (type == ECORE_IMF_EVENT_KEY_UP)
1397 Ecore_IMF_Event_Key_Up *ev = (Ecore_IMF_Event_Key_Up *)event;
1398 scim_string_to_key(key, ev->key);
1399 key.mask = SCIM_KEY_ReleaseMask;
1400 if (ev->modifiers & ECORE_IMF_KEYBOARD_MODIFIER_SHIFT) key.mask |=SCIM_KEY_ShiftMask;
1401 if (ev->modifiers & ECORE_IMF_KEYBOARD_MODIFIER_CTRL) key.mask |=SCIM_KEY_ControlMask;
1402 if (ev->modifiers & ECORE_IMF_KEYBOARD_MODIFIER_ALT) key.mask |=SCIM_KEY_AltMask;
1403 if (ev->locks & ECORE_IMF_KEYBOARD_LOCK_CAPS) key.mask |=SCIM_KEY_CapsLockMask;
1404 if (ev->locks & ECORE_IMF_KEYBOARD_LOCK_NUM) key.mask |=SCIM_KEY_NumLockMask;
1411 key.mask &= _valid_key_mask;
1413 _panel_client.prepare(ic->id);
1416 if (!filter_hotkeys(ic, key))
1418 if (!_focused_ic || !_focused_ic->impl->is_on ||
1419 !_focused_ic->impl->si->process_key_event(key))
1423 _panel_client.send();
1429 isf_imf_context_input_panel_show(Ecore_IMF_Context *ctx)
1431 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1433 EcoreIMFContextISF *ic = (EcoreIMFContextISF*)ecore_imf_context_data_get(ctx);
1434 if (ic == NULL || ic->impl == NULL)
1437 ecore_x_e_virtual_keyboard_state_set
1438 (ic->impl->client_window, ECORE_X_VIRTUAL_KEYBOARD_STATE_ON);
1442 isf_imf_context_input_panel_hide(Ecore_IMF_Context *ctx)
1444 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1446 EcoreIMFContextISF *ic = (EcoreIMFContextISF*)ecore_imf_context_data_get(ctx);
1447 if (ic == NULL || ic->impl == NULL)
1450 ecore_x_e_virtual_keyboard_state_set
1451 (ic->impl->client_window, ECORE_X_VIRTUAL_KEYBOARD_STATE_OFF);
1454 /* Panel Slot functions */
1456 panel_slot_reload_config(int context __UNUSED__)
1458 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1463 panel_slot_exit(int /* context */)
1465 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1471 panel_slot_update_lookup_table_page_size(int context, int page_size)
1473 EcoreIMFContextISF *ic = find_ic(context);
1474 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " context=" << context << " page_size=" << page_size << " ic=" << ic << "\n";
1477 _panel_client.prepare(ic->id);
1478 ic->impl->si->update_lookup_table_page_size(page_size);
1479 _panel_client.send();
1484 panel_slot_lookup_table_page_up(int context)
1486 EcoreIMFContextISF *ic = find_ic(context);
1487 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " context=" << context << " ic=" << ic << "\n";
1490 _panel_client.prepare(ic->id);
1491 ic->impl->si->lookup_table_page_up();
1492 _panel_client.send();
1497 panel_slot_lookup_table_page_down(int context)
1499 EcoreIMFContextISF *ic = find_ic(context);
1500 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " context=" << context << " ic=" << ic << "\n";
1503 _panel_client.prepare(ic->id);
1504 ic->impl->si->lookup_table_page_down();
1505 _panel_client.send();
1510 panel_slot_trigger_property(int context, const String &property)
1512 EcoreIMFContextISF *ic = find_ic(context);
1513 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " context=" << context << " property=" << property << " ic=" << ic << "\n";
1516 _panel_client.prepare(ic->id);
1517 ic->impl->si->trigger_property(property);
1518 _panel_client.send();
1523 panel_slot_process_helper_event(int context, const String &target_uuid, const String &helper_uuid, const Transaction &trans)
1525 EcoreIMFContextISF *ic = find_ic(context);
1526 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " context=" << context << " target=" << target_uuid
1527 << " helper=" << helper_uuid << " ic=" << ic << " ic->impl=" << (ic ? ic->impl : 0) << " ic-uuid="
1528 << ((ic && ic->impl) ? ic->impl->si->get_factory_uuid() : "" ) << "\n";
1529 if (ic && ic->impl && ic->impl->si->get_factory_uuid() == target_uuid)
1531 _panel_client.prepare(ic->id);
1532 SCIM_DEBUG_FRONTEND(2) << "call process_helper_event\n";
1533 ic->impl->si->process_helper_event(helper_uuid, trans);
1534 _panel_client.send();
1539 panel_slot_move_preedit_caret(int context, int caret_pos)
1541 EcoreIMFContextISF *ic = find_ic(context);
1542 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " context=" << context << " caret=" << caret_pos << " ic=" << ic << "\n";
1545 _panel_client.prepare(ic->id);
1546 ic->impl->si->move_preedit_caret(caret_pos);
1547 _panel_client.send();
1552 panel_slot_select_candidate(int context, int cand_index)
1554 EcoreIMFContextISF *ic = find_ic(context);
1555 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " context=" << context << " candidate=" << cand_index << " ic=" << ic << "\n";
1558 _panel_client.prepare(ic->id);
1559 ic->impl->si->select_candidate(cand_index);
1560 _panel_client.send();
1565 panel_slot_process_key_event(int context, const KeyEvent &key)
1567 EcoreIMFContextISF *ic = find_ic(context);
1568 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " context=" << context << " key=" << key.get_key_string() << " ic=" << ic << "\n";
1570 if (key.is_key_press())
1571 ecore_x_test_fake_key_press(key.get_key_string().c_str());
1575 panel_slot_commit_string(int context, const WideString &wstr)
1577 EcoreIMFContextISF *ic = find_ic(context);
1578 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " context=" << context << " str=" << utf8_wcstombs(wstr) << " ic=" << ic << "\n";
1582 if (_focused_ic != ic)
1585 ecore_imf_context_commit_event_add(ic->ctx, utf8_wcstombs(wstr).c_str());
1586 ecore_imf_context_event_callback_call(ic->ctx, ECORE_IMF_CALLBACK_COMMIT, (void *)utf8_wcstombs(wstr).c_str());
1591 panel_slot_forward_key_event(int context, const KeyEvent &key)
1593 EcoreIMFContextISF *ic = find_ic(context);
1594 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " context=" << context << " key=" << key.get_key_string() << " ic=" << ic << "\n";
1596 if (ic && ic->impl && ic->impl->client_canvas)
1597 feed_key_event(ic->impl->client_canvas, key.get_key_string().c_str(), EINA_TRUE);
1601 panel_slot_request_help(int context)
1603 EcoreIMFContextISF *ic = find_ic(context);
1604 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " context=" << context << " ic=" << ic << "\n";
1608 _panel_client.prepare(ic->id);
1609 panel_req_show_help(ic);
1610 _panel_client.send();
1615 panel_slot_request_factory_menu(int context)
1617 EcoreIMFContextISF *ic = find_ic(context);
1618 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " context=" << context << " ic=" << ic << "\n";
1622 _panel_client.prepare(ic->id);
1623 panel_req_show_factory_menu(ic);
1624 _panel_client.send();
1629 panel_slot_change_factory(int context, const String &uuid)
1631 EcoreIMFContextISF *ic = find_ic(context);
1632 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " context=" << context << " factory=" << uuid << " ic=" << ic << "\n";
1636 ic->impl->si->reset();
1637 _panel_client.prepare(ic->id);
1638 open_specific_factory(ic, uuid);
1639 _panel_client.send();
1643 /* Panel Requestion functions. */
1645 panel_req_show_help(EcoreIMFContextISF *ic)
1647 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1651 help = String("Smart Common Input Method platform ") +
1652 //String(SCIM_VERSION) +
1653 String("\n(C) 2002-2005 James Su <suzhe@tsinghua.org.cn>\n\n");
1657 IMEngineFactoryPointer sf = _backend->get_factory(ic->impl->si->get_factory_uuid());
1660 help += utf8_wcstombs(sf->get_name());
1661 help += String(":\n\n");
1663 help += utf8_wcstombs(sf->get_help());
1664 help += String("\n\n");
1666 help += utf8_wcstombs(sf->get_credits());
1668 _panel_client.show_help(ic->id, help);
1673 panel_req_show_factory_menu(EcoreIMFContextISF *ic)
1675 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1677 std::vector<IMEngineFactoryPointer> factories;
1678 std::vector <PanelFactoryInfo> menu;
1680 _backend->get_factories_for_encoding(factories, "UTF-8");
1682 for (size_t i = 0; i < factories.size(); ++ i)
1684 menu.push_back(PanelFactoryInfo(
1685 factories [i]->get_uuid(),
1686 utf8_wcstombs(factories [i]->get_name()),
1687 factories [i]->get_language(),
1688 factories [i]->get_icon_file()));
1692 _panel_client.show_factory_menu(ic->id, menu);
1696 panel_req_update_factory_info(EcoreIMFContextISF *ic)
1698 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1700 if (ic && ic->impl && ic == _focused_ic)
1702 PanelFactoryInfo info;
1703 if (ic->impl->is_on)
1705 IMEngineFactoryPointer sf = _backend->get_factory(ic->impl->si->get_factory_uuid());
1707 info = PanelFactoryInfo(sf->get_uuid(), utf8_wcstombs(sf->get_name()), sf->get_language(), sf->get_icon_file());
1711 info = PanelFactoryInfo(String(""), String("English/Keyboard"), String("C"), "");
1713 _panel_client.update_factory_info(ic->id, info);
1718 panel_req_focus_in(EcoreIMFContextISF *ic)
1720 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1722 _panel_client.focus_in(ic->id, ic->impl->si->get_factory_uuid());
1726 panel_req_update_spot_location(EcoreIMFContextISF *ic)
1728 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1730 _panel_client.update_spot_location(ic->id, ic->impl->cursor_x, ic->impl->cursor_y);
1734 filter_hotkeys(EcoreIMFContextISF *ic, const KeyEvent &key)
1736 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1740 _frontend_hotkey_matcher.push_key_event(key);
1741 _imengine_hotkey_matcher.push_key_event(key);
1743 FrontEndHotkeyAction hotkey_action = _frontend_hotkey_matcher.get_match_result();
1745 if (hotkey_action == SCIM_FRONTEND_HOTKEY_TRIGGER)
1747 if (!ic->impl->is_on)
1753 else if (hotkey_action == SCIM_FRONTEND_HOTKEY_ON)
1755 if (!ic->impl->is_on)
1759 else if (hotkey_action == SCIM_FRONTEND_HOTKEY_OFF)
1761 if (ic->impl->is_on)
1765 else if (hotkey_action == SCIM_FRONTEND_HOTKEY_NEXT_FACTORY)
1767 open_next_factory(ic);
1770 else if (hotkey_action == SCIM_FRONTEND_HOTKEY_PREVIOUS_FACTORY)
1772 open_previous_factory(ic);
1775 else if (hotkey_action == SCIM_FRONTEND_HOTKEY_SHOW_FACTORY_MENU)
1777 panel_req_show_factory_menu(ic);
1780 else if (_imengine_hotkey_matcher.is_matched())
1782 String sfid = _imengine_hotkey_matcher.get_match_result();
1783 open_specific_factory(ic, sfid);
1790 panel_initialize(void)
1792 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1794 String display_name;
1796 const char *p = getenv("DISPLAY");
1797 if (p) display_name = String(p);
1800 if (_panel_client.open_connection(_config->get_name(), display_name) >= 0)
1802 int fd = _panel_client.get_connection_number();
1804 _panel_iochannel_read_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, panel_iochannel_handler, NULL, NULL, NULL);
1806 SCIM_DEBUG_FRONTEND(2) << " Panel FD= " << fd << "\n";
1810 std::cerr << "panel_initialize() failed!!!\n";
1815 panel_finalize(void)
1817 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1819 _panel_client.close_connection();
1821 if (_panel_iochannel_read_handler)
1823 ecore_main_fd_handler_del(_panel_iochannel_read_handler);
1824 _panel_iochannel_read_handler = 0;
1827 if (_panel_iochannel_err_handler)
1829 ecore_main_fd_handler_del(_panel_iochannel_err_handler);
1830 _panel_iochannel_err_handler = 0;
1835 panel_iochannel_handler(void *data __UNUSED__, Ecore_Fd_Handler *fd_handler)
1837 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1839 if (fd_handler == _panel_iochannel_read_handler)
1841 if (!_panel_client.filter_event())
1845 return ECORE_CALLBACK_CANCEL;
1848 else if (fd_handler == _panel_iochannel_err_handler)
1852 return ECORE_CALLBACK_CANCEL;
1854 return ECORE_CALLBACK_RENEW;
1858 turn_on_ic(EcoreIMFContextISF *ic)
1860 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1862 if (ic && ic->impl && !ic->impl->is_on)
1864 ic->impl->is_on = true;
1866 if (ic == _focused_ic)
1868 panel_req_focus_in(ic);
1869 panel_req_update_spot_location(ic);
1870 panel_req_update_factory_info(ic);
1871 _panel_client.turn_on(ic->id);
1872 _panel_client.hide_preedit_string(ic->id);
1873 _panel_client.hide_aux_string(ic->id);
1874 _panel_client.hide_lookup_table(ic->id);
1875 ic->impl->si->focus_in();
1878 //Record the IC on/off status
1879 if (_shared_input_method)
1880 _config->write(String(SCIM_CONFIG_FRONTEND_IM_OPENED_BY_DEFAULT), true);
1882 if (ic->impl->use_preedit && ic->impl->preedit_string.length())
1884 ecore_imf_context_preedit_start_event_add(ic->ctx);
1885 ecore_imf_context_event_callback_call(ic->ctx, ECORE_IMF_CALLBACK_PREEDIT_START, NULL);
1886 ecore_imf_context_preedit_changed_event_add(ic->ctx);
1887 ecore_imf_context_event_callback_call(ic->ctx, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, NULL);
1888 ic->impl->preedit_started = true;
1894 turn_off_ic(EcoreIMFContextISF *ic)
1896 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1898 if (ic && ic->impl && ic->impl->is_on)
1900 ic->impl->is_on = false;
1902 if (ic == _focused_ic)
1904 ic->impl->si->focus_out();
1906 panel_req_update_factory_info(ic);
1907 _panel_client.turn_off(ic->id);
1910 //Record the IC on/off status
1911 if (_shared_input_method)
1912 _config->write(String(SCIM_CONFIG_FRONTEND_IM_OPENED_BY_DEFAULT), false);
1914 if (ic->impl->use_preedit && ic->impl->preedit_string.length())
1916 ecore_imf_context_preedit_changed_event_add(ic->ctx);
1917 ecore_imf_context_event_callback_call(ic->ctx, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, NULL);
1918 ecore_imf_context_preedit_end_event_add(ic->ctx);
1919 ecore_imf_context_event_callback_call(ic->ctx, ECORE_IMF_CALLBACK_PREEDIT_END, NULL);
1920 ic->impl->preedit_started = false;
1926 set_ic_capabilities(EcoreIMFContextISF *ic)
1928 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1932 unsigned int cap = SCIM_CLIENT_CAP_ALL_CAPABILITIES;
1934 if (!_on_the_spot || !ic->impl->use_preedit)
1935 cap -= SCIM_CLIENT_CAP_ONTHESPOT_PREEDIT;
1937 ic->impl->si->update_client_capabilities(cap);
1942 check_socket_frontend(void)
1944 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
1946 SocketAddress address;
1947 SocketClient client;
1951 address.set_address(scim_get_default_socket_frontend_address());
1953 if (!client.connect(address))
1956 if (!scim_socket_open_connection(magic,
1957 String("ConnectionTester"),
1958 String("SocketFrontEnd"),
1969 std::vector<String> config_list;
1970 std::vector<String> engine_list;
1971 std::vector<String> load_engine_list;
1973 std::vector<String>::iterator it;
1975 bool manual = false;
1979 String config_module_name = "simple";
1981 printf("Initializing Ecore SCIM IMModule...\n");
1983 SCIM_DEBUG_FRONTEND(1) << "Initializing Ecore SCIM IMModule...\n";
1985 // Get system language.
1986 _language = scim_get_locale_language(scim_get_current_locale());
1990 // If no Socket FrontEnd is running, then launch one.
1991 // And set manual to false.
1992 bool check_result = check_socket_frontend();
1995 std::cerr << "Launching a SCIM daemon with Socket FrontEnd...\n";
1997 scim_get_imengine_module_list(engine_list);
1999 for (it = engine_list.begin(); it != engine_list.end(); it++)
2001 if (*it != "socket")
2002 load_engine_list.push_back(*it);
2005 const char *new_argv [] = { "--no-stay", 0 };
2008 (load_engine_list.size() ? scim_combine_string_list(load_engine_list, ',') : "none"),
2014 // If there is one Socket FrontEnd running and it's not manual mode,
2015 // then just use this Socket Frontend.
2018 for (int i = 0; i < 200; ++i)
2022 config_module_name = "socket";
2023 load_engine_list.clear();
2024 load_engine_list.push_back("socket");
2028 check_result = check_socket_frontend();
2033 if (config_module_name != "dummy")
2035 //load config module
2036 SCIM_DEBUG_FRONTEND(1) << "Loading Config module: " << config_module_name << "...\n";
2037 _config_module = new ConfigModule(config_module_name);
2039 //create config instance
2040 if (_config_module != NULL && _config_module->valid())
2041 _config = _config_module->create_config();
2046 SCIM_DEBUG_FRONTEND(1) << "Config module cannot be loaded, using dummy Config.\n";
2048 if (_config_module) delete _config_module;
2049 _config_module = NULL;
2051 _config = new DummyConfig();
2052 config_module_name = "dummy";
2055 reload_config_callback(_config);
2056 _config->signal_connect_reload(slot(reload_config_callback));
2059 _backend = new CommonBackEnd(_config, load_engine_list.size() ? load_engine_list : engine_list);
2061 if (_backend.null())
2062 std::cerr << "Cannot create BackEnd Object!\n";
2064 _fallback_factory = _backend->get_factory(SCIM_COMPOSE_KEY_FACTORY_UUID);
2066 if (_fallback_factory.null())
2067 _fallback_factory = new DummyIMEngineFactory();
2069 _fallback_instance = _fallback_factory->create_instance(String("UTF-8"), 0);
2070 _fallback_instance->signal_connect_commit_string(slot(fallback_commit_string_cb));
2072 // Attach Panel Client signal.
2073 _panel_client.signal_connect_reload_config (slot(panel_slot_reload_config));
2074 _panel_client.signal_connect_exit (slot(panel_slot_exit));
2075 _panel_client.signal_connect_update_lookup_table_page_size(slot(panel_slot_update_lookup_table_page_size));
2076 _panel_client.signal_connect_lookup_table_page_up (slot(panel_slot_lookup_table_page_up));
2077 _panel_client.signal_connect_lookup_table_page_down (slot(panel_slot_lookup_table_page_down));
2078 _panel_client.signal_connect_trigger_property (slot(panel_slot_trigger_property));
2079 _panel_client.signal_connect_process_helper_event (slot(panel_slot_process_helper_event));
2080 _panel_client.signal_connect_move_preedit_caret (slot(panel_slot_move_preedit_caret));
2081 _panel_client.signal_connect_select_candidate (slot(panel_slot_select_candidate));
2082 _panel_client.signal_connect_process_key_event (slot(panel_slot_process_key_event));
2083 _panel_client.signal_connect_commit_string (slot(panel_slot_commit_string));
2084 _panel_client.signal_connect_forward_key_event (slot(panel_slot_forward_key_event));
2085 _panel_client.signal_connect_request_help (slot(panel_slot_request_help));
2086 _panel_client.signal_connect_request_factory_menu (slot(panel_slot_request_factory_menu));
2087 _panel_client.signal_connect_change_factory (slot(panel_slot_change_factory));
2089 if (!panel_initialize())
2090 std::cerr << "Ecore IM Module: Cannot connect to Panel!\n";
2096 SCIM_DEBUG_FRONTEND(1) << "Finalizing Ecore ISF IMModule...\n";
2098 // Reset this first so that the shared instance could be released correctly afterwards.
2099 _default_instance.reset();
2101 SCIM_DEBUG_FRONTEND(2) << "Finalize all IC partially.\n";
2102 while (_used_ic_impl_list)
2104 // In case in "shared input method" mode,
2105 // all contexts share only one instance,
2106 // so we need point the reference pointer correctly before finalizing.
2107 _used_ic_impl_list->si->set_frontend_data(static_cast <void*>(_used_ic_impl_list->parent));
2108 isf_imf_context_del(_used_ic_impl_list->parent->ctx);
2111 delete_all_ic_impl();
2113 _fallback_instance.reset();
2114 _fallback_factory.reset();
2116 SCIM_DEBUG_FRONTEND(2) << " Releasing BackEnd...\n";
2119 SCIM_DEBUG_FRONTEND(2) << " Releasing Config...\n";
2124 SCIM_DEBUG_FRONTEND(2) << " Deleting _config_module...\n";
2125 delete _config_module;
2132 _scim_initialized = false;
2138 open_next_factory(EcoreIMFContextISF *ic)
2140 SCIM_DEBUG_FRONTEND(2) << __FUNCTION__ << " context=" << ic->id << "\n";
2141 IMEngineFactoryPointer sf = _backend->get_next_factory("", "UTF-8", ic->impl->si->get_factory_uuid());
2146 ic->impl->si = sf->create_instance("UTF-8", ic->impl->si->get_id());
2147 ic->impl->si->set_frontend_data(static_cast <void*>(ic));
2148 ic->impl->preedit_string = WideString();
2149 ic->impl->preedit_caret = 0;
2150 attach_instance(ic->impl->si);
2151 _backend->set_default_factory(_language, sf->get_uuid());
2152 _panel_client.register_input_context(ic->id, sf->get_uuid());
2153 set_ic_capabilities(ic);
2156 if (_shared_input_method)
2158 _default_instance = ic->impl->si;
2159 ic->impl->shared_si = true;
2165 open_previous_factory(EcoreIMFContextISF *ic)
2170 SCIM_DEBUG_FRONTEND(2) << __FUNCTION__ << " context=" << ic->id << "\n";
2171 IMEngineFactoryPointer sf = _backend->get_previous_factory("", "UTF-8", ic->impl->si->get_factory_uuid());
2176 ic->impl->si = sf->create_instance("UTF-8", ic->impl->si->get_id());
2177 ic->impl->si->set_frontend_data(static_cast <void*>(ic));
2178 ic->impl->preedit_string = WideString();
2179 ic->impl->preedit_caret = 0;
2180 attach_instance(ic->impl->si);
2181 _backend->set_default_factory(_language, sf->get_uuid());
2182 _panel_client.register_input_context(ic->id, sf->get_uuid());
2183 set_ic_capabilities(ic);
2186 if (_shared_input_method)
2188 _default_instance = ic->impl->si;
2189 ic->impl->shared_si = true;
2195 open_specific_factory(EcoreIMFContextISF *ic,
2201 SCIM_DEBUG_FRONTEND(2) << __FUNCTION__ << " context=" << ic->id << "\n";
2203 // The same input method is selected, just turn on the IC.
2204 if (ic->impl->si->get_factory_uuid() == uuid)
2210 IMEngineFactoryPointer sf = _backend->get_factory(uuid);
2212 if (uuid.length() && !sf.null())
2215 ic->impl->si = sf->create_instance("UTF-8", ic->impl->si->get_id());
2216 ic->impl->si->set_frontend_data(static_cast <void*>(ic));
2217 ic->impl->preedit_string = WideString();
2218 ic->impl->preedit_caret = 0;
2219 attach_instance(ic->impl->si);
2220 _backend->set_default_factory(_language, sf->get_uuid());
2221 _panel_client.register_input_context(ic->id, sf->get_uuid());
2222 set_ic_capabilities(ic);
2225 if (_shared_input_method)
2227 _default_instance = ic->impl->si;
2228 ic->impl->shared_si = true;
2233 // turn_off_ic comment out panel_req_update_factory_info()
2235 if (ic && ic->impl->is_on)
2237 ic->impl->is_on = false;
2239 if (ic == _focused_ic)
2241 ic->impl->si->focus_out();
2243 panel_req_update_factory_info(ic);
2244 _panel_client.turn_off(ic->id);
2247 //Record the IC on/off status
2248 if (_shared_input_method)
2249 _config->write(String(SCIM_CONFIG_FRONTEND_IM_OPENED_BY_DEFAULT), false);
2251 if (ic->impl->use_preedit && ic->impl->preedit_string.length())
2253 ecore_imf_context_preedit_changed_event_add(ic->ctx);
2254 ecore_imf_context_event_callback_call(ic->ctx, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, NULL);
2255 ecore_imf_context_preedit_end_event_add(ic->ctx);
2256 ecore_imf_context_event_callback_call(ic->ctx, ECORE_IMF_CALLBACK_PREEDIT_END, NULL);
2257 ic->impl->preedit_started = false;
2263 static void initialize_modifier_bits(Display *display)
2265 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2267 if (__current_display == display)
2270 __current_display = display;
2274 __current_alt_mask = Mod1Mask;
2275 __current_meta_mask = ShiftMask | Mod1Mask;
2276 __current_super_mask = 0;
2277 __current_hyper_mask = 0;
2278 __current_numlock_mask = Mod2Mask;
2282 XModifierKeymap *mods = NULL;
2284 ::KeyCode ctrl_l = XKeysymToKeycode(display, XK_Control_L);
2285 ::KeyCode ctrl_r = XKeysymToKeycode(display, XK_Control_R);
2286 ::KeyCode meta_l = XKeysymToKeycode(display, XK_Meta_L);
2287 ::KeyCode meta_r = XKeysymToKeycode(display, XK_Meta_R);
2288 ::KeyCode alt_l = XKeysymToKeycode(display, XK_Alt_L);
2289 ::KeyCode alt_r = XKeysymToKeycode(display, XK_Alt_R);
2290 ::KeyCode super_l = XKeysymToKeycode(display, XK_Super_L);
2291 ::KeyCode super_r = XKeysymToKeycode(display, XK_Super_R);
2292 ::KeyCode hyper_l = XKeysymToKeycode(display, XK_Hyper_L);
2293 ::KeyCode hyper_r = XKeysymToKeycode(display, XK_Hyper_R);
2294 ::KeyCode numlock = XKeysymToKeycode(display, XK_Num_Lock);
2298 mods = XGetModifierMapping(display);
2302 __current_alt_mask = 0;
2303 __current_meta_mask = 0;
2304 __current_super_mask = 0;
2305 __current_hyper_mask = 0;
2306 __current_numlock_mask = 0;
2308 /* We skip the first three sets for Shift, Lock, and Control. The
2309 remaining sets are for Mod1, Mod2, Mod3, Mod4, and Mod5. */
2310 for (i = 3; i < 8; i++)
2312 for (j = 0; j < mods->max_keypermod; j++)
2314 ::KeyCode code = mods->modifiermap [i * mods->max_keypermod + j];
2315 if (! code) continue;
2316 if (code == alt_l || code == alt_r)
2317 __current_alt_mask |= (1 << i);
2318 else if (code == meta_l || code == meta_r)
2319 __current_meta_mask |= (1 << i);
2320 else if (code == super_l || code == super_r)
2321 __current_super_mask |= (1 << i);
2322 else if (code == hyper_l || code == hyper_r)
2323 __current_hyper_mask |= (1 << i);
2324 else if (code == numlock)
2325 __current_numlock_mask |= (1 << i);
2329 /* Check whether there is a combine keys mapped to Meta */
2330 if (__current_meta_mask == 0)
2334 KeySym keysym_l, keysym_r;
2336 xkey.type = KeyPress;
2337 xkey.display = display;
2339 xkey.send_event = False;
2340 xkey.x = xkey.y = xkey.x_root = xkey.y_root = 0;
2342 xkey.same_screen = False;
2343 xkey.subwindow = None;
2345 xkey.root = DefaultRootWindow(display);
2346 xkey.state = ShiftMask;
2348 xkey.keycode = meta_l;
2349 XLookupString(&xkey, buf, 32, &keysym_l, 0);
2350 xkey.keycode = meta_r;
2351 XLookupString(&xkey, buf, 32, &keysym_r, 0);
2353 if ((meta_l == alt_l && keysym_l == XK_Meta_L) || (meta_r == alt_r && keysym_r == XK_Meta_R))
2354 __current_meta_mask = ShiftMask + __current_alt_mask;
2355 else if ((meta_l == ctrl_l && keysym_l == XK_Meta_L) || (meta_r == ctrl_r && keysym_r == XK_Meta_R))
2356 __current_meta_mask = ShiftMask + ControlMask;
2359 XFreeModifiermap(mods);
2362 static unsigned int scim_x11_keymask_scim_to_x11(Display *display, uint16 scimkeymask)
2364 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2366 unsigned int state = 0;
2368 initialize_modifier_bits(display);
2370 if (scimkeymask & SCIM_KEY_ShiftMask) state |= ShiftMask;
2371 if (scimkeymask & SCIM_KEY_CapsLockMask) state |= LockMask;
2372 if (scimkeymask & SCIM_KEY_ControlMask) state |= ControlMask;
2373 if (scimkeymask & SCIM_KEY_AltMask) state |= __current_alt_mask;
2374 if (scimkeymask & SCIM_KEY_MetaMask) state |= __current_meta_mask;
2375 if (scimkeymask & SCIM_KEY_SuperMask) state |= __current_super_mask;
2376 if (scimkeymask & SCIM_KEY_HyperMask) state |= __current_hyper_mask;
2377 if (scimkeymask & SCIM_KEY_NumLockMask) state |= __current_numlock_mask;
2382 static XKeyEvent createKeyEvent(Display *display, Window &win,
2383 Window &winRoot, bool press,
2384 int keycode, int modifiers)
2386 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2390 event.display = display;
2392 event.root = winRoot;
2393 event.subwindow = None;
2394 event.time = CurrentTime;
2399 event.same_screen = EINA_TRUE;
2400 event.state = modifiers;
2401 event.keycode = XKeysymToKeycode(display, keycode);
2403 event.type = KeyPress;
2405 event.type = KeyRelease;
2406 event.send_event = EINA_FALSE;
2412 static void _x_send_key_event(const KeyEvent &key)
2414 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2416 // Obtain the X11 display.
2417 Display *display = XOpenDisplay(NULL);
2418 if (display == NULL)
2420 std::cerr << "XOpenDisplay failed\n";
2424 // Get the root window for the current display.
2427 // Find the window which has the current keyboard focus.
2428 Window winFocus = 0;
2429 int revert = RevertToParent;
2431 XGetInputFocus(display, &winFocus, &revert);
2433 // Send a fake key press event to the window.
2434 XSelectInput(display, winFocus, FocusChangeMask|KeyPressMask|KeyReleaseMask);
2435 XMapWindow(display, winFocus);
2437 unsigned int modifier = scim_x11_keymask_scim_to_x11(display, key.mask);
2439 if (key.is_key_press())
2441 event = createKeyEvent(display, winFocus, winRoot, true, key.code, modifier);
2442 XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
2446 event = createKeyEvent(display, winFocus, winRoot, false, key.code, modifier);
2447 XSendEvent(event.display, event.window, True, KeyReleaseMask, (XEvent *)&event);
2450 XCloseDisplay(display);
2454 attach_instance(const IMEngineInstancePointer &si)
2456 si->signal_connect_show_preedit_string(
2457 slot(slot_show_preedit_string));
2458 si->signal_connect_show_aux_string(
2459 slot(slot_show_aux_string));
2460 si->signal_connect_show_lookup_table(
2461 slot(slot_show_lookup_table));
2463 si->signal_connect_hide_preedit_string(
2464 slot(slot_hide_preedit_string));
2465 si->signal_connect_hide_aux_string(
2466 slot(slot_hide_aux_string));
2467 si->signal_connect_hide_lookup_table(
2468 slot(slot_hide_lookup_table));
2470 si->signal_connect_update_preedit_caret(
2471 slot(slot_update_preedit_caret));
2472 si->signal_connect_update_preedit_string(
2473 slot(slot_update_preedit_string));
2474 si->signal_connect_update_aux_string(
2475 slot(slot_update_aux_string));
2476 si->signal_connect_update_lookup_table(
2477 slot(slot_update_lookup_table));
2479 si->signal_connect_commit_string(
2480 slot(slot_commit_string));
2482 si->signal_connect_forward_key_event(
2483 slot(slot_forward_key_event));
2485 si->signal_connect_register_properties(
2486 slot(slot_register_properties));
2488 si->signal_connect_update_property(
2489 slot(slot_update_property));
2491 si->signal_connect_beep(
2494 si->signal_connect_start_helper(
2495 slot(slot_start_helper));
2497 si->signal_connect_stop_helper(
2498 slot(slot_stop_helper));
2500 si->signal_connect_send_helper_event(
2501 slot(slot_send_helper_event));
2503 si->signal_connect_get_surrounding_text(
2504 slot(slot_get_surrounding_text));
2506 si->signal_connect_delete_surrounding_text(
2507 slot(slot_delete_surrounding_text));
2510 // Implementation of slot functions
2512 slot_show_preedit_string(IMEngineInstanceBase *si)
2514 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2516 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2518 if (ic && ic->impl && _focused_ic == ic)
2520 if (ic->impl->use_preedit)
2522 if (!ic->impl->preedit_started)
2524 ecore_imf_context_preedit_start_event_add(_focused_ic->ctx);
2525 ecore_imf_context_event_callback_call(_focused_ic->ctx, ECORE_IMF_CALLBACK_PREEDIT_START, NULL);
2526 ic->impl->preedit_started = true;
2530 _panel_client.show_preedit_string(ic->id);
2535 slot_show_aux_string(IMEngineInstanceBase *si)
2537 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2539 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2541 if (ic && ic->impl && _focused_ic == ic)
2542 _panel_client.show_aux_string(ic->id);
2546 slot_show_lookup_table(IMEngineInstanceBase *si)
2548 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2550 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2552 if (ic && ic->impl && _focused_ic == ic)
2553 _panel_client.show_lookup_table(ic->id);
2557 slot_hide_preedit_string(IMEngineInstanceBase *si)
2559 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2561 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2563 if (ic && ic->impl && _focused_ic == ic)
2566 if (ic->impl->preedit_string.length())
2568 ic->impl->preedit_string = WideString();
2569 ic->impl->preedit_caret = 0;
2570 ic->impl->preedit_attrlist.clear();
2573 if (ic->impl->use_preedit)
2577 ecore_imf_context_preedit_changed_event_add(ic->ctx);
2578 ecore_imf_context_event_callback_call(ic->ctx, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, NULL);
2580 if (ic->impl->preedit_started)
2582 ecore_imf_context_preedit_end_event_add(ic->ctx);
2583 ecore_imf_context_event_callback_call(ic->ctx, ECORE_IMF_CALLBACK_PREEDIT_END, NULL);
2584 ic->impl->preedit_started = false;
2588 _panel_client.hide_preedit_string(ic->id);
2593 slot_hide_aux_string(IMEngineInstanceBase *si)
2595 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2597 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2599 if (ic && ic->impl && _focused_ic == ic)
2600 _panel_client.hide_aux_string(ic->id);
2604 slot_hide_lookup_table(IMEngineInstanceBase *si)
2606 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2608 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2610 if (ic && ic->impl && _focused_ic == ic)
2611 _panel_client.hide_lookup_table(ic->id);
2615 slot_update_preedit_caret(IMEngineInstanceBase *si, int caret)
2617 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2619 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2621 if (ic && ic->impl && _focused_ic == ic && ic->impl->preedit_caret != caret)
2623 ic->impl->preedit_caret = caret;
2624 if (ic->impl->use_preedit)
2626 if (!ic->impl->preedit_started)
2628 ecore_imf_context_preedit_start_event_add(ic->ctx);
2629 ecore_imf_context_event_callback_call(ic->ctx, ECORE_IMF_CALLBACK_PREEDIT_START, NULL);
2630 ic->impl->preedit_started = true;
2632 ecore_imf_context_preedit_changed_event_add(ic->ctx);
2633 ecore_imf_context_event_callback_call(ic->ctx, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, NULL);
2636 _panel_client.update_preedit_caret(ic->id, caret);
2641 slot_update_preedit_string(IMEngineInstanceBase *si,
2642 const WideString & str,
2643 const AttributeList & attrs)
2645 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2647 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2649 if (ic && ic->impl && _focused_ic == ic && (ic->impl->preedit_string != str || str.length()))
2651 ic->impl->preedit_string = str;
2652 ic->impl->preedit_attrlist = attrs;
2653 if (ic->impl->use_preedit)
2655 if (!ic->impl->preedit_started)
2657 ecore_imf_context_preedit_start_event_add(_focused_ic->ctx);
2658 ecore_imf_context_event_callback_call(_focused_ic->ctx, ECORE_IMF_CALLBACK_PREEDIT_START, NULL);
2659 ic->impl->preedit_started = true;
2661 ic->impl->preedit_caret = str.length();
2662 ic->impl->preedit_updating = true;
2663 ecore_imf_context_preedit_changed_event_add(ic->ctx);
2664 ecore_imf_context_event_callback_call(ic->ctx, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, NULL);
2665 ic->impl->preedit_updating = false;
2669 _panel_client.update_preedit_string(ic->id, str, attrs);
2675 slot_update_aux_string(IMEngineInstanceBase *si,
2676 const WideString & str,
2677 const AttributeList & attrs)
2679 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2681 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2683 if (ic && ic->impl && _focused_ic == ic)
2684 _panel_client.update_aux_string(ic->id, str, attrs);
2688 slot_commit_string(IMEngineInstanceBase *si,
2689 const WideString & str)
2691 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2693 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2697 ecore_imf_context_commit_event_add(ic->ctx, utf8_wcstombs(str).c_str());
2698 ecore_imf_context_event_callback_call(ic->ctx, ECORE_IMF_CALLBACK_COMMIT, (void *)utf8_wcstombs(str).c_str());
2703 slot_forward_key_event(IMEngineInstanceBase *si,
2704 const KeyEvent & key)
2706 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2708 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2710 if (ic && _focused_ic == ic)
2712 if (!_fallback_instance->process_key_event(key))
2713 _x_send_key_event(key);
2718 slot_update_lookup_table(IMEngineInstanceBase *si,
2719 const LookupTable & table)
2721 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2723 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2725 if (ic && ic->impl && _focused_ic == ic)
2726 _panel_client.update_lookup_table(ic->id, table);
2730 slot_register_properties(IMEngineInstanceBase *si,
2731 const PropertyList & properties)
2733 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2735 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2737 if (ic && ic->impl && _focused_ic == ic)
2738 _panel_client.register_properties(ic->id, properties);
2742 slot_update_property(IMEngineInstanceBase *si,
2743 const Property & property)
2745 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2747 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2749 if (ic && ic->impl && _focused_ic == ic)
2750 _panel_client.update_property(ic->id, property);
2754 slot_beep(IMEngineInstanceBase *si __UNUSED__)
2756 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2760 slot_start_helper(IMEngineInstanceBase *si,
2761 const String &helper_uuid)
2763 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2765 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " helper= " << helper_uuid << " context="
2766 << (ic ? ic->id : -1) << " ic=" << ic
2767 << " ic-uuid=" << ((ic ) ? ic->impl->si->get_factory_uuid() : "") << "...\n";
2770 _panel_client.start_helper(ic->id, helper_uuid);
2774 slot_stop_helper(IMEngineInstanceBase *si,
2775 const String &helper_uuid)
2777 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2779 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " helper= " << helper_uuid << " context=" << (ic ? ic->id : -1) << " ic=" << ic << "...\n";
2782 _panel_client.stop_helper(ic->id, helper_uuid);
2786 slot_send_helper_event(IMEngineInstanceBase *si,
2787 const String &helper_uuid,
2788 const Transaction &trans)
2790 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2792 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " helper= " << helper_uuid << " context="
2793 << (ic ? ic->id : -1) << " ic=" << ic
2794 << " ic-uuid=" << ((ic) ? ic->impl->si->get_factory_uuid() : "") << "...\n";
2797 _panel_client.send_helper_event(ic->id, helper_uuid, trans);
2801 slot_get_surrounding_text(IMEngineInstanceBase *si,
2807 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2809 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2811 if (ic && ic->impl && _focused_ic == ic)
2813 char *surrounding = NULL;
2815 if (ecore_imf_context_surrounding_get(_focused_ic->ctx, &surrounding, &cursor_index))
2817 SCIM_DEBUG_FRONTEND(2) << "Surrounding text: " << surrounding <<"\n";
2818 SCIM_DEBUG_FRONTEND(2) << "Cursor Index : " << cursor_index <<"\n";
2819 WideString before(utf8_mbstowcs(String(surrounding, surrounding + cursor_index)));
2820 WideString after(utf8_mbstowcs(String(surrounding + cursor_index)));
2821 if (maxlen_before > 0 && ((unsigned int)maxlen_before) < before.length())
2822 before = WideString(before.begin() + (before.length() - maxlen_before), before.end());
2823 else if (maxlen_before == 0) before = WideString();
2824 if (maxlen_after > 0 && ((unsigned int)maxlen_after) < after.length())
2825 after = WideString(after.begin(), after.begin() + maxlen_after);
2826 else if (maxlen_after == 0) after = WideString();
2827 text = before + after;
2828 cursor = before.length();
2836 slot_delete_surrounding_text(IMEngineInstanceBase *si,
2840 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2842 EcoreIMFContextISF *ic = static_cast<EcoreIMFContextISF *>(si->get_frontend_data());
2844 if (ic && ic->impl && _focused_ic == ic)
2846 Ecore_IMF_Event_Delete_Surrounding ev;
2847 ev.ctx = _focused_ic->ctx;
2850 ecore_imf_context_delete_surrounding_event_add(_focused_ic->ctx, offset, len);
2851 ecore_imf_context_event_callback_call(_focused_ic->ctx, ECORE_IMF_CALLBACK_DELETE_SURROUNDING, &ev);
2858 reload_config_callback(const ConfigPointer &config)
2860 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2862 _frontend_hotkey_matcher.load_hotkeys(config);
2863 _imengine_hotkey_matcher.load_hotkeys(config);
2867 scim_string_to_key(key,
2868 config->read(String(SCIM_CONFIG_HOTKEYS_FRONTEND_VALID_KEY_MASK),
2869 String("Shift+Control+Alt+Lock")));
2871 _valid_key_mask = (key.mask > 0)?(key.mask):0xFFFF;
2872 _valid_key_mask |= SCIM_KEY_ReleaseMask;
2873 // Special treatment for two backslash keys on jp106 keyboard.
2874 _valid_key_mask |= SCIM_KEY_QuirkKanaRoMask;
2876 _on_the_spot = config->read(String(SCIM_CONFIG_FRONTEND_ON_THE_SPOT), _on_the_spot);
2877 _shared_input_method = config->read(String(SCIM_CONFIG_FRONTEND_SHARED_INPUT_METHOD), _shared_input_method);
2879 // Get keyboard layout setting
2880 // Flush the global config first, in order to load the new configs from disk.
2881 scim_global_config_flush();
2883 _keyboard_layout = scim_get_default_keyboard_layout();
2887 fallback_commit_string_cb(IMEngineInstanceBase *si __UNUSED__,
2888 const WideString &str)
2890 SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
2892 if (_focused_ic && _focused_ic->impl)
2894 ecore_imf_context_commit_event_add(_focused_ic->ctx, utf8_wcstombs(str).c_str());
2895 ecore_imf_context_event_callback_call(_focused_ic->ctx, ECORE_IMF_CALLBACK_COMMIT, (void *)utf8_wcstombs(str).c_str());