Add get_surrounding_text and get_selection_text request 50/74150/3
authorJihoon Kim <jihoon48.kim@samsung.com>
Mon, 13 Jun 2016 04:27:50 +0000 (21:27 -0700)
committerJihoon Kim <jihoon48.kim@samsung.com>
Mon, 13 Jun 2016 04:32:29 +0000 (21:32 -0700)
Change-Id: I9fd7ced699d8b052802cc672d86e245200130bfe

ism/extras/wayland_immodule/wayland_imcontext.c
ism/modules/panelagent/wayland/isf_wsc_context.h
ism/modules/panelagent/wayland/wayland_panel_agent_module.cpp
ism/src/scim_helper.cpp
ism/src/scim_helper.h

index f1b60a6..4b89ab6 100644 (file)
@@ -82,6 +82,8 @@ struct _WaylandIMContext
         int32_t cursor;
     } pending_preedit;
 
+    int32_t cursor_position;
+
     struct
     {
         int x;
@@ -288,30 +290,9 @@ send_cursor_location(WaylandIMContext *imcontext)
 static void
 update_state(WaylandIMContext *imcontext)
 {
-    char *surrounding = NULL;
-    char *selection = NULL;
-    int cursor_pos;
-
     if (!imcontext->ctx)
         return;
 
-    /* cursor_pos is a byte index */
-    if (ecore_imf_context_surrounding_get(imcontext->ctx, &surrounding, &cursor_pos)) {
-        if (imcontext->text_input)
-            wl_text_input_set_surrounding_text(imcontext->text_input, surrounding,
-                    cursor_pos, cursor_pos);
-
-        if (surrounding)
-            free(surrounding);
-    }
-
-    ecore_imf_context_selection_get(imcontext->ctx, &selection);
-    if (imcontext->text_input)
-        wl_text_input_set_selection_text(imcontext->text_input, selection ? selection : "");
-
-    if (selection)
-        free(selection);
-
     send_cursor_location (imcontext);
 
     if (imcontext->text_input) {
@@ -468,6 +449,7 @@ static Eina_Bool
 show_input_panel(Ecore_IMF_Context *ctx)
 {
     WaylandIMContext *imcontext = (WaylandIMContext *)ecore_imf_context_data_get(ctx);
+
     char *surrounding = NULL;
     int cursor_pos;
 
@@ -537,16 +519,12 @@ show_input_panel(Ecore_IMF_Context *ctx)
             new_purpose);
 
     if (ecore_imf_context_surrounding_get(imcontext->ctx, &surrounding, &cursor_pos)) {
-        if (imcontext->text_input)
-            wl_text_input_set_surrounding_text(imcontext->text_input, surrounding,
-                    cursor_pos, cursor_pos);
+        if (surrounding)
+            free (surrounding);
 
-        if (surrounding) {
-            free(surrounding);
-            surrounding = NULL;
-        }
+        imcontext->cursor_position = cursor_pos;
+        wl_text_input_set_cursor_position(imcontext->text_input, cursor_pos);
     }
-
     // TIZEN_ONLY(20150716): Support return key type
     wl_text_input_set_return_key_type(imcontext->text_input,
             imcontext->return_key_type);
@@ -978,6 +956,82 @@ text_input_input_panel_data(void                 *data,
     memcpy(imcontext->input_panel_data, input_panel_data, length);
     imcontext->input_panel_data_length = length;
 }
+
+static void
+text_input_get_selection_text (void                 *data,
+                              struct wl_text_input *text_input EINA_UNUSED,
+                              uint32_t              serial)
+{
+    char *selection = NULL;
+    LOGD ("%d", serial);
+    WaylandIMContext *imcontext = (WaylandIMContext *)data;
+    if (!imcontext || !imcontext->ctx) {
+        LOGD ("");
+        return;
+    }
+
+    ecore_imf_context_selection_get (imcontext->ctx, &selection);
+    if (imcontext->text_input) {
+        LOGD ("selection :%s", selection ? selection : "");
+        wl_text_input_set_selection_text (imcontext->text_input, serial, selection ? selection : "");
+    }
+
+    if (selection)
+        free (selection);
+}
+
+static void
+text_input_get_surrounding_text (void                 *data,
+                                struct wl_text_input *text_input EINA_UNUSED,
+                                uint32_t              serial,
+                                uint32_t              maxlen_before,
+                                uint32_t              maxlen_after)
+{
+    int cursor_pos;
+    char *surrounding = NULL;
+    LOGD ("serial: %d maxlen_before: %d maxlen_after: %d", serial, maxlen_before, maxlen_after);
+    WaylandIMContext *imcontext = (WaylandIMContext *)data;
+    if (!imcontext || !imcontext->ctx) {
+        LOGD ("");
+        return;
+    }
+
+    /* cursor_pos is a byte index */
+    if (ecore_imf_context_surrounding_get (imcontext->ctx, &surrounding, &cursor_pos)) {
+        LOGD ("surrounding :%s, cursor: %d", surrounding? surrounding : "", cursor_pos);
+        if (imcontext->text_input) {
+            Eina_Unicode *wide_surrounding = eina_unicode_utf8_to_unicode (surrounding, NULL);
+            size_t wlen = eina_unicode_strlen (wide_surrounding);
+
+            if (cursor_pos > (int)wlen || cursor_pos < 0)
+                cursor_pos = 0;
+
+            if (maxlen_before > cursor_pos)
+                maxlen_before = 0;
+            else
+                maxlen_before = cursor_pos - maxlen_before;
+
+            if (maxlen_after > wlen - cursor_pos)
+                maxlen_after = wlen;
+            else
+                maxlen_after = cursor_pos + maxlen_after;
+
+            char *req_surrounding = eina_unicode_unicode_to_utf8_range (wide_surrounding + maxlen_before, maxlen_after - maxlen_before, NULL);
+
+            wl_text_input_set_surrounding_text (imcontext->text_input,
+                    serial, req_surrounding ? req_surrounding : "", cursor_pos);
+
+            if (req_surrounding)
+                free (req_surrounding);
+
+            if (wide_surrounding)
+                free (wide_surrounding);
+        }
+
+        if (surrounding)
+            free (surrounding);
+    }
+}
 //
 
 static const struct wl_text_input_listener text_input_listener =
@@ -999,7 +1053,9 @@ static const struct wl_text_input_listener text_input_listener =
     text_input_selection_region,
     text_input_private_command,
     text_input_input_panel_geometry,
-    text_input_input_panel_data
+    text_input_input_panel_data,
+    text_input_get_selection_text,
+    text_input_get_surrounding_text
     //
 };
 
@@ -1059,11 +1115,11 @@ wayland_im_context_add(Ecore_IMF_Context *ctx)
 }
 
 EAPI void
-wayland_im_context_del(Ecore_IMF_Context *ctx)
+wayland_im_context_del (Ecore_IMF_Context *ctx)
 {
     WaylandIMContext *imcontext = (WaylandIMContext *)ecore_imf_context_data_get(ctx);
 
-    LOGD("context_del. ctx : %p", ctx);
+    LOGD ("context_del. ctx : %p", ctx);
 
     // TIZEN_ONLY(20150708): Support back key
     if (_focused_ctx == ctx)
@@ -1079,28 +1135,28 @@ wayland_im_context_del(Ecore_IMF_Context *ctx)
     //
 
     if (imcontext->language) {
-        free(imcontext->language);
+        free (imcontext->language);
         imcontext->language = NULL;
     }
 
     // TIZEN_ONLY(20150922): Support to set input panel data
     if (imcontext->imdata) {
-        free(imcontext->imdata);
+        free (imcontext->imdata);
         imcontext->imdata = NULL;
         imcontext->imdata_size = 0;
     }
 
     if (imcontext->input_panel_data) {
-        free(imcontext->input_panel_data);
+        free (imcontext->input_panel_data);
         imcontext->input_panel_data = NULL;
         imcontext->input_panel_data_length = 0;
     }
     //
 
     if (imcontext->text_input)
-        wl_text_input_destroy(imcontext->text_input);
+        wl_text_input_destroy (imcontext->text_input);
 
-    clear_preedit(imcontext);
+    clear_preedit (imcontext);
 }
 
 EAPI void
@@ -1108,6 +1164,8 @@ wayland_im_context_reset(Ecore_IMF_Context *ctx)
 {
     WaylandIMContext *imcontext = (WaylandIMContext *)ecore_imf_context_data_get(ctx);
 
+    LOGD("ctx : %p", ctx);
+
     commit_preedit(imcontext);
     clear_preedit(imcontext);
 
@@ -1211,14 +1269,16 @@ wayland_im_context_preedit_string_with_attributes_get(Ecore_IMF_Context  *ctx,
 }
 
 EAPI void
-wayland_im_context_cursor_position_set(Ecore_IMF_Context *ctx,
+wayland_im_context_cursor_position_set (Ecore_IMF_Context *ctx,
                                        int                cursor_pos)
 {
     WaylandIMContext *imcontext = (WaylandIMContext *)ecore_imf_context_data_get(ctx);
 
-    LOGD("set cursor position (cursor: %d)", cursor_pos);
-
-    update_state(imcontext);
+    LOGD ("set cursor position (cursor: %d)", cursor_pos);
+    if (imcontext->cursor_position != cursor_pos) {
+        imcontext->cursor_position = cursor_pos;
+        wl_text_input_set_cursor_position (imcontext->text_input, cursor_pos);
+    }
 }
 
 EAPI void
index a84d98b..8b880d9 100644 (file)
@@ -113,7 +113,6 @@ void isf_wsc_context_add (WSCContextISF *wsc_ctx);
 void isf_wsc_context_del (WSCContextISF *wsc_ctx);
 void isf_wsc_context_focus_in (WSCContextISF *wsc_ctx);
 void isf_wsc_context_focus_out (WSCContextISF *wsc_ctx);
-void isf_wsc_context_cursor_position_set (WSCContextISF *wsc_ctx, int cursor_pos);
 void isf_wsc_context_preedit_string_get (WSCContextISF *wsc_ctx, char** str, int *cursor_pos);
 void isf_wsc_context_autocapital_type_set (WSCContextISF* wsc_ctx, Ecore_IMF_Autocapital_Type autocapital_type);
 void isf_wsc_context_bidi_direction_set (WSCContextISF* wsc_ctx, Ecore_IMF_BiDi_Direction direction);
index 36b7b27..897d92d 100644 (file)
@@ -178,10 +178,6 @@ static void     _update_preedit_string        (int                     context,
                                                          const WideString       &str,
                                                          const AttributeList    &attrs,
                                                          int               caret);
-static void     _get_surrounding_text         (int                     context,
-                                                         int                     maxlen_before,
-                                                         int                     maxlen_after);
-static void     panel_req_update_cursor_position        (WSCContextISF     *ic, int cursor_pos);
 static void     panel_req_update_bidi_direction         (WSCContextISF     *ic, int direction);
 
 /* Panel iochannel handler*/
@@ -201,11 +197,6 @@ static void     finalize                                (void);
 static void     send_wl_key_event                       (WSCContextISF *ic, const KeyEvent &key, bool fake);
 static void     _hide_preedit_string                    (int context, bool update_preedit);
 
-static bool     slot_get_surrounding_text               (WideString             &text,
-                                                         int                    &cursor,
-                                                         int                     maxlen_before,
-                                                         int                     maxlen_after);
-
 /* Local variables declaration */
 static String                                           _language;
 static WSCContextISFImpl                               *_used_ic_impl_list          = 0;
@@ -261,7 +252,7 @@ InfoManager* g_info_manager = NULL;
 // Implementation of Wayland Input Method functions.
 /////////////////////////////////////////////////////////////////////////////
 static void
-_wsc_im_ctx_surrounding_text(void *data, struct wl_input_method_context *im_ctx, const char *text, uint32_t cursor, uint32_t anchor)
+_wsc_im_ctx_surrounding_text(void *data, struct wl_input_method_context *im_ctx, uint32_t serial, const char *text, uint32_t cursor_position)
 {
     LOGD ("");
     WSCContextISF *wsc_ctx = (WSCContextISF*)data;
@@ -271,12 +262,10 @@ _wsc_im_ctx_surrounding_text(void *data, struct wl_input_method_context *im_ctx,
         free (wsc_ctx->surrounding_text);
 
     wsc_ctx->surrounding_text = strdup (text ? text : "");
-    wsc_ctx->surrounding_cursor = cursor;
+    wsc_ctx->surrounding_cursor = cursor_position;
     g_info_manager->socket_update_surrounding_text (wsc_ctx->surrounding_text, wsc_ctx->surrounding_cursor);
 
-    isf_wsc_context_cursor_position_set(wsc_ctx, cursor);
-
-    LOGD ("text : '%s', cursor : %d\n", text, cursor);
+    LOGD ("text : '%s', cursor : %d\n", text, cursor_position);
 }
 
 static void
@@ -341,9 +330,6 @@ _wsc_im_ctx_commit_state(void *data, struct wl_input_method_context *im_ctx, uin
 
     wsc_ctx->serial = serial;
 
-    if (wsc_ctx->surrounding_text)
-        LOGD ("Surrounding text updated: '%s'\n", wsc_ctx->surrounding_text);
-
     if (wsc_ctx->language)
         wl_input_method_context_language (im_ctx, wsc_ctx->serial, wsc_ctx->language);
 }
@@ -423,7 +409,7 @@ _wsc_im_ctx_bidi_direction(void *data, struct wl_input_method_context *im_ctx, u
 }
 
 static void
-_wsc_im_ctx_selection_text(void *data, struct wl_input_method_context *im_ctx, const char *text)
+_wsc_im_ctx_selection_text(void *data, struct wl_input_method_context *im_ctx, uint32_t serial, const char *text)
 {
     WSCContextISF *wsc_ctx = (WSCContextISF*)data;
 
@@ -433,6 +419,17 @@ _wsc_im_ctx_selection_text(void *data, struct wl_input_method_context *im_ctx, c
     g_info_manager->socket_update_selection (text);
 }
 
+static void
+_wsc_im_ctx_cursor_position(void *data, struct wl_input_method_context *im_ctx, uint32_t cursor_pos)
+{
+    WSCContextISF *wsc_ctx = (WSCContextISF*)data;
+
+    LOGD ("im_context = %p cursor_pos = %d\n", im_ctx, cursor_pos);
+    if (!wsc_ctx) return;
+    caps_mode_check (wsc_ctx, EINA_FALSE, EINA_TRUE);
+    g_info_manager->socket_update_cursor_position (cursor_pos);
+}
+
 static const struct wl_input_method_context_listener wsc_im_context_listener = {
      _wsc_im_ctx_surrounding_text,
      _wsc_im_ctx_reset,
@@ -444,7 +441,8 @@ static const struct wl_input_method_context_listener wsc_im_context_listener = {
      _wsc_im_ctx_return_key_disabled,
      _wsc_im_ctx_input_panel_data,
      _wsc_im_ctx_bidi_direction,
-     _wsc_im_ctx_selection_text
+     _wsc_im_ctx_selection_text,
+     _wsc_im_ctx_cursor_position,
 };
 
 static void
@@ -1498,27 +1496,6 @@ isf_wsc_context_focus_out (WSCContextISF *wsc_ctx)
 }
 
 void
-isf_wsc_context_cursor_position_set (WSCContextISF *wsc_ctx, int cursor_pos)
-{
-    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
-    LOGD ("");
-    WSCContextISF* context_scim = wsc_ctx;
-
-    if (context_scim && context_scim->impl && context_scim == _focused_ic) {
-        if (context_scim->impl->cursor_pos != cursor_pos) {
-            LOGD ("ctx : %p, cursor pos : %d\n", wsc_ctx, cursor_pos);
-            context_scim->impl->cursor_pos = cursor_pos;
-
-            caps_mode_check (wsc_ctx, EINA_FALSE, EINA_TRUE);
-
-            if (context_scim->impl->preedit_updating)
-                return;
-            panel_req_update_cursor_position (context_scim, cursor_pos);
-        }
-    }
-}
-
-void
 isf_wsc_context_preedit_string_get (WSCContextISF *wsc_ctx, char** str, int *cursor_pos)
 {
     SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
@@ -2177,16 +2154,6 @@ panel_req_show_help (WSCContextISF *ic)
 #endif
 
 static void
-panel_req_update_cursor_position (WSCContextISF *ic, int cursor_pos)
-{
-    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
-    LOGD ("");
-
-    if (ic)
-        g_info_manager->socket_update_cursor_position (cursor_pos);
-}
-
-static void
 panel_req_update_bidi_direction   (WSCContextISF *ic, int direction)
 {
     SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";
@@ -2470,22 +2437,6 @@ _update_preedit_string (int context,
     }
 }
 
-static void
-_get_surrounding_text (int context, int maxlen_before, int maxlen_after)
-{
-    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
-    LOGD ("");
-    WSCContextISF* ic = find_ic (context);
-
-    if (ic && ic->impl && _focused_ic == ic) {
-        int cursor = 0;
-        WideString text = WideString ();
-        slot_get_surrounding_text (text, cursor, maxlen_before, maxlen_after);
-        String _str = utf8_wcstombs (text);
-        g_info_manager->socket_update_surrounding_text (_str, (uint32)cursor);
-    }
-}
-
 void
 initialize (void)
 {
@@ -2635,55 +2586,6 @@ static void send_wl_key_event (WSCContextISF *ic, const KeyEvent &key, bool fake
         wsc_context_send_key (ic, key.code, modifiers, time, key.is_key_press ());
 }
 
-static bool
-slot_get_surrounding_text (WideString            &text,
-                           int                   &cursor,
-                           int                    maxlen_before,
-                           int                    maxlen_after)
-{
-    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
-    LOGD ("");
-
-    char *surrounding = NULL;
-    int   cursor_index;
-    if (wsc_context_surrounding_get (_focused_ic, &surrounding, &cursor_index)) {
-        SCIM_DEBUG_FRONTEND(2) << "Surrounding text: " << surrounding <<"\n";
-        SCIM_DEBUG_FRONTEND(2) << "Cursor Index    : " << cursor_index <<"\n";
-
-        if (!surrounding)
-            return false;
-
-        if (cursor_index < 0) {
-            free (surrounding);
-            surrounding = NULL;
-            return false;
-        }
-
-        WideString before = utf8_mbstowcs (String (surrounding));
-        free (surrounding);
-        surrounding = NULL;
-
-        if (cursor_index > (int)before.length ())
-            return false;
-        WideString after = before;
-        before = before.substr (0, cursor_index);
-        after =  after.substr (cursor_index, after.length () - cursor_index);
-        if (maxlen_before > 0 && ((unsigned int)maxlen_before) < before.length ())
-            before = WideString (before.begin () + (before.length () - maxlen_before), before.end ());
-        else if (maxlen_before == 0)
-            before = WideString ();
-        if (maxlen_after > 0 && ((unsigned int)maxlen_after) < after.length ())
-            after = WideString (after.begin (), after.begin () + maxlen_after);
-        else if (maxlen_after == 0)
-            after = WideString ();
-        text = before + after;
-        cursor = before.length ();
-        return true;
-    }
-
-    return false;
-}
-
 static void
 reload_config_callback (const ConfigPointer &config)
 {
@@ -2832,7 +2734,8 @@ public:
     void
     socket_helper_get_surrounding_text (int id, uint32 context_id, uint32 maxlen_before, uint32 maxlen_after) {
         LOGD ("client id:%d", id);
-        _get_surrounding_text (context_id, maxlen_before, maxlen_after);
+        WSCContextISF* ic = find_ic (context_id);
+        wl_input_method_context_get_surrounding_text(ic->im_ctx, ic->serial, maxlen_before, maxlen_after);
     }
 
     void
@@ -2872,6 +2775,12 @@ public:
         }
     }
 
+    void
+    socket_helper_get_selection (int id, uint32 context_id) {
+        LOGD ("client id:%d", id);
+        WSCContextISF* ic = find_ic (context_id);
+        wl_input_method_context_get_selection_text (ic->im_ctx, ic->serial);
+    }
 };
 
 static scim::PanelAgentPointer instance;
index 85b7099..a7537fd 100644 (file)
@@ -400,7 +400,7 @@ public:
     {
         LOGD ("");
         String _text;
-        thiz->get_selection (_text);
+        thiz->get_selection_text (_text);
         text = utf8_mbstowcs (_text);
         return true;
     }
@@ -816,6 +816,7 @@ HelperAgent::filter_event ()
                 uint32 cursor_pos;
                 if (m_impl->recv.get_data (cursor_pos)) {
                     m_impl->cursor_pos = cursor_pos;
+                    LOGD ("update cursor position %d", cursor_pos);
                     m_impl->signal_update_cursor_position (this, ic, ic_uuid, (int) cursor_pos);
                         if (!m_impl->si.null ()) m_impl->si->update_cursor_position(cursor_pos);
                 }
@@ -832,7 +833,7 @@ HelperAgent::filter_event ()
                         free (m_impl->surrounding_text);
                     m_impl->surrounding_text = strdup (text.c_str ());
                     m_impl->cursor_pos = cursor;
-                    LOGD ("surrounding text: %s, %d", m_impl->surrounding_text, m_impl->cursor_pos);
+                    LOGD ("surrounding text: %s, %d", m_impl->surrounding_text, cursor);
                     if (m_impl->need_update_surrounding_text) {
                         m_impl->need_update_surrounding_text = false;
                         m_impl->signal_update_surrounding_text (this, ic, text, (int) cursor);
@@ -1461,6 +1462,7 @@ HelperAgent::send_key_event (int            ic,
                              const KeyEvent &key) const
 {
     LOGD ("");
+
     //FIXME: remove shift_mode_off, shift_mode_on, shift_mode_lock from ISE side
     if (key.code == SHIFT_MODE_OFF ||
         key.code == SHIFT_MODE_ON ||
@@ -1953,30 +1955,35 @@ void
 HelperAgent::get_surrounding_text (int maxlen_before, int maxlen_after, String &text, int &cursor)
 {
     LOGD ("");
+    if (m_impl->surrounding_text) {
+        free (m_impl->surrounding_text);
+        m_impl->surrounding_text = NULL;
+    }
 
-    if (!m_impl || !m_impl->surrounding_text) {
-        text = utf8_wcstombs (WideString ());
-        cursor = 0;
+    if (!m_impl->socket_active.is_connected ())
         return;
-    }
 
-    WideString before = utf8_mbstowcs (String (m_impl->surrounding_text));
+    m_impl->send.clear ();
+    m_impl->send.put_command (SCIM_TRANS_CMD_REQUEST);
+    m_impl->send.put_data (m_impl->magic_active);
+    m_impl->send.put_command (SCIM_TRANS_CMD_GET_SURROUNDING_TEXT);
+    m_impl->send.put_data ("");
+    m_impl->send.put_data (maxlen_before);
+    m_impl->send.put_data (maxlen_after);
+    m_impl->send.write_to_socket (m_impl->socket_active, m_impl->magic_active);
+
+    for (int i = 0; i < 3; i++) {
+        if (filter_event () && m_impl->surrounding_text) {
+            text = m_impl->surrounding_text;
+            cursor = m_impl->cursor_pos;
+            break;
+        }
+    }
 
-    if (m_impl->cursor_pos > before.length ())
-        return;
-    WideString after = before;
-    before = before.substr (0, m_impl->cursor_pos);
-    after =  after.substr (m_impl->cursor_pos, after.length () - m_impl->cursor_pos);
-    if (maxlen_before > 0 && ((unsigned int)maxlen_before) < before.length ())
-        before = WideString (before.begin () + (before.length () - maxlen_before), before.end ());
-    else if (maxlen_before == 0)
-        before = WideString ();
-    if (maxlen_after > 0 && ((unsigned int)maxlen_after) < after.length ())
-        after = WideString (after.begin (), after.begin () + maxlen_after);
-    else if (maxlen_after == 0)
-        after = WideString ();
-    text = utf8_wcstombs (before + after);
-    cursor = before.length ();
+    if (m_impl->surrounding_text) {
+        free (m_impl->surrounding_text);
+        m_impl->surrounding_text = NULL;
+    }
 }
 
 /**
@@ -1990,32 +1997,6 @@ HelperAgent::delete_surrounding_text (int offset, int len) const
 {
     LOGD ("offset = %d, len = %d", offset, len);
 
-    if (!m_impl)
-        return;
-
-    WideString ws;
-
-    if (!m_impl->surrounding_text)
-        ws = utf8_mbstowcs (String (""));
-    else
-        ws = utf8_mbstowcs (String (m_impl->surrounding_text));
-
-    int _offset = offset + m_impl->cursor_pos;
-
-    if (len <= 0 || _offset < 0 || _offset + len > (int)ws.length ()) {
-        LOGW ("invalid offset and len");
-        return;
-    }
-
-    WideString sub_ws_before = ws.substr (0, _offset);
-    WideString sub_ws_after = ws.substr (_offset + len, ws.length ());
-    WideString new_ws = sub_ws_before + sub_ws_after;
-
-    if (m_impl->surrounding_text)
-        free (m_impl->surrounding_text);
-    m_impl->surrounding_text = strdup (utf8_wcstombs (new_ws).c_str ());
-    m_impl->cursor_pos = _offset;
-
     if (m_impl->socket_active.is_connected ()) {
         m_impl->send.clear ();
         m_impl->send.put_command (SCIM_TRANS_CMD_REQUEST);
@@ -2054,11 +2035,35 @@ HelperAgent::get_selection (const String &uuid) const
  * @param text The selection text.
  */
 void
-HelperAgent::get_selection (String &text) const
+HelperAgent::get_selection_text (String &text)
 {
     LOGD ("");
-    if (m_impl->selection_text)
-        text = m_impl->selection_text;
+    if (m_impl->selection_text) {
+        free (m_impl->selection_text);
+        m_impl->selection_text = NULL;
+    }
+
+    if (!m_impl->socket_active.is_connected ())
+        return;
+
+    m_impl->send.clear ();
+    m_impl->send.put_command (SCIM_TRANS_CMD_REQUEST);
+    m_impl->send.put_data (m_impl->magic_active);
+    m_impl->send.put_command (SCIM_TRANS_CMD_GET_SELECTION);
+    m_impl->send.put_data ("");
+    m_impl->send.write_to_socket (m_impl->socket_active, m_impl->magic_active);
+
+    for (int i = 0; i < 3; i++) {
+        if (filter_event () && m_impl->selection_text) {
+            text = m_impl->selection_text;
+            break;
+        }
+    }
+
+    if (m_impl->selection_text) {
+        free (m_impl->selection_text);
+        m_impl->selection_text = NULL;
+    }
 }
 
 /**
index 6e73d74..268254a 100644 (file)
@@ -584,7 +584,7 @@ public:
      *
      * @param text The selection text.
      */
-    void get_selection       (String                      &text) const;
+    void get_selection_text       (String                      &text);
 
     /**
      * @brief Request to selected text.