summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChoe Hwanjin <choe.hwanjin@gmail.com>2010-11-14 20:35:48 +0900
committerChoe Hwanjin <choe.hwanjin@gmail.com>2010-11-14 20:35:48 +0900
commite87ec32568f7d9194bb8ce00abf9e820a08a07c0 (patch)
tree8dd45eec9436925a520514de29043780234ea3ad
parentbeb8f19bdc7cd031a4caa127e53077a0b02822eb (diff)
downloadlibhangul-e87ec32568f7d9194bb8ce00abf9e820a08a07c0.tar.gz
한글 키보드의 리스트와 이름을 가져오는 인터페이스 구현
git-svn-id: http://kldp.net/svn/hangul/libhangul/trunk@227 8f00fcd2-89fc-0310-932e-b01be5b65e01
-rw-r--r--hangul/hangul.h4
-rw-r--r--hangul/hangulinputcontext.c199
-rw-r--r--hangul/hangulkeyboard.h4
-rw-r--r--test/test.c26
4 files changed, 158 insertions, 75 deletions
diff --git a/hangul/hangul.h b/hangul/hangul.h
index 26e1871..648e339 100644
--- a/hangul/hangul.h
+++ b/hangul/hangul.h
@@ -125,6 +125,10 @@ void hangul_ic_set_combination(HangulInputContext *hic,
void hangul_ic_connect_callback(HangulInputContext* hic, const char* event,
void* callback, void* user_data);
+unsigned hangul_ic_get_n_keyboards();
+const char* hangul_ic_get_keyboard_id(unsigned index_);
+const char* hangul_ic_get_keyboard_name(unsigned index_);
+
const ucschar* hangul_ic_get_preedit_string(HangulInputContext *hic);
const ucschar* hangul_ic_get_commit_string(HangulInputContext *hic);
const ucschar* hangul_ic_flush(HangulInputContext *hic);
diff --git a/hangul/hangulinputcontext.c b/hangul/hangulinputcontext.c
index c8a3ff5..40ce419 100644
--- a/hangul/hangulinputcontext.c
+++ b/hangul/hangulinputcontext.c
@@ -188,7 +188,10 @@ typedef struct _HangulCombinationItem HangulCombinationItem;
struct _HangulKeyboard {
int type;
- ucschar* table;
+ const char* id;
+ const char* name;
+ const ucschar* table;
+ const HangulCombination* combination_table;
};
struct _HangulCombinationItem {
@@ -237,65 +240,101 @@ struct _HangulInputContext {
#include "hangulkeyboard.h"
+static const HangulCombination hangul_combination_default = {
+ N_ELEMENTS(hangul_combination_table_default),
+ (HangulCombinationItem*)hangul_combination_table_default
+};
+
+static const HangulCombination hangul_combination_romaja = {
+ N_ELEMENTS(hangul_combination_table_romaja),
+ (HangulCombinationItem*)hangul_combination_table_romaja
+};
+
+static const HangulCombination hangul_combination_full = {
+ N_ELEMENTS(hangul_combination_table_full),
+ (HangulCombinationItem*)hangul_combination_table_full
+};
+
+static const HangulCombination hangul_combination_ahn = {
+ N_ELEMENTS(hangul_combination_table_ahn),
+ (HangulCombinationItem*)hangul_combination_table_ahn
+};
+
static const HangulKeyboard hangul_keyboard_2 = {
HANGUL_KEYBOARD_TYPE_JAMO,
- (ucschar*)hangul_keyboard_table_2
+ "2",
+ "Dubeolsik",
+ (ucschar*)hangul_keyboard_table_2,
+ &hangul_combination_default
};
static const HangulKeyboard hangul_keyboard_32 = {
HANGUL_KEYBOARD_TYPE_JASO,
- (ucschar*)hangul_keyboard_table_32
+ "32",
+ "Sebeolsik dubeol layout",
+ (ucschar*)hangul_keyboard_table_32,
+ &hangul_combination_default
};
static const HangulKeyboard hangul_keyboard_390 = {
HANGUL_KEYBOARD_TYPE_JASO,
- (ucschar*)hangul_keyboard_table_390
+ "39",
+ "Sebeolsik 390",
+ (ucschar*)hangul_keyboard_table_390,
+ &hangul_combination_default
};
static const HangulKeyboard hangul_keyboard_3final = {
HANGUL_KEYBOARD_TYPE_JASO,
- (ucschar*)hangul_keyboard_table_3final
+ "3f",
+ "Sebeolsik final",
+ (ucschar*)hangul_keyboard_table_3final,
+ &hangul_combination_default
};
static const HangulKeyboard hangul_keyboard_3sun = {
HANGUL_KEYBOARD_TYPE_JASO,
- (ucschar*)hangul_keyboard_table_3sun
+ "3sun",
+ "Sebeolsik no-shift",
+ (ucschar*)hangul_keyboard_table_3sun,
+ &hangul_combination_default
};
static const HangulKeyboard hangul_keyboard_3yet = {
HANGUL_KEYBOARD_TYPE_JASO,
- (ucschar*)hangul_keyboard_table_3yet
+ "3y",
+ "Sebeolsik yetgeul",
+ (ucschar*)hangul_keyboard_table_3yet,
+ &hangul_combination_full
};
static const HangulKeyboard hangul_keyboard_romaja = {
HANGUL_KEYBOARD_TYPE_ROMAJA,
- (ucschar*)hangul_keyboard_table_romaja
+ "ro",
+ "Romaja",
+ (ucschar*)hangul_keyboard_table_romaja,
+ &hangul_combination_romaja
};
-static const HangulKeyboard hangul_keyboard_an = {
+static const HangulKeyboard hangul_keyboard_ahn = {
HANGUL_KEYBOARD_TYPE_JASO,
- (ucschar*)hangul_keyboard_table_an
+ "ahn",
+ "Ahnmatae",
+ (ucschar*)hangul_keyboard_table_ahn,
+ &hangul_combination_ahn
};
-static const HangulCombination hangul_combination_default = {
- N_ELEMENTS(hangul_combination_table_default),
- (HangulCombinationItem*)hangul_combination_table_default
+static const HangulKeyboard* hangul_keyboards[] = {
+ &hangul_keyboard_2,
+ &hangul_keyboard_32,
+ &hangul_keyboard_390,
+ &hangul_keyboard_3final,
+ &hangul_keyboard_3sun,
+ &hangul_keyboard_3yet,
+ &hangul_keyboard_romaja,
+ &hangul_keyboard_ahn,
};
-static const HangulCombination hangul_combination_romaja = {
- N_ELEMENTS(hangul_combination_table_romaja),
- (HangulCombinationItem*)hangul_combination_table_romaja
-};
-
-static const HangulCombination hangul_combination_full = {
- N_ELEMENTS(hangul_combination_table_full),
- (HangulCombinationItem*)hangul_combination_table_full
-};
-
-static const HangulCombination hangul_combination_an = {
- N_ELEMENTS(hangul_combination_table_an),
- (HangulCombinationItem*)hangul_combination_table_an
-};
static void hangul_buffer_push(HangulBuffer *buffer, ucschar ch);
static ucschar hangul_buffer_pop (HangulBuffer *buffer);
@@ -312,12 +351,13 @@ hangul_keyboard_new()
{
HangulKeyboard *keyboard = malloc(sizeof(HangulKeyboard));
if (keyboard != NULL) {
- keyboard->table = malloc(sizeof(ucschar) * HANGUL_KEYBOARD_TABLE_SIZE);
- if (keyboard->table != NULL) {
+ ucschar* table = malloc(sizeof(ucschar) * HANGUL_KEYBOARD_TABLE_SIZE);
+ if (table != NULL) {
int i;
for (i = 0; i < HANGUL_KEYBOARD_TABLE_SIZE; i++)
- keyboard->table[i] = 0;
+ table[i] = 0;
+ keyboard->table = table;
return keyboard;
}
free(keyboard);
@@ -341,8 +381,10 @@ void
hangul_keyboard_set_value(HangulKeyboard *keyboard, int key, ucschar value)
{
if (keyboard != NULL) {
- if (key >= 0 && key < N_ELEMENTS(keyboard->table))
- keyboard->table[key] = value;
+ if (key >= 0 && key < N_ELEMENTS(keyboard->table)) {
+ ucschar* table = (ucschar*)keyboard->table;
+ table[key] = value;
+ }
}
}
@@ -1580,6 +1622,25 @@ hangul_ic_set_keyboard(HangulInputContext *hic, const HangulKeyboard* keyboard)
hic->keyboard = keyboard;
}
+static const HangulKeyboard*
+hangul_ic_get_keyboard_by_id(const char* id)
+{
+ unsigned i;
+ unsigned n;
+
+ /* hangul_keyboards 테이블은 id 순으로 정렬되어 있지 않으므로
+ * binary search를 할수 없고 linear search를 한다. */
+ n = hangul_ic_get_n_keyboards();
+ for (i = 0; i < n; ++i) {
+ const HangulKeyboard* keyboard = hangul_keyboards[i];
+ if (strcmp(id, keyboard->id) == 0) {
+ return keyboard;
+ }
+ }
+
+ return NULL;
+}
+
/**
* @ingroup hangulic
* @brief @ref HangulInputContext의 자판 배열을 바꾸는 함수
@@ -1604,53 +1665,19 @@ hangul_ic_set_keyboard(HangulInputContext *hic, const HangulKeyboard* keyboard)
void
hangul_ic_select_keyboard(HangulInputContext *hic, const char* id)
{
+ const HangulKeyboard* keyboard;
+
if (hic == NULL)
return;
if (id == NULL)
id = "2";
- if (strcmp(id, "32") == 0) {
- hic->keyboard = &hangul_keyboard_32;
- hic->combination = &hangul_combination_default;
- hic->output_mode = HANGUL_OUTPUT_SYLLABLE;
- hic->use_jamo_mode_only = FALSE;
- } else if (strcmp(id, "39") == 0) {
- hic->keyboard = &hangul_keyboard_390;
- hic->combination = &hangul_combination_default;
- hic->output_mode = HANGUL_OUTPUT_SYLLABLE;
- hic->use_jamo_mode_only = FALSE;
- } else if (strcmp(id, "3f") == 0) {
- hic->keyboard = &hangul_keyboard_3final;
- hic->combination = &hangul_combination_default;
- hic->output_mode = HANGUL_OUTPUT_SYLLABLE;
- hic->use_jamo_mode_only = FALSE;
- } else if (strcmp(id, "3s") == 0) {
- hic->keyboard = &hangul_keyboard_3sun;
- hic->combination = &hangul_combination_default;
- hic->output_mode = HANGUL_OUTPUT_SYLLABLE;
- hic->use_jamo_mode_only = FALSE;
- } else if (strcmp(id, "3y") == 0) {
- hic->keyboard = &hangul_keyboard_3yet;
- hic->combination = &hangul_combination_full;
- hic->output_mode = HANGUL_OUTPUT_JAMO;
- hic->use_jamo_mode_only = TRUE;
- } else if (strcmp(id, "ro") == 0) {
- hic->keyboard = &hangul_keyboard_romaja;
- hic->combination = &hangul_combination_romaja;
- hic->output_mode = HANGUL_OUTPUT_SYLLABLE;
- hic->use_jamo_mode_only = FALSE;
- } else if (strcmp(id, "an") == 0) {
- hic->keyboard = &hangul_keyboard_an;
- hic->combination = &hangul_combination_an;
- hic->output_mode = HANGUL_OUTPUT_SYLLABLE;
- hic->use_jamo_mode_only = FALSE;
- } else {
- hic->keyboard = &hangul_keyboard_2;
- hic->combination = &hangul_combination_default;
- hic->output_mode = HANGUL_OUTPUT_SYLLABLE;
- hic->use_jamo_mode_only = FALSE;
- }
+ keyboard = hangul_ic_get_keyboard_by_id(id);
+ if (keyboard != NULL) {
+ hic->keyboard = keyboard;
+ hic->combination = keyboard->combination_table;
+ }
}
void
@@ -1723,3 +1750,29 @@ hangul_ic_delete(HangulInputContext *hic)
free(hic);
}
+
+unsigned int
+hangul_ic_get_n_keyboards()
+{
+ return N_ELEMENTS(hangul_keyboards);
+}
+
+const char*
+hangul_ic_get_keyboard_id(unsigned index_)
+{
+ if (index_ < N_ELEMENTS(hangul_keyboards)) {
+ return hangul_keyboards[index_]->id;
+ }
+
+ return NULL;
+}
+
+const char*
+hangul_ic_get_keyboard_name(unsigned index_)
+{
+ if (index_ < N_ELEMENTS(hangul_keyboards)) {
+ return hangul_keyboards[index_]->name;
+ }
+
+ return NULL;
+}
diff --git a/hangul/hangulkeyboard.h b/hangul/hangulkeyboard.h
index 3fb3a80..08db9c2 100644
--- a/hangul/hangulkeyboard.h
+++ b/hangul/hangulkeyboard.h
@@ -915,7 +915,7 @@ static const ucschar hangul_keyboard_table_romaja[] = {
0x0000 /* 0x7F delete */
};
-static const ucschar hangul_keyboard_table_an[] = {
+static const ucschar hangul_keyboard_table_ahn[] = {
0x0000, /* 0x00 null */
0x0000, /* 0x01 start of heading */
0x0000, /* 0x02 start of text */
@@ -1470,7 +1470,7 @@ static const HangulCombinationItem hangul_combination_table_full[] = {
{ 0xd7f711b8, 0xd7f8 }, /* jongseong cieuc-pieup + pieup = cieuc-ssangpieup */
};
-static const HangulCombinationItem hangul_combination_table_an[] = {
+static const HangulCombinationItem hangul_combination_table_ahn[] = {
{ 0x11001103, 0x1104 }, /* choseong kiyeok + tikeut = ssangtikeut */
{ 0x1100110b, 0x1101 }, /* choseong kiyeok + ieung = ssangkiyeok */
{ 0x11001112, 0x110f }, /* choseong kiyeok + hieuh = khieukh */
diff --git a/test/test.c b/test/test.c
index 484a0c1..2b4b85f 100644
--- a/test/test.c
+++ b/test/test.c
@@ -234,6 +234,31 @@ START_TEST(test_syllable_iterator)
}
END_TEST
+START_TEST(test_hangul_keyboard)
+{
+ const char* id;
+ const char* name;
+ unsigned int n;
+ unsigned int i;
+
+ n = hangul_ic_get_n_keyboards();
+ fail_unless(n != 0,
+ "error: there is no builtin hangul keyboard");
+
+ for (i = 0; i < n; ++i) {
+ id = hangul_ic_get_keyboard_id(i);
+ fail_unless(id != NULL,
+ "error: keyboard id == NULL");
+ }
+
+ for (i = 0; i < n; ++i) {
+ name = hangul_ic_get_keyboard_name(i);
+ fail_unless(name != NULL,
+ "error: keyboard id == NULL");
+ }
+}
+END_TEST
+
Suite* libhangul_suite()
{
Suite* s = suite_create("libhangul");
@@ -241,6 +266,7 @@ Suite* libhangul_suite()
TCase* hangul = tcase_create("hangul");
tcase_add_test(hangul, test_hangul_ic_process_romaja);
tcase_add_test(hangul, test_syllable_iterator);
+ tcase_add_test(hangul, test_hangul_keyboard);
suite_add_tcase(s, hangul);
return s;