forked from enlightenment/enlightenment
parent
1454cbbd04
commit
1507ace882
|
@ -0,0 +1,573 @@
|
|||
#include "e.h"
|
||||
#include "e_kbd_buf.h"
|
||||
#include "e_kbd_dict.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
static E_Kbd_Buf_Layout *
|
||||
_e_kbd_buf_new(void)
|
||||
{
|
||||
E_Kbd_Buf_Layout *kbl;
|
||||
|
||||
kbl = E_NEW(E_Kbd_Buf_Layout, 1);
|
||||
kbl->ref =1;
|
||||
return kbl;
|
||||
}
|
||||
|
||||
static void
|
||||
_e_kbd_buf_layout_ref(E_Kbd_Buf_Layout *kbl)
|
||||
{
|
||||
kbl->ref++;
|
||||
}
|
||||
|
||||
static void
|
||||
_e_kbd_buf_layout_unref(E_Kbd_Buf_Layout *kbl)
|
||||
{
|
||||
kbl->ref--;
|
||||
if (kbl->ref > 0) return;
|
||||
while (kbl->keys)
|
||||
{
|
||||
E_Kbd_Buf_Key *ky;
|
||||
|
||||
ky = kbl->keys->data;
|
||||
if (ky->key) eina_stringshare_del(ky->key);
|
||||
if (ky->key_shift) eina_stringshare_del(ky->key_shift);
|
||||
if (ky->key_capslock) eina_stringshare_del(ky->key_capslock);
|
||||
free(ky);
|
||||
kbl->keys = eina_list_remove_list(kbl->keys, kbl->keys);
|
||||
}
|
||||
free(kbl);
|
||||
}
|
||||
|
||||
static void
|
||||
_e_kbd_buf_string_matches_clear(E_Kbd_Buf *kb)
|
||||
{
|
||||
while (kb->string_matches)
|
||||
{
|
||||
if (kb->string_matches->data)
|
||||
eina_stringshare_del(kb->string_matches->data);
|
||||
kb->string_matches = eina_list_remove_list(kb->string_matches, kb->string_matches);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
_e_kbd_buf_actual_string_clear(E_Kbd_Buf *kb)
|
||||
{
|
||||
if (kb->actual_string) eina_stringshare_del(kb->actual_string);
|
||||
kb->actual_string = NULL;
|
||||
}
|
||||
|
||||
static E_Kbd_Buf_Key *
|
||||
_e_kbd_buf_at_coord_get(E_Kbd_Buf *kb, E_Kbd_Buf_Layout *kbl, int x, int y)
|
||||
{
|
||||
Eina_List *l;
|
||||
|
||||
for (l = kbl->keys; l; l = l->next)
|
||||
{
|
||||
E_Kbd_Buf_Key *ky;
|
||||
|
||||
ky = l->data;
|
||||
if (ky->key)
|
||||
{
|
||||
if ((x >= ky->x) && (y >= ky->y) &&
|
||||
(x < (ky->x + ky->w)) && (y < (ky->y + ky->h)))
|
||||
return ky;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static E_Kbd_Buf_Key *
|
||||
_e_kbd_buf_closest_get(E_Kbd_Buf *kb, E_Kbd_Buf_Layout *kbl, int x, int y)
|
||||
{
|
||||
Eina_List *l;
|
||||
E_Kbd_Buf_Key *ky_closest = NULL;
|
||||
int dist_closest = 0x7fffffff;
|
||||
|
||||
for (l = kbl->keys; l; l = l->next)
|
||||
{
|
||||
E_Kbd_Buf_Key *ky;
|
||||
int dist, dx, dy;
|
||||
|
||||
ky = l->data;
|
||||
if (ky->key)
|
||||
{
|
||||
dx = x - (ky->x + (ky->w / 2));
|
||||
dy = y - (ky->y + (ky->h / 2));
|
||||
dist = (dx * dx) + (dy * dy);
|
||||
if (dist < dist_closest)
|
||||
{
|
||||
ky_closest = ky;
|
||||
dist_closest = dist;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ky_closest;
|
||||
}
|
||||
|
||||
static const char *
|
||||
_e_kbd_buf_keystroke_key_string_get(E_Kbd_Buf *kb, E_Kbd_Buf_Keystroke *ks, E_Kbd_Buf_Key *ky)
|
||||
{
|
||||
const char *str = NULL;
|
||||
|
||||
if ((ky) && (ky->key))
|
||||
{
|
||||
if (ks->shift)
|
||||
{
|
||||
if (ky->key_shift) str = ky->key_shift;
|
||||
else str = ky->key;
|
||||
}
|
||||
else if (ks->capslock)
|
||||
{
|
||||
if (ky->key_capslock) str = ky->key_capslock;
|
||||
else str = ky->key;
|
||||
}
|
||||
else str = ky->key;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
static const char *
|
||||
_e_kbd_buf_keystroke_string_get(E_Kbd_Buf *kb, E_Kbd_Buf_Keystroke *ks)
|
||||
{
|
||||
const char *str = NULL;
|
||||
|
||||
if (ks->key) str = ks->key;
|
||||
else
|
||||
{
|
||||
E_Kbd_Buf_Key *ky;
|
||||
|
||||
ky = _e_kbd_buf_at_coord_get(kb, ks->layout, ks->x, ks->y);
|
||||
if (!ky) ky = _e_kbd_buf_closest_get(kb, ks->layout, ks->x, ks->y);
|
||||
str = _e_kbd_buf_keystroke_key_string_get(kb, ks, ky);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
static void
|
||||
_e_kbd_buf_actual_string_update(E_Kbd_Buf *kb)
|
||||
{
|
||||
Eina_List *l;
|
||||
char *actual = NULL;
|
||||
int actual_len = 0;
|
||||
int actual_size = 0;
|
||||
|
||||
_e_kbd_buf_actual_string_clear(kb);
|
||||
for (l = kb->keystrokes; l; l = l->next)
|
||||
{
|
||||
E_Kbd_Buf_Keystroke *ks;
|
||||
const char *str;
|
||||
|
||||
ks = l->data;
|
||||
str = _e_kbd_buf_keystroke_string_get(kb, ks);
|
||||
if (str)
|
||||
{
|
||||
if ((actual_len + strlen(str) + 1) > actual_size)
|
||||
{
|
||||
actual_size += 64;
|
||||
actual = realloc(actual, actual_size);
|
||||
}
|
||||
strcpy(actual + actual_len, str);
|
||||
actual_len += strlen(str);
|
||||
}
|
||||
}
|
||||
if (actual)
|
||||
{
|
||||
kb->actual_string = eina_stringshare_add(actual);
|
||||
if (actual) free(actual);
|
||||
}
|
||||
}
|
||||
|
||||
static const char *
|
||||
_e_kbd_buf_matches_find(Eina_List *matches, const char *s)
|
||||
{
|
||||
Eina_List *l;
|
||||
|
||||
for (l = matches; l; l = l->next)
|
||||
{
|
||||
if (!strcmp(l->data, s)) return s;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
_e_kbd_buf_matches_update(E_Kbd_Buf *kb)
|
||||
{
|
||||
Eina_List *matches = NULL;
|
||||
const char *word;
|
||||
int pri, i;
|
||||
E_Kbd_Dict *dicts[3];
|
||||
|
||||
_e_kbd_buf_string_matches_clear(kb);
|
||||
dicts[0] = kb->dict.personal;
|
||||
dicts[1] = kb->dict.sys;
|
||||
dicts[2] = kb->dict.data;
|
||||
for (i = 0; i < 3; i++)
|
||||
{
|
||||
if (!dicts[i]) continue;
|
||||
e_kbd_dict_matches_lookup(dicts[i]);
|
||||
e_kbd_dict_matches_first(dicts[i]);
|
||||
for (;;)
|
||||
{
|
||||
word = e_kbd_dict_matches_match_get(dicts[i], &pri);
|
||||
if (!word) break;
|
||||
if (!_e_kbd_buf_matches_find(kb->string_matches, word))
|
||||
kb->string_matches = eina_list_append(kb->string_matches,
|
||||
eina_stringshare_add(word));
|
||||
e_kbd_dict_matches_next(dicts[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
_e_kbd_buf_cb_data_dict_reload(void *data)
|
||||
{
|
||||
E_Kbd_Buf *kb;
|
||||
char buf[PATH_MAX];
|
||||
|
||||
kb = data;
|
||||
kb->dict.data_reload_delay = NULL;
|
||||
e_kbd_buf_clear(kb);
|
||||
if (kb->dict.data) e_kbd_dict_free(kb->dict.data);
|
||||
e_user_dir_concat_static(buf, "dicts-dynamic/data.dic");
|
||||
kb->dict.data = e_kbd_dict_new(buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
_e_kbd_buf_cb_data_dict_change(void *data, Ecore_File_Monitor *em, Ecore_File_Event event, const char *path)
|
||||
{
|
||||
E_Kbd_Buf *kb;
|
||||
|
||||
kb = data;
|
||||
if (kb->dict.data_reload_delay) ecore_timer_del(kb->dict.data_reload_delay);
|
||||
kb->dict.data_reload_delay = ecore_timer_add(2.0, _e_kbd_buf_cb_data_dict_reload, kb);
|
||||
}
|
||||
|
||||
EAPI E_Kbd_Buf *
|
||||
e_kbd_buf_new(const char *sysdicts, const char *dict)
|
||||
{
|
||||
E_Kbd_Buf *kb;
|
||||
char buf[PATH_MAX];
|
||||
|
||||
kb = E_NEW(E_Kbd_Buf, 1);
|
||||
if (!kb) return NULL;
|
||||
kb->sysdicts = eina_stringshare_add(sysdicts);
|
||||
|
||||
e_user_dir_concat_static(buf, "dicts");
|
||||
if (!ecore_file_exists(buf)) ecore_file_mkpath(buf);
|
||||
|
||||
e_user_dir_snprintf(buf, sizeof(buf), "dicts/%s", dict);
|
||||
kb->dict.sys = e_kbd_dict_new(buf);
|
||||
if (!kb->dict.sys)
|
||||
{
|
||||
snprintf(buf, sizeof(buf), "%s/dicts/%s", kb->sysdicts, dict);
|
||||
kb->dict.sys = e_kbd_dict_new(buf);
|
||||
}
|
||||
|
||||
e_user_dir_concat_static(buf, "dicts-dynamic");
|
||||
if (!ecore_file_exists(buf)) ecore_file_mkpath(buf);
|
||||
|
||||
e_user_dir_concat_static(buf, "dicts-dynamic/personal.dic");
|
||||
kb->dict.personal = e_kbd_dict_new(buf);
|
||||
if (!kb->dict.personal)
|
||||
{
|
||||
FILE *f;
|
||||
|
||||
f = fopen(buf, "w");
|
||||
if (f)
|
||||
{
|
||||
fprintf(f, "\n");
|
||||
fclose(f);
|
||||
}
|
||||
kb->dict.personal = e_kbd_dict_new(buf);
|
||||
}
|
||||
e_user_dir_concat_static(buf, "dicts-dynamic/data.dic");
|
||||
kb->dict.data = e_kbd_dict_new(buf);
|
||||
kb->dict.data_monitor =
|
||||
ecore_file_monitor_add(buf, _e_kbd_buf_cb_data_dict_change, kb);
|
||||
return kb;
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_buf_free(E_Kbd_Buf *kb)
|
||||
{
|
||||
e_kbd_buf_clear(kb);
|
||||
e_kbd_buf_layout_clear(kb);
|
||||
e_kbd_buf_lookup_cancel(kb);
|
||||
eina_stringshare_del(kb->sysdicts);
|
||||
if (kb->dict.sys) e_kbd_dict_free(kb->dict.sys);
|
||||
if (kb->dict.personal) e_kbd_dict_free(kb->dict.personal);
|
||||
if (kb->dict.data) e_kbd_dict_free(kb->dict.data);
|
||||
if (kb->dict.data_monitor) ecore_file_monitor_del(kb->dict.data_monitor);
|
||||
if (kb->dict.data_reload_delay) ecore_timer_del(kb->dict.data_reload_delay);
|
||||
free(kb);
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_buf_dict_set(E_Kbd_Buf *kb, const char *dict)
|
||||
{
|
||||
char buf[PATH_MAX];
|
||||
|
||||
e_kbd_buf_clear(kb);
|
||||
|
||||
if (kb->dict.sys) e_kbd_dict_free(kb->dict.sys);
|
||||
|
||||
e_user_dir_concat_static(buf, "dicts");
|
||||
if (!ecore_file_exists(buf)) ecore_file_mkpath(buf);
|
||||
|
||||
e_user_dir_snprintf(buf, sizeof(buf), "dicts/%s", dict);
|
||||
kb->dict.sys = e_kbd_dict_new(buf);
|
||||
if (!kb->dict.sys)
|
||||
{
|
||||
snprintf(buf, sizeof(buf), "%s/dicts/%s", kb->sysdicts, dict);
|
||||
kb->dict.sys = e_kbd_dict_new(buf);
|
||||
}
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_buf_clear(E_Kbd_Buf *kb)
|
||||
{
|
||||
e_kbd_buf_lookup_cancel(kb);
|
||||
while (kb->keystrokes)
|
||||
{
|
||||
E_Kbd_Buf_Keystroke *ks;
|
||||
|
||||
ks = kb->keystrokes->data;
|
||||
if (ks->key) eina_stringshare_del(ks->key);
|
||||
_e_kbd_buf_layout_unref(ks->layout);
|
||||
free(ks);
|
||||
kb->keystrokes = eina_list_remove_list(kb->keystrokes, kb->keystrokes);
|
||||
}
|
||||
_e_kbd_buf_string_matches_clear(kb);
|
||||
if (kb->dict.sys) e_kbd_dict_word_letter_clear(kb->dict.sys);
|
||||
if (kb->dict.personal) e_kbd_dict_word_letter_clear(kb->dict.personal);
|
||||
if (kb->dict.data) e_kbd_dict_word_letter_clear(kb->dict.data);
|
||||
_e_kbd_buf_actual_string_clear(kb);
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_buf_layout_clear(E_Kbd_Buf *kb)
|
||||
{
|
||||
if (kb->layout)
|
||||
{
|
||||
_e_kbd_buf_layout_unref(kb->layout);
|
||||
kb->layout = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_buf_layout_size_set(E_Kbd_Buf *kb, int w, int h)
|
||||
{
|
||||
if (!kb->layout) kb->layout = _e_kbd_buf_new();
|
||||
if (!kb->layout) return;
|
||||
kb->layout->w = w;
|
||||
kb->layout->h = h;
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_buf_layout_fuzz_set(E_Kbd_Buf *kb, int fuzz)
|
||||
{
|
||||
if (!kb->layout) kb->layout = _e_kbd_buf_new();
|
||||
if (!kb->layout) return;
|
||||
kb->layout->fuzz = fuzz;
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_buf_layout_key_add(E_Kbd_Buf *kb, const char *key, const char *key_shift, const char *key_capslock, int x, int y, int w, int h)
|
||||
{
|
||||
E_Kbd_Buf_Key *ky;
|
||||
|
||||
if (!key) return;
|
||||
if (!kb->layout) kb->layout = _e_kbd_buf_new();
|
||||
if (!kb->layout) return;
|
||||
ky = E_NEW(E_Kbd_Buf_Key, 1);
|
||||
if (!ky) return;
|
||||
if (key) ky->key = eina_stringshare_add(key);
|
||||
if (key_shift) ky->key_shift = eina_stringshare_add(key_shift);
|
||||
if (key_capslock) ky->key_capslock = eina_stringshare_add(key_capslock);
|
||||
ky->x = x;
|
||||
ky->y = y;
|
||||
ky->w = w;
|
||||
ky->h = h;
|
||||
kb->layout->keys = eina_list_append(kb->layout->keys, ky);
|
||||
}
|
||||
|
||||
static void
|
||||
_e_kbd_buf_keystroke_add(E_Kbd_Buf *kb, E_Kbd_Buf_Keystroke *ks)
|
||||
{
|
||||
const char *str;
|
||||
|
||||
str = _e_kbd_buf_keystroke_string_get(kb, ks);
|
||||
if (str)
|
||||
{
|
||||
if (kb->dict.sys) e_kbd_dict_word_letter_add(kb->dict.sys, str, 0);
|
||||
if (kb->dict.personal) e_kbd_dict_word_letter_add(kb->dict.personal, str, 0);
|
||||
if (kb->dict.data) e_kbd_dict_word_letter_add(kb->dict.data, str, 0);
|
||||
}
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_buf_pressed_key_add(E_Kbd_Buf *kb, const char *key, int shift, int capslock)
|
||||
{
|
||||
E_Kbd_Buf_Keystroke *ks;
|
||||
|
||||
e_kbd_buf_lookup_cancel(kb);
|
||||
if (!key) return;
|
||||
if (!kb->layout) kb->layout = _e_kbd_buf_new();
|
||||
if (!kb->layout) return;
|
||||
ks = E_NEW(E_Kbd_Buf_Keystroke, 1);
|
||||
if (!ks) return;
|
||||
ks->key = eina_stringshare_add(key);
|
||||
if (shift) ks->shift = 1;
|
||||
if (capslock) ks->capslock = 1;
|
||||
ks->layout = kb->layout;
|
||||
_e_kbd_buf_layout_ref(ks->layout);
|
||||
kb->keystrokes = eina_list_append(kb->keystrokes, ks);
|
||||
|
||||
if (kb->dict.sys) e_kbd_dict_word_letter_advance(kb->dict.sys);
|
||||
if (kb->dict.personal) e_kbd_dict_word_letter_advance(kb->dict.personal);
|
||||
if (kb->dict.data) e_kbd_dict_word_letter_advance(kb->dict.data);
|
||||
_e_kbd_buf_keystroke_add(kb, ks);
|
||||
|
||||
_e_kbd_buf_actual_string_update(kb);
|
||||
_e_kbd_buf_matches_update(kb);
|
||||
}
|
||||
|
||||
static void
|
||||
_e_kbd_buf_keystroke_point_add(E_Kbd_Buf *kb, E_Kbd_Buf_Keystroke *ks)
|
||||
{
|
||||
Eina_List *l;
|
||||
|
||||
for (l = ks->layout->keys; l; l = l->next)
|
||||
{
|
||||
E_Kbd_Buf_Key *ky;
|
||||
const char *str;
|
||||
int px, py, dx, dy, d, fuzz;
|
||||
|
||||
ky = l->data;
|
||||
px = ky->x + (ky->w / 2);
|
||||
py = ky->y + (ky->h / 2);
|
||||
dx = ks->x - px;
|
||||
dy = ks->y - py;
|
||||
d = sqrt((dx * dx) + (dy * dy));
|
||||
if (d <= ks->layout->fuzz)
|
||||
{
|
||||
str = _e_kbd_buf_keystroke_key_string_get(kb, ks, ky);
|
||||
if (str)
|
||||
{
|
||||
if (kb->dict.sys) e_kbd_dict_word_letter_add(kb->dict.sys, str, d);
|
||||
if (kb->dict.personal) e_kbd_dict_word_letter_add(kb->dict.personal, str, d);
|
||||
if (kb->dict.data) e_kbd_dict_word_letter_add(kb->dict.data, str, d);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_buf_pressed_point_add(E_Kbd_Buf *kb, int x, int y, int shift, int capslock)
|
||||
{
|
||||
E_Kbd_Buf_Keystroke *ks;
|
||||
|
||||
e_kbd_buf_lookup_cancel(kb);
|
||||
if (!kb->layout) kb->layout = _e_kbd_buf_new();
|
||||
if (!kb->layout) return;
|
||||
ks = E_NEW(E_Kbd_Buf_Keystroke, 1);
|
||||
if (!ks) return;
|
||||
ks->x = x;
|
||||
ks->y = y;
|
||||
if (shift) ks->shift = 1;
|
||||
if (capslock) ks->capslock = 1;
|
||||
ks->layout = kb->layout;
|
||||
_e_kbd_buf_layout_ref(ks->layout);
|
||||
kb->keystrokes = eina_list_append(kb->keystrokes, ks);
|
||||
|
||||
if (kb->dict.sys) e_kbd_dict_word_letter_advance(kb->dict.sys);
|
||||
if (kb->dict.personal) e_kbd_dict_word_letter_advance(kb->dict.personal);
|
||||
if (kb->dict.data) e_kbd_dict_word_letter_advance(kb->dict.data);
|
||||
|
||||
_e_kbd_buf_keystroke_point_add(kb, ks);
|
||||
|
||||
_e_kbd_buf_actual_string_update(kb);
|
||||
_e_kbd_buf_matches_update(kb);
|
||||
}
|
||||
|
||||
EAPI const char *
|
||||
e_kbd_buf_actual_string_get(E_Kbd_Buf *kb)
|
||||
{
|
||||
return kb->actual_string;
|
||||
}
|
||||
|
||||
EAPI const Eina_List *
|
||||
e_kbd_buf_string_matches_get(E_Kbd_Buf *kb)
|
||||
{
|
||||
return kb->string_matches;
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_buf_backspace(E_Kbd_Buf *kb)
|
||||
{
|
||||
Eina_List *l;
|
||||
|
||||
l = eina_list_last(kb->keystrokes);
|
||||
if (l)
|
||||
{
|
||||
E_Kbd_Buf_Keystroke *ks;
|
||||
|
||||
ks = l->data;
|
||||
if (ks->key) eina_stringshare_del(ks->key);
|
||||
_e_kbd_buf_layout_unref(ks->layout);
|
||||
free(ks);
|
||||
kb->keystrokes = eina_list_remove_list(kb->keystrokes, l);
|
||||
if (kb->dict.sys) e_kbd_dict_word_letter_delete(kb->dict.sys);
|
||||
if (kb->dict.personal) e_kbd_dict_word_letter_delete(kb->dict.personal);
|
||||
if (kb->dict.data) e_kbd_dict_word_letter_delete(kb->dict.data);
|
||||
_e_kbd_buf_actual_string_update(kb);
|
||||
_e_kbd_buf_matches_update(kb);
|
||||
}
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_buf_word_use(E_Kbd_Buf *kb, const char *word)
|
||||
{
|
||||
if (kb->dict.personal)
|
||||
e_kbd_dict_word_usage_adjust(kb->dict.personal, word, 1);
|
||||
}
|
||||
|
||||
// FIXME: just faking delayed lookup with timer
|
||||
static int
|
||||
_e_kbd_buf_cb_faket(void *data)
|
||||
{
|
||||
E_Kbd_Buf *kb;
|
||||
|
||||
kb = data;
|
||||
kb->lookup.faket = NULL;
|
||||
kb->lookup.func(kb->lookup.data);
|
||||
kb->lookup.func = NULL;
|
||||
kb->lookup.data = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_buf_lookup(E_Kbd_Buf *kb, void (*func) (void *data), const void *data)
|
||||
{
|
||||
e_kbd_buf_lookup_cancel(kb);
|
||||
|
||||
kb->lookup.func = func;
|
||||
kb->lookup.data = data;
|
||||
|
||||
// FIXME: just faking delayed lookup with timer
|
||||
kb->lookup.faket = ecore_timer_add(0.1, _e_kbd_buf_cb_faket, kb);
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_buf_lookup_cancel(E_Kbd_Buf *kb)
|
||||
{
|
||||
// FIXME: just faking delayed lookup with timer
|
||||
if (!kb->lookup.faket) return;
|
||||
ecore_timer_del(kb->lookup.faket);
|
||||
kb->lookup.faket = NULL;
|
||||
|
||||
kb->lookup.func = NULL;
|
||||
kb->lookup.data = NULL;
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
#ifndef E_KBD_BUF_H
|
||||
#define E_KBD_BUF_H
|
||||
|
||||
#include "e_kbd_dict.h"
|
||||
|
||||
typedef struct _E_Kbd_Buf E_Kbd_Buf;
|
||||
typedef struct _E_Kbd_Buf_Key E_Kbd_Buf_Key;
|
||||
typedef struct _E_Kbd_Buf_Keystroke E_Kbd_Buf_Keystroke;
|
||||
typedef struct _E_Kbd_Buf_Layout E_Kbd_Buf_Layout;
|
||||
|
||||
struct _E_Kbd_Buf
|
||||
{
|
||||
const char *sysdicts;
|
||||
Eina_List *keystrokes;
|
||||
Eina_List *string_matches;
|
||||
const char *actual_string;
|
||||
E_Kbd_Buf_Layout *layout;
|
||||
struct {
|
||||
void (*func) (void *data);
|
||||
const void *data;
|
||||
// FIXME: just faking delayed lookup with timer
|
||||
Ecore_Timer *faket;
|
||||
} lookup;
|
||||
struct {
|
||||
E_Kbd_Dict *sys;
|
||||
E_Kbd_Dict *personal;
|
||||
E_Kbd_Dict *data;
|
||||
Ecore_File_Monitor *data_monitor;
|
||||
Ecore_Timer *data_reload_delay;
|
||||
} dict;
|
||||
};
|
||||
|
||||
struct _E_Kbd_Buf_Key
|
||||
{
|
||||
int x, y, w, h;
|
||||
const char *key, *key_shift, *key_capslock;
|
||||
};
|
||||
|
||||
struct _E_Kbd_Buf_Keystroke
|
||||
{
|
||||
const char *key;
|
||||
int x, y;
|
||||
E_Kbd_Buf_Layout *layout;
|
||||
unsigned char shift : 1;
|
||||
unsigned char capslock : 1;
|
||||
};
|
||||
|
||||
struct _E_Kbd_Buf_Layout {
|
||||
int ref;
|
||||
int w, h;
|
||||
int fuzz;
|
||||
Eina_List *keys;
|
||||
};
|
||||
|
||||
EAPI E_Kbd_Buf *e_kbd_buf_new(const char *sysdicts, const char *dicts);
|
||||
EAPI void e_kbd_buf_free(E_Kbd_Buf *kb);
|
||||
EAPI void e_kbd_buf_dict_set(E_Kbd_Buf *kb, const char *dict);
|
||||
EAPI void e_kbd_buf_clear(E_Kbd_Buf *kb);
|
||||
EAPI void e_kbd_buf_layout_clear(E_Kbd_Buf *kb);
|
||||
EAPI void e_kbd_buf_layout_size_set(E_Kbd_Buf *kb, int w, int h);
|
||||
EAPI void e_kbd_buf_layout_fuzz_set(E_Kbd_Buf *kb, int fuzz);
|
||||
EAPI void e_kbd_buf_layout_key_add(E_Kbd_Buf *kb, const char *key, const char *key_shift, const char *key_capslock, int x, int y, int w, int h);
|
||||
EAPI void e_kbd_buf_pressed_key_add(E_Kbd_Buf *kb, const char *key, int shift, int capslock);
|
||||
EAPI void e_kbd_buf_pressed_point_add(E_Kbd_Buf *kb, int x, int y, int shift, int capslock);
|
||||
EAPI const char *e_kbd_buf_actual_string_get(E_Kbd_Buf *kb);
|
||||
EAPI const Eina_List *e_kbd_buf_string_matches_get(E_Kbd_Buf *kb);
|
||||
EAPI void e_kbd_buf_backspace(E_Kbd_Buf *kb);
|
||||
EAPI void e_kbd_buf_lookup(E_Kbd_Buf *kb, void (*func) (void *data), const void *data);
|
||||
EAPI void e_kbd_buf_lookup_cancel(E_Kbd_Buf *kb);
|
||||
EAPI void e_kbd_buf_word_use(E_Kbd_Buf *kb, const char *word);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,878 @@
|
|||
#include "e.h"
|
||||
#include "e_kbd_dict.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
|
||||
#define MAXLATIN 0x100
|
||||
static unsigned char _e_kbd_normalise_base[MAXLATIN];
|
||||
static unsigned char _e_kbd_normalise_ready = 0;
|
||||
|
||||
static void
|
||||
_e_kbd_normalise_init(void)
|
||||
{
|
||||
int i;
|
||||
const char *table[][2] =
|
||||
{
|
||||
{"À", "a"},
|
||||
{"Á", "a"},
|
||||
{"Â", "a"},
|
||||
{"Ã", "a"},
|
||||
{"Ä", "a"},
|
||||
{"Å", "a"},
|
||||
{"Æ", "a"},
|
||||
{"Ç", "c"},
|
||||
{"È", "e"},
|
||||
{"É", "e"},
|
||||
{"Ê", "e"},
|
||||
{"Ë", "e"},
|
||||
{"Ì", "i"},
|
||||
{"Í", "i"},
|
||||
{"Î", "i"},
|
||||
{"Ï", "i"},
|
||||
{"Ð", "d"},
|
||||
{"Ñ", "n"},
|
||||
{"Ò", "o"},
|
||||
{"Ó", "o"},
|
||||
{"Ô", "o"},
|
||||
{"Õ", "o"},
|
||||
{"Ö", "o"},
|
||||
{"×", "x"},
|
||||
{"Ø", "o"},
|
||||
{"Ù", "u"},
|
||||
{"Ú", "u"},
|
||||
{"Û", "u"},
|
||||
{"Ü", "u"},
|
||||
{"Ý", "y"},
|
||||
{"Þ", "p"},
|
||||
{"ß", "s"},
|
||||
{"à", "a"},
|
||||
{"á", "a"},
|
||||
{"â", "a"},
|
||||
{"ã", "a"},
|
||||
{"ä", "a"},
|
||||
{"å", "a"},
|
||||
{"æ", "a"},
|
||||
{"ç", "c"},
|
||||
{"è", "e"},
|
||||
{"é", "e"},
|
||||
{"ê", "e"},
|
||||
{"ë", "e"},
|
||||
{"ì", "i"},
|
||||
{"í", "i"},
|
||||
{"î", "i"},
|
||||
{"ï", "i"},
|
||||
{"ð", "o"},
|
||||
{"ñ", "n"},
|
||||
{"ò", "o"},
|
||||
{"ó", "o"},
|
||||
{"ô", "o"},
|
||||
{"õ", "o"},
|
||||
{"ö", "o"},
|
||||
{"ø", "o"},
|
||||
{"ù", "u"},
|
||||
{"ú", "u"},
|
||||
{"û", "u"},
|
||||
{"ü", "u"},
|
||||
{"ý", "y"},
|
||||
{"þ", "p"},
|
||||
{"ÿ", "y"}
|
||||
}; // 63 items
|
||||
|
||||
if (_e_kbd_normalise_ready) return;
|
||||
_e_kbd_normalise_ready = 1;
|
||||
for (i = 0; i < 128; i++)
|
||||
_e_kbd_normalise_base[i] = tolower(i);
|
||||
for (;i < MAXLATIN; i++)
|
||||
{
|
||||
int glyph;
|
||||
int j;
|
||||
|
||||
for (j = 0; j < 63; j++)
|
||||
{
|
||||
evas_string_char_next_get(table[j][0], 0, &glyph);
|
||||
if (glyph == i)
|
||||
{
|
||||
_e_kbd_normalise_base[i] = *table[j][1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
_e_kbd_dict_letter_normalise(int glyph)
|
||||
{
|
||||
// FIXME: ö -> o, ä -> a, Ó -> o etc. - ie normalise to latin-1
|
||||
if (glyph < MAXLATIN) return _e_kbd_normalise_base[glyph];
|
||||
return tolower(glyph) & 0x7f;
|
||||
}
|
||||
|
||||
static int
|
||||
_e_kbd_dict_normalized_strncmp(const char *a, const char *b, int len)
|
||||
{
|
||||
// FIXME: normalise 2 strings and then compare
|
||||
if (len < 0) return strcasecmp(a, b);
|
||||
return strncasecmp(a, b, len);
|
||||
}
|
||||
|
||||
static int
|
||||
_e_kbd_dict_normalized_strcmp(const char *a, const char *b)
|
||||
{
|
||||
return _e_kbd_dict_normalized_strncmp(a, b, -1);
|
||||
}
|
||||
|
||||
static void
|
||||
_e_kbd_dict_normalized_strcpy(char *dst, const char *src)
|
||||
{
|
||||
const char *p;
|
||||
char *d;
|
||||
|
||||
for (p = src, d = dst; *p; p++, d++)
|
||||
{
|
||||
*d = _e_kbd_dict_letter_normalise(*p);
|
||||
}
|
||||
*d = 0;
|
||||
}
|
||||
|
||||
static int
|
||||
_e_kbd_dict_matches_loolup_cb_sort(const void *d1, const void *d2)
|
||||
{
|
||||
const E_Kbd_Dict_Word *kw1, *kw2;
|
||||
|
||||
kw1 = d1;
|
||||
kw2 = d2;
|
||||
if (kw1->usage < kw2->usage) return 1;
|
||||
else if (kw1->usage > kw2->usage) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
_e_kbd_dict_writes_cb_sort(const void *d1, const void *d2)
|
||||
{
|
||||
const E_Kbd_Dict_Word *kw1, *kw2;
|
||||
|
||||
kw1 = d1;
|
||||
kw2 = d2;
|
||||
return _e_kbd_dict_normalized_strcmp(kw1->word, kw2->word);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const char *
|
||||
_e_kbd_dict_line_next(E_Kbd_Dict *kd, const char *p)
|
||||
{
|
||||
const char *e, *pp;
|
||||
|
||||
e = kd->file.dict + kd->file.size;
|
||||
for (pp = p; pp < e; pp++)
|
||||
{
|
||||
if (*pp == '\n')
|
||||
return pp + 1;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static char *
|
||||
_e_kbd_dict_line_parse(E_Kbd_Dict *kd, const char *p, int *usage)
|
||||
{
|
||||
const char *ps;
|
||||
char *wd = NULL;
|
||||
|
||||
for (ps = p; !isspace(*ps); ps++);
|
||||
wd = malloc(ps - p + 1);
|
||||
if (!wd) return NULL;
|
||||
strncpy(wd, p, ps - p);
|
||||
wd[ps - p] = 0;
|
||||
if (*ps == '\n') *usage = 0;
|
||||
else
|
||||
{
|
||||
ps++;
|
||||
*usage = atoi(ps);
|
||||
}
|
||||
return wd;
|
||||
}
|
||||
|
||||
static void
|
||||
_e_kbd_dict_lookup_build_line(E_Kbd_Dict *kd, const char *p, const char *eol,
|
||||
int *glyphs)
|
||||
{
|
||||
char *s;
|
||||
int p2;
|
||||
|
||||
s = alloca(eol - p + 1);
|
||||
strncpy(s, p, eol - p);
|
||||
s[eol - p] = 0;
|
||||
p2 = evas_string_char_next_get(s, 0, &(glyphs[0]));
|
||||
if ((p2 > 0) && (glyphs[0] > 0))
|
||||
p2 = evas_string_char_next_get(s, p2, &(glyphs[1]));
|
||||
}
|
||||
|
||||
static void
|
||||
_e_kbd_dict_lookup_build(E_Kbd_Dict *kd)
|
||||
{
|
||||
const char *p, *e, *eol;
|
||||
int glyphs[2], pglyphs[2];
|
||||
|
||||
p = kd->file.dict;
|
||||
e = p + kd->file.size;
|
||||
pglyphs[0] = pglyphs[1] = 0;
|
||||
while (p < e)
|
||||
{
|
||||
eol = strchr(p, '\n');
|
||||
if (!eol) break;
|
||||
if (eol > p)
|
||||
{
|
||||
glyphs[0] = glyphs[1] = 0;
|
||||
_e_kbd_dict_lookup_build_line(kd, p, eol, glyphs);
|
||||
if ((glyphs[1] != pglyphs[1]) ||
|
||||
(glyphs[0] != pglyphs[0]))
|
||||
{
|
||||
int v1, v2;
|
||||
|
||||
if (isspace(glyphs[0]))
|
||||
{
|
||||
glyphs[0] = 0;
|
||||
glyphs[1] = 0;
|
||||
}
|
||||
else if (isspace(glyphs[1]))
|
||||
glyphs[1] = 0;
|
||||
if (glyphs[0] == 0)
|
||||
{
|
||||
pglyphs[0] = pglyphs[1] = 0;
|
||||
p = eol + 1;
|
||||
continue;
|
||||
}
|
||||
v1 = _e_kbd_dict_letter_normalise(glyphs[0]);
|
||||
v2 = _e_kbd_dict_letter_normalise(glyphs[1]);
|
||||
if (!kd->lookup.tuples[v1][v2])
|
||||
kd->lookup.tuples[v1][v2] = p;
|
||||
pglyphs[0] = v1;
|
||||
pglyphs[1] = v2;
|
||||
}
|
||||
}
|
||||
p = eol + 1;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
_e_kbd_dict_open(E_Kbd_Dict *kd)
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
kd->file.fd = open(kd->file.file, O_RDONLY);
|
||||
if (kd->file.fd < 0)
|
||||
return 0;
|
||||
if (fstat(kd->file.fd, &st) < 0)
|
||||
{
|
||||
close(kd->file.fd);
|
||||
return 0;
|
||||
}
|
||||
kd->file.size = st.st_size;
|
||||
kd->file.dict = mmap(NULL, kd->file.size, PROT_READ, MAP_SHARED,
|
||||
kd->file.fd, 0);
|
||||
if ((kd->file.dict== MAP_FAILED) || (kd->file.dict == NULL))
|
||||
{
|
||||
close(kd->file.fd);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
_e_kbd_dict_close(E_Kbd_Dict *kd)
|
||||
{
|
||||
if (kd->file.fd < 0) return;
|
||||
memset(kd->lookup.tuples, 0, sizeof(kd->lookup.tuples));
|
||||
munmap((void *)kd->file.dict, kd->file.size);
|
||||
close(kd->file.fd);
|
||||
kd->file.fd = -1;
|
||||
kd->file.dict = NULL;
|
||||
kd->file.size = 0;
|
||||
}
|
||||
|
||||
EAPI E_Kbd_Dict *
|
||||
e_kbd_dict_new(const char *file)
|
||||
{
|
||||
// alloc and load new dict - build quick-lookup table. words MUST be sorted
|
||||
E_Kbd_Dict *kd;
|
||||
|
||||
_e_kbd_normalise_init();
|
||||
kd = E_NEW(E_Kbd_Dict, 1);
|
||||
if (!kd) return NULL;
|
||||
kd->file.file = eina_stringshare_add(file);
|
||||
if (!kd->file.file)
|
||||
{
|
||||
free(kd);
|
||||
return NULL;
|
||||
}
|
||||
kd->file.fd = -1;
|
||||
if (!_e_kbd_dict_open(kd))
|
||||
{
|
||||
eina_stringshare_del(kd->file.file);
|
||||
free(kd);
|
||||
return NULL;
|
||||
}
|
||||
_e_kbd_dict_lookup_build(kd);
|
||||
return kd;
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_dict_free(E_Kbd_Dict *kd)
|
||||
{
|
||||
// free dict and anything in it
|
||||
e_kbd_dict_word_letter_clear(kd);
|
||||
e_kbd_dict_save(kd);
|
||||
_e_kbd_dict_close(kd);
|
||||
free(kd);
|
||||
}
|
||||
|
||||
static E_Kbd_Dict_Word *
|
||||
_e_kbd_dict_changed_write_find(E_Kbd_Dict *kd, const char *word)
|
||||
{
|
||||
Eina_List *l;
|
||||
|
||||
for (l = kd->changed.writes; l; l = l->next)
|
||||
{
|
||||
E_Kbd_Dict_Word *kw;
|
||||
|
||||
kw = l->data;
|
||||
if (!strcmp(kw->word, word)) return kw;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_dict_save(E_Kbd_Dict *kd)
|
||||
{
|
||||
FILE *f;
|
||||
|
||||
// save any changes (new words added, usage adjustments).
|
||||
// all words MUST be sorted
|
||||
if (!kd->changed.writes) return;
|
||||
if (kd->changed.flush_timer)
|
||||
{
|
||||
ecore_timer_del(kd->changed.flush_timer);
|
||||
kd->changed.flush_timer = NULL;
|
||||
}
|
||||
ecore_file_unlink(kd->file.file);
|
||||
f = fopen(kd->file.file, "w");
|
||||
kd->changed.writes = eina_list_sort(kd->changed.writes,
|
||||
eina_list_count(kd->changed.writes),
|
||||
_e_kbd_dict_writes_cb_sort);
|
||||
if (f)
|
||||
{
|
||||
const char *p, *pn;
|
||||
|
||||
p = kd->file.dict;
|
||||
while (p)
|
||||
{
|
||||
char *wd;
|
||||
int usage = 0;
|
||||
|
||||
pn = _e_kbd_dict_line_next(kd, p);
|
||||
if (!pn) return;
|
||||
wd = _e_kbd_dict_line_parse(kd, p, &usage);
|
||||
if ((wd) && (strlen(wd) > 0))
|
||||
{
|
||||
if (kd->changed.writes)
|
||||
{
|
||||
int writeline;
|
||||
|
||||
writeline = 0;
|
||||
while (kd->changed.writes)
|
||||
{
|
||||
E_Kbd_Dict_Word *kw;
|
||||
int cmp;
|
||||
|
||||
kw = kd->changed.writes->data;
|
||||
cmp = _e_kbd_dict_normalized_strcmp(kw->word, wd);
|
||||
if (cmp < 0)
|
||||
{
|
||||
fprintf(f, "%s %i\n", kw->word, kw->usage);
|
||||
writeline = 1;
|
||||
eina_stringshare_del(kw->word);
|
||||
free(kw);
|
||||
kd->changed.writes = eina_list_remove_list(kd->changed.writes, kd->changed.writes);
|
||||
writeline = 1;
|
||||
}
|
||||
else if (cmp == 0)
|
||||
{
|
||||
fprintf(f, "%s %i\n", wd, kw->usage);
|
||||
if (!strcmp(kw->word, wd))
|
||||
writeline = 0;
|
||||
else
|
||||
writeline = 1;
|
||||
eina_stringshare_del(kw->word);
|
||||
free(kw);
|
||||
kd->changed.writes = eina_list_remove_list(kd->changed.writes, kd->changed.writes);
|
||||
break;
|
||||
}
|
||||
else if (cmp > 0)
|
||||
{
|
||||
writeline = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (writeline)
|
||||
fprintf(f, "%s %i\n", wd, usage);
|
||||
}
|
||||
else
|
||||
fprintf(f, "%s %i\n", wd, usage);
|
||||
}
|
||||
if (wd) free(wd);
|
||||
p = pn;
|
||||
if (p >= (kd->file.dict + kd->file.size)) break;
|
||||
}
|
||||
while (kd->changed.writes)
|
||||
{
|
||||
E_Kbd_Dict_Word *kw;
|
||||
|
||||
kw = kd->changed.writes->data;
|
||||
fprintf(f, "%s %i\n", kw->word, kw->usage);
|
||||
eina_stringshare_del(kw->word);
|
||||
free(kw);
|
||||
kd->changed.writes = eina_list_remove_list(kd->changed.writes, kd->changed.writes);
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
_e_kbd_dict_close(kd);
|
||||
if (_e_kbd_dict_open(kd))
|
||||
_e_kbd_dict_lookup_build(kd);
|
||||
}
|
||||
|
||||
static int
|
||||
_e_kbd_dict_cb_save_flush(void *data)
|
||||
{
|
||||
E_Kbd_Dict *kd;
|
||||
|
||||
kd = data;
|
||||
if ((kd->matches.list) ||
|
||||
(kd->word.letters) ||
|
||||
(kd->matches.deadends) ||
|
||||
(kd->matches.leads))
|
||||
return 1;
|
||||
kd->changed.flush_timer = NULL;
|
||||
e_kbd_dict_save(kd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
_e_kbd_dict_changed_write_add(E_Kbd_Dict *kd, const char *word, int usage)
|
||||
{
|
||||
E_Kbd_Dict_Word *kw;
|
||||
|
||||
kw = E_NEW(E_Kbd_Dict_Word, 1);
|
||||
kw->word = eina_stringshare_add(word);
|
||||
kw->usage = usage;
|
||||
kd->changed.writes = eina_list_prepend(kd->changed.writes, kw);
|
||||
if (eina_list_count(kd->changed.writes) > 64)
|
||||
{
|
||||
e_kbd_dict_save(kd);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (kd->changed.flush_timer)
|
||||
ecore_timer_del(kd->changed.flush_timer);
|
||||
kd->changed.flush_timer = ecore_timer_add(5.0, _e_kbd_dict_cb_save_flush, kd);
|
||||
}
|
||||
}
|
||||
|
||||
static const char *
|
||||
_e_kbd_dict_find_pointer(E_Kbd_Dict *kd, const char *p, int baselen, const char *word)
|
||||
{
|
||||
const char *pn;
|
||||
int len;
|
||||
|
||||
if (!p) return NULL;
|
||||
len = strlen(word);
|
||||
while (p)
|
||||
{
|
||||
pn = _e_kbd_dict_line_next(kd, p);
|
||||
if (!pn) return NULL;
|
||||
if ((pn - p) > len)
|
||||
{
|
||||
if (!_e_kbd_dict_normalized_strncmp(p, word, len))
|
||||
return p;
|
||||
}
|
||||
if (_e_kbd_dict_normalized_strncmp(p, word, baselen))
|
||||
return NULL;
|
||||
p = pn;
|
||||
if (p >= (kd->file.dict + kd->file.size)) break;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char *
|
||||
_e_kbd_dict_find(E_Kbd_Dict *kd, const char *word)
|
||||
{
|
||||
const char *p;
|
||||
char *tword;
|
||||
int glyphs[2], p2, v1, v2, i;
|
||||
|
||||
/* work backwards in leads. i.e.:
|
||||
* going
|
||||
* goin
|
||||
* goi
|
||||
* go
|
||||
* g
|
||||
*/
|
||||
tword = alloca(strlen(word) + 1);
|
||||
_e_kbd_dict_normalized_strcpy(tword, word);
|
||||
p = eina_hash_find(kd->matches.leads, tword);
|
||||
if (p) return p;
|
||||
p2 = strlen(tword);
|
||||
while (tword[0])
|
||||
{
|
||||
p2 = evas_string_char_prev_get(tword, p2, &i);
|
||||
if (p2 < 0) break;
|
||||
tword[p2] = 0;
|
||||
p = eina_hash_find(kd->matches.leads, tword);
|
||||
if (p)
|
||||
return _e_kbd_dict_find_pointer(kd, p, p2, word);
|
||||
}
|
||||
/* looking at leads going back letters didn't work */
|
||||
p = kd->file.dict;
|
||||
if ((p[0] == '\n') && (kd->file.size <= 1)) return NULL;
|
||||
glyphs[0] = glyphs[1] = 0;
|
||||
p2 = evas_string_char_next_get(word, 0, &(glyphs[0]));
|
||||
if ((p2 > 0) && (glyphs[0] > 0))
|
||||
p2 = evas_string_char_next_get(word, p2, &(glyphs[1]));
|
||||
v1 = _e_kbd_dict_letter_normalise(glyphs[0]);
|
||||
if (glyphs[1] != 0)
|
||||
{
|
||||
v2 = _e_kbd_dict_letter_normalise(glyphs[1]);
|
||||
p = kd->lookup.tuples[v1][v2];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (i = 0; i < 128; i++)
|
||||
{
|
||||
p = kd->lookup.tuples[v1][i];
|
||||
if (p) break;
|
||||
}
|
||||
}
|
||||
return _e_kbd_dict_find_pointer(kd, p, p2, word);
|
||||
}
|
||||
|
||||
static const char *
|
||||
_e_kbd_dict_find_full(E_Kbd_Dict *kd, const char *word)
|
||||
{
|
||||
const char *p;
|
||||
int len;
|
||||
|
||||
p = _e_kbd_dict_find(kd, word);
|
||||
if (!p) return NULL;
|
||||
len = strlen(word);
|
||||
if (isspace(p[len])) return p;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_dict_word_usage_adjust(E_Kbd_Dict *kd, const char *word, int adjust)
|
||||
{
|
||||
// add "adjust" to word usage count
|
||||
E_Kbd_Dict_Word *kw;
|
||||
|
||||
kw = _e_kbd_dict_changed_write_find(kd, word);
|
||||
if (kw)
|
||||
{
|
||||
kw->usage += adjust;
|
||||
if (kd->changed.flush_timer)
|
||||
ecore_timer_del(kd->changed.flush_timer);
|
||||
kd->changed.flush_timer = ecore_timer_add(5.0, _e_kbd_dict_cb_save_flush, kd);
|
||||
}
|
||||
else
|
||||
{
|
||||
const char *line;
|
||||
int usage = 0;
|
||||
|
||||
line = _e_kbd_dict_find_full(kd, word);
|
||||
if (line)
|
||||
{
|
||||
char *wd;
|
||||
|
||||
// FIXME: we need to find an EXACT line match - case and all
|
||||
wd = _e_kbd_dict_line_parse(kd, line, &usage);
|
||||
if (wd) free(wd);
|
||||
}
|
||||
usage += adjust;
|
||||
_e_kbd_dict_changed_write_add(kd, word, usage);
|
||||
}
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_dict_word_delete(E_Kbd_Dict *kd, const char *word)
|
||||
{
|
||||
// delete a word from the dictionary
|
||||
E_Kbd_Dict_Word *kw;
|
||||
|
||||
kw = _e_kbd_dict_changed_write_find(kd, word);
|
||||
if (kw)
|
||||
kw->usage = -1;
|
||||
else
|
||||
{
|
||||
if (_e_kbd_dict_find_full(kd, word))
|
||||
_e_kbd_dict_changed_write_add(kd, word, -1);
|
||||
}
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_dict_word_letter_clear(E_Kbd_Dict *kd)
|
||||
{
|
||||
// clear the current word buffer
|
||||
while (kd->word.letters)
|
||||
e_kbd_dict_word_letter_delete(kd);
|
||||
if (kd->matches.deadends)
|
||||
{
|
||||
eina_hash_free(kd->matches.deadends);
|
||||
kd->matches.deadends = NULL;
|
||||
}
|
||||
if (kd->matches.leads)
|
||||
{
|
||||
eina_hash_free(kd->matches.leads);
|
||||
kd->matches.leads = NULL;
|
||||
}
|
||||
while (kd->matches.list)
|
||||
{
|
||||
E_Kbd_Dict_Word *kw;
|
||||
|
||||
kw = kd->matches.list->data;
|
||||
eina_stringshare_del(kw->word);
|
||||
free(kw);
|
||||
kd->matches.list = eina_list_remove_list(kd->matches.list, kd->matches.list);
|
||||
}
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_dict_word_letter_add(E_Kbd_Dict *kd, const char *letter, int dist)
|
||||
{
|
||||
// add a letter with a distance (0 == closest) as an option for the current
|
||||
// letter position - advance starts a new letter position
|
||||
Eina_List *l, *list;
|
||||
E_Kbd_Dict_Letter *kl;
|
||||
|
||||
l = eina_list_last(kd->word.letters);
|
||||
if (!l) return;
|
||||
list = l->data;
|
||||
kl = E_NEW(E_Kbd_Dict_Letter, 1);
|
||||
if (!kl) return;
|
||||
kl->letter = eina_stringshare_add(letter);
|
||||
kl->dist = dist;
|
||||
list = eina_list_append(list, kl);
|
||||
l->data = list;
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_dict_word_letter_advance(E_Kbd_Dict *kd)
|
||||
{
|
||||
// start a new letter in the word
|
||||
kd->word.letters = eina_list_append(kd->word.letters, NULL);
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_dict_word_letter_delete(E_Kbd_Dict *kd)
|
||||
{
|
||||
// delete the current letter completely
|
||||
Eina_List *l, *list;
|
||||
|
||||
l = eina_list_last(kd->word.letters);
|
||||
if (!l) return;
|
||||
list = l->data;
|
||||
while (list)
|
||||
{
|
||||
E_Kbd_Dict_Letter *kl;
|
||||
|
||||
kl = list->data;
|
||||
eina_stringshare_del(kl->letter);
|
||||
free(kl);
|
||||
list = eina_list_remove_list(list, list);
|
||||
}
|
||||
kd->word.letters = eina_list_remove_list(kd->word.letters, l);
|
||||
}
|
||||
|
||||
static void
|
||||
_e_kbd_dict_matches_lookup_iter(E_Kbd_Dict *kd, Eina_List *word,
|
||||
Eina_List *more)
|
||||
{
|
||||
Eina_List *l, *l2, *list;
|
||||
const char *p, *pn;
|
||||
char *base, *buf, *wd, *bufapp;
|
||||
E_Kbd_Dict_Letter *kl;
|
||||
int len, dist = 0, d, baselen, maxdist = 0, md;
|
||||
|
||||
static int level = 0, lv;
|
||||
|
||||
len = 0;
|
||||
level++;
|
||||
for (l = word; l; l = l->next)
|
||||
{
|
||||
kl = l->data;
|
||||
len += strlen(kl->letter);
|
||||
dist += kl->dist;
|
||||
if (kl->dist > maxdist) maxdist = kl->dist;
|
||||
}
|
||||
if (maxdist < 1) maxdist = 1;
|
||||
buf = alloca(len + 20); // 20 - just padding enough for 1 more utf8 char
|
||||
base = alloca(len + 20);
|
||||
base[0] = 0;
|
||||
for (l = word; l; l = l->next)
|
||||
{
|
||||
kl = l->data;
|
||||
strcat(base, kl->letter);
|
||||
}
|
||||
baselen = strlen(base);
|
||||
strcpy(buf, base);
|
||||
bufapp = buf + baselen;
|
||||
list = more->data;
|
||||
for (l = list; l; l = l->next)
|
||||
{
|
||||
kl = l->data;
|
||||
if (kl->dist > maxdist) maxdist = kl->dist;
|
||||
}
|
||||
for (l = list; l; l = l->next)
|
||||
{
|
||||
kl = l->data;
|
||||
strcpy(bufapp, kl->letter);
|
||||
if ((kd->matches.deadends) && eina_hash_find(kd->matches.deadends, buf))
|
||||
continue;
|
||||
p = eina_hash_find(kd->matches.leads, buf);
|
||||
if (p) p = _e_kbd_dict_find_pointer(kd, p, baselen, buf);
|
||||
else p = _e_kbd_dict_find(kd, buf);
|
||||
if (!p)
|
||||
{
|
||||
if (!kd->matches.deadends)
|
||||
kd->matches.deadends = eina_hash_string_superfast_new(NULL);
|
||||
eina_hash_add(kd->matches.deadends, buf, kd);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!kd->matches.leads)
|
||||
kd->matches.leads = eina_hash_string_superfast_new(NULL);
|
||||
eina_hash_add(kd->matches.leads, buf, p);
|
||||
}
|
||||
if ((!more->next) || (!more->next->data))
|
||||
{
|
||||
d = dist + kl->dist;
|
||||
md = maxdist;
|
||||
for (;;)
|
||||
{
|
||||
E_Kbd_Dict_Word *kw;
|
||||
int usage = 0;
|
||||
|
||||
wd = _e_kbd_dict_line_parse(kd, p, &usage);
|
||||
if (!wd) break;
|
||||
if (_e_kbd_dict_normalized_strcmp(wd, buf))
|
||||
{
|
||||
free(wd);
|
||||
break;
|
||||
}
|
||||
kw = E_NEW(E_Kbd_Dict_Word, 1);
|
||||
if (kw)
|
||||
{
|
||||
int accuracy;
|
||||
int w, b, w2, b2, wc, bc, upper;
|
||||
|
||||
// match any capitalisation
|
||||
for (w = 0, b = 0; wd[w] && buf[b];)
|
||||
{
|
||||
b2 = evas_string_char_next_get(buf, b, &bc);
|
||||
w2 = evas_string_char_next_get(wd, w, &wc);
|
||||
if (isupper(bc)) wd[w] = toupper(wc);
|
||||
w = w2;
|
||||
b = b2;
|
||||
}
|
||||
kw->word = eina_stringshare_add(wd);
|
||||
// FIXME: magic combination of distance metric and
|
||||
// frequency of useage. this is simple now, but could
|
||||
// be tweaked
|
||||
wc = eina_list_count(word);
|
||||
if (md < 1) md = 1;
|
||||
|
||||
// basically a metric to see how far away teh keys that
|
||||
// were actually pressed are away from the letters of
|
||||
// this word in a physical on-screen sense
|
||||
accuracy = md - (d / (wc + 1));
|
||||
// usage is the frequency of usage in the dictionary.
|
||||
// it its < 1 time, it's assumed to be 1.
|
||||
if (usage < 1) usage = 1;
|
||||
// multiply usage by a factor of 100 for better detailed
|
||||
// sorting. 10 == 1/10th factor
|
||||
usage = 100 + ((usage - 1) * 10);
|
||||
// and well just multiply and lets see. maybe this can
|
||||
// do with multiplication factors etc. but simple for
|
||||
// now.
|
||||
kw->usage = (usage * accuracy) / md;
|
||||
kd->matches.list = eina_list_append(kd->matches.list, kw);
|
||||
}
|
||||
free(wd);
|
||||
p = _e_kbd_dict_line_next(kd, p);
|
||||
if (p >= (kd->file.dict + kd->file.size)) break;
|
||||
if (!p) break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
word = eina_list_append(word, kl);
|
||||
_e_kbd_dict_matches_lookup_iter(kd, word, more->next);
|
||||
word = eina_list_remove_list(word, eina_list_last(word));
|
||||
}
|
||||
}
|
||||
level--;
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_dict_matches_lookup(E_Kbd_Dict *kd)
|
||||
{
|
||||
// find all matches and sort them
|
||||
while (kd->matches.list)
|
||||
{
|
||||
E_Kbd_Dict_Word *kw;
|
||||
|
||||
kw = kd->matches.list->data;
|
||||
eina_stringshare_del(kw->word);
|
||||
free(kw);
|
||||
kd->matches.list = eina_list_remove_list(kd->matches.list, kd->matches.list);
|
||||
}
|
||||
if (kd->word.letters)
|
||||
_e_kbd_dict_matches_lookup_iter(kd, NULL, kd->word.letters);
|
||||
kd->matches.list = eina_list_sort(kd->matches.list,
|
||||
eina_list_count(kd->matches.list),
|
||||
_e_kbd_dict_matches_loolup_cb_sort);
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_dict_matches_first(E_Kbd_Dict *kd)
|
||||
{
|
||||
// jump to first match
|
||||
kd->matches.list_ptr = kd->matches.list;
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_dict_matches_next(E_Kbd_Dict *kd)
|
||||
{
|
||||
// jump to next match
|
||||
kd->matches.list_ptr = kd->matches.list_ptr->next;
|
||||
}
|
||||
|
||||
EAPI const char *
|
||||
e_kbd_dict_matches_match_get(E_Kbd_Dict *kd, int *pri_ret)
|
||||
{
|
||||
// return the word (string utf-8) for the current match
|
||||
if (kd->matches.list_ptr)
|
||||
{
|
||||
E_Kbd_Dict_Word *kw;
|
||||
|
||||
kw = kd->matches.list_ptr->data;
|
||||
if (kw)
|
||||
{
|
||||
*pri_ret = kw->usage;
|
||||
return kw->word;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
#ifndef E_KBD_DICT_H
|
||||
#define E_KBD_DICT_H
|
||||
|
||||
typedef struct _E_Kbd_Dict E_Kbd_Dict;
|
||||
typedef struct _E_Kbd_Dict_Word E_Kbd_Dict_Word;
|
||||
typedef struct _E_Kbd_Dict_Letter E_Kbd_Dict_Letter;
|
||||
|
||||
struct _E_Kbd_Dict_Word
|
||||
{
|
||||
const char *word;
|
||||
int usage;
|
||||
};
|
||||
|
||||
struct _E_Kbd_Dict_Letter
|
||||
{
|
||||
const char *letter;
|
||||
int dist;
|
||||
};
|
||||
|
||||
struct _E_Kbd_Dict
|
||||
{
|
||||
struct {
|
||||
const char *file;
|
||||
int fd;
|
||||
const char *dict;
|
||||
int size;
|
||||
} file;
|
||||
struct {
|
||||
const char *tuples[128][128];
|
||||
} lookup;
|
||||
struct {
|
||||
Ecore_Timer *flush_timer;
|
||||
Eina_List *writes;
|
||||
} changed;
|
||||
struct {
|
||||
Eina_List *letters;
|
||||
} word;
|
||||
struct {
|
||||
Eina_Hash *deadends;
|
||||
Eina_Hash *leads;
|
||||
Eina_List *list;
|
||||
Eina_List *list_ptr;
|
||||
} matches;
|
||||
};
|
||||
|
||||
|
||||
EAPI E_Kbd_Dict *e_kbd_dict_new(const char *file);
|
||||
EAPI void e_kbd_dict_free(E_Kbd_Dict *kd);
|
||||
EAPI void e_kbd_dict_save(E_Kbd_Dict *kd);
|
||||
EAPI void e_kbd_dict_word_usage_adjust(E_Kbd_Dict *kd, const char *word, int adjust);
|
||||
EAPI void e_kbd_dict_word_delete(E_Kbd_Dict *kd, const char *word);
|
||||
EAPI void e_kbd_dict_word_letter_clear(E_Kbd_Dict *kd);
|
||||
EAPI void e_kbd_dict_word_letter_add(E_Kbd_Dict *kd, const char *letter, int dist);
|
||||
EAPI void e_kbd_dict_word_letter_advance(E_Kbd_Dict *kd);
|
||||
EAPI void e_kbd_dict_word_letter_delete(E_Kbd_Dict *kd);
|
||||
EAPI void e_kbd_dict_matches_lookup(E_Kbd_Dict *kd);
|
||||
EAPI void e_kbd_dict_matches_first(E_Kbd_Dict *kd);
|
||||
EAPI void e_kbd_dict_matches_next(E_Kbd_Dict *kd);
|
||||
EAPI const char *e_kbd_dict_matches_match_get(E_Kbd_Dict *kd, int *pri_ret);
|
||||
|
||||
#endif
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,115 @@
|
|||
#ifndef E_KBD_INT_H
|
||||
#define E_KBD_INT_H
|
||||
|
||||
#include "e_kbd_buf.h"
|
||||
|
||||
typedef enum _E_Kbd_Int_Type
|
||||
{
|
||||
E_KBD_INT_TYPE_UNKNOWN = 0,
|
||||
E_KBD_INT_TYPE_ALPHA = (1 << 0),
|
||||
E_KBD_INT_TYPE_NUMERIC = (1 << 1),
|
||||
E_KBD_INT_TYPE_PIN = (1 << 2),
|
||||
E_KBD_INT_TYPE_PHONE_NUMBER = (1 << 3),
|
||||
E_KBD_INT_TYPE_HEX = (1 << 4),
|
||||
E_KBD_INT_TYPE_TERMINAL = (1 << 5),
|
||||
E_KBD_INT_TYPE_PASSWORD = (1 << 6)
|
||||
} E_Kbd_Int_Type;
|
||||
|
||||
typedef struct _E_Kbd_Int E_Kbd_Int;
|
||||
typedef struct _E_Kbd_Int_Key E_Kbd_Int_Key;
|
||||
typedef struct _E_Kbd_Int_Key_State E_Kbd_Int_Key_State;
|
||||
typedef struct _E_Kbd_Int_Layout E_Kbd_Int_Layout;
|
||||
typedef struct _E_Kbd_Int_Match E_Kbd_Int_Match;
|
||||
|
||||
struct _E_Kbd_Int
|
||||
{
|
||||
E_Win *win;
|
||||
const char *themedir, *syskbds, *sysdicts;
|
||||
Evas_Object *base_obj, *layout_obj, *event_obj, *icon_obj, *box_obj;
|
||||
Eina_List *layouts;
|
||||
Eina_List *matches;
|
||||
Ecore_Event_Handler *client_message_handler;
|
||||
struct {
|
||||
char *directory;
|
||||
const char *file;
|
||||
int w, h;
|
||||
int fuzz;
|
||||
E_Kbd_Int_Type type;
|
||||
Eina_List *keys;
|
||||
E_Kbd_Int_Key *pressed;
|
||||
int state;
|
||||
} layout;
|
||||
struct {
|
||||
Evas_Coord x, y, cx, cy;
|
||||
int lx, ly, clx, cly;
|
||||
Ecore_Timer *hold_timer;
|
||||
unsigned char down : 1;
|
||||
unsigned char stroke : 1;
|
||||
unsigned char zoom : 1;
|
||||
} down;
|
||||
struct {
|
||||
E_Popup *popup;
|
||||
Evas_Object *base_obj, *ilist_obj;
|
||||
} layoutlist;
|
||||
struct {
|
||||
E_Popup *popup;
|
||||
Evas_Object *base_obj, *ilist_obj;
|
||||
Eina_List *matches;
|
||||
} matchlist;
|
||||
struct {
|
||||
E_Popup *popup;
|
||||
Evas_Object *base_obj, *ilist_obj;
|
||||
Eina_List *matches;
|
||||
} dictlist;
|
||||
struct {
|
||||
E_Popup *popup;
|
||||
Evas_Object *base_obj, *layout_obj, *sublayout_obj;
|
||||
E_Kbd_Int_Key *pressed;
|
||||
} zoomkey;
|
||||
|
||||
E_Kbd_Buf *kbuf;
|
||||
};
|
||||
|
||||
struct _E_Kbd_Int_Key
|
||||
{
|
||||
int x, y, w, h;
|
||||
|
||||
Eina_List *states;
|
||||
Evas_Object *obj, *zoom_obj, *icon_obj, *zoom_icon_obj;
|
||||
|
||||
unsigned char pressed : 1;
|
||||
unsigned char selected : 1;
|
||||
|
||||
unsigned char is_shift : 1;
|
||||
unsigned char is_ctrl : 1;
|
||||
unsigned char is_alt : 1;
|
||||
unsigned char is_capslock : 1;
|
||||
};
|
||||
|
||||
struct _E_Kbd_Int_Key_State
|
||||
{
|
||||
int state;
|
||||
const char *label, *icon;
|
||||
const char *out;
|
||||
};
|
||||
|
||||
struct _E_Kbd_Int_Layout
|
||||
{
|
||||
const char *path;
|
||||
const char *dir;
|
||||
const char *icon;
|
||||
const char *name;
|
||||
int type;
|
||||
};
|
||||
|
||||
struct _E_Kbd_Int_Match
|
||||
{
|
||||
E_Kbd_Int *ki;
|
||||
const char *str;
|
||||
Evas_Object *obj;
|
||||
};
|
||||
|
||||
EAPI E_Kbd_Int *e_kbd_int_new(const char *themedir, const char *syskbds, const char *sysdicts);
|
||||
EAPI void e_kbd_int_free(E_Kbd_Int *ki);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,38 @@
|
|||
#include "e.h"
|
||||
#include "e_kbd_send.h"
|
||||
|
||||
static const char *
|
||||
_string_to_keysym(const char *str)
|
||||
{
|
||||
int glyph, ok;
|
||||
|
||||
/* utf8 -> glyph id (unicode - ucs4) */
|
||||
glyph = 0;
|
||||
ok = evas_string_char_next_get(str, 0, &glyph);
|
||||
if (glyph <= 0) return NULL;
|
||||
/* glyph id -> keysym */
|
||||
if (glyph > 0xff) glyph |= 0x1000000;
|
||||
return ecore_x_keysym_string_get(glyph);
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_send_string_press(const char *str, Kbd_Mod mod)
|
||||
{
|
||||
const char *key = NULL;
|
||||
|
||||
key = _string_to_keysym(str);
|
||||
if (!key) return;
|
||||
e_kbd_send_keysym_press(key, mod);
|
||||
}
|
||||
|
||||
EAPI void
|
||||
e_kbd_send_keysym_press(const char *key, Kbd_Mod mod)
|
||||
{
|
||||
if (mod & KBD_MOD_CTRL) ecore_x_test_fake_key_down("Control_L");
|
||||
if (mod & KBD_MOD_ALT) ecore_x_test_fake_key_down("Alt_L");
|
||||
if (mod & KBD_MOD_WIN) ecore_x_test_fake_key_down("Super_L");
|
||||
ecore_x_test_fake_key_press(key);
|
||||
if (mod & KBD_MOD_WIN) ecore_x_test_fake_key_up("Super_L");
|
||||
if (mod & KBD_MOD_ALT) ecore_x_test_fake_key_up("Alt_L");
|
||||
if (mod & KBD_MOD_CTRL) ecore_x_test_fake_key_up("Control_L");
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
#ifndef E_KBD_SEND_H
|
||||
#define E_KBD_SEND_H
|
||||
|
||||
typedef enum _Kbd_Mod
|
||||
{
|
||||
KBD_MOD_SHIFT = (1 << 0),
|
||||
KBD_MOD_CTRL = (1 << 1),
|
||||
KBD_MOD_ALT = (1 << 2),
|
||||
KBD_MOD_WIN = (1 << 3)
|
||||
} Kbd_Mod;
|
||||
|
||||
EAPI void e_kbd_send_string_press(const char *str, Kbd_Mod mod);
|
||||
EAPI void e_kbd_send_keysym_press(const char *key, Kbd_Mod mod);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue