Back to index

im-sdk  12.3.91
SunIMConf.c
Go to the documentation of this file.
00001 /*
00002   Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004   Permission is hereby granted, free of charge, to any person obtaining a
00005   copy of this software and associated documentation files (the
00006   "Software"), to deal in the Software without restriction, including
00007   without limitation the rights to use, copy, modify, merge, publish,
00008   distribute, sublicense, and/or sell copies of the Software, and to
00009   permit persons to whom the Software is furnished to do so, subject to
00010   the following conditions: The above copyright notice and this
00011   permission notice shall be included in all copies or substantial
00012   portions of the Software.
00013 
00014 
00015   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018   IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019   FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020   CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021   THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022   ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025   Except as contained in this notice, the names of The Open Group and/or
00026   Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027   promote the sale, use or other dealings in this Software without prior
00028   written authorization from The Open Group and/or Sun Microsystems,
00029   Inc., as applicable.
00030 
00031 
00032   X Window System is a trademark of The Open Group
00033 
00034   OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035   logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036   Group. All other trademarks and registered trademarks mentioned herein
00037   are the property of their respective owners. No right, title or
00038   interest in or to any trademark, service mark, logo or trade name of
00039   Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 #ifdef WIN32
00043 #include <windows.h>
00044 #endif
00045 
00046 #include <stddef.h>
00047 #include <stdio.h>
00048 #include <stdlib.h>
00049 #include <string.h>
00050 
00051 #ifdef sun
00052 #include <sys/types.h>
00053 #include <sys/stat.h>
00054 #endif
00055 
00056 #ifndef       WIN32
00057 #include <unistd.h>
00058 #include <dlfcn.h>
00059 #endif
00060 
00061 #include "SunIM.h"
00062 
00063 #ifdef ENABLE_EIMIL
00064 #define IFPATH_ENV "CURRENT_IFPATH"
00065 #endif
00066 
00067 Public iml_methods_t *_iml_dup_iml_methods();
00068 Public iml_methods_wrapper_t *_iml_dup_iml_methods_wrapper();
00069 
00070 #ifdef sun
00071 int is_cm_available(char *locale);
00072 int is_engine_available(char *locale, char *engine);
00073 #endif
00074 
00075 Private void *open_le_module(char *);
00076 Public void close_le_module(void *);
00077 Private Bool get_ifinfo(const char*, iml_if_t *, void *);
00078 
00079 #ifdef _POSIX_PATH_MAX
00080 #define PATHNAME_MAX _POSIX_PATH_MAX
00081 #else
00082 #define PATHNAME_MAX 1024
00083 #endif
00084 
00085 #define       PROCNAME      "if_GetIfInfo"
00086 #define       HOTKEY_PROCNAME      "if_GetHotKeyInfo"
00087 
00088 #define       OPENWINHOME          "/usr/openwin"
00089 
00090 #ifdef USE_XSUNIM_ADAPTER
00091 #define       XSUNIMADAPTER_MODULE "xsunimadapter"
00092 #define       XSUNIMADAPTER_METHOD "xsunim_methods"
00093 static void *dl_module_xsunimadapter = 0;
00094 #endif
00095 
00096 #ifdef USE_SUNIM_ADAPTER
00097 #ifndef       WIN32
00098 static void *dl_module_sunimadapter = 0;
00099 #define       SUNIMADAPTER_MODULE  "sunimadapter"
00100 #define       SUNIMADAPTER_METHOD  "sunim_methods"
00101 #endif
00102 #endif /* USE_SUNIM_ADAPTER */
00103 
00104 extern if_methods_t sunim_default_methods;
00105 
00106 Private void *
00107 open_le_module(
00108               char *if_path
00109               )
00110 {
00111 #ifndef       WIN32
00112   void *pr;
00113   char *perr;
00114   if (access(if_path, F_OK) == -1) {
00115     return (void *) 0;
00116   }
00117   pr = dlopen(if_path, RTLD_LAZY);
00118   if (!pr) {
00119     perr = dlerror();
00120     if (perr) fprintf(stderr, "dlopen() failed:%s\n", perr);
00121   }
00122   return pr;
00123 #else
00124   return (void *) LoadLibrary(if_path);
00125 #endif
00126 }
00127 
00128 Public void
00129 close_le_module(void *module)
00130 {
00131 #ifndef       WIN32
00132   if(module && dlclose((void*)module) != 0){
00133     fprintf(stderr, dlerror());
00134     fprintf(stderr, "\n");
00135   }
00136 #else
00137   FreeLibrary((HINSTANCE) module);
00138 #endif
00139 }
00140 
00141 Private Bool
00142 get_ifinfo(
00143           const char *iiimf_home,
00144           iml_if_t * If,
00145           void *module
00146           )
00147 {
00148   void *(*get_le_info) ();
00149   IMHotkeyManagerStruct *(*get_hotkey_info) (IMLEName*);
00150   int count;
00151   int if_version_idx, if_lename_idx, if_locales_idx, if_objects_idx, if_keyrelease_idx;
00152   int if_methods_idx, if_thread_lock_idx, if_imeinfo_idx;
00153 #ifdef USE_SUNIM_ADAPTER
00154   int if_locale_dependency_idx;
00155 #endif
00156 #ifdef ENABLE_EIMIL
00157   int if_EIMIL_handle_idx;
00158 #endif
00159   IMArg p[11] ; /* XXX FIXME Magic number */
00160     
00161 #ifndef       WIN32
00162   get_le_info = (void *(*) ()) dlsym(module, PROCNAME);
00163 #else
00164   get_le_info = (void *(*) ()) GetProcAddress((HINSTANCE) module, TEXT(PROCNAME));
00165 #endif
00166     
00167   if (get_le_info == NULL) {
00168     return False;
00169   }
00170   count = 0;
00171   IMSetArg(p[count], IF_VERSION, NULL); if_version_idx = count++;
00172   IMSetArg(p[count], IF_LE_NAME, NULL); if_lename_idx = count++;
00173   IMSetArg(p[count], IF_SUPPORTED_LOCALES, NULL); if_locales_idx = count++;
00174   IMSetArg(p[count], IF_SUPPORTED_OBJECTS, NULL); if_objects_idx = count++;
00175   IMSetArg(p[count], IF_METHOD_TABLE, NULL); if_methods_idx = count++;
00176   IMSetArg(p[count], IF_NEED_THREAD_LOCK, NULL); if_thread_lock_idx = count++;
00177 #ifdef USE_SUNIM_ADAPTER
00178   IMSetArg(p[count], IF_HAVE_LOCALE_DEPENDENCY, NULL);
00179   if_locale_dependency_idx = count++;
00180 #endif
00181 #ifdef ENABLE_EIMIL
00182   IMSetArg(p[count], IF_EIMIL_HANDLE, EIMIL_VOID_HANDLE);
00183   if_EIMIL_handle_idx = count++;
00184   setenv(IFPATH_ENV, iiimf_home, 1);
00185 #endif
00186   IMSetArg(p[count], IF_SUPPORTED_KEYRELEASE, NULL); if_keyrelease_idx = count++;
00187   IMSetArg(p[count], IF_SUPPORTED_IMEINFO, NULL); if_imeinfo_idx = count++;
00188 
00189   (*get_le_info) (p, count);
00190     
00191   If->if_version = (char *) p[if_version_idx].value;
00192   If->ifm = (if_methods_t *) p[if_methods_idx].value;
00193   If->lename = (IMLEName *) p[if_lename_idx].value;
00194   If->locale_list = (IMLocale *) p[if_locales_idx].value;
00195   If->object_list = (IMObjectDescriptorStruct *) p[if_objects_idx].value;
00196   If->need_thread_lock = (Bool) p[if_thread_lock_idx].value;
00197   If->need_keyrelease = (Bool) p[if_keyrelease_idx].value;
00198   If->imeinfo_list = (IMEInfo *) p[if_imeinfo_idx].value;
00199 
00200   /* hotkey infomation */
00201   get_hotkey_info = (void *(*) ()) dlsym(module, HOTKEY_PROCNAME);
00202   if (get_hotkey_info) {
00203     If->hkm = (*get_hotkey_info)(If->lename);
00204   } else
00205     If->hkm = NULL;
00206   
00207 #ifdef USE_SUNIM_ADAPTER
00208 #ifndef       WIN32
00209   If->need_adapter = (Bool) p[if_locale_dependency_idx].value;
00210   if (If->need_adapter == True) {
00211     If->need_thread_lock = True;
00212   }
00213 #else
00214   If->need_adapter = False;
00215 #endif
00216 #endif /* USE_SUNIM_ADAPTER */
00217 
00218 #ifdef ENABLE_EIMIL
00219   If->eh = (EIMIL_handle) p[if_EIMIL_handle_idx].value;
00220 #endif
00221 
00222   if (!If->ifm || !If->lename || !If->locale_list) {
00223     return False;
00224   }
00225   return True;
00226 }
00227 
00228 
00229 Public Bool
00230 if_configure(
00231             iml_if_t * If,
00232             const char *iiimf_home,
00233             const char *if_name,
00234             const char *locale_,
00235             Bool call_openif
00236             )
00237 {
00238   const char *locale = locale_;
00239   char if_path[PATHNAME_MAX];
00240     
00241   void *if_so = (void *) NULL;
00242   If->xsunim = False;
00243 
00244   /* If OpenDesktop() fails, use this default engine */
00245   if(!strcmp(if_name, "sunim_default")){
00246     If->ifm = &sunim_default_methods;
00247     goto sunim_default;
00248   }
00249 
00250 #ifndef       WIN32
00251   sprintf(if_path, "%s/%s.so", iiimf_home, if_name);
00252     
00253   if_so = open_le_module(if_path);
00254     
00255   if (if_so) {
00256     if (get_ifinfo(iiimf_home, If, if_so) == False) {
00257       fprintf(stderr, "if_configure: get_method_table() failed\n");
00258       close_le_module(if_so);
00259       if_so = NULL;
00260     } else {
00261 #ifdef USE_SUNIM_ADAPTER
00262       if (If->need_adapter == True && call_openif == True) {
00263        if (dl_module_sunimadapter == 0) {
00264          sprintf(if_path, "%s/%s.so", iiimf_home, SUNIMADAPTER_MODULE);
00265          dl_module_sunimadapter = open_le_module(if_path);
00266        }
00267        if (dl_module_sunimadapter) {
00268          If->ifm = (if_methods_t *) dlsym(dl_module_sunimadapter, SUNIMADAPTER_METHOD);
00269        }
00270       }
00271 #endif /* SUNIM_ADAPTER */
00272     }
00273   }
00274 
00275 #ifdef USE_XSUNIM_ADAPTER
00276   if (if_so == NULL) {
00277     sprintf(if_path, "%s/lib/locale/%s/xim/%s.so", OPENWINHOME, locale, if_name);
00278     if (!access(if_path, F_OK)) {
00279       If->xsunim = True;
00280     } else {
00281       sprintf(if_path, "%s/lib/locale/common/xim/%s.so", OPENWINHOME, if_name);
00282       if (!access(if_path, F_OK)) {
00283        If->xsunim = True;
00284       }
00285     }
00286     if (If->xsunim == True) {
00287       if (dl_module_xsunimadapter == 0) {
00288        sprintf(if_path, "%s/%s.so", iiimf_home, XSUNIMADAPTER_MODULE);
00289        dl_module_xsunimadapter = open_le_module(if_path);
00290       }
00291       if (dl_module_xsunimadapter) {
00292        If->ifm = (if_methods_t *) dlsym(dl_module_xsunimadapter, XSUNIMADAPTER_METHOD);
00293        If->lename = (IMLEName *) 0;
00294        If->locale_list = (IMLocale *) 0;
00295        If->object_list = (IMObjectDescriptorStruct *) 0;
00296        If->hkm = (IMHotkeyManagerStruct *) 0;
00297        If->need_thread_lock = True;
00298        If->need_keyrelease = False;
00299       }
00300     }
00301   }
00302 #endif
00303     
00304   If->dl_module = if_so;
00305 #else /* WIN32 */
00306   sprintf(if_path, "%s\\%s.dll", iiimf_home, if_name);
00307   if_so = open_le_module(if_path);
00308   if (if_so) {
00309     get_ifinfo(iiimf_home, If, if_so);
00310     if (If->ifm == NULL) {
00311 #ifdef DEBUG
00312       printf("if_configure: get_method_table() failed\n");
00313 #endif
00314       close_le_module(if_so);
00315       if_so = NULL;
00316     }
00317   } else {
00318     printf("open_le_module failed\n");
00319   }
00320 #endif /* WIN32 */
00321     
00322   if (!If->ifm) {
00323     fprintf(stderr, "Fatal error in dynamic LE i/f loader -  A library is missing in your system. Please call your system administrator.\n");
00324     If->ifm = &sunim_default_methods;
00325   }
00326 
00327  sunim_default:
00328   If->locale = strdup(locale);
00329   If->if_name = strdup(if_name);
00330   If->ifpath_name = strdup(iiimf_home);
00331   If->m = _iml_dup_iml_methods();
00332   If->m_wrapper = _iml_dup_iml_methods_wrapper();
00333 #ifdef DEBUG
00334   printf("try to call if_OpenIF() with locale=%s if_name=%s if_path=%s\n",
00335         If->locale, If->if_name, if_path);
00336 #endif
00337 
00338   if (call_openif == True) {
00339     if (If->ifm->if_OpenIF(If) == False) {
00340       If->ifm = &sunim_default_methods;
00341       If->ifm->if_OpenIF(If);
00342     }
00343   }
00344   return True;
00345 }
00346 
00347 #ifdef  sun
00348 is_cm_available(
00349               char *locale
00350               )
00351 {
00352   struct stat buf;
00353   char path[256];
00354   sprintf(path, "/usr/openwin/lib/locale/%s/xim/cm.so", locale);
00355   if (access(path, F_OK) == -1) {
00356     sprintf(path, "/usr/openwin/lib/locale/common/xim/cm.so");
00357     if (access(path, F_OK) == -1) {
00358       return 0;
00359     }
00360   }
00361   sprintf(path, "/usr/lib/mle/%s/mle.so", locale);
00362   if (access(path, F_OK) == -1) {
00363     return 0;
00364   }
00365   sprintf(path, "/usr/lib/locale/%s/", locale);
00366   if ((stat(path, &buf) < 0) || !((buf.st_mode & S_IFMT) == S_IFDIR)) {
00367     return 0;
00368   }
00369   return 1;
00370 }
00371 
00372 is_engine_available(
00373                   char *locale,
00374                   char *engine
00375                   )
00376 {
00377   struct stat buf;
00378   char path[256];
00379   sprintf(path, "/usr/openwin/lib/locale/%s/xim/%s.so", locale,engine);
00380   if (access(path, F_OK) == -1) {
00381     return 0;
00382   }
00383   sprintf(path, "/usr/lib/locale/%s/", locale);
00384   if ((stat(path, &buf) < 0) || !((buf.st_mode & S_IFMT) == S_IFDIR)) {
00385     return 0;
00386   }
00387   return 1;
00388 }
00389 #endif