LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2333|回复: 13

如何进行 SCIM 前端开发?

[复制链接]
发表于 2008-6-22 15:37:40 | 显示全部楼层 |阅读模式
为linux游戏开发输入法,是个很头痛的问题。虽然从SDL-1.2.13开始 XIM已经被支持了,但是还有很多问题,比如说全屏下输入法不可见。


所以我想 沿用win32下的游戏模式,传递键盘事件给输入法引擎,得到一组字符串,再自己画出IM Panel。XIM好像很难这样作,SCIM的SocketFrontEnd有点这个意思,不过还是不行。所以我想自己作一个scim的frontEnd来提供类似候选字符串的查找的功能。

frontend我写了一点,不过在process_key_event (id, key)地方卡住了,没思路了,明明scim的scoket和XIM的前端是可以的。。。

scim的代码看了2天,头都大了,结构越复杂,代码看起来就越累,还没什么文档参考,


  1. #define Uses_SCIM_LOOKUP_TABLE
  2. #define Uses_SCIM_FRONTEND


  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <scim.h>
  7. #include <locale.h>
  8. #include <sstream>
  9. #include <iostream>
  10. using namespace scim;
  11. class MyFrontEnd: public FrontEndBase {
  12. private:
  13.         WideString factory_info(String uuid) {
  14.                 std::basic_stringstream<wchar_t> stream;
  15.                 stream << "Name:" << get_factory_name(uuid).c_str() << ' ';
  16.                 stream << "Language:" << get_factory_language(uuid).c_str() << ' ';
  17.                 stream << "Authors:" << get_factory_authors(uuid).c_str() << ' ';
  18.                 stream << "Credits:" << get_factory_credits(uuid).c_str() << ' ';
  19.                 //stream << "Help:"<<get_factory_help(uuid).c_str()<<' ';
  20.                 //stream << "Icon_file:"<<get_factory_icon_file(uuid).c_str()<<' ';
  21.                 stream << "Locales:" << get_factory_locales(uuid).c_str() << ' ';
  22.                 //stream<<'\n';
  23.                 return stream.str();
  24.         }

  25.         void dump_factory() {
  26.                 std::vector<String> uuids;
  27.                 String language;
  28.                 uint32 ret = get_factory_list_for_language(uuids, language);
  29.                 if (ret > 0) {
  30.                         std::vector<String>::const_iterator it;
  31.                         for (it = uuids.begin(); it != uuids.end();++it) {
  32.                                 printf("%S\n",factory_info(*it).c_str() );
  33.                         }
  34.                 }
  35.         }
  36. public:

  37.         MyFrontEnd(const BackEndPointer &backend) :
  38.         FrontEndBase(backend) {
  39.                 setlocale(LC_ALL, "zh_CN.UTF-8");
  40.         }

  41.         void init(int argc, char **argv) {
  42.                 String uuid;
  43.                 String language="zh_CN";
  44.                 String encoding="UTF-8";
  45.                 uuid=get_default_factory (language, encoding);
  46.                 printf("Default IM:%S\n",factory_info(uuid).c_str());
  47.                 int id= new_instance (uuid, encoding);
  48.                 if(id==-1){
  49.                         perror("new_instance Error\n");
  50.                 }
  51.                 else {
  52.                         printf("Get IM success\n");
  53.                         KeyEvent key("Control+space");
  54.                         focus_in(id);
  55.                         if(process_key_event (id, key)) {
  56.                                 printf("Process key success\n");
  57.                         }else{
  58.                                 perror("Process key error!");
  59.                         }

  60.                 }
  61.         }
  62.         void run () {}
  63. };

  64. //Module Interface
  65. static Pointer<MyFrontEnd> _scim_frontend(0);
  66. static int _argc;
  67. static char **_argv;

  68. extern "C" {
  69. void scim_module_init(void) {
  70.         SCIM_DEBUG_FRONTEND(1) << "Initializing Socket FrontEnd module...\n";
  71. }

  72. void scim_module_exit(void) {
  73.         SCIM_DEBUG_FRONTEND(1) << "Exiting Socket FrontEnd module...\n";
  74.         _scim_frontend.reset();
  75. }

  76. void scim_frontend_module_init(const BackEndPointer &backend,
  77.                 const ConfigPointer &config, int argc, char **argv) {
  78.         if (_scim_frontend.null()) {
  79.                 SCIM_DEBUG_FRONTEND(1) << "Initializing Socket FrontEnd module (more)...\n";
  80.                 _scim_frontend = new MyFrontEnd(backend);
  81.                 _argc = argc;
  82.                 _argv = argv;
  83.         }
  84. }

  85. void scim_frontend_module_run(void) {
  86.         if (!_scim_frontend.null()) {
  87.                 SCIM_DEBUG_FRONTEND(1) << "Starting Socket FrontEnd module...\n";
  88.                 _scim_frontend->init(_argc, _argv);
  89.                 _scim_frontend->run();
  90.         }
  91. }
  92. }

复制代码
 楼主| 发表于 2008-6-22 15:41:50 | 显示全部楼层
运行上面代码得到了
  1. scim -f libMyFrontEnd.so
  2. Smart Common Input Method 1.4.7
  3. Launching a SCIM process with libMyFrontEnd.so...
  4. Loading socket Config module ...
  5. Creating backend ...
  6. Loading libMyFrontEnd.so FrontEnd module ...
  7. Starting SCIM ...
  8. Default IM:Name:智能拼音 Language:zh_CN Authors:版权所有 (C) 2002, 2003 James Su <suzhe@tsinghua.org.cn> Credits: Locales:zh_CN.UTF-8,zh_CN.GB18030,zh_CN.GBK,zh_CN.GB2312,zh_CN
  9. Get IM success
  10. Process key error!: 没有那个文件或目录
  11. SCIM has exited successfully.
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-6-22 17:18:33 | 显示全部楼层
好了 弄出来了。。。。

  1. #define Uses_SCIM_LOOKUP_TABLE
  2. #define Uses_SCIM_FRONTEND

  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <scim.h>
  7. #include <locale.h>
  8. #include <sstream>
  9. #include <iostream>
  10. using namespace scim;
  11. class MyFrontEnd: public FrontEndBase {
  12. private:
  13.         int id;
  14. private:
  15.         WideString factory_info(String uuid) {
  16.                 std::basic_stringstream<wchar_t> stream;
  17.                 stream << "Name:" << get_factory_name(uuid).c_str() << ' ';
  18.                 stream << "Language:" << get_factory_language(uuid).c_str() << ' ';
  19.                 stream << "Authors:" << get_factory_authors(uuid).c_str() << ' ';
  20.                 stream << "Credits:" << get_factory_credits(uuid).c_str() << ' ';
  21.                 //stream << "Help:"<<get_factory_help(uuid).c_str()<<' ';
  22.                 //stream << "Icon_file:"<<get_factory_icon_file(uuid).c_str()<<' ';
  23.                 stream << "Locales:" << get_factory_locales(uuid).c_str() << ' ';
  24.                 //stream<<'\n';
  25.                 return stream.str();
  26.         }

  27.         WideString instance_info(int id) {
  28.                 std::basic_stringstream<wchar_t> stream;
  29.                 stream << "Name:" << get_instance_name(id).c_str() << ' ';
  30.                 stream << "Encoding:" << get_instance_encoding(id).c_str() << ' ';
  31.                 stream << "Authors:" << get_instance_authors(id).c_str() << ' ';
  32.                 stream << "Credits:" << get_instance_credits(id).c_str() << ' ';
  33.                 //stream << "Help:"<<get_instance_help(id).c_str()<<' ';
  34.                 //stream << "Icon_file:"<<get_instance_icon_file(id).c_str()<<' ';
  35.                 stream << "UUID:" << get_instance_uuid(id).c_str() << ' ';
  36.                 //stream<<'\n';
  37.                 return stream.str();
  38.         }

  39.         void dump_factory() {
  40.                 std::vector<String> uuids;
  41.                 String language;
  42.                 uint32 ret = get_factory_list_for_language(uuids, language);
  43.                 if (ret > 0) {
  44.                         std::vector<String>::const_iterator it;
  45.                         for (it = uuids.begin(); it != uuids.end();++it) {
  46.                                 printf("%S\n",factory_info(*it).c_str() );
  47.                         }
  48.                 }
  49.         }
  50.         void test_key(const KeyEvent& key) {
  51.                 focus_in(id);
  52.                 if(process_key_event (id, key)) {
  53.                         printf("Process key success\n");
  54.                 } else {
  55.                         perror("Process key error!\n");
  56.                 }
  57.         }
  58. public:

  59.         MyFrontEnd(const BackEndPointer &backend) :
  60.         FrontEndBase(backend) {
  61.                 setlocale(LC_ALL, "zh_CN.UTF-8");
  62.         }

  63.         void init(int argc, char **argv) {
  64.                 String uuid;
  65.                 String language="zh_CN";
  66.                 String encoding="UTF-8";
  67.                 uuid=get_default_factory (language, encoding);

  68.                 id= new_instance (uuid, encoding);
  69.                 printf("Default IM:%S\n",instance_info(id).c_str());

  70.         }
  71.         void run () {

  72.                 if(id==-1) {
  73.                         perror("new_instance Error\n");
  74.                 }
  75.                 else {
  76.                         printf("Get IM success\n");
  77.                         test_key(KeyEvent("a"));
  78.                         test_key(KeyEvent("n"));
  79.                         test_key(KeyEvent("n"));
  80.                         test_key(KeyEvent("n"));
  81.                         test_key(KeyEvent("n"));
  82.                 }
  83.         }

  84.         void update_aux_string(int id, const WideString & str, const AttributeList & attrs)
  85.         {
  86.                 printf("In update_aux_string -> Get string %S\n",str.c_str());

  87.         }
  88.         void update_preedit_string (int id, const WideString & str, const AttributeList & attrs)
  89.         {
  90.                 printf("In update_preedit_string-> Get string %S\n",str.c_str());
  91.         }
  92.         void
  93.         update_lookup_table   (int id, const LookupTable & table)
  94.         {
  95.                 printf("In update_lookup_table\n");
  96.                 size_t n=table.number_of_candidates();
  97.                 for(size_t i=0;i<n;++i){
  98.                         printf("%d->%S ",i,table.get_candidate(i).c_str());
  99.                         if(i>0 && i%10 ==0 ){
  100.                                 printf("\n");
  101.                         }
  102.                 }

  103.         }

  104. };

  105.                         //Module Interface
  106. static Pointer<MyFrontEnd> _scim_frontend(0);
  107. static int _argc;
  108. static char **_argv;

  109. extern "C" {
  110. void scim_module_init(void) {
  111.         SCIM_DEBUG_FRONTEND(1) << "Initializing Socket FrontEnd module...\n";
  112. }

  113. void scim_module_exit(void) {
  114.         SCIM_DEBUG_FRONTEND(1) << "Exiting Socket FrontEnd module...\n";
  115.         _scim_frontend.reset();
  116. }

  117. void scim_frontend_module_init(const BackEndPointer &backend,
  118.                 const ConfigPointer &config, int argc, char **argv) {
  119.         if (_scim_frontend.null()) {
  120.                 SCIM_DEBUG_FRONTEND(1) << "Initializing Socket FrontEnd module (more)...\n";
  121.                 _scim_frontend = new MyFrontEnd(backend);
  122.                 _argc = argc;
  123.                 _argv = argv;
  124.         }
  125. }

  126. void scim_frontend_module_run(void) {
  127.         if (!_scim_frontend.null()) {
  128.                 SCIM_DEBUG_FRONTEND(1) << "Starting Socket FrontEnd module...\n";
  129.                 _scim_frontend->init(_argc, _argv);
  130.                 _scim_frontend->run();
  131.         }
  132. }
  133. }


复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-6-22 17:36:07 | 显示全部楼层
输出


  1. [zarra@zarra-desktop Debug]$ scim -f libMyFrontEnd.so
  2. Smart Common Input Method 1.4.7

  3. Launching a SCIM process with libMyFrontEnd.so...
  4. Loading socket Config module ...
  5. Creating backend ...
  6. Loading libMyFrontEnd.so FrontEnd module ...
  7. Starting SCIM ...
  8. Default IM:Name:智能拼音 Encoding:UTF-8 Authors:版权所有 (C) 2002, 2003 James Su <suzhe@tsinghua.org.cn> Credits: UUID:05235cfc-43ce-490c-b1b1-c5a2185276ae
  9. Get IM success
  10. In update_preedit_string-> Get string 阿
  11. In update_aux_string -> Get string a
  12. In update_lookup_table
  13. 0->阿 1->啊 2->呵 3->腌 4->吖 5->嗄 6->锕 7->錒 Process key success
  14. In update_preedit_string-> Get string 阿
  15. In update_aux_string -> Get string a
  16. In update_lookup_table
  17. 0->阿 1->啊 2->呵 3->腌 4->吖 5->嗄 6->锕 7->錒 In update_preedit_string-> Get string 安
  18. In update_aux_string -> Get string an
  19. In update_lookup_table
  20. 0->安 1->案 2->暗 3->按 4->岸 5->俺 6->黯 7->庵 8->鞍 9->㐀 Process key success
  21. In update_preedit_string-> Get string 安
  22. In update_aux_string -> Get string an
  23. In update_lookup_table
  24. 0->安 1->案 2->暗 3->按 4->岸 5->俺 6->黯 7->庵 8->鞍 9->㐀 In update_preedit_string-> Get string 按钮
  25. In update_aux_string -> Get string n
  26. In update_lookup_table
  27. 0->按钮 1->安娜 2->安宁 3->安南 4->按捺 5->安妮 6->按年 7->安能 8->安捺 9->㐀 Process key success
  28. In update_preedit_string-> Get string 按钮
  29. In update_aux_string -> Get string n
  30. In update_lookup_table
  31. 0->按钮 1->安娜 2->安宁 3->安南 4->按捺 5->安妮 6->按年 7->安能 8->安捺 9->㐀 In update_preedit_string-> Get string 按钮你
  32. In update_aux_string -> Get string n
  33. In update_lookup_table
  34. 0->按钮你 1->按钮 2->安娜 3->安宁 4->安南 5->按捺 6->安妮 7->按年 8->安能 9->㐀 Process key success
  35. In update_preedit_string-> Get string 按钮你
  36. In update_aux_string -> Get string n
  37. In update_lookup_table
  38. 0->按钮你 1->按钮 2->安娜 3->安宁 4->安南 5->按捺 6->安妮 7->按年 8->安能 9->㐀 In update_preedit_string-> Get string 按钮那你
  39. In update_aux_string -> Get string n
  40. In update_lookup_table
  41. 0->按钮那你 1->按钮 2->安娜 3->安宁 4->安南 5->按捺 6->安妮 7->按年 8->安能 9->㐀 Process key success
  42. SCIM has exited successfully.


复制代码
回复 支持 反对

使用道具 举报

发表于 2009-3-2 15:49:20 | 显示全部楼层
这位大哥,你怎么做的啊,教教我。。。我也是象你那么一步步来的。我怎么不行啊~!
回复 支持 反对

使用道具 举报

发表于 2009-3-2 15:55:04 | 显示全部楼层
说我 "Failde to load lib...so FrontEnd module"
回复 支持 反对

使用道具 举报

发表于 2009-3-2 16:00:44 | 显示全部楼层
是不是我有什么与s c i m相关的东西没安的事啊~!?????
回复 支持 反对

使用道具 举报

发表于 2009-3-2 16:04:47 | 显示全部楼层
我的SCIM是1.4.4的而你的是1.4.7的,和这个没有关系吧~!??????
回复 支持 反对

使用道具 举报

发表于 2009-3-2 16:06:32 | 显示全部楼层
哥们你在哪啊?快出来help me!~!~!~!~!
回复 支持 反对

使用道具 举报

发表于 2009-3-2 16:10:32 | 显示全部楼层
你的那个lib*.so是怎么生成的啊.是这样吗???

#g++ -shared -o lib*.so lib*.so
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表